2014-12-08 Michael Meissner <meissner@linux.vnet.ibm.com>
[official-gcc.git] / gcc / gengtype.c
blob39d29183521793bf63e3ecab28feed397fb95a1e
1 /* Process source files and output type information.
2 Copyright (C) 2002-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
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"
36 /* Data types, macros, etc. used only in this file. */
39 /* The list of output files. */
40 outf_p output_files;
42 /* The output header file that is included into pretty much every
43 source file. */
44 outf_p header_file;
47 /* The name of the file containing the list of input files. */
48 static char *inputlist;
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;
55 /* The generated plugin output file and name. */
56 static outf_p plugin_output;
57 static char *plugin_output_filename;
59 /* Our source directory and its length. */
60 const char *srcdir;
61 size_t srcdir_len;
63 /* Variables used for reading and writing the state. */
64 const char *read_state_filename;
65 const char *write_state_filename;
67 /* Variables to help debugging. */
68 int do_dump;
69 int do_debug;
71 /* Level for verbose messages. */
72 int verbosity_level;
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;
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. */
84 static outf_p create_file (const char *, const char *);
86 static const char *get_file_basename (const input_file *);
87 static const char *get_file_realbasename (const input_file *);
89 static int get_prefix_langdir_index (const char *);
90 static const char *get_file_langdir (const input_file *);
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);
97 /* Nonzero iff an error has occurred. */
98 bool hit_error = false;
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);
105 /* Report an error at POS, printing MSG. */
107 void
108 error_at_line (const struct fileloc *pos, const char *msg, ...)
110 va_list ap;
112 gcc_assert (pos != NULL && pos->file != NULL);
113 va_start (ap, msg);
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;
120 va_end (ap);
123 /* asprintf, but produces fatal message on out-of-memory. */
124 char *
125 xasprintf (const char *format, ...)
127 int n;
128 char *result;
129 va_list ap;
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);
137 return result;
140 /* Locate the ultimate base class of struct S. */
142 static const_type_p
143 get_ultimate_base_class (const_type_p s)
145 while (s->u.s.base_class)
146 s = s->u.s.base_class;
147 return s;
150 static type_p
151 get_ultimate_base_class (type_p s)
153 while (s->u.s.base_class)
154 s = s->u.s.base_class;
155 return s;
158 /* Input file handling. */
160 /* Table of all input files. */
161 const input_file **gt_files;
162 size_t num_gt_files;
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;
171 /* Vector of per-language directories. */
172 const char **lang_dir_names;
173 size_t num_lang_dirs;
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;
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)
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, nb_param_struct = 0;
190 int nb_user_struct = 0, nb_undefined = 0;
191 type_p p = NULL;
192 for (p = t; p; p = p->next)
194 nb_types++;
195 switch (p->kind)
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_PARAM_STRUCT:
224 nb_param_struct++;
225 break;
226 case TYPE_NONE:
227 gcc_unreachable ();
230 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
231 lbasename (fil), lin, msg, nb_types);
232 if (nb_scalar > 0 || nb_string > 0)
233 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
234 if (nb_struct > 0 || nb_union > 0)
235 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
236 if (nb_pointer > 0 || nb_array > 0)
237 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
238 if (nb_lang_struct > 0 || nb_param_struct > 0)
239 fprintf (stderr, "@@%%@@ %d lang_structs, %d param_structs\n",
240 nb_lang_struct, nb_param_struct);
241 if (nb_user_struct > 0)
242 fprintf (stderr, "@@%%@@ %d user_structs\n", nb_user_struct);
243 if (nb_undefined > 0)
244 fprintf (stderr, "@@%%@@ %d undefined types\n", nb_undefined);
245 fprintf (stderr, "\n");
247 #endif /* ENABLE_CHECKING */
249 /* Scan the input file, LIST, and determine how much space we need to
250 store strings in. Also, count the number of language directories
251 and files. The numbers returned are overestimates as they does not
252 consider repeated files. */
253 static size_t
254 measure_input_list (FILE *list)
256 size_t n = 0;
257 int c;
258 bool atbol = true;
259 num_lang_dirs = 0;
260 num_gt_files = plugin_files ? nb_plugin_files : 0;
261 while ((c = getc (list)) != EOF)
263 n++;
264 if (atbol)
266 if (c == '[')
267 num_lang_dirs++;
268 else
270 /* Add space for a lang_bitmap before the input file name. */
271 n += sizeof (lang_bitmap);
272 num_gt_files++;
274 atbol = false;
277 if (c == '\n')
278 atbol = true;
281 rewind (list);
282 return n;
285 /* Read one input line from LIST to HEREP (which is updated). A
286 pointer to the string is returned via LINEP. If it was a language
287 subdirectory in square brackets, strip off the square brackets and
288 return true. Otherwise, leave space before the string for a
289 lang_bitmap, and return false. At EOF, returns false, does not
290 touch *HEREP, and sets *LINEP to NULL. POS is used for
291 diagnostics. */
292 static bool
293 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
295 char *here = *herep;
296 char *line;
297 int c = getc (list);
299 /* Read over whitespace. */
300 while (c == '\n' || c == ' ')
301 c = getc (list);
303 if (c == EOF)
305 *linep = 0;
306 return false;
308 else if (c == '[')
310 /* No space for a lang_bitmap is necessary. Discard the '['. */
311 c = getc (list);
312 line = here;
313 while (c != ']' && c != '\n' && c != EOF)
315 *here++ = c;
316 c = getc (list);
318 *here++ = '\0';
320 if (c == ']')
322 c = getc (list); /* eat what should be a newline */
323 if (c != '\n' && c != EOF)
324 error_at_line (pos, "junk on line after language tag [%s]", line);
326 else
327 error_at_line (pos, "missing close bracket for language tag [%s",
328 line);
330 *herep = here;
331 *linep = line;
332 return true;
334 else
336 /* Leave space for a lang_bitmap. */
337 memset (here, 0, sizeof (lang_bitmap));
338 here += sizeof (lang_bitmap);
339 line = here;
342 *here++ = c;
343 c = getc (list);
345 while (c != EOF && c != '\n');
346 *here++ = '\0';
347 *herep = here;
348 *linep = line;
349 return false;
353 /* Read the list of input files from LIST and compute all of the
354 relevant tables. There is one file per line of the list. At
355 first, all the files on the list are language-generic, but
356 eventually a line will appear which is the name of a language
357 subdirectory in square brackets, like this: [cp]. All subsequent
358 files are specific to that language, until another language
359 subdirectory tag appears. Files can appear more than once, if
360 they apply to more than one language. */
361 static void
362 read_input_list (const char *listname)
364 FILE *list = fopen (listname, "r");
365 if (!list)
366 fatal ("cannot open %s: %s", listname, xstrerror (errno));
367 else
369 struct fileloc epos;
370 size_t bufsz = measure_input_list (list);
371 char *buf = XNEWVEC (char, bufsz);
372 char *here = buf;
373 char *committed = buf;
374 char *limit = buf + bufsz;
375 char *line;
376 bool is_language;
377 size_t langno = 0;
378 size_t nfiles = 0;
379 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
381 epos.file = input_file_by_name (listname);
382 epos.line = 0;
384 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
385 gt_files = XNEWVEC (const input_file *, num_gt_files);
387 for (;;)
389 next_line:
390 epos.line++;
391 committed = here;
392 is_language = read_input_line (list, &here, &line, &epos);
393 gcc_assert (here <= limit);
394 if (line == 0)
395 break;
396 else if (is_language)
398 size_t i;
399 gcc_assert (langno <= num_lang_dirs);
400 for (i = 0; i < langno; i++)
401 if (strcmp (lang_dir_names[i], line) == 0)
403 error_at_line (&epos, "duplicate language tag [%s]",
404 line);
405 curlangs = 1 << i;
406 here = committed;
407 goto next_line;
410 curlangs = 1 << langno;
411 lang_dir_names[langno++] = line;
413 else
415 size_t i;
416 input_file *inpf = input_file_by_name (line);
417 gcc_assert (nfiles <= num_gt_files);
418 for (i = 0; i < nfiles; i++)
419 /* Since the input_file-s are uniquely hash-consed, we
420 can just compare pointers! */
421 if (gt_files[i] == inpf)
423 /* Throw away the string we just read, and add the
424 current language to the existing string's bitmap. */
425 lang_bitmap bmap = get_lang_bitmap (inpf);
426 if (bmap & curlangs)
427 error_at_line (&epos,
428 "file %s specified more than once "
429 "for language %s", line,
430 langno ==
431 0 ? "(all)" : lang_dir_names[langno -
432 1]);
434 bmap |= curlangs;
435 set_lang_bitmap (inpf, bmap);
436 here = committed;
437 goto next_line;
440 set_lang_bitmap (inpf, curlangs);
441 gt_files[nfiles++] = inpf;
444 /* Update the global counts now that we know accurately how many
445 things there are. (We do not bother resizing the arrays down.) */
446 num_lang_dirs = langno;
447 /* Add the plugin files if provided. */
448 if (plugin_files)
450 size_t i;
451 for (i = 0; i < nb_plugin_files; i++)
452 gt_files[nfiles++] = plugin_files[i];
454 num_gt_files = nfiles;
457 /* Sanity check: any file that resides in a language subdirectory
458 (e.g. 'cp') ought to belong to the corresponding language.
459 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
460 (Can you even do that? Should you be allowed to?) */
462 size_t f;
463 for (f = 0; f < num_gt_files; f++)
465 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
466 const char *basename = get_file_basename (gt_files[f]);
467 const char *slashpos = strchr (basename, '/');
468 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
469 const char *slashpos2 = strchr (basename, '\\');
471 if (!slashpos || (slashpos2 && slashpos2 < slashpos))
472 slashpos = slashpos2;
473 #endif
475 if (slashpos)
477 size_t l;
478 for (l = 0; l < num_lang_dirs; l++)
479 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
480 && memcmp (basename, lang_dir_names[l],
481 strlen (lang_dir_names[l])) == 0)
483 if (!(bitmap & (1 << l)))
484 error ("%s is in language directory '%s' but is not "
485 "tagged for that language",
486 basename, lang_dir_names[l]);
487 break;
493 if (ferror (list))
494 fatal ("error reading %s: %s", listname, xstrerror (errno));
496 fclose (list);
501 /* The one and only TYPE_STRING. */
503 struct type string_type = {
504 TYPE_STRING, 0, 0, 0, GC_USED, {0}
507 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
508 set early in main. */
510 struct type scalar_nonchar = {
511 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
514 struct type scalar_char = {
515 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
518 /* Lists of various things. */
520 pair_p typedefs = NULL;
521 type_p structures = NULL;
522 type_p param_structs = NULL;
523 pair_p variables = NULL;
525 static type_p find_param_structure (type_p t, type_p param[NUM_PARAM]);
526 static type_p adjust_field_tree_exp (type_p t, options_p opt);
527 static type_p adjust_field_rtx_def (type_p t, options_p opt);
529 /* Define S as a typedef to T at POS. */
531 void
532 do_typedef (const char *s, type_p t, struct fileloc *pos)
534 pair_p p;
536 /* temporary kludge - gengtype doesn't handle conditionals or
537 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
538 is coming from this file (main() sets them up with safe dummy
539 definitions). */
540 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
541 return;
543 for (p = typedefs; p != NULL; p = p->next)
544 if (strcmp (p->name, s) == 0)
546 if (p->type != t && strcmp (s, "result_type") != 0)
548 error_at_line (pos, "type `%s' previously defined", s);
549 error_at_line (&p->line, "previously defined here");
551 return;
554 p = XNEW (struct pair);
555 p->next = typedefs;
556 p->name = s;
557 p->type = t;
558 p->line = *pos;
559 p->opt = NULL;
560 typedefs = p;
563 /* Define S as a typename of a scalar. Cannot be used to define
564 typedefs of 'char'. Note: is also used for pointer-to-function
565 typedefs (which are therefore not treated as pointers). */
567 void
568 do_scalar_typedef (const char *s, struct fileloc *pos)
570 do_typedef (s, &scalar_nonchar, pos);
573 /* Similar to strtok_r. */
575 static char *
576 strtoken (char *str, const char *delim, char **next)
578 char *p;
580 if (str == NULL)
581 str = *next;
583 /* Skip the leading delimiters. */
584 str += strspn (str, delim);
585 if (*str == '\0')
586 /* This is an empty token. */
587 return NULL;
589 /* The current token. */
590 p = str;
592 /* Find the next delimiter. */
593 str += strcspn (str, delim);
594 if (*str == '\0')
595 /* This is the last token. */
596 *next = str;
597 else
599 /* Terminate the current token. */
600 *str = '\0';
601 /* Advance to the next token. */
602 *next = str + 1;
605 return p;
608 /* Define TYPE_NAME to be a user defined type at location POS. */
610 type_p
611 create_user_defined_type (const char *type_name, struct fileloc *pos)
613 type_p ty = find_structure (type_name, TYPE_USER_STRUCT);
615 /* We might have already seen an incomplete decl of the given type,
616 in which case we won't have yet seen a GTY((user)), and the type will
617 only have kind "TYPE_STRUCT". Mark it as a user struct. */
618 ty->kind = TYPE_USER_STRUCT;
620 ty->u.s.line = *pos;
621 ty->u.s.bitmap = get_lang_bitmap (pos->file);
622 do_typedef (type_name, ty, pos);
624 /* If TYPE_NAME specifies a template, create references to the types
625 in the template by pretending that each type is a field of TY.
626 This is needed to make sure that the types referenced by the
627 template are marked as used. */
628 char *str = xstrdup (type_name);
629 char *open_bracket = strchr (str, '<');
630 if (open_bracket)
632 /* We only accept simple template declarations (see
633 require_template_declaration), so we only need to parse a
634 comma-separated list of strings, implicitly assumed to
635 be type names, potentially with "*" characters. */
636 char *arg = open_bracket + 1;
637 char *next;
638 char *type_id = strtoken (arg, ",>", &next);
639 pair_p fields = 0;
640 while (type_id)
642 /* Create a new field for every type found inside the template
643 parameter list. */
645 /* Support a single trailing "*" character. */
646 const char *star = strchr (type_id, '*');
647 int is_ptr = (star != NULL);
648 size_t offset_to_star = star - type_id;
649 if (is_ptr)
650 offset_to_star = star - type_id;
652 if (strstr (type_id, "char*"))
654 type_id = strtoken (0, ",>", &next);
655 continue;
658 char *field_name = xstrdup (type_id);
660 type_p arg_type;
661 if (is_ptr)
663 /* Strip off the first '*' character (and any subsequent text). */
664 *(field_name + offset_to_star) = '\0';
666 arg_type = find_structure (field_name, TYPE_STRUCT);
667 arg_type = create_pointer (arg_type);
669 else
670 arg_type = resolve_typedef (field_name, pos);
672 fields = create_field_at (fields, arg_type, field_name, 0, pos);
673 type_id = strtoken (0, ",>", &next);
676 /* Associate the field list to TY. */
677 ty->u.s.fields = fields;
679 free (str);
681 return ty;
685 /* Given a typedef name S, return its associated type. Return NULL if
686 S is not a registered type name. */
688 static type_p
689 type_for_name (const char *s)
691 pair_p p;
693 /* Special-case support for types within a "gcc::" namespace. Rather
694 than fully-supporting namespaces, simply strip off the "gcc::" prefix
695 where present. This allows us to have GTY roots of this form:
696 extern GTY(()) gcc::some_type *some_ptr;
697 where the autogenerated functions will refer to simply "some_type",
698 where they can be resolved into their namespace. */
699 if (0 == strncmp (s, "gcc::", 5))
700 s += 5;
702 for (p = typedefs; p != NULL; p = p->next)
703 if (strcmp (p->name, s) == 0)
704 return p->type;
705 return NULL;
709 /* Create an undefined type with name S and location POS. Return the
710 newly created type. */
712 static type_p
713 create_undefined_type (const char *s, struct fileloc *pos)
715 type_p ty = find_structure (s, TYPE_UNDEFINED);
716 ty->u.s.line = *pos;
717 ty->u.s.bitmap = get_lang_bitmap (pos->file);
718 do_typedef (s, ty, pos);
719 return ty;
723 /* Return the type previously defined for S. Use POS to report errors. */
725 type_p
726 resolve_typedef (const char *s, struct fileloc *pos)
728 bool is_template_instance = (strchr (s, '<') != NULL);
729 type_p p = type_for_name (s);
731 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
732 type for regular type identifiers. If the type identifier S is a
733 template instantiation, however, we treat it as a user defined
734 type.
736 FIXME, this is actually a limitation in gengtype. Supporting
737 template types and their instances would require keeping separate
738 track of the basic types definition and its instances. This
739 essentially forces all template classes in GC to be marked
740 GTY((user)). */
741 if (!p)
742 p = (is_template_instance)
743 ? create_user_defined_type (s, pos)
744 : create_undefined_type (s, pos);
746 return p;
749 /* Add SUBCLASS to head of linked list of BASE's subclasses. */
751 void add_subclass (type_p base, type_p subclass)
753 gcc_assert (union_or_struct_p (base));
754 gcc_assert (union_or_struct_p (subclass));
756 subclass->u.s.next_sibling_class = base->u.s.first_subclass;
757 base->u.s.first_subclass = subclass;
760 /* Create and return a new structure with tag NAME at POS with fields
761 FIELDS and options O. The KIND of structure must be one of
762 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
764 type_p
765 new_structure (const char *name, enum typekind kind, struct fileloc *pos,
766 pair_p fields, options_p o, type_p base_class)
768 type_p si;
769 type_p s = NULL;
770 lang_bitmap bitmap = get_lang_bitmap (pos->file);
771 bool isunion = (kind == TYPE_UNION);
773 gcc_assert (union_or_struct_p (kind));
775 for (si = structures; si != NULL; si = si->next)
776 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
778 type_p ls = NULL;
779 if (si->kind == TYPE_LANG_STRUCT)
781 ls = si;
783 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
784 if (si->u.s.bitmap == bitmap)
785 s = si;
787 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
789 ls = si;
790 type_count++;
791 si = XCNEW (struct type);
792 memcpy (si, ls, sizeof (struct type));
793 ls->kind = TYPE_LANG_STRUCT;
794 ls->u.s.lang_struct = si;
795 ls->u.s.fields = NULL;
796 si->next = NULL;
797 si->state_number = -type_count;
798 si->pointer_to = NULL;
799 si->u.s.lang_struct = ls;
801 else
802 s = si;
804 if (ls != NULL && s == NULL)
806 type_count++;
807 s = XCNEW (struct type);
808 s->state_number = -type_count;
809 s->next = ls->u.s.lang_struct;
810 ls->u.s.lang_struct = s;
811 s->u.s.lang_struct = ls;
813 break;
816 if (s == NULL)
818 type_count++;
819 s = XCNEW (struct type);
820 s->state_number = -type_count;
821 s->next = structures;
822 structures = s;
825 if (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap))
827 error_at_line (pos, "duplicate definition of '%s %s'",
828 isunion ? "union" : "struct", s->u.s.tag);
829 error_at_line (&s->u.s.line, "previous definition here");
832 s->kind = kind;
833 s->u.s.tag = name;
834 s->u.s.line = *pos;
835 s->u.s.fields = fields;
836 s->u.s.opt = o;
837 s->u.s.bitmap = bitmap;
838 if (s->u.s.lang_struct)
839 s->u.s.lang_struct->u.s.bitmap |= bitmap;
840 s->u.s.base_class = base_class;
841 if (base_class)
842 add_subclass (base_class, s);
844 return s;
847 /* Return the previously-defined structure or union with tag NAME,
848 or a new empty structure or union if none was defined previously.
849 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
850 TYPE_USER_STRUCT. */
852 type_p
853 find_structure (const char *name, enum typekind kind)
855 type_p s;
856 bool isunion = (kind == TYPE_UNION);
858 gcc_assert (kind == TYPE_UNDEFINED || union_or_struct_p (kind));
860 for (s = structures; s != NULL; s = s->next)
861 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
862 return s;
864 type_count++;
865 s = XCNEW (struct type);
866 s->next = structures;
867 s->state_number = -type_count;
868 structures = s;
869 s->kind = kind;
870 s->u.s.tag = name;
871 structures = s;
872 return s;
875 /* Return the previously-defined parameterized structure for structure
876 T and parameters PARAM, or a new parameterized empty structure or
877 union if none was defined previously. */
879 static type_p
880 find_param_structure (type_p t, type_p param[NUM_PARAM])
882 type_p res;
884 for (res = param_structs; res; res = res->next)
885 if (res->u.param_struct.stru == t
886 && memcmp (res->u.param_struct.param, param,
887 sizeof (type_p) * NUM_PARAM) == 0)
888 break;
889 if (res == NULL)
891 type_count++;
892 res = XCNEW (struct type);
893 res->kind = TYPE_PARAM_STRUCT;
894 res->next = param_structs;
895 res->state_number = -type_count;
896 param_structs = res;
897 res->u.param_struct.stru = t;
898 memcpy (res->u.param_struct.param, param, sizeof (type_p) * NUM_PARAM);
900 return res;
903 /* Return a scalar type with name NAME. */
905 type_p
906 create_scalar_type (const char *name)
908 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
909 return &scalar_char;
910 else
911 return &scalar_nonchar;
915 /* Return a pointer to T. */
917 type_p
918 create_pointer (type_p t)
920 if (!t->pointer_to)
922 type_p r = XCNEW (struct type);
923 type_count++;
924 r->state_number = -type_count;
925 r->kind = TYPE_POINTER;
926 r->u.p = t;
927 t->pointer_to = r;
929 return t->pointer_to;
932 /* Return an array of length LEN. */
934 type_p
935 create_array (type_p t, const char *len)
937 type_p v;
939 type_count++;
940 v = XCNEW (struct type);
941 v->kind = TYPE_ARRAY;
942 v->state_number = -type_count;
943 v->u.a.p = t;
944 v->u.a.len = len;
945 return v;
948 /* Return a string options structure with name NAME and info INFO.
949 NEXT is the next option in the chain. */
950 options_p
951 create_string_option (options_p next, const char *name, const char *info)
953 options_p o = XNEW (struct options);
954 o->kind = OPTION_STRING;
955 o->next = next;
956 o->name = name;
957 o->info.string = info;
958 return o;
961 /* Create a type options structure with name NAME and info INFO. NEXT
962 is the next option in the chain. */
963 options_p
964 create_type_option (options_p next, const char* name, type_p info)
966 options_p o = XNEW (struct options);
967 o->next = next;
968 o->name = name;
969 o->kind = OPTION_TYPE;
970 o->info.type = info;
971 return o;
974 /* Create a nested pointer options structure with name NAME and info
975 INFO. NEXT is the next option in the chain. */
976 options_p
977 create_nested_option (options_p next, const char* name,
978 struct nested_ptr_data* info)
980 options_p o;
981 o = XNEW (struct options);
982 o->next = next;
983 o->name = name;
984 o->kind = OPTION_NESTED;
985 o->info.nested = info;
986 return o;
989 /* Return an options structure for a "nested_ptr" option. */
990 options_p
991 create_nested_ptr_option (options_p next, type_p t,
992 const char *to, const char *from)
994 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
996 d->type = adjust_field_type (t, 0);
997 d->convert_to = to;
998 d->convert_from = from;
999 return create_nested_option (next, "nested_ptr", d);
1002 /* Add a variable named S of type T with options O defined at POS,
1003 to `variables'. */
1004 void
1005 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
1007 pair_p n;
1008 n = XNEW (struct pair);
1009 n->name = s;
1010 n->type = t;
1011 n->line = *pos;
1012 n->opt = o;
1013 n->next = variables;
1014 variables = n;
1017 /* Most-general structure field creator. */
1018 static pair_p
1019 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
1020 const input_file *inpf, int line)
1022 pair_p field;
1024 field = XNEW (struct pair);
1025 field->next = next;
1026 field->type = type;
1027 field->name = name;
1028 field->opt = opt;
1029 field->line.file = inpf;
1030 field->line.line = line;
1031 return field;
1034 /* Create a field that came from the source code we are scanning,
1035 i.e. we have a 'struct fileloc', and possibly options; also,
1036 adjust_field_type should be called. */
1037 pair_p
1038 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
1039 struct fileloc *pos)
1041 return create_field_all (next, adjust_field_type (type, opt),
1042 name, opt, pos->file, pos->line);
1045 /* Create a fake field with the given type and name. NEXT is the next
1046 field in the chain. */
1047 #define create_field(next,type,name) \
1048 create_field_all (next,type,name, 0, this_file, __LINE__)
1050 /* Like create_field, but the field is only valid when condition COND
1051 is true. */
1053 static pair_p
1054 create_optional_field_ (pair_p next, type_p type, const char *name,
1055 const char *cond, int line)
1057 static int id = 1;
1058 pair_p union_fields;
1059 type_p union_type;
1061 /* Create a fake union type with a single nameless field of type TYPE.
1062 The field has a tag of "1". This allows us to make the presence
1063 of a field of type TYPE depend on some boolean "desc" being true. */
1064 union_fields = create_field (NULL, type, "");
1065 union_fields->opt =
1066 create_string_option (union_fields->opt, "dot", "");
1067 union_fields->opt =
1068 create_string_option (union_fields->opt, "tag", "1");
1069 union_type =
1070 new_structure (xasprintf ("%s_%d", "fake_union", id++), TYPE_UNION,
1071 &lexer_line, union_fields, NULL, NULL);
1073 /* Create the field and give it the new fake union type. Add a "desc"
1074 tag that specifies the condition under which the field is valid. */
1075 return create_field_all (next, union_type, name,
1076 create_string_option (0, "desc", cond),
1077 this_file, line);
1080 #define create_optional_field(next,type,name,cond) \
1081 create_optional_field_(next,type,name,cond,__LINE__)
1083 /* Reverse a linked list of 'struct pair's in place. */
1084 pair_p
1085 nreverse_pairs (pair_p list)
1087 pair_p prev = 0, p, next;
1088 for (p = list; p; p = next)
1090 next = p->next;
1091 p->next = prev;
1092 prev = p;
1094 return prev;
1098 /* We don't care how long a CONST_DOUBLE is. */
1099 #define CONST_DOUBLE_FORMAT "ww"
1100 /* We don't want to see codes that are only for generator files. */
1101 #undef GENERATOR_FILE
1103 enum rtx_code
1105 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1106 #include "rtl.def"
1107 #undef DEF_RTL_EXPR
1108 NUM_RTX_CODE
1111 static const char *const rtx_name[NUM_RTX_CODE] = {
1112 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1113 #include "rtl.def"
1114 #undef DEF_RTL_EXPR
1117 static const char *const rtx_format[NUM_RTX_CODE] = {
1118 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1119 #include "rtl.def"
1120 #undef DEF_RTL_EXPR
1123 static int rtx_next_new[NUM_RTX_CODE];
1125 /* We also need codes and names for insn notes (not register notes).
1126 Note that we do *not* bias the note values here. */
1127 enum insn_note
1129 #define DEF_INSN_NOTE(NAME) NAME,
1130 #include "insn-notes.def"
1131 #undef DEF_INSN_NOTE
1133 NOTE_INSN_MAX
1136 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1137 default field for line number notes. */
1138 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
1139 #define DEF_INSN_NOTE(NAME) #NAME,
1140 #include "insn-notes.def"
1141 #undef DEF_INSN_NOTE
1144 #undef CONST_DOUBLE_FORMAT
1145 #define GENERATOR_FILE
1147 /* Generate the contents of the rtx_next array. This really doesn't belong
1148 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1150 static void
1151 gen_rtx_next (void)
1153 int i;
1154 for (i = 0; i < NUM_RTX_CODE; i++)
1156 int k;
1158 rtx_next_new[i] = -1;
1159 if (strncmp (rtx_format[i], "uu", 2) == 0)
1160 rtx_next_new[i] = 1;
1161 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1162 rtx_next_new[i] = 1;
1163 else
1164 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
1165 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1166 rtx_next_new[i] = k;
1170 /* Write out the contents of the rtx_next array. */
1171 static void
1172 write_rtx_next (void)
1174 outf_p f = get_output_file_with_visibility (NULL);
1175 int i;
1176 if (!f)
1177 return;
1179 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
1180 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1181 for (i = 0; i < NUM_RTX_CODE; i++)
1182 if (rtx_next_new[i] == -1)
1183 oprintf (f, " 0,\n");
1184 else
1185 oprintf (f,
1186 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1187 oprintf (f, "};\n");
1190 /* Handle `special("rtx_def")'. This is a special case for field
1191 `fld' of struct rtx_def, which is an array of unions whose values
1192 are based in a complex way on the type of RTL. */
1194 static type_p
1195 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1197 pair_p flds = NULL;
1198 options_p nodot;
1199 int i;
1200 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1201 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1203 if (t->kind != TYPE_UNION)
1205 error_at_line (&lexer_line,
1206 "special `rtx_def' must be applied to a union");
1207 return &string_type;
1210 nodot = create_string_option (NULL, "dot", "");
1212 rtx_tp = create_pointer (find_structure ("rtx_def", TYPE_STRUCT));
1213 rtvec_tp = create_pointer (find_structure ("rtvec_def", TYPE_STRUCT));
1214 tree_tp = create_pointer (find_structure ("tree_node", TYPE_UNION));
1215 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", TYPE_STRUCT));
1216 reg_attrs_tp =
1217 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT));
1218 basic_block_tp =
1219 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT));
1220 constant_tp =
1221 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT));
1222 scalar_tp = &scalar_nonchar; /* rtunion int */
1225 pair_p note_flds = NULL;
1226 int c;
1228 for (c = 0; c <= NOTE_INSN_MAX; c++)
1230 switch (c)
1232 case NOTE_INSN_MAX:
1233 case NOTE_INSN_DELETED_LABEL:
1234 case NOTE_INSN_DELETED_DEBUG_LABEL:
1235 note_flds = create_field (note_flds, &string_type, "rt_str");
1236 break;
1238 case NOTE_INSN_BLOCK_BEG:
1239 case NOTE_INSN_BLOCK_END:
1240 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1241 break;
1243 case NOTE_INSN_VAR_LOCATION:
1244 case NOTE_INSN_CALL_ARG_LOCATION:
1245 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1246 break;
1248 default:
1249 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1250 break;
1252 /* NOTE_INSN_MAX is used as the default field for line
1253 number notes. */
1254 if (c == NOTE_INSN_MAX)
1255 note_flds->opt =
1256 create_string_option (nodot, "default", "");
1257 else
1258 note_flds->opt =
1259 create_string_option (nodot, "tag", note_insn_name[c]);
1261 note_union_tp = new_structure ("rtx_def_note_subunion", TYPE_UNION,
1262 &lexer_line, note_flds, NULL, NULL);
1264 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1266 pair_p sym_flds;
1267 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1268 sym_flds->opt = create_string_option (nodot, "default", "");
1269 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1270 sym_flds->opt = create_string_option (nodot, "tag", "1");
1271 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", TYPE_UNION,
1272 &lexer_line, sym_flds, NULL, NULL);
1274 for (i = 0; i < NUM_RTX_CODE; i++)
1276 pair_p subfields = NULL;
1277 size_t aindex, nmindex;
1278 const char *sname;
1279 type_p substruct;
1280 char *ftag;
1282 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1284 type_p t;
1285 const char *subname;
1287 switch (rtx_format[i][aindex])
1289 case '*':
1290 case 'i':
1291 case 'n':
1292 case 'w':
1293 t = scalar_tp;
1294 subname = "rt_int";
1295 break;
1297 case '0':
1298 if (i == MEM && aindex == 1)
1299 t = mem_attrs_tp, subname = "rt_mem";
1300 else if (i == JUMP_INSN && aindex == 7)
1301 t = rtx_tp, subname = "rt_rtx";
1302 else if (i == CODE_LABEL && aindex == 4)
1303 t = scalar_tp, subname = "rt_int";
1304 else if (i == CODE_LABEL && aindex == 3)
1305 t = rtx_tp, subname = "rt_rtx";
1306 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1307 t = rtx_tp, subname = "rt_rtx";
1308 else if (i == NOTE && aindex == 3)
1309 t = note_union_tp, subname = "";
1310 else if (i == NOTE && aindex == 4)
1311 t = scalar_tp, subname = "rt_int";
1312 else if (i == NOTE && aindex >= 6)
1313 t = scalar_tp, subname = "rt_int";
1314 else if (i == ADDR_DIFF_VEC && aindex == 4)
1315 t = scalar_tp, subname = "rt_int";
1316 else if (i == VALUE && aindex == 0)
1317 t = scalar_tp, subname = "rt_int";
1318 else if (i == DEBUG_EXPR && aindex == 0)
1319 t = tree_tp, subname = "rt_tree";
1320 else if (i == REG && aindex == 1)
1321 t = reg_attrs_tp, subname = "rt_reg";
1322 else if (i == SYMBOL_REF && aindex == 1)
1323 t = symbol_union_tp, subname = "";
1324 else if (i == JUMP_TABLE_DATA && aindex >= 4)
1325 t = scalar_tp, subname = "rt_int";
1326 else if (i == BARRIER && aindex >= 2)
1327 t = scalar_tp, subname = "rt_int";
1328 else if (i == ENTRY_VALUE && aindex == 0)
1329 t = rtx_tp, subname = "rt_rtx";
1330 else
1332 error_at_line
1333 (&lexer_line,
1334 "rtx type `%s' has `0' in position %lu, can't handle",
1335 rtx_name[i], (unsigned long) aindex);
1336 t = &string_type;
1337 subname = "rt_int";
1339 break;
1341 case 's':
1342 case 'S':
1343 case 'T':
1344 t = &string_type;
1345 subname = "rt_str";
1346 break;
1348 case 'e':
1349 case 'u':
1350 t = rtx_tp;
1351 subname = "rt_rtx";
1352 break;
1354 case 'E':
1355 case 'V':
1356 t = rtvec_tp;
1357 subname = "rt_rtvec";
1358 break;
1360 case 't':
1361 t = tree_tp;
1362 subname = "rt_tree";
1363 break;
1365 case 'B':
1366 t = basic_block_tp;
1367 subname = "rt_bb";
1368 break;
1370 default:
1371 error_at_line
1372 (&lexer_line,
1373 "rtx type `%s' has `%c' in position %lu, can't handle",
1374 rtx_name[i], rtx_format[i][aindex],
1375 (unsigned long) aindex);
1376 t = &string_type;
1377 subname = "rt_int";
1378 break;
1381 subfields = create_field (subfields, t,
1382 xasprintf (".fld[%lu].%s",
1383 (unsigned long) aindex,
1384 subname));
1385 subfields->opt = nodot;
1386 if (t == note_union_tp)
1387 subfields->opt =
1388 create_string_option (subfields->opt, "desc",
1389 "NOTE_KIND (&%0)");
1390 if (t == symbol_union_tp)
1391 subfields->opt =
1392 create_string_option (subfields->opt, "desc",
1393 "CONSTANT_POOL_ADDRESS_P (&%0)");
1396 if (i == SYMBOL_REF)
1398 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1399 holds. */
1400 type_p field_tp = find_structure ("block_symbol", TYPE_STRUCT);
1401 subfields
1402 = create_optional_field (subfields, field_tp, "block_sym",
1403 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1406 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1407 substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
1408 NULL, NULL);
1410 ftag = xstrdup (rtx_name[i]);
1411 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1412 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1413 flds = create_field (flds, substruct, "");
1414 flds->opt = create_string_option (nodot, "tag", ftag);
1416 return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
1417 nodot, NULL);
1420 /* Handle `special("tree_exp")'. This is a special case for
1421 field `operands' of struct tree_exp, which although it claims to contain
1422 pointers to trees, actually sometimes contains pointers to RTL too.
1423 Passed T, the old type of the field, and OPT its options. Returns
1424 a new type for the field. */
1426 static type_p
1427 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1429 pair_p flds;
1430 options_p nodot;
1432 if (t->kind != TYPE_ARRAY)
1434 error_at_line (&lexer_line,
1435 "special `tree_exp' must be applied to an array");
1436 return &string_type;
1439 nodot = create_string_option (NULL, "dot", "");
1441 flds = create_field (NULL, t, "");
1442 flds->opt = create_string_option (nodot, "length",
1443 "TREE_OPERAND_LENGTH ((tree) &%0)");
1444 flds->opt = create_string_option (flds->opt, "default", "");
1446 return new_structure ("tree_exp_subunion", TYPE_UNION, &lexer_line, flds,
1447 nodot, NULL);
1450 /* Perform any special processing on a type T, about to become the type
1451 of a field. Return the appropriate type for the field.
1452 At present:
1453 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1454 - Similarly for arrays of pointer-to-char;
1455 - Converts structures for which a parameter is provided to
1456 TYPE_PARAM_STRUCT;
1457 - Handles "special" options.
1460 type_p
1461 adjust_field_type (type_p t, options_p opt)
1463 int length_p = 0;
1464 const int pointer_p = t->kind == TYPE_POINTER;
1465 type_p params[NUM_PARAM];
1466 int params_p = 0;
1467 int i;
1469 for (i = 0; i < NUM_PARAM; i++)
1470 params[i] = NULL;
1472 for (; opt; opt = opt->next)
1473 if (strcmp (opt->name, "length") == 0)
1475 if (length_p)
1476 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1477 if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
1479 error_at_line (&lexer_line,
1480 "option `%s' may not be applied to "
1481 "arrays of atomic types", opt->name);
1483 length_p = 1;
1485 else if ((strcmp (opt->name, "param_is") == 0
1486 || (strncmp (opt->name, "param", 5) == 0
1487 && ISDIGIT (opt->name[5])
1488 && strcmp (opt->name + 6, "_is") == 0))
1489 && opt->kind == OPTION_TYPE)
1491 int num = ISDIGIT (opt->name[5]) ? opt->name[5] - '0' : 0;
1493 if (!union_or_struct_p (t)
1494 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
1496 error_at_line (&lexer_line,
1497 "option `%s' may only be applied to structures or structure pointers",
1498 opt->name);
1499 return t;
1502 params_p = 1;
1503 if (params[num] != NULL)
1504 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1505 if (!ISDIGIT (opt->name[5]))
1506 params[num] = create_pointer (opt->info.type);
1507 else
1508 params[num] = opt->info.type;
1510 else if (strcmp (opt->name, "special") == 0
1511 && opt->kind == OPTION_STRING)
1513 const char *special_name = opt->info.string;
1514 if (strcmp (special_name, "tree_exp") == 0)
1515 t = adjust_field_tree_exp (t, opt);
1516 else if (strcmp (special_name, "rtx_def") == 0)
1517 t = adjust_field_rtx_def (t, opt);
1518 else
1519 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1522 if (params_p)
1524 type_p realt;
1526 if (pointer_p)
1527 t = t->u.p;
1528 realt = find_param_structure (t, params);
1529 t = pointer_p ? create_pointer (realt) : realt;
1532 if (!length_p
1533 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1534 return &string_type;
1535 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1536 && t->u.a.p->u.p->kind == TYPE_SCALAR
1537 && t->u.a.p->u.p->u.scalar_is_char)
1538 return create_array (&string_type, t->u.a.len);
1540 return t;
1544 static void set_gc_used_type (type_p, enum gc_used_enum, type_p *,
1545 bool = false);
1546 static void set_gc_used (pair_p);
1548 /* Handle OPT for set_gc_used_type. */
1550 static void
1551 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1552 int *pass_param, int *length, int *skip,
1553 type_p *nested_ptr)
1555 options_p o;
1556 for (o = opt; o; o = o->next)
1557 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1558 && o->kind == OPTION_TYPE)
1559 set_gc_used_type (o->info.type,
1560 GC_POINTED_TO, NULL);
1561 else if (strcmp (o->name, "maybe_undef") == 0)
1562 *maybe_undef = 1;
1563 else if (strcmp (o->name, "use_params") == 0)
1564 *pass_param = 1;
1565 else if (strcmp (o->name, "length") == 0)
1566 *length = 1;
1567 else if (strcmp (o->name, "skip") == 0)
1568 *skip = 1;
1569 else if (strcmp (o->name, "nested_ptr") == 0
1570 && o->kind == OPTION_NESTED)
1571 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1575 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1577 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1578 are set to GC_UNUSED. Otherwise, an error is emitted for
1579 TYPE_UNDEFINED types. This is used to support user-defined
1580 template types with non-type arguments.
1582 For instance, when we parse a template type with enum arguments
1583 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1584 artificial fields for 'MyType', one for 'AnotherType', the other
1585 one for 'EnumValue'.
1587 At the time that we parse this type we don't know that 'EnumValue'
1588 is really an enum value, so the parser creates a TYPE_UNDEFINED
1589 type for it. Since 'EnumValue' is never resolved to a known
1590 structure, it will stay with TYPE_UNDEFINED.
1592 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1593 'EnumValue'. Generating marking code for it would cause
1594 compilation failures since the marking routines assumes that
1595 'EnumValue' is a type. */
1597 static void
1598 set_gc_used_type (type_p t, enum gc_used_enum level, type_p param[NUM_PARAM],
1599 bool allow_undefined_types)
1601 if (t->gc_used >= level)
1602 return;
1604 t->gc_used = level;
1606 switch (t->kind)
1608 case TYPE_STRUCT:
1609 case TYPE_UNION:
1610 case TYPE_USER_STRUCT:
1612 pair_p f;
1613 int dummy;
1614 type_p dummy2;
1615 bool allow_undefined_field_types = (t->kind == TYPE_USER_STRUCT);
1617 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
1618 &dummy2);
1620 if (t->u.s.base_class)
1621 set_gc_used_type (t->u.s.base_class, level, param,
1622 allow_undefined_types);
1623 /* Anything pointing to a base class might actually be pointing
1624 to a subclass. */
1625 for (type_p subclass = t->u.s.first_subclass; subclass;
1626 subclass = subclass->u.s.next_sibling_class)
1627 set_gc_used_type (subclass, level, param,
1628 allow_undefined_types);
1630 FOR_ALL_INHERITED_FIELDS(t, f)
1632 int maybe_undef = 0;
1633 int pass_param = 0;
1634 int length = 0;
1635 int skip = 0;
1636 type_p nested_ptr = NULL;
1637 process_gc_options (f->opt, level, &maybe_undef, &pass_param,
1638 &length, &skip, &nested_ptr);
1640 if (nested_ptr && f->type->kind == TYPE_POINTER)
1641 set_gc_used_type (nested_ptr, GC_POINTED_TO,
1642 pass_param ? param : NULL);
1643 else if (length && f->type->kind == TYPE_POINTER)
1644 set_gc_used_type (f->type->u.p, GC_USED, NULL);
1645 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1646 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO, NULL);
1647 else if (pass_param && f->type->kind == TYPE_POINTER && param)
1648 set_gc_used_type (find_param_structure (f->type->u.p, param),
1649 GC_POINTED_TO, NULL);
1650 else if (skip)
1651 ; /* target type is not used through this field */
1652 else
1653 set_gc_used_type (f->type, GC_USED, pass_param ? param : NULL,
1654 allow_undefined_field_types);
1656 break;
1659 case TYPE_UNDEFINED:
1660 if (level > GC_UNUSED)
1662 if (!allow_undefined_types)
1663 error_at_line (&t->u.s.line, "undefined type `%s'", t->u.s.tag);
1664 t->gc_used = GC_UNUSED;
1666 break;
1668 case TYPE_POINTER:
1669 set_gc_used_type (t->u.p, GC_POINTED_TO, NULL);
1670 break;
1672 case TYPE_ARRAY:
1673 set_gc_used_type (t->u.a.p, GC_USED, param);
1674 break;
1676 case TYPE_LANG_STRUCT:
1677 for (t = t->u.s.lang_struct; t; t = t->next)
1678 set_gc_used_type (t, level, param);
1679 break;
1681 case TYPE_PARAM_STRUCT:
1683 int i;
1684 for (i = 0; i < NUM_PARAM; i++)
1685 if (t->u.param_struct.param[i] != 0)
1686 set_gc_used_type (t->u.param_struct.param[i], GC_USED, NULL);
1688 if (t->u.param_struct.stru->gc_used == GC_POINTED_TO)
1689 level = GC_POINTED_TO;
1690 else
1691 level = GC_USED;
1692 t->u.param_struct.stru->gc_used = GC_UNUSED;
1693 set_gc_used_type (t->u.param_struct.stru, level,
1694 t->u.param_struct.param);
1695 break;
1697 default:
1698 break;
1702 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1704 static void
1705 set_gc_used (pair_p variables)
1707 int nbvars = 0;
1708 pair_p p;
1709 for (p = variables; p; p = p->next)
1711 set_gc_used_type (p->type, GC_USED, NULL);
1712 nbvars++;
1714 if (verbosity_level >= 2)
1715 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1718 /* File mapping routines. For each input file, there is one output .c file
1719 (but some output files have many input files), and there is one .h file
1720 for the whole build. */
1722 /* Output file handling. */
1724 /* Create and return an outf_p for a new file for NAME, to be called
1725 ONAME. */
1727 static outf_p
1728 create_file (const char *name, const char *oname)
1730 static const char *const hdr[] = {
1731 " Copyright (C) 2004-2014 Free Software Foundation, Inc.\n",
1732 "\n",
1733 "This file is part of GCC.\n",
1734 "\n",
1735 "GCC is free software; you can redistribute it and/or modify it under\n",
1736 "the terms of the GNU General Public License as published by the Free\n",
1737 "Software Foundation; either version 3, or (at your option) any later\n",
1738 "version.\n",
1739 "\n",
1740 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1741 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1742 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1743 "for more details.\n",
1744 "\n",
1745 "You should have received a copy of the GNU General Public License\n",
1746 "along with GCC; see the file COPYING3. If not see\n",
1747 "<http://www.gnu.org/licenses/>. */\n",
1748 "\n",
1749 "/* This file is machine generated. Do not edit. */\n"
1751 outf_p f;
1752 size_t i;
1754 gcc_assert (name != NULL);
1755 gcc_assert (oname != NULL);
1756 f = XCNEW (struct outf);
1757 f->next = output_files;
1758 f->name = oname;
1759 output_files = f;
1761 oprintf (f, "/* Type information for %s.\n", name);
1762 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1763 oprintf (f, "%s", hdr[i]);
1764 return f;
1767 /* Print, like fprintf, to O.
1768 N.B. You might think this could be implemented more efficiently
1769 with vsnprintf(). Unfortunately, there are C libraries that
1770 provide that function but without the C99 semantics for its return
1771 value, making it impossible to know how much space is required. */
1772 void
1773 oprintf (outf_p o, const char *format, ...)
1775 char *s;
1776 size_t slength;
1777 va_list ap;
1779 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1780 in that case. */
1781 if (!o)
1782 return;
1784 va_start (ap, format);
1785 slength = vasprintf (&s, format, ap);
1786 if (s == NULL || (int) slength < 0)
1787 fatal ("out of memory");
1788 va_end (ap);
1790 if (o->bufused + slength > o->buflength)
1792 size_t new_len = o->buflength;
1793 if (new_len == 0)
1794 new_len = 1024;
1797 new_len *= 2;
1799 while (o->bufused + slength >= new_len);
1800 o->buf = XRESIZEVEC (char, o->buf, new_len);
1801 o->buflength = new_len;
1803 memcpy (o->buf + o->bufused, s, slength);
1804 o->bufused += slength;
1805 free (s);
1808 /* Open the global header file and the language-specific header files. */
1810 static void
1811 open_base_files (void)
1813 size_t i;
1815 if (nb_plugin_files > 0 && plugin_files)
1816 return;
1818 header_file = create_file ("GCC", "gtype-desc.h");
1820 base_files = XNEWVEC (outf_p, num_lang_dirs);
1822 for (i = 0; i < num_lang_dirs; i++)
1823 base_files[i] = create_file (lang_dir_names[i],
1824 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1826 /* gtype-desc.c is a little special, so we create it here. */
1828 /* The order of files here matters very much. */
1829 static const char *const ifiles[] = {
1830 "config.h", "system.h", "coretypes.h", "tm.h", "insn-codes.h",
1831 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1832 "tree.h", "rtl.h", "wide-int.h", "hashtab.h", "hash-set.h", "vec.h",
1833 "machmode.h", "tm.h", "hard-reg-set.h", "input.h", "predict.h",
1834 "function.h", "insn-config.h", "expr.h", "alloc-pool.h",
1835 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1836 "optabs.h", "libfuncs.h", "debug.h", "ggc.h",
1837 "hash-table.h", "vec.h", "ggc.h", "dominance.h", "cfg.h", "basic-block.h",
1838 "tree-ssa-alias.h", "internal-fn.h", "gimple-fold.h", "tree-eh.h",
1839 "gimple-expr.h", "is-a.h",
1840 "gimple.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
1841 "tree-phinodes.h", "ssa-iterators.h", "stringpool.h", "tree-ssanames.h",
1842 "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h", "tree-ssa-loop-manip.h",
1843 "tree-ssa-loop-niter.h", "tree-into-ssa.h", "tree-dfa.h",
1844 "tree-ssa.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1845 "except.h", "output.h", "cfgloop.h", "target.h", "lto-streamer.h",
1846 "target-globals.h", "ipa-ref.h", "cgraph.h", "ipa-prop.h",
1847 "ipa-inline.h", "dwarf2out.h", "omp-low.h", NULL
1849 const char *const *ifp;
1850 outf_p gtype_desc_c;
1852 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1853 for (ifp = ifiles; *ifp; ifp++)
1854 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1856 /* Make sure we handle "cfun" specially. */
1857 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1858 oprintf (gtype_desc_c, "#undef cfun\n");
1860 oprintf (gtype_desc_c,
1861 "\n"
1862 "/* Types with a \"gcc::\" namespace have it stripped\n"
1863 " during gengtype parsing. Provide a \"using\" directive\n"
1864 " to ensure that the fully-qualified types are found. */\n"
1865 "using namespace gcc;\n");
1869 /* For INPF an input file, return the real basename of INPF, with all
1870 the directory components skipped. */
1872 static const char *
1873 get_file_realbasename (const input_file *inpf)
1875 return lbasename (get_input_file_name (inpf));
1878 /* For INPF a filename, return the relative path to INPF from
1879 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1881 const char *
1882 get_file_srcdir_relative_path (const input_file *inpf)
1884 const char *f = get_input_file_name (inpf);
1885 if (strlen (f) > srcdir_len
1886 && IS_DIR_SEPARATOR (f[srcdir_len])
1887 && strncmp (f, srcdir, srcdir_len) == 0)
1888 return f + srcdir_len + 1;
1889 else
1890 return NULL;
1893 /* For INPF an input_file, return the relative path to INPF from
1894 $(srcdir) if the latter is a prefix in INPF, or the real basename
1895 of INPF otherwise. */
1897 static const char *
1898 get_file_basename (const input_file *inpf)
1900 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1902 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1905 /* For F a filename, return the lang_dir_names relative index of the language
1906 directory that is a prefix in F, if any, -1 otherwise. */
1908 static int
1909 get_prefix_langdir_index (const char *f)
1911 size_t f_len = strlen (f);
1912 size_t lang_index;
1914 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1916 const char *langdir = lang_dir_names[lang_index];
1917 size_t langdir_len = strlen (langdir);
1919 if (f_len > langdir_len
1920 && IS_DIR_SEPARATOR (f[langdir_len])
1921 && memcmp (f, langdir, langdir_len) == 0)
1922 return lang_index;
1925 return -1;
1928 /* For INPF an input file, return the name of language directory where
1929 F is located, if any, NULL otherwise. */
1931 static const char *
1932 get_file_langdir (const input_file *inpf)
1934 /* Get the relative path to INPF from $(srcdir) and find the
1935 language by comparing the prefix with language directory names.
1936 If INPF is not even srcdir relative, no point in looking
1937 further. */
1939 int lang_index;
1940 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1941 const char *r;
1943 if (!srcdir_relative_path)
1944 return NULL;
1946 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1947 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1948 r = "c-family";
1949 else if (lang_index >= 0)
1950 r = lang_dir_names[lang_index];
1951 else
1952 r = NULL;
1954 return r;
1957 /* The gt- output file name for INPF. */
1959 static const char *
1960 get_file_gtfilename (const input_file *inpf)
1962 /* Cook up an initial version of the gt- file name from the file real
1963 basename and the language name, if any. */
1965 const char *basename = get_file_realbasename (inpf);
1966 const char *langdir = get_file_langdir (inpf);
1968 char *result =
1969 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1970 : xasprintf ("gt-%s", basename));
1972 /* Then replace all non alphanumerics characters by '-' and change the
1973 extension to ".h". We expect the input filename extension was at least
1974 one character long. */
1976 char *s = result;
1978 for (; *s != '.'; s++)
1979 if (!ISALNUM (*s) && *s != '-')
1980 *s = '-';
1982 memcpy (s, ".h", sizeof (".h"));
1984 return result;
1987 /* Each input_file has its associated output file outf_p. The
1988 association is computed by the function
1989 get_output_file_with_visibility. The associated file is cached
1990 inside input_file in its inpoutf field, so is really computed only
1991 once. Associated output file paths (i.e. output_name-s) are
1992 computed by a rule based regexp machinery, using the files_rules
1993 array of struct file_rule_st. A for_name is also computed, giving
1994 the source file name for which the output_file is generated; it is
1995 often the last component of the input_file path. */
1999 Regexpr machinery to compute the output_name and for_name-s of each
2000 input_file. We have a sequence of file rules which gives the POSIX
2001 extended regular expression to match an input file path, and two
2002 transformed strings for the corresponding output_name and the
2003 corresponding for_name. The transformed string contain dollars: $0
2004 is replaced by the entire match, $1 is replaced by the substring
2005 matching the first parenthesis in the regexp, etc. And $$ is replaced
2006 by a single verbatim dollar. The rule order is important. The
2007 general case is last, and the particular cases should come before.
2008 An action routine can, when needed, update the out_name & for_name
2009 and/or return the appropriate output file. It is invoked only when a
2010 rule is triggered. When a rule is triggered, the output_name and
2011 for_name are computed using their transform string in while $$, $0,
2012 $1, ... are suitably replaced. If there is an action, it is called.
2013 In some few cases, the action can directly return the outf_p, but
2014 usually it just updates the output_name and for_name so should free
2015 them before replacing them. The get_output_file_with_visibility
2016 function creates an outf_p only once per each output_name, so it
2017 scans the output_files list for previously seen output file names.
2020 /* Signature of actions in file rules. */
2021 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
2024 struct file_rule_st {
2025 const char* frul_srcexpr; /* Source string for regexp. */
2026 int frul_rflags; /* Flags passed to regcomp, usually
2027 * REG_EXTENDED. */
2028 regex_t* frul_re; /* Compiled regular expression
2029 obtained by regcomp. */
2030 const char* frul_tr_out; /* Transformation string for making
2031 * the output_name, with $1 ... $9 for
2032 * subpatterns and $0 for the whole
2033 * matched filename. */
2034 const char* frul_tr_for; /* Tranformation string for making the
2035 for_name. */
2036 frul_actionrout_t* frul_action; /* The action, if non null, is
2037 * called once the rule matches, on
2038 * the transformed out_name &
2039 * for_name. It could change them
2040 * and/or give the output file. */
2043 /* File rule action handling *.h files. */
2044 static outf_p header_dot_h_frul (input_file*, char**, char**);
2046 /* File rule action handling *.c files. */
2047 static outf_p source_dot_c_frul (input_file*, char**, char**);
2049 #define NULL_REGEX (regex_t*)0
2051 /* The prefix in our regexp-s matching the directory. */
2052 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
2054 #define NULL_FRULACT (frul_actionrout_t*)0
2056 /* The array of our rules governing file name generation. Rules order
2057 matters, so change with extreme care! */
2059 struct file_rule_st files_rules[] = {
2060 /* The general rule assumes that files in subdirectories belong to a
2061 particular front-end, and files not in subdirectories are shared.
2062 The following rules deal with exceptions - files that are in
2063 subdirectories and yet are shared, and files that are top-level,
2064 but are not shared. */
2066 /* the c-family/ source directory is special. */
2067 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
2068 REG_EXTENDED, NULL_REGEX,
2069 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
2071 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
2072 REG_EXTENDED, NULL_REGEX,
2073 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
2075 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
2076 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
2077 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
2079 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
2080 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
2082 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
2083 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
2084 REG_EXTENDED, NULL_REGEX,
2085 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
2087 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
2088 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
2089 REG_EXTENDED, NULL_REGEX,
2090 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
2092 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
2093 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
2094 REG_EXTENDED, NULL_REGEX,
2095 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
2097 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
2098 { DIR_PREFIX_REGEX "cp/parser\\.h$",
2099 REG_EXTENDED, NULL_REGEX,
2100 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
2102 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
2103 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
2104 REG_EXTENDED, NULL_REGEX,
2105 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
2107 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
2108 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
2109 REG_EXTENDED, NULL_REGEX,
2110 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
2112 /* General cases. For header *.h and source *.c or *.cc files, we
2113 * need special actions to handle the language. */
2115 /* Source *.c files are using get_file_gtfilename to compute their
2116 output_name and get_file_basename to compute their for_name
2117 through the source_dot_c_frul action. */
2118 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
2119 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
2121 /* Source *.cc files are using get_file_gtfilename to compute their
2122 output_name and get_file_basename to compute their for_name
2123 through the source_dot_c_frul action. */
2124 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.cc$",
2125 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.cc", source_dot_c_frul},
2127 /* Common header files get "gtype-desc.c" as their output_name,
2128 * while language specific header files are handled specially. So
2129 * we need the header_dot_h_frul action. */
2130 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
2131 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
2133 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
2134 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
2136 /* Mandatory null last entry signaling end of rules. */
2137 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
2140 /* Special file rules action for handling *.h header files. It gives
2141 "gtype-desc.c" for common headers and corresponding output
2142 files for language-specific header files. */
2143 static outf_p
2144 header_dot_h_frul (input_file* inpf, char**poutname,
2145 char**pforname ATTRIBUTE_UNUSED)
2147 const char *basename = 0;
2148 int lang_index = 0;
2149 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2150 (void*) inpf, get_input_file_name (inpf),
2151 *poutname, *pforname);
2152 basename = get_file_basename (inpf);
2153 lang_index = get_prefix_langdir_index (basename);
2154 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
2156 if (lang_index >= 0)
2158 /* The header is language specific. Given output_name &
2159 for_name remains unchanged. The base_files array gives the
2160 outf_p. */
2161 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2162 (void*) base_files[lang_index],
2163 (base_files[lang_index])->name);
2164 return base_files[lang_index];
2166 else
2168 /* The header is common to all front-end languages. So
2169 output_name is "gtype-desc.c" file. The calling function
2170 get_output_file_with_visibility will find its outf_p. */
2171 free (*poutname);
2172 *poutname = xstrdup ("gtype-desc.c");
2173 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2174 get_input_file_name (inpf));
2175 return NULL;
2180 /* Special file rules action for handling *.c source files using
2181 * get_file_gtfilename to compute their output_name and
2182 * get_file_basename to compute their for_name. The output_name is
2183 * gt-<LANG>-<BASE>.h for language specific source files, and
2184 * gt-<BASE>.h for common source files. */
2185 static outf_p
2186 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
2188 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
2189 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
2190 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2191 (void*) inpf, get_input_file_name (inpf),
2192 *poutname, *pforname);
2193 DBGPRINTF ("newoutname %s", newoutname);
2194 DBGPRINTF ("newbasename %s", newbasename);
2195 free (*poutname);
2196 free (*pforname);
2197 *poutname = newoutname;
2198 *pforname = newbasename;
2199 return NULL;
2202 /* Utility function for get_output_file_with_visibility which returns
2203 * a malloc-ed substituted string using TRS on matching of the FILNAM
2204 * file name, using the PMATCH array. */
2205 static char*
2206 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
2207 const char *trs)
2209 struct obstack str_obstack;
2210 char *str = NULL;
2211 char *rawstr = NULL;
2212 const char *pt = NULL;
2213 DBGPRINTF ("filnam %s", filnam);
2214 obstack_init (&str_obstack);
2215 for (pt = trs; *pt; pt++) {
2216 char c = *pt;
2217 if (c == '$')
2219 if (pt[1] == '$')
2221 /* A double dollar $$ is substituted by a single verbatim
2222 dollar, but who really uses dollar signs in file
2223 paths? */
2224 obstack_1grow (&str_obstack, '$');
2226 else if (ISDIGIT (pt[1]))
2228 /* Handle $0 $1 ... $9 by appropriate substitution. */
2229 int dolnum = pt[1] - '0';
2230 int so = pmatch[dolnum].rm_so;
2231 int eo = pmatch[dolnum].rm_eo;
2232 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2233 if (so>=0 && eo>=so)
2234 obstack_grow (&str_obstack, filnam + so, eo - so);
2236 else
2238 /* This can happen only when files_rules is buggy! */
2239 gcc_unreachable ();
2241 /* Always skip the character after the dollar. */
2242 pt++;
2244 else
2245 obstack_1grow (&str_obstack, c);
2247 obstack_1grow (&str_obstack, '\0');
2248 rawstr = XOBFINISH (&str_obstack, char *);
2249 str = xstrdup (rawstr);
2250 obstack_free (&str_obstack, NULL);
2251 DBGPRINTF ("matched replacement %s", str);
2252 rawstr = NULL;
2253 return str;
2257 /* An output file, suitable for definitions, that can see declarations
2258 made in INPF and is linked into every language that uses INPF.
2259 Since the result is cached inside INPF, that argument cannot be
2260 declared constant, but is "almost" constant. */
2262 outf_p
2263 get_output_file_with_visibility (input_file *inpf)
2265 outf_p r;
2266 char *for_name = NULL;
2267 char *output_name = NULL;
2268 const char* inpfname;
2270 /* This can happen when we need a file with visibility on a
2271 structure that we've never seen. We have to just hope that it's
2272 globally visible. */
2273 if (inpf == NULL)
2274 inpf = system_h_file;
2276 /* The result is cached in INPF, so return it if already known. */
2277 if (inpf->inpoutf)
2278 return inpf->inpoutf;
2280 /* In plugin mode, return NULL unless the input_file is one of the
2281 plugin_files. */
2282 if (plugin_files)
2284 size_t i;
2285 for (i = 0; i < nb_plugin_files; i++)
2286 if (inpf == plugin_files[i])
2288 inpf->inpoutf = plugin_output;
2289 return plugin_output;
2292 return NULL;
2295 inpfname = get_input_file_name (inpf);
2297 /* Try each rule in sequence in files_rules until one is triggered. */
2299 int rulix = 0;
2300 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2301 (void*) inpf, inpfname);
2303 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2305 DBGPRINTF ("rulix#%d srcexpr %s",
2306 rulix, files_rules[rulix].frul_srcexpr);
2308 if (!files_rules[rulix].frul_re)
2310 /* Compile the regexpr lazily. */
2311 int err = 0;
2312 files_rules[rulix].frul_re = XCNEW (regex_t);
2313 err = regcomp (files_rules[rulix].frul_re,
2314 files_rules[rulix].frul_srcexpr,
2315 files_rules[rulix].frul_rflags);
2316 if (err)
2318 /* The regular expression compilation fails only when
2319 file_rules is buggy. */
2320 gcc_unreachable ();
2324 output_name = NULL;
2325 for_name = NULL;
2327 /* Match the regexpr and trigger the rule if matched. */
2329 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2330 $3, ... $9. */
2331 regmatch_t pmatch[10];
2332 memset (pmatch, 0, sizeof (pmatch));
2333 if (!regexec (files_rules[rulix].frul_re,
2334 inpfname, 10, pmatch, 0))
2336 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2337 (void*) inpf, inpfname, rulix,
2338 files_rules[rulix].frul_srcexpr);
2339 for_name =
2340 matching_file_name_substitute (inpfname, pmatch,
2341 files_rules[rulix].frul_tr_for);
2342 DBGPRINTF ("for_name %s", for_name);
2343 output_name =
2344 matching_file_name_substitute (inpfname, pmatch,
2345 files_rules[rulix].frul_tr_out);
2346 DBGPRINTF ("output_name %s", output_name);
2347 if (files_rules[rulix].frul_action)
2349 /* Invoke our action routine. */
2350 outf_p of = NULL;
2351 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2352 rulix, output_name, for_name);
2353 of =
2354 (files_rules[rulix].frul_action) (inpf,
2355 &output_name, &for_name);
2356 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2357 rulix, (void*)of, output_name, for_name);
2358 /* If the action routine returned something, give it back
2359 immediately and cache it in inpf. */
2360 if (of)
2362 inpf->inpoutf = of;
2363 return of;
2366 /* The rule matched, and had no action, or that action did
2367 not return any output file but could have changed the
2368 output_name or for_name. We break out of the loop on the
2369 files_rules. */
2370 break;
2372 else
2374 /* The regexpr did not match. */
2375 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2376 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2377 continue;
2382 if (!output_name || !for_name)
2384 /* This should not be possible, and could only happen if the
2385 files_rules is incomplete or buggy. */
2386 fatal ("failed to compute output name for %s", inpfname);
2389 /* Look through to see if we've ever seen this output filename
2390 before. If found, cache the result in inpf. */
2391 for (r = output_files; r; r = r->next)
2392 if (filename_cmp (r->name, output_name) == 0)
2394 inpf->inpoutf = r;
2395 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2396 output_name, for_name);
2397 return r;
2400 /* If not found, create it, and cache it in inpf. */
2401 r = create_file (for_name, output_name);
2403 gcc_assert (r && r->name);
2404 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2405 output_name, for_name);
2406 inpf->inpoutf = r;
2407 return r;
2412 /* The name of an output file, suitable for definitions, that can see
2413 declarations made in INPF and is linked into every language that
2414 uses INPF. */
2416 const char *
2417 get_output_file_name (input_file* inpf)
2419 outf_p o = get_output_file_with_visibility (inpf);
2420 if (o)
2421 return o->name;
2422 return NULL;
2425 /* Check if existing file is equal to the in memory buffer. */
2427 static bool
2428 is_file_equal (outf_p of)
2430 FILE *newfile = fopen (of->name, "r");
2431 size_t i;
2432 bool equal;
2433 if (newfile == NULL)
2434 return false;
2436 equal = true;
2437 for (i = 0; i < of->bufused; i++)
2439 int ch;
2440 ch = fgetc (newfile);
2441 if (ch == EOF || ch != (unsigned char) of->buf[i])
2443 equal = false;
2444 break;
2447 if (equal && EOF != fgetc (newfile))
2448 equal = false;
2449 fclose (newfile);
2450 return equal;
2453 /* Copy the output to its final destination,
2454 but don't unnecessarily change modification times. */
2456 static void
2457 close_output_files (void)
2459 int nbwrittenfiles = 0;
2460 outf_p of;
2462 for (of = output_files; of; of = of->next)
2464 if (!is_file_equal (of))
2466 FILE *newfile = NULL;
2467 char *backupname = NULL;
2468 /* Back up the old version of the output file gt-FOO.c as
2469 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2470 if (backup_dir)
2472 backupname = concat (backup_dir, "/",
2473 lbasename (of->name), "~", NULL);
2474 if (!access (of->name, F_OK) && rename (of->name, backupname))
2475 fatal ("failed to back up %s as %s: %s",
2476 of->name, backupname, xstrerror (errno));
2479 newfile = fopen (of->name, "w");
2480 if (newfile == NULL)
2481 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2482 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2483 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2484 if (fclose (newfile) != 0)
2485 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2486 nbwrittenfiles++;
2487 if (verbosity_level >= 2 && backupname)
2488 printf ("%s wrote #%-3d %s backed-up in %s\n",
2489 progname, nbwrittenfiles, of->name, backupname);
2490 else if (verbosity_level >= 1)
2491 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2492 free (backupname);
2494 else
2496 /* output file remains unchanged. */
2497 if (verbosity_level >= 2)
2498 printf ("%s keep %s\n", progname, of->name);
2500 free (of->buf);
2501 of->buf = NULL;
2502 of->bufused = of->buflength = 0;
2504 if (verbosity_level >= 1)
2505 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2508 struct flist
2510 struct flist *next;
2511 int started_p;
2512 const input_file* file;
2513 outf_p f;
2516 struct walk_type_data;
2518 /* For scalars and strings, given the item in 'val'.
2519 For structures, given a pointer to the item in 'val'.
2520 For misc. pointers, given the item in 'val'.
2522 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2523 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2525 /* Parameters for write_types. */
2527 struct write_types_data
2529 const char *prefix;
2530 const char *param_prefix;
2531 const char *subfield_marker_routine;
2532 const char *marker_routine;
2533 const char *reorder_note_routine;
2534 const char *comment;
2535 int skip_hooks; /* skip hook generation if non zero */
2536 enum write_types_kinds kind;
2539 static void output_escaped_param (struct walk_type_data *d,
2540 const char *, const char *);
2541 static void output_mangled_typename (outf_p, const_type_p);
2542 static void walk_type (type_p t, struct walk_type_data *d);
2543 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2544 const struct write_types_data *wtd);
2545 static void write_types_process_field
2546 (type_p f, const struct walk_type_data *d);
2547 static void write_types (outf_p output_header,
2548 type_p structures,
2549 type_p param_structs,
2550 const struct write_types_data *wtd);
2551 static void write_types_local_process_field
2552 (type_p f, const struct walk_type_data *d);
2553 static void write_local_func_for_structure
2554 (const_type_p orig_s, type_p s, type_p *param);
2555 static void write_local (outf_p output_header,
2556 type_p structures, type_p param_structs);
2557 static int contains_scalar_p (type_p t);
2558 static void put_mangled_filename (outf_p, const input_file *);
2559 static void finish_root_table (struct flist *flp, const char *pfx,
2560 const char *tname, const char *lastname,
2561 const char *name);
2562 static void write_root (outf_p, pair_p, type_p, const char *, int,
2563 struct fileloc *, const char *, bool);
2564 static void write_array (outf_p f, pair_p v,
2565 const struct write_types_data *wtd);
2566 static void write_roots (pair_p, bool);
2568 /* Parameters for walk_type. */
2570 struct walk_type_data
2572 process_field_fn process_field;
2573 const void *cookie;
2574 outf_p of;
2575 options_p opt;
2576 const char *val;
2577 const char *prev_val[4];
2578 int indent;
2579 int counter;
2580 const struct fileloc *line;
2581 lang_bitmap bitmap;
2582 type_p *param;
2583 int used_length;
2584 type_p orig_s;
2585 const char *reorder_fn;
2586 bool needs_cast_p;
2587 bool fn_wants_lvalue;
2588 bool in_record_p;
2589 int loopcounter;
2590 bool in_ptr_field;
2591 bool have_this_obj;
2595 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2596 pre-processor identifier to use in a #define directive. This replaces
2597 special characters used in C++ identifiers like '>', '<' and ':' with
2598 '_'.
2600 If no C++ special characters are found in TYPE_NAME, return
2601 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2602 characters replaced with '_'. In this case, the caller is
2603 responsible for freeing the allocated string. */
2605 static const char *
2606 filter_type_name (const char *type_name)
2608 if (strchr (type_name, '<') || strchr (type_name, ':'))
2610 size_t i;
2611 char *s = xstrdup (type_name);
2612 for (i = 0; i < strlen (s); i++)
2613 if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ','
2614 || s[i] == '*')
2615 s[i] = '_';
2616 return s;
2618 else
2619 return type_name;
2623 /* Print a mangled name representing T to OF. */
2625 static void
2626 output_mangled_typename (outf_p of, const_type_p t)
2628 if (t == NULL)
2629 oprintf (of, "Z");
2630 else
2631 switch (t->kind)
2633 case TYPE_NONE:
2634 case TYPE_UNDEFINED:
2635 gcc_unreachable ();
2636 break;
2637 case TYPE_POINTER:
2638 oprintf (of, "P");
2639 output_mangled_typename (of, t->u.p);
2640 break;
2641 case TYPE_SCALAR:
2642 oprintf (of, "I");
2643 break;
2644 case TYPE_STRING:
2645 oprintf (of, "S");
2646 break;
2647 case TYPE_STRUCT:
2648 case TYPE_UNION:
2649 case TYPE_LANG_STRUCT:
2650 case TYPE_USER_STRUCT:
2652 /* For references to classes within an inheritance hierarchy,
2653 only ever reference the ultimate base class, since only
2654 it will have gt_ functions. */
2655 t = get_ultimate_base_class (t);
2656 const char *id_for_tag = filter_type_name (t->u.s.tag);
2657 oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2658 id_for_tag);
2659 if (id_for_tag != t->u.s.tag)
2660 free (CONST_CAST (char *, id_for_tag));
2662 break;
2663 case TYPE_PARAM_STRUCT:
2665 int i;
2666 for (i = 0; i < NUM_PARAM; i++)
2667 if (t->u.param_struct.param[i] != NULL)
2668 output_mangled_typename (of, t->u.param_struct.param[i]);
2669 output_mangled_typename (of, t->u.param_struct.stru);
2671 break;
2672 case TYPE_ARRAY:
2673 gcc_unreachable ();
2677 /* Print PARAM to D->OF processing escapes. D->VAL references the
2678 current object, D->PREV_VAL the object containing the current
2679 object, ONAME is the name of the option and D->LINE is used to
2680 print error messages. */
2682 static void
2683 output_escaped_param (struct walk_type_data *d, const char *param,
2684 const char *oname)
2686 const char *p;
2688 for (p = param; *p; p++)
2689 if (*p != '%')
2690 oprintf (d->of, "%c", *p);
2691 else
2692 switch (*++p)
2694 case 'h':
2695 oprintf (d->of, "(%s)", d->prev_val[2]);
2696 break;
2697 case '0':
2698 oprintf (d->of, "(%s)", d->prev_val[0]);
2699 break;
2700 case '1':
2701 oprintf (d->of, "(%s)", d->prev_val[1]);
2702 break;
2703 case 'a':
2705 const char *pp = d->val + strlen (d->val);
2706 while (pp[-1] == ']')
2707 while (*pp != '[')
2708 pp--;
2709 oprintf (d->of, "%s", pp);
2711 break;
2712 default:
2713 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2714 oname, '%', *p);
2718 const char *
2719 get_string_option (options_p opt, const char *key)
2721 for (; opt; opt = opt->next)
2722 if (strcmp (opt->name, key) == 0)
2723 return opt->info.string;
2724 return NULL;
2727 /* Machinery for avoiding duplicate tags within switch statements. */
2728 struct seen_tag
2730 const char *tag;
2731 struct seen_tag *next;
2735 already_seen_tag (struct seen_tag *seen_tags, const char *tag)
2737 /* Linear search, so O(n^2), but n is currently small. */
2738 while (seen_tags)
2740 if (!strcmp (seen_tags->tag, tag))
2741 return 1;
2742 seen_tags = seen_tags->next;
2744 /* Not yet seen this tag. */
2745 return 0;
2748 void
2749 mark_tag_as_seen (struct seen_tag **seen_tags, const char *tag)
2751 /* Add to front of linked list. */
2752 struct seen_tag *new_node = XCNEW (struct seen_tag);
2753 new_node->tag = tag;
2754 new_node->next = *seen_tags;
2755 *seen_tags = new_node;
2758 static void
2759 walk_subclasses (type_p base, struct walk_type_data *d,
2760 struct seen_tag **seen_tags)
2762 for (type_p sub = base->u.s.first_subclass; sub != NULL;
2763 sub = sub->u.s.next_sibling_class)
2765 const char *type_tag = get_string_option (sub->u.s.opt, "tag");
2766 if (type_tag && !already_seen_tag (*seen_tags, type_tag))
2768 mark_tag_as_seen (seen_tags, type_tag);
2769 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
2770 d->indent += 2;
2771 oprintf (d->of, "%*s{\n", d->indent, "");
2772 d->indent += 2;
2773 oprintf (d->of, "%*s%s *sub = static_cast <%s *> (x);\n",
2774 d->indent, "", sub->u.s.tag, sub->u.s.tag);
2775 const char *old_val = d->val;
2776 d->val = "(*sub)";
2777 walk_type (sub, d);
2778 d->val = old_val;
2779 d->indent -= 2;
2780 oprintf (d->of, "%*s}\n", d->indent, "");
2781 oprintf (d->of, "%*sbreak;\n", d->indent, "");
2782 d->indent -= 2;
2784 walk_subclasses (sub, d, seen_tags);
2788 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2789 which is of type T. Write code to D->OF to constrain execution (at
2790 the point that D->PROCESS_FIELD is called) to the appropriate
2791 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2792 pointers to those objects. D->PREV_VAL lists the objects
2793 containing the current object, D->OPT is a list of options to
2794 apply, D->INDENT is the current indentation level, D->LINE is used
2795 to print error messages, D->BITMAP indicates which languages to
2796 print the structure for, and D->PARAM is the current parameter
2797 (from an enclosing param_is option). */
2799 static void
2800 walk_type (type_p t, struct walk_type_data *d)
2802 const char *length = NULL;
2803 const char *desc = NULL;
2804 const char *type_tag = NULL;
2805 int maybe_undef_p = 0;
2806 int use_param_num = -1;
2807 int use_params_p = 0;
2808 int atomic_p = 0;
2809 options_p oo;
2810 const struct nested_ptr_data *nested_ptr_d = NULL;
2812 d->needs_cast_p = false;
2813 for (oo = d->opt; oo; oo = oo->next)
2814 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2815 length = oo->info.string;
2816 else if (strcmp (oo->name, "maybe_undef") == 0)
2817 maybe_undef_p = 1;
2818 else if (strncmp (oo->name, "use_param", 9) == 0
2819 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2820 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2821 else if (strcmp (oo->name, "use_params") == 0)
2822 use_params_p = 1;
2823 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2824 desc = oo->info.string;
2825 else if (strcmp (oo->name, "mark_hook") == 0)
2827 else if (strcmp (oo->name, "nested_ptr") == 0
2828 && oo->kind == OPTION_NESTED)
2829 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2830 else if (strcmp (oo->name, "dot") == 0)
2832 else if (strcmp (oo->name, "tag") == 0)
2833 type_tag = oo->info.string;
2834 else if (strcmp (oo->name, "special") == 0)
2836 else if (strcmp (oo->name, "skip") == 0)
2838 else if (strcmp (oo->name, "atomic") == 0)
2839 atomic_p = 1;
2840 else if (strcmp (oo->name, "default") == 0)
2842 else if (strcmp (oo->name, "param_is") == 0)
2844 else if (strncmp (oo->name, "param", 5) == 0
2845 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2847 else if (strcmp (oo->name, "chain_next") == 0)
2849 else if (strcmp (oo->name, "chain_prev") == 0)
2851 else if (strcmp (oo->name, "chain_circular") == 0)
2853 else if (strcmp (oo->name, "reorder") == 0)
2855 else if (strcmp (oo->name, "variable_size") == 0)
2857 else if (strcmp (oo->name, "for_user") == 0)
2859 else
2860 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2862 if (d->used_length)
2863 length = NULL;
2865 if (use_params_p)
2867 int pointer_p = t->kind == TYPE_POINTER;
2869 if (pointer_p)
2870 t = t->u.p;
2871 if (!union_or_struct_p (t))
2872 error_at_line (d->line, "`use_params' option on unimplemented type");
2873 else
2874 t = find_param_structure (t, d->param);
2875 if (pointer_p)
2876 t = create_pointer (t);
2879 if (use_param_num != -1)
2881 if (d->param != NULL && d->param[use_param_num] != NULL)
2883 type_p nt = d->param[use_param_num];
2885 if (t->kind == TYPE_ARRAY)
2886 nt = create_array (nt, t->u.a.len);
2887 else if (length != NULL && t->kind == TYPE_POINTER)
2888 nt = create_pointer (nt);
2889 d->needs_cast_p = (t->kind != TYPE_POINTER
2890 && (nt->kind == TYPE_POINTER
2891 || nt->kind == TYPE_STRING));
2892 t = nt;
2894 else
2895 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2898 if (maybe_undef_p
2899 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
2901 error_at_line (d->line,
2902 "field `%s' has invalid option `maybe_undef_p'\n",
2903 d->val);
2904 return;
2907 if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2909 error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2910 return;
2913 switch (t->kind)
2915 case TYPE_SCALAR:
2916 case TYPE_STRING:
2917 d->process_field (t, d);
2918 break;
2920 case TYPE_POINTER:
2922 d->in_ptr_field = true;
2923 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2925 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2926 break;
2929 /* If a pointer type is marked as "atomic", we process the
2930 field itself, but we don't walk the data that they point to.
2932 There are two main cases where we walk types: to mark
2933 pointers that are reachable, and to relocate pointers when
2934 writing a PCH file. In both cases, an atomic pointer is
2935 itself marked or relocated, but the memory that it points
2936 to is left untouched. In the case of PCH, that memory will
2937 be read/written unchanged to the PCH file. */
2938 if (atomic_p)
2940 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2941 d->indent += 2;
2942 d->process_field (t, d);
2943 d->indent -= 2;
2944 oprintf (d->of, "%*s}\n", d->indent, "");
2945 break;
2948 if (!length)
2950 if (!union_or_struct_p (t->u.p)
2951 && t->u.p->kind != TYPE_PARAM_STRUCT)
2953 error_at_line (d->line,
2954 "field `%s' is pointer to unimplemented type",
2955 d->val);
2956 break;
2959 if (nested_ptr_d)
2961 const char *oldprevval2 = d->prev_val[2];
2963 if (!union_or_struct_p (nested_ptr_d->type))
2965 error_at_line (d->line,
2966 "field `%s' has invalid "
2967 "option `nested_ptr'\n", d->val);
2968 return;
2971 d->prev_val[2] = d->val;
2972 oprintf (d->of, "%*s{\n", d->indent, "");
2973 d->indent += 2;
2974 d->val = xasprintf ("x%d", d->counter++);
2975 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2976 (nested_ptr_d->type->kind == TYPE_UNION
2977 ? "union" : "struct"),
2978 nested_ptr_d->type->u.s.tag,
2979 d->fn_wants_lvalue ? "" : "const ", d->val);
2980 oprintf (d->of, "%*s", d->indent + 2, "");
2981 output_escaped_param (d, nested_ptr_d->convert_from,
2982 "nested_ptr");
2983 oprintf (d->of, ";\n");
2985 d->process_field (nested_ptr_d->type, d);
2987 if (d->fn_wants_lvalue)
2989 oprintf (d->of, "%*s%s = ", d->indent, "",
2990 d->prev_val[2]);
2991 d->prev_val[2] = d->val;
2992 output_escaped_param (d, nested_ptr_d->convert_to,
2993 "nested_ptr");
2994 oprintf (d->of, ";\n");
2997 d->indent -= 2;
2998 oprintf (d->of, "%*s}\n", d->indent, "");
2999 d->val = d->prev_val[2];
3000 d->prev_val[2] = oldprevval2;
3002 else
3003 d->process_field (t->u.p, d);
3005 else
3007 int loopcounter = d->loopcounter;
3008 const char *oldval = d->val;
3009 const char *oldprevval3 = d->prev_val[3];
3010 char *newval;
3012 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
3013 d->indent += 2;
3014 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
3015 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
3016 "", loopcounter, loopcounter);
3017 if (!d->in_record_p)
3018 output_escaped_param (d, length, "length");
3019 else
3020 oprintf (d->of, "l%d", loopcounter);
3021 if (d->have_this_obj)
3022 /* Try to unswitch loops (see PR53880). */
3023 oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
3024 oprintf (d->of, "); i%d++) {\n", loopcounter);
3025 d->indent += 2;
3026 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
3027 d->used_length = 1;
3028 d->prev_val[3] = oldval;
3029 walk_type (t->u.p, d);
3030 free (newval);
3031 d->val = oldval;
3032 d->prev_val[3] = oldprevval3;
3033 d->used_length = 0;
3034 d->indent -= 2;
3035 oprintf (d->of, "%*s}\n", d->indent, "");
3036 d->process_field (t, d);
3037 d->indent -= 2;
3038 oprintf (d->of, "%*s}\n", d->indent, "");
3040 d->in_ptr_field = false;
3042 break;
3044 case TYPE_ARRAY:
3046 int loopcounter;
3047 const char *oldval = d->val;
3048 char *newval;
3050 /* If it's an array of scalars, we optimize by not generating
3051 any code. */
3052 if (t->u.a.p->kind == TYPE_SCALAR)
3053 break;
3055 if (length)
3056 loopcounter = d->loopcounter;
3057 else
3058 loopcounter = d->counter++;
3060 /* When walking an array, compute the length and store it in a
3061 local variable before walking the array elements, instead of
3062 recomputing the length expression each time through the loop.
3063 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
3064 where the length is stored in the first array element,
3065 because otherwise that operand can get overwritten on the
3066 first iteration. */
3067 oprintf (d->of, "%*s{\n", d->indent, "");
3068 d->indent += 2;
3069 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
3070 if (!d->in_record_p || !length)
3072 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
3073 d->indent, "", loopcounter);
3074 if (length)
3075 output_escaped_param (d, length, "length");
3076 else
3077 oprintf (d->of, "%s", t->u.a.len);
3078 oprintf (d->of, ");\n");
3081 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
3082 d->indent, "",
3083 loopcounter, loopcounter, loopcounter, loopcounter);
3084 d->indent += 2;
3085 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
3086 d->used_length = 1;
3087 walk_type (t->u.a.p, d);
3088 free (newval);
3089 d->used_length = 0;
3090 d->val = oldval;
3091 d->indent -= 2;
3092 oprintf (d->of, "%*s}\n", d->indent, "");
3093 d->indent -= 2;
3094 oprintf (d->of, "%*s}\n", d->indent, "");
3096 break;
3098 case TYPE_STRUCT:
3099 case TYPE_UNION:
3101 pair_p f;
3102 const char *oldval = d->val;
3103 const char *oldprevval1 = d->prev_val[1];
3104 const char *oldprevval2 = d->prev_val[2];
3105 const char *struct_mark_hook = NULL;
3106 const int union_p = t->kind == TYPE_UNION;
3107 int seen_default_p = 0;
3108 options_p o;
3109 int lengths_seen = 0;
3110 int endcounter;
3111 bool any_length_seen = false;
3113 if (!t->u.s.line.file)
3114 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
3116 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
3118 error_at_line (d->line,
3119 "structure `%s' defined for mismatching languages",
3120 t->u.s.tag);
3121 error_at_line (&t->u.s.line, "one structure defined here");
3124 /* Some things may also be defined in the structure's options. */
3125 for (o = t->u.s.opt; o; o = o->next)
3126 if (!desc && strcmp (o->name, "desc") == 0
3127 && o->kind == OPTION_STRING)
3128 desc = o->info.string;
3129 else if (!struct_mark_hook && strcmp (o->name, "mark_hook") == 0
3130 && o->kind == OPTION_STRING)
3131 struct_mark_hook = o->info.string;
3133 if (struct_mark_hook)
3134 oprintf (d->of, "%*s%s (&%s);\n",
3135 d->indent, "", struct_mark_hook, oldval);
3137 d->prev_val[2] = oldval;
3138 d->prev_val[1] = oldprevval2;
3139 if (union_p)
3141 if (desc == NULL)
3143 error_at_line (d->line,
3144 "missing `desc' option for union `%s'",
3145 t->u.s.tag);
3146 desc = "1";
3148 oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
3149 output_escaped_param (d, desc, "desc");
3150 oprintf (d->of, "))\n");
3151 d->indent += 2;
3152 oprintf (d->of, "%*s{\n", d->indent, "");
3154 else if (desc)
3156 /* We have a "desc" option on a struct, signifying the
3157 base class within a GC-managed inheritance hierarchy.
3158 The current code specialcases the base class, then walks
3159 into subclasses, recursing into this routine to handle them.
3160 This organization requires the base class to have a case in
3161 the switch statement, and hence a tag value is mandatory
3162 for the base class. This restriction could be removed, but
3163 it would require some restructing of this code. */
3164 if (!type_tag)
3166 error_at_line (d->line,
3167 "missing `tag' option for type `%s'",
3168 t->u.s.tag);
3170 oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
3171 output_escaped_param (d, desc, "desc");
3172 oprintf (d->of, "))\n");
3173 d->indent += 2;
3174 oprintf (d->of, "%*s{\n", d->indent, "");
3175 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
3176 d->indent += 2;
3179 FOR_ALL_INHERITED_FIELDS (t, f)
3181 options_p oo;
3182 int skip_p = 0;
3183 const char *fieldlength = NULL;
3185 d->reorder_fn = NULL;
3186 for (oo = f->opt; oo; oo = oo->next)
3187 if (strcmp (oo->name, "skip") == 0)
3188 skip_p = 1;
3189 else if (strcmp (oo->name, "length") == 0
3190 && oo->kind == OPTION_STRING)
3191 fieldlength = oo->info.string;
3193 if (skip_p)
3194 continue;
3195 if (fieldlength)
3197 lengths_seen++;
3198 d->counter++;
3199 if (!union_p)
3201 if (!any_length_seen)
3203 oprintf (d->of, "%*s{\n", d->indent, "");
3204 d->indent += 2;
3206 any_length_seen = true;
3208 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
3209 d->indent, "", d->counter - 1);
3210 output_escaped_param (d, fieldlength, "length");
3211 oprintf (d->of, ");\n");
3215 endcounter = d->counter;
3217 FOR_ALL_INHERITED_FIELDS (t, f)
3219 options_p oo;
3220 const char *dot = ".";
3221 const char *tagid = NULL;
3222 int skip_p = 0;
3223 int default_p = 0;
3224 int use_param_p = 0;
3225 const char *fieldlength = NULL;
3226 char *newval;
3228 d->reorder_fn = NULL;
3229 for (oo = f->opt; oo; oo = oo->next)
3230 if (strcmp (oo->name, "dot") == 0
3231 && oo->kind == OPTION_STRING)
3232 dot = oo->info.string;
3233 else if (strcmp (oo->name, "tag") == 0
3234 && oo->kind == OPTION_STRING)
3235 tagid = oo->info.string;
3236 else if (strcmp (oo->name, "skip") == 0)
3237 skip_p = 1;
3238 else if (strcmp (oo->name, "default") == 0)
3239 default_p = 1;
3240 else if (strcmp (oo->name, "reorder") == 0
3241 && oo->kind == OPTION_STRING)
3242 d->reorder_fn = oo->info.string;
3243 else if (strncmp (oo->name, "use_param", 9) == 0
3244 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
3245 use_param_p = 1;
3246 else if (strcmp (oo->name, "length") == 0
3247 && oo->kind == OPTION_STRING)
3248 fieldlength = oo->info.string;
3250 if (skip_p)
3251 continue;
3253 if (union_p && tagid)
3255 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
3256 d->indent += 2;
3258 else if (union_p && default_p)
3260 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3261 d->indent += 2;
3262 seen_default_p = 1;
3264 else if (!union_p && (default_p || tagid))
3265 error_at_line (d->line,
3266 "can't use `%s' outside a union on field `%s'",
3267 default_p ? "default" : "tag", f->name);
3268 else if (union_p && !(default_p || tagid)
3269 && f->type->kind == TYPE_SCALAR)
3271 fprintf (stderr,
3272 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3273 get_input_file_name (d->line->file), d->line->line,
3274 f->name);
3275 continue;
3277 else if (union_p && !(default_p || tagid))
3278 error_at_line (d->line,
3279 "field `%s' is missing `tag' or `default' option",
3280 f->name);
3282 if (fieldlength)
3284 d->loopcounter = endcounter - lengths_seen--;
3287 d->line = &f->line;
3288 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
3289 d->opt = f->opt;
3290 d->used_length = false;
3291 d->in_record_p = !union_p;
3293 if (union_p && use_param_p && d->param == NULL)
3294 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
3295 else
3296 walk_type (f->type, d);
3298 d->in_record_p = false;
3300 free (newval);
3302 if (union_p)
3304 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3305 d->indent -= 2;
3308 d->reorder_fn = NULL;
3310 d->val = oldval;
3311 d->prev_val[1] = oldprevval1;
3312 d->prev_val[2] = oldprevval2;
3314 if (union_p && !seen_default_p)
3316 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3317 oprintf (d->of, "%*s break;\n", d->indent, "");
3320 if (desc && !union_p)
3322 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3323 d->indent -= 2;
3325 if (union_p)
3327 oprintf (d->of, "%*s}\n", d->indent, "");
3328 d->indent -= 2;
3330 else if (desc)
3332 /* Add cases to handle subclasses. */
3333 struct seen_tag *tags = NULL;
3334 walk_subclasses (t, d, &tags);
3336 /* Ensure that if someone forgets a "tag" option that we don't
3337 silent fail to traverse that subclass's fields. */
3338 if (!seen_default_p)
3340 oprintf (d->of, "%*s/* Unrecognized tag value. */\n",
3341 d->indent, "");
3342 oprintf (d->of, "%*sdefault: gcc_unreachable (); \n",
3343 d->indent, "");
3346 /* End of the switch statement */
3347 oprintf (d->of, "%*s}\n", d->indent, "");
3348 d->indent -= 2;
3350 if (any_length_seen)
3352 d->indent -= 2;
3353 oprintf (d->of, "%*s}\n", d->indent, "");
3356 break;
3358 case TYPE_LANG_STRUCT:
3360 type_p nt;
3361 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3362 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3363 break;
3364 if (nt == NULL)
3365 error_at_line (d->line, "structure `%s' differs between languages",
3366 t->u.s.tag);
3367 else
3368 walk_type (nt, d);
3370 break;
3372 case TYPE_PARAM_STRUCT:
3374 type_p *oldparam = d->param;
3376 d->param = t->u.param_struct.param;
3377 walk_type (t->u.param_struct.stru, d);
3378 d->param = oldparam;
3380 break;
3382 case TYPE_USER_STRUCT:
3383 d->process_field (t, d);
3384 break;
3386 case TYPE_NONE:
3387 case TYPE_UNDEFINED:
3388 gcc_unreachable ();
3392 /* process_field routine for marking routines. */
3394 static void
3395 write_types_process_field (type_p f, const struct walk_type_data *d)
3397 const struct write_types_data *wtd;
3398 const char *cast = d->needs_cast_p ? "(void *)" : "";
3399 wtd = (const struct write_types_data *) d->cookie;
3401 switch (f->kind)
3403 case TYPE_NONE:
3404 case TYPE_UNDEFINED:
3405 gcc_unreachable ();
3406 case TYPE_POINTER:
3407 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
3408 wtd->subfield_marker_routine, cast, d->val);
3409 if (wtd->param_prefix)
3411 if (f->u.p->kind == TYPE_SCALAR)
3412 /* The current type is a pointer to a scalar (so not
3413 considered like a pointer to instances of user defined
3414 types) and we are seeing it; it means we must be even
3415 more careful about the second argument of the
3416 SUBFIELD_MARKER_ROUTINE call. That argument must
3417 always be the instance of the type for which
3418 write_func_for_structure was called - this really is
3419 what the function SUBFIELD_MARKER_ROUTINE expects.
3420 That is, it must be an instance of the ORIG_S type
3421 parameter of write_func_for_structure. The convention
3422 is that that argument must be "x" in that case (as set
3423 by write_func_for_structure). The problem is, we can't
3424 count on d->prev_val[3] to be always set to "x" in that
3425 case. Sometimes walk_type can set it to something else
3426 (to e.g cooperate with write_array when called from
3427 write_roots). So let's set it to "x" here then. */
3428 oprintf (d->of, ", x");
3429 else
3430 oprintf (d->of, ", %s", d->prev_val[3]);
3431 if (d->orig_s)
3433 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3434 output_mangled_typename (d->of, d->orig_s);
3436 else
3437 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3439 oprintf (d->of, ");\n");
3440 if (d->reorder_fn && wtd->reorder_note_routine)
3441 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
3442 wtd->reorder_note_routine, cast, d->val,
3443 d->prev_val[3], d->reorder_fn);
3444 break;
3446 case TYPE_STRING:
3447 case TYPE_STRUCT:
3448 case TYPE_UNION:
3449 case TYPE_LANG_STRUCT:
3450 case TYPE_PARAM_STRUCT:
3451 case TYPE_USER_STRUCT:
3452 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3454 /* If F is a user-defined type and the field is not a
3455 pointer to the type, then we should not generate the
3456 standard pointer-marking code. All we need to do is call
3457 the user-provided marking function to process the fields
3458 of F. */
3459 oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3460 d->val);
3462 else
3464 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3465 output_mangled_typename (d->of, f);
3466 oprintf (d->of, " (%s%s);\n", cast, d->val);
3467 if (d->reorder_fn && wtd->reorder_note_routine)
3468 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
3469 wtd->reorder_note_routine, cast, d->val, cast, d->val,
3470 d->reorder_fn);
3472 break;
3474 case TYPE_SCALAR:
3475 break;
3477 case TYPE_ARRAY:
3478 gcc_unreachable ();
3482 /* Return an output file that is suitable for definitions which can
3483 reference struct S */
3485 static outf_p
3486 get_output_file_for_structure (const_type_p s, type_p *param)
3488 const input_file *fn;
3489 int i;
3491 gcc_assert (union_or_struct_p (s));
3492 fn = s->u.s.line.file;
3494 /* This is a hack, and not the good kind either. */
3495 for (i = NUM_PARAM - 1; i >= 0; i--)
3496 if (param && param[i] && param[i]->kind == TYPE_POINTER
3497 && union_or_struct_p (param[i]->u.p))
3498 fn = param[i]->u.p->u.s.line.file;
3500 /* The call to get_output_file_with_visibility may update fn by
3501 caching its result inside, so we need the CONST_CAST. */
3502 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3506 /* Returns the specifier keyword for a string or union type S, empty string
3507 otherwise. */
3509 static const char *
3510 get_type_specifier (const type_p s)
3512 if (s->kind == TYPE_STRUCT)
3513 return "struct ";
3514 else if (s->kind == TYPE_LANG_STRUCT)
3515 return get_type_specifier (s->u.s.lang_struct);
3516 else if (s->kind == TYPE_UNION)
3517 return "union ";
3518 return "";
3522 /* Emits a declaration for type TY (assumed to be a union or a
3523 structure) on stream OUT. */
3525 static void
3526 write_type_decl (outf_p out, type_p ty)
3528 if (union_or_struct_p (ty))
3529 oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3530 else if (ty->kind == TYPE_SCALAR)
3532 if (ty->u.scalar_is_char)
3533 oprintf (out, "const char");
3534 else
3535 oprintf (out, "void");
3537 else if (ty->kind == TYPE_POINTER)
3539 write_type_decl (out, ty->u.p);
3540 oprintf (out, " *");
3542 else if (ty->kind == TYPE_ARRAY)
3544 write_type_decl (out, ty->u.a.p);
3545 oprintf (out, " *");
3547 else if (ty->kind == TYPE_STRING)
3549 oprintf (out, "const char *");
3551 else
3552 gcc_unreachable ();
3556 /* Write on OF the name of the marker function for structure S. PREFIX
3557 is the prefix to use (to distinguish ggc from pch markers). */
3559 static void
3560 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3562 if (union_or_struct_p (s))
3564 const char *id_for_tag = filter_type_name (s->u.s.tag);
3565 oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3566 if (id_for_tag != s->u.s.tag)
3567 free (CONST_CAST (char *, id_for_tag));
3569 else if (s->kind == TYPE_PARAM_STRUCT)
3571 oprintf (of, "gt_%s_", prefix);
3572 output_mangled_typename (of, s);
3574 else
3575 gcc_unreachable ();
3578 /* Write on OF a user-callable routine to act as an entry point for
3579 the marking routine for S, generated by write_func_for_structure.
3580 WTD distinguishes between ggc and pch markers. */
3582 static void
3583 write_user_func_for_structure_ptr (outf_p of, type_p s, const write_types_data *wtd)
3585 /* Parameterized structures are not supported in user markers. There
3586 is no way for the marker function to know which specific type
3587 to use to generate the call to the void * entry point. For
3588 instance, a marker for struct htab may need to call different
3589 routines to mark the fields, depending on the paramN_is attributes.
3591 A user-defined marker that accepts 'struct htab' as its argument
3592 would not know which variant to call. Generating several entry
3593 points accepting 'struct htab' would cause multiply-defined
3594 errors during compilation. */
3595 gcc_assert (union_or_struct_p (s));
3597 type_p alias_of = NULL;
3598 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3599 if (strcmp (opt->name, "ptr_alias") == 0)
3601 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3602 we do not generate marking code for ORIG_S here. Instead, a
3603 forwarder #define in gtype-desc.h will cause every call to its
3604 marker to call the target of this alias.
3606 However, we still want to create a user entry code for the
3607 aliased type. So, if ALIAS_OF is set, we only generate the
3608 user-callable marker function. */
3609 alias_of = opt->info.type;
3610 break;
3613 DBGPRINTF ("write_user_func_for_structure_ptr: %s %s", s->u.s.tag,
3614 wtd->prefix);
3616 /* Only write the function once. */
3617 if (s->u.s.wrote_user_func_for_ptr[wtd->kind])
3618 return;
3619 s->u.s.wrote_user_func_for_ptr[wtd->kind] = true;
3621 oprintf (of, "\nvoid\n");
3622 oprintf (of, "gt_%sx (", wtd->prefix);
3623 write_type_decl (of, s);
3624 oprintf (of, " *& x)\n");
3625 oprintf (of, "{\n");
3626 oprintf (of, " if (x)\n ");
3627 write_marker_function_name (of,
3628 alias_of ? alias_of : get_ultimate_base_class (s),
3629 wtd->prefix);
3630 oprintf (of, " ((void *) x);\n");
3631 oprintf (of, "}\n");
3635 /* Write a function to mark all the fields of type S on OF. PREFIX
3636 and D are as in write_user_marking_functions. */
3638 static void
3639 write_user_func_for_structure_body (type_p s, const char *prefix,
3640 struct walk_type_data *d)
3642 oprintf (d->of, "\nvoid\n");
3643 oprintf (d->of, "gt_%sx (", prefix);
3644 write_type_decl (d->of, s);
3645 oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3646 oprintf (d->of, "{\n");
3647 oprintf (d->of, " ");
3648 write_type_decl (d->of, s);
3649 oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3650 d->val = "(*x)";
3651 d->indent = 2;
3652 walk_type (s, d);
3653 oprintf (d->of, "}\n");
3656 /* Emit the user-callable functions needed to mark all the types used
3657 by the user structure S. PREFIX is the prefix to use to
3658 distinguish ggc and pch markers. D contains data needed to pass to
3659 walk_type when traversing the fields of a type.
3661 For every type T referenced by S, two routines are generated: one
3662 that takes 'T *', marks the pointer and calls the second routine,
3663 which just marks the fields of T. */
3665 static void
3666 write_user_marking_functions (type_p s,
3667 const write_types_data *w,
3668 struct walk_type_data *d)
3670 gcc_assert (s->kind == TYPE_USER_STRUCT);
3672 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3674 type_p fld_type = fld->type;
3675 if (fld_type->kind == TYPE_POINTER)
3677 type_p pointed_to_type = fld_type->u.p;
3678 if (union_or_struct_p (pointed_to_type))
3679 write_user_func_for_structure_ptr (d->of, pointed_to_type, w);
3681 else if (union_or_struct_p (fld_type))
3682 write_user_func_for_structure_body (fld_type, w->prefix, d);
3687 /* For S, a structure that's part of ORIG_S, and using parameters
3688 PARAM, write out a routine that:
3689 - Takes a parameter, a void * but actually of type *S
3690 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3691 field of S or its substructures and (in some cases) things
3692 that are pointed to by S. */
3694 static void
3695 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
3696 const struct write_types_data *wtd)
3698 const char *chain_next = NULL;
3699 const char *chain_prev = NULL;
3700 const char *chain_circular = NULL;
3701 const char *mark_hook_name = NULL;
3702 options_p opt;
3703 struct walk_type_data d;
3705 if (s->u.s.base_class)
3707 /* Verify that the base class has a "desc", since otherwise
3708 the traversal hooks there won't attempt to visit fields of
3709 subclasses such as this one. */
3710 const_type_p ubc = get_ultimate_base_class (s);
3711 if ((!opts_have (ubc->u.s.opt, "user")
3712 && !opts_have (ubc->u.s.opt, "desc")))
3713 error_at_line (&s->u.s.line,
3714 ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
3715 ", but '%s' lacks a discriminator 'desc' option"),
3716 s->u.s.tag, ubc->u.s.tag, ubc->u.s.tag);
3718 /* Don't write fns for subclasses, only for the ultimate base class
3719 within an inheritance hierarchy. */
3720 return;
3723 memset (&d, 0, sizeof (d));
3724 d.of = get_output_file_for_structure (s, param);
3726 bool for_user = false;
3727 for (opt = s->u.s.opt; opt; opt = opt->next)
3728 if (strcmp (opt->name, "chain_next") == 0
3729 && opt->kind == OPTION_STRING)
3730 chain_next = opt->info.string;
3731 else if (strcmp (opt->name, "chain_prev") == 0
3732 && opt->kind == OPTION_STRING)
3733 chain_prev = opt->info.string;
3734 else if (strcmp (opt->name, "chain_circular") == 0
3735 && opt->kind == OPTION_STRING)
3736 chain_circular = opt->info.string;
3737 else if (strcmp (opt->name, "mark_hook") == 0
3738 && opt->kind == OPTION_STRING)
3739 mark_hook_name = opt->info.string;
3740 else if (strcmp (opt->name, "for_user") == 0)
3741 for_user = true;
3742 if (chain_prev != NULL && chain_next == NULL)
3743 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3744 if (chain_circular != NULL && chain_next != NULL)
3745 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3746 if (chain_circular != NULL)
3747 chain_next = chain_circular;
3749 d.process_field = write_types_process_field;
3750 d.cookie = wtd;
3751 d.orig_s = orig_s;
3752 d.opt = s->u.s.opt;
3753 d.line = &s->u.s.line;
3754 d.bitmap = s->u.s.bitmap;
3755 d.param = param;
3756 d.prev_val[0] = "*x";
3757 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3758 d.prev_val[3] = "x";
3759 d.val = "(*x)";
3760 d.have_this_obj = false;
3762 oprintf (d.of, "\n");
3763 oprintf (d.of, "void\n");
3764 write_marker_function_name (d.of, orig_s, wtd->prefix);
3765 oprintf (d.of, " (void *x_p)\n");
3766 oprintf (d.of, "{\n ");
3767 write_type_decl (d.of, s);
3768 oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3769 write_type_decl (d.of, s);
3770 oprintf (d.of, " *)x_p;\n");
3771 if (chain_next != NULL)
3773 /* TYPE_USER_STRUCTs should not occur here. These structures
3774 are completely handled by user code. */
3775 gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
3777 oprintf (d.of, " ");
3778 write_type_decl (d.of, s);
3779 oprintf (d.of, " * xlimit = x;\n");
3781 if (chain_next == NULL)
3783 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3784 if (wtd->param_prefix)
3786 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3787 output_mangled_typename (d.of, orig_s);
3789 oprintf (d.of, "))\n");
3791 else
3793 if (chain_circular != NULL)
3794 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3795 else
3796 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3797 if (wtd->param_prefix)
3799 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3800 output_mangled_typename (d.of, orig_s);
3802 oprintf (d.of, "))\n");
3803 if (chain_circular != NULL)
3804 oprintf (d.of, " return;\n do\n");
3805 if (mark_hook_name && !wtd->skip_hooks)
3807 oprintf (d.of, " {\n");
3808 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
3810 oprintf (d.of, " xlimit = (");
3811 d.prev_val[2] = "*xlimit";
3812 output_escaped_param (&d, chain_next, "chain_next");
3813 oprintf (d.of, ");\n");
3814 if (mark_hook_name && !wtd->skip_hooks)
3815 oprintf (d.of, " }\n");
3816 if (chain_prev != NULL)
3818 oprintf (d.of, " if (x != xlimit)\n");
3819 oprintf (d.of, " for (;;)\n");
3820 oprintf (d.of, " {\n");
3821 oprintf (d.of, " %s %s * const xprev = (",
3822 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3824 d.prev_val[2] = "*x";
3825 output_escaped_param (&d, chain_prev, "chain_prev");
3826 oprintf (d.of, ");\n");
3827 oprintf (d.of, " if (xprev == NULL) break;\n");
3828 oprintf (d.of, " x = xprev;\n");
3829 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3830 if (wtd->param_prefix)
3832 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3833 output_mangled_typename (d.of, orig_s);
3835 oprintf (d.of, ");\n");
3836 oprintf (d.of, " }\n");
3838 if (chain_circular != NULL)
3840 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3841 if (wtd->param_prefix)
3843 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3844 output_mangled_typename (d.of, orig_s);
3846 oprintf (d.of, "));\n");
3847 if (mark_hook_name && !wtd->skip_hooks)
3848 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
3849 oprintf (d.of, " do\n");
3851 else
3852 oprintf (d.of, " while (x != xlimit)\n");
3854 oprintf (d.of, " {\n");
3855 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3857 oprintf (d.of, " %s (x);\n", mark_hook_name);
3860 d.prev_val[2] = "*x";
3861 d.indent = 6;
3862 if (orig_s->kind != TYPE_USER_STRUCT)
3863 walk_type (s, &d);
3864 else
3866 /* User structures have no fields to walk. Simply generate a call
3867 to the user-provided structure marker. */
3868 oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3871 if (chain_next != NULL)
3873 oprintf (d.of, " x = (");
3874 output_escaped_param (&d, chain_next, "chain_next");
3875 oprintf (d.of, ");\n");
3878 oprintf (d.of, " }\n");
3879 if (chain_circular != NULL)
3880 oprintf (d.of, " while (x != xlimit);\n");
3881 oprintf (d.of, "}\n");
3883 if (orig_s->kind == TYPE_USER_STRUCT)
3884 write_user_marking_functions (orig_s, wtd, &d);
3886 if (for_user)
3888 write_user_func_for_structure_body (orig_s, wtd->prefix, &d);
3889 write_user_func_for_structure_ptr (d.of, orig_s, wtd);
3894 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3896 static void
3897 write_types (outf_p output_header, type_p structures, type_p param_structs,
3898 const struct write_types_data *wtd)
3900 int nbfun = 0; /* Count the emitted functions. */
3901 type_p s;
3903 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3905 /* We first emit the macros and the declarations. Functions' code is
3906 emitted afterwards. This is needed in plugin mode. */
3907 oprintf (output_header, "/* Macros and declarations. */\n");
3908 for (s = structures; s; s = s->next)
3909 /* Do not emit handlers for derived classes; we only ever deal with
3910 the ultimate base class within an inheritance hierarchy. */
3911 if ((s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3912 && !s->u.s.base_class)
3914 options_p opt;
3916 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3917 continue;
3919 const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3921 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3922 output_mangled_typename (output_header, s);
3923 oprintf (output_header, "(X) do { \\\n");
3924 oprintf (output_header,
3925 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3926 s_id_for_tag);
3927 oprintf (output_header, " } while (0)\n");
3929 for (opt = s->u.s.opt; opt; opt = opt->next)
3930 if (strcmp (opt->name, "ptr_alias") == 0
3931 && opt->kind == OPTION_TYPE)
3933 const_type_p const t = (const_type_p) opt->info.type;
3934 if (t->kind == TYPE_STRUCT
3935 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3937 const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3938 oprintf (output_header,
3939 "#define gt_%sx_%s gt_%sx_%s\n",
3940 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3941 if (t_id_for_tag != t->u.s.tag)
3942 free (CONST_CAST (char *, t_id_for_tag));
3944 else
3945 error_at_line (&s->u.s.line,
3946 "structure alias is not a structure");
3947 break;
3949 if (opt)
3950 continue;
3952 /* Declare the marker procedure only once. */
3953 oprintf (output_header,
3954 "extern void gt_%sx_%s (void *);\n",
3955 wtd->prefix, s_id_for_tag);
3957 if (s_id_for_tag != s->u.s.tag)
3958 free (CONST_CAST (char *, s_id_for_tag));
3960 if (s->u.s.line.file == NULL)
3962 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3963 s->u.s.tag);
3964 continue;
3968 for (s = param_structs; s; s = s->next)
3969 if (s->gc_used == GC_POINTED_TO)
3971 type_p stru = s->u.param_struct.stru;
3973 /* Declare the marker procedure. */
3974 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
3975 output_mangled_typename (output_header, s);
3976 oprintf (output_header, " (void *);\n");
3978 if (stru->u.s.line.file == NULL)
3980 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3981 stru->u.s.tag);
3982 continue;
3986 /* At last we emit the functions code. */
3987 oprintf (output_header, "\n/* functions code */\n");
3988 for (s = structures; s; s = s->next)
3989 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3991 options_p opt;
3993 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3994 continue;
3995 for (opt = s->u.s.opt; opt; opt = opt->next)
3996 if (strcmp (opt->name, "ptr_alias") == 0)
3997 break;
3998 if (opt)
3999 continue;
4001 if (s->kind == TYPE_LANG_STRUCT)
4003 type_p ss;
4004 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4006 nbfun++;
4007 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
4008 nbfun, (void*) ss, ss->u.s.tag);
4009 write_func_for_structure (s, ss, NULL, wtd);
4012 else
4014 nbfun++;
4015 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
4016 nbfun, (void*) s, s->u.s.tag);
4017 write_func_for_structure (s, s, NULL, wtd);
4020 else
4022 /* Structure s is not possibly pointed to, so can be ignored. */
4023 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
4024 (void*)s, s->u.s.tag,
4025 (int) s->gc_used);
4028 for (s = param_structs; s; s = s->next)
4029 if (s->gc_used == GC_POINTED_TO)
4031 type_p *param = s->u.param_struct.param;
4032 type_p stru = s->u.param_struct.stru;
4033 if (stru->u.s.line.file == NULL)
4034 continue;
4035 if (stru->kind == TYPE_LANG_STRUCT)
4037 type_p ss;
4038 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
4040 nbfun++;
4041 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
4042 nbfun, (void*) ss, ss->u.s.tag);
4043 write_func_for_structure (s, ss, param, wtd);
4046 else
4048 nbfun++;
4049 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
4050 nbfun, (void*) s,
4051 (void*) stru, stru->u.s.tag);
4052 write_func_for_structure (s, stru, param, wtd);
4055 else
4057 /* Param structure s is not pointed to, so should be ignored. */
4058 DBGPRINTF ("ignored s @ %p", (void*)s);
4060 if (verbosity_level >= 2)
4061 printf ("%s emitted %d routines for %s\n",
4062 progname, nbfun, wtd->comment);
4065 static const struct write_types_data ggc_wtd = {
4066 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
4067 "GC marker procedures. ",
4068 FALSE, WTK_GGC
4071 static const struct write_types_data pch_wtd = {
4072 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
4073 "gt_pch_note_reorder",
4074 "PCH type-walking procedures. ",
4075 TRUE, WTK_PCH
4078 /* Write out the local pointer-walking routines. */
4080 /* process_field routine for local pointer-walking for user-callable
4081 routines. The difference between this and
4082 write_types_local_process_field is that, in this case, we do not
4083 need to check whether the given pointer matches the address of the
4084 parent structure. This check was already generated by the call
4085 to gt_pch_nx in the main gt_pch_p_*() function that is calling
4086 this code. */
4088 static void
4089 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
4091 switch (f->kind)
4093 case TYPE_POINTER:
4094 case TYPE_STRUCT:
4095 case TYPE_UNION:
4096 case TYPE_LANG_STRUCT:
4097 case TYPE_PARAM_STRUCT:
4098 case TYPE_STRING:
4099 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4100 break;
4102 case TYPE_USER_STRUCT:
4103 if (d->in_ptr_field)
4104 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4105 else
4106 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4107 d->indent, "", d->val);
4108 break;
4110 case TYPE_SCALAR:
4111 break;
4113 case TYPE_ARRAY:
4114 case TYPE_NONE:
4115 case TYPE_UNDEFINED:
4116 gcc_unreachable ();
4121 /* Write a function to PCH walk all the fields of type S on OF.
4122 D contains data needed by walk_type to recurse into the fields of S. */
4124 static void
4125 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
4127 oprintf (d->of, "\nvoid\n");
4128 oprintf (d->of, "gt_pch_nx (");
4129 write_type_decl (d->of, s);
4130 oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
4131 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4132 "\tATTRIBUTE_UNUSED void *cookie)\n");
4133 oprintf (d->of, "{\n");
4134 d->val = "(*x)";
4135 d->indent = 2;
4136 d->process_field = write_types_local_user_process_field;
4137 walk_type (s, d);
4138 oprintf (d->of, "}\n");
4142 /* Emit the user-callable functions needed to mark all the types used
4143 by the user structure S. PREFIX is the prefix to use to
4144 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
4145 chain_next option defined. D contains data needed to pass to
4146 walk_type when traversing the fields of a type.
4148 For every type T referenced by S, two routines are generated: one
4149 that takes 'T *', marks the pointer and calls the second routine,
4150 which just marks the fields of T. */
4152 static void
4153 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
4155 gcc_assert (s->kind == TYPE_USER_STRUCT);
4157 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
4159 type_p fld_type = fld->type;
4160 if (union_or_struct_p (fld_type))
4161 write_pch_user_walking_for_structure_body (fld_type, d);
4166 /* process_field routine for local pointer-walking. */
4168 static void
4169 write_types_local_process_field (type_p f, const struct walk_type_data *d)
4171 gcc_assert (d->have_this_obj);
4172 switch (f->kind)
4174 case TYPE_POINTER:
4175 case TYPE_STRUCT:
4176 case TYPE_UNION:
4177 case TYPE_LANG_STRUCT:
4178 case TYPE_PARAM_STRUCT:
4179 case TYPE_STRING:
4180 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
4181 d->prev_val[3]);
4182 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4183 break;
4185 case TYPE_USER_STRUCT:
4186 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
4187 d->prev_val[3]);
4188 if (d->in_ptr_field)
4189 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4190 else
4191 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4192 d->indent, "", d->val);
4193 break;
4195 case TYPE_SCALAR:
4196 break;
4198 case TYPE_ARRAY:
4199 case TYPE_NONE:
4200 case TYPE_UNDEFINED:
4201 gcc_unreachable ();
4206 /* For S, a structure that's part of ORIG_S, and using parameters
4207 PARAM, write out a routine that:
4208 - Is of type gt_note_pointers
4209 - Calls PROCESS_FIELD on each field of S or its substructures.
4212 static void
4213 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
4215 struct walk_type_data d;
4217 /* Don't write fns for subclasses, only for the ultimate base class
4218 within an inheritance hierarchy. */
4219 if (s->u.s.base_class)
4220 return;
4222 memset (&d, 0, sizeof (d));
4223 d.of = get_output_file_for_structure (s, param);
4224 d.process_field = write_types_local_process_field;
4225 d.opt = s->u.s.opt;
4226 d.line = &s->u.s.line;
4227 d.bitmap = s->u.s.bitmap;
4228 d.param = param;
4229 d.prev_val[0] = d.prev_val[2] = "*x";
4230 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
4231 d.prev_val[3] = "x";
4232 d.val = "(*x)";
4233 d.fn_wants_lvalue = true;
4235 oprintf (d.of, "\n");
4236 oprintf (d.of, "void\n");
4237 oprintf (d.of, "gt_pch_p_");
4238 output_mangled_typename (d.of, orig_s);
4239 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
4240 "\tvoid *x_p,\n"
4241 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4242 "\tATTRIBUTE_UNUSED void *cookie)\n");
4243 oprintf (d.of, "{\n");
4244 oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
4245 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
4246 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
4247 d.indent = 2;
4248 d.have_this_obj = true;
4250 if (s->kind != TYPE_USER_STRUCT)
4251 walk_type (s, &d);
4252 else
4254 /* User structures have no fields to walk. Simply generate a
4255 call to the user-provided PCH walker. */
4256 oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
4257 d.prev_val[3]);
4258 oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4259 d.indent, "", d.val);
4262 oprintf (d.of, "}\n");
4264 /* Write user-callable entry points for the PCH walking routines. */
4265 if (orig_s->kind == TYPE_USER_STRUCT)
4266 write_pch_user_walking_functions (s, &d);
4268 for (options_p o = s->u.s.opt; o; o = o->next)
4269 if (strcmp (o->name, "for_user") == 0)
4271 write_pch_user_walking_for_structure_body (s, &d);
4272 break;
4276 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
4278 static void
4279 write_local (outf_p output_header, type_p structures, type_p param_structs)
4281 type_p s;
4283 if (!output_header)
4284 return;
4286 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
4287 for (s = structures; s; s = s->next)
4288 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
4290 options_p opt;
4292 if (s->u.s.line.file == NULL)
4293 continue;
4294 for (opt = s->u.s.opt; opt; opt = opt->next)
4295 if (strcmp (opt->name, "ptr_alias") == 0
4296 && opt->kind == OPTION_TYPE)
4298 const_type_p const t = (const_type_p) opt->info.type;
4299 if (t->kind == TYPE_STRUCT
4300 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
4302 oprintf (output_header, "#define gt_pch_p_");
4303 output_mangled_typename (output_header, s);
4304 oprintf (output_header, " gt_pch_p_");
4305 output_mangled_typename (output_header, t);
4306 oprintf (output_header, "\n");
4308 else
4309 error_at_line (&s->u.s.line,
4310 "structure alias is not a structure");
4311 break;
4313 if (opt)
4314 continue;
4316 /* Declare the marker procedure only once. */
4317 oprintf (output_header, "extern void gt_pch_p_");
4318 output_mangled_typename (output_header, s);
4319 oprintf (output_header,
4320 "\n (void *, void *, gt_pointer_operator, void *);\n");
4322 if (s->kind == TYPE_LANG_STRUCT)
4324 type_p ss;
4325 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4326 write_local_func_for_structure (s, ss, NULL);
4328 else
4329 write_local_func_for_structure (s, s, NULL);
4332 for (s = param_structs; s; s = s->next)
4333 if (s->gc_used == GC_POINTED_TO)
4335 type_p *param = s->u.param_struct.param;
4336 type_p stru = s->u.param_struct.stru;
4338 /* Declare the marker procedure. */
4339 oprintf (output_header, "extern void gt_pch_p_");
4340 output_mangled_typename (output_header, s);
4341 oprintf (output_header,
4342 "\n (void *, void *, gt_pointer_operator, void *);\n");
4344 if (stru->u.s.line.file == NULL)
4346 fprintf (stderr, "warning: structure `%s' used but not defined\n",
4347 stru->u.s.tag);
4348 continue;
4351 if (stru->kind == TYPE_LANG_STRUCT)
4353 type_p ss;
4354 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
4355 write_local_func_for_structure (s, ss, param);
4357 else
4358 write_local_func_for_structure (s, stru, param);
4362 /* Nonzero if S is a type for which typed GC allocators should be output. */
4364 #define USED_BY_TYPED_GC_P(s) \
4365 ((s->kind == TYPE_POINTER \
4366 && (s->u.p->gc_used == GC_POINTED_TO \
4367 || s->u.p->gc_used == GC_USED)) \
4368 || (union_or_struct_p (s) \
4369 && ((s)->gc_used == GC_POINTED_TO \
4370 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4371 && s->u.s.line.file != NULL) \
4372 || ((s)->gc_used == GC_USED \
4373 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))) \
4374 || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
4378 /* Might T contain any non-pointer elements? */
4380 static int
4381 contains_scalar_p (type_p t)
4383 switch (t->kind)
4385 case TYPE_STRING:
4386 case TYPE_POINTER:
4387 return 0;
4388 case TYPE_ARRAY:
4389 return contains_scalar_p (t->u.a.p);
4390 case TYPE_USER_STRUCT:
4391 /* User-marked structures will typically contain pointers. */
4392 return 0;
4393 default:
4394 /* Could also check for structures that have no non-pointer
4395 fields, but there aren't enough of those to worry about. */
4396 return 1;
4400 /* Mangle INPF and print it to F. */
4402 static void
4403 put_mangled_filename (outf_p f, const input_file *inpf)
4405 /* The call to get_output_file_name may indirectly update fn since
4406 get_output_file_with_visibility caches its result inside, so we
4407 need the CONST_CAST. */
4408 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4409 if (!f || !name)
4410 return;
4411 for (; *name != 0; name++)
4412 if (ISALNUM (*name))
4413 oprintf (f, "%c", *name);
4414 else
4415 oprintf (f, "%c", '_');
4418 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4419 LASTNAME, and NAME are all strings to insert in various places in
4420 the resulting code. */
4422 static void
4423 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4424 const char *tname, const char *name)
4426 struct flist *fli2;
4428 for (fli2 = flp; fli2; fli2 = fli2->next)
4429 if (fli2->started_p)
4431 oprintf (fli2->f, " %s\n", lastname);
4432 oprintf (fli2->f, "};\n\n");
4435 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4436 if (fli2->started_p)
4438 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4439 int fnum;
4441 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4442 if (bitmap & 1)
4444 oprintf (base_files[fnum],
4445 "extern const struct %s gt_%s_", tname, pfx);
4446 put_mangled_filename (base_files[fnum], fli2->file);
4447 oprintf (base_files[fnum], "[];\n");
4452 size_t fnum;
4453 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4454 oprintf (base_files[fnum],
4455 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4459 for (fli2 = flp; fli2; fli2 = fli2->next)
4460 if (fli2->started_p)
4462 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4463 int fnum;
4465 fli2->started_p = 0;
4467 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4468 if (bitmap & 1)
4470 oprintf (base_files[fnum], " gt_%s_", pfx);
4471 put_mangled_filename (base_files[fnum], fli2->file);
4472 oprintf (base_files[fnum], ",\n");
4477 size_t fnum;
4478 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4480 oprintf (base_files[fnum], " NULL\n");
4481 oprintf (base_files[fnum], "};\n");
4486 /* Finish off the created gt_clear_caches_file_c functions. */
4488 static void
4489 finish_cache_funcs (flist *flp)
4491 struct flist *fli2;
4493 for (fli2 = flp; fli2; fli2 = fli2->next)
4494 if (fli2->started_p)
4496 oprintf (fli2->f, "}\n\n");
4499 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4500 if (fli2->started_p)
4502 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4503 int fnum;
4505 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4506 if (bitmap & 1)
4508 oprintf (base_files[fnum], "extern void gt_clear_caches_");
4509 put_mangled_filename (base_files[fnum], fli2->file);
4510 oprintf (base_files[fnum], " ();\n");
4514 for (size_t fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4515 oprintf (base_files[fnum], "void\ngt_clear_caches ()\n{\n");
4517 for (fli2 = flp; fli2; fli2 = fli2->next)
4518 if (fli2->started_p)
4520 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4521 int fnum;
4523 fli2->started_p = 0;
4525 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4526 if (bitmap & 1)
4528 oprintf (base_files[fnum], " gt_clear_caches_");
4529 put_mangled_filename (base_files[fnum], fli2->file);
4530 oprintf (base_files[fnum], " ();\n");
4534 for (size_t fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4536 oprintf (base_files[fnum], "}\n");
4540 /* Write the first three fields (pointer, count and stride) for
4541 root NAME to F. V and LINE are as for write_root.
4543 Return true if the entry could be written; return false on error. */
4545 static bool
4546 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4548 type_p ap;
4550 if (!v)
4552 error_at_line (line, "`%s' is too complex to be a root", name);
4553 return false;
4556 oprintf (f, " {\n");
4557 oprintf (f, " &%s,\n", name);
4558 oprintf (f, " 1");
4560 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4561 if (ap->u.a.len[0])
4562 oprintf (f, " * (%s)", ap->u.a.len);
4563 else if (ap == v->type)
4564 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4565 oprintf (f, ",\n");
4566 oprintf (f, " sizeof (%s", v->name);
4567 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4568 oprintf (f, "[0]");
4569 oprintf (f, "),\n");
4570 return true;
4573 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4574 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4575 of the caller. */
4577 static void
4578 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4579 int has_length, struct fileloc *line, const char *if_marked,
4580 bool emit_pch, type_p field_type, const char *field_name)
4582 struct pair newv;
4583 /* If the field reference is relative to V, rather than to some
4584 subcomponent of V, we can mark any subarrays with a single stride.
4585 We're effectively treating the field as a global variable in its
4586 own right. */
4587 if (v && type == v->type)
4589 newv = *v;
4590 newv.type = field_type;
4591 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4592 v = &newv;
4594 /* Otherwise, any arrays nested in the structure are too complex to
4595 handle. */
4596 else if (field_type->kind == TYPE_ARRAY)
4597 v = NULL;
4598 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4599 has_length, line, if_marked, emit_pch);
4602 /* Write out to F the table entry and any marker routines needed to
4603 mark NAME as TYPE. V can be one of three values:
4605 - null, if NAME is too complex to represent using a single
4606 count and stride. In this case, it is an error for NAME to
4607 contain any gc-ed data.
4609 - the outermost array that contains NAME, if NAME is part of an array.
4611 - the C variable that contains NAME, if NAME is not part of an array.
4613 LINE is the line of the C source that declares the root variable.
4614 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
4615 is nonzero iff we are building the root table for hash table caches. */
4617 static void
4618 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4619 struct fileloc *line, const char *if_marked, bool emit_pch)
4621 switch (type->kind)
4623 case TYPE_STRUCT:
4625 pair_p fld;
4626 for (fld = type->u.s.fields; fld; fld = fld->next)
4628 int skip_p = 0;
4629 const char *desc = NULL;
4630 options_p o;
4632 for (o = fld->opt; o; o = o->next)
4633 if (strcmp (o->name, "skip") == 0)
4634 skip_p = 1;
4635 else if (strcmp (o->name, "desc") == 0
4636 && o->kind == OPTION_STRING)
4637 desc = o->info.string;
4638 else if (strcmp (o->name, "param_is") == 0)
4640 else
4641 error_at_line (line,
4642 "field `%s' of global `%s' has unknown option `%s'",
4643 fld->name, name, o->name);
4645 if (skip_p)
4646 continue;
4647 else if (desc && fld->type->kind == TYPE_UNION)
4649 pair_p validf = NULL;
4650 pair_p ufld;
4652 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4654 const char *tag = NULL;
4655 options_p oo;
4656 for (oo = ufld->opt; oo; oo = oo->next)
4657 if (strcmp (oo->name, "tag") == 0
4658 && oo->kind == OPTION_STRING)
4659 tag = oo->info.string;
4660 if (tag == NULL || strcmp (tag, desc) != 0)
4661 continue;
4662 if (validf != NULL)
4663 error_at_line (line,
4664 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4665 name, fld->name, validf->name,
4666 name, fld->name, ufld->name, tag);
4667 validf = ufld;
4669 if (validf != NULL)
4670 write_field_root (f, v, type, name, 0, line, if_marked,
4671 emit_pch, validf->type,
4672 ACONCAT ((fld->name, ".",
4673 validf->name, NULL)));
4675 else if (desc)
4676 error_at_line (line,
4677 "global `%s.%s' has `desc' option but is not union",
4678 name, fld->name);
4679 else
4680 write_field_root (f, v, type, name, 0, line, if_marked,
4681 emit_pch, fld->type, fld->name);
4684 break;
4686 case TYPE_ARRAY:
4688 char *newname;
4689 newname = xasprintf ("%s[0]", name);
4690 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
4691 emit_pch);
4692 free (newname);
4694 break;
4696 case TYPE_USER_STRUCT:
4697 error_at_line (line, "`%s' must be a pointer type, because it is "
4698 "a GC root and its type is marked with GTY((user))",
4699 v->name);
4700 break;
4702 case TYPE_POINTER:
4704 const_type_p tp;
4706 if (!start_root_entry (f, v, name, line))
4707 return;
4709 tp = type->u.p;
4711 if (!has_length && union_or_struct_p (tp))
4713 tp = get_ultimate_base_class (tp);
4714 const char *id_for_tag = filter_type_name (tp->u.s.tag);
4715 oprintf (f, " &gt_ggc_mx_%s,\n", id_for_tag);
4716 if (emit_pch)
4717 oprintf (f, " &gt_pch_nx_%s", id_for_tag);
4718 else
4719 oprintf (f, " NULL");
4720 if (id_for_tag != tp->u.s.tag)
4721 free (CONST_CAST (char *, id_for_tag));
4723 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
4725 oprintf (f, " &gt_ggc_m_");
4726 output_mangled_typename (f, tp);
4727 if (emit_pch)
4729 oprintf (f, ",\n &gt_pch_n_");
4730 output_mangled_typename (f, tp);
4732 else
4733 oprintf (f, ",\n NULL");
4735 else if (has_length
4736 && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
4738 oprintf (f, " &gt_ggc_ma_%s,\n", name);
4739 if (emit_pch)
4740 oprintf (f, " &gt_pch_na_%s", name);
4741 else
4742 oprintf (f, " NULL");
4744 else
4746 error_at_line (line,
4747 "global `%s' is pointer to unimplemented type",
4748 name);
4750 if (if_marked)
4751 oprintf (f, ",\n &%s", if_marked);
4752 oprintf (f, "\n },\n");
4754 break;
4756 case TYPE_STRING:
4758 if (!start_root_entry (f, v, name, line))
4759 return;
4761 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
4762 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
4763 oprintf (f, " },\n");
4765 break;
4767 case TYPE_SCALAR:
4768 break;
4770 case TYPE_NONE:
4771 case TYPE_UNDEFINED:
4772 case TYPE_UNION:
4773 case TYPE_LANG_STRUCT:
4774 case TYPE_PARAM_STRUCT:
4775 error_at_line (line, "global `%s' is unimplemented type", name);
4779 /* This generates a routine to walk an array. */
4781 static void
4782 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4784 struct walk_type_data d;
4785 char *prevval3;
4787 memset (&d, 0, sizeof (d));
4788 d.of = f;
4789 d.cookie = wtd;
4790 d.indent = 2;
4791 d.line = &v->line;
4792 d.opt = v->opt;
4793 d.bitmap = get_lang_bitmap (v->line.file);
4794 d.param = NULL;
4796 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4798 if (wtd->param_prefix)
4800 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4801 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
4802 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4803 wtd->param_prefix, v->name);
4804 oprintf (d.of,
4805 " ATTRIBUTE_UNUSED void *x_p,\n"
4806 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4807 " ATTRIBUTE_UNUSED void * cookie)\n");
4808 oprintf (d.of, "{\n");
4809 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4810 d.process_field = write_types_local_process_field;
4811 d.have_this_obj = true;
4812 walk_type (v->type, &d);
4813 oprintf (f, "}\n\n");
4816 d.opt = v->opt;
4817 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4818 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4819 wtd->prefix, v->name);
4820 oprintf (f, "{\n");
4821 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4822 d.process_field = write_types_process_field;
4823 d.have_this_obj = false;
4824 walk_type (v->type, &d);
4825 free (prevval3);
4826 oprintf (f, "}\n\n");
4829 /* Output a table describing the locations and types of VARIABLES. */
4831 static void
4832 write_roots (pair_p variables, bool emit_pch)
4834 pair_p v;
4835 struct flist *flp = NULL;
4837 for (v = variables; v; v = v->next)
4839 outf_p f =
4840 get_output_file_with_visibility (CONST_CAST (input_file*,
4841 v->line.file));
4842 struct flist *fli;
4843 const char *length = NULL;
4844 int deletable_p = 0;
4845 options_p o;
4846 for (o = v->opt; o; o = o->next)
4847 if (strcmp (o->name, "length") == 0
4848 && o->kind == OPTION_STRING)
4849 length = o->info.string;
4850 else if (strcmp (o->name, "deletable") == 0)
4851 deletable_p = 1;
4852 else if (strcmp (o->name, "param_is") == 0)
4854 else if (strncmp (o->name, "param", 5) == 0
4855 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
4857 else if (strcmp (o->name, "if_marked") == 0)
4859 else if (strcmp (o->name, "cache") == 0)
4861 else
4862 error_at_line (&v->line,
4863 "global `%s' has unknown option `%s'",
4864 v->name, o->name);
4866 for (fli = flp; fli; fli = fli->next)
4867 if (fli->f == f && f)
4868 break;
4869 if (fli == NULL)
4871 fli = XNEW (struct flist);
4872 fli->f = f;
4873 fli->next = flp;
4874 fli->started_p = 0;
4875 fli->file = v->line.file;
4876 gcc_assert (fli->file);
4877 flp = fli;
4879 oprintf (f, "\n/* GC roots. */\n\n");
4882 if (!deletable_p
4883 && length
4884 && v->type->kind == TYPE_POINTER
4885 && (v->type->u.p->kind == TYPE_POINTER
4886 || v->type->u.p->kind == TYPE_STRUCT))
4888 write_array (f, v, &ggc_wtd);
4889 write_array (f, v, &pch_wtd);
4893 for (v = variables; v; v = v->next)
4895 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4896 v->line.file));
4897 struct flist *fli;
4898 int skip_p = 0;
4899 int length_p = 0;
4900 options_p o;
4902 for (o = v->opt; o; o = o->next)
4903 if (strcmp (o->name, "length") == 0)
4904 length_p = 1;
4905 else if (strcmp (o->name, "deletable") == 0
4906 || strcmp (o->name, "if_marked") == 0)
4907 skip_p = 1;
4909 if (skip_p)
4910 continue;
4912 for (fli = flp; fli; fli = fli->next)
4913 if (fli->f == f)
4914 break;
4915 if (!fli->started_p)
4917 fli->started_p = 1;
4919 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4920 put_mangled_filename (f, v->line.file);
4921 oprintf (f, "[] = {\n");
4924 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4927 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4928 "gt_ggc_rtab");
4930 for (v = variables; v; v = v->next)
4932 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4933 v->line.file));
4934 struct flist *fli;
4935 int skip_p = 1;
4936 options_p o;
4938 for (o = v->opt; o; o = o->next)
4939 if (strcmp (o->name, "deletable") == 0)
4940 skip_p = 0;
4941 else if (strcmp (o->name, "if_marked") == 0)
4942 skip_p = 1;
4944 if (skip_p)
4945 continue;
4947 for (fli = flp; fli; fli = fli->next)
4948 if (fli->f == f)
4949 break;
4950 if (!fli->started_p)
4952 fli->started_p = 1;
4954 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4955 put_mangled_filename (f, v->line.file);
4956 oprintf (f, "[] = {\n");
4959 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4960 v->name, v->name);
4963 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4964 "gt_ggc_deletable_rtab");
4966 for (v = variables; v; v = v->next)
4968 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4969 v->line.file));
4970 struct flist *fli;
4971 const char *if_marked = NULL;
4972 int length_p = 0;
4973 options_p o;
4975 for (o = v->opt; o; o = o->next)
4976 if (strcmp (o->name, "length") == 0)
4977 length_p = 1;
4978 else if (strcmp (o->name, "if_marked") == 0
4979 && o->kind == OPTION_STRING)
4980 if_marked = o->info.string;
4981 if (if_marked == NULL)
4982 continue;
4983 if (v->type->kind != TYPE_POINTER
4984 || v->type->u.p->kind != TYPE_PARAM_STRUCT
4985 || v->type->u.p->u.param_struct.stru != find_structure ("htab",
4986 TYPE_STRUCT))
4988 error_at_line (&v->line,
4989 "if_marked option used but not hash table");
4990 continue;
4993 for (fli = flp; fli; fli = fli->next)
4994 if (fli->f == f)
4995 break;
4996 if (!fli->started_p)
4998 fli->started_p = 1;
5000 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
5001 put_mangled_filename (f, v->line.file);
5002 oprintf (f, "[] = {\n");
5005 write_root (f, v, v->type->u.p->u.param_struct.param[0],
5006 v->name, length_p, &v->line, if_marked, emit_pch);
5009 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
5010 "gt_ggc_cache_rtab");
5012 for (v = variables; v; v = v->next)
5014 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
5015 v->line.file));
5016 struct flist *fli;
5017 bool cache = false;
5018 options_p o;
5020 for (o = v->opt; o; o = o->next)
5021 if (strcmp (o->name, "cache") == 0)
5022 cache = true;
5023 if (!cache)
5024 continue;
5026 for (fli = flp; fli; fli = fli->next)
5027 if (fli->f == f)
5028 break;
5029 if (!fli->started_p)
5031 fli->started_p = 1;
5033 oprintf (f, "void\ngt_clear_caches_");
5034 put_mangled_filename (f, v->line.file);
5035 oprintf (f, " ()\n{\n");
5038 oprintf (f, " gt_cleare_cache (%s);\n", v->name);
5041 finish_cache_funcs (flp);
5043 if (!emit_pch)
5044 return;
5046 for (v = variables; v; v = v->next)
5048 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
5049 v->line.file));
5050 struct flist *fli;
5051 int length_p = 0;
5052 int if_marked_p = 0;
5053 options_p o;
5055 for (o = v->opt; o; o = o->next)
5056 if (strcmp (o->name, "length") == 0)
5057 length_p = 1;
5058 else if (strcmp (o->name, "if_marked") == 0)
5059 if_marked_p = 1;
5061 if (!if_marked_p)
5062 continue;
5064 for (fli = flp; fli; fli = fli->next)
5065 if (fli->f == f)
5066 break;
5067 if (!fli->started_p)
5069 fli->started_p = 1;
5071 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
5072 put_mangled_filename (f, v->line.file);
5073 oprintf (f, "[] = {\n");
5076 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
5079 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
5080 "gt_pch_cache_rtab");
5082 for (v = variables; v; v = v->next)
5084 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
5085 v->line.file));
5086 struct flist *fli;
5087 int skip_p = 0;
5088 options_p o;
5090 for (o = v->opt; o; o = o->next)
5091 if (strcmp (o->name, "deletable") == 0
5092 || strcmp (o->name, "if_marked") == 0)
5094 skip_p = 1;
5095 break;
5098 if (skip_p)
5099 continue;
5101 if (!contains_scalar_p (v->type))
5102 continue;
5104 for (fli = flp; fli; fli = fli->next)
5105 if (fli->f == f)
5106 break;
5107 if (!fli->started_p)
5109 fli->started_p = 1;
5111 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
5112 put_mangled_filename (f, v->line.file);
5113 oprintf (f, "[] = {\n");
5116 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
5117 v->name, v->name);
5120 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
5121 "gt_pch_scalar_rtab");
5124 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
5125 guaranteee for somewhat increased readability. If name conflicts do happen,
5126 this funcion will have to be adjusted to be more like
5127 output_mangled_typename. */
5129 static void
5130 output_typename (outf_p of, const_type_p t)
5132 switch (t->kind)
5134 case TYPE_STRING:
5135 oprintf (of, "str");
5136 break;
5137 case TYPE_SCALAR:
5138 oprintf (of, "scalar");
5139 break;
5140 case TYPE_POINTER:
5141 output_typename (of, t->u.p);
5142 break;
5143 case TYPE_STRUCT:
5144 case TYPE_USER_STRUCT:
5145 case TYPE_UNION:
5146 case TYPE_LANG_STRUCT:
5147 oprintf (of, "%s", t->u.s.tag);
5148 break;
5149 case TYPE_PARAM_STRUCT:
5151 int i;
5152 for (i = 0; i < NUM_PARAM; i++)
5153 if (t->u.param_struct.param[i] != NULL)
5155 output_typename (of, t->u.param_struct.param[i]);
5156 oprintf (of, "_");
5158 output_typename (of, t->u.param_struct.stru);
5159 break;
5161 case TYPE_NONE:
5162 case TYPE_UNDEFINED:
5163 case TYPE_ARRAY:
5164 gcc_unreachable ();
5168 /* Writes a typed GC allocator for type S that is suitable as a callback for
5169 the splay tree implementation in libiberty. */
5171 static void
5172 write_splay_tree_allocator_def (const_type_p s)
5174 outf_p of = get_output_file_with_visibility (NULL);
5175 oprintf (of, "void * ggc_alloc_splay_tree_");
5176 output_typename (of, s);
5177 oprintf (of, " (int sz, void * nl)\n");
5178 oprintf (of, "{\n");
5179 oprintf (of, " return ggc_splay_alloc (sz, nl);\n");
5180 oprintf (of, "}\n\n");
5183 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
5184 for the splay tree implementation in libiberty. */
5186 static void
5187 write_splay_tree_allocators (const_type_p param_structs)
5189 const_type_p s;
5191 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
5192 for (s = param_structs; s; s = s->next)
5193 if (s->gc_used == GC_POINTED_TO)
5195 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
5196 output_typename (header_file, s);
5197 oprintf (header_file, " (int, void *);\n");
5198 write_splay_tree_allocator_def (s);
5202 #define INDENT 2
5204 /* Dumps the value of typekind KIND. */
5206 static void
5207 dump_typekind (int indent, enum typekind kind)
5209 printf ("%*ckind = ", indent, ' ');
5210 switch (kind)
5212 case TYPE_SCALAR:
5213 printf ("TYPE_SCALAR");
5214 break;
5215 case TYPE_STRING:
5216 printf ("TYPE_STRING");
5217 break;
5218 case TYPE_STRUCT:
5219 printf ("TYPE_STRUCT");
5220 break;
5221 case TYPE_UNDEFINED:
5222 printf ("TYPE_UNDEFINED");
5223 break;
5224 case TYPE_USER_STRUCT:
5225 printf ("TYPE_USER_STRUCT");
5226 break;
5227 case TYPE_UNION:
5228 printf ("TYPE_UNION");
5229 break;
5230 case TYPE_POINTER:
5231 printf ("TYPE_POINTER");
5232 break;
5233 case TYPE_ARRAY:
5234 printf ("TYPE_ARRAY");
5235 break;
5236 case TYPE_LANG_STRUCT:
5237 printf ("TYPE_LANG_STRUCT");
5238 break;
5239 case TYPE_PARAM_STRUCT:
5240 printf ("TYPE_PARAM_STRUCT");
5241 break;
5242 default:
5243 gcc_unreachable ();
5245 printf ("\n");
5248 /* Dumps the value of GC_USED flag. */
5250 static void
5251 dump_gc_used (int indent, enum gc_used_enum gc_used)
5253 printf ("%*cgc_used = ", indent, ' ');
5254 switch (gc_used)
5256 case GC_UNUSED:
5257 printf ("GC_UNUSED");
5258 break;
5259 case GC_USED:
5260 printf ("GC_USED");
5261 break;
5262 case GC_MAYBE_POINTED_TO:
5263 printf ("GC_MAYBE_POINTED_TO");
5264 break;
5265 case GC_POINTED_TO:
5266 printf ("GC_POINTED_TO");
5267 break;
5268 default:
5269 gcc_unreachable ();
5271 printf ("\n");
5274 /* Dumps the type options OPT. */
5276 static void
5277 dump_options (int indent, options_p opt)
5279 options_p o;
5280 printf ("%*coptions = ", indent, ' ');
5281 o = opt;
5282 while (o)
5284 switch (o->kind)
5286 case OPTION_STRING:
5287 printf ("%s:string %s ", o->name, o->info.string);
5288 break;
5289 case OPTION_TYPE:
5290 printf ("%s:type ", o->name);
5291 dump_type (indent+1, o->info.type);
5292 break;
5293 case OPTION_NESTED:
5294 printf ("%s:nested ", o->name);
5295 break;
5296 case OPTION_NONE:
5297 gcc_unreachable ();
5299 o = o->next;
5301 printf ("\n");
5304 /* Dumps the source file location in LINE. */
5306 static void
5307 dump_fileloc (int indent, struct fileloc line)
5309 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
5310 get_input_file_name (line.file),
5311 line.line);
5314 /* Recursively dumps the struct, union, or a language-specific
5315 struct T. */
5317 static void
5318 dump_type_u_s (int indent, type_p t)
5320 pair_p fields;
5322 gcc_assert (union_or_struct_p (t));
5323 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
5324 dump_fileloc (indent, t->u.s.line);
5325 printf ("%*cu.s.fields =\n", indent, ' ');
5326 fields = t->u.s.fields;
5327 while (fields)
5329 dump_pair (indent + INDENT, fields);
5330 fields = fields->next;
5332 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
5333 dump_options (indent, t->u.s.opt);
5334 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
5335 if (t->kind == TYPE_LANG_STRUCT)
5337 printf ("%*cu.s.lang_struct:\n", indent, ' ');
5338 dump_type_list (indent + INDENT, t->u.s.lang_struct);
5342 /* Recursively dumps the array T. */
5344 static void
5345 dump_type_u_a (int indent, type_p t)
5347 gcc_assert (t->kind == TYPE_ARRAY);
5348 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
5349 dump_type_list (indent + INDENT, t->u.a.p);
5352 /* Recursively dumps the parameterized struct T. */
5354 static void
5355 dump_type_u_param_struct (int indent, type_p t)
5357 int i;
5358 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
5359 printf ("%*cu.param_struct.stru:\n", indent, ' ');
5360 dump_type_list (indent, t->u.param_struct.stru);
5361 dump_fileloc (indent, t->u.param_struct.line);
5362 for (i = 0; i < NUM_PARAM; i++)
5364 if (t->u.param_struct.param[i] == NULL)
5365 continue;
5366 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
5367 dump_type (indent + INDENT, t->u.param_struct.param[i]);
5371 /* Recursively dumps the type list T. */
5373 static void
5374 dump_type_list (int indent, type_p t)
5376 type_p p = t;
5377 while (p)
5379 dump_type (indent, p);
5380 p = p->next;
5384 static htab_t seen_types;
5386 /* Recursively dumps the type T if it was not dumped previously. */
5388 static void
5389 dump_type (int indent, type_p t)
5391 PTR *slot;
5393 if (seen_types == NULL)
5394 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
5396 printf ("%*cType at %p: ", indent, ' ', (void *) t);
5397 slot = htab_find_slot (seen_types, t, INSERT);
5398 if (*slot != NULL)
5400 printf ("already seen.\n");
5401 return;
5403 *slot = t;
5404 printf ("\n");
5406 dump_typekind (indent, t->kind);
5407 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
5408 (void *) t->pointer_to);
5409 dump_gc_used (indent + INDENT, t->gc_used);
5410 switch (t->kind)
5412 case TYPE_SCALAR:
5413 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
5414 t->u.scalar_is_char ? "true" : "false");
5415 break;
5416 case TYPE_STRING:
5417 break;
5418 case TYPE_STRUCT:
5419 case TYPE_UNION:
5420 case TYPE_LANG_STRUCT:
5421 case TYPE_USER_STRUCT:
5422 dump_type_u_s (indent + INDENT, t);
5423 break;
5424 case TYPE_POINTER:
5425 printf ("%*cp:\n", indent + INDENT, ' ');
5426 dump_type (indent + INDENT, t->u.p);
5427 break;
5428 case TYPE_ARRAY:
5429 dump_type_u_a (indent + INDENT, t);
5430 break;
5431 case TYPE_PARAM_STRUCT:
5432 dump_type_u_param_struct (indent + INDENT, t);
5433 break;
5434 default:
5435 gcc_unreachable ();
5437 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
5440 /* Dumps the pair P. */
5442 static void
5443 dump_pair (int indent, pair_p p)
5445 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
5446 dump_type (indent, p->type);
5447 dump_fileloc (indent, p->line);
5448 dump_options (indent, p->opt);
5449 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
5452 /* Dumps the list of pairs PP. */
5454 static void
5455 dump_pair_list (const char *name, pair_p pp)
5457 pair_p p;
5458 printf ("%s:\n", name);
5459 for (p = pp; p != NULL; p = p->next)
5460 dump_pair (0, p);
5461 printf ("End of %s\n\n", name);
5464 /* Dumps the STRUCTURES. */
5466 static void
5467 dump_structures (const char *name, type_p structures)
5469 printf ("%s:\n", name);
5470 dump_type_list (0, structures);
5471 printf ("End of %s\n\n", name);
5474 /* Dumps the internal structures of gengtype. This is useful to debug
5475 gengtype itself, or to understand what it does, e.g. for plugin
5476 developers. */
5478 static void
5479 dump_everything (void)
5481 dump_pair_list ("typedefs", typedefs);
5482 dump_structures ("structures", structures);
5483 dump_structures ("param_structs", param_structs);
5484 dump_pair_list ("variables", variables);
5486 /* Allocated with the first call to dump_type. */
5487 htab_delete (seen_types);
5492 /* Option specification for getopt_long. */
5493 static const struct option gengtype_long_options[] = {
5494 {"help", no_argument, NULL, 'h'},
5495 {"version", no_argument, NULL, 'V'},
5496 {"verbose", no_argument, NULL, 'v'},
5497 {"dump", no_argument, NULL, 'd'},
5498 {"debug", no_argument, NULL, 'D'},
5499 {"plugin", required_argument, NULL, 'P'},
5500 {"srcdir", required_argument, NULL, 'S'},
5501 {"backupdir", required_argument, NULL, 'B'},
5502 {"inputs", required_argument, NULL, 'I'},
5503 {"read-state", required_argument, NULL, 'r'},
5504 {"write-state", required_argument, NULL, 'w'},
5505 /* Terminating NULL placeholder. */
5506 {NULL, no_argument, NULL, 0},
5510 static void
5511 print_usage (void)
5513 printf ("Usage: %s\n", progname);
5514 printf ("\t -h | --help " " \t# Give this help.\n");
5515 printf ("\t -D | --debug "
5516 " \t# Give debug output to debug %s itself.\n", progname);
5517 printf ("\t -V | --version " " \t# Give version information.\n");
5518 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5519 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5520 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5521 " \t# Generate for plugin.\n");
5522 printf ("\t -S | --srcdir <GCC-directory> "
5523 " \t# Specify the GCC source directory.\n");
5524 printf ("\t -B | --backupdir <directory> "
5525 " \t# Specify the backup directory for updated files.\n");
5526 printf ("\t -I | --inputs <input-list> "
5527 " \t# Specify the file with source files list.\n");
5528 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5529 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5532 static void
5533 print_version (void)
5535 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5536 printf ("Report bugs: %s\n", bug_report_url);
5539 /* Parse the program options using getopt_long... */
5540 static void
5541 parse_program_options (int argc, char **argv)
5543 int opt = -1;
5544 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5545 gengtype_long_options, NULL)) >= 0)
5547 switch (opt)
5549 case 'h': /* --help */
5550 print_usage ();
5551 break;
5552 case 'V': /* --version */
5553 print_version ();
5554 break;
5555 case 'd': /* --dump */
5556 do_dump = 1;
5557 break;
5558 case 'D': /* --debug */
5559 do_debug = 1;
5560 break;
5561 case 'v': /* --verbose */
5562 verbosity_level++;
5563 break;
5564 case 'P': /* --plugin */
5565 if (optarg)
5566 plugin_output_filename = optarg;
5567 else
5568 fatal ("missing plugin output file name");
5569 break;
5570 case 'S': /* --srcdir */
5571 if (optarg)
5572 srcdir = optarg;
5573 else
5574 fatal ("missing source directory");
5575 srcdir_len = strlen (srcdir);
5576 break;
5577 case 'B': /* --backupdir */
5578 if (optarg)
5579 backup_dir = optarg;
5580 else
5581 fatal ("missing backup directory");
5582 break;
5583 case 'I': /* --inputs */
5584 if (optarg)
5585 inputlist = optarg;
5586 else
5587 fatal ("missing input list");
5588 break;
5589 case 'r': /* --read-state */
5590 if (optarg)
5591 read_state_filename = optarg;
5592 else
5593 fatal ("missing read state file");
5594 DBGPRINTF ("read state %s\n", optarg);
5595 break;
5596 case 'w': /* --write-state */
5597 DBGPRINTF ("write state %s\n", optarg);
5598 if (optarg)
5599 write_state_filename = optarg;
5600 else
5601 fatal ("missing write state file");
5602 break;
5603 default:
5604 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5605 print_usage ();
5606 fatal ("unexpected flag");
5609 if (plugin_output_filename)
5611 /* In plugin mode we require some input files. */
5612 int i = 0;
5613 if (optind >= argc)
5614 fatal ("no source files given in plugin mode");
5615 nb_plugin_files = argc - optind;
5616 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5617 for (i = 0; i < (int) nb_plugin_files; i++)
5619 char *name = argv[i + optind];
5620 plugin_files[i] = input_file_by_name (name);
5627 /******* Manage input files. ******/
5629 /* Hash table of unique input file names. */
5630 static htab_t input_file_htab;
5632 /* Find or allocate a new input_file by hash-consing it. */
5633 input_file*
5634 input_file_by_name (const char* name)
5636 PTR* slot;
5637 input_file* f = NULL;
5638 int namlen = 0;
5639 if (!name)
5640 return NULL;
5641 namlen = strlen (name);
5642 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5643 f->inpbitmap = 0;
5644 f->inpoutf = NULL;
5645 f->inpisplugin = false;
5646 strcpy (f->inpname, name);
5647 slot = htab_find_slot (input_file_htab, f, INSERT);
5648 gcc_assert (slot != NULL);
5649 if (*slot)
5651 /* Already known input file. */
5652 free (f);
5653 return (input_file*)(*slot);
5655 /* New input file. */
5656 *slot = f;
5657 return f;
5660 /* Hash table support routines for input_file-s. */
5661 static hashval_t
5662 htab_hash_inputfile (const void *p)
5664 const input_file *inpf = (const input_file *) p;
5665 gcc_assert (inpf);
5666 return htab_hash_string (get_input_file_name (inpf));
5669 static int
5670 htab_eq_inputfile (const void *x, const void *y)
5672 const input_file *inpfx = (const input_file *) x;
5673 const input_file *inpfy = (const input_file *) y;
5674 gcc_assert (inpfx != NULL && inpfy != NULL);
5675 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5680 main (int argc, char **argv)
5682 size_t i;
5683 static struct fileloc pos = { NULL, 0 };
5684 outf_p output_header;
5686 /* Mandatory common initializations. */
5687 progname = "gengtype"; /* For fatal and messages. */
5688 /* Create the hash-table used to hash-cons input files. */
5689 input_file_htab =
5690 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5691 /* Initialize our special input files. */
5692 this_file = input_file_by_name (__FILE__);
5693 system_h_file = input_file_by_name ("system.h");
5694 /* Set the scalar_is_char union number for predefined scalar types. */
5695 scalar_nonchar.u.scalar_is_char = FALSE;
5696 scalar_char.u.scalar_is_char = TRUE;
5698 parse_program_options (argc, argv);
5700 #if ENABLE_CHECKING
5701 if (do_debug)
5703 time_t now = (time_t) 0;
5704 time (&now);
5705 DBGPRINTF ("gengtype started pid %d at %s",
5706 (int) getpid (), ctime (&now));
5708 #endif /* ENABLE_CHECKING */
5710 /* Parse the input list and the input files. */
5711 DBGPRINTF ("inputlist %s", inputlist);
5712 if (read_state_filename)
5714 if (inputlist)
5715 fatal ("input list %s cannot be given with a read state file %s",
5716 inputlist, read_state_filename);
5717 read_state (read_state_filename);
5718 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5719 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs);
5721 else if (inputlist)
5723 /* These types are set up with #define or else outside of where
5724 we can see them. We should initialize them before calling
5725 read_input_list. */
5726 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5727 Call;} while (0)
5728 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5729 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5730 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5731 POS_HERE (do_scalar_typedef ("double_int", &pos));
5732 POS_HERE (do_scalar_typedef ("offset_int", &pos));
5733 POS_HERE (do_scalar_typedef ("widest_int", &pos));
5734 POS_HERE (do_scalar_typedef ("int64_t", &pos));
5735 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5736 POS_HERE (do_scalar_typedef ("uint8", &pos));
5737 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5738 POS_HERE (do_scalar_typedef ("jword", &pos));
5739 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5740 POS_HERE (do_scalar_typedef ("void", &pos));
5741 POS_HERE (do_scalar_typedef ("machine_mode", &pos));
5742 POS_HERE (do_typedef ("PTR",
5743 create_pointer (resolve_typedef ("void", &pos)),
5744 &pos));
5745 #undef POS_HERE
5746 read_input_list (inputlist);
5747 for (i = 0; i < num_gt_files; i++)
5749 parse_file (get_input_file_name (gt_files[i]));
5750 DBGPRINTF ("parsed file #%d %s",
5751 (int) i, get_input_file_name (gt_files[i]));
5753 if (verbosity_level >= 1)
5754 printf ("%s parsed %d files with %d GTY types\n",
5755 progname, (int) num_gt_files, type_count);
5757 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5758 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
5761 else
5762 fatal ("either an input list or a read state file should be given");
5763 if (hit_error)
5764 return 1;
5767 if (plugin_output_filename)
5769 size_t ix = 0;
5770 /* In plugin mode, we should have read a state file, and have
5771 given at least one plugin file. */
5772 if (!read_state_filename)
5773 fatal ("No read state given in plugin mode for %s",
5774 plugin_output_filename);
5776 if (nb_plugin_files == 0 || !plugin_files)
5777 fatal ("No plugin files given in plugin mode for %s",
5778 plugin_output_filename);
5780 /* Parse our plugin files and augment the state. */
5781 for (ix = 0; ix < nb_plugin_files; ix++)
5783 input_file* pluginput = plugin_files [ix];
5784 pluginput->inpisplugin = true;
5785 parse_file (get_input_file_name (pluginput));
5787 if (hit_error)
5788 return 1;
5790 plugin_output = create_file ("GCC", plugin_output_filename);
5791 DBGPRINTF ("created plugin_output %p named %s",
5792 (void *) plugin_output, plugin_output->name);
5794 else
5795 { /* No plugin files, we are in normal mode. */
5796 if (!srcdir)
5797 fatal ("gengtype needs a source directory in normal mode");
5799 if (hit_error)
5800 return 1;
5802 gen_rtx_next ();
5804 /* The call to set_gc_used may indirectly call find_param_structure
5805 hence enlarge the param_structs list of types. */
5806 set_gc_used (variables);
5808 for (type_p t = structures; t; t = t->next)
5810 bool for_user = false;
5811 for (options_p o = t->u.s.opt; o; o = o->next)
5812 if (strcmp (o->name, "for_user") == 0)
5814 for_user = true;
5815 break;
5818 if (for_user)
5819 set_gc_used_type (t, GC_POINTED_TO, NULL);
5821 /* The state at this point is read from the state input file or by
5822 parsing source files and optionally augmented by parsing plugin
5823 source files. Write it now. */
5824 if (write_state_filename)
5826 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5827 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs);
5829 if (hit_error)
5830 fatal ("didn't write state file %s after errors",
5831 write_state_filename);
5833 DBGPRINTF ("before write_state %s", write_state_filename);
5834 write_state (write_state_filename);
5836 if (do_dump)
5837 dump_everything ();
5839 /* After having written the state file we return immediately to
5840 avoid generating any output file. */
5841 if (hit_error)
5842 return 1;
5843 else
5844 return 0;
5848 open_base_files ();
5850 output_header = plugin_output ? plugin_output : header_file;
5851 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5852 structures);
5853 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5854 param_structs);
5856 write_types (output_header, structures, param_structs, &ggc_wtd);
5857 if (plugin_files == NULL)
5859 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5860 structures);
5861 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5862 param_structs);
5863 write_types (header_file, structures, param_structs, &pch_wtd);
5864 write_local (header_file, structures, param_structs);
5866 write_splay_tree_allocators (param_structs);
5867 write_roots (variables, plugin_files == NULL);
5868 write_rtx_next ();
5869 close_output_files ();
5871 if (do_dump)
5872 dump_everything ();
5874 /* Don't bother about free-ing any input or plugin file, etc. */
5876 if (hit_error)
5877 return 1;
5878 return 0;