Simplify convert_modes, ignoring invalid old modes for CONST_INTs.
[official-gcc.git] / gcc / gengtype.c
blobe2d9dd4f9476fc214bf07dd3b3defafe22e15fa9
1 /* Process source files and output type information.
2 Copyright (C) 2002-2013 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 GENERATOR_FILE
21 #include "bconfig.h"
22 #else
23 #include "config.h"
24 #endif
25 #include "system.h"
26 #include "errors.h" /* for fatal */
27 #include "getopt.h"
28 #include "version.h" /* for version_string & pkgversion_string. */
29 #include "hashtab.h"
30 #include "xregex.h"
31 #include "obstack.h"
32 #include "gengtype.h"
33 #include "filenames.h"
35 /* Data types, macros, etc. used only in this file. */
38 /* The list of output files. */
39 outf_p output_files;
41 /* The output header file that is included into pretty much every
42 source file. */
43 outf_p header_file;
46 /* The name of the file containing the list of input files. */
47 static char *inputlist;
49 /* The plugin input files and their number; in that case only
50 a single file is produced. */
51 static input_file **plugin_files;
52 static size_t nb_plugin_files;
54 /* The generated plugin output file and name. */
55 static outf_p plugin_output;
56 static char *plugin_output_filename;
58 /* Our source directory and its length. */
59 const char *srcdir;
60 size_t srcdir_len;
62 /* Variables used for reading and writing the state. */
63 const char *read_state_filename;
64 const char *write_state_filename;
66 /* Variables to help debugging. */
67 int do_dump;
68 int do_debug;
70 /* Level for verbose messages. */
71 int verbosity_level;
73 /* We have a type count and use it to set the state_number of newly
74 allocated types to some unique negative number. */
75 static int type_count;
77 /* The backup directory should be in the same file system as the
78 generated files, otherwise the rename(2) system call would fail.
79 If NULL, no backup is made when overwriting a generated file. */
80 static const char* backup_dir; /* (-B) program option. */
83 static outf_p create_file (const char *, const char *);
85 static const char *get_file_basename (const input_file *);
86 static const char *get_file_realbasename (const input_file *);
88 static int get_prefix_langdir_index (const char *);
89 static const char *get_file_langdir (const input_file *);
91 static void dump_pair (int indent, pair_p p);
92 static void dump_type (int indent, type_p p);
93 static void dump_type_list (int indent, type_p p);
96 /* Nonzero iff an error has occurred. */
97 bool hit_error = false;
99 static void gen_rtx_next (void);
100 static void write_rtx_next (void);
101 static void open_base_files (void);
102 static void close_output_files (void);
104 /* Report an error at POS, printing MSG. */
106 void
107 error_at_line (const struct fileloc *pos, const char *msg, ...)
109 va_list ap;
111 gcc_assert (pos != NULL && pos->file != NULL);
112 va_start (ap, msg);
114 fprintf (stderr, "%s:%d: ", get_input_file_name (pos->file), pos->line);
115 vfprintf (stderr, msg, ap);
116 fputc ('\n', stderr);
117 hit_error = true;
119 va_end (ap);
122 /* asprintf, but produces fatal message on out-of-memory. */
123 char *
124 xasprintf (const char *format, ...)
126 int n;
127 char *result;
128 va_list ap;
130 va_start (ap, format);
131 n = vasprintf (&result, format, ap);
132 if (result == NULL || n < 0)
133 fatal ("out of memory");
134 va_end (ap);
136 return result;
139 /* Input file handling. */
141 /* Table of all input files. */
142 const input_file **gt_files;
143 size_t num_gt_files;
145 /* A number of places use the name of this "gengtype.c" file for a
146 location for things that we can't rely on the source to define.
147 Make sure we can still use pointer comparison on filenames. */
148 input_file* this_file;
149 /* The "system.h" file is likewise specially useful. */
150 input_file* system_h_file;
152 /* Vector of per-language directories. */
153 const char **lang_dir_names;
154 size_t num_lang_dirs;
156 /* An array of output files suitable for definitions. There is one
157 BASE_FILES entry for each language. */
158 static outf_p *base_files;
162 #if ENABLE_CHECKING
163 /* Utility debugging function, printing the various type counts within
164 a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
165 void
166 dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
168 int nb_types = 0, nb_scalar = 0, nb_string = 0;
169 int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
170 int nb_lang_struct = 0, nb_param_struct = 0;
171 int nb_user_struct = 0, nb_undefined = 0;
172 type_p p = NULL;
173 for (p = t; p; p = p->next)
175 nb_types++;
176 switch (p->kind)
178 case TYPE_UNDEFINED:
179 nb_undefined++;
180 case TYPE_SCALAR:
181 nb_scalar++;
182 break;
183 case TYPE_STRING:
184 nb_string++;
185 break;
186 case TYPE_STRUCT:
187 nb_struct++;
188 break;
189 case TYPE_USER_STRUCT:
190 nb_user_struct++;
191 break;
192 case TYPE_UNION:
193 nb_union++;
194 break;
195 case TYPE_POINTER:
196 nb_pointer++;
197 break;
198 case TYPE_ARRAY:
199 nb_array++;
200 break;
201 case TYPE_LANG_STRUCT:
202 nb_lang_struct++;
203 break;
204 case TYPE_PARAM_STRUCT:
205 nb_param_struct++;
206 break;
207 case TYPE_NONE:
208 gcc_unreachable ();
211 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
212 lbasename (fil), lin, msg, nb_types);
213 if (nb_scalar > 0 || nb_string > 0)
214 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
215 if (nb_struct > 0 || nb_union > 0)
216 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
217 if (nb_pointer > 0 || nb_array > 0)
218 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
219 if (nb_lang_struct > 0 || nb_param_struct > 0)
220 fprintf (stderr, "@@%%@@ %d lang_structs, %d param_structs\n",
221 nb_lang_struct, nb_param_struct);
222 if (nb_user_struct > 0)
223 fprintf (stderr, "@@%%@@ %d user_structs\n", nb_user_struct);
224 if (nb_undefined > 0)
225 fprintf (stderr, "@@%%@@ %d undefined types\n", nb_undefined);
226 fprintf (stderr, "\n");
228 #endif /* ENABLE_CHECKING */
230 /* Scan the input file, LIST, and determine how much space we need to
231 store strings in. Also, count the number of language directories
232 and files. The numbers returned are overestimates as they does not
233 consider repeated files. */
234 static size_t
235 measure_input_list (FILE *list)
237 size_t n = 0;
238 int c;
239 bool atbol = true;
240 num_lang_dirs = 0;
241 num_gt_files = plugin_files ? nb_plugin_files : 0;
242 while ((c = getc (list)) != EOF)
244 n++;
245 if (atbol)
247 if (c == '[')
248 num_lang_dirs++;
249 else
251 /* Add space for a lang_bitmap before the input file name. */
252 n += sizeof (lang_bitmap);
253 num_gt_files++;
255 atbol = false;
258 if (c == '\n')
259 atbol = true;
262 rewind (list);
263 return n;
266 /* Read one input line from LIST to HEREP (which is updated). A
267 pointer to the string is returned via LINEP. If it was a language
268 subdirectory in square brackets, strip off the square brackets and
269 return true. Otherwise, leave space before the string for a
270 lang_bitmap, and return false. At EOF, returns false, does not
271 touch *HEREP, and sets *LINEP to NULL. POS is used for
272 diagnostics. */
273 static bool
274 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
276 char *here = *herep;
277 char *line;
278 int c = getc (list);
280 /* Read over whitespace. */
281 while (c == '\n' || c == ' ')
282 c = getc (list);
284 if (c == EOF)
286 *linep = 0;
287 return false;
289 else if (c == '[')
291 /* No space for a lang_bitmap is necessary. Discard the '['. */
292 c = getc (list);
293 line = here;
294 while (c != ']' && c != '\n' && c != EOF)
296 *here++ = c;
297 c = getc (list);
299 *here++ = '\0';
301 if (c == ']')
303 c = getc (list); /* eat what should be a newline */
304 if (c != '\n' && c != EOF)
305 error_at_line (pos, "junk on line after language tag [%s]", line);
307 else
308 error_at_line (pos, "missing close bracket for language tag [%s",
309 line);
311 *herep = here;
312 *linep = line;
313 return true;
315 else
317 /* Leave space for a lang_bitmap. */
318 memset (here, 0, sizeof (lang_bitmap));
319 here += sizeof (lang_bitmap);
320 line = here;
323 *here++ = c;
324 c = getc (list);
326 while (c != EOF && c != '\n');
327 *here++ = '\0';
328 *herep = here;
329 *linep = line;
330 return false;
334 /* Read the list of input files from LIST and compute all of the
335 relevant tables. There is one file per line of the list. At
336 first, all the files on the list are language-generic, but
337 eventually a line will appear which is the name of a language
338 subdirectory in square brackets, like this: [cp]. All subsequent
339 files are specific to that language, until another language
340 subdirectory tag appears. Files can appear more than once, if
341 they apply to more than one language. */
342 static void
343 read_input_list (const char *listname)
345 FILE *list = fopen (listname, "r");
346 if (!list)
347 fatal ("cannot open %s: %s", listname, xstrerror (errno));
348 else
350 struct fileloc epos;
351 size_t bufsz = measure_input_list (list);
352 char *buf = XNEWVEC (char, bufsz);
353 char *here = buf;
354 char *committed = buf;
355 char *limit = buf + bufsz;
356 char *line;
357 bool is_language;
358 size_t langno = 0;
359 size_t nfiles = 0;
360 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
362 epos.file = input_file_by_name (listname);
363 epos.line = 0;
365 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
366 gt_files = XNEWVEC (const input_file *, num_gt_files);
368 for (;;)
370 next_line:
371 epos.line++;
372 committed = here;
373 is_language = read_input_line (list, &here, &line, &epos);
374 gcc_assert (here <= limit);
375 if (line == 0)
376 break;
377 else if (is_language)
379 size_t i;
380 gcc_assert (langno <= num_lang_dirs);
381 for (i = 0; i < langno; i++)
382 if (strcmp (lang_dir_names[i], line) == 0)
384 error_at_line (&epos, "duplicate language tag [%s]",
385 line);
386 curlangs = 1 << i;
387 here = committed;
388 goto next_line;
391 curlangs = 1 << langno;
392 lang_dir_names[langno++] = line;
394 else
396 size_t i;
397 input_file *inpf = input_file_by_name (line);
398 gcc_assert (nfiles <= num_gt_files);
399 for (i = 0; i < nfiles; i++)
400 /* Since the input_file-s are uniquely hash-consed, we
401 can just compare pointers! */
402 if (gt_files[i] == inpf)
404 /* Throw away the string we just read, and add the
405 current language to the existing string's bitmap. */
406 lang_bitmap bmap = get_lang_bitmap (inpf);
407 if (bmap & curlangs)
408 error_at_line (&epos,
409 "file %s specified more than once "
410 "for language %s", line,
411 langno ==
412 0 ? "(all)" : lang_dir_names[langno -
413 1]);
415 bmap |= curlangs;
416 set_lang_bitmap (inpf, bmap);
417 here = committed;
418 goto next_line;
421 set_lang_bitmap (inpf, curlangs);
422 gt_files[nfiles++] = inpf;
425 /* Update the global counts now that we know accurately how many
426 things there are. (We do not bother resizing the arrays down.) */
427 num_lang_dirs = langno;
428 /* Add the plugin files if provided. */
429 if (plugin_files)
431 size_t i;
432 for (i = 0; i < nb_plugin_files; i++)
433 gt_files[nfiles++] = plugin_files[i];
435 num_gt_files = nfiles;
438 /* Sanity check: any file that resides in a language subdirectory
439 (e.g. 'cp') ought to belong to the corresponding language.
440 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
441 (Can you even do that? Should you be allowed to?) */
443 size_t f;
444 for (f = 0; f < num_gt_files; f++)
446 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
447 const char *basename = get_file_basename (gt_files[f]);
448 const char *slashpos = strchr (basename, '/');
449 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
450 const char *slashpos2 = strchr (basename, '\\');
452 if (!slashpos || (slashpos2 && slashpos2 < slashpos))
453 slashpos = slashpos2;
454 #endif
456 if (slashpos)
458 size_t l;
459 for (l = 0; l < num_lang_dirs; l++)
460 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
461 && memcmp (basename, lang_dir_names[l],
462 strlen (lang_dir_names[l])) == 0)
464 if (!(bitmap & (1 << l)))
465 error ("%s is in language directory '%s' but is not "
466 "tagged for that language",
467 basename, lang_dir_names[l]);
468 break;
474 if (ferror (list))
475 fatal ("error reading %s: %s", listname, xstrerror (errno));
477 fclose (list);
482 /* The one and only TYPE_STRING. */
484 struct type string_type = {
485 TYPE_STRING, 0, 0, 0, GC_USED, {0}
488 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
489 set early in main. */
491 struct type scalar_nonchar = {
492 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
495 struct type scalar_char = {
496 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
499 /* Lists of various things. */
501 pair_p typedefs = NULL;
502 type_p structures = NULL;
503 type_p param_structs = NULL;
504 pair_p variables = NULL;
506 static type_p find_param_structure (type_p t, type_p param[NUM_PARAM]);
507 static type_p adjust_field_tree_exp (type_p t, options_p opt);
508 static type_p adjust_field_rtx_def (type_p t, options_p opt);
510 /* Define S as a typedef to T at POS. */
512 void
513 do_typedef (const char *s, type_p t, struct fileloc *pos)
515 pair_p p;
517 /* temporary kludge - gengtype doesn't handle conditionals or
518 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
519 is coming from this file (main() sets them up with safe dummy
520 definitions). */
521 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
522 return;
524 for (p = typedefs; p != NULL; p = p->next)
525 if (strcmp (p->name, s) == 0)
527 if (p->type != t && strcmp (s, "result_type") != 0)
529 error_at_line (pos, "type `%s' previously defined", s);
530 error_at_line (&p->line, "previously defined here");
532 return;
535 p = XNEW (struct pair);
536 p->next = typedefs;
537 p->name = s;
538 p->type = t;
539 p->line = *pos;
540 p->opt = NULL;
541 typedefs = p;
544 /* Define S as a typename of a scalar. Cannot be used to define
545 typedefs of 'char'. Note: is also used for pointer-to-function
546 typedefs (which are therefore not treated as pointers). */
548 void
549 do_scalar_typedef (const char *s, struct fileloc *pos)
551 do_typedef (s, &scalar_nonchar, pos);
555 /* Define TYPE_NAME to be a user defined type at location POS. */
557 type_p
558 create_user_defined_type (const char *type_name, struct fileloc *pos)
560 type_p ty = find_structure (type_name, TYPE_USER_STRUCT);
562 /* We might have already seen an incomplete decl of the given type,
563 in which case we won't have yet seen a GTY((user)), and the type will
564 only have kind "TYPE_STRUCT". Mark it as a user struct. */
565 ty->kind = TYPE_USER_STRUCT;
567 ty->u.s.line = *pos;
568 ty->u.s.bitmap = get_lang_bitmap (pos->file);
569 do_typedef (type_name, ty, pos);
571 /* If TYPE_NAME specifies a template, create references to the types
572 in the template by pretending that each type is a field of TY.
573 This is needed to make sure that the types referenced by the
574 template are marked as used. */
575 char *str = xstrdup (type_name);
576 char *open_bracket = strchr (str, '<');
577 if (open_bracket)
579 /* We only accept simple template declarations (see
580 require_template_declaration), so we only need to parse a
581 comma-separated list of strings, implicitly assumed to
582 be type names. */
583 char *arg = open_bracket + 1;
584 char *type_id = strtok (arg, ",>");
585 pair_p fields = 0;
586 while (type_id)
588 /* Create a new field for every type found inside the template
589 parameter list. */
590 const char *field_name = xstrdup (type_id);
591 type_p arg_type = resolve_typedef (field_name, pos);
592 fields = create_field_at (fields, arg_type, field_name, 0, pos);
593 type_id = strtok (0, ",>");
596 /* Associate the field list to TY. */
597 ty->u.s.fields = fields;
599 free (str);
601 return ty;
605 /* Given a typedef name S, return its associated type. Return NULL if
606 S is not a registered type name. */
608 static type_p
609 type_for_name (const char *s)
611 pair_p p;
613 /* Special-case support for types within a "gcc::" namespace. Rather
614 than fully-supporting namespaces, simply strip off the "gcc::" prefix
615 where present. This allows us to have GTY roots of this form:
616 extern GTY(()) gcc::some_type *some_ptr;
617 where the autogenerated functions will refer to simply "some_type",
618 where they can be resolved into their namespace. */
619 if (0 == strncmp (s, "gcc::", 5))
620 s += 5;
622 for (p = typedefs; p != NULL; p = p->next)
623 if (strcmp (p->name, s) == 0)
624 return p->type;
625 return NULL;
629 /* Create an undefined type with name S and location POS. Return the
630 newly created type. */
632 static type_p
633 create_undefined_type (const char *s, struct fileloc *pos)
635 type_p ty = find_structure (s, TYPE_UNDEFINED);
636 ty->u.s.line = *pos;
637 ty->u.s.bitmap = get_lang_bitmap (pos->file);
638 do_typedef (s, ty, pos);
639 return ty;
643 /* Return the type previously defined for S. Use POS to report errors. */
645 type_p
646 resolve_typedef (const char *s, struct fileloc *pos)
648 bool is_template_instance = (strchr (s, '<') != NULL);
649 type_p p = type_for_name (s);
651 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
652 type for regular type identifiers. If the type identifier S is a
653 template instantiation, however, we treat it as a user defined
654 type.
656 FIXME, this is actually a limitation in gengtype. Supporting
657 template types and their instances would require keeping separate
658 track of the basic types definition and its instances. This
659 essentially forces all template classes in GC to be marked
660 GTY((user)). */
661 if (!p)
662 p = (is_template_instance)
663 ? create_user_defined_type (s, pos)
664 : create_undefined_type (s, pos);
666 return p;
670 /* Create and return a new structure with tag NAME at POS with fields
671 FIELDS and options O. The KIND of structure must be one of
672 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
674 type_p
675 new_structure (const char *name, enum typekind kind, struct fileloc *pos,
676 pair_p fields, options_p o)
678 type_p si;
679 type_p s = NULL;
680 lang_bitmap bitmap = get_lang_bitmap (pos->file);
681 bool isunion = (kind == TYPE_UNION);
683 gcc_assert (union_or_struct_p (kind));
685 for (si = structures; si != NULL; si = si->next)
686 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
688 type_p ls = NULL;
689 if (si->kind == TYPE_LANG_STRUCT)
691 ls = si;
693 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
694 if (si->u.s.bitmap == bitmap)
695 s = si;
697 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
699 ls = si;
700 type_count++;
701 si = XCNEW (struct type);
702 memcpy (si, ls, sizeof (struct type));
703 ls->kind = TYPE_LANG_STRUCT;
704 ls->u.s.lang_struct = si;
705 ls->u.s.fields = NULL;
706 si->next = NULL;
707 si->state_number = -type_count;
708 si->pointer_to = NULL;
709 si->u.s.lang_struct = ls;
711 else
712 s = si;
714 if (ls != NULL && s == NULL)
716 type_count++;
717 s = XCNEW (struct type);
718 s->state_number = -type_count;
719 s->next = ls->u.s.lang_struct;
720 ls->u.s.lang_struct = s;
721 s->u.s.lang_struct = ls;
723 break;
726 if (s == NULL)
728 type_count++;
729 s = XCNEW (struct type);
730 s->state_number = -type_count;
731 s->next = structures;
732 structures = s;
735 if (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap))
737 error_at_line (pos, "duplicate definition of '%s %s'",
738 isunion ? "union" : "struct", s->u.s.tag);
739 error_at_line (&s->u.s.line, "previous definition here");
742 s->kind = kind;
743 s->u.s.tag = name;
744 s->u.s.line = *pos;
745 s->u.s.fields = fields;
746 s->u.s.opt = o;
747 s->u.s.bitmap = bitmap;
748 if (s->u.s.lang_struct)
749 s->u.s.lang_struct->u.s.bitmap |= bitmap;
751 return s;
754 /* Return the previously-defined structure or union with tag NAME,
755 or a new empty structure or union if none was defined previously.
756 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
757 TYPE_USER_STRUCT. */
759 type_p
760 find_structure (const char *name, enum typekind kind)
762 type_p s;
763 bool isunion = (kind == TYPE_UNION);
765 gcc_assert (kind == TYPE_UNDEFINED || union_or_struct_p (kind));
767 for (s = structures; s != NULL; s = s->next)
768 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
769 return s;
771 type_count++;
772 s = XCNEW (struct type);
773 s->next = structures;
774 s->state_number = -type_count;
775 structures = s;
776 s->kind = kind;
777 s->u.s.tag = name;
778 structures = s;
779 return s;
782 /* Return the previously-defined parameterized structure for structure
783 T and parameters PARAM, or a new parameterized empty structure or
784 union if none was defined previously. */
786 static type_p
787 find_param_structure (type_p t, type_p param[NUM_PARAM])
789 type_p res;
791 for (res = param_structs; res; res = res->next)
792 if (res->u.param_struct.stru == t
793 && memcmp (res->u.param_struct.param, param,
794 sizeof (type_p) * NUM_PARAM) == 0)
795 break;
796 if (res == NULL)
798 type_count++;
799 res = XCNEW (struct type);
800 res->kind = TYPE_PARAM_STRUCT;
801 res->next = param_structs;
802 res->state_number = -type_count;
803 param_structs = res;
804 res->u.param_struct.stru = t;
805 memcpy (res->u.param_struct.param, param, sizeof (type_p) * NUM_PARAM);
807 return res;
810 /* Return a scalar type with name NAME. */
812 type_p
813 create_scalar_type (const char *name)
815 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
816 return &scalar_char;
817 else
818 return &scalar_nonchar;
822 /* Return a pointer to T. */
824 type_p
825 create_pointer (type_p t)
827 if (!t->pointer_to)
829 type_p r = XCNEW (struct type);
830 type_count++;
831 r->state_number = -type_count;
832 r->kind = TYPE_POINTER;
833 r->u.p = t;
834 t->pointer_to = r;
836 return t->pointer_to;
839 /* Return an array of length LEN. */
841 type_p
842 create_array (type_p t, const char *len)
844 type_p v;
846 type_count++;
847 v = XCNEW (struct type);
848 v->kind = TYPE_ARRAY;
849 v->state_number = -type_count;
850 v->u.a.p = t;
851 v->u.a.len = len;
852 return v;
855 /* Return a string options structure with name NAME and info INFO.
856 NEXT is the next option in the chain. */
857 options_p
858 create_string_option (options_p next, const char *name, const char *info)
860 options_p o = XNEW (struct options);
861 o->kind = OPTION_STRING;
862 o->next = next;
863 o->name = name;
864 o->info.string = info;
865 return o;
868 /* Create a type options structure with name NAME and info INFO. NEXT
869 is the next option in the chain. */
870 options_p
871 create_type_option (options_p next, const char* name, type_p info)
873 options_p o = XNEW (struct options);
874 o->next = next;
875 o->name = name;
876 o->kind = OPTION_TYPE;
877 o->info.type = info;
878 return o;
881 /* Create a nested pointer options structure with name NAME and info
882 INFO. NEXT is the next option in the chain. */
883 options_p
884 create_nested_option (options_p next, const char* name,
885 struct nested_ptr_data* info)
887 options_p o;
888 o = XNEW (struct options);
889 o->next = next;
890 o->name = name;
891 o->kind = OPTION_NESTED;
892 o->info.nested = info;
893 return o;
896 /* Return an options structure for a "nested_ptr" option. */
897 options_p
898 create_nested_ptr_option (options_p next, type_p t,
899 const char *to, const char *from)
901 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
903 d->type = adjust_field_type (t, 0);
904 d->convert_to = to;
905 d->convert_from = from;
906 return create_nested_option (next, "nested_ptr", d);
909 /* Add a variable named S of type T with options O defined at POS,
910 to `variables'. */
911 void
912 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
914 pair_p n;
915 n = XNEW (struct pair);
916 n->name = s;
917 n->type = t;
918 n->line = *pos;
919 n->opt = o;
920 n->next = variables;
921 variables = n;
924 /* Most-general structure field creator. */
925 static pair_p
926 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
927 const input_file *inpf, int line)
929 pair_p field;
931 field = XNEW (struct pair);
932 field->next = next;
933 field->type = type;
934 field->name = name;
935 field->opt = opt;
936 field->line.file = inpf;
937 field->line.line = line;
938 return field;
941 /* Create a field that came from the source code we are scanning,
942 i.e. we have a 'struct fileloc', and possibly options; also,
943 adjust_field_type should be called. */
944 pair_p
945 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
946 struct fileloc *pos)
948 return create_field_all (next, adjust_field_type (type, opt),
949 name, opt, pos->file, pos->line);
952 /* Create a fake field with the given type and name. NEXT is the next
953 field in the chain. */
954 #define create_field(next,type,name) \
955 create_field_all (next,type,name, 0, this_file, __LINE__)
957 /* Like create_field, but the field is only valid when condition COND
958 is true. */
960 static pair_p
961 create_optional_field_ (pair_p next, type_p type, const char *name,
962 const char *cond, int line)
964 static int id = 1;
965 pair_p union_fields;
966 type_p union_type;
968 /* Create a fake union type with a single nameless field of type TYPE.
969 The field has a tag of "1". This allows us to make the presence
970 of a field of type TYPE depend on some boolean "desc" being true. */
971 union_fields = create_field (NULL, type, "");
972 union_fields->opt =
973 create_string_option (union_fields->opt, "dot", "");
974 union_fields->opt =
975 create_string_option (union_fields->opt, "tag", "1");
976 union_type =
977 new_structure (xasprintf ("%s_%d", "fake_union", id++), TYPE_UNION,
978 &lexer_line, union_fields, NULL);
980 /* Create the field and give it the new fake union type. Add a "desc"
981 tag that specifies the condition under which the field is valid. */
982 return create_field_all (next, union_type, name,
983 create_string_option (0, "desc", cond),
984 this_file, line);
987 #define create_optional_field(next,type,name,cond) \
988 create_optional_field_(next,type,name,cond,__LINE__)
990 /* Reverse a linked list of 'struct pair's in place. */
991 pair_p
992 nreverse_pairs (pair_p list)
994 pair_p prev = 0, p, next;
995 for (p = list; p; p = next)
997 next = p->next;
998 p->next = prev;
999 prev = p;
1001 return prev;
1005 /* We don't care how long a CONST_DOUBLE is. */
1006 #define CONST_DOUBLE_FORMAT "ww"
1007 /* We don't want to see codes that are only for generator files. */
1008 #undef GENERATOR_FILE
1010 enum rtx_code
1012 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1013 #include "rtl.def"
1014 #undef DEF_RTL_EXPR
1015 NUM_RTX_CODE
1018 static const char *const rtx_name[NUM_RTX_CODE] = {
1019 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1020 #include "rtl.def"
1021 #undef DEF_RTL_EXPR
1024 static const char *const rtx_format[NUM_RTX_CODE] = {
1025 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1026 #include "rtl.def"
1027 #undef DEF_RTL_EXPR
1030 static int rtx_next_new[NUM_RTX_CODE];
1032 /* We also need codes and names for insn notes (not register notes).
1033 Note that we do *not* bias the note values here. */
1034 enum insn_note
1036 #define DEF_INSN_NOTE(NAME) NAME,
1037 #include "insn-notes.def"
1038 #undef DEF_INSN_NOTE
1040 NOTE_INSN_MAX
1043 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1044 default field for line number notes. */
1045 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
1046 #define DEF_INSN_NOTE(NAME) #NAME,
1047 #include "insn-notes.def"
1048 #undef DEF_INSN_NOTE
1051 #undef CONST_DOUBLE_FORMAT
1052 #define GENERATOR_FILE
1054 /* Generate the contents of the rtx_next array. This really doesn't belong
1055 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1057 static void
1058 gen_rtx_next (void)
1060 int i;
1061 for (i = 0; i < NUM_RTX_CODE; i++)
1063 int k;
1065 rtx_next_new[i] = -1;
1066 if (strncmp (rtx_format[i], "iuu", 3) == 0)
1067 rtx_next_new[i] = 2;
1068 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1069 rtx_next_new[i] = 1;
1070 else
1071 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
1072 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1073 rtx_next_new[i] = k;
1077 /* Write out the contents of the rtx_next array. */
1078 static void
1079 write_rtx_next (void)
1081 outf_p f = get_output_file_with_visibility (NULL);
1082 int i;
1083 if (!f)
1084 return;
1086 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
1087 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1088 for (i = 0; i < NUM_RTX_CODE; i++)
1089 if (rtx_next_new[i] == -1)
1090 oprintf (f, " 0,\n");
1091 else
1092 oprintf (f,
1093 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1094 oprintf (f, "};\n");
1097 /* Handle `special("rtx_def")'. This is a special case for field
1098 `fld' of struct rtx_def, which is an array of unions whose values
1099 are based in a complex way on the type of RTL. */
1101 static type_p
1102 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1104 pair_p flds = NULL;
1105 options_p nodot;
1106 int i;
1107 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1108 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1110 if (t->kind != TYPE_UNION)
1112 error_at_line (&lexer_line,
1113 "special `rtx_def' must be applied to a union");
1114 return &string_type;
1117 nodot = create_string_option (NULL, "dot", "");
1119 rtx_tp = create_pointer (find_structure ("rtx_def", TYPE_STRUCT));
1120 rtvec_tp = create_pointer (find_structure ("rtvec_def", TYPE_STRUCT));
1121 tree_tp = create_pointer (find_structure ("tree_node", TYPE_UNION));
1122 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", TYPE_STRUCT));
1123 reg_attrs_tp =
1124 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT));
1125 basic_block_tp =
1126 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT));
1127 constant_tp =
1128 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT));
1129 scalar_tp = &scalar_nonchar; /* rtunion int */
1132 pair_p note_flds = NULL;
1133 int c;
1135 for (c = 0; c <= NOTE_INSN_MAX; c++)
1137 switch (c)
1139 case NOTE_INSN_MAX:
1140 case NOTE_INSN_DELETED_LABEL:
1141 case NOTE_INSN_DELETED_DEBUG_LABEL:
1142 note_flds = create_field (note_flds, &string_type, "rt_str");
1143 break;
1145 case NOTE_INSN_BLOCK_BEG:
1146 case NOTE_INSN_BLOCK_END:
1147 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1148 break;
1150 case NOTE_INSN_VAR_LOCATION:
1151 case NOTE_INSN_CALL_ARG_LOCATION:
1152 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1153 break;
1155 default:
1156 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1157 break;
1159 /* NOTE_INSN_MAX is used as the default field for line
1160 number notes. */
1161 if (c == NOTE_INSN_MAX)
1162 note_flds->opt =
1163 create_string_option (nodot, "default", "");
1164 else
1165 note_flds->opt =
1166 create_string_option (nodot, "tag", note_insn_name[c]);
1168 note_union_tp = new_structure ("rtx_def_note_subunion", TYPE_UNION,
1169 &lexer_line, note_flds, NULL);
1171 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1173 pair_p sym_flds;
1174 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1175 sym_flds->opt = create_string_option (nodot, "default", "");
1176 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1177 sym_flds->opt = create_string_option (nodot, "tag", "1");
1178 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", TYPE_UNION,
1179 &lexer_line, sym_flds, NULL);
1181 for (i = 0; i < NUM_RTX_CODE; i++)
1183 pair_p subfields = NULL;
1184 size_t aindex, nmindex;
1185 const char *sname;
1186 type_p substruct;
1187 char *ftag;
1189 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1191 type_p t;
1192 const char *subname;
1194 switch (rtx_format[i][aindex])
1196 case '*':
1197 case 'i':
1198 case 'n':
1199 case 'w':
1200 t = scalar_tp;
1201 subname = "rt_int";
1202 break;
1204 case '0':
1205 if (i == MEM && aindex == 1)
1206 t = mem_attrs_tp, subname = "rt_mem";
1207 else if (i == JUMP_INSN && aindex == 8)
1208 t = rtx_tp, subname = "rt_rtx";
1209 else if (i == CODE_LABEL && aindex == 5)
1210 t = scalar_tp, subname = "rt_int";
1211 else if (i == CODE_LABEL && aindex == 4)
1212 t = rtx_tp, subname = "rt_rtx";
1213 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1214 t = rtx_tp, subname = "rt_rtx";
1215 else if (i == NOTE && aindex == 4)
1216 t = note_union_tp, subname = "";
1217 else if (i == NOTE && aindex == 5)
1218 t = scalar_tp, subname = "rt_int";
1219 else if (i == NOTE && aindex >= 7)
1220 t = scalar_tp, subname = "rt_int";
1221 else if (i == ADDR_DIFF_VEC && aindex == 4)
1222 t = scalar_tp, subname = "rt_int";
1223 else if (i == VALUE && aindex == 0)
1224 t = scalar_tp, subname = "rt_int";
1225 else if (i == DEBUG_EXPR && aindex == 0)
1226 t = tree_tp, subname = "rt_tree";
1227 else if (i == REG && aindex == 1)
1228 t = scalar_tp, subname = "rt_int";
1229 else if (i == REG && aindex == 2)
1230 t = reg_attrs_tp, subname = "rt_reg";
1231 else if (i == SCRATCH && aindex == 0)
1232 t = scalar_tp, subname = "rt_int";
1233 else if (i == SYMBOL_REF && aindex == 1)
1234 t = scalar_tp, subname = "rt_int";
1235 else if (i == SYMBOL_REF && aindex == 2)
1236 t = symbol_union_tp, subname = "";
1237 else if (i == JUMP_TABLE_DATA && aindex >= 5)
1238 t = scalar_tp, subname = "rt_int";
1239 else if (i == BARRIER && aindex >= 3)
1240 t = scalar_tp, subname = "rt_int";
1241 else if (i == ENTRY_VALUE && aindex == 0)
1242 t = rtx_tp, subname = "rt_rtx";
1243 else
1245 error_at_line
1246 (&lexer_line,
1247 "rtx type `%s' has `0' in position %lu, can't handle",
1248 rtx_name[i], (unsigned long) aindex);
1249 t = &string_type;
1250 subname = "rt_int";
1252 break;
1254 case 's':
1255 case 'S':
1256 case 'T':
1257 t = &string_type;
1258 subname = "rt_str";
1259 break;
1261 case 'e':
1262 case 'u':
1263 t = rtx_tp;
1264 subname = "rt_rtx";
1265 break;
1267 case 'E':
1268 case 'V':
1269 t = rtvec_tp;
1270 subname = "rt_rtvec";
1271 break;
1273 case 't':
1274 t = tree_tp;
1275 subname = "rt_tree";
1276 break;
1278 case 'B':
1279 t = basic_block_tp;
1280 subname = "rt_bb";
1281 break;
1283 default:
1284 error_at_line
1285 (&lexer_line,
1286 "rtx type `%s' has `%c' in position %lu, can't handle",
1287 rtx_name[i], rtx_format[i][aindex],
1288 (unsigned long) aindex);
1289 t = &string_type;
1290 subname = "rt_int";
1291 break;
1294 subfields = create_field (subfields, t,
1295 xasprintf (".fld[%lu].%s",
1296 (unsigned long) aindex,
1297 subname));
1298 subfields->opt = nodot;
1299 if (t == note_union_tp)
1300 subfields->opt =
1301 create_string_option (subfields->opt, "desc",
1302 "NOTE_KIND (&%0)");
1303 if (t == symbol_union_tp)
1304 subfields->opt =
1305 create_string_option (subfields->opt, "desc",
1306 "CONSTANT_POOL_ADDRESS_P (&%0)");
1309 if (i == SYMBOL_REF)
1311 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1312 holds. */
1313 type_p field_tp = find_structure ("block_symbol", TYPE_STRUCT);
1314 subfields
1315 = create_optional_field (subfields, field_tp, "block_sym",
1316 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1319 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1320 substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
1321 NULL);
1323 ftag = xstrdup (rtx_name[i]);
1324 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1325 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1326 flds = create_field (flds, substruct, "");
1327 flds->opt = create_string_option (nodot, "tag", ftag);
1329 return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
1330 nodot);
1333 /* Handle `special("tree_exp")'. This is a special case for
1334 field `operands' of struct tree_exp, which although it claims to contain
1335 pointers to trees, actually sometimes contains pointers to RTL too.
1336 Passed T, the old type of the field, and OPT its options. Returns
1337 a new type for the field. */
1339 static type_p
1340 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1342 pair_p flds;
1343 options_p nodot;
1345 if (t->kind != TYPE_ARRAY)
1347 error_at_line (&lexer_line,
1348 "special `tree_exp' must be applied to an array");
1349 return &string_type;
1352 nodot = create_string_option (NULL, "dot", "");
1354 flds = create_field (NULL, t, "");
1355 flds->opt = create_string_option (nodot, "length",
1356 "TREE_OPERAND_LENGTH ((tree) &%0)");
1357 flds->opt = create_string_option (flds->opt, "default", "");
1359 return new_structure ("tree_exp_subunion", TYPE_UNION, &lexer_line, flds,
1360 nodot);
1363 /* Perform any special processing on a type T, about to become the type
1364 of a field. Return the appropriate type for the field.
1365 At present:
1366 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1367 - Similarly for arrays of pointer-to-char;
1368 - Converts structures for which a parameter is provided to
1369 TYPE_PARAM_STRUCT;
1370 - Handles "special" options.
1373 type_p
1374 adjust_field_type (type_p t, options_p opt)
1376 int length_p = 0;
1377 const int pointer_p = t->kind == TYPE_POINTER;
1378 type_p params[NUM_PARAM];
1379 int params_p = 0;
1380 int i;
1382 for (i = 0; i < NUM_PARAM; i++)
1383 params[i] = NULL;
1385 for (; opt; opt = opt->next)
1386 if (strcmp (opt->name, "length") == 0)
1388 if (length_p)
1389 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1390 if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
1392 error_at_line (&lexer_line,
1393 "option `%s' may not be applied to "
1394 "arrays of atomic types", opt->name);
1396 length_p = 1;
1398 else if ((strcmp (opt->name, "param_is") == 0
1399 || (strncmp (opt->name, "param", 5) == 0
1400 && ISDIGIT (opt->name[5])
1401 && strcmp (opt->name + 6, "_is") == 0))
1402 && opt->kind == OPTION_TYPE)
1404 int num = ISDIGIT (opt->name[5]) ? opt->name[5] - '0' : 0;
1406 if (!union_or_struct_p (t)
1407 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
1409 error_at_line (&lexer_line,
1410 "option `%s' may only be applied to structures or structure pointers",
1411 opt->name);
1412 return t;
1415 params_p = 1;
1416 if (params[num] != NULL)
1417 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1418 if (!ISDIGIT (opt->name[5]))
1419 params[num] = create_pointer (opt->info.type);
1420 else
1421 params[num] = opt->info.type;
1423 else if (strcmp (opt->name, "special") == 0
1424 && opt->kind == OPTION_STRING)
1426 const char *special_name = opt->info.string;
1427 if (strcmp (special_name, "tree_exp") == 0)
1428 t = adjust_field_tree_exp (t, opt);
1429 else if (strcmp (special_name, "rtx_def") == 0)
1430 t = adjust_field_rtx_def (t, opt);
1431 else
1432 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1435 if (params_p)
1437 type_p realt;
1439 if (pointer_p)
1440 t = t->u.p;
1441 realt = find_param_structure (t, params);
1442 t = pointer_p ? create_pointer (realt) : realt;
1445 if (!length_p
1446 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1447 return &string_type;
1448 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1449 && t->u.a.p->u.p->kind == TYPE_SCALAR
1450 && t->u.a.p->u.p->u.scalar_is_char)
1451 return create_array (&string_type, t->u.a.len);
1453 return t;
1457 static void set_gc_used_type (type_p, enum gc_used_enum, type_p *,
1458 bool = false);
1459 static void set_gc_used (pair_p);
1461 /* Handle OPT for set_gc_used_type. */
1463 static void
1464 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1465 int *pass_param, int *length, int *skip,
1466 type_p *nested_ptr)
1468 options_p o;
1469 for (o = opt; o; o = o->next)
1470 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1471 && o->kind == OPTION_TYPE)
1472 set_gc_used_type (o->info.type,
1473 GC_POINTED_TO, NULL);
1474 else if (strcmp (o->name, "maybe_undef") == 0)
1475 *maybe_undef = 1;
1476 else if (strcmp (o->name, "use_params") == 0)
1477 *pass_param = 1;
1478 else if (strcmp (o->name, "length") == 0)
1479 *length = 1;
1480 else if (strcmp (o->name, "skip") == 0)
1481 *skip = 1;
1482 else if (strcmp (o->name, "nested_ptr") == 0
1483 && o->kind == OPTION_NESTED)
1484 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1488 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1490 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1491 are set to GC_UNUSED. Otherwise, an error is emitted for
1492 TYPE_UNDEFINED types. This is used to support user-defined
1493 template types with non-type arguments.
1495 For instance, when we parse a template type with enum arguments
1496 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1497 artificial fields for 'MyType', one for 'AnotherType', the other
1498 one for 'EnumValue'.
1500 At the time that we parse this type we don't know that 'EnumValue'
1501 is really an enum value, so the parser creates a TYPE_UNDEFINED
1502 type for it. Since 'EnumValue' is never resolved to a known
1503 structure, it will stay with TYPE_UNDEFINED.
1505 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1506 'EnumValue'. Generating marking code for it would cause
1507 compilation failures since the marking routines assumes that
1508 'EnumValue' is a type. */
1510 static void
1511 set_gc_used_type (type_p t, enum gc_used_enum level, type_p param[NUM_PARAM],
1512 bool allow_undefined_types)
1514 if (t->gc_used >= level)
1515 return;
1517 t->gc_used = level;
1519 switch (t->kind)
1521 case TYPE_STRUCT:
1522 case TYPE_UNION:
1523 case TYPE_USER_STRUCT:
1525 pair_p f;
1526 int dummy;
1527 type_p dummy2;
1528 bool allow_undefined_field_types = (t->kind == TYPE_USER_STRUCT);
1530 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
1531 &dummy2);
1533 for (f = t->u.s.fields; f; f = f->next)
1535 int maybe_undef = 0;
1536 int pass_param = 0;
1537 int length = 0;
1538 int skip = 0;
1539 type_p nested_ptr = NULL;
1540 process_gc_options (f->opt, level, &maybe_undef, &pass_param,
1541 &length, &skip, &nested_ptr);
1543 if (nested_ptr && f->type->kind == TYPE_POINTER)
1544 set_gc_used_type (nested_ptr, GC_POINTED_TO,
1545 pass_param ? param : NULL);
1546 else if (length && f->type->kind == TYPE_POINTER)
1547 set_gc_used_type (f->type->u.p, GC_USED, NULL);
1548 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1549 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO, NULL);
1550 else if (pass_param && f->type->kind == TYPE_POINTER && param)
1551 set_gc_used_type (find_param_structure (f->type->u.p, param),
1552 GC_POINTED_TO, NULL);
1553 else if (skip)
1554 ; /* target type is not used through this field */
1555 else
1556 set_gc_used_type (f->type, GC_USED, pass_param ? param : NULL,
1557 allow_undefined_field_types);
1559 break;
1562 case TYPE_UNDEFINED:
1563 if (level > GC_UNUSED)
1565 if (!allow_undefined_types)
1566 error_at_line (&t->u.s.line, "undefined type `%s'", t->u.s.tag);
1567 t->gc_used = GC_UNUSED;
1569 break;
1571 case TYPE_POINTER:
1572 set_gc_used_type (t->u.p, GC_POINTED_TO, NULL);
1573 break;
1575 case TYPE_ARRAY:
1576 set_gc_used_type (t->u.a.p, GC_USED, param);
1577 break;
1579 case TYPE_LANG_STRUCT:
1580 for (t = t->u.s.lang_struct; t; t = t->next)
1581 set_gc_used_type (t, level, param);
1582 break;
1584 case TYPE_PARAM_STRUCT:
1586 int i;
1587 for (i = 0; i < NUM_PARAM; i++)
1588 if (t->u.param_struct.param[i] != 0)
1589 set_gc_used_type (t->u.param_struct.param[i], GC_USED, NULL);
1591 if (t->u.param_struct.stru->gc_used == GC_POINTED_TO)
1592 level = GC_POINTED_TO;
1593 else
1594 level = GC_USED;
1595 t->u.param_struct.stru->gc_used = GC_UNUSED;
1596 set_gc_used_type (t->u.param_struct.stru, level,
1597 t->u.param_struct.param);
1598 break;
1600 default:
1601 break;
1605 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1607 static void
1608 set_gc_used (pair_p variables)
1610 int nbvars = 0;
1611 pair_p p;
1612 for (p = variables; p; p = p->next)
1614 set_gc_used_type (p->type, GC_USED, NULL);
1615 nbvars++;
1617 if (verbosity_level >= 2)
1618 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1621 /* File mapping routines. For each input file, there is one output .c file
1622 (but some output files have many input files), and there is one .h file
1623 for the whole build. */
1625 /* Output file handling. */
1627 /* Create and return an outf_p for a new file for NAME, to be called
1628 ONAME. */
1630 static outf_p
1631 create_file (const char *name, const char *oname)
1633 static const char *const hdr[] = {
1634 " Copyright (C) 2004-2013 Free Software Foundation, Inc.\n",
1635 "\n",
1636 "This file is part of GCC.\n",
1637 "\n",
1638 "GCC is free software; you can redistribute it and/or modify it under\n",
1639 "the terms of the GNU General Public License as published by the Free\n",
1640 "Software Foundation; either version 3, or (at your option) any later\n",
1641 "version.\n",
1642 "\n",
1643 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1644 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1645 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1646 "for more details.\n",
1647 "\n",
1648 "You should have received a copy of the GNU General Public License\n",
1649 "along with GCC; see the file COPYING3. If not see\n",
1650 "<http://www.gnu.org/licenses/>. */\n",
1651 "\n",
1652 "/* This file is machine generated. Do not edit. */\n"
1654 outf_p f;
1655 size_t i;
1657 gcc_assert (name != NULL);
1658 gcc_assert (oname != NULL);
1659 f = XCNEW (struct outf);
1660 f->next = output_files;
1661 f->name = oname;
1662 output_files = f;
1664 oprintf (f, "/* Type information for %s.\n", name);
1665 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1666 oprintf (f, "%s", hdr[i]);
1667 return f;
1670 /* Print, like fprintf, to O.
1671 N.B. You might think this could be implemented more efficiently
1672 with vsnprintf(). Unfortunately, there are C libraries that
1673 provide that function but without the C99 semantics for its return
1674 value, making it impossible to know how much space is required. */
1675 void
1676 oprintf (outf_p o, const char *format, ...)
1678 char *s;
1679 size_t slength;
1680 va_list ap;
1682 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1683 in that case. */
1684 if (!o)
1685 return;
1687 va_start (ap, format);
1688 slength = vasprintf (&s, format, ap);
1689 if (s == NULL || (int) slength < 0)
1690 fatal ("out of memory");
1691 va_end (ap);
1693 if (o->bufused + slength > o->buflength)
1695 size_t new_len = o->buflength;
1696 if (new_len == 0)
1697 new_len = 1024;
1700 new_len *= 2;
1702 while (o->bufused + slength >= new_len);
1703 o->buf = XRESIZEVEC (char, o->buf, new_len);
1704 o->buflength = new_len;
1706 memcpy (o->buf + o->bufused, s, slength);
1707 o->bufused += slength;
1708 free (s);
1711 /* Open the global header file and the language-specific header files. */
1713 static void
1714 open_base_files (void)
1716 size_t i;
1718 if (nb_plugin_files > 0 && plugin_files)
1719 return;
1721 header_file = create_file ("GCC", "gtype-desc.h");
1723 base_files = XNEWVEC (outf_p, num_lang_dirs);
1725 for (i = 0; i < num_lang_dirs; i++)
1726 base_files[i] = create_file (lang_dir_names[i],
1727 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1729 /* gtype-desc.c is a little special, so we create it here. */
1731 /* The order of files here matters very much. */
1732 static const char *const ifiles[] = {
1733 "config.h", "system.h", "coretypes.h", "tm.h",
1734 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1735 "tree.h", "rtl.h", "wide-int.h", "function.h", "insn-config.h", "expr.h",
1736 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1737 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1738 "tree-ssa.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1739 "except.h", "output.h", "gimple.h", "cfgloop.h",
1740 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h",
1741 "ipa-inline.h", "dwarf2out.h", NULL
1743 const char *const *ifp;
1744 outf_p gtype_desc_c;
1746 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1747 for (ifp = ifiles; *ifp; ifp++)
1748 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1750 /* Make sure we handle "cfun" specially. */
1751 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1752 oprintf (gtype_desc_c, "#undef cfun\n");
1754 oprintf (gtype_desc_c,
1755 "\n"
1756 "/* Types with a \"gcc::\" namespace have it stripped\n"
1757 " during gengtype parsing. Provide a \"using\" directive\n"
1758 " to ensure that the fully-qualified types are found. */\n"
1759 "using namespace gcc;\n");
1763 /* For INPF an input file, return the real basename of INPF, with all
1764 the directory components skipped. */
1766 static const char *
1767 get_file_realbasename (const input_file *inpf)
1769 return lbasename (get_input_file_name (inpf));
1772 /* For INPF a filename, return the relative path to INPF from
1773 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1775 const char *
1776 get_file_srcdir_relative_path (const input_file *inpf)
1778 const char *f = get_input_file_name (inpf);
1779 if (strlen (f) > srcdir_len
1780 && IS_DIR_SEPARATOR (f[srcdir_len])
1781 && strncmp (f, srcdir, srcdir_len) == 0)
1782 return f + srcdir_len + 1;
1783 else
1784 return NULL;
1787 /* For INPF an input_file, return the relative path to INPF from
1788 $(srcdir) if the latter is a prefix in INPF, or the real basename
1789 of INPF otherwise. */
1791 static const char *
1792 get_file_basename (const input_file *inpf)
1794 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1796 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1799 /* For F a filename, return the lang_dir_names relative index of the language
1800 directory that is a prefix in F, if any, -1 otherwise. */
1802 static int
1803 get_prefix_langdir_index (const char *f)
1805 size_t f_len = strlen (f);
1806 size_t lang_index;
1808 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1810 const char *langdir = lang_dir_names[lang_index];
1811 size_t langdir_len = strlen (langdir);
1813 if (f_len > langdir_len
1814 && IS_DIR_SEPARATOR (f[langdir_len])
1815 && memcmp (f, langdir, langdir_len) == 0)
1816 return lang_index;
1819 return -1;
1822 /* For INPF an input file, return the name of language directory where
1823 F is located, if any, NULL otherwise. */
1825 static const char *
1826 get_file_langdir (const input_file *inpf)
1828 /* Get the relative path to INPF from $(srcdir) and find the
1829 language by comparing the prefix with language directory names.
1830 If INPF is not even srcdir relative, no point in looking
1831 further. */
1833 int lang_index;
1834 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1835 const char *r;
1837 if (!srcdir_relative_path)
1838 return NULL;
1840 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1841 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1842 r = "c-family";
1843 else if (lang_index >= 0)
1844 r = lang_dir_names[lang_index];
1845 else
1846 r = NULL;
1848 return r;
1851 /* The gt- output file name for INPF. */
1853 static const char *
1854 get_file_gtfilename (const input_file *inpf)
1856 /* Cook up an initial version of the gt- file name from the file real
1857 basename and the language name, if any. */
1859 const char *basename = get_file_realbasename (inpf);
1860 const char *langdir = get_file_langdir (inpf);
1862 char *result =
1863 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1864 : xasprintf ("gt-%s", basename));
1866 /* Then replace all non alphanumerics characters by '-' and change the
1867 extension to ".h". We expect the input filename extension was at least
1868 one character long. */
1870 char *s = result;
1872 for (; *s != '.'; s++)
1873 if (!ISALNUM (*s) && *s != '-')
1874 *s = '-';
1876 memcpy (s, ".h", sizeof (".h"));
1878 return result;
1881 /* Each input_file has its associated output file outf_p. The
1882 association is computed by the function
1883 get_output_file_with_visibility. The associated file is cached
1884 inside input_file in its inpoutf field, so is really computed only
1885 once. Associated output file paths (i.e. output_name-s) are
1886 computed by a rule based regexp machinery, using the files_rules
1887 array of struct file_rule_st. A for_name is also computed, giving
1888 the source file name for which the output_file is generated; it is
1889 often the last component of the input_file path. */
1893 Regexpr machinery to compute the output_name and for_name-s of each
1894 input_file. We have a sequence of file rules which gives the POSIX
1895 extended regular expression to match an input file path, and two
1896 transformed strings for the corresponding output_name and the
1897 corresponding for_name. The transformed string contain dollars: $0
1898 is replaced by the entire match, $1 is replaced by the substring
1899 matching the first parenthesis in the regexp, etc. And $$ is replaced
1900 by a single verbatim dollar. The rule order is important. The
1901 general case is last, and the particular cases should come before.
1902 An action routine can, when needed, update the out_name & for_name
1903 and/or return the appropriate output file. It is invoked only when a
1904 rule is triggered. When a rule is triggered, the output_name and
1905 for_name are computed using their transform string in while $$, $0,
1906 $1, ... are suitably replaced. If there is an action, it is called.
1907 In some few cases, the action can directly return the outf_p, but
1908 usually it just updates the output_name and for_name so should free
1909 them before replacing them. The get_output_file_with_visibility
1910 function creates an outf_p only once per each output_name, so it
1911 scans the output_files list for previously seen output file names.
1914 /* Signature of actions in file rules. */
1915 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
1918 struct file_rule_st {
1919 const char* frul_srcexpr; /* Source string for regexp. */
1920 int frul_rflags; /* Flags passed to regcomp, usually
1921 * REG_EXTENDED. */
1922 regex_t* frul_re; /* Compiled regular expression
1923 obtained by regcomp. */
1924 const char* frul_tr_out; /* Transformation string for making
1925 * the output_name, with $1 ... $9 for
1926 * subpatterns and $0 for the whole
1927 * matched filename. */
1928 const char* frul_tr_for; /* Tranformation string for making the
1929 for_name. */
1930 frul_actionrout_t* frul_action; /* The action, if non null, is
1931 * called once the rule matches, on
1932 * the transformed out_name &
1933 * for_name. It could change them
1934 * and/or give the output file. */
1937 /* File rule action handling *.h files. */
1938 static outf_p header_dot_h_frul (input_file*, char**, char**);
1940 /* File rule action handling *.c files. */
1941 static outf_p source_dot_c_frul (input_file*, char**, char**);
1943 #define NULL_REGEX (regex_t*)0
1945 /* The prefix in our regexp-s matching the directory. */
1946 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1948 #define NULL_FRULACT (frul_actionrout_t*)0
1950 /* The array of our rules governing file name generation. Rules order
1951 matters, so change with extreme care! */
1953 struct file_rule_st files_rules[] = {
1954 /* The general rule assumes that files in subdirectories belong to a
1955 particular front-end, and files not in subdirectories are shared.
1956 The following rules deal with exceptions - files that are in
1957 subdirectories and yet are shared, and files that are top-level,
1958 but are not shared. */
1960 /* the c-family/ source directory is special. */
1961 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
1962 REG_EXTENDED, NULL_REGEX,
1963 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
1965 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
1966 REG_EXTENDED, NULL_REGEX,
1967 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
1969 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
1970 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
1971 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1973 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
1974 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1976 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
1977 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
1978 REG_EXTENDED, NULL_REGEX,
1979 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
1981 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
1982 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
1983 REG_EXTENDED, NULL_REGEX,
1984 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
1986 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
1987 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
1988 REG_EXTENDED, NULL_REGEX,
1989 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
1991 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
1992 { DIR_PREFIX_REGEX "cp/parser\\.h$",
1993 REG_EXTENDED, NULL_REGEX,
1994 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
1996 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
1997 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
1998 REG_EXTENDED, NULL_REGEX,
1999 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
2001 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
2002 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
2003 REG_EXTENDED, NULL_REGEX,
2004 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
2006 /* General cases. For header *.h and source *.c or *.cc files, we
2007 * need special actions to handle the language. */
2009 /* Source *.c files are using get_file_gtfilename to compute their
2010 output_name and get_file_basename to compute their for_name
2011 through the source_dot_c_frul action. */
2012 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
2013 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
2015 /* Source *.cc files are using get_file_gtfilename to compute their
2016 output_name and get_file_basename to compute their for_name
2017 through the source_dot_c_frul action. */
2018 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.cc$",
2019 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.cc", source_dot_c_frul},
2021 /* Common header files get "gtype-desc.c" as their output_name,
2022 * while language specific header files are handled specially. So
2023 * we need the header_dot_h_frul action. */
2024 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
2025 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
2027 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
2028 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
2030 /* Mandatory null last entry signaling end of rules. */
2031 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
2034 /* Special file rules action for handling *.h header files. It gives
2035 "gtype-desc.c" for common headers and corresponding output
2036 files for language-specific header files. */
2037 static outf_p
2038 header_dot_h_frul (input_file* inpf, char**poutname,
2039 char**pforname ATTRIBUTE_UNUSED)
2041 const char *basename = 0;
2042 int lang_index = 0;
2043 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2044 (void*) inpf, get_input_file_name (inpf),
2045 *poutname, *pforname);
2046 basename = get_file_basename (inpf);
2047 lang_index = get_prefix_langdir_index (basename);
2048 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
2050 if (lang_index >= 0)
2052 /* The header is language specific. Given output_name &
2053 for_name remains unchanged. The base_files array gives the
2054 outf_p. */
2055 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2056 (void*) base_files[lang_index],
2057 (base_files[lang_index])->name);
2058 return base_files[lang_index];
2060 else
2062 /* The header is common to all front-end languages. So
2063 output_name is "gtype-desc.c" file. The calling function
2064 get_output_file_with_visibility will find its outf_p. */
2065 free (*poutname);
2066 *poutname = xstrdup ("gtype-desc.c");
2067 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2068 get_input_file_name (inpf));
2069 return NULL;
2074 /* Special file rules action for handling *.c source files using
2075 * get_file_gtfilename to compute their output_name and
2076 * get_file_basename to compute their for_name. The output_name is
2077 * gt-<LANG>-<BASE>.h for language specific source files, and
2078 * gt-<BASE>.h for common source files. */
2079 static outf_p
2080 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
2082 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
2083 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
2084 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2085 (void*) inpf, get_input_file_name (inpf),
2086 *poutname, *pforname);
2087 DBGPRINTF ("newoutname %s", newoutname);
2088 DBGPRINTF ("newbasename %s", newbasename);
2089 free (*poutname);
2090 free (*pforname);
2091 *poutname = newoutname;
2092 *pforname = newbasename;
2093 return NULL;
2096 /* Utility function for get_output_file_with_visibility which returns
2097 * a malloc-ed substituted string using TRS on matching of the FILNAM
2098 * file name, using the PMATCH array. */
2099 static char*
2100 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
2101 const char *trs)
2103 struct obstack str_obstack;
2104 char *str = NULL;
2105 char *rawstr = NULL;
2106 const char *pt = NULL;
2107 DBGPRINTF ("filnam %s", filnam);
2108 obstack_init (&str_obstack);
2109 for (pt = trs; *pt; pt++) {
2110 char c = *pt;
2111 if (c == '$')
2113 if (pt[1] == '$')
2115 /* A double dollar $$ is substituted by a single verbatim
2116 dollar, but who really uses dollar signs in file
2117 paths? */
2118 obstack_1grow (&str_obstack, '$');
2120 else if (ISDIGIT (pt[1]))
2122 /* Handle $0 $1 ... $9 by appropriate substitution. */
2123 int dolnum = pt[1] - '0';
2124 int so = pmatch[dolnum].rm_so;
2125 int eo = pmatch[dolnum].rm_eo;
2126 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2127 if (so>=0 && eo>=so)
2128 obstack_grow (&str_obstack, filnam + so, eo - so);
2130 else
2132 /* This can happen only when files_rules is buggy! */
2133 gcc_unreachable ();
2135 /* Always skip the character after the dollar. */
2136 pt++;
2138 else
2139 obstack_1grow (&str_obstack, c);
2141 obstack_1grow (&str_obstack, '\0');
2142 rawstr = XOBFINISH (&str_obstack, char *);
2143 str = xstrdup (rawstr);
2144 obstack_free (&str_obstack, NULL);
2145 DBGPRINTF ("matched replacement %s", str);
2146 rawstr = NULL;
2147 return str;
2151 /* An output file, suitable for definitions, that can see declarations
2152 made in INPF and is linked into every language that uses INPF.
2153 Since the result is cached inside INPF, that argument cannot be
2154 declared constant, but is "almost" constant. */
2156 outf_p
2157 get_output_file_with_visibility (input_file *inpf)
2159 outf_p r;
2160 char *for_name = NULL;
2161 char *output_name = NULL;
2162 const char* inpfname;
2164 /* This can happen when we need a file with visibility on a
2165 structure that we've never seen. We have to just hope that it's
2166 globally visible. */
2167 if (inpf == NULL)
2168 inpf = system_h_file;
2170 /* The result is cached in INPF, so return it if already known. */
2171 if (inpf->inpoutf)
2172 return inpf->inpoutf;
2174 /* In plugin mode, return NULL unless the input_file is one of the
2175 plugin_files. */
2176 if (plugin_files)
2178 size_t i;
2179 for (i = 0; i < nb_plugin_files; i++)
2180 if (inpf == plugin_files[i])
2182 inpf->inpoutf = plugin_output;
2183 return plugin_output;
2186 return NULL;
2189 inpfname = get_input_file_name (inpf);
2191 /* Try each rule in sequence in files_rules until one is triggered. */
2193 int rulix = 0;
2194 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2195 (void*) inpf, inpfname);
2197 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2199 DBGPRINTF ("rulix#%d srcexpr %s",
2200 rulix, files_rules[rulix].frul_srcexpr);
2202 if (!files_rules[rulix].frul_re)
2204 /* Compile the regexpr lazily. */
2205 int err = 0;
2206 files_rules[rulix].frul_re = XCNEW (regex_t);
2207 err = regcomp (files_rules[rulix].frul_re,
2208 files_rules[rulix].frul_srcexpr,
2209 files_rules[rulix].frul_rflags);
2210 if (err)
2212 /* The regular expression compilation fails only when
2213 file_rules is buggy. */
2214 gcc_unreachable ();
2218 output_name = NULL;
2219 for_name = NULL;
2221 /* Match the regexpr and trigger the rule if matched. */
2223 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2224 $3, ... $9. */
2225 regmatch_t pmatch[10];
2226 memset (pmatch, 0, sizeof (pmatch));
2227 if (!regexec (files_rules[rulix].frul_re,
2228 inpfname, 10, pmatch, 0))
2230 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2231 (void*) inpf, inpfname, rulix,
2232 files_rules[rulix].frul_srcexpr);
2233 for_name =
2234 matching_file_name_substitute (inpfname, pmatch,
2235 files_rules[rulix].frul_tr_for);
2236 DBGPRINTF ("for_name %s", for_name);
2237 output_name =
2238 matching_file_name_substitute (inpfname, pmatch,
2239 files_rules[rulix].frul_tr_out);
2240 DBGPRINTF ("output_name %s", output_name);
2241 if (files_rules[rulix].frul_action)
2243 /* Invoke our action routine. */
2244 outf_p of = NULL;
2245 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2246 rulix, output_name, for_name);
2247 of =
2248 (files_rules[rulix].frul_action) (inpf,
2249 &output_name, &for_name);
2250 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2251 rulix, (void*)of, output_name, for_name);
2252 /* If the action routine returned something, give it back
2253 immediately and cache it in inpf. */
2254 if (of)
2256 inpf->inpoutf = of;
2257 return of;
2260 /* The rule matched, and had no action, or that action did
2261 not return any output file but could have changed the
2262 output_name or for_name. We break out of the loop on the
2263 files_rules. */
2264 break;
2266 else
2268 /* The regexpr did not match. */
2269 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2270 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2271 continue;
2276 if (!output_name || !for_name)
2278 /* This should not be possible, and could only happen if the
2279 files_rules is incomplete or buggy. */
2280 fatal ("failed to compute output name for %s", inpfname);
2283 /* Look through to see if we've ever seen this output filename
2284 before. If found, cache the result in inpf. */
2285 for (r = output_files; r; r = r->next)
2286 if (filename_cmp (r->name, output_name) == 0)
2288 inpf->inpoutf = r;
2289 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2290 output_name, for_name);
2291 return r;
2294 /* If not found, create it, and cache it in inpf. */
2295 r = create_file (for_name, output_name);
2297 gcc_assert (r && r->name);
2298 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2299 output_name, for_name);
2300 inpf->inpoutf = r;
2301 return r;
2306 /* The name of an output file, suitable for definitions, that can see
2307 declarations made in INPF and is linked into every language that
2308 uses INPF. */
2310 const char *
2311 get_output_file_name (input_file* inpf)
2313 outf_p o = get_output_file_with_visibility (inpf);
2314 if (o)
2315 return o->name;
2316 return NULL;
2319 /* Check if existing file is equal to the in memory buffer. */
2321 static bool
2322 is_file_equal (outf_p of)
2324 FILE *newfile = fopen (of->name, "r");
2325 size_t i;
2326 bool equal;
2327 if (newfile == NULL)
2328 return false;
2330 equal = true;
2331 for (i = 0; i < of->bufused; i++)
2333 int ch;
2334 ch = fgetc (newfile);
2335 if (ch == EOF || ch != (unsigned char) of->buf[i])
2337 equal = false;
2338 break;
2341 fclose (newfile);
2342 return equal;
2345 /* Copy the output to its final destination,
2346 but don't unnecessarily change modification times. */
2348 static void
2349 close_output_files (void)
2351 int nbwrittenfiles = 0;
2352 outf_p of;
2354 for (of = output_files; of; of = of->next)
2356 if (!is_file_equal (of))
2358 FILE *newfile = NULL;
2359 char *backupname = NULL;
2360 /* Back up the old version of the output file gt-FOO.c as
2361 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2362 if (backup_dir)
2364 backupname = concat (backup_dir, "/",
2365 lbasename (of->name), "~", NULL);
2366 if (!access (of->name, F_OK) && rename (of->name, backupname))
2367 fatal ("failed to back up %s as %s: %s",
2368 of->name, backupname, xstrerror (errno));
2371 newfile = fopen (of->name, "w");
2372 if (newfile == NULL)
2373 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2374 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2375 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2376 if (fclose (newfile) != 0)
2377 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2378 nbwrittenfiles++;
2379 if (verbosity_level >= 2 && backupname)
2380 printf ("%s wrote #%-3d %s backed-up in %s\n",
2381 progname, nbwrittenfiles, of->name, backupname);
2382 else if (verbosity_level >= 1)
2383 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2384 free (backupname);
2386 else
2388 /* output file remains unchanged. */
2389 if (verbosity_level >= 2)
2390 printf ("%s keep %s\n", progname, of->name);
2392 free (of->buf);
2393 of->buf = NULL;
2394 of->bufused = of->buflength = 0;
2396 if (verbosity_level >= 1)
2397 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2400 struct flist
2402 struct flist *next;
2403 int started_p;
2404 const input_file* file;
2405 outf_p f;
2408 struct walk_type_data;
2410 /* For scalars and strings, given the item in 'val'.
2411 For structures, given a pointer to the item in 'val'.
2412 For misc. pointers, given the item in 'val'.
2414 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2415 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2417 /* Parameters for write_types. */
2419 struct write_types_data
2421 const char *prefix;
2422 const char *param_prefix;
2423 const char *subfield_marker_routine;
2424 const char *marker_routine;
2425 const char *reorder_note_routine;
2426 const char *comment;
2427 int skip_hooks; /* skip hook generation if non zero */
2430 static void output_escaped_param (struct walk_type_data *d,
2431 const char *, const char *);
2432 static void output_mangled_typename (outf_p, const_type_p);
2433 static void walk_type (type_p t, struct walk_type_data *d);
2434 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2435 const struct write_types_data *wtd);
2436 static void write_types_process_field
2437 (type_p f, const struct walk_type_data *d);
2438 static void write_types (outf_p output_header,
2439 type_p structures,
2440 type_p param_structs,
2441 const struct write_types_data *wtd);
2442 static void write_types_local_process_field
2443 (type_p f, const struct walk_type_data *d);
2444 static void write_local_func_for_structure
2445 (const_type_p orig_s, type_p s, type_p *param);
2446 static void write_local (outf_p output_header,
2447 type_p structures, type_p param_structs);
2448 static int contains_scalar_p (type_p t);
2449 static void put_mangled_filename (outf_p, const input_file *);
2450 static void finish_root_table (struct flist *flp, const char *pfx,
2451 const char *tname, const char *lastname,
2452 const char *name);
2453 static void write_root (outf_p, pair_p, type_p, const char *, int,
2454 struct fileloc *, const char *, bool);
2455 static void write_array (outf_p f, pair_p v,
2456 const struct write_types_data *wtd);
2457 static void write_roots (pair_p, bool);
2459 /* Parameters for walk_type. */
2461 struct walk_type_data
2463 process_field_fn process_field;
2464 const void *cookie;
2465 outf_p of;
2466 options_p opt;
2467 const char *val;
2468 const char *prev_val[4];
2469 int indent;
2470 int counter;
2471 const struct fileloc *line;
2472 lang_bitmap bitmap;
2473 type_p *param;
2474 int used_length;
2475 type_p orig_s;
2476 const char *reorder_fn;
2477 bool needs_cast_p;
2478 bool fn_wants_lvalue;
2479 bool in_record_p;
2480 int loopcounter;
2481 bool in_ptr_field;
2482 bool have_this_obj;
2486 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2487 pre-processor identifier to use in a #define directive. This replaces
2488 special characters used in C++ identifiers like '>', '<' and ':' with
2489 '_'.
2491 If no C++ special characters are found in TYPE_NAME, return
2492 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2493 characters replaced with '_'. In this case, the caller is
2494 responsible for freeing the allocated string. */
2496 static const char *
2497 filter_type_name (const char *type_name)
2499 if (strchr (type_name, '<') || strchr (type_name, ':'))
2501 size_t i;
2502 char *s = xstrdup (type_name);
2503 for (i = 0; i < strlen (s); i++)
2504 if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ',')
2505 s[i] = '_';
2506 return s;
2508 else
2509 return type_name;
2513 /* Print a mangled name representing T to OF. */
2515 static void
2516 output_mangled_typename (outf_p of, const_type_p t)
2518 if (t == NULL)
2519 oprintf (of, "Z");
2520 else
2521 switch (t->kind)
2523 case TYPE_NONE:
2524 case TYPE_UNDEFINED:
2525 gcc_unreachable ();
2526 break;
2527 case TYPE_POINTER:
2528 oprintf (of, "P");
2529 output_mangled_typename (of, t->u.p);
2530 break;
2531 case TYPE_SCALAR:
2532 oprintf (of, "I");
2533 break;
2534 case TYPE_STRING:
2535 oprintf (of, "S");
2536 break;
2537 case TYPE_STRUCT:
2538 case TYPE_UNION:
2539 case TYPE_LANG_STRUCT:
2540 case TYPE_USER_STRUCT:
2542 const char *id_for_tag = filter_type_name (t->u.s.tag);
2543 oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2544 id_for_tag);
2545 if (id_for_tag != t->u.s.tag)
2546 free (CONST_CAST (char *, id_for_tag));
2548 break;
2549 case TYPE_PARAM_STRUCT:
2551 int i;
2552 for (i = 0; i < NUM_PARAM; i++)
2553 if (t->u.param_struct.param[i] != NULL)
2554 output_mangled_typename (of, t->u.param_struct.param[i]);
2555 output_mangled_typename (of, t->u.param_struct.stru);
2557 break;
2558 case TYPE_ARRAY:
2559 gcc_unreachable ();
2563 /* Print PARAM to D->OF processing escapes. D->VAL references the
2564 current object, D->PREV_VAL the object containing the current
2565 object, ONAME is the name of the option and D->LINE is used to
2566 print error messages. */
2568 static void
2569 output_escaped_param (struct walk_type_data *d, const char *param,
2570 const char *oname)
2572 const char *p;
2574 for (p = param; *p; p++)
2575 if (*p != '%')
2576 oprintf (d->of, "%c", *p);
2577 else
2578 switch (*++p)
2580 case 'h':
2581 oprintf (d->of, "(%s)", d->prev_val[2]);
2582 break;
2583 case '0':
2584 oprintf (d->of, "(%s)", d->prev_val[0]);
2585 break;
2586 case '1':
2587 oprintf (d->of, "(%s)", d->prev_val[1]);
2588 break;
2589 case 'a':
2591 const char *pp = d->val + strlen (d->val);
2592 while (pp[-1] == ']')
2593 while (*pp != '[')
2594 pp--;
2595 oprintf (d->of, "%s", pp);
2597 break;
2598 default:
2599 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2600 oname, '%', *p);
2605 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2606 which is of type T. Write code to D->OF to constrain execution (at
2607 the point that D->PROCESS_FIELD is called) to the appropriate
2608 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2609 pointers to those objects. D->PREV_VAL lists the objects
2610 containing the current object, D->OPT is a list of options to
2611 apply, D->INDENT is the current indentation level, D->LINE is used
2612 to print error messages, D->BITMAP indicates which languages to
2613 print the structure for, and D->PARAM is the current parameter
2614 (from an enclosing param_is option). */
2616 static void
2617 walk_type (type_p t, struct walk_type_data *d)
2619 const char *length = NULL;
2620 const char *desc = NULL;
2621 int maybe_undef_p = 0;
2622 int use_param_num = -1;
2623 int use_params_p = 0;
2624 int atomic_p = 0;
2625 options_p oo;
2626 const struct nested_ptr_data *nested_ptr_d = NULL;
2628 d->needs_cast_p = false;
2629 for (oo = d->opt; oo; oo = oo->next)
2630 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2631 length = oo->info.string;
2632 else if (strcmp (oo->name, "maybe_undef") == 0)
2633 maybe_undef_p = 1;
2634 else if (strncmp (oo->name, "use_param", 9) == 0
2635 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2636 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2637 else if (strcmp (oo->name, "use_params") == 0)
2638 use_params_p = 1;
2639 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2640 desc = oo->info.string;
2641 else if (strcmp (oo->name, "mark_hook") == 0)
2643 else if (strcmp (oo->name, "nested_ptr") == 0
2644 && oo->kind == OPTION_NESTED)
2645 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2646 else if (strcmp (oo->name, "dot") == 0)
2648 else if (strcmp (oo->name, "tag") == 0)
2650 else if (strcmp (oo->name, "special") == 0)
2652 else if (strcmp (oo->name, "skip") == 0)
2654 else if (strcmp (oo->name, "atomic") == 0)
2655 atomic_p = 1;
2656 else if (strcmp (oo->name, "default") == 0)
2658 else if (strcmp (oo->name, "param_is") == 0)
2660 else if (strncmp (oo->name, "param", 5) == 0
2661 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2663 else if (strcmp (oo->name, "chain_next") == 0)
2665 else if (strcmp (oo->name, "chain_prev") == 0)
2667 else if (strcmp (oo->name, "chain_circular") == 0)
2669 else if (strcmp (oo->name, "reorder") == 0)
2671 else if (strcmp (oo->name, "variable_size") == 0)
2673 else
2674 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2676 if (d->used_length)
2677 length = NULL;
2679 if (use_params_p)
2681 int pointer_p = t->kind == TYPE_POINTER;
2683 if (pointer_p)
2684 t = t->u.p;
2685 if (!union_or_struct_p (t))
2686 error_at_line (d->line, "`use_params' option on unimplemented type");
2687 else
2688 t = find_param_structure (t, d->param);
2689 if (pointer_p)
2690 t = create_pointer (t);
2693 if (use_param_num != -1)
2695 if (d->param != NULL && d->param[use_param_num] != NULL)
2697 type_p nt = d->param[use_param_num];
2699 if (t->kind == TYPE_ARRAY)
2700 nt = create_array (nt, t->u.a.len);
2701 else if (length != NULL && t->kind == TYPE_POINTER)
2702 nt = create_pointer (nt);
2703 d->needs_cast_p = (t->kind != TYPE_POINTER
2704 && (nt->kind == TYPE_POINTER
2705 || nt->kind == TYPE_STRING));
2706 t = nt;
2708 else
2709 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2712 if (maybe_undef_p
2713 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
2715 error_at_line (d->line,
2716 "field `%s' has invalid option `maybe_undef_p'\n",
2717 d->val);
2718 return;
2721 if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2723 error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2724 return;
2727 switch (t->kind)
2729 case TYPE_SCALAR:
2730 case TYPE_STRING:
2731 d->process_field (t, d);
2732 break;
2734 case TYPE_POINTER:
2736 d->in_ptr_field = true;
2737 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2739 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2740 break;
2743 /* If a pointer type is marked as "atomic", we process the
2744 field itself, but we don't walk the data that they point to.
2746 There are two main cases where we walk types: to mark
2747 pointers that are reachable, and to relocate pointers when
2748 writing a PCH file. In both cases, an atomic pointer is
2749 itself marked or relocated, but the memory that it points
2750 to is left untouched. In the case of PCH, that memory will
2751 be read/written unchanged to the PCH file. */
2752 if (atomic_p)
2754 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2755 d->indent += 2;
2756 d->process_field (t, d);
2757 d->indent -= 2;
2758 oprintf (d->of, "%*s}\n", d->indent, "");
2759 break;
2762 if (!length)
2764 if (!union_or_struct_p (t->u.p)
2765 && t->u.p->kind != TYPE_PARAM_STRUCT)
2767 error_at_line (d->line,
2768 "field `%s' is pointer to unimplemented type",
2769 d->val);
2770 break;
2773 if (nested_ptr_d)
2775 const char *oldprevval2 = d->prev_val[2];
2777 if (!union_or_struct_p (nested_ptr_d->type))
2779 error_at_line (d->line,
2780 "field `%s' has invalid "
2781 "option `nested_ptr'\n", d->val);
2782 return;
2785 d->prev_val[2] = d->val;
2786 oprintf (d->of, "%*s{\n", d->indent, "");
2787 d->indent += 2;
2788 d->val = xasprintf ("x%d", d->counter++);
2789 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2790 (nested_ptr_d->type->kind == TYPE_UNION
2791 ? "union" : "struct"),
2792 nested_ptr_d->type->u.s.tag,
2793 d->fn_wants_lvalue ? "" : "const ", d->val);
2794 oprintf (d->of, "%*s", d->indent + 2, "");
2795 output_escaped_param (d, nested_ptr_d->convert_from,
2796 "nested_ptr");
2797 oprintf (d->of, ";\n");
2799 d->process_field (nested_ptr_d->type, d);
2801 if (d->fn_wants_lvalue)
2803 oprintf (d->of, "%*s%s = ", d->indent, "",
2804 d->prev_val[2]);
2805 d->prev_val[2] = d->val;
2806 output_escaped_param (d, nested_ptr_d->convert_to,
2807 "nested_ptr");
2808 oprintf (d->of, ";\n");
2811 d->indent -= 2;
2812 oprintf (d->of, "%*s}\n", d->indent, "");
2813 d->val = d->prev_val[2];
2814 d->prev_val[2] = oldprevval2;
2816 else
2817 d->process_field (t->u.p, d);
2819 else
2821 int loopcounter = d->loopcounter;
2822 const char *oldval = d->val;
2823 const char *oldprevval3 = d->prev_val[3];
2824 char *newval;
2826 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2827 d->indent += 2;
2828 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2829 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2830 "", loopcounter, loopcounter);
2831 if (!d->in_record_p)
2832 output_escaped_param (d, length, "length");
2833 else
2834 oprintf (d->of, "l%d", loopcounter);
2835 if (d->have_this_obj)
2836 /* Try to unswitch loops (see PR53880). */
2837 oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
2838 oprintf (d->of, "); i%d++) {\n", loopcounter);
2839 d->indent += 2;
2840 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2841 d->used_length = 1;
2842 d->prev_val[3] = oldval;
2843 walk_type (t->u.p, d);
2844 free (newval);
2845 d->val = oldval;
2846 d->prev_val[3] = oldprevval3;
2847 d->used_length = 0;
2848 d->indent -= 2;
2849 oprintf (d->of, "%*s}\n", d->indent, "");
2850 d->process_field (t, d);
2851 d->indent -= 2;
2852 oprintf (d->of, "%*s}\n", d->indent, "");
2854 d->in_ptr_field = false;
2856 break;
2858 case TYPE_ARRAY:
2860 int loopcounter;
2861 const char *oldval = d->val;
2862 char *newval;
2864 /* If it's an array of scalars, we optimize by not generating
2865 any code. */
2866 if (t->u.a.p->kind == TYPE_SCALAR)
2867 break;
2869 if (length)
2870 loopcounter = d->loopcounter;
2871 else
2872 loopcounter = d->counter++;
2874 /* When walking an array, compute the length and store it in a
2875 local variable before walking the array elements, instead of
2876 recomputing the length expression each time through the loop.
2877 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2878 where the length is stored in the first array element,
2879 because otherwise that operand can get overwritten on the
2880 first iteration. */
2881 oprintf (d->of, "%*s{\n", d->indent, "");
2882 d->indent += 2;
2883 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2884 if (!d->in_record_p || !length)
2886 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2887 d->indent, "", loopcounter);
2888 if (length)
2889 output_escaped_param (d, length, "length");
2890 else
2891 oprintf (d->of, "%s", t->u.a.len);
2892 oprintf (d->of, ");\n");
2895 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2896 d->indent, "",
2897 loopcounter, loopcounter, loopcounter, loopcounter);
2898 d->indent += 2;
2899 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2900 d->used_length = 1;
2901 walk_type (t->u.a.p, d);
2902 free (newval);
2903 d->used_length = 0;
2904 d->val = oldval;
2905 d->indent -= 2;
2906 oprintf (d->of, "%*s}\n", d->indent, "");
2907 d->indent -= 2;
2908 oprintf (d->of, "%*s}\n", d->indent, "");
2910 break;
2912 case TYPE_STRUCT:
2913 case TYPE_UNION:
2915 pair_p f;
2916 const char *oldval = d->val;
2917 const char *oldprevval1 = d->prev_val[1];
2918 const char *oldprevval2 = d->prev_val[2];
2919 const char *struct_mark_hook = NULL;
2920 const int union_p = t->kind == TYPE_UNION;
2921 int seen_default_p = 0;
2922 options_p o;
2923 int lengths_seen = 0;
2924 int endcounter;
2925 bool any_length_seen = false;
2927 if (!t->u.s.line.file)
2928 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
2930 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2932 error_at_line (d->line,
2933 "structure `%s' defined for mismatching languages",
2934 t->u.s.tag);
2935 error_at_line (&t->u.s.line, "one structure defined here");
2938 /* Some things may also be defined in the structure's options. */
2939 for (o = t->u.s.opt; o; o = o->next)
2940 if (!desc && strcmp (o->name, "desc") == 0
2941 && o->kind == OPTION_STRING)
2942 desc = o->info.string;
2943 else if (!struct_mark_hook && strcmp (o->name, "mark_hook") == 0
2944 && o->kind == OPTION_STRING)
2945 struct_mark_hook = o->info.string;
2947 if (struct_mark_hook)
2948 oprintf (d->of, "%*s%s (&%s);\n",
2949 d->indent, "", struct_mark_hook, oldval);
2951 d->prev_val[2] = oldval;
2952 d->prev_val[1] = oldprevval2;
2953 if (union_p)
2955 if (desc == NULL)
2957 error_at_line (d->line,
2958 "missing `desc' option for union `%s'",
2959 t->u.s.tag);
2960 desc = "1";
2962 oprintf (d->of, "%*sswitch (", d->indent, "");
2963 output_escaped_param (d, desc, "desc");
2964 oprintf (d->of, ")\n");
2965 d->indent += 2;
2966 oprintf (d->of, "%*s{\n", d->indent, "");
2969 for (f = t->u.s.fields; f; f = f->next)
2971 options_p oo;
2972 int skip_p = 0;
2973 const char *fieldlength = NULL;
2975 d->reorder_fn = NULL;
2976 for (oo = f->opt; oo; oo = oo->next)
2977 if (strcmp (oo->name, "skip") == 0)
2978 skip_p = 1;
2979 else if (strcmp (oo->name, "length") == 0
2980 && oo->kind == OPTION_STRING)
2981 fieldlength = oo->info.string;
2983 if (skip_p)
2984 continue;
2985 if (fieldlength)
2987 lengths_seen++;
2988 d->counter++;
2989 if (!union_p)
2991 if (!any_length_seen)
2993 oprintf (d->of, "%*s{\n", d->indent, "");
2994 d->indent += 2;
2996 any_length_seen = true;
2998 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2999 d->indent, "", d->counter - 1);
3000 output_escaped_param (d, fieldlength, "length");
3001 oprintf (d->of, ");\n");
3005 endcounter = d->counter;
3007 for (f = t->u.s.fields; f; f = f->next)
3009 options_p oo;
3010 const char *dot = ".";
3011 const char *tagid = NULL;
3012 int skip_p = 0;
3013 int default_p = 0;
3014 int use_param_p = 0;
3015 const char *fieldlength = NULL;
3016 char *newval;
3018 d->reorder_fn = NULL;
3019 for (oo = f->opt; oo; oo = oo->next)
3020 if (strcmp (oo->name, "dot") == 0
3021 && oo->kind == OPTION_STRING)
3022 dot = oo->info.string;
3023 else if (strcmp (oo->name, "tag") == 0
3024 && oo->kind == OPTION_STRING)
3025 tagid = oo->info.string;
3026 else if (strcmp (oo->name, "skip") == 0)
3027 skip_p = 1;
3028 else if (strcmp (oo->name, "default") == 0)
3029 default_p = 1;
3030 else if (strcmp (oo->name, "reorder") == 0
3031 && oo->kind == OPTION_STRING)
3032 d->reorder_fn = oo->info.string;
3033 else if (strncmp (oo->name, "use_param", 9) == 0
3034 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
3035 use_param_p = 1;
3036 else if (strcmp (oo->name, "length") == 0
3037 && oo->kind == OPTION_STRING)
3038 fieldlength = oo->info.string;
3040 if (skip_p)
3041 continue;
3043 if (union_p && tagid)
3045 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
3046 d->indent += 2;
3048 else if (union_p && default_p)
3050 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3051 d->indent += 2;
3052 seen_default_p = 1;
3054 else if (!union_p && (default_p || tagid))
3055 error_at_line (d->line,
3056 "can't use `%s' outside a union on field `%s'",
3057 default_p ? "default" : "tag", f->name);
3058 else if (union_p && !(default_p || tagid)
3059 && f->type->kind == TYPE_SCALAR)
3061 fprintf (stderr,
3062 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3063 get_input_file_name (d->line->file), d->line->line,
3064 f->name);
3065 continue;
3067 else if (union_p && !(default_p || tagid))
3068 error_at_line (d->line,
3069 "field `%s' is missing `tag' or `default' option",
3070 f->name);
3072 if (fieldlength)
3074 d->loopcounter = endcounter - lengths_seen--;
3077 d->line = &f->line;
3078 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
3079 d->opt = f->opt;
3080 d->used_length = false;
3081 d->in_record_p = !union_p;
3083 if (union_p && use_param_p && d->param == NULL)
3084 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
3085 else
3086 walk_type (f->type, d);
3088 d->in_record_p = false;
3090 free (newval);
3092 if (union_p)
3094 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3095 d->indent -= 2;
3098 d->reorder_fn = NULL;
3100 d->val = oldval;
3101 d->prev_val[1] = oldprevval1;
3102 d->prev_val[2] = oldprevval2;
3104 if (union_p && !seen_default_p)
3106 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3107 oprintf (d->of, "%*s break;\n", d->indent, "");
3109 if (union_p)
3111 oprintf (d->of, "%*s}\n", d->indent, "");
3112 d->indent -= 2;
3114 if (any_length_seen)
3116 d->indent -= 2;
3117 oprintf (d->of, "%*s}\n", d->indent, "");
3120 break;
3122 case TYPE_LANG_STRUCT:
3124 type_p nt;
3125 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3126 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3127 break;
3128 if (nt == NULL)
3129 error_at_line (d->line, "structure `%s' differs between languages",
3130 t->u.s.tag);
3131 else
3132 walk_type (nt, d);
3134 break;
3136 case TYPE_PARAM_STRUCT:
3138 type_p *oldparam = d->param;
3140 d->param = t->u.param_struct.param;
3141 walk_type (t->u.param_struct.stru, d);
3142 d->param = oldparam;
3144 break;
3146 case TYPE_USER_STRUCT:
3147 d->process_field (t, d);
3148 break;
3150 case TYPE_NONE:
3151 case TYPE_UNDEFINED:
3152 gcc_unreachable ();
3156 /* process_field routine for marking routines. */
3158 static void
3159 write_types_process_field (type_p f, const struct walk_type_data *d)
3161 const struct write_types_data *wtd;
3162 const char *cast = d->needs_cast_p ? "(void *)" : "";
3163 wtd = (const struct write_types_data *) d->cookie;
3165 switch (f->kind)
3167 case TYPE_NONE:
3168 case TYPE_UNDEFINED:
3169 gcc_unreachable ();
3170 case TYPE_POINTER:
3171 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
3172 wtd->subfield_marker_routine, cast, d->val);
3173 if (wtd->param_prefix)
3175 if (f->u.p->kind == TYPE_SCALAR)
3176 /* The current type is a pointer to a scalar (so not
3177 considered like a pointer to instances of user defined
3178 types) and we are seeing it; it means we must be even
3179 more careful about the second argument of the
3180 SUBFIELD_MARKER_ROUTINE call. That argument must
3181 always be the instance of the type for which
3182 write_func_for_structure was called - this really is
3183 what the function SUBFIELD_MARKER_ROUTINE expects.
3184 That is, it must be an instance of the ORIG_S type
3185 parameter of write_func_for_structure. The convention
3186 is that that argument must be "x" in that case (as set
3187 by write_func_for_structure). The problem is, we can't
3188 count on d->prev_val[3] to be always set to "x" in that
3189 case. Sometimes walk_type can set it to something else
3190 (to e.g cooperate with write_array when called from
3191 write_roots). So let's set it to "x" here then. */
3192 oprintf (d->of, ", x");
3193 else
3194 oprintf (d->of, ", %s", d->prev_val[3]);
3195 if (d->orig_s)
3197 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3198 output_mangled_typename (d->of, d->orig_s);
3200 else
3201 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3203 oprintf (d->of, ");\n");
3204 if (d->reorder_fn && wtd->reorder_note_routine)
3205 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
3206 wtd->reorder_note_routine, cast, d->val,
3207 d->prev_val[3], d->reorder_fn);
3208 break;
3210 case TYPE_STRING:
3211 case TYPE_STRUCT:
3212 case TYPE_UNION:
3213 case TYPE_LANG_STRUCT:
3214 case TYPE_PARAM_STRUCT:
3215 case TYPE_USER_STRUCT:
3216 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3218 /* If F is a user-defined type and the field is not a
3219 pointer to the type, then we should not generate the
3220 standard pointer-marking code. All we need to do is call
3221 the user-provided marking function to process the fields
3222 of F. */
3223 oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3224 d->val);
3226 else
3228 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3229 output_mangled_typename (d->of, f);
3230 oprintf (d->of, " (%s%s);\n", cast, d->val);
3231 if (d->reorder_fn && wtd->reorder_note_routine)
3232 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
3233 wtd->reorder_note_routine, cast, d->val, cast, d->val,
3234 d->reorder_fn);
3236 break;
3238 case TYPE_SCALAR:
3239 break;
3241 case TYPE_ARRAY:
3242 gcc_unreachable ();
3246 /* Return an output file that is suitable for definitions which can
3247 reference struct S */
3249 static outf_p
3250 get_output_file_for_structure (const_type_p s, type_p *param)
3252 const input_file *fn;
3253 int i;
3255 gcc_assert (union_or_struct_p (s));
3256 fn = s->u.s.line.file;
3258 /* This is a hack, and not the good kind either. */
3259 for (i = NUM_PARAM - 1; i >= 0; i--)
3260 if (param && param[i] && param[i]->kind == TYPE_POINTER
3261 && union_or_struct_p (param[i]->u.p))
3262 fn = param[i]->u.p->u.s.line.file;
3264 /* The call to get_output_file_with_visibility may update fn by
3265 caching its result inside, so we need the CONST_CAST. */
3266 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3270 /* Returns the specifier keyword for a string or union type S, empty string
3271 otherwise. */
3273 static const char *
3274 get_type_specifier (const type_p s)
3276 if (s->kind == TYPE_STRUCT)
3277 return "struct ";
3278 else if (s->kind == TYPE_LANG_STRUCT)
3279 return get_type_specifier (s->u.s.lang_struct);
3280 else if (s->kind == TYPE_UNION)
3281 return "union ";
3282 return "";
3286 /* Emits a declaration for type TY (assumed to be a union or a
3287 structure) on stream OUT. */
3289 static void
3290 write_type_decl (outf_p out, type_p ty)
3292 if (union_or_struct_p (ty))
3293 oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3294 else if (ty->kind == TYPE_SCALAR)
3296 if (ty->u.scalar_is_char)
3297 oprintf (out, "const char");
3298 else
3299 oprintf (out, "void");
3301 else if (ty->kind == TYPE_POINTER)
3303 write_type_decl (out, ty->u.p);
3304 oprintf (out, " *");
3306 else if (ty->kind == TYPE_ARRAY)
3308 write_type_decl (out, ty->u.a.p);
3309 oprintf (out, " *");
3311 else if (ty->kind == TYPE_STRING)
3313 oprintf (out, "const char *");
3315 else
3316 gcc_unreachable ();
3320 /* Write on OF the name of the marker function for structure S. PREFIX
3321 is the prefix to use (to distinguish ggc from pch markers). */
3323 static void
3324 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3326 if (union_or_struct_p (s))
3328 const char *id_for_tag = filter_type_name (s->u.s.tag);
3329 oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3330 if (id_for_tag != s->u.s.tag)
3331 free (CONST_CAST (char *, id_for_tag));
3333 else if (s->kind == TYPE_PARAM_STRUCT)
3335 oprintf (of, "gt_%s_", prefix);
3336 output_mangled_typename (of, s);
3338 else
3339 gcc_unreachable ();
3342 /* Write on OF a user-callable routine to act as an entry point for
3343 the marking routine for S, generated by write_func_for_structure.
3344 PREFIX is the prefix to use to distinguish ggc and pch markers. */
3346 static void
3347 write_user_func_for_structure_ptr (outf_p of, type_p s, const char *prefix)
3349 /* Parameterized structures are not supported in user markers. There
3350 is no way for the marker function to know which specific type
3351 to use to generate the call to the void * entry point. For
3352 instance, a marker for struct htab may need to call different
3353 routines to mark the fields, depending on the paramN_is attributes.
3355 A user-defined marker that accepts 'struct htab' as its argument
3356 would not know which variant to call. Generating several entry
3357 points accepting 'struct htab' would cause multiply-defined
3358 errors during compilation. */
3359 gcc_assert (union_or_struct_p (s));
3361 type_p alias_of = NULL;
3362 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3363 if (strcmp (opt->name, "ptr_alias") == 0)
3365 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3366 we do not generate marking code for ORIG_S here. Instead, a
3367 forwarder #define in gtype-desc.h will cause every call to its
3368 marker to call the target of this alias.
3370 However, we still want to create a user entry code for the
3371 aliased type. So, if ALIAS_OF is set, we only generate the
3372 user-callable marker function. */
3373 alias_of = opt->info.type;
3374 break;
3377 oprintf (of, "\nvoid\n");
3378 oprintf (of, "gt_%sx (", prefix);
3379 write_type_decl (of, s);
3380 oprintf (of, " *& x)\n");
3381 oprintf (of, "{\n");
3382 oprintf (of, " if (x)\n ");
3383 write_marker_function_name (of, alias_of ? alias_of : s, prefix);
3384 oprintf (of, " ((void *) x);\n");
3385 oprintf (of, "}\n");
3389 /* Write a function to mark all the fields of type S on OF. PREFIX
3390 and D are as in write_user_marking_functions. */
3392 static void
3393 write_user_func_for_structure_body (type_p s, const char *prefix,
3394 struct walk_type_data *d)
3396 oprintf (d->of, "\nvoid\n");
3397 oprintf (d->of, "gt_%sx (", prefix);
3398 write_type_decl (d->of, s);
3399 oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3400 oprintf (d->of, "{\n");
3401 oprintf (d->of, " ");
3402 write_type_decl (d->of, s);
3403 oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3404 d->val = "(*x)";
3405 d->indent = 2;
3406 walk_type (s, d);
3407 oprintf (d->of, "}\n");
3411 /* Emit the user-callable functions needed to mark all the types used
3412 by the user structure S. PREFIX is the prefix to use to
3413 distinguish ggc and pch markers. D contains data needed to pass to
3414 walk_type when traversing the fields of a type.
3416 For every type T referenced by S, two routines are generated: one
3417 that takes 'T *', marks the pointer and calls the second routine,
3418 which just marks the fields of T. */
3420 static void
3421 write_user_marking_functions (type_p s, const char *prefix,
3422 struct walk_type_data *d)
3424 gcc_assert (s->kind == TYPE_USER_STRUCT);
3426 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3428 type_p fld_type = fld->type;
3429 if (fld_type->kind == TYPE_POINTER)
3431 type_p pointed_to_type = fld_type->u.p;
3432 if (union_or_struct_p (pointed_to_type))
3433 write_user_func_for_structure_ptr (d->of, pointed_to_type, prefix);
3435 else if (union_or_struct_p (fld_type))
3436 write_user_func_for_structure_body (fld_type, prefix, d);
3441 /* For S, a structure that's part of ORIG_S, and using parameters
3442 PARAM, write out a routine that:
3443 - Takes a parameter, a void * but actually of type *S
3444 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3445 field of S or its substructures and (in some cases) things
3446 that are pointed to by S. */
3448 static void
3449 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
3450 const struct write_types_data *wtd)
3452 const char *chain_next = NULL;
3453 const char *chain_prev = NULL;
3454 const char *chain_circular = NULL;
3455 const char *mark_hook_name = NULL;
3456 options_p opt;
3457 struct walk_type_data d;
3459 memset (&d, 0, sizeof (d));
3460 d.of = get_output_file_for_structure (s, param);
3461 for (opt = s->u.s.opt; opt; opt = opt->next)
3462 if (strcmp (opt->name, "chain_next") == 0
3463 && opt->kind == OPTION_STRING)
3464 chain_next = opt->info.string;
3465 else if (strcmp (opt->name, "chain_prev") == 0
3466 && opt->kind == OPTION_STRING)
3467 chain_prev = opt->info.string;
3468 else if (strcmp (opt->name, "chain_circular") == 0
3469 && opt->kind == OPTION_STRING)
3470 chain_circular = opt->info.string;
3471 else if (strcmp (opt->name, "mark_hook") == 0
3472 && opt->kind == OPTION_STRING)
3473 mark_hook_name = opt->info.string;
3474 if (chain_prev != NULL && chain_next == NULL)
3475 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3476 if (chain_circular != NULL && chain_next != NULL)
3477 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3478 if (chain_circular != NULL)
3479 chain_next = chain_circular;
3481 d.process_field = write_types_process_field;
3482 d.cookie = wtd;
3483 d.orig_s = orig_s;
3484 d.opt = s->u.s.opt;
3485 d.line = &s->u.s.line;
3486 d.bitmap = s->u.s.bitmap;
3487 d.param = param;
3488 d.prev_val[0] = "*x";
3489 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3490 d.prev_val[3] = "x";
3491 d.val = "(*x)";
3492 d.have_this_obj = false;
3494 oprintf (d.of, "\n");
3495 oprintf (d.of, "void\n");
3496 write_marker_function_name (d.of, orig_s, wtd->prefix);
3497 oprintf (d.of, " (void *x_p)\n");
3498 oprintf (d.of, "{\n ");
3499 write_type_decl (d.of, s);
3500 oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3501 write_type_decl (d.of, s);
3502 oprintf (d.of, " *)x_p;\n");
3503 if (chain_next != NULL)
3505 /* TYPE_USER_STRUCTs should not occur here. These structures
3506 are completely handled by user code. */
3507 gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
3509 oprintf (d.of, " ");
3510 write_type_decl (d.of, s);
3511 oprintf (d.of, " * xlimit = x;\n");
3513 if (chain_next == NULL)
3515 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3516 if (wtd->param_prefix)
3518 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3519 output_mangled_typename (d.of, orig_s);
3521 oprintf (d.of, "))\n");
3523 else
3525 if (chain_circular != NULL)
3526 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3527 else
3528 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3529 if (wtd->param_prefix)
3531 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3532 output_mangled_typename (d.of, orig_s);
3534 oprintf (d.of, "))\n");
3535 if (chain_circular != NULL)
3536 oprintf (d.of, " return;\n do\n");
3537 if (mark_hook_name && !wtd->skip_hooks)
3539 oprintf (d.of, " {\n");
3540 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
3542 oprintf (d.of, " xlimit = (");
3543 d.prev_val[2] = "*xlimit";
3544 output_escaped_param (&d, chain_next, "chain_next");
3545 oprintf (d.of, ");\n");
3546 if (mark_hook_name && !wtd->skip_hooks)
3547 oprintf (d.of, " }\n");
3548 if (chain_prev != NULL)
3550 oprintf (d.of, " if (x != xlimit)\n");
3551 oprintf (d.of, " for (;;)\n");
3552 oprintf (d.of, " {\n");
3553 oprintf (d.of, " %s %s * const xprev = (",
3554 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3556 d.prev_val[2] = "*x";
3557 output_escaped_param (&d, chain_prev, "chain_prev");
3558 oprintf (d.of, ");\n");
3559 oprintf (d.of, " if (xprev == NULL) break;\n");
3560 oprintf (d.of, " x = xprev;\n");
3561 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3562 if (wtd->param_prefix)
3564 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3565 output_mangled_typename (d.of, orig_s);
3567 oprintf (d.of, ");\n");
3568 oprintf (d.of, " }\n");
3570 if (chain_circular != NULL)
3572 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3573 if (wtd->param_prefix)
3575 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3576 output_mangled_typename (d.of, orig_s);
3578 oprintf (d.of, "));\n");
3579 if (mark_hook_name && !wtd->skip_hooks)
3580 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
3581 oprintf (d.of, " do\n");
3583 else
3584 oprintf (d.of, " while (x != xlimit)\n");
3586 oprintf (d.of, " {\n");
3587 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3589 oprintf (d.of, " %s (x);\n", mark_hook_name);
3592 d.prev_val[2] = "*x";
3593 d.indent = 6;
3594 if (orig_s->kind != TYPE_USER_STRUCT)
3595 walk_type (s, &d);
3596 else
3598 /* User structures have no fields to walk. Simply generate a call
3599 to the user-provided structure marker. */
3600 oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3603 if (chain_next != NULL)
3605 oprintf (d.of, " x = (");
3606 output_escaped_param (&d, chain_next, "chain_next");
3607 oprintf (d.of, ");\n");
3610 oprintf (d.of, " }\n");
3611 if (chain_circular != NULL)
3612 oprintf (d.of, " while (x != xlimit);\n");
3613 oprintf (d.of, "}\n");
3615 if (orig_s->kind == TYPE_USER_STRUCT)
3616 write_user_marking_functions (orig_s, wtd->prefix, &d);
3620 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3622 static void
3623 write_types (outf_p output_header, type_p structures, type_p param_structs,
3624 const struct write_types_data *wtd)
3626 int nbfun = 0; /* Count the emitted functions. */
3627 type_p s;
3629 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3631 /* We first emit the macros and the declarations. Functions' code is
3632 emitted afterwards. This is needed in plugin mode. */
3633 oprintf (output_header, "/* Macros and declarations. */\n");
3634 for (s = structures; s; s = s->next)
3635 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3637 options_p opt;
3639 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3640 continue;
3642 const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3644 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3645 output_mangled_typename (output_header, s);
3646 oprintf (output_header, "(X) do { \\\n");
3647 oprintf (output_header,
3648 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3649 s_id_for_tag);
3650 oprintf (output_header, " } while (0)\n");
3652 for (opt = s->u.s.opt; opt; opt = opt->next)
3653 if (strcmp (opt->name, "ptr_alias") == 0
3654 && opt->kind == OPTION_TYPE)
3656 const_type_p const t = (const_type_p) opt->info.type;
3657 if (t->kind == TYPE_STRUCT
3658 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3660 const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3661 oprintf (output_header,
3662 "#define gt_%sx_%s gt_%sx_%s\n",
3663 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3664 if (t_id_for_tag != t->u.s.tag)
3665 free (CONST_CAST (char *, t_id_for_tag));
3667 else
3668 error_at_line (&s->u.s.line,
3669 "structure alias is not a structure");
3670 break;
3672 if (opt)
3673 continue;
3675 /* Declare the marker procedure only once. */
3676 oprintf (output_header,
3677 "extern void gt_%sx_%s (void *);\n",
3678 wtd->prefix, s_id_for_tag);
3680 if (s_id_for_tag != s->u.s.tag)
3681 free (CONST_CAST (char *, s_id_for_tag));
3683 if (s->u.s.line.file == NULL)
3685 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3686 s->u.s.tag);
3687 continue;
3691 for (s = param_structs; s; s = s->next)
3692 if (s->gc_used == GC_POINTED_TO)
3694 type_p stru = s->u.param_struct.stru;
3696 /* Declare the marker procedure. */
3697 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
3698 output_mangled_typename (output_header, s);
3699 oprintf (output_header, " (void *);\n");
3701 if (stru->u.s.line.file == NULL)
3703 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3704 stru->u.s.tag);
3705 continue;
3709 /* At last we emit the functions code. */
3710 oprintf (output_header, "\n/* functions code */\n");
3711 for (s = structures; s; s = s->next)
3712 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3714 options_p opt;
3716 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3717 continue;
3718 for (opt = s->u.s.opt; opt; opt = opt->next)
3719 if (strcmp (opt->name, "ptr_alias") == 0)
3720 break;
3721 if (opt)
3722 continue;
3724 if (s->kind == TYPE_LANG_STRUCT)
3726 type_p ss;
3727 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3729 nbfun++;
3730 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3731 nbfun, (void*) ss, ss->u.s.tag);
3732 write_func_for_structure (s, ss, NULL, wtd);
3735 else
3737 nbfun++;
3738 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3739 nbfun, (void*) s, s->u.s.tag);
3740 write_func_for_structure (s, s, NULL, wtd);
3743 else
3745 /* Structure s is not possibly pointed to, so can be ignored. */
3746 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3747 (void*)s, s->u.s.tag,
3748 (int) s->gc_used);
3751 for (s = param_structs; s; s = s->next)
3752 if (s->gc_used == GC_POINTED_TO)
3754 type_p *param = s->u.param_struct.param;
3755 type_p stru = s->u.param_struct.stru;
3756 if (stru->u.s.line.file == NULL)
3757 continue;
3758 if (stru->kind == TYPE_LANG_STRUCT)
3760 type_p ss;
3761 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3763 nbfun++;
3764 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
3765 nbfun, (void*) ss, ss->u.s.tag);
3766 write_func_for_structure (s, ss, param, wtd);
3769 else
3771 nbfun++;
3772 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
3773 nbfun, (void*) s,
3774 (void*) stru, stru->u.s.tag);
3775 write_func_for_structure (s, stru, param, wtd);
3778 else
3780 /* Param structure s is not pointed to, so should be ignored. */
3781 DBGPRINTF ("ignored s @ %p", (void*)s);
3783 if (verbosity_level >= 2)
3784 printf ("%s emitted %d routines for %s\n",
3785 progname, nbfun, wtd->comment);
3788 static const struct write_types_data ggc_wtd = {
3789 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
3790 "GC marker procedures. ",
3791 FALSE
3794 static const struct write_types_data pch_wtd = {
3795 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3796 "gt_pch_note_reorder",
3797 "PCH type-walking procedures. ",
3798 TRUE
3801 /* Write out the local pointer-walking routines. */
3803 /* process_field routine for local pointer-walking for user-callable
3804 routines. The difference between this and
3805 write_types_local_process_field is that, in this case, we do not
3806 need to check whether the given pointer matches the address of the
3807 parent structure. This check was already generated by the call
3808 to gt_pch_nx in the main gt_pch_p_*() function that is calling
3809 this code. */
3811 static void
3812 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
3814 switch (f->kind)
3816 case TYPE_POINTER:
3817 case TYPE_STRUCT:
3818 case TYPE_UNION:
3819 case TYPE_LANG_STRUCT:
3820 case TYPE_PARAM_STRUCT:
3821 case TYPE_STRING:
3822 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3823 break;
3825 case TYPE_USER_STRUCT:
3826 if (d->in_ptr_field)
3827 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3828 else
3829 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3830 d->indent, "", d->val);
3831 break;
3833 case TYPE_SCALAR:
3834 break;
3836 case TYPE_ARRAY:
3837 case TYPE_NONE:
3838 case TYPE_UNDEFINED:
3839 gcc_unreachable ();
3844 /* Write a function to PCH walk all the fields of type S on OF.
3845 D contains data needed by walk_type to recurse into the fields of S. */
3847 static void
3848 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
3850 oprintf (d->of, "\nvoid\n");
3851 oprintf (d->of, "gt_pch_nx (");
3852 write_type_decl (d->of, s);
3853 oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
3854 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3855 "\tATTRIBUTE_UNUSED void *cookie)\n");
3856 oprintf (d->of, "{\n");
3857 d->val = "(*x)";
3858 d->indent = 2;
3859 d->process_field = write_types_local_user_process_field;
3860 walk_type (s, d);
3861 oprintf (d->of, "}\n");
3865 /* Emit the user-callable functions needed to mark all the types used
3866 by the user structure S. PREFIX is the prefix to use to
3867 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
3868 chain_next option defined. D contains data needed to pass to
3869 walk_type when traversing the fields of a type.
3871 For every type T referenced by S, two routines are generated: one
3872 that takes 'T *', marks the pointer and calls the second routine,
3873 which just marks the fields of T. */
3875 static void
3876 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
3878 gcc_assert (s->kind == TYPE_USER_STRUCT);
3880 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3882 type_p fld_type = fld->type;
3883 if (union_or_struct_p (fld_type))
3884 write_pch_user_walking_for_structure_body (fld_type, d);
3889 /* process_field routine for local pointer-walking. */
3891 static void
3892 write_types_local_process_field (type_p f, const struct walk_type_data *d)
3894 gcc_assert (d->have_this_obj);
3895 switch (f->kind)
3897 case TYPE_POINTER:
3898 case TYPE_STRUCT:
3899 case TYPE_UNION:
3900 case TYPE_LANG_STRUCT:
3901 case TYPE_PARAM_STRUCT:
3902 case TYPE_STRING:
3903 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3904 d->prev_val[3]);
3905 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3906 break;
3908 case TYPE_USER_STRUCT:
3909 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3910 d->prev_val[3]);
3911 if (d->in_ptr_field)
3912 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3913 else
3914 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3915 d->indent, "", d->val);
3916 break;
3918 case TYPE_SCALAR:
3919 break;
3921 case TYPE_ARRAY:
3922 case TYPE_NONE:
3923 case TYPE_UNDEFINED:
3924 gcc_unreachable ();
3929 /* For S, a structure that's part of ORIG_S, and using parameters
3930 PARAM, write out a routine that:
3931 - Is of type gt_note_pointers
3932 - Calls PROCESS_FIELD on each field of S or its substructures.
3935 static void
3936 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
3938 struct walk_type_data d;
3940 memset (&d, 0, sizeof (d));
3941 d.of = get_output_file_for_structure (s, param);
3942 d.process_field = write_types_local_process_field;
3943 d.opt = s->u.s.opt;
3944 d.line = &s->u.s.line;
3945 d.bitmap = s->u.s.bitmap;
3946 d.param = param;
3947 d.prev_val[0] = d.prev_val[2] = "*x";
3948 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3949 d.prev_val[3] = "x";
3950 d.val = "(*x)";
3951 d.fn_wants_lvalue = true;
3953 oprintf (d.of, "\n");
3954 oprintf (d.of, "void\n");
3955 oprintf (d.of, "gt_pch_p_");
3956 output_mangled_typename (d.of, orig_s);
3957 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3958 "\tvoid *x_p,\n"
3959 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3960 "\tATTRIBUTE_UNUSED void *cookie)\n");
3961 oprintf (d.of, "{\n");
3962 oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3963 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3964 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3965 d.indent = 2;
3966 d.have_this_obj = true;
3968 if (s->kind != TYPE_USER_STRUCT)
3969 walk_type (s, &d);
3970 else
3972 /* User structures have no fields to walk. Simply generate a
3973 call to the user-provided PCH walker. */
3974 oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
3975 d.prev_val[3]);
3976 oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3977 d.indent, "", d.val);
3980 oprintf (d.of, "}\n");
3982 /* Write user-callable entry points for the PCH walking routines. */
3983 if (orig_s->kind == TYPE_USER_STRUCT)
3984 write_pch_user_walking_functions (s, &d);
3987 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
3989 static void
3990 write_local (outf_p output_header, type_p structures, type_p param_structs)
3992 type_p s;
3994 if (!output_header)
3995 return;
3997 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
3998 for (s = structures; s; s = s->next)
3999 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
4001 options_p opt;
4003 if (s->u.s.line.file == NULL)
4004 continue;
4005 for (opt = s->u.s.opt; opt; opt = opt->next)
4006 if (strcmp (opt->name, "ptr_alias") == 0
4007 && opt->kind == OPTION_TYPE)
4009 const_type_p const t = (const_type_p) opt->info.type;
4010 if (t->kind == TYPE_STRUCT
4011 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
4013 oprintf (output_header, "#define gt_pch_p_");
4014 output_mangled_typename (output_header, s);
4015 oprintf (output_header, " gt_pch_p_");
4016 output_mangled_typename (output_header, t);
4017 oprintf (output_header, "\n");
4019 else
4020 error_at_line (&s->u.s.line,
4021 "structure alias is not a structure");
4022 break;
4024 if (opt)
4025 continue;
4027 /* Declare the marker procedure only once. */
4028 oprintf (output_header, "extern void gt_pch_p_");
4029 output_mangled_typename (output_header, s);
4030 oprintf (output_header,
4031 "\n (void *, void *, gt_pointer_operator, void *);\n");
4033 if (s->kind == TYPE_LANG_STRUCT)
4035 type_p ss;
4036 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4037 write_local_func_for_structure (s, ss, NULL);
4039 else
4040 write_local_func_for_structure (s, s, NULL);
4043 for (s = param_structs; s; s = s->next)
4044 if (s->gc_used == GC_POINTED_TO)
4046 type_p *param = s->u.param_struct.param;
4047 type_p stru = s->u.param_struct.stru;
4049 /* Declare the marker procedure. */
4050 oprintf (output_header, "extern void gt_pch_p_");
4051 output_mangled_typename (output_header, s);
4052 oprintf (output_header,
4053 "\n (void *, void *, gt_pointer_operator, void *);\n");
4055 if (stru->u.s.line.file == NULL)
4057 fprintf (stderr, "warning: structure `%s' used but not defined\n",
4058 stru->u.s.tag);
4059 continue;
4062 if (stru->kind == TYPE_LANG_STRUCT)
4064 type_p ss;
4065 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
4066 write_local_func_for_structure (s, ss, param);
4068 else
4069 write_local_func_for_structure (s, stru, param);
4073 /* Nonzero if S is a type for which typed GC allocators should be output. */
4075 #define USED_BY_TYPED_GC_P(s) \
4076 ((s->kind == TYPE_POINTER \
4077 && (s->u.p->gc_used == GC_POINTED_TO \
4078 || s->u.p->gc_used == GC_USED)) \
4079 || (union_or_struct_p (s) \
4080 && ((s)->gc_used == GC_POINTED_TO \
4081 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4082 && s->u.s.line.file != NULL) \
4083 || ((s)->gc_used == GC_USED \
4084 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))))))
4087 /* Might T contain any non-pointer elements? */
4089 static int
4090 contains_scalar_p (type_p t)
4092 switch (t->kind)
4094 case TYPE_STRING:
4095 case TYPE_POINTER:
4096 return 0;
4097 case TYPE_ARRAY:
4098 return contains_scalar_p (t->u.a.p);
4099 case TYPE_USER_STRUCT:
4100 /* User-marked structures will typically contain pointers. */
4101 return 0;
4102 default:
4103 /* Could also check for structures that have no non-pointer
4104 fields, but there aren't enough of those to worry about. */
4105 return 1;
4109 /* Mangle INPF and print it to F. */
4111 static void
4112 put_mangled_filename (outf_p f, const input_file *inpf)
4114 /* The call to get_output_file_name may indirectly update fn since
4115 get_output_file_with_visibility caches its result inside, so we
4116 need the CONST_CAST. */
4117 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4118 if (!f || !name)
4119 return;
4120 for (; *name != 0; name++)
4121 if (ISALNUM (*name))
4122 oprintf (f, "%c", *name);
4123 else
4124 oprintf (f, "%c", '_');
4127 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4128 LASTNAME, and NAME are all strings to insert in various places in
4129 the resulting code. */
4131 static void
4132 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4133 const char *tname, const char *name)
4135 struct flist *fli2;
4137 for (fli2 = flp; fli2; fli2 = fli2->next)
4138 if (fli2->started_p)
4140 oprintf (fli2->f, " %s\n", lastname);
4141 oprintf (fli2->f, "};\n\n");
4144 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4145 if (fli2->started_p)
4147 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4148 int fnum;
4150 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4151 if (bitmap & 1)
4153 oprintf (base_files[fnum],
4154 "extern const struct %s gt_%s_", tname, pfx);
4155 put_mangled_filename (base_files[fnum], fli2->file);
4156 oprintf (base_files[fnum], "[];\n");
4161 size_t fnum;
4162 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4163 oprintf (base_files[fnum],
4164 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4168 for (fli2 = flp; fli2; fli2 = fli2->next)
4169 if (fli2->started_p)
4171 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4172 int fnum;
4174 fli2->started_p = 0;
4176 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4177 if (bitmap & 1)
4179 oprintf (base_files[fnum], " gt_%s_", pfx);
4180 put_mangled_filename (base_files[fnum], fli2->file);
4181 oprintf (base_files[fnum], ",\n");
4186 size_t fnum;
4187 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4189 oprintf (base_files[fnum], " NULL\n");
4190 oprintf (base_files[fnum], "};\n");
4195 /* Write the first three fields (pointer, count and stride) for
4196 root NAME to F. V and LINE are as for write_root.
4198 Return true if the entry could be written; return false on error. */
4200 static bool
4201 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4203 type_p ap;
4205 if (!v)
4207 error_at_line (line, "`%s' is too complex to be a root", name);
4208 return false;
4211 oprintf (f, " {\n");
4212 oprintf (f, " &%s,\n", name);
4213 oprintf (f, " 1");
4215 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4216 if (ap->u.a.len[0])
4217 oprintf (f, " * (%s)", ap->u.a.len);
4218 else if (ap == v->type)
4219 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4220 oprintf (f, ",\n");
4221 oprintf (f, " sizeof (%s", v->name);
4222 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4223 oprintf (f, "[0]");
4224 oprintf (f, "),\n");
4225 return true;
4228 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4229 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4230 of the caller. */
4232 static void
4233 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4234 int has_length, struct fileloc *line, const char *if_marked,
4235 bool emit_pch, type_p field_type, const char *field_name)
4237 struct pair newv;
4238 /* If the field reference is relative to V, rather than to some
4239 subcomponent of V, we can mark any subarrays with a single stride.
4240 We're effectively treating the field as a global variable in its
4241 own right. */
4242 if (v && type == v->type)
4244 newv = *v;
4245 newv.type = field_type;
4246 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4247 v = &newv;
4249 /* Otherwise, any arrays nested in the structure are too complex to
4250 handle. */
4251 else if (field_type->kind == TYPE_ARRAY)
4252 v = NULL;
4253 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4254 has_length, line, if_marked, emit_pch);
4257 /* Write out to F the table entry and any marker routines needed to
4258 mark NAME as TYPE. V can be one of three values:
4260 - null, if NAME is too complex to represent using a single
4261 count and stride. In this case, it is an error for NAME to
4262 contain any gc-ed data.
4264 - the outermost array that contains NAME, if NAME is part of an array.
4266 - the C variable that contains NAME, if NAME is not part of an array.
4268 LINE is the line of the C source that declares the root variable.
4269 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
4270 is nonzero iff we are building the root table for hash table caches. */
4272 static void
4273 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4274 struct fileloc *line, const char *if_marked, bool emit_pch)
4276 switch (type->kind)
4278 case TYPE_STRUCT:
4280 pair_p fld;
4281 for (fld = type->u.s.fields; fld; fld = fld->next)
4283 int skip_p = 0;
4284 const char *desc = NULL;
4285 options_p o;
4287 for (o = fld->opt; o; o = o->next)
4288 if (strcmp (o->name, "skip") == 0)
4289 skip_p = 1;
4290 else if (strcmp (o->name, "desc") == 0
4291 && o->kind == OPTION_STRING)
4292 desc = o->info.string;
4293 else if (strcmp (o->name, "param_is") == 0)
4295 else
4296 error_at_line (line,
4297 "field `%s' of global `%s' has unknown option `%s'",
4298 fld->name, name, o->name);
4300 if (skip_p)
4301 continue;
4302 else if (desc && fld->type->kind == TYPE_UNION)
4304 pair_p validf = NULL;
4305 pair_p ufld;
4307 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4309 const char *tag = NULL;
4310 options_p oo;
4311 for (oo = ufld->opt; oo; oo = oo->next)
4312 if (strcmp (oo->name, "tag") == 0
4313 && oo->kind == OPTION_STRING)
4314 tag = oo->info.string;
4315 if (tag == NULL || strcmp (tag, desc) != 0)
4316 continue;
4317 if (validf != NULL)
4318 error_at_line (line,
4319 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4320 name, fld->name, validf->name,
4321 name, fld->name, ufld->name, tag);
4322 validf = ufld;
4324 if (validf != NULL)
4325 write_field_root (f, v, type, name, 0, line, if_marked,
4326 emit_pch, validf->type,
4327 ACONCAT ((fld->name, ".",
4328 validf->name, NULL)));
4330 else if (desc)
4331 error_at_line (line,
4332 "global `%s.%s' has `desc' option but is not union",
4333 name, fld->name);
4334 else
4335 write_field_root (f, v, type, name, 0, line, if_marked,
4336 emit_pch, fld->type, fld->name);
4339 break;
4341 case TYPE_ARRAY:
4343 char *newname;
4344 newname = xasprintf ("%s[0]", name);
4345 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
4346 emit_pch);
4347 free (newname);
4349 break;
4351 case TYPE_USER_STRUCT:
4352 error_at_line (line, "`%s' must be a pointer type, because it is "
4353 "a GC root and its type is marked with GTY((user))",
4354 v->name);
4355 break;
4357 case TYPE_POINTER:
4359 type_p tp;
4361 if (!start_root_entry (f, v, name, line))
4362 return;
4364 tp = type->u.p;
4366 if (!has_length && union_or_struct_p (tp))
4368 const char *id_for_tag = filter_type_name (tp->u.s.tag);
4369 oprintf (f, " &gt_ggc_mx_%s,\n", id_for_tag);
4370 if (emit_pch)
4371 oprintf (f, " &gt_pch_nx_%s", id_for_tag);
4372 else
4373 oprintf (f, " NULL");
4374 if (id_for_tag != tp->u.s.tag)
4375 free (CONST_CAST (char *, id_for_tag));
4377 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
4379 oprintf (f, " &gt_ggc_m_");
4380 output_mangled_typename (f, tp);
4381 if (emit_pch)
4383 oprintf (f, ",\n &gt_pch_n_");
4384 output_mangled_typename (f, tp);
4386 else
4387 oprintf (f, ",\n NULL");
4389 else if (has_length
4390 && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
4392 oprintf (f, " &gt_ggc_ma_%s,\n", name);
4393 if (emit_pch)
4394 oprintf (f, " &gt_pch_na_%s", name);
4395 else
4396 oprintf (f, " NULL");
4398 else
4400 error_at_line (line,
4401 "global `%s' is pointer to unimplemented type",
4402 name);
4404 if (if_marked)
4405 oprintf (f, ",\n &%s", if_marked);
4406 oprintf (f, "\n },\n");
4408 break;
4410 case TYPE_STRING:
4412 if (!start_root_entry (f, v, name, line))
4413 return;
4415 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
4416 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
4417 oprintf (f, " },\n");
4419 break;
4421 case TYPE_SCALAR:
4422 break;
4424 case TYPE_NONE:
4425 case TYPE_UNDEFINED:
4426 case TYPE_UNION:
4427 case TYPE_LANG_STRUCT:
4428 case TYPE_PARAM_STRUCT:
4429 error_at_line (line, "global `%s' is unimplemented type", name);
4433 /* This generates a routine to walk an array. */
4435 static void
4436 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4438 struct walk_type_data d;
4439 char *prevval3;
4441 memset (&d, 0, sizeof (d));
4442 d.of = f;
4443 d.cookie = wtd;
4444 d.indent = 2;
4445 d.line = &v->line;
4446 d.opt = v->opt;
4447 d.bitmap = get_lang_bitmap (v->line.file);
4448 d.param = NULL;
4450 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4452 if (wtd->param_prefix)
4454 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4455 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
4456 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4457 wtd->param_prefix, v->name);
4458 oprintf (d.of,
4459 " ATTRIBUTE_UNUSED void *x_p,\n"
4460 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4461 " ATTRIBUTE_UNUSED void * cookie)\n");
4462 oprintf (d.of, "{\n");
4463 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4464 d.process_field = write_types_local_process_field;
4465 d.have_this_obj = true;
4466 walk_type (v->type, &d);
4467 oprintf (f, "}\n\n");
4470 d.opt = v->opt;
4471 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4472 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4473 wtd->prefix, v->name);
4474 oprintf (f, "{\n");
4475 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4476 d.process_field = write_types_process_field;
4477 d.have_this_obj = false;
4478 walk_type (v->type, &d);
4479 free (prevval3);
4480 oprintf (f, "}\n\n");
4483 /* Output a table describing the locations and types of VARIABLES. */
4485 static void
4486 write_roots (pair_p variables, bool emit_pch)
4488 pair_p v;
4489 struct flist *flp = NULL;
4491 for (v = variables; v; v = v->next)
4493 outf_p f =
4494 get_output_file_with_visibility (CONST_CAST (input_file*,
4495 v->line.file));
4496 struct flist *fli;
4497 const char *length = NULL;
4498 int deletable_p = 0;
4499 options_p o;
4500 for (o = v->opt; o; o = o->next)
4501 if (strcmp (o->name, "length") == 0
4502 && o->kind == OPTION_STRING)
4503 length = o->info.string;
4504 else if (strcmp (o->name, "deletable") == 0)
4505 deletable_p = 1;
4506 else if (strcmp (o->name, "param_is") == 0)
4508 else if (strncmp (o->name, "param", 5) == 0
4509 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
4511 else if (strcmp (o->name, "if_marked") == 0)
4513 else
4514 error_at_line (&v->line,
4515 "global `%s' has unknown option `%s'",
4516 v->name, o->name);
4518 for (fli = flp; fli; fli = fli->next)
4519 if (fli->f == f && f)
4520 break;
4521 if (fli == NULL)
4523 fli = XNEW (struct flist);
4524 fli->f = f;
4525 fli->next = flp;
4526 fli->started_p = 0;
4527 fli->file = v->line.file;
4528 gcc_assert (fli->file);
4529 flp = fli;
4531 oprintf (f, "\n/* GC roots. */\n\n");
4534 if (!deletable_p
4535 && length
4536 && v->type->kind == TYPE_POINTER
4537 && (v->type->u.p->kind == TYPE_POINTER
4538 || v->type->u.p->kind == TYPE_STRUCT))
4540 write_array (f, v, &ggc_wtd);
4541 write_array (f, v, &pch_wtd);
4545 for (v = variables; v; v = v->next)
4547 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4548 v->line.file));
4549 struct flist *fli;
4550 int skip_p = 0;
4551 int length_p = 0;
4552 options_p o;
4554 for (o = v->opt; o; o = o->next)
4555 if (strcmp (o->name, "length") == 0)
4556 length_p = 1;
4557 else if (strcmp (o->name, "deletable") == 0
4558 || strcmp (o->name, "if_marked") == 0)
4559 skip_p = 1;
4561 if (skip_p)
4562 continue;
4564 for (fli = flp; fli; fli = fli->next)
4565 if (fli->f == f)
4566 break;
4567 if (!fli->started_p)
4569 fli->started_p = 1;
4571 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4572 put_mangled_filename (f, v->line.file);
4573 oprintf (f, "[] = {\n");
4576 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4579 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4580 "gt_ggc_rtab");
4582 for (v = variables; v; v = v->next)
4584 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4585 v->line.file));
4586 struct flist *fli;
4587 int skip_p = 1;
4588 options_p o;
4590 for (o = v->opt; o; o = o->next)
4591 if (strcmp (o->name, "deletable") == 0)
4592 skip_p = 0;
4593 else if (strcmp (o->name, "if_marked") == 0)
4594 skip_p = 1;
4596 if (skip_p)
4597 continue;
4599 for (fli = flp; fli; fli = fli->next)
4600 if (fli->f == f)
4601 break;
4602 if (!fli->started_p)
4604 fli->started_p = 1;
4606 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4607 put_mangled_filename (f, v->line.file);
4608 oprintf (f, "[] = {\n");
4611 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4612 v->name, v->name);
4615 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4616 "gt_ggc_deletable_rtab");
4618 for (v = variables; v; v = v->next)
4620 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4621 v->line.file));
4622 struct flist *fli;
4623 const char *if_marked = NULL;
4624 int length_p = 0;
4625 options_p o;
4627 for (o = v->opt; o; o = o->next)
4628 if (strcmp (o->name, "length") == 0)
4629 length_p = 1;
4630 else if (strcmp (o->name, "if_marked") == 0
4631 && o->kind == OPTION_STRING)
4632 if_marked = o->info.string;
4633 if (if_marked == NULL)
4634 continue;
4635 if (v->type->kind != TYPE_POINTER
4636 || v->type->u.p->kind != TYPE_PARAM_STRUCT
4637 || v->type->u.p->u.param_struct.stru != find_structure ("htab",
4638 TYPE_STRUCT))
4640 error_at_line (&v->line,
4641 "if_marked option used but not hash table");
4642 continue;
4645 for (fli = flp; fli; fli = fli->next)
4646 if (fli->f == f)
4647 break;
4648 if (!fli->started_p)
4650 fli->started_p = 1;
4652 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4653 put_mangled_filename (f, v->line.file);
4654 oprintf (f, "[] = {\n");
4657 write_root (f, v, v->type->u.p->u.param_struct.param[0],
4658 v->name, length_p, &v->line, if_marked, emit_pch);
4661 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4662 "gt_ggc_cache_rtab");
4664 if (!emit_pch)
4665 return;
4667 for (v = variables; v; v = v->next)
4669 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4670 v->line.file));
4671 struct flist *fli;
4672 int length_p = 0;
4673 int if_marked_p = 0;
4674 options_p o;
4676 for (o = v->opt; o; o = o->next)
4677 if (strcmp (o->name, "length") == 0)
4678 length_p = 1;
4679 else if (strcmp (o->name, "if_marked") == 0)
4680 if_marked_p = 1;
4682 if (!if_marked_p)
4683 continue;
4685 for (fli = flp; fli; fli = fli->next)
4686 if (fli->f == f)
4687 break;
4688 if (!fli->started_p)
4690 fli->started_p = 1;
4692 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4693 put_mangled_filename (f, v->line.file);
4694 oprintf (f, "[] = {\n");
4697 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4700 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4701 "gt_pch_cache_rtab");
4703 for (v = variables; v; v = v->next)
4705 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4706 v->line.file));
4707 struct flist *fli;
4708 int skip_p = 0;
4709 options_p o;
4711 for (o = v->opt; o; o = o->next)
4712 if (strcmp (o->name, "deletable") == 0
4713 || strcmp (o->name, "if_marked") == 0)
4715 skip_p = 1;
4716 break;
4719 if (skip_p)
4720 continue;
4722 if (!contains_scalar_p (v->type))
4723 continue;
4725 for (fli = flp; fli; fli = fli->next)
4726 if (fli->f == f)
4727 break;
4728 if (!fli->started_p)
4730 fli->started_p = 1;
4732 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4733 put_mangled_filename (f, v->line.file);
4734 oprintf (f, "[] = {\n");
4737 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4738 v->name, v->name);
4741 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4742 "gt_pch_scalar_rtab");
4745 /* TRUE if type S has the GTY variable_size annotation. */
4747 static bool
4748 variable_size_p (const type_p s)
4750 options_p o;
4751 for (o = s->u.s.opt; o; o = o->next)
4752 if (strcmp (o->name, "variable_size") == 0)
4753 return true;
4754 return false;
4757 enum alloc_quantity
4758 { single, vector };
4760 /* Writes one typed allocator definition into output F for type
4761 identifier TYPE_NAME with optional type specifier TYPE_SPECIFIER.
4762 The allocator name will contain ALLOCATOR_TYPE. If VARIABLE_SIZE
4763 is true, the allocator will have an extra parameter specifying
4764 number of bytes to allocate. If QUANTITY is set to VECTOR, a
4765 vector allocator will be output. */
4767 static void
4768 write_typed_alloc_def (outf_p f,
4769 bool variable_size, const char *type_specifier,
4770 const char *type_name, const char *allocator_type,
4771 enum alloc_quantity quantity)
4773 bool two_args = variable_size && (quantity == vector);
4774 gcc_assert (f != NULL);
4775 const char *type_name_as_id = filter_type_name (type_name);
4776 oprintf (f, "#define ggc_alloc_%s%s", allocator_type, type_name_as_id);
4777 oprintf (f, "(%s%s%s) ",
4778 (variable_size ? "SIZE" : ""),
4779 (two_args ? ", " : ""),
4780 (quantity == vector) ? "n" : "");
4781 oprintf (f, "((%s%s *)", type_specifier, type_name);
4782 oprintf (f, "(ggc_internal_%salloc_stat (", allocator_type);
4783 if (variable_size)
4784 oprintf (f, "SIZE");
4785 else
4786 oprintf (f, "sizeof (%s%s)", type_specifier, type_name);
4787 if (quantity == vector)
4788 oprintf (f, ", n");
4789 oprintf (f, " MEM_STAT_INFO)))\n");
4790 if (type_name_as_id != type_name)
4791 free (CONST_CAST (char *, type_name_as_id));
4794 /* Writes a typed allocator definition into output F for a struct or
4795 union S, with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4797 static void
4798 write_typed_struct_alloc_def (outf_p f,
4799 const type_p s, const char *allocator_type,
4800 enum alloc_quantity quantity)
4802 gcc_assert (union_or_struct_p (s));
4803 write_typed_alloc_def (f, variable_size_p (s), get_type_specifier (s),
4804 s->u.s.tag, allocator_type, quantity);
4807 /* Writes a typed allocator definition into output F for a typedef P,
4808 with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4810 static void
4811 write_typed_typedef_alloc_def (outf_p f,
4812 const pair_p p, const char *allocator_type,
4813 enum alloc_quantity quantity)
4815 write_typed_alloc_def (f, variable_size_p (p->type), "", p->name,
4816 allocator_type, quantity);
4819 /* Writes typed allocator definitions into output F for the types in
4820 STRUCTURES and TYPEDEFS that are used by GC. */
4822 static void
4823 write_typed_alloc_defns (outf_p f,
4824 const type_p structures, const pair_p typedefs)
4826 type_p s;
4827 pair_p p;
4829 gcc_assert (f != NULL);
4830 oprintf (f,
4831 "\n/* Allocators for known structs and unions. */\n\n");
4832 for (s = structures; s; s = s->next)
4834 if (!USED_BY_TYPED_GC_P (s))
4835 continue;
4836 gcc_assert (union_or_struct_p (s));
4837 /* In plugin mode onput output ggc_alloc macro definitions
4838 relevant to plugin input files. */
4839 if (nb_plugin_files > 0
4840 && ((s->u.s.line.file == NULL) || !s->u.s.line.file->inpisplugin))
4841 continue;
4842 write_typed_struct_alloc_def (f, s, "", single);
4843 write_typed_struct_alloc_def (f, s, "cleared_", single);
4844 write_typed_struct_alloc_def (f, s, "vec_", vector);
4845 write_typed_struct_alloc_def (f, s, "cleared_vec_", vector);
4848 oprintf (f, "\n/* Allocators for known typedefs. */\n");
4849 for (p = typedefs; p; p = p->next)
4851 s = p->type;
4852 if (!USED_BY_TYPED_GC_P (s) || (strcmp (p->name, s->u.s.tag) == 0))
4853 continue;
4854 /* In plugin mode onput output ggc_alloc macro definitions
4855 relevant to plugin input files. */
4856 if (nb_plugin_files > 0)
4858 struct fileloc* filoc = type_fileloc (s);
4859 if (!filoc || !filoc->file->inpisplugin)
4860 continue;
4862 write_typed_typedef_alloc_def (f, p, "", single);
4863 write_typed_typedef_alloc_def (f, p, "cleared_", single);
4864 write_typed_typedef_alloc_def (f, p, "vec_", vector);
4865 write_typed_typedef_alloc_def (f, p, "cleared_vec_", vector);
4869 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4870 guaranteee for somewhat increased readability. If name conflicts do happen,
4871 this funcion will have to be adjusted to be more like
4872 output_mangled_typename. */
4874 static void
4875 output_typename (outf_p of, const_type_p t)
4877 switch (t->kind)
4879 case TYPE_STRING:
4880 oprintf (of, "str");
4881 break;
4882 case TYPE_SCALAR:
4883 oprintf (of, "scalar");
4884 break;
4885 case TYPE_POINTER:
4886 output_typename (of, t->u.p);
4887 break;
4888 case TYPE_STRUCT:
4889 case TYPE_USER_STRUCT:
4890 case TYPE_UNION:
4891 case TYPE_LANG_STRUCT:
4892 oprintf (of, "%s", t->u.s.tag);
4893 break;
4894 case TYPE_PARAM_STRUCT:
4896 int i;
4897 for (i = 0; i < NUM_PARAM; i++)
4898 if (t->u.param_struct.param[i] != NULL)
4900 output_typename (of, t->u.param_struct.param[i]);
4901 oprintf (of, "_");
4903 output_typename (of, t->u.param_struct.stru);
4904 break;
4906 case TYPE_NONE:
4907 case TYPE_UNDEFINED:
4908 case TYPE_ARRAY:
4909 gcc_unreachable ();
4913 /* Writes a typed GC allocator for type S that is suitable as a callback for
4914 the splay tree implementation in libiberty. */
4916 static void
4917 write_splay_tree_allocator_def (const_type_p s)
4919 outf_p of = get_output_file_with_visibility (NULL);
4920 oprintf (of, "void * ggc_alloc_splay_tree_");
4921 output_typename (of, s);
4922 oprintf (of, " (int sz, void * nl)\n");
4923 oprintf (of, "{\n");
4924 oprintf (of, " return ggc_splay_alloc (sz, nl);\n");
4925 oprintf (of, "}\n\n");
4928 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
4929 for the splay tree implementation in libiberty. */
4931 static void
4932 write_splay_tree_allocators (const_type_p param_structs)
4934 const_type_p s;
4936 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
4937 for (s = param_structs; s; s = s->next)
4938 if (s->gc_used == GC_POINTED_TO)
4940 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
4941 output_typename (header_file, s);
4942 oprintf (header_file, " (int, void *);\n");
4943 write_splay_tree_allocator_def (s);
4947 #define INDENT 2
4949 /* Dumps the value of typekind KIND. */
4951 static void
4952 dump_typekind (int indent, enum typekind kind)
4954 printf ("%*ckind = ", indent, ' ');
4955 switch (kind)
4957 case TYPE_SCALAR:
4958 printf ("TYPE_SCALAR");
4959 break;
4960 case TYPE_STRING:
4961 printf ("TYPE_STRING");
4962 break;
4963 case TYPE_STRUCT:
4964 printf ("TYPE_STRUCT");
4965 break;
4966 case TYPE_UNDEFINED:
4967 printf ("TYPE_UNDEFINED");
4968 break;
4969 case TYPE_USER_STRUCT:
4970 printf ("TYPE_USER_STRUCT");
4971 break;
4972 case TYPE_UNION:
4973 printf ("TYPE_UNION");
4974 break;
4975 case TYPE_POINTER:
4976 printf ("TYPE_POINTER");
4977 break;
4978 case TYPE_ARRAY:
4979 printf ("TYPE_ARRAY");
4980 break;
4981 case TYPE_LANG_STRUCT:
4982 printf ("TYPE_LANG_STRUCT");
4983 break;
4984 case TYPE_PARAM_STRUCT:
4985 printf ("TYPE_PARAM_STRUCT");
4986 break;
4987 default:
4988 gcc_unreachable ();
4990 printf ("\n");
4993 /* Dumps the value of GC_USED flag. */
4995 static void
4996 dump_gc_used (int indent, enum gc_used_enum gc_used)
4998 printf ("%*cgc_used = ", indent, ' ');
4999 switch (gc_used)
5001 case GC_UNUSED:
5002 printf ("GC_UNUSED");
5003 break;
5004 case GC_USED:
5005 printf ("GC_USED");
5006 break;
5007 case GC_MAYBE_POINTED_TO:
5008 printf ("GC_MAYBE_POINTED_TO");
5009 break;
5010 case GC_POINTED_TO:
5011 printf ("GC_POINTED_TO");
5012 break;
5013 default:
5014 gcc_unreachable ();
5016 printf ("\n");
5019 /* Dumps the type options OPT. */
5021 static void
5022 dump_options (int indent, options_p opt)
5024 options_p o;
5025 printf ("%*coptions = ", indent, ' ');
5026 o = opt;
5027 while (o)
5029 switch (o->kind)
5031 case OPTION_STRING:
5032 printf ("%s:string %s ", o->name, o->info.string);
5033 break;
5034 case OPTION_TYPE:
5035 printf ("%s:type ", o->name);
5036 dump_type (indent+1, o->info.type);
5037 break;
5038 case OPTION_NESTED:
5039 printf ("%s:nested ", o->name);
5040 break;
5041 case OPTION_NONE:
5042 gcc_unreachable ();
5044 o = o->next;
5046 printf ("\n");
5049 /* Dumps the source file location in LINE. */
5051 static void
5052 dump_fileloc (int indent, struct fileloc line)
5054 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
5055 get_input_file_name (line.file),
5056 line.line);
5059 /* Recursively dumps the struct, union, or a language-specific
5060 struct T. */
5062 static void
5063 dump_type_u_s (int indent, type_p t)
5065 pair_p fields;
5067 gcc_assert (union_or_struct_p (t));
5068 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
5069 dump_fileloc (indent, t->u.s.line);
5070 printf ("%*cu.s.fields =\n", indent, ' ');
5071 fields = t->u.s.fields;
5072 while (fields)
5074 dump_pair (indent + INDENT, fields);
5075 fields = fields->next;
5077 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
5078 dump_options (indent, t->u.s.opt);
5079 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
5080 if (t->kind == TYPE_LANG_STRUCT)
5082 printf ("%*cu.s.lang_struct:\n", indent, ' ');
5083 dump_type_list (indent + INDENT, t->u.s.lang_struct);
5087 /* Recursively dumps the array T. */
5089 static void
5090 dump_type_u_a (int indent, type_p t)
5092 gcc_assert (t->kind == TYPE_ARRAY);
5093 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
5094 dump_type_list (indent + INDENT, t->u.a.p);
5097 /* Recursively dumps the parameterized struct T. */
5099 static void
5100 dump_type_u_param_struct (int indent, type_p t)
5102 int i;
5103 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
5104 printf ("%*cu.param_struct.stru:\n", indent, ' ');
5105 dump_type_list (indent, t->u.param_struct.stru);
5106 dump_fileloc (indent, t->u.param_struct.line);
5107 for (i = 0; i < NUM_PARAM; i++)
5109 if (t->u.param_struct.param[i] == NULL)
5110 continue;
5111 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
5112 dump_type (indent + INDENT, t->u.param_struct.param[i]);
5116 /* Recursively dumps the type list T. */
5118 static void
5119 dump_type_list (int indent, type_p t)
5121 type_p p = t;
5122 while (p)
5124 dump_type (indent, p);
5125 p = p->next;
5129 static htab_t seen_types;
5131 /* Recursively dumps the type T if it was not dumped previously. */
5133 static void
5134 dump_type (int indent, type_p t)
5136 PTR *slot;
5138 if (seen_types == NULL)
5139 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
5141 printf ("%*cType at %p: ", indent, ' ', (void *) t);
5142 slot = htab_find_slot (seen_types, t, INSERT);
5143 if (*slot != NULL)
5145 printf ("already seen.\n");
5146 return;
5148 *slot = t;
5149 printf ("\n");
5151 dump_typekind (indent, t->kind);
5152 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
5153 (void *) t->pointer_to);
5154 dump_gc_used (indent + INDENT, t->gc_used);
5155 switch (t->kind)
5157 case TYPE_SCALAR:
5158 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
5159 t->u.scalar_is_char ? "true" : "false");
5160 break;
5161 case TYPE_STRING:
5162 break;
5163 case TYPE_STRUCT:
5164 case TYPE_UNION:
5165 case TYPE_LANG_STRUCT:
5166 case TYPE_USER_STRUCT:
5167 dump_type_u_s (indent + INDENT, t);
5168 break;
5169 case TYPE_POINTER:
5170 printf ("%*cp:\n", indent + INDENT, ' ');
5171 dump_type (indent + INDENT, t->u.p);
5172 break;
5173 case TYPE_ARRAY:
5174 dump_type_u_a (indent + INDENT, t);
5175 break;
5176 case TYPE_PARAM_STRUCT:
5177 dump_type_u_param_struct (indent + INDENT, t);
5178 break;
5179 default:
5180 gcc_unreachable ();
5182 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
5185 /* Dumps the pair P. */
5187 static void
5188 dump_pair (int indent, pair_p p)
5190 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
5191 dump_type (indent, p->type);
5192 dump_fileloc (indent, p->line);
5193 dump_options (indent, p->opt);
5194 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
5197 /* Dumps the list of pairs PP. */
5199 static void
5200 dump_pair_list (const char *name, pair_p pp)
5202 pair_p p;
5203 printf ("%s:\n", name);
5204 for (p = pp; p != NULL; p = p->next)
5205 dump_pair (0, p);
5206 printf ("End of %s\n\n", name);
5209 /* Dumps the STRUCTURES. */
5211 static void
5212 dump_structures (const char *name, type_p structures)
5214 printf ("%s:\n", name);
5215 dump_type_list (0, structures);
5216 printf ("End of %s\n\n", name);
5219 /* Dumps the internal structures of gengtype. This is useful to debug
5220 gengtype itself, or to understand what it does, e.g. for plugin
5221 developers. */
5223 static void
5224 dump_everything (void)
5226 dump_pair_list ("typedefs", typedefs);
5227 dump_structures ("structures", structures);
5228 dump_structures ("param_structs", param_structs);
5229 dump_pair_list ("variables", variables);
5231 /* Allocated with the first call to dump_type. */
5232 htab_delete (seen_types);
5237 /* Option specification for getopt_long. */
5238 static const struct option gengtype_long_options[] = {
5239 {"help", no_argument, NULL, 'h'},
5240 {"version", no_argument, NULL, 'V'},
5241 {"verbose", no_argument, NULL, 'v'},
5242 {"dump", no_argument, NULL, 'd'},
5243 {"debug", no_argument, NULL, 'D'},
5244 {"plugin", required_argument, NULL, 'P'},
5245 {"srcdir", required_argument, NULL, 'S'},
5246 {"backupdir", required_argument, NULL, 'B'},
5247 {"inputs", required_argument, NULL, 'I'},
5248 {"read-state", required_argument, NULL, 'r'},
5249 {"write-state", required_argument, NULL, 'w'},
5250 /* Terminating NULL placeholder. */
5251 {NULL, no_argument, NULL, 0},
5255 static void
5256 print_usage (void)
5258 printf ("Usage: %s\n", progname);
5259 printf ("\t -h | --help " " \t# Give this help.\n");
5260 printf ("\t -D | --debug "
5261 " \t# Give debug output to debug %s itself.\n", progname);
5262 printf ("\t -V | --version " " \t# Give version information.\n");
5263 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5264 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5265 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5266 " \t# Generate for plugin.\n");
5267 printf ("\t -S | --srcdir <GCC-directory> "
5268 " \t# Specify the GCC source directory.\n");
5269 printf ("\t -B | --backupdir <directory> "
5270 " \t# Specify the backup directory for updated files.\n");
5271 printf ("\t -I | --inputs <input-list> "
5272 " \t# Specify the file with source files list.\n");
5273 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5274 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5277 static void
5278 print_version (void)
5280 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5281 printf ("Report bugs: %s\n", bug_report_url);
5284 /* Parse the program options using getopt_long... */
5285 static void
5286 parse_program_options (int argc, char **argv)
5288 int opt = -1;
5289 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5290 gengtype_long_options, NULL)) >= 0)
5292 switch (opt)
5294 case 'h': /* --help */
5295 print_usage ();
5296 break;
5297 case 'V': /* --version */
5298 print_version ();
5299 break;
5300 case 'd': /* --dump */
5301 do_dump = 1;
5302 break;
5303 case 'D': /* --debug */
5304 do_debug = 1;
5305 break;
5306 case 'v': /* --verbose */
5307 verbosity_level++;
5308 break;
5309 case 'P': /* --plugin */
5310 if (optarg)
5311 plugin_output_filename = optarg;
5312 else
5313 fatal ("missing plugin output file name");
5314 break;
5315 case 'S': /* --srcdir */
5316 if (optarg)
5317 srcdir = optarg;
5318 else
5319 fatal ("missing source directory");
5320 srcdir_len = strlen (srcdir);
5321 break;
5322 case 'B': /* --backupdir */
5323 if (optarg)
5324 backup_dir = optarg;
5325 else
5326 fatal ("missing backup directory");
5327 break;
5328 case 'I': /* --inputs */
5329 if (optarg)
5330 inputlist = optarg;
5331 else
5332 fatal ("missing input list");
5333 break;
5334 case 'r': /* --read-state */
5335 if (optarg)
5336 read_state_filename = optarg;
5337 else
5338 fatal ("missing read state file");
5339 DBGPRINTF ("read state %s\n", optarg);
5340 break;
5341 case 'w': /* --write-state */
5342 DBGPRINTF ("write state %s\n", optarg);
5343 if (optarg)
5344 write_state_filename = optarg;
5345 else
5346 fatal ("missing write state file");
5347 break;
5348 default:
5349 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5350 print_usage ();
5351 fatal ("unexpected flag");
5354 if (plugin_output_filename)
5356 /* In plugin mode we require some input files. */
5357 int i = 0;
5358 if (optind >= argc)
5359 fatal ("no source files given in plugin mode");
5360 nb_plugin_files = argc - optind;
5361 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5362 for (i = 0; i < (int) nb_plugin_files; i++)
5364 char *name = argv[i + optind];
5365 plugin_files[i] = input_file_by_name (name);
5372 /******* Manage input files. ******/
5374 /* Hash table of unique input file names. */
5375 static htab_t input_file_htab;
5377 /* Find or allocate a new input_file by hash-consing it. */
5378 input_file*
5379 input_file_by_name (const char* name)
5381 PTR* slot;
5382 input_file* f = NULL;
5383 int namlen = 0;
5384 if (!name)
5385 return NULL;
5386 namlen = strlen (name);
5387 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5388 f->inpbitmap = 0;
5389 f->inpoutf = NULL;
5390 f->inpisplugin = false;
5391 strcpy (f->inpname, name);
5392 slot = htab_find_slot (input_file_htab, f, INSERT);
5393 gcc_assert (slot != NULL);
5394 if (*slot)
5396 /* Already known input file. */
5397 free (f);
5398 return (input_file*)(*slot);
5400 /* New input file. */
5401 *slot = f;
5402 return f;
5405 /* Hash table support routines for input_file-s. */
5406 static hashval_t
5407 htab_hash_inputfile (const void *p)
5409 const input_file *inpf = (const input_file *) p;
5410 gcc_assert (inpf);
5411 return htab_hash_string (get_input_file_name (inpf));
5414 static int
5415 htab_eq_inputfile (const void *x, const void *y)
5417 const input_file *inpfx = (const input_file *) x;
5418 const input_file *inpfy = (const input_file *) y;
5419 gcc_assert (inpfx != NULL && inpfy != NULL);
5420 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5425 main (int argc, char **argv)
5427 size_t i;
5428 static struct fileloc pos = { NULL, 0 };
5429 outf_p output_header;
5431 /* Mandatory common initializations. */
5432 progname = "gengtype"; /* For fatal and messages. */
5433 /* Create the hash-table used to hash-cons input files. */
5434 input_file_htab =
5435 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5436 /* Initialize our special input files. */
5437 this_file = input_file_by_name (__FILE__);
5438 system_h_file = input_file_by_name ("system.h");
5439 /* Set the scalar_is_char union number for predefined scalar types. */
5440 scalar_nonchar.u.scalar_is_char = FALSE;
5441 scalar_char.u.scalar_is_char = TRUE;
5443 parse_program_options (argc, argv);
5445 #if ENABLE_CHECKING
5446 if (do_debug)
5448 time_t now = (time_t) 0;
5449 time (&now);
5450 DBGPRINTF ("gengtype started pid %d at %s",
5451 (int) getpid (), ctime (&now));
5453 #endif /* ENABLE_CHECKING */
5455 /* Parse the input list and the input files. */
5456 DBGPRINTF ("inputlist %s", inputlist);
5457 if (read_state_filename)
5459 if (inputlist)
5460 fatal ("input list %s cannot be given with a read state file %s",
5461 inputlist, read_state_filename);
5462 read_state (read_state_filename);
5463 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5464 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs);
5466 else if (inputlist)
5468 /* These types are set up with #define or else outside of where
5469 we can see them. We should initialize them before calling
5470 read_input_list. */
5471 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5472 Call;} while (0)
5473 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5474 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5475 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5476 POS_HERE (do_scalar_typedef ("double_int", &pos));
5477 POS_HERE (do_scalar_typedef ("double_int_storage", &pos));
5478 POS_HERE (do_scalar_typedef ("offset_int", &pos));
5479 POS_HERE (do_scalar_typedef ("widest_int", &pos));
5480 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5481 POS_HERE (do_scalar_typedef ("uint8", &pos));
5482 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5483 POS_HERE (do_scalar_typedef ("jword", &pos));
5484 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5485 POS_HERE (do_scalar_typedef ("void", &pos));
5486 POS_HERE (do_typedef ("PTR",
5487 create_pointer (resolve_typedef ("void", &pos)),
5488 &pos));
5489 #undef POS_HERE
5490 read_input_list (inputlist);
5491 for (i = 0; i < num_gt_files; i++)
5493 parse_file (get_input_file_name (gt_files[i]));
5494 DBGPRINTF ("parsed file #%d %s",
5495 (int) i, get_input_file_name (gt_files[i]));
5497 if (verbosity_level >= 1)
5498 printf ("%s parsed %d files with %d GTY types\n",
5499 progname, (int) num_gt_files, type_count);
5501 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5502 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
5505 else
5506 fatal ("either an input list or a read state file should be given");
5507 if (hit_error)
5508 return 1;
5511 if (plugin_output_filename)
5513 size_t ix = 0;
5514 /* In plugin mode, we should have read a state file, and have
5515 given at least one plugin file. */
5516 if (!read_state_filename)
5517 fatal ("No read state given in plugin mode for %s",
5518 plugin_output_filename);
5520 if (nb_plugin_files == 0 || !plugin_files)
5521 fatal ("No plugin files given in plugin mode for %s",
5522 plugin_output_filename);
5524 /* Parse our plugin files and augment the state. */
5525 for (ix = 0; ix < nb_plugin_files; ix++)
5527 input_file* pluginput = plugin_files [ix];
5528 pluginput->inpisplugin = true;
5529 parse_file (get_input_file_name (pluginput));
5531 if (hit_error)
5532 return 1;
5534 plugin_output = create_file ("GCC", plugin_output_filename);
5535 DBGPRINTF ("created plugin_output %p named %s",
5536 (void *) plugin_output, plugin_output->name);
5538 else
5539 { /* No plugin files, we are in normal mode. */
5540 if (!srcdir)
5541 fatal ("gengtype needs a source directory in normal mode");
5543 if (hit_error)
5544 return 1;
5546 gen_rtx_next ();
5548 /* The call to set_gc_used may indirectly call find_param_structure
5549 hence enlarge the param_structs list of types. */
5550 set_gc_used (variables);
5552 /* The state at this point is read from the state input file or by
5553 parsing source files and optionally augmented by parsing plugin
5554 source files. Write it now. */
5555 if (write_state_filename)
5557 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5558 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs);
5560 if (hit_error)
5561 fatal ("didn't write state file %s after errors",
5562 write_state_filename);
5564 DBGPRINTF ("before write_state %s", write_state_filename);
5565 write_state (write_state_filename);
5567 if (do_dump)
5568 dump_everything ();
5570 /* After having written the state file we return immediately to
5571 avoid generating any output file. */
5572 if (hit_error)
5573 return 1;
5574 else
5575 return 0;
5579 open_base_files ();
5581 output_header = plugin_output ? plugin_output : header_file;
5582 write_typed_alloc_defns (output_header, structures, typedefs);
5583 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5584 structures);
5585 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5586 param_structs);
5588 write_types (output_header, structures, param_structs, &ggc_wtd);
5589 if (plugin_files == NULL)
5591 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5592 structures);
5593 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5594 param_structs);
5595 write_types (header_file, structures, param_structs, &pch_wtd);
5596 write_local (header_file, structures, param_structs);
5598 write_splay_tree_allocators (param_structs);
5599 write_roots (variables, plugin_files == NULL);
5600 write_rtx_next ();
5601 close_output_files ();
5603 if (do_dump)
5604 dump_everything ();
5606 /* Don't bother about free-ing any input or plugin file, etc. */
5608 if (hit_error)
5609 return 1;
5610 return 0;