2013-10-01 Saurabh Verma <saurabh.verma@codito.com>
[official-gcc.git] / gcc / gengtype.c
blobb9bef4df76c12ae2e55ff40c3d71f0eeb1129a6f
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-ssa.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 or *.cc files, we
2008 * need 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},
2016 /* Source *.cc files are using get_file_gtfilename to compute their
2017 output_name and get_file_basename to compute their for_name
2018 through the source_dot_c_frul action. */
2019 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.cc$",
2020 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.cc", source_dot_c_frul},
2022 /* Common header files get "gtype-desc.c" as their output_name,
2023 * while language specific header files are handled specially. So
2024 * we need the header_dot_h_frul action. */
2025 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
2026 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
2028 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
2029 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
2031 /* Mandatory null last entry signaling end of rules. */
2032 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
2035 /* Special file rules action for handling *.h header files. It gives
2036 "gtype-desc.c" for common headers and corresponding output
2037 files for language-specific header files. */
2038 static outf_p
2039 header_dot_h_frul (input_file* inpf, char**poutname,
2040 char**pforname ATTRIBUTE_UNUSED)
2042 const char *basename = 0;
2043 int lang_index = 0;
2044 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2045 (void*) inpf, get_input_file_name (inpf),
2046 *poutname, *pforname);
2047 basename = get_file_basename (inpf);
2048 lang_index = get_prefix_langdir_index (basename);
2049 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
2051 if (lang_index >= 0)
2053 /* The header is language specific. Given output_name &
2054 for_name remains unchanged. The base_files array gives the
2055 outf_p. */
2056 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2057 (void*) base_files[lang_index],
2058 (base_files[lang_index])->name);
2059 return base_files[lang_index];
2061 else
2063 /* The header is common to all front-end languages. So
2064 output_name is "gtype-desc.c" file. The calling function
2065 get_output_file_with_visibility will find its outf_p. */
2066 free (*poutname);
2067 *poutname = xstrdup ("gtype-desc.c");
2068 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2069 get_input_file_name (inpf));
2070 return NULL;
2075 /* Special file rules action for handling *.c source files using
2076 * get_file_gtfilename to compute their output_name and
2077 * get_file_basename to compute their for_name. The output_name is
2078 * gt-<LANG>-<BASE>.h for language specific source files, and
2079 * gt-<BASE>.h for common source files. */
2080 static outf_p
2081 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
2083 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
2084 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
2085 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2086 (void*) inpf, get_input_file_name (inpf),
2087 *poutname, *pforname);
2088 DBGPRINTF ("newoutname %s", newoutname);
2089 DBGPRINTF ("newbasename %s", newbasename);
2090 free (*poutname);
2091 free (*pforname);
2092 *poutname = newoutname;
2093 *pforname = newbasename;
2094 return NULL;
2097 /* Utility function for get_output_file_with_visibility which returns
2098 * a malloc-ed substituted string using TRS on matching of the FILNAM
2099 * file name, using the PMATCH array. */
2100 static char*
2101 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
2102 const char *trs)
2104 struct obstack str_obstack;
2105 char *str = NULL;
2106 char *rawstr = NULL;
2107 const char *pt = NULL;
2108 DBGPRINTF ("filnam %s", filnam);
2109 obstack_init (&str_obstack);
2110 for (pt = trs; *pt; pt++) {
2111 char c = *pt;
2112 if (c == '$')
2114 if (pt[1] == '$')
2116 /* A double dollar $$ is substituted by a single verbatim
2117 dollar, but who really uses dollar signs in file
2118 paths? */
2119 obstack_1grow (&str_obstack, '$');
2121 else if (ISDIGIT (pt[1]))
2123 /* Handle $0 $1 ... $9 by appropriate substitution. */
2124 int dolnum = pt[1] - '0';
2125 int so = pmatch[dolnum].rm_so;
2126 int eo = pmatch[dolnum].rm_eo;
2127 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2128 if (so>=0 && eo>=so)
2129 obstack_grow (&str_obstack, filnam + so, eo - so);
2131 else
2133 /* This can happen only when files_rules is buggy! */
2134 gcc_unreachable ();
2136 /* Always skip the character after the dollar. */
2137 pt++;
2139 else
2140 obstack_1grow (&str_obstack, c);
2142 obstack_1grow (&str_obstack, '\0');
2143 rawstr = XOBFINISH (&str_obstack, char *);
2144 str = xstrdup (rawstr);
2145 obstack_free (&str_obstack, NULL);
2146 DBGPRINTF ("matched replacement %s", str);
2147 rawstr = NULL;
2148 return str;
2152 /* An output file, suitable for definitions, that can see declarations
2153 made in INPF and is linked into every language that uses INPF.
2154 Since the result is cached inside INPF, that argument cannot be
2155 declared constant, but is "almost" constant. */
2157 outf_p
2158 get_output_file_with_visibility (input_file *inpf)
2160 outf_p r;
2161 char *for_name = NULL;
2162 char *output_name = NULL;
2163 const char* inpfname;
2165 /* This can happen when we need a file with visibility on a
2166 structure that we've never seen. We have to just hope that it's
2167 globally visible. */
2168 if (inpf == NULL)
2169 inpf = system_h_file;
2171 /* The result is cached in INPF, so return it if already known. */
2172 if (inpf->inpoutf)
2173 return inpf->inpoutf;
2175 /* In plugin mode, return NULL unless the input_file is one of the
2176 plugin_files. */
2177 if (plugin_files)
2179 size_t i;
2180 for (i = 0; i < nb_plugin_files; i++)
2181 if (inpf == plugin_files[i])
2183 inpf->inpoutf = plugin_output;
2184 return plugin_output;
2187 return NULL;
2190 inpfname = get_input_file_name (inpf);
2192 /* Try each rule in sequence in files_rules until one is triggered. */
2194 int rulix = 0;
2195 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2196 (void*) inpf, inpfname);
2198 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2200 DBGPRINTF ("rulix#%d srcexpr %s",
2201 rulix, files_rules[rulix].frul_srcexpr);
2203 if (!files_rules[rulix].frul_re)
2205 /* Compile the regexpr lazily. */
2206 int err = 0;
2207 files_rules[rulix].frul_re = XCNEW (regex_t);
2208 err = regcomp (files_rules[rulix].frul_re,
2209 files_rules[rulix].frul_srcexpr,
2210 files_rules[rulix].frul_rflags);
2211 if (err)
2213 /* The regular expression compilation fails only when
2214 file_rules is buggy. */
2215 gcc_unreachable ();
2219 output_name = NULL;
2220 for_name = NULL;
2222 /* Match the regexpr and trigger the rule if matched. */
2224 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2225 $3, ... $9. */
2226 regmatch_t pmatch[10];
2227 memset (pmatch, 0, sizeof (pmatch));
2228 if (!regexec (files_rules[rulix].frul_re,
2229 inpfname, 10, pmatch, 0))
2231 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2232 (void*) inpf, inpfname, rulix,
2233 files_rules[rulix].frul_srcexpr);
2234 for_name =
2235 matching_file_name_substitute (inpfname, pmatch,
2236 files_rules[rulix].frul_tr_for);
2237 DBGPRINTF ("for_name %s", for_name);
2238 output_name =
2239 matching_file_name_substitute (inpfname, pmatch,
2240 files_rules[rulix].frul_tr_out);
2241 DBGPRINTF ("output_name %s", output_name);
2242 if (files_rules[rulix].frul_action)
2244 /* Invoke our action routine. */
2245 outf_p of = NULL;
2246 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2247 rulix, output_name, for_name);
2248 of =
2249 (files_rules[rulix].frul_action) (inpf,
2250 &output_name, &for_name);
2251 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2252 rulix, (void*)of, output_name, for_name);
2253 /* If the action routine returned something, give it back
2254 immediately and cache it in inpf. */
2255 if (of)
2257 inpf->inpoutf = of;
2258 return of;
2261 /* The rule matched, and had no action, or that action did
2262 not return any output file but could have changed the
2263 output_name or for_name. We break out of the loop on the
2264 files_rules. */
2265 break;
2267 else
2269 /* The regexpr did not match. */
2270 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2271 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2272 continue;
2277 if (!output_name || !for_name)
2279 /* This should not be possible, and could only happen if the
2280 files_rules is incomplete or buggy. */
2281 fatal ("failed to compute output name for %s", inpfname);
2284 /* Look through to see if we've ever seen this output filename
2285 before. If found, cache the result in inpf. */
2286 for (r = output_files; r; r = r->next)
2287 if (filename_cmp (r->name, output_name) == 0)
2289 inpf->inpoutf = r;
2290 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2291 output_name, for_name);
2292 return r;
2295 /* If not found, create it, and cache it in inpf. */
2296 r = create_file (for_name, output_name);
2298 gcc_assert (r && r->name);
2299 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2300 output_name, for_name);
2301 inpf->inpoutf = r;
2302 return r;
2307 /* The name of an output file, suitable for definitions, that can see
2308 declarations made in INPF and is linked into every language that
2309 uses INPF. */
2311 const char *
2312 get_output_file_name (input_file* inpf)
2314 outf_p o = get_output_file_with_visibility (inpf);
2315 if (o)
2316 return o->name;
2317 return NULL;
2320 /* Check if existing file is equal to the in memory buffer. */
2322 static bool
2323 is_file_equal (outf_p of)
2325 FILE *newfile = fopen (of->name, "r");
2326 size_t i;
2327 bool equal;
2328 if (newfile == NULL)
2329 return false;
2331 equal = true;
2332 for (i = 0; i < of->bufused; i++)
2334 int ch;
2335 ch = fgetc (newfile);
2336 if (ch == EOF || ch != (unsigned char) of->buf[i])
2338 equal = false;
2339 break;
2342 fclose (newfile);
2343 return equal;
2346 /* Copy the output to its final destination,
2347 but don't unnecessarily change modification times. */
2349 static void
2350 close_output_files (void)
2352 int nbwrittenfiles = 0;
2353 outf_p of;
2355 for (of = output_files; of; of = of->next)
2357 if (!is_file_equal (of))
2359 FILE *newfile = NULL;
2360 char *backupname = NULL;
2361 /* Back up the old version of the output file gt-FOO.c as
2362 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2363 if (backup_dir)
2365 backupname = concat (backup_dir, "/",
2366 lbasename (of->name), "~", NULL);
2367 if (!access (of->name, F_OK) && rename (of->name, backupname))
2368 fatal ("failed to back up %s as %s: %s",
2369 of->name, backupname, xstrerror (errno));
2372 newfile = fopen (of->name, "w");
2373 if (newfile == NULL)
2374 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2375 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2376 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2377 if (fclose (newfile) != 0)
2378 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2379 nbwrittenfiles++;
2380 if (verbosity_level >= 2 && backupname)
2381 printf ("%s wrote #%-3d %s backed-up in %s\n",
2382 progname, nbwrittenfiles, of->name, backupname);
2383 else if (verbosity_level >= 1)
2384 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2385 free (backupname);
2387 else
2389 /* output file remains unchanged. */
2390 if (verbosity_level >= 2)
2391 printf ("%s keep %s\n", progname, of->name);
2393 free (of->buf);
2394 of->buf = NULL;
2395 of->bufused = of->buflength = 0;
2397 if (verbosity_level >= 1)
2398 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2401 struct flist
2403 struct flist *next;
2404 int started_p;
2405 const input_file* file;
2406 outf_p f;
2409 struct walk_type_data;
2411 /* For scalars and strings, given the item in 'val'.
2412 For structures, given a pointer to the item in 'val'.
2413 For misc. pointers, given the item in 'val'.
2415 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2416 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2418 /* Parameters for write_types. */
2420 struct write_types_data
2422 const char *prefix;
2423 const char *param_prefix;
2424 const char *subfield_marker_routine;
2425 const char *marker_routine;
2426 const char *reorder_note_routine;
2427 const char *comment;
2428 int skip_hooks; /* skip hook generation if non zero */
2431 static void output_escaped_param (struct walk_type_data *d,
2432 const char *, const char *);
2433 static void output_mangled_typename (outf_p, const_type_p);
2434 static void walk_type (type_p t, struct walk_type_data *d);
2435 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2436 const struct write_types_data *wtd);
2437 static void write_types_process_field
2438 (type_p f, const struct walk_type_data *d);
2439 static void write_types (outf_p output_header,
2440 type_p structures,
2441 type_p param_structs,
2442 const struct write_types_data *wtd);
2443 static void write_types_local_process_field
2444 (type_p f, const struct walk_type_data *d);
2445 static void write_local_func_for_structure
2446 (const_type_p orig_s, type_p s, type_p *param);
2447 static void write_local (outf_p output_header,
2448 type_p structures, type_p param_structs);
2449 static int contains_scalar_p (type_p t);
2450 static void put_mangled_filename (outf_p, const input_file *);
2451 static void finish_root_table (struct flist *flp, const char *pfx,
2452 const char *tname, const char *lastname,
2453 const char *name);
2454 static void write_root (outf_p, pair_p, type_p, const char *, int,
2455 struct fileloc *, const char *, bool);
2456 static void write_array (outf_p f, pair_p v,
2457 const struct write_types_data *wtd);
2458 static void write_roots (pair_p, bool);
2460 /* Parameters for walk_type. */
2462 struct walk_type_data
2464 process_field_fn process_field;
2465 const void *cookie;
2466 outf_p of;
2467 options_p opt;
2468 const char *val;
2469 const char *prev_val[4];
2470 int indent;
2471 int counter;
2472 const struct fileloc *line;
2473 lang_bitmap bitmap;
2474 type_p *param;
2475 int used_length;
2476 type_p orig_s;
2477 const char *reorder_fn;
2478 bool needs_cast_p;
2479 bool fn_wants_lvalue;
2480 bool in_record_p;
2481 int loopcounter;
2482 bool in_ptr_field;
2483 bool have_this_obj;
2487 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2488 pre-processor identifier to use in a #define directive. This replaces
2489 special characters used in C++ identifiers like '>', '<' and ':' with
2490 '_'.
2492 If no C++ special characters are found in TYPE_NAME, return
2493 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2494 characters replaced with '_'. In this case, the caller is
2495 responsible for freeing the allocated string. */
2497 static const char *
2498 filter_type_name (const char *type_name)
2500 if (strchr (type_name, '<') || strchr (type_name, ':'))
2502 size_t i;
2503 char *s = xstrdup (type_name);
2504 for (i = 0; i < strlen (s); i++)
2505 if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ',')
2506 s[i] = '_';
2507 return s;
2509 else
2510 return type_name;
2514 /* Print a mangled name representing T to OF. */
2516 static void
2517 output_mangled_typename (outf_p of, const_type_p t)
2519 if (t == NULL)
2520 oprintf (of, "Z");
2521 else
2522 switch (t->kind)
2524 case TYPE_NONE:
2525 case TYPE_UNDEFINED:
2526 gcc_unreachable ();
2527 break;
2528 case TYPE_POINTER:
2529 oprintf (of, "P");
2530 output_mangled_typename (of, t->u.p);
2531 break;
2532 case TYPE_SCALAR:
2533 oprintf (of, "I");
2534 break;
2535 case TYPE_STRING:
2536 oprintf (of, "S");
2537 break;
2538 case TYPE_STRUCT:
2539 case TYPE_UNION:
2540 case TYPE_LANG_STRUCT:
2541 case TYPE_USER_STRUCT:
2543 const char *id_for_tag = filter_type_name (t->u.s.tag);
2544 oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2545 id_for_tag);
2546 if (id_for_tag != t->u.s.tag)
2547 free (CONST_CAST (char *, id_for_tag));
2549 break;
2550 case TYPE_PARAM_STRUCT:
2552 int i;
2553 for (i = 0; i < NUM_PARAM; i++)
2554 if (t->u.param_struct.param[i] != NULL)
2555 output_mangled_typename (of, t->u.param_struct.param[i]);
2556 output_mangled_typename (of, t->u.param_struct.stru);
2558 break;
2559 case TYPE_ARRAY:
2560 gcc_unreachable ();
2564 /* Print PARAM to D->OF processing escapes. D->VAL references the
2565 current object, D->PREV_VAL the object containing the current
2566 object, ONAME is the name of the option and D->LINE is used to
2567 print error messages. */
2569 static void
2570 output_escaped_param (struct walk_type_data *d, const char *param,
2571 const char *oname)
2573 const char *p;
2575 for (p = param; *p; p++)
2576 if (*p != '%')
2577 oprintf (d->of, "%c", *p);
2578 else
2579 switch (*++p)
2581 case 'h':
2582 oprintf (d->of, "(%s)", d->prev_val[2]);
2583 break;
2584 case '0':
2585 oprintf (d->of, "(%s)", d->prev_val[0]);
2586 break;
2587 case '1':
2588 oprintf (d->of, "(%s)", d->prev_val[1]);
2589 break;
2590 case 'a':
2592 const char *pp = d->val + strlen (d->val);
2593 while (pp[-1] == ']')
2594 while (*pp != '[')
2595 pp--;
2596 oprintf (d->of, "%s", pp);
2598 break;
2599 default:
2600 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2601 oname, '%', *p);
2606 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2607 which is of type T. Write code to D->OF to constrain execution (at
2608 the point that D->PROCESS_FIELD is called) to the appropriate
2609 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2610 pointers to those objects. D->PREV_VAL lists the objects
2611 containing the current object, D->OPT is a list of options to
2612 apply, D->INDENT is the current indentation level, D->LINE is used
2613 to print error messages, D->BITMAP indicates which languages to
2614 print the structure for, and D->PARAM is the current parameter
2615 (from an enclosing param_is option). */
2617 static void
2618 walk_type (type_p t, struct walk_type_data *d)
2620 const char *length = NULL;
2621 const char *desc = NULL;
2622 int maybe_undef_p = 0;
2623 int use_param_num = -1;
2624 int use_params_p = 0;
2625 int atomic_p = 0;
2626 options_p oo;
2627 const struct nested_ptr_data *nested_ptr_d = NULL;
2629 d->needs_cast_p = false;
2630 for (oo = d->opt; oo; oo = oo->next)
2631 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2632 length = oo->info.string;
2633 else if (strcmp (oo->name, "maybe_undef") == 0)
2634 maybe_undef_p = 1;
2635 else if (strncmp (oo->name, "use_param", 9) == 0
2636 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2637 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2638 else if (strcmp (oo->name, "use_params") == 0)
2639 use_params_p = 1;
2640 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2641 desc = oo->info.string;
2642 else if (strcmp (oo->name, "mark_hook") == 0)
2644 else if (strcmp (oo->name, "nested_ptr") == 0
2645 && oo->kind == OPTION_NESTED)
2646 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2647 else if (strcmp (oo->name, "dot") == 0)
2649 else if (strcmp (oo->name, "tag") == 0)
2651 else if (strcmp (oo->name, "special") == 0)
2653 else if (strcmp (oo->name, "skip") == 0)
2655 else if (strcmp (oo->name, "atomic") == 0)
2656 atomic_p = 1;
2657 else if (strcmp (oo->name, "default") == 0)
2659 else if (strcmp (oo->name, "param_is") == 0)
2661 else if (strncmp (oo->name, "param", 5) == 0
2662 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2664 else if (strcmp (oo->name, "chain_next") == 0)
2666 else if (strcmp (oo->name, "chain_prev") == 0)
2668 else if (strcmp (oo->name, "chain_circular") == 0)
2670 else if (strcmp (oo->name, "reorder") == 0)
2672 else if (strcmp (oo->name, "variable_size") == 0)
2674 else
2675 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2677 if (d->used_length)
2678 length = NULL;
2680 if (use_params_p)
2682 int pointer_p = t->kind == TYPE_POINTER;
2684 if (pointer_p)
2685 t = t->u.p;
2686 if (!union_or_struct_p (t))
2687 error_at_line (d->line, "`use_params' option on unimplemented type");
2688 else
2689 t = find_param_structure (t, d->param);
2690 if (pointer_p)
2691 t = create_pointer (t);
2694 if (use_param_num != -1)
2696 if (d->param != NULL && d->param[use_param_num] != NULL)
2698 type_p nt = d->param[use_param_num];
2700 if (t->kind == TYPE_ARRAY)
2701 nt = create_array (nt, t->u.a.len);
2702 else if (length != NULL && t->kind == TYPE_POINTER)
2703 nt = create_pointer (nt);
2704 d->needs_cast_p = (t->kind != TYPE_POINTER
2705 && (nt->kind == TYPE_POINTER
2706 || nt->kind == TYPE_STRING));
2707 t = nt;
2709 else
2710 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2713 if (maybe_undef_p
2714 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
2716 error_at_line (d->line,
2717 "field `%s' has invalid option `maybe_undef_p'\n",
2718 d->val);
2719 return;
2722 if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2724 error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2725 return;
2728 switch (t->kind)
2730 case TYPE_SCALAR:
2731 case TYPE_STRING:
2732 d->process_field (t, d);
2733 break;
2735 case TYPE_POINTER:
2737 d->in_ptr_field = true;
2738 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2740 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2741 break;
2744 /* If a pointer type is marked as "atomic", we process the
2745 field itself, but we don't walk the data that they point to.
2747 There are two main cases where we walk types: to mark
2748 pointers that are reachable, and to relocate pointers when
2749 writing a PCH file. In both cases, an atomic pointer is
2750 itself marked or relocated, but the memory that it points
2751 to is left untouched. In the case of PCH, that memory will
2752 be read/written unchanged to the PCH file. */
2753 if (atomic_p)
2755 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2756 d->indent += 2;
2757 d->process_field (t, d);
2758 d->indent -= 2;
2759 oprintf (d->of, "%*s}\n", d->indent, "");
2760 break;
2763 if (!length)
2765 if (!union_or_struct_p (t->u.p)
2766 && t->u.p->kind != TYPE_PARAM_STRUCT)
2768 error_at_line (d->line,
2769 "field `%s' is pointer to unimplemented type",
2770 d->val);
2771 break;
2774 if (nested_ptr_d)
2776 const char *oldprevval2 = d->prev_val[2];
2778 if (!union_or_struct_p (nested_ptr_d->type))
2780 error_at_line (d->line,
2781 "field `%s' has invalid "
2782 "option `nested_ptr'\n", d->val);
2783 return;
2786 d->prev_val[2] = d->val;
2787 oprintf (d->of, "%*s{\n", d->indent, "");
2788 d->indent += 2;
2789 d->val = xasprintf ("x%d", d->counter++);
2790 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2791 (nested_ptr_d->type->kind == TYPE_UNION
2792 ? "union" : "struct"),
2793 nested_ptr_d->type->u.s.tag,
2794 d->fn_wants_lvalue ? "" : "const ", d->val);
2795 oprintf (d->of, "%*s", d->indent + 2, "");
2796 output_escaped_param (d, nested_ptr_d->convert_from,
2797 "nested_ptr");
2798 oprintf (d->of, ";\n");
2800 d->process_field (nested_ptr_d->type, d);
2802 if (d->fn_wants_lvalue)
2804 oprintf (d->of, "%*s%s = ", d->indent, "",
2805 d->prev_val[2]);
2806 d->prev_val[2] = d->val;
2807 output_escaped_param (d, nested_ptr_d->convert_to,
2808 "nested_ptr");
2809 oprintf (d->of, ";\n");
2812 d->indent -= 2;
2813 oprintf (d->of, "%*s}\n", d->indent, "");
2814 d->val = d->prev_val[2];
2815 d->prev_val[2] = oldprevval2;
2817 else
2818 d->process_field (t->u.p, d);
2820 else
2822 int loopcounter = d->loopcounter;
2823 const char *oldval = d->val;
2824 const char *oldprevval3 = d->prev_val[3];
2825 char *newval;
2827 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2828 d->indent += 2;
2829 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2830 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2831 "", loopcounter, loopcounter);
2832 if (!d->in_record_p)
2833 output_escaped_param (d, length, "length");
2834 else
2835 oprintf (d->of, "l%d", loopcounter);
2836 if (d->have_this_obj)
2837 /* Try to unswitch loops (see PR53880). */
2838 oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
2839 oprintf (d->of, "); i%d++) {\n", loopcounter);
2840 d->indent += 2;
2841 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2842 d->used_length = 1;
2843 d->prev_val[3] = oldval;
2844 walk_type (t->u.p, d);
2845 free (newval);
2846 d->val = oldval;
2847 d->prev_val[3] = oldprevval3;
2848 d->used_length = 0;
2849 d->indent -= 2;
2850 oprintf (d->of, "%*s}\n", d->indent, "");
2851 d->process_field (t, d);
2852 d->indent -= 2;
2853 oprintf (d->of, "%*s}\n", d->indent, "");
2855 d->in_ptr_field = false;
2857 break;
2859 case TYPE_ARRAY:
2861 int loopcounter;
2862 const char *oldval = d->val;
2863 char *newval;
2865 /* If it's an array of scalars, we optimize by not generating
2866 any code. */
2867 if (t->u.a.p->kind == TYPE_SCALAR)
2868 break;
2870 if (length)
2871 loopcounter = d->loopcounter;
2872 else
2873 loopcounter = d->counter++;
2875 /* When walking an array, compute the length and store it in a
2876 local variable before walking the array elements, instead of
2877 recomputing the length expression each time through the loop.
2878 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2879 where the length is stored in the first array element,
2880 because otherwise that operand can get overwritten on the
2881 first iteration. */
2882 oprintf (d->of, "%*s{\n", d->indent, "");
2883 d->indent += 2;
2884 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2885 if (!d->in_record_p || !length)
2887 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2888 d->indent, "", loopcounter);
2889 if (length)
2890 output_escaped_param (d, length, "length");
2891 else
2892 oprintf (d->of, "%s", t->u.a.len);
2893 oprintf (d->of, ");\n");
2896 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2897 d->indent, "",
2898 loopcounter, loopcounter, loopcounter, loopcounter);
2899 d->indent += 2;
2900 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2901 d->used_length = 1;
2902 walk_type (t->u.a.p, d);
2903 free (newval);
2904 d->used_length = 0;
2905 d->val = oldval;
2906 d->indent -= 2;
2907 oprintf (d->of, "%*s}\n", d->indent, "");
2908 d->indent -= 2;
2909 oprintf (d->of, "%*s}\n", d->indent, "");
2911 break;
2913 case TYPE_STRUCT:
2914 case TYPE_UNION:
2916 pair_p f;
2917 const char *oldval = d->val;
2918 const char *oldprevval1 = d->prev_val[1];
2919 const char *oldprevval2 = d->prev_val[2];
2920 const char *struct_mark_hook = NULL;
2921 const int union_p = t->kind == TYPE_UNION;
2922 int seen_default_p = 0;
2923 options_p o;
2924 int lengths_seen = 0;
2925 int endcounter;
2926 bool any_length_seen = false;
2928 if (!t->u.s.line.file)
2929 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
2931 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2933 error_at_line (d->line,
2934 "structure `%s' defined for mismatching languages",
2935 t->u.s.tag);
2936 error_at_line (&t->u.s.line, "one structure defined here");
2939 /* Some things may also be defined in the structure's options. */
2940 for (o = t->u.s.opt; o; o = o->next)
2941 if (!desc && strcmp (o->name, "desc") == 0
2942 && o->kind == OPTION_STRING)
2943 desc = o->info.string;
2944 else if (!struct_mark_hook && strcmp (o->name, "mark_hook") == 0
2945 && o->kind == OPTION_STRING)
2946 struct_mark_hook = o->info.string;
2948 if (struct_mark_hook)
2949 oprintf (d->of, "%*s%s (&%s);\n",
2950 d->indent, "", struct_mark_hook, oldval);
2952 d->prev_val[2] = oldval;
2953 d->prev_val[1] = oldprevval2;
2954 if (union_p)
2956 if (desc == NULL)
2958 error_at_line (d->line,
2959 "missing `desc' option for union `%s'",
2960 t->u.s.tag);
2961 desc = "1";
2963 oprintf (d->of, "%*sswitch (", d->indent, "");
2964 output_escaped_param (d, desc, "desc");
2965 oprintf (d->of, ")\n");
2966 d->indent += 2;
2967 oprintf (d->of, "%*s{\n", d->indent, "");
2970 for (f = t->u.s.fields; f; f = f->next)
2972 options_p oo;
2973 int skip_p = 0;
2974 const char *fieldlength = NULL;
2976 d->reorder_fn = NULL;
2977 for (oo = f->opt; oo; oo = oo->next)
2978 if (strcmp (oo->name, "skip") == 0)
2979 skip_p = 1;
2980 else if (strcmp (oo->name, "length") == 0
2981 && oo->kind == OPTION_STRING)
2982 fieldlength = oo->info.string;
2984 if (skip_p)
2985 continue;
2986 if (fieldlength)
2988 lengths_seen++;
2989 d->counter++;
2990 if (!union_p)
2992 if (!any_length_seen)
2994 oprintf (d->of, "%*s{\n", d->indent, "");
2995 d->indent += 2;
2997 any_length_seen = true;
2999 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
3000 d->indent, "", d->counter - 1);
3001 output_escaped_param (d, fieldlength, "length");
3002 oprintf (d->of, ");\n");
3006 endcounter = d->counter;
3008 for (f = t->u.s.fields; f; f = f->next)
3010 options_p oo;
3011 const char *dot = ".";
3012 const char *tagid = NULL;
3013 int skip_p = 0;
3014 int default_p = 0;
3015 int use_param_p = 0;
3016 const char *fieldlength = NULL;
3017 char *newval;
3019 d->reorder_fn = NULL;
3020 for (oo = f->opt; oo; oo = oo->next)
3021 if (strcmp (oo->name, "dot") == 0
3022 && oo->kind == OPTION_STRING)
3023 dot = oo->info.string;
3024 else if (strcmp (oo->name, "tag") == 0
3025 && oo->kind == OPTION_STRING)
3026 tagid = oo->info.string;
3027 else if (strcmp (oo->name, "skip") == 0)
3028 skip_p = 1;
3029 else if (strcmp (oo->name, "default") == 0)
3030 default_p = 1;
3031 else if (strcmp (oo->name, "reorder") == 0
3032 && oo->kind == OPTION_STRING)
3033 d->reorder_fn = oo->info.string;
3034 else if (strncmp (oo->name, "use_param", 9) == 0
3035 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
3036 use_param_p = 1;
3037 else if (strcmp (oo->name, "length") == 0
3038 && oo->kind == OPTION_STRING)
3039 fieldlength = oo->info.string;
3041 if (skip_p)
3042 continue;
3044 if (union_p && tagid)
3046 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
3047 d->indent += 2;
3049 else if (union_p && default_p)
3051 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3052 d->indent += 2;
3053 seen_default_p = 1;
3055 else if (!union_p && (default_p || tagid))
3056 error_at_line (d->line,
3057 "can't use `%s' outside a union on field `%s'",
3058 default_p ? "default" : "tag", f->name);
3059 else if (union_p && !(default_p || tagid)
3060 && f->type->kind == TYPE_SCALAR)
3062 fprintf (stderr,
3063 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3064 get_input_file_name (d->line->file), d->line->line,
3065 f->name);
3066 continue;
3068 else if (union_p && !(default_p || tagid))
3069 error_at_line (d->line,
3070 "field `%s' is missing `tag' or `default' option",
3071 f->name);
3073 if (fieldlength)
3075 d->loopcounter = endcounter - lengths_seen--;
3078 d->line = &f->line;
3079 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
3080 d->opt = f->opt;
3081 d->used_length = false;
3082 d->in_record_p = !union_p;
3084 if (union_p && use_param_p && d->param == NULL)
3085 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
3086 else
3087 walk_type (f->type, d);
3089 d->in_record_p = false;
3091 free (newval);
3093 if (union_p)
3095 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3096 d->indent -= 2;
3099 d->reorder_fn = NULL;
3101 d->val = oldval;
3102 d->prev_val[1] = oldprevval1;
3103 d->prev_val[2] = oldprevval2;
3105 if (union_p && !seen_default_p)
3107 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3108 oprintf (d->of, "%*s break;\n", d->indent, "");
3110 if (union_p)
3112 oprintf (d->of, "%*s}\n", d->indent, "");
3113 d->indent -= 2;
3115 if (any_length_seen)
3117 d->indent -= 2;
3118 oprintf (d->of, "%*s}\n", d->indent, "");
3121 break;
3123 case TYPE_LANG_STRUCT:
3125 type_p nt;
3126 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3127 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3128 break;
3129 if (nt == NULL)
3130 error_at_line (d->line, "structure `%s' differs between languages",
3131 t->u.s.tag);
3132 else
3133 walk_type (nt, d);
3135 break;
3137 case TYPE_PARAM_STRUCT:
3139 type_p *oldparam = d->param;
3141 d->param = t->u.param_struct.param;
3142 walk_type (t->u.param_struct.stru, d);
3143 d->param = oldparam;
3145 break;
3147 case TYPE_USER_STRUCT:
3148 d->process_field (t, d);
3149 break;
3151 case TYPE_NONE:
3152 case TYPE_UNDEFINED:
3153 gcc_unreachable ();
3157 /* process_field routine for marking routines. */
3159 static void
3160 write_types_process_field (type_p f, const struct walk_type_data *d)
3162 const struct write_types_data *wtd;
3163 const char *cast = d->needs_cast_p ? "(void *)" : "";
3164 wtd = (const struct write_types_data *) d->cookie;
3166 switch (f->kind)
3168 case TYPE_NONE:
3169 case TYPE_UNDEFINED:
3170 gcc_unreachable ();
3171 case TYPE_POINTER:
3172 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
3173 wtd->subfield_marker_routine, cast, d->val);
3174 if (wtd->param_prefix)
3176 if (f->u.p->kind == TYPE_SCALAR)
3177 /* The current type is a pointer to a scalar (so not
3178 considered like a pointer to instances of user defined
3179 types) and we are seeing it; it means we must be even
3180 more careful about the second argument of the
3181 SUBFIELD_MARKER_ROUTINE call. That argument must
3182 always be the instance of the type for which
3183 write_func_for_structure was called - this really is
3184 what the function SUBFIELD_MARKER_ROUTINE expects.
3185 That is, it must be an instance of the ORIG_S type
3186 parameter of write_func_for_structure. The convention
3187 is that that argument must be "x" in that case (as set
3188 by write_func_for_structure). The problem is, we can't
3189 count on d->prev_val[3] to be always set to "x" in that
3190 case. Sometimes walk_type can set it to something else
3191 (to e.g cooperate with write_array when called from
3192 write_roots). So let's set it to "x" here then. */
3193 oprintf (d->of, ", x");
3194 else
3195 oprintf (d->of, ", %s", d->prev_val[3]);
3196 if (d->orig_s)
3198 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3199 output_mangled_typename (d->of, d->orig_s);
3201 else
3202 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3204 oprintf (d->of, ");\n");
3205 if (d->reorder_fn && wtd->reorder_note_routine)
3206 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
3207 wtd->reorder_note_routine, cast, d->val,
3208 d->prev_val[3], d->reorder_fn);
3209 break;
3211 case TYPE_STRING:
3212 case TYPE_STRUCT:
3213 case TYPE_UNION:
3214 case TYPE_LANG_STRUCT:
3215 case TYPE_PARAM_STRUCT:
3216 case TYPE_USER_STRUCT:
3217 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3219 /* If F is a user-defined type and the field is not a
3220 pointer to the type, then we should not generate the
3221 standard pointer-marking code. All we need to do is call
3222 the user-provided marking function to process the fields
3223 of F. */
3224 oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3225 d->val);
3227 else
3229 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3230 output_mangled_typename (d->of, f);
3231 oprintf (d->of, " (%s%s);\n", cast, d->val);
3232 if (d->reorder_fn && wtd->reorder_note_routine)
3233 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
3234 wtd->reorder_note_routine, cast, d->val, cast, d->val,
3235 d->reorder_fn);
3237 break;
3239 case TYPE_SCALAR:
3240 break;
3242 case TYPE_ARRAY:
3243 gcc_unreachable ();
3247 /* Return an output file that is suitable for definitions which can
3248 reference struct S */
3250 static outf_p
3251 get_output_file_for_structure (const_type_p s, type_p *param)
3253 const input_file *fn;
3254 int i;
3256 gcc_assert (union_or_struct_p (s));
3257 fn = s->u.s.line.file;
3259 /* This is a hack, and not the good kind either. */
3260 for (i = NUM_PARAM - 1; i >= 0; i--)
3261 if (param && param[i] && param[i]->kind == TYPE_POINTER
3262 && union_or_struct_p (param[i]->u.p))
3263 fn = param[i]->u.p->u.s.line.file;
3265 /* The call to get_output_file_with_visibility may update fn by
3266 caching its result inside, so we need the CONST_CAST. */
3267 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3271 /* Returns the specifier keyword for a string or union type S, empty string
3272 otherwise. */
3274 static const char *
3275 get_type_specifier (const type_p s)
3277 if (s->kind == TYPE_STRUCT)
3278 return "struct ";
3279 else if (s->kind == TYPE_LANG_STRUCT)
3280 return get_type_specifier (s->u.s.lang_struct);
3281 else if (s->kind == TYPE_UNION)
3282 return "union ";
3283 return "";
3287 /* Emits a declaration for type TY (assumed to be a union or a
3288 structure) on stream OUT. */
3290 static void
3291 write_type_decl (outf_p out, type_p ty)
3293 if (union_or_struct_p (ty))
3294 oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3295 else if (ty->kind == TYPE_SCALAR)
3297 if (ty->u.scalar_is_char)
3298 oprintf (out, "const char");
3299 else
3300 oprintf (out, "void");
3302 else if (ty->kind == TYPE_POINTER)
3304 write_type_decl (out, ty->u.p);
3305 oprintf (out, " *");
3307 else if (ty->kind == TYPE_ARRAY)
3309 write_type_decl (out, ty->u.a.p);
3310 oprintf (out, " *");
3312 else if (ty->kind == TYPE_STRING)
3314 oprintf (out, "const char *");
3316 else
3317 gcc_unreachable ();
3321 /* Write on OF the name of the marker function for structure S. PREFIX
3322 is the prefix to use (to distinguish ggc from pch markers). */
3324 static void
3325 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3327 if (union_or_struct_p (s))
3329 const char *id_for_tag = filter_type_name (s->u.s.tag);
3330 oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3331 if (id_for_tag != s->u.s.tag)
3332 free (CONST_CAST (char *, id_for_tag));
3334 else if (s->kind == TYPE_PARAM_STRUCT)
3336 oprintf (of, "gt_%s_", prefix);
3337 output_mangled_typename (of, s);
3339 else
3340 gcc_unreachable ();
3343 /* Write on OF a user-callable routine to act as an entry point for
3344 the marking routine for S, generated by write_func_for_structure.
3345 PREFIX is the prefix to use to distinguish ggc and pch markers. */
3347 static void
3348 write_user_func_for_structure_ptr (outf_p of, type_p s, const char *prefix)
3350 /* Parameterized structures are not supported in user markers. There
3351 is no way for the marker function to know which specific type
3352 to use to generate the call to the void * entry point. For
3353 instance, a marker for struct htab may need to call different
3354 routines to mark the fields, depending on the paramN_is attributes.
3356 A user-defined marker that accepts 'struct htab' as its argument
3357 would not know which variant to call. Generating several entry
3358 points accepting 'struct htab' would cause multiply-defined
3359 errors during compilation. */
3360 gcc_assert (union_or_struct_p (s));
3362 type_p alias_of = NULL;
3363 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3364 if (strcmp (opt->name, "ptr_alias") == 0)
3366 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3367 we do not generate marking code for ORIG_S here. Instead, a
3368 forwarder #define in gtype-desc.h will cause every call to its
3369 marker to call the target of this alias.
3371 However, we still want to create a user entry code for the
3372 aliased type. So, if ALIAS_OF is set, we only generate the
3373 user-callable marker function. */
3374 alias_of = opt->info.type;
3375 break;
3378 oprintf (of, "\nvoid\n");
3379 oprintf (of, "gt_%sx (", prefix);
3380 write_type_decl (of, s);
3381 oprintf (of, " *& x)\n");
3382 oprintf (of, "{\n");
3383 oprintf (of, " if (x)\n ");
3384 write_marker_function_name (of, alias_of ? alias_of : s, prefix);
3385 oprintf (of, " ((void *) x);\n");
3386 oprintf (of, "}\n");
3390 /* Write a function to mark all the fields of type S on OF. PREFIX
3391 and D are as in write_user_marking_functions. */
3393 static void
3394 write_user_func_for_structure_body (type_p s, const char *prefix,
3395 struct walk_type_data *d)
3397 oprintf (d->of, "\nvoid\n");
3398 oprintf (d->of, "gt_%sx (", prefix);
3399 write_type_decl (d->of, s);
3400 oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3401 oprintf (d->of, "{\n");
3402 oprintf (d->of, " ");
3403 write_type_decl (d->of, s);
3404 oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3405 d->val = "(*x)";
3406 d->indent = 2;
3407 walk_type (s, d);
3408 oprintf (d->of, "}\n");
3412 /* Emit the user-callable functions needed to mark all the types used
3413 by the user structure S. PREFIX is the prefix to use to
3414 distinguish ggc and pch markers. D contains data needed to pass to
3415 walk_type when traversing the fields of a type.
3417 For every type T referenced by S, two routines are generated: one
3418 that takes 'T *', marks the pointer and calls the second routine,
3419 which just marks the fields of T. */
3421 static void
3422 write_user_marking_functions (type_p s, const char *prefix,
3423 struct walk_type_data *d)
3425 gcc_assert (s->kind == TYPE_USER_STRUCT);
3427 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3429 type_p fld_type = fld->type;
3430 if (fld_type->kind == TYPE_POINTER)
3432 type_p pointed_to_type = fld_type->u.p;
3433 if (union_or_struct_p (pointed_to_type))
3434 write_user_func_for_structure_ptr (d->of, pointed_to_type, prefix);
3436 else if (union_or_struct_p (fld_type))
3437 write_user_func_for_structure_body (fld_type, prefix, d);
3442 /* For S, a structure that's part of ORIG_S, and using parameters
3443 PARAM, write out a routine that:
3444 - Takes a parameter, a void * but actually of type *S
3445 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3446 field of S or its substructures and (in some cases) things
3447 that are pointed to by S. */
3449 static void
3450 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
3451 const struct write_types_data *wtd)
3453 const char *chain_next = NULL;
3454 const char *chain_prev = NULL;
3455 const char *chain_circular = NULL;
3456 const char *mark_hook_name = NULL;
3457 options_p opt;
3458 struct walk_type_data d;
3460 memset (&d, 0, sizeof (d));
3461 d.of = get_output_file_for_structure (s, param);
3462 for (opt = s->u.s.opt; opt; opt = opt->next)
3463 if (strcmp (opt->name, "chain_next") == 0
3464 && opt->kind == OPTION_STRING)
3465 chain_next = opt->info.string;
3466 else if (strcmp (opt->name, "chain_prev") == 0
3467 && opt->kind == OPTION_STRING)
3468 chain_prev = opt->info.string;
3469 else if (strcmp (opt->name, "chain_circular") == 0
3470 && opt->kind == OPTION_STRING)
3471 chain_circular = opt->info.string;
3472 else if (strcmp (opt->name, "mark_hook") == 0
3473 && opt->kind == OPTION_STRING)
3474 mark_hook_name = opt->info.string;
3475 if (chain_prev != NULL && chain_next == NULL)
3476 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3477 if (chain_circular != NULL && chain_next != NULL)
3478 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3479 if (chain_circular != NULL)
3480 chain_next = chain_circular;
3482 d.process_field = write_types_process_field;
3483 d.cookie = wtd;
3484 d.orig_s = orig_s;
3485 d.opt = s->u.s.opt;
3486 d.line = &s->u.s.line;
3487 d.bitmap = s->u.s.bitmap;
3488 d.param = param;
3489 d.prev_val[0] = "*x";
3490 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3491 d.prev_val[3] = "x";
3492 d.val = "(*x)";
3493 d.have_this_obj = false;
3495 oprintf (d.of, "\n");
3496 oprintf (d.of, "void\n");
3497 write_marker_function_name (d.of, orig_s, wtd->prefix);
3498 oprintf (d.of, " (void *x_p)\n");
3499 oprintf (d.of, "{\n ");
3500 write_type_decl (d.of, s);
3501 oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3502 write_type_decl (d.of, s);
3503 oprintf (d.of, " *)x_p;\n");
3504 if (chain_next != NULL)
3506 /* TYPE_USER_STRUCTs should not occur here. These structures
3507 are completely handled by user code. */
3508 gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
3510 oprintf (d.of, " ");
3511 write_type_decl (d.of, s);
3512 oprintf (d.of, " * xlimit = x;\n");
3514 if (chain_next == NULL)
3516 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3517 if (wtd->param_prefix)
3519 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3520 output_mangled_typename (d.of, orig_s);
3522 oprintf (d.of, "))\n");
3524 else
3526 if (chain_circular != NULL)
3527 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3528 else
3529 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3530 if (wtd->param_prefix)
3532 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3533 output_mangled_typename (d.of, orig_s);
3535 oprintf (d.of, "))\n");
3536 if (chain_circular != NULL)
3537 oprintf (d.of, " return;\n do\n");
3538 if (mark_hook_name && !wtd->skip_hooks)
3540 oprintf (d.of, " {\n");
3541 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
3543 oprintf (d.of, " xlimit = (");
3544 d.prev_val[2] = "*xlimit";
3545 output_escaped_param (&d, chain_next, "chain_next");
3546 oprintf (d.of, ");\n");
3547 if (mark_hook_name && !wtd->skip_hooks)
3548 oprintf (d.of, " }\n");
3549 if (chain_prev != NULL)
3551 oprintf (d.of, " if (x != xlimit)\n");
3552 oprintf (d.of, " for (;;)\n");
3553 oprintf (d.of, " {\n");
3554 oprintf (d.of, " %s %s * const xprev = (",
3555 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3557 d.prev_val[2] = "*x";
3558 output_escaped_param (&d, chain_prev, "chain_prev");
3559 oprintf (d.of, ");\n");
3560 oprintf (d.of, " if (xprev == NULL) break;\n");
3561 oprintf (d.of, " x = xprev;\n");
3562 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3563 if (wtd->param_prefix)
3565 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3566 output_mangled_typename (d.of, orig_s);
3568 oprintf (d.of, ");\n");
3569 oprintf (d.of, " }\n");
3571 if (chain_circular != NULL)
3573 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3574 if (wtd->param_prefix)
3576 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3577 output_mangled_typename (d.of, orig_s);
3579 oprintf (d.of, "));\n");
3580 if (mark_hook_name && !wtd->skip_hooks)
3581 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
3582 oprintf (d.of, " do\n");
3584 else
3585 oprintf (d.of, " while (x != xlimit)\n");
3587 oprintf (d.of, " {\n");
3588 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3590 oprintf (d.of, " %s (x);\n", mark_hook_name);
3593 d.prev_val[2] = "*x";
3594 d.indent = 6;
3595 if (orig_s->kind != TYPE_USER_STRUCT)
3596 walk_type (s, &d);
3597 else
3599 /* User structures have no fields to walk. Simply generate a call
3600 to the user-provided structure marker. */
3601 oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3604 if (chain_next != NULL)
3606 oprintf (d.of, " x = (");
3607 output_escaped_param (&d, chain_next, "chain_next");
3608 oprintf (d.of, ");\n");
3611 oprintf (d.of, " }\n");
3612 if (chain_circular != NULL)
3613 oprintf (d.of, " while (x != xlimit);\n");
3614 oprintf (d.of, "}\n");
3616 if (orig_s->kind == TYPE_USER_STRUCT)
3617 write_user_marking_functions (orig_s, wtd->prefix, &d);
3621 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3623 static void
3624 write_types (outf_p output_header, type_p structures, type_p param_structs,
3625 const struct write_types_data *wtd)
3627 int nbfun = 0; /* Count the emitted functions. */
3628 type_p s;
3630 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3632 /* We first emit the macros and the declarations. Functions' code is
3633 emitted afterwards. This is needed in plugin mode. */
3634 oprintf (output_header, "/* Macros and declarations. */\n");
3635 for (s = structures; s; s = s->next)
3636 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3638 options_p opt;
3640 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3641 continue;
3643 const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3645 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3646 output_mangled_typename (output_header, s);
3647 oprintf (output_header, "(X) do { \\\n");
3648 oprintf (output_header,
3649 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3650 s_id_for_tag);
3651 oprintf (output_header, " } while (0)\n");
3653 for (opt = s->u.s.opt; opt; opt = opt->next)
3654 if (strcmp (opt->name, "ptr_alias") == 0
3655 && opt->kind == OPTION_TYPE)
3657 const_type_p const t = (const_type_p) opt->info.type;
3658 if (t->kind == TYPE_STRUCT
3659 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3661 const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3662 oprintf (output_header,
3663 "#define gt_%sx_%s gt_%sx_%s\n",
3664 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3665 if (t_id_for_tag != t->u.s.tag)
3666 free (CONST_CAST (char *, t_id_for_tag));
3668 else
3669 error_at_line (&s->u.s.line,
3670 "structure alias is not a structure");
3671 break;
3673 if (opt)
3674 continue;
3676 /* Declare the marker procedure only once. */
3677 oprintf (output_header,
3678 "extern void gt_%sx_%s (void *);\n",
3679 wtd->prefix, s_id_for_tag);
3681 if (s_id_for_tag != s->u.s.tag)
3682 free (CONST_CAST (char *, s_id_for_tag));
3684 if (s->u.s.line.file == NULL)
3686 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3687 s->u.s.tag);
3688 continue;
3692 for (s = param_structs; s; s = s->next)
3693 if (s->gc_used == GC_POINTED_TO)
3695 type_p stru = s->u.param_struct.stru;
3697 /* Declare the marker procedure. */
3698 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
3699 output_mangled_typename (output_header, s);
3700 oprintf (output_header, " (void *);\n");
3702 if (stru->u.s.line.file == NULL)
3704 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3705 stru->u.s.tag);
3706 continue;
3710 /* At last we emit the functions code. */
3711 oprintf (output_header, "\n/* functions code */\n");
3712 for (s = structures; s; s = s->next)
3713 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3715 options_p opt;
3717 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3718 continue;
3719 for (opt = s->u.s.opt; opt; opt = opt->next)
3720 if (strcmp (opt->name, "ptr_alias") == 0)
3721 break;
3722 if (opt)
3723 continue;
3725 if (s->kind == TYPE_LANG_STRUCT)
3727 type_p ss;
3728 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3730 nbfun++;
3731 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3732 nbfun, (void*) ss, ss->u.s.tag);
3733 write_func_for_structure (s, ss, NULL, wtd);
3736 else
3738 nbfun++;
3739 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3740 nbfun, (void*) s, s->u.s.tag);
3741 write_func_for_structure (s, s, NULL, wtd);
3744 else
3746 /* Structure s is not possibly pointed to, so can be ignored. */
3747 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3748 (void*)s, s->u.s.tag,
3749 (int) s->gc_used);
3752 for (s = param_structs; s; s = s->next)
3753 if (s->gc_used == GC_POINTED_TO)
3755 type_p *param = s->u.param_struct.param;
3756 type_p stru = s->u.param_struct.stru;
3757 if (stru->u.s.line.file == NULL)
3758 continue;
3759 if (stru->kind == TYPE_LANG_STRUCT)
3761 type_p ss;
3762 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3764 nbfun++;
3765 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
3766 nbfun, (void*) ss, ss->u.s.tag);
3767 write_func_for_structure (s, ss, param, wtd);
3770 else
3772 nbfun++;
3773 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
3774 nbfun, (void*) s,
3775 (void*) stru, stru->u.s.tag);
3776 write_func_for_structure (s, stru, param, wtd);
3779 else
3781 /* Param structure s is not pointed to, so should be ignored. */
3782 DBGPRINTF ("ignored s @ %p", (void*)s);
3784 if (verbosity_level >= 2)
3785 printf ("%s emitted %d routines for %s\n",
3786 progname, nbfun, wtd->comment);
3789 static const struct write_types_data ggc_wtd = {
3790 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
3791 "GC marker procedures. ",
3792 FALSE
3795 static const struct write_types_data pch_wtd = {
3796 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3797 "gt_pch_note_reorder",
3798 "PCH type-walking procedures. ",
3799 TRUE
3802 /* Write out the local pointer-walking routines. */
3804 /* process_field routine for local pointer-walking for user-callable
3805 routines. The difference between this and
3806 write_types_local_process_field is that, in this case, we do not
3807 need to check whether the given pointer matches the address of the
3808 parent structure. This check was already generated by the call
3809 to gt_pch_nx in the main gt_pch_p_*() function that is calling
3810 this code. */
3812 static void
3813 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
3815 switch (f->kind)
3817 case TYPE_POINTER:
3818 case TYPE_STRUCT:
3819 case TYPE_UNION:
3820 case TYPE_LANG_STRUCT:
3821 case TYPE_PARAM_STRUCT:
3822 case TYPE_STRING:
3823 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3824 break;
3826 case TYPE_USER_STRUCT:
3827 if (d->in_ptr_field)
3828 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3829 else
3830 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3831 d->indent, "", d->val);
3832 break;
3834 case TYPE_SCALAR:
3835 break;
3837 case TYPE_ARRAY:
3838 case TYPE_NONE:
3839 case TYPE_UNDEFINED:
3840 gcc_unreachable ();
3845 /* Write a function to PCH walk all the fields of type S on OF.
3846 D contains data needed by walk_type to recurse into the fields of S. */
3848 static void
3849 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
3851 oprintf (d->of, "\nvoid\n");
3852 oprintf (d->of, "gt_pch_nx (");
3853 write_type_decl (d->of, s);
3854 oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
3855 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3856 "\tATTRIBUTE_UNUSED void *cookie)\n");
3857 oprintf (d->of, "{\n");
3858 d->val = "(*x)";
3859 d->indent = 2;
3860 d->process_field = write_types_local_user_process_field;
3861 walk_type (s, d);
3862 oprintf (d->of, "}\n");
3866 /* Emit the user-callable functions needed to mark all the types used
3867 by the user structure S. PREFIX is the prefix to use to
3868 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
3869 chain_next option defined. D contains data needed to pass to
3870 walk_type when traversing the fields of a type.
3872 For every type T referenced by S, two routines are generated: one
3873 that takes 'T *', marks the pointer and calls the second routine,
3874 which just marks the fields of T. */
3876 static void
3877 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
3879 gcc_assert (s->kind == TYPE_USER_STRUCT);
3881 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3883 type_p fld_type = fld->type;
3884 if (union_or_struct_p (fld_type))
3885 write_pch_user_walking_for_structure_body (fld_type, d);
3890 /* process_field routine for local pointer-walking. */
3892 static void
3893 write_types_local_process_field (type_p f, const struct walk_type_data *d)
3895 gcc_assert (d->have_this_obj);
3896 switch (f->kind)
3898 case TYPE_POINTER:
3899 case TYPE_STRUCT:
3900 case TYPE_UNION:
3901 case TYPE_LANG_STRUCT:
3902 case TYPE_PARAM_STRUCT:
3903 case TYPE_STRING:
3904 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3905 d->prev_val[3]);
3906 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3907 break;
3909 case TYPE_USER_STRUCT:
3910 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3911 d->prev_val[3]);
3912 if (d->in_ptr_field)
3913 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3914 else
3915 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3916 d->indent, "", d->val);
3917 break;
3919 case TYPE_SCALAR:
3920 break;
3922 case TYPE_ARRAY:
3923 case TYPE_NONE:
3924 case TYPE_UNDEFINED:
3925 gcc_unreachable ();
3930 /* For S, a structure that's part of ORIG_S, and using parameters
3931 PARAM, write out a routine that:
3932 - Is of type gt_note_pointers
3933 - Calls PROCESS_FIELD on each field of S or its substructures.
3936 static void
3937 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
3939 struct walk_type_data d;
3941 memset (&d, 0, sizeof (d));
3942 d.of = get_output_file_for_structure (s, param);
3943 d.process_field = write_types_local_process_field;
3944 d.opt = s->u.s.opt;
3945 d.line = &s->u.s.line;
3946 d.bitmap = s->u.s.bitmap;
3947 d.param = param;
3948 d.prev_val[0] = d.prev_val[2] = "*x";
3949 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3950 d.prev_val[3] = "x";
3951 d.val = "(*x)";
3952 d.fn_wants_lvalue = true;
3954 oprintf (d.of, "\n");
3955 oprintf (d.of, "void\n");
3956 oprintf (d.of, "gt_pch_p_");
3957 output_mangled_typename (d.of, orig_s);
3958 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3959 "\tvoid *x_p,\n"
3960 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3961 "\tATTRIBUTE_UNUSED void *cookie)\n");
3962 oprintf (d.of, "{\n");
3963 oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3964 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3965 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3966 d.indent = 2;
3967 d.have_this_obj = true;
3969 if (s->kind != TYPE_USER_STRUCT)
3970 walk_type (s, &d);
3971 else
3973 /* User structures have no fields to walk. Simply generate a
3974 call to the user-provided PCH walker. */
3975 oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
3976 d.prev_val[3]);
3977 oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3978 d.indent, "", d.val);
3981 oprintf (d.of, "}\n");
3983 /* Write user-callable entry points for the PCH walking routines. */
3984 if (orig_s->kind == TYPE_USER_STRUCT)
3985 write_pch_user_walking_functions (s, &d);
3988 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
3990 static void
3991 write_local (outf_p output_header, type_p structures, type_p param_structs)
3993 type_p s;
3995 if (!output_header)
3996 return;
3998 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
3999 for (s = structures; s; s = s->next)
4000 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
4002 options_p opt;
4004 if (s->u.s.line.file == NULL)
4005 continue;
4006 for (opt = s->u.s.opt; opt; opt = opt->next)
4007 if (strcmp (opt->name, "ptr_alias") == 0
4008 && opt->kind == OPTION_TYPE)
4010 const_type_p const t = (const_type_p) opt->info.type;
4011 if (t->kind == TYPE_STRUCT
4012 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
4014 oprintf (output_header, "#define gt_pch_p_");
4015 output_mangled_typename (output_header, s);
4016 oprintf (output_header, " gt_pch_p_");
4017 output_mangled_typename (output_header, t);
4018 oprintf (output_header, "\n");
4020 else
4021 error_at_line (&s->u.s.line,
4022 "structure alias is not a structure");
4023 break;
4025 if (opt)
4026 continue;
4028 /* Declare the marker procedure only once. */
4029 oprintf (output_header, "extern void gt_pch_p_");
4030 output_mangled_typename (output_header, s);
4031 oprintf (output_header,
4032 "\n (void *, void *, gt_pointer_operator, void *);\n");
4034 if (s->kind == TYPE_LANG_STRUCT)
4036 type_p ss;
4037 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4038 write_local_func_for_structure (s, ss, NULL);
4040 else
4041 write_local_func_for_structure (s, s, NULL);
4044 for (s = param_structs; s; s = s->next)
4045 if (s->gc_used == GC_POINTED_TO)
4047 type_p *param = s->u.param_struct.param;
4048 type_p stru = s->u.param_struct.stru;
4050 /* Declare the marker procedure. */
4051 oprintf (output_header, "extern void gt_pch_p_");
4052 output_mangled_typename (output_header, s);
4053 oprintf (output_header,
4054 "\n (void *, void *, gt_pointer_operator, void *);\n");
4056 if (stru->u.s.line.file == NULL)
4058 fprintf (stderr, "warning: structure `%s' used but not defined\n",
4059 stru->u.s.tag);
4060 continue;
4063 if (stru->kind == TYPE_LANG_STRUCT)
4065 type_p ss;
4066 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
4067 write_local_func_for_structure (s, ss, param);
4069 else
4070 write_local_func_for_structure (s, stru, param);
4074 /* Nonzero if S is a type for which typed GC allocators should be output. */
4076 #define USED_BY_TYPED_GC_P(s) \
4077 ((s->kind == TYPE_POINTER \
4078 && (s->u.p->gc_used == GC_POINTED_TO \
4079 || s->u.p->gc_used == GC_USED)) \
4080 || (union_or_struct_p (s) \
4081 && ((s)->gc_used == GC_POINTED_TO \
4082 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4083 && s->u.s.line.file != NULL) \
4084 || ((s)->gc_used == GC_USED \
4085 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))))))
4088 /* Might T contain any non-pointer elements? */
4090 static int
4091 contains_scalar_p (type_p t)
4093 switch (t->kind)
4095 case TYPE_STRING:
4096 case TYPE_POINTER:
4097 return 0;
4098 case TYPE_ARRAY:
4099 return contains_scalar_p (t->u.a.p);
4100 case TYPE_USER_STRUCT:
4101 /* User-marked structures will typically contain pointers. */
4102 return 0;
4103 default:
4104 /* Could also check for structures that have no non-pointer
4105 fields, but there aren't enough of those to worry about. */
4106 return 1;
4110 /* Mangle INPF and print it to F. */
4112 static void
4113 put_mangled_filename (outf_p f, const input_file *inpf)
4115 /* The call to get_output_file_name may indirectly update fn since
4116 get_output_file_with_visibility caches its result inside, so we
4117 need the CONST_CAST. */
4118 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4119 if (!f || !name)
4120 return;
4121 for (; *name != 0; name++)
4122 if (ISALNUM (*name))
4123 oprintf (f, "%c", *name);
4124 else
4125 oprintf (f, "%c", '_');
4128 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4129 LASTNAME, and NAME are all strings to insert in various places in
4130 the resulting code. */
4132 static void
4133 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4134 const char *tname, const char *name)
4136 struct flist *fli2;
4138 for (fli2 = flp; fli2; fli2 = fli2->next)
4139 if (fli2->started_p)
4141 oprintf (fli2->f, " %s\n", lastname);
4142 oprintf (fli2->f, "};\n\n");
4145 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4146 if (fli2->started_p)
4148 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4149 int fnum;
4151 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4152 if (bitmap & 1)
4154 oprintf (base_files[fnum],
4155 "extern const struct %s gt_%s_", tname, pfx);
4156 put_mangled_filename (base_files[fnum], fli2->file);
4157 oprintf (base_files[fnum], "[];\n");
4162 size_t fnum;
4163 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4164 oprintf (base_files[fnum],
4165 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4169 for (fli2 = flp; fli2; fli2 = fli2->next)
4170 if (fli2->started_p)
4172 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4173 int fnum;
4175 fli2->started_p = 0;
4177 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4178 if (bitmap & 1)
4180 oprintf (base_files[fnum], " gt_%s_", pfx);
4181 put_mangled_filename (base_files[fnum], fli2->file);
4182 oprintf (base_files[fnum], ",\n");
4187 size_t fnum;
4188 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4190 oprintf (base_files[fnum], " NULL\n");
4191 oprintf (base_files[fnum], "};\n");
4196 /* Write the first three fields (pointer, count and stride) for
4197 root NAME to F. V and LINE are as for write_root.
4199 Return true if the entry could be written; return false on error. */
4201 static bool
4202 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4204 type_p ap;
4206 if (!v)
4208 error_at_line (line, "`%s' is too complex to be a root", name);
4209 return false;
4212 oprintf (f, " {\n");
4213 oprintf (f, " &%s,\n", name);
4214 oprintf (f, " 1");
4216 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4217 if (ap->u.a.len[0])
4218 oprintf (f, " * (%s)", ap->u.a.len);
4219 else if (ap == v->type)
4220 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4221 oprintf (f, ",\n");
4222 oprintf (f, " sizeof (%s", v->name);
4223 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4224 oprintf (f, "[0]");
4225 oprintf (f, "),\n");
4226 return true;
4229 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4230 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4231 of the caller. */
4233 static void
4234 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4235 int has_length, struct fileloc *line, const char *if_marked,
4236 bool emit_pch, type_p field_type, const char *field_name)
4238 struct pair newv;
4239 /* If the field reference is relative to V, rather than to some
4240 subcomponent of V, we can mark any subarrays with a single stride.
4241 We're effectively treating the field as a global variable in its
4242 own right. */
4243 if (v && type == v->type)
4245 newv = *v;
4246 newv.type = field_type;
4247 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4248 v = &newv;
4250 /* Otherwise, any arrays nested in the structure are too complex to
4251 handle. */
4252 else if (field_type->kind == TYPE_ARRAY)
4253 v = NULL;
4254 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4255 has_length, line, if_marked, emit_pch);
4258 /* Write out to F the table entry and any marker routines needed to
4259 mark NAME as TYPE. V can be one of three values:
4261 - null, if NAME is too complex to represent using a single
4262 count and stride. In this case, it is an error for NAME to
4263 contain any gc-ed data.
4265 - the outermost array that contains NAME, if NAME is part of an array.
4267 - the C variable that contains NAME, if NAME is not part of an array.
4269 LINE is the line of the C source that declares the root variable.
4270 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
4271 is nonzero iff we are building the root table for hash table caches. */
4273 static void
4274 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4275 struct fileloc *line, const char *if_marked, bool emit_pch)
4277 switch (type->kind)
4279 case TYPE_STRUCT:
4281 pair_p fld;
4282 for (fld = type->u.s.fields; fld; fld = fld->next)
4284 int skip_p = 0;
4285 const char *desc = NULL;
4286 options_p o;
4288 for (o = fld->opt; o; o = o->next)
4289 if (strcmp (o->name, "skip") == 0)
4290 skip_p = 1;
4291 else if (strcmp (o->name, "desc") == 0
4292 && o->kind == OPTION_STRING)
4293 desc = o->info.string;
4294 else if (strcmp (o->name, "param_is") == 0)
4296 else
4297 error_at_line (line,
4298 "field `%s' of global `%s' has unknown option `%s'",
4299 fld->name, name, o->name);
4301 if (skip_p)
4302 continue;
4303 else if (desc && fld->type->kind == TYPE_UNION)
4305 pair_p validf = NULL;
4306 pair_p ufld;
4308 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4310 const char *tag = NULL;
4311 options_p oo;
4312 for (oo = ufld->opt; oo; oo = oo->next)
4313 if (strcmp (oo->name, "tag") == 0
4314 && oo->kind == OPTION_STRING)
4315 tag = oo->info.string;
4316 if (tag == NULL || strcmp (tag, desc) != 0)
4317 continue;
4318 if (validf != NULL)
4319 error_at_line (line,
4320 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4321 name, fld->name, validf->name,
4322 name, fld->name, ufld->name, tag);
4323 validf = ufld;
4325 if (validf != NULL)
4326 write_field_root (f, v, type, name, 0, line, if_marked,
4327 emit_pch, validf->type,
4328 ACONCAT ((fld->name, ".",
4329 validf->name, NULL)));
4331 else if (desc)
4332 error_at_line (line,
4333 "global `%s.%s' has `desc' option but is not union",
4334 name, fld->name);
4335 else
4336 write_field_root (f, v, type, name, 0, line, if_marked,
4337 emit_pch, fld->type, fld->name);
4340 break;
4342 case TYPE_ARRAY:
4344 char *newname;
4345 newname = xasprintf ("%s[0]", name);
4346 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
4347 emit_pch);
4348 free (newname);
4350 break;
4352 case TYPE_USER_STRUCT:
4353 error_at_line (line, "`%s' must be a pointer type, because it is "
4354 "a GC root and its type is marked with GTY((user))",
4355 v->name);
4356 break;
4358 case TYPE_POINTER:
4360 type_p tp;
4362 if (!start_root_entry (f, v, name, line))
4363 return;
4365 tp = type->u.p;
4367 if (!has_length && union_or_struct_p (tp))
4369 const char *id_for_tag = filter_type_name (tp->u.s.tag);
4370 oprintf (f, " &gt_ggc_mx_%s,\n", id_for_tag);
4371 if (emit_pch)
4372 oprintf (f, " &gt_pch_nx_%s", id_for_tag);
4373 else
4374 oprintf (f, " NULL");
4375 if (id_for_tag != tp->u.s.tag)
4376 free (CONST_CAST (char *, id_for_tag));
4378 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
4380 oprintf (f, " &gt_ggc_m_");
4381 output_mangled_typename (f, tp);
4382 if (emit_pch)
4384 oprintf (f, ",\n &gt_pch_n_");
4385 output_mangled_typename (f, tp);
4387 else
4388 oprintf (f, ",\n NULL");
4390 else if (has_length
4391 && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
4393 oprintf (f, " &gt_ggc_ma_%s,\n", name);
4394 if (emit_pch)
4395 oprintf (f, " &gt_pch_na_%s", name);
4396 else
4397 oprintf (f, " NULL");
4399 else
4401 error_at_line (line,
4402 "global `%s' is pointer to unimplemented type",
4403 name);
4405 if (if_marked)
4406 oprintf (f, ",\n &%s", if_marked);
4407 oprintf (f, "\n },\n");
4409 break;
4411 case TYPE_STRING:
4413 if (!start_root_entry (f, v, name, line))
4414 return;
4416 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
4417 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
4418 oprintf (f, " },\n");
4420 break;
4422 case TYPE_SCALAR:
4423 break;
4425 case TYPE_NONE:
4426 case TYPE_UNDEFINED:
4427 case TYPE_UNION:
4428 case TYPE_LANG_STRUCT:
4429 case TYPE_PARAM_STRUCT:
4430 error_at_line (line, "global `%s' is unimplemented type", name);
4434 /* This generates a routine to walk an array. */
4436 static void
4437 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4439 struct walk_type_data d;
4440 char *prevval3;
4442 memset (&d, 0, sizeof (d));
4443 d.of = f;
4444 d.cookie = wtd;
4445 d.indent = 2;
4446 d.line = &v->line;
4447 d.opt = v->opt;
4448 d.bitmap = get_lang_bitmap (v->line.file);
4449 d.param = NULL;
4451 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4453 if (wtd->param_prefix)
4455 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4456 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
4457 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4458 wtd->param_prefix, v->name);
4459 oprintf (d.of,
4460 " ATTRIBUTE_UNUSED void *x_p,\n"
4461 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4462 " ATTRIBUTE_UNUSED void * cookie)\n");
4463 oprintf (d.of, "{\n");
4464 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4465 d.process_field = write_types_local_process_field;
4466 d.have_this_obj = true;
4467 walk_type (v->type, &d);
4468 oprintf (f, "}\n\n");
4471 d.opt = v->opt;
4472 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4473 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4474 wtd->prefix, v->name);
4475 oprintf (f, "{\n");
4476 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4477 d.process_field = write_types_process_field;
4478 d.have_this_obj = false;
4479 walk_type (v->type, &d);
4480 free (prevval3);
4481 oprintf (f, "}\n\n");
4484 /* Output a table describing the locations and types of VARIABLES. */
4486 static void
4487 write_roots (pair_p variables, bool emit_pch)
4489 pair_p v;
4490 struct flist *flp = NULL;
4492 for (v = variables; v; v = v->next)
4494 outf_p f =
4495 get_output_file_with_visibility (CONST_CAST (input_file*,
4496 v->line.file));
4497 struct flist *fli;
4498 const char *length = NULL;
4499 int deletable_p = 0;
4500 options_p o;
4501 for (o = v->opt; o; o = o->next)
4502 if (strcmp (o->name, "length") == 0
4503 && o->kind == OPTION_STRING)
4504 length = o->info.string;
4505 else if (strcmp (o->name, "deletable") == 0)
4506 deletable_p = 1;
4507 else if (strcmp (o->name, "param_is") == 0)
4509 else if (strncmp (o->name, "param", 5) == 0
4510 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
4512 else if (strcmp (o->name, "if_marked") == 0)
4514 else
4515 error_at_line (&v->line,
4516 "global `%s' has unknown option `%s'",
4517 v->name, o->name);
4519 for (fli = flp; fli; fli = fli->next)
4520 if (fli->f == f && f)
4521 break;
4522 if (fli == NULL)
4524 fli = XNEW (struct flist);
4525 fli->f = f;
4526 fli->next = flp;
4527 fli->started_p = 0;
4528 fli->file = v->line.file;
4529 gcc_assert (fli->file);
4530 flp = fli;
4532 oprintf (f, "\n/* GC roots. */\n\n");
4535 if (!deletable_p
4536 && length
4537 && v->type->kind == TYPE_POINTER
4538 && (v->type->u.p->kind == TYPE_POINTER
4539 || v->type->u.p->kind == TYPE_STRUCT))
4541 write_array (f, v, &ggc_wtd);
4542 write_array (f, v, &pch_wtd);
4546 for (v = variables; v; v = v->next)
4548 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4549 v->line.file));
4550 struct flist *fli;
4551 int skip_p = 0;
4552 int length_p = 0;
4553 options_p o;
4555 for (o = v->opt; o; o = o->next)
4556 if (strcmp (o->name, "length") == 0)
4557 length_p = 1;
4558 else if (strcmp (o->name, "deletable") == 0
4559 || strcmp (o->name, "if_marked") == 0)
4560 skip_p = 1;
4562 if (skip_p)
4563 continue;
4565 for (fli = flp; fli; fli = fli->next)
4566 if (fli->f == f)
4567 break;
4568 if (!fli->started_p)
4570 fli->started_p = 1;
4572 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4573 put_mangled_filename (f, v->line.file);
4574 oprintf (f, "[] = {\n");
4577 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4580 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4581 "gt_ggc_rtab");
4583 for (v = variables; v; v = v->next)
4585 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4586 v->line.file));
4587 struct flist *fli;
4588 int skip_p = 1;
4589 options_p o;
4591 for (o = v->opt; o; o = o->next)
4592 if (strcmp (o->name, "deletable") == 0)
4593 skip_p = 0;
4594 else if (strcmp (o->name, "if_marked") == 0)
4595 skip_p = 1;
4597 if (skip_p)
4598 continue;
4600 for (fli = flp; fli; fli = fli->next)
4601 if (fli->f == f)
4602 break;
4603 if (!fli->started_p)
4605 fli->started_p = 1;
4607 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4608 put_mangled_filename (f, v->line.file);
4609 oprintf (f, "[] = {\n");
4612 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4613 v->name, v->name);
4616 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4617 "gt_ggc_deletable_rtab");
4619 for (v = variables; v; v = v->next)
4621 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4622 v->line.file));
4623 struct flist *fli;
4624 const char *if_marked = NULL;
4625 int length_p = 0;
4626 options_p o;
4628 for (o = v->opt; o; o = o->next)
4629 if (strcmp (o->name, "length") == 0)
4630 length_p = 1;
4631 else if (strcmp (o->name, "if_marked") == 0
4632 && o->kind == OPTION_STRING)
4633 if_marked = o->info.string;
4634 if (if_marked == NULL)
4635 continue;
4636 if (v->type->kind != TYPE_POINTER
4637 || v->type->u.p->kind != TYPE_PARAM_STRUCT
4638 || v->type->u.p->u.param_struct.stru != find_structure ("htab",
4639 TYPE_STRUCT))
4641 error_at_line (&v->line,
4642 "if_marked option used but not hash table");
4643 continue;
4646 for (fli = flp; fli; fli = fli->next)
4647 if (fli->f == f)
4648 break;
4649 if (!fli->started_p)
4651 fli->started_p = 1;
4653 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4654 put_mangled_filename (f, v->line.file);
4655 oprintf (f, "[] = {\n");
4658 write_root (f, v, v->type->u.p->u.param_struct.param[0],
4659 v->name, length_p, &v->line, if_marked, emit_pch);
4662 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4663 "gt_ggc_cache_rtab");
4665 if (!emit_pch)
4666 return;
4668 for (v = variables; v; v = v->next)
4670 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4671 v->line.file));
4672 struct flist *fli;
4673 int length_p = 0;
4674 int if_marked_p = 0;
4675 options_p o;
4677 for (o = v->opt; o; o = o->next)
4678 if (strcmp (o->name, "length") == 0)
4679 length_p = 1;
4680 else if (strcmp (o->name, "if_marked") == 0)
4681 if_marked_p = 1;
4683 if (!if_marked_p)
4684 continue;
4686 for (fli = flp; fli; fli = fli->next)
4687 if (fli->f == f)
4688 break;
4689 if (!fli->started_p)
4691 fli->started_p = 1;
4693 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4694 put_mangled_filename (f, v->line.file);
4695 oprintf (f, "[] = {\n");
4698 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4701 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4702 "gt_pch_cache_rtab");
4704 for (v = variables; v; v = v->next)
4706 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4707 v->line.file));
4708 struct flist *fli;
4709 int skip_p = 0;
4710 options_p o;
4712 for (o = v->opt; o; o = o->next)
4713 if (strcmp (o->name, "deletable") == 0
4714 || strcmp (o->name, "if_marked") == 0)
4716 skip_p = 1;
4717 break;
4720 if (skip_p)
4721 continue;
4723 if (!contains_scalar_p (v->type))
4724 continue;
4726 for (fli = flp; fli; fli = fli->next)
4727 if (fli->f == f)
4728 break;
4729 if (!fli->started_p)
4731 fli->started_p = 1;
4733 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4734 put_mangled_filename (f, v->line.file);
4735 oprintf (f, "[] = {\n");
4738 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4739 v->name, v->name);
4742 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4743 "gt_pch_scalar_rtab");
4746 /* TRUE if type S has the GTY variable_size annotation. */
4748 static bool
4749 variable_size_p (const type_p s)
4751 options_p o;
4752 for (o = s->u.s.opt; o; o = o->next)
4753 if (strcmp (o->name, "variable_size") == 0)
4754 return true;
4755 return false;
4758 enum alloc_quantity
4759 { single, vector };
4761 /* Writes one typed allocator definition into output F for type
4762 identifier TYPE_NAME with optional type specifier TYPE_SPECIFIER.
4763 The allocator name will contain ALLOCATOR_TYPE. If VARIABLE_SIZE
4764 is true, the allocator will have an extra parameter specifying
4765 number of bytes to allocate. If QUANTITY is set to VECTOR, a
4766 vector allocator will be output. */
4768 static void
4769 write_typed_alloc_def (outf_p f,
4770 bool variable_size, const char *type_specifier,
4771 const char *type_name, const char *allocator_type,
4772 enum alloc_quantity quantity)
4774 bool two_args = variable_size && (quantity == vector);
4775 gcc_assert (f != NULL);
4776 const char *type_name_as_id = filter_type_name (type_name);
4777 oprintf (f, "#define ggc_alloc_%s%s", allocator_type, type_name_as_id);
4778 oprintf (f, "(%s%s%s) ",
4779 (variable_size ? "SIZE" : ""),
4780 (two_args ? ", " : ""),
4781 (quantity == vector) ? "n" : "");
4782 oprintf (f, "((%s%s *)", type_specifier, type_name);
4783 oprintf (f, "(ggc_internal_%salloc_stat (", allocator_type);
4784 if (variable_size)
4785 oprintf (f, "SIZE");
4786 else
4787 oprintf (f, "sizeof (%s%s)", type_specifier, type_name);
4788 if (quantity == vector)
4789 oprintf (f, ", n");
4790 oprintf (f, " MEM_STAT_INFO)))\n");
4791 if (type_name_as_id != type_name)
4792 free (CONST_CAST (char *, type_name_as_id));
4795 /* Writes a typed allocator definition into output F for a struct or
4796 union S, with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4798 static void
4799 write_typed_struct_alloc_def (outf_p f,
4800 const type_p s, const char *allocator_type,
4801 enum alloc_quantity quantity)
4803 gcc_assert (union_or_struct_p (s));
4804 write_typed_alloc_def (f, variable_size_p (s), get_type_specifier (s),
4805 s->u.s.tag, allocator_type, quantity);
4808 /* Writes a typed allocator definition into output F for a typedef P,
4809 with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4811 static void
4812 write_typed_typedef_alloc_def (outf_p f,
4813 const pair_p p, const char *allocator_type,
4814 enum alloc_quantity quantity)
4816 write_typed_alloc_def (f, variable_size_p (p->type), "", p->name,
4817 allocator_type, quantity);
4820 /* Writes typed allocator definitions into output F for the types in
4821 STRUCTURES and TYPEDEFS that are used by GC. */
4823 static void
4824 write_typed_alloc_defns (outf_p f,
4825 const type_p structures, const pair_p typedefs)
4827 type_p s;
4828 pair_p p;
4830 gcc_assert (f != NULL);
4831 oprintf (f,
4832 "\n/* Allocators for known structs and unions. */\n\n");
4833 for (s = structures; s; s = s->next)
4835 if (!USED_BY_TYPED_GC_P (s))
4836 continue;
4837 gcc_assert (union_or_struct_p (s));
4838 /* In plugin mode onput output ggc_alloc macro definitions
4839 relevant to plugin input files. */
4840 if (nb_plugin_files > 0
4841 && ((s->u.s.line.file == NULL) || !s->u.s.line.file->inpisplugin))
4842 continue;
4843 write_typed_struct_alloc_def (f, s, "", single);
4844 write_typed_struct_alloc_def (f, s, "cleared_", single);
4845 write_typed_struct_alloc_def (f, s, "vec_", vector);
4846 write_typed_struct_alloc_def (f, s, "cleared_vec_", vector);
4849 oprintf (f, "\n/* Allocators for known typedefs. */\n");
4850 for (p = typedefs; p; p = p->next)
4852 s = p->type;
4853 if (!USED_BY_TYPED_GC_P (s) || (strcmp (p->name, s->u.s.tag) == 0))
4854 continue;
4855 /* In plugin mode onput output ggc_alloc macro definitions
4856 relevant to plugin input files. */
4857 if (nb_plugin_files > 0)
4859 struct fileloc* filoc = type_fileloc (s);
4860 if (!filoc || !filoc->file->inpisplugin)
4861 continue;
4863 write_typed_typedef_alloc_def (f, p, "", single);
4864 write_typed_typedef_alloc_def (f, p, "cleared_", single);
4865 write_typed_typedef_alloc_def (f, p, "vec_", vector);
4866 write_typed_typedef_alloc_def (f, p, "cleared_vec_", vector);
4870 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4871 guaranteee for somewhat increased readability. If name conflicts do happen,
4872 this funcion will have to be adjusted to be more like
4873 output_mangled_typename. */
4875 static void
4876 output_typename (outf_p of, const_type_p t)
4878 switch (t->kind)
4880 case TYPE_STRING:
4881 oprintf (of, "str");
4882 break;
4883 case TYPE_SCALAR:
4884 oprintf (of, "scalar");
4885 break;
4886 case TYPE_POINTER:
4887 output_typename (of, t->u.p);
4888 break;
4889 case TYPE_STRUCT:
4890 case TYPE_USER_STRUCT:
4891 case TYPE_UNION:
4892 case TYPE_LANG_STRUCT:
4893 oprintf (of, "%s", t->u.s.tag);
4894 break;
4895 case TYPE_PARAM_STRUCT:
4897 int i;
4898 for (i = 0; i < NUM_PARAM; i++)
4899 if (t->u.param_struct.param[i] != NULL)
4901 output_typename (of, t->u.param_struct.param[i]);
4902 oprintf (of, "_");
4904 output_typename (of, t->u.param_struct.stru);
4905 break;
4907 case TYPE_NONE:
4908 case TYPE_UNDEFINED:
4909 case TYPE_ARRAY:
4910 gcc_unreachable ();
4914 /* Writes a typed GC allocator for type S that is suitable as a callback for
4915 the splay tree implementation in libiberty. */
4917 static void
4918 write_splay_tree_allocator_def (const_type_p s)
4920 outf_p of = get_output_file_with_visibility (NULL);
4921 oprintf (of, "void * ggc_alloc_splay_tree_");
4922 output_typename (of, s);
4923 oprintf (of, " (int sz, void * nl)\n");
4924 oprintf (of, "{\n");
4925 oprintf (of, " return ggc_splay_alloc (sz, nl);\n");
4926 oprintf (of, "}\n\n");
4929 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
4930 for the splay tree implementation in libiberty. */
4932 static void
4933 write_splay_tree_allocators (const_type_p param_structs)
4935 const_type_p s;
4937 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
4938 for (s = param_structs; s; s = s->next)
4939 if (s->gc_used == GC_POINTED_TO)
4941 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
4942 output_typename (header_file, s);
4943 oprintf (header_file, " (int, void *);\n");
4944 write_splay_tree_allocator_def (s);
4948 #define INDENT 2
4950 /* Dumps the value of typekind KIND. */
4952 static void
4953 dump_typekind (int indent, enum typekind kind)
4955 printf ("%*ckind = ", indent, ' ');
4956 switch (kind)
4958 case TYPE_SCALAR:
4959 printf ("TYPE_SCALAR");
4960 break;
4961 case TYPE_STRING:
4962 printf ("TYPE_STRING");
4963 break;
4964 case TYPE_STRUCT:
4965 printf ("TYPE_STRUCT");
4966 break;
4967 case TYPE_UNDEFINED:
4968 printf ("TYPE_UNDEFINED");
4969 break;
4970 case TYPE_USER_STRUCT:
4971 printf ("TYPE_USER_STRUCT");
4972 break;
4973 case TYPE_UNION:
4974 printf ("TYPE_UNION");
4975 break;
4976 case TYPE_POINTER:
4977 printf ("TYPE_POINTER");
4978 break;
4979 case TYPE_ARRAY:
4980 printf ("TYPE_ARRAY");
4981 break;
4982 case TYPE_LANG_STRUCT:
4983 printf ("TYPE_LANG_STRUCT");
4984 break;
4985 case TYPE_PARAM_STRUCT:
4986 printf ("TYPE_PARAM_STRUCT");
4987 break;
4988 default:
4989 gcc_unreachable ();
4991 printf ("\n");
4994 /* Dumps the value of GC_USED flag. */
4996 static void
4997 dump_gc_used (int indent, enum gc_used_enum gc_used)
4999 printf ("%*cgc_used = ", indent, ' ');
5000 switch (gc_used)
5002 case GC_UNUSED:
5003 printf ("GC_UNUSED");
5004 break;
5005 case GC_USED:
5006 printf ("GC_USED");
5007 break;
5008 case GC_MAYBE_POINTED_TO:
5009 printf ("GC_MAYBE_POINTED_TO");
5010 break;
5011 case GC_POINTED_TO:
5012 printf ("GC_POINTED_TO");
5013 break;
5014 default:
5015 gcc_unreachable ();
5017 printf ("\n");
5020 /* Dumps the type options OPT. */
5022 static void
5023 dump_options (int indent, options_p opt)
5025 options_p o;
5026 printf ("%*coptions = ", indent, ' ');
5027 o = opt;
5028 while (o)
5030 switch (o->kind)
5032 case OPTION_STRING:
5033 printf ("%s:string %s ", o->name, o->info.string);
5034 break;
5035 case OPTION_TYPE:
5036 printf ("%s:type ", o->name);
5037 dump_type (indent+1, o->info.type);
5038 break;
5039 case OPTION_NESTED:
5040 printf ("%s:nested ", o->name);
5041 break;
5042 case OPTION_NONE:
5043 gcc_unreachable ();
5045 o = o->next;
5047 printf ("\n");
5050 /* Dumps the source file location in LINE. */
5052 static void
5053 dump_fileloc (int indent, struct fileloc line)
5055 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
5056 get_input_file_name (line.file),
5057 line.line);
5060 /* Recursively dumps the struct, union, or a language-specific
5061 struct T. */
5063 static void
5064 dump_type_u_s (int indent, type_p t)
5066 pair_p fields;
5068 gcc_assert (union_or_struct_p (t));
5069 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
5070 dump_fileloc (indent, t->u.s.line);
5071 printf ("%*cu.s.fields =\n", indent, ' ');
5072 fields = t->u.s.fields;
5073 while (fields)
5075 dump_pair (indent + INDENT, fields);
5076 fields = fields->next;
5078 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
5079 dump_options (indent, t->u.s.opt);
5080 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
5081 if (t->kind == TYPE_LANG_STRUCT)
5083 printf ("%*cu.s.lang_struct:\n", indent, ' ');
5084 dump_type_list (indent + INDENT, t->u.s.lang_struct);
5088 /* Recursively dumps the array T. */
5090 static void
5091 dump_type_u_a (int indent, type_p t)
5093 gcc_assert (t->kind == TYPE_ARRAY);
5094 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
5095 dump_type_list (indent + INDENT, t->u.a.p);
5098 /* Recursively dumps the parameterized struct T. */
5100 static void
5101 dump_type_u_param_struct (int indent, type_p t)
5103 int i;
5104 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
5105 printf ("%*cu.param_struct.stru:\n", indent, ' ');
5106 dump_type_list (indent, t->u.param_struct.stru);
5107 dump_fileloc (indent, t->u.param_struct.line);
5108 for (i = 0; i < NUM_PARAM; i++)
5110 if (t->u.param_struct.param[i] == NULL)
5111 continue;
5112 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
5113 dump_type (indent + INDENT, t->u.param_struct.param[i]);
5117 /* Recursively dumps the type list T. */
5119 static void
5120 dump_type_list (int indent, type_p t)
5122 type_p p = t;
5123 while (p)
5125 dump_type (indent, p);
5126 p = p->next;
5130 static htab_t seen_types;
5132 /* Recursively dumps the type T if it was not dumped previously. */
5134 static void
5135 dump_type (int indent, type_p t)
5137 PTR *slot;
5139 if (seen_types == NULL)
5140 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
5142 printf ("%*cType at %p: ", indent, ' ', (void *) t);
5143 slot = htab_find_slot (seen_types, t, INSERT);
5144 if (*slot != NULL)
5146 printf ("already seen.\n");
5147 return;
5149 *slot = t;
5150 printf ("\n");
5152 dump_typekind (indent, t->kind);
5153 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
5154 (void *) t->pointer_to);
5155 dump_gc_used (indent + INDENT, t->gc_used);
5156 switch (t->kind)
5158 case TYPE_SCALAR:
5159 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
5160 t->u.scalar_is_char ? "true" : "false");
5161 break;
5162 case TYPE_STRING:
5163 break;
5164 case TYPE_STRUCT:
5165 case TYPE_UNION:
5166 case TYPE_LANG_STRUCT:
5167 case TYPE_USER_STRUCT:
5168 dump_type_u_s (indent + INDENT, t);
5169 break;
5170 case TYPE_POINTER:
5171 printf ("%*cp:\n", indent + INDENT, ' ');
5172 dump_type (indent + INDENT, t->u.p);
5173 break;
5174 case TYPE_ARRAY:
5175 dump_type_u_a (indent + INDENT, t);
5176 break;
5177 case TYPE_PARAM_STRUCT:
5178 dump_type_u_param_struct (indent + INDENT, t);
5179 break;
5180 default:
5181 gcc_unreachable ();
5183 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
5186 /* Dumps the pair P. */
5188 static void
5189 dump_pair (int indent, pair_p p)
5191 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
5192 dump_type (indent, p->type);
5193 dump_fileloc (indent, p->line);
5194 dump_options (indent, p->opt);
5195 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
5198 /* Dumps the list of pairs PP. */
5200 static void
5201 dump_pair_list (const char *name, pair_p pp)
5203 pair_p p;
5204 printf ("%s:\n", name);
5205 for (p = pp; p != NULL; p = p->next)
5206 dump_pair (0, p);
5207 printf ("End of %s\n\n", name);
5210 /* Dumps the STRUCTURES. */
5212 static void
5213 dump_structures (const char *name, type_p structures)
5215 printf ("%s:\n", name);
5216 dump_type_list (0, structures);
5217 printf ("End of %s\n\n", name);
5220 /* Dumps the internal structures of gengtype. This is useful to debug
5221 gengtype itself, or to understand what it does, e.g. for plugin
5222 developers. */
5224 static void
5225 dump_everything (void)
5227 dump_pair_list ("typedefs", typedefs);
5228 dump_structures ("structures", structures);
5229 dump_structures ("param_structs", param_structs);
5230 dump_pair_list ("variables", variables);
5232 /* Allocated with the first call to dump_type. */
5233 htab_delete (seen_types);
5238 /* Option specification for getopt_long. */
5239 static const struct option gengtype_long_options[] = {
5240 {"help", no_argument, NULL, 'h'},
5241 {"version", no_argument, NULL, 'V'},
5242 {"verbose", no_argument, NULL, 'v'},
5243 {"dump", no_argument, NULL, 'd'},
5244 {"debug", no_argument, NULL, 'D'},
5245 {"plugin", required_argument, NULL, 'P'},
5246 {"srcdir", required_argument, NULL, 'S'},
5247 {"backupdir", required_argument, NULL, 'B'},
5248 {"inputs", required_argument, NULL, 'I'},
5249 {"read-state", required_argument, NULL, 'r'},
5250 {"write-state", required_argument, NULL, 'w'},
5251 /* Terminating NULL placeholder. */
5252 {NULL, no_argument, NULL, 0},
5256 static void
5257 print_usage (void)
5259 printf ("Usage: %s\n", progname);
5260 printf ("\t -h | --help " " \t# Give this help.\n");
5261 printf ("\t -D | --debug "
5262 " \t# Give debug output to debug %s itself.\n", progname);
5263 printf ("\t -V | --version " " \t# Give version information.\n");
5264 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5265 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5266 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5267 " \t# Generate for plugin.\n");
5268 printf ("\t -S | --srcdir <GCC-directory> "
5269 " \t# Specify the GCC source directory.\n");
5270 printf ("\t -B | --backupdir <directory> "
5271 " \t# Specify the backup directory for updated files.\n");
5272 printf ("\t -I | --inputs <input-list> "
5273 " \t# Specify the file with source files list.\n");
5274 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5275 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5278 static void
5279 print_version (void)
5281 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5282 printf ("Report bugs: %s\n", bug_report_url);
5285 /* Parse the program options using getopt_long... */
5286 static void
5287 parse_program_options (int argc, char **argv)
5289 int opt = -1;
5290 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5291 gengtype_long_options, NULL)) >= 0)
5293 switch (opt)
5295 case 'h': /* --help */
5296 print_usage ();
5297 break;
5298 case 'V': /* --version */
5299 print_version ();
5300 break;
5301 case 'd': /* --dump */
5302 do_dump = 1;
5303 break;
5304 case 'D': /* --debug */
5305 do_debug = 1;
5306 break;
5307 case 'v': /* --verbose */
5308 verbosity_level++;
5309 break;
5310 case 'P': /* --plugin */
5311 if (optarg)
5312 plugin_output_filename = optarg;
5313 else
5314 fatal ("missing plugin output file name");
5315 break;
5316 case 'S': /* --srcdir */
5317 if (optarg)
5318 srcdir = optarg;
5319 else
5320 fatal ("missing source directory");
5321 srcdir_len = strlen (srcdir);
5322 break;
5323 case 'B': /* --backupdir */
5324 if (optarg)
5325 backup_dir = optarg;
5326 else
5327 fatal ("missing backup directory");
5328 break;
5329 case 'I': /* --inputs */
5330 if (optarg)
5331 inputlist = optarg;
5332 else
5333 fatal ("missing input list");
5334 break;
5335 case 'r': /* --read-state */
5336 if (optarg)
5337 read_state_filename = optarg;
5338 else
5339 fatal ("missing read state file");
5340 DBGPRINTF ("read state %s\n", optarg);
5341 break;
5342 case 'w': /* --write-state */
5343 DBGPRINTF ("write state %s\n", optarg);
5344 if (optarg)
5345 write_state_filename = optarg;
5346 else
5347 fatal ("missing write state file");
5348 break;
5349 default:
5350 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5351 print_usage ();
5352 fatal ("unexpected flag");
5355 if (plugin_output_filename)
5357 /* In plugin mode we require some input files. */
5358 int i = 0;
5359 if (optind >= argc)
5360 fatal ("no source files given in plugin mode");
5361 nb_plugin_files = argc - optind;
5362 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5363 for (i = 0; i < (int) nb_plugin_files; i++)
5365 char *name = argv[i + optind];
5366 plugin_files[i] = input_file_by_name (name);
5373 /******* Manage input files. ******/
5375 /* Hash table of unique input file names. */
5376 static htab_t input_file_htab;
5378 /* Find or allocate a new input_file by hash-consing it. */
5379 input_file*
5380 input_file_by_name (const char* name)
5382 PTR* slot;
5383 input_file* f = NULL;
5384 int namlen = 0;
5385 if (!name)
5386 return NULL;
5387 namlen = strlen (name);
5388 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5389 f->inpbitmap = 0;
5390 f->inpoutf = NULL;
5391 f->inpisplugin = false;
5392 strcpy (f->inpname, name);
5393 slot = htab_find_slot (input_file_htab, f, INSERT);
5394 gcc_assert (slot != NULL);
5395 if (*slot)
5397 /* Already known input file. */
5398 free (f);
5399 return (input_file*)(*slot);
5401 /* New input file. */
5402 *slot = f;
5403 return f;
5406 /* Hash table support routines for input_file-s. */
5407 static hashval_t
5408 htab_hash_inputfile (const void *p)
5410 const input_file *inpf = (const input_file *) p;
5411 gcc_assert (inpf);
5412 return htab_hash_string (get_input_file_name (inpf));
5415 static int
5416 htab_eq_inputfile (const void *x, const void *y)
5418 const input_file *inpfx = (const input_file *) x;
5419 const input_file *inpfy = (const input_file *) y;
5420 gcc_assert (inpfx != NULL && inpfy != NULL);
5421 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5426 main (int argc, char **argv)
5428 size_t i;
5429 static struct fileloc pos = { NULL, 0 };
5430 outf_p output_header;
5432 /* Mandatory common initializations. */
5433 progname = "gengtype"; /* For fatal and messages. */
5434 /* Create the hash-table used to hash-cons input files. */
5435 input_file_htab =
5436 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5437 /* Initialize our special input files. */
5438 this_file = input_file_by_name (__FILE__);
5439 system_h_file = input_file_by_name ("system.h");
5440 /* Set the scalar_is_char union number for predefined scalar types. */
5441 scalar_nonchar.u.scalar_is_char = FALSE;
5442 scalar_char.u.scalar_is_char = TRUE;
5444 parse_program_options (argc, argv);
5446 #if ENABLE_CHECKING
5447 if (do_debug)
5449 time_t now = (time_t) 0;
5450 time (&now);
5451 DBGPRINTF ("gengtype started pid %d at %s",
5452 (int) getpid (), ctime (&now));
5454 #endif /* ENABLE_CHECKING */
5456 /* Parse the input list and the input files. */
5457 DBGPRINTF ("inputlist %s", inputlist);
5458 if (read_state_filename)
5460 if (inputlist)
5461 fatal ("input list %s cannot be given with a read state file %s",
5462 inputlist, read_state_filename);
5463 read_state (read_state_filename);
5464 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5465 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs);
5467 else if (inputlist)
5469 /* These types are set up with #define or else outside of where
5470 we can see them. We should initialize them before calling
5471 read_input_list. */
5472 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5473 Call;} while (0)
5474 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5475 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5476 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5477 POS_HERE (do_scalar_typedef ("double_int", &pos));
5478 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5479 POS_HERE (do_scalar_typedef ("uint8", &pos));
5480 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5481 POS_HERE (do_scalar_typedef ("jword", &pos));
5482 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5483 POS_HERE (do_scalar_typedef ("void", &pos));
5484 POS_HERE (do_typedef ("PTR",
5485 create_pointer (resolve_typedef ("void", &pos)),
5486 &pos));
5487 #undef POS_HERE
5488 read_input_list (inputlist);
5489 for (i = 0; i < num_gt_files; i++)
5491 parse_file (get_input_file_name (gt_files[i]));
5492 DBGPRINTF ("parsed file #%d %s",
5493 (int) i, get_input_file_name (gt_files[i]));
5495 if (verbosity_level >= 1)
5496 printf ("%s parsed %d files with %d GTY types\n",
5497 progname, (int) num_gt_files, type_count);
5499 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5500 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
5503 else
5504 fatal ("either an input list or a read state file should be given");
5505 if (hit_error)
5506 return 1;
5509 if (plugin_output_filename)
5511 size_t ix = 0;
5512 /* In plugin mode, we should have read a state file, and have
5513 given at least one plugin file. */
5514 if (!read_state_filename)
5515 fatal ("No read state given in plugin mode for %s",
5516 plugin_output_filename);
5518 if (nb_plugin_files == 0 || !plugin_files)
5519 fatal ("No plugin files given in plugin mode for %s",
5520 plugin_output_filename);
5522 /* Parse our plugin files and augment the state. */
5523 for (ix = 0; ix < nb_plugin_files; ix++)
5525 input_file* pluginput = plugin_files [ix];
5526 pluginput->inpisplugin = true;
5527 parse_file (get_input_file_name (pluginput));
5529 if (hit_error)
5530 return 1;
5532 plugin_output = create_file ("GCC", plugin_output_filename);
5533 DBGPRINTF ("created plugin_output %p named %s",
5534 (void *) plugin_output, plugin_output->name);
5536 else
5537 { /* No plugin files, we are in normal mode. */
5538 if (!srcdir)
5539 fatal ("gengtype needs a source directory in normal mode");
5541 if (hit_error)
5542 return 1;
5544 gen_rtx_next ();
5546 /* The call to set_gc_used may indirectly call find_param_structure
5547 hence enlarge the param_structs list of types. */
5548 set_gc_used (variables);
5550 /* The state at this point is read from the state input file or by
5551 parsing source files and optionally augmented by parsing plugin
5552 source files. Write it now. */
5553 if (write_state_filename)
5555 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5556 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs);
5558 if (hit_error)
5559 fatal ("didn't write state file %s after errors",
5560 write_state_filename);
5562 DBGPRINTF ("before write_state %s", write_state_filename);
5563 write_state (write_state_filename);
5565 if (do_dump)
5566 dump_everything ();
5568 /* After having written the state file we return immediately to
5569 avoid generating any output file. */
5570 if (hit_error)
5571 return 1;
5572 else
5573 return 0;
5577 open_base_files ();
5579 output_header = plugin_output ? plugin_output : header_file;
5580 write_typed_alloc_defns (output_header, structures, typedefs);
5581 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5582 structures);
5583 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5584 param_structs);
5586 write_types (output_header, structures, param_structs, &ggc_wtd);
5587 if (plugin_files == NULL)
5589 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5590 structures);
5591 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5592 param_structs);
5593 write_types (header_file, structures, param_structs, &pch_wtd);
5594 write_local (header_file, structures, param_structs);
5596 write_splay_tree_allocators (param_structs);
5597 write_roots (variables, plugin_files == NULL);
5598 write_rtx_next ();
5599 close_output_files ();
5601 if (do_dump)
5602 dump_everything ();
5604 /* Don't bother about free-ing any input or plugin file, etc. */
5606 if (hit_error)
5607 return 1;
5608 return 0;