summaryrefslogtreecommitdiff
path: root/m4/symtab.c
blob: b04b55e70a8270af277a2880da65682a4987eb96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
/* GNU m4 -- A simple macro processor
   Copyright (C) 1989-1994, 2001, 2005-2008, 2010, 2013-2014, 2017 Free
   Software Foundation, Inc.

   This file is part of GNU M4.

   GNU M4 is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   GNU M4 is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>

#include "m4private.h"

/* Define this to see runtime debug info.  Implied by DEBUG.  */
/*#define DEBUG_SYM */

/* This file handles all the low level work around the symbol table.  The
   symbol table is an abstract hash table type implemented in hash.c.  Each
   symbol is represented by `struct m4_symbol', which is stored in the hash
   table keyed by the symbol name.  As a special case, to facilitate the
   "pushdef" and "popdef" builtins, the value stored against each key is a
   stack of `m4_symbol_value'. All the value entries for a symbol name are
   simply ordered on the stack by age.  The most recently pushed definition
   will then always be the first found.

   Also worthy of mention is the way traced symbols are managed: the
   trace bit is associated with a particular symbol name.  If a symbol
   is undefined and then redefined, it does not lose its trace bit.
   This is achieved by not removing traced symbol names from the
   symbol table, even if their value stack is empty.  That way, when
   the name is given a new value, it is pushed onto the empty stack,
   and the trace bit attached to the name was never lost.  There is a
   small amount of fluff in these functions to make sure that such
   symbols (with empty value stacks) are invisible to the users of
   this module.  */

#define M4_SYMTAB_DEFAULT_SIZE          2047

struct m4_symbol_table {
  m4_hash *table;
};

static m4_symbol *symtab_fetch          (m4_symbol_table*, const char *,
                                         size_t);
static void       symbol_popval         (m4_symbol *);
static void *     symbol_destroy_CB     (m4_symbol_table *, const char *,
                                         size_t, m4_symbol *, void *);
static void *     arg_destroy_CB        (m4_hash *, const void *, void *,
                                         void *);
static void *     arg_copy_CB           (m4_hash *, const void *, void *,
                                         m4_hash *);


/* -- SYMBOL TABLE MANAGEMENT --

   These functions are used to manage a symbol table as a whole.  */

m4_symbol_table *
m4_symtab_create (size_t size)
{
  m4_symbol_table *symtab = (m4_symbol_table *) xmalloc (sizeof *symtab);

  symtab->table = m4_hash_new (size ? size : M4_SYMTAB_DEFAULT_SIZE,
                               m4_hash_string_hash, m4_hash_string_cmp);
  return symtab;
}

void
m4_symtab_delete (m4_symbol_table *symtab)
{
  assert (symtab);
  assert (symtab->table);

  m4_symtab_apply (symtab, true, symbol_destroy_CB, NULL);
  m4_hash_delete (symtab->table);
  free (symtab);
}

/* For every symbol in SYMTAB, execute the callback FUNC with the name
   and value of the symbol being visited, and the opaque parameter
   USERDATA.  Skip undefined symbols that are placeholders for
   traceon, unless INCLUDE_TRACE is true.  If FUNC returns non-NULL,
   abort the iteration and return the same result; otherwise return
   NULL when iteration completes.  */
void *
m4_symtab_apply (m4_symbol_table *symtab, bool include_trace,
                 m4_symtab_apply_func *func, void *userdata)
{
  m4_hash_iterator *place  = NULL;
  void *            result = NULL;

  assert (symtab);
  assert (symtab->table);
  assert (func);

  while ((place = m4_get_hash_iterator_next (symtab->table, place)))
    {
      m4_symbol *symbol = m4_get_hash_iterator_value (place);
      if (symbol->value || include_trace)
        {
          const m4_string *key
            = (const m4_string *) m4_get_hash_iterator_key (place);
          result = func (symtab, key->str, key->len, symbol, userdata);
        }
      if (result != NULL)
        {
          m4_free_hash_iterator (symtab->table, place);
          break;
        }
    }

  return result;
}

/* Ensure that NAME of length LEN exists in the table, creating an
   entry if needed.  */
static m4_symbol *
symtab_fetch (m4_symbol_table *symtab, const char *name, size_t len)
{
  m4_symbol **psymbol;
  m4_symbol *symbol;
  m4_string key;

  assert (symtab);
  assert (name);

  /* Safe to cast away const, since m4_hash_lookup doesn't modify
     key.  */
  key.str = (char *) name;
  key.len = len;
  psymbol = (m4_symbol **) m4_hash_lookup (symtab->table, &key);
  if (psymbol)
    {
      symbol = *psymbol;
    }
  else
    {
      /* Use xmemdup0 rather than memdup so that debugging the symbol
         table is easier.  */
      m4_string *new_key = (m4_string *) xmalloc (sizeof *new_key);
      new_key->str = xmemdup0 (name, len);
      new_key->len = len;
      symbol = (m4_symbol *) xzalloc (sizeof *symbol);
      m4_hash_insert (symtab->table, new_key, symbol);
    }

  return symbol;
}

/* Remove every symbol that references the given module from
   the symbol table.  */
void
m4__symtab_remove_module_references (m4_symbol_table *symtab,
                                     m4_module *module)
{
  m4_hash_iterator *place = 0;

  assert (module);

   /* Traverse each symbol name in the hash table.  */
  while ((place = m4_get_hash_iterator_next (symtab->table, place)))
    {
      m4_symbol *symbol = (m4_symbol *) m4_get_hash_iterator_value (place);
      m4_symbol_value *data = m4_get_symbol_value (symbol);

      /* For symbols that have token data... */
      if (data)
        {
          /* Purge any shadowed references.  */
          while (VALUE_NEXT (data))
            {
              m4_symbol_value *next = VALUE_NEXT (data);

              if (VALUE_MODULE (next) == module)
                {
                  VALUE_NEXT (data) = VALUE_NEXT (next);

                  assert (next->type != M4_SYMBOL_PLACEHOLDER);
                  m4_symbol_value_delete (next);
                }
              else
                data = next;
            }

          /* Purge the live reference if necessary.  */
          if (SYMBOL_MODULE (symbol) == module)
            {
              const m4_string *key
                = (const m4_string *) m4_get_hash_iterator_key (place);
              m4_symbol_popdef (symtab, key->str, key->len);
            }
        }
    }
}


/* This callback is used exclusively by m4_symtab_delete(), to cleanup
   the memory used by the symbol table.  As such, the trace bit is reset
   on every symbol so that m4_symbol_popdef() doesn't try to preserve
   the table entry.  */
static void *
symbol_destroy_CB (m4_symbol_table *symtab, const char *name, size_t len,
                   m4_symbol *symbol, void *ignored M4_GNUC_UNUSED)
{
  m4_string key;
  key.str = xmemdup0 (name, len);
  key.len = len;

  symbol->traced = false;

  while (m4_hash_lookup (symtab->table, &key))
    m4_symbol_popdef (symtab, key.str, key.len);

  free (key.str);

  return NULL;
}



/* -- SYMBOL MANAGEMENT --

   The following functions manipulate individual symbols within
   an existing table.  */

/* Return the symbol associated to NAME of length LEN, or else
   NULL.  */
m4_symbol *
m4_symbol_lookup (m4_symbol_table *symtab, const char *name, size_t len)
{
  m4_string key;
  m4_symbol **psymbol;

  /* Safe to cast away const, since m4_hash_lookup doesn't modify
     key.  */
  key.str = (char *) name;
  key.len = len;
  psymbol = (m4_symbol **) m4_hash_lookup (symtab->table, &key);

  /* If just searching, return status of search -- if only an empty
     struct is returned, that is treated as a failed lookup.  */
  return (psymbol && m4_get_symbol_value (*psymbol)) ? *psymbol : NULL;
}


/* Insert NAME of length LEN into the symbol table.  If there is
   already a symbol associated with NAME, push the new VALUE on top of
   the value stack for this symbol.  Otherwise create a new
   association.  */
m4_symbol *
m4_symbol_pushdef (m4_symbol_table *symtab, const char *name, size_t len,
                   m4_symbol_value *value)
{
  m4_symbol *symbol;

  assert (symtab);
  assert (name);
  assert (value);

  symbol                = symtab_fetch (symtab, name, len);
  VALUE_NEXT (value)    = m4_get_symbol_value (symbol);
  symbol->value         = value;

  assert (m4_get_symbol_value (symbol));

  return symbol;
}

/* Return the symbol associated with NAME of length LEN in the symbol
   table, creating a new symbol if necessary.  In either case set the
   symbol's VALUE.  */
m4_symbol *
m4_symbol_define (m4_symbol_table *symtab, const char *name, size_t len,
                  m4_symbol_value *value)
{
  m4_symbol *symbol;

  assert (symtab);
  assert (name);
  assert (value);

  symbol = symtab_fetch (symtab, name, len);
  if (m4_get_symbol_value (symbol))
    symbol_popval (symbol);

  VALUE_NEXT (value) = m4_get_symbol_value (symbol);
  symbol->value      = value;

  assert (m4_get_symbol_value (symbol));

  return symbol;
}

/* Pop the topmost value stack entry from the symbol associated with
   NAME of length LEN, deleting it from the table entirely if that was
   the last remaining value in the stack.  */
void
m4_symbol_popdef (m4_symbol_table *symtab, const char *name, size_t len)
{
  m4_string key;
  m4_symbol **psymbol;

  /* Safe to cast away const, since m4_hash_lookup doesn't modify
     key.  */
  key.str = (char *) name;
  key.len = len;
  psymbol = (m4_symbol **) m4_hash_lookup (symtab->table, &key);

  assert (psymbol);
  assert (*psymbol);

  symbol_popval (*psymbol);

  /* Only remove the hash table entry if the last value in the
     symbol value stack was successfully removed.  */
  if (!m4_get_symbol_value (*psymbol) && !m4_get_symbol_traced (*psymbol))
    {
      m4_string *old_key;
      DELETE (*psymbol);
      old_key = (m4_string *) m4_hash_remove (symtab->table, &key);
      free (old_key->str);
      free (old_key);
    }
}

/* Remove the top-most value from SYMBOL's stack.  */
static void
symbol_popval (m4_symbol *symbol)
{
  m4_symbol_value  *stale;

  assert (symbol);

  stale = m4_get_symbol_value (symbol);

  if (stale)
    {
      symbol->value = VALUE_NEXT (stale);
      m4_symbol_value_delete (stale);
    }
}

/* Create a new symbol value, with fields populated for default
   behavior.  */
m4_symbol_value *
m4_symbol_value_create (void)
{
  m4_symbol_value *value = (m4_symbol_value *) xzalloc (sizeof *value);
  VALUE_MAX_ARGS (value) = SIZE_MAX;
  return value;
}

/* Remove VALUE from the symbol table, and mark it as deleted.  If no
   expansions are pending, reclaim its resources.  */
void
m4_symbol_value_delete (m4_symbol_value *value)
{
  if (VALUE_PENDING (value) > 0)
    BIT_SET (VALUE_FLAGS (value), VALUE_DELETED_BIT);
  else
    {
      if (VALUE_ARG_SIGNATURE (value))
        {
          m4_hash_apply (VALUE_ARG_SIGNATURE (value), arg_destroy_CB, NULL);
          m4_hash_delete (VALUE_ARG_SIGNATURE (value));
        }
      switch (value->type)
        {
        case M4_SYMBOL_TEXT:
          DELETE (value->u.u_t.text);
          break;
        case M4_SYMBOL_PLACEHOLDER:
          DELETE (value->u.u_t.text);
          break;
        case M4_SYMBOL_VOID:
        case M4_SYMBOL_FUNC:
          break;
        default:
          assert (!"m4_symbol_value_delete");
          abort ();
        }
      free (value);
    }
}

/* Rename the entire stack of values associated with NAME and LEN1 to
   NEWNAME and LEN2.  */
m4_symbol *
m4_symbol_rename (m4_symbol_table *symtab, const char *name, size_t len1,
                  const char *newname, size_t len2)
{
  m4_symbol *symbol     = NULL;
  m4_symbol **psymbol;
  m4_string key;
  m4_string *pkey;

  assert (symtab);
  assert (name);
  assert (newname);

  /* Safe to cast away const, since m4_hash_lookup doesn't modify
     key.  */
  key.str = (char *) name;
  key.len = len1;
  /* Use a low level hash fetch, so we can save the symbol value when
     removing the symbol name from the symbol table.  */
  psymbol = (m4_symbol **) m4_hash_lookup (symtab->table, &key);

  if (psymbol)
    {
      symbol = *psymbol;

      /* Remove the old name from the symbol table.  */
      pkey = (m4_string *) m4_hash_remove (symtab->table, &key);
      assert (pkey && !m4_hash_lookup (symtab->table, &key));
      free (pkey->str);

      pkey->str = xmemdup0 (newname, len2);
      pkey->len = len2;
      m4_hash_insert (symtab->table, pkey, *psymbol);
    }
  /* else
       NAME does not name a symbol in symtab->table!  */

  return symbol;
}


/* Callback used by m4_symbol_popdef () to release the memory used
   by values in the arg_signature hash.  */
static void *
arg_destroy_CB (m4_hash *hash, const void *name, void *arg, void *ignored)
{
  struct m4_symbol_arg *token_arg = (struct m4_symbol_arg *) arg;

  assert (name);
  assert (hash);

  if (SYMBOL_ARG_DEFAULT (token_arg))
    DELETE (SYMBOL_ARG_DEFAULT (token_arg));
  free (token_arg);
  free (m4_hash_remove (hash, (const char *) name));

  return NULL;
}

/* Copy the symbol SRC into DEST.  Return true if builtin tokens were
   flattened.  */
bool
m4_symbol_value_copy (m4 *context, m4_symbol_value *dest, m4_symbol_value *src)
{
  m4_symbol_value *next;
  bool result = false;

  assert (dest);
  assert (src);

  switch (dest->type)
    {
    case M4_SYMBOL_TEXT:
      DELETE (dest->u.u_t.text);
      break;
    case M4_SYMBOL_PLACEHOLDER:
      DELETE (dest->u.u_t.text);
      break;
    case M4_SYMBOL_VOID:
    case M4_SYMBOL_FUNC:
      break;
    default:
      assert (!"m4_symbol_value_delete");
      abort ();
    }

  if (VALUE_ARG_SIGNATURE (dest))
    {
      m4_hash_apply (VALUE_ARG_SIGNATURE (dest), arg_destroy_CB, NULL);
      m4_hash_delete (VALUE_ARG_SIGNATURE (dest));
    }

  /* Copy the value contents over, being careful to preserve
     the next pointer.  */
  next = VALUE_NEXT (dest);
  memcpy (dest, src, sizeof (m4_symbol_value));
  VALUE_NEXT (dest) = next;

  /* Caller is supposed to free text token strings, so we have to
     copy the string not just its address in that case.  */
  switch (src->type)
    {
    case M4_SYMBOL_TEXT:
      {
        size_t len = m4_get_symbol_value_len (src);
        unsigned int age = m4_get_symbol_value_quote_age (src);
        m4_set_symbol_value_text (dest,
                                  xmemdup0 (m4_get_symbol_value_text (src),
                                            len), len, age);
      }
      break;
    case M4_SYMBOL_FUNC:
      m4__set_symbol_value_builtin (dest, src->u.builtin);
      break;
    case M4_SYMBOL_PLACEHOLDER:
      m4_set_symbol_value_placeholder (dest,
                                       xstrdup (m4_get_symbol_value_placeholder
                                                (src)));
      break;
    case M4_SYMBOL_COMP:
      {
        m4__symbol_chain *chain = src->u.u_c.chain;
        size_t len;
        char *str;
        const m4_string_pair *quotes;
        m4_obstack *obs = m4_arg_scratch (context);
        while (chain)
          {
            switch (chain->type)
              {
              case M4__CHAIN_STR:
                obstack_grow (obs, chain->u.u_s.str, chain->u.u_s.len);
                break;
              case M4__CHAIN_FUNC:
                result = true;
                break;
              case M4__CHAIN_ARGV:
                quotes = m4__quote_cache (M4SYNTAX, NULL, chain->quote_age,
                                          chain->u.u_a.quotes);
                if (chain->u.u_a.has_func && !chain->u.u_a.flatten)
                  result = true;
                m4__arg_print (context, obs, chain->u.u_a.argv,
                               chain->u.u_a.index, quotes, true, NULL, NULL,
                               NULL, false, false);
                break;
              default:
                assert (!"m4_symbol_value_copy");
                abort ();
              }
            chain = chain->next;
          }
        obstack_1grow (obs, '\0');
        len = obstack_object_size (obs);
        str = xcharalloc (len);
        memcpy (str, obstack_finish (obs), len);
        m4_set_symbol_value_text (dest, str, len - 1, 0);
      }
      break;
    default:
      assert (!"m4_symbol_value_copy");
      abort ();
    }
  if (VALUE_ARG_SIGNATURE (src))
    VALUE_ARG_SIGNATURE (dest) = m4_hash_dup (VALUE_ARG_SIGNATURE (src),
                                              arg_copy_CB);
  return result;
}

static void *
arg_copy_CB (m4_hash *src, const void *name, void *arg, m4_hash *dest)
{
  m4_hash_insert ((m4_hash *) dest, name, arg);
  return NULL;
}

/* Set the tracing status of the symbol NAME of length LEN to TRACED.
   This takes a name, rather than a symbol, since we hide macros that
   are traced but otherwise undefined from normal lookups, but still
   can affect their tracing status.  Return true iff the macro was
   previously traced.  */
bool
m4_set_symbol_name_traced (m4_symbol_table *symtab, const char *name,
                           size_t len, bool traced)
{
  m4_symbol *symbol;
  bool result;

  assert (symtab);
  assert (name);

  if (traced)
    symbol = symtab_fetch (symtab, name, len);
  else
    {
      m4_string key;
      m4_symbol **psymbol;

      /* Safe to cast away const, since m4_hash_lookup doesn't modify
         key.  */
      key.str = (char *) name;
      key.len = len;
      psymbol = (m4_symbol **) m4_hash_lookup (symtab->table, &key);
      if (!psymbol)
        return false;
      symbol = *psymbol;
    }

  result = symbol->traced;
  symbol->traced = traced;
  if (!traced && !m4_get_symbol_value (symbol))
    {
      /* Free an undefined entry once it is no longer traced.  */
      m4_string key;
      m4_string *old_key;
      assert (result);
      free (symbol);

      /* Safe to cast away const, since m4_hash_lookup doesn't modify
         key.  */
      key.str = (char *) name;
      key.len = len;
      old_key = (m4_string *) m4_hash_remove (symtab->table, &key);
      free (old_key->str);
      free (old_key);
    }

  return result;
}

/* Grow OBS with a text representation of VALUE.  If QUOTES, then use
   it to surround a text definition.  If FLATTEN, builtins are
   converted to empty quotes; if CHAINP, *CHAINP is updated with macro
   tokens; otherwise, builtins are represented by their name.  If
   MAXLEN, then truncate text definitions to *MAXLEN, and adjust by
   how many characters are printed.  If MODULE, then include which
   module defined a builtin.  Return true if the output was truncated.
   QUOTES and MODULE do not count against the truncation length.  */
bool
m4__symbol_value_print (m4 *context, m4_symbol_value *value, m4_obstack *obs,
                        const m4_string_pair *quotes, bool flatten,
                        m4__symbol_chain **chainp, size_t *maxlen, bool module)
{
  const char *text;
  m4__symbol_chain *chain;
  size_t len = maxlen ? *maxlen : SIZE_MAX;
  bool result = false;

  switch (value->type)
    {
    case M4_SYMBOL_TEXT:
      if (m4_shipout_string_trunc (obs, m4_get_symbol_value_text (value),
                                   m4_get_symbol_value_len (value), quotes,
                                   &len))
        result = true;
      break;
    case M4_SYMBOL_FUNC:
      m4__builtin_print (obs, value->u.builtin, flatten, chainp, quotes,
                         module);
      module = false;
      break;
    case M4_SYMBOL_PLACEHOLDER:
      if (flatten)
        {
          if (quotes)
            {
              obstack_grow (obs, quotes->str1, quotes->len1);
              obstack_grow (obs, quotes->str2, quotes->len2);
            }
          module = false;
        }
      else
        {
          text = m4_get_symbol_value_placeholder (value);
          obstack_1grow (obs, '<');
          obstack_1grow (obs, '<');
          obstack_grow (obs, text, strlen (text));
          obstack_1grow (obs, '>');
          obstack_1grow (obs, '>');
        }
      break;
    case M4_SYMBOL_COMP:
      chain = value->u.u_c.chain;
      assert (!module);
      if (quotes)
        obstack_grow (obs, quotes->str1, quotes->len1);
      while (chain && !result)
        {
          switch (chain->type)
            {
            case M4__CHAIN_STR:
              if (m4_shipout_string_trunc (obs, chain->u.u_s.str,
                                           chain->u.u_s.len, NULL, &len))
                result = true;
              break;
            case M4__CHAIN_FUNC:
              m4__builtin_print (obs, chain->u.builtin, flatten, chainp,
                                 quotes, module);
              break;
            case M4__CHAIN_ARGV:
              if (m4__arg_print (context, obs, chain->u.u_a.argv,
                                 chain->u.u_a.index,
                                 m4__quote_cache (M4SYNTAX, NULL,
                                                  chain->quote_age,
                                                  chain->u.u_a.quotes),
                                 chain->u.u_a.flatten, chainp, NULL, &len,
                                 false, module))
                result = true;
              break;
            default:
              assert (!"m4__symbol_value_print");
              abort ();
            }
            chain = chain->next;
          }
      if (quotes)
        obstack_grow (obs, quotes->str2, quotes->len2);
      break;
    default:
      assert (!"m4__symbol_value_print");
      abort ();
    }

  if (module && VALUE_MODULE (value))
    {
      obstack_1grow (obs, '{');
      text = m4_get_module_name (VALUE_MODULE (value));
      obstack_grow (obs, text, strlen (text));
      obstack_1grow (obs, '}');
    }
  if (maxlen)
    *maxlen = len;
  return result;
}

/* Grow OBS with a text representation of SYMBOL.  If QUOTES, then use
   it to surround each text definition.  If STACK, then append all
   pushdef'd values, rather than just the top.  If ARG_LENGTH is less
   than SIZE_MAX, then truncate text definitions to that length.  If
   MODULE, then include which module defined a builtin.  QUOTES and
   MODULE do not count toward truncation.  */
void
m4_symbol_print (m4 *context, m4_symbol *symbol, m4_obstack *obs,
                 const m4_string_pair *quotes, bool stack, size_t arg_length,
                 bool module)
{
  m4_symbol_value *value;
  size_t len = arg_length;

  assert (symbol);
  assert (obs);

  value = m4_get_symbol_value (symbol);
  m4__symbol_value_print (context, value, obs, quotes, false, NULL, &len,
                          module);
  if (stack)
    {
      value = VALUE_NEXT (value);
      while (value)
        {
          obstack_1grow (obs, ',');
          obstack_1grow (obs, ' ');
          len = arg_length;
          m4__symbol_value_print (context, value, obs, quotes, false, NULL,
                                  &len, module);
          value = VALUE_NEXT (value);
        }
    }
}


/* Define these functions at the end, so that calls in the file use the
   faster macro version from m4module.h.  */

/* Pop all values from the symbol associated with NAME.  */
#undef m4_symbol_delete
void
m4_symbol_delete (m4_symbol_table *symtab, const char *name, size_t len)
{
  while (m4_symbol_lookup (symtab, name, len))
    m4_symbol_popdef (symtab, name, len);
}

#undef m4_get_symbol_traced
bool
m4_get_symbol_traced (m4_symbol *symbol)
{
  assert (symbol);
  return symbol->traced;
}

#undef m4_symbol_value_flatten_args
bool
m4_symbol_value_flatten_args (m4_symbol_value *value)
{
  assert (value);
  return BIT_TEST (value->flags, VALUE_FLATTEN_ARGS_BIT);
}

#undef m4_get_symbol_value
m4_symbol_value *
m4_get_symbol_value (m4_symbol *symbol)
{
  assert (symbol);
  return symbol->value;
}

#undef m4_is_symbol_value_text
bool
m4_is_symbol_value_text (m4_symbol_value *value)
{
  assert (value);
  return (value->type == M4_SYMBOL_TEXT);
}

#undef m4_is_symbol_value_func
bool
m4_is_symbol_value_func (m4_symbol_value *value)
{
  assert (value);
  return (value->type == M4_SYMBOL_FUNC);
}

#undef m4_is_symbol_value_placeholder
bool
m4_is_symbol_value_placeholder (m4_symbol_value *value)
{
  assert (value);
  return (value->type == M4_SYMBOL_PLACEHOLDER);
}

#undef m4_is_symbol_value_void
bool
m4_is_symbol_value_void (m4_symbol_value *value)
{
  assert (value);
  return (value->type == M4_SYMBOL_VOID);
}

#undef m4_get_symbol_value_text
const char *
m4_get_symbol_value_text (m4_symbol_value *value)
{
  assert (value && value->type == M4_SYMBOL_TEXT);
  return value->u.u_t.text;
}

#undef m4_get_symbol_value_len
size_t
m4_get_symbol_value_len (m4_symbol_value *value)
{
  assert (value && value->type == M4_SYMBOL_TEXT);
  return value->u.u_t.len;
}

#undef m4_get_symbol_value_quote_age
unsigned int
m4_get_symbol_value_quote_age (m4_symbol_value *value)
{
  assert (value && value->type == M4_SYMBOL_TEXT);
  return value->u.u_t.quote_age;
}

#undef m4_get_symbol_value_func
m4_builtin_func *
m4_get_symbol_value_func (m4_symbol_value *value)
{
  assert (value && value->type == M4_SYMBOL_FUNC);
  return value->u.builtin->builtin.func;
}

#undef m4_get_symbol_value_builtin
const m4_builtin *
m4_get_symbol_value_builtin (m4_symbol_value *value)
{
  assert (value && value->type == M4_SYMBOL_FUNC);
  return &value->u.builtin->builtin;
}

#undef m4_get_symbol_value_placeholder
const char *
m4_get_symbol_value_placeholder (m4_symbol_value *value)
{
  assert (value && value->type == M4_SYMBOL_PLACEHOLDER);
  return value->u.u_t.text;
}

#undef m4_set_symbol_value_text
void
m4_set_symbol_value_text (m4_symbol_value *value, const char *text, size_t len,
                          unsigned int quote_age)
{
  assert (value && text);
  /* In practice, it is easier to debug when we guarantee a
     terminating NUL, even when there are embedded NULs.  */
  assert (!text[len]);

  value->type = M4_SYMBOL_TEXT;
  value->u.u_t.text = text;
  value->u.u_t.len = len;
  value->u.u_t.quote_age = quote_age;
}

#undef m4__set_symbol_value_builtin
void
m4__set_symbol_value_builtin (m4_symbol_value *value,
                              const m4__builtin *builtin)
{
  assert (value && builtin);

  value->type = M4_SYMBOL_FUNC;
  value->u.builtin = builtin;
  VALUE_MODULE (value) = builtin->module;
  VALUE_FLAGS (value) = builtin->builtin.flags;
  VALUE_MIN_ARGS (value) = builtin->builtin.min_args;
  VALUE_MAX_ARGS (value) = builtin->builtin.max_args;
}

#undef m4_set_symbol_value_placeholder
void
m4_set_symbol_value_placeholder (m4_symbol_value *value, const char *text)
{
  assert (value);
  assert (text);

  value->type = M4_SYMBOL_PLACEHOLDER;
  value->u.u_t.text = text;
  value->u.u_t.len = SIZE_MAX; /* len is not tracked for placeholders.  */
}


#ifdef DEBUG_SYM

static void *dump_symbol_CB     (m4_symbol_table *symtab, const char *name,
                                 m4_symbol *symbol, void *userdata);
static M4_GNUC_UNUSED void *
symtab_dump (m4 *context, m4_symbol_table *symtab)
{
  return m4_symtab_apply (symtab, true, dump_symbol_CB, context);
}

static void *
dump_symbol_CB (m4_symbol_table *symtab, const char *name,
                m4_symbol *symbol, void *ptr)
{
  m4 *             context      = (m4 *) ptr;
  m4_symbol_value *value        = m4_get_symbol_value (symbol);
  int              flags        = value ? SYMBOL_FLAGS (symbol) : 0;
  m4_module *      module       = value ? SYMBOL_MODULE (symbol) : NULL;
  const char *     module_name  = module ? m4_get_module_name (module) : "NONE";

  xfprintf (stderr, "%10s: (%d%s) %s=", module_name, flags,
            m4_get_symbol_traced (symbol) ? "!" : "", name);

  if (!value)
    fputs ("<!UNDEFINED!>", stderr);
  else if (m4_is_symbol_value_void (value))
    fputs ("<!VOID!>", stderr);
  else
    {
      m4_obstack obs;
      obstack_init (&obs);
      m4__symbol_value_print (context, value, &obs, NULL, false, NULL, NULL,
                              true);
      xfprintf (stderr, "%s", (char *) obstack_finish (&obs));
      obstack_free (&obs, NULL);
    }
  fputc ('\n', stderr);
  return NULL;
}
#endif /* DEBUG_SYM */