[c++] [testsuite] adjust contracts9.C for negative addresses
[official-gcc.git] / gcc / gengtype.cc
blobc0ecbe8558b1f255dd80048756916c14d0fe0b00
1 /* Process source files and output type information.
2 Copyright (C) 2002-2024 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #ifdef HOST_GENERATOR_FILE
21 #include "config.h"
22 #define GENERATOR_FILE 1
23 #else
24 #include "bconfig.h"
25 #endif
26 #include "system.h"
27 #include "errors.h" /* for fatal */
28 #include "getopt.h"
29 #include "version.h" /* for version_string & pkgversion_string. */
30 #include "xregex.h"
31 #include "obstack.h"
32 #include "gengtype.h"
33 #include "filenames.h"
35 /* Data types, macros, etc. used only in this file. */
38 /* The list of output files. */
39 outf_p output_files;
41 /* The output header file that is included into pretty much every
42 source file. */
43 outf_p header_file;
46 /* The name of the file containing the list of input files. */
47 static char *inputlist;
49 /* The plugin input files and their number; in that case only
50 a single file is produced. */
51 static input_file **plugin_files;
52 static size_t nb_plugin_files;
54 /* The generated plugin output file and name. */
55 static outf_p plugin_output;
56 static char *plugin_output_filename;
58 /* Our source directory and its length. */
59 const char *srcdir;
60 size_t srcdir_len;
62 /* Variables used for reading and writing the state. */
63 const char *read_state_filename;
64 const char *write_state_filename;
66 /* Variables to help debugging. */
67 int do_dump;
68 int do_debug;
70 /* Level for verbose messages. */
71 int verbosity_level;
73 /* We have a type count and use it to set the state_number of newly
74 allocated types to some unique negative number. */
75 static int type_count;
77 /* The backup directory should be in the same file system as the
78 generated files, otherwise the rename(2) system call would fail.
79 If NULL, no backup is made when overwriting a generated file. */
80 static const char* backup_dir; /* (-B) program option. */
83 static outf_p create_file (const char *, const char *);
85 static const char *get_file_basename (const input_file *);
86 static const char *get_file_realbasename (const input_file *);
88 static int get_prefix_langdir_index (const char *);
89 static const char *get_file_langdir (const input_file *);
91 static void dump_pair (int indent, pair_p p);
92 static void dump_type (int indent, type_p p);
93 static void dump_type_list (int indent, type_p p);
96 /* Nonzero iff an error has occurred. */
97 bool hit_error = false;
99 static void gen_rtx_next (void);
100 static void write_rtx_next (void);
101 static void open_base_files (void);
102 static void close_output_files (void);
104 /* Report an error at POS, printing MSG. */
106 void
107 error_at_line (const struct fileloc *pos, const char *msg, ...)
109 va_list ap;
111 gcc_assert (pos != NULL && pos->file != NULL);
112 va_start (ap, msg);
114 fprintf (stderr, "%s:%d: ", get_input_file_name (pos->file), pos->line);
115 vfprintf (stderr, msg, ap);
116 fputc ('\n', stderr);
117 hit_error = true;
119 va_end (ap);
122 /* Locate the ultimate base class of struct S. */
124 static const_type_p
125 get_ultimate_base_class (const_type_p s)
127 while (s->u.s.base_class)
128 s = s->u.s.base_class;
129 return s;
132 static type_p
133 get_ultimate_base_class (type_p s)
135 while (s->u.s.base_class)
136 s = s->u.s.base_class;
137 return s;
140 /* Input file handling. */
142 /* Table of all input files. */
143 const input_file **gt_files;
144 size_t num_gt_files;
146 /* Table of headers to be included in gtype-desc.cc that are generated
147 during the build. These are identified as "./<filename>.h". */
148 const char **build_headers;
149 size_t num_build_headers;
151 /* A number of places use the name of this "gengtype.cc" file for a
152 location for things that we can't rely on the source to define.
153 Make sure we can still use pointer comparison on filenames. */
154 input_file* this_file;
155 /* The "system.h" file is likewise specially useful. */
156 input_file* system_h_file;
158 /* Vector of per-language directories. */
159 const char **lang_dir_names;
160 size_t num_lang_dirs;
162 /* An array of output files suitable for definitions. There is one
163 BASE_FILES entry for each language. */
164 static outf_p *base_files;
166 /* Utility debugging function, printing the various type counts within
167 a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
168 void
169 dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
171 int nb_types = 0, nb_scalar = 0, nb_string = 0;
172 int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
173 int nb_lang_struct = 0;
174 int nb_user_struct = 0, nb_undefined = 0;
175 int nb_callback = 0;
176 type_p p = NULL;
177 for (p = t; p; p = p->next)
179 nb_types++;
180 switch (p->kind)
182 case TYPE_UNDEFINED:
183 nb_undefined++;
184 break;
185 case TYPE_SCALAR:
186 nb_scalar++;
187 break;
188 case TYPE_STRING:
189 nb_string++;
190 break;
191 case TYPE_STRUCT:
192 nb_struct++;
193 break;
194 case TYPE_USER_STRUCT:
195 nb_user_struct++;
196 break;
197 case TYPE_UNION:
198 nb_union++;
199 break;
200 case TYPE_POINTER:
201 nb_pointer++;
202 break;
203 case TYPE_ARRAY:
204 nb_array++;
205 break;
206 case TYPE_CALLBACK:
207 nb_callback++;
208 break;
209 case TYPE_LANG_STRUCT:
210 nb_lang_struct++;
211 break;
212 case TYPE_NONE:
213 gcc_unreachable ();
216 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
217 lbasename (fil), lin, msg, nb_types);
218 if (nb_scalar > 0 || nb_string > 0)
219 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
220 if (nb_struct > 0 || nb_union > 0)
221 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
222 if (nb_pointer > 0 || nb_array > 0)
223 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
224 if (nb_callback > 0)
225 fprintf (stderr, "@@%%@@ %d callbacks\n", nb_callback);
226 if (nb_lang_struct > 0)
227 fprintf (stderr, "@@%%@@ %d lang_structs\n", nb_lang_struct);
228 if (nb_user_struct > 0)
229 fprintf (stderr, "@@%%@@ %d user_structs\n", nb_user_struct);
230 if (nb_undefined > 0)
231 fprintf (stderr, "@@%%@@ %d undefined types\n", nb_undefined);
232 fprintf (stderr, "\n");
235 /* Scan the input file, LIST, and determine how much space we need to
236 store strings in. Also, count the number of language directories
237 and files. The numbers returned are overestimates as they does not
238 consider repeated files. */
239 static size_t
240 measure_input_list (FILE *list)
242 size_t n = 0;
243 int c;
244 bool atbol = true;
245 num_lang_dirs = 0;
246 num_gt_files = plugin_files ? nb_plugin_files : 0;
247 while ((c = getc (list)) != EOF)
249 n++;
250 if (atbol)
252 if (c == '[')
253 num_lang_dirs++;
254 else
256 /* Add space for a lang_bitmap before the input file name. */
257 n += sizeof (lang_bitmap);
258 num_gt_files++;
260 atbol = false;
263 if (c == '\n')
264 atbol = true;
267 rewind (list);
268 return n;
271 /* Read one input line from LIST to HEREP (which is updated). A
272 pointer to the string is returned via LINEP. If it was a language
273 subdirectory in square brackets, strip off the square brackets and
274 return true. Otherwise, leave space before the string for a
275 lang_bitmap, and return false. At EOF, returns false, does not
276 touch *HEREP, and sets *LINEP to NULL. POS is used for
277 diagnostics. */
278 static bool
279 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
281 char *here = *herep;
282 char *line;
283 int c = getc (list);
285 /* Read over whitespace. */
286 while (c == '\n' || c == ' ')
287 c = getc (list);
289 if (c == EOF)
291 *linep = 0;
292 return false;
294 else if (c == '[')
296 /* No space for a lang_bitmap is necessary. Discard the '['. */
297 c = getc (list);
298 line = here;
299 while (c != ']' && c != '\n' && c != EOF)
301 *here++ = c;
302 c = getc (list);
304 *here++ = '\0';
306 if (c == ']')
308 c = getc (list); /* eat what should be a newline */
309 if (c != '\n' && c != EOF)
310 error_at_line (pos, "junk on line after language tag [%s]", line);
312 else
313 error_at_line (pos, "missing close bracket for language tag [%s",
314 line);
316 *herep = here;
317 *linep = line;
318 return true;
320 else
322 /* Leave space for a lang_bitmap. */
323 memset (here, 0, sizeof (lang_bitmap));
324 here += sizeof (lang_bitmap);
325 line = here;
328 *here++ = c;
329 c = getc (list);
331 while (c != EOF && c != '\n');
332 *here++ = '\0';
333 *herep = here;
334 *linep = line;
335 return false;
339 /* Read the list of input files from LIST and compute all of the
340 relevant tables. There is one file per line of the list. At
341 first, all the files on the list are language-generic, but
342 eventually a line will appear which is the name of a language
343 subdirectory in square brackets, like this: [cp]. All subsequent
344 files are specific to that language, until another language
345 subdirectory tag appears. Files can appear more than once, if
346 they apply to more than one language. */
347 static void
348 read_input_list (const char *listname)
350 FILE *list = fopen (listname, "r");
351 if (!list)
352 fatal ("cannot open %s: %s", listname, xstrerror (errno));
353 else
355 struct fileloc epos;
356 size_t bufsz = measure_input_list (list);
357 char *buf = XNEWVEC (char, bufsz);
358 char *here = buf;
359 char *committed = buf;
360 char *limit = buf + bufsz;
361 char *line;
362 bool is_language;
363 size_t langno = 0;
364 size_t nfiles = 0;
365 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
367 epos.file = input_file_by_name (listname);
368 epos.line = 0;
370 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
371 gt_files = XNEWVEC (const input_file *, num_gt_files);
373 for (;;)
375 next_line:
376 epos.line++;
377 committed = here;
378 is_language = read_input_line (list, &here, &line, &epos);
379 gcc_assert (here <= limit);
380 if (line == 0)
381 break;
382 else if (is_language)
384 size_t i;
385 gcc_assert (langno <= num_lang_dirs);
386 for (i = 0; i < langno; i++)
387 if (strcmp (lang_dir_names[i], line) == 0)
389 error_at_line (&epos, "duplicate language tag [%s]",
390 line);
391 curlangs = 1 << i;
392 here = committed;
393 goto next_line;
396 curlangs = 1 << langno;
397 lang_dir_names[langno++] = line;
399 else
401 size_t i;
402 input_file *inpf = input_file_by_name (line);
403 gcc_assert (nfiles <= num_gt_files);
404 for (i = 0; i < nfiles; i++)
405 /* Since the input_file-s are uniquely hash-consed, we
406 can just compare pointers! */
407 if (gt_files[i] == inpf)
409 /* Throw away the string we just read, and add the
410 current language to the existing string's bitmap. */
411 lang_bitmap bmap = get_lang_bitmap (inpf);
412 if (bmap & curlangs)
413 error_at_line (&epos,
414 "file %s specified more than once "
415 "for language %s", line,
416 langno ==
417 0 ? "(all)" : lang_dir_names[langno -
418 1]);
420 bmap |= curlangs;
421 set_lang_bitmap (inpf, bmap);
422 here = committed;
423 goto next_line;
426 set_lang_bitmap (inpf, curlangs);
427 gt_files[nfiles++] = inpf;
430 /* Update the global counts now that we know accurately how many
431 things there are. (We do not bother resizing the arrays down.) */
432 num_lang_dirs = langno;
433 /* Add the plugin files if provided. */
434 if (plugin_files)
436 size_t i;
437 for (i = 0; i < nb_plugin_files; i++)
438 gt_files[nfiles++] = plugin_files[i];
440 num_gt_files = nfiles;
443 /* Sanity check: any file that resides in a language subdirectory
444 (e.g. 'cp') ought to belong to the corresponding language.
445 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
446 (Can you even do that? Should you be allowed to?) */
448 size_t f;
449 for (f = 0; f < num_gt_files; f++)
451 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
452 const char *basename = get_file_basename (gt_files[f]);
453 const char *slashpos = strchr (basename, '/');
454 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
455 const char *slashpos2 = strchr (basename, '\\');
457 if (!slashpos || (slashpos2 && slashpos2 < slashpos))
458 slashpos = slashpos2;
459 #endif
461 if (slashpos)
463 size_t l;
464 for (l = 0; l < num_lang_dirs; l++)
465 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
466 && memcmp (basename, lang_dir_names[l],
467 strlen (lang_dir_names[l])) == 0)
469 if (!(bitmap & (1 << l)))
470 error ("%s is in language directory '%s' but is not "
471 "tagged for that language",
472 basename, lang_dir_names[l]);
473 break;
479 if (ferror (list))
480 fatal ("error reading %s: %s", listname, xstrerror (errno));
482 fclose (list);
487 /* The one and only TYPE_STRING. */
489 struct type string_type = {
490 TYPE_STRING, 0, 0, 0, GC_USED, {0}
493 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
494 set early in main. */
496 struct type scalar_nonchar = {
497 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
500 struct type scalar_char = {
501 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
504 struct type callback_type = {
505 TYPE_CALLBACK, 0, 0, 0, GC_USED, {0}
508 /* Lists of various things. */
510 pair_p typedefs = NULL;
511 type_p structures = NULL;
512 pair_p variables = NULL;
514 static type_p adjust_field_rtx_def (type_p t, options_p opt);
516 /* Define S as a typedef to T at POS. */
518 void
519 do_typedef (const char *s, type_p t, struct fileloc *pos)
521 pair_p p;
523 /* temporary kludge - gengtype doesn't handle conditionals or
524 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
525 is coming from this file (main() sets them up with safe dummy
526 definitions). */
527 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
528 return;
530 for (p = typedefs; p != NULL; p = p->next)
531 if (strcmp (p->name, s) == 0)
533 if (p->type != t && strcmp (s, "result_type") != 0)
535 error_at_line (pos, "type `%s' previously defined", s);
536 error_at_line (&p->line, "previously defined here");
538 return;
541 p = XNEW (struct pair);
542 p->next = typedefs;
543 p->name = s;
544 p->type = t;
545 p->line = *pos;
546 p->opt = NULL;
547 typedefs = p;
550 /* Define S as a typename of a scalar. Cannot be used to define
551 typedefs of 'char'. Note: is also used for pointer-to-function
552 typedefs (which are therefore not treated as pointers). */
554 void
555 do_scalar_typedef (const char *s, struct fileloc *pos)
557 do_typedef (s, &scalar_nonchar, pos);
560 /* Similar to strtok_r. */
562 static char *
563 strtoken (char *str, const char *delim, char **next)
565 char *p;
567 if (str == NULL)
568 str = *next;
570 /* Skip the leading delimiters. */
571 str += strspn (str, delim);
572 if (*str == '\0')
573 /* This is an empty token. */
574 return NULL;
576 /* The current token. */
577 p = str;
579 /* Find the next delimiter. */
580 str += strcspn (str, delim);
581 if (*str == '\0')
582 /* This is the last token. */
583 *next = str;
584 else
586 /* Terminate the current token. */
587 *str = '\0';
588 /* Advance to the next token. */
589 *next = str + 1;
592 return p;
595 /* Define TYPE_NAME to be a user defined type at location POS. */
597 type_p
598 create_user_defined_type (const char *type_name, struct fileloc *pos)
600 type_p ty = find_structure (type_name, TYPE_USER_STRUCT);
602 /* We might have already seen an incomplete decl of the given type,
603 in which case we won't have yet seen a GTY((user)), and the type will
604 only have kind "TYPE_STRUCT". Mark it as a user struct. */
605 ty->kind = TYPE_USER_STRUCT;
607 ty->u.s.line = *pos;
608 ty->u.s.bitmap = get_lang_bitmap (pos->file);
609 do_typedef (type_name, ty, pos);
611 /* If TYPE_NAME specifies a template, create references to the types
612 in the template by pretending that each type is a field of TY.
613 This is needed to make sure that the types referenced by the
614 template are marked as used. */
615 char *str = xstrdup (type_name);
616 char *open_bracket = strchr (str, '<');
617 if (open_bracket)
619 /* We only accept simple template declarations (see
620 require_template_declaration), so we only need to parse a
621 comma-separated list of strings, implicitly assumed to
622 be type names, potentially with "*" characters. */
623 char *arg = open_bracket + 1;
624 /* Workaround -Wmaybe-uninitialized false positive during
625 profiledbootstrap by initializing it. */
626 char *next = NULL;
627 char *type_id = strtoken (arg, ",>", &next);
628 pair_p fields = 0;
629 while (type_id)
631 /* Create a new field for every type found inside the template
632 parameter list. */
634 /* Support a single trailing "*" character. */
635 const char *star = strchr (type_id, '*');
636 int is_ptr = (star != NULL);
637 size_t offset_to_star = star - type_id;
638 if (is_ptr)
639 offset_to_star = star - type_id;
641 if (strstr (type_id, "char*"))
643 type_id = strtoken (0, ",>", &next);
644 continue;
647 char *field_name = xstrdup (type_id);
649 type_p arg_type;
650 if (is_ptr)
652 /* Strip off the first '*' character (and any subsequent text). */
653 *(field_name + offset_to_star) = '\0';
655 arg_type = find_structure (field_name, TYPE_STRUCT);
656 arg_type = create_pointer (arg_type);
658 else
659 arg_type = resolve_typedef (field_name, pos);
661 fields = create_field_at (fields, arg_type, field_name, 0, pos);
662 type_id = strtoken (0, ",>", &next);
665 /* Associate the field list to TY. */
666 ty->u.s.fields = fields;
668 free (str);
670 return ty;
674 /* Given a typedef name S, return its associated type. Return NULL if
675 S is not a registered type name. */
677 static type_p
678 type_for_name (const char *s)
680 pair_p p;
682 /* Special-case support for types within a "gcc::" namespace. Rather
683 than fully-supporting namespaces, simply strip off the "gcc::" prefix
684 where present. This allows us to have GTY roots of this form:
685 extern GTY(()) gcc::some_type *some_ptr;
686 where the autogenerated functions will refer to simply "some_type",
687 where they can be resolved into their namespace. */
688 if (startswith (s, "gcc::"))
689 s += 5;
691 for (p = typedefs; p != NULL; p = p->next)
692 if (strcmp (p->name, s) == 0)
693 return p->type;
694 return NULL;
698 /* Create an undefined type with name S and location POS. Return the
699 newly created type. */
701 static type_p
702 create_undefined_type (const char *s, struct fileloc *pos)
704 type_p ty = find_structure (s, TYPE_UNDEFINED);
705 ty->u.s.line = *pos;
706 ty->u.s.bitmap = get_lang_bitmap (pos->file);
707 do_typedef (s, ty, pos);
708 return ty;
712 /* Return the type previously defined for S. Use POS to report errors. */
714 type_p
715 resolve_typedef (const char *s, struct fileloc *pos)
717 bool is_template_instance = (strchr (s, '<') != NULL);
718 type_p p = type_for_name (s);
720 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
721 type for regular type identifiers. If the type identifier S is a
722 template instantiation, however, we treat it as a user defined
723 type.
725 FIXME, this is actually a limitation in gengtype. Supporting
726 template types and their instances would require keeping separate
727 track of the basic types definition and its instances. This
728 essentially forces all template classes in GC to be marked
729 GTY((user)). */
730 if (!p)
731 p = (is_template_instance)
732 ? create_user_defined_type (s, pos)
733 : create_undefined_type (s, pos);
735 return p;
738 /* Add SUBCLASS to head of linked list of BASE's subclasses. */
740 void add_subclass (type_p base, type_p subclass)
742 gcc_assert (union_or_struct_p (base));
743 gcc_assert (union_or_struct_p (subclass));
745 subclass->u.s.next_sibling_class = base->u.s.first_subclass;
746 base->u.s.first_subclass = subclass;
749 /* Create and return a new structure with tag NAME at POS with fields
750 FIELDS and options O. The KIND of structure must be one of
751 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
753 type_p
754 new_structure (const char *name, enum typekind kind, struct fileloc *pos,
755 pair_p fields, options_p o, type_p base_class)
757 type_p si;
758 type_p s = NULL;
759 lang_bitmap bitmap = get_lang_bitmap (pos->file);
760 bool isunion = (kind == TYPE_UNION);
761 type_p *p = &structures;
763 gcc_assert (union_or_struct_p (kind));
765 for (si = structures; si != NULL; p = &si->next, si = *p)
766 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
768 type_p ls = NULL;
769 if (si->kind == TYPE_LANG_STRUCT)
771 ls = si;
773 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
774 if (si->u.s.bitmap == bitmap)
775 s = si;
777 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
779 ls = si;
780 type_count++;
781 si = XCNEW (struct type);
782 memcpy (si, ls, sizeof (struct type));
783 ls->kind = TYPE_LANG_STRUCT;
784 ls->u.s.lang_struct = si;
785 ls->u.s.fields = NULL;
786 si->next = NULL;
787 si->state_number = -type_count;
788 si->pointer_to = NULL;
789 si->u.s.lang_struct = ls;
791 else
792 s = si;
794 if (ls != NULL && s == NULL)
796 type_count++;
797 s = XCNEW (struct type);
798 s->state_number = -type_count;
799 s->next = ls->u.s.lang_struct;
800 ls->u.s.lang_struct = s;
801 s->u.s.lang_struct = ls;
803 break;
806 if (s == NULL)
808 type_count++;
809 s = XCNEW (struct type);
810 s->state_number = -type_count;
811 *p = s;
814 if (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap))
816 error_at_line (pos, "duplicate definition of '%s %s'",
817 isunion ? "union" : "struct", s->u.s.tag);
818 error_at_line (&s->u.s.line, "previous definition here");
821 s->kind = kind;
822 s->u.s.tag = name;
823 s->u.s.line = *pos;
824 s->u.s.fields = fields;
825 s->u.s.opt = o;
826 s->u.s.bitmap = bitmap;
827 if (s->u.s.lang_struct)
828 s->u.s.lang_struct->u.s.bitmap |= bitmap;
829 s->u.s.base_class = base_class;
830 if (base_class)
831 add_subclass (base_class, s);
833 return s;
836 /* Return the previously-defined structure or union with tag NAME,
837 or a new empty structure or union if none was defined previously.
838 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
839 TYPE_USER_STRUCT. */
841 type_p
842 find_structure (const char *name, enum typekind kind)
844 type_p s;
845 bool isunion = (kind == TYPE_UNION);
846 type_p *p = &structures;
848 gcc_assert (kind == TYPE_UNDEFINED || union_or_struct_p (kind));
850 for (s = structures; s != NULL; p = &s->next, s = *p)
851 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
852 return s;
854 type_count++;
855 s = XCNEW (struct type);
856 s->state_number = -type_count;
857 s->kind = kind;
858 s->u.s.tag = name;
859 *p = s;
860 return s;
863 /* Return a scalar type with name NAME. */
865 type_p
866 create_scalar_type (const char *name)
868 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
869 return &scalar_char;
870 else
871 return &scalar_nonchar;
875 /* Return a pointer to T. */
877 type_p
878 create_pointer (type_p t)
880 if (!t->pointer_to)
882 type_p r = XCNEW (struct type);
883 type_count++;
884 r->state_number = -type_count;
885 r->kind = TYPE_POINTER;
886 r->u.p = t;
887 t->pointer_to = r;
889 return t->pointer_to;
892 /* Return an array of length LEN. */
894 type_p
895 create_array (type_p t, const char *len)
897 type_p v;
899 type_count++;
900 v = XCNEW (struct type);
901 v->kind = TYPE_ARRAY;
902 v->state_number = -type_count;
903 v->u.a.p = t;
904 v->u.a.len = len;
905 return v;
908 /* Return a string options structure with name NAME and info INFO.
909 NEXT is the next option in the chain. */
910 options_p
911 create_string_option (options_p next, const char *name, const char *info)
913 options_p o = XNEW (struct options);
914 o->kind = OPTION_STRING;
915 o->next = next;
916 o->name = name;
917 o->info.string = info;
918 return o;
921 /* Create a type options structure with name NAME and info INFO. NEXT
922 is the next option in the chain. */
923 options_p
924 create_type_option (options_p next, const char* name, type_p info)
926 options_p o = XNEW (struct options);
927 o->next = next;
928 o->name = name;
929 o->kind = OPTION_TYPE;
930 o->info.type = info;
931 return o;
934 /* Create a nested pointer options structure with name NAME and info
935 INFO. NEXT is the next option in the chain. */
936 options_p
937 create_nested_option (options_p next, const char* name,
938 struct nested_ptr_data* info)
940 options_p o;
941 o = XNEW (struct options);
942 o->next = next;
943 o->name = name;
944 o->kind = OPTION_NESTED;
945 o->info.nested = info;
946 return o;
949 /* Return an options structure for a "nested_ptr" option. */
950 options_p
951 create_nested_ptr_option (options_p next, type_p t,
952 const char *to, const char *from)
954 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
956 d->type = adjust_field_type (t, 0);
957 d->convert_to = to;
958 d->convert_from = from;
959 return create_nested_option (next, "nested_ptr", d);
962 /* Add a variable named S of type T with options O defined at POS,
963 to `variables'. */
964 void
965 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
967 pair_p n;
968 n = XNEW (struct pair);
969 n->name = s;
970 n->type = t;
971 n->line = *pos;
972 n->opt = o;
973 n->next = variables;
974 variables = n;
977 /* Most-general structure field creator. */
978 static pair_p
979 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
980 const input_file *inpf, int line)
982 pair_p field;
984 field = XNEW (struct pair);
985 field->next = next;
986 field->type = type;
987 field->name = name;
988 field->opt = opt;
989 field->line.file = inpf;
990 field->line.line = line;
991 return field;
994 /* Create a field that came from the source code we are scanning,
995 i.e. we have a 'struct fileloc', and possibly options; also,
996 adjust_field_type should be called. */
997 pair_p
998 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
999 struct fileloc *pos)
1001 return create_field_all (next, adjust_field_type (type, opt),
1002 name, opt, pos->file, pos->line);
1005 /* Create a fake field with the given type and name. NEXT is the next
1006 field in the chain. */
1007 #define create_field(next,type,name) \
1008 create_field_all (next,type,name, 0, this_file, __LINE__)
1010 /* Like create_field, but the field is only valid when condition COND
1011 is true. */
1013 static pair_p
1014 create_optional_field_ (pair_p next, type_p type, const char *name,
1015 const char *cond, int line)
1017 static int id = 1;
1018 pair_p union_fields;
1019 type_p union_type;
1021 /* Create a fake union type with a single nameless field of type TYPE.
1022 The field has a tag of "1". This allows us to make the presence
1023 of a field of type TYPE depend on some boolean "desc" being true. */
1024 union_fields = create_field (NULL, type, "");
1025 union_fields->opt =
1026 create_string_option (union_fields->opt, "dot", "");
1027 union_fields->opt =
1028 create_string_option (union_fields->opt, "tag", "1");
1029 union_type =
1030 new_structure (xasprintf ("%s_%d", "fake_union", id++), TYPE_UNION,
1031 &lexer_line, union_fields, NULL, NULL);
1033 /* Create the field and give it the new fake union type. Add a "desc"
1034 tag that specifies the condition under which the field is valid. */
1035 return create_field_all (next, union_type, name,
1036 create_string_option (0, "desc", cond),
1037 this_file, line);
1040 #define create_optional_field(next,type,name,cond) \
1041 create_optional_field_(next,type,name,cond,__LINE__)
1043 /* Reverse a linked list of 'struct pair's in place. */
1044 pair_p
1045 nreverse_pairs (pair_p list)
1047 pair_p prev = 0, p, next;
1048 for (p = list; p; p = next)
1050 next = p->next;
1051 p->next = prev;
1052 prev = p;
1054 return prev;
1058 /* We don't care how long a CONST_DOUBLE is. */
1059 #define CONST_DOUBLE_FORMAT "ww"
1060 /* We don't want to see codes that are only for generator files. */
1061 #undef GENERATOR_FILE
1063 enum rtx_code
1065 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1066 #include "rtl.def"
1067 #undef DEF_RTL_EXPR
1068 NUM_RTX_CODE
1071 static const char *const rtx_name[NUM_RTX_CODE] = {
1072 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1073 #include "rtl.def"
1074 #undef DEF_RTL_EXPR
1077 static const char *const rtx_format[NUM_RTX_CODE] = {
1078 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1079 #include "rtl.def"
1080 #undef DEF_RTL_EXPR
1083 static int rtx_next_new[NUM_RTX_CODE];
1085 /* We also need codes and names for insn notes (not register notes).
1086 Note that we do *not* bias the note values here. */
1087 enum insn_note
1089 #define DEF_INSN_NOTE(NAME) NAME,
1090 #include "insn-notes.def"
1091 #undef DEF_INSN_NOTE
1093 NOTE_INSN_MAX
1096 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1097 default field for line number notes. */
1098 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
1099 #define DEF_INSN_NOTE(NAME) #NAME,
1100 #include "insn-notes.def"
1101 #undef DEF_INSN_NOTE
1104 #undef CONST_DOUBLE_FORMAT
1105 #define GENERATOR_FILE
1107 /* Generate the contents of the rtx_next array. This really doesn't belong
1108 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1110 static void
1111 gen_rtx_next (void)
1113 int i;
1114 for (i = 0; i < NUM_RTX_CODE; i++)
1116 int k;
1118 rtx_next_new[i] = -1;
1119 if (startswith (rtx_format[i], "uu"))
1120 rtx_next_new[i] = 1;
1121 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1122 rtx_next_new[i] = 1;
1123 else
1124 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
1125 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1126 rtx_next_new[i] = k;
1130 /* Write out the contents of the rtx_next array. */
1131 static void
1132 write_rtx_next (void)
1134 outf_p f = get_output_file_with_visibility (NULL);
1135 int i;
1136 if (!f)
1137 return;
1139 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
1140 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1141 for (i = 0; i < NUM_RTX_CODE; i++)
1142 if (rtx_next_new[i] == -1)
1143 oprintf (f, " 0,\n");
1144 else
1145 oprintf (f,
1146 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1147 oprintf (f, "};\n");
1150 /* Handle `special("rtx_def")'. This is a special case for field
1151 `fld' of struct rtx_def, which is an array of unions whose values
1152 are based in a complex way on the type of RTL. */
1154 static type_p
1155 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1157 pair_p flds = NULL;
1158 options_p nodot;
1159 int i;
1160 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1161 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1163 if (t->kind != TYPE_UNION)
1165 error_at_line (&lexer_line,
1166 "special `rtx_def' must be applied to a union");
1167 return &string_type;
1170 nodot = create_string_option (NULL, "dot", "");
1172 rtx_tp = create_pointer (find_structure ("rtx_def", TYPE_STRUCT));
1173 rtvec_tp = create_pointer (find_structure ("rtvec_def", TYPE_STRUCT));
1174 tree_tp = create_pointer (find_structure ("tree_node", TYPE_UNION));
1175 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", TYPE_STRUCT));
1176 reg_attrs_tp =
1177 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT));
1178 basic_block_tp =
1179 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT));
1180 constant_tp =
1181 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT));
1182 scalar_tp = &scalar_nonchar; /* rtunion int */
1185 pair_p note_flds = NULL;
1186 int c;
1188 for (c = 0; c <= NOTE_INSN_MAX; c++)
1190 switch (c)
1192 case NOTE_INSN_MAX:
1193 case NOTE_INSN_DELETED_LABEL:
1194 case NOTE_INSN_DELETED_DEBUG_LABEL:
1195 note_flds = create_field (note_flds, &string_type, "rt_str");
1196 break;
1198 case NOTE_INSN_BLOCK_BEG:
1199 case NOTE_INSN_BLOCK_END:
1200 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1201 break;
1203 case NOTE_INSN_VAR_LOCATION:
1204 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1205 break;
1207 default:
1208 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1209 break;
1211 /* NOTE_INSN_MAX is used as the default field for line
1212 number notes. */
1213 if (c == NOTE_INSN_MAX)
1214 note_flds->opt =
1215 create_string_option (nodot, "default", "");
1216 else
1217 note_flds->opt =
1218 create_string_option (nodot, "tag", note_insn_name[c]);
1220 note_union_tp = new_structure ("rtx_def_note_subunion", TYPE_UNION,
1221 &lexer_line, note_flds, NULL, NULL);
1223 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1225 pair_p sym_flds;
1226 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1227 sym_flds->opt = create_string_option (nodot, "default", "");
1228 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1229 sym_flds->opt = create_string_option (nodot, "tag", "1");
1230 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", TYPE_UNION,
1231 &lexer_line, sym_flds, NULL, NULL);
1233 for (i = 0; i < NUM_RTX_CODE; i++)
1235 pair_p subfields = NULL;
1236 size_t aindex, nmindex;
1237 const char *sname;
1238 type_p substruct;
1239 char *ftag;
1241 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1243 type_p t;
1244 const char *subname;
1246 switch (rtx_format[i][aindex])
1248 case '*':
1249 case 'i':
1250 case 'n':
1251 case 'w':
1252 case 'r':
1253 t = scalar_tp;
1254 subname = "rt_int";
1255 break;
1257 case 'p':
1258 t = scalar_tp;
1259 subname = "rt_subreg";
1260 break;
1262 case '0':
1263 if (i == MEM && aindex == 1)
1264 t = mem_attrs_tp, subname = "rt_mem";
1265 else if (i == JUMP_INSN && aindex == 7)
1266 t = rtx_tp, subname = "rt_rtx";
1267 else if (i == CODE_LABEL && aindex == 4)
1268 t = scalar_tp, subname = "rt_int";
1269 else if (i == CODE_LABEL && aindex == 3)
1270 t = rtx_tp, subname = "rt_rtx";
1271 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1272 t = rtx_tp, subname = "rt_rtx";
1273 else if (i == NOTE && aindex == 3)
1274 t = note_union_tp, subname = "";
1275 else if (i == NOTE && aindex == 4)
1276 t = scalar_tp, subname = "rt_int";
1277 else if (i == NOTE && aindex >= 6)
1278 t = scalar_tp, subname = "rt_int";
1279 else if (i == ADDR_DIFF_VEC && aindex == 4)
1280 t = scalar_tp, subname = "rt_int";
1281 else if (i == VALUE && aindex == 0)
1282 t = scalar_tp, subname = "rt_int";
1283 else if (i == DEBUG_EXPR && aindex == 0)
1284 t = tree_tp, subname = "rt_tree";
1285 else if (i == SYMBOL_REF && aindex == 1)
1286 t = symbol_union_tp, subname = "";
1287 else if (i == JUMP_TABLE_DATA && aindex >= 4)
1288 t = scalar_tp, subname = "rt_int";
1289 else if (i == BARRIER && aindex >= 2)
1290 t = scalar_tp, subname = "rt_int";
1291 else if (i == ENTRY_VALUE && aindex == 0)
1292 t = rtx_tp, subname = "rt_rtx";
1293 else
1295 error_at_line
1296 (&lexer_line,
1297 "rtx type `%s' has `0' in position "
1298 HOST_SIZE_T_PRINT_UNSIGNED ", can't handle",
1299 rtx_name[i], (fmt_size_t) aindex);
1300 t = &string_type;
1301 subname = "rt_int";
1303 break;
1305 case 's':
1306 case 'S':
1307 case 'T':
1308 t = &string_type;
1309 subname = "rt_str";
1310 break;
1312 case 'e':
1313 case 'u':
1314 t = rtx_tp;
1315 subname = "rt_rtx";
1316 break;
1318 case 'E':
1319 case 'V':
1320 t = rtvec_tp;
1321 subname = "rt_rtvec";
1322 break;
1324 case 't':
1325 t = tree_tp;
1326 subname = "rt_tree";
1327 break;
1329 case 'B':
1330 t = basic_block_tp;
1331 subname = "rt_bb";
1332 break;
1334 default:
1335 error_at_line
1336 (&lexer_line,
1337 "rtx type `%s' has `%c' in position "
1338 HOST_SIZE_T_PRINT_UNSIGNED ", can't handle",
1339 rtx_name[i], rtx_format[i][aindex],
1340 (fmt_size_t) aindex);
1341 t = &string_type;
1342 subname = "rt_int";
1343 break;
1346 subfields = create_field (subfields, t,
1347 xasprintf (".fld["
1348 HOST_SIZE_T_PRINT_UNSIGNED
1349 "].%s",
1350 (fmt_size_t) aindex,
1351 subname));
1352 subfields->opt = nodot;
1353 if (t == note_union_tp)
1354 subfields->opt =
1355 create_string_option (subfields->opt, "desc",
1356 "NOTE_KIND (&%0)");
1357 if (t == symbol_union_tp)
1358 subfields->opt =
1359 create_string_option (subfields->opt, "desc",
1360 "CONSTANT_POOL_ADDRESS_P (&%0)");
1363 if (i == REG)
1364 subfields = create_field (subfields, reg_attrs_tp, "reg.attrs");
1366 if (i == SYMBOL_REF)
1368 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1369 holds. */
1370 type_p field_tp = find_structure ("block_symbol", TYPE_STRUCT);
1371 subfields
1372 = create_optional_field (subfields, field_tp, "block_sym",
1373 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1376 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1377 substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
1378 NULL, NULL);
1380 ftag = xstrdup (rtx_name[i]);
1381 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1382 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1383 flds = create_field (flds, substruct, "");
1384 flds->opt = create_string_option (nodot, "tag", ftag);
1386 return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
1387 nodot, NULL);
1390 /* Perform any special processing on a type T, about to become the type
1391 of a field. Return the appropriate type for the field.
1392 At present:
1393 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1394 - Similarly for arrays of pointer-to-char;
1395 - Handles "special" options.
1398 type_p
1399 adjust_field_type (type_p t, options_p opt)
1401 int length_p = 0;
1402 const int pointer_p = t->kind == TYPE_POINTER;
1404 for (; opt; opt = opt->next)
1405 if (strcmp (opt->name, "length") == 0)
1407 if (length_p)
1408 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1409 if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
1411 error_at_line (&lexer_line,
1412 "option `%s' may not be applied to "
1413 "arrays of atomic types", opt->name);
1415 length_p = 1;
1417 else if (strcmp (opt->name, "special") == 0
1418 && opt->kind == OPTION_STRING)
1420 const char *special_name = opt->info.string;
1421 if (strcmp (special_name, "rtx_def") == 0)
1422 t = adjust_field_rtx_def (t, opt);
1423 else
1424 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1427 if (!length_p
1428 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1429 return &string_type;
1430 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1431 && t->u.a.p->u.p->kind == TYPE_SCALAR
1432 && t->u.a.p->u.p->u.scalar_is_char)
1433 return create_array (&string_type, t->u.a.len);
1435 return t;
1439 static void set_gc_used_type (type_p, enum gc_used_enum, bool = false);
1440 static void set_gc_used (pair_p);
1442 /* Handle OPT for set_gc_used_type. */
1444 static void
1445 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1446 int *length, int *skip, int *callback, type_p *nested_ptr)
1448 options_p o;
1449 for (o = opt; o; o = o->next)
1450 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1451 && o->kind == OPTION_TYPE)
1452 set_gc_used_type (o->info.type,
1453 GC_POINTED_TO);
1454 else if (strcmp (o->name, "maybe_undef") == 0)
1455 *maybe_undef = 1;
1456 else if (strcmp (o->name, "length") == 0)
1457 *length = 1;
1458 else if (strcmp (o->name, "skip") == 0)
1459 *skip = 1;
1460 else if (strcmp (o->name, "callback") == 0)
1461 *callback = 1;
1462 else if (strcmp (o->name, "nested_ptr") == 0
1463 && o->kind == OPTION_NESTED)
1464 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1468 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1470 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1471 are set to GC_UNUSED. Otherwise, an error is emitted for
1472 TYPE_UNDEFINED types. This is used to support user-defined
1473 template types with non-type arguments.
1475 For instance, when we parse a template type with enum arguments
1476 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1477 artificial fields for 'MyType', one for 'AnotherType', the other
1478 one for 'EnumValue'.
1480 At the time that we parse this type we don't know that 'EnumValue'
1481 is really an enum value, so the parser creates a TYPE_UNDEFINED
1482 type for it. Since 'EnumValue' is never resolved to a known
1483 structure, it will stay with TYPE_UNDEFINED.
1485 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1486 'EnumValue'. Generating marking code for it would cause
1487 compilation failures since the marking routines assumes that
1488 'EnumValue' is a type. */
1490 static void
1491 set_gc_used_type (type_p t, enum gc_used_enum level,
1492 bool allow_undefined_types)
1494 if (t->gc_used >= level)
1495 return;
1497 t->gc_used = level;
1499 switch (t->kind)
1501 case TYPE_STRUCT:
1502 case TYPE_UNION:
1503 case TYPE_USER_STRUCT:
1505 pair_p f;
1506 int dummy;
1507 type_p dummy2;
1508 bool allow_undefined_field_types = (t->kind == TYPE_USER_STRUCT);
1510 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
1511 &dummy2);
1513 if (t->u.s.base_class)
1514 set_gc_used_type (t->u.s.base_class, level, allow_undefined_types);
1515 /* Anything pointing to a base class might actually be pointing
1516 to a subclass. */
1517 for (type_p subclass = t->u.s.first_subclass; subclass;
1518 subclass = subclass->u.s.next_sibling_class)
1519 set_gc_used_type (subclass, level, allow_undefined_types);
1521 FOR_ALL_INHERITED_FIELDS(t, f)
1523 int maybe_undef = 0;
1524 int length = 0;
1525 int skip = 0;
1526 int callback = 0;
1527 type_p nested_ptr = NULL;
1528 process_gc_options (f->opt, level, &maybe_undef, &length, &skip,
1529 &callback, &nested_ptr);
1531 if (nested_ptr && f->type->kind == TYPE_POINTER)
1532 set_gc_used_type (nested_ptr, GC_POINTED_TO);
1533 else if (length && f->type->kind == TYPE_POINTER)
1534 set_gc_used_type (f->type->u.p, GC_USED);
1535 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1536 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO);
1537 else if (skip)
1538 ; /* target type is not used through this field */
1539 else if (callback)
1540 f->type = &callback_type;
1541 else
1542 set_gc_used_type (f->type, GC_USED, allow_undefined_field_types);
1544 break;
1547 case TYPE_UNDEFINED:
1548 if (level > GC_UNUSED)
1550 if (!allow_undefined_types)
1551 error_at_line (&t->u.s.line, "undefined type `%s'", t->u.s.tag);
1552 t->gc_used = GC_UNUSED;
1554 break;
1556 case TYPE_POINTER:
1557 set_gc_used_type (t->u.p, GC_POINTED_TO);
1558 break;
1560 case TYPE_ARRAY:
1561 set_gc_used_type (t->u.a.p, GC_USED);
1562 break;
1564 case TYPE_LANG_STRUCT:
1565 for (t = t->u.s.lang_struct; t; t = t->next)
1566 set_gc_used_type (t, level);
1567 break;
1569 default:
1570 break;
1574 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1576 static void
1577 set_gc_used (pair_p variables)
1579 int nbvars = 0;
1580 pair_p p;
1581 for (p = variables; p; p = p->next)
1583 set_gc_used_type (p->type, GC_USED);
1584 nbvars++;
1586 if (verbosity_level >= 2)
1587 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1590 /* File mapping routines. For each input file, there is one output .cc file
1591 (but some output files have many input files), and there is one .h file
1592 for the whole build. */
1594 /* Output file handling. */
1596 /* Create and return an outf_p for a new file for NAME, to be called
1597 ONAME. */
1599 static outf_p
1600 create_file (const char *name, const char *oname)
1602 static const char *const hdr[] = {
1603 " Copyright (C) 2004-2024 Free Software Foundation, Inc.\n",
1604 "\n",
1605 "This file is part of GCC.\n",
1606 "\n",
1607 "GCC is free software; you can redistribute it and/or modify it under\n",
1608 "the terms of the GNU General Public License as published by the Free\n",
1609 "Software Foundation; either version 3, or (at your option) any later\n",
1610 "version.\n",
1611 "\n",
1612 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1613 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1614 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1615 "for more details.\n",
1616 "\n",
1617 "You should have received a copy of the GNU General Public License\n",
1618 "along with GCC; see the file COPYING3. If not see\n",
1619 "<http://www.gnu.org/licenses/>. */\n",
1620 "\n",
1621 "/* This file is machine generated. Do not edit. */\n"
1623 outf_p f;
1624 size_t i;
1626 gcc_assert (name != NULL);
1627 gcc_assert (oname != NULL);
1628 f = XCNEW (struct outf);
1629 f->next = output_files;
1630 f->name = oname;
1631 output_files = f;
1633 oprintf (f, "/* Type information for %s.\n", name);
1634 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1635 oprintf (f, "%s", hdr[i]);
1636 return f;
1639 /* Print, like fprintf, to O.
1640 N.B. You might think this could be implemented more efficiently
1641 with vsnprintf(). Unfortunately, there are C libraries that
1642 provide that function but without the C99 semantics for its return
1643 value, making it impossible to know how much space is required. */
1644 void
1645 oprintf (outf_p o, const char *format, ...)
1647 char *s;
1648 size_t slength;
1649 va_list ap;
1651 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1652 in that case. */
1653 if (!o)
1654 return;
1656 va_start (ap, format);
1657 slength = vasprintf (&s, format, ap);
1658 if (s == NULL || (int) slength < 0)
1659 fatal ("out of memory");
1660 va_end (ap);
1662 if (o->bufused + slength > o->buflength)
1664 size_t new_len = o->buflength;
1665 if (new_len == 0)
1666 new_len = 1024;
1669 new_len *= 2;
1671 while (o->bufused + slength >= new_len);
1672 o->buf = XRESIZEVEC (char, o->buf, new_len);
1673 o->buflength = new_len;
1675 memcpy (o->buf + o->bufused, s, slength);
1676 o->bufused += slength;
1677 free (s);
1680 /* Open the global header file and the language-specific header files. */
1682 static void
1683 open_base_files (void)
1685 size_t i;
1687 if (nb_plugin_files > 0 && plugin_files)
1688 return;
1690 header_file = create_file ("GCC", "gtype-desc.h");
1692 base_files = XNEWVEC (outf_p, num_lang_dirs);
1694 for (i = 0; i < num_lang_dirs; i++)
1695 base_files[i] = create_file (lang_dir_names[i],
1696 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1698 /* gtype-desc.cc is a little special, so we create it here. */
1700 /* The order of files here matters very much. */
1701 static const char *const ifiles[] = {
1702 "config.h", "system.h", "coretypes.h",
1703 "backend.h", "predict.h", "tree.h",
1704 "rtl.h", "gimple.h", "fold-const.h", "insn-codes.h", "splay-tree.h",
1705 "alias.h", "insn-config.h", "flags.h", "expmed.h", "dojump.h",
1706 "explow.h", "calls.h", "memmodel.h", "emit-rtl.h", "varasm.h",
1707 "stmt.h", "expr.h", "alloc-pool.h", "cselib.h", "insn-addr.h",
1708 "optabs.h", "libfuncs.h", "debug.h", "internal-fn.h",
1709 "gimple-iterator.h", "gimple-fold.h", "value-range.h",
1710 "value-range-storage.h",
1711 "tree-eh.h", "gimple-ssa.h", "tree-cfg.h",
1712 "tree-vrp.h", "tree-phinodes.h", "ssa-iterators.h", "stringpool.h",
1713 "tree-ssanames.h", "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h",
1714 "tree-ssa-loop-manip.h", "tree-ssa-loop-niter.h", "tree-into-ssa.h",
1715 "tree-dfa.h", "tree-ssa.h", "reload.h", "cpplib.h", "tree-chrec.h",
1716 "except.h", "output.h", "cfgloop.h", "target.h", "lto-streamer.h",
1717 "target-globals.h", "ipa-ref.h", "cgraph.h", "symbol-summary.h",
1718 "sreal.h", "ipa-cp.h", "ipa-prop.h", "ipa-fnsummary.h", "dwarf2out.h",
1719 "omp-general.h", "omp-offload.h", "ipa-modref-tree.h", "ipa-modref.h",
1720 "symtab-thunks.h", "symtab-clones.h", "diagnostic-spec.h", "ctfc.h",
1721 NULL
1723 const char *const *ifp;
1724 outf_p gtype_desc_c;
1726 gtype_desc_c = create_file ("GCC", "gtype-desc.cc");
1727 for (ifp = ifiles; *ifp; ifp++)
1728 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1729 for (int j = 0; j < (int) num_build_headers; j++)
1730 oprintf (gtype_desc_c, "#include \"%s\"\n", build_headers[j]);
1732 /* Make sure we handle "cfun" specially. */
1733 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1734 oprintf (gtype_desc_c, "#undef cfun\n");
1736 oprintf (gtype_desc_c,
1737 "\n"
1738 "/* Types with a \"gcc::\" namespace have it stripped\n"
1739 " during gengtype parsing. Provide a \"using\" directive\n"
1740 " to ensure that the fully-qualified types are found. */\n"
1741 "using namespace gcc;\n");
1745 /* For INPF an input file, return the real basename of INPF, with all
1746 the directory components skipped. */
1748 static const char *
1749 get_file_realbasename (const input_file *inpf)
1751 return lbasename (get_input_file_name (inpf));
1754 /* For INPF a filename, return the relative path to INPF from
1755 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1757 const char *
1758 get_file_srcdir_relative_path (const input_file *inpf)
1760 const char *f = get_input_file_name (inpf);
1761 if (strlen (f) > srcdir_len
1762 && IS_DIR_SEPARATOR (f[srcdir_len])
1763 && strncmp (f, srcdir, srcdir_len) == 0)
1764 return f + srcdir_len + 1;
1765 else
1766 return NULL;
1769 /* For INPF an input_file, return the relative path to INPF from
1770 $(srcdir) if the latter is a prefix in INPF, or the real basename
1771 of INPF otherwise. */
1773 static const char *
1774 get_file_basename (const input_file *inpf)
1776 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1778 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1781 /* For F a filename, return the lang_dir_names relative index of the language
1782 directory that is a prefix in F, if any, -1 otherwise. */
1784 static int
1785 get_prefix_langdir_index (const char *f)
1787 size_t f_len = strlen (f);
1788 size_t lang_index;
1790 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1792 const char *langdir = lang_dir_names[lang_index];
1793 size_t langdir_len = strlen (langdir);
1795 if (f_len > langdir_len
1796 && IS_DIR_SEPARATOR (f[langdir_len])
1797 && memcmp (f, langdir, langdir_len) == 0)
1798 return lang_index;
1801 return -1;
1804 /* For INPF an input file, return the name of language directory where
1805 F is located, if any, NULL otherwise. */
1807 static const char *
1808 get_file_langdir (const input_file *inpf)
1810 /* Get the relative path to INPF from $(srcdir) and find the
1811 language by comparing the prefix with language directory names.
1812 If INPF is not even srcdir relative, no point in looking
1813 further. */
1815 int lang_index;
1816 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1817 const char *r;
1819 if (!srcdir_relative_path)
1820 return NULL;
1822 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1823 if (lang_index < 0 && startswith (srcdir_relative_path, "c-family"))
1824 r = "c-family";
1825 else if (lang_index >= 0)
1826 r = lang_dir_names[lang_index];
1827 else
1828 r = NULL;
1830 return r;
1833 /* The gt- output file name for INPF. */
1835 static const char *
1836 get_file_gtfilename (const input_file *inpf)
1838 /* Cook up an initial version of the gt- file name from the file real
1839 basename and the language name, if any. */
1841 const char *basename = get_file_realbasename (inpf);
1842 const char *langdir = get_file_langdir (inpf);
1844 char *result =
1845 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1846 : xasprintf ("gt-%s", basename));
1848 /* Then replace all non alphanumerics characters by '-' and change the
1849 extension to ".h". We expect the input filename extension was at least
1850 one character long. */
1852 char *s = result;
1854 for (; *s != '.'; s++)
1855 if (!ISALNUM (*s) && *s != '-')
1856 *s = '-';
1858 memcpy (s, ".h", sizeof (".h"));
1860 return result;
1863 /* Each input_file has its associated output file outf_p. The
1864 association is computed by the function
1865 get_output_file_with_visibility. The associated file is cached
1866 inside input_file in its inpoutf field, so is really computed only
1867 once. Associated output file paths (i.e. output_name-s) are
1868 computed by a rule based regexp machinery, using the files_rules
1869 array of struct file_rule_st. A for_name is also computed, giving
1870 the source file name for which the output_file is generated; it is
1871 often the last component of the input_file path. */
1875 Regexpr machinery to compute the output_name and for_name-s of each
1876 input_file. We have a sequence of file rules which gives the POSIX
1877 extended regular expression to match an input file path, and two
1878 transformed strings for the corresponding output_name and the
1879 corresponding for_name. The transformed string contain dollars: $0
1880 is replaced by the entire match, $1 is replaced by the substring
1881 matching the first parenthesis in the regexp, etc. And $$ is replaced
1882 by a single verbatim dollar. The rule order is important. The
1883 general case is last, and the particular cases should come before.
1884 An action routine can, when needed, update the out_name & for_name
1885 and/or return the appropriate output file. It is invoked only when a
1886 rule is triggered. When a rule is triggered, the output_name and
1887 for_name are computed using their transform string in while $$, $0,
1888 $1, ... are suitably replaced. If there is an action, it is called.
1889 In some few cases, the action can directly return the outf_p, but
1890 usually it just updates the output_name and for_name so should free
1891 them before replacing them. The get_output_file_with_visibility
1892 function creates an outf_p only once per each output_name, so it
1893 scans the output_files list for previously seen output file names.
1896 /* Signature of actions in file rules. */
1897 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
1900 struct file_rule_st {
1901 const char* frul_srcexpr; /* Source string for regexp. */
1902 int frul_rflags; /* Flags passed to regcomp, usually
1903 * REG_EXTENDED. */
1904 regex_t* frul_re; /* Compiled regular expression
1905 obtained by regcomp. */
1906 const char* frul_tr_out; /* Transformation string for making
1907 * the output_name, with $1 ... $9 for
1908 * subpatterns and $0 for the whole
1909 * matched filename. */
1910 const char* frul_tr_for; /* Tranformation string for making the
1911 for_name. */
1912 frul_actionrout_t* frul_action; /* The action, if non null, is
1913 * called once the rule matches, on
1914 * the transformed out_name &
1915 * for_name. It could change them
1916 * and/or give the output file. */
1919 /* File rule action handling *.h files. */
1920 static outf_p header_dot_h_frul (input_file*, char**, char**);
1922 /* File rule action handling *.cc files. */
1923 static outf_p source_dot_cc_frul (input_file*, char**, char**);
1925 #define NULL_REGEX (regex_t*)0
1927 /* The prefix in our regexp-s matching the directory. */
1928 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1930 #define NULL_FRULACT (frul_actionrout_t*)0
1932 /* The array of our rules governing file name generation. Rules order
1933 matters, so change with extreme care! */
1935 struct file_rule_st files_rules[] = {
1936 /* The general rule assumes that files in subdirectories belong to a
1937 particular front-end, and files not in subdirectories are shared.
1938 The following rules deal with exceptions - files that are in
1939 subdirectories and yet are shared, and files that are top-level,
1940 but are not shared. */
1942 /* the c-family/ source directory is special. */
1943 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.cc$",
1944 REG_EXTENDED, NULL_REGEX,
1945 "gt-c-family-$3.h", "c-family/$3.cc", NULL_FRULACT},
1947 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
1948 REG_EXTENDED, NULL_REGEX,
1949 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
1951 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.cc ! */
1952 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
1953 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.cc", NULL_FRULACT},
1955 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
1956 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.cc", NULL_FRULACT},
1958 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.cc ! */
1959 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
1960 REG_EXTENDED, NULL_REGEX,
1961 "gt-cp-tree.h", "cp/tree.cc", NULL_FRULACT },
1963 /* cp/decl.h & cp/decl.cc gives gt-cp-decl.h for cp/decl.cc ! */
1964 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
1965 REG_EXTENDED, NULL_REGEX,
1966 "gt-cp-decl.h", "cp/decl.cc", NULL_FRULACT },
1968 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.cc ! */
1969 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
1970 REG_EXTENDED, NULL_REGEX,
1971 "gt-cp-name-lookup.h", "cp/name-lookup.cc", NULL_FRULACT },
1973 /* cp/parser.h gives gt-cp-parser.h for cp/parser.cc ! */
1974 { DIR_PREFIX_REGEX "cp/parser\\.h$",
1975 REG_EXTENDED, NULL_REGEX,
1976 "gt-cp-parser.h", "cp/parser.cc", NULL_FRULACT },
1978 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.cc ! */
1979 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
1980 REG_EXTENDED, NULL_REGEX,
1981 "gt-objc-objc-act.h", "objc/objc-act.cc", NULL_FRULACT },
1983 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.cc ! */
1984 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
1985 REG_EXTENDED, NULL_REGEX,
1986 "gt-objc-objc-map.h", "objc/objc-map.cc", NULL_FRULACT },
1988 /* General cases. For header *.h and *.cc files, we
1989 * need special actions to handle the language. */
1991 /* Source *.cc files are using get_file_gtfilename to compute their
1992 output_name and get_file_basename to compute their for_name
1993 through the source_dot_cc_frul action. */
1994 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.cc$",
1995 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.cc", source_dot_cc_frul},
1997 /* Common header files get "gtype-desc.cc" as their output_name,
1998 * while language specific header files are handled specially. So
1999 * we need the header_dot_h_frul action. */
2000 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
2001 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
2003 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
2004 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
2006 /* Mandatory null last entry signaling end of rules. */
2007 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
2010 /* Special file rules action for handling *.h header files. It gives
2011 "gtype-desc.cc" for common headers and corresponding output
2012 files for language-specific header files. */
2013 static outf_p
2014 header_dot_h_frul (input_file* inpf, char**poutname,
2015 char**pforname ATTRIBUTE_UNUSED)
2017 const char *basename = 0;
2018 int lang_index = 0;
2019 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2020 (void*) inpf, get_input_file_name (inpf),
2021 *poutname, *pforname);
2022 basename = get_file_basename (inpf);
2023 lang_index = get_prefix_langdir_index (basename);
2024 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
2026 if (lang_index >= 0)
2028 /* The header is language specific. Given output_name &
2029 for_name remains unchanged. The base_files array gives the
2030 outf_p. */
2031 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2032 (void*) base_files[lang_index],
2033 (base_files[lang_index])->name);
2034 return base_files[lang_index];
2036 else
2038 /* The header is common to all front-end languages. So
2039 output_name is "gtype-desc.cc" file. The calling function
2040 get_output_file_with_visibility will find its outf_p. */
2041 free (*poutname);
2042 *poutname = xstrdup ("gtype-desc.cc");
2043 DBGPRINTF ("special 'gtype-desc.cc' for inpname %s",
2044 get_input_file_name (inpf));
2045 return NULL;
2050 /* Special file rules action for handling *.cc source files using
2051 * get_file_gtfilename to compute their output_name and
2052 * get_file_basename to compute their for_name. The output_name is
2053 * gt-<LANG>-<BASE>.h for language specific source files, and
2054 * gt-<BASE>.h for common source files. */
2055 static outf_p
2056 source_dot_cc_frul (input_file* inpf, char**poutname, char**pforname)
2058 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
2059 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
2060 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2061 (void*) inpf, get_input_file_name (inpf),
2062 *poutname, *pforname);
2063 DBGPRINTF ("newoutname %s", newoutname);
2064 DBGPRINTF ("newbasename %s", newbasename);
2065 free (*poutname);
2066 free (*pforname);
2067 *poutname = newoutname;
2068 *pforname = newbasename;
2069 return NULL;
2072 /* Utility function for get_output_file_with_visibility which returns
2073 * a malloc-ed substituted string using TRS on matching of the FILNAM
2074 * file name, using the PMATCH array. */
2075 static char*
2076 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
2077 const char *trs)
2079 struct obstack str_obstack;
2080 char *str = NULL;
2081 char *rawstr = NULL;
2082 const char *pt = NULL;
2083 DBGPRINTF ("filnam %s", filnam);
2084 obstack_init (&str_obstack);
2085 for (pt = trs; *pt; pt++) {
2086 char c = *pt;
2087 if (c == '$')
2089 if (pt[1] == '$')
2091 /* A double dollar $$ is substituted by a single verbatim
2092 dollar, but who really uses dollar signs in file
2093 paths? */
2094 obstack_1grow (&str_obstack, '$');
2096 else if (ISDIGIT (pt[1]))
2098 /* Handle $0 $1 ... $9 by appropriate substitution. */
2099 int dolnum = pt[1] - '0';
2100 int so = pmatch[dolnum].rm_so;
2101 int eo = pmatch[dolnum].rm_eo;
2102 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2103 if (so>=0 && eo>=so)
2104 obstack_grow (&str_obstack, filnam + so, eo - so);
2106 else
2108 /* This can happen only when files_rules is buggy! */
2109 gcc_unreachable ();
2111 /* Always skip the character after the dollar. */
2112 pt++;
2114 else
2115 obstack_1grow (&str_obstack, c);
2117 obstack_1grow (&str_obstack, '\0');
2118 rawstr = XOBFINISH (&str_obstack, char *);
2119 str = xstrdup (rawstr);
2120 obstack_free (&str_obstack, NULL);
2121 DBGPRINTF ("matched replacement %s", str);
2122 rawstr = NULL;
2123 return str;
2127 /* An output file, suitable for definitions, that can see declarations
2128 made in INPF and is linked into every language that uses INPF.
2129 Since the result is cached inside INPF, that argument cannot be
2130 declared constant, but is "almost" constant. */
2132 outf_p
2133 get_output_file_with_visibility (input_file *inpf)
2135 outf_p r;
2136 char *for_name = NULL;
2137 char *output_name = NULL;
2138 const char* inpfname;
2140 /* This can happen when we need a file with visibility on a
2141 structure that we've never seen. We have to just hope that it's
2142 globally visible. */
2143 if (inpf == NULL)
2144 inpf = system_h_file;
2146 /* The result is cached in INPF, so return it if already known. */
2147 if (inpf->inpoutf)
2148 return inpf->inpoutf;
2150 /* In plugin mode, return NULL unless the input_file is one of the
2151 plugin_files. */
2152 if (plugin_files)
2154 size_t i;
2155 for (i = 0; i < nb_plugin_files; i++)
2156 if (inpf == plugin_files[i])
2158 inpf->inpoutf = plugin_output;
2159 return plugin_output;
2162 return NULL;
2165 inpfname = get_input_file_name (inpf);
2167 /* Try each rule in sequence in files_rules until one is triggered. */
2169 int rulix = 0;
2170 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2171 (void*) inpf, inpfname);
2173 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2175 DBGPRINTF ("rulix#%d srcexpr %s",
2176 rulix, files_rules[rulix].frul_srcexpr);
2178 if (!files_rules[rulix].frul_re)
2180 /* Compile the regexpr lazily. */
2181 int err = 0;
2182 files_rules[rulix].frul_re = XCNEW (regex_t);
2183 err = regcomp (files_rules[rulix].frul_re,
2184 files_rules[rulix].frul_srcexpr,
2185 files_rules[rulix].frul_rflags);
2186 if (err)
2188 /* The regular expression compilation fails only when
2189 file_rules is buggy. */
2190 gcc_unreachable ();
2194 output_name = NULL;
2195 for_name = NULL;
2197 /* Match the regexpr and trigger the rule if matched. */
2199 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2200 $3, ... $9. */
2201 regmatch_t pmatch[10];
2202 memset (pmatch, 0, sizeof (pmatch));
2203 if (!regexec (files_rules[rulix].frul_re,
2204 inpfname, 10, pmatch, 0))
2206 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2207 (void*) inpf, inpfname, rulix,
2208 files_rules[rulix].frul_srcexpr);
2209 for_name =
2210 matching_file_name_substitute (inpfname, pmatch,
2211 files_rules[rulix].frul_tr_for);
2212 DBGPRINTF ("for_name %s", for_name);
2213 output_name =
2214 matching_file_name_substitute (inpfname, pmatch,
2215 files_rules[rulix].frul_tr_out);
2216 DBGPRINTF ("output_name %s", output_name);
2217 if (files_rules[rulix].frul_action)
2219 /* Invoke our action routine. */
2220 outf_p of = NULL;
2221 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2222 rulix, output_name, for_name);
2223 of =
2224 (files_rules[rulix].frul_action) (inpf,
2225 &output_name, &for_name);
2226 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2227 rulix, (void*)of, output_name, for_name);
2228 /* If the action routine returned something, give it back
2229 immediately and cache it in inpf. */
2230 if (of)
2232 inpf->inpoutf = of;
2233 return of;
2236 /* The rule matched, and had no action, or that action did
2237 not return any output file but could have changed the
2238 output_name or for_name. We break out of the loop on the
2239 files_rules. */
2240 break;
2242 else
2244 /* The regexpr did not match. */
2245 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2246 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2247 continue;
2252 if (!output_name || !for_name)
2254 /* This should not be possible, and could only happen if the
2255 files_rules is incomplete or buggy. */
2256 fatal ("failed to compute output name for %s", inpfname);
2259 /* Look through to see if we've ever seen this output filename
2260 before. If found, cache the result in inpf. */
2261 for (r = output_files; r; r = r->next)
2262 if (filename_cmp (r->name, output_name) == 0)
2264 inpf->inpoutf = r;
2265 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2266 output_name, for_name);
2267 return r;
2270 /* If not found, create it, and cache it in inpf. */
2271 r = create_file (for_name, output_name);
2273 gcc_assert (r && r->name);
2274 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2275 output_name, for_name);
2276 inpf->inpoutf = r;
2277 return r;
2282 /* The name of an output file, suitable for definitions, that can see
2283 declarations made in INPF and is linked into every language that
2284 uses INPF. */
2286 const char *
2287 get_output_file_name (input_file* inpf)
2289 outf_p o = get_output_file_with_visibility (inpf);
2290 if (o)
2291 return o->name;
2292 return NULL;
2295 /* Check if existing file is equal to the in memory buffer. */
2297 static bool
2298 is_file_equal (outf_p of)
2300 FILE *newfile = fopen (of->name, "r");
2301 size_t i;
2302 bool equal;
2303 if (newfile == NULL)
2304 return false;
2306 equal = true;
2307 for (i = 0; i < of->bufused; i++)
2309 int ch;
2310 ch = fgetc (newfile);
2311 if (ch == EOF || ch != (unsigned char) of->buf[i])
2313 equal = false;
2314 break;
2317 if (equal && EOF != fgetc (newfile))
2318 equal = false;
2319 fclose (newfile);
2320 return equal;
2323 /* Copy the output to its final destination,
2324 but don't unnecessarily change modification times. */
2326 static void
2327 close_output_files (void)
2329 int nbwrittenfiles = 0;
2330 outf_p of;
2332 for (of = output_files; of; of = of->next)
2334 if (!is_file_equal (of))
2336 FILE *newfile = NULL;
2337 char *backupname = NULL;
2338 /* Back up the old version of the output file gt-FOO.cc as
2339 BACKUPDIR/gt-FOO.cc~ if we have a backup directory. */
2340 if (backup_dir)
2342 backupname = concat (backup_dir, "/",
2343 lbasename (of->name), "~", NULL);
2344 if (!access (of->name, F_OK) && rename (of->name, backupname))
2345 fatal ("failed to back up %s as %s: %s",
2346 of->name, backupname, xstrerror (errno));
2349 newfile = fopen (of->name, "w");
2350 if (newfile == NULL)
2351 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2352 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2353 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2354 if (fclose (newfile) != 0)
2355 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2356 nbwrittenfiles++;
2357 if (verbosity_level >= 2 && backupname)
2358 printf ("%s wrote #%-3d %s backed-up in %s\n",
2359 progname, nbwrittenfiles, of->name, backupname);
2360 else if (verbosity_level >= 1)
2361 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2362 free (backupname);
2364 else
2366 /* output file remains unchanged. */
2367 if (verbosity_level >= 2)
2368 printf ("%s keep %s\n", progname, of->name);
2370 free (of->buf);
2371 of->buf = NULL;
2372 of->bufused = of->buflength = 0;
2374 if (verbosity_level >= 1)
2375 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2378 struct flist
2380 struct flist *next;
2381 int started_p;
2382 const input_file* file;
2383 outf_p f;
2386 struct walk_type_data;
2388 /* For scalars and strings, given the item in 'val'.
2389 For structures, given a pointer to the item in 'val'.
2390 For misc. pointers, given the item in 'val'.
2392 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2393 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2395 /* Parameters for write_types. */
2397 struct write_types_data
2399 const char *prefix;
2400 const char *param_prefix;
2401 const char *subfield_marker_routine;
2402 const char *marker_routine;
2403 const char *reorder_note_routine;
2404 const char *comment;
2405 enum write_types_kinds kind;
2408 static void output_escaped_param (const struct walk_type_data *d,
2409 const char *, const char *);
2410 static void output_mangled_typename (outf_p, const_type_p);
2411 static void walk_type (type_p t, struct walk_type_data *d);
2412 static void write_func_for_structure (type_p orig_s, type_p s,
2413 const struct write_types_data *wtd);
2414 static void write_types_process_field
2415 (type_p f, const struct walk_type_data *d);
2416 static void write_types (outf_p output_header,
2417 type_p structures,
2418 const struct write_types_data *wtd);
2419 static void write_types_local_process_field
2420 (type_p f, const struct walk_type_data *d);
2421 static void write_local_func_for_structure (const_type_p orig_s, type_p s);
2422 static void write_local (outf_p output_header,
2423 type_p structures);
2424 static int contains_scalar_p (type_p t);
2425 static void put_mangled_filename (outf_p, const input_file *);
2426 static void finish_root_table (struct flist *flp, const char *pfx,
2427 const char *lastname,
2428 const char *tname, const char *name);
2429 static void write_root (outf_p, pair_p, type_p, const char *, int,
2430 struct fileloc *, bool);
2431 static void write_array (outf_p f, pair_p v,
2432 const struct write_types_data *wtd);
2433 static void write_roots (pair_p, bool);
2435 /* Parameters for walk_type. */
2437 struct walk_type_data
2439 process_field_fn process_field;
2440 const void *cookie;
2441 outf_p of;
2442 options_p opt;
2443 const char *val;
2444 const char *prev_val[4];
2445 int indent;
2446 int counter;
2447 const struct fileloc *line;
2448 lang_bitmap bitmap;
2449 int used_length;
2450 type_p orig_s;
2451 const char *reorder_fn;
2452 bool fn_wants_lvalue;
2453 bool in_record_p;
2454 int loopcounter;
2455 bool in_ptr_field;
2456 bool have_this_obj;
2457 bool in_nested_ptr;
2461 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2462 pre-processor identifier to use in a #define directive. This replaces
2463 special characters used in C++ identifiers like '>', '<' and ':' with
2464 '_'.
2466 If no C++ special characters are found in TYPE_NAME, return
2467 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2468 characters replaced with '_'. In this case, the caller is
2469 responsible for freeing the allocated string. */
2471 static const char *
2472 filter_type_name (const char *type_name)
2474 if (strchr (type_name, '<') || strchr (type_name, ':'))
2476 size_t i;
2477 char *s = xstrdup (type_name);
2478 for (i = 0; i < strlen (s); i++)
2479 if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ','
2480 || s[i] == '*')
2481 s[i] = '_';
2482 return s;
2484 else
2485 return type_name;
2489 /* Print a mangled name representing T to OF. */
2491 static void
2492 output_mangled_typename (outf_p of, const_type_p t)
2494 if (t == NULL)
2495 oprintf (of, "Z");
2496 else
2497 switch (t->kind)
2499 case TYPE_NONE:
2500 case TYPE_UNDEFINED:
2501 case TYPE_CALLBACK:
2502 gcc_unreachable ();
2503 break;
2504 case TYPE_POINTER:
2505 oprintf (of, "P");
2506 output_mangled_typename (of, t->u.p);
2507 break;
2508 case TYPE_SCALAR:
2509 oprintf (of, "I");
2510 break;
2511 case TYPE_STRING:
2512 oprintf (of, "S");
2513 break;
2514 case TYPE_STRUCT:
2515 case TYPE_UNION:
2516 case TYPE_LANG_STRUCT:
2517 case TYPE_USER_STRUCT:
2519 /* For references to classes within an inheritance hierarchy,
2520 only ever reference the ultimate base class, since only
2521 it will have gt_ functions. */
2522 t = get_ultimate_base_class (t);
2523 const char *id_for_tag = filter_type_name (t->u.s.tag);
2524 oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2525 id_for_tag);
2526 if (id_for_tag != t->u.s.tag)
2527 free (CONST_CAST (char *, id_for_tag));
2529 break;
2530 case TYPE_ARRAY:
2531 gcc_unreachable ();
2535 /* Print PARAM to D->OF processing escapes. D->VAL references the
2536 current object, D->PREV_VAL the object containing the current
2537 object, ONAME is the name of the option and D->LINE is used to
2538 print error messages. */
2540 static void
2541 output_escaped_param (const struct walk_type_data *d, const char *param,
2542 const char *oname)
2544 const char *p;
2546 for (p = param; *p; p++)
2547 if (*p != '%')
2548 oprintf (d->of, "%c", *p);
2549 else
2550 switch (*++p)
2552 case 'h':
2553 oprintf (d->of, "(%s)", d->prev_val[2]);
2554 break;
2555 case '0':
2556 oprintf (d->of, "(%s)", d->prev_val[0]);
2557 break;
2558 case '1':
2559 oprintf (d->of, "(%s)", d->prev_val[1]);
2560 break;
2561 case 'a':
2563 const char *pp = d->val + strlen (d->val);
2564 while (pp[-1] == ']')
2565 while (*pp != '[')
2566 pp--;
2567 oprintf (d->of, "%s", pp);
2569 break;
2570 default:
2571 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2572 oname, '%', *p);
2576 const char *
2577 get_string_option (options_p opt, const char *key)
2579 for (; opt; opt = opt->next)
2580 if (opt->kind == OPTION_STRING && strcmp (opt->name, key) == 0)
2581 return opt->info.string;
2582 return NULL;
2585 /* Machinery for avoiding duplicate tags within switch statements. */
2586 struct seen_tag
2588 const char *tag;
2589 struct seen_tag *next;
2593 already_seen_tag (struct seen_tag *seen_tags, const char *tag)
2595 /* Linear search, so O(n^2), but n is currently small. */
2596 while (seen_tags)
2598 if (!strcmp (seen_tags->tag, tag))
2599 return 1;
2600 seen_tags = seen_tags->next;
2602 /* Not yet seen this tag. */
2603 return 0;
2606 void
2607 mark_tag_as_seen (struct seen_tag **seen_tags, const char *tag)
2609 /* Add to front of linked list. */
2610 struct seen_tag *new_node = XCNEW (struct seen_tag);
2611 new_node->tag = tag;
2612 new_node->next = *seen_tags;
2613 *seen_tags = new_node;
2616 static void
2617 walk_subclasses (type_p base, struct walk_type_data *d,
2618 struct seen_tag **seen_tags)
2620 for (type_p sub = base->u.s.first_subclass; sub != NULL;
2621 sub = sub->u.s.next_sibling_class)
2623 const char *type_tag = get_string_option (sub->u.s.opt, "tag");
2624 if (type_tag && !already_seen_tag (*seen_tags, type_tag))
2626 mark_tag_as_seen (seen_tags, type_tag);
2627 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
2628 d->indent += 2;
2629 oprintf (d->of, "%*s{\n", d->indent, "");
2630 d->indent += 2;
2631 oprintf (d->of, "%*s%s *sub = static_cast <%s *> (x);\n",
2632 d->indent, "", sub->u.s.tag, sub->u.s.tag);
2633 const char *old_val = d->val;
2634 d->val = "(*sub)";
2635 walk_type (sub, d);
2636 d->val = old_val;
2637 d->indent -= 2;
2638 oprintf (d->of, "%*s}\n", d->indent, "");
2639 oprintf (d->of, "%*sbreak;\n", d->indent, "");
2640 d->indent -= 2;
2642 walk_subclasses (sub, d, seen_tags);
2646 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2647 which is of type T. Write code to D->OF to constrain execution (at
2648 the point that D->PROCESS_FIELD is called) to the appropriate
2649 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2650 pointers to those objects. D->PREV_VAL lists the objects
2651 containing the current object, D->OPT is a list of options to
2652 apply, D->INDENT is the current indentation level, D->LINE is used
2653 to print error messages, D->BITMAP indicates which languages to
2654 print the structure for. */
2656 static void
2657 walk_type (type_p t, struct walk_type_data *d)
2659 const char *length = NULL;
2660 const char *desc = NULL;
2661 const char *type_tag = NULL;
2662 int maybe_undef_p = 0;
2663 int atomic_p = 0;
2664 options_p oo;
2665 const struct nested_ptr_data *nested_ptr_d = NULL;
2667 for (oo = d->opt; oo; oo = oo->next)
2668 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2669 length = oo->info.string;
2670 else if (strcmp (oo->name, "maybe_undef") == 0)
2671 maybe_undef_p = 1;
2672 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2673 desc = oo->info.string;
2674 else if (strcmp (oo->name, "nested_ptr") == 0
2675 && oo->kind == OPTION_NESTED)
2676 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2677 else if (strcmp (oo->name, "dot") == 0)
2679 else if (strcmp (oo->name, "tag") == 0)
2680 type_tag = oo->info.string;
2681 else if (strcmp (oo->name, "special") == 0)
2683 else if (strcmp (oo->name, "skip") == 0)
2685 else if (strcmp (oo->name, "atomic") == 0)
2686 atomic_p = 1;
2687 else if (strcmp (oo->name, "default") == 0)
2689 else if (strcmp (oo->name, "chain_next") == 0)
2691 else if (strcmp (oo->name, "chain_prev") == 0)
2693 else if (strcmp (oo->name, "chain_circular") == 0)
2695 else if (strcmp (oo->name, "reorder") == 0)
2697 else if (strcmp (oo->name, "variable_size") == 0)
2699 else if (strcmp (oo->name, "for_user") == 0)
2701 else if (strcmp (oo->name, "callback") == 0)
2703 else if (strcmp (oo->name, "string_length") == 0)
2705 else
2706 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2708 if (d->used_length)
2709 length = NULL;
2711 if (maybe_undef_p
2712 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
2714 error_at_line (d->line,
2715 "field `%s' has invalid option `maybe_undef_p'\n",
2716 d->val);
2717 return;
2720 if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2722 error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2723 return;
2726 switch (t->kind)
2728 case TYPE_SCALAR:
2729 case TYPE_STRING:
2730 case TYPE_CALLBACK:
2731 d->process_field (t, d);
2732 break;
2734 case TYPE_POINTER:
2736 d->in_ptr_field = true;
2737 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2739 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2740 break;
2743 /* If a pointer type is marked as "atomic", we process the
2744 field itself, but we don't walk the data that they point to.
2746 There are two main cases where we walk types: to mark
2747 pointers that are reachable, and to relocate pointers when
2748 writing a PCH file. In both cases, an atomic pointer is
2749 itself marked or relocated, but the memory that it points
2750 to is left untouched. In the case of PCH, that memory will
2751 be read/written unchanged to the PCH file. */
2752 if (atomic_p)
2754 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2755 d->indent += 2;
2756 d->process_field (t, d);
2757 d->indent -= 2;
2758 oprintf (d->of, "%*s}\n", d->indent, "");
2759 break;
2762 if (!length)
2764 if (!union_or_struct_p (t->u.p))
2766 error_at_line (d->line,
2767 "field `%s' is pointer to unimplemented type",
2768 d->val);
2769 break;
2772 if (nested_ptr_d)
2774 const char *oldprevval2 = d->prev_val[2];
2775 bool old_in_nested_ptr = d->in_nested_ptr;
2777 if (!union_or_struct_p (nested_ptr_d->type))
2779 error_at_line (d->line,
2780 "field `%s' has invalid "
2781 "option `nested_ptr'\n", d->val);
2782 return;
2785 d->prev_val[2] = d->val;
2786 d->in_nested_ptr = true;
2787 oprintf (d->of, "%*s{\n", d->indent, "");
2788 d->indent += 2;
2789 d->val = xasprintf ("x%d", d->counter++);
2790 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2791 (nested_ptr_d->type->kind == TYPE_UNION
2792 ? "union" : "struct"),
2793 nested_ptr_d->type->u.s.tag,
2794 d->fn_wants_lvalue ? "" : "const ", d->val);
2795 oprintf (d->of, "%*s", d->indent + 2, "");
2796 output_escaped_param (d, nested_ptr_d->convert_from,
2797 "nested_ptr");
2798 oprintf (d->of, ";\n");
2800 d->process_field (nested_ptr_d->type, d);
2802 if (d->fn_wants_lvalue)
2804 oprintf (d->of, "%*s%s = ", d->indent, "",
2805 d->prev_val[2]);
2806 d->prev_val[2] = d->val;
2807 output_escaped_param (d, nested_ptr_d->convert_to,
2808 "nested_ptr");
2809 oprintf (d->of, ";\n");
2812 d->indent -= 2;
2813 oprintf (d->of, "%*s}\n", d->indent, "");
2814 d->val = d->prev_val[2];
2815 d->prev_val[2] = oldprevval2;
2816 d->in_nested_ptr = old_in_nested_ptr;
2818 else
2819 d->process_field (t->u.p, d);
2821 else
2823 int loopcounter = d->loopcounter;
2824 const char *oldval = d->val;
2825 const char *oldprevval3 = d->prev_val[3];
2826 char *newval;
2828 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2829 d->indent += 2;
2830 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2831 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2832 "", loopcounter, loopcounter);
2833 if (!d->in_record_p)
2834 output_escaped_param (d, length, "length");
2835 else
2836 oprintf (d->of, "l%d", loopcounter);
2837 if (d->have_this_obj)
2838 /* Try to unswitch loops (see PR53880). */
2839 oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
2840 oprintf (d->of, "); i%d++) {\n", loopcounter);
2841 d->indent += 2;
2842 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2843 d->used_length = 1;
2844 d->prev_val[3] = oldval;
2845 walk_type (t->u.p, d);
2846 free (newval);
2847 d->val = oldval;
2848 d->prev_val[3] = oldprevval3;
2849 d->used_length = 0;
2850 d->indent -= 2;
2851 oprintf (d->of, "%*s}\n", d->indent, "");
2852 d->process_field (t, d);
2853 d->indent -= 2;
2854 oprintf (d->of, "%*s}\n", d->indent, "");
2856 d->in_ptr_field = false;
2858 break;
2860 case TYPE_ARRAY:
2862 int loopcounter;
2863 const char *oldval = d->val;
2864 char *newval;
2866 /* If it's an array of scalars, we optimize by not generating
2867 any code. */
2868 if (t->u.a.p->kind == TYPE_SCALAR)
2869 break;
2871 if (length)
2872 loopcounter = d->loopcounter;
2873 else
2874 loopcounter = d->counter++;
2876 /* When walking an array, compute the length and store it in a
2877 local variable before walking the array elements, instead of
2878 recomputing the length expression each time through the loop.
2879 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2880 where the length is stored in the first array element,
2881 because otherwise that operand can get overwritten on the
2882 first iteration. */
2883 oprintf (d->of, "%*s{\n", d->indent, "");
2884 d->indent += 2;
2885 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2886 if (!d->in_record_p || !length)
2888 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2889 d->indent, "", loopcounter);
2890 if (length)
2891 output_escaped_param (d, length, "length");
2892 else
2893 oprintf (d->of, "%s", t->u.a.len);
2894 oprintf (d->of, ");\n");
2897 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2898 d->indent, "",
2899 loopcounter, loopcounter, loopcounter, loopcounter);
2900 d->indent += 2;
2901 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2902 d->used_length = 1;
2903 walk_type (t->u.a.p, d);
2904 free (newval);
2905 d->used_length = 0;
2906 d->val = oldval;
2907 d->indent -= 2;
2908 oprintf (d->of, "%*s}\n", d->indent, "");
2909 d->indent -= 2;
2910 oprintf (d->of, "%*s}\n", d->indent, "");
2912 break;
2914 case TYPE_STRUCT:
2915 case TYPE_UNION:
2917 pair_p f;
2918 const char *oldval = d->val;
2919 const char *oldprevval1 = d->prev_val[1];
2920 const char *oldprevval2 = d->prev_val[2];
2921 const int union_p = t->kind == TYPE_UNION;
2922 int seen_default_p = 0;
2923 options_p o;
2924 int lengths_seen = 0;
2925 int endcounter;
2926 bool any_length_seen = false;
2928 if (!t->u.s.line.file)
2929 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
2931 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2933 error_at_line (d->line,
2934 "structure `%s' defined for mismatching languages",
2935 t->u.s.tag);
2936 error_at_line (&t->u.s.line, "one structure defined here");
2939 /* Some things may also be defined in the structure's options. */
2940 for (o = t->u.s.opt; o; o = o->next)
2941 if (!desc && strcmp (o->name, "desc") == 0
2942 && o->kind == OPTION_STRING)
2943 desc = o->info.string;
2945 d->prev_val[2] = oldval;
2946 d->prev_val[1] = oldprevval2;
2947 if (union_p)
2949 if (desc == NULL)
2951 error_at_line (d->line,
2952 "missing `desc' option for union `%s'",
2953 t->u.s.tag);
2954 desc = "1";
2956 oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
2957 output_escaped_param (d, desc, "desc");
2958 oprintf (d->of, "))\n");
2959 d->indent += 2;
2960 oprintf (d->of, "%*s{\n", d->indent, "");
2962 else if (desc)
2964 /* We have a "desc" option on a struct, signifying the
2965 base class within a GC-managed inheritance hierarchy.
2966 The current code specialcases the base class, then walks
2967 into subclasses, recursing into this routine to handle them.
2968 This organization requires the base class to have a case in
2969 the switch statement, and hence a tag value is mandatory
2970 for the base class. This restriction could be removed, but
2971 it would require some restructing of this code. */
2972 if (!type_tag)
2974 error_at_line (d->line,
2975 "missing `tag' option for type `%s'",
2976 t->u.s.tag);
2978 oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
2979 output_escaped_param (d, desc, "desc");
2980 oprintf (d->of, "))\n");
2981 d->indent += 2;
2982 oprintf (d->of, "%*s{\n", d->indent, "");
2983 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
2984 d->indent += 2;
2987 FOR_ALL_INHERITED_FIELDS (t, f)
2989 options_p oo;
2990 int skip_p = 0;
2991 const char *fieldlength = NULL;
2993 d->reorder_fn = NULL;
2994 for (oo = f->opt; oo; oo = oo->next)
2995 if (strcmp (oo->name, "skip") == 0)
2996 skip_p = 1;
2997 else if (strcmp (oo->name, "length") == 0
2998 && oo->kind == OPTION_STRING)
2999 fieldlength = oo->info.string;
3001 if (skip_p)
3002 continue;
3003 if (fieldlength)
3005 lengths_seen++;
3006 d->counter++;
3007 if (!union_p)
3009 if (!any_length_seen)
3011 oprintf (d->of, "%*s{\n", d->indent, "");
3012 d->indent += 2;
3014 any_length_seen = true;
3016 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
3017 d->indent, "", d->counter - 1);
3018 output_escaped_param (d, fieldlength, "length");
3019 oprintf (d->of, ");\n");
3023 endcounter = d->counter;
3025 FOR_ALL_INHERITED_FIELDS (t, f)
3027 options_p oo;
3028 const char *dot = ".";
3029 const char *tagid = NULL;
3030 int skip_p = 0;
3031 int default_p = 0;
3032 const char *fieldlength = NULL;
3033 char *newval;
3035 d->reorder_fn = NULL;
3036 for (oo = f->opt; oo; oo = oo->next)
3037 if (strcmp (oo->name, "dot") == 0
3038 && oo->kind == OPTION_STRING)
3039 dot = oo->info.string;
3040 else if (strcmp (oo->name, "tag") == 0
3041 && oo->kind == OPTION_STRING)
3042 tagid = oo->info.string;
3043 else if (strcmp (oo->name, "skip") == 0)
3044 skip_p = 1;
3045 else if (strcmp (oo->name, "default") == 0)
3046 default_p = 1;
3047 else if (strcmp (oo->name, "reorder") == 0
3048 && oo->kind == OPTION_STRING)
3049 d->reorder_fn = oo->info.string;
3050 else if (strcmp (oo->name, "length") == 0
3051 && oo->kind == OPTION_STRING)
3052 fieldlength = oo->info.string;
3054 if (skip_p)
3055 continue;
3057 if (union_p && tagid)
3059 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
3060 d->indent += 2;
3062 else if (union_p && default_p)
3064 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3065 d->indent += 2;
3066 seen_default_p = 1;
3068 else if (!union_p && (default_p || tagid))
3069 error_at_line (d->line,
3070 "can't use `%s' outside a union on field `%s'",
3071 default_p ? "default" : "tag", f->name);
3072 else if (union_p && !(default_p || tagid)
3073 && f->type->kind == TYPE_SCALAR)
3075 fprintf (stderr,
3076 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3077 get_input_file_name (d->line->file), d->line->line,
3078 f->name);
3079 continue;
3081 else if (union_p && !(default_p || tagid))
3082 error_at_line (d->line,
3083 "field `%s' is missing `tag' or `default' option",
3084 f->name);
3086 if (fieldlength)
3088 d->loopcounter = endcounter - lengths_seen--;
3091 d->line = &f->line;
3092 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
3093 d->opt = f->opt;
3094 d->used_length = false;
3095 d->in_record_p = !union_p;
3097 walk_type (f->type, d);
3099 d->in_record_p = false;
3101 free (newval);
3103 if (union_p)
3105 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3106 d->indent -= 2;
3109 d->reorder_fn = NULL;
3111 d->val = oldval;
3112 d->prev_val[1] = oldprevval1;
3113 d->prev_val[2] = oldprevval2;
3115 if (union_p && !seen_default_p)
3117 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3118 oprintf (d->of, "%*s break;\n", d->indent, "");
3121 if (desc && !union_p)
3123 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3124 d->indent -= 2;
3126 if (union_p)
3128 oprintf (d->of, "%*s}\n", d->indent, "");
3129 d->indent -= 2;
3131 else if (desc)
3133 /* Add cases to handle subclasses. */
3134 struct seen_tag *tags = NULL;
3135 walk_subclasses (t, d, &tags);
3137 /* Ensure that if someone forgets a "tag" option that we don't
3138 silent fail to traverse that subclass's fields. */
3139 if (!seen_default_p)
3141 oprintf (d->of, "%*s/* Unrecognized tag value. */\n",
3142 d->indent, "");
3143 oprintf (d->of, "%*sdefault: gcc_unreachable (); \n",
3144 d->indent, "");
3147 /* End of the switch statement */
3148 oprintf (d->of, "%*s}\n", d->indent, "");
3149 d->indent -= 2;
3151 if (any_length_seen)
3153 d->indent -= 2;
3154 oprintf (d->of, "%*s}\n", d->indent, "");
3157 break;
3159 case TYPE_LANG_STRUCT:
3161 type_p nt;
3162 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3163 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3164 break;
3165 if (nt == NULL)
3166 error_at_line (d->line, "structure `%s' differs between languages",
3167 t->u.s.tag);
3168 else
3169 walk_type (nt, d);
3171 break;
3173 case TYPE_USER_STRUCT:
3174 d->process_field (t, d);
3175 break;
3177 case TYPE_NONE:
3178 case TYPE_UNDEFINED:
3179 gcc_unreachable ();
3183 /* process_field routine for marking routines. */
3185 static void
3186 write_types_process_field (type_p f, const struct walk_type_data *d)
3188 const struct write_types_data *wtd;
3189 wtd = (const struct write_types_data *) d->cookie;
3191 switch (f->kind)
3193 case TYPE_NONE:
3194 case TYPE_UNDEFINED:
3195 gcc_unreachable ();
3196 case TYPE_POINTER:
3197 oprintf (d->of, "%*s%s (%s", d->indent, "",
3198 wtd->subfield_marker_routine, d->val);
3199 if (wtd->param_prefix)
3201 if (f->u.p->kind == TYPE_SCALAR)
3202 /* The current type is a pointer to a scalar (so not
3203 considered like a pointer to instances of user defined
3204 types) and we are seeing it; it means we must be even
3205 more careful about the second argument of the
3206 SUBFIELD_MARKER_ROUTINE call. That argument must
3207 always be the instance of the type for which
3208 write_func_for_structure was called - this really is
3209 what the function SUBFIELD_MARKER_ROUTINE expects.
3210 That is, it must be an instance of the ORIG_S type
3211 parameter of write_func_for_structure. The convention
3212 is that that argument must be "x" in that case (as set
3213 by write_func_for_structure). The problem is, we can't
3214 count on d->prev_val[3] to be always set to "x" in that
3215 case. Sometimes walk_type can set it to something else
3216 (to e.g cooperate with write_array when called from
3217 write_roots). So let's set it to "x" here then. */
3218 oprintf (d->of, ", x");
3219 else
3220 oprintf (d->of, ", %s", d->prev_val[3]);
3221 if (d->orig_s)
3223 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3224 output_mangled_typename (d->of, d->orig_s);
3226 else
3227 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3229 oprintf (d->of, ");\n");
3230 if (d->reorder_fn && wtd->reorder_note_routine)
3231 oprintf (d->of, "%*s%s (%s, %s, %s);\n", d->indent, "",
3232 wtd->reorder_note_routine, d->val,
3233 d->prev_val[3], d->reorder_fn);
3234 break;
3236 case TYPE_STRING:
3237 case TYPE_STRUCT:
3238 case TYPE_UNION:
3239 case TYPE_LANG_STRUCT:
3240 case TYPE_USER_STRUCT:
3241 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3243 /* If F is a user-defined type and the field is not a
3244 pointer to the type, then we should not generate the
3245 standard pointer-marking code. All we need to do is call
3246 the user-provided marking function to process the fields
3247 of F. */
3248 oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3249 d->val);
3251 else
3253 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3254 output_mangled_typename (d->of, f);
3256 /* Check if we need to call the special pch note version
3257 for strings that takes an explicit length. */
3258 const auto length_override
3259 = (f->kind == TYPE_STRING && !strcmp (wtd->prefix, "pch_n")
3260 ? get_string_option (d->opt, "string_length")
3261 : nullptr);
3262 if (length_override)
3264 oprintf (d->of, "2 (%s, ", d->val);
3265 output_escaped_param (d, length_override, "string_length");
3267 else
3268 oprintf (d->of, " (%s", d->val);
3270 oprintf (d->of, ");\n");
3271 if (d->reorder_fn && wtd->reorder_note_routine)
3272 oprintf (d->of, "%*s%s (%s, %s, %s);\n", d->indent, "",
3273 wtd->reorder_note_routine, d->val, d->val,
3274 d->reorder_fn);
3276 break;
3278 case TYPE_SCALAR:
3279 case TYPE_CALLBACK:
3280 break;
3282 case TYPE_ARRAY:
3283 gcc_unreachable ();
3287 /* Return an output file that is suitable for definitions which can
3288 reference struct S */
3290 static outf_p
3291 get_output_file_for_structure (const_type_p s)
3293 const input_file *fn;
3295 gcc_assert (union_or_struct_p (s));
3296 fn = s->u.s.line.file;
3298 /* The call to get_output_file_with_visibility may update fn by
3299 caching its result inside, so we need the CONST_CAST. */
3300 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3304 /* Returns the specifier keyword for a string or union type S, empty string
3305 otherwise. */
3307 static const char *
3308 get_type_specifier (const type_p s)
3310 if (s->kind == TYPE_STRUCT)
3311 return "struct ";
3312 else if (s->kind == TYPE_LANG_STRUCT)
3313 return get_type_specifier (s->u.s.lang_struct);
3314 else if (s->kind == TYPE_UNION)
3315 return "union ";
3316 return "";
3320 /* Emits a declaration for type TY (assumed to be a union or a
3321 structure) on stream OUT. */
3323 static void
3324 write_type_decl (outf_p out, type_p ty)
3326 if (union_or_struct_p (ty))
3327 oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3328 else if (ty->kind == TYPE_SCALAR)
3330 if (ty->u.scalar_is_char)
3331 oprintf (out, "const char");
3332 else
3333 oprintf (out, "void");
3335 else if (ty->kind == TYPE_POINTER)
3337 write_type_decl (out, ty->u.p);
3338 oprintf (out, " *");
3340 else if (ty->kind == TYPE_ARRAY)
3342 write_type_decl (out, ty->u.a.p);
3343 oprintf (out, " *");
3345 else if (ty->kind == TYPE_STRING)
3347 oprintf (out, "const char *");
3349 else
3350 gcc_unreachable ();
3354 /* Write on OF the name of the marker function for structure S. PREFIX
3355 is the prefix to use (to distinguish ggc from pch markers). */
3357 static void
3358 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3360 if (union_or_struct_p (s))
3362 const char *id_for_tag = filter_type_name (s->u.s.tag);
3363 oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3364 if (id_for_tag != s->u.s.tag)
3365 free (CONST_CAST (char *, id_for_tag));
3367 else
3368 gcc_unreachable ();
3371 /* Write on OF a user-callable routine to act as an entry point for
3372 the marking routine for S, generated by write_func_for_structure.
3373 WTD distinguishes between ggc and pch markers. */
3375 static void
3376 write_user_func_for_structure_ptr (outf_p of, type_p s, const write_types_data *wtd)
3378 gcc_assert (union_or_struct_p (s));
3380 type_p alias_of = NULL;
3381 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3382 if (strcmp (opt->name, "ptr_alias") == 0)
3384 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3385 we do not generate marking code for ORIG_S here. Instead, a
3386 forwarder #define in gtype-desc.h will cause every call to its
3387 marker to call the target of this alias.
3389 However, we still want to create a user entry code for the
3390 aliased type. So, if ALIAS_OF is set, we only generate the
3391 user-callable marker function. */
3392 alias_of = opt->info.type;
3393 break;
3396 DBGPRINTF ("write_user_func_for_structure_ptr: %s %s", s->u.s.tag,
3397 wtd->prefix);
3399 /* Only write the function once. */
3400 if (s->u.s.wrote_user_func_for_ptr[wtd->kind])
3401 return;
3402 s->u.s.wrote_user_func_for_ptr[wtd->kind] = true;
3404 oprintf (of, "\nvoid\n");
3405 oprintf (of, "gt_%sx (", wtd->prefix);
3406 write_type_decl (of, s);
3407 oprintf (of, " *& x)\n");
3408 oprintf (of, "{\n");
3409 oprintf (of, " if (x)\n ");
3410 write_marker_function_name (of,
3411 alias_of ? alias_of : get_ultimate_base_class (s),
3412 wtd->prefix);
3413 oprintf (of, " ((void *) x);\n");
3414 oprintf (of, "}\n");
3418 /* Write a function to mark all the fields of type S on OF. PREFIX
3419 and D are as in write_user_marking_functions. */
3421 static void
3422 write_user_func_for_structure_body (type_p s, const char *prefix,
3423 struct walk_type_data *d)
3425 oprintf (d->of, "\nvoid\n");
3426 oprintf (d->of, "gt_%sx (", prefix);
3427 write_type_decl (d->of, s);
3428 oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3429 oprintf (d->of, "{\n");
3430 oprintf (d->of, " ");
3431 write_type_decl (d->of, s);
3432 oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3433 d->val = "(*x)";
3434 d->indent = 2;
3435 walk_type (s, d);
3436 oprintf (d->of, "}\n");
3439 /* Emit the user-callable functions needed to mark all the types used
3440 by the user structure S. PREFIX is the prefix to use to
3441 distinguish ggc and pch markers. D contains data needed to pass to
3442 walk_type when traversing the fields of a type.
3444 For every type T referenced by S, two routines are generated: one
3445 that takes 'T *', marks the pointer and calls the second routine,
3446 which just marks the fields of T. */
3448 static void
3449 write_user_marking_functions (type_p s,
3450 const write_types_data *w,
3451 struct walk_type_data *d)
3453 gcc_assert (s->kind == TYPE_USER_STRUCT);
3455 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3457 type_p fld_type = fld->type;
3458 if (fld_type->kind == TYPE_POINTER)
3460 type_p pointed_to_type = fld_type->u.p;
3461 if (union_or_struct_p (pointed_to_type))
3462 write_user_func_for_structure_ptr (d->of, pointed_to_type, w);
3464 else if (union_or_struct_p (fld_type))
3465 write_user_func_for_structure_body (fld_type, w->prefix, d);
3470 /* For S, a structure that's part of ORIG_S write out a routine that:
3471 - Takes a parameter, a void * but actually of type *S
3472 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3473 field of S or its substructures and (in some cases) things
3474 that are pointed to by S. */
3476 static void
3477 write_func_for_structure (type_p orig_s, type_p s,
3478 const struct write_types_data *wtd)
3480 const char *chain_next = NULL;
3481 const char *chain_prev = NULL;
3482 const char *chain_circular = NULL;
3483 options_p opt;
3484 struct walk_type_data d;
3486 if (s->u.s.base_class)
3488 /* Verify that the base class has a "desc", since otherwise
3489 the traversal hooks there won't attempt to visit fields of
3490 subclasses such as this one. */
3491 const_type_p ubc = get_ultimate_base_class (s);
3492 if ((!opts_have (ubc->u.s.opt, "user")
3493 && !opts_have (ubc->u.s.opt, "desc")))
3494 error_at_line (&s->u.s.line,
3495 ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
3496 ", but '%s' lacks a discriminator 'desc' option"),
3497 s->u.s.tag, ubc->u.s.tag, ubc->u.s.tag);
3499 /* Don't write fns for subclasses, only for the ultimate base class
3500 within an inheritance hierarchy. */
3501 return;
3504 memset (&d, 0, sizeof (d));
3505 d.of = get_output_file_for_structure (s);
3507 bool for_user = false;
3508 for (opt = s->u.s.opt; opt; opt = opt->next)
3509 if (strcmp (opt->name, "chain_next") == 0
3510 && opt->kind == OPTION_STRING)
3511 chain_next = opt->info.string;
3512 else if (strcmp (opt->name, "chain_prev") == 0
3513 && opt->kind == OPTION_STRING)
3514 chain_prev = opt->info.string;
3515 else if (strcmp (opt->name, "chain_circular") == 0
3516 && opt->kind == OPTION_STRING)
3517 chain_circular = opt->info.string;
3518 else if (strcmp (opt->name, "for_user") == 0)
3519 for_user = true;
3520 if (chain_prev != NULL && chain_next == NULL)
3521 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3522 if (chain_circular != NULL && chain_next != NULL)
3523 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3524 if (chain_circular != NULL)
3525 chain_next = chain_circular;
3527 d.process_field = write_types_process_field;
3528 d.cookie = wtd;
3529 d.orig_s = orig_s;
3530 d.opt = s->u.s.opt;
3531 d.line = &s->u.s.line;
3532 d.bitmap = s->u.s.bitmap;
3533 d.prev_val[0] = "*x";
3534 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3535 d.prev_val[3] = "x";
3536 d.val = "(*x)";
3537 d.have_this_obj = false;
3539 oprintf (d.of, "\n");
3540 oprintf (d.of, "void\n");
3541 write_marker_function_name (d.of, orig_s, wtd->prefix);
3542 oprintf (d.of, " (void *x_p)\n");
3543 oprintf (d.of, "{\n ");
3544 write_type_decl (d.of, s);
3545 oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3546 write_type_decl (d.of, s);
3547 oprintf (d.of, " *)x_p;\n");
3548 if (chain_next != NULL)
3550 /* TYPE_USER_STRUCTs should not occur here. These structures
3551 are completely handled by user code. */
3552 gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
3554 oprintf (d.of, " ");
3555 write_type_decl (d.of, s);
3556 oprintf (d.of, " * xlimit = x;\n");
3558 if (chain_next == NULL)
3560 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3561 if (wtd->param_prefix)
3563 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3564 output_mangled_typename (d.of, orig_s);
3566 oprintf (d.of, "))\n");
3568 else
3570 if (chain_circular != NULL)
3571 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3572 else
3573 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3574 if (wtd->param_prefix)
3576 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3577 output_mangled_typename (d.of, orig_s);
3579 oprintf (d.of, "))\n");
3580 if (chain_circular != NULL)
3581 oprintf (d.of, " return;\n do\n");
3583 oprintf (d.of, " xlimit = (");
3584 d.prev_val[2] = "*xlimit";
3585 output_escaped_param (&d, chain_next, "chain_next");
3586 oprintf (d.of, ");\n");
3587 if (chain_prev != NULL)
3589 oprintf (d.of, " if (x != xlimit)\n");
3590 oprintf (d.of, " for (;;)\n");
3591 oprintf (d.of, " {\n");
3592 oprintf (d.of, " %s %s * const xprev = (",
3593 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3595 d.prev_val[2] = "*x";
3596 output_escaped_param (&d, chain_prev, "chain_prev");
3597 oprintf (d.of, ");\n");
3598 oprintf (d.of, " if (xprev == NULL) break;\n");
3599 oprintf (d.of, " x = xprev;\n");
3600 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3601 if (wtd->param_prefix)
3603 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3604 output_mangled_typename (d.of, orig_s);
3606 oprintf (d.of, ");\n");
3607 oprintf (d.of, " }\n");
3609 if (chain_circular != NULL)
3611 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3612 if (wtd->param_prefix)
3614 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3615 output_mangled_typename (d.of, orig_s);
3617 oprintf (d.of, "));\n");
3618 oprintf (d.of, " do\n");
3620 else
3621 oprintf (d.of, " while (x != xlimit)\n");
3623 oprintf (d.of, " {\n");
3625 d.prev_val[2] = "*x";
3626 d.indent = 6;
3627 if (orig_s->kind != TYPE_USER_STRUCT)
3628 walk_type (s, &d);
3629 else
3631 /* User structures have no fields to walk. Simply generate a call
3632 to the user-provided structure marker. */
3633 oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3636 if (chain_next != NULL)
3638 oprintf (d.of, " x = (");
3639 output_escaped_param (&d, chain_next, "chain_next");
3640 oprintf (d.of, ");\n");
3643 oprintf (d.of, " }\n");
3644 if (chain_circular != NULL)
3645 oprintf (d.of, " while (x != xlimit);\n");
3646 oprintf (d.of, "}\n");
3648 if (orig_s->kind == TYPE_USER_STRUCT)
3649 write_user_marking_functions (orig_s, wtd, &d);
3651 if (for_user)
3653 write_user_func_for_structure_body (orig_s, wtd->prefix, &d);
3654 write_user_func_for_structure_ptr (d.of, orig_s, wtd);
3659 /* Write out marker routines for STRUCTURES. */
3661 static void
3662 write_types (outf_p output_header, type_p structures,
3663 const struct write_types_data *wtd)
3665 int nbfun = 0; /* Count the emitted functions. */
3666 type_p s;
3668 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3670 /* We first emit the macros and the declarations. Functions' code is
3671 emitted afterwards. This is needed in plugin mode. */
3672 oprintf (output_header, "/* Macros and declarations. */\n");
3673 for (s = structures; s; s = s->next)
3674 /* Do not emit handlers for derived classes; we only ever deal with
3675 the ultimate base class within an inheritance hierarchy. */
3676 if ((s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3677 && !s->u.s.base_class)
3679 options_p opt;
3681 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3682 continue;
3684 const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3686 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3687 output_mangled_typename (output_header, s);
3688 oprintf (output_header, "(X) do { \\\n");
3689 oprintf (output_header,
3690 " if ((intptr_t)(X) != 0) gt_%sx_%s (X);\\\n",
3691 wtd->prefix, s_id_for_tag);
3692 oprintf (output_header, " } while (0)\n");
3694 for (opt = s->u.s.opt; opt; opt = opt->next)
3695 if (strcmp (opt->name, "ptr_alias") == 0
3696 && opt->kind == OPTION_TYPE)
3698 const_type_p const t = (const_type_p) opt->info.type;
3699 if (t->kind == TYPE_STRUCT
3700 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3702 const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3703 oprintf (output_header,
3704 "#define gt_%sx_%s gt_%sx_%s\n",
3705 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3706 if (t_id_for_tag != t->u.s.tag)
3707 free (CONST_CAST (char *, t_id_for_tag));
3709 else
3710 error_at_line (&s->u.s.line,
3711 "structure alias is not a structure");
3712 break;
3714 if (opt)
3715 continue;
3717 /* Declare the marker procedure only once. */
3718 oprintf (output_header,
3719 "extern void gt_%sx_%s (void *);\n",
3720 wtd->prefix, s_id_for_tag);
3722 if (s_id_for_tag != s->u.s.tag)
3723 free (CONST_CAST (char *, s_id_for_tag));
3725 if (s->u.s.line.file == NULL)
3727 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3728 s->u.s.tag);
3729 continue;
3733 /* At last we emit the functions code. */
3734 oprintf (output_header, "\n/* functions code */\n");
3735 for (s = structures; s; s = s->next)
3736 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3738 options_p opt;
3740 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3741 continue;
3742 for (opt = s->u.s.opt; opt; opt = opt->next)
3743 if (strcmp (opt->name, "ptr_alias") == 0)
3744 break;
3745 if (opt)
3746 continue;
3748 if (s->kind == TYPE_LANG_STRUCT)
3750 type_p ss;
3751 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3753 nbfun++;
3754 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3755 nbfun, (void*) ss, ss->u.s.tag);
3756 write_func_for_structure (s, ss, wtd);
3759 else
3761 nbfun++;
3762 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3763 nbfun, (void*) s, s->u.s.tag);
3764 write_func_for_structure (s, s, wtd);
3767 else
3769 /* Structure s is not possibly pointed to, so can be ignored. */
3770 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3771 (void*)s, s->u.s.tag,
3772 (int) s->gc_used);
3775 if (verbosity_level >= 2)
3776 printf ("%s emitted %d routines for %s\n",
3777 progname, nbfun, wtd->comment);
3780 static const struct write_types_data ggc_wtd = {
3781 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
3782 "GC marker procedures. ",
3783 WTK_GGC
3786 static const struct write_types_data pch_wtd = {
3787 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3788 "gt_pch_note_reorder",
3789 "PCH type-walking procedures. ",
3790 WTK_PCH
3793 /* Write out the local pointer-walking routines. */
3795 /* process_field routine for local pointer-walking for user-callable
3796 routines. The difference between this and
3797 write_types_local_process_field is that, in this case, we do not
3798 need to check whether the given pointer matches the address of the
3799 parent structure. This check was already generated by the call
3800 to gt_pch_nx in the main gt_pch_p_*() function that is calling
3801 this code. */
3803 static void
3804 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
3806 switch (f->kind)
3808 case TYPE_POINTER:
3809 case TYPE_STRUCT:
3810 case TYPE_UNION:
3811 case TYPE_LANG_STRUCT:
3812 case TYPE_STRING:
3813 if (d->in_nested_ptr)
3814 oprintf (d->of, "%*s op (&(%s), &(%s), cookie);\n",
3815 d->indent, "", d->val, d->prev_val[2]);
3816 oprintf (d->of, "%*s op (&(%s), NULL, cookie);\n",
3817 d->indent, "", d->val);
3818 break;
3820 case TYPE_USER_STRUCT:
3821 if (d->in_ptr_field)
3822 oprintf (d->of, "%*s op (&(%s), NULL, cookie);\n",
3823 d->indent, "", d->val);
3824 else
3825 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3826 d->indent, "", d->val);
3827 break;
3829 case TYPE_SCALAR:
3830 case TYPE_CALLBACK:
3831 break;
3833 case TYPE_ARRAY:
3834 case TYPE_NONE:
3835 case TYPE_UNDEFINED:
3836 gcc_unreachable ();
3841 /* Write a function to PCH walk all the fields of type S on OF.
3842 D contains data needed by walk_type to recurse into the fields of S. */
3844 static void
3845 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
3847 oprintf (d->of, "\nvoid\n");
3848 oprintf (d->of, "gt_pch_nx (");
3849 write_type_decl (d->of, s);
3850 oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
3851 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3852 "\tATTRIBUTE_UNUSED void *cookie)\n");
3853 oprintf (d->of, "{\n");
3854 d->val = "(*x)";
3855 d->indent = 2;
3856 d->process_field = write_types_local_user_process_field;
3857 walk_type (s, d);
3858 oprintf (d->of, "}\n");
3862 /* Emit the user-callable functions needed to mark all the types used
3863 by the user structure S. PREFIX is the prefix to use to
3864 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
3865 chain_next option defined. D contains data needed to pass to
3866 walk_type when traversing the fields of a type.
3868 For every type T referenced by S, two routines are generated: one
3869 that takes 'T *', marks the pointer and calls the second routine,
3870 which just marks the fields of T. */
3872 static void
3873 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
3875 gcc_assert (s->kind == TYPE_USER_STRUCT);
3877 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3879 type_p fld_type = fld->type;
3880 if (union_or_struct_p (fld_type))
3881 write_pch_user_walking_for_structure_body (fld_type, d);
3886 /* process_field routine for local pointer-walking. */
3888 static void
3889 write_types_local_process_field (type_p f, const struct walk_type_data *d)
3891 gcc_assert (d->have_this_obj);
3892 switch (f->kind)
3894 case TYPE_POINTER:
3895 case TYPE_STRUCT:
3896 case TYPE_UNION:
3897 case TYPE_LANG_STRUCT:
3898 case TYPE_STRING:
3899 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3900 d->prev_val[3]);
3901 if (d->in_nested_ptr)
3902 oprintf (d->of, "%*s op (&(%s), &(%s), cookie);\n",
3903 d->indent, "", d->val, d->prev_val[2]);
3904 else
3905 oprintf (d->of, "%*s op (&(%s), NULL, cookie);\n",
3906 d->indent, "", d->val);
3907 break;
3909 case TYPE_USER_STRUCT:
3910 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3911 d->prev_val[3]);
3912 if (d->in_ptr_field)
3913 oprintf (d->of, "%*s op (&(%s), NULL, cookie);\n",
3914 d->indent, "", d->val);
3915 else
3916 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3917 d->indent, "", d->val);
3918 break;
3920 case TYPE_SCALAR:
3921 break;
3923 case TYPE_CALLBACK:
3924 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3925 d->prev_val[3]);
3926 oprintf (d->of, "%*s gt_pch_note_callback (&(%s), this_obj);\n",
3927 d->indent, "", d->val);
3928 break;
3930 case TYPE_ARRAY:
3931 case TYPE_NONE:
3932 case TYPE_UNDEFINED:
3933 gcc_unreachable ();
3938 /* For S, a structure that's part of ORIG_S, and using parameters
3939 PARAM, write out a routine that:
3940 - Is of type gt_note_pointers
3941 - Calls PROCESS_FIELD on each field of S or its substructures.
3944 static void
3945 write_local_func_for_structure (const_type_p orig_s, type_p s)
3947 struct walk_type_data d;
3949 /* Don't write fns for subclasses, only for the ultimate base class
3950 within an inheritance hierarchy. */
3951 if (s->u.s.base_class)
3952 return;
3954 memset (&d, 0, sizeof (d));
3955 d.of = get_output_file_for_structure (s);
3956 d.process_field = write_types_local_process_field;
3957 d.opt = s->u.s.opt;
3958 d.line = &s->u.s.line;
3959 d.bitmap = s->u.s.bitmap;
3960 d.prev_val[0] = d.prev_val[2] = "*x";
3961 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3962 d.prev_val[3] = "x";
3963 d.val = "(*x)";
3964 d.fn_wants_lvalue = true;
3966 oprintf (d.of, "\n");
3967 oprintf (d.of, "void\n");
3968 oprintf (d.of, "gt_pch_p_");
3969 output_mangled_typename (d.of, orig_s);
3970 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3971 "\tvoid *x_p,\n"
3972 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3973 "\tATTRIBUTE_UNUSED void *cookie)\n");
3974 oprintf (d.of, "{\n");
3975 oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3976 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3977 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3978 d.indent = 2;
3979 d.have_this_obj = true;
3981 if (s->kind != TYPE_USER_STRUCT)
3982 walk_type (s, &d);
3983 else
3985 /* User structures have no fields to walk. Simply generate a
3986 call to the user-provided PCH walker. */
3987 oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
3988 d.prev_val[3]);
3989 oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3990 d.indent, "", d.val);
3993 oprintf (d.of, "}\n");
3995 /* Write user-callable entry points for the PCH walking routines. */
3996 if (orig_s->kind == TYPE_USER_STRUCT)
3997 write_pch_user_walking_functions (s, &d);
3999 for (options_p o = s->u.s.opt; o; o = o->next)
4000 if (strcmp (o->name, "for_user") == 0)
4002 write_pch_user_walking_for_structure_body (s, &d);
4003 break;
4007 /* Write out local marker routines for STRUCTURES. */
4009 static void
4010 write_local (outf_p output_header, type_p structures)
4012 type_p s;
4014 if (!output_header)
4015 return;
4017 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
4018 for (s = structures; s; s = s->next)
4019 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
4021 options_p opt;
4023 if (s->u.s.line.file == NULL)
4024 continue;
4025 for (opt = s->u.s.opt; opt; opt = opt->next)
4026 if (strcmp (opt->name, "ptr_alias") == 0
4027 && opt->kind == OPTION_TYPE)
4029 const_type_p const t = (const_type_p) opt->info.type;
4030 if (t->kind == TYPE_STRUCT
4031 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
4033 oprintf (output_header, "#define gt_pch_p_");
4034 output_mangled_typename (output_header, s);
4035 oprintf (output_header, " gt_pch_p_");
4036 output_mangled_typename (output_header, t);
4037 oprintf (output_header, "\n");
4039 else
4040 error_at_line (&s->u.s.line,
4041 "structure alias is not a structure");
4042 break;
4044 if (opt)
4045 continue;
4047 /* Declare the marker procedure only once. */
4048 oprintf (output_header, "extern void gt_pch_p_");
4049 output_mangled_typename (output_header, s);
4050 oprintf (output_header,
4051 "\n (void *, void *, gt_pointer_operator, void *);\n");
4053 if (s->kind == TYPE_LANG_STRUCT)
4055 type_p ss;
4056 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4057 write_local_func_for_structure (s, ss);
4059 else
4060 write_local_func_for_structure (s, s);
4064 /* Nonzero if S is a type for which typed GC allocators should be output. */
4066 #define USED_BY_TYPED_GC_P(s) \
4067 ((s->kind == TYPE_POINTER \
4068 && (s->u.p->gc_used == GC_POINTED_TO \
4069 || s->u.p->gc_used == GC_USED)) \
4070 || (union_or_struct_p (s) \
4071 && ((s)->gc_used == GC_POINTED_TO \
4072 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4073 && s->u.s.line.file != NULL) \
4074 || ((s)->gc_used == GC_USED \
4075 && !startswith (s->u.s.tag, "anonymous")) \
4076 || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
4080 /* Might T contain any non-pointer elements? */
4082 static int
4083 contains_scalar_p (type_p t)
4085 switch (t->kind)
4087 case TYPE_STRING:
4088 case TYPE_POINTER:
4089 return 0;
4090 case TYPE_ARRAY:
4091 return contains_scalar_p (t->u.a.p);
4092 case TYPE_USER_STRUCT:
4093 /* User-marked structures will typically contain pointers. */
4094 return 0;
4095 default:
4096 /* Could also check for structures that have no non-pointer
4097 fields, but there aren't enough of those to worry about. */
4098 return 1;
4102 /* Mangle INPF and print it to F. */
4104 static void
4105 put_mangled_filename (outf_p f, const input_file *inpf)
4107 /* The call to get_output_file_name may indirectly update fn since
4108 get_output_file_with_visibility caches its result inside, so we
4109 need the CONST_CAST. */
4110 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4111 if (!f || !name)
4112 return;
4113 for (; *name != 0; name++)
4114 if (ISALNUM (*name))
4115 oprintf (f, "%c", *name);
4116 else
4117 oprintf (f, "%c", '_');
4120 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4121 LASTNAME, and NAME are all strings to insert in various places in
4122 the resulting code. */
4124 static void
4125 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4126 const char *tname, const char *name)
4128 struct flist *fli2;
4130 for (fli2 = flp; fli2; fli2 = fli2->next)
4131 if (fli2->started_p)
4133 oprintf (fli2->f, " %s\n", lastname);
4134 oprintf (fli2->f, "};\n\n");
4137 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4138 if (fli2->started_p)
4140 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4141 int fnum;
4143 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4144 if (bitmap & 1)
4146 oprintf (base_files[fnum],
4147 "extern const struct %s gt_%s_", tname, pfx);
4148 put_mangled_filename (base_files[fnum], fli2->file);
4149 oprintf (base_files[fnum], "[];\n");
4154 size_t fnum;
4155 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4156 oprintf (base_files[fnum],
4157 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4161 for (fli2 = flp; fli2; fli2 = fli2->next)
4162 if (fli2->started_p)
4164 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4165 int fnum;
4167 fli2->started_p = 0;
4169 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4170 if (bitmap & 1)
4172 oprintf (base_files[fnum], " gt_%s_", pfx);
4173 put_mangled_filename (base_files[fnum], fli2->file);
4174 oprintf (base_files[fnum], ",\n");
4179 size_t fnum;
4180 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4182 oprintf (base_files[fnum], " NULL\n");
4183 oprintf (base_files[fnum], "};\n");
4188 /* Finish off the created gt_clear_caches_file_c functions. */
4190 static void
4191 finish_cache_funcs (flist *flp)
4193 struct flist *fli2;
4195 for (fli2 = flp; fli2; fli2 = fli2->next)
4196 if (fli2->started_p)
4198 oprintf (fli2->f, "}\n\n");
4201 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4202 if (fli2->started_p)
4204 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4205 int fnum;
4207 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4208 if (bitmap & 1)
4210 oprintf (base_files[fnum], "extern void gt_clear_caches_");
4211 put_mangled_filename (base_files[fnum], fli2->file);
4212 oprintf (base_files[fnum], " ();\n");
4216 for (size_t fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4217 oprintf (base_files[fnum], "void\ngt_clear_caches ()\n{\n");
4219 for (fli2 = flp; fli2; fli2 = fli2->next)
4220 if (fli2->started_p)
4222 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4223 int fnum;
4225 fli2->started_p = 0;
4227 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4228 if (bitmap & 1)
4230 oprintf (base_files[fnum], " gt_clear_caches_");
4231 put_mangled_filename (base_files[fnum], fli2->file);
4232 oprintf (base_files[fnum], " ();\n");
4236 for (size_t fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4238 oprintf (base_files[fnum], "}\n");
4242 /* Write the first three fields (pointer, count and stride) for
4243 root NAME to F. V and LINE are as for write_root.
4245 Return true if the entry could be written; return false on error. */
4247 static bool
4248 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4250 type_p ap;
4252 if (!v)
4254 error_at_line (line, "`%s' is too complex to be a root", name);
4255 return false;
4258 oprintf (f, " {\n");
4259 oprintf (f, " &%s,\n", name);
4260 oprintf (f, " 1");
4262 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4263 if (ap->u.a.len[0])
4264 oprintf (f, " * (%s)", ap->u.a.len);
4265 else if (ap == v->type)
4266 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4267 oprintf (f, ",\n");
4268 oprintf (f, " sizeof (%s", v->name);
4269 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4270 oprintf (f, "[0]");
4271 oprintf (f, "),\n");
4272 return true;
4275 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4276 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4277 of the caller. */
4279 static void
4280 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4281 int has_length, struct fileloc *line,
4282 bool emit_pch, type_p field_type, const char *field_name)
4284 struct pair newv;
4285 /* If the field reference is relative to V, rather than to some
4286 subcomponent of V, we can mark any subarrays with a single stride.
4287 We're effectively treating the field as a global variable in its
4288 own right. */
4289 if (v && type == v->type)
4291 newv = *v;
4292 newv.type = field_type;
4293 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4294 v = &newv;
4296 /* Otherwise, any arrays nested in the structure are too complex to
4297 handle. */
4298 else if (field_type->kind == TYPE_ARRAY)
4299 v = NULL;
4300 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4301 has_length, line, emit_pch);
4304 /* Write out to F the table entry and any marker routines needed to
4305 mark NAME as TYPE. V can be one of three values:
4307 - null, if NAME is too complex to represent using a single
4308 count and stride. In this case, it is an error for NAME to
4309 contain any gc-ed data.
4311 - the outermost array that contains NAME, if NAME is part of an array.
4313 - the C variable that contains NAME, if NAME is not part of an array.
4315 LINE is the line of the C source that declares the root variable.
4316 HAS_LENGTH is nonzero iff V was a variable-length array. */
4318 static void
4319 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4320 struct fileloc *line, bool emit_pch)
4322 switch (type->kind)
4324 case TYPE_STRUCT:
4326 pair_p fld;
4327 for (fld = type->u.s.fields; fld; fld = fld->next)
4329 int skip_p = 0;
4330 const char *desc = NULL;
4331 options_p o;
4333 for (o = fld->opt; o; o = o->next)
4334 if (strcmp (o->name, "skip") == 0)
4335 skip_p = 1;
4336 else if (strcmp (o->name, "desc") == 0
4337 && o->kind == OPTION_STRING)
4338 desc = o->info.string;
4339 else if (strcmp (o->name, "string_length") == 0)
4340 /* See 'doc/gty.texi'. */
4341 error_at_line (line,
4342 "option `%s' not supported for field `%s' of global `%s'",
4343 o->name, fld->name, name);
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 case TYPE_CALLBACK:
4464 error_at_line (line, "global `%s' is unimplemented type", name);
4468 /* This generates a routine to walk an array. */
4470 static void
4471 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4473 struct walk_type_data d;
4474 char *prevval3;
4476 memset (&d, 0, sizeof (d));
4477 d.of = f;
4478 d.cookie = wtd;
4479 d.indent = 2;
4480 d.line = &v->line;
4481 d.opt = v->opt;
4482 d.bitmap = get_lang_bitmap (v->line.file);
4484 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4486 if (wtd->param_prefix)
4488 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4489 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
4490 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4491 wtd->param_prefix, v->name);
4492 oprintf (d.of,
4493 " ATTRIBUTE_UNUSED void *x_p,\n"
4494 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4495 " ATTRIBUTE_UNUSED void * cookie)\n");
4496 oprintf (d.of, "{\n");
4497 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4498 d.process_field = write_types_local_process_field;
4499 d.have_this_obj = true;
4500 walk_type (v->type, &d);
4501 oprintf (f, "}\n\n");
4504 d.opt = v->opt;
4505 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4506 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4507 wtd->prefix, v->name);
4508 oprintf (f, "{\n");
4509 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4510 d.process_field = write_types_process_field;
4511 d.have_this_obj = false;
4512 walk_type (v->type, &d);
4513 free (prevval3);
4514 oprintf (f, "}\n\n");
4517 /* Output a table describing the locations and types of VARIABLES. */
4519 static void
4520 write_roots (pair_p variables, bool emit_pch)
4522 pair_p v;
4523 struct flist *flp = NULL;
4525 for (v = variables; v; v = v->next)
4527 outf_p f =
4528 get_output_file_with_visibility (CONST_CAST (input_file*,
4529 v->line.file));
4530 struct flist *fli;
4531 const char *length = NULL;
4532 int deletable_p = 0;
4533 options_p o;
4534 for (o = v->opt; o; o = o->next)
4535 if (strcmp (o->name, "length") == 0
4536 && o->kind == OPTION_STRING)
4537 length = o->info.string;
4538 else if (strcmp (o->name, "deletable") == 0)
4539 deletable_p = 1;
4540 else if (strcmp (o->name, "cache") == 0)
4542 else if (strcmp (o->name, "string_length") == 0)
4543 /* See 'doc/gty.texi'. */
4544 error_at_line (&v->line,
4545 "option `%s' not supported for global `%s'",
4546 o->name, v->name);
4547 else
4548 error_at_line (&v->line,
4549 "global `%s' has unknown option `%s'",
4550 v->name, o->name);
4552 for (fli = flp; fli; fli = fli->next)
4553 if (fli->f == f && f)
4554 break;
4555 if (fli == NULL)
4557 fli = XNEW (struct flist);
4558 fli->f = f;
4559 fli->next = flp;
4560 fli->started_p = 0;
4561 fli->file = v->line.file;
4562 gcc_assert (fli->file);
4563 flp = fli;
4565 oprintf (f, "\n/* GC roots. */\n\n");
4568 if (!deletable_p
4569 && length
4570 && v->type->kind == TYPE_POINTER
4571 && (v->type->u.p->kind == TYPE_POINTER
4572 || v->type->u.p->kind == TYPE_STRUCT))
4574 write_array (f, v, &ggc_wtd);
4575 write_array (f, v, &pch_wtd);
4579 for (v = variables; v; v = v->next)
4581 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4582 v->line.file));
4583 struct flist *fli;
4584 int skip_p = 0;
4585 int length_p = 0;
4586 options_p o;
4588 for (o = v->opt; o; o = o->next)
4589 if (strcmp (o->name, "length") == 0)
4590 length_p = 1;
4591 else if (strcmp (o->name, "deletable") == 0)
4592 skip_p = 1;
4594 if (skip_p)
4595 continue;
4597 for (fli = flp; fli; fli = fli->next)
4598 if (fli->f == f)
4599 break;
4600 if (!fli->started_p)
4602 fli->started_p = 1;
4604 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4605 put_mangled_filename (f, v->line.file);
4606 oprintf (f, "[] = {\n");
4609 write_root (f, v, v->type, v->name, length_p, &v->line, emit_pch);
4612 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4613 "gt_ggc_rtab");
4615 for (v = variables; v; v = v->next)
4617 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4618 v->line.file));
4619 struct flist *fli;
4620 int skip_p = 1;
4621 options_p o;
4623 for (o = v->opt; o; o = o->next)
4624 if (strcmp (o->name, "deletable") == 0)
4625 skip_p = 0;
4627 if (skip_p)
4628 continue;
4630 for (fli = flp; fli; fli = fli->next)
4631 if (fli->f == f)
4632 break;
4633 if (!fli->started_p)
4635 fli->started_p = 1;
4637 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4638 put_mangled_filename (f, v->line.file);
4639 oprintf (f, "[] = {\n");
4642 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4643 v->name, v->name);
4646 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4647 "gt_ggc_deletable_rtab");
4649 for (v = variables; v; v = v->next)
4651 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4652 v->line.file));
4653 struct flist *fli;
4654 bool cache = false;
4655 options_p o;
4657 for (o = v->opt; o; o = o->next)
4658 if (strcmp (o->name, "cache") == 0)
4659 cache = true;
4660 if (!cache)
4661 continue;
4663 for (fli = flp; fli; fli = fli->next)
4664 if (fli->f == f)
4665 break;
4666 if (!fli->started_p)
4668 fli->started_p = 1;
4670 oprintf (f, "void\ngt_clear_caches_");
4671 put_mangled_filename (f, v->line.file);
4672 oprintf (f, " ()\n{\n");
4675 oprintf (f, " gt_cleare_cache (%s);\n", v->name);
4678 finish_cache_funcs (flp);
4680 if (!emit_pch)
4681 return;
4683 for (v = variables; v; v = v->next)
4685 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4686 v->line.file));
4687 struct flist *fli;
4688 int skip_p = 0;
4689 options_p o;
4691 for (o = v->opt; o; o = o->next)
4692 if (strcmp (o->name, "deletable") == 0)
4694 skip_p = 1;
4695 break;
4698 if (skip_p)
4699 continue;
4701 if (!contains_scalar_p (v->type))
4702 continue;
4704 for (fli = flp; fli; fli = fli->next)
4705 if (fli->f == f)
4706 break;
4707 if (!fli->started_p)
4709 fli->started_p = 1;
4711 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4712 put_mangled_filename (f, v->line.file);
4713 oprintf (f, "[] = {\n");
4716 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4717 v->name, v->name);
4720 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4721 "gt_pch_scalar_rtab");
4724 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4725 guarantee for somewhat increased readability. If name conflicts do happen,
4726 this function will have to be adjusted to be more like
4727 output_mangled_typename. */
4729 #define INDENT 2
4731 /* Dumps the value of typekind KIND. */
4733 static void
4734 dump_typekind (int indent, enum typekind kind)
4736 printf ("%*ckind = ", indent, ' ');
4737 switch (kind)
4739 case TYPE_SCALAR:
4740 printf ("TYPE_SCALAR");
4741 break;
4742 case TYPE_STRING:
4743 printf ("TYPE_STRING");
4744 break;
4745 case TYPE_STRUCT:
4746 printf ("TYPE_STRUCT");
4747 break;
4748 case TYPE_UNDEFINED:
4749 printf ("TYPE_UNDEFINED");
4750 break;
4751 case TYPE_USER_STRUCT:
4752 printf ("TYPE_USER_STRUCT");
4753 break;
4754 case TYPE_UNION:
4755 printf ("TYPE_UNION");
4756 break;
4757 case TYPE_POINTER:
4758 printf ("TYPE_POINTER");
4759 break;
4760 case TYPE_ARRAY:
4761 printf ("TYPE_ARRAY");
4762 break;
4763 case TYPE_CALLBACK:
4764 printf ("TYPE_CALLBACK");
4765 break;
4766 case TYPE_LANG_STRUCT:
4767 printf ("TYPE_LANG_STRUCT");
4768 break;
4769 default:
4770 gcc_unreachable ();
4772 printf ("\n");
4775 /* Dumps the value of GC_USED flag. */
4777 static void
4778 dump_gc_used (int indent, enum gc_used_enum gc_used)
4780 printf ("%*cgc_used = ", indent, ' ');
4781 switch (gc_used)
4783 case GC_UNUSED:
4784 printf ("GC_UNUSED");
4785 break;
4786 case GC_USED:
4787 printf ("GC_USED");
4788 break;
4789 case GC_MAYBE_POINTED_TO:
4790 printf ("GC_MAYBE_POINTED_TO");
4791 break;
4792 case GC_POINTED_TO:
4793 printf ("GC_POINTED_TO");
4794 break;
4795 default:
4796 gcc_unreachable ();
4798 printf ("\n");
4801 /* Dumps the type options OPT. */
4803 static void
4804 dump_options (int indent, options_p opt)
4806 options_p o;
4807 printf ("%*coptions = ", indent, ' ');
4808 o = opt;
4809 while (o)
4811 switch (o->kind)
4813 case OPTION_STRING:
4814 printf ("%s:string %s ", o->name, o->info.string);
4815 break;
4816 case OPTION_TYPE:
4817 printf ("%s:type ", o->name);
4818 dump_type (indent+1, o->info.type);
4819 break;
4820 case OPTION_NESTED:
4821 printf ("%s:nested ", o->name);
4822 break;
4823 case OPTION_NONE:
4824 gcc_unreachable ();
4826 o = o->next;
4828 printf ("\n");
4831 /* Dumps the source file location in LINE. */
4833 static void
4834 dump_fileloc (int indent, struct fileloc line)
4836 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
4837 get_input_file_name (line.file),
4838 line.line);
4841 /* Recursively dumps the struct, union, or a language-specific
4842 struct T. */
4844 static void
4845 dump_type_u_s (int indent, type_p t)
4847 pair_p fields;
4849 gcc_assert (union_or_struct_p (t));
4850 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
4851 dump_fileloc (indent, t->u.s.line);
4852 printf ("%*cu.s.fields =\n", indent, ' ');
4853 fields = t->u.s.fields;
4854 while (fields)
4856 dump_pair (indent + INDENT, fields);
4857 fields = fields->next;
4859 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
4860 dump_options (indent, t->u.s.opt);
4861 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
4862 if (t->kind == TYPE_LANG_STRUCT)
4864 printf ("%*cu.s.lang_struct:\n", indent, ' ');
4865 dump_type_list (indent + INDENT, t->u.s.lang_struct);
4869 /* Recursively dumps the array T. */
4871 static void
4872 dump_type_u_a (int indent, type_p t)
4874 gcc_assert (t->kind == TYPE_ARRAY);
4875 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
4876 dump_type_list (indent + INDENT, t->u.a.p);
4879 /* Recursively dumps the type list T. */
4881 static void
4882 dump_type_list (int indent, type_p t)
4884 type_p p = t;
4885 while (p)
4887 dump_type (indent, p);
4888 p = p->next;
4892 static htab_t seen_types;
4894 /* Recursively dumps the type T if it was not dumped previously. */
4896 static void
4897 dump_type (int indent, type_p t)
4899 void **slot;
4901 printf ("%*cType at %p: ", indent, ' ', (void *) t);
4902 if (t->kind == TYPE_UNDEFINED)
4904 gcc_assert (t->gc_used == GC_UNUSED);
4905 printf ("undefined.\n");
4906 return;
4909 if (seen_types == NULL)
4910 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
4912 slot = htab_find_slot (seen_types, t, INSERT);
4913 if (*slot != NULL)
4915 printf ("already seen.\n");
4916 return;
4918 *slot = t;
4919 printf ("\n");
4921 dump_typekind (indent, t->kind);
4922 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
4923 (void *) t->pointer_to);
4924 dump_gc_used (indent + INDENT, t->gc_used);
4925 switch (t->kind)
4927 case TYPE_SCALAR:
4928 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
4929 t->u.scalar_is_char ? "true" : "false");
4930 break;
4931 case TYPE_STRING:
4932 case TYPE_CALLBACK:
4933 break;
4934 case TYPE_STRUCT:
4935 case TYPE_UNION:
4936 case TYPE_LANG_STRUCT:
4937 case TYPE_USER_STRUCT:
4938 dump_type_u_s (indent + INDENT, t);
4939 break;
4940 case TYPE_POINTER:
4941 printf ("%*cp:\n", indent + INDENT, ' ');
4942 dump_type (indent + INDENT, t->u.p);
4943 break;
4944 case TYPE_ARRAY:
4945 dump_type_u_a (indent + INDENT, t);
4946 break;
4947 default:
4948 gcc_unreachable ();
4950 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
4953 /* Dumps the pair P. */
4955 static void
4956 dump_pair (int indent, pair_p p)
4958 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
4959 dump_type (indent, p->type);
4960 dump_fileloc (indent, p->line);
4961 dump_options (indent, p->opt);
4962 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
4965 /* Dumps the list of pairs PP. */
4967 static void
4968 dump_pair_list (const char *name, pair_p pp)
4970 pair_p p;
4971 printf ("%s:\n", name);
4972 for (p = pp; p != NULL; p = p->next)
4973 dump_pair (0, p);
4974 printf ("End of %s\n\n", name);
4977 /* Dumps the STRUCTURES. */
4979 static void
4980 dump_structures (const char *name, type_p structures)
4982 printf ("%s:\n", name);
4983 dump_type_list (0, structures);
4984 printf ("End of %s\n\n", name);
4987 /* Dumps the internal structures of gengtype. This is useful to debug
4988 gengtype itself, or to understand what it does, e.g. for plugin
4989 developers. */
4991 static void
4992 dump_everything (void)
4994 dump_pair_list ("typedefs", typedefs);
4995 dump_structures ("structures", structures);
4996 dump_pair_list ("variables", variables);
4998 /* Allocated with the first call to dump_type. */
4999 htab_delete (seen_types);
5004 /* Option specification for getopt_long. */
5005 static const struct option gengtype_long_options[] = {
5006 {"help", no_argument, NULL, 'h'},
5007 {"version", no_argument, NULL, 'V'},
5008 {"verbose", no_argument, NULL, 'v'},
5009 {"dump", no_argument, NULL, 'd'},
5010 {"debug", no_argument, NULL, 'D'},
5011 {"plugin", required_argument, NULL, 'P'},
5012 {"srcdir", required_argument, NULL, 'S'},
5013 {"backupdir", required_argument, NULL, 'B'},
5014 {"inputs", required_argument, NULL, 'I'},
5015 {"read-state", required_argument, NULL, 'r'},
5016 {"write-state", required_argument, NULL, 'w'},
5017 /* Terminating NULL placeholder. */
5018 {NULL, no_argument, NULL, 0},
5022 static void
5023 print_usage (void)
5025 printf ("Usage: %s\n", progname);
5026 printf ("\t -h | --help " " \t# Give this help.\n");
5027 printf ("\t -D | --debug "
5028 " \t# Give debug output to debug %s itself.\n", progname);
5029 printf ("\t -V | --version " " \t# Give version information.\n");
5030 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5031 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5032 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5033 " \t# Generate for plugin.\n");
5034 printf ("\t -S | --srcdir <GCC-directory> "
5035 " \t# Specify the GCC source directory.\n");
5036 printf ("\t -B | --backupdir <directory> "
5037 " \t# Specify the backup directory for updated files.\n");
5038 printf ("\t -I | --inputs <input-list> "
5039 " \t# Specify the file with source files list.\n");
5040 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5041 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5044 static void
5045 print_version (void)
5047 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5048 printf ("Report bugs: %s\n", bug_report_url);
5051 /* Parse the program options using getopt_long... */
5052 static void
5053 parse_program_options (int argc, char **argv)
5055 int opt = -1;
5056 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5057 gengtype_long_options, NULL)) >= 0)
5059 switch (opt)
5061 case 'h': /* --help */
5062 print_usage ();
5063 break;
5064 case 'V': /* --version */
5065 print_version ();
5066 break;
5067 case 'd': /* --dump */
5068 do_dump = 1;
5069 break;
5070 case 'D': /* --debug */
5071 do_debug = 1;
5072 break;
5073 case 'v': /* --verbose */
5074 verbosity_level++;
5075 break;
5076 case 'P': /* --plugin */
5077 if (optarg)
5078 plugin_output_filename = optarg;
5079 else
5080 fatal ("missing plugin output file name");
5081 break;
5082 case 'S': /* --srcdir */
5083 if (optarg)
5084 srcdir = optarg;
5085 else
5086 fatal ("missing source directory");
5087 srcdir_len = strlen (srcdir);
5088 break;
5089 case 'B': /* --backupdir */
5090 if (optarg)
5091 backup_dir = optarg;
5092 else
5093 fatal ("missing backup directory");
5094 break;
5095 case 'I': /* --inputs */
5096 if (optarg)
5097 inputlist = optarg;
5098 else
5099 fatal ("missing input list");
5100 break;
5101 case 'r': /* --read-state */
5102 if (optarg)
5103 read_state_filename = optarg;
5104 else
5105 fatal ("missing read state file");
5106 DBGPRINTF ("read state %s\n", optarg);
5107 break;
5108 case 'w': /* --write-state */
5109 DBGPRINTF ("write state %s\n", optarg);
5110 if (optarg)
5111 write_state_filename = optarg;
5112 else
5113 fatal ("missing write state file");
5114 break;
5115 default:
5116 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5117 print_usage ();
5118 fatal ("unexpected flag");
5121 if (plugin_output_filename)
5123 /* In plugin mode we require some input files. */
5124 int i = 0;
5125 if (optind >= argc)
5126 fatal ("no source files given in plugin mode");
5127 nb_plugin_files = argc - optind;
5128 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5129 for (i = 0; i < (int) nb_plugin_files; i++)
5131 char *name = argv[i + optind];
5132 plugin_files[i] = input_file_by_name (name);
5139 /******* Manage input files. ******/
5141 /* Hash table of unique input file names. */
5142 static htab_t input_file_htab;
5144 /* Find or allocate a new input_file by hash-consing it. */
5145 input_file*
5146 input_file_by_name (const char* name)
5148 void ** slot;
5149 input_file* f = NULL;
5150 int namlen = 0;
5151 if (!name)
5152 return NULL;
5153 namlen = strlen (name);
5154 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5155 f->inpbitmap = 0;
5156 f->inpoutf = NULL;
5157 f->inpisplugin = false;
5158 strcpy (f->inpname, name);
5159 slot = htab_find_slot (input_file_htab, f, INSERT);
5160 gcc_assert (slot != NULL);
5161 if (*slot)
5163 /* Already known input file. */
5164 free (f);
5165 return (input_file*)(*slot);
5167 /* New input file. */
5168 *slot = f;
5169 return f;
5172 /* Hash table support routines for input_file-s. */
5173 static hashval_t
5174 htab_hash_inputfile (const void *p)
5176 const input_file *inpf = (const input_file *) p;
5177 gcc_assert (inpf);
5178 return htab_hash_string (get_input_file_name (inpf));
5181 static int
5182 htab_eq_inputfile (const void *x, const void *y)
5184 const input_file *inpfx = (const input_file *) x;
5185 const input_file *inpfy = (const input_file *) y;
5186 gcc_assert (inpfx != NULL && inpfy != NULL);
5187 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5192 main (int argc, char **argv)
5194 size_t i;
5195 static struct fileloc pos = { NULL, 0 };
5196 outf_p output_header;
5198 /* Mandatory common initializations. */
5199 progname = "gengtype"; /* For fatal and messages. */
5200 /* Create the hash-table used to hash-cons input files. */
5201 input_file_htab =
5202 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5203 /* Initialize our special input files. */
5204 this_file = input_file_by_name (__FILE__);
5205 system_h_file = input_file_by_name ("system.h");
5206 /* Set the scalar_is_char union number for predefined scalar types. */
5207 scalar_nonchar.u.scalar_is_char = false;
5208 scalar_char.u.scalar_is_char = true;
5210 parse_program_options (argc, argv);
5212 if (do_debug)
5214 time_t now = (time_t) 0;
5215 time (&now);
5216 DBGPRINTF ("gengtype started pid %d at %s",
5217 (int) getpid (), ctime (&now));
5220 /* Parse the input list and the input files. */
5221 DBGPRINTF ("inputlist %s", inputlist);
5222 if (read_state_filename)
5224 if (inputlist)
5225 fatal ("input list %s cannot be given with a read state file %s",
5226 inputlist, read_state_filename);
5227 read_state (read_state_filename);
5228 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5230 else if (inputlist)
5232 /* These types are set up with #define or else outside of where
5233 we can see them. We should initialize them before calling
5234 read_input_list. */
5235 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5236 Call;} while (0)
5237 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5238 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5239 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5240 POS_HERE (do_scalar_typedef ("double_int", &pos));
5241 POS_HERE (do_scalar_typedef ("offset_int", &pos));
5242 POS_HERE (do_scalar_typedef ("int64_t", &pos));
5243 POS_HERE (do_scalar_typedef ("poly_int64", &pos));
5244 POS_HERE (do_scalar_typedef ("poly_uint64", &pos));
5245 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5246 POS_HERE (do_scalar_typedef ("uint32_t", &pos));
5247 POS_HERE (do_scalar_typedef ("uint8", &pos));
5248 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5249 POS_HERE (do_scalar_typedef ("jword", &pos));
5250 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5251 POS_HERE (do_scalar_typedef ("void", &pos));
5252 POS_HERE (do_scalar_typedef ("machine_mode", &pos));
5253 POS_HERE (do_scalar_typedef ("fixed_size_mode", &pos));
5254 POS_HERE (do_scalar_typedef ("CONSTEXPR", &pos));
5255 POS_HERE (do_typedef ("void *",
5256 create_pointer (resolve_typedef ("void", &pos)),
5257 &pos));
5258 #undef POS_HERE
5259 read_input_list (inputlist);
5260 num_build_headers = 0;
5261 for (i = 0; i < num_gt_files; i++)
5263 const char *fname = get_input_file_name (gt_files[i]);
5264 parse_file (fname);
5265 DBGPRINTF ("parsed file #%d %s", (int) i, fname);
5266 /* Check if this is a header file generated during the build. */
5267 int len = strlen (fname);
5268 if (len >= 5
5269 && fname[0] == '.'
5270 && IS_DIR_SEPARATOR (fname[1])
5271 && fname[len-2] == '.'
5272 && fname[len-1] == 'h')
5273 num_build_headers++;
5275 if (verbosity_level >= 1)
5276 printf ("%s parsed %d files with %d GTY types\n",
5277 progname, (int) num_gt_files, type_count);
5279 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5281 else
5282 fatal ("either an input list or a read state file should be given");
5283 if (hit_error)
5284 return 1;
5287 if (plugin_output_filename)
5289 size_t ix = 0;
5290 /* In plugin mode, we should have read a state file, and have
5291 given at least one plugin file. */
5292 if (!read_state_filename)
5293 fatal ("No read state given in plugin mode for %s",
5294 plugin_output_filename);
5296 if (nb_plugin_files == 0 || !plugin_files)
5297 fatal ("No plugin files given in plugin mode for %s",
5298 plugin_output_filename);
5300 /* Parse our plugin files and augment the state. */
5301 for (ix = 0; ix < nb_plugin_files; ix++)
5303 input_file* pluginput = plugin_files [ix];
5304 pluginput->inpisplugin = true;
5305 parse_file (get_input_file_name (pluginput));
5307 if (hit_error)
5308 return 1;
5310 plugin_output = create_file ("GCC", plugin_output_filename);
5311 DBGPRINTF ("created plugin_output %p named %s",
5312 (void *) plugin_output, plugin_output->name);
5314 else
5315 { /* No plugin files, we are in normal mode. */
5316 if (!srcdir)
5317 fatal ("gengtype needs a source directory in normal mode");
5319 if (hit_error)
5320 return 1;
5322 gen_rtx_next ();
5324 set_gc_used (variables);
5326 for (type_p t = structures; t; t = t->next)
5328 bool for_user = false;
5329 for (options_p o = t->u.s.opt; o; o = o->next)
5330 if (strcmp (o->name, "for_user") == 0)
5332 for_user = true;
5333 break;
5336 if (for_user)
5337 set_gc_used_type (t, GC_POINTED_TO);
5339 /* The state at this point is read from the state input file or by
5340 parsing source files and optionally augmented by parsing plugin
5341 source files. Write it now. */
5342 if (write_state_filename)
5344 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5346 if (hit_error)
5347 fatal ("didn't write state file %s after errors",
5348 write_state_filename);
5350 DBGPRINTF ("before write_state %s", write_state_filename);
5351 write_state (write_state_filename);
5353 if (do_dump)
5354 dump_everything ();
5356 /* After having written the state file we return immediately to
5357 avoid generating any output file. */
5358 if (hit_error)
5359 return 1;
5360 else
5361 return 0;
5365 open_base_files ();
5367 output_header = plugin_output ? plugin_output : header_file;
5368 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5369 structures);
5371 write_types (output_header, structures, &ggc_wtd);
5372 if (plugin_files == NULL)
5374 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5375 structures);
5376 write_types (header_file, structures, &pch_wtd);
5377 write_local (header_file, structures);
5379 write_roots (variables, plugin_files == NULL);
5380 write_rtx_next ();
5381 close_output_files ();
5383 if (do_dump)
5384 dump_everything ();
5386 /* Don't bother about free-ing any input or plugin file, etc. */
5388 if (hit_error)
5389 return 1;
5390 return 0;