New symbol_summary class introduced.
[platform/upstream/gcc.git] / gcc / gengtype.c
1 /* Process source files and output type information.
2    Copyright (C) 2002-2014 Free Software Foundation, Inc.
3
4    This file is part of GCC.
5
6    GCC is free software; you can redistribute it and/or modify it under
7    the terms of the GNU General Public License as published by the Free
8    Software Foundation; either version 3, or (at your option) any later
9    version.
10
11    GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12    WARRANTY; without even the implied warranty of MERCHANTABILITY or
13    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14    for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GCC; see the file COPYING3.  If not see
18    <http://www.gnu.org/licenses/>.  */
19
20 #ifdef HOST_GENERATOR_FILE
21 #include "config.h"
22 #define GENERATOR_FILE 1
23 #else
24 #include "bconfig.h"
25 #endif
26 #include "system.h"
27 #include "errors.h"             /* for fatal */
28 #include "getopt.h"
29 #include "version.h"            /* for version_string & pkgversion_string.  */
30 #include "hashtab.h"
31 #include "xregex.h"
32 #include "obstack.h"
33 #include "gengtype.h"
34 #include "filenames.h"
35
36 /* Data types, macros, etc. used only in this file.  */
37
38
39 /* The list of output files.  */
40 outf_p output_files;
41
42 /* The output header file that is included into pretty much every
43    source file.  */
44 outf_p header_file;
45
46
47 /* The name of the file containing the list of input files.  */
48 static char *inputlist;
49
50 /* The plugin input files and their number; in that case only
51    a single file is produced.  */
52 static input_file **plugin_files;
53 static size_t nb_plugin_files;
54
55 /* The generated plugin output file and name.  */
56 static outf_p plugin_output;
57 static char *plugin_output_filename;
58
59 /* Our source directory and its length.  */
60 const char *srcdir;
61 size_t srcdir_len;
62
63 /* Variables used for reading and writing the state.  */
64 const char *read_state_filename;
65 const char *write_state_filename;
66
67 /* Variables to help debugging.  */
68 int do_dump;
69 int do_debug;
70
71 /* Level for verbose messages.  */
72 int verbosity_level;
73
74 /* We have a type count and use it to set the state_number of newly
75    allocated types to some unique negative number.  */
76 static int type_count;
77
78 /* The backup directory should be in the same file system as the
79    generated files, otherwise the rename(2) system call would fail.
80    If NULL, no backup is made when overwriting a generated file.  */
81 static const char* backup_dir;  /* (-B) program option.  */
82
83
84 static outf_p create_file (const char *, const char *);
85
86 static const char *get_file_basename (const input_file *);
87 static const char *get_file_realbasename (const input_file *);
88
89 static int get_prefix_langdir_index (const char *);
90 static const char *get_file_langdir (const input_file *);
91
92 static void dump_pair (int indent, pair_p p);
93 static void dump_type (int indent, type_p p);
94 static void dump_type_list (int indent, type_p p);
95 \f
96
97 /* Nonzero iff an error has occurred.  */
98 bool hit_error = false;
99
100 static void gen_rtx_next (void);
101 static void write_rtx_next (void);
102 static void open_base_files (void);
103 static void close_output_files (void);
104
105 /* Report an error at POS, printing MSG.  */
106
107 void
108 error_at_line (const struct fileloc *pos, const char *msg, ...)
109 {
110   va_list ap;
111
112   gcc_assert (pos != NULL && pos->file != NULL);
113   va_start (ap, msg);
114
115   fprintf (stderr, "%s:%d: ", get_input_file_name (pos->file), pos->line);
116   vfprintf (stderr, msg, ap);
117   fputc ('\n', stderr);
118   hit_error = true;
119
120   va_end (ap);
121 }
122
123 /* asprintf, but produces fatal message on out-of-memory.  */
124 char *
125 xasprintf (const char *format, ...)
126 {
127   int n;
128   char *result;
129   va_list ap;
130
131   va_start (ap, format);
132   n = vasprintf (&result, format, ap);
133   if (result == NULL || n < 0)
134     fatal ("out of memory");
135   va_end (ap);
136
137   return result;
138 }
139 \f
140 /* Locate the ultimate base class of struct S.  */
141
142 static const_type_p
143 get_ultimate_base_class (const_type_p s)
144 {
145   while (s->u.s.base_class)
146     s = s->u.s.base_class;
147   return s;
148 }
149
150 static type_p
151 get_ultimate_base_class (type_p s)
152 {
153   while (s->u.s.base_class)
154     s = s->u.s.base_class;
155   return s;
156 }
157 \f
158 /* Input file handling. */
159
160 /* Table of all input files.  */
161 const input_file **gt_files;
162 size_t num_gt_files;
163
164 /* A number of places use the name of this "gengtype.c" file for a
165    location for things that we can't rely on the source to define.
166    Make sure we can still use pointer comparison on filenames.  */
167 input_file* this_file;
168 /* The "system.h" file is likewise specially useful.  */
169 input_file* system_h_file;
170
171 /* Vector of per-language directories.  */
172 const char **lang_dir_names;
173 size_t num_lang_dirs;
174
175 /* An array of output files suitable for definitions.  There is one
176    BASE_FILES entry for each language.  */
177 static outf_p *base_files;
178
179
180
181 #if ENABLE_CHECKING
182 /* Utility debugging function, printing the various type counts within
183    a list of types.  Called through the DBGPRINT_COUNT_TYPE macro.  */
184 void
185 dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
186 {
187   int nb_types = 0, nb_scalar = 0, nb_string = 0;
188   int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
189   int nb_lang_struct = 0;
190   int nb_user_struct = 0, nb_undefined = 0;
191   type_p p = NULL;
192   for (p = t; p; p = p->next)
193     {
194       nb_types++;
195       switch (p->kind)
196         {
197         case TYPE_UNDEFINED:
198           nb_undefined++;
199         case TYPE_SCALAR:
200           nb_scalar++;
201           break;
202         case TYPE_STRING:
203           nb_string++;
204           break;
205         case TYPE_STRUCT:
206           nb_struct++;
207           break;
208         case TYPE_USER_STRUCT:
209           nb_user_struct++;
210           break;
211         case TYPE_UNION:
212           nb_union++;
213           break;
214         case TYPE_POINTER:
215           nb_pointer++;
216           break;
217         case TYPE_ARRAY:
218           nb_array++;
219           break;
220         case TYPE_LANG_STRUCT:
221           nb_lang_struct++;
222           break;
223         case TYPE_NONE:
224           gcc_unreachable ();
225         }
226     }
227   fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
228            lbasename (fil), lin, msg, nb_types);
229   if (nb_scalar > 0 || nb_string > 0)
230     fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
231   if (nb_struct > 0 || nb_union > 0)
232     fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
233   if (nb_pointer > 0 || nb_array > 0)
234     fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
235   if (nb_lang_struct > 0)
236     fprintf (stderr, "@@%%@@ %d lang_structs\n", nb_lang_struct);
237   if (nb_user_struct > 0)
238     fprintf (stderr, "@@%%@@ %d user_structs\n", nb_user_struct);
239   if (nb_undefined > 0)
240     fprintf (stderr, "@@%%@@ %d undefined types\n", nb_undefined);
241   fprintf (stderr, "\n");
242 }
243 #endif /* ENABLE_CHECKING */
244
245 /* Scan the input file, LIST, and determine how much space we need to
246    store strings in.  Also, count the number of language directories
247    and files.  The numbers returned are overestimates as they does not
248    consider repeated files.  */
249 static size_t
250 measure_input_list (FILE *list)
251 {
252   size_t n = 0;
253   int c;
254   bool atbol = true;
255   num_lang_dirs = 0;
256   num_gt_files = plugin_files ? nb_plugin_files : 0;
257   while ((c = getc (list)) != EOF)
258     {
259       n++;
260       if (atbol)
261         {
262           if (c == '[')
263             num_lang_dirs++;
264           else
265             {
266               /* Add space for a lang_bitmap before the input file name.  */
267               n += sizeof (lang_bitmap);
268               num_gt_files++;
269             }
270           atbol = false;
271         }
272
273       if (c == '\n')
274         atbol = true;
275     }
276
277   rewind (list);
278   return n;
279 }
280
281 /* Read one input line from LIST to HEREP (which is updated).  A
282    pointer to the string is returned via LINEP.  If it was a language
283    subdirectory in square brackets, strip off the square brackets and
284    return true.  Otherwise, leave space before the string for a
285    lang_bitmap, and return false.  At EOF, returns false, does not
286    touch *HEREP, and sets *LINEP to NULL.  POS is used for
287    diagnostics.  */
288 static bool
289 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
290 {
291   char *here = *herep;
292   char *line;
293   int c = getc (list);
294
295   /* Read over whitespace.  */
296   while (c == '\n' || c == ' ')
297     c = getc (list);
298
299   if (c == EOF)
300     {
301       *linep = 0;
302       return false;
303     }
304   else if (c == '[')
305     {
306       /* No space for a lang_bitmap is necessary.  Discard the '['. */
307       c = getc (list);
308       line = here;
309       while (c != ']' && c != '\n' && c != EOF)
310         {
311           *here++ = c;
312           c = getc (list);
313         }
314       *here++ = '\0';
315
316       if (c == ']')
317         {
318           c = getc (list);      /* eat what should be a newline */
319           if (c != '\n' && c != EOF)
320             error_at_line (pos, "junk on line after language tag [%s]", line);
321         }
322       else
323         error_at_line (pos, "missing close bracket for language tag [%s",
324                        line);
325
326       *herep = here;
327       *linep = line;
328       return true;
329     }
330   else
331     {
332       /* Leave space for a lang_bitmap.  */
333       memset (here, 0, sizeof (lang_bitmap));
334       here += sizeof (lang_bitmap);
335       line = here;
336       do
337         {
338           *here++ = c;
339           c = getc (list);
340         }
341       while (c != EOF && c != '\n');
342       *here++ = '\0';
343       *herep = here;
344       *linep = line;
345       return false;
346     }
347 }
348
349 /* Read the list of input files from LIST and compute all of the
350    relevant tables.  There is one file per line of the list.  At
351    first, all the files on the list are language-generic, but
352    eventually a line will appear which is the name of a language
353    subdirectory in square brackets, like this: [cp].  All subsequent
354    files are specific to that language, until another language
355    subdirectory tag appears.  Files can appear more than once, if
356    they apply to more than one language.  */
357 static void
358 read_input_list (const char *listname)
359 {
360   FILE *list = fopen (listname, "r");
361   if (!list)
362     fatal ("cannot open %s: %s", listname, xstrerror (errno));
363   else
364     {
365       struct fileloc epos;
366       size_t bufsz = measure_input_list (list);
367       char *buf = XNEWVEC (char, bufsz);
368       char *here = buf;
369       char *committed = buf;
370       char *limit = buf + bufsz;
371       char *line;
372       bool is_language;
373       size_t langno = 0;
374       size_t nfiles = 0;
375       lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
376
377       epos.file = input_file_by_name (listname);
378       epos.line = 0;
379
380       lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
381       gt_files = XNEWVEC (const input_file *, num_gt_files);
382
383       for (;;)
384         {
385         next_line:
386           epos.line++;
387           committed = here;
388           is_language = read_input_line (list, &here, &line, &epos);
389           gcc_assert (here <= limit);
390           if (line == 0)
391             break;
392           else if (is_language)
393             {
394               size_t i;
395               gcc_assert (langno <= num_lang_dirs);
396               for (i = 0; i < langno; i++)
397                 if (strcmp (lang_dir_names[i], line) == 0)
398                   {
399                     error_at_line (&epos, "duplicate language tag [%s]",
400                                    line);
401                     curlangs = 1 << i;
402                     here = committed;
403                     goto next_line;
404                   }
405
406               curlangs = 1 << langno;
407               lang_dir_names[langno++] = line;
408             }
409           else
410             {
411               size_t i;
412               input_file *inpf = input_file_by_name (line);
413               gcc_assert (nfiles <= num_gt_files);
414               for (i = 0; i < nfiles; i++)
415                 /* Since the input_file-s are uniquely hash-consed, we
416                    can just compare pointers! */
417                 if (gt_files[i] == inpf)
418                   {
419                     /* Throw away the string we just read, and add the
420                        current language to the existing string's bitmap.  */
421                     lang_bitmap bmap = get_lang_bitmap (inpf);
422                     if (bmap & curlangs)
423                       error_at_line (&epos,
424                                      "file %s specified more than once "
425                                      "for language %s", line,
426                                      langno ==
427                                      0 ? "(all)" : lang_dir_names[langno -
428                                                                   1]);
429
430                     bmap |= curlangs;
431                     set_lang_bitmap (inpf, bmap);
432                     here = committed;
433                     goto next_line;
434                   }
435
436               set_lang_bitmap (inpf, curlangs);
437               gt_files[nfiles++] = inpf;
438             }
439         }
440       /* Update the global counts now that we know accurately how many
441          things there are.  (We do not bother resizing the arrays down.)  */
442       num_lang_dirs = langno;
443       /* Add the plugin files if provided.  */
444       if (plugin_files)
445         {
446           size_t i;
447           for (i = 0; i < nb_plugin_files; i++)
448             gt_files[nfiles++] = plugin_files[i];
449         }
450       num_gt_files = nfiles;
451     }
452
453   /* Sanity check: any file that resides in a language subdirectory
454      (e.g. 'cp') ought to belong to the corresponding language.
455      ??? Still true if for instance ObjC++ is enabled and C++ isn't?
456      (Can you even do that?  Should you be allowed to?)  */
457   {
458     size_t f;
459     for (f = 0; f < num_gt_files; f++)
460       {
461         lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
462         const char *basename = get_file_basename (gt_files[f]);
463         const char *slashpos = strchr (basename, '/');
464 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
465         const char *slashpos2 = strchr (basename, '\\');
466
467         if (!slashpos || (slashpos2 && slashpos2 < slashpos))
468           slashpos = slashpos2;
469 #endif
470
471         if (slashpos)
472           {
473             size_t l;
474             for (l = 0; l < num_lang_dirs; l++)
475               if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
476                   && memcmp (basename, lang_dir_names[l],
477                              strlen (lang_dir_names[l])) == 0)
478                 {
479                   if (!(bitmap & (1 << l)))
480                     error ("%s is in language directory '%s' but is not "
481                            "tagged for that language",
482                            basename, lang_dir_names[l]);
483                   break;
484                 }
485           }
486       }
487   }
488
489   if (ferror (list))
490     fatal ("error reading %s: %s", listname, xstrerror (errno));
491
492   fclose (list);
493 }
494 \f
495
496
497 /* The one and only TYPE_STRING.  */
498
499 struct type string_type = {
500   TYPE_STRING, 0, 0, 0, GC_USED, {0}
501 };
502
503 /* The two and only TYPE_SCALARs.  Their u.scalar_is_char flags are
504    set early in main.  */
505
506 struct type scalar_nonchar = {
507   TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
508 };
509
510 struct type scalar_char = {
511   TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
512 };
513
514 /* Lists of various things.  */
515
516 pair_p typedefs = NULL;
517 type_p structures = NULL;
518 pair_p variables = NULL;
519
520 static type_p adjust_field_tree_exp (type_p t, options_p opt);
521 static type_p adjust_field_rtx_def (type_p t, options_p opt);
522
523 /* Define S as a typedef to T at POS.  */
524
525 void
526 do_typedef (const char *s, type_p t, struct fileloc *pos)
527 {
528   pair_p p;
529
530   /* temporary kludge - gengtype doesn't handle conditionals or
531      macros.  Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
532      is coming from this file (main() sets them up with safe dummy
533      definitions).  */
534   if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
535     return;
536
537   for (p = typedefs; p != NULL; p = p->next)
538     if (strcmp (p->name, s) == 0)
539       {
540         if (p->type != t && strcmp (s, "result_type") != 0)
541           {
542             error_at_line (pos, "type `%s' previously defined", s);
543             error_at_line (&p->line, "previously defined here");
544           }
545         return;
546       }
547
548   p = XNEW (struct pair);
549   p->next = typedefs;
550   p->name = s;
551   p->type = t;
552   p->line = *pos;
553   p->opt = NULL;
554   typedefs = p;
555 }
556
557 /* Define S as a typename of a scalar.  Cannot be used to define
558    typedefs of 'char'.  Note: is also used for pointer-to-function
559    typedefs (which are therefore not treated as pointers).  */
560
561 void
562 do_scalar_typedef (const char *s, struct fileloc *pos)
563 {
564   do_typedef (s, &scalar_nonchar, pos);
565 }
566
567 /* Similar to strtok_r.  */
568
569 static char *
570 strtoken (char *str, const char *delim, char **next)
571 {
572   char *p;
573
574   if (str == NULL)
575     str = *next;
576
577   /* Skip the leading delimiters.  */
578   str += strspn (str, delim);
579   if (*str == '\0')
580     /* This is an empty token.  */
581     return NULL;
582
583   /* The current token.  */
584   p = str;
585
586   /* Find the next delimiter.  */
587   str += strcspn (str, delim);
588   if (*str == '\0')
589     /* This is the last token.  */
590     *next = str;
591   else
592     {
593       /* Terminate the current token.  */
594       *str = '\0';
595       /* Advance to the next token.  */
596       *next = str + 1;
597     }
598
599   return p;
600 }
601
602 /* Define TYPE_NAME to be a user defined type at location POS.  */
603
604 type_p
605 create_user_defined_type (const char *type_name, struct fileloc *pos)
606 {
607   type_p ty = find_structure (type_name, TYPE_USER_STRUCT);
608
609   /* We might have already seen an incomplete decl of the given type,
610      in which case we won't have yet seen a GTY((user)), and the type will
611      only have kind "TYPE_STRUCT".  Mark it as a user struct.  */
612   ty->kind = TYPE_USER_STRUCT;
613
614   ty->u.s.line = *pos;
615   ty->u.s.bitmap = get_lang_bitmap (pos->file);
616   do_typedef (type_name, ty, pos);
617
618   /* If TYPE_NAME specifies a template, create references to the types
619      in the template by pretending that each type is a field of TY.
620      This is needed to make sure that the types referenced by the
621      template are marked as used.  */
622   char *str = xstrdup (type_name);
623   char *open_bracket = strchr (str, '<');
624   if (open_bracket)
625     {
626       /* We only accept simple template declarations (see
627          require_template_declaration), so we only need to parse a
628          comma-separated list of strings, implicitly assumed to
629          be type names, potentially with "*" characters.  */
630       char *arg = open_bracket + 1;
631       char *next;
632       char *type_id = strtoken (arg, ",>", &next);
633       pair_p fields = 0;
634       while (type_id)
635         {
636           /* Create a new field for every type found inside the template
637              parameter list.  */
638
639           /* Support a single trailing "*" character.  */
640           const char *star = strchr (type_id, '*');
641           int is_ptr = (star != NULL);
642           size_t offset_to_star = star - type_id;
643           if (is_ptr)
644             offset_to_star = star - type_id;
645
646           if (strstr (type_id, "char*"))
647             {
648           type_id = strtoken (0, ",>", &next);
649           continue;
650             }
651
652           char *field_name = xstrdup (type_id);
653
654           type_p arg_type;
655           if (is_ptr)
656             {
657               /* Strip off the first '*' character (and any subsequent text). */
658               *(field_name + offset_to_star) = '\0';
659
660               arg_type = find_structure (field_name, TYPE_STRUCT);
661               arg_type = create_pointer (arg_type);
662             }
663           else
664             arg_type = resolve_typedef (field_name, pos);
665
666           fields = create_field_at (fields, arg_type, field_name, 0, pos);
667           type_id = strtoken (0, ",>", &next);
668         }
669
670       /* Associate the field list to TY.  */
671       ty->u.s.fields = fields;
672     }
673   free (str);
674
675   return ty;
676 }
677
678
679 /* Given a typedef name S, return its associated type.  Return NULL if
680    S is not a registered type name.  */
681
682 static type_p
683 type_for_name (const char *s)
684 {
685   pair_p p;
686
687   /* Special-case support for types within a "gcc::" namespace.  Rather
688      than fully-supporting namespaces, simply strip off the "gcc::" prefix
689      where present.  This allows us to have GTY roots of this form:
690          extern GTY(()) gcc::some_type *some_ptr;
691      where the autogenerated functions will refer to simply "some_type",
692      where they can be resolved into their namespace.  */
693   if (0 == strncmp (s, "gcc::", 5))
694     s += 5;
695
696   for (p = typedefs; p != NULL; p = p->next)
697     if (strcmp (p->name, s) == 0)
698       return p->type;
699   return NULL;
700 }
701
702
703 /* Create an undefined type with name S and location POS.  Return the
704    newly created type.  */
705
706 static type_p
707 create_undefined_type (const char *s, struct fileloc *pos)
708 {
709   type_p ty = find_structure (s, TYPE_UNDEFINED);
710   ty->u.s.line = *pos;
711   ty->u.s.bitmap = get_lang_bitmap (pos->file);
712   do_typedef (s, ty, pos);
713   return ty;
714 }
715
716
717 /* Return the type previously defined for S.  Use POS to report errors.  */
718
719 type_p
720 resolve_typedef (const char *s, struct fileloc *pos)
721 {
722   bool is_template_instance = (strchr (s, '<') != NULL);
723   type_p p = type_for_name (s);
724
725   /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
726      type for regular type identifiers.  If the type identifier S is a
727      template instantiation, however, we treat it as a user defined
728      type.
729
730      FIXME, this is actually a limitation in gengtype.  Supporting
731      template types and their instances would require keeping separate
732      track of the basic types definition and its instances.  This
733      essentially forces all template classes in GC to be marked
734      GTY((user)).  */
735   if (!p)
736     p = (is_template_instance)
737         ? create_user_defined_type (s, pos)
738         : create_undefined_type (s, pos);
739
740   return p;
741 }
742
743 /* Add SUBCLASS to head of linked list of BASE's subclasses.  */
744
745 void add_subclass (type_p base, type_p subclass)
746 {
747   gcc_assert (union_or_struct_p (base));
748   gcc_assert (union_or_struct_p (subclass));
749
750   subclass->u.s.next_sibling_class = base->u.s.first_subclass;
751   base->u.s.first_subclass = subclass;
752 }
753
754 /* Create and return a new structure with tag NAME at POS with fields
755    FIELDS and options O.  The KIND of structure must be one of
756    TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT.  */
757
758 type_p
759 new_structure (const char *name, enum typekind kind, struct fileloc *pos,
760                pair_p fields, options_p o, type_p base_class)
761 {
762   type_p si;
763   type_p s = NULL;
764   lang_bitmap bitmap = get_lang_bitmap (pos->file);
765   bool isunion = (kind == TYPE_UNION);
766
767   gcc_assert (union_or_struct_p (kind));
768
769   for (si = structures; si != NULL; si = si->next)
770     if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
771       {
772         type_p ls = NULL;
773         if (si->kind == TYPE_LANG_STRUCT)
774           {
775             ls = si;
776
777             for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
778               if (si->u.s.bitmap == bitmap)
779                 s = si;
780           }
781         else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
782           {
783             ls = si;
784             type_count++;
785             si = XCNEW (struct type);
786             memcpy (si, ls, sizeof (struct type));
787             ls->kind = TYPE_LANG_STRUCT;
788             ls->u.s.lang_struct = si;
789             ls->u.s.fields = NULL;
790             si->next = NULL;
791             si->state_number = -type_count;
792             si->pointer_to = NULL;
793             si->u.s.lang_struct = ls;
794           }
795         else
796           s = si;
797
798         if (ls != NULL && s == NULL)
799           {
800             type_count++;
801             s = XCNEW (struct type);
802             s->state_number = -type_count;
803             s->next = ls->u.s.lang_struct;
804             ls->u.s.lang_struct = s;
805             s->u.s.lang_struct = ls;
806           }
807         break;
808       }
809
810   if (s == NULL)
811     {
812       type_count++;
813       s = XCNEW (struct type);
814       s->state_number = -type_count;
815       s->next = structures;
816       structures = s;
817     }
818
819   if (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap))
820     {
821       error_at_line (pos, "duplicate definition of '%s %s'",
822                      isunion ? "union" : "struct", s->u.s.tag);
823       error_at_line (&s->u.s.line, "previous definition here");
824     }
825
826   s->kind = kind;
827   s->u.s.tag = name;
828   s->u.s.line = *pos;
829   s->u.s.fields = fields;
830   s->u.s.opt = o;
831   s->u.s.bitmap = bitmap;
832   if (s->u.s.lang_struct)
833     s->u.s.lang_struct->u.s.bitmap |= bitmap;
834   s->u.s.base_class = base_class;
835   if (base_class)
836     add_subclass (base_class, s);
837
838   return s;
839 }
840
841 /* Return the previously-defined structure or union with tag NAME,
842    or a new empty structure or union if none was defined previously.
843    The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
844    TYPE_USER_STRUCT.  */
845
846 type_p
847 find_structure (const char *name, enum typekind kind)
848 {
849   type_p s;
850   bool isunion = (kind == TYPE_UNION);
851
852   gcc_assert (kind == TYPE_UNDEFINED || union_or_struct_p (kind));
853
854   for (s = structures; s != NULL; s = s->next)
855     if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
856       return s;
857
858   type_count++;
859   s = XCNEW (struct type);
860   s->next = structures;
861   s->state_number = -type_count;
862   structures = s;
863   s->kind = kind;
864   s->u.s.tag = name;
865   structures = s;
866   return s;
867 }
868
869 /* Return a scalar type with name NAME.  */
870
871 type_p
872 create_scalar_type (const char *name)
873 {
874   if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
875     return &scalar_char;
876   else
877     return &scalar_nonchar;
878 }
879
880
881 /* Return a pointer to T.  */
882
883 type_p
884 create_pointer (type_p t)
885 {
886   if (!t->pointer_to)
887     {
888       type_p r = XCNEW (struct type);
889       type_count++;
890       r->state_number = -type_count;
891       r->kind = TYPE_POINTER;
892       r->u.p = t;
893       t->pointer_to = r;
894     }
895   return t->pointer_to;
896 }
897
898 /* Return an array of length LEN.  */
899
900 type_p
901 create_array (type_p t, const char *len)
902 {
903   type_p v;
904
905   type_count++;
906   v = XCNEW (struct type);
907   v->kind = TYPE_ARRAY;
908   v->state_number = -type_count;
909   v->u.a.p = t;
910   v->u.a.len = len;
911   return v;
912 }
913
914 /* Return a string options structure with name NAME and info INFO.
915    NEXT is the next option in the chain.  */
916 options_p
917 create_string_option (options_p next, const char *name, const char *info)
918 {
919   options_p o = XNEW (struct options);
920   o->kind = OPTION_STRING;
921   o->next = next;
922   o->name = name;
923   o->info.string = info;
924   return o;
925 }
926
927 /* Create a type options structure with name NAME and info INFO.  NEXT
928    is the next option in the chain.  */
929 options_p
930 create_type_option (options_p next, const char* name, type_p info)
931 {
932   options_p o = XNEW (struct options);
933   o->next = next;
934   o->name = name;
935   o->kind = OPTION_TYPE;
936   o->info.type = info;
937   return o;
938 }
939
940 /* Create a nested pointer options structure with name NAME and info
941    INFO.  NEXT is the next option in the chain.  */
942 options_p
943 create_nested_option (options_p next, const char* name,
944                       struct nested_ptr_data* info)
945 {
946   options_p o;
947   o = XNEW (struct options);
948   o->next = next;
949   o->name = name;
950   o->kind = OPTION_NESTED;
951   o->info.nested = info;
952   return o;
953 }
954
955 /* Return an options structure for a "nested_ptr" option.  */
956 options_p
957 create_nested_ptr_option (options_p next, type_p t,
958                           const char *to, const char *from)
959 {
960   struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
961
962   d->type = adjust_field_type (t, 0);
963   d->convert_to = to;
964   d->convert_from = from;
965   return create_nested_option (next, "nested_ptr", d);
966 }
967
968 /* Add a variable named S of type T with options O defined at POS,
969    to `variables'.  */
970 void
971 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
972 {
973   pair_p n;
974   n = XNEW (struct pair);
975   n->name = s;
976   n->type = t;
977   n->line = *pos;
978   n->opt = o;
979   n->next = variables;
980   variables = n;
981 }
982
983 /* Most-general structure field creator.  */
984 static pair_p
985 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
986                   const input_file *inpf, int line)
987 {
988   pair_p field;
989
990   field = XNEW (struct pair);
991   field->next = next;
992   field->type = type;
993   field->name = name;
994   field->opt = opt;
995   field->line.file = inpf;
996   field->line.line = line;
997   return field;
998 }
999
1000 /* Create a field that came from the source code we are scanning,
1001    i.e. we have a 'struct fileloc', and possibly options; also,
1002    adjust_field_type should be called.  */
1003 pair_p
1004 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
1005                  struct fileloc *pos)
1006 {
1007   return create_field_all (next, adjust_field_type (type, opt),
1008                            name, opt, pos->file, pos->line);
1009 }
1010
1011 /* Create a fake field with the given type and name.  NEXT is the next
1012    field in the chain.  */
1013 #define create_field(next,type,name) \
1014     create_field_all (next,type,name, 0, this_file, __LINE__)
1015
1016 /* Like create_field, but the field is only valid when condition COND
1017    is true.  */
1018
1019 static pair_p
1020 create_optional_field_ (pair_p next, type_p type, const char *name,
1021                         const char *cond, int line)
1022 {
1023   static int id = 1;
1024   pair_p union_fields;
1025   type_p union_type;
1026
1027   /* Create a fake union type with a single nameless field of type TYPE.
1028      The field has a tag of "1".  This allows us to make the presence
1029      of a field of type TYPE depend on some boolean "desc" being true.  */
1030   union_fields = create_field (NULL, type, "");
1031   union_fields->opt = 
1032     create_string_option (union_fields->opt, "dot", "");
1033   union_fields->opt = 
1034     create_string_option (union_fields->opt, "tag", "1");
1035   union_type = 
1036     new_structure (xasprintf ("%s_%d", "fake_union", id++), TYPE_UNION,
1037                    &lexer_line, union_fields, NULL, NULL);
1038
1039   /* Create the field and give it the new fake union type.  Add a "desc"
1040      tag that specifies the condition under which the field is valid.  */
1041   return create_field_all (next, union_type, name,
1042                            create_string_option (0, "desc", cond), 
1043                            this_file, line);
1044 }
1045
1046 #define create_optional_field(next,type,name,cond)      \
1047        create_optional_field_(next,type,name,cond,__LINE__)
1048
1049 /* Reverse a linked list of 'struct pair's in place.  */
1050 pair_p
1051 nreverse_pairs (pair_p list)
1052 {
1053   pair_p prev = 0, p, next;
1054   for (p = list; p; p = next)
1055     {
1056       next = p->next;
1057       p->next = prev;
1058       prev = p;
1059     }
1060   return prev;
1061 }
1062 \f
1063
1064 /* We don't care how long a CONST_DOUBLE is.  */
1065 #define CONST_DOUBLE_FORMAT "ww"
1066 /* We don't want to see codes that are only for generator files.  */
1067 #undef GENERATOR_FILE
1068
1069 enum rtx_code
1070 {
1071 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1072 #include "rtl.def"
1073 #undef DEF_RTL_EXPR
1074   NUM_RTX_CODE
1075 };
1076
1077 static const char *const rtx_name[NUM_RTX_CODE] = {
1078 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS)   NAME ,
1079 #include "rtl.def"
1080 #undef DEF_RTL_EXPR
1081 };
1082
1083 static const char *const rtx_format[NUM_RTX_CODE] = {
1084 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS)   FORMAT ,
1085 #include "rtl.def"
1086 #undef DEF_RTL_EXPR
1087 };
1088
1089 static int rtx_next_new[NUM_RTX_CODE];
1090
1091 /* We also need codes and names for insn notes (not register notes).
1092    Note that we do *not* bias the note values here.  */
1093 enum insn_note
1094 {
1095 #define DEF_INSN_NOTE(NAME) NAME,
1096 #include "insn-notes.def"
1097 #undef DEF_INSN_NOTE
1098
1099   NOTE_INSN_MAX
1100 };
1101
1102 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1103    default field for line number notes.  */
1104 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
1105 #define DEF_INSN_NOTE(NAME) #NAME,
1106 #include "insn-notes.def"
1107 #undef DEF_INSN_NOTE
1108 };
1109
1110 #undef CONST_DOUBLE_FORMAT
1111 #define GENERATOR_FILE
1112
1113 /* Generate the contents of the rtx_next array.  This really doesn't belong
1114    in gengtype at all, but it's needed for adjust_field_rtx_def.  */
1115
1116 static void
1117 gen_rtx_next (void)
1118 {
1119   int i;
1120   for (i = 0; i < NUM_RTX_CODE; i++)
1121     {
1122       int k;
1123
1124       rtx_next_new[i] = -1;
1125       if (strncmp (rtx_format[i], "uu", 2) == 0)
1126         rtx_next_new[i] = 1;
1127       else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1128         rtx_next_new[i] = 1;
1129       else
1130         for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
1131           if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1132             rtx_next_new[i] = k;
1133     }
1134 }
1135
1136 /* Write out the contents of the rtx_next array.  */
1137 static void
1138 write_rtx_next (void)
1139 {
1140   outf_p f = get_output_file_with_visibility (NULL);
1141   int i;
1142   if (!f)
1143     return;
1144
1145   oprintf (f, "\n/* Used to implement the RTX_NEXT macro.  */\n");
1146   oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1147   for (i = 0; i < NUM_RTX_CODE; i++)
1148     if (rtx_next_new[i] == -1)
1149       oprintf (f, "  0,\n");
1150     else
1151       oprintf (f,
1152                "  RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1153   oprintf (f, "};\n");
1154 }
1155
1156 /* Handle `special("rtx_def")'.  This is a special case for field
1157    `fld' of struct rtx_def, which is an array of unions whose values
1158    are based in a complex way on the type of RTL.  */
1159
1160 static type_p
1161 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1162 {
1163   pair_p flds = NULL;
1164   options_p nodot;
1165   int i;
1166   type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1167   type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1168
1169   if (t->kind != TYPE_UNION)
1170     {
1171       error_at_line (&lexer_line,
1172                      "special `rtx_def' must be applied to a union");
1173       return &string_type;
1174     }
1175
1176   nodot = create_string_option (NULL, "dot", "");
1177
1178   rtx_tp = create_pointer (find_structure ("rtx_def", TYPE_STRUCT));
1179   rtvec_tp = create_pointer (find_structure ("rtvec_def", TYPE_STRUCT));
1180   tree_tp = create_pointer (find_structure ("tree_node", TYPE_UNION));
1181   mem_attrs_tp = create_pointer (find_structure ("mem_attrs", TYPE_STRUCT));
1182   reg_attrs_tp = 
1183     create_pointer (find_structure ("reg_attrs", TYPE_STRUCT));
1184   basic_block_tp = 
1185     create_pointer (find_structure ("basic_block_def", TYPE_STRUCT));
1186   constant_tp =
1187     create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT));
1188   scalar_tp = &scalar_nonchar;  /* rtunion int */
1189
1190   {
1191     pair_p note_flds = NULL;
1192     int c;
1193
1194     for (c = 0; c <= NOTE_INSN_MAX; c++)
1195       {
1196         switch (c)
1197           {
1198           case NOTE_INSN_MAX:
1199           case NOTE_INSN_DELETED_LABEL:
1200           case NOTE_INSN_DELETED_DEBUG_LABEL:
1201             note_flds = create_field (note_flds, &string_type, "rt_str");
1202             break;
1203
1204           case NOTE_INSN_BLOCK_BEG:
1205           case NOTE_INSN_BLOCK_END:
1206             note_flds = create_field (note_flds, tree_tp, "rt_tree");
1207             break;
1208
1209           case NOTE_INSN_VAR_LOCATION:
1210           case NOTE_INSN_CALL_ARG_LOCATION:
1211             note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1212             break;
1213
1214           default:
1215             note_flds = create_field (note_flds, scalar_tp, "rt_int");
1216             break;
1217           }
1218         /* NOTE_INSN_MAX is used as the default field for line
1219            number notes.  */
1220         if (c == NOTE_INSN_MAX)
1221           note_flds->opt = 
1222             create_string_option (nodot, "default", "");
1223         else
1224           note_flds->opt = 
1225             create_string_option (nodot, "tag", note_insn_name[c]);
1226       }
1227     note_union_tp = new_structure ("rtx_def_note_subunion", TYPE_UNION,
1228                                    &lexer_line, note_flds, NULL, NULL);
1229   }
1230   /* Create a type to represent the various forms of SYMBOL_REF_DATA.  */
1231   {
1232     pair_p sym_flds;
1233     sym_flds = create_field (NULL, tree_tp, "rt_tree");
1234     sym_flds->opt = create_string_option (nodot, "default", "");
1235     sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1236     sym_flds->opt = create_string_option (nodot, "tag", "1");
1237     symbol_union_tp = new_structure ("rtx_def_symbol_subunion", TYPE_UNION,
1238                                      &lexer_line, sym_flds, NULL, NULL);
1239   }
1240   for (i = 0; i < NUM_RTX_CODE; i++)
1241     {
1242       pair_p subfields = NULL;
1243       size_t aindex, nmindex;
1244       const char *sname;
1245       type_p substruct;
1246       char *ftag;
1247
1248       for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1249         {
1250           type_p t;
1251           const char *subname;
1252
1253           switch (rtx_format[i][aindex])
1254             {
1255             case '*':
1256             case 'i':
1257             case 'n':
1258             case 'w':
1259               t = scalar_tp;
1260               subname = "rt_int";
1261               break;
1262
1263             case '0':
1264               if (i == MEM && aindex == 1)
1265                 t = mem_attrs_tp, subname = "rt_mem";
1266               else if (i == JUMP_INSN && aindex == 7)
1267                 t = rtx_tp, subname = "rt_rtx";
1268               else if (i == CODE_LABEL && aindex == 4)
1269                 t = scalar_tp, subname = "rt_int";
1270               else if (i == CODE_LABEL && aindex == 3)
1271                 t = rtx_tp, subname = "rt_rtx";
1272               else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1273                 t = rtx_tp, subname = "rt_rtx";
1274               else if (i == NOTE && aindex == 3)
1275                 t = note_union_tp, subname = "";
1276               else if (i == NOTE && aindex == 4)
1277                 t = scalar_tp, subname = "rt_int";
1278               else if (i == NOTE && aindex >= 6)
1279                 t = scalar_tp, subname = "rt_int";
1280               else if (i == ADDR_DIFF_VEC && aindex == 4)
1281                 t = scalar_tp, subname = "rt_int";
1282               else if (i == VALUE && aindex == 0)
1283                 t = scalar_tp, subname = "rt_int";
1284               else if (i == DEBUG_EXPR && aindex == 0)
1285                 t = tree_tp, subname = "rt_tree";
1286               else if (i == REG && aindex == 1)
1287                 t = reg_attrs_tp, subname = "rt_reg";
1288               else if (i == SYMBOL_REF && aindex == 1)
1289                 t = symbol_union_tp, subname = "";
1290               else if (i == JUMP_TABLE_DATA && aindex >= 4)
1291                 t = scalar_tp, subname = "rt_int";
1292               else if (i == BARRIER && aindex >= 2)
1293                 t = scalar_tp, subname = "rt_int";
1294               else if (i == ENTRY_VALUE && aindex == 0)
1295                 t = rtx_tp, subname = "rt_rtx";
1296               else
1297                 {
1298                   error_at_line 
1299                     (&lexer_line,
1300                      "rtx type `%s' has `0' in position %lu, can't handle",
1301                      rtx_name[i], (unsigned long) aindex);
1302                   t = &string_type;
1303                   subname = "rt_int";
1304                 }
1305               break;
1306
1307             case 's':
1308             case 'S':
1309             case 'T':
1310               t = &string_type;
1311               subname = "rt_str";
1312               break;
1313
1314             case 'e':
1315             case 'u':
1316               t = rtx_tp;
1317               subname = "rt_rtx";
1318               break;
1319
1320             case 'E':
1321             case 'V':
1322               t = rtvec_tp;
1323               subname = "rt_rtvec";
1324               break;
1325
1326             case 't':
1327               t = tree_tp;
1328               subname = "rt_tree";
1329               break;
1330
1331             case 'B':
1332               t = basic_block_tp;
1333               subname = "rt_bb";
1334               break;
1335
1336             default:
1337               error_at_line
1338                 (&lexer_line,
1339                  "rtx type `%s' has `%c' in position %lu, can't handle",
1340                  rtx_name[i], rtx_format[i][aindex],
1341                  (unsigned long) aindex);
1342               t = &string_type;
1343               subname = "rt_int";
1344               break;
1345             }
1346
1347           subfields = create_field (subfields, t,
1348                                     xasprintf (".fld[%lu].%s",
1349                                                (unsigned long) aindex,
1350                                                subname));
1351           subfields->opt = nodot;
1352           if (t == note_union_tp)
1353             subfields->opt =
1354               create_string_option (subfields->opt, "desc",
1355                                     "NOTE_KIND (&%0)");
1356           if (t == symbol_union_tp)
1357             subfields->opt = 
1358               create_string_option (subfields->opt, "desc",
1359                                     "CONSTANT_POOL_ADDRESS_P (&%0)");
1360         }
1361
1362       if (i == SYMBOL_REF)
1363         {
1364           /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1365              holds.  */
1366           type_p field_tp = find_structure ("block_symbol", TYPE_STRUCT);
1367           subfields
1368             = create_optional_field (subfields, field_tp, "block_sym",
1369                                      "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1370         }
1371
1372       sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1373       substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
1374                                  NULL, NULL);
1375
1376       ftag = xstrdup (rtx_name[i]);
1377       for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1378         ftag[nmindex] = TOUPPER (ftag[nmindex]);
1379       flds = create_field (flds, substruct, "");
1380       flds->opt = create_string_option (nodot, "tag", ftag);
1381     }
1382   return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
1383                         nodot, NULL);
1384 }
1385
1386 /* Handle `special("tree_exp")'.  This is a special case for
1387    field `operands' of struct tree_exp, which although it claims to contain
1388    pointers to trees, actually sometimes contains pointers to RTL too.
1389    Passed T, the old type of the field, and OPT its options.  Returns
1390    a new type for the field.  */
1391
1392 static type_p
1393 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1394 {
1395   pair_p flds;
1396   options_p nodot;
1397
1398   if (t->kind != TYPE_ARRAY)
1399     {
1400       error_at_line (&lexer_line,
1401                      "special `tree_exp' must be applied to an array");
1402       return &string_type;
1403     }
1404
1405   nodot = create_string_option (NULL, "dot", "");
1406
1407   flds = create_field (NULL, t, "");
1408   flds->opt = create_string_option (nodot, "length",
1409                                     "TREE_OPERAND_LENGTH ((tree) &%0)");
1410   flds->opt = create_string_option (flds->opt, "default", "");
1411
1412   return new_structure ("tree_exp_subunion", TYPE_UNION, &lexer_line, flds,
1413                         nodot, NULL);
1414 }
1415
1416 /* Perform any special processing on a type T, about to become the type
1417    of a field.  Return the appropriate type for the field.
1418    At present:
1419    - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1420    - Similarly for arrays of pointer-to-char;
1421    - Converts structures for which a parameter is provided to
1422      TYPE_PARAM_STRUCT;
1423    - Handles "special" options.
1424 */
1425
1426 type_p
1427 adjust_field_type (type_p t, options_p opt)
1428 {
1429   int length_p = 0;
1430   const int pointer_p = t->kind == TYPE_POINTER;
1431
1432   for (; opt; opt = opt->next)
1433     if (strcmp (opt->name, "length") == 0)
1434       {
1435         if (length_p)
1436           error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1437         if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
1438           {
1439             error_at_line (&lexer_line,
1440                            "option `%s' may not be applied to "
1441                            "arrays of atomic types", opt->name);
1442           }
1443         length_p = 1;
1444       }
1445     else if (strcmp (opt->name, "special") == 0
1446              && opt->kind == OPTION_STRING)
1447       {
1448         const char *special_name = opt->info.string;
1449         if (strcmp (special_name, "tree_exp") == 0)
1450           t = adjust_field_tree_exp (t, opt);
1451         else if (strcmp (special_name, "rtx_def") == 0)
1452           t = adjust_field_rtx_def (t, opt);
1453         else
1454           error_at_line (&lexer_line, "unknown special `%s'", special_name);
1455       }
1456
1457   if (!length_p
1458       && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1459     return &string_type;
1460   if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1461       && t->u.a.p->u.p->kind == TYPE_SCALAR
1462       && t->u.a.p->u.p->u.scalar_is_char)
1463     return create_array (&string_type, t->u.a.len);
1464
1465   return t;
1466 }
1467 \f
1468
1469 static void set_gc_used_type (type_p, enum gc_used_enum, bool = false);
1470 static void set_gc_used (pair_p);
1471
1472 /* Handle OPT for set_gc_used_type.  */
1473
1474 static void
1475 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1476                     int *length, int *skip, type_p *nested_ptr)
1477 {
1478   options_p o;
1479   for (o = opt; o; o = o->next)
1480     if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1481         && o->kind == OPTION_TYPE)
1482       set_gc_used_type (o->info.type,
1483                         GC_POINTED_TO);
1484     else if (strcmp (o->name, "maybe_undef") == 0)
1485       *maybe_undef = 1;
1486     else if (strcmp (o->name, "length") == 0)
1487       *length = 1;
1488     else if (strcmp (o->name, "skip") == 0)
1489       *skip = 1;
1490     else if (strcmp (o->name, "nested_ptr") == 0
1491              && o->kind == OPTION_NESTED)
1492       *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1493 }
1494
1495
1496 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1497
1498    If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1499    are set to GC_UNUSED.  Otherwise, an error is emitted for
1500    TYPE_UNDEFINED types.  This is used to support user-defined
1501    template types with non-type arguments.
1502
1503    For instance, when we parse a template type with enum arguments
1504    (e.g. MyType<AnotherType, EnumValue>), the parser created two
1505    artificial fields for 'MyType', one for 'AnotherType', the other
1506    one for 'EnumValue'.
1507
1508    At the time that we parse this type we don't know that 'EnumValue'
1509    is really an enum value, so the parser creates a TYPE_UNDEFINED
1510    type for it.  Since 'EnumValue' is never resolved to a known
1511    structure, it will stay with TYPE_UNDEFINED.
1512
1513    Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1514    'EnumValue'.  Generating marking code for it would cause
1515    compilation failures since the marking routines assumes that
1516    'EnumValue' is a type.  */
1517
1518 static void
1519 set_gc_used_type (type_p t, enum gc_used_enum level,
1520                   bool allow_undefined_types)
1521 {
1522   if (t->gc_used >= level)
1523     return;
1524
1525   t->gc_used = level;
1526
1527   switch (t->kind)
1528     {
1529     case TYPE_STRUCT:
1530     case TYPE_UNION:
1531     case TYPE_USER_STRUCT:
1532       {
1533         pair_p f;
1534         int dummy;
1535         type_p dummy2;
1536         bool allow_undefined_field_types = (t->kind == TYPE_USER_STRUCT);
1537
1538         process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy,
1539                             &dummy2);
1540
1541         if (t->u.s.base_class)
1542           set_gc_used_type (t->u.s.base_class, level, allow_undefined_types);
1543         /* Anything pointing to a base class might actually be pointing
1544            to a subclass.  */
1545         for (type_p subclass = t->u.s.first_subclass; subclass;
1546              subclass = subclass->u.s.next_sibling_class)
1547           set_gc_used_type (subclass, level, allow_undefined_types);
1548
1549         FOR_ALL_INHERITED_FIELDS(t, f)
1550           {
1551             int maybe_undef = 0;
1552             int length = 0;
1553             int skip = 0;
1554             type_p nested_ptr = NULL;
1555             process_gc_options (f->opt, level, &maybe_undef, &length, &skip,
1556                                 &nested_ptr);
1557
1558             if (nested_ptr && f->type->kind == TYPE_POINTER)
1559               set_gc_used_type (nested_ptr, GC_POINTED_TO);
1560             else if (length && f->type->kind == TYPE_POINTER)
1561               set_gc_used_type (f->type->u.p, GC_USED);
1562             else if (maybe_undef && f->type->kind == TYPE_POINTER)
1563               set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO);
1564             else if (skip)
1565               ;                 /* target type is not used through this field */
1566             else
1567               set_gc_used_type (f->type, GC_USED, allow_undefined_field_types);
1568           }
1569         break;
1570       }
1571
1572     case TYPE_UNDEFINED:
1573       if (level > GC_UNUSED)
1574         {
1575           if (!allow_undefined_types)
1576             error_at_line (&t->u.s.line, "undefined type `%s'", t->u.s.tag);
1577           t->gc_used = GC_UNUSED;
1578         }
1579       break;
1580
1581     case TYPE_POINTER:
1582       set_gc_used_type (t->u.p, GC_POINTED_TO);
1583       break;
1584
1585     case TYPE_ARRAY:
1586       set_gc_used_type (t->u.a.p, GC_USED);
1587       break;
1588
1589     case TYPE_LANG_STRUCT:
1590       for (t = t->u.s.lang_struct; t; t = t->next)
1591         set_gc_used_type (t, level);
1592       break;
1593
1594     default:
1595       break;
1596     }
1597 }
1598
1599 /* Set the gc_used fields of all the types pointed to by VARIABLES.  */
1600
1601 static void
1602 set_gc_used (pair_p variables)
1603 {
1604   int nbvars = 0;
1605   pair_p p;
1606   for (p = variables; p; p = p->next)
1607     {
1608       set_gc_used_type (p->type, GC_USED);
1609       nbvars++;
1610     };
1611   if (verbosity_level >= 2)
1612     printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1613 }
1614 \f
1615 /* File mapping routines.  For each input file, there is one output .c file
1616    (but some output files have many input files), and there is one .h file
1617    for the whole build.  */
1618
1619 /* Output file handling.  */
1620
1621 /* Create and return an outf_p for a new file for NAME, to be called
1622    ONAME.  */
1623
1624 static outf_p
1625 create_file (const char *name, const char *oname)
1626 {
1627   static const char *const hdr[] = {
1628     "   Copyright (C) 2004-2014 Free Software Foundation, Inc.\n",
1629     "\n",
1630     "This file is part of GCC.\n",
1631     "\n",
1632     "GCC is free software; you can redistribute it and/or modify it under\n",
1633     "the terms of the GNU General Public License as published by the Free\n",
1634     "Software Foundation; either version 3, or (at your option) any later\n",
1635     "version.\n",
1636     "\n",
1637     "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1638     "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1639     "FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\n",
1640     "for more details.\n",
1641     "\n",
1642     "You should have received a copy of the GNU General Public License\n",
1643     "along with GCC; see the file COPYING3.  If not see\n",
1644     "<http://www.gnu.org/licenses/>.  */\n",
1645     "\n",
1646     "/* This file is machine generated.  Do not edit.  */\n"
1647   };
1648   outf_p f;
1649   size_t i;
1650
1651   gcc_assert (name != NULL);
1652   gcc_assert (oname != NULL);
1653   f = XCNEW (struct outf);
1654   f->next = output_files;
1655   f->name = oname;
1656   output_files = f;
1657
1658   oprintf (f, "/* Type information for %s.\n", name);
1659   for (i = 0; i < ARRAY_SIZE (hdr); i++)
1660     oprintf (f, "%s", hdr[i]);
1661   return f;
1662 }
1663
1664 /* Print, like fprintf, to O.
1665    N.B. You might think this could be implemented more efficiently
1666    with vsnprintf().  Unfortunately, there are C libraries that
1667    provide that function but without the C99 semantics for its return
1668    value, making it impossible to know how much space is required.  */
1669 void
1670 oprintf (outf_p o, const char *format, ...)
1671 {
1672   char *s;
1673   size_t slength;
1674   va_list ap;
1675
1676   /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1677      in that case.  */
1678   if (!o)
1679     return;
1680
1681   va_start (ap, format);
1682   slength = vasprintf (&s, format, ap);
1683   if (s == NULL || (int) slength < 0)
1684     fatal ("out of memory");
1685   va_end (ap);
1686
1687   if (o->bufused + slength > o->buflength)
1688     {
1689       size_t new_len = o->buflength;
1690       if (new_len == 0)
1691         new_len = 1024;
1692       do
1693         {
1694           new_len *= 2;
1695         }
1696       while (o->bufused + slength >= new_len);
1697       o->buf = XRESIZEVEC (char, o->buf, new_len);
1698       o->buflength = new_len;
1699     }
1700   memcpy (o->buf + o->bufused, s, slength);
1701   o->bufused += slength;
1702   free (s);
1703 }
1704
1705 /* Open the global header file and the language-specific header files.  */
1706
1707 static void
1708 open_base_files (void)
1709 {
1710   size_t i;
1711
1712   if (nb_plugin_files > 0 && plugin_files)
1713     return;
1714
1715   header_file = create_file ("GCC", "gtype-desc.h");
1716
1717   base_files = XNEWVEC (outf_p, num_lang_dirs);
1718
1719   for (i = 0; i < num_lang_dirs; i++)
1720     base_files[i] = create_file (lang_dir_names[i],
1721                                  xasprintf ("gtype-%s.h", lang_dir_names[i]));
1722
1723   /* gtype-desc.c is a little special, so we create it here.  */
1724   {
1725     /* The order of files here matters very much.  */
1726     static const char *const ifiles[] = {
1727       "config.h", "system.h", "coretypes.h", "tm.h", "insn-codes.h",
1728       "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1729       "tree.h", "rtl.h", "wide-int.h", "hashtab.h", "hash-set.h", "vec.h",
1730       "machmode.h", "tm.h", "hard-reg-set.h", "input.h", "predict.h",
1731       "function.h", "insn-config.h", "expr.h", "alloc-pool.h",
1732       "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1733       "optabs.h", "libfuncs.h", "debug.h", "ggc.h", 
1734       "hash-table.h", "vec.h", "ggc.h", "dominance.h", "cfg.h", "basic-block.h",
1735       "tree-ssa-alias.h", "internal-fn.h", "gimple-fold.h", "tree-eh.h",
1736       "gimple-expr.h", "is-a.h",
1737       "gimple.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
1738       "tree-phinodes.h", "ssa-iterators.h", "stringpool.h", "tree-ssanames.h",
1739       "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h", "tree-ssa-loop-manip.h",
1740       "tree-ssa-loop-niter.h", "tree-into-ssa.h", "tree-dfa.h", 
1741       "tree-ssa.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1742       "except.h", "output.h",  "cfgloop.h", "target.h", "lto-streamer.h",
1743       "target-globals.h", "ipa-ref.h", "cgraph.h", "symbol-summary.h",
1744       "ipa-prop.h", "ipa-inline.h", "dwarf2out.h", "omp-low.h", NULL
1745     };
1746     const char *const *ifp;
1747     outf_p gtype_desc_c;
1748
1749     gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1750     for (ifp = ifiles; *ifp; ifp++)
1751       oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1752
1753     /* Make sure we handle "cfun" specially.  */
1754     oprintf (gtype_desc_c, "\n/* See definition in function.h.  */\n");
1755     oprintf (gtype_desc_c, "#undef cfun\n");
1756
1757     oprintf (gtype_desc_c,
1758              "\n"
1759              "/* Types with a \"gcc::\" namespace have it stripped\n"
1760              "   during gengtype parsing.  Provide a \"using\" directive\n"
1761              "   to ensure that the fully-qualified types are found.  */\n"
1762              "using namespace gcc;\n");
1763   }
1764 }
1765
1766 /* For INPF an input file, return the real basename of INPF, with all
1767    the directory components skipped.  */
1768
1769 static const char *
1770 get_file_realbasename (const input_file *inpf)
1771 {
1772   return lbasename (get_input_file_name (inpf));
1773 }
1774
1775 /* For INPF a filename, return the relative path to INPF from
1776    $(srcdir) if the latter is a prefix in INPF, NULL otherwise.  */
1777
1778 const char *
1779 get_file_srcdir_relative_path (const input_file *inpf)
1780 {
1781   const char *f = get_input_file_name (inpf);
1782   if (strlen (f) > srcdir_len
1783       && IS_DIR_SEPARATOR (f[srcdir_len])
1784       && strncmp (f, srcdir, srcdir_len) == 0)
1785     return f + srcdir_len + 1;
1786   else
1787     return NULL;
1788 }
1789
1790 /*  For INPF an input_file, return the relative path to INPF from
1791     $(srcdir) if the latter is a prefix in INPF, or the real basename
1792     of INPF otherwise. */
1793
1794 static const char *
1795 get_file_basename (const input_file *inpf)
1796 {
1797   const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1798
1799   return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1800 }
1801
1802 /* For F a filename, return the lang_dir_names relative index of the language
1803    directory that is a prefix in F, if any, -1 otherwise.  */
1804
1805 static int
1806 get_prefix_langdir_index (const char *f)
1807 {
1808   size_t f_len = strlen (f);
1809   size_t lang_index;
1810
1811   for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1812     {
1813       const char *langdir = lang_dir_names[lang_index];
1814       size_t langdir_len = strlen (langdir);
1815
1816       if (f_len > langdir_len
1817           && IS_DIR_SEPARATOR (f[langdir_len])
1818           && memcmp (f, langdir, langdir_len) == 0)
1819         return lang_index;
1820     }
1821
1822   return -1;
1823 }
1824
1825 /* For INPF an input file, return the name of language directory where
1826    F is located, if any, NULL otherwise.  */
1827
1828 static const char *
1829 get_file_langdir (const input_file *inpf)
1830 {
1831   /* Get the relative path to INPF from $(srcdir) and find the
1832      language by comparing the prefix with language directory names.
1833      If INPF is not even srcdir relative, no point in looking
1834      further.  */
1835
1836   int lang_index;
1837   const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1838   const char *r;
1839
1840   if (!srcdir_relative_path)
1841     return NULL;
1842
1843   lang_index = get_prefix_langdir_index (srcdir_relative_path);
1844   if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1845     r = "c-family";
1846   else if (lang_index >= 0)
1847     r = lang_dir_names[lang_index];
1848   else
1849     r = NULL;
1850
1851   return r;
1852 }
1853
1854 /* The gt- output file name for INPF.  */
1855
1856 static const char *
1857 get_file_gtfilename (const input_file *inpf)
1858 {
1859   /* Cook up an initial version of the gt- file name from the file real
1860      basename and the language name, if any.  */
1861
1862   const char *basename = get_file_realbasename (inpf);
1863   const char *langdir = get_file_langdir (inpf);
1864
1865   char *result =
1866     (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1867      : xasprintf ("gt-%s", basename));
1868
1869   /* Then replace all non alphanumerics characters by '-' and change the
1870      extension to ".h".  We expect the input filename extension was at least
1871      one character long.  */
1872
1873   char *s = result;
1874
1875   for (; *s != '.'; s++)
1876     if (!ISALNUM (*s) && *s != '-')
1877       *s = '-';
1878
1879   memcpy (s, ".h", sizeof (".h"));
1880
1881   return result;
1882 }
1883
1884 /* Each input_file has its associated output file outf_p.  The
1885    association is computed by the function
1886    get_output_file_with_visibility.  The associated file is cached
1887    inside input_file in its inpoutf field, so is really computed only
1888    once.  Associated output file paths (i.e. output_name-s) are
1889    computed by a rule based regexp machinery, using the files_rules
1890    array of struct file_rule_st.  A for_name is also computed, giving
1891    the source file name for which the output_file is generated; it is
1892    often the last component of the input_file path.  */
1893
1894
1895 /*
1896  Regexpr machinery to compute the output_name and for_name-s of each
1897  input_file.  We have a sequence of file rules which gives the POSIX
1898  extended regular expression to match an input file path, and two
1899  transformed strings for the corresponding output_name and the
1900  corresponding for_name.  The transformed string contain dollars: $0
1901  is replaced by the entire match, $1 is replaced by the substring
1902  matching the first parenthesis in the regexp, etc.  And $$ is replaced
1903  by a single verbatim dollar.  The rule order is important.  The
1904  general case is last, and the particular cases should come before.
1905  An action routine can, when needed, update the out_name & for_name
1906  and/or return the appropriate output file.  It is invoked only when a
1907  rule is triggered.  When a rule is triggered, the output_name and
1908  for_name are computed using their transform string in while $$, $0,
1909  $1, ... are suitably replaced.  If there is an action, it is called.
1910  In some few cases, the action can directly return the outf_p, but
1911  usually it just updates the output_name and for_name so should free
1912  them before replacing them.  The get_output_file_with_visibility
1913  function creates an outf_p only once per each output_name, so it
1914  scans the output_files list for previously seen output file names.
1915  */
1916
1917 /* Signature of actions in file rules.  */
1918 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
1919
1920
1921 struct file_rule_st {
1922   const char* frul_srcexpr;     /* Source string for regexp.  */
1923   int frul_rflags;              /* Flags passed to regcomp, usually
1924                                  * REG_EXTENDED.  */
1925   regex_t* frul_re;             /* Compiled regular expression
1926                                    obtained by regcomp.  */
1927   const char* frul_tr_out;      /* Transformation string for making
1928                                  * the output_name, with $1 ... $9 for
1929                                  * subpatterns and $0 for the whole
1930                                  * matched filename.  */
1931   const char* frul_tr_for;      /* Tranformation string for making the
1932                                    for_name.  */
1933   frul_actionrout_t* frul_action; /* The action, if non null, is
1934                                    * called once the rule matches, on
1935                                    * the transformed out_name &
1936                                    * for_name.  It could change them
1937                                    * and/or give the output file.  */
1938 };
1939
1940 /* File rule action handling *.h files.  */
1941 static outf_p header_dot_h_frul (input_file*, char**, char**);
1942
1943 /* File rule action handling *.c files.  */
1944 static outf_p source_dot_c_frul (input_file*, char**, char**);
1945
1946 #define NULL_REGEX (regex_t*)0
1947
1948 /* The prefix in our regexp-s matching the directory.  */
1949 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1950
1951 #define NULL_FRULACT (frul_actionrout_t*)0
1952
1953 /* The array of our rules governing file name generation.  Rules order
1954    matters, so change with extreme care!  */
1955
1956 struct file_rule_st files_rules[] = {
1957   /* The general rule assumes that files in subdirectories belong to a
1958      particular front-end, and files not in subdirectories are shared.
1959      The following rules deal with exceptions - files that are in
1960      subdirectories and yet are shared, and files that are top-level,
1961      but are not shared.  */
1962
1963   /* the c-family/ source directory is special.  */
1964   { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
1965     REG_EXTENDED, NULL_REGEX,
1966     "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
1967
1968   { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
1969     REG_EXTENDED, NULL_REGEX,
1970     "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
1971
1972   /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c !  */
1973   { DIR_PREFIX_REGEX "c/c-lang\\.h$",
1974     REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1975
1976   { DIR_PREFIX_REGEX "c/c-tree\\.h$",
1977     REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1978
1979   /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c !  */
1980   { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
1981     REG_EXTENDED, NULL_REGEX,
1982     "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
1983
1984   /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c !  */
1985   { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
1986     REG_EXTENDED, NULL_REGEX,
1987     "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
1988
1989   /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c !  */
1990   { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
1991     REG_EXTENDED, NULL_REGEX,
1992     "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
1993
1994   /* cp/parser.h gives gt-cp-parser.h for cp/parser.c !  */
1995   { DIR_PREFIX_REGEX "cp/parser\\.h$",
1996     REG_EXTENDED, NULL_REGEX,
1997     "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
1998
1999   /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c !  */
2000   { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
2001     REG_EXTENDED, NULL_REGEX,
2002     "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
2003
2004   /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c !  */
2005   { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
2006     REG_EXTENDED, NULL_REGEX,
2007     "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
2008
2009   /* General cases.  For header *.h and source *.c or *.cc files, we
2010    * need special actions to handle the language.  */
2011
2012   /* Source *.c files are using get_file_gtfilename to compute their
2013      output_name and get_file_basename to compute their for_name
2014      through the source_dot_c_frul action.  */
2015   { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
2016     REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
2017
2018   /* Source *.cc files are using get_file_gtfilename to compute their
2019      output_name and get_file_basename to compute their for_name
2020      through the source_dot_c_frul action.  */
2021   { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.cc$",
2022     REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.cc", source_dot_c_frul},
2023
2024   /* Common header files get "gtype-desc.c" as their output_name,
2025    * while language specific header files are handled specially.  So
2026    * we need the header_dot_h_frul action.  */
2027   { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
2028     REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
2029
2030   { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
2031     REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
2032
2033   /* Mandatory null last entry signaling end of rules.  */
2034   {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
2035 };
2036
2037 /* Special file rules action for handling *.h header files.  It gives
2038    "gtype-desc.c" for common headers and corresponding output
2039    files for language-specific header files.  */
2040 static outf_p
2041 header_dot_h_frul (input_file* inpf, char**poutname,
2042                    char**pforname ATTRIBUTE_UNUSED)
2043 {
2044   const char *basename = 0;
2045   int lang_index = 0;
2046   DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2047              (void*) inpf, get_input_file_name (inpf),
2048              *poutname, *pforname);
2049   basename = get_file_basename (inpf);
2050   lang_index = get_prefix_langdir_index (basename);
2051   DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
2052
2053   if (lang_index >= 0)
2054     {
2055       /* The header is language specific.  Given output_name &
2056          for_name remains unchanged.  The base_files array gives the
2057          outf_p.  */
2058       DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2059                  (void*) base_files[lang_index],
2060                  (base_files[lang_index])->name);
2061       return base_files[lang_index];
2062     }
2063   else
2064     {
2065       /* The header is common to all front-end languages.  So
2066          output_name is "gtype-desc.c" file.  The calling function
2067          get_output_file_with_visibility will find its outf_p.  */
2068       free (*poutname);
2069       *poutname = xstrdup ("gtype-desc.c");
2070       DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2071                  get_input_file_name (inpf));
2072       return NULL;
2073     }
2074 }
2075
2076
2077 /* Special file rules action for handling *.c source files using
2078  * get_file_gtfilename to compute their output_name and
2079  * get_file_basename to compute their for_name.  The output_name is
2080  * gt-<LANG>-<BASE>.h for language specific source files, and
2081  * gt-<BASE>.h for common source files.  */
2082 static outf_p
2083 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
2084 {
2085   char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
2086   char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
2087   DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2088              (void*) inpf, get_input_file_name (inpf),
2089              *poutname, *pforname);
2090   DBGPRINTF ("newoutname %s", newoutname);
2091   DBGPRINTF ("newbasename %s", newbasename);
2092   free (*poutname);
2093   free (*pforname);
2094   *poutname = newoutname;
2095   *pforname = newbasename;
2096   return NULL;
2097 }
2098
2099 /* Utility function for get_output_file_with_visibility which returns
2100  * a malloc-ed substituted string using TRS on matching of the FILNAM
2101  * file name, using the PMATCH array.  */
2102 static char*
2103 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
2104                                const char *trs)
2105 {
2106   struct obstack str_obstack;
2107   char *str = NULL;
2108   char *rawstr = NULL;
2109   const char *pt = NULL;
2110   DBGPRINTF ("filnam %s", filnam);
2111   obstack_init (&str_obstack);
2112   for (pt = trs; *pt; pt++) {
2113     char c = *pt;
2114     if (c == '$')
2115       {
2116         if (pt[1] == '$')
2117           {
2118             /* A double dollar $$ is substituted by a single verbatim
2119                dollar, but who really uses dollar signs in file
2120                paths? */
2121             obstack_1grow (&str_obstack, '$');
2122           }
2123         else if (ISDIGIT (pt[1]))
2124           {
2125             /* Handle $0 $1 ... $9 by appropriate substitution.  */
2126             int dolnum = pt[1] - '0';
2127             int so = pmatch[dolnum].rm_so;
2128             int eo = pmatch[dolnum].rm_eo;
2129             DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2130             if (so>=0 && eo>=so)
2131               obstack_grow (&str_obstack, filnam + so, eo - so);
2132           }
2133         else
2134           {
2135             /* This can happen only when files_rules is buggy! */
2136             gcc_unreachable ();
2137           }
2138         /* Always skip the character after the dollar.  */
2139         pt++;
2140       }
2141     else
2142       obstack_1grow (&str_obstack, c);
2143   }
2144   obstack_1grow (&str_obstack, '\0');
2145   rawstr = XOBFINISH (&str_obstack, char *);
2146   str = xstrdup (rawstr);
2147   obstack_free (&str_obstack, NULL);
2148   DBGPRINTF ("matched replacement %s", str);
2149   rawstr = NULL;
2150   return str;
2151 }
2152
2153
2154 /* An output file, suitable for definitions, that can see declarations
2155    made in INPF and is linked into every language that uses INPF.
2156    Since the result is cached inside INPF, that argument cannot be
2157    declared constant, but is "almost" constant. */
2158
2159 outf_p
2160 get_output_file_with_visibility (input_file *inpf)
2161 {
2162   outf_p r;
2163   char *for_name = NULL;
2164   char *output_name = NULL;
2165   const char* inpfname;
2166
2167   /* This can happen when we need a file with visibility on a
2168      structure that we've never seen.  We have to just hope that it's
2169      globally visible.  */
2170   if (inpf == NULL)
2171     inpf = system_h_file;
2172
2173   /* The result is cached in INPF, so return it if already known.  */
2174   if (inpf->inpoutf)
2175     return inpf->inpoutf;
2176
2177   /* In plugin mode, return NULL unless the input_file is one of the
2178      plugin_files.  */
2179   if (plugin_files)
2180     {
2181       size_t i;
2182       for (i = 0; i < nb_plugin_files; i++)
2183         if (inpf == plugin_files[i]) 
2184           {
2185             inpf->inpoutf = plugin_output;
2186             return plugin_output;
2187           }
2188
2189       return NULL;
2190     }
2191
2192   inpfname = get_input_file_name (inpf);
2193
2194   /* Try each rule in sequence in files_rules until one is triggered. */
2195   {
2196     int rulix = 0;
2197     DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2198                (void*) inpf, inpfname);
2199
2200     for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2201       {
2202         DBGPRINTF ("rulix#%d srcexpr %s",
2203                    rulix, files_rules[rulix].frul_srcexpr);
2204
2205         if (!files_rules[rulix].frul_re)
2206           {
2207             /* Compile the regexpr lazily.  */
2208             int err = 0;
2209             files_rules[rulix].frul_re = XCNEW (regex_t);
2210             err = regcomp (files_rules[rulix].frul_re,
2211                            files_rules[rulix].frul_srcexpr,
2212                            files_rules[rulix].frul_rflags);
2213             if (err)
2214               {
2215                 /* The regular expression compilation fails only when
2216                    file_rules is buggy.  */
2217                 gcc_unreachable ();
2218               }
2219           }
2220
2221         output_name = NULL;
2222         for_name = NULL;
2223
2224         /* Match the regexpr and trigger the rule if matched.  */
2225         {
2226           /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2227              $3, ... $9.  */
2228           regmatch_t pmatch[10];
2229           memset (pmatch, 0, sizeof (pmatch));
2230           if (!regexec (files_rules[rulix].frul_re,
2231                         inpfname, 10, pmatch, 0))
2232             {
2233               DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2234                          (void*) inpf, inpfname, rulix,
2235                          files_rules[rulix].frul_srcexpr);
2236               for_name =
2237                 matching_file_name_substitute (inpfname, pmatch,
2238                                                files_rules[rulix].frul_tr_for);
2239               DBGPRINTF ("for_name %s", for_name);
2240               output_name =
2241                 matching_file_name_substitute (inpfname, pmatch,
2242                                                files_rules[rulix].frul_tr_out);
2243               DBGPRINTF ("output_name %s", output_name);
2244               if (files_rules[rulix].frul_action)
2245                 {
2246                   /* Invoke our action routine.  */
2247                   outf_p of = NULL;
2248                   DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2249                              rulix, output_name, for_name);
2250                   of =
2251                     (files_rules[rulix].frul_action) (inpf,
2252                                                       &output_name, &for_name);
2253                   DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2254                              rulix, (void*)of, output_name, for_name);
2255                   /* If the action routine returned something, give it back
2256                      immediately and cache it in inpf.  */
2257                   if (of)
2258                     {
2259                       inpf->inpoutf = of;
2260                       return of;
2261                     }
2262                 }
2263               /* The rule matched, and had no action, or that action did
2264                  not return any output file but could have changed the
2265                  output_name or for_name.  We break out of the loop on the
2266                  files_rules.  */
2267               break;
2268             }
2269           else
2270             {
2271               /* The regexpr did not match.  */
2272               DBGPRINTF ("rulix#%d did not match %s pattern %s",
2273                          rulix, inpfname, files_rules[rulix].frul_srcexpr);
2274               continue;
2275             }
2276         }
2277       }
2278   }
2279   if (!output_name || !for_name)
2280     {
2281       /* This should not be possible, and could only happen if the
2282          files_rules is incomplete or buggy.  */
2283       fatal ("failed to compute output name for %s", inpfname);
2284     }
2285
2286   /* Look through to see if we've ever seen this output filename
2287      before.  If found, cache the result in inpf.  */
2288   for (r = output_files; r; r = r->next)
2289     if (filename_cmp (r->name, output_name) == 0)
2290       {
2291         inpf->inpoutf = r;
2292         DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2293                    output_name, for_name);
2294         return r;
2295       }
2296
2297   /* If not found, create it, and cache it in inpf.  */
2298   r = create_file (for_name, output_name);
2299
2300   gcc_assert (r && r->name);
2301   DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2302              output_name, for_name);
2303   inpf->inpoutf = r;
2304   return r;
2305
2306
2307 }
2308
2309 /* The name of an output file, suitable for definitions, that can see
2310    declarations made in INPF and is linked into every language that
2311    uses INPF.  */
2312
2313 const char *
2314 get_output_file_name (input_file* inpf)
2315 {
2316   outf_p o = get_output_file_with_visibility (inpf);
2317   if (o)
2318     return o->name;
2319   return NULL;
2320 }
2321
2322 /* Check if existing file is equal to the in memory buffer. */
2323
2324 static bool
2325 is_file_equal (outf_p of)
2326 {
2327   FILE *newfile = fopen (of->name, "r");
2328   size_t i;
2329   bool equal;
2330   if (newfile == NULL)
2331     return false;
2332
2333   equal = true;
2334   for (i = 0; i < of->bufused; i++)
2335     {
2336       int ch;
2337       ch = fgetc (newfile);
2338       if (ch == EOF || ch != (unsigned char) of->buf[i])
2339         {
2340           equal = false;
2341           break;
2342         }
2343     }
2344   if (equal && EOF != fgetc (newfile))
2345     equal = false;
2346   fclose (newfile);
2347   return equal;
2348 }
2349
2350 /* Copy the output to its final destination,
2351    but don't unnecessarily change modification times.  */
2352
2353 static void
2354 close_output_files (void)
2355 {
2356   int nbwrittenfiles = 0;
2357   outf_p of;
2358
2359   for (of = output_files; of; of = of->next)
2360     {
2361       if (!is_file_equal (of))
2362         {
2363           FILE *newfile = NULL;
2364           char *backupname = NULL;
2365           /* Back up the old version of the output file gt-FOO.c as
2366              BACKUPDIR/gt-FOO.c~ if we have a backup directory.  */
2367           if (backup_dir)
2368             {
2369               backupname = concat (backup_dir, "/",
2370                                    lbasename (of->name), "~", NULL);
2371               if (!access (of->name, F_OK) && rename (of->name, backupname))
2372                 fatal ("failed to back up %s as %s: %s",
2373                        of->name, backupname, xstrerror (errno));
2374             }
2375
2376           newfile = fopen (of->name, "w");
2377           if (newfile == NULL)
2378             fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2379           if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2380             fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2381           if (fclose (newfile) != 0)
2382             fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2383           nbwrittenfiles++;
2384           if (verbosity_level >= 2 && backupname)
2385             printf ("%s wrote #%-3d %s backed-up in %s\n",
2386                     progname, nbwrittenfiles, of->name, backupname);
2387           else if (verbosity_level >= 1)
2388             printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2389           free (backupname);
2390         }
2391       else 
2392         { 
2393           /* output file remains unchanged. */
2394           if (verbosity_level >= 2)
2395             printf ("%s keep %s\n", progname, of->name);
2396         }
2397       free (of->buf);
2398       of->buf = NULL;
2399       of->bufused = of->buflength = 0;
2400     }
2401   if (verbosity_level >= 1)
2402     printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2403 }
2404 \f
2405 struct flist
2406 {
2407   struct flist *next;
2408   int started_p;
2409   const input_file* file;
2410   outf_p f;
2411 };
2412
2413 struct walk_type_data;
2414
2415 /* For scalars and strings, given the item in 'val'.
2416    For structures, given a pointer to the item in 'val'.
2417    For misc. pointers, given the item in 'val'.
2418 */
2419 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2420 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2421
2422 /* Parameters for write_types.  */
2423
2424 struct write_types_data
2425 {
2426   const char *prefix;
2427   const char *param_prefix;
2428   const char *subfield_marker_routine;
2429   const char *marker_routine;
2430   const char *reorder_note_routine;
2431   const char *comment;
2432   int skip_hooks;               /* skip hook generation if non zero */
2433   enum write_types_kinds kind;
2434 };
2435
2436 static void output_escaped_param (struct walk_type_data *d,
2437                                   const char *, const char *);
2438 static void output_mangled_typename (outf_p, const_type_p);
2439 static void walk_type (type_p t, struct walk_type_data *d);
2440 static void write_func_for_structure (type_p orig_s, type_p s,
2441                                       const struct write_types_data *wtd);
2442 static void write_types_process_field
2443   (type_p f, const struct walk_type_data *d);
2444 static void write_types (outf_p output_header,
2445                          type_p structures,
2446                          const struct write_types_data *wtd);
2447 static void write_types_local_process_field
2448   (type_p f, const struct walk_type_data *d);
2449 static void write_local_func_for_structure (const_type_p orig_s, type_p s);
2450 static void write_local (outf_p output_header,
2451                          type_p structures);
2452 static int contains_scalar_p (type_p t);
2453 static void put_mangled_filename (outf_p, const input_file *);
2454 static void finish_root_table (struct flist *flp, const char *pfx,
2455                                const char *tname, const char *lastname,
2456                                const char *name);
2457 static void write_root (outf_p, pair_p, type_p, const char *, int,
2458                         struct fileloc *, bool);
2459 static void write_array (outf_p f, pair_p v,
2460                          const struct write_types_data *wtd);
2461 static void write_roots (pair_p, bool);
2462
2463 /* Parameters for walk_type.  */
2464
2465 struct walk_type_data
2466 {
2467   process_field_fn process_field;
2468   const void *cookie;
2469   outf_p of;
2470   options_p opt;
2471   const char *val;
2472   const char *prev_val[4];
2473   int indent;
2474   int counter;
2475   const struct fileloc *line;
2476   lang_bitmap bitmap;
2477   int used_length;
2478   type_p orig_s;
2479   const char *reorder_fn;
2480   bool needs_cast_p;
2481   bool fn_wants_lvalue;
2482   bool in_record_p;
2483   int loopcounter;
2484   bool in_ptr_field;
2485   bool have_this_obj;
2486 };
2487
2488
2489 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2490    pre-processor identifier to use in a #define directive.  This replaces
2491    special characters used in C++ identifiers like '>', '<' and ':' with
2492    '_'.
2493
2494    If no C++ special characters are found in TYPE_NAME, return
2495    TYPE_NAME.  Otherwise, return a copy of TYPE_NAME with the special
2496    characters replaced with '_'.  In this case, the caller is
2497    responsible for freeing the allocated string.  */
2498
2499 static const char *
2500 filter_type_name (const char *type_name)
2501 {
2502   if (strchr (type_name, '<') || strchr (type_name, ':'))
2503     {
2504       size_t i;
2505       char *s = xstrdup (type_name);
2506       for (i = 0; i < strlen (s); i++)
2507         if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ','
2508             || s[i] == '*')
2509           s[i] = '_';
2510       return s;
2511     }
2512   else
2513     return type_name;
2514 }
2515
2516
2517 /* Print a mangled name representing T to OF.  */
2518
2519 static void
2520 output_mangled_typename (outf_p of, const_type_p t)
2521 {
2522   if (t == NULL)
2523     oprintf (of, "Z");
2524   else
2525     switch (t->kind)
2526       {
2527       case TYPE_NONE:
2528       case TYPE_UNDEFINED:
2529         gcc_unreachable ();
2530         break;
2531       case TYPE_POINTER:
2532         oprintf (of, "P");
2533         output_mangled_typename (of, t->u.p);
2534         break;
2535       case TYPE_SCALAR:
2536         oprintf (of, "I");
2537         break;
2538       case TYPE_STRING:
2539         oprintf (of, "S");
2540         break;
2541       case TYPE_STRUCT:
2542       case TYPE_UNION:
2543       case TYPE_LANG_STRUCT:
2544       case TYPE_USER_STRUCT:
2545         {
2546           /* For references to classes within an inheritance hierarchy,
2547              only ever reference the ultimate base class, since only
2548              it will have gt_ functions.  */
2549           t = get_ultimate_base_class (t);
2550           const char *id_for_tag = filter_type_name (t->u.s.tag);
2551           oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2552                    id_for_tag);
2553           if (id_for_tag != t->u.s.tag)
2554             free (CONST_CAST (char *, id_for_tag));
2555         }
2556         break;
2557       case TYPE_ARRAY:
2558         gcc_unreachable ();
2559       }
2560 }
2561
2562 /* Print PARAM to D->OF processing escapes.  D->VAL references the
2563    current object, D->PREV_VAL the object containing the current
2564    object, ONAME is the name of the option and D->LINE is used to
2565    print error messages.  */
2566
2567 static void
2568 output_escaped_param (struct walk_type_data *d, const char *param,
2569                       const char *oname)
2570 {
2571   const char *p;
2572
2573   for (p = param; *p; p++)
2574     if (*p != '%')
2575       oprintf (d->of, "%c", *p);
2576     else
2577       switch (*++p)
2578         {
2579         case 'h':
2580           oprintf (d->of, "(%s)", d->prev_val[2]);
2581           break;
2582         case '0':
2583           oprintf (d->of, "(%s)", d->prev_val[0]);
2584           break;
2585         case '1':
2586           oprintf (d->of, "(%s)", d->prev_val[1]);
2587           break;
2588         case 'a':
2589           {
2590             const char *pp = d->val + strlen (d->val);
2591             while (pp[-1] == ']')
2592               while (*pp != '[')
2593                 pp--;
2594             oprintf (d->of, "%s", pp);
2595           }
2596           break;
2597         default:
2598           error_at_line (d->line, "`%s' option contains bad escape %c%c",
2599                          oname, '%', *p);
2600         }
2601 }
2602
2603 const char *
2604 get_string_option (options_p opt, const char *key)
2605 {
2606   for (; opt; opt = opt->next)
2607     if (strcmp (opt->name, key) == 0)
2608       return opt->info.string;
2609   return NULL;
2610 }
2611
2612 /* Machinery for avoiding duplicate tags within switch statements.  */
2613 struct seen_tag
2614 {
2615   const char *tag;
2616   struct seen_tag *next;
2617 };
2618
2619 int
2620 already_seen_tag (struct seen_tag *seen_tags, const char *tag)
2621 {
2622   /* Linear search, so O(n^2), but n is currently small.  */
2623   while (seen_tags)
2624     {
2625       if (!strcmp (seen_tags->tag, tag))
2626         return 1;
2627       seen_tags = seen_tags->next;
2628     }
2629   /* Not yet seen this tag. */
2630   return 0;
2631 }
2632
2633 void
2634 mark_tag_as_seen (struct seen_tag **seen_tags, const char *tag)
2635 {
2636   /* Add to front of linked list. */
2637   struct seen_tag *new_node = XCNEW (struct seen_tag);
2638   new_node->tag = tag;
2639   new_node->next = *seen_tags;
2640   *seen_tags = new_node;
2641 }
2642
2643 static void
2644 walk_subclasses (type_p base, struct walk_type_data *d,
2645                  struct seen_tag **seen_tags)
2646 {
2647   for (type_p sub = base->u.s.first_subclass; sub != NULL;
2648        sub = sub->u.s.next_sibling_class)
2649     {
2650       const char *type_tag = get_string_option (sub->u.s.opt, "tag");
2651       if (type_tag && !already_seen_tag (*seen_tags, type_tag))
2652         {
2653           mark_tag_as_seen (seen_tags, type_tag);
2654           oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
2655           d->indent += 2;
2656           oprintf (d->of, "%*s{\n", d->indent, "");
2657           d->indent += 2;
2658           oprintf (d->of, "%*s%s *sub = static_cast <%s *> (x);\n",
2659                    d->indent, "", sub->u.s.tag, sub->u.s.tag);
2660           const char *old_val = d->val;
2661           d->val = "(*sub)";
2662           walk_type (sub, d);
2663           d->val = old_val;
2664           d->indent -= 2;
2665           oprintf (d->of, "%*s}\n", d->indent, "");
2666           oprintf (d->of, "%*sbreak;\n", d->indent, "");
2667           d->indent -= 2;
2668         }
2669       walk_subclasses (sub, d, seen_tags);
2670     }
2671 }
2672
2673 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2674    which is of type T.  Write code to D->OF to constrain execution (at
2675    the point that D->PROCESS_FIELD is called) to the appropriate
2676    cases.  Call D->PROCESS_FIELD on subobjects before calling it on
2677    pointers to those objects.  D->PREV_VAL lists the objects
2678    containing the current object, D->OPT is a list of options to
2679    apply, D->INDENT is the current indentation level, D->LINE is used
2680    to print error messages, D->BITMAP indicates which languages to
2681    print the structure for.  */
2682
2683 static void
2684 walk_type (type_p t, struct walk_type_data *d)
2685 {
2686   const char *length = NULL;
2687   const char *desc = NULL;
2688   const char *type_tag = NULL;
2689   int maybe_undef_p = 0;
2690   int atomic_p = 0;
2691   options_p oo;
2692   const struct nested_ptr_data *nested_ptr_d = NULL;
2693
2694   d->needs_cast_p = false;
2695   for (oo = d->opt; oo; oo = oo->next)
2696     if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2697       length = oo->info.string;
2698     else if (strcmp (oo->name, "maybe_undef") == 0)
2699       maybe_undef_p = 1;
2700     else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2701       desc = oo->info.string;
2702     else if (strcmp (oo->name, "mark_hook") == 0)
2703       ;
2704     else if (strcmp (oo->name, "nested_ptr") == 0 
2705              && oo->kind == OPTION_NESTED)
2706       nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2707     else if (strcmp (oo->name, "dot") == 0)
2708       ;
2709     else if (strcmp (oo->name, "tag") == 0)
2710       type_tag = oo->info.string;
2711     else if (strcmp (oo->name, "special") == 0)
2712       ;
2713     else if (strcmp (oo->name, "skip") == 0)
2714       ;
2715     else if (strcmp (oo->name, "atomic") == 0)
2716       atomic_p = 1;
2717     else if (strcmp (oo->name, "default") == 0)
2718       ;
2719     else if (strcmp (oo->name, "chain_next") == 0)
2720       ;
2721     else if (strcmp (oo->name, "chain_prev") == 0)
2722       ;
2723     else if (strcmp (oo->name, "chain_circular") == 0)
2724       ;
2725     else if (strcmp (oo->name, "reorder") == 0)
2726       ;
2727     else if (strcmp (oo->name, "variable_size") == 0)
2728       ;
2729     else if (strcmp (oo->name, "for_user") == 0)
2730       ;
2731     else
2732       error_at_line (d->line, "unknown option `%s'\n", oo->name);
2733
2734   if (d->used_length)
2735     length = NULL;
2736
2737   if (maybe_undef_p
2738       && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
2739     {
2740       error_at_line (d->line,
2741                      "field `%s' has invalid option `maybe_undef_p'\n",
2742                      d->val);
2743       return;
2744     }
2745
2746   if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2747     {
2748       error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2749       return;
2750     }
2751
2752   switch (t->kind)
2753     {
2754     case TYPE_SCALAR:
2755     case TYPE_STRING:
2756       d->process_field (t, d);
2757       break;
2758
2759     case TYPE_POINTER:
2760       {
2761         d->in_ptr_field = true;
2762         if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2763           {
2764             oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2765             break;
2766           }
2767
2768         /* If a pointer type is marked as "atomic", we process the
2769            field itself, but we don't walk the data that they point to.
2770
2771            There are two main cases where we walk types: to mark
2772            pointers that are reachable, and to relocate pointers when
2773            writing a PCH file.  In both cases, an atomic pointer is
2774            itself marked or relocated, but the memory that it points
2775            to is left untouched.  In the case of PCH, that memory will
2776            be read/written unchanged to the PCH file.  */
2777         if (atomic_p)
2778           {
2779             oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2780             d->indent += 2;
2781             d->process_field (t, d);
2782             d->indent -= 2;
2783             oprintf (d->of, "%*s}\n", d->indent, "");
2784             break;
2785           }
2786
2787         if (!length)
2788           {
2789             if (!union_or_struct_p (t->u.p))
2790               {
2791                 error_at_line (d->line,
2792                                "field `%s' is pointer to unimplemented type",
2793                                d->val);
2794                 break;
2795               }
2796
2797             if (nested_ptr_d)
2798               {
2799                 const char *oldprevval2 = d->prev_val[2];
2800
2801                 if (!union_or_struct_p (nested_ptr_d->type))
2802                   {
2803                     error_at_line (d->line,
2804                                    "field `%s' has invalid "
2805                                    "option `nested_ptr'\n", d->val);
2806                     return;
2807                   }
2808
2809                 d->prev_val[2] = d->val;
2810                 oprintf (d->of, "%*s{\n", d->indent, "");
2811                 d->indent += 2;
2812                 d->val = xasprintf ("x%d", d->counter++);
2813                 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2814                          (nested_ptr_d->type->kind == TYPE_UNION
2815                           ? "union" : "struct"),
2816                          nested_ptr_d->type->u.s.tag,
2817                          d->fn_wants_lvalue ? "" : "const ", d->val);
2818                 oprintf (d->of, "%*s", d->indent + 2, "");
2819                 output_escaped_param (d, nested_ptr_d->convert_from,
2820                                       "nested_ptr");
2821                 oprintf (d->of, ";\n");
2822
2823                 d->process_field (nested_ptr_d->type, d);
2824
2825                 if (d->fn_wants_lvalue)
2826                   {
2827                     oprintf (d->of, "%*s%s = ", d->indent, "",
2828                              d->prev_val[2]);
2829                     d->prev_val[2] = d->val;
2830                     output_escaped_param (d, nested_ptr_d->convert_to,
2831                                           "nested_ptr");
2832                     oprintf (d->of, ";\n");
2833                   }
2834
2835                 d->indent -= 2;
2836                 oprintf (d->of, "%*s}\n", d->indent, "");
2837                 d->val = d->prev_val[2];
2838                 d->prev_val[2] = oldprevval2;
2839               }
2840             else
2841               d->process_field (t->u.p, d);
2842           }
2843         else
2844           {
2845             int loopcounter = d->loopcounter;
2846             const char *oldval = d->val;
2847             const char *oldprevval3 = d->prev_val[3];
2848             char *newval;
2849
2850             oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2851             d->indent += 2;
2852             oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2853             oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2854                      "", loopcounter, loopcounter);
2855             if (!d->in_record_p)
2856               output_escaped_param (d, length, "length");
2857             else
2858               oprintf (d->of, "l%d", loopcounter);
2859             if (d->have_this_obj)
2860               /* Try to unswitch loops (see PR53880).  */
2861               oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
2862             oprintf (d->of, "); i%d++) {\n", loopcounter);
2863             d->indent += 2;
2864             d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2865             d->used_length = 1;
2866             d->prev_val[3] = oldval;
2867             walk_type (t->u.p, d);
2868             free (newval);
2869             d->val = oldval;
2870             d->prev_val[3] = oldprevval3;
2871             d->used_length = 0;
2872             d->indent -= 2;
2873             oprintf (d->of, "%*s}\n", d->indent, "");
2874             d->process_field (t, d);
2875             d->indent -= 2;
2876             oprintf (d->of, "%*s}\n", d->indent, "");
2877           }
2878         d->in_ptr_field = false;
2879       }
2880       break;
2881
2882     case TYPE_ARRAY:
2883       {
2884         int loopcounter;
2885         const char *oldval = d->val;
2886         char *newval;
2887
2888         /* If it's an array of scalars, we optimize by not generating
2889            any code.  */
2890         if (t->u.a.p->kind == TYPE_SCALAR)
2891           break;
2892
2893         if (length)
2894           loopcounter = d->loopcounter;
2895         else
2896           loopcounter = d->counter++;
2897
2898         /* When walking an array, compute the length and store it in a
2899            local variable before walking the array elements, instead of
2900            recomputing the length expression each time through the loop.
2901            This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2902            where the length is stored in the first array element,
2903            because otherwise that operand can get overwritten on the
2904            first iteration.  */
2905         oprintf (d->of, "%*s{\n", d->indent, "");
2906         d->indent += 2;
2907         oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2908         if (!d->in_record_p || !length)
2909           {
2910             oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2911                      d->indent, "", loopcounter);
2912             if (length)
2913               output_escaped_param (d, length, "length");
2914             else
2915               oprintf (d->of, "%s", t->u.a.len);
2916             oprintf (d->of, ");\n");
2917           }
2918
2919         oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2920                  d->indent, "",
2921                  loopcounter, loopcounter, loopcounter, loopcounter);
2922         d->indent += 2;
2923         d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2924         d->used_length = 1;
2925         walk_type (t->u.a.p, d);
2926         free (newval);
2927         d->used_length = 0;
2928         d->val = oldval;
2929         d->indent -= 2;
2930         oprintf (d->of, "%*s}\n", d->indent, "");
2931         d->indent -= 2;
2932         oprintf (d->of, "%*s}\n", d->indent, "");
2933       }
2934       break;
2935
2936     case TYPE_STRUCT:
2937     case TYPE_UNION:
2938       {
2939         pair_p f;
2940         const char *oldval = d->val;
2941         const char *oldprevval1 = d->prev_val[1];
2942         const char *oldprevval2 = d->prev_val[2];
2943         const char *struct_mark_hook = NULL;
2944         const int union_p = t->kind == TYPE_UNION;
2945         int seen_default_p = 0;
2946         options_p o;
2947         int lengths_seen = 0;
2948         int endcounter;
2949         bool any_length_seen = false;
2950
2951         if (!t->u.s.line.file)
2952           error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
2953
2954         if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2955           {
2956             error_at_line (d->line,
2957                            "structure `%s' defined for mismatching languages",
2958                            t->u.s.tag);
2959             error_at_line (&t->u.s.line, "one structure defined here");
2960           }
2961
2962         /* Some things may also be defined in the structure's options.  */
2963         for (o = t->u.s.opt; o; o = o->next)
2964           if (!desc && strcmp (o->name, "desc") == 0
2965               && o->kind == OPTION_STRING)
2966             desc = o->info.string;
2967           else if (!struct_mark_hook && strcmp (o->name, "mark_hook") == 0
2968                    && o->kind == OPTION_STRING)
2969             struct_mark_hook = o->info.string;
2970
2971         if (struct_mark_hook)
2972           oprintf (d->of, "%*s%s (&%s);\n",
2973                    d->indent, "", struct_mark_hook, oldval);
2974
2975         d->prev_val[2] = oldval;
2976         d->prev_val[1] = oldprevval2;
2977         if (union_p)
2978           {
2979             if (desc == NULL)
2980               {
2981                 error_at_line (d->line,
2982                                "missing `desc' option for union `%s'",
2983                                t->u.s.tag);
2984                 desc = "1";
2985               }
2986             oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
2987             output_escaped_param (d, desc, "desc");
2988             oprintf (d->of, "))\n");
2989             d->indent += 2;
2990             oprintf (d->of, "%*s{\n", d->indent, "");
2991           }
2992         else if (desc)
2993           {
2994             /* We have a "desc" option on a struct, signifying the
2995                base class within a GC-managed inheritance hierarchy.
2996                The current code specialcases the base class, then walks
2997                into subclasses, recursing into this routine to handle them.
2998                This organization requires the base class to have a case in
2999                the switch statement, and hence a tag value is mandatory
3000                for the base class.   This restriction could be removed, but
3001                it would require some restructing of this code.  */
3002             if (!type_tag)
3003               {
3004                 error_at_line (d->line,
3005                                "missing `tag' option for type `%s'",
3006                                t->u.s.tag);
3007               }
3008             oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
3009             output_escaped_param (d, desc, "desc");
3010             oprintf (d->of, "))\n");
3011             d->indent += 2;
3012             oprintf (d->of, "%*s{\n", d->indent, "");
3013             oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
3014             d->indent += 2;
3015           }
3016
3017         FOR_ALL_INHERITED_FIELDS (t, f)
3018           {
3019             options_p oo;
3020             int skip_p = 0;
3021             const char *fieldlength = NULL;
3022
3023             d->reorder_fn = NULL;
3024             for (oo = f->opt; oo; oo = oo->next)
3025               if (strcmp (oo->name, "skip") == 0)
3026                 skip_p = 1;
3027               else if (strcmp (oo->name, "length") == 0
3028                        && oo->kind == OPTION_STRING)
3029                 fieldlength = oo->info.string;
3030
3031             if (skip_p)
3032               continue;
3033             if (fieldlength)
3034               {
3035                 lengths_seen++;
3036                 d->counter++;
3037                 if (!union_p)
3038                   {
3039                     if (!any_length_seen)
3040                       {
3041                         oprintf (d->of, "%*s{\n", d->indent, "");
3042                         d->indent += 2;
3043                       }
3044                     any_length_seen = true;
3045
3046                     oprintf (d->of, "%*ssize_t l%d = (size_t)(",
3047                              d->indent, "", d->counter - 1);
3048                     output_escaped_param (d, fieldlength, "length");
3049                     oprintf (d->of, ");\n");
3050                   }
3051               }
3052           }
3053         endcounter = d->counter;
3054
3055         FOR_ALL_INHERITED_FIELDS (t, f)
3056           {
3057             options_p oo;
3058             const char *dot = ".";
3059             const char *tagid = NULL;
3060             int skip_p = 0;
3061             int default_p = 0;
3062             const char *fieldlength = NULL;
3063             char *newval;
3064
3065             d->reorder_fn = NULL;
3066             for (oo = f->opt; oo; oo = oo->next)
3067               if (strcmp (oo->name, "dot") == 0
3068                   && oo->kind == OPTION_STRING)
3069                 dot = oo->info.string;
3070               else if (strcmp (oo->name, "tag") == 0
3071                        && oo->kind == OPTION_STRING)
3072                 tagid = oo->info.string;
3073               else if (strcmp (oo->name, "skip") == 0)
3074                 skip_p = 1;
3075               else if (strcmp (oo->name, "default") == 0)
3076                 default_p = 1;
3077               else if (strcmp (oo->name, "reorder") == 0
3078                   && oo->kind == OPTION_STRING)
3079                 d->reorder_fn = oo->info.string;
3080               else if (strcmp (oo->name, "length") == 0
3081                        && oo->kind == OPTION_STRING)
3082                 fieldlength = oo->info.string;
3083
3084             if (skip_p)
3085               continue;
3086
3087             if (union_p && tagid)
3088               {
3089                 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
3090                 d->indent += 2;
3091               }
3092             else if (union_p && default_p)
3093               {
3094                 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3095                 d->indent += 2;
3096                 seen_default_p = 1;
3097               }
3098             else if (!union_p && (default_p || tagid))
3099               error_at_line (d->line,
3100                              "can't use `%s' outside a union on field `%s'",
3101                              default_p ? "default" : "tag", f->name);
3102             else if (union_p && !(default_p || tagid)
3103                      && f->type->kind == TYPE_SCALAR)
3104               {
3105                 fprintf (stderr,
3106                          "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3107                          get_input_file_name (d->line->file), d->line->line, 
3108                          f->name);
3109                 continue;
3110               }
3111             else if (union_p && !(default_p || tagid))
3112               error_at_line (d->line,
3113                              "field `%s' is missing `tag' or `default' option",
3114                              f->name);
3115
3116             if (fieldlength)
3117               {
3118                 d->loopcounter = endcounter - lengths_seen--;
3119               }
3120
3121             d->line = &f->line;
3122             d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
3123             d->opt = f->opt;
3124             d->used_length = false;
3125             d->in_record_p = !union_p;
3126
3127             walk_type (f->type, d);
3128
3129             d->in_record_p = false;
3130
3131             free (newval);
3132
3133             if (union_p)
3134               {
3135                 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3136                 d->indent -= 2;
3137               }
3138           }
3139         d->reorder_fn = NULL;
3140
3141         d->val = oldval;
3142         d->prev_val[1] = oldprevval1;
3143         d->prev_val[2] = oldprevval2;
3144
3145         if (union_p && !seen_default_p)
3146           {
3147             oprintf (d->of, "%*sdefault:\n", d->indent, "");
3148             oprintf (d->of, "%*s  break;\n", d->indent, "");
3149           }
3150
3151         if (desc && !union_p)
3152           {
3153                 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3154                 d->indent -= 2;
3155           }
3156         if (union_p)
3157           {
3158             oprintf (d->of, "%*s}\n", d->indent, "");
3159             d->indent -= 2;
3160           }
3161         else if (desc)
3162           {
3163             /* Add cases to handle subclasses.  */
3164             struct seen_tag *tags = NULL;
3165             walk_subclasses (t, d, &tags);
3166
3167             /* Ensure that if someone forgets a "tag" option that we don't
3168                silent fail to traverse that subclass's fields.  */
3169             if (!seen_default_p)
3170               {
3171                 oprintf (d->of, "%*s/* Unrecognized tag value.  */\n",
3172                          d->indent, "");
3173                 oprintf (d->of, "%*sdefault: gcc_unreachable (); \n",
3174                          d->indent, "");
3175               }
3176
3177             /* End of the switch statement */
3178             oprintf (d->of, "%*s}\n", d->indent, "");
3179             d->indent -= 2;
3180           }
3181         if (any_length_seen)
3182           {
3183             d->indent -= 2;
3184             oprintf (d->of, "%*s}\n", d->indent, "");
3185           }
3186       }
3187       break;
3188
3189     case TYPE_LANG_STRUCT:
3190       {
3191         type_p nt;
3192         for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3193           if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3194             break;
3195         if (nt == NULL)
3196           error_at_line (d->line, "structure `%s' differs between languages",
3197                          t->u.s.tag);
3198         else
3199           walk_type (nt, d);
3200       }
3201       break;
3202
3203     case TYPE_USER_STRUCT:
3204       d->process_field (t, d);
3205       break;
3206
3207     case TYPE_NONE:
3208     case TYPE_UNDEFINED:
3209       gcc_unreachable ();
3210     }
3211 }
3212
3213 /* process_field routine for marking routines.  */
3214
3215 static void
3216 write_types_process_field (type_p f, const struct walk_type_data *d)
3217 {
3218   const struct write_types_data *wtd;
3219   const char *cast = d->needs_cast_p ? "(void *)" : "";
3220   wtd = (const struct write_types_data *) d->cookie;
3221
3222   switch (f->kind)
3223     {
3224     case TYPE_NONE:
3225     case TYPE_UNDEFINED:
3226       gcc_unreachable ();
3227     case TYPE_POINTER:
3228       oprintf (d->of, "%*s%s (%s%s", d->indent, "",
3229                wtd->subfield_marker_routine, cast, d->val);
3230       if (wtd->param_prefix)
3231         {
3232           if (f->u.p->kind == TYPE_SCALAR)
3233             /* The current type is a pointer to a scalar (so not
3234                considered like a pointer to instances of user defined
3235                types) and we are seeing it; it means we must be even
3236                more careful about the second argument of the
3237                SUBFIELD_MARKER_ROUTINE call.  That argument must
3238                always be the instance of the type for which
3239                write_func_for_structure was called - this really is
3240                what the function SUBFIELD_MARKER_ROUTINE expects.
3241                That is, it must be an instance of the ORIG_S type
3242                parameter of write_func_for_structure.  The convention
3243                is that that argument must be "x" in that case (as set
3244                by write_func_for_structure).  The problem is, we can't
3245                count on d->prev_val[3] to be always set to "x" in that
3246                case.  Sometimes walk_type can set it to something else
3247                (to e.g cooperate with write_array when called from
3248                write_roots).  So let's set it to "x" here then.  */
3249             oprintf (d->of, ", x");
3250           else
3251             oprintf (d->of, ", %s", d->prev_val[3]);
3252           if (d->orig_s)
3253             {
3254               oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3255               output_mangled_typename (d->of, d->orig_s);
3256             }
3257           else
3258             oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3259         }
3260       oprintf (d->of, ");\n");
3261       if (d->reorder_fn && wtd->reorder_note_routine)
3262         oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
3263                  wtd->reorder_note_routine, cast, d->val,
3264                  d->prev_val[3], d->reorder_fn);
3265       break;
3266
3267     case TYPE_STRING:
3268     case TYPE_STRUCT:
3269     case TYPE_UNION:
3270     case TYPE_LANG_STRUCT:
3271     case TYPE_USER_STRUCT:
3272       if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3273         {
3274           /* If F is a user-defined type and the field is not a
3275              pointer to the type, then we should not generate the
3276              standard pointer-marking code.  All we need to do is call
3277              the user-provided marking function to process the fields
3278              of F.  */
3279           oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3280                    d->val);
3281         }
3282       else
3283         {
3284           oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3285           output_mangled_typename (d->of, f);
3286           oprintf (d->of, " (%s%s);\n", cast, d->val);
3287           if (d->reorder_fn && wtd->reorder_note_routine)
3288             oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
3289                      wtd->reorder_note_routine, cast, d->val, cast, d->val,
3290                      d->reorder_fn);
3291         }
3292       break;
3293
3294     case TYPE_SCALAR:
3295       break;
3296
3297     case TYPE_ARRAY:
3298       gcc_unreachable ();
3299     }
3300 }
3301
3302 /* Return an output file that is suitable for definitions which can
3303    reference struct S */
3304
3305 static outf_p
3306 get_output_file_for_structure (const_type_p s)
3307 {
3308   const input_file *fn;
3309
3310   gcc_assert (union_or_struct_p (s));
3311   fn = s->u.s.line.file;
3312
3313   /* The call to get_output_file_with_visibility may update fn by
3314      caching its result inside, so we need the CONST_CAST.  */
3315   return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3316 }
3317
3318
3319 /* Returns the specifier keyword for a string or union type S, empty string
3320    otherwise.  */
3321
3322 static const char *
3323 get_type_specifier (const type_p s)
3324 {
3325   if (s->kind == TYPE_STRUCT)
3326     return "struct ";
3327   else if (s->kind == TYPE_LANG_STRUCT)
3328     return get_type_specifier (s->u.s.lang_struct);
3329   else if (s->kind == TYPE_UNION)
3330     return "union ";
3331   return "";
3332 }
3333
3334
3335 /* Emits a declaration for type TY (assumed to be a union or a
3336    structure) on stream OUT.  */
3337
3338 static void
3339 write_type_decl (outf_p out, type_p ty)
3340 {
3341   if (union_or_struct_p (ty))
3342     oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3343   else if (ty->kind == TYPE_SCALAR)
3344     {
3345       if (ty->u.scalar_is_char)
3346         oprintf (out, "const char");
3347       else
3348         oprintf (out, "void");
3349     }
3350   else if (ty->kind == TYPE_POINTER)
3351     {
3352       write_type_decl (out, ty->u.p);
3353       oprintf (out, " *");
3354     }
3355   else if (ty->kind == TYPE_ARRAY)
3356     {
3357       write_type_decl (out, ty->u.a.p);
3358       oprintf (out, " *");
3359     }
3360   else if (ty->kind == TYPE_STRING)
3361     {
3362       oprintf (out, "const char *");
3363     }
3364   else
3365     gcc_unreachable ();
3366 }
3367
3368
3369 /* Write on OF the name of the marker function for structure S. PREFIX
3370    is the prefix to use (to distinguish ggc from pch markers).  */
3371
3372 static void
3373 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3374 {
3375   if (union_or_struct_p (s))
3376     {
3377       const char *id_for_tag = filter_type_name (s->u.s.tag);
3378       oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3379       if (id_for_tag != s->u.s.tag)
3380         free (CONST_CAST (char *, id_for_tag));
3381     }
3382   else
3383     gcc_unreachable ();
3384 }
3385
3386 /* Write on OF a user-callable routine to act as an entry point for
3387    the marking routine for S, generated by write_func_for_structure.
3388    WTD distinguishes between ggc and pch markers.  */
3389
3390 static void
3391 write_user_func_for_structure_ptr (outf_p of, type_p s, const write_types_data *wtd)
3392 {
3393   gcc_assert (union_or_struct_p (s));
3394
3395   type_p alias_of = NULL;
3396   for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3397     if (strcmp (opt->name, "ptr_alias") == 0)
3398       {
3399         /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3400            we do not generate marking code for ORIG_S here. Instead, a
3401            forwarder #define in gtype-desc.h will cause every call to its
3402            marker to call the target of this alias.
3403
3404            However, we still want to create a user entry code for the
3405            aliased type. So, if ALIAS_OF is set, we only generate the
3406            user-callable marker function.  */
3407         alias_of = opt->info.type;
3408         break;
3409       }
3410
3411   DBGPRINTF ("write_user_func_for_structure_ptr: %s %s", s->u.s.tag,
3412              wtd->prefix);
3413
3414   /* Only write the function once. */
3415   if (s->u.s.wrote_user_func_for_ptr[wtd->kind])
3416     return;
3417   s->u.s.wrote_user_func_for_ptr[wtd->kind] = true;
3418
3419   oprintf (of, "\nvoid\n");
3420   oprintf (of, "gt_%sx (", wtd->prefix);
3421   write_type_decl (of, s);
3422   oprintf (of, " *& x)\n");
3423   oprintf (of, "{\n");
3424   oprintf (of, "  if (x)\n    ");
3425   write_marker_function_name (of,
3426                               alias_of ? alias_of : get_ultimate_base_class (s),
3427                               wtd->prefix);
3428   oprintf (of, " ((void *) x);\n");
3429   oprintf (of, "}\n");
3430 }
3431
3432
3433 /* Write a function to mark all the fields of type S on OF.  PREFIX
3434    and D are as in write_user_marking_functions.  */
3435
3436 static void
3437 write_user_func_for_structure_body (type_p s, const char *prefix,
3438                                     struct walk_type_data *d)
3439 {
3440   oprintf (d->of, "\nvoid\n");
3441   oprintf (d->of, "gt_%sx (", prefix);
3442   write_type_decl (d->of, s);
3443   oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3444   oprintf (d->of, "{\n");
3445   oprintf (d->of, "  ");
3446   write_type_decl (d->of, s);
3447   oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3448   d->val = "(*x)";
3449   d->indent = 2;
3450   walk_type (s, d);
3451   oprintf (d->of, "}\n");
3452 }
3453
3454 /* Emit the user-callable functions needed to mark all the types used
3455    by the user structure S.  PREFIX is the prefix to use to
3456    distinguish ggc and pch markers.  D contains data needed to pass to
3457    walk_type when traversing the fields of a type.
3458
3459    For every type T referenced by S, two routines are generated: one
3460    that takes 'T *', marks the pointer and calls the second routine,
3461    which just marks the fields of T.  */
3462
3463 static void
3464 write_user_marking_functions (type_p s,
3465                               const write_types_data *w,
3466                               struct walk_type_data *d)
3467 {
3468   gcc_assert (s->kind == TYPE_USER_STRUCT);
3469
3470   for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3471     {
3472       type_p fld_type = fld->type;
3473       if (fld_type->kind == TYPE_POINTER)
3474         {
3475           type_p pointed_to_type = fld_type->u.p;
3476           if (union_or_struct_p (pointed_to_type))
3477             write_user_func_for_structure_ptr (d->of, pointed_to_type, w);
3478         }
3479       else if (union_or_struct_p (fld_type))
3480         write_user_func_for_structure_body (fld_type, w->prefix, d);
3481     }
3482 }
3483
3484
3485 /* For S, a structure that's part of ORIG_S write out a routine that:
3486    - Takes a parameter, a void * but actually of type *S
3487    - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3488    field of S or its substructures and (in some cases) things
3489    that are pointed to by S.  */
3490
3491 static void
3492 write_func_for_structure (type_p orig_s, type_p s,
3493                           const struct write_types_data *wtd)
3494 {
3495   const char *chain_next = NULL;
3496   const char *chain_prev = NULL;
3497   const char *chain_circular = NULL;
3498   const char *mark_hook_name = NULL;
3499   options_p opt;
3500   struct walk_type_data d;
3501
3502   if (s->u.s.base_class)
3503     {
3504       /* Verify that the base class has a "desc", since otherwise
3505          the traversal hooks there won't attempt to visit fields of
3506          subclasses such as this one.  */
3507       const_type_p ubc = get_ultimate_base_class (s);
3508       if ((!opts_have (ubc->u.s.opt, "user")
3509            && !opts_have (ubc->u.s.opt, "desc")))
3510         error_at_line (&s->u.s.line,
3511                        ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
3512                         ", but '%s' lacks a discriminator 'desc' option"),
3513                        s->u.s.tag, ubc->u.s.tag, ubc->u.s.tag);
3514
3515       /* Don't write fns for subclasses, only for the ultimate base class
3516          within an inheritance hierarchy.  */
3517       return;
3518     }
3519
3520   memset (&d, 0, sizeof (d));
3521   d.of = get_output_file_for_structure (s);
3522
3523   bool for_user = false;
3524   for (opt = s->u.s.opt; opt; opt = opt->next)
3525     if (strcmp (opt->name, "chain_next") == 0
3526         && opt->kind == OPTION_STRING)
3527       chain_next = opt->info.string;
3528     else if (strcmp (opt->name, "chain_prev") == 0
3529              && opt->kind == OPTION_STRING)
3530       chain_prev = opt->info.string;
3531     else if (strcmp (opt->name, "chain_circular") == 0
3532              && opt->kind == OPTION_STRING)
3533       chain_circular = opt->info.string;
3534     else if (strcmp (opt->name, "mark_hook") == 0
3535              && opt->kind == OPTION_STRING)
3536       mark_hook_name = opt->info.string;
3537     else if (strcmp (opt->name, "for_user") == 0)
3538       for_user = true;
3539   if (chain_prev != NULL && chain_next == NULL)
3540     error_at_line (&s->u.s.line, "chain_prev without chain_next");
3541   if (chain_circular != NULL && chain_next != NULL)
3542     error_at_line (&s->u.s.line, "chain_circular with chain_next");
3543   if (chain_circular != NULL)
3544     chain_next = chain_circular;
3545
3546   d.process_field = write_types_process_field;
3547   d.cookie = wtd;
3548   d.orig_s = orig_s;
3549   d.opt = s->u.s.opt;
3550   d.line = &s->u.s.line;
3551   d.bitmap = s->u.s.bitmap;
3552   d.prev_val[0] = "*x";
3553   d.prev_val[1] = "not valid postage";  /* Guarantee an error.  */
3554   d.prev_val[3] = "x";
3555   d.val = "(*x)";
3556   d.have_this_obj = false;
3557
3558   oprintf (d.of, "\n");
3559   oprintf (d.of, "void\n");
3560   write_marker_function_name (d.of, orig_s, wtd->prefix);
3561   oprintf (d.of, " (void *x_p)\n");
3562   oprintf (d.of, "{\n  ");
3563   write_type_decl (d.of, s);
3564   oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3565   write_type_decl (d.of, s);
3566   oprintf (d.of, " *)x_p;\n");
3567   if (chain_next != NULL)
3568     {
3569       /* TYPE_USER_STRUCTs should not occur here.  These structures
3570          are completely handled by user code.  */
3571       gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
3572
3573       oprintf (d.of, "  ");
3574       write_type_decl (d.of, s);
3575       oprintf (d.of, " * xlimit = x;\n");
3576     }
3577   if (chain_next == NULL)
3578     {
3579       oprintf (d.of, "  if (%s (x", wtd->marker_routine);
3580       if (wtd->param_prefix)
3581         {
3582           oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3583           output_mangled_typename (d.of, orig_s);
3584         }
3585       oprintf (d.of, "))\n");
3586     }
3587   else
3588     {
3589       if (chain_circular != NULL)
3590         oprintf (d.of, "  if (!%s (xlimit", wtd->marker_routine);
3591       else
3592         oprintf (d.of, "  while (%s (xlimit", wtd->marker_routine);
3593       if (wtd->param_prefix)
3594         {
3595           oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3596           output_mangled_typename (d.of, orig_s);
3597         }
3598       oprintf (d.of, "))\n");
3599       if (chain_circular != NULL)
3600         oprintf (d.of, "    return;\n  do\n");
3601       if (mark_hook_name && !wtd->skip_hooks)
3602         {
3603           oprintf (d.of, "    {\n");
3604           oprintf (d.of, "      %s (xlimit);\n   ", mark_hook_name);
3605         }
3606       oprintf (d.of, "   xlimit = (");
3607       d.prev_val[2] = "*xlimit";
3608       output_escaped_param (&d, chain_next, "chain_next");
3609       oprintf (d.of, ");\n");
3610       if (mark_hook_name && !wtd->skip_hooks)
3611         oprintf (d.of, "    }\n");
3612       if (chain_prev != NULL)
3613         {
3614           oprintf (d.of, "  if (x != xlimit)\n");
3615           oprintf (d.of, "    for (;;)\n");
3616           oprintf (d.of, "      {\n");
3617           oprintf (d.of, "        %s %s * const xprev = (",
3618                    s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3619
3620           d.prev_val[2] = "*x";
3621           output_escaped_param (&d, chain_prev, "chain_prev");
3622           oprintf (d.of, ");\n");
3623           oprintf (d.of, "        if (xprev == NULL) break;\n");
3624           oprintf (d.of, "        x = xprev;\n");
3625           oprintf (d.of, "        (void) %s (xprev", wtd->marker_routine);
3626           if (wtd->param_prefix)
3627             {
3628               oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3629               output_mangled_typename (d.of, orig_s);
3630             }
3631           oprintf (d.of, ");\n");
3632           oprintf (d.of, "      }\n");
3633         }
3634       if (chain_circular != NULL)
3635         {
3636           oprintf (d.of, "  while (%s (xlimit", wtd->marker_routine);
3637           if (wtd->param_prefix)
3638             {
3639               oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3640               output_mangled_typename (d.of, orig_s);
3641             }
3642           oprintf (d.of, "));\n");
3643           if (mark_hook_name && !wtd->skip_hooks)
3644             oprintf (d.of, "  %s (xlimit);\n", mark_hook_name);
3645           oprintf (d.of, "  do\n");
3646         }
3647       else
3648         oprintf (d.of, "  while (x != xlimit)\n");
3649     }
3650   oprintf (d.of, "    {\n");
3651   if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3652     {
3653       oprintf (d.of, "      %s (x);\n", mark_hook_name);
3654     }
3655
3656   d.prev_val[2] = "*x";
3657   d.indent = 6;
3658   if (orig_s->kind != TYPE_USER_STRUCT)
3659     walk_type (s, &d);
3660   else
3661     {
3662       /* User structures have no fields to walk. Simply generate a call
3663          to the user-provided structure marker.  */
3664       oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3665     }
3666
3667   if (chain_next != NULL)
3668     {
3669       oprintf (d.of, "      x = (");
3670       output_escaped_param (&d, chain_next, "chain_next");
3671       oprintf (d.of, ");\n");
3672     }
3673
3674   oprintf (d.of, "    }\n");
3675   if (chain_circular != NULL)
3676     oprintf (d.of, "  while (x != xlimit);\n");
3677   oprintf (d.of, "}\n");
3678
3679   if (orig_s->kind == TYPE_USER_STRUCT)
3680     write_user_marking_functions (orig_s, wtd, &d);
3681
3682   if (for_user)
3683     {
3684       write_user_func_for_structure_body (orig_s, wtd->prefix, &d);
3685       write_user_func_for_structure_ptr (d.of, orig_s, wtd);
3686     }
3687 }
3688
3689
3690 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS.  */
3691
3692 static void
3693 write_types (outf_p output_header, type_p structures,
3694              const struct write_types_data *wtd)
3695 {
3696   int nbfun = 0;                /* Count the emitted functions.  */
3697   type_p s;
3698
3699   oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3700
3701   /* We first emit the macros and the declarations. Functions' code is
3702      emitted afterwards.  This is needed in plugin mode.  */
3703   oprintf (output_header, "/* Macros and declarations.  */\n");
3704   for (s = structures; s; s = s->next)
3705     /* Do not emit handlers for derived classes; we only ever deal with
3706        the ultimate base class within an inheritance hierarchy.  */
3707     if ((s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3708         && !s->u.s.base_class)
3709       {
3710         options_p opt;
3711
3712         if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3713           continue;
3714
3715         const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3716
3717         oprintf (output_header, "#define gt_%s_", wtd->prefix);
3718         output_mangled_typename (output_header, s);
3719         oprintf (output_header, "(X) do { \\\n");
3720         oprintf (output_header,
3721                  "  if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3722                  s_id_for_tag);
3723         oprintf (output_header, "  } while (0)\n");
3724
3725         for (opt = s->u.s.opt; opt; opt = opt->next)
3726           if (strcmp (opt->name, "ptr_alias") == 0
3727               && opt->kind == OPTION_TYPE)
3728             {
3729               const_type_p const t = (const_type_p) opt->info.type;
3730               if (t->kind == TYPE_STRUCT
3731                   || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3732                 {
3733                   const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3734                   oprintf (output_header,
3735                            "#define gt_%sx_%s gt_%sx_%s\n",
3736                            wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3737                   if (t_id_for_tag != t->u.s.tag)
3738                     free (CONST_CAST (char *, t_id_for_tag));
3739                 }
3740               else
3741                 error_at_line (&s->u.s.line,
3742                                "structure alias is not a structure");
3743               break;
3744             }
3745         if (opt)
3746           continue;
3747
3748         /* Declare the marker procedure only once.  */
3749         oprintf (output_header,
3750                  "extern void gt_%sx_%s (void *);\n",
3751                  wtd->prefix, s_id_for_tag);
3752
3753         if (s_id_for_tag != s->u.s.tag)
3754           free (CONST_CAST (char *, s_id_for_tag));
3755
3756         if (s->u.s.line.file == NULL)
3757           {
3758             fprintf (stderr, "warning: structure `%s' used but not defined\n",
3759                      s->u.s.tag);
3760             continue;
3761           }
3762       }
3763
3764   /* At last we emit the functions code.  */
3765   oprintf (output_header, "\n/* functions code */\n");
3766   for (s = structures; s; s = s->next)
3767     if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3768       {
3769         options_p opt;
3770
3771         if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3772           continue;
3773         for (opt = s->u.s.opt; opt; opt = opt->next)
3774           if (strcmp (opt->name, "ptr_alias") == 0)
3775             break;
3776         if (opt)
3777           continue;
3778
3779         if (s->kind == TYPE_LANG_STRUCT)
3780           {
3781             type_p ss;
3782             for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3783               {
3784                 nbfun++;
3785                 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3786                            nbfun, (void*) ss, ss->u.s.tag);
3787                 write_func_for_structure (s, ss, wtd);
3788               }
3789           }
3790         else
3791           {
3792             nbfun++;
3793             DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3794                        nbfun, (void*) s, s->u.s.tag);
3795             write_func_for_structure (s, s, wtd);
3796           }
3797       }
3798     else
3799       {
3800         /* Structure s is not possibly pointed to, so can be ignored.  */
3801         DBGPRINTF ("ignored s @ %p  '%s' gc_used#%d",
3802                    (void*)s,  s->u.s.tag,
3803                    (int) s->gc_used);
3804       }
3805
3806   if (verbosity_level >= 2)
3807     printf ("%s emitted %d routines for %s\n",
3808             progname, nbfun, wtd->comment);
3809 }
3810
3811 static const struct write_types_data ggc_wtd = {
3812   "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
3813   "GC marker procedures.  ",
3814   FALSE, WTK_GGC
3815 };
3816
3817 static const struct write_types_data pch_wtd = {
3818   "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3819   "gt_pch_note_reorder",
3820   "PCH type-walking procedures.  ",
3821   TRUE, WTK_PCH
3822 };
3823
3824 /* Write out the local pointer-walking routines.  */
3825
3826 /* process_field routine for local pointer-walking for user-callable
3827    routines.  The difference between this and
3828    write_types_local_process_field is that, in this case, we do not
3829    need to check whether the given pointer matches the address of the
3830    parent structure.  This check was already generated by the call
3831    to gt_pch_nx in the main gt_pch_p_*() function that is calling
3832    this code.  */
3833
3834 static void
3835 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
3836 {
3837   switch (f->kind)
3838     {
3839     case TYPE_POINTER:
3840     case TYPE_STRUCT:
3841     case TYPE_UNION:
3842     case TYPE_LANG_STRUCT:
3843     case TYPE_STRING:
3844       oprintf (d->of, "%*s  op (&(%s), cookie);\n", d->indent, "", d->val);
3845       break;
3846
3847     case TYPE_USER_STRUCT:
3848       if (d->in_ptr_field)
3849         oprintf (d->of, "%*s  op (&(%s), cookie);\n", d->indent, "", d->val);
3850       else
3851         oprintf (d->of, "%*s  gt_pch_nx (&(%s), op, cookie);\n",
3852                  d->indent, "", d->val);
3853       break;
3854
3855     case TYPE_SCALAR:
3856       break;
3857
3858     case TYPE_ARRAY:
3859     case TYPE_NONE:
3860     case TYPE_UNDEFINED:
3861       gcc_unreachable ();
3862     }
3863 }
3864
3865
3866 /* Write a function to PCH walk all the fields of type S on OF.
3867    D contains data needed by walk_type to recurse into the fields of S.  */
3868
3869 static void
3870 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
3871 {
3872   oprintf (d->of, "\nvoid\n");
3873   oprintf (d->of, "gt_pch_nx (");
3874   write_type_decl (d->of, s);
3875   oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
3876            "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3877            "\tATTRIBUTE_UNUSED void *cookie)\n");
3878   oprintf (d->of, "{\n");
3879   d->val = "(*x)";
3880   d->indent = 2;
3881   d->process_field = write_types_local_user_process_field;
3882   walk_type (s, d);
3883   oprintf (d->of, "}\n");
3884 }
3885
3886
3887 /* Emit the user-callable functions needed to mark all the types used
3888    by the user structure S.  PREFIX is the prefix to use to
3889    distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
3890    chain_next option defined.  D contains data needed to pass to
3891    walk_type when traversing the fields of a type.
3892
3893    For every type T referenced by S, two routines are generated: one
3894    that takes 'T *', marks the pointer and calls the second routine,
3895    which just marks the fields of T.  */
3896
3897 static void
3898 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
3899 {
3900   gcc_assert (s->kind == TYPE_USER_STRUCT);
3901
3902   for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3903     {
3904       type_p fld_type = fld->type;
3905       if (union_or_struct_p (fld_type))
3906         write_pch_user_walking_for_structure_body (fld_type, d);
3907     }
3908 }
3909
3910
3911 /* process_field routine for local pointer-walking.  */
3912
3913 static void
3914 write_types_local_process_field (type_p f, const struct walk_type_data *d)
3915 {
3916   gcc_assert (d->have_this_obj);
3917   switch (f->kind)
3918     {
3919     case TYPE_POINTER:
3920     case TYPE_STRUCT:
3921     case TYPE_UNION:
3922     case TYPE_LANG_STRUCT:
3923     case TYPE_STRING:
3924       oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3925                d->prev_val[3]);
3926       oprintf (d->of, "%*s  op (&(%s), cookie);\n", d->indent, "", d->val);
3927       break;
3928
3929     case TYPE_USER_STRUCT:
3930       oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3931                d->prev_val[3]);
3932       if (d->in_ptr_field)
3933         oprintf (d->of, "%*s  op (&(%s), cookie);\n", d->indent, "", d->val);
3934       else
3935         oprintf (d->of, "%*s  gt_pch_nx (&(%s), op, cookie);\n",
3936                  d->indent, "", d->val);
3937       break;
3938
3939     case TYPE_SCALAR:
3940       break;
3941
3942     case TYPE_ARRAY:
3943     case TYPE_NONE:
3944     case TYPE_UNDEFINED:
3945       gcc_unreachable ();
3946     }
3947 }
3948
3949
3950 /* For S, a structure that's part of ORIG_S, and using parameters
3951    PARAM, write out a routine that:
3952    - Is of type gt_note_pointers
3953    - Calls PROCESS_FIELD on each field of S or its substructures.
3954 */
3955
3956 static void
3957 write_local_func_for_structure (const_type_p orig_s, type_p s)
3958 {
3959   struct walk_type_data d;
3960
3961   /* Don't write fns for subclasses, only for the ultimate base class
3962      within an inheritance hierarchy.  */
3963   if (s->u.s.base_class)
3964     return;
3965
3966   memset (&d, 0, sizeof (d));
3967   d.of = get_output_file_for_structure (s);
3968   d.process_field = write_types_local_process_field;
3969   d.opt = s->u.s.opt;
3970   d.line = &s->u.s.line;
3971   d.bitmap = s->u.s.bitmap;
3972   d.prev_val[0] = d.prev_val[2] = "*x";
3973   d.prev_val[1] = "not valid postage";  /* Guarantee an error.  */
3974   d.prev_val[3] = "x";
3975   d.val = "(*x)";
3976   d.fn_wants_lvalue = true;
3977
3978   oprintf (d.of, "\n");
3979   oprintf (d.of, "void\n");
3980   oprintf (d.of, "gt_pch_p_");
3981   output_mangled_typename (d.of, orig_s);
3982   oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3983            "\tvoid *x_p,\n"
3984            "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3985            "\tATTRIBUTE_UNUSED void *cookie)\n");
3986   oprintf (d.of, "{\n");
3987   oprintf (d.of, "  %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3988            s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3989            s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3990   d.indent = 2;
3991   d.have_this_obj = true;
3992
3993   if (s->kind != TYPE_USER_STRUCT)
3994     walk_type (s, &d);
3995   else
3996     {
3997       /* User structures have no fields to walk. Simply generate a
3998          call to the user-provided PCH walker.  */
3999       oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
4000                d.prev_val[3]);
4001       oprintf (d.of, "%*s  gt_pch_nx (&(%s), op, cookie);\n",
4002                d.indent, "", d.val);
4003     }
4004
4005   oprintf (d.of, "}\n");
4006
4007   /* Write user-callable entry points for the PCH walking routines.  */
4008   if (orig_s->kind == TYPE_USER_STRUCT)
4009     write_pch_user_walking_functions (s, &d);
4010
4011   for (options_p o = s->u.s.opt; o; o = o->next)
4012     if (strcmp (o->name, "for_user") == 0)
4013       {
4014         write_pch_user_walking_for_structure_body (s, &d);
4015         break;
4016       }
4017 }
4018
4019 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS.  */
4020
4021 static void
4022 write_local (outf_p output_header, type_p structures)
4023 {
4024   type_p s;
4025
4026   if (!output_header)
4027     return;
4028
4029   oprintf (output_header, "\n/* Local pointer-walking routines.  */\n");
4030   for (s = structures; s; s = s->next)
4031     if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
4032       {
4033         options_p opt;
4034
4035         if (s->u.s.line.file == NULL)
4036           continue;
4037         for (opt = s->u.s.opt; opt; opt = opt->next)
4038           if (strcmp (opt->name, "ptr_alias") == 0
4039               && opt->kind == OPTION_TYPE)
4040             {
4041               const_type_p const t = (const_type_p) opt->info.type;
4042               if (t->kind == TYPE_STRUCT
4043                   || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
4044                 {
4045                   oprintf (output_header, "#define gt_pch_p_");
4046                   output_mangled_typename (output_header, s);
4047                   oprintf (output_header, " gt_pch_p_");
4048                   output_mangled_typename (output_header, t);
4049                   oprintf (output_header, "\n");
4050                 }
4051               else
4052                 error_at_line (&s->u.s.line,
4053                                "structure alias is not a structure");
4054               break;
4055             }
4056         if (opt)
4057           continue;
4058
4059         /* Declare the marker procedure only once.  */
4060         oprintf (output_header, "extern void gt_pch_p_");
4061         output_mangled_typename (output_header, s);
4062         oprintf (output_header,
4063                  "\n    (void *, void *, gt_pointer_operator, void *);\n");
4064
4065         if (s->kind == TYPE_LANG_STRUCT)
4066           {
4067             type_p ss;
4068             for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4069               write_local_func_for_structure (s, ss);
4070           }
4071         else
4072           write_local_func_for_structure (s, s);
4073       }
4074 }
4075
4076 /* Nonzero if S is a type for which typed GC allocators should be output.  */
4077
4078 #define USED_BY_TYPED_GC_P(s)                                           \
4079   ((s->kind == TYPE_POINTER                                             \
4080     && (s->u.p->gc_used == GC_POINTED_TO                                \
4081         || s->u.p->gc_used == GC_USED))                                 \
4082    || (union_or_struct_p (s)                                            \
4083        && ((s)->gc_used == GC_POINTED_TO                                \
4084            || ((s)->gc_used == GC_MAYBE_POINTED_TO                      \
4085                && s->u.s.line.file != NULL)                             \
4086            || ((s)->gc_used == GC_USED                                  \
4087                && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))) \
4088            || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
4089
4090
4091
4092 /* Might T contain any non-pointer elements?  */
4093
4094 static int
4095 contains_scalar_p (type_p t)
4096 {
4097   switch (t->kind)
4098     {
4099     case TYPE_STRING:
4100     case TYPE_POINTER:
4101       return 0;
4102     case TYPE_ARRAY:
4103       return contains_scalar_p (t->u.a.p);
4104     case TYPE_USER_STRUCT:
4105       /* User-marked structures will typically contain pointers.  */
4106       return 0;
4107     default:
4108       /* Could also check for structures that have no non-pointer
4109          fields, but there aren't enough of those to worry about.  */
4110       return 1;
4111     }
4112 }
4113
4114 /* Mangle INPF and print it to F.  */
4115
4116 static void
4117 put_mangled_filename (outf_p f, const input_file *inpf)
4118 {
4119   /* The call to get_output_file_name may indirectly update fn since
4120      get_output_file_with_visibility caches its result inside, so we
4121      need the CONST_CAST.  */
4122   const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4123   if (!f || !name)
4124     return;
4125   for (; *name != 0; name++)
4126     if (ISALNUM (*name))
4127       oprintf (f, "%c", *name);
4128     else
4129       oprintf (f, "%c", '_');
4130 }
4131
4132 /* Finish off the currently-created root tables in FLP.  PFX, TNAME,
4133    LASTNAME, and NAME are all strings to insert in various places in
4134    the resulting code.  */
4135
4136 static void
4137 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4138                    const char *tname, const char *name)
4139 {
4140   struct flist *fli2;
4141
4142   for (fli2 = flp; fli2; fli2 = fli2->next)
4143     if (fli2->started_p)
4144       {
4145         oprintf (fli2->f, "  %s\n", lastname);
4146         oprintf (fli2->f, "};\n\n");
4147       }
4148
4149   for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4150     if (fli2->started_p)
4151       {
4152         lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4153         int fnum;
4154
4155         for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4156           if (bitmap & 1)
4157             {
4158               oprintf (base_files[fnum],
4159                        "extern const struct %s gt_%s_", tname, pfx);
4160               put_mangled_filename (base_files[fnum], fli2->file);
4161               oprintf (base_files[fnum], "[];\n");
4162             }
4163       }
4164
4165   {
4166     size_t fnum;
4167     for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4168       oprintf (base_files[fnum],
4169                "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4170   }
4171
4172
4173   for (fli2 = flp; fli2; fli2 = fli2->next)
4174     if (fli2->started_p)
4175       {
4176         lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4177         int fnum;
4178
4179         fli2->started_p = 0;
4180
4181         for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4182           if (bitmap & 1)
4183             {
4184               oprintf (base_files[fnum], "  gt_%s_", pfx);
4185               put_mangled_filename (base_files[fnum], fli2->file);
4186               oprintf (base_files[fnum], ",\n");
4187             }
4188       }
4189
4190   {
4191     size_t fnum;
4192     for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4193       {
4194         oprintf (base_files[fnum], "  NULL\n");
4195         oprintf (base_files[fnum], "};\n");
4196       }
4197   }
4198 }
4199
4200 /* Finish off the created gt_clear_caches_file_c functions.  */
4201
4202 static void
4203 finish_cache_funcs (flist *flp)
4204 {
4205   struct flist *fli2;
4206
4207   for (fli2 = flp; fli2; fli2 = fli2->next)
4208     if (fli2->started_p)
4209       {
4210         oprintf (fli2->f, "}\n\n");
4211       }
4212
4213   for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4214     if (fli2->started_p)
4215       {
4216         lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4217         int fnum;
4218
4219         for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4220           if (bitmap & 1)
4221             {
4222               oprintf (base_files[fnum], "extern void gt_clear_caches_");
4223               put_mangled_filename (base_files[fnum], fli2->file);
4224               oprintf (base_files[fnum], " ();\n");
4225             }
4226       }
4227
4228   for (size_t fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4229     oprintf (base_files[fnum], "void\ngt_clear_caches ()\n{\n");
4230
4231   for (fli2 = flp; fli2; fli2 = fli2->next)
4232     if (fli2->started_p)
4233       {
4234         lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4235         int fnum;
4236
4237         fli2->started_p = 0;
4238
4239         for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4240           if (bitmap & 1)
4241             {
4242               oprintf (base_files[fnum], "  gt_clear_caches_");
4243               put_mangled_filename (base_files[fnum], fli2->file);
4244               oprintf (base_files[fnum], " ();\n");
4245             }
4246       }
4247
4248   for (size_t fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4249     {
4250       oprintf (base_files[fnum], "}\n");
4251     }
4252 }
4253
4254 /* Write the first three fields (pointer, count and stride) for
4255    root NAME to F.  V and LINE are as for write_root.
4256
4257    Return true if the entry could be written; return false on error.  */
4258
4259 static bool
4260 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4261 {
4262   type_p ap;
4263
4264   if (!v)
4265     {
4266       error_at_line (line, "`%s' is too complex to be a root", name);
4267       return false;
4268     }
4269
4270   oprintf (f, "  {\n");
4271   oprintf (f, "    &%s,\n", name);
4272   oprintf (f, "    1");
4273
4274   for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4275     if (ap->u.a.len[0])
4276       oprintf (f, " * (%s)", ap->u.a.len);
4277     else if (ap == v->type)
4278       oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4279   oprintf (f, ",\n");
4280   oprintf (f, "    sizeof (%s", v->name);
4281   for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4282     oprintf (f, "[0]");
4283   oprintf (f, "),\n");
4284   return true;
4285 }
4286
4287 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4288    which has type FIELD_TYPE.  Parameters F to EMIT_PCH are the parameters
4289    of the caller.  */
4290
4291 static void
4292 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4293                   int has_length, struct fileloc *line,
4294                   bool emit_pch, type_p field_type, const char *field_name)
4295 {
4296   struct pair newv;
4297   /* If the field reference is relative to V, rather than to some
4298      subcomponent of V, we can mark any subarrays with a single stride.
4299      We're effectively treating the field as a global variable in its
4300      own right.  */
4301   if (v && type == v->type)
4302     {
4303       newv = *v;
4304       newv.type = field_type;
4305       newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4306       v = &newv;
4307     }
4308   /* Otherwise, any arrays nested in the structure are too complex to
4309      handle.  */
4310   else if (field_type->kind == TYPE_ARRAY)
4311     v = NULL;
4312   write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4313               has_length, line, emit_pch);
4314 }
4315
4316 /* Write out to F the table entry and any marker routines needed to
4317    mark NAME as TYPE.  V can be one of three values:
4318
4319      - null, if NAME is too complex to represent using a single
4320        count and stride.  In this case, it is an error for NAME to
4321        contain any gc-ed data.
4322
4323      - the outermost array that contains NAME, if NAME is part of an array.
4324
4325      - the C variable that contains NAME, if NAME is not part of an array.
4326
4327    LINE is the line of the C source that declares the root variable.
4328    HAS_LENGTH is nonzero iff V was a variable-length array.  */
4329
4330 static void
4331 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4332             struct fileloc *line, bool emit_pch)
4333 {
4334   switch (type->kind)
4335     {
4336     case TYPE_STRUCT:
4337       {
4338         pair_p fld;
4339         for (fld = type->u.s.fields; fld; fld = fld->next)
4340           {
4341             int skip_p = 0;
4342             const char *desc = NULL;
4343             options_p o;
4344
4345             for (o = fld->opt; o; o = o->next)
4346               if (strcmp (o->name, "skip") == 0)
4347                 skip_p = 1;
4348               else if (strcmp (o->name, "desc") == 0
4349                        && o->kind == OPTION_STRING)
4350                 desc = o->info.string;
4351               else
4352                 error_at_line (line,
4353                                "field `%s' of global `%s' has unknown option `%s'",
4354                                fld->name, name, o->name);
4355
4356             if (skip_p)
4357               continue;
4358             else if (desc && fld->type->kind == TYPE_UNION)
4359               {
4360                 pair_p validf = NULL;
4361                 pair_p ufld;
4362
4363                 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4364                   {
4365                     const char *tag = NULL;
4366                     options_p oo;
4367                     for (oo = ufld->opt; oo; oo = oo->next)
4368                       if (strcmp (oo->name, "tag") == 0
4369                           && oo->kind == OPTION_STRING)
4370                         tag = oo->info.string;
4371                     if (tag == NULL || strcmp (tag, desc) != 0)
4372                       continue;
4373                     if (validf != NULL)
4374                       error_at_line (line,
4375                                      "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4376                                      name, fld->name, validf->name,
4377                                      name, fld->name, ufld->name, tag);
4378                     validf = ufld;
4379                   }
4380                 if (validf != NULL)
4381                   write_field_root (f, v, type, name, 0, line, emit_pch,
4382                                     validf->type,
4383                                     ACONCAT ((fld->name, ".",
4384                                               validf->name, NULL)));
4385               }
4386             else if (desc)
4387               error_at_line (line,
4388                              "global `%s.%s' has `desc' option but is not union",
4389                              name, fld->name);
4390             else
4391               write_field_root (f, v, type, name, 0, line, emit_pch, fld->type,
4392                                 fld->name);
4393           }
4394       }
4395       break;
4396
4397     case TYPE_ARRAY:
4398       {
4399         char *newname;
4400         newname = xasprintf ("%s[0]", name);
4401         write_root (f, v, type->u.a.p, newname, has_length, line, emit_pch);
4402         free (newname);
4403       }
4404       break;
4405
4406     case TYPE_USER_STRUCT:
4407       error_at_line (line, "`%s' must be a pointer type, because it is "
4408                      "a GC root and its type is marked with GTY((user))",
4409                      v->name);
4410       break;
4411
4412     case TYPE_POINTER:
4413       {
4414         const_type_p tp;
4415
4416         if (!start_root_entry (f, v, name, line))
4417           return;
4418
4419         tp = type->u.p;
4420
4421         if (!has_length && union_or_struct_p (tp))
4422           {
4423             tp = get_ultimate_base_class (tp);
4424             const char *id_for_tag = filter_type_name (tp->u.s.tag);
4425             oprintf (f, "    &gt_ggc_mx_%s,\n", id_for_tag);
4426             if (emit_pch)
4427               oprintf (f, "    &gt_pch_nx_%s", id_for_tag);
4428             else
4429               oprintf (f, "    NULL");
4430             if (id_for_tag != tp->u.s.tag)
4431               free (CONST_CAST (char *, id_for_tag));
4432           }
4433         else if (has_length
4434                  && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
4435           {
4436             oprintf (f, "    &gt_ggc_ma_%s,\n", name);
4437             if (emit_pch)
4438               oprintf (f, "    &gt_pch_na_%s", name);
4439             else
4440               oprintf (f, "    NULL");
4441           }
4442         else
4443           {
4444             error_at_line (line,
4445                            "global `%s' is pointer to unimplemented type",
4446                            name);
4447           }
4448         oprintf (f, "\n  },\n");
4449       }
4450       break;
4451
4452     case TYPE_STRING:
4453       {
4454         if (!start_root_entry (f, v, name, line))
4455           return;
4456
4457         oprintf (f, "    (gt_pointer_walker) &gt_ggc_m_S,\n");
4458         oprintf (f, "    (gt_pointer_walker) &gt_pch_n_S\n");
4459         oprintf (f, "  },\n");
4460       }
4461       break;
4462
4463     case TYPE_SCALAR:
4464       break;
4465
4466     case TYPE_NONE:
4467     case TYPE_UNDEFINED:
4468     case TYPE_UNION:
4469     case TYPE_LANG_STRUCT:
4470       error_at_line (line, "global `%s' is unimplemented type", name);
4471     }
4472 }
4473
4474 /* This generates a routine to walk an array.  */
4475
4476 static void
4477 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4478 {
4479   struct walk_type_data d;
4480   char *prevval3;
4481
4482   memset (&d, 0, sizeof (d));
4483   d.of = f;
4484   d.cookie = wtd;
4485   d.indent = 2;
4486   d.line = &v->line;
4487   d.opt = v->opt;
4488   d.bitmap = get_lang_bitmap (v->line.file);
4489
4490   d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4491
4492   if (wtd->param_prefix)
4493     {
4494       oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4495       oprintf (f, "    (void *, void *, gt_pointer_operator, void *);\n");
4496       oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4497                wtd->param_prefix, v->name);
4498       oprintf (d.of,
4499                "      ATTRIBUTE_UNUSED void *x_p,\n"
4500                "      ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4501                "      ATTRIBUTE_UNUSED void * cookie)\n");
4502       oprintf (d.of, "{\n");
4503       d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4504       d.process_field = write_types_local_process_field;
4505       d.have_this_obj = true;
4506       walk_type (v->type, &d);
4507       oprintf (f, "}\n\n");
4508     }
4509
4510   d.opt = v->opt;
4511   oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4512   oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4513            wtd->prefix, v->name);
4514   oprintf (f, "{\n");
4515   d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4516   d.process_field = write_types_process_field;
4517   d.have_this_obj = false;
4518   walk_type (v->type, &d);
4519   free (prevval3);
4520   oprintf (f, "}\n\n");
4521 }
4522
4523 /* Output a table describing the locations and types of VARIABLES.  */
4524
4525 static void
4526 write_roots (pair_p variables, bool emit_pch)
4527 {
4528   pair_p v;
4529   struct flist *flp = NULL;
4530
4531   for (v = variables; v; v = v->next)
4532     {
4533       outf_p f = 
4534         get_output_file_with_visibility (CONST_CAST (input_file*,
4535                                                      v->line.file));
4536       struct flist *fli;
4537       const char *length = NULL;
4538       int deletable_p = 0;
4539       options_p o;
4540       for (o = v->opt; o; o = o->next)
4541         if (strcmp (o->name, "length") == 0
4542             && o->kind == OPTION_STRING)
4543           length = o->info.string;
4544         else if (strcmp (o->name, "deletable") == 0)
4545           deletable_p = 1;
4546         else if (strcmp (o->name, "cache") == 0)
4547           ;
4548         else
4549           error_at_line (&v->line,
4550                          "global `%s' has unknown option `%s'",
4551                          v->name, o->name);
4552
4553       for (fli = flp; fli; fli = fli->next)
4554         if (fli->f == f && f)
4555           break;
4556       if (fli == NULL)
4557         {
4558           fli = XNEW (struct flist);
4559           fli->f = f;
4560           fli->next = flp;
4561           fli->started_p = 0;
4562           fli->file = v->line.file;
4563           gcc_assert (fli->file);
4564           flp = fli;
4565
4566           oprintf (f, "\n/* GC roots.  */\n\n");
4567         }
4568
4569       if (!deletable_p
4570           && length
4571           && v->type->kind == TYPE_POINTER
4572           && (v->type->u.p->kind == TYPE_POINTER
4573               || v->type->u.p->kind == TYPE_STRUCT))
4574         {
4575           write_array (f, v, &ggc_wtd);
4576           write_array (f, v, &pch_wtd);
4577         }
4578     }
4579
4580   for (v = variables; v; v = v->next)
4581     {
4582       outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4583                                                               v->line.file));
4584       struct flist *fli;
4585       int skip_p = 0;
4586       int length_p = 0;
4587       options_p o;
4588
4589       for (o = v->opt; o; o = o->next)
4590         if (strcmp (o->name, "length") == 0)
4591           length_p = 1;
4592         else if (strcmp (o->name, "deletable") == 0)
4593           skip_p = 1;
4594
4595       if (skip_p)
4596         continue;
4597
4598       for (fli = flp; fli; fli = fli->next)
4599         if (fli->f == f)
4600           break;
4601       if (!fli->started_p)
4602         {
4603           fli->started_p = 1;
4604
4605           oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4606           put_mangled_filename (f, v->line.file);
4607           oprintf (f, "[] = {\n");
4608         }
4609
4610       write_root (f, v, v->type, v->name, length_p, &v->line, emit_pch);
4611     }
4612
4613   finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4614                      "gt_ggc_rtab");
4615
4616   for (v = variables; v; v = v->next)
4617     {
4618       outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4619                                                               v->line.file));
4620       struct flist *fli;
4621       int skip_p = 1;
4622       options_p o;
4623
4624       for (o = v->opt; o; o = o->next)
4625         if (strcmp (o->name, "deletable") == 0)
4626           skip_p = 0;
4627
4628       if (skip_p)
4629         continue;
4630
4631       for (fli = flp; fli; fli = fli->next)
4632         if (fli->f == f)
4633           break;
4634       if (!fli->started_p)
4635         {
4636           fli->started_p = 1;
4637
4638           oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4639           put_mangled_filename (f, v->line.file);
4640           oprintf (f, "[] = {\n");
4641         }
4642
4643       oprintf (f, "  { &%s, 1, sizeof (%s), NULL, NULL },\n",
4644                v->name, v->name);
4645     }
4646
4647   finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4648                      "gt_ggc_deletable_rtab");
4649
4650   for (v = variables; v; v = v->next)
4651     {
4652       outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4653                                                               v->line.file));
4654       struct flist *fli;
4655       bool cache = false;
4656       options_p o;
4657
4658       for (o = v->opt; o; o = o->next)
4659         if (strcmp (o->name, "cache") == 0)
4660           cache = true;
4661        if (!cache)
4662         continue;
4663
4664       for (fli = flp; fli; fli = fli->next)
4665         if (fli->f == f)
4666           break;
4667       if (!fli->started_p)
4668         {
4669           fli->started_p = 1;
4670
4671           oprintf (f, "void\ngt_clear_caches_");
4672           put_mangled_filename (f, v->line.file);
4673           oprintf (f, " ()\n{\n");
4674         }
4675
4676       oprintf (f, "  gt_cleare_cache (%s);\n", v->name);
4677     }
4678
4679   finish_cache_funcs (flp);
4680
4681   if (!emit_pch)
4682     return;
4683
4684   for (v = variables; v; v = v->next)
4685     {
4686       outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4687                                                               v->line.file));
4688       struct flist *fli;
4689       int skip_p = 0;
4690       options_p o;
4691
4692       for (o = v->opt; o; o = o->next)
4693         if (strcmp (o->name, "deletable") == 0)
4694           {
4695             skip_p = 1;
4696             break;
4697           }
4698
4699       if (skip_p)
4700         continue;
4701
4702       if (!contains_scalar_p (v->type))
4703         continue;
4704
4705       for (fli = flp; fli; fli = fli->next)
4706         if (fli->f == f)
4707           break;
4708       if (!fli->started_p)
4709         {
4710           fli->started_p = 1;
4711
4712           oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4713           put_mangled_filename (f, v->line.file);
4714           oprintf (f, "[] = {\n");
4715         }
4716
4717       oprintf (f, "  { &%s, 1, sizeof (%s), NULL, NULL },\n",
4718                v->name, v->name);
4719     }
4720
4721   finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4722                      "gt_pch_scalar_rtab");
4723 }
4724
4725 /* Prints not-as-ugly version of a typename of T to OF.  Trades the uniquness
4726    guaranteee for somewhat increased readability.  If name conflicts do happen,
4727    this funcion will have to be adjusted to be more like
4728    output_mangled_typename.  */
4729
4730 static void
4731 output_typename (outf_p of, const_type_p t)
4732 {
4733   switch (t->kind)
4734     {
4735     case TYPE_STRING:
4736       oprintf (of, "str");
4737       break;
4738     case TYPE_SCALAR:
4739       oprintf (of, "scalar");
4740       break;
4741     case TYPE_POINTER:
4742       output_typename (of, t->u.p);
4743       break;
4744     case TYPE_STRUCT:
4745     case TYPE_USER_STRUCT:
4746     case TYPE_UNION:
4747     case TYPE_LANG_STRUCT:
4748       oprintf (of, "%s", t->u.s.tag);
4749       break;
4750     case TYPE_NONE:
4751     case TYPE_UNDEFINED:
4752     case TYPE_ARRAY:
4753       gcc_unreachable ();
4754     }
4755 }
4756
4757 #define INDENT 2
4758
4759 /* Dumps the value of typekind KIND.  */
4760
4761 static void
4762 dump_typekind (int indent, enum typekind kind)
4763 {
4764   printf ("%*ckind = ", indent, ' ');
4765   switch (kind)
4766     {
4767     case TYPE_SCALAR:
4768       printf ("TYPE_SCALAR");
4769       break;
4770     case TYPE_STRING:
4771       printf ("TYPE_STRING");
4772       break;
4773     case TYPE_STRUCT:
4774       printf ("TYPE_STRUCT");
4775       break;
4776     case TYPE_UNDEFINED:
4777       printf ("TYPE_UNDEFINED");
4778       break;
4779     case TYPE_USER_STRUCT:
4780       printf ("TYPE_USER_STRUCT");
4781       break;
4782     case TYPE_UNION:
4783       printf ("TYPE_UNION");
4784       break;
4785     case TYPE_POINTER:
4786       printf ("TYPE_POINTER");
4787       break;
4788     case TYPE_ARRAY:
4789       printf ("TYPE_ARRAY");
4790       break;
4791     case TYPE_LANG_STRUCT:
4792       printf ("TYPE_LANG_STRUCT");
4793       break;
4794     default:
4795       gcc_unreachable ();
4796     }
4797   printf ("\n");
4798 }
4799
4800 /* Dumps the value of GC_USED flag.  */
4801
4802 static void
4803 dump_gc_used (int indent, enum gc_used_enum gc_used)
4804 {
4805   printf ("%*cgc_used = ", indent, ' ');
4806   switch (gc_used)
4807     {
4808     case GC_UNUSED:
4809       printf ("GC_UNUSED");
4810       break;
4811     case GC_USED:
4812       printf ("GC_USED");
4813       break;
4814     case GC_MAYBE_POINTED_TO:
4815       printf ("GC_MAYBE_POINTED_TO");
4816       break;
4817     case GC_POINTED_TO:
4818       printf ("GC_POINTED_TO");
4819       break;
4820     default:
4821       gcc_unreachable ();
4822     }
4823   printf ("\n");
4824 }
4825
4826 /* Dumps the type options OPT.  */
4827
4828 static void
4829 dump_options (int indent, options_p opt)
4830 {
4831   options_p o;
4832   printf ("%*coptions = ", indent, ' ');
4833   o = opt;
4834   while (o)
4835     {
4836       switch (o->kind)
4837         {
4838         case OPTION_STRING:
4839           printf ("%s:string %s ", o->name, o->info.string);
4840           break;
4841         case OPTION_TYPE:
4842           printf ("%s:type ", o->name);
4843           dump_type (indent+1, o->info.type);
4844           break;
4845         case OPTION_NESTED:
4846           printf ("%s:nested ", o->name);
4847           break;
4848         case OPTION_NONE:
4849           gcc_unreachable ();
4850         }
4851       o = o->next;
4852     }
4853   printf ("\n");
4854 }
4855
4856 /* Dumps the source file location in LINE.  */
4857
4858 static void
4859 dump_fileloc (int indent, struct fileloc line)
4860 {
4861   printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ', 
4862           get_input_file_name (line.file),
4863           line.line);
4864 }
4865
4866 /* Recursively dumps the struct, union, or a language-specific
4867    struct T.  */
4868
4869 static void
4870 dump_type_u_s (int indent, type_p t)
4871 {
4872   pair_p fields;
4873
4874   gcc_assert (union_or_struct_p (t));
4875   printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
4876   dump_fileloc (indent, t->u.s.line);
4877   printf ("%*cu.s.fields =\n", indent, ' ');
4878   fields = t->u.s.fields;
4879   while (fields)
4880     {
4881       dump_pair (indent + INDENT, fields);
4882       fields = fields->next;
4883     }
4884   printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
4885   dump_options (indent, t->u.s.opt);
4886   printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
4887   if (t->kind == TYPE_LANG_STRUCT)
4888     {
4889       printf ("%*cu.s.lang_struct:\n", indent, ' ');
4890       dump_type_list (indent + INDENT, t->u.s.lang_struct);
4891     }
4892 }
4893
4894 /* Recursively dumps the array T.  */
4895
4896 static void
4897 dump_type_u_a (int indent, type_p t)
4898 {
4899   gcc_assert (t->kind == TYPE_ARRAY);
4900   printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
4901   dump_type_list (indent + INDENT, t->u.a.p);
4902 }
4903
4904 /* Recursively dumps the type list T.  */
4905
4906 static void
4907 dump_type_list (int indent, type_p t)
4908 {
4909   type_p p = t;
4910   while (p)
4911     {
4912       dump_type (indent, p);
4913       p = p->next;
4914     }
4915 }
4916
4917 static htab_t seen_types;
4918
4919 /* Recursively dumps the type T if it was not dumped previously.  */
4920
4921 static void
4922 dump_type (int indent, type_p t)
4923 {
4924   PTR *slot;
4925
4926   if (seen_types == NULL)
4927     seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
4928
4929   printf ("%*cType at %p: ", indent, ' ', (void *) t);
4930   slot = htab_find_slot (seen_types, t, INSERT);
4931   if (*slot != NULL)
4932     {
4933       printf ("already seen.\n");
4934       return;
4935     }
4936   *slot = t;
4937   printf ("\n");
4938
4939   dump_typekind (indent, t->kind);
4940   printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
4941           (void *) t->pointer_to);
4942   dump_gc_used (indent + INDENT, t->gc_used);
4943   switch (t->kind)
4944     {
4945     case TYPE_SCALAR:
4946       printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
4947               t->u.scalar_is_char ? "true" : "false");
4948       break;
4949     case TYPE_STRING:
4950       break;
4951     case TYPE_STRUCT:
4952     case TYPE_UNION:
4953     case TYPE_LANG_STRUCT:
4954     case TYPE_USER_STRUCT:
4955       dump_type_u_s (indent + INDENT, t);
4956       break;
4957     case TYPE_POINTER:
4958       printf ("%*cp:\n", indent + INDENT, ' ');
4959       dump_type (indent + INDENT, t->u.p);
4960       break;
4961     case TYPE_ARRAY:
4962       dump_type_u_a (indent + INDENT, t);
4963       break;
4964     default:
4965       gcc_unreachable ();
4966     }
4967   printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
4968 }
4969
4970 /* Dumps the pair P.  */
4971
4972 static void
4973 dump_pair (int indent, pair_p p)
4974 {
4975   printf ("%*cpair: name = %s\n", indent, ' ', p->name);
4976   dump_type (indent, p->type);
4977   dump_fileloc (indent, p->line);
4978   dump_options (indent, p->opt);
4979   printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
4980 }
4981
4982 /* Dumps the list of pairs PP.  */
4983
4984 static void
4985 dump_pair_list (const char *name, pair_p pp)
4986 {
4987   pair_p p;
4988   printf ("%s:\n", name);
4989   for (p = pp; p != NULL; p = p->next)
4990     dump_pair (0, p);
4991   printf ("End of %s\n\n", name);
4992 }
4993
4994 /* Dumps the STRUCTURES.  */
4995
4996 static void
4997 dump_structures (const char *name, type_p structures)
4998 {
4999   printf ("%s:\n", name);
5000   dump_type_list (0, structures);
5001   printf ("End of %s\n\n", name);
5002 }
5003
5004 /* Dumps the internal structures of gengtype.  This is useful to debug
5005    gengtype itself, or to understand what it does, e.g. for plugin
5006    developers.  */
5007
5008 static void
5009 dump_everything (void)
5010 {
5011   dump_pair_list ("typedefs", typedefs);
5012   dump_structures ("structures", structures);
5013   dump_pair_list ("variables", variables);
5014
5015   /* Allocated with the first call to dump_type.  */
5016   htab_delete (seen_types);
5017 }
5018 \f
5019
5020
5021 /* Option specification for getopt_long.  */
5022 static const struct option gengtype_long_options[] = {
5023   {"help", no_argument, NULL, 'h'},
5024   {"version", no_argument, NULL, 'V'},
5025   {"verbose", no_argument, NULL, 'v'},
5026   {"dump", no_argument, NULL, 'd'},
5027   {"debug", no_argument, NULL, 'D'},
5028   {"plugin", required_argument, NULL, 'P'},
5029   {"srcdir", required_argument, NULL, 'S'},
5030   {"backupdir", required_argument, NULL, 'B'},
5031   {"inputs", required_argument, NULL, 'I'},
5032   {"read-state", required_argument, NULL, 'r'},
5033   {"write-state", required_argument, NULL, 'w'},
5034   /* Terminating NULL placeholder.  */
5035   {NULL, no_argument, NULL, 0},
5036 };
5037
5038
5039 static void
5040 print_usage (void)
5041 {
5042   printf ("Usage: %s\n", progname);
5043   printf ("\t -h | --help " " \t# Give this help.\n");
5044   printf ("\t -D | --debug "
5045           " \t# Give debug output to debug %s itself.\n", progname);
5046   printf ("\t -V | --version " " \t# Give version information.\n");
5047   printf ("\t -v | --verbose  \t# Increase verbosity.  Can be given several times.\n");
5048   printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5049   printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5050           " \t# Generate for plugin.\n");
5051   printf ("\t -S | --srcdir <GCC-directory> "
5052           " \t# Specify the GCC source directory.\n");
5053   printf ("\t -B | --backupdir <directory> "
5054           " \t# Specify the backup directory for updated files.\n");
5055   printf ("\t -I | --inputs <input-list> "
5056           " \t# Specify the file with source files list.\n");
5057   printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5058   printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5059 }
5060
5061 static void
5062 print_version (void)
5063 {
5064   printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5065   printf ("Report bugs: %s\n", bug_report_url);
5066 }
5067
5068 /* Parse the program options using getopt_long... */
5069 static void
5070 parse_program_options (int argc, char **argv)
5071 {
5072   int opt = -1;
5073   while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5074                              gengtype_long_options, NULL)) >= 0)
5075     {
5076       switch (opt)
5077         {
5078         case 'h':               /* --help */
5079           print_usage ();
5080           break;
5081         case 'V':               /* --version */
5082           print_version ();
5083           break;
5084         case 'd':               /* --dump */
5085           do_dump = 1;
5086           break;
5087         case 'D':               /* --debug */
5088           do_debug = 1;
5089           break;
5090         case 'v':               /* --verbose */
5091           verbosity_level++;
5092           break;
5093         case 'P':               /* --plugin */
5094           if (optarg)
5095             plugin_output_filename = optarg;
5096           else
5097             fatal ("missing plugin output file name");
5098           break;
5099         case 'S':               /* --srcdir */
5100           if (optarg)
5101             srcdir = optarg;
5102           else
5103             fatal ("missing source directory");
5104           srcdir_len = strlen (srcdir);
5105           break;
5106         case 'B':               /* --backupdir */
5107           if (optarg)
5108             backup_dir = optarg;
5109           else
5110             fatal ("missing backup directory");
5111           break;
5112         case 'I':               /* --inputs */
5113           if (optarg)
5114             inputlist = optarg;
5115           else
5116             fatal ("missing input list");
5117           break;
5118         case 'r':               /* --read-state */
5119           if (optarg)
5120             read_state_filename = optarg;
5121           else
5122             fatal ("missing read state file");
5123           DBGPRINTF ("read state %s\n", optarg);
5124           break;
5125         case 'w':               /* --write-state */
5126           DBGPRINTF ("write state %s\n", optarg);
5127           if (optarg)
5128             write_state_filename = optarg;
5129           else
5130             fatal ("missing write state file");
5131           break;
5132         default:
5133           fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5134           print_usage ();
5135           fatal ("unexpected flag");
5136         }
5137     };
5138   if (plugin_output_filename)
5139     {
5140       /* In plugin mode we require some input files.  */
5141       int i = 0;
5142       if (optind >= argc)
5143         fatal ("no source files given in plugin mode");
5144       nb_plugin_files = argc - optind;
5145       plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5146       for (i = 0; i < (int) nb_plugin_files; i++)
5147         {
5148           char *name = argv[i + optind];
5149           plugin_files[i] = input_file_by_name (name);
5150         }
5151     }
5152 }
5153
5154
5155 \f
5156 /******* Manage input files.  ******/
5157
5158 /* Hash table of unique input file names.  */
5159 static htab_t input_file_htab;
5160
5161 /* Find or allocate a new input_file by hash-consing it.  */
5162 input_file*
5163 input_file_by_name (const char* name)
5164 {
5165   PTR* slot;
5166   input_file* f = NULL;
5167   int namlen = 0;
5168   if (!name)
5169     return NULL;
5170   namlen = strlen (name);
5171   f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5172   f->inpbitmap = 0;
5173   f->inpoutf = NULL;
5174   f->inpisplugin = false;
5175   strcpy (f->inpname, name);
5176   slot = htab_find_slot (input_file_htab, f, INSERT);
5177   gcc_assert (slot != NULL);
5178   if (*slot)
5179     {
5180       /* Already known input file.  */
5181       free (f);
5182       return (input_file*)(*slot);
5183     }
5184   /* New input file.  */
5185   *slot = f;
5186   return f;
5187     }
5188
5189 /* Hash table support routines for input_file-s.  */
5190 static hashval_t
5191 htab_hash_inputfile (const void *p)
5192 {
5193   const input_file *inpf = (const input_file *) p;
5194   gcc_assert (inpf);
5195   return htab_hash_string (get_input_file_name (inpf));
5196 }
5197
5198 static int
5199 htab_eq_inputfile (const void *x, const void *y)
5200 {
5201   const input_file *inpfx = (const input_file *) x;
5202   const input_file *inpfy = (const input_file *) y;
5203   gcc_assert (inpfx != NULL && inpfy != NULL);
5204   return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5205 }
5206
5207
5208 int
5209 main (int argc, char **argv)
5210 {
5211   size_t i;
5212   static struct fileloc pos = { NULL, 0 };
5213   outf_p output_header;
5214
5215   /* Mandatory common initializations.  */
5216   progname = "gengtype";        /* For fatal and messages.  */
5217   /* Create the hash-table used to hash-cons input files.  */
5218   input_file_htab =
5219     htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5220   /* Initialize our special input files.  */
5221   this_file = input_file_by_name (__FILE__);
5222   system_h_file = input_file_by_name ("system.h");
5223   /* Set the scalar_is_char union number for predefined scalar types.  */
5224   scalar_nonchar.u.scalar_is_char = FALSE;
5225   scalar_char.u.scalar_is_char = TRUE;
5226
5227   parse_program_options (argc, argv);
5228
5229 #if ENABLE_CHECKING
5230   if (do_debug)
5231     {
5232       time_t now = (time_t) 0;
5233       time (&now);
5234       DBGPRINTF ("gengtype started pid %d at %s",
5235                  (int) getpid (), ctime (&now));
5236     }
5237 #endif  /* ENABLE_CHECKING */
5238
5239   /* Parse the input list and the input files.  */
5240   DBGPRINTF ("inputlist %s", inputlist);
5241   if (read_state_filename)
5242     {
5243       if (inputlist)
5244         fatal ("input list %s cannot be given with a read state file %s",
5245                inputlist, read_state_filename);
5246       read_state (read_state_filename);
5247       DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5248     }
5249   else if (inputlist)
5250     {
5251       /* These types are set up with #define or else outside of where
5252          we can see them.  We should initialize them before calling
5253          read_input_list.  */
5254 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5255         Call;} while (0)
5256       POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5257       POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5258       POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5259       POS_HERE (do_scalar_typedef ("double_int", &pos));
5260       POS_HERE (do_scalar_typedef ("offset_int", &pos));
5261       POS_HERE (do_scalar_typedef ("widest_int", &pos));
5262       POS_HERE (do_scalar_typedef ("int64_t", &pos));
5263       POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5264       POS_HERE (do_scalar_typedef ("uint8", &pos));
5265       POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5266       POS_HERE (do_scalar_typedef ("jword", &pos));
5267       POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5268       POS_HERE (do_scalar_typedef ("void", &pos));
5269       POS_HERE (do_scalar_typedef ("machine_mode", &pos));
5270       POS_HERE (do_typedef ("PTR", 
5271                             create_pointer (resolve_typedef ("void", &pos)),
5272                             &pos));
5273 #undef POS_HERE
5274       read_input_list (inputlist);
5275       for (i = 0; i < num_gt_files; i++)
5276         {
5277           parse_file (get_input_file_name (gt_files[i]));
5278           DBGPRINTF ("parsed file #%d %s", 
5279                      (int) i, get_input_file_name (gt_files[i]));
5280         }
5281       if (verbosity_level >= 1)
5282         printf ("%s parsed %d files with %d GTY types\n", 
5283                 progname, (int) num_gt_files, type_count);
5284
5285       DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5286     }
5287   else
5288     fatal ("either an input list or a read state file should be given");
5289   if (hit_error)
5290     return 1;
5291
5292
5293   if (plugin_output_filename)
5294     {
5295       size_t ix = 0;
5296       /* In plugin mode, we should have read a state file, and have
5297          given at least one plugin file.  */
5298       if (!read_state_filename)
5299         fatal ("No read state given in plugin mode for %s",
5300                plugin_output_filename);
5301
5302       if (nb_plugin_files == 0 || !plugin_files)
5303         fatal ("No plugin files given in plugin mode for %s",
5304                plugin_output_filename);
5305
5306       /* Parse our plugin files and augment the state.  */
5307       for (ix = 0; ix < nb_plugin_files; ix++)
5308         {
5309           input_file* pluginput = plugin_files [ix];
5310           pluginput->inpisplugin = true;
5311           parse_file (get_input_file_name (pluginput));
5312         }
5313       if (hit_error)
5314         return 1;
5315
5316       plugin_output = create_file ("GCC", plugin_output_filename);
5317       DBGPRINTF ("created plugin_output %p named %s",
5318                  (void *) plugin_output, plugin_output->name);
5319     }
5320   else
5321     {                           /* No plugin files, we are in normal mode.  */
5322       if (!srcdir)
5323         fatal ("gengtype needs a source directory in normal mode");
5324     }
5325   if (hit_error)
5326     return 1;
5327
5328   gen_rtx_next ();
5329
5330   set_gc_used (variables);
5331
5332   for (type_p t = structures; t; t = t->next)
5333     {
5334       bool for_user = false;
5335       for (options_p o = t->u.s.opt; o; o = o->next)
5336         if (strcmp (o->name, "for_user") == 0)
5337           {
5338             for_user = true;
5339             break;
5340           }
5341
5342       if (for_user)
5343         set_gc_used_type (t, GC_POINTED_TO);
5344     }
5345  /* The state at this point is read from the state input file or by
5346     parsing source files and optionally augmented by parsing plugin
5347     source files.  Write it now.  */
5348   if (write_state_filename)
5349     {
5350       DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5351
5352       if (hit_error)
5353         fatal ("didn't write state file %s after errors", 
5354                write_state_filename);
5355
5356       DBGPRINTF ("before write_state %s", write_state_filename);
5357       write_state (write_state_filename);
5358
5359       if (do_dump)
5360         dump_everything ();
5361
5362       /* After having written the state file we return immediately to
5363          avoid generating any output file.  */
5364       if (hit_error)
5365         return 1;
5366       else
5367         return 0;
5368     }
5369
5370
5371   open_base_files ();
5372
5373   output_header = plugin_output ? plugin_output : header_file;
5374   DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5375                        structures);
5376
5377   write_types (output_header, structures, &ggc_wtd);
5378   if (plugin_files == NULL)
5379     {
5380       DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5381                            structures);
5382       write_types (header_file, structures, &pch_wtd);
5383       write_local (header_file, structures);
5384     }
5385   write_roots (variables, plugin_files == NULL);
5386   write_rtx_next ();
5387   close_output_files ();
5388
5389   if (do_dump)
5390     dump_everything ();
5391
5392   /* Don't bother about free-ing any input or plugin file, etc.  */
5393
5394   if (hit_error)
5395     return 1;
5396   return 0;
5397 }