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