[RS6000] Don't be too clever with dg-do run and dg-do compile
[official-gcc.git] / gcc / gengtype.c
blobdd21ade57956ec6c09f8ba68e2469e6023255035
1 /* Process source files and output type information.
2 Copyright (C) 2002-2020 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 "xregex.h"
31 #include "obstack.h"
32 #include "gengtype.h"
33 #include "filenames.h"
35 /* Data types, macros, etc. used only in this file. */
38 /* The list of output files. */
39 outf_p output_files;
41 /* The output header file that is included into pretty much every
42 source file. */
43 outf_p header_file;
46 /* The name of the file containing the list of input files. */
47 static char *inputlist;
49 /* The plugin input files and their number; in that case only
50 a single file is produced. */
51 static input_file **plugin_files;
52 static size_t nb_plugin_files;
54 /* The generated plugin output file and name. */
55 static outf_p plugin_output;
56 static char *plugin_output_filename;
58 /* Our source directory and its length. */
59 const char *srcdir;
60 size_t srcdir_len;
62 /* Variables used for reading and writing the state. */
63 const char *read_state_filename;
64 const char *write_state_filename;
66 /* Variables to help debugging. */
67 int do_dump;
68 int do_debug;
70 /* Level for verbose messages. */
71 int verbosity_level;
73 /* We have a type count and use it to set the state_number of newly
74 allocated types to some unique negative number. */
75 static int type_count;
77 /* The backup directory should be in the same file system as the
78 generated files, otherwise the rename(2) system call would fail.
79 If NULL, no backup is made when overwriting a generated file. */
80 static const char* backup_dir; /* (-B) program option. */
83 static outf_p create_file (const char *, const char *);
85 static const char *get_file_basename (const input_file *);
86 static const char *get_file_realbasename (const input_file *);
88 static int get_prefix_langdir_index (const char *);
89 static const char *get_file_langdir (const input_file *);
91 static void dump_pair (int indent, pair_p p);
92 static void dump_type (int indent, type_p p);
93 static void dump_type_list (int indent, type_p p);
96 /* Nonzero iff an error has occurred. */
97 bool hit_error = false;
99 static void gen_rtx_next (void);
100 static void write_rtx_next (void);
101 static void open_base_files (void);
102 static void close_output_files (void);
104 /* Report an error at POS, printing MSG. */
106 void
107 error_at_line (const struct fileloc *pos, const char *msg, ...)
109 va_list ap;
111 gcc_assert (pos != NULL && pos->file != NULL);
112 va_start (ap, msg);
114 fprintf (stderr, "%s:%d: ", get_input_file_name (pos->file), pos->line);
115 vfprintf (stderr, msg, ap);
116 fputc ('\n', stderr);
117 hit_error = true;
119 va_end (ap);
122 /* Locate the ultimate base class of struct S. */
124 static const_type_p
125 get_ultimate_base_class (const_type_p s)
127 while (s->u.s.base_class)
128 s = s->u.s.base_class;
129 return s;
132 static type_p
133 get_ultimate_base_class (type_p s)
135 while (s->u.s.base_class)
136 s = s->u.s.base_class;
137 return s;
140 /* Input file handling. */
142 /* Table of all input files. */
143 const input_file **gt_files;
144 size_t num_gt_files;
146 /* A number of places use the name of this "gengtype.c" file for a
147 location for things that we can't rely on the source to define.
148 Make sure we can still use pointer comparison on filenames. */
149 input_file* this_file;
150 /* The "system.h" file is likewise specially useful. */
151 input_file* system_h_file;
153 /* Vector of per-language directories. */
154 const char **lang_dir_names;
155 size_t num_lang_dirs;
157 /* An array of output files suitable for definitions. There is one
158 BASE_FILES entry for each language. */
159 static outf_p *base_files;
161 /* Utility debugging function, printing the various type counts within
162 a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
163 void
164 dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
166 int nb_types = 0, nb_scalar = 0, nb_string = 0;
167 int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
168 int nb_lang_struct = 0;
169 int nb_user_struct = 0, nb_undefined = 0;
170 type_p p = NULL;
171 for (p = t; p; p = p->next)
173 nb_types++;
174 switch (p->kind)
176 case TYPE_UNDEFINED:
177 nb_undefined++;
178 break;
179 case TYPE_SCALAR:
180 nb_scalar++;
181 break;
182 case TYPE_STRING:
183 nb_string++;
184 break;
185 case TYPE_STRUCT:
186 nb_struct++;
187 break;
188 case TYPE_USER_STRUCT:
189 nb_user_struct++;
190 break;
191 case TYPE_UNION:
192 nb_union++;
193 break;
194 case TYPE_POINTER:
195 nb_pointer++;
196 break;
197 case TYPE_ARRAY:
198 nb_array++;
199 break;
200 case TYPE_LANG_STRUCT:
201 nb_lang_struct++;
202 break;
203 case TYPE_NONE:
204 gcc_unreachable ();
207 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
208 lbasename (fil), lin, msg, nb_types);
209 if (nb_scalar > 0 || nb_string > 0)
210 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
211 if (nb_struct > 0 || nb_union > 0)
212 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
213 if (nb_pointer > 0 || nb_array > 0)
214 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
215 if (nb_lang_struct > 0)
216 fprintf (stderr, "@@%%@@ %d lang_structs\n", nb_lang_struct);
217 if (nb_user_struct > 0)
218 fprintf (stderr, "@@%%@@ %d user_structs\n", nb_user_struct);
219 if (nb_undefined > 0)
220 fprintf (stderr, "@@%%@@ %d undefined types\n", nb_undefined);
221 fprintf (stderr, "\n");
224 /* Scan the input file, LIST, and determine how much space we need to
225 store strings in. Also, count the number of language directories
226 and files. The numbers returned are overestimates as they does not
227 consider repeated files. */
228 static size_t
229 measure_input_list (FILE *list)
231 size_t n = 0;
232 int c;
233 bool atbol = true;
234 num_lang_dirs = 0;
235 num_gt_files = plugin_files ? nb_plugin_files : 0;
236 while ((c = getc (list)) != EOF)
238 n++;
239 if (atbol)
241 if (c == '[')
242 num_lang_dirs++;
243 else
245 /* Add space for a lang_bitmap before the input file name. */
246 n += sizeof (lang_bitmap);
247 num_gt_files++;
249 atbol = false;
252 if (c == '\n')
253 atbol = true;
256 rewind (list);
257 return n;
260 /* Read one input line from LIST to HEREP (which is updated). A
261 pointer to the string is returned via LINEP. If it was a language
262 subdirectory in square brackets, strip off the square brackets and
263 return true. Otherwise, leave space before the string for a
264 lang_bitmap, and return false. At EOF, returns false, does not
265 touch *HEREP, and sets *LINEP to NULL. POS is used for
266 diagnostics. */
267 static bool
268 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
270 char *here = *herep;
271 char *line;
272 int c = getc (list);
274 /* Read over whitespace. */
275 while (c == '\n' || c == ' ')
276 c = getc (list);
278 if (c == EOF)
280 *linep = 0;
281 return false;
283 else if (c == '[')
285 /* No space for a lang_bitmap is necessary. Discard the '['. */
286 c = getc (list);
287 line = here;
288 while (c != ']' && c != '\n' && c != EOF)
290 *here++ = c;
291 c = getc (list);
293 *here++ = '\0';
295 if (c == ']')
297 c = getc (list); /* eat what should be a newline */
298 if (c != '\n' && c != EOF)
299 error_at_line (pos, "junk on line after language tag [%s]", line);
301 else
302 error_at_line (pos, "missing close bracket for language tag [%s",
303 line);
305 *herep = here;
306 *linep = line;
307 return true;
309 else
311 /* Leave space for a lang_bitmap. */
312 memset (here, 0, sizeof (lang_bitmap));
313 here += sizeof (lang_bitmap);
314 line = here;
317 *here++ = c;
318 c = getc (list);
320 while (c != EOF && c != '\n');
321 *here++ = '\0';
322 *herep = here;
323 *linep = line;
324 return false;
328 /* Read the list of input files from LIST and compute all of the
329 relevant tables. There is one file per line of the list. At
330 first, all the files on the list are language-generic, but
331 eventually a line will appear which is the name of a language
332 subdirectory in square brackets, like this: [cp]. All subsequent
333 files are specific to that language, until another language
334 subdirectory tag appears. Files can appear more than once, if
335 they apply to more than one language. */
336 static void
337 read_input_list (const char *listname)
339 FILE *list = fopen (listname, "r");
340 if (!list)
341 fatal ("cannot open %s: %s", listname, xstrerror (errno));
342 else
344 struct fileloc epos;
345 size_t bufsz = measure_input_list (list);
346 char *buf = XNEWVEC (char, bufsz);
347 char *here = buf;
348 char *committed = buf;
349 char *limit = buf + bufsz;
350 char *line;
351 bool is_language;
352 size_t langno = 0;
353 size_t nfiles = 0;
354 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
356 epos.file = input_file_by_name (listname);
357 epos.line = 0;
359 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
360 gt_files = XNEWVEC (const input_file *, num_gt_files);
362 for (;;)
364 next_line:
365 epos.line++;
366 committed = here;
367 is_language = read_input_line (list, &here, &line, &epos);
368 gcc_assert (here <= limit);
369 if (line == 0)
370 break;
371 else if (is_language)
373 size_t i;
374 gcc_assert (langno <= num_lang_dirs);
375 for (i = 0; i < langno; i++)
376 if (strcmp (lang_dir_names[i], line) == 0)
378 error_at_line (&epos, "duplicate language tag [%s]",
379 line);
380 curlangs = 1 << i;
381 here = committed;
382 goto next_line;
385 curlangs = 1 << langno;
386 lang_dir_names[langno++] = line;
388 else
390 size_t i;
391 input_file *inpf = input_file_by_name (line);
392 gcc_assert (nfiles <= num_gt_files);
393 for (i = 0; i < nfiles; i++)
394 /* Since the input_file-s are uniquely hash-consed, we
395 can just compare pointers! */
396 if (gt_files[i] == inpf)
398 /* Throw away the string we just read, and add the
399 current language to the existing string's bitmap. */
400 lang_bitmap bmap = get_lang_bitmap (inpf);
401 if (bmap & curlangs)
402 error_at_line (&epos,
403 "file %s specified more than once "
404 "for language %s", line,
405 langno ==
406 0 ? "(all)" : lang_dir_names[langno -
407 1]);
409 bmap |= curlangs;
410 set_lang_bitmap (inpf, bmap);
411 here = committed;
412 goto next_line;
415 set_lang_bitmap (inpf, curlangs);
416 gt_files[nfiles++] = inpf;
419 /* Update the global counts now that we know accurately how many
420 things there are. (We do not bother resizing the arrays down.) */
421 num_lang_dirs = langno;
422 /* Add the plugin files if provided. */
423 if (plugin_files)
425 size_t i;
426 for (i = 0; i < nb_plugin_files; i++)
427 gt_files[nfiles++] = plugin_files[i];
429 num_gt_files = nfiles;
432 /* Sanity check: any file that resides in a language subdirectory
433 (e.g. 'cp') ought to belong to the corresponding language.
434 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
435 (Can you even do that? Should you be allowed to?) */
437 size_t f;
438 for (f = 0; f < num_gt_files; f++)
440 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
441 const char *basename = get_file_basename (gt_files[f]);
442 const char *slashpos = strchr (basename, '/');
443 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
444 const char *slashpos2 = strchr (basename, '\\');
446 if (!slashpos || (slashpos2 && slashpos2 < slashpos))
447 slashpos = slashpos2;
448 #endif
450 if (slashpos)
452 size_t l;
453 for (l = 0; l < num_lang_dirs; l++)
454 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
455 && memcmp (basename, lang_dir_names[l],
456 strlen (lang_dir_names[l])) == 0)
458 if (!(bitmap & (1 << l)))
459 error ("%s is in language directory '%s' but is not "
460 "tagged for that language",
461 basename, lang_dir_names[l]);
462 break;
468 if (ferror (list))
469 fatal ("error reading %s: %s", listname, xstrerror (errno));
471 fclose (list);
476 /* The one and only TYPE_STRING. */
478 struct type string_type = {
479 TYPE_STRING, 0, 0, 0, GC_USED, {0}
482 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
483 set early in main. */
485 struct type scalar_nonchar = {
486 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
489 struct type scalar_char = {
490 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
493 /* Lists of various things. */
495 pair_p typedefs = NULL;
496 type_p structures = NULL;
497 pair_p variables = NULL;
499 static type_p adjust_field_tree_exp (type_p t, options_p opt);
500 static type_p adjust_field_rtx_def (type_p t, options_p opt);
502 /* Define S as a typedef to T at POS. */
504 void
505 do_typedef (const char *s, type_p t, struct fileloc *pos)
507 pair_p p;
509 /* temporary kludge - gengtype doesn't handle conditionals or
510 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
511 is coming from this file (main() sets them up with safe dummy
512 definitions). */
513 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
514 return;
516 for (p = typedefs; p != NULL; p = p->next)
517 if (strcmp (p->name, s) == 0)
519 if (p->type != t && strcmp (s, "result_type") != 0)
521 error_at_line (pos, "type `%s' previously defined", s);
522 error_at_line (&p->line, "previously defined here");
524 return;
527 p = XNEW (struct pair);
528 p->next = typedefs;
529 p->name = s;
530 p->type = t;
531 p->line = *pos;
532 p->opt = NULL;
533 typedefs = p;
536 /* Define S as a typename of a scalar. Cannot be used to define
537 typedefs of 'char'. Note: is also used for pointer-to-function
538 typedefs (which are therefore not treated as pointers). */
540 void
541 do_scalar_typedef (const char *s, struct fileloc *pos)
543 do_typedef (s, &scalar_nonchar, pos);
546 /* Similar to strtok_r. */
548 static char *
549 strtoken (char *str, const char *delim, char **next)
551 char *p;
553 if (str == NULL)
554 str = *next;
556 /* Skip the leading delimiters. */
557 str += strspn (str, delim);
558 if (*str == '\0')
559 /* This is an empty token. */
560 return NULL;
562 /* The current token. */
563 p = str;
565 /* Find the next delimiter. */
566 str += strcspn (str, delim);
567 if (*str == '\0')
568 /* This is the last token. */
569 *next = str;
570 else
572 /* Terminate the current token. */
573 *str = '\0';
574 /* Advance to the next token. */
575 *next = str + 1;
578 return p;
581 /* Define TYPE_NAME to be a user defined type at location POS. */
583 type_p
584 create_user_defined_type (const char *type_name, struct fileloc *pos)
586 type_p ty = find_structure (type_name, TYPE_USER_STRUCT);
588 /* We might have already seen an incomplete decl of the given type,
589 in which case we won't have yet seen a GTY((user)), and the type will
590 only have kind "TYPE_STRUCT". Mark it as a user struct. */
591 ty->kind = TYPE_USER_STRUCT;
593 ty->u.s.line = *pos;
594 ty->u.s.bitmap = get_lang_bitmap (pos->file);
595 do_typedef (type_name, ty, pos);
597 /* If TYPE_NAME specifies a template, create references to the types
598 in the template by pretending that each type is a field of TY.
599 This is needed to make sure that the types referenced by the
600 template are marked as used. */
601 char *str = xstrdup (type_name);
602 char *open_bracket = strchr (str, '<');
603 if (open_bracket)
605 /* We only accept simple template declarations (see
606 require_template_declaration), so we only need to parse a
607 comma-separated list of strings, implicitly assumed to
608 be type names, potentially with "*" characters. */
609 char *arg = open_bracket + 1;
610 /* Workaround -Wmaybe-uninitialized false positive during
611 profiledbootstrap by initializing it. */
612 char *next = NULL;
613 char *type_id = strtoken (arg, ",>", &next);
614 pair_p fields = 0;
615 while (type_id)
617 /* Create a new field for every type found inside the template
618 parameter list. */
620 /* Support a single trailing "*" character. */
621 const char *star = strchr (type_id, '*');
622 int is_ptr = (star != NULL);
623 size_t offset_to_star = star - type_id;
624 if (is_ptr)
625 offset_to_star = star - type_id;
627 if (strstr (type_id, "char*"))
629 type_id = strtoken (0, ",>", &next);
630 continue;
633 char *field_name = xstrdup (type_id);
635 type_p arg_type;
636 if (is_ptr)
638 /* Strip off the first '*' character (and any subsequent text). */
639 *(field_name + offset_to_star) = '\0';
641 arg_type = find_structure (field_name, TYPE_STRUCT);
642 arg_type = create_pointer (arg_type);
644 else
645 arg_type = resolve_typedef (field_name, pos);
647 fields = create_field_at (fields, arg_type, field_name, 0, pos);
648 type_id = strtoken (0, ",>", &next);
651 /* Associate the field list to TY. */
652 ty->u.s.fields = fields;
654 free (str);
656 return ty;
660 /* Given a typedef name S, return its associated type. Return NULL if
661 S is not a registered type name. */
663 static type_p
664 type_for_name (const char *s)
666 pair_p p;
668 /* Special-case support for types within a "gcc::" namespace. Rather
669 than fully-supporting namespaces, simply strip off the "gcc::" prefix
670 where present. This allows us to have GTY roots of this form:
671 extern GTY(()) gcc::some_type *some_ptr;
672 where the autogenerated functions will refer to simply "some_type",
673 where they can be resolved into their namespace. */
674 if (strncmp (s, "gcc::", 5) == 0)
675 s += 5;
677 for (p = typedefs; p != NULL; p = p->next)
678 if (strcmp (p->name, s) == 0)
679 return p->type;
680 return NULL;
684 /* Create an undefined type with name S and location POS. Return the
685 newly created type. */
687 static type_p
688 create_undefined_type (const char *s, struct fileloc *pos)
690 type_p ty = find_structure (s, TYPE_UNDEFINED);
691 ty->u.s.line = *pos;
692 ty->u.s.bitmap = get_lang_bitmap (pos->file);
693 do_typedef (s, ty, pos);
694 return ty;
698 /* Return the type previously defined for S. Use POS to report errors. */
700 type_p
701 resolve_typedef (const char *s, struct fileloc *pos)
703 bool is_template_instance = (strchr (s, '<') != NULL);
704 type_p p = type_for_name (s);
706 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
707 type for regular type identifiers. If the type identifier S is a
708 template instantiation, however, we treat it as a user defined
709 type.
711 FIXME, this is actually a limitation in gengtype. Supporting
712 template types and their instances would require keeping separate
713 track of the basic types definition and its instances. This
714 essentially forces all template classes in GC to be marked
715 GTY((user)). */
716 if (!p)
717 p = (is_template_instance)
718 ? create_user_defined_type (s, pos)
719 : create_undefined_type (s, pos);
721 return p;
724 /* Add SUBCLASS to head of linked list of BASE's subclasses. */
726 void add_subclass (type_p base, type_p subclass)
728 gcc_assert (union_or_struct_p (base));
729 gcc_assert (union_or_struct_p (subclass));
731 subclass->u.s.next_sibling_class = base->u.s.first_subclass;
732 base->u.s.first_subclass = subclass;
735 /* Create and return a new structure with tag NAME at POS with fields
736 FIELDS and options O. The KIND of structure must be one of
737 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
739 type_p
740 new_structure (const char *name, enum typekind kind, struct fileloc *pos,
741 pair_p fields, options_p o, type_p base_class)
743 type_p si;
744 type_p s = NULL;
745 lang_bitmap bitmap = get_lang_bitmap (pos->file);
746 bool isunion = (kind == TYPE_UNION);
747 type_p *p = &structures;
749 gcc_assert (union_or_struct_p (kind));
751 for (si = structures; si != NULL; p = &si->next, si = *p)
752 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
754 type_p ls = NULL;
755 if (si->kind == TYPE_LANG_STRUCT)
757 ls = si;
759 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
760 if (si->u.s.bitmap == bitmap)
761 s = si;
763 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
765 ls = si;
766 type_count++;
767 si = XCNEW (struct type);
768 memcpy (si, ls, sizeof (struct type));
769 ls->kind = TYPE_LANG_STRUCT;
770 ls->u.s.lang_struct = si;
771 ls->u.s.fields = NULL;
772 si->next = NULL;
773 si->state_number = -type_count;
774 si->pointer_to = NULL;
775 si->u.s.lang_struct = ls;
777 else
778 s = si;
780 if (ls != NULL && s == NULL)
782 type_count++;
783 s = XCNEW (struct type);
784 s->state_number = -type_count;
785 s->next = ls->u.s.lang_struct;
786 ls->u.s.lang_struct = s;
787 s->u.s.lang_struct = ls;
789 break;
792 if (s == NULL)
794 type_count++;
795 s = XCNEW (struct type);
796 s->state_number = -type_count;
797 *p = s;
800 if (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap))
802 error_at_line (pos, "duplicate definition of '%s %s'",
803 isunion ? "union" : "struct", s->u.s.tag);
804 error_at_line (&s->u.s.line, "previous definition here");
807 s->kind = kind;
808 s->u.s.tag = name;
809 s->u.s.line = *pos;
810 s->u.s.fields = fields;
811 s->u.s.opt = o;
812 s->u.s.bitmap = bitmap;
813 if (s->u.s.lang_struct)
814 s->u.s.lang_struct->u.s.bitmap |= bitmap;
815 s->u.s.base_class = base_class;
816 if (base_class)
817 add_subclass (base_class, s);
819 return s;
822 /* Return the previously-defined structure or union with tag NAME,
823 or a new empty structure or union if none was defined previously.
824 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
825 TYPE_USER_STRUCT. */
827 type_p
828 find_structure (const char *name, enum typekind kind)
830 type_p s;
831 bool isunion = (kind == TYPE_UNION);
832 type_p *p = &structures;
834 gcc_assert (kind == TYPE_UNDEFINED || union_or_struct_p (kind));
836 for (s = structures; s != NULL; p = &s->next, s = *p)
837 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
838 return s;
840 type_count++;
841 s = XCNEW (struct type);
842 s->state_number = -type_count;
843 s->kind = kind;
844 s->u.s.tag = name;
845 *p = s;
846 return s;
849 /* Return a scalar type with name NAME. */
851 type_p
852 create_scalar_type (const char *name)
854 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
855 return &scalar_char;
856 else
857 return &scalar_nonchar;
861 /* Return a pointer to T. */
863 type_p
864 create_pointer (type_p t)
866 if (!t->pointer_to)
868 type_p r = XCNEW (struct type);
869 type_count++;
870 r->state_number = -type_count;
871 r->kind = TYPE_POINTER;
872 r->u.p = t;
873 t->pointer_to = r;
875 return t->pointer_to;
878 /* Return an array of length LEN. */
880 type_p
881 create_array (type_p t, const char *len)
883 type_p v;
885 type_count++;
886 v = XCNEW (struct type);
887 v->kind = TYPE_ARRAY;
888 v->state_number = -type_count;
889 v->u.a.p = t;
890 v->u.a.len = len;
891 return v;
894 /* Return a string options structure with name NAME and info INFO.
895 NEXT is the next option in the chain. */
896 options_p
897 create_string_option (options_p next, const char *name, const char *info)
899 options_p o = XNEW (struct options);
900 o->kind = OPTION_STRING;
901 o->next = next;
902 o->name = name;
903 o->info.string = info;
904 return o;
907 /* Create a type options structure with name NAME and info INFO. NEXT
908 is the next option in the chain. */
909 options_p
910 create_type_option (options_p next, const char* name, type_p info)
912 options_p o = XNEW (struct options);
913 o->next = next;
914 o->name = name;
915 o->kind = OPTION_TYPE;
916 o->info.type = info;
917 return o;
920 /* Create a nested pointer options structure with name NAME and info
921 INFO. NEXT is the next option in the chain. */
922 options_p
923 create_nested_option (options_p next, const char* name,
924 struct nested_ptr_data* info)
926 options_p o;
927 o = XNEW (struct options);
928 o->next = next;
929 o->name = name;
930 o->kind = OPTION_NESTED;
931 o->info.nested = info;
932 return o;
935 /* Return an options structure for a "nested_ptr" option. */
936 options_p
937 create_nested_ptr_option (options_p next, type_p t,
938 const char *to, const char *from)
940 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
942 d->type = adjust_field_type (t, 0);
943 d->convert_to = to;
944 d->convert_from = from;
945 return create_nested_option (next, "nested_ptr", d);
948 /* Add a variable named S of type T with options O defined at POS,
949 to `variables'. */
950 void
951 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
953 pair_p n;
954 n = XNEW (struct pair);
955 n->name = s;
956 n->type = t;
957 n->line = *pos;
958 n->opt = o;
959 n->next = variables;
960 variables = n;
963 /* Most-general structure field creator. */
964 static pair_p
965 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
966 const input_file *inpf, int line)
968 pair_p field;
970 field = XNEW (struct pair);
971 field->next = next;
972 field->type = type;
973 field->name = name;
974 field->opt = opt;
975 field->line.file = inpf;
976 field->line.line = line;
977 return field;
980 /* Create a field that came from the source code we are scanning,
981 i.e. we have a 'struct fileloc', and possibly options; also,
982 adjust_field_type should be called. */
983 pair_p
984 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
985 struct fileloc *pos)
987 return create_field_all (next, adjust_field_type (type, opt),
988 name, opt, pos->file, pos->line);
991 /* Create a fake field with the given type and name. NEXT is the next
992 field in the chain. */
993 #define create_field(next,type,name) \
994 create_field_all (next,type,name, 0, this_file, __LINE__)
996 /* Like create_field, but the field is only valid when condition COND
997 is true. */
999 static pair_p
1000 create_optional_field_ (pair_p next, type_p type, const char *name,
1001 const char *cond, int line)
1003 static int id = 1;
1004 pair_p union_fields;
1005 type_p union_type;
1007 /* Create a fake union type with a single nameless field of type TYPE.
1008 The field has a tag of "1". This allows us to make the presence
1009 of a field of type TYPE depend on some boolean "desc" being true. */
1010 union_fields = create_field (NULL, type, "");
1011 union_fields->opt =
1012 create_string_option (union_fields->opt, "dot", "");
1013 union_fields->opt =
1014 create_string_option (union_fields->opt, "tag", "1");
1015 union_type =
1016 new_structure (xasprintf ("%s_%d", "fake_union", id++), TYPE_UNION,
1017 &lexer_line, union_fields, NULL, NULL);
1019 /* Create the field and give it the new fake union type. Add a "desc"
1020 tag that specifies the condition under which the field is valid. */
1021 return create_field_all (next, union_type, name,
1022 create_string_option (0, "desc", cond),
1023 this_file, line);
1026 #define create_optional_field(next,type,name,cond) \
1027 create_optional_field_(next,type,name,cond,__LINE__)
1029 /* Reverse a linked list of 'struct pair's in place. */
1030 pair_p
1031 nreverse_pairs (pair_p list)
1033 pair_p prev = 0, p, next;
1034 for (p = list; p; p = next)
1036 next = p->next;
1037 p->next = prev;
1038 prev = p;
1040 return prev;
1044 /* We don't care how long a CONST_DOUBLE is. */
1045 #define CONST_DOUBLE_FORMAT "ww"
1046 /* We don't want to see codes that are only for generator files. */
1047 #undef GENERATOR_FILE
1049 enum rtx_code
1051 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1052 #include "rtl.def"
1053 #undef DEF_RTL_EXPR
1054 NUM_RTX_CODE
1057 static const char *const rtx_name[NUM_RTX_CODE] = {
1058 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1059 #include "rtl.def"
1060 #undef DEF_RTL_EXPR
1063 static const char *const rtx_format[NUM_RTX_CODE] = {
1064 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1065 #include "rtl.def"
1066 #undef DEF_RTL_EXPR
1069 static int rtx_next_new[NUM_RTX_CODE];
1071 /* We also need codes and names for insn notes (not register notes).
1072 Note that we do *not* bias the note values here. */
1073 enum insn_note
1075 #define DEF_INSN_NOTE(NAME) NAME,
1076 #include "insn-notes.def"
1077 #undef DEF_INSN_NOTE
1079 NOTE_INSN_MAX
1082 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1083 default field for line number notes. */
1084 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
1085 #define DEF_INSN_NOTE(NAME) #NAME,
1086 #include "insn-notes.def"
1087 #undef DEF_INSN_NOTE
1090 #undef CONST_DOUBLE_FORMAT
1091 #define GENERATOR_FILE
1093 /* Generate the contents of the rtx_next array. This really doesn't belong
1094 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1096 static void
1097 gen_rtx_next (void)
1099 int i;
1100 for (i = 0; i < NUM_RTX_CODE; i++)
1102 int k;
1104 rtx_next_new[i] = -1;
1105 if (strncmp (rtx_format[i], "uu", 2) == 0)
1106 rtx_next_new[i] = 1;
1107 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1108 rtx_next_new[i] = 1;
1109 else
1110 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
1111 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1112 rtx_next_new[i] = k;
1116 /* Write out the contents of the rtx_next array. */
1117 static void
1118 write_rtx_next (void)
1120 outf_p f = get_output_file_with_visibility (NULL);
1121 int i;
1122 if (!f)
1123 return;
1125 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
1126 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1127 for (i = 0; i < NUM_RTX_CODE; i++)
1128 if (rtx_next_new[i] == -1)
1129 oprintf (f, " 0,\n");
1130 else
1131 oprintf (f,
1132 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1133 oprintf (f, "};\n");
1136 /* Handle `special("rtx_def")'. This is a special case for field
1137 `fld' of struct rtx_def, which is an array of unions whose values
1138 are based in a complex way on the type of RTL. */
1140 static type_p
1141 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1143 pair_p flds = NULL;
1144 options_p nodot;
1145 int i;
1146 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1147 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1149 if (t->kind != TYPE_UNION)
1151 error_at_line (&lexer_line,
1152 "special `rtx_def' must be applied to a union");
1153 return &string_type;
1156 nodot = create_string_option (NULL, "dot", "");
1158 rtx_tp = create_pointer (find_structure ("rtx_def", TYPE_STRUCT));
1159 rtvec_tp = create_pointer (find_structure ("rtvec_def", TYPE_STRUCT));
1160 tree_tp = create_pointer (find_structure ("tree_node", TYPE_UNION));
1161 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", TYPE_STRUCT));
1162 reg_attrs_tp =
1163 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT));
1164 basic_block_tp =
1165 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT));
1166 constant_tp =
1167 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT));
1168 scalar_tp = &scalar_nonchar; /* rtunion int */
1171 pair_p note_flds = NULL;
1172 int c;
1174 for (c = 0; c <= NOTE_INSN_MAX; c++)
1176 switch (c)
1178 case NOTE_INSN_MAX:
1179 case NOTE_INSN_DELETED_LABEL:
1180 case NOTE_INSN_DELETED_DEBUG_LABEL:
1181 note_flds = create_field (note_flds, &string_type, "rt_str");
1182 break;
1184 case NOTE_INSN_BLOCK_BEG:
1185 case NOTE_INSN_BLOCK_END:
1186 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1187 break;
1189 case NOTE_INSN_VAR_LOCATION:
1190 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1191 break;
1193 default:
1194 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1195 break;
1197 /* NOTE_INSN_MAX is used as the default field for line
1198 number notes. */
1199 if (c == NOTE_INSN_MAX)
1200 note_flds->opt =
1201 create_string_option (nodot, "default", "");
1202 else
1203 note_flds->opt =
1204 create_string_option (nodot, "tag", note_insn_name[c]);
1206 note_union_tp = new_structure ("rtx_def_note_subunion", TYPE_UNION,
1207 &lexer_line, note_flds, NULL, NULL);
1209 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1211 pair_p sym_flds;
1212 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1213 sym_flds->opt = create_string_option (nodot, "default", "");
1214 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1215 sym_flds->opt = create_string_option (nodot, "tag", "1");
1216 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", TYPE_UNION,
1217 &lexer_line, sym_flds, NULL, NULL);
1219 for (i = 0; i < NUM_RTX_CODE; i++)
1221 pair_p subfields = NULL;
1222 size_t aindex, nmindex;
1223 const char *sname;
1224 type_p substruct;
1225 char *ftag;
1227 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1229 type_p t;
1230 const char *subname;
1232 switch (rtx_format[i][aindex])
1234 case '*':
1235 case 'i':
1236 case 'n':
1237 case 'w':
1238 case 'r':
1239 t = scalar_tp;
1240 subname = "rt_int";
1241 break;
1243 case 'p':
1244 t = scalar_tp;
1245 subname = "rt_subreg";
1246 break;
1248 case '0':
1249 if (i == MEM && aindex == 1)
1250 t = mem_attrs_tp, subname = "rt_mem";
1251 else if (i == JUMP_INSN && aindex == 7)
1252 t = rtx_tp, subname = "rt_rtx";
1253 else if (i == CODE_LABEL && aindex == 4)
1254 t = scalar_tp, subname = "rt_int";
1255 else if (i == CODE_LABEL && aindex == 3)
1256 t = rtx_tp, subname = "rt_rtx";
1257 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1258 t = rtx_tp, subname = "rt_rtx";
1259 else if (i == NOTE && aindex == 3)
1260 t = note_union_tp, subname = "";
1261 else if (i == NOTE && aindex == 4)
1262 t = scalar_tp, subname = "rt_int";
1263 else if (i == NOTE && aindex >= 6)
1264 t = scalar_tp, subname = "rt_int";
1265 else if (i == ADDR_DIFF_VEC && aindex == 4)
1266 t = scalar_tp, subname = "rt_int";
1267 else if (i == VALUE && aindex == 0)
1268 t = scalar_tp, subname = "rt_int";
1269 else if (i == DEBUG_EXPR && aindex == 0)
1270 t = tree_tp, subname = "rt_tree";
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 == REG)
1346 subfields = create_field (subfields, reg_attrs_tp, "reg.attrs");
1348 if (i == SYMBOL_REF)
1350 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1351 holds. */
1352 type_p field_tp = find_structure ("block_symbol", TYPE_STRUCT);
1353 subfields
1354 = create_optional_field (subfields, field_tp, "block_sym",
1355 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1358 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1359 substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
1360 NULL, NULL);
1362 ftag = xstrdup (rtx_name[i]);
1363 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1364 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1365 flds = create_field (flds, substruct, "");
1366 flds->opt = create_string_option (nodot, "tag", ftag);
1368 return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
1369 nodot, NULL);
1372 /* Handle `special("tree_exp")'. This is a special case for
1373 field `operands' of struct tree_exp, which although it claims to contain
1374 pointers to trees, actually sometimes contains pointers to RTL too.
1375 Passed T, the old type of the field, and OPT its options. Returns
1376 a new type for the field. */
1378 static type_p
1379 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1381 pair_p flds;
1382 options_p nodot;
1384 if (t->kind != TYPE_ARRAY)
1386 error_at_line (&lexer_line,
1387 "special `tree_exp' must be applied to an array");
1388 return &string_type;
1391 nodot = create_string_option (NULL, "dot", "");
1393 flds = create_field (NULL, t, "");
1394 flds->opt = create_string_option (nodot, "length",
1395 "TREE_OPERAND_LENGTH ((tree) &%0)");
1396 flds->opt = create_string_option (flds->opt, "default", "");
1398 return new_structure ("tree_exp_subunion", TYPE_UNION, &lexer_line, flds,
1399 nodot, NULL);
1402 /* Perform any special processing on a type T, about to become the type
1403 of a field. Return the appropriate type for the field.
1404 At present:
1405 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1406 - Similarly for arrays of pointer-to-char;
1407 - Converts structures for which a parameter is provided to
1408 TYPE_PARAM_STRUCT;
1409 - Handles "special" options.
1412 type_p
1413 adjust_field_type (type_p t, options_p opt)
1415 int length_p = 0;
1416 const int pointer_p = t->kind == TYPE_POINTER;
1418 for (; opt; opt = opt->next)
1419 if (strcmp (opt->name, "length") == 0)
1421 if (length_p)
1422 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1423 if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
1425 error_at_line (&lexer_line,
1426 "option `%s' may not be applied to "
1427 "arrays of atomic types", opt->name);
1429 length_p = 1;
1431 else if (strcmp (opt->name, "special") == 0
1432 && opt->kind == OPTION_STRING)
1434 const char *special_name = opt->info.string;
1435 if (strcmp (special_name, "tree_exp") == 0)
1436 t = adjust_field_tree_exp (t, opt);
1437 else if (strcmp (special_name, "rtx_def") == 0)
1438 t = adjust_field_rtx_def (t, opt);
1439 else
1440 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1443 if (!length_p
1444 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1445 return &string_type;
1446 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1447 && t->u.a.p->u.p->kind == TYPE_SCALAR
1448 && t->u.a.p->u.p->u.scalar_is_char)
1449 return create_array (&string_type, t->u.a.len);
1451 return t;
1455 static void set_gc_used_type (type_p, enum gc_used_enum, bool = false);
1456 static void set_gc_used (pair_p);
1458 /* Handle OPT for set_gc_used_type. */
1460 static void
1461 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1462 int *length, int *skip, type_p *nested_ptr)
1464 options_p o;
1465 for (o = opt; o; o = o->next)
1466 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1467 && o->kind == OPTION_TYPE)
1468 set_gc_used_type (o->info.type,
1469 GC_POINTED_TO);
1470 else if (strcmp (o->name, "maybe_undef") == 0)
1471 *maybe_undef = 1;
1472 else if (strcmp (o->name, "length") == 0)
1473 *length = 1;
1474 else if (strcmp (o->name, "skip") == 0)
1475 *skip = 1;
1476 else if (strcmp (o->name, "nested_ptr") == 0
1477 && o->kind == OPTION_NESTED)
1478 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1482 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1484 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1485 are set to GC_UNUSED. Otherwise, an error is emitted for
1486 TYPE_UNDEFINED types. This is used to support user-defined
1487 template types with non-type arguments.
1489 For instance, when we parse a template type with enum arguments
1490 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1491 artificial fields for 'MyType', one for 'AnotherType', the other
1492 one for 'EnumValue'.
1494 At the time that we parse this type we don't know that 'EnumValue'
1495 is really an enum value, so the parser creates a TYPE_UNDEFINED
1496 type for it. Since 'EnumValue' is never resolved to a known
1497 structure, it will stay with TYPE_UNDEFINED.
1499 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1500 'EnumValue'. Generating marking code for it would cause
1501 compilation failures since the marking routines assumes that
1502 'EnumValue' is a type. */
1504 static void
1505 set_gc_used_type (type_p t, enum gc_used_enum level,
1506 bool allow_undefined_types)
1508 if (t->gc_used >= level)
1509 return;
1511 t->gc_used = level;
1513 switch (t->kind)
1515 case TYPE_STRUCT:
1516 case TYPE_UNION:
1517 case TYPE_USER_STRUCT:
1519 pair_p f;
1520 int dummy;
1521 type_p dummy2;
1522 bool allow_undefined_field_types = (t->kind == TYPE_USER_STRUCT);
1524 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy,
1525 &dummy2);
1527 if (t->u.s.base_class)
1528 set_gc_used_type (t->u.s.base_class, level, allow_undefined_types);
1529 /* Anything pointing to a base class might actually be pointing
1530 to a subclass. */
1531 for (type_p subclass = t->u.s.first_subclass; subclass;
1532 subclass = subclass->u.s.next_sibling_class)
1533 set_gc_used_type (subclass, level, allow_undefined_types);
1535 FOR_ALL_INHERITED_FIELDS(t, f)
1537 int maybe_undef = 0;
1538 int length = 0;
1539 int skip = 0;
1540 type_p nested_ptr = NULL;
1541 process_gc_options (f->opt, level, &maybe_undef, &length, &skip,
1542 &nested_ptr);
1544 if (nested_ptr && f->type->kind == TYPE_POINTER)
1545 set_gc_used_type (nested_ptr, GC_POINTED_TO);
1546 else if (length && f->type->kind == TYPE_POINTER)
1547 set_gc_used_type (f->type->u.p, GC_USED);
1548 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1549 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO);
1550 else if (skip)
1551 ; /* target type is not used through this field */
1552 else
1553 set_gc_used_type (f->type, GC_USED, allow_undefined_field_types);
1555 break;
1558 case TYPE_UNDEFINED:
1559 if (level > GC_UNUSED)
1561 if (!allow_undefined_types)
1562 error_at_line (&t->u.s.line, "undefined type `%s'", t->u.s.tag);
1563 t->gc_used = GC_UNUSED;
1565 break;
1567 case TYPE_POINTER:
1568 set_gc_used_type (t->u.p, GC_POINTED_TO);
1569 break;
1571 case TYPE_ARRAY:
1572 set_gc_used_type (t->u.a.p, GC_USED);
1573 break;
1575 case TYPE_LANG_STRUCT:
1576 for (t = t->u.s.lang_struct; t; t = t->next)
1577 set_gc_used_type (t, level);
1578 break;
1580 default:
1581 break;
1585 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1587 static void
1588 set_gc_used (pair_p variables)
1590 int nbvars = 0;
1591 pair_p p;
1592 for (p = variables; p; p = p->next)
1594 set_gc_used_type (p->type, GC_USED);
1595 nbvars++;
1597 if (verbosity_level >= 2)
1598 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1601 /* File mapping routines. For each input file, there is one output .c file
1602 (but some output files have many input files), and there is one .h file
1603 for the whole build. */
1605 /* Output file handling. */
1607 /* Create and return an outf_p for a new file for NAME, to be called
1608 ONAME. */
1610 static outf_p
1611 create_file (const char *name, const char *oname)
1613 static const char *const hdr[] = {
1614 " Copyright (C) 2004-2020 Free Software Foundation, Inc.\n",
1615 "\n",
1616 "This file is part of GCC.\n",
1617 "\n",
1618 "GCC is free software; you can redistribute it and/or modify it under\n",
1619 "the terms of the GNU General Public License as published by the Free\n",
1620 "Software Foundation; either version 3, or (at your option) any later\n",
1621 "version.\n",
1622 "\n",
1623 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1624 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1625 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1626 "for more details.\n",
1627 "\n",
1628 "You should have received a copy of the GNU General Public License\n",
1629 "along with GCC; see the file COPYING3. If not see\n",
1630 "<http://www.gnu.org/licenses/>. */\n",
1631 "\n",
1632 "/* This file is machine generated. Do not edit. */\n"
1634 outf_p f;
1635 size_t i;
1637 gcc_assert (name != NULL);
1638 gcc_assert (oname != NULL);
1639 f = XCNEW (struct outf);
1640 f->next = output_files;
1641 f->name = oname;
1642 output_files = f;
1644 oprintf (f, "/* Type information for %s.\n", name);
1645 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1646 oprintf (f, "%s", hdr[i]);
1647 return f;
1650 /* Print, like fprintf, to O.
1651 N.B. You might think this could be implemented more efficiently
1652 with vsnprintf(). Unfortunately, there are C libraries that
1653 provide that function but without the C99 semantics for its return
1654 value, making it impossible to know how much space is required. */
1655 void
1656 oprintf (outf_p o, const char *format, ...)
1658 char *s;
1659 size_t slength;
1660 va_list ap;
1662 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1663 in that case. */
1664 if (!o)
1665 return;
1667 va_start (ap, format);
1668 slength = vasprintf (&s, format, ap);
1669 if (s == NULL || (int) slength < 0)
1670 fatal ("out of memory");
1671 va_end (ap);
1673 if (o->bufused + slength > o->buflength)
1675 size_t new_len = o->buflength;
1676 if (new_len == 0)
1677 new_len = 1024;
1680 new_len *= 2;
1682 while (o->bufused + slength >= new_len);
1683 o->buf = XRESIZEVEC (char, o->buf, new_len);
1684 o->buflength = new_len;
1686 memcpy (o->buf + o->bufused, s, slength);
1687 o->bufused += slength;
1688 free (s);
1691 /* Open the global header file and the language-specific header files. */
1693 static void
1694 open_base_files (void)
1696 size_t i;
1698 if (nb_plugin_files > 0 && plugin_files)
1699 return;
1701 header_file = create_file ("GCC", "gtype-desc.h");
1703 base_files = XNEWVEC (outf_p, num_lang_dirs);
1705 for (i = 0; i < num_lang_dirs; i++)
1706 base_files[i] = create_file (lang_dir_names[i],
1707 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1709 /* gtype-desc.c is a little special, so we create it here. */
1711 /* The order of files here matters very much. */
1712 static const char *const ifiles[] = {
1713 "config.h", "system.h", "coretypes.h",
1714 "backend.h", "predict.h", "tree.h",
1715 "rtl.h", "gimple.h", "fold-const.h", "insn-codes.h", "splay-tree.h",
1716 "alias.h", "insn-config.h", "flags.h", "expmed.h", "dojump.h",
1717 "explow.h", "calls.h", "memmodel.h", "emit-rtl.h", "varasm.h",
1718 "stmt.h", "expr.h", "alloc-pool.h", "cselib.h", "insn-addr.h",
1719 "optabs.h", "libfuncs.h", "debug.h", "internal-fn.h", "gimple-fold.h",
1720 "value-range.h",
1721 "tree-eh.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
1722 "tree-vrp.h", "tree-phinodes.h", "ssa-iterators.h", "stringpool.h",
1723 "tree-ssanames.h", "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h",
1724 "tree-ssa-loop-manip.h", "tree-ssa-loop-niter.h", "tree-into-ssa.h",
1725 "tree-dfa.h", "tree-ssa.h", "reload.h", "cpplib.h", "tree-chrec.h",
1726 "except.h", "output.h", "cfgloop.h", "target.h", "lto-streamer.h",
1727 "target-globals.h", "ipa-ref.h", "cgraph.h", "symbol-summary.h",
1728 "ipa-prop.h", "ipa-fnsummary.h", "dwarf2out.h", "omp-general.h",
1729 "omp-offload.h", "ipa-modref-tree.h", "ipa-modref.h", "symtab-thunks.h",
1730 NULL
1732 const char *const *ifp;
1733 outf_p gtype_desc_c;
1735 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1736 for (ifp = ifiles; *ifp; ifp++)
1737 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1739 /* Make sure we handle "cfun" specially. */
1740 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1741 oprintf (gtype_desc_c, "#undef cfun\n");
1743 oprintf (gtype_desc_c,
1744 "\n"
1745 "/* Types with a \"gcc::\" namespace have it stripped\n"
1746 " during gengtype parsing. Provide a \"using\" directive\n"
1747 " to ensure that the fully-qualified types are found. */\n"
1748 "using namespace gcc;\n");
1752 /* For INPF an input file, return the real basename of INPF, with all
1753 the directory components skipped. */
1755 static const char *
1756 get_file_realbasename (const input_file *inpf)
1758 return lbasename (get_input_file_name (inpf));
1761 /* For INPF a filename, return the relative path to INPF from
1762 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1764 const char *
1765 get_file_srcdir_relative_path (const input_file *inpf)
1767 const char *f = get_input_file_name (inpf);
1768 if (strlen (f) > srcdir_len
1769 && IS_DIR_SEPARATOR (f[srcdir_len])
1770 && strncmp (f, srcdir, srcdir_len) == 0)
1771 return f + srcdir_len + 1;
1772 else
1773 return NULL;
1776 /* For INPF an input_file, return the relative path to INPF from
1777 $(srcdir) if the latter is a prefix in INPF, or the real basename
1778 of INPF otherwise. */
1780 static const char *
1781 get_file_basename (const input_file *inpf)
1783 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1785 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1788 /* For F a filename, return the lang_dir_names relative index of the language
1789 directory that is a prefix in F, if any, -1 otherwise. */
1791 static int
1792 get_prefix_langdir_index (const char *f)
1794 size_t f_len = strlen (f);
1795 size_t lang_index;
1797 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1799 const char *langdir = lang_dir_names[lang_index];
1800 size_t langdir_len = strlen (langdir);
1802 if (f_len > langdir_len
1803 && IS_DIR_SEPARATOR (f[langdir_len])
1804 && memcmp (f, langdir, langdir_len) == 0)
1805 return lang_index;
1808 return -1;
1811 /* For INPF an input file, return the name of language directory where
1812 F is located, if any, NULL otherwise. */
1814 static const char *
1815 get_file_langdir (const input_file *inpf)
1817 /* Get the relative path to INPF from $(srcdir) and find the
1818 language by comparing the prefix with language directory names.
1819 If INPF is not even srcdir relative, no point in looking
1820 further. */
1822 int lang_index;
1823 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1824 const char *r;
1826 if (!srcdir_relative_path)
1827 return NULL;
1829 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1830 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1831 r = "c-family";
1832 else if (lang_index >= 0)
1833 r = lang_dir_names[lang_index];
1834 else
1835 r = NULL;
1837 return r;
1840 /* The gt- output file name for INPF. */
1842 static const char *
1843 get_file_gtfilename (const input_file *inpf)
1845 /* Cook up an initial version of the gt- file name from the file real
1846 basename and the language name, if any. */
1848 const char *basename = get_file_realbasename (inpf);
1849 const char *langdir = get_file_langdir (inpf);
1851 char *result =
1852 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1853 : xasprintf ("gt-%s", basename));
1855 /* Then replace all non alphanumerics characters by '-' and change the
1856 extension to ".h". We expect the input filename extension was at least
1857 one character long. */
1859 char *s = result;
1861 for (; *s != '.'; s++)
1862 if (!ISALNUM (*s) && *s != '-')
1863 *s = '-';
1865 memcpy (s, ".h", sizeof (".h"));
1867 return result;
1870 /* Each input_file has its associated output file outf_p. The
1871 association is computed by the function
1872 get_output_file_with_visibility. The associated file is cached
1873 inside input_file in its inpoutf field, so is really computed only
1874 once. Associated output file paths (i.e. output_name-s) are
1875 computed by a rule based regexp machinery, using the files_rules
1876 array of struct file_rule_st. A for_name is also computed, giving
1877 the source file name for which the output_file is generated; it is
1878 often the last component of the input_file path. */
1882 Regexpr machinery to compute the output_name and for_name-s of each
1883 input_file. We have a sequence of file rules which gives the POSIX
1884 extended regular expression to match an input file path, and two
1885 transformed strings for the corresponding output_name and the
1886 corresponding for_name. The transformed string contain dollars: $0
1887 is replaced by the entire match, $1 is replaced by the substring
1888 matching the first parenthesis in the regexp, etc. And $$ is replaced
1889 by a single verbatim dollar. The rule order is important. The
1890 general case is last, and the particular cases should come before.
1891 An action routine can, when needed, update the out_name & for_name
1892 and/or return the appropriate output file. It is invoked only when a
1893 rule is triggered. When a rule is triggered, the output_name and
1894 for_name are computed using their transform string in while $$, $0,
1895 $1, ... are suitably replaced. If there is an action, it is called.
1896 In some few cases, the action can directly return the outf_p, but
1897 usually it just updates the output_name and for_name so should free
1898 them before replacing them. The get_output_file_with_visibility
1899 function creates an outf_p only once per each output_name, so it
1900 scans the output_files list for previously seen output file names.
1903 /* Signature of actions in file rules. */
1904 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
1907 struct file_rule_st {
1908 const char* frul_srcexpr; /* Source string for regexp. */
1909 int frul_rflags; /* Flags passed to regcomp, usually
1910 * REG_EXTENDED. */
1911 regex_t* frul_re; /* Compiled regular expression
1912 obtained by regcomp. */
1913 const char* frul_tr_out; /* Transformation string for making
1914 * the output_name, with $1 ... $9 for
1915 * subpatterns and $0 for the whole
1916 * matched filename. */
1917 const char* frul_tr_for; /* Tranformation string for making the
1918 for_name. */
1919 frul_actionrout_t* frul_action; /* The action, if non null, is
1920 * called once the rule matches, on
1921 * the transformed out_name &
1922 * for_name. It could change them
1923 * and/or give the output file. */
1926 /* File rule action handling *.h files. */
1927 static outf_p header_dot_h_frul (input_file*, char**, char**);
1929 /* File rule action handling *.c files. */
1930 static outf_p source_dot_c_frul (input_file*, char**, char**);
1932 #define NULL_REGEX (regex_t*)0
1934 /* The prefix in our regexp-s matching the directory. */
1935 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1937 #define NULL_FRULACT (frul_actionrout_t*)0
1939 /* The array of our rules governing file name generation. Rules order
1940 matters, so change with extreme care! */
1942 struct file_rule_st files_rules[] = {
1943 /* The general rule assumes that files in subdirectories belong to a
1944 particular front-end, and files not in subdirectories are shared.
1945 The following rules deal with exceptions - files that are in
1946 subdirectories and yet are shared, and files that are top-level,
1947 but are not shared. */
1949 /* the c-family/ source directory is special. */
1950 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
1951 REG_EXTENDED, NULL_REGEX,
1952 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
1954 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
1955 REG_EXTENDED, NULL_REGEX,
1956 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
1958 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
1959 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
1960 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1962 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
1963 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1965 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
1966 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
1967 REG_EXTENDED, NULL_REGEX,
1968 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
1970 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
1971 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
1972 REG_EXTENDED, NULL_REGEX,
1973 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
1975 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
1976 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
1977 REG_EXTENDED, NULL_REGEX,
1978 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
1980 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
1981 { DIR_PREFIX_REGEX "cp/parser\\.h$",
1982 REG_EXTENDED, NULL_REGEX,
1983 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
1985 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
1986 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
1987 REG_EXTENDED, NULL_REGEX,
1988 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
1990 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
1991 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
1992 REG_EXTENDED, NULL_REGEX,
1993 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
1995 /* General cases. For header *.h and source *.c or *.cc files, we
1996 * need special actions to handle the language. */
1998 /* Source *.c files are using get_file_gtfilename to compute their
1999 output_name and get_file_basename to compute their for_name
2000 through the source_dot_c_frul action. */
2001 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
2002 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
2004 /* Source *.cc files are using get_file_gtfilename to compute their
2005 output_name and get_file_basename to compute their for_name
2006 through the source_dot_c_frul action. */
2007 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.cc$",
2008 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.cc", source_dot_c_frul},
2010 /* Common header files get "gtype-desc.c" as their output_name,
2011 * while language specific header files are handled specially. So
2012 * we need the header_dot_h_frul action. */
2013 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
2014 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
2016 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
2017 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
2019 /* Mandatory null last entry signaling end of rules. */
2020 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
2023 /* Special file rules action for handling *.h header files. It gives
2024 "gtype-desc.c" for common headers and corresponding output
2025 files for language-specific header files. */
2026 static outf_p
2027 header_dot_h_frul (input_file* inpf, char**poutname,
2028 char**pforname ATTRIBUTE_UNUSED)
2030 const char *basename = 0;
2031 int lang_index = 0;
2032 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2033 (void*) inpf, get_input_file_name (inpf),
2034 *poutname, *pforname);
2035 basename = get_file_basename (inpf);
2036 lang_index = get_prefix_langdir_index (basename);
2037 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
2039 if (lang_index >= 0)
2041 /* The header is language specific. Given output_name &
2042 for_name remains unchanged. The base_files array gives the
2043 outf_p. */
2044 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2045 (void*) base_files[lang_index],
2046 (base_files[lang_index])->name);
2047 return base_files[lang_index];
2049 else
2051 /* The header is common to all front-end languages. So
2052 output_name is "gtype-desc.c" file. The calling function
2053 get_output_file_with_visibility will find its outf_p. */
2054 free (*poutname);
2055 *poutname = xstrdup ("gtype-desc.c");
2056 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2057 get_input_file_name (inpf));
2058 return NULL;
2063 /* Special file rules action for handling *.c source files using
2064 * get_file_gtfilename to compute their output_name and
2065 * get_file_basename to compute their for_name. The output_name is
2066 * gt-<LANG>-<BASE>.h for language specific source files, and
2067 * gt-<BASE>.h for common source files. */
2068 static outf_p
2069 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
2071 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
2072 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
2073 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2074 (void*) inpf, get_input_file_name (inpf),
2075 *poutname, *pforname);
2076 DBGPRINTF ("newoutname %s", newoutname);
2077 DBGPRINTF ("newbasename %s", newbasename);
2078 free (*poutname);
2079 free (*pforname);
2080 *poutname = newoutname;
2081 *pforname = newbasename;
2082 return NULL;
2085 /* Utility function for get_output_file_with_visibility which returns
2086 * a malloc-ed substituted string using TRS on matching of the FILNAM
2087 * file name, using the PMATCH array. */
2088 static char*
2089 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
2090 const char *trs)
2092 struct obstack str_obstack;
2093 char *str = NULL;
2094 char *rawstr = NULL;
2095 const char *pt = NULL;
2096 DBGPRINTF ("filnam %s", filnam);
2097 obstack_init (&str_obstack);
2098 for (pt = trs; *pt; pt++) {
2099 char c = *pt;
2100 if (c == '$')
2102 if (pt[1] == '$')
2104 /* A double dollar $$ is substituted by a single verbatim
2105 dollar, but who really uses dollar signs in file
2106 paths? */
2107 obstack_1grow (&str_obstack, '$');
2109 else if (ISDIGIT (pt[1]))
2111 /* Handle $0 $1 ... $9 by appropriate substitution. */
2112 int dolnum = pt[1] - '0';
2113 int so = pmatch[dolnum].rm_so;
2114 int eo = pmatch[dolnum].rm_eo;
2115 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2116 if (so>=0 && eo>=so)
2117 obstack_grow (&str_obstack, filnam + so, eo - so);
2119 else
2121 /* This can happen only when files_rules is buggy! */
2122 gcc_unreachable ();
2124 /* Always skip the character after the dollar. */
2125 pt++;
2127 else
2128 obstack_1grow (&str_obstack, c);
2130 obstack_1grow (&str_obstack, '\0');
2131 rawstr = XOBFINISH (&str_obstack, char *);
2132 str = xstrdup (rawstr);
2133 obstack_free (&str_obstack, NULL);
2134 DBGPRINTF ("matched replacement %s", str);
2135 rawstr = NULL;
2136 return str;
2140 /* An output file, suitable for definitions, that can see declarations
2141 made in INPF and is linked into every language that uses INPF.
2142 Since the result is cached inside INPF, that argument cannot be
2143 declared constant, but is "almost" constant. */
2145 outf_p
2146 get_output_file_with_visibility (input_file *inpf)
2148 outf_p r;
2149 char *for_name = NULL;
2150 char *output_name = NULL;
2151 const char* inpfname;
2153 /* This can happen when we need a file with visibility on a
2154 structure that we've never seen. We have to just hope that it's
2155 globally visible. */
2156 if (inpf == NULL)
2157 inpf = system_h_file;
2159 /* The result is cached in INPF, so return it if already known. */
2160 if (inpf->inpoutf)
2161 return inpf->inpoutf;
2163 /* In plugin mode, return NULL unless the input_file is one of the
2164 plugin_files. */
2165 if (plugin_files)
2167 size_t i;
2168 for (i = 0; i < nb_plugin_files; i++)
2169 if (inpf == plugin_files[i])
2171 inpf->inpoutf = plugin_output;
2172 return plugin_output;
2175 return NULL;
2178 inpfname = get_input_file_name (inpf);
2180 /* Try each rule in sequence in files_rules until one is triggered. */
2182 int rulix = 0;
2183 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2184 (void*) inpf, inpfname);
2186 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2188 DBGPRINTF ("rulix#%d srcexpr %s",
2189 rulix, files_rules[rulix].frul_srcexpr);
2191 if (!files_rules[rulix].frul_re)
2193 /* Compile the regexpr lazily. */
2194 int err = 0;
2195 files_rules[rulix].frul_re = XCNEW (regex_t);
2196 err = regcomp (files_rules[rulix].frul_re,
2197 files_rules[rulix].frul_srcexpr,
2198 files_rules[rulix].frul_rflags);
2199 if (err)
2201 /* The regular expression compilation fails only when
2202 file_rules is buggy. */
2203 gcc_unreachable ();
2207 output_name = NULL;
2208 for_name = NULL;
2210 /* Match the regexpr and trigger the rule if matched. */
2212 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2213 $3, ... $9. */
2214 regmatch_t pmatch[10];
2215 memset (pmatch, 0, sizeof (pmatch));
2216 if (!regexec (files_rules[rulix].frul_re,
2217 inpfname, 10, pmatch, 0))
2219 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2220 (void*) inpf, inpfname, rulix,
2221 files_rules[rulix].frul_srcexpr);
2222 for_name =
2223 matching_file_name_substitute (inpfname, pmatch,
2224 files_rules[rulix].frul_tr_for);
2225 DBGPRINTF ("for_name %s", for_name);
2226 output_name =
2227 matching_file_name_substitute (inpfname, pmatch,
2228 files_rules[rulix].frul_tr_out);
2229 DBGPRINTF ("output_name %s", output_name);
2230 if (files_rules[rulix].frul_action)
2232 /* Invoke our action routine. */
2233 outf_p of = NULL;
2234 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2235 rulix, output_name, for_name);
2236 of =
2237 (files_rules[rulix].frul_action) (inpf,
2238 &output_name, &for_name);
2239 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2240 rulix, (void*)of, output_name, for_name);
2241 /* If the action routine returned something, give it back
2242 immediately and cache it in inpf. */
2243 if (of)
2245 inpf->inpoutf = of;
2246 return of;
2249 /* The rule matched, and had no action, or that action did
2250 not return any output file but could have changed the
2251 output_name or for_name. We break out of the loop on the
2252 files_rules. */
2253 break;
2255 else
2257 /* The regexpr did not match. */
2258 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2259 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2260 continue;
2265 if (!output_name || !for_name)
2267 /* This should not be possible, and could only happen if the
2268 files_rules is incomplete or buggy. */
2269 fatal ("failed to compute output name for %s", inpfname);
2272 /* Look through to see if we've ever seen this output filename
2273 before. If found, cache the result in inpf. */
2274 for (r = output_files; r; r = r->next)
2275 if (filename_cmp (r->name, output_name) == 0)
2277 inpf->inpoutf = r;
2278 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2279 output_name, for_name);
2280 return r;
2283 /* If not found, create it, and cache it in inpf. */
2284 r = create_file (for_name, output_name);
2286 gcc_assert (r && r->name);
2287 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2288 output_name, for_name);
2289 inpf->inpoutf = r;
2290 return r;
2295 /* The name of an output file, suitable for definitions, that can see
2296 declarations made in INPF and is linked into every language that
2297 uses INPF. */
2299 const char *
2300 get_output_file_name (input_file* inpf)
2302 outf_p o = get_output_file_with_visibility (inpf);
2303 if (o)
2304 return o->name;
2305 return NULL;
2308 /* Check if existing file is equal to the in memory buffer. */
2310 static bool
2311 is_file_equal (outf_p of)
2313 FILE *newfile = fopen (of->name, "r");
2314 size_t i;
2315 bool equal;
2316 if (newfile == NULL)
2317 return false;
2319 equal = true;
2320 for (i = 0; i < of->bufused; i++)
2322 int ch;
2323 ch = fgetc (newfile);
2324 if (ch == EOF || ch != (unsigned char) of->buf[i])
2326 equal = false;
2327 break;
2330 if (equal && EOF != fgetc (newfile))
2331 equal = false;
2332 fclose (newfile);
2333 return equal;
2336 /* Copy the output to its final destination,
2337 but don't unnecessarily change modification times. */
2339 static void
2340 close_output_files (void)
2342 int nbwrittenfiles = 0;
2343 outf_p of;
2345 for (of = output_files; of; of = of->next)
2347 if (!is_file_equal (of))
2349 FILE *newfile = NULL;
2350 char *backupname = NULL;
2351 /* Back up the old version of the output file gt-FOO.c as
2352 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2353 if (backup_dir)
2355 backupname = concat (backup_dir, "/",
2356 lbasename (of->name), "~", NULL);
2357 if (!access (of->name, F_OK) && rename (of->name, backupname))
2358 fatal ("failed to back up %s as %s: %s",
2359 of->name, backupname, xstrerror (errno));
2362 newfile = fopen (of->name, "w");
2363 if (newfile == NULL)
2364 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2365 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2366 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2367 if (fclose (newfile) != 0)
2368 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2369 nbwrittenfiles++;
2370 if (verbosity_level >= 2 && backupname)
2371 printf ("%s wrote #%-3d %s backed-up in %s\n",
2372 progname, nbwrittenfiles, of->name, backupname);
2373 else if (verbosity_level >= 1)
2374 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2375 free (backupname);
2377 else
2379 /* output file remains unchanged. */
2380 if (verbosity_level >= 2)
2381 printf ("%s keep %s\n", progname, of->name);
2383 free (of->buf);
2384 of->buf = NULL;
2385 of->bufused = of->buflength = 0;
2387 if (verbosity_level >= 1)
2388 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2391 struct flist
2393 struct flist *next;
2394 int started_p;
2395 const input_file* file;
2396 outf_p f;
2399 struct walk_type_data;
2401 /* For scalars and strings, given the item in 'val'.
2402 For structures, given a pointer to the item in 'val'.
2403 For misc. pointers, given the item in 'val'.
2405 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2406 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2408 /* Parameters for write_types. */
2410 struct write_types_data
2412 const char *prefix;
2413 const char *param_prefix;
2414 const char *subfield_marker_routine;
2415 const char *marker_routine;
2416 const char *reorder_note_routine;
2417 const char *comment;
2418 enum write_types_kinds kind;
2421 static void output_escaped_param (struct walk_type_data *d,
2422 const char *, const char *);
2423 static void output_mangled_typename (outf_p, const_type_p);
2424 static void walk_type (type_p t, struct walk_type_data *d);
2425 static void write_func_for_structure (type_p orig_s, type_p s,
2426 const struct write_types_data *wtd);
2427 static void write_types_process_field
2428 (type_p f, const struct walk_type_data *d);
2429 static void write_types (outf_p output_header,
2430 type_p structures,
2431 const struct write_types_data *wtd);
2432 static void write_types_local_process_field
2433 (type_p f, const struct walk_type_data *d);
2434 static void write_local_func_for_structure (const_type_p orig_s, type_p s);
2435 static void write_local (outf_p output_header,
2436 type_p structures);
2437 static int contains_scalar_p (type_p t);
2438 static void put_mangled_filename (outf_p, const input_file *);
2439 static void finish_root_table (struct flist *flp, const char *pfx,
2440 const char *tname, const char *lastname,
2441 const char *name);
2442 static void write_root (outf_p, pair_p, type_p, const char *, int,
2443 struct fileloc *, bool);
2444 static void write_array (outf_p f, pair_p v,
2445 const struct write_types_data *wtd);
2446 static void write_roots (pair_p, bool);
2448 /* Parameters for walk_type. */
2450 struct walk_type_data
2452 process_field_fn process_field;
2453 const void *cookie;
2454 outf_p of;
2455 options_p opt;
2456 const char *val;
2457 const char *prev_val[4];
2458 int indent;
2459 int counter;
2460 const struct fileloc *line;
2461 lang_bitmap bitmap;
2462 int used_length;
2463 type_p orig_s;
2464 const char *reorder_fn;
2465 bool needs_cast_p;
2466 bool fn_wants_lvalue;
2467 bool in_record_p;
2468 int loopcounter;
2469 bool in_ptr_field;
2470 bool have_this_obj;
2474 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2475 pre-processor identifier to use in a #define directive. This replaces
2476 special characters used in C++ identifiers like '>', '<' and ':' with
2477 '_'.
2479 If no C++ special characters are found in TYPE_NAME, return
2480 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2481 characters replaced with '_'. In this case, the caller is
2482 responsible for freeing the allocated string. */
2484 static const char *
2485 filter_type_name (const char *type_name)
2487 if (strchr (type_name, '<') || strchr (type_name, ':'))
2489 size_t i;
2490 char *s = xstrdup (type_name);
2491 for (i = 0; i < strlen (s); i++)
2492 if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ','
2493 || s[i] == '*')
2494 s[i] = '_';
2495 return s;
2497 else
2498 return type_name;
2502 /* Print a mangled name representing T to OF. */
2504 static void
2505 output_mangled_typename (outf_p of, const_type_p t)
2507 if (t == NULL)
2508 oprintf (of, "Z");
2509 else
2510 switch (t->kind)
2512 case TYPE_NONE:
2513 case TYPE_UNDEFINED:
2514 gcc_unreachable ();
2515 break;
2516 case TYPE_POINTER:
2517 oprintf (of, "P");
2518 output_mangled_typename (of, t->u.p);
2519 break;
2520 case TYPE_SCALAR:
2521 oprintf (of, "I");
2522 break;
2523 case TYPE_STRING:
2524 oprintf (of, "S");
2525 break;
2526 case TYPE_STRUCT:
2527 case TYPE_UNION:
2528 case TYPE_LANG_STRUCT:
2529 case TYPE_USER_STRUCT:
2531 /* For references to classes within an inheritance hierarchy,
2532 only ever reference the ultimate base class, since only
2533 it will have gt_ functions. */
2534 t = get_ultimate_base_class (t);
2535 const char *id_for_tag = filter_type_name (t->u.s.tag);
2536 oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2537 id_for_tag);
2538 if (id_for_tag != t->u.s.tag)
2539 free (CONST_CAST (char *, id_for_tag));
2541 break;
2542 case TYPE_ARRAY:
2543 gcc_unreachable ();
2547 /* Print PARAM to D->OF processing escapes. D->VAL references the
2548 current object, D->PREV_VAL the object containing the current
2549 object, ONAME is the name of the option and D->LINE is used to
2550 print error messages. */
2552 static void
2553 output_escaped_param (struct walk_type_data *d, const char *param,
2554 const char *oname)
2556 const char *p;
2558 for (p = param; *p; p++)
2559 if (*p != '%')
2560 oprintf (d->of, "%c", *p);
2561 else
2562 switch (*++p)
2564 case 'h':
2565 oprintf (d->of, "(%s)", d->prev_val[2]);
2566 break;
2567 case '0':
2568 oprintf (d->of, "(%s)", d->prev_val[0]);
2569 break;
2570 case '1':
2571 oprintf (d->of, "(%s)", d->prev_val[1]);
2572 break;
2573 case 'a':
2575 const char *pp = d->val + strlen (d->val);
2576 while (pp[-1] == ']')
2577 while (*pp != '[')
2578 pp--;
2579 oprintf (d->of, "%s", pp);
2581 break;
2582 default:
2583 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2584 oname, '%', *p);
2588 const char *
2589 get_string_option (options_p opt, const char *key)
2591 for (; opt; opt = opt->next)
2592 if (strcmp (opt->name, key) == 0)
2593 return opt->info.string;
2594 return NULL;
2597 /* Machinery for avoiding duplicate tags within switch statements. */
2598 struct seen_tag
2600 const char *tag;
2601 struct seen_tag *next;
2605 already_seen_tag (struct seen_tag *seen_tags, const char *tag)
2607 /* Linear search, so O(n^2), but n is currently small. */
2608 while (seen_tags)
2610 if (!strcmp (seen_tags->tag, tag))
2611 return 1;
2612 seen_tags = seen_tags->next;
2614 /* Not yet seen this tag. */
2615 return 0;
2618 void
2619 mark_tag_as_seen (struct seen_tag **seen_tags, const char *tag)
2621 /* Add to front of linked list. */
2622 struct seen_tag *new_node = XCNEW (struct seen_tag);
2623 new_node->tag = tag;
2624 new_node->next = *seen_tags;
2625 *seen_tags = new_node;
2628 static void
2629 walk_subclasses (type_p base, struct walk_type_data *d,
2630 struct seen_tag **seen_tags)
2632 for (type_p sub = base->u.s.first_subclass; sub != NULL;
2633 sub = sub->u.s.next_sibling_class)
2635 const char *type_tag = get_string_option (sub->u.s.opt, "tag");
2636 if (type_tag && !already_seen_tag (*seen_tags, type_tag))
2638 mark_tag_as_seen (seen_tags, type_tag);
2639 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
2640 d->indent += 2;
2641 oprintf (d->of, "%*s{\n", d->indent, "");
2642 d->indent += 2;
2643 oprintf (d->of, "%*s%s *sub = static_cast <%s *> (x);\n",
2644 d->indent, "", sub->u.s.tag, sub->u.s.tag);
2645 const char *old_val = d->val;
2646 d->val = "(*sub)";
2647 walk_type (sub, d);
2648 d->val = old_val;
2649 d->indent -= 2;
2650 oprintf (d->of, "%*s}\n", d->indent, "");
2651 oprintf (d->of, "%*sbreak;\n", d->indent, "");
2652 d->indent -= 2;
2654 walk_subclasses (sub, d, seen_tags);
2658 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2659 which is of type T. Write code to D->OF to constrain execution (at
2660 the point that D->PROCESS_FIELD is called) to the appropriate
2661 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2662 pointers to those objects. D->PREV_VAL lists the objects
2663 containing the current object, D->OPT is a list of options to
2664 apply, D->INDENT is the current indentation level, D->LINE is used
2665 to print error messages, D->BITMAP indicates which languages to
2666 print the structure for. */
2668 static void
2669 walk_type (type_p t, struct walk_type_data *d)
2671 const char *length = NULL;
2672 const char *desc = NULL;
2673 const char *type_tag = NULL;
2674 int maybe_undef_p = 0;
2675 int atomic_p = 0;
2676 options_p oo;
2677 const struct nested_ptr_data *nested_ptr_d = NULL;
2679 d->needs_cast_p = false;
2680 for (oo = d->opt; oo; oo = oo->next)
2681 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2682 length = oo->info.string;
2683 else if (strcmp (oo->name, "maybe_undef") == 0)
2684 maybe_undef_p = 1;
2685 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2686 desc = oo->info.string;
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 int union_p = t->kind == TYPE_UNION;
2927 int seen_default_p = 0;
2928 options_p o;
2929 int lengths_seen = 0;
2930 int endcounter;
2931 bool any_length_seen = false;
2933 if (!t->u.s.line.file)
2934 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
2936 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2938 error_at_line (d->line,
2939 "structure `%s' defined for mismatching languages",
2940 t->u.s.tag);
2941 error_at_line (&t->u.s.line, "one structure defined here");
2944 /* Some things may also be defined in the structure's options. */
2945 for (o = t->u.s.opt; o; o = o->next)
2946 if (!desc && strcmp (o->name, "desc") == 0
2947 && o->kind == OPTION_STRING)
2948 desc = o->info.string;
2950 d->prev_val[2] = oldval;
2951 d->prev_val[1] = oldprevval2;
2952 if (union_p)
2954 if (desc == NULL)
2956 error_at_line (d->line,
2957 "missing `desc' option for union `%s'",
2958 t->u.s.tag);
2959 desc = "1";
2961 oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
2962 output_escaped_param (d, desc, "desc");
2963 oprintf (d->of, "))\n");
2964 d->indent += 2;
2965 oprintf (d->of, "%*s{\n", d->indent, "");
2967 else if (desc)
2969 /* We have a "desc" option on a struct, signifying the
2970 base class within a GC-managed inheritance hierarchy.
2971 The current code specialcases the base class, then walks
2972 into subclasses, recursing into this routine to handle them.
2973 This organization requires the base class to have a case in
2974 the switch statement, and hence a tag value is mandatory
2975 for the base class. This restriction could be removed, but
2976 it would require some restructing of this code. */
2977 if (!type_tag)
2979 error_at_line (d->line,
2980 "missing `tag' option for type `%s'",
2981 t->u.s.tag);
2983 oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
2984 output_escaped_param (d, desc, "desc");
2985 oprintf (d->of, "))\n");
2986 d->indent += 2;
2987 oprintf (d->of, "%*s{\n", d->indent, "");
2988 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
2989 d->indent += 2;
2992 FOR_ALL_INHERITED_FIELDS (t, f)
2994 options_p oo;
2995 int skip_p = 0;
2996 const char *fieldlength = NULL;
2998 d->reorder_fn = NULL;
2999 for (oo = f->opt; oo; oo = oo->next)
3000 if (strcmp (oo->name, "skip") == 0)
3001 skip_p = 1;
3002 else if (strcmp (oo->name, "length") == 0
3003 && oo->kind == OPTION_STRING)
3004 fieldlength = oo->info.string;
3006 if (skip_p)
3007 continue;
3008 if (fieldlength)
3010 lengths_seen++;
3011 d->counter++;
3012 if (!union_p)
3014 if (!any_length_seen)
3016 oprintf (d->of, "%*s{\n", d->indent, "");
3017 d->indent += 2;
3019 any_length_seen = true;
3021 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
3022 d->indent, "", d->counter - 1);
3023 output_escaped_param (d, fieldlength, "length");
3024 oprintf (d->of, ");\n");
3028 endcounter = d->counter;
3030 FOR_ALL_INHERITED_FIELDS (t, f)
3032 options_p oo;
3033 const char *dot = ".";
3034 const char *tagid = NULL;
3035 int skip_p = 0;
3036 int default_p = 0;
3037 const char *fieldlength = NULL;
3038 char *newval;
3040 d->reorder_fn = NULL;
3041 for (oo = f->opt; oo; oo = oo->next)
3042 if (strcmp (oo->name, "dot") == 0
3043 && oo->kind == OPTION_STRING)
3044 dot = oo->info.string;
3045 else if (strcmp (oo->name, "tag") == 0
3046 && oo->kind == OPTION_STRING)
3047 tagid = oo->info.string;
3048 else if (strcmp (oo->name, "skip") == 0)
3049 skip_p = 1;
3050 else if (strcmp (oo->name, "default") == 0)
3051 default_p = 1;
3052 else if (strcmp (oo->name, "reorder") == 0
3053 && oo->kind == OPTION_STRING)
3054 d->reorder_fn = oo->info.string;
3055 else if (strcmp (oo->name, "length") == 0
3056 && oo->kind == OPTION_STRING)
3057 fieldlength = oo->info.string;
3059 if (skip_p)
3060 continue;
3062 if (union_p && tagid)
3064 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
3065 d->indent += 2;
3067 else if (union_p && default_p)
3069 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3070 d->indent += 2;
3071 seen_default_p = 1;
3073 else if (!union_p && (default_p || tagid))
3074 error_at_line (d->line,
3075 "can't use `%s' outside a union on field `%s'",
3076 default_p ? "default" : "tag", f->name);
3077 else if (union_p && !(default_p || tagid)
3078 && f->type->kind == TYPE_SCALAR)
3080 fprintf (stderr,
3081 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3082 get_input_file_name (d->line->file), d->line->line,
3083 f->name);
3084 continue;
3086 else if (union_p && !(default_p || tagid))
3087 error_at_line (d->line,
3088 "field `%s' is missing `tag' or `default' option",
3089 f->name);
3091 if (fieldlength)
3093 d->loopcounter = endcounter - lengths_seen--;
3096 d->line = &f->line;
3097 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
3098 d->opt = f->opt;
3099 d->used_length = false;
3100 d->in_record_p = !union_p;
3102 walk_type (f->type, d);
3104 d->in_record_p = false;
3106 free (newval);
3108 if (union_p)
3110 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3111 d->indent -= 2;
3114 d->reorder_fn = NULL;
3116 d->val = oldval;
3117 d->prev_val[1] = oldprevval1;
3118 d->prev_val[2] = oldprevval2;
3120 if (union_p && !seen_default_p)
3122 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3123 oprintf (d->of, "%*s break;\n", d->indent, "");
3126 if (desc && !union_p)
3128 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3129 d->indent -= 2;
3131 if (union_p)
3133 oprintf (d->of, "%*s}\n", d->indent, "");
3134 d->indent -= 2;
3136 else if (desc)
3138 /* Add cases to handle subclasses. */
3139 struct seen_tag *tags = NULL;
3140 walk_subclasses (t, d, &tags);
3142 /* Ensure that if someone forgets a "tag" option that we don't
3143 silent fail to traverse that subclass's fields. */
3144 if (!seen_default_p)
3146 oprintf (d->of, "%*s/* Unrecognized tag value. */\n",
3147 d->indent, "");
3148 oprintf (d->of, "%*sdefault: gcc_unreachable (); \n",
3149 d->indent, "");
3152 /* End of the switch statement */
3153 oprintf (d->of, "%*s}\n", d->indent, "");
3154 d->indent -= 2;
3156 if (any_length_seen)
3158 d->indent -= 2;
3159 oprintf (d->of, "%*s}\n", d->indent, "");
3162 break;
3164 case TYPE_LANG_STRUCT:
3166 type_p nt;
3167 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3168 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3169 break;
3170 if (nt == NULL)
3171 error_at_line (d->line, "structure `%s' differs between languages",
3172 t->u.s.tag);
3173 else
3174 walk_type (nt, d);
3176 break;
3178 case TYPE_USER_STRUCT:
3179 d->process_field (t, d);
3180 break;
3182 case TYPE_NONE:
3183 case TYPE_UNDEFINED:
3184 gcc_unreachable ();
3188 /* process_field routine for marking routines. */
3190 static void
3191 write_types_process_field (type_p f, const struct walk_type_data *d)
3193 const struct write_types_data *wtd;
3194 const char *cast = d->needs_cast_p ? "(void *)" : "";
3195 wtd = (const struct write_types_data *) d->cookie;
3197 switch (f->kind)
3199 case TYPE_NONE:
3200 case TYPE_UNDEFINED:
3201 gcc_unreachable ();
3202 case TYPE_POINTER:
3203 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
3204 wtd->subfield_marker_routine, cast, d->val);
3205 if (wtd->param_prefix)
3207 if (f->u.p->kind == TYPE_SCALAR)
3208 /* The current type is a pointer to a scalar (so not
3209 considered like a pointer to instances of user defined
3210 types) and we are seeing it; it means we must be even
3211 more careful about the second argument of the
3212 SUBFIELD_MARKER_ROUTINE call. That argument must
3213 always be the instance of the type for which
3214 write_func_for_structure was called - this really is
3215 what the function SUBFIELD_MARKER_ROUTINE expects.
3216 That is, it must be an instance of the ORIG_S type
3217 parameter of write_func_for_structure. The convention
3218 is that that argument must be "x" in that case (as set
3219 by write_func_for_structure). The problem is, we can't
3220 count on d->prev_val[3] to be always set to "x" in that
3221 case. Sometimes walk_type can set it to something else
3222 (to e.g cooperate with write_array when called from
3223 write_roots). So let's set it to "x" here then. */
3224 oprintf (d->of, ", x");
3225 else
3226 oprintf (d->of, ", %s", d->prev_val[3]);
3227 if (d->orig_s)
3229 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3230 output_mangled_typename (d->of, d->orig_s);
3232 else
3233 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3235 oprintf (d->of, ");\n");
3236 if (d->reorder_fn && wtd->reorder_note_routine)
3237 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
3238 wtd->reorder_note_routine, cast, d->val,
3239 d->prev_val[3], d->reorder_fn);
3240 break;
3242 case TYPE_STRING:
3243 case TYPE_STRUCT:
3244 case TYPE_UNION:
3245 case TYPE_LANG_STRUCT:
3246 case TYPE_USER_STRUCT:
3247 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3249 /* If F is a user-defined type and the field is not a
3250 pointer to the type, then we should not generate the
3251 standard pointer-marking code. All we need to do is call
3252 the user-provided marking function to process the fields
3253 of F. */
3254 oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3255 d->val);
3257 else
3259 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3260 output_mangled_typename (d->of, f);
3261 oprintf (d->of, " (%s%s);\n", cast, d->val);
3262 if (d->reorder_fn && wtd->reorder_note_routine)
3263 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
3264 wtd->reorder_note_routine, cast, d->val, cast, d->val,
3265 d->reorder_fn);
3267 break;
3269 case TYPE_SCALAR:
3270 break;
3272 case TYPE_ARRAY:
3273 gcc_unreachable ();
3277 /* Return an output file that is suitable for definitions which can
3278 reference struct S */
3280 static outf_p
3281 get_output_file_for_structure (const_type_p s)
3283 const input_file *fn;
3285 gcc_assert (union_or_struct_p (s));
3286 fn = s->u.s.line.file;
3288 /* The call to get_output_file_with_visibility may update fn by
3289 caching its result inside, so we need the CONST_CAST. */
3290 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3294 /* Returns the specifier keyword for a string or union type S, empty string
3295 otherwise. */
3297 static const char *
3298 get_type_specifier (const type_p s)
3300 if (s->kind == TYPE_STRUCT)
3301 return "struct ";
3302 else if (s->kind == TYPE_LANG_STRUCT)
3303 return get_type_specifier (s->u.s.lang_struct);
3304 else if (s->kind == TYPE_UNION)
3305 return "union ";
3306 return "";
3310 /* Emits a declaration for type TY (assumed to be a union or a
3311 structure) on stream OUT. */
3313 static void
3314 write_type_decl (outf_p out, type_p ty)
3316 if (union_or_struct_p (ty))
3317 oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3318 else if (ty->kind == TYPE_SCALAR)
3320 if (ty->u.scalar_is_char)
3321 oprintf (out, "const char");
3322 else
3323 oprintf (out, "void");
3325 else if (ty->kind == TYPE_POINTER)
3327 write_type_decl (out, ty->u.p);
3328 oprintf (out, " *");
3330 else if (ty->kind == TYPE_ARRAY)
3332 write_type_decl (out, ty->u.a.p);
3333 oprintf (out, " *");
3335 else if (ty->kind == TYPE_STRING)
3337 oprintf (out, "const char *");
3339 else
3340 gcc_unreachable ();
3344 /* Write on OF the name of the marker function for structure S. PREFIX
3345 is the prefix to use (to distinguish ggc from pch markers). */
3347 static void
3348 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3350 if (union_or_struct_p (s))
3352 const char *id_for_tag = filter_type_name (s->u.s.tag);
3353 oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3354 if (id_for_tag != s->u.s.tag)
3355 free (CONST_CAST (char *, id_for_tag));
3357 else
3358 gcc_unreachable ();
3361 /* Write on OF a user-callable routine to act as an entry point for
3362 the marking routine for S, generated by write_func_for_structure.
3363 WTD distinguishes between ggc and pch markers. */
3365 static void
3366 write_user_func_for_structure_ptr (outf_p of, type_p s, const write_types_data *wtd)
3368 gcc_assert (union_or_struct_p (s));
3370 type_p alias_of = NULL;
3371 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3372 if (strcmp (opt->name, "ptr_alias") == 0)
3374 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3375 we do not generate marking code for ORIG_S here. Instead, a
3376 forwarder #define in gtype-desc.h will cause every call to its
3377 marker to call the target of this alias.
3379 However, we still want to create a user entry code for the
3380 aliased type. So, if ALIAS_OF is set, we only generate the
3381 user-callable marker function. */
3382 alias_of = opt->info.type;
3383 break;
3386 DBGPRINTF ("write_user_func_for_structure_ptr: %s %s", s->u.s.tag,
3387 wtd->prefix);
3389 /* Only write the function once. */
3390 if (s->u.s.wrote_user_func_for_ptr[wtd->kind])
3391 return;
3392 s->u.s.wrote_user_func_for_ptr[wtd->kind] = true;
3394 oprintf (of, "\nvoid\n");
3395 oprintf (of, "gt_%sx (", wtd->prefix);
3396 write_type_decl (of, s);
3397 oprintf (of, " *& x)\n");
3398 oprintf (of, "{\n");
3399 oprintf (of, " if (x)\n ");
3400 write_marker_function_name (of,
3401 alias_of ? alias_of : get_ultimate_base_class (s),
3402 wtd->prefix);
3403 oprintf (of, " ((void *) x);\n");
3404 oprintf (of, "}\n");
3408 /* Write a function to mark all the fields of type S on OF. PREFIX
3409 and D are as in write_user_marking_functions. */
3411 static void
3412 write_user_func_for_structure_body (type_p s, const char *prefix,
3413 struct walk_type_data *d)
3415 oprintf (d->of, "\nvoid\n");
3416 oprintf (d->of, "gt_%sx (", prefix);
3417 write_type_decl (d->of, s);
3418 oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3419 oprintf (d->of, "{\n");
3420 oprintf (d->of, " ");
3421 write_type_decl (d->of, s);
3422 oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3423 d->val = "(*x)";
3424 d->indent = 2;
3425 walk_type (s, d);
3426 oprintf (d->of, "}\n");
3429 /* Emit the user-callable functions needed to mark all the types used
3430 by the user structure S. PREFIX is the prefix to use to
3431 distinguish ggc and pch markers. D contains data needed to pass to
3432 walk_type when traversing the fields of a type.
3434 For every type T referenced by S, two routines are generated: one
3435 that takes 'T *', marks the pointer and calls the second routine,
3436 which just marks the fields of T. */
3438 static void
3439 write_user_marking_functions (type_p s,
3440 const write_types_data *w,
3441 struct walk_type_data *d)
3443 gcc_assert (s->kind == TYPE_USER_STRUCT);
3445 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3447 type_p fld_type = fld->type;
3448 if (fld_type->kind == TYPE_POINTER)
3450 type_p pointed_to_type = fld_type->u.p;
3451 if (union_or_struct_p (pointed_to_type))
3452 write_user_func_for_structure_ptr (d->of, pointed_to_type, w);
3454 else if (union_or_struct_p (fld_type))
3455 write_user_func_for_structure_body (fld_type, w->prefix, d);
3460 /* For S, a structure that's part of ORIG_S write out a routine that:
3461 - Takes a parameter, a void * but actually of type *S
3462 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3463 field of S or its substructures and (in some cases) things
3464 that are pointed to by S. */
3466 static void
3467 write_func_for_structure (type_p orig_s, type_p s,
3468 const struct write_types_data *wtd)
3470 const char *chain_next = NULL;
3471 const char *chain_prev = NULL;
3472 const char *chain_circular = NULL;
3473 options_p opt;
3474 struct walk_type_data d;
3476 if (s->u.s.base_class)
3478 /* Verify that the base class has a "desc", since otherwise
3479 the traversal hooks there won't attempt to visit fields of
3480 subclasses such as this one. */
3481 const_type_p ubc = get_ultimate_base_class (s);
3482 if ((!opts_have (ubc->u.s.opt, "user")
3483 && !opts_have (ubc->u.s.opt, "desc")))
3484 error_at_line (&s->u.s.line,
3485 ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
3486 ", but '%s' lacks a discriminator 'desc' option"),
3487 s->u.s.tag, ubc->u.s.tag, ubc->u.s.tag);
3489 /* Don't write fns for subclasses, only for the ultimate base class
3490 within an inheritance hierarchy. */
3491 return;
3494 memset (&d, 0, sizeof (d));
3495 d.of = get_output_file_for_structure (s);
3497 bool for_user = false;
3498 for (opt = s->u.s.opt; opt; opt = opt->next)
3499 if (strcmp (opt->name, "chain_next") == 0
3500 && opt->kind == OPTION_STRING)
3501 chain_next = opt->info.string;
3502 else if (strcmp (opt->name, "chain_prev") == 0
3503 && opt->kind == OPTION_STRING)
3504 chain_prev = opt->info.string;
3505 else if (strcmp (opt->name, "chain_circular") == 0
3506 && opt->kind == OPTION_STRING)
3507 chain_circular = opt->info.string;
3508 else if (strcmp (opt->name, "for_user") == 0)
3509 for_user = true;
3510 if (chain_prev != NULL && chain_next == NULL)
3511 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3512 if (chain_circular != NULL && chain_next != NULL)
3513 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3514 if (chain_circular != NULL)
3515 chain_next = chain_circular;
3517 d.process_field = write_types_process_field;
3518 d.cookie = wtd;
3519 d.orig_s = orig_s;
3520 d.opt = s->u.s.opt;
3521 d.line = &s->u.s.line;
3522 d.bitmap = s->u.s.bitmap;
3523 d.prev_val[0] = "*x";
3524 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3525 d.prev_val[3] = "x";
3526 d.val = "(*x)";
3527 d.have_this_obj = false;
3529 oprintf (d.of, "\n");
3530 oprintf (d.of, "void\n");
3531 write_marker_function_name (d.of, orig_s, wtd->prefix);
3532 oprintf (d.of, " (void *x_p)\n");
3533 oprintf (d.of, "{\n ");
3534 write_type_decl (d.of, s);
3535 oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3536 write_type_decl (d.of, s);
3537 oprintf (d.of, " *)x_p;\n");
3538 if (chain_next != NULL)
3540 /* TYPE_USER_STRUCTs should not occur here. These structures
3541 are completely handled by user code. */
3542 gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
3544 oprintf (d.of, " ");
3545 write_type_decl (d.of, s);
3546 oprintf (d.of, " * xlimit = x;\n");
3548 if (chain_next == NULL)
3550 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3551 if (wtd->param_prefix)
3553 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3554 output_mangled_typename (d.of, orig_s);
3556 oprintf (d.of, "))\n");
3558 else
3560 if (chain_circular != NULL)
3561 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3562 else
3563 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3564 if (wtd->param_prefix)
3566 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3567 output_mangled_typename (d.of, orig_s);
3569 oprintf (d.of, "))\n");
3570 if (chain_circular != NULL)
3571 oprintf (d.of, " return;\n do\n");
3573 oprintf (d.of, " xlimit = (");
3574 d.prev_val[2] = "*xlimit";
3575 output_escaped_param (&d, chain_next, "chain_next");
3576 oprintf (d.of, ");\n");
3577 if (chain_prev != NULL)
3579 oprintf (d.of, " if (x != xlimit)\n");
3580 oprintf (d.of, " for (;;)\n");
3581 oprintf (d.of, " {\n");
3582 oprintf (d.of, " %s %s * const xprev = (",
3583 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3585 d.prev_val[2] = "*x";
3586 output_escaped_param (&d, chain_prev, "chain_prev");
3587 oprintf (d.of, ");\n");
3588 oprintf (d.of, " if (xprev == NULL) break;\n");
3589 oprintf (d.of, " x = xprev;\n");
3590 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3591 if (wtd->param_prefix)
3593 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3594 output_mangled_typename (d.of, orig_s);
3596 oprintf (d.of, ");\n");
3597 oprintf (d.of, " }\n");
3599 if (chain_circular != NULL)
3601 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3602 if (wtd->param_prefix)
3604 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3605 output_mangled_typename (d.of, orig_s);
3607 oprintf (d.of, "));\n");
3608 oprintf (d.of, " do\n");
3610 else
3611 oprintf (d.of, " while (x != xlimit)\n");
3613 oprintf (d.of, " {\n");
3615 d.prev_val[2] = "*x";
3616 d.indent = 6;
3617 if (orig_s->kind != TYPE_USER_STRUCT)
3618 walk_type (s, &d);
3619 else
3621 /* User structures have no fields to walk. Simply generate a call
3622 to the user-provided structure marker. */
3623 oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3626 if (chain_next != NULL)
3628 oprintf (d.of, " x = (");
3629 output_escaped_param (&d, chain_next, "chain_next");
3630 oprintf (d.of, ");\n");
3633 oprintf (d.of, " }\n");
3634 if (chain_circular != NULL)
3635 oprintf (d.of, " while (x != xlimit);\n");
3636 oprintf (d.of, "}\n");
3638 if (orig_s->kind == TYPE_USER_STRUCT)
3639 write_user_marking_functions (orig_s, wtd, &d);
3641 if (for_user)
3643 write_user_func_for_structure_body (orig_s, wtd->prefix, &d);
3644 write_user_func_for_structure_ptr (d.of, orig_s, wtd);
3649 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3651 static void
3652 write_types (outf_p output_header, type_p structures,
3653 const struct write_types_data *wtd)
3655 int nbfun = 0; /* Count the emitted functions. */
3656 type_p s;
3658 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3660 /* We first emit the macros and the declarations. Functions' code is
3661 emitted afterwards. This is needed in plugin mode. */
3662 oprintf (output_header, "/* Macros and declarations. */\n");
3663 for (s = structures; s; s = s->next)
3664 /* Do not emit handlers for derived classes; we only ever deal with
3665 the ultimate base class within an inheritance hierarchy. */
3666 if ((s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3667 && !s->u.s.base_class)
3669 options_p opt;
3671 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3672 continue;
3674 const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3676 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3677 output_mangled_typename (output_header, s);
3678 oprintf (output_header, "(X) do { \\\n");
3679 oprintf (output_header,
3680 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3681 s_id_for_tag);
3682 oprintf (output_header, " } while (0)\n");
3684 for (opt = s->u.s.opt; opt; opt = opt->next)
3685 if (strcmp (opt->name, "ptr_alias") == 0
3686 && opt->kind == OPTION_TYPE)
3688 const_type_p const t = (const_type_p) opt->info.type;
3689 if (t->kind == TYPE_STRUCT
3690 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3692 const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3693 oprintf (output_header,
3694 "#define gt_%sx_%s gt_%sx_%s\n",
3695 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3696 if (t_id_for_tag != t->u.s.tag)
3697 free (CONST_CAST (char *, t_id_for_tag));
3699 else
3700 error_at_line (&s->u.s.line,
3701 "structure alias is not a structure");
3702 break;
3704 if (opt)
3705 continue;
3707 /* Declare the marker procedure only once. */
3708 oprintf (output_header,
3709 "extern void gt_%sx_%s (void *);\n",
3710 wtd->prefix, s_id_for_tag);
3712 if (s_id_for_tag != s->u.s.tag)
3713 free (CONST_CAST (char *, s_id_for_tag));
3715 if (s->u.s.line.file == NULL)
3717 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3718 s->u.s.tag);
3719 continue;
3723 /* At last we emit the functions code. */
3724 oprintf (output_header, "\n/* functions code */\n");
3725 for (s = structures; s; s = s->next)
3726 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3728 options_p opt;
3730 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3731 continue;
3732 for (opt = s->u.s.opt; opt; opt = opt->next)
3733 if (strcmp (opt->name, "ptr_alias") == 0)
3734 break;
3735 if (opt)
3736 continue;
3738 if (s->kind == TYPE_LANG_STRUCT)
3740 type_p ss;
3741 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3743 nbfun++;
3744 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3745 nbfun, (void*) ss, ss->u.s.tag);
3746 write_func_for_structure (s, ss, wtd);
3749 else
3751 nbfun++;
3752 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3753 nbfun, (void*) s, s->u.s.tag);
3754 write_func_for_structure (s, s, wtd);
3757 else
3759 /* Structure s is not possibly pointed to, so can be ignored. */
3760 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3761 (void*)s, s->u.s.tag,
3762 (int) s->gc_used);
3765 if (verbosity_level >= 2)
3766 printf ("%s emitted %d routines for %s\n",
3767 progname, nbfun, wtd->comment);
3770 static const struct write_types_data ggc_wtd = {
3771 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
3772 "GC marker procedures. ",
3773 WTK_GGC
3776 static const struct write_types_data pch_wtd = {
3777 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3778 "gt_pch_note_reorder",
3779 "PCH type-walking procedures. ",
3780 WTK_PCH
3783 /* Write out the local pointer-walking routines. */
3785 /* process_field routine for local pointer-walking for user-callable
3786 routines. The difference between this and
3787 write_types_local_process_field is that, in this case, we do not
3788 need to check whether the given pointer matches the address of the
3789 parent structure. This check was already generated by the call
3790 to gt_pch_nx in the main gt_pch_p_*() function that is calling
3791 this code. */
3793 static void
3794 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
3796 switch (f->kind)
3798 case TYPE_POINTER:
3799 case TYPE_STRUCT:
3800 case TYPE_UNION:
3801 case TYPE_LANG_STRUCT:
3802 case TYPE_STRING:
3803 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3804 break;
3806 case TYPE_USER_STRUCT:
3807 if (d->in_ptr_field)
3808 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3809 else
3810 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3811 d->indent, "", d->val);
3812 break;
3814 case TYPE_SCALAR:
3815 break;
3817 case TYPE_ARRAY:
3818 case TYPE_NONE:
3819 case TYPE_UNDEFINED:
3820 gcc_unreachable ();
3825 /* Write a function to PCH walk all the fields of type S on OF.
3826 D contains data needed by walk_type to recurse into the fields of S. */
3828 static void
3829 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
3831 oprintf (d->of, "\nvoid\n");
3832 oprintf (d->of, "gt_pch_nx (");
3833 write_type_decl (d->of, s);
3834 oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
3835 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3836 "\tATTRIBUTE_UNUSED void *cookie)\n");
3837 oprintf (d->of, "{\n");
3838 d->val = "(*x)";
3839 d->indent = 2;
3840 d->process_field = write_types_local_user_process_field;
3841 walk_type (s, d);
3842 oprintf (d->of, "}\n");
3846 /* Emit the user-callable functions needed to mark all the types used
3847 by the user structure S. PREFIX is the prefix to use to
3848 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
3849 chain_next option defined. D contains data needed to pass to
3850 walk_type when traversing the fields of a type.
3852 For every type T referenced by S, two routines are generated: one
3853 that takes 'T *', marks the pointer and calls the second routine,
3854 which just marks the fields of T. */
3856 static void
3857 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
3859 gcc_assert (s->kind == TYPE_USER_STRUCT);
3861 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3863 type_p fld_type = fld->type;
3864 if (union_or_struct_p (fld_type))
3865 write_pch_user_walking_for_structure_body (fld_type, d);
3870 /* process_field routine for local pointer-walking. */
3872 static void
3873 write_types_local_process_field (type_p f, const struct walk_type_data *d)
3875 gcc_assert (d->have_this_obj);
3876 switch (f->kind)
3878 case TYPE_POINTER:
3879 case TYPE_STRUCT:
3880 case TYPE_UNION:
3881 case TYPE_LANG_STRUCT:
3882 case TYPE_STRING:
3883 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3884 d->prev_val[3]);
3885 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3886 break;
3888 case TYPE_USER_STRUCT:
3889 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3890 d->prev_val[3]);
3891 if (d->in_ptr_field)
3892 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3893 else
3894 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3895 d->indent, "", d->val);
3896 break;
3898 case TYPE_SCALAR:
3899 break;
3901 case TYPE_ARRAY:
3902 case TYPE_NONE:
3903 case TYPE_UNDEFINED:
3904 gcc_unreachable ();
3909 /* For S, a structure that's part of ORIG_S, and using parameters
3910 PARAM, write out a routine that:
3911 - Is of type gt_note_pointers
3912 - Calls PROCESS_FIELD on each field of S or its substructures.
3915 static void
3916 write_local_func_for_structure (const_type_p orig_s, type_p s)
3918 struct walk_type_data d;
3920 /* Don't write fns for subclasses, only for the ultimate base class
3921 within an inheritance hierarchy. */
3922 if (s->u.s.base_class)
3923 return;
3925 memset (&d, 0, sizeof (d));
3926 d.of = get_output_file_for_structure (s);
3927 d.process_field = write_types_local_process_field;
3928 d.opt = s->u.s.opt;
3929 d.line = &s->u.s.line;
3930 d.bitmap = s->u.s.bitmap;
3931 d.prev_val[0] = d.prev_val[2] = "*x";
3932 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3933 d.prev_val[3] = "x";
3934 d.val = "(*x)";
3935 d.fn_wants_lvalue = true;
3937 oprintf (d.of, "\n");
3938 oprintf (d.of, "void\n");
3939 oprintf (d.of, "gt_pch_p_");
3940 output_mangled_typename (d.of, orig_s);
3941 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3942 "\tvoid *x_p,\n"
3943 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3944 "\tATTRIBUTE_UNUSED void *cookie)\n");
3945 oprintf (d.of, "{\n");
3946 oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3947 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3948 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3949 d.indent = 2;
3950 d.have_this_obj = true;
3952 if (s->kind != TYPE_USER_STRUCT)
3953 walk_type (s, &d);
3954 else
3956 /* User structures have no fields to walk. Simply generate a
3957 call to the user-provided PCH walker. */
3958 oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
3959 d.prev_val[3]);
3960 oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3961 d.indent, "", d.val);
3964 oprintf (d.of, "}\n");
3966 /* Write user-callable entry points for the PCH walking routines. */
3967 if (orig_s->kind == TYPE_USER_STRUCT)
3968 write_pch_user_walking_functions (s, &d);
3970 for (options_p o = s->u.s.opt; o; o = o->next)
3971 if (strcmp (o->name, "for_user") == 0)
3973 write_pch_user_walking_for_structure_body (s, &d);
3974 break;
3978 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
3980 static void
3981 write_local (outf_p output_header, type_p structures)
3983 type_p s;
3985 if (!output_header)
3986 return;
3988 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
3989 for (s = structures; s; s = s->next)
3990 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3992 options_p opt;
3994 if (s->u.s.line.file == NULL)
3995 continue;
3996 for (opt = s->u.s.opt; opt; opt = opt->next)
3997 if (strcmp (opt->name, "ptr_alias") == 0
3998 && opt->kind == OPTION_TYPE)
4000 const_type_p const t = (const_type_p) opt->info.type;
4001 if (t->kind == TYPE_STRUCT
4002 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
4004 oprintf (output_header, "#define gt_pch_p_");
4005 output_mangled_typename (output_header, s);
4006 oprintf (output_header, " gt_pch_p_");
4007 output_mangled_typename (output_header, t);
4008 oprintf (output_header, "\n");
4010 else
4011 error_at_line (&s->u.s.line,
4012 "structure alias is not a structure");
4013 break;
4015 if (opt)
4016 continue;
4018 /* Declare the marker procedure only once. */
4019 oprintf (output_header, "extern void gt_pch_p_");
4020 output_mangled_typename (output_header, s);
4021 oprintf (output_header,
4022 "\n (void *, void *, gt_pointer_operator, void *);\n");
4024 if (s->kind == TYPE_LANG_STRUCT)
4026 type_p ss;
4027 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4028 write_local_func_for_structure (s, ss);
4030 else
4031 write_local_func_for_structure (s, s);
4035 /* Nonzero if S is a type for which typed GC allocators should be output. */
4037 #define USED_BY_TYPED_GC_P(s) \
4038 ((s->kind == TYPE_POINTER \
4039 && (s->u.p->gc_used == GC_POINTED_TO \
4040 || s->u.p->gc_used == GC_USED)) \
4041 || (union_or_struct_p (s) \
4042 && ((s)->gc_used == GC_POINTED_TO \
4043 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4044 && s->u.s.line.file != NULL) \
4045 || ((s)->gc_used == GC_USED \
4046 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))) \
4047 || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
4051 /* Might T contain any non-pointer elements? */
4053 static int
4054 contains_scalar_p (type_p t)
4056 switch (t->kind)
4058 case TYPE_STRING:
4059 case TYPE_POINTER:
4060 return 0;
4061 case TYPE_ARRAY:
4062 return contains_scalar_p (t->u.a.p);
4063 case TYPE_USER_STRUCT:
4064 /* User-marked structures will typically contain pointers. */
4065 return 0;
4066 default:
4067 /* Could also check for structures that have no non-pointer
4068 fields, but there aren't enough of those to worry about. */
4069 return 1;
4073 /* Mangle INPF and print it to F. */
4075 static void
4076 put_mangled_filename (outf_p f, const input_file *inpf)
4078 /* The call to get_output_file_name may indirectly update fn since
4079 get_output_file_with_visibility caches its result inside, so we
4080 need the CONST_CAST. */
4081 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4082 if (!f || !name)
4083 return;
4084 for (; *name != 0; name++)
4085 if (ISALNUM (*name))
4086 oprintf (f, "%c", *name);
4087 else
4088 oprintf (f, "%c", '_');
4091 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4092 LASTNAME, and NAME are all strings to insert in various places in
4093 the resulting code. */
4095 static void
4096 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4097 const char *tname, const char *name)
4099 struct flist *fli2;
4101 for (fli2 = flp; fli2; fli2 = fli2->next)
4102 if (fli2->started_p)
4104 oprintf (fli2->f, " %s\n", lastname);
4105 oprintf (fli2->f, "};\n\n");
4108 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4109 if (fli2->started_p)
4111 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4112 int fnum;
4114 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4115 if (bitmap & 1)
4117 oprintf (base_files[fnum],
4118 "extern const struct %s gt_%s_", tname, pfx);
4119 put_mangled_filename (base_files[fnum], fli2->file);
4120 oprintf (base_files[fnum], "[];\n");
4125 size_t fnum;
4126 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4127 oprintf (base_files[fnum],
4128 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4132 for (fli2 = flp; fli2; fli2 = fli2->next)
4133 if (fli2->started_p)
4135 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4136 int fnum;
4138 fli2->started_p = 0;
4140 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4141 if (bitmap & 1)
4143 oprintf (base_files[fnum], " gt_%s_", pfx);
4144 put_mangled_filename (base_files[fnum], fli2->file);
4145 oprintf (base_files[fnum], ",\n");
4150 size_t fnum;
4151 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4153 oprintf (base_files[fnum], " NULL\n");
4154 oprintf (base_files[fnum], "};\n");
4159 /* Finish off the created gt_clear_caches_file_c functions. */
4161 static void
4162 finish_cache_funcs (flist *flp)
4164 struct flist *fli2;
4166 for (fli2 = flp; fli2; fli2 = fli2->next)
4167 if (fli2->started_p)
4169 oprintf (fli2->f, "}\n\n");
4172 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4173 if (fli2->started_p)
4175 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4176 int fnum;
4178 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4179 if (bitmap & 1)
4181 oprintf (base_files[fnum], "extern void gt_clear_caches_");
4182 put_mangled_filename (base_files[fnum], fli2->file);
4183 oprintf (base_files[fnum], " ();\n");
4187 for (size_t fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4188 oprintf (base_files[fnum], "void\ngt_clear_caches ()\n{\n");
4190 for (fli2 = flp; fli2; fli2 = fli2->next)
4191 if (fli2->started_p)
4193 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4194 int fnum;
4196 fli2->started_p = 0;
4198 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4199 if (bitmap & 1)
4201 oprintf (base_files[fnum], " gt_clear_caches_");
4202 put_mangled_filename (base_files[fnum], fli2->file);
4203 oprintf (base_files[fnum], " ();\n");
4207 for (size_t fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4209 oprintf (base_files[fnum], "}\n");
4213 /* Write the first three fields (pointer, count and stride) for
4214 root NAME to F. V and LINE are as for write_root.
4216 Return true if the entry could be written; return false on error. */
4218 static bool
4219 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4221 type_p ap;
4223 if (!v)
4225 error_at_line (line, "`%s' is too complex to be a root", name);
4226 return false;
4229 oprintf (f, " {\n");
4230 oprintf (f, " &%s,\n", name);
4231 oprintf (f, " 1");
4233 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4234 if (ap->u.a.len[0])
4235 oprintf (f, " * (%s)", ap->u.a.len);
4236 else if (ap == v->type)
4237 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4238 oprintf (f, ",\n");
4239 oprintf (f, " sizeof (%s", v->name);
4240 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4241 oprintf (f, "[0]");
4242 oprintf (f, "),\n");
4243 return true;
4246 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4247 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4248 of the caller. */
4250 static void
4251 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4252 int has_length, struct fileloc *line,
4253 bool emit_pch, type_p field_type, const char *field_name)
4255 struct pair newv;
4256 /* If the field reference is relative to V, rather than to some
4257 subcomponent of V, we can mark any subarrays with a single stride.
4258 We're effectively treating the field as a global variable in its
4259 own right. */
4260 if (v && type == v->type)
4262 newv = *v;
4263 newv.type = field_type;
4264 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4265 v = &newv;
4267 /* Otherwise, any arrays nested in the structure are too complex to
4268 handle. */
4269 else if (field_type->kind == TYPE_ARRAY)
4270 v = NULL;
4271 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4272 has_length, line, emit_pch);
4275 /* Write out to F the table entry and any marker routines needed to
4276 mark NAME as TYPE. V can be one of three values:
4278 - null, if NAME is too complex to represent using a single
4279 count and stride. In this case, it is an error for NAME to
4280 contain any gc-ed data.
4282 - the outermost array that contains NAME, if NAME is part of an array.
4284 - the C variable that contains NAME, if NAME is not part of an array.
4286 LINE is the line of the C source that declares the root variable.
4287 HAS_LENGTH is nonzero iff V was a variable-length array. */
4289 static void
4290 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4291 struct fileloc *line, bool emit_pch)
4293 switch (type->kind)
4295 case TYPE_STRUCT:
4297 pair_p fld;
4298 for (fld = type->u.s.fields; fld; fld = fld->next)
4300 int skip_p = 0;
4301 const char *desc = NULL;
4302 options_p o;
4304 for (o = fld->opt; o; o = o->next)
4305 if (strcmp (o->name, "skip") == 0)
4306 skip_p = 1;
4307 else if (strcmp (o->name, "desc") == 0
4308 && o->kind == OPTION_STRING)
4309 desc = o->info.string;
4310 else
4311 error_at_line (line,
4312 "field `%s' of global `%s' has unknown option `%s'",
4313 fld->name, name, o->name);
4315 if (skip_p)
4316 continue;
4317 else if (desc && fld->type->kind == TYPE_UNION)
4319 pair_p validf = NULL;
4320 pair_p ufld;
4322 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4324 const char *tag = NULL;
4325 options_p oo;
4326 for (oo = ufld->opt; oo; oo = oo->next)
4327 if (strcmp (oo->name, "tag") == 0
4328 && oo->kind == OPTION_STRING)
4329 tag = oo->info.string;
4330 if (tag == NULL || strcmp (tag, desc) != 0)
4331 continue;
4332 if (validf != NULL)
4333 error_at_line (line,
4334 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4335 name, fld->name, validf->name,
4336 name, fld->name, ufld->name, tag);
4337 validf = ufld;
4339 if (validf != NULL)
4340 write_field_root (f, v, type, name, 0, line, emit_pch,
4341 validf->type,
4342 ACONCAT ((fld->name, ".",
4343 validf->name, NULL)));
4345 else if (desc)
4346 error_at_line (line,
4347 "global `%s.%s' has `desc' option but is not union",
4348 name, fld->name);
4349 else
4350 write_field_root (f, v, type, name, 0, line, emit_pch, fld->type,
4351 fld->name);
4354 break;
4356 case TYPE_ARRAY:
4358 char *newname;
4359 newname = xasprintf ("%s[0]", name);
4360 write_root (f, v, type->u.a.p, newname, has_length, line, emit_pch);
4361 free (newname);
4363 break;
4365 case TYPE_USER_STRUCT:
4366 error_at_line (line, "`%s' must be a pointer type, because it is "
4367 "a GC root and its type is marked with GTY((user))",
4368 v->name);
4369 break;
4371 case TYPE_POINTER:
4373 const_type_p tp;
4375 if (!start_root_entry (f, v, name, line))
4376 return;
4378 tp = type->u.p;
4380 if (!has_length && union_or_struct_p (tp))
4382 tp = get_ultimate_base_class (tp);
4383 const char *id_for_tag = filter_type_name (tp->u.s.tag);
4384 oprintf (f, " &gt_ggc_mx_%s,\n", id_for_tag);
4385 if (emit_pch)
4386 oprintf (f, " &gt_pch_nx_%s", id_for_tag);
4387 else
4388 oprintf (f, " NULL");
4389 if (id_for_tag != tp->u.s.tag)
4390 free (CONST_CAST (char *, id_for_tag));
4392 else if (has_length
4393 && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
4395 oprintf (f, " &gt_ggc_ma_%s,\n", name);
4396 if (emit_pch)
4397 oprintf (f, " &gt_pch_na_%s", name);
4398 else
4399 oprintf (f, " NULL");
4401 else
4403 error_at_line (line,
4404 "global `%s' is pointer to unimplemented type",
4405 name);
4407 oprintf (f, "\n },\n");
4409 break;
4411 case TYPE_STRING:
4413 if (!start_root_entry (f, v, name, line))
4414 return;
4416 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
4417 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
4418 oprintf (f, " },\n");
4420 break;
4422 case TYPE_SCALAR:
4423 break;
4425 case TYPE_NONE:
4426 case TYPE_UNDEFINED:
4427 case TYPE_UNION:
4428 case TYPE_LANG_STRUCT:
4429 error_at_line (line, "global `%s' is unimplemented type", name);
4433 /* This generates a routine to walk an array. */
4435 static void
4436 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4438 struct walk_type_data d;
4439 char *prevval3;
4441 memset (&d, 0, sizeof (d));
4442 d.of = f;
4443 d.cookie = wtd;
4444 d.indent = 2;
4445 d.line = &v->line;
4446 d.opt = v->opt;
4447 d.bitmap = get_lang_bitmap (v->line.file);
4449 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4451 if (wtd->param_prefix)
4453 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4454 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
4455 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4456 wtd->param_prefix, v->name);
4457 oprintf (d.of,
4458 " ATTRIBUTE_UNUSED void *x_p,\n"
4459 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4460 " ATTRIBUTE_UNUSED void * cookie)\n");
4461 oprintf (d.of, "{\n");
4462 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4463 d.process_field = write_types_local_process_field;
4464 d.have_this_obj = true;
4465 walk_type (v->type, &d);
4466 oprintf (f, "}\n\n");
4469 d.opt = v->opt;
4470 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4471 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4472 wtd->prefix, v->name);
4473 oprintf (f, "{\n");
4474 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4475 d.process_field = write_types_process_field;
4476 d.have_this_obj = false;
4477 walk_type (v->type, &d);
4478 free (prevval3);
4479 oprintf (f, "}\n\n");
4482 /* Output a table describing the locations and types of VARIABLES. */
4484 static void
4485 write_roots (pair_p variables, bool emit_pch)
4487 pair_p v;
4488 struct flist *flp = NULL;
4490 for (v = variables; v; v = v->next)
4492 outf_p f =
4493 get_output_file_with_visibility (CONST_CAST (input_file*,
4494 v->line.file));
4495 struct flist *fli;
4496 const char *length = NULL;
4497 int deletable_p = 0;
4498 options_p o;
4499 for (o = v->opt; o; o = o->next)
4500 if (strcmp (o->name, "length") == 0
4501 && o->kind == OPTION_STRING)
4502 length = o->info.string;
4503 else if (strcmp (o->name, "deletable") == 0)
4504 deletable_p = 1;
4505 else if (strcmp (o->name, "cache") == 0)
4507 else
4508 error_at_line (&v->line,
4509 "global `%s' has unknown option `%s'",
4510 v->name, o->name);
4512 for (fli = flp; fli; fli = fli->next)
4513 if (fli->f == f && f)
4514 break;
4515 if (fli == NULL)
4517 fli = XNEW (struct flist);
4518 fli->f = f;
4519 fli->next = flp;
4520 fli->started_p = 0;
4521 fli->file = v->line.file;
4522 gcc_assert (fli->file);
4523 flp = fli;
4525 oprintf (f, "\n/* GC roots. */\n\n");
4528 if (!deletable_p
4529 && length
4530 && v->type->kind == TYPE_POINTER
4531 && (v->type->u.p->kind == TYPE_POINTER
4532 || v->type->u.p->kind == TYPE_STRUCT))
4534 write_array (f, v, &ggc_wtd);
4535 write_array (f, v, &pch_wtd);
4539 for (v = variables; v; v = v->next)
4541 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4542 v->line.file));
4543 struct flist *fli;
4544 int skip_p = 0;
4545 int length_p = 0;
4546 options_p o;
4548 for (o = v->opt; o; o = o->next)
4549 if (strcmp (o->name, "length") == 0)
4550 length_p = 1;
4551 else if (strcmp (o->name, "deletable") == 0)
4552 skip_p = 1;
4554 if (skip_p)
4555 continue;
4557 for (fli = flp; fli; fli = fli->next)
4558 if (fli->f == f)
4559 break;
4560 if (!fli->started_p)
4562 fli->started_p = 1;
4564 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4565 put_mangled_filename (f, v->line.file);
4566 oprintf (f, "[] = {\n");
4569 write_root (f, v, v->type, v->name, length_p, &v->line, emit_pch);
4572 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4573 "gt_ggc_rtab");
4575 for (v = variables; v; v = v->next)
4577 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4578 v->line.file));
4579 struct flist *fli;
4580 int skip_p = 1;
4581 options_p o;
4583 for (o = v->opt; o; o = o->next)
4584 if (strcmp (o->name, "deletable") == 0)
4585 skip_p = 0;
4587 if (skip_p)
4588 continue;
4590 for (fli = flp; fli; fli = fli->next)
4591 if (fli->f == f)
4592 break;
4593 if (!fli->started_p)
4595 fli->started_p = 1;
4597 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4598 put_mangled_filename (f, v->line.file);
4599 oprintf (f, "[] = {\n");
4602 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4603 v->name, v->name);
4606 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4607 "gt_ggc_deletable_rtab");
4609 for (v = variables; v; v = v->next)
4611 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4612 v->line.file));
4613 struct flist *fli;
4614 bool cache = false;
4615 options_p o;
4617 for (o = v->opt; o; o = o->next)
4618 if (strcmp (o->name, "cache") == 0)
4619 cache = true;
4620 if (!cache)
4621 continue;
4623 for (fli = flp; fli; fli = fli->next)
4624 if (fli->f == f)
4625 break;
4626 if (!fli->started_p)
4628 fli->started_p = 1;
4630 oprintf (f, "void\ngt_clear_caches_");
4631 put_mangled_filename (f, v->line.file);
4632 oprintf (f, " ()\n{\n");
4635 oprintf (f, " gt_cleare_cache (%s);\n", v->name);
4638 finish_cache_funcs (flp);
4640 if (!emit_pch)
4641 return;
4643 for (v = variables; v; v = v->next)
4645 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4646 v->line.file));
4647 struct flist *fli;
4648 int skip_p = 0;
4649 options_p o;
4651 for (o = v->opt; o; o = o->next)
4652 if (strcmp (o->name, "deletable") == 0)
4654 skip_p = 1;
4655 break;
4658 if (skip_p)
4659 continue;
4661 if (!contains_scalar_p (v->type))
4662 continue;
4664 for (fli = flp; fli; fli = fli->next)
4665 if (fli->f == f)
4666 break;
4667 if (!fli->started_p)
4669 fli->started_p = 1;
4671 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4672 put_mangled_filename (f, v->line.file);
4673 oprintf (f, "[] = {\n");
4676 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4677 v->name, v->name);
4680 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4681 "gt_pch_scalar_rtab");
4684 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4685 guaranteee for somewhat increased readability. If name conflicts do happen,
4686 this funcion will have to be adjusted to be more like
4687 output_mangled_typename. */
4689 #define INDENT 2
4691 /* Dumps the value of typekind KIND. */
4693 static void
4694 dump_typekind (int indent, enum typekind kind)
4696 printf ("%*ckind = ", indent, ' ');
4697 switch (kind)
4699 case TYPE_SCALAR:
4700 printf ("TYPE_SCALAR");
4701 break;
4702 case TYPE_STRING:
4703 printf ("TYPE_STRING");
4704 break;
4705 case TYPE_STRUCT:
4706 printf ("TYPE_STRUCT");
4707 break;
4708 case TYPE_UNDEFINED:
4709 printf ("TYPE_UNDEFINED");
4710 break;
4711 case TYPE_USER_STRUCT:
4712 printf ("TYPE_USER_STRUCT");
4713 break;
4714 case TYPE_UNION:
4715 printf ("TYPE_UNION");
4716 break;
4717 case TYPE_POINTER:
4718 printf ("TYPE_POINTER");
4719 break;
4720 case TYPE_ARRAY:
4721 printf ("TYPE_ARRAY");
4722 break;
4723 case TYPE_LANG_STRUCT:
4724 printf ("TYPE_LANG_STRUCT");
4725 break;
4726 default:
4727 gcc_unreachable ();
4729 printf ("\n");
4732 /* Dumps the value of GC_USED flag. */
4734 static void
4735 dump_gc_used (int indent, enum gc_used_enum gc_used)
4737 printf ("%*cgc_used = ", indent, ' ');
4738 switch (gc_used)
4740 case GC_UNUSED:
4741 printf ("GC_UNUSED");
4742 break;
4743 case GC_USED:
4744 printf ("GC_USED");
4745 break;
4746 case GC_MAYBE_POINTED_TO:
4747 printf ("GC_MAYBE_POINTED_TO");
4748 break;
4749 case GC_POINTED_TO:
4750 printf ("GC_POINTED_TO");
4751 break;
4752 default:
4753 gcc_unreachable ();
4755 printf ("\n");
4758 /* Dumps the type options OPT. */
4760 static void
4761 dump_options (int indent, options_p opt)
4763 options_p o;
4764 printf ("%*coptions = ", indent, ' ');
4765 o = opt;
4766 while (o)
4768 switch (o->kind)
4770 case OPTION_STRING:
4771 printf ("%s:string %s ", o->name, o->info.string);
4772 break;
4773 case OPTION_TYPE:
4774 printf ("%s:type ", o->name);
4775 dump_type (indent+1, o->info.type);
4776 break;
4777 case OPTION_NESTED:
4778 printf ("%s:nested ", o->name);
4779 break;
4780 case OPTION_NONE:
4781 gcc_unreachable ();
4783 o = o->next;
4785 printf ("\n");
4788 /* Dumps the source file location in LINE. */
4790 static void
4791 dump_fileloc (int indent, struct fileloc line)
4793 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
4794 get_input_file_name (line.file),
4795 line.line);
4798 /* Recursively dumps the struct, union, or a language-specific
4799 struct T. */
4801 static void
4802 dump_type_u_s (int indent, type_p t)
4804 pair_p fields;
4806 gcc_assert (union_or_struct_p (t));
4807 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
4808 dump_fileloc (indent, t->u.s.line);
4809 printf ("%*cu.s.fields =\n", indent, ' ');
4810 fields = t->u.s.fields;
4811 while (fields)
4813 dump_pair (indent + INDENT, fields);
4814 fields = fields->next;
4816 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
4817 dump_options (indent, t->u.s.opt);
4818 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
4819 if (t->kind == TYPE_LANG_STRUCT)
4821 printf ("%*cu.s.lang_struct:\n", indent, ' ');
4822 dump_type_list (indent + INDENT, t->u.s.lang_struct);
4826 /* Recursively dumps the array T. */
4828 static void
4829 dump_type_u_a (int indent, type_p t)
4831 gcc_assert (t->kind == TYPE_ARRAY);
4832 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
4833 dump_type_list (indent + INDENT, t->u.a.p);
4836 /* Recursively dumps the type list T. */
4838 static void
4839 dump_type_list (int indent, type_p t)
4841 type_p p = t;
4842 while (p)
4844 dump_type (indent, p);
4845 p = p->next;
4849 static htab_t seen_types;
4851 /* Recursively dumps the type T if it was not dumped previously. */
4853 static void
4854 dump_type (int indent, type_p t)
4856 PTR *slot;
4858 printf ("%*cType at %p: ", indent, ' ', (void *) t);
4859 if (t->kind == TYPE_UNDEFINED)
4861 gcc_assert (t->gc_used == GC_UNUSED);
4862 printf ("undefined.\n");
4863 return;
4866 if (seen_types == NULL)
4867 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
4869 slot = htab_find_slot (seen_types, t, INSERT);
4870 if (*slot != NULL)
4872 printf ("already seen.\n");
4873 return;
4875 *slot = t;
4876 printf ("\n");
4878 dump_typekind (indent, t->kind);
4879 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
4880 (void *) t->pointer_to);
4881 dump_gc_used (indent + INDENT, t->gc_used);
4882 switch (t->kind)
4884 case TYPE_SCALAR:
4885 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
4886 t->u.scalar_is_char ? "true" : "false");
4887 break;
4888 case TYPE_STRING:
4889 break;
4890 case TYPE_STRUCT:
4891 case TYPE_UNION:
4892 case TYPE_LANG_STRUCT:
4893 case TYPE_USER_STRUCT:
4894 dump_type_u_s (indent + INDENT, t);
4895 break;
4896 case TYPE_POINTER:
4897 printf ("%*cp:\n", indent + INDENT, ' ');
4898 dump_type (indent + INDENT, t->u.p);
4899 break;
4900 case TYPE_ARRAY:
4901 dump_type_u_a (indent + INDENT, t);
4902 break;
4903 default:
4904 gcc_unreachable ();
4906 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
4909 /* Dumps the pair P. */
4911 static void
4912 dump_pair (int indent, pair_p p)
4914 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
4915 dump_type (indent, p->type);
4916 dump_fileloc (indent, p->line);
4917 dump_options (indent, p->opt);
4918 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
4921 /* Dumps the list of pairs PP. */
4923 static void
4924 dump_pair_list (const char *name, pair_p pp)
4926 pair_p p;
4927 printf ("%s:\n", name);
4928 for (p = pp; p != NULL; p = p->next)
4929 dump_pair (0, p);
4930 printf ("End of %s\n\n", name);
4933 /* Dumps the STRUCTURES. */
4935 static void
4936 dump_structures (const char *name, type_p structures)
4938 printf ("%s:\n", name);
4939 dump_type_list (0, structures);
4940 printf ("End of %s\n\n", name);
4943 /* Dumps the internal structures of gengtype. This is useful to debug
4944 gengtype itself, or to understand what it does, e.g. for plugin
4945 developers. */
4947 static void
4948 dump_everything (void)
4950 dump_pair_list ("typedefs", typedefs);
4951 dump_structures ("structures", structures);
4952 dump_pair_list ("variables", variables);
4954 /* Allocated with the first call to dump_type. */
4955 htab_delete (seen_types);
4960 /* Option specification for getopt_long. */
4961 static const struct option gengtype_long_options[] = {
4962 {"help", no_argument, NULL, 'h'},
4963 {"version", no_argument, NULL, 'V'},
4964 {"verbose", no_argument, NULL, 'v'},
4965 {"dump", no_argument, NULL, 'd'},
4966 {"debug", no_argument, NULL, 'D'},
4967 {"plugin", required_argument, NULL, 'P'},
4968 {"srcdir", required_argument, NULL, 'S'},
4969 {"backupdir", required_argument, NULL, 'B'},
4970 {"inputs", required_argument, NULL, 'I'},
4971 {"read-state", required_argument, NULL, 'r'},
4972 {"write-state", required_argument, NULL, 'w'},
4973 /* Terminating NULL placeholder. */
4974 {NULL, no_argument, NULL, 0},
4978 static void
4979 print_usage (void)
4981 printf ("Usage: %s\n", progname);
4982 printf ("\t -h | --help " " \t# Give this help.\n");
4983 printf ("\t -D | --debug "
4984 " \t# Give debug output to debug %s itself.\n", progname);
4985 printf ("\t -V | --version " " \t# Give version information.\n");
4986 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
4987 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
4988 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
4989 " \t# Generate for plugin.\n");
4990 printf ("\t -S | --srcdir <GCC-directory> "
4991 " \t# Specify the GCC source directory.\n");
4992 printf ("\t -B | --backupdir <directory> "
4993 " \t# Specify the backup directory for updated files.\n");
4994 printf ("\t -I | --inputs <input-list> "
4995 " \t# Specify the file with source files list.\n");
4996 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
4997 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5000 static void
5001 print_version (void)
5003 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5004 printf ("Report bugs: %s\n", bug_report_url);
5007 /* Parse the program options using getopt_long... */
5008 static void
5009 parse_program_options (int argc, char **argv)
5011 int opt = -1;
5012 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5013 gengtype_long_options, NULL)) >= 0)
5015 switch (opt)
5017 case 'h': /* --help */
5018 print_usage ();
5019 break;
5020 case 'V': /* --version */
5021 print_version ();
5022 break;
5023 case 'd': /* --dump */
5024 do_dump = 1;
5025 break;
5026 case 'D': /* --debug */
5027 do_debug = 1;
5028 break;
5029 case 'v': /* --verbose */
5030 verbosity_level++;
5031 break;
5032 case 'P': /* --plugin */
5033 if (optarg)
5034 plugin_output_filename = optarg;
5035 else
5036 fatal ("missing plugin output file name");
5037 break;
5038 case 'S': /* --srcdir */
5039 if (optarg)
5040 srcdir = optarg;
5041 else
5042 fatal ("missing source directory");
5043 srcdir_len = strlen (srcdir);
5044 break;
5045 case 'B': /* --backupdir */
5046 if (optarg)
5047 backup_dir = optarg;
5048 else
5049 fatal ("missing backup directory");
5050 break;
5051 case 'I': /* --inputs */
5052 if (optarg)
5053 inputlist = optarg;
5054 else
5055 fatal ("missing input list");
5056 break;
5057 case 'r': /* --read-state */
5058 if (optarg)
5059 read_state_filename = optarg;
5060 else
5061 fatal ("missing read state file");
5062 DBGPRINTF ("read state %s\n", optarg);
5063 break;
5064 case 'w': /* --write-state */
5065 DBGPRINTF ("write state %s\n", optarg);
5066 if (optarg)
5067 write_state_filename = optarg;
5068 else
5069 fatal ("missing write state file");
5070 break;
5071 default:
5072 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5073 print_usage ();
5074 fatal ("unexpected flag");
5077 if (plugin_output_filename)
5079 /* In plugin mode we require some input files. */
5080 int i = 0;
5081 if (optind >= argc)
5082 fatal ("no source files given in plugin mode");
5083 nb_plugin_files = argc - optind;
5084 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5085 for (i = 0; i < (int) nb_plugin_files; i++)
5087 char *name = argv[i + optind];
5088 plugin_files[i] = input_file_by_name (name);
5095 /******* Manage input files. ******/
5097 /* Hash table of unique input file names. */
5098 static htab_t input_file_htab;
5100 /* Find or allocate a new input_file by hash-consing it. */
5101 input_file*
5102 input_file_by_name (const char* name)
5104 PTR* slot;
5105 input_file* f = NULL;
5106 int namlen = 0;
5107 if (!name)
5108 return NULL;
5109 namlen = strlen (name);
5110 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5111 f->inpbitmap = 0;
5112 f->inpoutf = NULL;
5113 f->inpisplugin = false;
5114 strcpy (f->inpname, name);
5115 slot = htab_find_slot (input_file_htab, f, INSERT);
5116 gcc_assert (slot != NULL);
5117 if (*slot)
5119 /* Already known input file. */
5120 free (f);
5121 return (input_file*)(*slot);
5123 /* New input file. */
5124 *slot = f;
5125 return f;
5128 /* Hash table support routines for input_file-s. */
5129 static hashval_t
5130 htab_hash_inputfile (const void *p)
5132 const input_file *inpf = (const input_file *) p;
5133 gcc_assert (inpf);
5134 return htab_hash_string (get_input_file_name (inpf));
5137 static int
5138 htab_eq_inputfile (const void *x, const void *y)
5140 const input_file *inpfx = (const input_file *) x;
5141 const input_file *inpfy = (const input_file *) y;
5142 gcc_assert (inpfx != NULL && inpfy != NULL);
5143 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5148 main (int argc, char **argv)
5150 size_t i;
5151 static struct fileloc pos = { NULL, 0 };
5152 outf_p output_header;
5154 /* Mandatory common initializations. */
5155 progname = "gengtype"; /* For fatal and messages. */
5156 /* Create the hash-table used to hash-cons input files. */
5157 input_file_htab =
5158 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5159 /* Initialize our special input files. */
5160 this_file = input_file_by_name (__FILE__);
5161 system_h_file = input_file_by_name ("system.h");
5162 /* Set the scalar_is_char union number for predefined scalar types. */
5163 scalar_nonchar.u.scalar_is_char = FALSE;
5164 scalar_char.u.scalar_is_char = TRUE;
5166 parse_program_options (argc, argv);
5168 if (do_debug)
5170 time_t now = (time_t) 0;
5171 time (&now);
5172 DBGPRINTF ("gengtype started pid %d at %s",
5173 (int) getpid (), ctime (&now));
5176 /* Parse the input list and the input files. */
5177 DBGPRINTF ("inputlist %s", inputlist);
5178 if (read_state_filename)
5180 if (inputlist)
5181 fatal ("input list %s cannot be given with a read state file %s",
5182 inputlist, read_state_filename);
5183 read_state (read_state_filename);
5184 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5186 else if (inputlist)
5188 /* These types are set up with #define or else outside of where
5189 we can see them. We should initialize them before calling
5190 read_input_list. */
5191 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5192 Call;} while (0)
5193 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5194 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5195 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5196 POS_HERE (do_scalar_typedef ("double_int", &pos));
5197 POS_HERE (do_scalar_typedef ("poly_int64_pod", &pos));
5198 POS_HERE (do_scalar_typedef ("offset_int", &pos));
5199 POS_HERE (do_scalar_typedef ("widest_int", &pos));
5200 POS_HERE (do_scalar_typedef ("int64_t", &pos));
5201 POS_HERE (do_scalar_typedef ("poly_int64", &pos));
5202 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5203 POS_HERE (do_scalar_typedef ("uint8", &pos));
5204 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5205 POS_HERE (do_scalar_typedef ("jword", &pos));
5206 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5207 POS_HERE (do_scalar_typedef ("void", &pos));
5208 POS_HERE (do_scalar_typedef ("machine_mode", &pos));
5209 POS_HERE (do_scalar_typedef ("fixed_size_mode", &pos));
5210 POS_HERE (do_scalar_typedef ("CONSTEXPR", &pos));
5211 POS_HERE (do_typedef ("PTR",
5212 create_pointer (resolve_typedef ("void", &pos)),
5213 &pos));
5214 #undef POS_HERE
5215 read_input_list (inputlist);
5216 for (i = 0; i < num_gt_files; i++)
5218 parse_file (get_input_file_name (gt_files[i]));
5219 DBGPRINTF ("parsed file #%d %s",
5220 (int) i, get_input_file_name (gt_files[i]));
5222 if (verbosity_level >= 1)
5223 printf ("%s parsed %d files with %d GTY types\n",
5224 progname, (int) num_gt_files, type_count);
5226 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5228 else
5229 fatal ("either an input list or a read state file should be given");
5230 if (hit_error)
5231 return 1;
5234 if (plugin_output_filename)
5236 size_t ix = 0;
5237 /* In plugin mode, we should have read a state file, and have
5238 given at least one plugin file. */
5239 if (!read_state_filename)
5240 fatal ("No read state given in plugin mode for %s",
5241 plugin_output_filename);
5243 if (nb_plugin_files == 0 || !plugin_files)
5244 fatal ("No plugin files given in plugin mode for %s",
5245 plugin_output_filename);
5247 /* Parse our plugin files and augment the state. */
5248 for (ix = 0; ix < nb_plugin_files; ix++)
5250 input_file* pluginput = plugin_files [ix];
5251 pluginput->inpisplugin = true;
5252 parse_file (get_input_file_name (pluginput));
5254 if (hit_error)
5255 return 1;
5257 plugin_output = create_file ("GCC", plugin_output_filename);
5258 DBGPRINTF ("created plugin_output %p named %s",
5259 (void *) plugin_output, plugin_output->name);
5261 else
5262 { /* No plugin files, we are in normal mode. */
5263 if (!srcdir)
5264 fatal ("gengtype needs a source directory in normal mode");
5266 if (hit_error)
5267 return 1;
5269 gen_rtx_next ();
5271 set_gc_used (variables);
5273 for (type_p t = structures; t; t = t->next)
5275 bool for_user = false;
5276 for (options_p o = t->u.s.opt; o; o = o->next)
5277 if (strcmp (o->name, "for_user") == 0)
5279 for_user = true;
5280 break;
5283 if (for_user)
5284 set_gc_used_type (t, GC_POINTED_TO);
5286 /* The state at this point is read from the state input file or by
5287 parsing source files and optionally augmented by parsing plugin
5288 source files. Write it now. */
5289 if (write_state_filename)
5291 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5293 if (hit_error)
5294 fatal ("didn't write state file %s after errors",
5295 write_state_filename);
5297 DBGPRINTF ("before write_state %s", write_state_filename);
5298 write_state (write_state_filename);
5300 if (do_dump)
5301 dump_everything ();
5303 /* After having written the state file we return immediately to
5304 avoid generating any output file. */
5305 if (hit_error)
5306 return 1;
5307 else
5308 return 0;
5312 open_base_files ();
5314 output_header = plugin_output ? plugin_output : header_file;
5315 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5316 structures);
5318 write_types (output_header, structures, &ggc_wtd);
5319 if (plugin_files == NULL)
5321 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5322 structures);
5323 write_types (header_file, structures, &pch_wtd);
5324 write_local (header_file, structures);
5326 write_roots (variables, plugin_files == NULL);
5327 write_rtx_next ();
5328 close_output_files ();
5330 if (do_dump)
5331 dump_everything ();
5333 /* Don't bother about free-ing any input or plugin file, etc. */
5335 if (hit_error)
5336 return 1;
5337 return 0;