* c-ubsan.c (ubsan_instrument_shift): Use type0.
[official-gcc.git] / gcc / gengtype.c
blob5d219c37e27e87c8fd08843e18c1ab05aae5c93e
1 /* Process source files and output type information.
2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #ifdef HOST_GENERATOR_FILE
21 #include "config.h"
22 #define GENERATOR_FILE 1
23 #else
24 #include "bconfig.h"
25 #endif
26 #include "system.h"
27 #include "errors.h" /* for fatal */
28 #include "getopt.h"
29 #include "version.h" /* for version_string & pkgversion_string. */
30 #include "hashtab.h"
31 #include "xregex.h"
32 #include "obstack.h"
33 #include "gengtype.h"
34 #include "filenames.h"
36 /* Data types, macros, etc. used only in this file. */
39 /* The list of output files. */
40 outf_p output_files;
42 /* The output header file that is included into pretty much every
43 source file. */
44 outf_p header_file;
47 /* The name of the file containing the list of input files. */
48 static char *inputlist;
50 /* The plugin input files and their number; in that case only
51 a single file is produced. */
52 static input_file **plugin_files;
53 static size_t nb_plugin_files;
55 /* The generated plugin output file and name. */
56 static outf_p plugin_output;
57 static char *plugin_output_filename;
59 /* Our source directory and its length. */
60 const char *srcdir;
61 size_t srcdir_len;
63 /* Variables used for reading and writing the state. */
64 const char *read_state_filename;
65 const char *write_state_filename;
67 /* Variables to help debugging. */
68 int do_dump;
69 int do_debug;
71 /* Level for verbose messages. */
72 int verbosity_level;
74 /* We have a type count and use it to set the state_number of newly
75 allocated types to some unique negative number. */
76 static int type_count;
78 /* The backup directory should be in the same file system as the
79 generated files, otherwise the rename(2) system call would fail.
80 If NULL, no backup is made when overwriting a generated file. */
81 static const char* backup_dir; /* (-B) program option. */
84 static outf_p create_file (const char *, const char *);
86 static const char *get_file_basename (const input_file *);
87 static const char *get_file_realbasename (const input_file *);
89 static int get_prefix_langdir_index (const char *);
90 static const char *get_file_langdir (const input_file *);
92 static void dump_pair (int indent, pair_p p);
93 static void dump_type (int indent, type_p p);
94 static void dump_type_list (int indent, type_p p);
97 /* Nonzero iff an error has occurred. */
98 bool hit_error = false;
100 static void gen_rtx_next (void);
101 static void write_rtx_next (void);
102 static void open_base_files (void);
103 static void close_output_files (void);
105 /* Report an error at POS, printing MSG. */
107 void
108 error_at_line (const struct fileloc *pos, const char *msg, ...)
110 va_list ap;
112 gcc_assert (pos != NULL && pos->file != NULL);
113 va_start (ap, msg);
115 fprintf (stderr, "%s:%d: ", get_input_file_name (pos->file), pos->line);
116 vfprintf (stderr, msg, ap);
117 fputc ('\n', stderr);
118 hit_error = true;
120 va_end (ap);
123 /* Locate the ultimate base class of struct S. */
125 static const_type_p
126 get_ultimate_base_class (const_type_p s)
128 while (s->u.s.base_class)
129 s = s->u.s.base_class;
130 return s;
133 static type_p
134 get_ultimate_base_class (type_p s)
136 while (s->u.s.base_class)
137 s = s->u.s.base_class;
138 return s;
141 /* Input file handling. */
143 /* Table of all input files. */
144 const input_file **gt_files;
145 size_t num_gt_files;
147 /* A number of places use the name of this "gengtype.c" file for a
148 location for things that we can't rely on the source to define.
149 Make sure we can still use pointer comparison on filenames. */
150 input_file* this_file;
151 /* The "system.h" file is likewise specially useful. */
152 input_file* system_h_file;
154 /* Vector of per-language directories. */
155 const char **lang_dir_names;
156 size_t num_lang_dirs;
158 /* An array of output files suitable for definitions. There is one
159 BASE_FILES entry for each language. */
160 static outf_p *base_files;
164 #if ENABLE_CHECKING
165 /* Utility debugging function, printing the various type counts within
166 a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
167 void
168 dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
170 int nb_types = 0, nb_scalar = 0, nb_string = 0;
171 int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
172 int nb_lang_struct = 0;
173 int nb_user_struct = 0, nb_undefined = 0;
174 type_p p = NULL;
175 for (p = t; p; p = p->next)
177 nb_types++;
178 switch (p->kind)
180 case TYPE_UNDEFINED:
181 nb_undefined++;
182 case TYPE_SCALAR:
183 nb_scalar++;
184 break;
185 case TYPE_STRING:
186 nb_string++;
187 break;
188 case TYPE_STRUCT:
189 nb_struct++;
190 break;
191 case TYPE_USER_STRUCT:
192 nb_user_struct++;
193 break;
194 case TYPE_UNION:
195 nb_union++;
196 break;
197 case TYPE_POINTER:
198 nb_pointer++;
199 break;
200 case TYPE_ARRAY:
201 nb_array++;
202 break;
203 case TYPE_LANG_STRUCT:
204 nb_lang_struct++;
205 break;
206 case TYPE_NONE:
207 gcc_unreachable ();
210 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
211 lbasename (fil), lin, msg, nb_types);
212 if (nb_scalar > 0 || nb_string > 0)
213 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
214 if (nb_struct > 0 || nb_union > 0)
215 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
216 if (nb_pointer > 0 || nb_array > 0)
217 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
218 if (nb_lang_struct > 0)
219 fprintf (stderr, "@@%%@@ %d lang_structs\n", nb_lang_struct);
220 if (nb_user_struct > 0)
221 fprintf (stderr, "@@%%@@ %d user_structs\n", nb_user_struct);
222 if (nb_undefined > 0)
223 fprintf (stderr, "@@%%@@ %d undefined types\n", nb_undefined);
224 fprintf (stderr, "\n");
226 #endif /* ENABLE_CHECKING */
228 /* Scan the input file, LIST, and determine how much space we need to
229 store strings in. Also, count the number of language directories
230 and files. The numbers returned are overestimates as they does not
231 consider repeated files. */
232 static size_t
233 measure_input_list (FILE *list)
235 size_t n = 0;
236 int c;
237 bool atbol = true;
238 num_lang_dirs = 0;
239 num_gt_files = plugin_files ? nb_plugin_files : 0;
240 while ((c = getc (list)) != EOF)
242 n++;
243 if (atbol)
245 if (c == '[')
246 num_lang_dirs++;
247 else
249 /* Add space for a lang_bitmap before the input file name. */
250 n += sizeof (lang_bitmap);
251 num_gt_files++;
253 atbol = false;
256 if (c == '\n')
257 atbol = true;
260 rewind (list);
261 return n;
264 /* Read one input line from LIST to HEREP (which is updated). A
265 pointer to the string is returned via LINEP. If it was a language
266 subdirectory in square brackets, strip off the square brackets and
267 return true. Otherwise, leave space before the string for a
268 lang_bitmap, and return false. At EOF, returns false, does not
269 touch *HEREP, and sets *LINEP to NULL. POS is used for
270 diagnostics. */
271 static bool
272 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
274 char *here = *herep;
275 char *line;
276 int c = getc (list);
278 /* Read over whitespace. */
279 while (c == '\n' || c == ' ')
280 c = getc (list);
282 if (c == EOF)
284 *linep = 0;
285 return false;
287 else if (c == '[')
289 /* No space for a lang_bitmap is necessary. Discard the '['. */
290 c = getc (list);
291 line = here;
292 while (c != ']' && c != '\n' && c != EOF)
294 *here++ = c;
295 c = getc (list);
297 *here++ = '\0';
299 if (c == ']')
301 c = getc (list); /* eat what should be a newline */
302 if (c != '\n' && c != EOF)
303 error_at_line (pos, "junk on line after language tag [%s]", line);
305 else
306 error_at_line (pos, "missing close bracket for language tag [%s",
307 line);
309 *herep = here;
310 *linep = line;
311 return true;
313 else
315 /* Leave space for a lang_bitmap. */
316 memset (here, 0, sizeof (lang_bitmap));
317 here += sizeof (lang_bitmap);
318 line = here;
321 *here++ = c;
322 c = getc (list);
324 while (c != EOF && c != '\n');
325 *here++ = '\0';
326 *herep = here;
327 *linep = line;
328 return false;
332 /* Read the list of input files from LIST and compute all of the
333 relevant tables. There is one file per line of the list. At
334 first, all the files on the list are language-generic, but
335 eventually a line will appear which is the name of a language
336 subdirectory in square brackets, like this: [cp]. All subsequent
337 files are specific to that language, until another language
338 subdirectory tag appears. Files can appear more than once, if
339 they apply to more than one language. */
340 static void
341 read_input_list (const char *listname)
343 FILE *list = fopen (listname, "r");
344 if (!list)
345 fatal ("cannot open %s: %s", listname, xstrerror (errno));
346 else
348 struct fileloc epos;
349 size_t bufsz = measure_input_list (list);
350 char *buf = XNEWVEC (char, bufsz);
351 char *here = buf;
352 char *committed = buf;
353 char *limit = buf + bufsz;
354 char *line;
355 bool is_language;
356 size_t langno = 0;
357 size_t nfiles = 0;
358 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
360 epos.file = input_file_by_name (listname);
361 epos.line = 0;
363 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
364 gt_files = XNEWVEC (const input_file *, num_gt_files);
366 for (;;)
368 next_line:
369 epos.line++;
370 committed = here;
371 is_language = read_input_line (list, &here, &line, &epos);
372 gcc_assert (here <= limit);
373 if (line == 0)
374 break;
375 else if (is_language)
377 size_t i;
378 gcc_assert (langno <= num_lang_dirs);
379 for (i = 0; i < langno; i++)
380 if (strcmp (lang_dir_names[i], line) == 0)
382 error_at_line (&epos, "duplicate language tag [%s]",
383 line);
384 curlangs = 1 << i;
385 here = committed;
386 goto next_line;
389 curlangs = 1 << langno;
390 lang_dir_names[langno++] = line;
392 else
394 size_t i;
395 input_file *inpf = input_file_by_name (line);
396 gcc_assert (nfiles <= num_gt_files);
397 for (i = 0; i < nfiles; i++)
398 /* Since the input_file-s are uniquely hash-consed, we
399 can just compare pointers! */
400 if (gt_files[i] == inpf)
402 /* Throw away the string we just read, and add the
403 current language to the existing string's bitmap. */
404 lang_bitmap bmap = get_lang_bitmap (inpf);
405 if (bmap & curlangs)
406 error_at_line (&epos,
407 "file %s specified more than once "
408 "for language %s", line,
409 langno ==
410 0 ? "(all)" : lang_dir_names[langno -
411 1]);
413 bmap |= curlangs;
414 set_lang_bitmap (inpf, bmap);
415 here = committed;
416 goto next_line;
419 set_lang_bitmap (inpf, curlangs);
420 gt_files[nfiles++] = inpf;
423 /* Update the global counts now that we know accurately how many
424 things there are. (We do not bother resizing the arrays down.) */
425 num_lang_dirs = langno;
426 /* Add the plugin files if provided. */
427 if (plugin_files)
429 size_t i;
430 for (i = 0; i < nb_plugin_files; i++)
431 gt_files[nfiles++] = plugin_files[i];
433 num_gt_files = nfiles;
436 /* Sanity check: any file that resides in a language subdirectory
437 (e.g. 'cp') ought to belong to the corresponding language.
438 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
439 (Can you even do that? Should you be allowed to?) */
441 size_t f;
442 for (f = 0; f < num_gt_files; f++)
444 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
445 const char *basename = get_file_basename (gt_files[f]);
446 const char *slashpos = strchr (basename, '/');
447 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
448 const char *slashpos2 = strchr (basename, '\\');
450 if (!slashpos || (slashpos2 && slashpos2 < slashpos))
451 slashpos = slashpos2;
452 #endif
454 if (slashpos)
456 size_t l;
457 for (l = 0; l < num_lang_dirs; l++)
458 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
459 && memcmp (basename, lang_dir_names[l],
460 strlen (lang_dir_names[l])) == 0)
462 if (!(bitmap & (1 << l)))
463 error ("%s is in language directory '%s' but is not "
464 "tagged for that language",
465 basename, lang_dir_names[l]);
466 break;
472 if (ferror (list))
473 fatal ("error reading %s: %s", listname, xstrerror (errno));
475 fclose (list);
480 /* The one and only TYPE_STRING. */
482 struct type string_type = {
483 TYPE_STRING, 0, 0, 0, GC_USED, {0}
486 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
487 set early in main. */
489 struct type scalar_nonchar = {
490 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
493 struct type scalar_char = {
494 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
497 /* Lists of various things. */
499 pair_p typedefs = NULL;
500 type_p structures = NULL;
501 pair_p variables = NULL;
503 static type_p adjust_field_tree_exp (type_p t, options_p opt);
504 static type_p adjust_field_rtx_def (type_p t, options_p opt);
506 /* Define S as a typedef to T at POS. */
508 void
509 do_typedef (const char *s, type_p t, struct fileloc *pos)
511 pair_p p;
513 /* temporary kludge - gengtype doesn't handle conditionals or
514 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
515 is coming from this file (main() sets them up with safe dummy
516 definitions). */
517 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
518 return;
520 for (p = typedefs; p != NULL; p = p->next)
521 if (strcmp (p->name, s) == 0)
523 if (p->type != t && strcmp (s, "result_type") != 0)
525 error_at_line (pos, "type `%s' previously defined", s);
526 error_at_line (&p->line, "previously defined here");
528 return;
531 p = XNEW (struct pair);
532 p->next = typedefs;
533 p->name = s;
534 p->type = t;
535 p->line = *pos;
536 p->opt = NULL;
537 typedefs = p;
540 /* Define S as a typename of a scalar. Cannot be used to define
541 typedefs of 'char'. Note: is also used for pointer-to-function
542 typedefs (which are therefore not treated as pointers). */
544 void
545 do_scalar_typedef (const char *s, struct fileloc *pos)
547 do_typedef (s, &scalar_nonchar, pos);
550 /* Similar to strtok_r. */
552 static char *
553 strtoken (char *str, const char *delim, char **next)
555 char *p;
557 if (str == NULL)
558 str = *next;
560 /* Skip the leading delimiters. */
561 str += strspn (str, delim);
562 if (*str == '\0')
563 /* This is an empty token. */
564 return NULL;
566 /* The current token. */
567 p = str;
569 /* Find the next delimiter. */
570 str += strcspn (str, delim);
571 if (*str == '\0')
572 /* This is the last token. */
573 *next = str;
574 else
576 /* Terminate the current token. */
577 *str = '\0';
578 /* Advance to the next token. */
579 *next = str + 1;
582 return p;
585 /* Define TYPE_NAME to be a user defined type at location POS. */
587 type_p
588 create_user_defined_type (const char *type_name, struct fileloc *pos)
590 type_p ty = find_structure (type_name, TYPE_USER_STRUCT);
592 /* We might have already seen an incomplete decl of the given type,
593 in which case we won't have yet seen a GTY((user)), and the type will
594 only have kind "TYPE_STRUCT". Mark it as a user struct. */
595 ty->kind = TYPE_USER_STRUCT;
597 ty->u.s.line = *pos;
598 ty->u.s.bitmap = get_lang_bitmap (pos->file);
599 do_typedef (type_name, ty, pos);
601 /* If TYPE_NAME specifies a template, create references to the types
602 in the template by pretending that each type is a field of TY.
603 This is needed to make sure that the types referenced by the
604 template are marked as used. */
605 char *str = xstrdup (type_name);
606 char *open_bracket = strchr (str, '<');
607 if (open_bracket)
609 /* We only accept simple template declarations (see
610 require_template_declaration), so we only need to parse a
611 comma-separated list of strings, implicitly assumed to
612 be type names, potentially with "*" characters. */
613 char *arg = open_bracket + 1;
614 /* Workaround -Wmaybe-uninitialized false positive during
615 profiledbootstrap by initializing it. */
616 char *next = NULL;
617 char *type_id = strtoken (arg, ",>", &next);
618 pair_p fields = 0;
619 while (type_id)
621 /* Create a new field for every type found inside the template
622 parameter list. */
624 /* Support a single trailing "*" character. */
625 const char *star = strchr (type_id, '*');
626 int is_ptr = (star != NULL);
627 size_t offset_to_star = star - type_id;
628 if (is_ptr)
629 offset_to_star = star - type_id;
631 if (strstr (type_id, "char*"))
633 type_id = strtoken (0, ",>", &next);
634 continue;
637 char *field_name = xstrdup (type_id);
639 type_p arg_type;
640 if (is_ptr)
642 /* Strip off the first '*' character (and any subsequent text). */
643 *(field_name + offset_to_star) = '\0';
645 arg_type = find_structure (field_name, TYPE_STRUCT);
646 arg_type = create_pointer (arg_type);
648 else
649 arg_type = resolve_typedef (field_name, pos);
651 fields = create_field_at (fields, arg_type, field_name, 0, pos);
652 type_id = strtoken (0, ",>", &next);
655 /* Associate the field list to TY. */
656 ty->u.s.fields = fields;
658 free (str);
660 return ty;
664 /* Given a typedef name S, return its associated type. Return NULL if
665 S is not a registered type name. */
667 static type_p
668 type_for_name (const char *s)
670 pair_p p;
672 /* Special-case support for types within a "gcc::" namespace. Rather
673 than fully-supporting namespaces, simply strip off the "gcc::" prefix
674 where present. This allows us to have GTY roots of this form:
675 extern GTY(()) gcc::some_type *some_ptr;
676 where the autogenerated functions will refer to simply "some_type",
677 where they can be resolved into their namespace. */
678 if (0 == strncmp (s, "gcc::", 5))
679 s += 5;
681 for (p = typedefs; p != NULL; p = p->next)
682 if (strcmp (p->name, s) == 0)
683 return p->type;
684 return NULL;
688 /* Create an undefined type with name S and location POS. Return the
689 newly created type. */
691 static type_p
692 create_undefined_type (const char *s, struct fileloc *pos)
694 type_p ty = find_structure (s, TYPE_UNDEFINED);
695 ty->u.s.line = *pos;
696 ty->u.s.bitmap = get_lang_bitmap (pos->file);
697 do_typedef (s, ty, pos);
698 return ty;
702 /* Return the type previously defined for S. Use POS to report errors. */
704 type_p
705 resolve_typedef (const char *s, struct fileloc *pos)
707 bool is_template_instance = (strchr (s, '<') != NULL);
708 type_p p = type_for_name (s);
710 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
711 type for regular type identifiers. If the type identifier S is a
712 template instantiation, however, we treat it as a user defined
713 type.
715 FIXME, this is actually a limitation in gengtype. Supporting
716 template types and their instances would require keeping separate
717 track of the basic types definition and its instances. This
718 essentially forces all template classes in GC to be marked
719 GTY((user)). */
720 if (!p)
721 p = (is_template_instance)
722 ? create_user_defined_type (s, pos)
723 : create_undefined_type (s, pos);
725 return p;
728 /* Add SUBCLASS to head of linked list of BASE's subclasses. */
730 void add_subclass (type_p base, type_p subclass)
732 gcc_assert (union_or_struct_p (base));
733 gcc_assert (union_or_struct_p (subclass));
735 subclass->u.s.next_sibling_class = base->u.s.first_subclass;
736 base->u.s.first_subclass = subclass;
739 /* Create and return a new structure with tag NAME at POS with fields
740 FIELDS and options O. The KIND of structure must be one of
741 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
743 type_p
744 new_structure (const char *name, enum typekind kind, struct fileloc *pos,
745 pair_p fields, options_p o, type_p base_class)
747 type_p si;
748 type_p s = NULL;
749 lang_bitmap bitmap = get_lang_bitmap (pos->file);
750 bool isunion = (kind == TYPE_UNION);
752 gcc_assert (union_or_struct_p (kind));
754 for (si = structures; si != NULL; si = si->next)
755 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
757 type_p ls = NULL;
758 if (si->kind == TYPE_LANG_STRUCT)
760 ls = si;
762 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
763 if (si->u.s.bitmap == bitmap)
764 s = si;
766 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
768 ls = si;
769 type_count++;
770 si = XCNEW (struct type);
771 memcpy (si, ls, sizeof (struct type));
772 ls->kind = TYPE_LANG_STRUCT;
773 ls->u.s.lang_struct = si;
774 ls->u.s.fields = NULL;
775 si->next = NULL;
776 si->state_number = -type_count;
777 si->pointer_to = NULL;
778 si->u.s.lang_struct = ls;
780 else
781 s = si;
783 if (ls != NULL && s == NULL)
785 type_count++;
786 s = XCNEW (struct type);
787 s->state_number = -type_count;
788 s->next = ls->u.s.lang_struct;
789 ls->u.s.lang_struct = s;
790 s->u.s.lang_struct = ls;
792 break;
795 if (s == NULL)
797 type_count++;
798 s = XCNEW (struct type);
799 s->state_number = -type_count;
800 s->next = structures;
801 structures = s;
804 if (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap))
806 error_at_line (pos, "duplicate definition of '%s %s'",
807 isunion ? "union" : "struct", s->u.s.tag);
808 error_at_line (&s->u.s.line, "previous definition here");
811 s->kind = kind;
812 s->u.s.tag = name;
813 s->u.s.line = *pos;
814 s->u.s.fields = fields;
815 s->u.s.opt = o;
816 s->u.s.bitmap = bitmap;
817 if (s->u.s.lang_struct)
818 s->u.s.lang_struct->u.s.bitmap |= bitmap;
819 s->u.s.base_class = base_class;
820 if (base_class)
821 add_subclass (base_class, s);
823 return s;
826 /* Return the previously-defined structure or union with tag NAME,
827 or a new empty structure or union if none was defined previously.
828 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
829 TYPE_USER_STRUCT. */
831 type_p
832 find_structure (const char *name, enum typekind kind)
834 type_p s;
835 bool isunion = (kind == TYPE_UNION);
837 gcc_assert (kind == TYPE_UNDEFINED || union_or_struct_p (kind));
839 for (s = structures; s != NULL; s = s->next)
840 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
841 return s;
843 type_count++;
844 s = XCNEW (struct type);
845 s->next = structures;
846 s->state_number = -type_count;
847 structures = s;
848 s->kind = kind;
849 s->u.s.tag = name;
850 structures = s;
851 return s;
854 /* Return a scalar type with name NAME. */
856 type_p
857 create_scalar_type (const char *name)
859 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
860 return &scalar_char;
861 else
862 return &scalar_nonchar;
866 /* Return a pointer to T. */
868 type_p
869 create_pointer (type_p t)
871 if (!t->pointer_to)
873 type_p r = XCNEW (struct type);
874 type_count++;
875 r->state_number = -type_count;
876 r->kind = TYPE_POINTER;
877 r->u.p = t;
878 t->pointer_to = r;
880 return t->pointer_to;
883 /* Return an array of length LEN. */
885 type_p
886 create_array (type_p t, const char *len)
888 type_p v;
890 type_count++;
891 v = XCNEW (struct type);
892 v->kind = TYPE_ARRAY;
893 v->state_number = -type_count;
894 v->u.a.p = t;
895 v->u.a.len = len;
896 return v;
899 /* Return a string options structure with name NAME and info INFO.
900 NEXT is the next option in the chain. */
901 options_p
902 create_string_option (options_p next, const char *name, const char *info)
904 options_p o = XNEW (struct options);
905 o->kind = OPTION_STRING;
906 o->next = next;
907 o->name = name;
908 o->info.string = info;
909 return o;
912 /* Create a type options structure with name NAME and info INFO. NEXT
913 is the next option in the chain. */
914 options_p
915 create_type_option (options_p next, const char* name, type_p info)
917 options_p o = XNEW (struct options);
918 o->next = next;
919 o->name = name;
920 o->kind = OPTION_TYPE;
921 o->info.type = info;
922 return o;
925 /* Create a nested pointer options structure with name NAME and info
926 INFO. NEXT is the next option in the chain. */
927 options_p
928 create_nested_option (options_p next, const char* name,
929 struct nested_ptr_data* info)
931 options_p o;
932 o = XNEW (struct options);
933 o->next = next;
934 o->name = name;
935 o->kind = OPTION_NESTED;
936 o->info.nested = info;
937 return o;
940 /* Return an options structure for a "nested_ptr" option. */
941 options_p
942 create_nested_ptr_option (options_p next, type_p t,
943 const char *to, const char *from)
945 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
947 d->type = adjust_field_type (t, 0);
948 d->convert_to = to;
949 d->convert_from = from;
950 return create_nested_option (next, "nested_ptr", d);
953 /* Add a variable named S of type T with options O defined at POS,
954 to `variables'. */
955 void
956 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
958 pair_p n;
959 n = XNEW (struct pair);
960 n->name = s;
961 n->type = t;
962 n->line = *pos;
963 n->opt = o;
964 n->next = variables;
965 variables = n;
968 /* Most-general structure field creator. */
969 static pair_p
970 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
971 const input_file *inpf, int line)
973 pair_p field;
975 field = XNEW (struct pair);
976 field->next = next;
977 field->type = type;
978 field->name = name;
979 field->opt = opt;
980 field->line.file = inpf;
981 field->line.line = line;
982 return field;
985 /* Create a field that came from the source code we are scanning,
986 i.e. we have a 'struct fileloc', and possibly options; also,
987 adjust_field_type should be called. */
988 pair_p
989 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
990 struct fileloc *pos)
992 return create_field_all (next, adjust_field_type (type, opt),
993 name, opt, pos->file, pos->line);
996 /* Create a fake field with the given type and name. NEXT is the next
997 field in the chain. */
998 #define create_field(next,type,name) \
999 create_field_all (next,type,name, 0, this_file, __LINE__)
1001 /* Like create_field, but the field is only valid when condition COND
1002 is true. */
1004 static pair_p
1005 create_optional_field_ (pair_p next, type_p type, const char *name,
1006 const char *cond, int line)
1008 static int id = 1;
1009 pair_p union_fields;
1010 type_p union_type;
1012 /* Create a fake union type with a single nameless field of type TYPE.
1013 The field has a tag of "1". This allows us to make the presence
1014 of a field of type TYPE depend on some boolean "desc" being true. */
1015 union_fields = create_field (NULL, type, "");
1016 union_fields->opt =
1017 create_string_option (union_fields->opt, "dot", "");
1018 union_fields->opt =
1019 create_string_option (union_fields->opt, "tag", "1");
1020 union_type =
1021 new_structure (xasprintf ("%s_%d", "fake_union", id++), TYPE_UNION,
1022 &lexer_line, union_fields, NULL, NULL);
1024 /* Create the field and give it the new fake union type. Add a "desc"
1025 tag that specifies the condition under which the field is valid. */
1026 return create_field_all (next, union_type, name,
1027 create_string_option (0, "desc", cond),
1028 this_file, line);
1031 #define create_optional_field(next,type,name,cond) \
1032 create_optional_field_(next,type,name,cond,__LINE__)
1034 /* Reverse a linked list of 'struct pair's in place. */
1035 pair_p
1036 nreverse_pairs (pair_p list)
1038 pair_p prev = 0, p, next;
1039 for (p = list; p; p = next)
1041 next = p->next;
1042 p->next = prev;
1043 prev = p;
1045 return prev;
1049 /* We don't care how long a CONST_DOUBLE is. */
1050 #define CONST_DOUBLE_FORMAT "ww"
1051 /* We don't want to see codes that are only for generator files. */
1052 #undef GENERATOR_FILE
1054 enum rtx_code
1056 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1057 #include "rtl.def"
1058 #undef DEF_RTL_EXPR
1059 NUM_RTX_CODE
1062 static const char *const rtx_name[NUM_RTX_CODE] = {
1063 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1064 #include "rtl.def"
1065 #undef DEF_RTL_EXPR
1068 static const char *const rtx_format[NUM_RTX_CODE] = {
1069 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1070 #include "rtl.def"
1071 #undef DEF_RTL_EXPR
1074 static int rtx_next_new[NUM_RTX_CODE];
1076 /* We also need codes and names for insn notes (not register notes).
1077 Note that we do *not* bias the note values here. */
1078 enum insn_note
1080 #define DEF_INSN_NOTE(NAME) NAME,
1081 #include "insn-notes.def"
1082 #undef DEF_INSN_NOTE
1084 NOTE_INSN_MAX
1087 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1088 default field for line number notes. */
1089 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
1090 #define DEF_INSN_NOTE(NAME) #NAME,
1091 #include "insn-notes.def"
1092 #undef DEF_INSN_NOTE
1095 #undef CONST_DOUBLE_FORMAT
1096 #define GENERATOR_FILE
1098 /* Generate the contents of the rtx_next array. This really doesn't belong
1099 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1101 static void
1102 gen_rtx_next (void)
1104 int i;
1105 for (i = 0; i < NUM_RTX_CODE; i++)
1107 int k;
1109 rtx_next_new[i] = -1;
1110 if (strncmp (rtx_format[i], "uu", 2) == 0)
1111 rtx_next_new[i] = 1;
1112 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1113 rtx_next_new[i] = 1;
1114 else
1115 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
1116 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1117 rtx_next_new[i] = k;
1121 /* Write out the contents of the rtx_next array. */
1122 static void
1123 write_rtx_next (void)
1125 outf_p f = get_output_file_with_visibility (NULL);
1126 int i;
1127 if (!f)
1128 return;
1130 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
1131 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1132 for (i = 0; i < NUM_RTX_CODE; i++)
1133 if (rtx_next_new[i] == -1)
1134 oprintf (f, " 0,\n");
1135 else
1136 oprintf (f,
1137 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1138 oprintf (f, "};\n");
1141 /* Handle `special("rtx_def")'. This is a special case for field
1142 `fld' of struct rtx_def, which is an array of unions whose values
1143 are based in a complex way on the type of RTL. */
1145 static type_p
1146 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1148 pair_p flds = NULL;
1149 options_p nodot;
1150 int i;
1151 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1152 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1154 if (t->kind != TYPE_UNION)
1156 error_at_line (&lexer_line,
1157 "special `rtx_def' must be applied to a union");
1158 return &string_type;
1161 nodot = create_string_option (NULL, "dot", "");
1163 rtx_tp = create_pointer (find_structure ("rtx_def", TYPE_STRUCT));
1164 rtvec_tp = create_pointer (find_structure ("rtvec_def", TYPE_STRUCT));
1165 tree_tp = create_pointer (find_structure ("tree_node", TYPE_UNION));
1166 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", TYPE_STRUCT));
1167 reg_attrs_tp =
1168 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT));
1169 basic_block_tp =
1170 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT));
1171 constant_tp =
1172 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT));
1173 scalar_tp = &scalar_nonchar; /* rtunion int */
1176 pair_p note_flds = NULL;
1177 int c;
1179 for (c = 0; c <= NOTE_INSN_MAX; c++)
1181 switch (c)
1183 case NOTE_INSN_MAX:
1184 case NOTE_INSN_DELETED_LABEL:
1185 case NOTE_INSN_DELETED_DEBUG_LABEL:
1186 note_flds = create_field (note_flds, &string_type, "rt_str");
1187 break;
1189 case NOTE_INSN_BLOCK_BEG:
1190 case NOTE_INSN_BLOCK_END:
1191 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1192 break;
1194 case NOTE_INSN_VAR_LOCATION:
1195 case NOTE_INSN_CALL_ARG_LOCATION:
1196 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1197 break;
1199 default:
1200 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1201 break;
1203 /* NOTE_INSN_MAX is used as the default field for line
1204 number notes. */
1205 if (c == NOTE_INSN_MAX)
1206 note_flds->opt =
1207 create_string_option (nodot, "default", "");
1208 else
1209 note_flds->opt =
1210 create_string_option (nodot, "tag", note_insn_name[c]);
1212 note_union_tp = new_structure ("rtx_def_note_subunion", TYPE_UNION,
1213 &lexer_line, note_flds, NULL, NULL);
1215 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1217 pair_p sym_flds;
1218 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1219 sym_flds->opt = create_string_option (nodot, "default", "");
1220 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1221 sym_flds->opt = create_string_option (nodot, "tag", "1");
1222 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", TYPE_UNION,
1223 &lexer_line, sym_flds, NULL, NULL);
1225 for (i = 0; i < NUM_RTX_CODE; i++)
1227 pair_p subfields = NULL;
1228 size_t aindex, nmindex;
1229 const char *sname;
1230 type_p substruct;
1231 char *ftag;
1233 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1235 type_p t;
1236 const char *subname;
1238 switch (rtx_format[i][aindex])
1240 case '*':
1241 case 'i':
1242 case 'n':
1243 case 'w':
1244 case 'r':
1245 t = scalar_tp;
1246 subname = "rt_int";
1247 break;
1249 case '0':
1250 if (i == MEM && aindex == 1)
1251 t = mem_attrs_tp, subname = "rt_mem";
1252 else if (i == JUMP_INSN && aindex == 7)
1253 t = rtx_tp, subname = "rt_rtx";
1254 else if (i == CODE_LABEL && aindex == 4)
1255 t = scalar_tp, subname = "rt_int";
1256 else if (i == CODE_LABEL && aindex == 3)
1257 t = rtx_tp, subname = "rt_rtx";
1258 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1259 t = rtx_tp, subname = "rt_rtx";
1260 else if (i == NOTE && aindex == 3)
1261 t = note_union_tp, subname = "";
1262 else if (i == NOTE && aindex == 4)
1263 t = scalar_tp, subname = "rt_int";
1264 else if (i == NOTE && aindex >= 6)
1265 t = scalar_tp, subname = "rt_int";
1266 else if (i == ADDR_DIFF_VEC && aindex == 4)
1267 t = scalar_tp, subname = "rt_int";
1268 else if (i == VALUE && aindex == 0)
1269 t = scalar_tp, subname = "rt_int";
1270 else if (i == DEBUG_EXPR && aindex == 0)
1271 t = tree_tp, subname = "rt_tree";
1272 else if (i == SYMBOL_REF && aindex == 1)
1273 t = symbol_union_tp, subname = "";
1274 else if (i == JUMP_TABLE_DATA && aindex >= 4)
1275 t = scalar_tp, subname = "rt_int";
1276 else if (i == BARRIER && aindex >= 2)
1277 t = scalar_tp, subname = "rt_int";
1278 else if (i == ENTRY_VALUE && aindex == 0)
1279 t = rtx_tp, subname = "rt_rtx";
1280 else
1282 error_at_line
1283 (&lexer_line,
1284 "rtx type `%s' has `0' in position %lu, can't handle",
1285 rtx_name[i], (unsigned long) aindex);
1286 t = &string_type;
1287 subname = "rt_int";
1289 break;
1291 case 's':
1292 case 'S':
1293 case 'T':
1294 t = &string_type;
1295 subname = "rt_str";
1296 break;
1298 case 'e':
1299 case 'u':
1300 t = rtx_tp;
1301 subname = "rt_rtx";
1302 break;
1304 case 'E':
1305 case 'V':
1306 t = rtvec_tp;
1307 subname = "rt_rtvec";
1308 break;
1310 case 't':
1311 t = tree_tp;
1312 subname = "rt_tree";
1313 break;
1315 case 'B':
1316 t = basic_block_tp;
1317 subname = "rt_bb";
1318 break;
1320 default:
1321 error_at_line
1322 (&lexer_line,
1323 "rtx type `%s' has `%c' in position %lu, can't handle",
1324 rtx_name[i], rtx_format[i][aindex],
1325 (unsigned long) aindex);
1326 t = &string_type;
1327 subname = "rt_int";
1328 break;
1331 subfields = create_field (subfields, t,
1332 xasprintf (".fld[%lu].%s",
1333 (unsigned long) aindex,
1334 subname));
1335 subfields->opt = nodot;
1336 if (t == note_union_tp)
1337 subfields->opt =
1338 create_string_option (subfields->opt, "desc",
1339 "NOTE_KIND (&%0)");
1340 if (t == symbol_union_tp)
1341 subfields->opt =
1342 create_string_option (subfields->opt, "desc",
1343 "CONSTANT_POOL_ADDRESS_P (&%0)");
1346 if (i == REG)
1347 subfields = create_field (subfields, reg_attrs_tp, "reg.attrs");
1349 if (i == SYMBOL_REF)
1351 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1352 holds. */
1353 type_p field_tp = find_structure ("block_symbol", TYPE_STRUCT);
1354 subfields
1355 = create_optional_field (subfields, field_tp, "block_sym",
1356 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1359 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1360 substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
1361 NULL, NULL);
1363 ftag = xstrdup (rtx_name[i]);
1364 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1365 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1366 flds = create_field (flds, substruct, "");
1367 flds->opt = create_string_option (nodot, "tag", ftag);
1369 return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
1370 nodot, NULL);
1373 /* Handle `special("tree_exp")'. This is a special case for
1374 field `operands' of struct tree_exp, which although it claims to contain
1375 pointers to trees, actually sometimes contains pointers to RTL too.
1376 Passed T, the old type of the field, and OPT its options. Returns
1377 a new type for the field. */
1379 static type_p
1380 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1382 pair_p flds;
1383 options_p nodot;
1385 if (t->kind != TYPE_ARRAY)
1387 error_at_line (&lexer_line,
1388 "special `tree_exp' must be applied to an array");
1389 return &string_type;
1392 nodot = create_string_option (NULL, "dot", "");
1394 flds = create_field (NULL, t, "");
1395 flds->opt = create_string_option (nodot, "length",
1396 "TREE_OPERAND_LENGTH ((tree) &%0)");
1397 flds->opt = create_string_option (flds->opt, "default", "");
1399 return new_structure ("tree_exp_subunion", TYPE_UNION, &lexer_line, flds,
1400 nodot, NULL);
1403 /* Perform any special processing on a type T, about to become the type
1404 of a field. Return the appropriate type for the field.
1405 At present:
1406 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1407 - Similarly for arrays of pointer-to-char;
1408 - Converts structures for which a parameter is provided to
1409 TYPE_PARAM_STRUCT;
1410 - Handles "special" options.
1413 type_p
1414 adjust_field_type (type_p t, options_p opt)
1416 int length_p = 0;
1417 const int pointer_p = t->kind == TYPE_POINTER;
1419 for (; opt; opt = opt->next)
1420 if (strcmp (opt->name, "length") == 0)
1422 if (length_p)
1423 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1424 if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
1426 error_at_line (&lexer_line,
1427 "option `%s' may not be applied to "
1428 "arrays of atomic types", opt->name);
1430 length_p = 1;
1432 else if (strcmp (opt->name, "special") == 0
1433 && opt->kind == OPTION_STRING)
1435 const char *special_name = opt->info.string;
1436 if (strcmp (special_name, "tree_exp") == 0)
1437 t = adjust_field_tree_exp (t, opt);
1438 else if (strcmp (special_name, "rtx_def") == 0)
1439 t = adjust_field_rtx_def (t, opt);
1440 else
1441 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1444 if (!length_p
1445 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1446 return &string_type;
1447 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1448 && t->u.a.p->u.p->kind == TYPE_SCALAR
1449 && t->u.a.p->u.p->u.scalar_is_char)
1450 return create_array (&string_type, t->u.a.len);
1452 return t;
1456 static void set_gc_used_type (type_p, enum gc_used_enum, bool = false);
1457 static void set_gc_used (pair_p);
1459 /* Handle OPT for set_gc_used_type. */
1461 static void
1462 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1463 int *length, int *skip, type_p *nested_ptr)
1465 options_p o;
1466 for (o = opt; o; o = o->next)
1467 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1468 && o->kind == OPTION_TYPE)
1469 set_gc_used_type (o->info.type,
1470 GC_POINTED_TO);
1471 else if (strcmp (o->name, "maybe_undef") == 0)
1472 *maybe_undef = 1;
1473 else if (strcmp (o->name, "length") == 0)
1474 *length = 1;
1475 else if (strcmp (o->name, "skip") == 0)
1476 *skip = 1;
1477 else if (strcmp (o->name, "nested_ptr") == 0
1478 && o->kind == OPTION_NESTED)
1479 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1483 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1485 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1486 are set to GC_UNUSED. Otherwise, an error is emitted for
1487 TYPE_UNDEFINED types. This is used to support user-defined
1488 template types with non-type arguments.
1490 For instance, when we parse a template type with enum arguments
1491 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1492 artificial fields for 'MyType', one for 'AnotherType', the other
1493 one for 'EnumValue'.
1495 At the time that we parse this type we don't know that 'EnumValue'
1496 is really an enum value, so the parser creates a TYPE_UNDEFINED
1497 type for it. Since 'EnumValue' is never resolved to a known
1498 structure, it will stay with TYPE_UNDEFINED.
1500 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1501 'EnumValue'. Generating marking code for it would cause
1502 compilation failures since the marking routines assumes that
1503 'EnumValue' is a type. */
1505 static void
1506 set_gc_used_type (type_p t, enum gc_used_enum level,
1507 bool allow_undefined_types)
1509 if (t->gc_used >= level)
1510 return;
1512 t->gc_used = level;
1514 switch (t->kind)
1516 case TYPE_STRUCT:
1517 case TYPE_UNION:
1518 case TYPE_USER_STRUCT:
1520 pair_p f;
1521 int dummy;
1522 type_p dummy2;
1523 bool allow_undefined_field_types = (t->kind == TYPE_USER_STRUCT);
1525 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy,
1526 &dummy2);
1528 if (t->u.s.base_class)
1529 set_gc_used_type (t->u.s.base_class, level, allow_undefined_types);
1530 /* Anything pointing to a base class might actually be pointing
1531 to a subclass. */
1532 for (type_p subclass = t->u.s.first_subclass; subclass;
1533 subclass = subclass->u.s.next_sibling_class)
1534 set_gc_used_type (subclass, level, allow_undefined_types);
1536 FOR_ALL_INHERITED_FIELDS(t, f)
1538 int maybe_undef = 0;
1539 int length = 0;
1540 int skip = 0;
1541 type_p nested_ptr = NULL;
1542 process_gc_options (f->opt, level, &maybe_undef, &length, &skip,
1543 &nested_ptr);
1545 if (nested_ptr && f->type->kind == TYPE_POINTER)
1546 set_gc_used_type (nested_ptr, GC_POINTED_TO);
1547 else if (length && f->type->kind == TYPE_POINTER)
1548 set_gc_used_type (f->type->u.p, GC_USED);
1549 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1550 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO);
1551 else if (skip)
1552 ; /* target type is not used through this field */
1553 else
1554 set_gc_used_type (f->type, GC_USED, allow_undefined_field_types);
1556 break;
1559 case TYPE_UNDEFINED:
1560 if (level > GC_UNUSED)
1562 if (!allow_undefined_types)
1563 error_at_line (&t->u.s.line, "undefined type `%s'", t->u.s.tag);
1564 t->gc_used = GC_UNUSED;
1566 break;
1568 case TYPE_POINTER:
1569 set_gc_used_type (t->u.p, GC_POINTED_TO);
1570 break;
1572 case TYPE_ARRAY:
1573 set_gc_used_type (t->u.a.p, GC_USED);
1574 break;
1576 case TYPE_LANG_STRUCT:
1577 for (t = t->u.s.lang_struct; t; t = t->next)
1578 set_gc_used_type (t, level);
1579 break;
1581 default:
1582 break;
1586 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1588 static void
1589 set_gc_used (pair_p variables)
1591 int nbvars = 0;
1592 pair_p p;
1593 for (p = variables; p; p = p->next)
1595 set_gc_used_type (p->type, GC_USED);
1596 nbvars++;
1598 if (verbosity_level >= 2)
1599 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1602 /* File mapping routines. For each input file, there is one output .c file
1603 (but some output files have many input files), and there is one .h file
1604 for the whole build. */
1606 /* Output file handling. */
1608 /* Create and return an outf_p for a new file for NAME, to be called
1609 ONAME. */
1611 static outf_p
1612 create_file (const char *name, const char *oname)
1614 static const char *const hdr[] = {
1615 " Copyright (C) 2004-2015 Free Software Foundation, Inc.\n",
1616 "\n",
1617 "This file is part of GCC.\n",
1618 "\n",
1619 "GCC is free software; you can redistribute it and/or modify it under\n",
1620 "the terms of the GNU General Public License as published by the Free\n",
1621 "Software Foundation; either version 3, or (at your option) any later\n",
1622 "version.\n",
1623 "\n",
1624 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1625 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1626 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1627 "for more details.\n",
1628 "\n",
1629 "You should have received a copy of the GNU General Public License\n",
1630 "along with GCC; see the file COPYING3. If not see\n",
1631 "<http://www.gnu.org/licenses/>. */\n",
1632 "\n",
1633 "/* This file is machine generated. Do not edit. */\n"
1635 outf_p f;
1636 size_t i;
1638 gcc_assert (name != NULL);
1639 gcc_assert (oname != NULL);
1640 f = XCNEW (struct outf);
1641 f->next = output_files;
1642 f->name = oname;
1643 output_files = f;
1645 oprintf (f, "/* Type information for %s.\n", name);
1646 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1647 oprintf (f, "%s", hdr[i]);
1648 return f;
1651 /* Print, like fprintf, to O.
1652 N.B. You might think this could be implemented more efficiently
1653 with vsnprintf(). Unfortunately, there are C libraries that
1654 provide that function but without the C99 semantics for its return
1655 value, making it impossible to know how much space is required. */
1656 void
1657 oprintf (outf_p o, const char *format, ...)
1659 char *s;
1660 size_t slength;
1661 va_list ap;
1663 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1664 in that case. */
1665 if (!o)
1666 return;
1668 va_start (ap, format);
1669 slength = vasprintf (&s, format, ap);
1670 if (s == NULL || (int) slength < 0)
1671 fatal ("out of memory");
1672 va_end (ap);
1674 if (o->bufused + slength > o->buflength)
1676 size_t new_len = o->buflength;
1677 if (new_len == 0)
1678 new_len = 1024;
1681 new_len *= 2;
1683 while (o->bufused + slength >= new_len);
1684 o->buf = XRESIZEVEC (char, o->buf, new_len);
1685 o->buflength = new_len;
1687 memcpy (o->buf + o->bufused, s, slength);
1688 o->bufused += slength;
1689 free (s);
1692 /* Open the global header file and the language-specific header files. */
1694 static void
1695 open_base_files (void)
1697 size_t i;
1699 if (nb_plugin_files > 0 && plugin_files)
1700 return;
1702 header_file = create_file ("GCC", "gtype-desc.h");
1704 base_files = XNEWVEC (outf_p, num_lang_dirs);
1706 for (i = 0; i < num_lang_dirs; i++)
1707 base_files[i] = create_file (lang_dir_names[i],
1708 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1710 /* gtype-desc.c is a little special, so we create it here. */
1712 /* The order of files here matters very much. */
1713 static const char *const ifiles[] = {
1714 "config.h", "system.h", "coretypes.h", "tm.h", "insn-codes.h",
1715 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1716 "hash-set.h", "machmode.h", "vec.h", "double-int.h", "input.h",
1717 "alias.h", "symtab.h", "options.h",
1718 "wide-int.h", "inchash.h",
1719 "tree.h", "fold-const.h", "rtl.h",
1720 "machmode.h", "tm.h", "hard-reg-set.h", "input.h", "predict.h",
1721 "function.h", "insn-config.h", "flags.h", "statistics.h",
1722 "real.h", "fixed-value.h", "tree.h", "expmed.h", "dojump.h",
1723 "explow.h", "calls.h", "emit-rtl.h", "varasm.h", "stmt.h",
1724 "expr.h", "alloc-pool.h",
1725 "basic-block.h", "cselib.h", "insn-addr.h",
1726 "optabs.h", "libfuncs.h", "debug.h", "ggc.h",
1727 "ggc.h", "dominance.h", "cfg.h", "basic-block.h",
1728 "tree-ssa-alias.h", "internal-fn.h", "gimple-fold.h", "tree-eh.h",
1729 "gimple-expr.h", "is-a.h",
1730 "gimple.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
1731 "tree-phinodes.h", "ssa-iterators.h", "stringpool.h", "tree-ssanames.h",
1732 "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h", "tree-ssa-loop-manip.h",
1733 "tree-ssa-loop-niter.h", "tree-into-ssa.h", "tree-dfa.h",
1734 "tree-ssa.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1735 "except.h", "output.h", "cfgloop.h", "target.h", "lto-streamer.h",
1736 "target-globals.h", "ipa-ref.h", "cgraph.h", "symbol-summary.h",
1737 "ipa-prop.h", "ipa-inline.h", "dwarf2out.h", "omp-low.h", NULL
1739 const char *const *ifp;
1740 outf_p gtype_desc_c;
1742 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1743 for (ifp = ifiles; *ifp; ifp++)
1744 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1746 /* Make sure we handle "cfun" specially. */
1747 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1748 oprintf (gtype_desc_c, "#undef cfun\n");
1750 oprintf (gtype_desc_c,
1751 "\n"
1752 "/* Types with a \"gcc::\" namespace have it stripped\n"
1753 " during gengtype parsing. Provide a \"using\" directive\n"
1754 " to ensure that the fully-qualified types are found. */\n"
1755 "using namespace gcc;\n");
1759 /* For INPF an input file, return the real basename of INPF, with all
1760 the directory components skipped. */
1762 static const char *
1763 get_file_realbasename (const input_file *inpf)
1765 return lbasename (get_input_file_name (inpf));
1768 /* For INPF a filename, return the relative path to INPF from
1769 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1771 const char *
1772 get_file_srcdir_relative_path (const input_file *inpf)
1774 const char *f = get_input_file_name (inpf);
1775 if (strlen (f) > srcdir_len
1776 && IS_DIR_SEPARATOR (f[srcdir_len])
1777 && strncmp (f, srcdir, srcdir_len) == 0)
1778 return f + srcdir_len + 1;
1779 else
1780 return NULL;
1783 /* For INPF an input_file, return the relative path to INPF from
1784 $(srcdir) if the latter is a prefix in INPF, or the real basename
1785 of INPF otherwise. */
1787 static const char *
1788 get_file_basename (const input_file *inpf)
1790 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1792 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1795 /* For F a filename, return the lang_dir_names relative index of the language
1796 directory that is a prefix in F, if any, -1 otherwise. */
1798 static int
1799 get_prefix_langdir_index (const char *f)
1801 size_t f_len = strlen (f);
1802 size_t lang_index;
1804 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1806 const char *langdir = lang_dir_names[lang_index];
1807 size_t langdir_len = strlen (langdir);
1809 if (f_len > langdir_len
1810 && IS_DIR_SEPARATOR (f[langdir_len])
1811 && memcmp (f, langdir, langdir_len) == 0)
1812 return lang_index;
1815 return -1;
1818 /* For INPF an input file, return the name of language directory where
1819 F is located, if any, NULL otherwise. */
1821 static const char *
1822 get_file_langdir (const input_file *inpf)
1824 /* Get the relative path to INPF from $(srcdir) and find the
1825 language by comparing the prefix with language directory names.
1826 If INPF is not even srcdir relative, no point in looking
1827 further. */
1829 int lang_index;
1830 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1831 const char *r;
1833 if (!srcdir_relative_path)
1834 return NULL;
1836 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1837 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1838 r = "c-family";
1839 else if (lang_index >= 0)
1840 r = lang_dir_names[lang_index];
1841 else
1842 r = NULL;
1844 return r;
1847 /* The gt- output file name for INPF. */
1849 static const char *
1850 get_file_gtfilename (const input_file *inpf)
1852 /* Cook up an initial version of the gt- file name from the file real
1853 basename and the language name, if any. */
1855 const char *basename = get_file_realbasename (inpf);
1856 const char *langdir = get_file_langdir (inpf);
1858 char *result =
1859 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1860 : xasprintf ("gt-%s", basename));
1862 /* Then replace all non alphanumerics characters by '-' and change the
1863 extension to ".h". We expect the input filename extension was at least
1864 one character long. */
1866 char *s = result;
1868 for (; *s != '.'; s++)
1869 if (!ISALNUM (*s) && *s != '-')
1870 *s = '-';
1872 memcpy (s, ".h", sizeof (".h"));
1874 return result;
1877 /* Each input_file has its associated output file outf_p. The
1878 association is computed by the function
1879 get_output_file_with_visibility. The associated file is cached
1880 inside input_file in its inpoutf field, so is really computed only
1881 once. Associated output file paths (i.e. output_name-s) are
1882 computed by a rule based regexp machinery, using the files_rules
1883 array of struct file_rule_st. A for_name is also computed, giving
1884 the source file name for which the output_file is generated; it is
1885 often the last component of the input_file path. */
1889 Regexpr machinery to compute the output_name and for_name-s of each
1890 input_file. We have a sequence of file rules which gives the POSIX
1891 extended regular expression to match an input file path, and two
1892 transformed strings for the corresponding output_name and the
1893 corresponding for_name. The transformed string contain dollars: $0
1894 is replaced by the entire match, $1 is replaced by the substring
1895 matching the first parenthesis in the regexp, etc. And $$ is replaced
1896 by a single verbatim dollar. The rule order is important. The
1897 general case is last, and the particular cases should come before.
1898 An action routine can, when needed, update the out_name & for_name
1899 and/or return the appropriate output file. It is invoked only when a
1900 rule is triggered. When a rule is triggered, the output_name and
1901 for_name are computed using their transform string in while $$, $0,
1902 $1, ... are suitably replaced. If there is an action, it is called.
1903 In some few cases, the action can directly return the outf_p, but
1904 usually it just updates the output_name and for_name so should free
1905 them before replacing them. The get_output_file_with_visibility
1906 function creates an outf_p only once per each output_name, so it
1907 scans the output_files list for previously seen output file names.
1910 /* Signature of actions in file rules. */
1911 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
1914 struct file_rule_st {
1915 const char* frul_srcexpr; /* Source string for regexp. */
1916 int frul_rflags; /* Flags passed to regcomp, usually
1917 * REG_EXTENDED. */
1918 regex_t* frul_re; /* Compiled regular expression
1919 obtained by regcomp. */
1920 const char* frul_tr_out; /* Transformation string for making
1921 * the output_name, with $1 ... $9 for
1922 * subpatterns and $0 for the whole
1923 * matched filename. */
1924 const char* frul_tr_for; /* Tranformation string for making the
1925 for_name. */
1926 frul_actionrout_t* frul_action; /* The action, if non null, is
1927 * called once the rule matches, on
1928 * the transformed out_name &
1929 * for_name. It could change them
1930 * and/or give the output file. */
1933 /* File rule action handling *.h files. */
1934 static outf_p header_dot_h_frul (input_file*, char**, char**);
1936 /* File rule action handling *.c files. */
1937 static outf_p source_dot_c_frul (input_file*, char**, char**);
1939 #define NULL_REGEX (regex_t*)0
1941 /* The prefix in our regexp-s matching the directory. */
1942 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1944 #define NULL_FRULACT (frul_actionrout_t*)0
1946 /* The array of our rules governing file name generation. Rules order
1947 matters, so change with extreme care! */
1949 struct file_rule_st files_rules[] = {
1950 /* The general rule assumes that files in subdirectories belong to a
1951 particular front-end, and files not in subdirectories are shared.
1952 The following rules deal with exceptions - files that are in
1953 subdirectories and yet are shared, and files that are top-level,
1954 but are not shared. */
1956 /* the c-family/ source directory is special. */
1957 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
1958 REG_EXTENDED, NULL_REGEX,
1959 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
1961 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
1962 REG_EXTENDED, NULL_REGEX,
1963 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
1965 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
1966 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
1967 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1969 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
1970 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1972 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
1973 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
1974 REG_EXTENDED, NULL_REGEX,
1975 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
1977 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
1978 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
1979 REG_EXTENDED, NULL_REGEX,
1980 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
1982 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
1983 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
1984 REG_EXTENDED, NULL_REGEX,
1985 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
1987 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
1988 { DIR_PREFIX_REGEX "cp/parser\\.h$",
1989 REG_EXTENDED, NULL_REGEX,
1990 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
1992 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
1993 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
1994 REG_EXTENDED, NULL_REGEX,
1995 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
1997 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
1998 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
1999 REG_EXTENDED, NULL_REGEX,
2000 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
2002 /* General cases. For header *.h and source *.c or *.cc files, we
2003 * need special actions to handle the language. */
2005 /* Source *.c files are using get_file_gtfilename to compute their
2006 output_name and get_file_basename to compute their for_name
2007 through the source_dot_c_frul action. */
2008 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
2009 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
2011 /* Source *.cc files are using get_file_gtfilename to compute their
2012 output_name and get_file_basename to compute their for_name
2013 through the source_dot_c_frul action. */
2014 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.cc$",
2015 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.cc", source_dot_c_frul},
2017 /* Common header files get "gtype-desc.c" as their output_name,
2018 * while language specific header files are handled specially. So
2019 * we need the header_dot_h_frul action. */
2020 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
2021 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
2023 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
2024 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
2026 /* Mandatory null last entry signaling end of rules. */
2027 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
2030 /* Special file rules action for handling *.h header files. It gives
2031 "gtype-desc.c" for common headers and corresponding output
2032 files for language-specific header files. */
2033 static outf_p
2034 header_dot_h_frul (input_file* inpf, char**poutname,
2035 char**pforname ATTRIBUTE_UNUSED)
2037 const char *basename = 0;
2038 int lang_index = 0;
2039 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2040 (void*) inpf, get_input_file_name (inpf),
2041 *poutname, *pforname);
2042 basename = get_file_basename (inpf);
2043 lang_index = get_prefix_langdir_index (basename);
2044 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
2046 if (lang_index >= 0)
2048 /* The header is language specific. Given output_name &
2049 for_name remains unchanged. The base_files array gives the
2050 outf_p. */
2051 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2052 (void*) base_files[lang_index],
2053 (base_files[lang_index])->name);
2054 return base_files[lang_index];
2056 else
2058 /* The header is common to all front-end languages. So
2059 output_name is "gtype-desc.c" file. The calling function
2060 get_output_file_with_visibility will find its outf_p. */
2061 free (*poutname);
2062 *poutname = xstrdup ("gtype-desc.c");
2063 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2064 get_input_file_name (inpf));
2065 return NULL;
2070 /* Special file rules action for handling *.c source files using
2071 * get_file_gtfilename to compute their output_name and
2072 * get_file_basename to compute their for_name. The output_name is
2073 * gt-<LANG>-<BASE>.h for language specific source files, and
2074 * gt-<BASE>.h for common source files. */
2075 static outf_p
2076 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
2078 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
2079 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
2080 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2081 (void*) inpf, get_input_file_name (inpf),
2082 *poutname, *pforname);
2083 DBGPRINTF ("newoutname %s", newoutname);
2084 DBGPRINTF ("newbasename %s", newbasename);
2085 free (*poutname);
2086 free (*pforname);
2087 *poutname = newoutname;
2088 *pforname = newbasename;
2089 return NULL;
2092 /* Utility function for get_output_file_with_visibility which returns
2093 * a malloc-ed substituted string using TRS on matching of the FILNAM
2094 * file name, using the PMATCH array. */
2095 static char*
2096 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
2097 const char *trs)
2099 struct obstack str_obstack;
2100 char *str = NULL;
2101 char *rawstr = NULL;
2102 const char *pt = NULL;
2103 DBGPRINTF ("filnam %s", filnam);
2104 obstack_init (&str_obstack);
2105 for (pt = trs; *pt; pt++) {
2106 char c = *pt;
2107 if (c == '$')
2109 if (pt[1] == '$')
2111 /* A double dollar $$ is substituted by a single verbatim
2112 dollar, but who really uses dollar signs in file
2113 paths? */
2114 obstack_1grow (&str_obstack, '$');
2116 else if (ISDIGIT (pt[1]))
2118 /* Handle $0 $1 ... $9 by appropriate substitution. */
2119 int dolnum = pt[1] - '0';
2120 int so = pmatch[dolnum].rm_so;
2121 int eo = pmatch[dolnum].rm_eo;
2122 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2123 if (so>=0 && eo>=so)
2124 obstack_grow (&str_obstack, filnam + so, eo - so);
2126 else
2128 /* This can happen only when files_rules is buggy! */
2129 gcc_unreachable ();
2131 /* Always skip the character after the dollar. */
2132 pt++;
2134 else
2135 obstack_1grow (&str_obstack, c);
2137 obstack_1grow (&str_obstack, '\0');
2138 rawstr = XOBFINISH (&str_obstack, char *);
2139 str = xstrdup (rawstr);
2140 obstack_free (&str_obstack, NULL);
2141 DBGPRINTF ("matched replacement %s", str);
2142 rawstr = NULL;
2143 return str;
2147 /* An output file, suitable for definitions, that can see declarations
2148 made in INPF and is linked into every language that uses INPF.
2149 Since the result is cached inside INPF, that argument cannot be
2150 declared constant, but is "almost" constant. */
2152 outf_p
2153 get_output_file_with_visibility (input_file *inpf)
2155 outf_p r;
2156 char *for_name = NULL;
2157 char *output_name = NULL;
2158 const char* inpfname;
2160 /* This can happen when we need a file with visibility on a
2161 structure that we've never seen. We have to just hope that it's
2162 globally visible. */
2163 if (inpf == NULL)
2164 inpf = system_h_file;
2166 /* The result is cached in INPF, so return it if already known. */
2167 if (inpf->inpoutf)
2168 return inpf->inpoutf;
2170 /* In plugin mode, return NULL unless the input_file is one of the
2171 plugin_files. */
2172 if (plugin_files)
2174 size_t i;
2175 for (i = 0; i < nb_plugin_files; i++)
2176 if (inpf == plugin_files[i])
2178 inpf->inpoutf = plugin_output;
2179 return plugin_output;
2182 return NULL;
2185 inpfname = get_input_file_name (inpf);
2187 /* Try each rule in sequence in files_rules until one is triggered. */
2189 int rulix = 0;
2190 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2191 (void*) inpf, inpfname);
2193 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2195 DBGPRINTF ("rulix#%d srcexpr %s",
2196 rulix, files_rules[rulix].frul_srcexpr);
2198 if (!files_rules[rulix].frul_re)
2200 /* Compile the regexpr lazily. */
2201 int err = 0;
2202 files_rules[rulix].frul_re = XCNEW (regex_t);
2203 err = regcomp (files_rules[rulix].frul_re,
2204 files_rules[rulix].frul_srcexpr,
2205 files_rules[rulix].frul_rflags);
2206 if (err)
2208 /* The regular expression compilation fails only when
2209 file_rules is buggy. */
2210 gcc_unreachable ();
2214 output_name = NULL;
2215 for_name = NULL;
2217 /* Match the regexpr and trigger the rule if matched. */
2219 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2220 $3, ... $9. */
2221 regmatch_t pmatch[10];
2222 memset (pmatch, 0, sizeof (pmatch));
2223 if (!regexec (files_rules[rulix].frul_re,
2224 inpfname, 10, pmatch, 0))
2226 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2227 (void*) inpf, inpfname, rulix,
2228 files_rules[rulix].frul_srcexpr);
2229 for_name =
2230 matching_file_name_substitute (inpfname, pmatch,
2231 files_rules[rulix].frul_tr_for);
2232 DBGPRINTF ("for_name %s", for_name);
2233 output_name =
2234 matching_file_name_substitute (inpfname, pmatch,
2235 files_rules[rulix].frul_tr_out);
2236 DBGPRINTF ("output_name %s", output_name);
2237 if (files_rules[rulix].frul_action)
2239 /* Invoke our action routine. */
2240 outf_p of = NULL;
2241 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2242 rulix, output_name, for_name);
2243 of =
2244 (files_rules[rulix].frul_action) (inpf,
2245 &output_name, &for_name);
2246 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2247 rulix, (void*)of, output_name, for_name);
2248 /* If the action routine returned something, give it back
2249 immediately and cache it in inpf. */
2250 if (of)
2252 inpf->inpoutf = of;
2253 return of;
2256 /* The rule matched, and had no action, or that action did
2257 not return any output file but could have changed the
2258 output_name or for_name. We break out of the loop on the
2259 files_rules. */
2260 break;
2262 else
2264 /* The regexpr did not match. */
2265 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2266 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2267 continue;
2272 if (!output_name || !for_name)
2274 /* This should not be possible, and could only happen if the
2275 files_rules is incomplete or buggy. */
2276 fatal ("failed to compute output name for %s", inpfname);
2279 /* Look through to see if we've ever seen this output filename
2280 before. If found, cache the result in inpf. */
2281 for (r = output_files; r; r = r->next)
2282 if (filename_cmp (r->name, output_name) == 0)
2284 inpf->inpoutf = r;
2285 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2286 output_name, for_name);
2287 return r;
2290 /* If not found, create it, and cache it in inpf. */
2291 r = create_file (for_name, output_name);
2293 gcc_assert (r && r->name);
2294 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2295 output_name, for_name);
2296 inpf->inpoutf = r;
2297 return r;
2302 /* The name of an output file, suitable for definitions, that can see
2303 declarations made in INPF and is linked into every language that
2304 uses INPF. */
2306 const char *
2307 get_output_file_name (input_file* inpf)
2309 outf_p o = get_output_file_with_visibility (inpf);
2310 if (o)
2311 return o->name;
2312 return NULL;
2315 /* Check if existing file is equal to the in memory buffer. */
2317 static bool
2318 is_file_equal (outf_p of)
2320 FILE *newfile = fopen (of->name, "r");
2321 size_t i;
2322 bool equal;
2323 if (newfile == NULL)
2324 return false;
2326 equal = true;
2327 for (i = 0; i < of->bufused; i++)
2329 int ch;
2330 ch = fgetc (newfile);
2331 if (ch == EOF || ch != (unsigned char) of->buf[i])
2333 equal = false;
2334 break;
2337 if (equal && EOF != fgetc (newfile))
2338 equal = false;
2339 fclose (newfile);
2340 return equal;
2343 /* Copy the output to its final destination,
2344 but don't unnecessarily change modification times. */
2346 static void
2347 close_output_files (void)
2349 int nbwrittenfiles = 0;
2350 outf_p of;
2352 for (of = output_files; of; of = of->next)
2354 if (!is_file_equal (of))
2356 FILE *newfile = NULL;
2357 char *backupname = NULL;
2358 /* Back up the old version of the output file gt-FOO.c as
2359 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2360 if (backup_dir)
2362 backupname = concat (backup_dir, "/",
2363 lbasename (of->name), "~", NULL);
2364 if (!access (of->name, F_OK) && rename (of->name, backupname))
2365 fatal ("failed to back up %s as %s: %s",
2366 of->name, backupname, xstrerror (errno));
2369 newfile = fopen (of->name, "w");
2370 if (newfile == NULL)
2371 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2372 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2373 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2374 if (fclose (newfile) != 0)
2375 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2376 nbwrittenfiles++;
2377 if (verbosity_level >= 2 && backupname)
2378 printf ("%s wrote #%-3d %s backed-up in %s\n",
2379 progname, nbwrittenfiles, of->name, backupname);
2380 else if (verbosity_level >= 1)
2381 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2382 free (backupname);
2384 else
2386 /* output file remains unchanged. */
2387 if (verbosity_level >= 2)
2388 printf ("%s keep %s\n", progname, of->name);
2390 free (of->buf);
2391 of->buf = NULL;
2392 of->bufused = of->buflength = 0;
2394 if (verbosity_level >= 1)
2395 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2398 struct flist
2400 struct flist *next;
2401 int started_p;
2402 const input_file* file;
2403 outf_p f;
2406 struct walk_type_data;
2408 /* For scalars and strings, given the item in 'val'.
2409 For structures, given a pointer to the item in 'val'.
2410 For misc. pointers, given the item in 'val'.
2412 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2413 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2415 /* Parameters for write_types. */
2417 struct write_types_data
2419 const char *prefix;
2420 const char *param_prefix;
2421 const char *subfield_marker_routine;
2422 const char *marker_routine;
2423 const char *reorder_note_routine;
2424 const char *comment;
2425 int skip_hooks; /* skip hook generation if non zero */
2426 enum write_types_kinds kind;
2429 static void output_escaped_param (struct walk_type_data *d,
2430 const char *, const char *);
2431 static void output_mangled_typename (outf_p, const_type_p);
2432 static void walk_type (type_p t, struct walk_type_data *d);
2433 static void write_func_for_structure (type_p orig_s, type_p s,
2434 const struct write_types_data *wtd);
2435 static void write_types_process_field
2436 (type_p f, const struct walk_type_data *d);
2437 static void write_types (outf_p output_header,
2438 type_p structures,
2439 const struct write_types_data *wtd);
2440 static void write_types_local_process_field
2441 (type_p f, const struct walk_type_data *d);
2442 static void write_local_func_for_structure (const_type_p orig_s, type_p s);
2443 static void write_local (outf_p output_header,
2444 type_p structures);
2445 static int contains_scalar_p (type_p t);
2446 static void put_mangled_filename (outf_p, const input_file *);
2447 static void finish_root_table (struct flist *flp, const char *pfx,
2448 const char *tname, const char *lastname,
2449 const char *name);
2450 static void write_root (outf_p, pair_p, type_p, const char *, int,
2451 struct fileloc *, bool);
2452 static void write_array (outf_p f, pair_p v,
2453 const struct write_types_data *wtd);
2454 static void write_roots (pair_p, bool);
2456 /* Parameters for walk_type. */
2458 struct walk_type_data
2460 process_field_fn process_field;
2461 const void *cookie;
2462 outf_p of;
2463 options_p opt;
2464 const char *val;
2465 const char *prev_val[4];
2466 int indent;
2467 int counter;
2468 const struct fileloc *line;
2469 lang_bitmap bitmap;
2470 int used_length;
2471 type_p orig_s;
2472 const char *reorder_fn;
2473 bool needs_cast_p;
2474 bool fn_wants_lvalue;
2475 bool in_record_p;
2476 int loopcounter;
2477 bool in_ptr_field;
2478 bool have_this_obj;
2482 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2483 pre-processor identifier to use in a #define directive. This replaces
2484 special characters used in C++ identifiers like '>', '<' and ':' with
2485 '_'.
2487 If no C++ special characters are found in TYPE_NAME, return
2488 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2489 characters replaced with '_'. In this case, the caller is
2490 responsible for freeing the allocated string. */
2492 static const char *
2493 filter_type_name (const char *type_name)
2495 if (strchr (type_name, '<') || strchr (type_name, ':'))
2497 size_t i;
2498 char *s = xstrdup (type_name);
2499 for (i = 0; i < strlen (s); i++)
2500 if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ','
2501 || s[i] == '*')
2502 s[i] = '_';
2503 return s;
2505 else
2506 return type_name;
2510 /* Print a mangled name representing T to OF. */
2512 static void
2513 output_mangled_typename (outf_p of, const_type_p t)
2515 if (t == NULL)
2516 oprintf (of, "Z");
2517 else
2518 switch (t->kind)
2520 case TYPE_NONE:
2521 case TYPE_UNDEFINED:
2522 gcc_unreachable ();
2523 break;
2524 case TYPE_POINTER:
2525 oprintf (of, "P");
2526 output_mangled_typename (of, t->u.p);
2527 break;
2528 case TYPE_SCALAR:
2529 oprintf (of, "I");
2530 break;
2531 case TYPE_STRING:
2532 oprintf (of, "S");
2533 break;
2534 case TYPE_STRUCT:
2535 case TYPE_UNION:
2536 case TYPE_LANG_STRUCT:
2537 case TYPE_USER_STRUCT:
2539 /* For references to classes within an inheritance hierarchy,
2540 only ever reference the ultimate base class, since only
2541 it will have gt_ functions. */
2542 t = get_ultimate_base_class (t);
2543 const char *id_for_tag = filter_type_name (t->u.s.tag);
2544 oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2545 id_for_tag);
2546 if (id_for_tag != t->u.s.tag)
2547 free (CONST_CAST (char *, id_for_tag));
2549 break;
2550 case TYPE_ARRAY:
2551 gcc_unreachable ();
2555 /* Print PARAM to D->OF processing escapes. D->VAL references the
2556 current object, D->PREV_VAL the object containing the current
2557 object, ONAME is the name of the option and D->LINE is used to
2558 print error messages. */
2560 static void
2561 output_escaped_param (struct walk_type_data *d, const char *param,
2562 const char *oname)
2564 const char *p;
2566 for (p = param; *p; p++)
2567 if (*p != '%')
2568 oprintf (d->of, "%c", *p);
2569 else
2570 switch (*++p)
2572 case 'h':
2573 oprintf (d->of, "(%s)", d->prev_val[2]);
2574 break;
2575 case '0':
2576 oprintf (d->of, "(%s)", d->prev_val[0]);
2577 break;
2578 case '1':
2579 oprintf (d->of, "(%s)", d->prev_val[1]);
2580 break;
2581 case 'a':
2583 const char *pp = d->val + strlen (d->val);
2584 while (pp[-1] == ']')
2585 while (*pp != '[')
2586 pp--;
2587 oprintf (d->of, "%s", pp);
2589 break;
2590 default:
2591 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2592 oname, '%', *p);
2596 const char *
2597 get_string_option (options_p opt, const char *key)
2599 for (; opt; opt = opt->next)
2600 if (strcmp (opt->name, key) == 0)
2601 return opt->info.string;
2602 return NULL;
2605 /* Machinery for avoiding duplicate tags within switch statements. */
2606 struct seen_tag
2608 const char *tag;
2609 struct seen_tag *next;
2613 already_seen_tag (struct seen_tag *seen_tags, const char *tag)
2615 /* Linear search, so O(n^2), but n is currently small. */
2616 while (seen_tags)
2618 if (!strcmp (seen_tags->tag, tag))
2619 return 1;
2620 seen_tags = seen_tags->next;
2622 /* Not yet seen this tag. */
2623 return 0;
2626 void
2627 mark_tag_as_seen (struct seen_tag **seen_tags, const char *tag)
2629 /* Add to front of linked list. */
2630 struct seen_tag *new_node = XCNEW (struct seen_tag);
2631 new_node->tag = tag;
2632 new_node->next = *seen_tags;
2633 *seen_tags = new_node;
2636 static void
2637 walk_subclasses (type_p base, struct walk_type_data *d,
2638 struct seen_tag **seen_tags)
2640 for (type_p sub = base->u.s.first_subclass; sub != NULL;
2641 sub = sub->u.s.next_sibling_class)
2643 const char *type_tag = get_string_option (sub->u.s.opt, "tag");
2644 if (type_tag && !already_seen_tag (*seen_tags, type_tag))
2646 mark_tag_as_seen (seen_tags, type_tag);
2647 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
2648 d->indent += 2;
2649 oprintf (d->of, "%*s{\n", d->indent, "");
2650 d->indent += 2;
2651 oprintf (d->of, "%*s%s *sub = static_cast <%s *> (x);\n",
2652 d->indent, "", sub->u.s.tag, sub->u.s.tag);
2653 const char *old_val = d->val;
2654 d->val = "(*sub)";
2655 walk_type (sub, d);
2656 d->val = old_val;
2657 d->indent -= 2;
2658 oprintf (d->of, "%*s}\n", d->indent, "");
2659 oprintf (d->of, "%*sbreak;\n", d->indent, "");
2660 d->indent -= 2;
2662 walk_subclasses (sub, d, seen_tags);
2666 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2667 which is of type T. Write code to D->OF to constrain execution (at
2668 the point that D->PROCESS_FIELD is called) to the appropriate
2669 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2670 pointers to those objects. D->PREV_VAL lists the objects
2671 containing the current object, D->OPT is a list of options to
2672 apply, D->INDENT is the current indentation level, D->LINE is used
2673 to print error messages, D->BITMAP indicates which languages to
2674 print the structure for. */
2676 static void
2677 walk_type (type_p t, struct walk_type_data *d)
2679 const char *length = NULL;
2680 const char *desc = NULL;
2681 const char *type_tag = NULL;
2682 int maybe_undef_p = 0;
2683 int atomic_p = 0;
2684 options_p oo;
2685 const struct nested_ptr_data *nested_ptr_d = NULL;
2687 d->needs_cast_p = false;
2688 for (oo = d->opt; oo; oo = oo->next)
2689 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2690 length = oo->info.string;
2691 else if (strcmp (oo->name, "maybe_undef") == 0)
2692 maybe_undef_p = 1;
2693 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2694 desc = oo->info.string;
2695 else if (strcmp (oo->name, "mark_hook") == 0)
2697 else if (strcmp (oo->name, "nested_ptr") == 0
2698 && oo->kind == OPTION_NESTED)
2699 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2700 else if (strcmp (oo->name, "dot") == 0)
2702 else if (strcmp (oo->name, "tag") == 0)
2703 type_tag = oo->info.string;
2704 else if (strcmp (oo->name, "special") == 0)
2706 else if (strcmp (oo->name, "skip") == 0)
2708 else if (strcmp (oo->name, "atomic") == 0)
2709 atomic_p = 1;
2710 else if (strcmp (oo->name, "default") == 0)
2712 else if (strcmp (oo->name, "chain_next") == 0)
2714 else if (strcmp (oo->name, "chain_prev") == 0)
2716 else if (strcmp (oo->name, "chain_circular") == 0)
2718 else if (strcmp (oo->name, "reorder") == 0)
2720 else if (strcmp (oo->name, "variable_size") == 0)
2722 else if (strcmp (oo->name, "for_user") == 0)
2724 else
2725 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2727 if (d->used_length)
2728 length = NULL;
2730 if (maybe_undef_p
2731 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
2733 error_at_line (d->line,
2734 "field `%s' has invalid option `maybe_undef_p'\n",
2735 d->val);
2736 return;
2739 if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2741 error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2742 return;
2745 switch (t->kind)
2747 case TYPE_SCALAR:
2748 case TYPE_STRING:
2749 d->process_field (t, d);
2750 break;
2752 case TYPE_POINTER:
2754 d->in_ptr_field = true;
2755 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2757 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2758 break;
2761 /* If a pointer type is marked as "atomic", we process the
2762 field itself, but we don't walk the data that they point to.
2764 There are two main cases where we walk types: to mark
2765 pointers that are reachable, and to relocate pointers when
2766 writing a PCH file. In both cases, an atomic pointer is
2767 itself marked or relocated, but the memory that it points
2768 to is left untouched. In the case of PCH, that memory will
2769 be read/written unchanged to the PCH file. */
2770 if (atomic_p)
2772 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2773 d->indent += 2;
2774 d->process_field (t, d);
2775 d->indent -= 2;
2776 oprintf (d->of, "%*s}\n", d->indent, "");
2777 break;
2780 if (!length)
2782 if (!union_or_struct_p (t->u.p))
2784 error_at_line (d->line,
2785 "field `%s' is pointer to unimplemented type",
2786 d->val);
2787 break;
2790 if (nested_ptr_d)
2792 const char *oldprevval2 = d->prev_val[2];
2794 if (!union_or_struct_p (nested_ptr_d->type))
2796 error_at_line (d->line,
2797 "field `%s' has invalid "
2798 "option `nested_ptr'\n", d->val);
2799 return;
2802 d->prev_val[2] = d->val;
2803 oprintf (d->of, "%*s{\n", d->indent, "");
2804 d->indent += 2;
2805 d->val = xasprintf ("x%d", d->counter++);
2806 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2807 (nested_ptr_d->type->kind == TYPE_UNION
2808 ? "union" : "struct"),
2809 nested_ptr_d->type->u.s.tag,
2810 d->fn_wants_lvalue ? "" : "const ", d->val);
2811 oprintf (d->of, "%*s", d->indent + 2, "");
2812 output_escaped_param (d, nested_ptr_d->convert_from,
2813 "nested_ptr");
2814 oprintf (d->of, ";\n");
2816 d->process_field (nested_ptr_d->type, d);
2818 if (d->fn_wants_lvalue)
2820 oprintf (d->of, "%*s%s = ", d->indent, "",
2821 d->prev_val[2]);
2822 d->prev_val[2] = d->val;
2823 output_escaped_param (d, nested_ptr_d->convert_to,
2824 "nested_ptr");
2825 oprintf (d->of, ";\n");
2828 d->indent -= 2;
2829 oprintf (d->of, "%*s}\n", d->indent, "");
2830 d->val = d->prev_val[2];
2831 d->prev_val[2] = oldprevval2;
2833 else
2834 d->process_field (t->u.p, d);
2836 else
2838 int loopcounter = d->loopcounter;
2839 const char *oldval = d->val;
2840 const char *oldprevval3 = d->prev_val[3];
2841 char *newval;
2843 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2844 d->indent += 2;
2845 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2846 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2847 "", loopcounter, loopcounter);
2848 if (!d->in_record_p)
2849 output_escaped_param (d, length, "length");
2850 else
2851 oprintf (d->of, "l%d", loopcounter);
2852 if (d->have_this_obj)
2853 /* Try to unswitch loops (see PR53880). */
2854 oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
2855 oprintf (d->of, "); i%d++) {\n", loopcounter);
2856 d->indent += 2;
2857 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2858 d->used_length = 1;
2859 d->prev_val[3] = oldval;
2860 walk_type (t->u.p, d);
2861 free (newval);
2862 d->val = oldval;
2863 d->prev_val[3] = oldprevval3;
2864 d->used_length = 0;
2865 d->indent -= 2;
2866 oprintf (d->of, "%*s}\n", d->indent, "");
2867 d->process_field (t, d);
2868 d->indent -= 2;
2869 oprintf (d->of, "%*s}\n", d->indent, "");
2871 d->in_ptr_field = false;
2873 break;
2875 case TYPE_ARRAY:
2877 int loopcounter;
2878 const char *oldval = d->val;
2879 char *newval;
2881 /* If it's an array of scalars, we optimize by not generating
2882 any code. */
2883 if (t->u.a.p->kind == TYPE_SCALAR)
2884 break;
2886 if (length)
2887 loopcounter = d->loopcounter;
2888 else
2889 loopcounter = d->counter++;
2891 /* When walking an array, compute the length and store it in a
2892 local variable before walking the array elements, instead of
2893 recomputing the length expression each time through the loop.
2894 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2895 where the length is stored in the first array element,
2896 because otherwise that operand can get overwritten on the
2897 first iteration. */
2898 oprintf (d->of, "%*s{\n", d->indent, "");
2899 d->indent += 2;
2900 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2901 if (!d->in_record_p || !length)
2903 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2904 d->indent, "", loopcounter);
2905 if (length)
2906 output_escaped_param (d, length, "length");
2907 else
2908 oprintf (d->of, "%s", t->u.a.len);
2909 oprintf (d->of, ");\n");
2912 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2913 d->indent, "",
2914 loopcounter, loopcounter, loopcounter, loopcounter);
2915 d->indent += 2;
2916 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2917 d->used_length = 1;
2918 walk_type (t->u.a.p, d);
2919 free (newval);
2920 d->used_length = 0;
2921 d->val = oldval;
2922 d->indent -= 2;
2923 oprintf (d->of, "%*s}\n", d->indent, "");
2924 d->indent -= 2;
2925 oprintf (d->of, "%*s}\n", d->indent, "");
2927 break;
2929 case TYPE_STRUCT:
2930 case TYPE_UNION:
2932 pair_p f;
2933 const char *oldval = d->val;
2934 const char *oldprevval1 = d->prev_val[1];
2935 const char *oldprevval2 = d->prev_val[2];
2936 const char *struct_mark_hook = NULL;
2937 const int union_p = t->kind == TYPE_UNION;
2938 int seen_default_p = 0;
2939 options_p o;
2940 int lengths_seen = 0;
2941 int endcounter;
2942 bool any_length_seen = false;
2944 if (!t->u.s.line.file)
2945 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
2947 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2949 error_at_line (d->line,
2950 "structure `%s' defined for mismatching languages",
2951 t->u.s.tag);
2952 error_at_line (&t->u.s.line, "one structure defined here");
2955 /* Some things may also be defined in the structure's options. */
2956 for (o = t->u.s.opt; o; o = o->next)
2957 if (!desc && strcmp (o->name, "desc") == 0
2958 && o->kind == OPTION_STRING)
2959 desc = o->info.string;
2960 else if (!struct_mark_hook && strcmp (o->name, "mark_hook") == 0
2961 && o->kind == OPTION_STRING)
2962 struct_mark_hook = o->info.string;
2964 if (struct_mark_hook)
2965 oprintf (d->of, "%*s%s (&%s);\n",
2966 d->indent, "", struct_mark_hook, oldval);
2968 d->prev_val[2] = oldval;
2969 d->prev_val[1] = oldprevval2;
2970 if (union_p)
2972 if (desc == NULL)
2974 error_at_line (d->line,
2975 "missing `desc' option for union `%s'",
2976 t->u.s.tag);
2977 desc = "1";
2979 oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
2980 output_escaped_param (d, desc, "desc");
2981 oprintf (d->of, "))\n");
2982 d->indent += 2;
2983 oprintf (d->of, "%*s{\n", d->indent, "");
2985 else if (desc)
2987 /* We have a "desc" option on a struct, signifying the
2988 base class within a GC-managed inheritance hierarchy.
2989 The current code specialcases the base class, then walks
2990 into subclasses, recursing into this routine to handle them.
2991 This organization requires the base class to have a case in
2992 the switch statement, and hence a tag value is mandatory
2993 for the base class. This restriction could be removed, but
2994 it would require some restructing of this code. */
2995 if (!type_tag)
2997 error_at_line (d->line,
2998 "missing `tag' option for type `%s'",
2999 t->u.s.tag);
3001 oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
3002 output_escaped_param (d, desc, "desc");
3003 oprintf (d->of, "))\n");
3004 d->indent += 2;
3005 oprintf (d->of, "%*s{\n", d->indent, "");
3006 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
3007 d->indent += 2;
3010 FOR_ALL_INHERITED_FIELDS (t, f)
3012 options_p oo;
3013 int skip_p = 0;
3014 const char *fieldlength = NULL;
3016 d->reorder_fn = NULL;
3017 for (oo = f->opt; oo; oo = oo->next)
3018 if (strcmp (oo->name, "skip") == 0)
3019 skip_p = 1;
3020 else if (strcmp (oo->name, "length") == 0
3021 && oo->kind == OPTION_STRING)
3022 fieldlength = oo->info.string;
3024 if (skip_p)
3025 continue;
3026 if (fieldlength)
3028 lengths_seen++;
3029 d->counter++;
3030 if (!union_p)
3032 if (!any_length_seen)
3034 oprintf (d->of, "%*s{\n", d->indent, "");
3035 d->indent += 2;
3037 any_length_seen = true;
3039 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
3040 d->indent, "", d->counter - 1);
3041 output_escaped_param (d, fieldlength, "length");
3042 oprintf (d->of, ");\n");
3046 endcounter = d->counter;
3048 FOR_ALL_INHERITED_FIELDS (t, f)
3050 options_p oo;
3051 const char *dot = ".";
3052 const char *tagid = NULL;
3053 int skip_p = 0;
3054 int default_p = 0;
3055 const char *fieldlength = NULL;
3056 char *newval;
3058 d->reorder_fn = NULL;
3059 for (oo = f->opt; oo; oo = oo->next)
3060 if (strcmp (oo->name, "dot") == 0
3061 && oo->kind == OPTION_STRING)
3062 dot = oo->info.string;
3063 else if (strcmp (oo->name, "tag") == 0
3064 && oo->kind == OPTION_STRING)
3065 tagid = oo->info.string;
3066 else if (strcmp (oo->name, "skip") == 0)
3067 skip_p = 1;
3068 else if (strcmp (oo->name, "default") == 0)
3069 default_p = 1;
3070 else if (strcmp (oo->name, "reorder") == 0
3071 && oo->kind == OPTION_STRING)
3072 d->reorder_fn = oo->info.string;
3073 else if (strcmp (oo->name, "length") == 0
3074 && oo->kind == OPTION_STRING)
3075 fieldlength = oo->info.string;
3077 if (skip_p)
3078 continue;
3080 if (union_p && tagid)
3082 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
3083 d->indent += 2;
3085 else if (union_p && default_p)
3087 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3088 d->indent += 2;
3089 seen_default_p = 1;
3091 else if (!union_p && (default_p || tagid))
3092 error_at_line (d->line,
3093 "can't use `%s' outside a union on field `%s'",
3094 default_p ? "default" : "tag", f->name);
3095 else if (union_p && !(default_p || tagid)
3096 && f->type->kind == TYPE_SCALAR)
3098 fprintf (stderr,
3099 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3100 get_input_file_name (d->line->file), d->line->line,
3101 f->name);
3102 continue;
3104 else if (union_p && !(default_p || tagid))
3105 error_at_line (d->line,
3106 "field `%s' is missing `tag' or `default' option",
3107 f->name);
3109 if (fieldlength)
3111 d->loopcounter = endcounter - lengths_seen--;
3114 d->line = &f->line;
3115 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
3116 d->opt = f->opt;
3117 d->used_length = false;
3118 d->in_record_p = !union_p;
3120 walk_type (f->type, d);
3122 d->in_record_p = false;
3124 free (newval);
3126 if (union_p)
3128 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3129 d->indent -= 2;
3132 d->reorder_fn = NULL;
3134 d->val = oldval;
3135 d->prev_val[1] = oldprevval1;
3136 d->prev_val[2] = oldprevval2;
3138 if (union_p && !seen_default_p)
3140 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3141 oprintf (d->of, "%*s break;\n", d->indent, "");
3144 if (desc && !union_p)
3146 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3147 d->indent -= 2;
3149 if (union_p)
3151 oprintf (d->of, "%*s}\n", d->indent, "");
3152 d->indent -= 2;
3154 else if (desc)
3156 /* Add cases to handle subclasses. */
3157 struct seen_tag *tags = NULL;
3158 walk_subclasses (t, d, &tags);
3160 /* Ensure that if someone forgets a "tag" option that we don't
3161 silent fail to traverse that subclass's fields. */
3162 if (!seen_default_p)
3164 oprintf (d->of, "%*s/* Unrecognized tag value. */\n",
3165 d->indent, "");
3166 oprintf (d->of, "%*sdefault: gcc_unreachable (); \n",
3167 d->indent, "");
3170 /* End of the switch statement */
3171 oprintf (d->of, "%*s}\n", d->indent, "");
3172 d->indent -= 2;
3174 if (any_length_seen)
3176 d->indent -= 2;
3177 oprintf (d->of, "%*s}\n", d->indent, "");
3180 break;
3182 case TYPE_LANG_STRUCT:
3184 type_p nt;
3185 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3186 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3187 break;
3188 if (nt == NULL)
3189 error_at_line (d->line, "structure `%s' differs between languages",
3190 t->u.s.tag);
3191 else
3192 walk_type (nt, d);
3194 break;
3196 case TYPE_USER_STRUCT:
3197 d->process_field (t, d);
3198 break;
3200 case TYPE_NONE:
3201 case TYPE_UNDEFINED:
3202 gcc_unreachable ();
3206 /* process_field routine for marking routines. */
3208 static void
3209 write_types_process_field (type_p f, const struct walk_type_data *d)
3211 const struct write_types_data *wtd;
3212 const char *cast = d->needs_cast_p ? "(void *)" : "";
3213 wtd = (const struct write_types_data *) d->cookie;
3215 switch (f->kind)
3217 case TYPE_NONE:
3218 case TYPE_UNDEFINED:
3219 gcc_unreachable ();
3220 case TYPE_POINTER:
3221 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
3222 wtd->subfield_marker_routine, cast, d->val);
3223 if (wtd->param_prefix)
3225 if (f->u.p->kind == TYPE_SCALAR)
3226 /* The current type is a pointer to a scalar (so not
3227 considered like a pointer to instances of user defined
3228 types) and we are seeing it; it means we must be even
3229 more careful about the second argument of the
3230 SUBFIELD_MARKER_ROUTINE call. That argument must
3231 always be the instance of the type for which
3232 write_func_for_structure was called - this really is
3233 what the function SUBFIELD_MARKER_ROUTINE expects.
3234 That is, it must be an instance of the ORIG_S type
3235 parameter of write_func_for_structure. The convention
3236 is that that argument must be "x" in that case (as set
3237 by write_func_for_structure). The problem is, we can't
3238 count on d->prev_val[3] to be always set to "x" in that
3239 case. Sometimes walk_type can set it to something else
3240 (to e.g cooperate with write_array when called from
3241 write_roots). So let's set it to "x" here then. */
3242 oprintf (d->of, ", x");
3243 else
3244 oprintf (d->of, ", %s", d->prev_val[3]);
3245 if (d->orig_s)
3247 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3248 output_mangled_typename (d->of, d->orig_s);
3250 else
3251 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3253 oprintf (d->of, ");\n");
3254 if (d->reorder_fn && wtd->reorder_note_routine)
3255 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
3256 wtd->reorder_note_routine, cast, d->val,
3257 d->prev_val[3], d->reorder_fn);
3258 break;
3260 case TYPE_STRING:
3261 case TYPE_STRUCT:
3262 case TYPE_UNION:
3263 case TYPE_LANG_STRUCT:
3264 case TYPE_USER_STRUCT:
3265 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3267 /* If F is a user-defined type and the field is not a
3268 pointer to the type, then we should not generate the
3269 standard pointer-marking code. All we need to do is call
3270 the user-provided marking function to process the fields
3271 of F. */
3272 oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3273 d->val);
3275 else
3277 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3278 output_mangled_typename (d->of, f);
3279 oprintf (d->of, " (%s%s);\n", cast, d->val);
3280 if (d->reorder_fn && wtd->reorder_note_routine)
3281 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
3282 wtd->reorder_note_routine, cast, d->val, cast, d->val,
3283 d->reorder_fn);
3285 break;
3287 case TYPE_SCALAR:
3288 break;
3290 case TYPE_ARRAY:
3291 gcc_unreachable ();
3295 /* Return an output file that is suitable for definitions which can
3296 reference struct S */
3298 static outf_p
3299 get_output_file_for_structure (const_type_p s)
3301 const input_file *fn;
3303 gcc_assert (union_or_struct_p (s));
3304 fn = s->u.s.line.file;
3306 /* The call to get_output_file_with_visibility may update fn by
3307 caching its result inside, so we need the CONST_CAST. */
3308 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3312 /* Returns the specifier keyword for a string or union type S, empty string
3313 otherwise. */
3315 static const char *
3316 get_type_specifier (const type_p s)
3318 if (s->kind == TYPE_STRUCT)
3319 return "struct ";
3320 else if (s->kind == TYPE_LANG_STRUCT)
3321 return get_type_specifier (s->u.s.lang_struct);
3322 else if (s->kind == TYPE_UNION)
3323 return "union ";
3324 return "";
3328 /* Emits a declaration for type TY (assumed to be a union or a
3329 structure) on stream OUT. */
3331 static void
3332 write_type_decl (outf_p out, type_p ty)
3334 if (union_or_struct_p (ty))
3335 oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3336 else if (ty->kind == TYPE_SCALAR)
3338 if (ty->u.scalar_is_char)
3339 oprintf (out, "const char");
3340 else
3341 oprintf (out, "void");
3343 else if (ty->kind == TYPE_POINTER)
3345 write_type_decl (out, ty->u.p);
3346 oprintf (out, " *");
3348 else if (ty->kind == TYPE_ARRAY)
3350 write_type_decl (out, ty->u.a.p);
3351 oprintf (out, " *");
3353 else if (ty->kind == TYPE_STRING)
3355 oprintf (out, "const char *");
3357 else
3358 gcc_unreachable ();
3362 /* Write on OF the name of the marker function for structure S. PREFIX
3363 is the prefix to use (to distinguish ggc from pch markers). */
3365 static void
3366 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3368 if (union_or_struct_p (s))
3370 const char *id_for_tag = filter_type_name (s->u.s.tag);
3371 oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3372 if (id_for_tag != s->u.s.tag)
3373 free (CONST_CAST (char *, id_for_tag));
3375 else
3376 gcc_unreachable ();
3379 /* Write on OF a user-callable routine to act as an entry point for
3380 the marking routine for S, generated by write_func_for_structure.
3381 WTD distinguishes between ggc and pch markers. */
3383 static void
3384 write_user_func_for_structure_ptr (outf_p of, type_p s, const write_types_data *wtd)
3386 gcc_assert (union_or_struct_p (s));
3388 type_p alias_of = NULL;
3389 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3390 if (strcmp (opt->name, "ptr_alias") == 0)
3392 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3393 we do not generate marking code for ORIG_S here. Instead, a
3394 forwarder #define in gtype-desc.h will cause every call to its
3395 marker to call the target of this alias.
3397 However, we still want to create a user entry code for the
3398 aliased type. So, if ALIAS_OF is set, we only generate the
3399 user-callable marker function. */
3400 alias_of = opt->info.type;
3401 break;
3404 DBGPRINTF ("write_user_func_for_structure_ptr: %s %s", s->u.s.tag,
3405 wtd->prefix);
3407 /* Only write the function once. */
3408 if (s->u.s.wrote_user_func_for_ptr[wtd->kind])
3409 return;
3410 s->u.s.wrote_user_func_for_ptr[wtd->kind] = true;
3412 oprintf (of, "\nvoid\n");
3413 oprintf (of, "gt_%sx (", wtd->prefix);
3414 write_type_decl (of, s);
3415 oprintf (of, " *& x)\n");
3416 oprintf (of, "{\n");
3417 oprintf (of, " if (x)\n ");
3418 write_marker_function_name (of,
3419 alias_of ? alias_of : get_ultimate_base_class (s),
3420 wtd->prefix);
3421 oprintf (of, " ((void *) x);\n");
3422 oprintf (of, "}\n");
3426 /* Write a function to mark all the fields of type S on OF. PREFIX
3427 and D are as in write_user_marking_functions. */
3429 static void
3430 write_user_func_for_structure_body (type_p s, const char *prefix,
3431 struct walk_type_data *d)
3433 oprintf (d->of, "\nvoid\n");
3434 oprintf (d->of, "gt_%sx (", prefix);
3435 write_type_decl (d->of, s);
3436 oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3437 oprintf (d->of, "{\n");
3438 oprintf (d->of, " ");
3439 write_type_decl (d->of, s);
3440 oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3441 d->val = "(*x)";
3442 d->indent = 2;
3443 walk_type (s, d);
3444 oprintf (d->of, "}\n");
3447 /* Emit the user-callable functions needed to mark all the types used
3448 by the user structure S. PREFIX is the prefix to use to
3449 distinguish ggc and pch markers. D contains data needed to pass to
3450 walk_type when traversing the fields of a type.
3452 For every type T referenced by S, two routines are generated: one
3453 that takes 'T *', marks the pointer and calls the second routine,
3454 which just marks the fields of T. */
3456 static void
3457 write_user_marking_functions (type_p s,
3458 const write_types_data *w,
3459 struct walk_type_data *d)
3461 gcc_assert (s->kind == TYPE_USER_STRUCT);
3463 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3465 type_p fld_type = fld->type;
3466 if (fld_type->kind == TYPE_POINTER)
3468 type_p pointed_to_type = fld_type->u.p;
3469 if (union_or_struct_p (pointed_to_type))
3470 write_user_func_for_structure_ptr (d->of, pointed_to_type, w);
3472 else if (union_or_struct_p (fld_type))
3473 write_user_func_for_structure_body (fld_type, w->prefix, d);
3478 /* For S, a structure that's part of ORIG_S write out a routine that:
3479 - Takes a parameter, a void * but actually of type *S
3480 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3481 field of S or its substructures and (in some cases) things
3482 that are pointed to by S. */
3484 static void
3485 write_func_for_structure (type_p orig_s, type_p s,
3486 const struct write_types_data *wtd)
3488 const char *chain_next = NULL;
3489 const char *chain_prev = NULL;
3490 const char *chain_circular = NULL;
3491 const char *mark_hook_name = NULL;
3492 options_p opt;
3493 struct walk_type_data d;
3495 if (s->u.s.base_class)
3497 /* Verify that the base class has a "desc", since otherwise
3498 the traversal hooks there won't attempt to visit fields of
3499 subclasses such as this one. */
3500 const_type_p ubc = get_ultimate_base_class (s);
3501 if ((!opts_have (ubc->u.s.opt, "user")
3502 && !opts_have (ubc->u.s.opt, "desc")))
3503 error_at_line (&s->u.s.line,
3504 ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
3505 ", but '%s' lacks a discriminator 'desc' option"),
3506 s->u.s.tag, ubc->u.s.tag, ubc->u.s.tag);
3508 /* Don't write fns for subclasses, only for the ultimate base class
3509 within an inheritance hierarchy. */
3510 return;
3513 memset (&d, 0, sizeof (d));
3514 d.of = get_output_file_for_structure (s);
3516 bool for_user = false;
3517 for (opt = s->u.s.opt; opt; opt = opt->next)
3518 if (strcmp (opt->name, "chain_next") == 0
3519 && opt->kind == OPTION_STRING)
3520 chain_next = opt->info.string;
3521 else if (strcmp (opt->name, "chain_prev") == 0
3522 && opt->kind == OPTION_STRING)
3523 chain_prev = opt->info.string;
3524 else if (strcmp (opt->name, "chain_circular") == 0
3525 && opt->kind == OPTION_STRING)
3526 chain_circular = opt->info.string;
3527 else if (strcmp (opt->name, "mark_hook") == 0
3528 && opt->kind == OPTION_STRING)
3529 mark_hook_name = opt->info.string;
3530 else if (strcmp (opt->name, "for_user") == 0)
3531 for_user = true;
3532 if (chain_prev != NULL && chain_next == NULL)
3533 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3534 if (chain_circular != NULL && chain_next != NULL)
3535 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3536 if (chain_circular != NULL)
3537 chain_next = chain_circular;
3539 d.process_field = write_types_process_field;
3540 d.cookie = wtd;
3541 d.orig_s = orig_s;
3542 d.opt = s->u.s.opt;
3543 d.line = &s->u.s.line;
3544 d.bitmap = s->u.s.bitmap;
3545 d.prev_val[0] = "*x";
3546 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3547 d.prev_val[3] = "x";
3548 d.val = "(*x)";
3549 d.have_this_obj = false;
3551 oprintf (d.of, "\n");
3552 oprintf (d.of, "void\n");
3553 write_marker_function_name (d.of, orig_s, wtd->prefix);
3554 oprintf (d.of, " (void *x_p)\n");
3555 oprintf (d.of, "{\n ");
3556 write_type_decl (d.of, s);
3557 oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3558 write_type_decl (d.of, s);
3559 oprintf (d.of, " *)x_p;\n");
3560 if (chain_next != NULL)
3562 /* TYPE_USER_STRUCTs should not occur here. These structures
3563 are completely handled by user code. */
3564 gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
3566 oprintf (d.of, " ");
3567 write_type_decl (d.of, s);
3568 oprintf (d.of, " * xlimit = x;\n");
3570 if (chain_next == NULL)
3572 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3573 if (wtd->param_prefix)
3575 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3576 output_mangled_typename (d.of, orig_s);
3578 oprintf (d.of, "))\n");
3580 else
3582 if (chain_circular != NULL)
3583 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3584 else
3585 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3586 if (wtd->param_prefix)
3588 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3589 output_mangled_typename (d.of, orig_s);
3591 oprintf (d.of, "))\n");
3592 if (chain_circular != NULL)
3593 oprintf (d.of, " return;\n do\n");
3594 if (mark_hook_name && !wtd->skip_hooks)
3596 oprintf (d.of, " {\n");
3597 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
3599 oprintf (d.of, " xlimit = (");
3600 d.prev_val[2] = "*xlimit";
3601 output_escaped_param (&d, chain_next, "chain_next");
3602 oprintf (d.of, ");\n");
3603 if (mark_hook_name && !wtd->skip_hooks)
3604 oprintf (d.of, " }\n");
3605 if (chain_prev != NULL)
3607 oprintf (d.of, " if (x != xlimit)\n");
3608 oprintf (d.of, " for (;;)\n");
3609 oprintf (d.of, " {\n");
3610 oprintf (d.of, " %s %s * const xprev = (",
3611 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3613 d.prev_val[2] = "*x";
3614 output_escaped_param (&d, chain_prev, "chain_prev");
3615 oprintf (d.of, ");\n");
3616 oprintf (d.of, " if (xprev == NULL) break;\n");
3617 oprintf (d.of, " x = xprev;\n");
3618 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3619 if (wtd->param_prefix)
3621 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3622 output_mangled_typename (d.of, orig_s);
3624 oprintf (d.of, ");\n");
3625 oprintf (d.of, " }\n");
3627 if (chain_circular != NULL)
3629 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3630 if (wtd->param_prefix)
3632 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3633 output_mangled_typename (d.of, orig_s);
3635 oprintf (d.of, "));\n");
3636 if (mark_hook_name && !wtd->skip_hooks)
3637 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
3638 oprintf (d.of, " do\n");
3640 else
3641 oprintf (d.of, " while (x != xlimit)\n");
3643 oprintf (d.of, " {\n");
3644 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3646 oprintf (d.of, " %s (x);\n", mark_hook_name);
3649 d.prev_val[2] = "*x";
3650 d.indent = 6;
3651 if (orig_s->kind != TYPE_USER_STRUCT)
3652 walk_type (s, &d);
3653 else
3655 /* User structures have no fields to walk. Simply generate a call
3656 to the user-provided structure marker. */
3657 oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3660 if (chain_next != NULL)
3662 oprintf (d.of, " x = (");
3663 output_escaped_param (&d, chain_next, "chain_next");
3664 oprintf (d.of, ");\n");
3667 oprintf (d.of, " }\n");
3668 if (chain_circular != NULL)
3669 oprintf (d.of, " while (x != xlimit);\n");
3670 oprintf (d.of, "}\n");
3672 if (orig_s->kind == TYPE_USER_STRUCT)
3673 write_user_marking_functions (orig_s, wtd, &d);
3675 if (for_user)
3677 write_user_func_for_structure_body (orig_s, wtd->prefix, &d);
3678 write_user_func_for_structure_ptr (d.of, orig_s, wtd);
3683 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3685 static void
3686 write_types (outf_p output_header, type_p structures,
3687 const struct write_types_data *wtd)
3689 int nbfun = 0; /* Count the emitted functions. */
3690 type_p s;
3692 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3694 /* We first emit the macros and the declarations. Functions' code is
3695 emitted afterwards. This is needed in plugin mode. */
3696 oprintf (output_header, "/* Macros and declarations. */\n");
3697 for (s = structures; s; s = s->next)
3698 /* Do not emit handlers for derived classes; we only ever deal with
3699 the ultimate base class within an inheritance hierarchy. */
3700 if ((s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3701 && !s->u.s.base_class)
3703 options_p opt;
3705 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3706 continue;
3708 const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3710 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3711 output_mangled_typename (output_header, s);
3712 oprintf (output_header, "(X) do { \\\n");
3713 oprintf (output_header,
3714 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3715 s_id_for_tag);
3716 oprintf (output_header, " } while (0)\n");
3718 for (opt = s->u.s.opt; opt; opt = opt->next)
3719 if (strcmp (opt->name, "ptr_alias") == 0
3720 && opt->kind == OPTION_TYPE)
3722 const_type_p const t = (const_type_p) opt->info.type;
3723 if (t->kind == TYPE_STRUCT
3724 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3726 const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3727 oprintf (output_header,
3728 "#define gt_%sx_%s gt_%sx_%s\n",
3729 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3730 if (t_id_for_tag != t->u.s.tag)
3731 free (CONST_CAST (char *, t_id_for_tag));
3733 else
3734 error_at_line (&s->u.s.line,
3735 "structure alias is not a structure");
3736 break;
3738 if (opt)
3739 continue;
3741 /* Declare the marker procedure only once. */
3742 oprintf (output_header,
3743 "extern void gt_%sx_%s (void *);\n",
3744 wtd->prefix, s_id_for_tag);
3746 if (s_id_for_tag != s->u.s.tag)
3747 free (CONST_CAST (char *, s_id_for_tag));
3749 if (s->u.s.line.file == NULL)
3751 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3752 s->u.s.tag);
3753 continue;
3757 /* At last we emit the functions code. */
3758 oprintf (output_header, "\n/* functions code */\n");
3759 for (s = structures; s; s = s->next)
3760 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3762 options_p opt;
3764 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3765 continue;
3766 for (opt = s->u.s.opt; opt; opt = opt->next)
3767 if (strcmp (opt->name, "ptr_alias") == 0)
3768 break;
3769 if (opt)
3770 continue;
3772 if (s->kind == TYPE_LANG_STRUCT)
3774 type_p ss;
3775 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3777 nbfun++;
3778 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3779 nbfun, (void*) ss, ss->u.s.tag);
3780 write_func_for_structure (s, ss, wtd);
3783 else
3785 nbfun++;
3786 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3787 nbfun, (void*) s, s->u.s.tag);
3788 write_func_for_structure (s, s, wtd);
3791 else
3793 /* Structure s is not possibly pointed to, so can be ignored. */
3794 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3795 (void*)s, s->u.s.tag,
3796 (int) s->gc_used);
3799 if (verbosity_level >= 2)
3800 printf ("%s emitted %d routines for %s\n",
3801 progname, nbfun, wtd->comment);
3804 static const struct write_types_data ggc_wtd = {
3805 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
3806 "GC marker procedures. ",
3807 FALSE, WTK_GGC
3810 static const struct write_types_data pch_wtd = {
3811 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3812 "gt_pch_note_reorder",
3813 "PCH type-walking procedures. ",
3814 TRUE, WTK_PCH
3817 /* Write out the local pointer-walking routines. */
3819 /* process_field routine for local pointer-walking for user-callable
3820 routines. The difference between this and
3821 write_types_local_process_field is that, in this case, we do not
3822 need to check whether the given pointer matches the address of the
3823 parent structure. This check was already generated by the call
3824 to gt_pch_nx in the main gt_pch_p_*() function that is calling
3825 this code. */
3827 static void
3828 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
3830 switch (f->kind)
3832 case TYPE_POINTER:
3833 case TYPE_STRUCT:
3834 case TYPE_UNION:
3835 case TYPE_LANG_STRUCT:
3836 case TYPE_STRING:
3837 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3838 break;
3840 case TYPE_USER_STRUCT:
3841 if (d->in_ptr_field)
3842 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3843 else
3844 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3845 d->indent, "", d->val);
3846 break;
3848 case TYPE_SCALAR:
3849 break;
3851 case TYPE_ARRAY:
3852 case TYPE_NONE:
3853 case TYPE_UNDEFINED:
3854 gcc_unreachable ();
3859 /* Write a function to PCH walk all the fields of type S on OF.
3860 D contains data needed by walk_type to recurse into the fields of S. */
3862 static void
3863 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
3865 oprintf (d->of, "\nvoid\n");
3866 oprintf (d->of, "gt_pch_nx (");
3867 write_type_decl (d->of, s);
3868 oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
3869 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3870 "\tATTRIBUTE_UNUSED void *cookie)\n");
3871 oprintf (d->of, "{\n");
3872 d->val = "(*x)";
3873 d->indent = 2;
3874 d->process_field = write_types_local_user_process_field;
3875 walk_type (s, d);
3876 oprintf (d->of, "}\n");
3880 /* Emit the user-callable functions needed to mark all the types used
3881 by the user structure S. PREFIX is the prefix to use to
3882 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
3883 chain_next option defined. D contains data needed to pass to
3884 walk_type when traversing the fields of a type.
3886 For every type T referenced by S, two routines are generated: one
3887 that takes 'T *', marks the pointer and calls the second routine,
3888 which just marks the fields of T. */
3890 static void
3891 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
3893 gcc_assert (s->kind == TYPE_USER_STRUCT);
3895 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3897 type_p fld_type = fld->type;
3898 if (union_or_struct_p (fld_type))
3899 write_pch_user_walking_for_structure_body (fld_type, d);
3904 /* process_field routine for local pointer-walking. */
3906 static void
3907 write_types_local_process_field (type_p f, const struct walk_type_data *d)
3909 gcc_assert (d->have_this_obj);
3910 switch (f->kind)
3912 case TYPE_POINTER:
3913 case TYPE_STRUCT:
3914 case TYPE_UNION:
3915 case TYPE_LANG_STRUCT:
3916 case TYPE_STRING:
3917 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3918 d->prev_val[3]);
3919 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3920 break;
3922 case TYPE_USER_STRUCT:
3923 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3924 d->prev_val[3]);
3925 if (d->in_ptr_field)
3926 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3927 else
3928 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3929 d->indent, "", d->val);
3930 break;
3932 case TYPE_SCALAR:
3933 break;
3935 case TYPE_ARRAY:
3936 case TYPE_NONE:
3937 case TYPE_UNDEFINED:
3938 gcc_unreachable ();
3943 /* For S, a structure that's part of ORIG_S, and using parameters
3944 PARAM, write out a routine that:
3945 - Is of type gt_note_pointers
3946 - Calls PROCESS_FIELD on each field of S or its substructures.
3949 static void
3950 write_local_func_for_structure (const_type_p orig_s, type_p s)
3952 struct walk_type_data d;
3954 /* Don't write fns for subclasses, only for the ultimate base class
3955 within an inheritance hierarchy. */
3956 if (s->u.s.base_class)
3957 return;
3959 memset (&d, 0, sizeof (d));
3960 d.of = get_output_file_for_structure (s);
3961 d.process_field = write_types_local_process_field;
3962 d.opt = s->u.s.opt;
3963 d.line = &s->u.s.line;
3964 d.bitmap = s->u.s.bitmap;
3965 d.prev_val[0] = d.prev_val[2] = "*x";
3966 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3967 d.prev_val[3] = "x";
3968 d.val = "(*x)";
3969 d.fn_wants_lvalue = true;
3971 oprintf (d.of, "\n");
3972 oprintf (d.of, "void\n");
3973 oprintf (d.of, "gt_pch_p_");
3974 output_mangled_typename (d.of, orig_s);
3975 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3976 "\tvoid *x_p,\n"
3977 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3978 "\tATTRIBUTE_UNUSED void *cookie)\n");
3979 oprintf (d.of, "{\n");
3980 oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3981 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3982 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3983 d.indent = 2;
3984 d.have_this_obj = true;
3986 if (s->kind != TYPE_USER_STRUCT)
3987 walk_type (s, &d);
3988 else
3990 /* User structures have no fields to walk. Simply generate a
3991 call to the user-provided PCH walker. */
3992 oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
3993 d.prev_val[3]);
3994 oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3995 d.indent, "", d.val);
3998 oprintf (d.of, "}\n");
4000 /* Write user-callable entry points for the PCH walking routines. */
4001 if (orig_s->kind == TYPE_USER_STRUCT)
4002 write_pch_user_walking_functions (s, &d);
4004 for (options_p o = s->u.s.opt; o; o = o->next)
4005 if (strcmp (o->name, "for_user") == 0)
4007 write_pch_user_walking_for_structure_body (s, &d);
4008 break;
4012 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
4014 static void
4015 write_local (outf_p output_header, type_p structures)
4017 type_p s;
4019 if (!output_header)
4020 return;
4022 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
4023 for (s = structures; s; s = s->next)
4024 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
4026 options_p opt;
4028 if (s->u.s.line.file == NULL)
4029 continue;
4030 for (opt = s->u.s.opt; opt; opt = opt->next)
4031 if (strcmp (opt->name, "ptr_alias") == 0
4032 && opt->kind == OPTION_TYPE)
4034 const_type_p const t = (const_type_p) opt->info.type;
4035 if (t->kind == TYPE_STRUCT
4036 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
4038 oprintf (output_header, "#define gt_pch_p_");
4039 output_mangled_typename (output_header, s);
4040 oprintf (output_header, " gt_pch_p_");
4041 output_mangled_typename (output_header, t);
4042 oprintf (output_header, "\n");
4044 else
4045 error_at_line (&s->u.s.line,
4046 "structure alias is not a structure");
4047 break;
4049 if (opt)
4050 continue;
4052 /* Declare the marker procedure only once. */
4053 oprintf (output_header, "extern void gt_pch_p_");
4054 output_mangled_typename (output_header, s);
4055 oprintf (output_header,
4056 "\n (void *, void *, gt_pointer_operator, void *);\n");
4058 if (s->kind == TYPE_LANG_STRUCT)
4060 type_p ss;
4061 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4062 write_local_func_for_structure (s, ss);
4064 else
4065 write_local_func_for_structure (s, s);
4069 /* Nonzero if S is a type for which typed GC allocators should be output. */
4071 #define USED_BY_TYPED_GC_P(s) \
4072 ((s->kind == TYPE_POINTER \
4073 && (s->u.p->gc_used == GC_POINTED_TO \
4074 || s->u.p->gc_used == GC_USED)) \
4075 || (union_or_struct_p (s) \
4076 && ((s)->gc_used == GC_POINTED_TO \
4077 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4078 && s->u.s.line.file != NULL) \
4079 || ((s)->gc_used == GC_USED \
4080 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))) \
4081 || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
4085 /* Might T contain any non-pointer elements? */
4087 static int
4088 contains_scalar_p (type_p t)
4090 switch (t->kind)
4092 case TYPE_STRING:
4093 case TYPE_POINTER:
4094 return 0;
4095 case TYPE_ARRAY:
4096 return contains_scalar_p (t->u.a.p);
4097 case TYPE_USER_STRUCT:
4098 /* User-marked structures will typically contain pointers. */
4099 return 0;
4100 default:
4101 /* Could also check for structures that have no non-pointer
4102 fields, but there aren't enough of those to worry about. */
4103 return 1;
4107 /* Mangle INPF and print it to F. */
4109 static void
4110 put_mangled_filename (outf_p f, const input_file *inpf)
4112 /* The call to get_output_file_name may indirectly update fn since
4113 get_output_file_with_visibility caches its result inside, so we
4114 need the CONST_CAST. */
4115 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4116 if (!f || !name)
4117 return;
4118 for (; *name != 0; name++)
4119 if (ISALNUM (*name))
4120 oprintf (f, "%c", *name);
4121 else
4122 oprintf (f, "%c", '_');
4125 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4126 LASTNAME, and NAME are all strings to insert in various places in
4127 the resulting code. */
4129 static void
4130 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4131 const char *tname, const char *name)
4133 struct flist *fli2;
4135 for (fli2 = flp; fli2; fli2 = fli2->next)
4136 if (fli2->started_p)
4138 oprintf (fli2->f, " %s\n", lastname);
4139 oprintf (fli2->f, "};\n\n");
4142 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4143 if (fli2->started_p)
4145 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4146 int fnum;
4148 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4149 if (bitmap & 1)
4151 oprintf (base_files[fnum],
4152 "extern const struct %s gt_%s_", tname, pfx);
4153 put_mangled_filename (base_files[fnum], fli2->file);
4154 oprintf (base_files[fnum], "[];\n");
4159 size_t fnum;
4160 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4161 oprintf (base_files[fnum],
4162 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4166 for (fli2 = flp; fli2; fli2 = fli2->next)
4167 if (fli2->started_p)
4169 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4170 int fnum;
4172 fli2->started_p = 0;
4174 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4175 if (bitmap & 1)
4177 oprintf (base_files[fnum], " gt_%s_", pfx);
4178 put_mangled_filename (base_files[fnum], fli2->file);
4179 oprintf (base_files[fnum], ",\n");
4184 size_t fnum;
4185 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4187 oprintf (base_files[fnum], " NULL\n");
4188 oprintf (base_files[fnum], "};\n");
4193 /* Finish off the created gt_clear_caches_file_c functions. */
4195 static void
4196 finish_cache_funcs (flist *flp)
4198 struct flist *fli2;
4200 for (fli2 = flp; fli2; fli2 = fli2->next)
4201 if (fli2->started_p)
4203 oprintf (fli2->f, "}\n\n");
4206 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4207 if (fli2->started_p)
4209 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4210 int fnum;
4212 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4213 if (bitmap & 1)
4215 oprintf (base_files[fnum], "extern void gt_clear_caches_");
4216 put_mangled_filename (base_files[fnum], fli2->file);
4217 oprintf (base_files[fnum], " ();\n");
4221 for (size_t fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4222 oprintf (base_files[fnum], "void\ngt_clear_caches ()\n{\n");
4224 for (fli2 = flp; fli2; fli2 = fli2->next)
4225 if (fli2->started_p)
4227 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4228 int fnum;
4230 fli2->started_p = 0;
4232 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4233 if (bitmap & 1)
4235 oprintf (base_files[fnum], " gt_clear_caches_");
4236 put_mangled_filename (base_files[fnum], fli2->file);
4237 oprintf (base_files[fnum], " ();\n");
4241 for (size_t fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4243 oprintf (base_files[fnum], "}\n");
4247 /* Write the first three fields (pointer, count and stride) for
4248 root NAME to F. V and LINE are as for write_root.
4250 Return true if the entry could be written; return false on error. */
4252 static bool
4253 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4255 type_p ap;
4257 if (!v)
4259 error_at_line (line, "`%s' is too complex to be a root", name);
4260 return false;
4263 oprintf (f, " {\n");
4264 oprintf (f, " &%s,\n", name);
4265 oprintf (f, " 1");
4267 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4268 if (ap->u.a.len[0])
4269 oprintf (f, " * (%s)", ap->u.a.len);
4270 else if (ap == v->type)
4271 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4272 oprintf (f, ",\n");
4273 oprintf (f, " sizeof (%s", v->name);
4274 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4275 oprintf (f, "[0]");
4276 oprintf (f, "),\n");
4277 return true;
4280 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4281 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4282 of the caller. */
4284 static void
4285 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4286 int has_length, struct fileloc *line,
4287 bool emit_pch, type_p field_type, const char *field_name)
4289 struct pair newv;
4290 /* If the field reference is relative to V, rather than to some
4291 subcomponent of V, we can mark any subarrays with a single stride.
4292 We're effectively treating the field as a global variable in its
4293 own right. */
4294 if (v && type == v->type)
4296 newv = *v;
4297 newv.type = field_type;
4298 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4299 v = &newv;
4301 /* Otherwise, any arrays nested in the structure are too complex to
4302 handle. */
4303 else if (field_type->kind == TYPE_ARRAY)
4304 v = NULL;
4305 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4306 has_length, line, emit_pch);
4309 /* Write out to F the table entry and any marker routines needed to
4310 mark NAME as TYPE. V can be one of three values:
4312 - null, if NAME is too complex to represent using a single
4313 count and stride. In this case, it is an error for NAME to
4314 contain any gc-ed data.
4316 - the outermost array that contains NAME, if NAME is part of an array.
4318 - the C variable that contains NAME, if NAME is not part of an array.
4320 LINE is the line of the C source that declares the root variable.
4321 HAS_LENGTH is nonzero iff V was a variable-length array. */
4323 static void
4324 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4325 struct fileloc *line, bool emit_pch)
4327 switch (type->kind)
4329 case TYPE_STRUCT:
4331 pair_p fld;
4332 for (fld = type->u.s.fields; fld; fld = fld->next)
4334 int skip_p = 0;
4335 const char *desc = NULL;
4336 options_p o;
4338 for (o = fld->opt; o; o = o->next)
4339 if (strcmp (o->name, "skip") == 0)
4340 skip_p = 1;
4341 else if (strcmp (o->name, "desc") == 0
4342 && o->kind == OPTION_STRING)
4343 desc = o->info.string;
4344 else
4345 error_at_line (line,
4346 "field `%s' of global `%s' has unknown option `%s'",
4347 fld->name, name, o->name);
4349 if (skip_p)
4350 continue;
4351 else if (desc && fld->type->kind == TYPE_UNION)
4353 pair_p validf = NULL;
4354 pair_p ufld;
4356 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4358 const char *tag = NULL;
4359 options_p oo;
4360 for (oo = ufld->opt; oo; oo = oo->next)
4361 if (strcmp (oo->name, "tag") == 0
4362 && oo->kind == OPTION_STRING)
4363 tag = oo->info.string;
4364 if (tag == NULL || strcmp (tag, desc) != 0)
4365 continue;
4366 if (validf != NULL)
4367 error_at_line (line,
4368 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4369 name, fld->name, validf->name,
4370 name, fld->name, ufld->name, tag);
4371 validf = ufld;
4373 if (validf != NULL)
4374 write_field_root (f, v, type, name, 0, line, emit_pch,
4375 validf->type,
4376 ACONCAT ((fld->name, ".",
4377 validf->name, NULL)));
4379 else if (desc)
4380 error_at_line (line,
4381 "global `%s.%s' has `desc' option but is not union",
4382 name, fld->name);
4383 else
4384 write_field_root (f, v, type, name, 0, line, emit_pch, fld->type,
4385 fld->name);
4388 break;
4390 case TYPE_ARRAY:
4392 char *newname;
4393 newname = xasprintf ("%s[0]", name);
4394 write_root (f, v, type->u.a.p, newname, has_length, line, emit_pch);
4395 free (newname);
4397 break;
4399 case TYPE_USER_STRUCT:
4400 error_at_line (line, "`%s' must be a pointer type, because it is "
4401 "a GC root and its type is marked with GTY((user))",
4402 v->name);
4403 break;
4405 case TYPE_POINTER:
4407 const_type_p tp;
4409 if (!start_root_entry (f, v, name, line))
4410 return;
4412 tp = type->u.p;
4414 if (!has_length && union_or_struct_p (tp))
4416 tp = get_ultimate_base_class (tp);
4417 const char *id_for_tag = filter_type_name (tp->u.s.tag);
4418 oprintf (f, " &gt_ggc_mx_%s,\n", id_for_tag);
4419 if (emit_pch)
4420 oprintf (f, " &gt_pch_nx_%s", id_for_tag);
4421 else
4422 oprintf (f, " NULL");
4423 if (id_for_tag != tp->u.s.tag)
4424 free (CONST_CAST (char *, id_for_tag));
4426 else if (has_length
4427 && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
4429 oprintf (f, " &gt_ggc_ma_%s,\n", name);
4430 if (emit_pch)
4431 oprintf (f, " &gt_pch_na_%s", name);
4432 else
4433 oprintf (f, " NULL");
4435 else
4437 error_at_line (line,
4438 "global `%s' is pointer to unimplemented type",
4439 name);
4441 oprintf (f, "\n },\n");
4443 break;
4445 case TYPE_STRING:
4447 if (!start_root_entry (f, v, name, line))
4448 return;
4450 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
4451 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
4452 oprintf (f, " },\n");
4454 break;
4456 case TYPE_SCALAR:
4457 break;
4459 case TYPE_NONE:
4460 case TYPE_UNDEFINED:
4461 case TYPE_UNION:
4462 case TYPE_LANG_STRUCT:
4463 error_at_line (line, "global `%s' is unimplemented type", name);
4467 /* This generates a routine to walk an array. */
4469 static void
4470 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4472 struct walk_type_data d;
4473 char *prevval3;
4475 memset (&d, 0, sizeof (d));
4476 d.of = f;
4477 d.cookie = wtd;
4478 d.indent = 2;
4479 d.line = &v->line;
4480 d.opt = v->opt;
4481 d.bitmap = get_lang_bitmap (v->line.file);
4483 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4485 if (wtd->param_prefix)
4487 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4488 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
4489 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4490 wtd->param_prefix, v->name);
4491 oprintf (d.of,
4492 " ATTRIBUTE_UNUSED void *x_p,\n"
4493 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4494 " ATTRIBUTE_UNUSED void * cookie)\n");
4495 oprintf (d.of, "{\n");
4496 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4497 d.process_field = write_types_local_process_field;
4498 d.have_this_obj = true;
4499 walk_type (v->type, &d);
4500 oprintf (f, "}\n\n");
4503 d.opt = v->opt;
4504 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4505 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4506 wtd->prefix, v->name);
4507 oprintf (f, "{\n");
4508 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4509 d.process_field = write_types_process_field;
4510 d.have_this_obj = false;
4511 walk_type (v->type, &d);
4512 free (prevval3);
4513 oprintf (f, "}\n\n");
4516 /* Output a table describing the locations and types of VARIABLES. */
4518 static void
4519 write_roots (pair_p variables, bool emit_pch)
4521 pair_p v;
4522 struct flist *flp = NULL;
4524 for (v = variables; v; v = v->next)
4526 outf_p f =
4527 get_output_file_with_visibility (CONST_CAST (input_file*,
4528 v->line.file));
4529 struct flist *fli;
4530 const char *length = NULL;
4531 int deletable_p = 0;
4532 options_p o;
4533 for (o = v->opt; o; o = o->next)
4534 if (strcmp (o->name, "length") == 0
4535 && o->kind == OPTION_STRING)
4536 length = o->info.string;
4537 else if (strcmp (o->name, "deletable") == 0)
4538 deletable_p = 1;
4539 else if (strcmp (o->name, "cache") == 0)
4541 else
4542 error_at_line (&v->line,
4543 "global `%s' has unknown option `%s'",
4544 v->name, o->name);
4546 for (fli = flp; fli; fli = fli->next)
4547 if (fli->f == f && f)
4548 break;
4549 if (fli == NULL)
4551 fli = XNEW (struct flist);
4552 fli->f = f;
4553 fli->next = flp;
4554 fli->started_p = 0;
4555 fli->file = v->line.file;
4556 gcc_assert (fli->file);
4557 flp = fli;
4559 oprintf (f, "\n/* GC roots. */\n\n");
4562 if (!deletable_p
4563 && length
4564 && v->type->kind == TYPE_POINTER
4565 && (v->type->u.p->kind == TYPE_POINTER
4566 || v->type->u.p->kind == TYPE_STRUCT))
4568 write_array (f, v, &ggc_wtd);
4569 write_array (f, v, &pch_wtd);
4573 for (v = variables; v; v = v->next)
4575 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4576 v->line.file));
4577 struct flist *fli;
4578 int skip_p = 0;
4579 int length_p = 0;
4580 options_p o;
4582 for (o = v->opt; o; o = o->next)
4583 if (strcmp (o->name, "length") == 0)
4584 length_p = 1;
4585 else if (strcmp (o->name, "deletable") == 0)
4586 skip_p = 1;
4588 if (skip_p)
4589 continue;
4591 for (fli = flp; fli; fli = fli->next)
4592 if (fli->f == f)
4593 break;
4594 if (!fli->started_p)
4596 fli->started_p = 1;
4598 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4599 put_mangled_filename (f, v->line.file);
4600 oprintf (f, "[] = {\n");
4603 write_root (f, v, v->type, v->name, length_p, &v->line, emit_pch);
4606 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4607 "gt_ggc_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 int skip_p = 1;
4615 options_p o;
4617 for (o = v->opt; o; o = o->next)
4618 if (strcmp (o->name, "deletable") == 0)
4619 skip_p = 0;
4621 if (skip_p)
4622 continue;
4624 for (fli = flp; fli; fli = fli->next)
4625 if (fli->f == f)
4626 break;
4627 if (!fli->started_p)
4629 fli->started_p = 1;
4631 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4632 put_mangled_filename (f, v->line.file);
4633 oprintf (f, "[] = {\n");
4636 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4637 v->name, v->name);
4640 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4641 "gt_ggc_deletable_rtab");
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 bool cache = false;
4649 options_p o;
4651 for (o = v->opt; o; o = o->next)
4652 if (strcmp (o->name, "cache") == 0)
4653 cache = true;
4654 if (!cache)
4655 continue;
4657 for (fli = flp; fli; fli = fli->next)
4658 if (fli->f == f)
4659 break;
4660 if (!fli->started_p)
4662 fli->started_p = 1;
4664 oprintf (f, "void\ngt_clear_caches_");
4665 put_mangled_filename (f, v->line.file);
4666 oprintf (f, " ()\n{\n");
4669 oprintf (f, " gt_cleare_cache (%s);\n", v->name);
4672 finish_cache_funcs (flp);
4674 if (!emit_pch)
4675 return;
4677 for (v = variables; v; v = v->next)
4679 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4680 v->line.file));
4681 struct flist *fli;
4682 int skip_p = 0;
4683 options_p o;
4685 for (o = v->opt; o; o = o->next)
4686 if (strcmp (o->name, "deletable") == 0)
4688 skip_p = 1;
4689 break;
4692 if (skip_p)
4693 continue;
4695 if (!contains_scalar_p (v->type))
4696 continue;
4698 for (fli = flp; fli; fli = fli->next)
4699 if (fli->f == f)
4700 break;
4701 if (!fli->started_p)
4703 fli->started_p = 1;
4705 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4706 put_mangled_filename (f, v->line.file);
4707 oprintf (f, "[] = {\n");
4710 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4711 v->name, v->name);
4714 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4715 "gt_pch_scalar_rtab");
4718 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4719 guaranteee for somewhat increased readability. If name conflicts do happen,
4720 this funcion will have to be adjusted to be more like
4721 output_mangled_typename. */
4723 static void
4724 output_typename (outf_p of, const_type_p t)
4726 switch (t->kind)
4728 case TYPE_STRING:
4729 oprintf (of, "str");
4730 break;
4731 case TYPE_SCALAR:
4732 oprintf (of, "scalar");
4733 break;
4734 case TYPE_POINTER:
4735 output_typename (of, t->u.p);
4736 break;
4737 case TYPE_STRUCT:
4738 case TYPE_USER_STRUCT:
4739 case TYPE_UNION:
4740 case TYPE_LANG_STRUCT:
4741 oprintf (of, "%s", t->u.s.tag);
4742 break;
4743 case TYPE_NONE:
4744 case TYPE_UNDEFINED:
4745 case TYPE_ARRAY:
4746 gcc_unreachable ();
4750 #define INDENT 2
4752 /* Dumps the value of typekind KIND. */
4754 static void
4755 dump_typekind (int indent, enum typekind kind)
4757 printf ("%*ckind = ", indent, ' ');
4758 switch (kind)
4760 case TYPE_SCALAR:
4761 printf ("TYPE_SCALAR");
4762 break;
4763 case TYPE_STRING:
4764 printf ("TYPE_STRING");
4765 break;
4766 case TYPE_STRUCT:
4767 printf ("TYPE_STRUCT");
4768 break;
4769 case TYPE_UNDEFINED:
4770 printf ("TYPE_UNDEFINED");
4771 break;
4772 case TYPE_USER_STRUCT:
4773 printf ("TYPE_USER_STRUCT");
4774 break;
4775 case TYPE_UNION:
4776 printf ("TYPE_UNION");
4777 break;
4778 case TYPE_POINTER:
4779 printf ("TYPE_POINTER");
4780 break;
4781 case TYPE_ARRAY:
4782 printf ("TYPE_ARRAY");
4783 break;
4784 case TYPE_LANG_STRUCT:
4785 printf ("TYPE_LANG_STRUCT");
4786 break;
4787 default:
4788 gcc_unreachable ();
4790 printf ("\n");
4793 /* Dumps the value of GC_USED flag. */
4795 static void
4796 dump_gc_used (int indent, enum gc_used_enum gc_used)
4798 printf ("%*cgc_used = ", indent, ' ');
4799 switch (gc_used)
4801 case GC_UNUSED:
4802 printf ("GC_UNUSED");
4803 break;
4804 case GC_USED:
4805 printf ("GC_USED");
4806 break;
4807 case GC_MAYBE_POINTED_TO:
4808 printf ("GC_MAYBE_POINTED_TO");
4809 break;
4810 case GC_POINTED_TO:
4811 printf ("GC_POINTED_TO");
4812 break;
4813 default:
4814 gcc_unreachable ();
4816 printf ("\n");
4819 /* Dumps the type options OPT. */
4821 static void
4822 dump_options (int indent, options_p opt)
4824 options_p o;
4825 printf ("%*coptions = ", indent, ' ');
4826 o = opt;
4827 while (o)
4829 switch (o->kind)
4831 case OPTION_STRING:
4832 printf ("%s:string %s ", o->name, o->info.string);
4833 break;
4834 case OPTION_TYPE:
4835 printf ("%s:type ", o->name);
4836 dump_type (indent+1, o->info.type);
4837 break;
4838 case OPTION_NESTED:
4839 printf ("%s:nested ", o->name);
4840 break;
4841 case OPTION_NONE:
4842 gcc_unreachable ();
4844 o = o->next;
4846 printf ("\n");
4849 /* Dumps the source file location in LINE. */
4851 static void
4852 dump_fileloc (int indent, struct fileloc line)
4854 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
4855 get_input_file_name (line.file),
4856 line.line);
4859 /* Recursively dumps the struct, union, or a language-specific
4860 struct T. */
4862 static void
4863 dump_type_u_s (int indent, type_p t)
4865 pair_p fields;
4867 gcc_assert (union_or_struct_p (t));
4868 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
4869 dump_fileloc (indent, t->u.s.line);
4870 printf ("%*cu.s.fields =\n", indent, ' ');
4871 fields = t->u.s.fields;
4872 while (fields)
4874 dump_pair (indent + INDENT, fields);
4875 fields = fields->next;
4877 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
4878 dump_options (indent, t->u.s.opt);
4879 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
4880 if (t->kind == TYPE_LANG_STRUCT)
4882 printf ("%*cu.s.lang_struct:\n", indent, ' ');
4883 dump_type_list (indent + INDENT, t->u.s.lang_struct);
4887 /* Recursively dumps the array T. */
4889 static void
4890 dump_type_u_a (int indent, type_p t)
4892 gcc_assert (t->kind == TYPE_ARRAY);
4893 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
4894 dump_type_list (indent + INDENT, t->u.a.p);
4897 /* Recursively dumps the type list T. */
4899 static void
4900 dump_type_list (int indent, type_p t)
4902 type_p p = t;
4903 while (p)
4905 dump_type (indent, p);
4906 p = p->next;
4910 static htab_t seen_types;
4912 /* Recursively dumps the type T if it was not dumped previously. */
4914 static void
4915 dump_type (int indent, type_p t)
4917 PTR *slot;
4919 if (seen_types == NULL)
4920 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
4922 printf ("%*cType at %p: ", indent, ' ', (void *) t);
4923 slot = htab_find_slot (seen_types, t, INSERT);
4924 if (*slot != NULL)
4926 printf ("already seen.\n");
4927 return;
4929 *slot = t;
4930 printf ("\n");
4932 dump_typekind (indent, t->kind);
4933 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
4934 (void *) t->pointer_to);
4935 dump_gc_used (indent + INDENT, t->gc_used);
4936 switch (t->kind)
4938 case TYPE_SCALAR:
4939 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
4940 t->u.scalar_is_char ? "true" : "false");
4941 break;
4942 case TYPE_STRING:
4943 break;
4944 case TYPE_STRUCT:
4945 case TYPE_UNION:
4946 case TYPE_LANG_STRUCT:
4947 case TYPE_USER_STRUCT:
4948 dump_type_u_s (indent + INDENT, t);
4949 break;
4950 case TYPE_POINTER:
4951 printf ("%*cp:\n", indent + INDENT, ' ');
4952 dump_type (indent + INDENT, t->u.p);
4953 break;
4954 case TYPE_ARRAY:
4955 dump_type_u_a (indent + INDENT, t);
4956 break;
4957 default:
4958 gcc_unreachable ();
4960 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
4963 /* Dumps the pair P. */
4965 static void
4966 dump_pair (int indent, pair_p p)
4968 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
4969 dump_type (indent, p->type);
4970 dump_fileloc (indent, p->line);
4971 dump_options (indent, p->opt);
4972 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
4975 /* Dumps the list of pairs PP. */
4977 static void
4978 dump_pair_list (const char *name, pair_p pp)
4980 pair_p p;
4981 printf ("%s:\n", name);
4982 for (p = pp; p != NULL; p = p->next)
4983 dump_pair (0, p);
4984 printf ("End of %s\n\n", name);
4987 /* Dumps the STRUCTURES. */
4989 static void
4990 dump_structures (const char *name, type_p structures)
4992 printf ("%s:\n", name);
4993 dump_type_list (0, structures);
4994 printf ("End of %s\n\n", name);
4997 /* Dumps the internal structures of gengtype. This is useful to debug
4998 gengtype itself, or to understand what it does, e.g. for plugin
4999 developers. */
5001 static void
5002 dump_everything (void)
5004 dump_pair_list ("typedefs", typedefs);
5005 dump_structures ("structures", structures);
5006 dump_pair_list ("variables", variables);
5008 /* Allocated with the first call to dump_type. */
5009 htab_delete (seen_types);
5014 /* Option specification for getopt_long. */
5015 static const struct option gengtype_long_options[] = {
5016 {"help", no_argument, NULL, 'h'},
5017 {"version", no_argument, NULL, 'V'},
5018 {"verbose", no_argument, NULL, 'v'},
5019 {"dump", no_argument, NULL, 'd'},
5020 {"debug", no_argument, NULL, 'D'},
5021 {"plugin", required_argument, NULL, 'P'},
5022 {"srcdir", required_argument, NULL, 'S'},
5023 {"backupdir", required_argument, NULL, 'B'},
5024 {"inputs", required_argument, NULL, 'I'},
5025 {"read-state", required_argument, NULL, 'r'},
5026 {"write-state", required_argument, NULL, 'w'},
5027 /* Terminating NULL placeholder. */
5028 {NULL, no_argument, NULL, 0},
5032 static void
5033 print_usage (void)
5035 printf ("Usage: %s\n", progname);
5036 printf ("\t -h | --help " " \t# Give this help.\n");
5037 printf ("\t -D | --debug "
5038 " \t# Give debug output to debug %s itself.\n", progname);
5039 printf ("\t -V | --version " " \t# Give version information.\n");
5040 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5041 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5042 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5043 " \t# Generate for plugin.\n");
5044 printf ("\t -S | --srcdir <GCC-directory> "
5045 " \t# Specify the GCC source directory.\n");
5046 printf ("\t -B | --backupdir <directory> "
5047 " \t# Specify the backup directory for updated files.\n");
5048 printf ("\t -I | --inputs <input-list> "
5049 " \t# Specify the file with source files list.\n");
5050 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5051 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5054 static void
5055 print_version (void)
5057 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5058 printf ("Report bugs: %s\n", bug_report_url);
5061 /* Parse the program options using getopt_long... */
5062 static void
5063 parse_program_options (int argc, char **argv)
5065 int opt = -1;
5066 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5067 gengtype_long_options, NULL)) >= 0)
5069 switch (opt)
5071 case 'h': /* --help */
5072 print_usage ();
5073 break;
5074 case 'V': /* --version */
5075 print_version ();
5076 break;
5077 case 'd': /* --dump */
5078 do_dump = 1;
5079 break;
5080 case 'D': /* --debug */
5081 do_debug = 1;
5082 break;
5083 case 'v': /* --verbose */
5084 verbosity_level++;
5085 break;
5086 case 'P': /* --plugin */
5087 if (optarg)
5088 plugin_output_filename = optarg;
5089 else
5090 fatal ("missing plugin output file name");
5091 break;
5092 case 'S': /* --srcdir */
5093 if (optarg)
5094 srcdir = optarg;
5095 else
5096 fatal ("missing source directory");
5097 srcdir_len = strlen (srcdir);
5098 break;
5099 case 'B': /* --backupdir */
5100 if (optarg)
5101 backup_dir = optarg;
5102 else
5103 fatal ("missing backup directory");
5104 break;
5105 case 'I': /* --inputs */
5106 if (optarg)
5107 inputlist = optarg;
5108 else
5109 fatal ("missing input list");
5110 break;
5111 case 'r': /* --read-state */
5112 if (optarg)
5113 read_state_filename = optarg;
5114 else
5115 fatal ("missing read state file");
5116 DBGPRINTF ("read state %s\n", optarg);
5117 break;
5118 case 'w': /* --write-state */
5119 DBGPRINTF ("write state %s\n", optarg);
5120 if (optarg)
5121 write_state_filename = optarg;
5122 else
5123 fatal ("missing write state file");
5124 break;
5125 default:
5126 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5127 print_usage ();
5128 fatal ("unexpected flag");
5131 if (plugin_output_filename)
5133 /* In plugin mode we require some input files. */
5134 int i = 0;
5135 if (optind >= argc)
5136 fatal ("no source files given in plugin mode");
5137 nb_plugin_files = argc - optind;
5138 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5139 for (i = 0; i < (int) nb_plugin_files; i++)
5141 char *name = argv[i + optind];
5142 plugin_files[i] = input_file_by_name (name);
5149 /******* Manage input files. ******/
5151 /* Hash table of unique input file names. */
5152 static htab_t input_file_htab;
5154 /* Find or allocate a new input_file by hash-consing it. */
5155 input_file*
5156 input_file_by_name (const char* name)
5158 PTR* slot;
5159 input_file* f = NULL;
5160 int namlen = 0;
5161 if (!name)
5162 return NULL;
5163 namlen = strlen (name);
5164 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5165 f->inpbitmap = 0;
5166 f->inpoutf = NULL;
5167 f->inpisplugin = false;
5168 strcpy (f->inpname, name);
5169 slot = htab_find_slot (input_file_htab, f, INSERT);
5170 gcc_assert (slot != NULL);
5171 if (*slot)
5173 /* Already known input file. */
5174 free (f);
5175 return (input_file*)(*slot);
5177 /* New input file. */
5178 *slot = f;
5179 return f;
5182 /* Hash table support routines for input_file-s. */
5183 static hashval_t
5184 htab_hash_inputfile (const void *p)
5186 const input_file *inpf = (const input_file *) p;
5187 gcc_assert (inpf);
5188 return htab_hash_string (get_input_file_name (inpf));
5191 static int
5192 htab_eq_inputfile (const void *x, const void *y)
5194 const input_file *inpfx = (const input_file *) x;
5195 const input_file *inpfy = (const input_file *) y;
5196 gcc_assert (inpfx != NULL && inpfy != NULL);
5197 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5202 main (int argc, char **argv)
5204 size_t i;
5205 static struct fileloc pos = { NULL, 0 };
5206 outf_p output_header;
5208 /* Mandatory common initializations. */
5209 progname = "gengtype"; /* For fatal and messages. */
5210 /* Create the hash-table used to hash-cons input files. */
5211 input_file_htab =
5212 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5213 /* Initialize our special input files. */
5214 this_file = input_file_by_name (__FILE__);
5215 system_h_file = input_file_by_name ("system.h");
5216 /* Set the scalar_is_char union number for predefined scalar types. */
5217 scalar_nonchar.u.scalar_is_char = FALSE;
5218 scalar_char.u.scalar_is_char = TRUE;
5220 parse_program_options (argc, argv);
5222 #if ENABLE_CHECKING
5223 if (do_debug)
5225 time_t now = (time_t) 0;
5226 time (&now);
5227 DBGPRINTF ("gengtype started pid %d at %s",
5228 (int) getpid (), ctime (&now));
5230 #endif /* ENABLE_CHECKING */
5232 /* Parse the input list and the input files. */
5233 DBGPRINTF ("inputlist %s", inputlist);
5234 if (read_state_filename)
5236 if (inputlist)
5237 fatal ("input list %s cannot be given with a read state file %s",
5238 inputlist, read_state_filename);
5239 read_state (read_state_filename);
5240 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5242 else if (inputlist)
5244 /* These types are set up with #define or else outside of where
5245 we can see them. We should initialize them before calling
5246 read_input_list. */
5247 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5248 Call;} while (0)
5249 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5250 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5251 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5252 POS_HERE (do_scalar_typedef ("double_int", &pos));
5253 POS_HERE (do_scalar_typedef ("offset_int", &pos));
5254 POS_HERE (do_scalar_typedef ("widest_int", &pos));
5255 POS_HERE (do_scalar_typedef ("int64_t", &pos));
5256 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5257 POS_HERE (do_scalar_typedef ("uint8", &pos));
5258 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5259 POS_HERE (do_scalar_typedef ("jword", &pos));
5260 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5261 POS_HERE (do_scalar_typedef ("void", &pos));
5262 POS_HERE (do_scalar_typedef ("machine_mode", &pos));
5263 POS_HERE (do_typedef ("PTR",
5264 create_pointer (resolve_typedef ("void", &pos)),
5265 &pos));
5266 #undef POS_HERE
5267 read_input_list (inputlist);
5268 for (i = 0; i < num_gt_files; i++)
5270 parse_file (get_input_file_name (gt_files[i]));
5271 DBGPRINTF ("parsed file #%d %s",
5272 (int) i, get_input_file_name (gt_files[i]));
5274 if (verbosity_level >= 1)
5275 printf ("%s parsed %d files with %d GTY types\n",
5276 progname, (int) num_gt_files, type_count);
5278 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5280 else
5281 fatal ("either an input list or a read state file should be given");
5282 if (hit_error)
5283 return 1;
5286 if (plugin_output_filename)
5288 size_t ix = 0;
5289 /* In plugin mode, we should have read a state file, and have
5290 given at least one plugin file. */
5291 if (!read_state_filename)
5292 fatal ("No read state given in plugin mode for %s",
5293 plugin_output_filename);
5295 if (nb_plugin_files == 0 || !plugin_files)
5296 fatal ("No plugin files given in plugin mode for %s",
5297 plugin_output_filename);
5299 /* Parse our plugin files and augment the state. */
5300 for (ix = 0; ix < nb_plugin_files; ix++)
5302 input_file* pluginput = plugin_files [ix];
5303 pluginput->inpisplugin = true;
5304 parse_file (get_input_file_name (pluginput));
5306 if (hit_error)
5307 return 1;
5309 plugin_output = create_file ("GCC", plugin_output_filename);
5310 DBGPRINTF ("created plugin_output %p named %s",
5311 (void *) plugin_output, plugin_output->name);
5313 else
5314 { /* No plugin files, we are in normal mode. */
5315 if (!srcdir)
5316 fatal ("gengtype needs a source directory in normal mode");
5318 if (hit_error)
5319 return 1;
5321 gen_rtx_next ();
5323 set_gc_used (variables);
5325 for (type_p t = structures; t; t = t->next)
5327 bool for_user = false;
5328 for (options_p o = t->u.s.opt; o; o = o->next)
5329 if (strcmp (o->name, "for_user") == 0)
5331 for_user = true;
5332 break;
5335 if (for_user)
5336 set_gc_used_type (t, GC_POINTED_TO);
5338 /* The state at this point is read from the state input file or by
5339 parsing source files and optionally augmented by parsing plugin
5340 source files. Write it now. */
5341 if (write_state_filename)
5343 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5345 if (hit_error)
5346 fatal ("didn't write state file %s after errors",
5347 write_state_filename);
5349 DBGPRINTF ("before write_state %s", write_state_filename);
5350 write_state (write_state_filename);
5352 if (do_dump)
5353 dump_everything ();
5355 /* After having written the state file we return immediately to
5356 avoid generating any output file. */
5357 if (hit_error)
5358 return 1;
5359 else
5360 return 0;
5364 open_base_files ();
5366 output_header = plugin_output ? plugin_output : header_file;
5367 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5368 structures);
5370 write_types (output_header, structures, &ggc_wtd);
5371 if (plugin_files == NULL)
5373 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5374 structures);
5375 write_types (header_file, structures, &pch_wtd);
5376 write_local (header_file, structures);
5378 write_roots (variables, plugin_files == NULL);
5379 write_rtx_next ();
5380 close_output_files ();
5382 if (do_dump)
5383 dump_everything ();
5385 /* Don't bother about free-ing any input or plugin file, etc. */
5387 if (hit_error)
5388 return 1;
5389 return 0;