* ipa.c (function_and_variable_visibility): First remember function was global
[official-gcc.git] / gcc / gengtype.c
blobc12a4e2393d750da08a1acc3e89d34a6b8ca6da9
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 "double-int.h"
29 #include "version.h" /* for version_string & pkgversion_string. */
30 #include "hashtab.h"
31 #include "xregex.h"
32 #include "obstack.h"
33 #include "gengtype.h"
34 #include "filenames.h"
36 /* Data types, macros, etc. used only in this file. */
39 /* The list of output files. */
40 outf_p output_files;
42 /* The output header file that is included into pretty much every
43 source file. */
44 outf_p header_file;
47 /* The name of the file containing the list of input files. */
48 static char *inputlist;
50 /* The plugin input files and their number; in that case only
51 a single file is produced. */
52 static input_file **plugin_files;
53 static size_t nb_plugin_files;
55 /* The generated plugin output file and name. */
56 static outf_p plugin_output;
57 static char *plugin_output_filename;
59 /* Our source directory and its length. */
60 const char *srcdir;
61 size_t srcdir_len;
63 /* Variables used for reading and writing the state. */
64 const char *read_state_filename;
65 const char *write_state_filename;
67 /* Variables to help debugging. */
68 int do_dump;
69 int do_debug;
71 /* Level for verbose messages. */
72 int verbosity_level;
74 /* We have a type count and use it to set the state_number of newly
75 allocated types to some unique negative number. */
76 static int type_count;
78 /* The backup directory should be in the same file system as the
79 generated files, otherwise the rename(2) system call would fail.
80 If NULL, no backup is made when overwriting a generated file. */
81 static const char* backup_dir; /* (-B) program option. */
84 static outf_p create_file (const char *, const char *);
86 static const char *get_file_basename (const input_file *);
87 static const char *get_file_realbasename (const input_file *);
89 static int get_prefix_langdir_index (const char *);
90 static const char *get_file_langdir (const input_file *);
92 static void dump_pair (int indent, pair_p p);
93 static void dump_type (int indent, type_p p);
94 static void dump_type_list (int indent, type_p p);
97 /* Nonzero iff an error has occurred. */
98 bool hit_error = false;
100 static void gen_rtx_next (void);
101 static void write_rtx_next (void);
102 static void open_base_files (void);
103 static void close_output_files (void);
105 /* Report an error at POS, printing MSG. */
107 void
108 error_at_line (const struct fileloc *pos, const char *msg, ...)
110 va_list ap;
112 gcc_assert (pos != NULL && pos->file != NULL);
113 va_start (ap, msg);
115 fprintf (stderr, "%s:%d: ", get_input_file_name (pos->file), pos->line);
116 vfprintf (stderr, msg, ap);
117 fputc ('\n', stderr);
118 hit_error = true;
120 va_end (ap);
123 /* asprintf, but produces fatal message on out-of-memory. */
124 char *
125 xasprintf (const char *format, ...)
127 int n;
128 char *result;
129 va_list ap;
131 va_start (ap, format);
132 n = vasprintf (&result, format, ap);
133 if (result == NULL || n < 0)
134 fatal ("out of memory");
135 va_end (ap);
137 return result;
140 /* Input file handling. */
142 /* Table of all input files. */
143 const input_file **gt_files;
144 size_t num_gt_files;
146 /* A number of places use the name of this "gengtype.c" file for a
147 location for things that we can't rely on the source to define.
148 Make sure we can still use pointer comparison on filenames. */
149 input_file* this_file;
150 /* The "system.h" file is likewise specially useful. */
151 input_file* system_h_file;
153 /* Vector of per-language directories. */
154 const char **lang_dir_names;
155 size_t num_lang_dirs;
157 /* An array of output files suitable for definitions. There is one
158 BASE_FILES entry for each language. */
159 static outf_p *base_files;
163 #if ENABLE_CHECKING
164 /* Utility debugging function, printing the various type counts within
165 a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
166 void
167 dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
169 int nb_types = 0, nb_scalar = 0, nb_string = 0;
170 int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
171 int nb_lang_struct = 0, nb_param_struct = 0;
172 int nb_user_struct = 0, nb_undefined = 0;
173 type_p p = NULL;
174 for (p = t; p; p = p->next)
176 nb_types++;
177 switch (p->kind)
179 case TYPE_UNDEFINED:
180 nb_undefined++;
181 case TYPE_SCALAR:
182 nb_scalar++;
183 break;
184 case TYPE_STRING:
185 nb_string++;
186 break;
187 case TYPE_STRUCT:
188 nb_struct++;
189 break;
190 case TYPE_USER_STRUCT:
191 nb_user_struct++;
192 break;
193 case TYPE_UNION:
194 nb_union++;
195 break;
196 case TYPE_POINTER:
197 nb_pointer++;
198 break;
199 case TYPE_ARRAY:
200 nb_array++;
201 break;
202 case TYPE_LANG_STRUCT:
203 nb_lang_struct++;
204 break;
205 case TYPE_PARAM_STRUCT:
206 nb_param_struct++;
207 break;
208 case TYPE_NONE:
209 gcc_unreachable ();
212 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
213 lbasename (fil), lin, msg, nb_types);
214 if (nb_scalar > 0 || nb_string > 0)
215 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
216 if (nb_struct > 0 || nb_union > 0)
217 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
218 if (nb_pointer > 0 || nb_array > 0)
219 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
220 if (nb_lang_struct > 0 || nb_param_struct > 0)
221 fprintf (stderr, "@@%%@@ %d lang_structs, %d param_structs\n",
222 nb_lang_struct, nb_param_struct);
223 if (nb_user_struct > 0)
224 fprintf (stderr, "@@%%@@ %d user_structs\n", nb_user_struct);
225 if (nb_undefined > 0)
226 fprintf (stderr, "@@%%@@ %d undefined types\n", nb_undefined);
227 fprintf (stderr, "\n");
229 #endif /* ENABLE_CHECKING */
231 /* Scan the input file, LIST, and determine how much space we need to
232 store strings in. Also, count the number of language directories
233 and files. The numbers returned are overestimates as they does not
234 consider repeated files. */
235 static size_t
236 measure_input_list (FILE *list)
238 size_t n = 0;
239 int c;
240 bool atbol = true;
241 num_lang_dirs = 0;
242 num_gt_files = plugin_files ? nb_plugin_files : 0;
243 while ((c = getc (list)) != EOF)
245 n++;
246 if (atbol)
248 if (c == '[')
249 num_lang_dirs++;
250 else
252 /* Add space for a lang_bitmap before the input file name. */
253 n += sizeof (lang_bitmap);
254 num_gt_files++;
256 atbol = false;
259 if (c == '\n')
260 atbol = true;
263 rewind (list);
264 return n;
267 /* Read one input line from LIST to HEREP (which is updated). A
268 pointer to the string is returned via LINEP. If it was a language
269 subdirectory in square brackets, strip off the square brackets and
270 return true. Otherwise, leave space before the string for a
271 lang_bitmap, and return false. At EOF, returns false, does not
272 touch *HEREP, and sets *LINEP to NULL. POS is used for
273 diagnostics. */
274 static bool
275 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
277 char *here = *herep;
278 char *line;
279 int c = getc (list);
281 /* Read over whitespace. */
282 while (c == '\n' || c == ' ')
283 c = getc (list);
285 if (c == EOF)
287 *linep = 0;
288 return false;
290 else if (c == '[')
292 /* No space for a lang_bitmap is necessary. Discard the '['. */
293 c = getc (list);
294 line = here;
295 while (c != ']' && c != '\n' && c != EOF)
297 *here++ = c;
298 c = getc (list);
300 *here++ = '\0';
302 if (c == ']')
304 c = getc (list); /* eat what should be a newline */
305 if (c != '\n' && c != EOF)
306 error_at_line (pos, "junk on line after language tag [%s]", line);
308 else
309 error_at_line (pos, "missing close bracket for language tag [%s",
310 line);
312 *herep = here;
313 *linep = line;
314 return true;
316 else
318 /* Leave space for a lang_bitmap. */
319 memset (here, 0, sizeof (lang_bitmap));
320 here += sizeof (lang_bitmap);
321 line = here;
324 *here++ = c;
325 c = getc (list);
327 while (c != EOF && c != '\n');
328 *here++ = '\0';
329 *herep = here;
330 *linep = line;
331 return false;
335 /* Read the list of input files from LIST and compute all of the
336 relevant tables. There is one file per line of the list. At
337 first, all the files on the list are language-generic, but
338 eventually a line will appear which is the name of a language
339 subdirectory in square brackets, like this: [cp]. All subsequent
340 files are specific to that language, until another language
341 subdirectory tag appears. Files can appear more than once, if
342 they apply to more than one language. */
343 static void
344 read_input_list (const char *listname)
346 FILE *list = fopen (listname, "r");
347 if (!list)
348 fatal ("cannot open %s: %s", listname, xstrerror (errno));
349 else
351 struct fileloc epos;
352 size_t bufsz = measure_input_list (list);
353 char *buf = XNEWVEC (char, bufsz);
354 char *here = buf;
355 char *committed = buf;
356 char *limit = buf + bufsz;
357 char *line;
358 bool is_language;
359 size_t langno = 0;
360 size_t nfiles = 0;
361 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
363 epos.file = input_file_by_name (listname);
364 epos.line = 0;
366 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
367 gt_files = XNEWVEC (const input_file *, num_gt_files);
369 for (;;)
371 next_line:
372 epos.line++;
373 committed = here;
374 is_language = read_input_line (list, &here, &line, &epos);
375 gcc_assert (here <= limit);
376 if (line == 0)
377 break;
378 else if (is_language)
380 size_t i;
381 gcc_assert (langno <= num_lang_dirs);
382 for (i = 0; i < langno; i++)
383 if (strcmp (lang_dir_names[i], line) == 0)
385 error_at_line (&epos, "duplicate language tag [%s]",
386 line);
387 curlangs = 1 << i;
388 here = committed;
389 goto next_line;
392 curlangs = 1 << langno;
393 lang_dir_names[langno++] = line;
395 else
397 size_t i;
398 input_file *inpf = input_file_by_name (line);
399 gcc_assert (nfiles <= num_gt_files);
400 for (i = 0; i < nfiles; i++)
401 /* Since the input_file-s are uniquely hash-consed, we
402 can just compare pointers! */
403 if (gt_files[i] == inpf)
405 /* Throw away the string we just read, and add the
406 current language to the existing string's bitmap. */
407 lang_bitmap bmap = get_lang_bitmap (inpf);
408 if (bmap & curlangs)
409 error_at_line (&epos,
410 "file %s specified more than once "
411 "for language %s", line,
412 langno ==
413 0 ? "(all)" : lang_dir_names[langno -
414 1]);
416 bmap |= curlangs;
417 set_lang_bitmap (inpf, bmap);
418 here = committed;
419 goto next_line;
422 set_lang_bitmap (inpf, curlangs);
423 gt_files[nfiles++] = inpf;
426 /* Update the global counts now that we know accurately how many
427 things there are. (We do not bother resizing the arrays down.) */
428 num_lang_dirs = langno;
429 /* Add the plugin files if provided. */
430 if (plugin_files)
432 size_t i;
433 for (i = 0; i < nb_plugin_files; i++)
434 gt_files[nfiles++] = plugin_files[i];
436 num_gt_files = nfiles;
439 /* Sanity check: any file that resides in a language subdirectory
440 (e.g. 'cp') ought to belong to the corresponding language.
441 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
442 (Can you even do that? Should you be allowed to?) */
444 size_t f;
445 for (f = 0; f < num_gt_files; f++)
447 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
448 const char *basename = get_file_basename (gt_files[f]);
449 const char *slashpos = strchr (basename, '/');
450 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
451 const char *slashpos2 = strchr (basename, '\\');
453 if (!slashpos || (slashpos2 && slashpos2 < slashpos))
454 slashpos = slashpos2;
455 #endif
457 if (slashpos)
459 size_t l;
460 for (l = 0; l < num_lang_dirs; l++)
461 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
462 && memcmp (basename, lang_dir_names[l],
463 strlen (lang_dir_names[l])) == 0)
465 if (!(bitmap & (1 << l)))
466 error ("%s is in language directory '%s' but is not "
467 "tagged for that language",
468 basename, lang_dir_names[l]);
469 break;
475 if (ferror (list))
476 fatal ("error reading %s: %s", listname, xstrerror (errno));
478 fclose (list);
483 /* The one and only TYPE_STRING. */
485 struct type string_type = {
486 TYPE_STRING, 0, 0, 0, GC_USED, {0}
489 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
490 set early in main. */
492 struct type scalar_nonchar = {
493 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
496 struct type scalar_char = {
497 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
500 /* Lists of various things. */
502 pair_p typedefs = NULL;
503 type_p structures = NULL;
504 type_p param_structs = NULL;
505 pair_p variables = NULL;
507 static type_p find_param_structure (type_p t, type_p param[NUM_PARAM]);
508 static type_p adjust_field_tree_exp (type_p t, options_p opt);
509 static type_p adjust_field_rtx_def (type_p t, options_p opt);
511 /* Define S as a typedef to T at POS. */
513 void
514 do_typedef (const char *s, type_p t, struct fileloc *pos)
516 pair_p p;
518 /* temporary kludge - gengtype doesn't handle conditionals or
519 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
520 is coming from this file (main() sets them up with safe dummy
521 definitions). */
522 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
523 return;
525 for (p = typedefs; p != NULL; p = p->next)
526 if (strcmp (p->name, s) == 0)
528 if (p->type != t)
530 error_at_line (pos, "type `%s' previously defined", s);
531 error_at_line (&p->line, "previously defined here");
533 return;
536 p = XNEW (struct pair);
537 p->next = typedefs;
538 p->name = s;
539 p->type = t;
540 p->line = *pos;
541 p->opt = NULL;
542 typedefs = p;
545 /* Define S as a typename of a scalar. Cannot be used to define
546 typedefs of 'char'. Note: is also used for pointer-to-function
547 typedefs (which are therefore not treated as pointers). */
549 void
550 do_scalar_typedef (const char *s, struct fileloc *pos)
552 do_typedef (s, &scalar_nonchar, pos);
556 /* Define TYPE_NAME to be a user defined type at location POS. */
558 type_p
559 create_user_defined_type (const char *type_name, struct fileloc *pos)
561 type_p ty = find_structure (type_name, TYPE_USER_STRUCT);
563 /* We might have already seen an incomplete decl of the given type,
564 in which case we won't have yet seen a GTY((user)), and the type will
565 only have kind "TYPE_STRUCT". Mark it as a user struct. */
566 ty->kind = TYPE_USER_STRUCT;
568 ty->u.s.line = *pos;
569 ty->u.s.bitmap = get_lang_bitmap (pos->file);
570 do_typedef (type_name, ty, pos);
572 /* If TYPE_NAME specifies a template, create references to the types
573 in the template by pretending that each type is a field of TY.
574 This is needed to make sure that the types referenced by the
575 template are marked as used. */
576 char *str = xstrdup (type_name);
577 char *open_bracket = strchr (str, '<');
578 if (open_bracket)
580 /* We only accept simple template declarations (see
581 require_template_declaration), so we only need to parse a
582 comma-separated list of strings, implicitly assumed to
583 be type names. */
584 char *arg = open_bracket + 1;
585 char *type_id = strtok (arg, ",>");
586 pair_p fields = 0;
587 while (type_id)
589 /* Create a new field for every type found inside the template
590 parameter list. */
591 const char *field_name = xstrdup (type_id);
592 type_p arg_type = resolve_typedef (field_name, pos);
593 fields = create_field_at (fields, arg_type, field_name, 0, pos);
594 type_id = strtok (0, ",>");
597 /* Associate the field list to TY. */
598 ty->u.s.fields = fields;
600 free (str);
602 return ty;
606 /* Given a typedef name S, return its associated type. Return NULL if
607 S is not a registered type name. */
609 static type_p
610 type_for_name (const char *s)
612 pair_p p;
614 /* Special-case support for types within a "gcc::" namespace. Rather
615 than fully-supporting namespaces, simply strip off the "gcc::" prefix
616 where present. This allows us to have GTY roots of this form:
617 extern GTY(()) gcc::some_type *some_ptr;
618 where the autogenerated functions will refer to simply "some_type",
619 where they can be resolved into their namespace. */
620 if (0 == strncmp(s, "gcc::", 5))
621 s += 5;
623 for (p = typedefs; p != NULL; p = p->next)
624 if (strcmp (p->name, s) == 0)
625 return p->type;
626 return NULL;
630 /* Create an undefined type with name S and location POS. Return the
631 newly created type. */
633 static type_p
634 create_undefined_type (const char *s, struct fileloc *pos)
636 type_p ty = find_structure (s, TYPE_UNDEFINED);
637 ty->u.s.line = *pos;
638 ty->u.s.bitmap = get_lang_bitmap (pos->file);
639 do_typedef (s, ty, pos);
640 return ty;
644 /* Return the type previously defined for S. Use POS to report errors. */
646 type_p
647 resolve_typedef (const char *s, struct fileloc *pos)
649 bool is_template_instance = (strchr (s, '<') != NULL);
650 type_p p = type_for_name (s);
652 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
653 type for regular type identifiers. If the type identifier S is a
654 template instantiation, however, we treat it as a user defined
655 type.
657 FIXME, this is actually a limitation in gengtype. Supporting
658 template types and their instances would require keeping separate
659 track of the basic types definition and its instances. This
660 essentially forces all template classes in GC to be marked
661 GTY((user)). */
662 if (!p)
663 p = (is_template_instance)
664 ? create_user_defined_type (s, pos)
665 : create_undefined_type (s, pos);
667 return p;
671 /* Create and return a new structure with tag NAME at POS with fields
672 FIELDS and options O. The KIND of structure must be one of
673 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
675 type_p
676 new_structure (const char *name, enum typekind kind, struct fileloc *pos,
677 pair_p fields, options_p o)
679 type_p si;
680 type_p s = NULL;
681 lang_bitmap bitmap = get_lang_bitmap (pos->file);
682 bool isunion = (kind == TYPE_UNION);
684 gcc_assert (union_or_struct_p (kind));
686 for (si = structures; si != NULL; si = si->next)
687 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
689 type_p ls = NULL;
690 if (si->kind == TYPE_LANG_STRUCT)
692 ls = si;
694 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
695 if (si->u.s.bitmap == bitmap)
696 s = si;
698 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
700 ls = si;
701 type_count++;
702 si = XCNEW (struct type);
703 memcpy (si, ls, sizeof (struct type));
704 ls->kind = TYPE_LANG_STRUCT;
705 ls->u.s.lang_struct = si;
706 ls->u.s.fields = NULL;
707 si->next = NULL;
708 si->state_number = -type_count;
709 si->pointer_to = NULL;
710 si->u.s.lang_struct = ls;
712 else
713 s = si;
715 if (ls != NULL && s == NULL)
717 type_count++;
718 s = XCNEW (struct type);
719 s->state_number = -type_count;
720 s->next = ls->u.s.lang_struct;
721 ls->u.s.lang_struct = s;
722 s->u.s.lang_struct = ls;
724 break;
727 if (s == NULL)
729 type_count++;
730 s = XCNEW (struct type);
731 s->state_number = -type_count;
732 s->next = structures;
733 structures = s;
736 if (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap))
738 error_at_line (pos, "duplicate definition of '%s %s'",
739 isunion ? "union" : "struct", s->u.s.tag);
740 error_at_line (&s->u.s.line, "previous definition here");
743 s->kind = kind;
744 s->u.s.tag = name;
745 s->u.s.line = *pos;
746 s->u.s.fields = fields;
747 s->u.s.opt = o;
748 s->u.s.bitmap = bitmap;
749 if (s->u.s.lang_struct)
750 s->u.s.lang_struct->u.s.bitmap |= bitmap;
752 return s;
755 /* Return the previously-defined structure or union with tag NAME,
756 or a new empty structure or union if none was defined previously.
757 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
758 TYPE_USER_STRUCT. */
760 type_p
761 find_structure (const char *name, enum typekind kind)
763 type_p s;
764 bool isunion = (kind == TYPE_UNION);
766 gcc_assert (kind == TYPE_UNDEFINED || union_or_struct_p (kind));
768 for (s = structures; s != NULL; s = s->next)
769 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
770 return s;
772 type_count++;
773 s = XCNEW (struct type);
774 s->next = structures;
775 s->state_number = -type_count;
776 structures = s;
777 s->kind = kind;
778 s->u.s.tag = name;
779 structures = s;
780 return s;
783 /* Return the previously-defined parameterized structure for structure
784 T and parameters PARAM, or a new parameterized empty structure or
785 union if none was defined previously. */
787 static type_p
788 find_param_structure (type_p t, type_p param[NUM_PARAM])
790 type_p res;
792 for (res = param_structs; res; res = res->next)
793 if (res->u.param_struct.stru == t
794 && memcmp (res->u.param_struct.param, param,
795 sizeof (type_p) * NUM_PARAM) == 0)
796 break;
797 if (res == NULL)
799 type_count++;
800 res = XCNEW (struct type);
801 res->kind = TYPE_PARAM_STRUCT;
802 res->next = param_structs;
803 res->state_number = -type_count;
804 param_structs = res;
805 res->u.param_struct.stru = t;
806 memcpy (res->u.param_struct.param, param, sizeof (type_p) * NUM_PARAM);
808 return res;
811 /* Return a scalar type with name NAME. */
813 type_p
814 create_scalar_type (const char *name)
816 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
817 return &scalar_char;
818 else
819 return &scalar_nonchar;
823 /* Return a pointer to T. */
825 type_p
826 create_pointer (type_p t)
828 if (!t->pointer_to)
830 type_p r = XCNEW (struct type);
831 type_count++;
832 r->state_number = -type_count;
833 r->kind = TYPE_POINTER;
834 r->u.p = t;
835 t->pointer_to = r;
837 return t->pointer_to;
840 /* Return an array of length LEN. */
842 type_p
843 create_array (type_p t, const char *len)
845 type_p v;
847 type_count++;
848 v = XCNEW (struct type);
849 v->kind = TYPE_ARRAY;
850 v->state_number = -type_count;
851 v->u.a.p = t;
852 v->u.a.len = len;
853 return v;
856 /* Return a string options structure with name NAME and info INFO.
857 NEXT is the next option in the chain. */
858 options_p
859 create_string_option (options_p next, const char *name, const char *info)
861 options_p o = XNEW (struct options);
862 o->kind = OPTION_STRING;
863 o->next = next;
864 o->name = name;
865 o->info.string = info;
866 return o;
869 /* Create a type options structure with name NAME and info INFO. NEXT
870 is the next option in the chain. */
871 options_p
872 create_type_option (options_p next, const char* name, type_p info)
874 options_p o = XNEW (struct options);
875 o->next = next;
876 o->name = name;
877 o->kind = OPTION_TYPE;
878 o->info.type = info;
879 return o;
882 /* Create a nested pointer options structure with name NAME and info
883 INFO. NEXT is the next option in the chain. */
884 options_p
885 create_nested_option (options_p next, const char* name,
886 struct nested_ptr_data* info)
888 options_p o;
889 o = XNEW (struct options);
890 o->next = next;
891 o->name = name;
892 o->kind = OPTION_NESTED;
893 o->info.nested = info;
894 return o;
897 /* Return an options structure for a "nested_ptr" option. */
898 options_p
899 create_nested_ptr_option (options_p next, type_p t,
900 const char *to, const char *from)
902 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
904 d->type = adjust_field_type (t, 0);
905 d->convert_to = to;
906 d->convert_from = from;
907 return create_nested_option (next, "nested_ptr", d);
910 /* Add a variable named S of type T with options O defined at POS,
911 to `variables'. */
912 void
913 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
915 pair_p n;
916 n = XNEW (struct pair);
917 n->name = s;
918 n->type = t;
919 n->line = *pos;
920 n->opt = o;
921 n->next = variables;
922 variables = n;
925 /* Most-general structure field creator. */
926 static pair_p
927 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
928 const input_file *inpf, int line)
930 pair_p field;
932 field = XNEW (struct pair);
933 field->next = next;
934 field->type = type;
935 field->name = name;
936 field->opt = opt;
937 field->line.file = inpf;
938 field->line.line = line;
939 return field;
942 /* Create a field that came from the source code we are scanning,
943 i.e. we have a 'struct fileloc', and possibly options; also,
944 adjust_field_type should be called. */
945 pair_p
946 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
947 struct fileloc *pos)
949 return create_field_all (next, adjust_field_type (type, opt),
950 name, opt, pos->file, pos->line);
953 /* Create a fake field with the given type and name. NEXT is the next
954 field in the chain. */
955 #define create_field(next,type,name) \
956 create_field_all(next,type,name, 0, this_file, __LINE__)
958 /* Like create_field, but the field is only valid when condition COND
959 is true. */
961 static pair_p
962 create_optional_field_ (pair_p next, type_p type, const char *name,
963 const char *cond, int line)
965 static int id = 1;
966 pair_p union_fields;
967 type_p union_type;
969 /* Create a fake union type with a single nameless field of type TYPE.
970 The field has a tag of "1". This allows us to make the presence
971 of a field of type TYPE depend on some boolean "desc" being true. */
972 union_fields = create_field (NULL, type, "");
973 union_fields->opt =
974 create_string_option (union_fields->opt, "dot", "");
975 union_fields->opt =
976 create_string_option (union_fields->opt, "tag", "1");
977 union_type =
978 new_structure (xasprintf ("%s_%d", "fake_union", id++), TYPE_UNION,
979 &lexer_line, union_fields, NULL);
981 /* Create the field and give it the new fake union type. Add a "desc"
982 tag that specifies the condition under which the field is valid. */
983 return create_field_all (next, union_type, name,
984 create_string_option (0, "desc", cond),
985 this_file, line);
988 #define create_optional_field(next,type,name,cond) \
989 create_optional_field_(next,type,name,cond,__LINE__)
991 /* Reverse a linked list of 'struct pair's in place. */
992 pair_p
993 nreverse_pairs (pair_p list)
995 pair_p prev = 0, p, next;
996 for (p = list; p; p = next)
998 next = p->next;
999 p->next = prev;
1000 prev = p;
1002 return prev;
1006 /* We don't care how long a CONST_DOUBLE is. */
1007 #define CONST_DOUBLE_FORMAT "ww"
1008 /* We don't want to see codes that are only for generator files. */
1009 #undef GENERATOR_FILE
1011 enum rtx_code
1013 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1014 #include "rtl.def"
1015 #undef DEF_RTL_EXPR
1016 NUM_RTX_CODE
1019 static const char *const rtx_name[NUM_RTX_CODE] = {
1020 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1021 #include "rtl.def"
1022 #undef DEF_RTL_EXPR
1025 static const char *const rtx_format[NUM_RTX_CODE] = {
1026 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1027 #include "rtl.def"
1028 #undef DEF_RTL_EXPR
1031 static int rtx_next_new[NUM_RTX_CODE];
1033 /* We also need codes and names for insn notes (not register notes).
1034 Note that we do *not* bias the note values here. */
1035 enum insn_note
1037 #define DEF_INSN_NOTE(NAME) NAME,
1038 #include "insn-notes.def"
1039 #undef DEF_INSN_NOTE
1041 NOTE_INSN_MAX
1044 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1045 default field for line number notes. */
1046 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
1047 #define DEF_INSN_NOTE(NAME) #NAME,
1048 #include "insn-notes.def"
1049 #undef DEF_INSN_NOTE
1052 #undef CONST_DOUBLE_FORMAT
1053 #define GENERATOR_FILE
1055 /* Generate the contents of the rtx_next array. This really doesn't belong
1056 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1058 static void
1059 gen_rtx_next (void)
1061 int i;
1062 for (i = 0; i < NUM_RTX_CODE; i++)
1064 int k;
1066 rtx_next_new[i] = -1;
1067 if (strncmp (rtx_format[i], "iuu", 3) == 0)
1068 rtx_next_new[i] = 2;
1069 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1070 rtx_next_new[i] = 1;
1071 else
1072 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
1073 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1074 rtx_next_new[i] = k;
1078 /* Write out the contents of the rtx_next array. */
1079 static void
1080 write_rtx_next (void)
1082 outf_p f = get_output_file_with_visibility (NULL);
1083 int i;
1084 if (!f)
1085 return;
1087 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
1088 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1089 for (i = 0; i < NUM_RTX_CODE; i++)
1090 if (rtx_next_new[i] == -1)
1091 oprintf (f, " 0,\n");
1092 else
1093 oprintf (f,
1094 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1095 oprintf (f, "};\n");
1098 /* Handle `special("rtx_def")'. This is a special case for field
1099 `fld' of struct rtx_def, which is an array of unions whose values
1100 are based in a complex way on the type of RTL. */
1102 static type_p
1103 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1105 pair_p flds = NULL;
1106 options_p nodot;
1107 int i;
1108 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1109 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1111 if (t->kind != TYPE_UNION)
1113 error_at_line (&lexer_line,
1114 "special `rtx_def' must be applied to a union");
1115 return &string_type;
1118 nodot = create_string_option (NULL, "dot", "");
1120 rtx_tp = create_pointer (find_structure ("rtx_def", TYPE_STRUCT));
1121 rtvec_tp = create_pointer (find_structure ("rtvec_def", TYPE_STRUCT));
1122 tree_tp = create_pointer (find_structure ("tree_node", TYPE_UNION));
1123 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", TYPE_STRUCT));
1124 reg_attrs_tp =
1125 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT));
1126 basic_block_tp =
1127 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT));
1128 constant_tp =
1129 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT));
1130 scalar_tp = &scalar_nonchar; /* rtunion int */
1133 pair_p note_flds = NULL;
1134 int c;
1136 for (c = 0; c <= NOTE_INSN_MAX; c++)
1138 switch (c)
1140 case NOTE_INSN_MAX:
1141 case NOTE_INSN_DELETED_LABEL:
1142 case NOTE_INSN_DELETED_DEBUG_LABEL:
1143 note_flds = create_field (note_flds, &string_type, "rt_str");
1144 break;
1146 case NOTE_INSN_BLOCK_BEG:
1147 case NOTE_INSN_BLOCK_END:
1148 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1149 break;
1151 case NOTE_INSN_VAR_LOCATION:
1152 case NOTE_INSN_CALL_ARG_LOCATION:
1153 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1154 break;
1156 default:
1157 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1158 break;
1160 /* NOTE_INSN_MAX is used as the default field for line
1161 number notes. */
1162 if (c == NOTE_INSN_MAX)
1163 note_flds->opt =
1164 create_string_option (nodot, "default", "");
1165 else
1166 note_flds->opt =
1167 create_string_option (nodot, "tag", note_insn_name[c]);
1169 note_union_tp = new_structure ("rtx_def_note_subunion", TYPE_UNION,
1170 &lexer_line, note_flds, NULL);
1172 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1174 pair_p sym_flds;
1175 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1176 sym_flds->opt = create_string_option (nodot, "default", "");
1177 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1178 sym_flds->opt = create_string_option (nodot, "tag", "1");
1179 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", TYPE_UNION,
1180 &lexer_line, sym_flds, NULL);
1182 for (i = 0; i < NUM_RTX_CODE; i++)
1184 pair_p subfields = NULL;
1185 size_t aindex, nmindex;
1186 const char *sname;
1187 type_p substruct;
1188 char *ftag;
1190 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1192 type_p t;
1193 const char *subname;
1195 switch (rtx_format[i][aindex])
1197 case '*':
1198 case 'i':
1199 case 'n':
1200 case 'w':
1201 t = scalar_tp;
1202 subname = "rt_int";
1203 break;
1205 case '0':
1206 if (i == MEM && aindex == 1)
1207 t = mem_attrs_tp, subname = "rt_mem";
1208 else if (i == JUMP_INSN && aindex == 8)
1209 t = rtx_tp, subname = "rt_rtx";
1210 else if (i == CODE_LABEL && aindex == 5)
1211 t = scalar_tp, subname = "rt_int";
1212 else if (i == CODE_LABEL && aindex == 4)
1213 t = rtx_tp, subname = "rt_rtx";
1214 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1215 t = rtx_tp, subname = "rt_rtx";
1216 else if (i == NOTE && aindex == 4)
1217 t = note_union_tp, subname = "";
1218 else if (i == NOTE && aindex == 5)
1219 t = scalar_tp, subname = "rt_int";
1220 else if (i == NOTE && aindex >= 7)
1221 t = scalar_tp, subname = "rt_int";
1222 else if (i == ADDR_DIFF_VEC && aindex == 4)
1223 t = scalar_tp, subname = "rt_int";
1224 else if (i == VALUE && aindex == 0)
1225 t = scalar_tp, subname = "rt_int";
1226 else if (i == DEBUG_EXPR && aindex == 0)
1227 t = tree_tp, subname = "rt_tree";
1228 else if (i == REG && aindex == 1)
1229 t = scalar_tp, subname = "rt_int";
1230 else if (i == REG && aindex == 2)
1231 t = reg_attrs_tp, subname = "rt_reg";
1232 else if (i == SCRATCH && aindex == 0)
1233 t = scalar_tp, subname = "rt_int";
1234 else if (i == SYMBOL_REF && aindex == 1)
1235 t = scalar_tp, subname = "rt_int";
1236 else if (i == SYMBOL_REF && aindex == 2)
1237 t = symbol_union_tp, subname = "";
1238 else if (i == JUMP_TABLE_DATA && aindex >= 5)
1239 t = scalar_tp, subname = "rt_int";
1240 else if (i == BARRIER && aindex >= 3)
1241 t = scalar_tp, subname = "rt_int";
1242 else if (i == ENTRY_VALUE && aindex == 0)
1243 t = rtx_tp, subname = "rt_rtx";
1244 else
1246 error_at_line
1247 (&lexer_line,
1248 "rtx type `%s' has `0' in position %lu, can't handle",
1249 rtx_name[i], (unsigned long) aindex);
1250 t = &string_type;
1251 subname = "rt_int";
1253 break;
1255 case 's':
1256 case 'S':
1257 case 'T':
1258 t = &string_type;
1259 subname = "rt_str";
1260 break;
1262 case 'e':
1263 case 'u':
1264 t = rtx_tp;
1265 subname = "rt_rtx";
1266 break;
1268 case 'E':
1269 case 'V':
1270 t = rtvec_tp;
1271 subname = "rt_rtvec";
1272 break;
1274 case 't':
1275 t = tree_tp;
1276 subname = "rt_tree";
1277 break;
1279 case 'B':
1280 t = basic_block_tp;
1281 subname = "rt_bb";
1282 break;
1284 default:
1285 error_at_line
1286 (&lexer_line,
1287 "rtx type `%s' has `%c' in position %lu, can't handle",
1288 rtx_name[i], rtx_format[i][aindex],
1289 (unsigned long) aindex);
1290 t = &string_type;
1291 subname = "rt_int";
1292 break;
1295 subfields = create_field (subfields, t,
1296 xasprintf (".fld[%lu].%s",
1297 (unsigned long) aindex,
1298 subname));
1299 subfields->opt = nodot;
1300 if (t == note_union_tp)
1301 subfields->opt =
1302 create_string_option (subfields->opt, "desc",
1303 "NOTE_KIND (&%0)");
1304 if (t == symbol_union_tp)
1305 subfields->opt =
1306 create_string_option (subfields->opt, "desc",
1307 "CONSTANT_POOL_ADDRESS_P (&%0)");
1310 if (i == SYMBOL_REF)
1312 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1313 holds. */
1314 type_p field_tp = find_structure ("block_symbol", TYPE_STRUCT);
1315 subfields
1316 = create_optional_field (subfields, field_tp, "block_sym",
1317 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1320 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1321 substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
1322 NULL);
1324 ftag = xstrdup (rtx_name[i]);
1325 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1326 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1327 flds = create_field (flds, substruct, "");
1328 flds->opt = create_string_option (nodot, "tag", ftag);
1330 return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
1331 nodot);
1334 /* Handle `special("tree_exp")'. This is a special case for
1335 field `operands' of struct tree_exp, which although it claims to contain
1336 pointers to trees, actually sometimes contains pointers to RTL too.
1337 Passed T, the old type of the field, and OPT its options. Returns
1338 a new type for the field. */
1340 static type_p
1341 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1343 pair_p flds;
1344 options_p nodot;
1346 if (t->kind != TYPE_ARRAY)
1348 error_at_line (&lexer_line,
1349 "special `tree_exp' must be applied to an array");
1350 return &string_type;
1353 nodot = create_string_option (NULL, "dot", "");
1355 flds = create_field (NULL, t, "");
1356 flds->opt = create_string_option (nodot, "length",
1357 "TREE_OPERAND_LENGTH ((tree) &%0)");
1358 flds->opt = create_string_option (flds->opt, "default", "");
1360 return new_structure ("tree_exp_subunion", TYPE_UNION, &lexer_line, flds,
1361 nodot);
1364 /* Perform any special processing on a type T, about to become the type
1365 of a field. Return the appropriate type for the field.
1366 At present:
1367 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1368 - Similarly for arrays of pointer-to-char;
1369 - Converts structures for which a parameter is provided to
1370 TYPE_PARAM_STRUCT;
1371 - Handles "special" options.
1374 type_p
1375 adjust_field_type (type_p t, options_p opt)
1377 int length_p = 0;
1378 const int pointer_p = t->kind == TYPE_POINTER;
1379 type_p params[NUM_PARAM];
1380 int params_p = 0;
1381 int i;
1383 for (i = 0; i < NUM_PARAM; i++)
1384 params[i] = NULL;
1386 for (; opt; opt = opt->next)
1387 if (strcmp (opt->name, "length") == 0)
1389 if (length_p)
1390 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1391 if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
1393 error_at_line (&lexer_line,
1394 "option `%s' may not be applied to "
1395 "arrays of atomic types", opt->name);
1397 length_p = 1;
1399 else if ((strcmp (opt->name, "param_is") == 0
1400 || (strncmp (opt->name, "param", 5) == 0
1401 && ISDIGIT (opt->name[5])
1402 && strcmp (opt->name + 6, "_is") == 0))
1403 && opt->kind == OPTION_TYPE)
1405 int num = ISDIGIT (opt->name[5]) ? opt->name[5] - '0' : 0;
1407 if (!union_or_struct_p (t)
1408 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
1410 error_at_line (&lexer_line,
1411 "option `%s' may only be applied to structures or structure pointers",
1412 opt->name);
1413 return t;
1416 params_p = 1;
1417 if (params[num] != NULL)
1418 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1419 if (!ISDIGIT (opt->name[5]))
1420 params[num] = create_pointer (opt->info.type);
1421 else
1422 params[num] = opt->info.type;
1424 else if (strcmp (opt->name, "special") == 0
1425 && opt->kind == OPTION_STRING)
1427 const char *special_name = opt->info.string;
1428 if (strcmp (special_name, "tree_exp") == 0)
1429 t = adjust_field_tree_exp (t, opt);
1430 else if (strcmp (special_name, "rtx_def") == 0)
1431 t = adjust_field_rtx_def (t, opt);
1432 else
1433 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1436 if (params_p)
1438 type_p realt;
1440 if (pointer_p)
1441 t = t->u.p;
1442 realt = find_param_structure (t, params);
1443 t = pointer_p ? create_pointer (realt) : realt;
1446 if (!length_p
1447 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1448 return &string_type;
1449 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1450 && t->u.a.p->u.p->kind == TYPE_SCALAR
1451 && t->u.a.p->u.p->u.scalar_is_char)
1452 return create_array (&string_type, t->u.a.len);
1454 return t;
1458 static void set_gc_used_type (type_p, enum gc_used_enum, type_p *,
1459 bool = false);
1460 static void set_gc_used (pair_p);
1462 /* Handle OPT for set_gc_used_type. */
1464 static void
1465 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1466 int *pass_param, int *length, int *skip,
1467 type_p *nested_ptr)
1469 options_p o;
1470 for (o = opt; o; o = o->next)
1471 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1472 && o->kind == OPTION_TYPE)
1473 set_gc_used_type (o->info.type,
1474 GC_POINTED_TO, NULL);
1475 else if (strcmp (o->name, "maybe_undef") == 0)
1476 *maybe_undef = 1;
1477 else if (strcmp (o->name, "use_params") == 0)
1478 *pass_param = 1;
1479 else if (strcmp (o->name, "length") == 0)
1480 *length = 1;
1481 else if (strcmp (o->name, "skip") == 0)
1482 *skip = 1;
1483 else if (strcmp (o->name, "nested_ptr") == 0
1484 && o->kind == OPTION_NESTED)
1485 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1489 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1491 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1492 are set to GC_UNUSED. Otherwise, an error is emitted for
1493 TYPE_UNDEFINED types. This is used to support user-defined
1494 template types with non-type arguments.
1496 For instance, when we parse a template type with enum arguments
1497 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1498 artificial fields for 'MyType', one for 'AnotherType', the other
1499 one for 'EnumValue'.
1501 At the time that we parse this type we don't know that 'EnumValue'
1502 is really an enum value, so the parser creates a TYPE_UNDEFINED
1503 type for it. Since 'EnumValue' is never resolved to a known
1504 structure, it will stay with TYPE_UNDEFINED.
1506 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1507 'EnumValue'. Generating marking code for it would cause
1508 compilation failures since the marking routines assumes that
1509 'EnumValue' is a type. */
1511 static void
1512 set_gc_used_type (type_p t, enum gc_used_enum level, type_p param[NUM_PARAM],
1513 bool allow_undefined_types)
1515 if (t->gc_used >= level)
1516 return;
1518 t->gc_used = level;
1520 switch (t->kind)
1522 case TYPE_STRUCT:
1523 case TYPE_UNION:
1524 case TYPE_USER_STRUCT:
1526 pair_p f;
1527 int dummy;
1528 type_p dummy2;
1529 bool allow_undefined_field_types = (t->kind == TYPE_USER_STRUCT);
1531 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
1532 &dummy2);
1534 for (f = t->u.s.fields; f; f = f->next)
1536 int maybe_undef = 0;
1537 int pass_param = 0;
1538 int length = 0;
1539 int skip = 0;
1540 type_p nested_ptr = NULL;
1541 process_gc_options (f->opt, level, &maybe_undef, &pass_param,
1542 &length, &skip, &nested_ptr);
1544 if (nested_ptr && f->type->kind == TYPE_POINTER)
1545 set_gc_used_type (nested_ptr, GC_POINTED_TO,
1546 pass_param ? param : NULL);
1547 else if (length && f->type->kind == TYPE_POINTER)
1548 set_gc_used_type (f->type->u.p, GC_USED, NULL);
1549 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1550 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO, NULL);
1551 else if (pass_param && f->type->kind == TYPE_POINTER && param)
1552 set_gc_used_type (find_param_structure (f->type->u.p, param),
1553 GC_POINTED_TO, NULL);
1554 else if (skip)
1555 ; /* target type is not used through this field */
1556 else
1557 set_gc_used_type (f->type, GC_USED, pass_param ? param : NULL,
1558 allow_undefined_field_types);
1560 break;
1563 case TYPE_UNDEFINED:
1564 if (level > GC_UNUSED)
1566 if (!allow_undefined_types)
1567 error_at_line (&t->u.s.line, "undefined type `%s'", t->u.s.tag);
1568 t->gc_used = GC_UNUSED;
1570 break;
1572 case TYPE_POINTER:
1573 set_gc_used_type (t->u.p, GC_POINTED_TO, NULL);
1574 break;
1576 case TYPE_ARRAY:
1577 set_gc_used_type (t->u.a.p, GC_USED, param);
1578 break;
1580 case TYPE_LANG_STRUCT:
1581 for (t = t->u.s.lang_struct; t; t = t->next)
1582 set_gc_used_type (t, level, param);
1583 break;
1585 case TYPE_PARAM_STRUCT:
1587 int i;
1588 for (i = 0; i < NUM_PARAM; i++)
1589 if (t->u.param_struct.param[i] != 0)
1590 set_gc_used_type (t->u.param_struct.param[i], GC_USED, NULL);
1592 if (t->u.param_struct.stru->gc_used == GC_POINTED_TO)
1593 level = GC_POINTED_TO;
1594 else
1595 level = GC_USED;
1596 t->u.param_struct.stru->gc_used = GC_UNUSED;
1597 set_gc_used_type (t->u.param_struct.stru, level,
1598 t->u.param_struct.param);
1599 break;
1601 default:
1602 break;
1606 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1608 static void
1609 set_gc_used (pair_p variables)
1611 int nbvars = 0;
1612 pair_p p;
1613 for (p = variables; p; p = p->next)
1615 set_gc_used_type (p->type, GC_USED, NULL);
1616 nbvars++;
1618 if (verbosity_level >= 2)
1619 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1622 /* File mapping routines. For each input file, there is one output .c file
1623 (but some output files have many input files), and there is one .h file
1624 for the whole build. */
1626 /* Output file handling. */
1628 /* Create and return an outf_p for a new file for NAME, to be called
1629 ONAME. */
1631 static outf_p
1632 create_file (const char *name, const char *oname)
1634 static const char *const hdr[] = {
1635 " Copyright (C) 2004-2013 Free Software Foundation, Inc.\n",
1636 "\n",
1637 "This file is part of GCC.\n",
1638 "\n",
1639 "GCC is free software; you can redistribute it and/or modify it under\n",
1640 "the terms of the GNU General Public License as published by the Free\n",
1641 "Software Foundation; either version 3, or (at your option) any later\n",
1642 "version.\n",
1643 "\n",
1644 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1645 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1646 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1647 "for more details.\n",
1648 "\n",
1649 "You should have received a copy of the GNU General Public License\n",
1650 "along with GCC; see the file COPYING3. If not see\n",
1651 "<http://www.gnu.org/licenses/>. */\n",
1652 "\n",
1653 "/* This file is machine generated. Do not edit. */\n"
1655 outf_p f;
1656 size_t i;
1658 gcc_assert (name != NULL);
1659 gcc_assert (oname != NULL);
1660 f = XCNEW (struct outf);
1661 f->next = output_files;
1662 f->name = oname;
1663 output_files = f;
1665 oprintf (f, "/* Type information for %s.\n", name);
1666 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1667 oprintf (f, "%s", hdr[i]);
1668 return f;
1671 /* Print, like fprintf, to O.
1672 N.B. You might think this could be implemented more efficiently
1673 with vsnprintf(). Unfortunately, there are C libraries that
1674 provide that function but without the C99 semantics for its return
1675 value, making it impossible to know how much space is required. */
1676 void
1677 oprintf (outf_p o, const char *format, ...)
1679 char *s;
1680 size_t slength;
1681 va_list ap;
1683 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1684 in that case. */
1685 if (!o)
1686 return;
1688 va_start (ap, format);
1689 slength = vasprintf (&s, format, ap);
1690 if (s == NULL || (int) slength < 0)
1691 fatal ("out of memory");
1692 va_end (ap);
1694 if (o->bufused + slength > o->buflength)
1696 size_t new_len = o->buflength;
1697 if (new_len == 0)
1698 new_len = 1024;
1701 new_len *= 2;
1703 while (o->bufused + slength >= new_len);
1704 o->buf = XRESIZEVEC (char, o->buf, new_len);
1705 o->buflength = new_len;
1707 memcpy (o->buf + o->bufused, s, slength);
1708 o->bufused += slength;
1709 free (s);
1712 /* Open the global header file and the language-specific header files. */
1714 static void
1715 open_base_files (void)
1717 size_t i;
1719 if (nb_plugin_files > 0 && plugin_files)
1720 return;
1722 header_file = create_file ("GCC", "gtype-desc.h");
1724 base_files = XNEWVEC (outf_p, num_lang_dirs);
1726 for (i = 0; i < num_lang_dirs; i++)
1727 base_files[i] = create_file (lang_dir_names[i],
1728 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1730 /* gtype-desc.c is a little special, so we create it here. */
1732 /* The order of files here matters very much. */
1733 static const char *const ifiles[] = {
1734 "config.h", "system.h", "coretypes.h", "tm.h",
1735 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1736 "tree.h", "rtl.h", "function.h", "insn-config.h", "expr.h",
1737 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1738 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1739 "tree-flow.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1740 "except.h", "output.h", "gimple.h", "cfgloop.h",
1741 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h",
1742 "ipa-inline.h", "dwarf2out.h", NULL
1744 const char *const *ifp;
1745 outf_p gtype_desc_c;
1747 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1748 for (ifp = ifiles; *ifp; ifp++)
1749 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1751 /* Make sure we handle "cfun" specially. */
1752 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1753 oprintf (gtype_desc_c, "#undef cfun\n");
1755 oprintf (gtype_desc_c,
1756 "\n"
1757 "/* Types with a \"gcc::\" namespace have it stripped\n"
1758 " during gengtype parsing. Provide a \"using\" directive\n"
1759 " to ensure that the fully-qualified types are found. */\n"
1760 "using namespace gcc;\n");
1764 /* For INPF an input file, return the real basename of INPF, with all
1765 the directory components skipped. */
1767 static const char *
1768 get_file_realbasename (const input_file *inpf)
1770 return lbasename (get_input_file_name (inpf));
1773 /* For INPF a filename, return the relative path to INPF from
1774 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1776 const char *
1777 get_file_srcdir_relative_path (const input_file *inpf)
1779 const char *f = get_input_file_name (inpf);
1780 if (strlen (f) > srcdir_len
1781 && IS_DIR_SEPARATOR (f[srcdir_len])
1782 && strncmp (f, srcdir, srcdir_len) == 0)
1783 return f + srcdir_len + 1;
1784 else
1785 return NULL;
1788 /* For INPF an input_file, return the relative path to INPF from
1789 $(srcdir) if the latter is a prefix in INPF, or the real basename
1790 of INPF otherwise. */
1792 static const char *
1793 get_file_basename (const input_file *inpf)
1795 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1797 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1800 /* For F a filename, return the lang_dir_names relative index of the language
1801 directory that is a prefix in F, if any, -1 otherwise. */
1803 static int
1804 get_prefix_langdir_index (const char *f)
1806 size_t f_len = strlen (f);
1807 size_t lang_index;
1809 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1811 const char *langdir = lang_dir_names[lang_index];
1812 size_t langdir_len = strlen (langdir);
1814 if (f_len > langdir_len
1815 && IS_DIR_SEPARATOR (f[langdir_len])
1816 && memcmp (f, langdir, langdir_len) == 0)
1817 return lang_index;
1820 return -1;
1823 /* For INPF an input file, return the name of language directory where
1824 F is located, if any, NULL otherwise. */
1826 static const char *
1827 get_file_langdir (const input_file *inpf)
1829 /* Get the relative path to INPF from $(srcdir) and find the
1830 language by comparing the prefix with language directory names.
1831 If INPF is not even srcdir relative, no point in looking
1832 further. */
1834 int lang_index;
1835 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1836 const char *r;
1838 if (!srcdir_relative_path)
1839 return NULL;
1841 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1842 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1843 r = "c-family";
1844 else if (lang_index >= 0)
1845 r = lang_dir_names[lang_index];
1846 else
1847 r = NULL;
1849 return r;
1852 /* The gt- output file name for INPF. */
1854 static const char *
1855 get_file_gtfilename (const input_file *inpf)
1857 /* Cook up an initial version of the gt- file name from the file real
1858 basename and the language name, if any. */
1860 const char *basename = get_file_realbasename (inpf);
1861 const char *langdir = get_file_langdir (inpf);
1863 char *result =
1864 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1865 : xasprintf ("gt-%s", basename));
1867 /* Then replace all non alphanumerics characters by '-' and change the
1868 extension to ".h". We expect the input filename extension was at least
1869 one character long. */
1871 char *s = result;
1873 for (; *s != '.'; s++)
1874 if (!ISALNUM (*s) && *s != '-')
1875 *s = '-';
1877 memcpy (s, ".h", sizeof (".h"));
1879 return result;
1882 /* Each input_file has its associated output file outf_p. The
1883 association is computed by the function
1884 get_output_file_with_visibility. The associated file is cached
1885 inside input_file in its inpoutf field, so is really computed only
1886 once. Associated output file paths (i.e. output_name-s) are
1887 computed by a rule based regexp machinery, using the files_rules
1888 array of struct file_rule_st. A for_name is also computed, giving
1889 the source file name for which the output_file is generated; it is
1890 often the last component of the input_file path. */
1894 Regexpr machinery to compute the output_name and for_name-s of each
1895 input_file. We have a sequence of file rules which gives the POSIX
1896 extended regular expression to match an input file path, and two
1897 transformed strings for the corresponding output_name and the
1898 corresponding for_name. The transformed string contain dollars: $0
1899 is replaced by the entire match, $1 is replaced by the substring
1900 matching the first parenthesis in the regexp, etc. And $$ is replaced
1901 by a single verbatim dollar. The rule order is important. The
1902 general case is last, and the particular cases should come before.
1903 An action routine can, when needed, update the out_name & for_name
1904 and/or return the appropriate output file. It is invoked only when a
1905 rule is triggered. When a rule is triggered, the output_name and
1906 for_name are computed using their transform string in while $$, $0,
1907 $1, ... are suitably replaced. If there is an action, it is called.
1908 In some few cases, the action can directly return the outf_p, but
1909 usually it just updates the output_name and for_name so should free
1910 them before replacing them. The get_output_file_with_visibility
1911 function creates an outf_p only once per each output_name, so it
1912 scans the output_files list for previously seen output file names.
1915 /* Signature of actions in file rules. */
1916 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
1919 struct file_rule_st {
1920 const char* frul_srcexpr; /* Source string for regexp. */
1921 int frul_rflags; /* Flags passed to regcomp, usually
1922 * REG_EXTENDED. */
1923 regex_t* frul_re; /* Compiled regular expression
1924 obtained by regcomp. */
1925 const char* frul_tr_out; /* Transformation string for making
1926 * the output_name, with $1 ... $9 for
1927 * subpatterns and $0 for the whole
1928 * matched filename. */
1929 const char* frul_tr_for; /* Tranformation string for making the
1930 for_name. */
1931 frul_actionrout_t* frul_action; /* The action, if non null, is
1932 * called once the rule matches, on
1933 * the transformed out_name &
1934 * for_name. It could change them
1935 * and/or give the output file. */
1938 /* File rule action handling *.h files. */
1939 static outf_p header_dot_h_frul (input_file*, char**, char**);
1941 /* File rule action handling *.c files. */
1942 static outf_p source_dot_c_frul (input_file*, char**, char**);
1944 #define NULL_REGEX (regex_t*)0
1946 /* The prefix in our regexp-s matching the directory. */
1947 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1949 #define NULL_FRULACT (frul_actionrout_t*)0
1951 /* The array of our rules governing file name generation. Rules order
1952 matters, so change with extreme care! */
1954 struct file_rule_st files_rules[] = {
1955 /* The general rule assumes that files in subdirectories belong to a
1956 particular front-end, and files not in subdirectories are shared.
1957 The following rules deal with exceptions - files that are in
1958 subdirectories and yet are shared, and files that are top-level,
1959 but are not shared. */
1961 /* the c-family/ source directory is special. */
1962 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
1963 REG_EXTENDED, NULL_REGEX,
1964 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
1966 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
1967 REG_EXTENDED, NULL_REGEX,
1968 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
1970 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
1971 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
1972 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1974 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
1975 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1977 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
1978 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
1979 REG_EXTENDED, NULL_REGEX,
1980 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
1982 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
1983 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
1984 REG_EXTENDED, NULL_REGEX,
1985 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
1987 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
1988 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
1989 REG_EXTENDED, NULL_REGEX,
1990 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
1992 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
1993 { DIR_PREFIX_REGEX "cp/parser\\.h$",
1994 REG_EXTENDED, NULL_REGEX,
1995 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
1997 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
1998 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
1999 REG_EXTENDED, NULL_REGEX,
2000 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
2002 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
2003 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
2004 REG_EXTENDED, NULL_REGEX,
2005 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
2007 /* General cases. For header *.h and source *.c files, we need
2008 * special actions to handle the language. */
2010 /* Source *.c files are using get_file_gtfilename to compute their
2011 output_name and get_file_basename to compute their for_name
2012 through the source_dot_c_frul action. */
2013 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
2014 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
2015 /* Common header files get "gtype-desc.c" as their output_name,
2016 * while language specific header files are handled specially. So
2017 * we need the header_dot_h_frul action. */
2018 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
2019 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
2021 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
2022 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
2024 /* Mandatory null last entry signaling end of rules. */
2025 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
2028 /* Special file rules action for handling *.h header files. It gives
2029 "gtype-desc.c" for common headers and corresponding output
2030 files for language-specific header files. */
2031 static outf_p
2032 header_dot_h_frul (input_file* inpf, char**poutname,
2033 char**pforname ATTRIBUTE_UNUSED)
2035 const char *basename = 0;
2036 int lang_index = 0;
2037 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2038 (void*) inpf, get_input_file_name (inpf),
2039 *poutname, *pforname);
2040 basename = get_file_basename (inpf);
2041 lang_index = get_prefix_langdir_index (basename);
2042 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
2044 if (lang_index >= 0)
2046 /* The header is language specific. Given output_name &
2047 for_name remains unchanged. The base_files array gives the
2048 outf_p. */
2049 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2050 (void*) base_files[lang_index],
2051 (base_files[lang_index])->name);
2052 return base_files[lang_index];
2054 else
2056 /* The header is common to all front-end languages. So
2057 output_name is "gtype-desc.c" file. The calling function
2058 get_output_file_with_visibility will find its outf_p. */
2059 free (*poutname);
2060 *poutname = xstrdup ("gtype-desc.c");
2061 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2062 get_input_file_name (inpf));
2063 return NULL;
2068 /* Special file rules action for handling *.c source files using
2069 * get_file_gtfilename to compute their output_name and
2070 * get_file_basename to compute their for_name. The output_name is
2071 * gt-<LANG>-<BASE>.h for language specific source files, and
2072 * gt-<BASE>.h for common source files. */
2073 static outf_p
2074 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
2076 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
2077 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
2078 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2079 (void*) inpf, get_input_file_name (inpf),
2080 *poutname, *pforname);
2081 DBGPRINTF ("newoutname %s", newoutname);
2082 DBGPRINTF ("newbasename %s", newbasename);
2083 free (*poutname);
2084 free (*pforname);
2085 *poutname = newoutname;
2086 *pforname = newbasename;
2087 return NULL;
2090 /* Utility function for get_output_file_with_visibility which returns
2091 * a malloc-ed substituted string using TRS on matching of the FILNAM
2092 * file name, using the PMATCH array. */
2093 static char*
2094 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
2095 const char *trs)
2097 struct obstack str_obstack;
2098 char *str = NULL;
2099 char *rawstr = NULL;
2100 const char *pt = NULL;
2101 DBGPRINTF ("filnam %s", filnam);
2102 obstack_init (&str_obstack);
2103 for (pt = trs; *pt; pt++) {
2104 char c = *pt;
2105 if (c == '$')
2107 if (pt[1] == '$')
2109 /* A double dollar $$ is substituted by a single verbatim
2110 dollar, but who really uses dollar signs in file
2111 paths? */
2112 obstack_1grow (&str_obstack, '$');
2114 else if (ISDIGIT (pt[1]))
2116 /* Handle $0 $1 ... $9 by appropriate substitution. */
2117 int dolnum = pt[1] - '0';
2118 int so = pmatch[dolnum].rm_so;
2119 int eo = pmatch[dolnum].rm_eo;
2120 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2121 if (so>=0 && eo>=so)
2122 obstack_grow (&str_obstack, filnam + so, eo - so);
2124 else
2126 /* This can happen only when files_rules is buggy! */
2127 gcc_unreachable();
2129 /* Always skip the character after the dollar. */
2130 pt++;
2132 else
2133 obstack_1grow (&str_obstack, c);
2135 obstack_1grow (&str_obstack, '\0');
2136 rawstr = XOBFINISH (&str_obstack, char *);
2137 str = xstrdup (rawstr);
2138 obstack_free (&str_obstack, NULL);
2139 DBGPRINTF ("matched replacement %s", str);
2140 rawstr = NULL;
2141 return str;
2145 /* An output file, suitable for definitions, that can see declarations
2146 made in INPF and is linked into every language that uses INPF.
2147 Since the result is cached inside INPF, that argument cannot be
2148 declared constant, but is "almost" constant. */
2150 outf_p
2151 get_output_file_with_visibility (input_file *inpf)
2153 outf_p r;
2154 char *for_name = NULL;
2155 char *output_name = NULL;
2156 const char* inpfname;
2158 /* This can happen when we need a file with visibility on a
2159 structure that we've never seen. We have to just hope that it's
2160 globally visible. */
2161 if (inpf == NULL)
2162 inpf = system_h_file;
2164 /* The result is cached in INPF, so return it if already known. */
2165 if (inpf->inpoutf)
2166 return inpf->inpoutf;
2168 /* In plugin mode, return NULL unless the input_file is one of the
2169 plugin_files. */
2170 if (plugin_files)
2172 size_t i;
2173 for (i = 0; i < nb_plugin_files; i++)
2174 if (inpf == plugin_files[i])
2176 inpf->inpoutf = plugin_output;
2177 return plugin_output;
2180 return NULL;
2183 inpfname = get_input_file_name (inpf);
2185 /* Try each rule in sequence in files_rules until one is triggered. */
2187 int rulix = 0;
2188 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2189 (void*) inpf, inpfname);
2191 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2193 DBGPRINTF ("rulix#%d srcexpr %s",
2194 rulix, files_rules[rulix].frul_srcexpr);
2196 if (!files_rules[rulix].frul_re)
2198 /* Compile the regexpr lazily. */
2199 int err = 0;
2200 files_rules[rulix].frul_re = XCNEW (regex_t);
2201 err = regcomp (files_rules[rulix].frul_re,
2202 files_rules[rulix].frul_srcexpr,
2203 files_rules[rulix].frul_rflags);
2204 if (err)
2206 /* The regular expression compilation fails only when
2207 file_rules is buggy. */
2208 gcc_unreachable ();
2212 output_name = NULL;
2213 for_name = NULL;
2215 /* Match the regexpr and trigger the rule if matched. */
2217 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2218 $3, ... $9. */
2219 regmatch_t pmatch[10];
2220 memset (pmatch, 0, sizeof (pmatch));
2221 if (!regexec (files_rules[rulix].frul_re,
2222 inpfname, 10, pmatch, 0))
2224 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2225 (void*) inpf, inpfname, rulix,
2226 files_rules[rulix].frul_srcexpr);
2227 for_name =
2228 matching_file_name_substitute (inpfname, pmatch,
2229 files_rules[rulix].frul_tr_for);
2230 DBGPRINTF ("for_name %s", for_name);
2231 output_name =
2232 matching_file_name_substitute (inpfname, pmatch,
2233 files_rules[rulix].frul_tr_out);
2234 DBGPRINTF ("output_name %s", output_name);
2235 if (files_rules[rulix].frul_action)
2237 /* Invoke our action routine. */
2238 outf_p of = NULL;
2239 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2240 rulix, output_name, for_name);
2241 of =
2242 (files_rules[rulix].frul_action) (inpf,
2243 &output_name, &for_name);
2244 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2245 rulix, (void*)of, output_name, for_name);
2246 /* If the action routine returned something, give it back
2247 immediately and cache it in inpf. */
2248 if (of)
2250 inpf->inpoutf = of;
2251 return of;
2254 /* The rule matched, and had no action, or that action did
2255 not return any output file but could have changed the
2256 output_name or for_name. We break out of the loop on the
2257 files_rules. */
2258 break;
2260 else
2262 /* The regexpr did not match. */
2263 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2264 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2265 continue;
2270 if (!output_name || !for_name)
2272 /* This is impossible, and could only happen if the files_rules is
2273 incomplete or buggy. */
2274 gcc_unreachable ();
2277 /* Look through to see if we've ever seen this output filename
2278 before. If found, cache the result in inpf. */
2279 for (r = output_files; r; r = r->next)
2280 if (filename_cmp (r->name, output_name) == 0)
2282 inpf->inpoutf = r;
2283 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2284 output_name, for_name);
2285 return r;
2288 /* If not found, create it, and cache it in inpf. */
2289 r = create_file (for_name, output_name);
2291 gcc_assert (r && r->name);
2292 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2293 output_name, for_name);
2294 inpf->inpoutf = r;
2295 return r;
2300 /* The name of an output file, suitable for definitions, that can see
2301 declarations made in INPF and is linked into every language that
2302 uses INPF. */
2304 const char *
2305 get_output_file_name (input_file* inpf)
2307 outf_p o = get_output_file_with_visibility (inpf);
2308 if (o)
2309 return o->name;
2310 return NULL;
2313 /* Check if existing file is equal to the in memory buffer. */
2315 static bool
2316 is_file_equal (outf_p of)
2318 FILE *newfile = fopen (of->name, "r");
2319 size_t i;
2320 bool equal;
2321 if (newfile == NULL)
2322 return false;
2324 equal = true;
2325 for (i = 0; i < of->bufused; i++)
2327 int ch;
2328 ch = fgetc (newfile);
2329 if (ch == EOF || ch != (unsigned char) of->buf[i])
2331 equal = false;
2332 break;
2335 fclose (newfile);
2336 return equal;
2339 /* Copy the output to its final destination,
2340 but don't unnecessarily change modification times. */
2342 static void
2343 close_output_files (void)
2345 int nbwrittenfiles = 0;
2346 outf_p of;
2348 for (of = output_files; of; of = of->next)
2350 if (!is_file_equal (of))
2352 FILE *newfile = NULL;
2353 char *backupname = NULL;
2354 /* Back up the old version of the output file gt-FOO.c as
2355 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2356 if (backup_dir)
2358 backupname = concat (backup_dir, "/",
2359 lbasename (of->name), "~", NULL);
2360 if (!access (of->name, F_OK) && rename (of->name, backupname))
2361 fatal ("failed to back up %s as %s: %s",
2362 of->name, backupname, xstrerror (errno));
2365 newfile = fopen (of->name, "w");
2366 if (newfile == NULL)
2367 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2368 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2369 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2370 if (fclose (newfile) != 0)
2371 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2372 nbwrittenfiles++;
2373 if (verbosity_level >= 2 && backupname)
2374 printf ("%s wrote #%-3d %s backed-up in %s\n",
2375 progname, nbwrittenfiles, of->name, backupname);
2376 else if (verbosity_level >= 1)
2377 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2378 free (backupname);
2380 else
2382 /* output file remains unchanged. */
2383 if (verbosity_level >= 2)
2384 printf ("%s keep %s\n", progname, of->name);
2386 free (of->buf);
2387 of->buf = NULL;
2388 of->bufused = of->buflength = 0;
2390 if (verbosity_level >= 1)
2391 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2394 struct flist
2396 struct flist *next;
2397 int started_p;
2398 const input_file* file;
2399 outf_p f;
2402 struct walk_type_data;
2404 /* For scalars and strings, given the item in 'val'.
2405 For structures, given a pointer to the item in 'val'.
2406 For misc. pointers, given the item in 'val'.
2408 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2409 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2411 /* Parameters for write_types. */
2413 struct write_types_data
2415 const char *prefix;
2416 const char *param_prefix;
2417 const char *subfield_marker_routine;
2418 const char *marker_routine;
2419 const char *reorder_note_routine;
2420 const char *comment;
2421 int skip_hooks; /* skip hook generation if non zero */
2424 static void output_escaped_param (struct walk_type_data *d,
2425 const char *, const char *);
2426 static void output_mangled_typename (outf_p, const_type_p);
2427 static void walk_type (type_p t, struct walk_type_data *d);
2428 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2429 const struct write_types_data *wtd);
2430 static void write_types_process_field
2431 (type_p f, const struct walk_type_data *d);
2432 static void write_types (outf_p output_header,
2433 type_p structures,
2434 type_p param_structs,
2435 const struct write_types_data *wtd);
2436 static void write_types_local_process_field
2437 (type_p f, const struct walk_type_data *d);
2438 static void write_local_func_for_structure
2439 (const_type_p orig_s, type_p s, type_p *param);
2440 static void write_local (outf_p output_header,
2441 type_p structures, type_p param_structs);
2442 static int contains_scalar_p (type_p t);
2443 static void put_mangled_filename (outf_p, const input_file *);
2444 static void finish_root_table (struct flist *flp, const char *pfx,
2445 const char *tname, const char *lastname,
2446 const char *name);
2447 static void write_root (outf_p, pair_p, type_p, const char *, int,
2448 struct fileloc *, const char *, bool);
2449 static void write_array (outf_p f, pair_p v,
2450 const struct write_types_data *wtd);
2451 static void write_roots (pair_p, bool);
2453 /* Parameters for walk_type. */
2455 struct walk_type_data
2457 process_field_fn process_field;
2458 const void *cookie;
2459 outf_p of;
2460 options_p opt;
2461 const char *val;
2462 const char *prev_val[4];
2463 int indent;
2464 int counter;
2465 const struct fileloc *line;
2466 lang_bitmap bitmap;
2467 type_p *param;
2468 int used_length;
2469 type_p orig_s;
2470 const char *reorder_fn;
2471 bool needs_cast_p;
2472 bool fn_wants_lvalue;
2473 bool in_record_p;
2474 int loopcounter;
2475 bool in_ptr_field;
2476 bool have_this_obj;
2480 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2481 pre-processor identifier to use in a #define directive. This replaces
2482 special characters used in C++ identifiers like '>', '<' and ':' with
2483 '_'.
2485 If no C++ special characters are found in TYPE_NAME, return
2486 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2487 characters replaced with '_'. In this case, the caller is
2488 responsible for freeing the allocated string. */
2490 static const char *
2491 filter_type_name (const char *type_name)
2493 if (strchr (type_name, '<') || strchr (type_name, ':'))
2495 size_t i;
2496 char *s = xstrdup (type_name);
2497 for (i = 0; i < strlen (s); i++)
2498 if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ',')
2499 s[i] = '_';
2500 return s;
2502 else
2503 return type_name;
2507 /* Print a mangled name representing T to OF. */
2509 static void
2510 output_mangled_typename (outf_p of, const_type_p t)
2512 if (t == NULL)
2513 oprintf (of, "Z");
2514 else
2515 switch (t->kind)
2517 case TYPE_NONE:
2518 case TYPE_UNDEFINED:
2519 gcc_unreachable ();
2520 break;
2521 case TYPE_POINTER:
2522 oprintf (of, "P");
2523 output_mangled_typename (of, t->u.p);
2524 break;
2525 case TYPE_SCALAR:
2526 oprintf (of, "I");
2527 break;
2528 case TYPE_STRING:
2529 oprintf (of, "S");
2530 break;
2531 case TYPE_STRUCT:
2532 case TYPE_UNION:
2533 case TYPE_LANG_STRUCT:
2534 case TYPE_USER_STRUCT:
2536 const char *id_for_tag = filter_type_name (t->u.s.tag);
2537 oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2538 id_for_tag);
2539 if (id_for_tag != t->u.s.tag)
2540 free (CONST_CAST(char *, id_for_tag));
2542 break;
2543 case TYPE_PARAM_STRUCT:
2545 int i;
2546 for (i = 0; i < NUM_PARAM; i++)
2547 if (t->u.param_struct.param[i] != NULL)
2548 output_mangled_typename (of, t->u.param_struct.param[i]);
2549 output_mangled_typename (of, t->u.param_struct.stru);
2551 break;
2552 case TYPE_ARRAY:
2553 gcc_unreachable ();
2557 /* Print PARAM to D->OF processing escapes. D->VAL references the
2558 current object, D->PREV_VAL the object containing the current
2559 object, ONAME is the name of the option and D->LINE is used to
2560 print error messages. */
2562 static void
2563 output_escaped_param (struct walk_type_data *d, const char *param,
2564 const char *oname)
2566 const char *p;
2568 for (p = param; *p; p++)
2569 if (*p != '%')
2570 oprintf (d->of, "%c", *p);
2571 else
2572 switch (*++p)
2574 case 'h':
2575 oprintf (d->of, "(%s)", d->prev_val[2]);
2576 break;
2577 case '0':
2578 oprintf (d->of, "(%s)", d->prev_val[0]);
2579 break;
2580 case '1':
2581 oprintf (d->of, "(%s)", d->prev_val[1]);
2582 break;
2583 case 'a':
2585 const char *pp = d->val + strlen (d->val);
2586 while (pp[-1] == ']')
2587 while (*pp != '[')
2588 pp--;
2589 oprintf (d->of, "%s", pp);
2591 break;
2592 default:
2593 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2594 oname, '%', *p);
2599 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2600 which is of type T. Write code to D->OF to constrain execution (at
2601 the point that D->PROCESS_FIELD is called) to the appropriate
2602 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2603 pointers to those objects. D->PREV_VAL lists the objects
2604 containing the current object, D->OPT is a list of options to
2605 apply, D->INDENT is the current indentation level, D->LINE is used
2606 to print error messages, D->BITMAP indicates which languages to
2607 print the structure for, and D->PARAM is the current parameter
2608 (from an enclosing param_is option). */
2610 static void
2611 walk_type (type_p t, struct walk_type_data *d)
2613 const char *length = NULL;
2614 const char *desc = NULL;
2615 int maybe_undef_p = 0;
2616 int use_param_num = -1;
2617 int use_params_p = 0;
2618 int atomic_p = 0;
2619 options_p oo;
2620 const struct nested_ptr_data *nested_ptr_d = NULL;
2622 d->needs_cast_p = false;
2623 for (oo = d->opt; oo; oo = oo->next)
2624 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2625 length = oo->info.string;
2626 else if (strcmp (oo->name, "maybe_undef") == 0)
2627 maybe_undef_p = 1;
2628 else if (strncmp (oo->name, "use_param", 9) == 0
2629 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2630 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2631 else if (strcmp (oo->name, "use_params") == 0)
2632 use_params_p = 1;
2633 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2634 desc = oo->info.string;
2635 else if (strcmp (oo->name, "mark_hook") == 0)
2637 else if (strcmp (oo->name, "nested_ptr") == 0
2638 && oo->kind == OPTION_NESTED)
2639 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2640 else if (strcmp (oo->name, "dot") == 0)
2642 else if (strcmp (oo->name, "tag") == 0)
2644 else if (strcmp (oo->name, "special") == 0)
2646 else if (strcmp (oo->name, "skip") == 0)
2648 else if (strcmp (oo->name, "atomic") == 0)
2649 atomic_p = 1;
2650 else if (strcmp (oo->name, "default") == 0)
2652 else if (strcmp (oo->name, "param_is") == 0)
2654 else if (strncmp (oo->name, "param", 5) == 0
2655 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2657 else if (strcmp (oo->name, "chain_next") == 0)
2659 else if (strcmp (oo->name, "chain_prev") == 0)
2661 else if (strcmp (oo->name, "chain_circular") == 0)
2663 else if (strcmp (oo->name, "reorder") == 0)
2665 else if (strcmp (oo->name, "variable_size") == 0)
2667 else
2668 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2670 if (d->used_length)
2671 length = NULL;
2673 if (use_params_p)
2675 int pointer_p = t->kind == TYPE_POINTER;
2677 if (pointer_p)
2678 t = t->u.p;
2679 if (!union_or_struct_p (t))
2680 error_at_line (d->line, "`use_params' option on unimplemented type");
2681 else
2682 t = find_param_structure (t, d->param);
2683 if (pointer_p)
2684 t = create_pointer (t);
2687 if (use_param_num != -1)
2689 if (d->param != NULL && d->param[use_param_num] != NULL)
2691 type_p nt = d->param[use_param_num];
2693 if (t->kind == TYPE_ARRAY)
2694 nt = create_array (nt, t->u.a.len);
2695 else if (length != NULL && t->kind == TYPE_POINTER)
2696 nt = create_pointer (nt);
2697 d->needs_cast_p = (t->kind != TYPE_POINTER
2698 && (nt->kind == TYPE_POINTER
2699 || nt->kind == TYPE_STRING));
2700 t = nt;
2702 else
2703 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2706 if (maybe_undef_p
2707 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
2709 error_at_line (d->line,
2710 "field `%s' has invalid option `maybe_undef_p'\n",
2711 d->val);
2712 return;
2715 if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2717 error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2718 return;
2721 switch (t->kind)
2723 case TYPE_SCALAR:
2724 case TYPE_STRING:
2725 d->process_field (t, d);
2726 break;
2728 case TYPE_POINTER:
2730 d->in_ptr_field = true;
2731 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2733 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2734 break;
2737 /* If a pointer type is marked as "atomic", we process the
2738 field itself, but we don't walk the data that they point to.
2740 There are two main cases where we walk types: to mark
2741 pointers that are reachable, and to relocate pointers when
2742 writing a PCH file. In both cases, an atomic pointer is
2743 itself marked or relocated, but the memory that it points
2744 to is left untouched. In the case of PCH, that memory will
2745 be read/written unchanged to the PCH file. */
2746 if (atomic_p)
2748 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2749 d->indent += 2;
2750 d->process_field (t, d);
2751 d->indent -= 2;
2752 oprintf (d->of, "%*s}\n", d->indent, "");
2753 break;
2756 if (!length)
2758 if (!union_or_struct_p (t->u.p)
2759 && t->u.p->kind != TYPE_PARAM_STRUCT)
2761 error_at_line (d->line,
2762 "field `%s' is pointer to unimplemented type",
2763 d->val);
2764 break;
2767 if (nested_ptr_d)
2769 const char *oldprevval2 = d->prev_val[2];
2771 if (!union_or_struct_p (nested_ptr_d->type))
2773 error_at_line (d->line,
2774 "field `%s' has invalid "
2775 "option `nested_ptr'\n", d->val);
2776 return;
2779 d->prev_val[2] = d->val;
2780 oprintf (d->of, "%*s{\n", d->indent, "");
2781 d->indent += 2;
2782 d->val = xasprintf ("x%d", d->counter++);
2783 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2784 (nested_ptr_d->type->kind == TYPE_UNION
2785 ? "union" : "struct"),
2786 nested_ptr_d->type->u.s.tag,
2787 d->fn_wants_lvalue ? "" : "const ", d->val);
2788 oprintf (d->of, "%*s", d->indent + 2, "");
2789 output_escaped_param (d, nested_ptr_d->convert_from,
2790 "nested_ptr");
2791 oprintf (d->of, ";\n");
2793 d->process_field (nested_ptr_d->type, d);
2795 if (d->fn_wants_lvalue)
2797 oprintf (d->of, "%*s%s = ", d->indent, "",
2798 d->prev_val[2]);
2799 d->prev_val[2] = d->val;
2800 output_escaped_param (d, nested_ptr_d->convert_to,
2801 "nested_ptr");
2802 oprintf (d->of, ";\n");
2805 d->indent -= 2;
2806 oprintf (d->of, "%*s}\n", d->indent, "");
2807 d->val = d->prev_val[2];
2808 d->prev_val[2] = oldprevval2;
2810 else
2811 d->process_field (t->u.p, d);
2813 else
2815 int loopcounter = d->loopcounter;
2816 const char *oldval = d->val;
2817 const char *oldprevval3 = d->prev_val[3];
2818 char *newval;
2820 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2821 d->indent += 2;
2822 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2823 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2824 "", loopcounter, loopcounter);
2825 if (!d->in_record_p)
2826 output_escaped_param (d, length, "length");
2827 else
2828 oprintf (d->of, "l%d", loopcounter);
2829 if (d->have_this_obj)
2830 /* Try to unswitch loops (see PR53880). */
2831 oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
2832 oprintf (d->of, "); i%d++) {\n", loopcounter);
2833 d->indent += 2;
2834 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2835 d->used_length = 1;
2836 d->prev_val[3] = oldval;
2837 walk_type (t->u.p, d);
2838 free (newval);
2839 d->val = oldval;
2840 d->prev_val[3] = oldprevval3;
2841 d->used_length = 0;
2842 d->indent -= 2;
2843 oprintf (d->of, "%*s}\n", d->indent, "");
2844 d->process_field (t, d);
2845 d->indent -= 2;
2846 oprintf (d->of, "%*s}\n", d->indent, "");
2848 d->in_ptr_field = false;
2850 break;
2852 case TYPE_ARRAY:
2854 int loopcounter;
2855 const char *oldval = d->val;
2856 char *newval;
2858 /* If it's an array of scalars, we optimize by not generating
2859 any code. */
2860 if (t->u.a.p->kind == TYPE_SCALAR)
2861 break;
2863 if (length)
2864 loopcounter = d->loopcounter;
2865 else
2866 loopcounter = d->counter++;
2868 /* When walking an array, compute the length and store it in a
2869 local variable before walking the array elements, instead of
2870 recomputing the length expression each time through the loop.
2871 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2872 where the length is stored in the first array element,
2873 because otherwise that operand can get overwritten on the
2874 first iteration. */
2875 oprintf (d->of, "%*s{\n", d->indent, "");
2876 d->indent += 2;
2877 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2878 if (!d->in_record_p || !length)
2880 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2881 d->indent, "", loopcounter);
2882 if (length)
2883 output_escaped_param (d, length, "length");
2884 else
2885 oprintf (d->of, "%s", t->u.a.len);
2886 oprintf (d->of, ");\n");
2889 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2890 d->indent, "",
2891 loopcounter, loopcounter, loopcounter, loopcounter);
2892 d->indent += 2;
2893 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2894 d->used_length = 1;
2895 walk_type (t->u.a.p, d);
2896 free (newval);
2897 d->used_length = 0;
2898 d->val = oldval;
2899 d->indent -= 2;
2900 oprintf (d->of, "%*s}\n", d->indent, "");
2901 d->indent -= 2;
2902 oprintf (d->of, "%*s}\n", d->indent, "");
2904 break;
2906 case TYPE_STRUCT:
2907 case TYPE_UNION:
2909 pair_p f;
2910 const char *oldval = d->val;
2911 const char *oldprevval1 = d->prev_val[1];
2912 const char *oldprevval2 = d->prev_val[2];
2913 const char *struct_mark_hook = NULL;
2914 const int union_p = t->kind == TYPE_UNION;
2915 int seen_default_p = 0;
2916 options_p o;
2917 int lengths_seen = 0;
2918 int endcounter;
2919 bool any_length_seen = false;
2921 if (!t->u.s.line.file)
2922 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
2924 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2926 error_at_line (d->line,
2927 "structure `%s' defined for mismatching languages",
2928 t->u.s.tag);
2929 error_at_line (&t->u.s.line, "one structure defined here");
2932 /* Some things may also be defined in the structure's options. */
2933 for (o = t->u.s.opt; o; o = o->next)
2934 if (!desc && strcmp (o->name, "desc") == 0
2935 && o->kind == OPTION_STRING)
2936 desc = o->info.string;
2937 else if (!struct_mark_hook && strcmp (o->name, "mark_hook") == 0
2938 && o->kind == OPTION_STRING)
2939 struct_mark_hook = o->info.string;
2941 if (struct_mark_hook)
2942 oprintf (d->of, "%*s%s (&%s);\n",
2943 d->indent, "", struct_mark_hook, oldval);
2945 d->prev_val[2] = oldval;
2946 d->prev_val[1] = oldprevval2;
2947 if (union_p)
2949 if (desc == NULL)
2951 error_at_line (d->line,
2952 "missing `desc' option for union `%s'",
2953 t->u.s.tag);
2954 desc = "1";
2956 oprintf (d->of, "%*sswitch (", d->indent, "");
2957 output_escaped_param (d, desc, "desc");
2958 oprintf (d->of, ")\n");
2959 d->indent += 2;
2960 oprintf (d->of, "%*s{\n", d->indent, "");
2963 for (f = t->u.s.fields; f; f = f->next)
2965 options_p oo;
2966 int skip_p = 0;
2967 const char *fieldlength = NULL;
2969 d->reorder_fn = NULL;
2970 for (oo = f->opt; oo; oo = oo->next)
2971 if (strcmp (oo->name, "skip") == 0)
2972 skip_p = 1;
2973 else if (strcmp (oo->name, "length") == 0
2974 && oo->kind == OPTION_STRING)
2975 fieldlength = oo->info.string;
2977 if (skip_p)
2978 continue;
2979 if (fieldlength)
2981 lengths_seen++;
2982 d->counter++;
2983 if (!union_p)
2985 if (!any_length_seen)
2987 oprintf (d->of, "%*s{\n", d->indent, "");
2988 d->indent += 2;
2990 any_length_seen = true;
2992 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2993 d->indent, "", d->counter - 1);
2994 output_escaped_param (d, fieldlength, "length");
2995 oprintf (d->of, ");\n");
2999 endcounter = d->counter;
3001 for (f = t->u.s.fields; f; f = f->next)
3003 options_p oo;
3004 const char *dot = ".";
3005 const char *tagid = NULL;
3006 int skip_p = 0;
3007 int default_p = 0;
3008 int use_param_p = 0;
3009 const char *fieldlength = NULL;
3010 char *newval;
3012 d->reorder_fn = NULL;
3013 for (oo = f->opt; oo; oo = oo->next)
3014 if (strcmp (oo->name, "dot") == 0
3015 && oo->kind == OPTION_STRING)
3016 dot = oo->info.string;
3017 else if (strcmp (oo->name, "tag") == 0
3018 && oo->kind == OPTION_STRING)
3019 tagid = oo->info.string;
3020 else if (strcmp (oo->name, "skip") == 0)
3021 skip_p = 1;
3022 else if (strcmp (oo->name, "default") == 0)
3023 default_p = 1;
3024 else if (strcmp (oo->name, "reorder") == 0
3025 && oo->kind == OPTION_STRING)
3026 d->reorder_fn = oo->info.string;
3027 else if (strncmp (oo->name, "use_param", 9) == 0
3028 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
3029 use_param_p = 1;
3030 else if (strcmp (oo->name, "length") == 0
3031 && oo->kind == OPTION_STRING)
3032 fieldlength = oo->info.string;
3034 if (skip_p)
3035 continue;
3037 if (union_p && tagid)
3039 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
3040 d->indent += 2;
3042 else if (union_p && default_p)
3044 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3045 d->indent += 2;
3046 seen_default_p = 1;
3048 else if (!union_p && (default_p || tagid))
3049 error_at_line (d->line,
3050 "can't use `%s' outside a union on field `%s'",
3051 default_p ? "default" : "tag", f->name);
3052 else if (union_p && !(default_p || tagid)
3053 && f->type->kind == TYPE_SCALAR)
3055 fprintf (stderr,
3056 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3057 get_input_file_name (d->line->file), d->line->line,
3058 f->name);
3059 continue;
3061 else if (union_p && !(default_p || tagid))
3062 error_at_line (d->line,
3063 "field `%s' is missing `tag' or `default' option",
3064 f->name);
3066 if (fieldlength)
3068 d->loopcounter = endcounter - lengths_seen--;
3071 d->line = &f->line;
3072 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
3073 d->opt = f->opt;
3074 d->used_length = false;
3075 d->in_record_p = !union_p;
3077 if (union_p && use_param_p && d->param == NULL)
3078 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
3079 else
3080 walk_type (f->type, d);
3082 d->in_record_p = false;
3084 free (newval);
3086 if (union_p)
3088 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3089 d->indent -= 2;
3092 d->reorder_fn = NULL;
3094 d->val = oldval;
3095 d->prev_val[1] = oldprevval1;
3096 d->prev_val[2] = oldprevval2;
3098 if (union_p && !seen_default_p)
3100 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3101 oprintf (d->of, "%*s break;\n", d->indent, "");
3103 if (union_p)
3105 oprintf (d->of, "%*s}\n", d->indent, "");
3106 d->indent -= 2;
3108 if (any_length_seen)
3110 d->indent -= 2;
3111 oprintf (d->of, "%*s}\n", d->indent, "");
3114 break;
3116 case TYPE_LANG_STRUCT:
3118 type_p nt;
3119 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3120 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3121 break;
3122 if (nt == NULL)
3123 error_at_line (d->line, "structure `%s' differs between languages",
3124 t->u.s.tag);
3125 else
3126 walk_type (nt, d);
3128 break;
3130 case TYPE_PARAM_STRUCT:
3132 type_p *oldparam = d->param;
3134 d->param = t->u.param_struct.param;
3135 walk_type (t->u.param_struct.stru, d);
3136 d->param = oldparam;
3138 break;
3140 case TYPE_USER_STRUCT:
3141 d->process_field (t, d);
3142 break;
3144 case TYPE_NONE:
3145 case TYPE_UNDEFINED:
3146 gcc_unreachable ();
3150 /* process_field routine for marking routines. */
3152 static void
3153 write_types_process_field (type_p f, const struct walk_type_data *d)
3155 const struct write_types_data *wtd;
3156 const char *cast = d->needs_cast_p ? "(void *)" : "";
3157 wtd = (const struct write_types_data *) d->cookie;
3159 switch (f->kind)
3161 case TYPE_NONE:
3162 case TYPE_UNDEFINED:
3163 gcc_unreachable ();
3164 case TYPE_POINTER:
3165 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
3166 wtd->subfield_marker_routine, cast, d->val);
3167 if (wtd->param_prefix)
3169 if (f->u.p->kind == TYPE_SCALAR)
3170 /* The current type is a pointer to a scalar (so not
3171 considered like a pointer to instances of user defined
3172 types) and we are seeing it; it means we must be even
3173 more careful about the second argument of the
3174 SUBFIELD_MARKER_ROUTINE call. That argument must
3175 always be the instance of the type for which
3176 write_func_for_structure was called - this really is
3177 what the function SUBFIELD_MARKER_ROUTINE expects.
3178 That is, it must be an instance of the ORIG_S type
3179 parameter of write_func_for_structure. The convention
3180 is that that argument must be "x" in that case (as set
3181 by write_func_for_structure). The problem is, we can't
3182 count on d->prev_val[3] to be always set to "x" in that
3183 case. Sometimes walk_type can set it to something else
3184 (to e.g cooperate with write_array when called from
3185 write_roots). So let's set it to "x" here then. */
3186 oprintf (d->of, ", x");
3187 else
3188 oprintf (d->of, ", %s", d->prev_val[3]);
3189 if (d->orig_s)
3191 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3192 output_mangled_typename (d->of, d->orig_s);
3194 else
3195 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3197 oprintf (d->of, ");\n");
3198 if (d->reorder_fn && wtd->reorder_note_routine)
3199 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
3200 wtd->reorder_note_routine, cast, d->val,
3201 d->prev_val[3], d->reorder_fn);
3202 break;
3204 case TYPE_STRING:
3205 case TYPE_STRUCT:
3206 case TYPE_UNION:
3207 case TYPE_LANG_STRUCT:
3208 case TYPE_PARAM_STRUCT:
3209 case TYPE_USER_STRUCT:
3210 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3212 /* If F is a user-defined type and the field is not a
3213 pointer to the type, then we should not generate the
3214 standard pointer-marking code. All we need to do is call
3215 the user-provided marking function to process the fields
3216 of F. */
3217 oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3218 d->val);
3220 else
3222 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3223 output_mangled_typename (d->of, f);
3224 oprintf (d->of, " (%s%s);\n", cast, d->val);
3225 if (d->reorder_fn && wtd->reorder_note_routine)
3226 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
3227 wtd->reorder_note_routine, cast, d->val, cast, d->val,
3228 d->reorder_fn);
3230 break;
3232 case TYPE_SCALAR:
3233 break;
3235 case TYPE_ARRAY:
3236 gcc_unreachable ();
3240 /* Return an output file that is suitable for definitions which can
3241 reference struct S */
3243 static outf_p
3244 get_output_file_for_structure (const_type_p s, type_p *param)
3246 const input_file *fn;
3247 int i;
3249 gcc_assert (union_or_struct_p (s));
3250 fn = s->u.s.line.file;
3252 /* This is a hack, and not the good kind either. */
3253 for (i = NUM_PARAM - 1; i >= 0; i--)
3254 if (param && param[i] && param[i]->kind == TYPE_POINTER
3255 && union_or_struct_p (param[i]->u.p))
3256 fn = param[i]->u.p->u.s.line.file;
3258 /* The call to get_output_file_with_visibility may update fn by
3259 caching its result inside, so we need the CONST_CAST. */
3260 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3264 /* Returns the specifier keyword for a string or union type S, empty string
3265 otherwise. */
3267 static const char *
3268 get_type_specifier (const type_p s)
3270 if (s->kind == TYPE_STRUCT)
3271 return "struct ";
3272 else if (s->kind == TYPE_LANG_STRUCT)
3273 return get_type_specifier (s->u.s.lang_struct);
3274 else if (s->kind == TYPE_UNION)
3275 return "union ";
3276 return "";
3280 /* Emits a declaration for type TY (assumed to be a union or a
3281 structure) on stream OUT. */
3283 static void
3284 write_type_decl (outf_p out, type_p ty)
3286 if (union_or_struct_p (ty))
3287 oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3288 else if (ty->kind == TYPE_SCALAR)
3290 if (ty->u.scalar_is_char)
3291 oprintf (out, "const char");
3292 else
3293 oprintf (out, "void");
3295 else if (ty->kind == TYPE_POINTER)
3297 write_type_decl (out, ty->u.p);
3298 oprintf (out, " *");
3300 else if (ty->kind == TYPE_ARRAY)
3302 write_type_decl (out, ty->u.a.p);
3303 oprintf (out, " *");
3305 else if (ty->kind == TYPE_STRING)
3307 oprintf (out, "const char *");
3309 else
3310 gcc_unreachable ();
3314 /* Write on OF the name of the marker function for structure S. PREFIX
3315 is the prefix to use (to distinguish ggc from pch markers). */
3317 static void
3318 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3320 if (union_or_struct_p (s))
3322 const char *id_for_tag = filter_type_name (s->u.s.tag);
3323 oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3324 if (id_for_tag != s->u.s.tag)
3325 free (CONST_CAST(char *, id_for_tag));
3327 else if (s->kind == TYPE_PARAM_STRUCT)
3329 oprintf (of, "gt_%s_", prefix);
3330 output_mangled_typename (of, s);
3332 else
3333 gcc_unreachable ();
3336 /* Write on OF a user-callable routine to act as an entry point for
3337 the marking routine for S, generated by write_func_for_structure.
3338 PREFIX is the prefix to use to distinguish ggc and pch markers. */
3340 static void
3341 write_user_func_for_structure_ptr (outf_p of, type_p s, const char *prefix)
3343 /* Parameterized structures are not supported in user markers. There
3344 is no way for the marker function to know which specific type
3345 to use to generate the call to the void * entry point. For
3346 instance, a marker for struct htab may need to call different
3347 routines to mark the fields, depending on the paramN_is attributes.
3349 A user-defined marker that accepts 'struct htab' as its argument
3350 would not know which variant to call. Generating several entry
3351 points accepting 'struct htab' would cause multiply-defined
3352 errors during compilation. */
3353 gcc_assert (union_or_struct_p (s));
3355 type_p alias_of = NULL;
3356 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3357 if (strcmp (opt->name, "ptr_alias") == 0)
3359 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3360 we do not generate marking code for ORIG_S here. Instead, a
3361 forwarder #define in gtype-desc.h will cause every call to its
3362 marker to call the target of this alias.
3364 However, we still want to create a user entry code for the
3365 aliased type. So, if ALIAS_OF is set, we only generate the
3366 user-callable marker function. */
3367 alias_of = opt->info.type;
3368 break;
3371 oprintf (of, "\nvoid\n");
3372 oprintf (of, "gt_%sx (", prefix);
3373 write_type_decl (of, s);
3374 oprintf (of, " *& x)\n");
3375 oprintf (of, "{\n");
3376 oprintf (of, " if (x)\n ");
3377 write_marker_function_name (of, alias_of ? alias_of : s, prefix);
3378 oprintf (of, " ((void *) x);\n");
3379 oprintf (of, "}\n");
3383 /* Write a function to mark all the fields of type S on OF. PREFIX
3384 and D are as in write_user_marking_functions. */
3386 static void
3387 write_user_func_for_structure_body (type_p s, const char *prefix,
3388 struct walk_type_data *d)
3390 oprintf (d->of, "\nvoid\n");
3391 oprintf (d->of, "gt_%sx (", prefix);
3392 write_type_decl (d->of, s);
3393 oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3394 oprintf (d->of, "{\n");
3395 oprintf (d->of, " ");
3396 write_type_decl (d->of, s);
3397 oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3398 d->val = "(*x)";
3399 d->indent = 2;
3400 walk_type (s, d);
3401 oprintf (d->of, "}\n");
3405 /* Emit the user-callable functions needed to mark all the types used
3406 by the user structure S. PREFIX is the prefix to use to
3407 distinguish ggc and pch markers. D contains data needed to pass to
3408 walk_type when traversing the fields of a type.
3410 For every type T referenced by S, two routines are generated: one
3411 that takes 'T *', marks the pointer and calls the second routine,
3412 which just marks the fields of T. */
3414 static void
3415 write_user_marking_functions (type_p s, const char *prefix,
3416 struct walk_type_data *d)
3418 gcc_assert (s->kind == TYPE_USER_STRUCT);
3420 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3422 type_p fld_type = fld->type;
3423 if (fld_type->kind == TYPE_POINTER)
3425 type_p pointed_to_type = fld_type->u.p;
3426 if (union_or_struct_p (pointed_to_type))
3427 write_user_func_for_structure_ptr (d->of, pointed_to_type, prefix);
3429 else if (union_or_struct_p (fld_type))
3430 write_user_func_for_structure_body (fld_type, prefix, d);
3435 /* For S, a structure that's part of ORIG_S, and using parameters
3436 PARAM, write out a routine that:
3437 - Takes a parameter, a void * but actually of type *S
3438 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3439 field of S or its substructures and (in some cases) things
3440 that are pointed to by S. */
3442 static void
3443 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
3444 const struct write_types_data *wtd)
3446 const char *chain_next = NULL;
3447 const char *chain_prev = NULL;
3448 const char *chain_circular = NULL;
3449 const char *mark_hook_name = NULL;
3450 options_p opt;
3451 struct walk_type_data d;
3453 memset (&d, 0, sizeof (d));
3454 d.of = get_output_file_for_structure (s, param);
3455 for (opt = s->u.s.opt; opt; opt = opt->next)
3456 if (strcmp (opt->name, "chain_next") == 0
3457 && opt->kind == OPTION_STRING)
3458 chain_next = opt->info.string;
3459 else if (strcmp (opt->name, "chain_prev") == 0
3460 && opt->kind == OPTION_STRING)
3461 chain_prev = opt->info.string;
3462 else if (strcmp (opt->name, "chain_circular") == 0
3463 && opt->kind == OPTION_STRING)
3464 chain_circular = opt->info.string;
3465 else if (strcmp (opt->name, "mark_hook") == 0
3466 && opt->kind == OPTION_STRING)
3467 mark_hook_name = opt->info.string;
3468 if (chain_prev != NULL && chain_next == NULL)
3469 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3470 if (chain_circular != NULL && chain_next != NULL)
3471 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3472 if (chain_circular != NULL)
3473 chain_next = chain_circular;
3475 d.process_field = write_types_process_field;
3476 d.cookie = wtd;
3477 d.orig_s = orig_s;
3478 d.opt = s->u.s.opt;
3479 d.line = &s->u.s.line;
3480 d.bitmap = s->u.s.bitmap;
3481 d.param = param;
3482 d.prev_val[0] = "*x";
3483 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3484 d.prev_val[3] = "x";
3485 d.val = "(*x)";
3486 d.have_this_obj = false;
3488 oprintf (d.of, "\n");
3489 oprintf (d.of, "void\n");
3490 write_marker_function_name (d.of, orig_s, wtd->prefix);
3491 oprintf (d.of, " (void *x_p)\n");
3492 oprintf (d.of, "{\n ");
3493 write_type_decl (d.of, s);
3494 oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3495 write_type_decl (d.of, s);
3496 oprintf (d.of, " *)x_p;\n");
3497 if (chain_next != NULL)
3499 /* TYPE_USER_STRUCTs should not occur here. These structures
3500 are completely handled by user code. */
3501 gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
3503 oprintf (d.of, " ");
3504 write_type_decl (d.of, s);
3505 oprintf (d.of, " * xlimit = x;\n");
3507 if (chain_next == NULL)
3509 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3510 if (wtd->param_prefix)
3512 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3513 output_mangled_typename (d.of, orig_s);
3515 oprintf (d.of, "))\n");
3517 else
3519 if (chain_circular != NULL)
3520 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3521 else
3522 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3523 if (wtd->param_prefix)
3525 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3526 output_mangled_typename (d.of, orig_s);
3528 oprintf (d.of, "))\n");
3529 if (chain_circular != NULL)
3530 oprintf (d.of, " return;\n do\n");
3531 if (mark_hook_name && !wtd->skip_hooks)
3533 oprintf (d.of, " {\n");
3534 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
3536 oprintf (d.of, " xlimit = (");
3537 d.prev_val[2] = "*xlimit";
3538 output_escaped_param (&d, chain_next, "chain_next");
3539 oprintf (d.of, ");\n");
3540 if (mark_hook_name && !wtd->skip_hooks)
3541 oprintf (d.of, " }\n");
3542 if (chain_prev != NULL)
3544 oprintf (d.of, " if (x != xlimit)\n");
3545 oprintf (d.of, " for (;;)\n");
3546 oprintf (d.of, " {\n");
3547 oprintf (d.of, " %s %s * const xprev = (",
3548 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3550 d.prev_val[2] = "*x";
3551 output_escaped_param (&d, chain_prev, "chain_prev");
3552 oprintf (d.of, ");\n");
3553 oprintf (d.of, " if (xprev == NULL) break;\n");
3554 oprintf (d.of, " x = xprev;\n");
3555 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3556 if (wtd->param_prefix)
3558 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3559 output_mangled_typename (d.of, orig_s);
3561 oprintf (d.of, ");\n");
3562 oprintf (d.of, " }\n");
3564 if (chain_circular != NULL)
3566 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3567 if (wtd->param_prefix)
3569 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3570 output_mangled_typename (d.of, orig_s);
3572 oprintf (d.of, "));\n");
3573 if (mark_hook_name && !wtd->skip_hooks)
3574 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
3575 oprintf (d.of, " do\n");
3577 else
3578 oprintf (d.of, " while (x != xlimit)\n");
3580 oprintf (d.of, " {\n");
3581 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3583 oprintf (d.of, " %s (x);\n", mark_hook_name);
3586 d.prev_val[2] = "*x";
3587 d.indent = 6;
3588 if (orig_s->kind != TYPE_USER_STRUCT)
3589 walk_type (s, &d);
3590 else
3592 /* User structures have no fields to walk. Simply generate a call
3593 to the user-provided structure marker. */
3594 oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3597 if (chain_next != NULL)
3599 oprintf (d.of, " x = (");
3600 output_escaped_param (&d, chain_next, "chain_next");
3601 oprintf (d.of, ");\n");
3604 oprintf (d.of, " }\n");
3605 if (chain_circular != NULL)
3606 oprintf (d.of, " while (x != xlimit);\n");
3607 oprintf (d.of, "}\n");
3609 if (orig_s->kind == TYPE_USER_STRUCT)
3610 write_user_marking_functions (orig_s, wtd->prefix, &d);
3614 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3616 static void
3617 write_types (outf_p output_header, type_p structures, type_p param_structs,
3618 const struct write_types_data *wtd)
3620 int nbfun = 0; /* Count the emitted functions. */
3621 type_p s;
3623 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3625 /* We first emit the macros and the declarations. Functions' code is
3626 emitted afterwards. This is needed in plugin mode. */
3627 oprintf (output_header, "/* Macros and declarations. */\n");
3628 for (s = structures; s; s = s->next)
3629 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3631 options_p opt;
3633 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3634 continue;
3636 const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3638 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3639 output_mangled_typename (output_header, s);
3640 oprintf (output_header, "(X) do { \\\n");
3641 oprintf (output_header,
3642 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3643 s_id_for_tag);
3644 oprintf (output_header, " } while (0)\n");
3646 for (opt = s->u.s.opt; opt; opt = opt->next)
3647 if (strcmp (opt->name, "ptr_alias") == 0
3648 && opt->kind == OPTION_TYPE)
3650 const_type_p const t = (const_type_p) opt->info.type;
3651 if (t->kind == TYPE_STRUCT
3652 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3654 const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3655 oprintf (output_header,
3656 "#define gt_%sx_%s gt_%sx_%s\n",
3657 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3658 if (t_id_for_tag != t->u.s.tag)
3659 free (CONST_CAST(char *, t_id_for_tag));
3661 else
3662 error_at_line (&s->u.s.line,
3663 "structure alias is not a structure");
3664 break;
3666 if (opt)
3667 continue;
3669 /* Declare the marker procedure only once. */
3670 oprintf (output_header,
3671 "extern void gt_%sx_%s (void *);\n",
3672 wtd->prefix, s_id_for_tag);
3674 if (s_id_for_tag != s->u.s.tag)
3675 free (CONST_CAST(char *, s_id_for_tag));
3677 if (s->u.s.line.file == NULL)
3679 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3680 s->u.s.tag);
3681 continue;
3685 for (s = param_structs; s; s = s->next)
3686 if (s->gc_used == GC_POINTED_TO)
3688 type_p stru = s->u.param_struct.stru;
3690 /* Declare the marker procedure. */
3691 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
3692 output_mangled_typename (output_header, s);
3693 oprintf (output_header, " (void *);\n");
3695 if (stru->u.s.line.file == NULL)
3697 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3698 stru->u.s.tag);
3699 continue;
3703 /* At last we emit the functions code. */
3704 oprintf (output_header, "\n/* functions code */\n");
3705 for (s = structures; s; s = s->next)
3706 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3708 options_p opt;
3710 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3711 continue;
3712 for (opt = s->u.s.opt; opt; opt = opt->next)
3713 if (strcmp (opt->name, "ptr_alias") == 0)
3714 break;
3715 if (opt)
3716 continue;
3718 if (s->kind == TYPE_LANG_STRUCT)
3720 type_p ss;
3721 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3723 nbfun++;
3724 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3725 nbfun, (void*) ss, ss->u.s.tag);
3726 write_func_for_structure (s, ss, NULL, wtd);
3729 else
3731 nbfun++;
3732 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3733 nbfun, (void*) s, s->u.s.tag);
3734 write_func_for_structure (s, s, NULL, wtd);
3737 else
3739 /* Structure s is not possibly pointed to, so can be ignored. */
3740 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3741 (void*)s, s->u.s.tag,
3742 (int) s->gc_used);
3745 for (s = param_structs; s; s = s->next)
3746 if (s->gc_used == GC_POINTED_TO)
3748 type_p *param = s->u.param_struct.param;
3749 type_p stru = s->u.param_struct.stru;
3750 if (stru->u.s.line.file == NULL)
3751 continue;
3752 if (stru->kind == TYPE_LANG_STRUCT)
3754 type_p ss;
3755 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3757 nbfun++;
3758 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
3759 nbfun, (void*) ss, ss->u.s.tag);
3760 write_func_for_structure (s, ss, param, wtd);
3763 else
3765 nbfun++;
3766 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
3767 nbfun, (void*) s,
3768 (void*) stru, stru->u.s.tag);
3769 write_func_for_structure (s, stru, param, wtd);
3772 else
3774 /* Param structure s is not pointed to, so should be ignored. */
3775 DBGPRINTF ("ignored s @ %p", (void*)s);
3777 if (verbosity_level >= 2)
3778 printf ("%s emitted %d routines for %s\n",
3779 progname, nbfun, wtd->comment);
3782 static const struct write_types_data ggc_wtd = {
3783 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
3784 "GC marker procedures. ",
3785 FALSE
3788 static const struct write_types_data pch_wtd = {
3789 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3790 "gt_pch_note_reorder",
3791 "PCH type-walking procedures. ",
3792 TRUE
3795 /* Write out the local pointer-walking routines. */
3797 /* process_field routine for local pointer-walking for user-callable
3798 routines. The difference between this and
3799 write_types_local_process_field is that, in this case, we do not
3800 need to check whether the given pointer matches the address of the
3801 parent structure. This check was already generated by the call
3802 to gt_pch_nx in the main gt_pch_p_*() function that is calling
3803 this code. */
3805 static void
3806 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
3808 switch (f->kind)
3810 case TYPE_POINTER:
3811 case TYPE_STRUCT:
3812 case TYPE_UNION:
3813 case TYPE_LANG_STRUCT:
3814 case TYPE_PARAM_STRUCT:
3815 case TYPE_STRING:
3816 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3817 break;
3819 case TYPE_USER_STRUCT:
3820 if (d->in_ptr_field)
3821 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3822 else
3823 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3824 d->indent, "", d->val);
3825 break;
3827 case TYPE_SCALAR:
3828 break;
3830 case TYPE_ARRAY:
3831 case TYPE_NONE:
3832 case TYPE_UNDEFINED:
3833 gcc_unreachable ();
3838 /* Write a function to PCH walk all the fields of type S on OF.
3839 D contains data needed by walk_type to recurse into the fields of S. */
3841 static void
3842 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
3844 oprintf (d->of, "\nvoid\n");
3845 oprintf (d->of, "gt_pch_nx (");
3846 write_type_decl (d->of, s);
3847 oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
3848 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3849 "\tATTRIBUTE_UNUSED void *cookie)\n");
3850 oprintf (d->of, "{\n");
3851 d->val = "(*x)";
3852 d->indent = 2;
3853 d->process_field = write_types_local_user_process_field;
3854 walk_type (s, d);
3855 oprintf (d->of, "}\n");
3859 /* Emit the user-callable functions needed to mark all the types used
3860 by the user structure S. PREFIX is the prefix to use to
3861 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
3862 chain_next option defined. D contains data needed to pass to
3863 walk_type when traversing the fields of a type.
3865 For every type T referenced by S, two routines are generated: one
3866 that takes 'T *', marks the pointer and calls the second routine,
3867 which just marks the fields of T. */
3869 static void
3870 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
3872 gcc_assert (s->kind == TYPE_USER_STRUCT);
3874 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3876 type_p fld_type = fld->type;
3877 if (union_or_struct_p (fld_type))
3878 write_pch_user_walking_for_structure_body (fld_type, d);
3883 /* process_field routine for local pointer-walking. */
3885 static void
3886 write_types_local_process_field (type_p f, const struct walk_type_data *d)
3888 gcc_assert (d->have_this_obj);
3889 switch (f->kind)
3891 case TYPE_POINTER:
3892 case TYPE_STRUCT:
3893 case TYPE_UNION:
3894 case TYPE_LANG_STRUCT:
3895 case TYPE_PARAM_STRUCT:
3896 case TYPE_STRING:
3897 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3898 d->prev_val[3]);
3899 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3900 break;
3902 case TYPE_USER_STRUCT:
3903 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3904 d->prev_val[3]);
3905 if (d->in_ptr_field)
3906 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3907 else
3908 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3909 d->indent, "", d->val);
3910 break;
3912 case TYPE_SCALAR:
3913 break;
3915 case TYPE_ARRAY:
3916 case TYPE_NONE:
3917 case TYPE_UNDEFINED:
3918 gcc_unreachable ();
3923 /* For S, a structure that's part of ORIG_S, and using parameters
3924 PARAM, write out a routine that:
3925 - Is of type gt_note_pointers
3926 - Calls PROCESS_FIELD on each field of S or its substructures.
3929 static void
3930 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
3932 struct walk_type_data d;
3934 memset (&d, 0, sizeof (d));
3935 d.of = get_output_file_for_structure (s, param);
3936 d.process_field = write_types_local_process_field;
3937 d.opt = s->u.s.opt;
3938 d.line = &s->u.s.line;
3939 d.bitmap = s->u.s.bitmap;
3940 d.param = param;
3941 d.prev_val[0] = d.prev_val[2] = "*x";
3942 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3943 d.prev_val[3] = "x";
3944 d.val = "(*x)";
3945 d.fn_wants_lvalue = true;
3947 oprintf (d.of, "\n");
3948 oprintf (d.of, "void\n");
3949 oprintf (d.of, "gt_pch_p_");
3950 output_mangled_typename (d.of, orig_s);
3951 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3952 "\tvoid *x_p,\n"
3953 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3954 "\tATTRIBUTE_UNUSED void *cookie)\n");
3955 oprintf (d.of, "{\n");
3956 oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3957 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3958 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3959 d.indent = 2;
3960 d.have_this_obj = true;
3962 if (s->kind != TYPE_USER_STRUCT)
3963 walk_type (s, &d);
3964 else
3966 /* User structures have no fields to walk. Simply generate a
3967 call to the user-provided PCH walker. */
3968 oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
3969 d.prev_val[3]);
3970 oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3971 d.indent, "", d.val);
3974 oprintf (d.of, "}\n");
3976 /* Write user-callable entry points for the PCH walking routines. */
3977 if (orig_s->kind == TYPE_USER_STRUCT)
3978 write_pch_user_walking_functions (s, &d);
3981 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
3983 static void
3984 write_local (outf_p output_header, type_p structures, type_p param_structs)
3986 type_p s;
3988 if (!output_header)
3989 return;
3991 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
3992 for (s = structures; s; s = s->next)
3993 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3995 options_p opt;
3997 if (s->u.s.line.file == NULL)
3998 continue;
3999 for (opt = s->u.s.opt; opt; opt = opt->next)
4000 if (strcmp (opt->name, "ptr_alias") == 0
4001 && opt->kind == OPTION_TYPE)
4003 const_type_p const t = (const_type_p) opt->info.type;
4004 if (t->kind == TYPE_STRUCT
4005 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
4007 oprintf (output_header, "#define gt_pch_p_");
4008 output_mangled_typename (output_header, s);
4009 oprintf (output_header, " gt_pch_p_");
4010 output_mangled_typename (output_header, t);
4011 oprintf (output_header, "\n");
4013 else
4014 error_at_line (&s->u.s.line,
4015 "structure alias is not a structure");
4016 break;
4018 if (opt)
4019 continue;
4021 /* Declare the marker procedure only once. */
4022 oprintf (output_header, "extern void gt_pch_p_");
4023 output_mangled_typename (output_header, s);
4024 oprintf (output_header,
4025 "\n (void *, void *, gt_pointer_operator, void *);\n");
4027 if (s->kind == TYPE_LANG_STRUCT)
4029 type_p ss;
4030 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4031 write_local_func_for_structure (s, ss, NULL);
4033 else
4034 write_local_func_for_structure (s, s, NULL);
4037 for (s = param_structs; s; s = s->next)
4038 if (s->gc_used == GC_POINTED_TO)
4040 type_p *param = s->u.param_struct.param;
4041 type_p stru = s->u.param_struct.stru;
4043 /* Declare the marker procedure. */
4044 oprintf (output_header, "extern void gt_pch_p_");
4045 output_mangled_typename (output_header, s);
4046 oprintf (output_header,
4047 "\n (void *, void *, gt_pointer_operator, void *);\n");
4049 if (stru->u.s.line.file == NULL)
4051 fprintf (stderr, "warning: structure `%s' used but not defined\n",
4052 stru->u.s.tag);
4053 continue;
4056 if (stru->kind == TYPE_LANG_STRUCT)
4058 type_p ss;
4059 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
4060 write_local_func_for_structure (s, ss, param);
4062 else
4063 write_local_func_for_structure (s, stru, param);
4067 /* Nonzero if S is a type for which typed GC allocators should be output. */
4069 #define USED_BY_TYPED_GC_P(s) \
4070 ((s->kind == TYPE_POINTER \
4071 && (s->u.p->gc_used == GC_POINTED_TO \
4072 || s->u.p->gc_used == GC_USED)) \
4073 || (union_or_struct_p (s) \
4074 && ((s)->gc_used == GC_POINTED_TO \
4075 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4076 && s->u.s.line.file != NULL) \
4077 || ((s)->gc_used == GC_USED \
4078 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))))))
4081 /* Might T contain any non-pointer elements? */
4083 static int
4084 contains_scalar_p (type_p t)
4086 switch (t->kind)
4088 case TYPE_STRING:
4089 case TYPE_POINTER:
4090 return 0;
4091 case TYPE_ARRAY:
4092 return contains_scalar_p (t->u.a.p);
4093 case TYPE_USER_STRUCT:
4094 /* User-marked structures will typically contain pointers. */
4095 return 0;
4096 default:
4097 /* Could also check for structures that have no non-pointer
4098 fields, but there aren't enough of those to worry about. */
4099 return 1;
4103 /* Mangle INPF and print it to F. */
4105 static void
4106 put_mangled_filename (outf_p f, const input_file *inpf)
4108 /* The call to get_output_file_name may indirectly update fn since
4109 get_output_file_with_visibility caches its result inside, so we
4110 need the CONST_CAST. */
4111 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4112 if (!f || !name)
4113 return;
4114 for (; *name != 0; name++)
4115 if (ISALNUM (*name))
4116 oprintf (f, "%c", *name);
4117 else
4118 oprintf (f, "%c", '_');
4121 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4122 LASTNAME, and NAME are all strings to insert in various places in
4123 the resulting code. */
4125 static void
4126 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4127 const char *tname, const char *name)
4129 struct flist *fli2;
4131 for (fli2 = flp; fli2; fli2 = fli2->next)
4132 if (fli2->started_p)
4134 oprintf (fli2->f, " %s\n", lastname);
4135 oprintf (fli2->f, "};\n\n");
4138 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4139 if (fli2->started_p)
4141 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4142 int fnum;
4144 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4145 if (bitmap & 1)
4147 oprintf (base_files[fnum],
4148 "extern const struct %s gt_%s_", tname, pfx);
4149 put_mangled_filename (base_files[fnum], fli2->file);
4150 oprintf (base_files[fnum], "[];\n");
4155 size_t fnum;
4156 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4157 oprintf (base_files[fnum],
4158 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4162 for (fli2 = flp; fli2; fli2 = fli2->next)
4163 if (fli2->started_p)
4165 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4166 int fnum;
4168 fli2->started_p = 0;
4170 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4171 if (bitmap & 1)
4173 oprintf (base_files[fnum], " gt_%s_", pfx);
4174 put_mangled_filename (base_files[fnum], fli2->file);
4175 oprintf (base_files[fnum], ",\n");
4180 size_t fnum;
4181 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4183 oprintf (base_files[fnum], " NULL\n");
4184 oprintf (base_files[fnum], "};\n");
4189 /* Write the first three fields (pointer, count and stride) for
4190 root NAME to F. V and LINE are as for write_root.
4192 Return true if the entry could be written; return false on error. */
4194 static bool
4195 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4197 type_p ap;
4199 if (!v)
4201 error_at_line (line, "`%s' is too complex to be a root", name);
4202 return false;
4205 oprintf (f, " {\n");
4206 oprintf (f, " &%s,\n", name);
4207 oprintf (f, " 1");
4209 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4210 if (ap->u.a.len[0])
4211 oprintf (f, " * (%s)", ap->u.a.len);
4212 else if (ap == v->type)
4213 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4214 oprintf (f, ",\n");
4215 oprintf (f, " sizeof (%s", v->name);
4216 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4217 oprintf (f, "[0]");
4218 oprintf (f, "),\n");
4219 return true;
4222 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4223 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4224 of the caller. */
4226 static void
4227 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4228 int has_length, struct fileloc *line, const char *if_marked,
4229 bool emit_pch, type_p field_type, const char *field_name)
4231 struct pair newv;
4232 /* If the field reference is relative to V, rather than to some
4233 subcomponent of V, we can mark any subarrays with a single stride.
4234 We're effectively treating the field as a global variable in its
4235 own right. */
4236 if (v && type == v->type)
4238 newv = *v;
4239 newv.type = field_type;
4240 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4241 v = &newv;
4243 /* Otherwise, any arrays nested in the structure are too complex to
4244 handle. */
4245 else if (field_type->kind == TYPE_ARRAY)
4246 v = NULL;
4247 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4248 has_length, line, if_marked, emit_pch);
4251 /* Write out to F the table entry and any marker routines needed to
4252 mark NAME as TYPE. V can be one of three values:
4254 - null, if NAME is too complex to represent using a single
4255 count and stride. In this case, it is an error for NAME to
4256 contain any gc-ed data.
4258 - the outermost array that contains NAME, if NAME is part of an array.
4260 - the C variable that contains NAME, if NAME is not part of an array.
4262 LINE is the line of the C source that declares the root variable.
4263 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
4264 is nonzero iff we are building the root table for hash table caches. */
4266 static void
4267 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4268 struct fileloc *line, const char *if_marked, bool emit_pch)
4270 switch (type->kind)
4272 case TYPE_STRUCT:
4274 pair_p fld;
4275 for (fld = type->u.s.fields; fld; fld = fld->next)
4277 int skip_p = 0;
4278 const char *desc = NULL;
4279 options_p o;
4281 for (o = fld->opt; o; o = o->next)
4282 if (strcmp (o->name, "skip") == 0)
4283 skip_p = 1;
4284 else if (strcmp (o->name, "desc") == 0
4285 && o->kind == OPTION_STRING)
4286 desc = o->info.string;
4287 else if (strcmp (o->name, "param_is") == 0)
4289 else
4290 error_at_line (line,
4291 "field `%s' of global `%s' has unknown option `%s'",
4292 fld->name, name, o->name);
4294 if (skip_p)
4295 continue;
4296 else if (desc && fld->type->kind == TYPE_UNION)
4298 pair_p validf = NULL;
4299 pair_p ufld;
4301 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4303 const char *tag = NULL;
4304 options_p oo;
4305 for (oo = ufld->opt; oo; oo = oo->next)
4306 if (strcmp (oo->name, "tag") == 0
4307 && oo->kind == OPTION_STRING)
4308 tag = oo->info.string;
4309 if (tag == NULL || strcmp (tag, desc) != 0)
4310 continue;
4311 if (validf != NULL)
4312 error_at_line (line,
4313 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4314 name, fld->name, validf->name,
4315 name, fld->name, ufld->name, tag);
4316 validf = ufld;
4318 if (validf != NULL)
4319 write_field_root (f, v, type, name, 0, line, if_marked,
4320 emit_pch, validf->type,
4321 ACONCAT ((fld->name, ".",
4322 validf->name, NULL)));
4324 else if (desc)
4325 error_at_line (line,
4326 "global `%s.%s' has `desc' option but is not union",
4327 name, fld->name);
4328 else
4329 write_field_root (f, v, type, name, 0, line, if_marked,
4330 emit_pch, fld->type, fld->name);
4333 break;
4335 case TYPE_ARRAY:
4337 char *newname;
4338 newname = xasprintf ("%s[0]", name);
4339 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
4340 emit_pch);
4341 free (newname);
4343 break;
4345 case TYPE_USER_STRUCT:
4346 error_at_line (line, "`%s' must be a pointer type, because it is "
4347 "a GC root and its type is marked with GTY((user))",
4348 v->name);
4349 break;
4351 case TYPE_POINTER:
4353 type_p tp;
4355 if (!start_root_entry (f, v, name, line))
4356 return;
4358 tp = type->u.p;
4360 if (!has_length && union_or_struct_p (tp))
4362 const char *id_for_tag = filter_type_name (tp->u.s.tag);
4363 oprintf (f, " &gt_ggc_mx_%s,\n", id_for_tag);
4364 if (emit_pch)
4365 oprintf (f, " &gt_pch_nx_%s", id_for_tag);
4366 else
4367 oprintf (f, " NULL");
4368 if (id_for_tag != tp->u.s.tag)
4369 free (CONST_CAST(char *, id_for_tag));
4371 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
4373 oprintf (f, " &gt_ggc_m_");
4374 output_mangled_typename (f, tp);
4375 if (emit_pch)
4377 oprintf (f, ",\n &gt_pch_n_");
4378 output_mangled_typename (f, tp);
4380 else
4381 oprintf (f, ",\n NULL");
4383 else if (has_length
4384 && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
4386 oprintf (f, " &gt_ggc_ma_%s,\n", name);
4387 if (emit_pch)
4388 oprintf (f, " &gt_pch_na_%s", name);
4389 else
4390 oprintf (f, " NULL");
4392 else
4394 error_at_line (line,
4395 "global `%s' is pointer to unimplemented type",
4396 name);
4398 if (if_marked)
4399 oprintf (f, ",\n &%s", if_marked);
4400 oprintf (f, "\n },\n");
4402 break;
4404 case TYPE_STRING:
4406 if (!start_root_entry (f, v, name, line))
4407 return;
4409 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
4410 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
4411 oprintf (f, " },\n");
4413 break;
4415 case TYPE_SCALAR:
4416 break;
4418 case TYPE_NONE:
4419 case TYPE_UNDEFINED:
4420 case TYPE_UNION:
4421 case TYPE_LANG_STRUCT:
4422 case TYPE_PARAM_STRUCT:
4423 error_at_line (line, "global `%s' is unimplemented type", name);
4427 /* This generates a routine to walk an array. */
4429 static void
4430 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4432 struct walk_type_data d;
4433 char *prevval3;
4435 memset (&d, 0, sizeof (d));
4436 d.of = f;
4437 d.cookie = wtd;
4438 d.indent = 2;
4439 d.line = &v->line;
4440 d.opt = v->opt;
4441 d.bitmap = get_lang_bitmap (v->line.file);
4442 d.param = NULL;
4444 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4446 if (wtd->param_prefix)
4448 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4449 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
4450 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4451 wtd->param_prefix, v->name);
4452 oprintf (d.of,
4453 " ATTRIBUTE_UNUSED void *x_p,\n"
4454 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4455 " ATTRIBUTE_UNUSED void * cookie)\n");
4456 oprintf (d.of, "{\n");
4457 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4458 d.process_field = write_types_local_process_field;
4459 d.have_this_obj = true;
4460 walk_type (v->type, &d);
4461 oprintf (f, "}\n\n");
4464 d.opt = v->opt;
4465 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4466 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4467 wtd->prefix, v->name);
4468 oprintf (f, "{\n");
4469 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4470 d.process_field = write_types_process_field;
4471 d.have_this_obj = false;
4472 walk_type (v->type, &d);
4473 free (prevval3);
4474 oprintf (f, "}\n\n");
4477 /* Output a table describing the locations and types of VARIABLES. */
4479 static void
4480 write_roots (pair_p variables, bool emit_pch)
4482 pair_p v;
4483 struct flist *flp = NULL;
4485 for (v = variables; v; v = v->next)
4487 outf_p f =
4488 get_output_file_with_visibility (CONST_CAST (input_file*,
4489 v->line.file));
4490 struct flist *fli;
4491 const char *length = NULL;
4492 int deletable_p = 0;
4493 options_p o;
4494 for (o = v->opt; o; o = o->next)
4495 if (strcmp (o->name, "length") == 0
4496 && o->kind == OPTION_STRING)
4497 length = o->info.string;
4498 else if (strcmp (o->name, "deletable") == 0)
4499 deletable_p = 1;
4500 else if (strcmp (o->name, "param_is") == 0)
4502 else if (strncmp (o->name, "param", 5) == 0
4503 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
4505 else if (strcmp (o->name, "if_marked") == 0)
4507 else
4508 error_at_line (&v->line,
4509 "global `%s' has unknown option `%s'",
4510 v->name, o->name);
4512 for (fli = flp; fli; fli = fli->next)
4513 if (fli->f == f && f)
4514 break;
4515 if (fli == NULL)
4517 fli = XNEW (struct flist);
4518 fli->f = f;
4519 fli->next = flp;
4520 fli->started_p = 0;
4521 fli->file = v->line.file;
4522 gcc_assert (fli->file);
4523 flp = fli;
4525 oprintf (f, "\n/* GC roots. */\n\n");
4528 if (!deletable_p
4529 && length
4530 && v->type->kind == TYPE_POINTER
4531 && (v->type->u.p->kind == TYPE_POINTER
4532 || v->type->u.p->kind == TYPE_STRUCT))
4534 write_array (f, v, &ggc_wtd);
4535 write_array (f, v, &pch_wtd);
4539 for (v = variables; v; v = v->next)
4541 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4542 v->line.file));
4543 struct flist *fli;
4544 int skip_p = 0;
4545 int length_p = 0;
4546 options_p o;
4548 for (o = v->opt; o; o = o->next)
4549 if (strcmp (o->name, "length") == 0)
4550 length_p = 1;
4551 else if (strcmp (o->name, "deletable") == 0
4552 || strcmp (o->name, "if_marked") == 0)
4553 skip_p = 1;
4555 if (skip_p)
4556 continue;
4558 for (fli = flp; fli; fli = fli->next)
4559 if (fli->f == f)
4560 break;
4561 if (!fli->started_p)
4563 fli->started_p = 1;
4565 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4566 put_mangled_filename (f, v->line.file);
4567 oprintf (f, "[] = {\n");
4570 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4573 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4574 "gt_ggc_rtab");
4576 for (v = variables; v; v = v->next)
4578 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4579 v->line.file));
4580 struct flist *fli;
4581 int skip_p = 1;
4582 options_p o;
4584 for (o = v->opt; o; o = o->next)
4585 if (strcmp (o->name, "deletable") == 0)
4586 skip_p = 0;
4587 else if (strcmp (o->name, "if_marked") == 0)
4588 skip_p = 1;
4590 if (skip_p)
4591 continue;
4593 for (fli = flp; fli; fli = fli->next)
4594 if (fli->f == f)
4595 break;
4596 if (!fli->started_p)
4598 fli->started_p = 1;
4600 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4601 put_mangled_filename (f, v->line.file);
4602 oprintf (f, "[] = {\n");
4605 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4606 v->name, v->name);
4609 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4610 "gt_ggc_deletable_rtab");
4612 for (v = variables; v; v = v->next)
4614 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4615 v->line.file));
4616 struct flist *fli;
4617 const char *if_marked = NULL;
4618 int length_p = 0;
4619 options_p o;
4621 for (o = v->opt; o; o = o->next)
4622 if (strcmp (o->name, "length") == 0)
4623 length_p = 1;
4624 else if (strcmp (o->name, "if_marked") == 0
4625 && o->kind == OPTION_STRING)
4626 if_marked = o->info.string;
4627 if (if_marked == NULL)
4628 continue;
4629 if (v->type->kind != TYPE_POINTER
4630 || v->type->u.p->kind != TYPE_PARAM_STRUCT
4631 || v->type->u.p->u.param_struct.stru != find_structure ("htab",
4632 TYPE_STRUCT))
4634 error_at_line (&v->line,
4635 "if_marked option used but not hash table");
4636 continue;
4639 for (fli = flp; fli; fli = fli->next)
4640 if (fli->f == f)
4641 break;
4642 if (!fli->started_p)
4644 fli->started_p = 1;
4646 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4647 put_mangled_filename (f, v->line.file);
4648 oprintf (f, "[] = {\n");
4651 write_root (f, v, v->type->u.p->u.param_struct.param[0],
4652 v->name, length_p, &v->line, if_marked, emit_pch);
4655 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4656 "gt_ggc_cache_rtab");
4658 if (!emit_pch)
4659 return;
4661 for (v = variables; v; v = v->next)
4663 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4664 v->line.file));
4665 struct flist *fli;
4666 int length_p = 0;
4667 int if_marked_p = 0;
4668 options_p o;
4670 for (o = v->opt; o; o = o->next)
4671 if (strcmp (o->name, "length") == 0)
4672 length_p = 1;
4673 else if (strcmp (o->name, "if_marked") == 0)
4674 if_marked_p = 1;
4676 if (!if_marked_p)
4677 continue;
4679 for (fli = flp; fli; fli = fli->next)
4680 if (fli->f == f)
4681 break;
4682 if (!fli->started_p)
4684 fli->started_p = 1;
4686 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4687 put_mangled_filename (f, v->line.file);
4688 oprintf (f, "[] = {\n");
4691 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4694 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4695 "gt_pch_cache_rtab");
4697 for (v = variables; v; v = v->next)
4699 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4700 v->line.file));
4701 struct flist *fli;
4702 int skip_p = 0;
4703 options_p o;
4705 for (o = v->opt; o; o = o->next)
4706 if (strcmp (o->name, "deletable") == 0
4707 || strcmp (o->name, "if_marked") == 0)
4709 skip_p = 1;
4710 break;
4713 if (skip_p)
4714 continue;
4716 if (!contains_scalar_p (v->type))
4717 continue;
4719 for (fli = flp; fli; fli = fli->next)
4720 if (fli->f == f)
4721 break;
4722 if (!fli->started_p)
4724 fli->started_p = 1;
4726 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4727 put_mangled_filename (f, v->line.file);
4728 oprintf (f, "[] = {\n");
4731 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4732 v->name, v->name);
4735 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4736 "gt_pch_scalar_rtab");
4739 /* TRUE if type S has the GTY variable_size annotation. */
4741 static bool
4742 variable_size_p (const type_p s)
4744 options_p o;
4745 for (o = s->u.s.opt; o; o = o->next)
4746 if (strcmp (o->name, "variable_size") == 0)
4747 return true;
4748 return false;
4751 enum alloc_quantity
4752 { single, vector };
4754 /* Writes one typed allocator definition into output F for type
4755 identifier TYPE_NAME with optional type specifier TYPE_SPECIFIER.
4756 The allocator name will contain ALLOCATOR_TYPE. If VARIABLE_SIZE
4757 is true, the allocator will have an extra parameter specifying
4758 number of bytes to allocate. If QUANTITY is set to VECTOR, a
4759 vector allocator will be output. */
4761 static void
4762 write_typed_alloc_def (outf_p f,
4763 bool variable_size, const char *type_specifier,
4764 const char *type_name, const char *allocator_type,
4765 enum alloc_quantity quantity)
4767 bool two_args = variable_size && (quantity == vector);
4768 gcc_assert (f != NULL);
4769 const char *type_name_as_id = filter_type_name (type_name);
4770 oprintf (f, "#define ggc_alloc_%s%s", allocator_type, type_name_as_id);
4771 oprintf (f, "(%s%s%s) ",
4772 (variable_size ? "SIZE" : ""),
4773 (two_args ? ", " : ""),
4774 (quantity == vector) ? "n" : "");
4775 oprintf (f, "((%s%s *)", type_specifier, type_name);
4776 oprintf (f, "(ggc_internal_%salloc_stat (", allocator_type);
4777 if (variable_size)
4778 oprintf (f, "SIZE");
4779 else
4780 oprintf (f, "sizeof (%s%s)", type_specifier, type_name);
4781 if (quantity == vector)
4782 oprintf (f, ", n");
4783 oprintf (f, " MEM_STAT_INFO)))\n");
4784 if (type_name_as_id != type_name)
4785 free (CONST_CAST(char *, type_name_as_id));
4788 /* Writes a typed allocator definition into output F for a struct or
4789 union S, with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4791 static void
4792 write_typed_struct_alloc_def (outf_p f,
4793 const type_p s, const char *allocator_type,
4794 enum alloc_quantity quantity)
4796 gcc_assert (union_or_struct_p (s));
4797 write_typed_alloc_def (f, variable_size_p (s), get_type_specifier (s),
4798 s->u.s.tag, allocator_type, quantity);
4801 /* Writes a typed allocator definition into output F for a typedef P,
4802 with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4804 static void
4805 write_typed_typedef_alloc_def (outf_p f,
4806 const pair_p p, const char *allocator_type,
4807 enum alloc_quantity quantity)
4809 write_typed_alloc_def (f, variable_size_p (p->type), "", p->name,
4810 allocator_type, quantity);
4813 /* Writes typed allocator definitions into output F for the types in
4814 STRUCTURES and TYPEDEFS that are used by GC. */
4816 static void
4817 write_typed_alloc_defns (outf_p f,
4818 const type_p structures, const pair_p typedefs)
4820 type_p s;
4821 pair_p p;
4823 gcc_assert (f != NULL);
4824 oprintf (f,
4825 "\n/* Allocators for known structs and unions. */\n\n");
4826 for (s = structures; s; s = s->next)
4828 if (!USED_BY_TYPED_GC_P (s))
4829 continue;
4830 gcc_assert (union_or_struct_p (s));
4831 /* In plugin mode onput output ggc_alloc macro definitions
4832 relevant to plugin input files. */
4833 if (nb_plugin_files > 0
4834 && ((s->u.s.line.file == NULL) || !s->u.s.line.file->inpisplugin))
4835 continue;
4836 write_typed_struct_alloc_def (f, s, "", single);
4837 write_typed_struct_alloc_def (f, s, "cleared_", single);
4838 write_typed_struct_alloc_def (f, s, "vec_", vector);
4839 write_typed_struct_alloc_def (f, s, "cleared_vec_", vector);
4842 oprintf (f, "\n/* Allocators for known typedefs. */\n");
4843 for (p = typedefs; p; p = p->next)
4845 s = p->type;
4846 if (!USED_BY_TYPED_GC_P (s) || (strcmp (p->name, s->u.s.tag) == 0))
4847 continue;
4848 /* In plugin mode onput output ggc_alloc macro definitions
4849 relevant to plugin input files. */
4850 if (nb_plugin_files > 0)
4852 struct fileloc* filoc = type_fileloc(s);
4853 if (!filoc || !filoc->file->inpisplugin)
4854 continue;
4856 write_typed_typedef_alloc_def (f, p, "", single);
4857 write_typed_typedef_alloc_def (f, p, "cleared_", single);
4858 write_typed_typedef_alloc_def (f, p, "vec_", vector);
4859 write_typed_typedef_alloc_def (f, p, "cleared_vec_", vector);
4863 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4864 guaranteee for somewhat increased readability. If name conflicts do happen,
4865 this funcion will have to be adjusted to be more like
4866 output_mangled_typename. */
4868 static void
4869 output_typename (outf_p of, const_type_p t)
4871 switch (t->kind)
4873 case TYPE_STRING:
4874 oprintf (of, "str");
4875 break;
4876 case TYPE_SCALAR:
4877 oprintf (of, "scalar");
4878 break;
4879 case TYPE_POINTER:
4880 output_typename (of, t->u.p);
4881 break;
4882 case TYPE_STRUCT:
4883 case TYPE_USER_STRUCT:
4884 case TYPE_UNION:
4885 case TYPE_LANG_STRUCT:
4886 oprintf (of, "%s", t->u.s.tag);
4887 break;
4888 case TYPE_PARAM_STRUCT:
4890 int i;
4891 for (i = 0; i < NUM_PARAM; i++)
4892 if (t->u.param_struct.param[i] != NULL)
4894 output_typename (of, t->u.param_struct.param[i]);
4895 oprintf (of, "_");
4897 output_typename (of, t->u.param_struct.stru);
4898 break;
4900 case TYPE_NONE:
4901 case TYPE_UNDEFINED:
4902 case TYPE_ARRAY:
4903 gcc_unreachable ();
4907 /* Writes a typed GC allocator for type S that is suitable as a callback for
4908 the splay tree implementation in libiberty. */
4910 static void
4911 write_splay_tree_allocator_def (const_type_p s)
4913 outf_p of = get_output_file_with_visibility (NULL);
4914 oprintf (of, "void * ggc_alloc_splay_tree_");
4915 output_typename (of, s);
4916 oprintf (of, " (int sz, void * nl)\n");
4917 oprintf (of, "{\n");
4918 oprintf (of, " return ggc_splay_alloc (sz, nl);\n");
4919 oprintf (of, "}\n\n");
4922 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
4923 for the splay tree implementation in libiberty. */
4925 static void
4926 write_splay_tree_allocators (const_type_p param_structs)
4928 const_type_p s;
4930 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
4931 for (s = param_structs; s; s = s->next)
4932 if (s->gc_used == GC_POINTED_TO)
4934 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
4935 output_typename (header_file, s);
4936 oprintf (header_file, " (int, void *);\n");
4937 write_splay_tree_allocator_def (s);
4941 #define INDENT 2
4943 /* Dumps the value of typekind KIND. */
4945 static void
4946 dump_typekind (int indent, enum typekind kind)
4948 printf ("%*ckind = ", indent, ' ');
4949 switch (kind)
4951 case TYPE_SCALAR:
4952 printf ("TYPE_SCALAR");
4953 break;
4954 case TYPE_STRING:
4955 printf ("TYPE_STRING");
4956 break;
4957 case TYPE_STRUCT:
4958 printf ("TYPE_STRUCT");
4959 break;
4960 case TYPE_UNDEFINED:
4961 printf ("TYPE_UNDEFINED");
4962 break;
4963 case TYPE_USER_STRUCT:
4964 printf ("TYPE_USER_STRUCT");
4965 break;
4966 case TYPE_UNION:
4967 printf ("TYPE_UNION");
4968 break;
4969 case TYPE_POINTER:
4970 printf ("TYPE_POINTER");
4971 break;
4972 case TYPE_ARRAY:
4973 printf ("TYPE_ARRAY");
4974 break;
4975 case TYPE_LANG_STRUCT:
4976 printf ("TYPE_LANG_STRUCT");
4977 break;
4978 case TYPE_PARAM_STRUCT:
4979 printf ("TYPE_PARAM_STRUCT");
4980 break;
4981 default:
4982 gcc_unreachable ();
4984 printf ("\n");
4987 /* Dumps the value of GC_USED flag. */
4989 static void
4990 dump_gc_used (int indent, enum gc_used_enum gc_used)
4992 printf ("%*cgc_used = ", indent, ' ');
4993 switch (gc_used)
4995 case GC_UNUSED:
4996 printf ("GC_UNUSED");
4997 break;
4998 case GC_USED:
4999 printf ("GC_USED");
5000 break;
5001 case GC_MAYBE_POINTED_TO:
5002 printf ("GC_MAYBE_POINTED_TO");
5003 break;
5004 case GC_POINTED_TO:
5005 printf ("GC_POINTED_TO");
5006 break;
5007 default:
5008 gcc_unreachable ();
5010 printf ("\n");
5013 /* Dumps the type options OPT. */
5015 static void
5016 dump_options (int indent, options_p opt)
5018 options_p o;
5019 printf ("%*coptions = ", indent, ' ');
5020 o = opt;
5021 while (o)
5023 switch (o->kind)
5025 case OPTION_STRING:
5026 printf ("%s:string %s ", o->name, o->info.string);
5027 break;
5028 case OPTION_TYPE:
5029 printf ("%s:type ", o->name);
5030 dump_type (indent+1, o->info.type);
5031 break;
5032 case OPTION_NESTED:
5033 printf ("%s:nested ", o->name);
5034 break;
5035 case OPTION_NONE:
5036 gcc_unreachable ();
5038 o = o->next;
5040 printf ("\n");
5043 /* Dumps the source file location in LINE. */
5045 static void
5046 dump_fileloc (int indent, struct fileloc line)
5048 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
5049 get_input_file_name (line.file),
5050 line.line);
5053 /* Recursively dumps the struct, union, or a language-specific
5054 struct T. */
5056 static void
5057 dump_type_u_s (int indent, type_p t)
5059 pair_p fields;
5061 gcc_assert (union_or_struct_p (t));
5062 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
5063 dump_fileloc (indent, t->u.s.line);
5064 printf ("%*cu.s.fields =\n", indent, ' ');
5065 fields = t->u.s.fields;
5066 while (fields)
5068 dump_pair (indent + INDENT, fields);
5069 fields = fields->next;
5071 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
5072 dump_options (indent, t->u.s.opt);
5073 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
5074 if (t->kind == TYPE_LANG_STRUCT)
5076 printf ("%*cu.s.lang_struct:\n", indent, ' ');
5077 dump_type_list (indent + INDENT, t->u.s.lang_struct);
5081 /* Recursively dumps the array T. */
5083 static void
5084 dump_type_u_a (int indent, type_p t)
5086 gcc_assert (t->kind == TYPE_ARRAY);
5087 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
5088 dump_type_list (indent + INDENT, t->u.a.p);
5091 /* Recursively dumps the parameterized struct T. */
5093 static void
5094 dump_type_u_param_struct (int indent, type_p t)
5096 int i;
5097 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
5098 printf ("%*cu.param_struct.stru:\n", indent, ' ');
5099 dump_type_list (indent, t->u.param_struct.stru);
5100 dump_fileloc (indent, t->u.param_struct.line);
5101 for (i = 0; i < NUM_PARAM; i++)
5103 if (t->u.param_struct.param[i] == NULL)
5104 continue;
5105 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
5106 dump_type (indent + INDENT, t->u.param_struct.param[i]);
5110 /* Recursively dumps the type list T. */
5112 static void
5113 dump_type_list (int indent, type_p t)
5115 type_p p = t;
5116 while (p)
5118 dump_type (indent, p);
5119 p = p->next;
5123 static htab_t seen_types;
5125 /* Recursively dumps the type T if it was not dumped previously. */
5127 static void
5128 dump_type (int indent, type_p t)
5130 PTR *slot;
5132 if (seen_types == NULL)
5133 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
5135 printf ("%*cType at %p: ", indent, ' ', (void *) t);
5136 slot = htab_find_slot (seen_types, t, INSERT);
5137 if (*slot != NULL)
5139 printf ("already seen.\n");
5140 return;
5142 *slot = t;
5143 printf ("\n");
5145 dump_typekind (indent, t->kind);
5146 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
5147 (void *) t->pointer_to);
5148 dump_gc_used (indent + INDENT, t->gc_used);
5149 switch (t->kind)
5151 case TYPE_SCALAR:
5152 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
5153 t->u.scalar_is_char ? "true" : "false");
5154 break;
5155 case TYPE_STRING:
5156 break;
5157 case TYPE_STRUCT:
5158 case TYPE_UNION:
5159 case TYPE_LANG_STRUCT:
5160 case TYPE_USER_STRUCT:
5161 dump_type_u_s (indent + INDENT, t);
5162 break;
5163 case TYPE_POINTER:
5164 printf ("%*cp:\n", indent + INDENT, ' ');
5165 dump_type (indent + INDENT, t->u.p);
5166 break;
5167 case TYPE_ARRAY:
5168 dump_type_u_a (indent + INDENT, t);
5169 break;
5170 case TYPE_PARAM_STRUCT:
5171 dump_type_u_param_struct (indent + INDENT, t);
5172 break;
5173 default:
5174 gcc_unreachable ();
5176 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
5179 /* Dumps the pair P. */
5181 static void
5182 dump_pair (int indent, pair_p p)
5184 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
5185 dump_type (indent, p->type);
5186 dump_fileloc (indent, p->line);
5187 dump_options (indent, p->opt);
5188 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
5191 /* Dumps the list of pairs PP. */
5193 static void
5194 dump_pair_list (const char *name, pair_p pp)
5196 pair_p p;
5197 printf ("%s:\n", name);
5198 for (p = pp; p != NULL; p = p->next)
5199 dump_pair (0, p);
5200 printf ("End of %s\n\n", name);
5203 /* Dumps the STRUCTURES. */
5205 static void
5206 dump_structures (const char *name, type_p structures)
5208 printf ("%s:\n", name);
5209 dump_type_list (0, structures);
5210 printf ("End of %s\n\n", name);
5213 /* Dumps the internal structures of gengtype. This is useful to debug
5214 gengtype itself, or to understand what it does, e.g. for plugin
5215 developers. */
5217 static void
5218 dump_everything (void)
5220 dump_pair_list ("typedefs", typedefs);
5221 dump_structures ("structures", structures);
5222 dump_structures ("param_structs", param_structs);
5223 dump_pair_list ("variables", variables);
5225 /* Allocated with the first call to dump_type. */
5226 htab_delete (seen_types);
5231 /* Option specification for getopt_long. */
5232 static const struct option gengtype_long_options[] = {
5233 {"help", no_argument, NULL, 'h'},
5234 {"version", no_argument, NULL, 'V'},
5235 {"verbose", no_argument, NULL, 'v'},
5236 {"dump", no_argument, NULL, 'd'},
5237 {"debug", no_argument, NULL, 'D'},
5238 {"plugin", required_argument, NULL, 'P'},
5239 {"srcdir", required_argument, NULL, 'S'},
5240 {"backupdir", required_argument, NULL, 'B'},
5241 {"inputs", required_argument, NULL, 'I'},
5242 {"read-state", required_argument, NULL, 'r'},
5243 {"write-state", required_argument, NULL, 'w'},
5244 /* Terminating NULL placeholder. */
5245 {NULL, no_argument, NULL, 0},
5249 static void
5250 print_usage (void)
5252 printf ("Usage: %s\n", progname);
5253 printf ("\t -h | --help " " \t# Give this help.\n");
5254 printf ("\t -D | --debug "
5255 " \t# Give debug output to debug %s itself.\n", progname);
5256 printf ("\t -V | --version " " \t# Give version information.\n");
5257 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5258 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5259 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5260 " \t# Generate for plugin.\n");
5261 printf ("\t -S | --srcdir <GCC-directory> "
5262 " \t# Specify the GCC source directory.\n");
5263 printf ("\t -B | --backupdir <directory> "
5264 " \t# Specify the backup directory for updated files.\n");
5265 printf ("\t -I | --inputs <input-list> "
5266 " \t# Specify the file with source files list.\n");
5267 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5268 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5271 static void
5272 print_version (void)
5274 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5275 printf ("Report bugs: %s\n", bug_report_url);
5278 /* Parse the program options using getopt_long... */
5279 static void
5280 parse_program_options (int argc, char **argv)
5282 int opt = -1;
5283 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5284 gengtype_long_options, NULL)) >= 0)
5286 switch (opt)
5288 case 'h': /* --help */
5289 print_usage ();
5290 break;
5291 case 'V': /* --version */
5292 print_version ();
5293 break;
5294 case 'd': /* --dump */
5295 do_dump = 1;
5296 break;
5297 case 'D': /* --debug */
5298 do_debug = 1;
5299 break;
5300 case 'v': /* --verbose */
5301 verbosity_level++;
5302 break;
5303 case 'P': /* --plugin */
5304 if (optarg)
5305 plugin_output_filename = optarg;
5306 else
5307 fatal ("missing plugin output file name");
5308 break;
5309 case 'S': /* --srcdir */
5310 if (optarg)
5311 srcdir = optarg;
5312 else
5313 fatal ("missing source directory");
5314 srcdir_len = strlen (srcdir);
5315 break;
5316 case 'B': /* --backupdir */
5317 if (optarg)
5318 backup_dir = optarg;
5319 else
5320 fatal ("missing backup directory");
5321 break;
5322 case 'I': /* --inputs */
5323 if (optarg)
5324 inputlist = optarg;
5325 else
5326 fatal ("missing input list");
5327 break;
5328 case 'r': /* --read-state */
5329 if (optarg)
5330 read_state_filename = optarg;
5331 else
5332 fatal ("missing read state file");
5333 DBGPRINTF ("read state %s\n", optarg);
5334 break;
5335 case 'w': /* --write-state */
5336 DBGPRINTF ("write state %s\n", optarg);
5337 if (optarg)
5338 write_state_filename = optarg;
5339 else
5340 fatal ("missing write state file");
5341 break;
5342 default:
5343 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5344 print_usage ();
5345 fatal ("unexpected flag");
5348 if (plugin_output_filename)
5350 /* In plugin mode we require some input files. */
5351 int i = 0;
5352 if (optind >= argc)
5353 fatal ("no source files given in plugin mode");
5354 nb_plugin_files = argc - optind;
5355 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5356 for (i = 0; i < (int) nb_plugin_files; i++)
5358 char *name = argv[i + optind];
5359 plugin_files[i] = input_file_by_name (name);
5366 /******* Manage input files. ******/
5368 /* Hash table of unique input file names. */
5369 static htab_t input_file_htab;
5371 /* Find or allocate a new input_file by hash-consing it. */
5372 input_file*
5373 input_file_by_name (const char* name)
5375 PTR* slot;
5376 input_file* f = NULL;
5377 int namlen = 0;
5378 if (!name)
5379 return NULL;
5380 namlen = strlen (name);
5381 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5382 f->inpbitmap = 0;
5383 f->inpoutf = NULL;
5384 f->inpisplugin = false;
5385 strcpy (f->inpname, name);
5386 slot = htab_find_slot (input_file_htab, f, INSERT);
5387 gcc_assert (slot != NULL);
5388 if (*slot)
5390 /* Already known input file. */
5391 free (f);
5392 return (input_file*)(*slot);
5394 /* New input file. */
5395 *slot = f;
5396 return f;
5399 /* Hash table support routines for input_file-s. */
5400 static hashval_t
5401 htab_hash_inputfile (const void *p)
5403 const input_file *inpf = (const input_file *) p;
5404 gcc_assert (inpf);
5405 return htab_hash_string (get_input_file_name (inpf));
5408 static int
5409 htab_eq_inputfile (const void *x, const void *y)
5411 const input_file *inpfx = (const input_file *) x;
5412 const input_file *inpfy = (const input_file *) y;
5413 gcc_assert (inpfx != NULL && inpfy != NULL);
5414 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5419 main (int argc, char **argv)
5421 size_t i;
5422 static struct fileloc pos = { NULL, 0 };
5423 outf_p output_header;
5425 /* Mandatory common initializations. */
5426 progname = "gengtype"; /* For fatal and messages. */
5427 /* Create the hash-table used to hash-cons input files. */
5428 input_file_htab =
5429 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5430 /* Initialize our special input files. */
5431 this_file = input_file_by_name (__FILE__);
5432 system_h_file = input_file_by_name ("system.h");
5433 /* Set the scalar_is_char union number for predefined scalar types. */
5434 scalar_nonchar.u.scalar_is_char = FALSE;
5435 scalar_char.u.scalar_is_char = TRUE;
5437 parse_program_options (argc, argv);
5439 #if ENABLE_CHECKING
5440 if (do_debug)
5442 time_t now = (time_t) 0;
5443 time (&now);
5444 DBGPRINTF ("gengtype started pid %d at %s",
5445 (int) getpid (), ctime (&now));
5447 #endif /* ENABLE_CHECKING */
5449 /* Parse the input list and the input files. */
5450 DBGPRINTF ("inputlist %s", inputlist);
5451 if (read_state_filename)
5453 if (inputlist)
5454 fatal ("input list %s cannot be given with a read state file %s",
5455 inputlist, read_state_filename);
5456 read_state (read_state_filename);
5457 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5458 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs);
5460 else if (inputlist)
5462 /* These types are set up with #define or else outside of where
5463 we can see them. We should initialize them before calling
5464 read_input_list. */
5465 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5466 Call;} while(0)
5467 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5468 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5469 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5470 POS_HERE (do_scalar_typedef ("double_int", &pos));
5471 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5472 POS_HERE (do_scalar_typedef ("uint8", &pos));
5473 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5474 POS_HERE (do_scalar_typedef ("jword", &pos));
5475 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5476 POS_HERE (do_scalar_typedef ("void", &pos));
5477 POS_HERE (do_typedef ("PTR",
5478 create_pointer (resolve_typedef ("void", &pos)),
5479 &pos));
5480 #undef POS_HERE
5481 read_input_list (inputlist);
5482 for (i = 0; i < num_gt_files; i++)
5484 parse_file (get_input_file_name (gt_files[i]));
5485 DBGPRINTF ("parsed file #%d %s",
5486 (int) i, get_input_file_name (gt_files[i]));
5488 if (verbosity_level >= 1)
5489 printf ("%s parsed %d files with %d GTY types\n",
5490 progname, (int) num_gt_files, type_count);
5492 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5493 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
5496 else
5497 fatal ("either an input list or a read state file should be given");
5498 if (hit_error)
5499 return 1;
5502 if (plugin_output_filename)
5504 size_t ix = 0;
5505 /* In plugin mode, we should have read a state file, and have
5506 given at least one plugin file. */
5507 if (!read_state_filename)
5508 fatal ("No read state given in plugin mode for %s",
5509 plugin_output_filename);
5511 if (nb_plugin_files == 0 || !plugin_files)
5512 fatal ("No plugin files given in plugin mode for %s",
5513 plugin_output_filename);
5515 /* Parse our plugin files and augment the state. */
5516 for (ix = 0; ix < nb_plugin_files; ix++)
5518 input_file* pluginput = plugin_files [ix];
5519 pluginput->inpisplugin = true;
5520 parse_file (get_input_file_name (pluginput));
5522 if (hit_error)
5523 return 1;
5525 plugin_output = create_file ("GCC", plugin_output_filename);
5526 DBGPRINTF ("created plugin_output %p named %s",
5527 (void *) plugin_output, plugin_output->name);
5529 else
5530 { /* No plugin files, we are in normal mode. */
5531 if (!srcdir)
5532 fatal ("gengtype needs a source directory in normal mode");
5534 if (hit_error)
5535 return 1;
5537 gen_rtx_next ();
5539 /* The call to set_gc_used may indirectly call find_param_structure
5540 hence enlarge the param_structs list of types. */
5541 set_gc_used (variables);
5543 /* The state at this point is read from the state input file or by
5544 parsing source files and optionally augmented by parsing plugin
5545 source files. Write it now. */
5546 if (write_state_filename)
5548 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5549 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs);
5551 if (hit_error)
5552 fatal ("didn't write state file %s after errors",
5553 write_state_filename);
5555 DBGPRINTF ("before write_state %s", write_state_filename);
5556 write_state (write_state_filename);
5558 if (do_dump)
5559 dump_everything ();
5561 /* After having written the state file we return immediately to
5562 avoid generating any output file. */
5563 if (hit_error)
5564 return 1;
5565 else
5566 return 0;
5570 open_base_files ();
5572 output_header = plugin_output ? plugin_output : header_file;
5573 write_typed_alloc_defns (output_header, structures, typedefs);
5574 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5575 structures);
5576 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5577 param_structs);
5579 write_types (output_header, structures, param_structs, &ggc_wtd);
5580 if (plugin_files == NULL)
5582 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5583 structures);
5584 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5585 param_structs);
5586 write_types (header_file, structures, param_structs, &pch_wtd);
5587 write_local (header_file, structures, param_structs);
5589 write_splay_tree_allocators (param_structs);
5590 write_roots (variables, plugin_files == NULL);
5591 write_rtx_next ();
5592 close_output_files ();
5594 if (do_dump)
5595 dump_everything ();
5597 /* Don't bother about free-ing any input or plugin file, etc. */
5599 if (hit_error)
5600 return 1;
5601 return 0;