Introduce gimple_phi and use it in various places
[official-gcc.git] / gcc / gengtype.c
blobbc88eb8224f13e7ec0f202f7421e6b0f56771cb5
1 /* Process source files and output type information.
2 Copyright (C) 2002-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #ifdef GENERATOR_FILE
21 #include "bconfig.h"
22 #else
23 #include "config.h"
24 #endif
25 #include "system.h"
26 #include "errors.h" /* for fatal */
27 #include "getopt.h"
28 #include "version.h" /* for version_string & pkgversion_string. */
29 #include "hashtab.h"
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 /* asprintf, but produces fatal message on out-of-memory. */
123 char *
124 xasprintf (const char *format, ...)
126 int n;
127 char *result;
128 va_list ap;
130 va_start (ap, format);
131 n = vasprintf (&result, format, ap);
132 if (result == NULL || n < 0)
133 fatal ("out of memory");
134 va_end (ap);
136 return result;
139 /* Locate the ultimate base class of struct S. */
141 static const_type_p
142 get_ultimate_base_class (const_type_p s)
144 while (s->u.s.base_class)
145 s = s->u.s.base_class;
146 return s;
149 static type_p
150 get_ultimate_base_class (type_p s)
152 while (s->u.s.base_class)
153 s = s->u.s.base_class;
154 return s;
157 /* Input file handling. */
159 /* Table of all input files. */
160 const input_file **gt_files;
161 size_t num_gt_files;
163 /* A number of places use the name of this "gengtype.c" file for a
164 location for things that we can't rely on the source to define.
165 Make sure we can still use pointer comparison on filenames. */
166 input_file* this_file;
167 /* The "system.h" file is likewise specially useful. */
168 input_file* system_h_file;
170 /* Vector of per-language directories. */
171 const char **lang_dir_names;
172 size_t num_lang_dirs;
174 /* An array of output files suitable for definitions. There is one
175 BASE_FILES entry for each language. */
176 static outf_p *base_files;
180 #if ENABLE_CHECKING
181 /* Utility debugging function, printing the various type counts within
182 a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
183 void
184 dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
186 int nb_types = 0, nb_scalar = 0, nb_string = 0;
187 int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
188 int nb_lang_struct = 0, nb_param_struct = 0;
189 int nb_user_struct = 0, nb_undefined = 0;
190 type_p p = NULL;
191 for (p = t; p; p = p->next)
193 nb_types++;
194 switch (p->kind)
196 case TYPE_UNDEFINED:
197 nb_undefined++;
198 case TYPE_SCALAR:
199 nb_scalar++;
200 break;
201 case TYPE_STRING:
202 nb_string++;
203 break;
204 case TYPE_STRUCT:
205 nb_struct++;
206 break;
207 case TYPE_USER_STRUCT:
208 nb_user_struct++;
209 break;
210 case TYPE_UNION:
211 nb_union++;
212 break;
213 case TYPE_POINTER:
214 nb_pointer++;
215 break;
216 case TYPE_ARRAY:
217 nb_array++;
218 break;
219 case TYPE_LANG_STRUCT:
220 nb_lang_struct++;
221 break;
222 case TYPE_PARAM_STRUCT:
223 nb_param_struct++;
224 break;
225 case TYPE_NONE:
226 gcc_unreachable ();
229 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
230 lbasename (fil), lin, msg, nb_types);
231 if (nb_scalar > 0 || nb_string > 0)
232 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
233 if (nb_struct > 0 || nb_union > 0)
234 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
235 if (nb_pointer > 0 || nb_array > 0)
236 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
237 if (nb_lang_struct > 0 || nb_param_struct > 0)
238 fprintf (stderr, "@@%%@@ %d lang_structs, %d param_structs\n",
239 nb_lang_struct, nb_param_struct);
240 if (nb_user_struct > 0)
241 fprintf (stderr, "@@%%@@ %d user_structs\n", nb_user_struct);
242 if (nb_undefined > 0)
243 fprintf (stderr, "@@%%@@ %d undefined types\n", nb_undefined);
244 fprintf (stderr, "\n");
246 #endif /* ENABLE_CHECKING */
248 /* Scan the input file, LIST, and determine how much space we need to
249 store strings in. Also, count the number of language directories
250 and files. The numbers returned are overestimates as they does not
251 consider repeated files. */
252 static size_t
253 measure_input_list (FILE *list)
255 size_t n = 0;
256 int c;
257 bool atbol = true;
258 num_lang_dirs = 0;
259 num_gt_files = plugin_files ? nb_plugin_files : 0;
260 while ((c = getc (list)) != EOF)
262 n++;
263 if (atbol)
265 if (c == '[')
266 num_lang_dirs++;
267 else
269 /* Add space for a lang_bitmap before the input file name. */
270 n += sizeof (lang_bitmap);
271 num_gt_files++;
273 atbol = false;
276 if (c == '\n')
277 atbol = true;
280 rewind (list);
281 return n;
284 /* Read one input line from LIST to HEREP (which is updated). A
285 pointer to the string is returned via LINEP. If it was a language
286 subdirectory in square brackets, strip off the square brackets and
287 return true. Otherwise, leave space before the string for a
288 lang_bitmap, and return false. At EOF, returns false, does not
289 touch *HEREP, and sets *LINEP to NULL. POS is used for
290 diagnostics. */
291 static bool
292 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
294 char *here = *herep;
295 char *line;
296 int c = getc (list);
298 /* Read over whitespace. */
299 while (c == '\n' || c == ' ')
300 c = getc (list);
302 if (c == EOF)
304 *linep = 0;
305 return false;
307 else if (c == '[')
309 /* No space for a lang_bitmap is necessary. Discard the '['. */
310 c = getc (list);
311 line = here;
312 while (c != ']' && c != '\n' && c != EOF)
314 *here++ = c;
315 c = getc (list);
317 *here++ = '\0';
319 if (c == ']')
321 c = getc (list); /* eat what should be a newline */
322 if (c != '\n' && c != EOF)
323 error_at_line (pos, "junk on line after language tag [%s]", line);
325 else
326 error_at_line (pos, "missing close bracket for language tag [%s",
327 line);
329 *herep = here;
330 *linep = line;
331 return true;
333 else
335 /* Leave space for a lang_bitmap. */
336 memset (here, 0, sizeof (lang_bitmap));
337 here += sizeof (lang_bitmap);
338 line = here;
341 *here++ = c;
342 c = getc (list);
344 while (c != EOF && c != '\n');
345 *here++ = '\0';
346 *herep = here;
347 *linep = line;
348 return false;
352 /* Read the list of input files from LIST and compute all of the
353 relevant tables. There is one file per line of the list. At
354 first, all the files on the list are language-generic, but
355 eventually a line will appear which is the name of a language
356 subdirectory in square brackets, like this: [cp]. All subsequent
357 files are specific to that language, until another language
358 subdirectory tag appears. Files can appear more than once, if
359 they apply to more than one language. */
360 static void
361 read_input_list (const char *listname)
363 FILE *list = fopen (listname, "r");
364 if (!list)
365 fatal ("cannot open %s: %s", listname, xstrerror (errno));
366 else
368 struct fileloc epos;
369 size_t bufsz = measure_input_list (list);
370 char *buf = XNEWVEC (char, bufsz);
371 char *here = buf;
372 char *committed = buf;
373 char *limit = buf + bufsz;
374 char *line;
375 bool is_language;
376 size_t langno = 0;
377 size_t nfiles = 0;
378 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
380 epos.file = input_file_by_name (listname);
381 epos.line = 0;
383 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
384 gt_files = XNEWVEC (const input_file *, num_gt_files);
386 for (;;)
388 next_line:
389 epos.line++;
390 committed = here;
391 is_language = read_input_line (list, &here, &line, &epos);
392 gcc_assert (here <= limit);
393 if (line == 0)
394 break;
395 else if (is_language)
397 size_t i;
398 gcc_assert (langno <= num_lang_dirs);
399 for (i = 0; i < langno; i++)
400 if (strcmp (lang_dir_names[i], line) == 0)
402 error_at_line (&epos, "duplicate language tag [%s]",
403 line);
404 curlangs = 1 << i;
405 here = committed;
406 goto next_line;
409 curlangs = 1 << langno;
410 lang_dir_names[langno++] = line;
412 else
414 size_t i;
415 input_file *inpf = input_file_by_name (line);
416 gcc_assert (nfiles <= num_gt_files);
417 for (i = 0; i < nfiles; i++)
418 /* Since the input_file-s are uniquely hash-consed, we
419 can just compare pointers! */
420 if (gt_files[i] == inpf)
422 /* Throw away the string we just read, and add the
423 current language to the existing string's bitmap. */
424 lang_bitmap bmap = get_lang_bitmap (inpf);
425 if (bmap & curlangs)
426 error_at_line (&epos,
427 "file %s specified more than once "
428 "for language %s", line,
429 langno ==
430 0 ? "(all)" : lang_dir_names[langno -
431 1]);
433 bmap |= curlangs;
434 set_lang_bitmap (inpf, bmap);
435 here = committed;
436 goto next_line;
439 set_lang_bitmap (inpf, curlangs);
440 gt_files[nfiles++] = inpf;
443 /* Update the global counts now that we know accurately how many
444 things there are. (We do not bother resizing the arrays down.) */
445 num_lang_dirs = langno;
446 /* Add the plugin files if provided. */
447 if (plugin_files)
449 size_t i;
450 for (i = 0; i < nb_plugin_files; i++)
451 gt_files[nfiles++] = plugin_files[i];
453 num_gt_files = nfiles;
456 /* Sanity check: any file that resides in a language subdirectory
457 (e.g. 'cp') ought to belong to the corresponding language.
458 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
459 (Can you even do that? Should you be allowed to?) */
461 size_t f;
462 for (f = 0; f < num_gt_files; f++)
464 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
465 const char *basename = get_file_basename (gt_files[f]);
466 const char *slashpos = strchr (basename, '/');
467 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
468 const char *slashpos2 = strchr (basename, '\\');
470 if (!slashpos || (slashpos2 && slashpos2 < slashpos))
471 slashpos = slashpos2;
472 #endif
474 if (slashpos)
476 size_t l;
477 for (l = 0; l < num_lang_dirs; l++)
478 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
479 && memcmp (basename, lang_dir_names[l],
480 strlen (lang_dir_names[l])) == 0)
482 if (!(bitmap & (1 << l)))
483 error ("%s is in language directory '%s' but is not "
484 "tagged for that language",
485 basename, lang_dir_names[l]);
486 break;
492 if (ferror (list))
493 fatal ("error reading %s: %s", listname, xstrerror (errno));
495 fclose (list);
500 /* The one and only TYPE_STRING. */
502 struct type string_type = {
503 TYPE_STRING, 0, 0, 0, GC_USED, {0}
506 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
507 set early in main. */
509 struct type scalar_nonchar = {
510 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
513 struct type scalar_char = {
514 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
517 /* Lists of various things. */
519 pair_p typedefs = NULL;
520 type_p structures = NULL;
521 type_p param_structs = NULL;
522 pair_p variables = NULL;
524 static type_p find_param_structure (type_p t, type_p param[NUM_PARAM]);
525 static type_p adjust_field_tree_exp (type_p t, options_p opt);
526 static type_p adjust_field_rtx_def (type_p t, options_p opt);
528 /* Define S as a typedef to T at POS. */
530 void
531 do_typedef (const char *s, type_p t, struct fileloc *pos)
533 pair_p p;
535 /* temporary kludge - gengtype doesn't handle conditionals or
536 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
537 is coming from this file (main() sets them up with safe dummy
538 definitions). */
539 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
540 return;
542 for (p = typedefs; p != NULL; p = p->next)
543 if (strcmp (p->name, s) == 0)
545 if (p->type != t && strcmp (s, "result_type") != 0)
547 error_at_line (pos, "type `%s' previously defined", s);
548 error_at_line (&p->line, "previously defined here");
550 return;
553 p = XNEW (struct pair);
554 p->next = typedefs;
555 p->name = s;
556 p->type = t;
557 p->line = *pos;
558 p->opt = NULL;
559 typedefs = p;
562 /* Define S as a typename of a scalar. Cannot be used to define
563 typedefs of 'char'. Note: is also used for pointer-to-function
564 typedefs (which are therefore not treated as pointers). */
566 void
567 do_scalar_typedef (const char *s, struct fileloc *pos)
569 do_typedef (s, &scalar_nonchar, pos);
572 /* Similar to strtok_r. */
574 static char *
575 strtoken (char *str, const char *delim, char **next)
577 char *p;
579 if (str == NULL)
580 str = *next;
582 /* Skip the leading delimiters. */
583 str += strspn (str, delim);
584 if (*str == '\0')
585 /* This is an empty token. */
586 return NULL;
588 /* The current token. */
589 p = str;
591 /* Find the next delimiter. */
592 str += strcspn (str, delim);
593 if (*str == '\0')
594 /* This is the last token. */
595 *next = str;
596 else
598 /* Terminate the current token. */
599 *str = '\0';
600 /* Advance to the next token. */
601 *next = str + 1;
604 return p;
607 /* Define TYPE_NAME to be a user defined type at location POS. */
609 type_p
610 create_user_defined_type (const char *type_name, struct fileloc *pos)
612 type_p ty = find_structure (type_name, TYPE_USER_STRUCT);
614 /* We might have already seen an incomplete decl of the given type,
615 in which case we won't have yet seen a GTY((user)), and the type will
616 only have kind "TYPE_STRUCT". Mark it as a user struct. */
617 ty->kind = TYPE_USER_STRUCT;
619 ty->u.s.line = *pos;
620 ty->u.s.bitmap = get_lang_bitmap (pos->file);
621 do_typedef (type_name, ty, pos);
623 /* If TYPE_NAME specifies a template, create references to the types
624 in the template by pretending that each type is a field of TY.
625 This is needed to make sure that the types referenced by the
626 template are marked as used. */
627 char *str = xstrdup (type_name);
628 char *open_bracket = strchr (str, '<');
629 if (open_bracket)
631 /* We only accept simple template declarations (see
632 require_template_declaration), so we only need to parse a
633 comma-separated list of strings, implicitly assumed to
634 be type names, potentially with "*" characters. */
635 char *arg = open_bracket + 1;
636 char *next;
637 char *type_id = strtoken (arg, ",>", &next);
638 pair_p fields = 0;
639 while (type_id)
641 /* Create a new field for every type found inside the template
642 parameter list. */
644 /* Support a single trailing "*" character. */
645 const char *star = strchr (type_id, '*');
646 int is_ptr = (star != NULL);
647 size_t offset_to_star = star - type_id;
648 if (is_ptr)
649 offset_to_star = star - type_id;
651 if (strstr (type_id, "char*"))
653 type_id = strtoken (0, ",>", &next);
654 continue;
657 char *field_name = xstrdup (type_id);
659 type_p arg_type;
660 if (is_ptr)
662 /* Strip off the first '*' character (and any subsequent text). */
663 *(field_name + offset_to_star) = '\0';
665 arg_type = find_structure (field_name, TYPE_STRUCT);
666 arg_type = create_pointer (arg_type);
668 else
669 arg_type = resolve_typedef (field_name, pos);
671 fields = create_field_at (fields, arg_type, field_name, 0, pos);
672 type_id = strtoken (0, ",>", &next);
675 /* Associate the field list to TY. */
676 ty->u.s.fields = fields;
678 free (str);
680 return ty;
684 /* Given a typedef name S, return its associated type. Return NULL if
685 S is not a registered type name. */
687 static type_p
688 type_for_name (const char *s)
690 pair_p p;
692 /* Special-case support for types within a "gcc::" namespace. Rather
693 than fully-supporting namespaces, simply strip off the "gcc::" prefix
694 where present. This allows us to have GTY roots of this form:
695 extern GTY(()) gcc::some_type *some_ptr;
696 where the autogenerated functions will refer to simply "some_type",
697 where they can be resolved into their namespace. */
698 if (0 == strncmp (s, "gcc::", 5))
699 s += 5;
701 for (p = typedefs; p != NULL; p = p->next)
702 if (strcmp (p->name, s) == 0)
703 return p->type;
704 return NULL;
708 /* Create an undefined type with name S and location POS. Return the
709 newly created type. */
711 static type_p
712 create_undefined_type (const char *s, struct fileloc *pos)
714 type_p ty = find_structure (s, TYPE_UNDEFINED);
715 ty->u.s.line = *pos;
716 ty->u.s.bitmap = get_lang_bitmap (pos->file);
717 do_typedef (s, ty, pos);
718 return ty;
722 /* Return the type previously defined for S. Use POS to report errors. */
724 type_p
725 resolve_typedef (const char *s, struct fileloc *pos)
727 bool is_template_instance = (strchr (s, '<') != NULL);
728 type_p p = type_for_name (s);
730 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
731 type for regular type identifiers. If the type identifier S is a
732 template instantiation, however, we treat it as a user defined
733 type.
735 FIXME, this is actually a limitation in gengtype. Supporting
736 template types and their instances would require keeping separate
737 track of the basic types definition and its instances. This
738 essentially forces all template classes in GC to be marked
739 GTY((user)). */
740 if (!p)
741 p = (is_template_instance)
742 ? create_user_defined_type (s, pos)
743 : create_undefined_type (s, pos);
745 return p;
748 /* Add SUBCLASS to head of linked list of BASE's subclasses. */
750 void add_subclass (type_p base, type_p subclass)
752 gcc_assert (union_or_struct_p (base));
753 gcc_assert (union_or_struct_p (subclass));
755 subclass->u.s.next_sibling_class = base->u.s.first_subclass;
756 base->u.s.first_subclass = subclass;
759 /* Create and return a new structure with tag NAME at POS with fields
760 FIELDS and options O. The KIND of structure must be one of
761 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
763 type_p
764 new_structure (const char *name, enum typekind kind, struct fileloc *pos,
765 pair_p fields, options_p o, type_p base_class)
767 type_p si;
768 type_p s = NULL;
769 lang_bitmap bitmap = get_lang_bitmap (pos->file);
770 bool isunion = (kind == TYPE_UNION);
772 gcc_assert (union_or_struct_p (kind));
774 for (si = structures; si != NULL; si = si->next)
775 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
777 type_p ls = NULL;
778 if (si->kind == TYPE_LANG_STRUCT)
780 ls = si;
782 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
783 if (si->u.s.bitmap == bitmap)
784 s = si;
786 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
788 ls = si;
789 type_count++;
790 si = XCNEW (struct type);
791 memcpy (si, ls, sizeof (struct type));
792 ls->kind = TYPE_LANG_STRUCT;
793 ls->u.s.lang_struct = si;
794 ls->u.s.fields = NULL;
795 si->next = NULL;
796 si->state_number = -type_count;
797 si->pointer_to = NULL;
798 si->u.s.lang_struct = ls;
800 else
801 s = si;
803 if (ls != NULL && s == NULL)
805 type_count++;
806 s = XCNEW (struct type);
807 s->state_number = -type_count;
808 s->next = ls->u.s.lang_struct;
809 ls->u.s.lang_struct = s;
810 s->u.s.lang_struct = ls;
812 break;
815 if (s == NULL)
817 type_count++;
818 s = XCNEW (struct type);
819 s->state_number = -type_count;
820 s->next = structures;
821 structures = s;
824 if (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap))
826 error_at_line (pos, "duplicate definition of '%s %s'",
827 isunion ? "union" : "struct", s->u.s.tag);
828 error_at_line (&s->u.s.line, "previous definition here");
831 s->kind = kind;
832 s->u.s.tag = name;
833 s->u.s.line = *pos;
834 s->u.s.fields = fields;
835 s->u.s.opt = o;
836 s->u.s.bitmap = bitmap;
837 if (s->u.s.lang_struct)
838 s->u.s.lang_struct->u.s.bitmap |= bitmap;
839 s->u.s.base_class = base_class;
840 if (base_class)
841 add_subclass (base_class, s);
843 return s;
846 /* Return the previously-defined structure or union with tag NAME,
847 or a new empty structure or union if none was defined previously.
848 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
849 TYPE_USER_STRUCT. */
851 type_p
852 find_structure (const char *name, enum typekind kind)
854 type_p s;
855 bool isunion = (kind == TYPE_UNION);
857 gcc_assert (kind == TYPE_UNDEFINED || union_or_struct_p (kind));
859 for (s = structures; s != NULL; s = s->next)
860 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
861 return s;
863 type_count++;
864 s = XCNEW (struct type);
865 s->next = structures;
866 s->state_number = -type_count;
867 structures = s;
868 s->kind = kind;
869 s->u.s.tag = name;
870 structures = s;
871 return s;
874 /* Return the previously-defined parameterized structure for structure
875 T and parameters PARAM, or a new parameterized empty structure or
876 union if none was defined previously. */
878 static type_p
879 find_param_structure (type_p t, type_p param[NUM_PARAM])
881 type_p res;
883 for (res = param_structs; res; res = res->next)
884 if (res->u.param_struct.stru == t
885 && memcmp (res->u.param_struct.param, param,
886 sizeof (type_p) * NUM_PARAM) == 0)
887 break;
888 if (res == NULL)
890 type_count++;
891 res = XCNEW (struct type);
892 res->kind = TYPE_PARAM_STRUCT;
893 res->next = param_structs;
894 res->state_number = -type_count;
895 param_structs = res;
896 res->u.param_struct.stru = t;
897 memcpy (res->u.param_struct.param, param, sizeof (type_p) * NUM_PARAM);
899 return res;
902 /* Return a scalar type with name NAME. */
904 type_p
905 create_scalar_type (const char *name)
907 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
908 return &scalar_char;
909 else
910 return &scalar_nonchar;
914 /* Return a pointer to T. */
916 type_p
917 create_pointer (type_p t)
919 if (!t->pointer_to)
921 type_p r = XCNEW (struct type);
922 type_count++;
923 r->state_number = -type_count;
924 r->kind = TYPE_POINTER;
925 r->u.p = t;
926 t->pointer_to = r;
928 return t->pointer_to;
931 /* Return an array of length LEN. */
933 type_p
934 create_array (type_p t, const char *len)
936 type_p v;
938 type_count++;
939 v = XCNEW (struct type);
940 v->kind = TYPE_ARRAY;
941 v->state_number = -type_count;
942 v->u.a.p = t;
943 v->u.a.len = len;
944 return v;
947 /* Return a string options structure with name NAME and info INFO.
948 NEXT is the next option in the chain. */
949 options_p
950 create_string_option (options_p next, const char *name, const char *info)
952 options_p o = XNEW (struct options);
953 o->kind = OPTION_STRING;
954 o->next = next;
955 o->name = name;
956 o->info.string = info;
957 return o;
960 /* Create a type options structure with name NAME and info INFO. NEXT
961 is the next option in the chain. */
962 options_p
963 create_type_option (options_p next, const char* name, type_p info)
965 options_p o = XNEW (struct options);
966 o->next = next;
967 o->name = name;
968 o->kind = OPTION_TYPE;
969 o->info.type = info;
970 return o;
973 /* Create a nested pointer options structure with name NAME and info
974 INFO. NEXT is the next option in the chain. */
975 options_p
976 create_nested_option (options_p next, const char* name,
977 struct nested_ptr_data* info)
979 options_p o;
980 o = XNEW (struct options);
981 o->next = next;
982 o->name = name;
983 o->kind = OPTION_NESTED;
984 o->info.nested = info;
985 return o;
988 /* Return an options structure for a "nested_ptr" option. */
989 options_p
990 create_nested_ptr_option (options_p next, type_p t,
991 const char *to, const char *from)
993 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
995 d->type = adjust_field_type (t, 0);
996 d->convert_to = to;
997 d->convert_from = from;
998 return create_nested_option (next, "nested_ptr", d);
1001 /* Add a variable named S of type T with options O defined at POS,
1002 to `variables'. */
1003 void
1004 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
1006 pair_p n;
1007 n = XNEW (struct pair);
1008 n->name = s;
1009 n->type = t;
1010 n->line = *pos;
1011 n->opt = o;
1012 n->next = variables;
1013 variables = n;
1016 /* Most-general structure field creator. */
1017 static pair_p
1018 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
1019 const input_file *inpf, int line)
1021 pair_p field;
1023 field = XNEW (struct pair);
1024 field->next = next;
1025 field->type = type;
1026 field->name = name;
1027 field->opt = opt;
1028 field->line.file = inpf;
1029 field->line.line = line;
1030 return field;
1033 /* Create a field that came from the source code we are scanning,
1034 i.e. we have a 'struct fileloc', and possibly options; also,
1035 adjust_field_type should be called. */
1036 pair_p
1037 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
1038 struct fileloc *pos)
1040 return create_field_all (next, adjust_field_type (type, opt),
1041 name, opt, pos->file, pos->line);
1044 /* Create a fake field with the given type and name. NEXT is the next
1045 field in the chain. */
1046 #define create_field(next,type,name) \
1047 create_field_all (next,type,name, 0, this_file, __LINE__)
1049 /* Like create_field, but the field is only valid when condition COND
1050 is true. */
1052 static pair_p
1053 create_optional_field_ (pair_p next, type_p type, const char *name,
1054 const char *cond, int line)
1056 static int id = 1;
1057 pair_p union_fields;
1058 type_p union_type;
1060 /* Create a fake union type with a single nameless field of type TYPE.
1061 The field has a tag of "1". This allows us to make the presence
1062 of a field of type TYPE depend on some boolean "desc" being true. */
1063 union_fields = create_field (NULL, type, "");
1064 union_fields->opt =
1065 create_string_option (union_fields->opt, "dot", "");
1066 union_fields->opt =
1067 create_string_option (union_fields->opt, "tag", "1");
1068 union_type =
1069 new_structure (xasprintf ("%s_%d", "fake_union", id++), TYPE_UNION,
1070 &lexer_line, union_fields, NULL, NULL);
1072 /* Create the field and give it the new fake union type. Add a "desc"
1073 tag that specifies the condition under which the field is valid. */
1074 return create_field_all (next, union_type, name,
1075 create_string_option (0, "desc", cond),
1076 this_file, line);
1079 #define create_optional_field(next,type,name,cond) \
1080 create_optional_field_(next,type,name,cond,__LINE__)
1082 /* Reverse a linked list of 'struct pair's in place. */
1083 pair_p
1084 nreverse_pairs (pair_p list)
1086 pair_p prev = 0, p, next;
1087 for (p = list; p; p = next)
1089 next = p->next;
1090 p->next = prev;
1091 prev = p;
1093 return prev;
1097 /* We don't care how long a CONST_DOUBLE is. */
1098 #define CONST_DOUBLE_FORMAT "ww"
1099 /* We don't want to see codes that are only for generator files. */
1100 #undef GENERATOR_FILE
1102 enum rtx_code
1104 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1105 #include "rtl.def"
1106 #undef DEF_RTL_EXPR
1107 NUM_RTX_CODE
1110 static const char *const rtx_name[NUM_RTX_CODE] = {
1111 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1112 #include "rtl.def"
1113 #undef DEF_RTL_EXPR
1116 static const char *const rtx_format[NUM_RTX_CODE] = {
1117 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1118 #include "rtl.def"
1119 #undef DEF_RTL_EXPR
1122 static int rtx_next_new[NUM_RTX_CODE];
1124 /* We also need codes and names for insn notes (not register notes).
1125 Note that we do *not* bias the note values here. */
1126 enum insn_note
1128 #define DEF_INSN_NOTE(NAME) NAME,
1129 #include "insn-notes.def"
1130 #undef DEF_INSN_NOTE
1132 NOTE_INSN_MAX
1135 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1136 default field for line number notes. */
1137 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
1138 #define DEF_INSN_NOTE(NAME) #NAME,
1139 #include "insn-notes.def"
1140 #undef DEF_INSN_NOTE
1143 #undef CONST_DOUBLE_FORMAT
1144 #define GENERATOR_FILE
1146 /* Generate the contents of the rtx_next array. This really doesn't belong
1147 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1149 static void
1150 gen_rtx_next (void)
1152 int i;
1153 for (i = 0; i < NUM_RTX_CODE; i++)
1155 int k;
1157 rtx_next_new[i] = -1;
1158 if (strncmp (rtx_format[i], "uu", 2) == 0)
1159 rtx_next_new[i] = 1;
1160 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1161 rtx_next_new[i] = 1;
1162 else
1163 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
1164 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1165 rtx_next_new[i] = k;
1169 /* Write out the contents of the rtx_next array. */
1170 static void
1171 write_rtx_next (void)
1173 outf_p f = get_output_file_with_visibility (NULL);
1174 int i;
1175 if (!f)
1176 return;
1178 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
1179 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1180 for (i = 0; i < NUM_RTX_CODE; i++)
1181 if (rtx_next_new[i] == -1)
1182 oprintf (f, " 0,\n");
1183 else
1184 oprintf (f,
1185 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1186 oprintf (f, "};\n");
1189 /* Handle `special("rtx_def")'. This is a special case for field
1190 `fld' of struct rtx_def, which is an array of unions whose values
1191 are based in a complex way on the type of RTL. */
1193 static type_p
1194 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1196 pair_p flds = NULL;
1197 options_p nodot;
1198 int i;
1199 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1200 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1202 if (t->kind != TYPE_UNION)
1204 error_at_line (&lexer_line,
1205 "special `rtx_def' must be applied to a union");
1206 return &string_type;
1209 nodot = create_string_option (NULL, "dot", "");
1211 rtx_tp = create_pointer (find_structure ("rtx_def", TYPE_STRUCT));
1212 rtvec_tp = create_pointer (find_structure ("rtvec_def", TYPE_STRUCT));
1213 tree_tp = create_pointer (find_structure ("tree_node", TYPE_UNION));
1214 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", TYPE_STRUCT));
1215 reg_attrs_tp =
1216 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT));
1217 basic_block_tp =
1218 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT));
1219 constant_tp =
1220 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT));
1221 scalar_tp = &scalar_nonchar; /* rtunion int */
1224 pair_p note_flds = NULL;
1225 int c;
1227 for (c = 0; c <= NOTE_INSN_MAX; c++)
1229 switch (c)
1231 case NOTE_INSN_MAX:
1232 case NOTE_INSN_DELETED_LABEL:
1233 case NOTE_INSN_DELETED_DEBUG_LABEL:
1234 note_flds = create_field (note_flds, &string_type, "rt_str");
1235 break;
1237 case NOTE_INSN_BLOCK_BEG:
1238 case NOTE_INSN_BLOCK_END:
1239 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1240 break;
1242 case NOTE_INSN_VAR_LOCATION:
1243 case NOTE_INSN_CALL_ARG_LOCATION:
1244 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1245 break;
1247 default:
1248 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1249 break;
1251 /* NOTE_INSN_MAX is used as the default field for line
1252 number notes. */
1253 if (c == NOTE_INSN_MAX)
1254 note_flds->opt =
1255 create_string_option (nodot, "default", "");
1256 else
1257 note_flds->opt =
1258 create_string_option (nodot, "tag", note_insn_name[c]);
1260 note_union_tp = new_structure ("rtx_def_note_subunion", TYPE_UNION,
1261 &lexer_line, note_flds, NULL, NULL);
1263 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1265 pair_p sym_flds;
1266 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1267 sym_flds->opt = create_string_option (nodot, "default", "");
1268 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1269 sym_flds->opt = create_string_option (nodot, "tag", "1");
1270 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", TYPE_UNION,
1271 &lexer_line, sym_flds, NULL, NULL);
1273 for (i = 0; i < NUM_RTX_CODE; i++)
1275 pair_p subfields = NULL;
1276 size_t aindex, nmindex;
1277 const char *sname;
1278 type_p substruct;
1279 char *ftag;
1281 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1283 type_p t;
1284 const char *subname;
1286 switch (rtx_format[i][aindex])
1288 case '*':
1289 case 'i':
1290 case 'n':
1291 case 'w':
1292 t = scalar_tp;
1293 subname = "rt_int";
1294 break;
1296 case '0':
1297 if (i == MEM && aindex == 1)
1298 t = mem_attrs_tp, subname = "rt_mem";
1299 else if (i == JUMP_INSN && aindex == 7)
1300 t = rtx_tp, subname = "rt_rtx";
1301 else if (i == CODE_LABEL && aindex == 4)
1302 t = scalar_tp, subname = "rt_int";
1303 else if (i == CODE_LABEL && aindex == 3)
1304 t = rtx_tp, subname = "rt_rtx";
1305 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1306 t = rtx_tp, subname = "rt_rtx";
1307 else if (i == NOTE && aindex == 3)
1308 t = note_union_tp, subname = "";
1309 else if (i == NOTE && aindex == 4)
1310 t = scalar_tp, subname = "rt_int";
1311 else if (i == NOTE && aindex >= 6)
1312 t = scalar_tp, subname = "rt_int";
1313 else if (i == ADDR_DIFF_VEC && aindex == 4)
1314 t = scalar_tp, subname = "rt_int";
1315 else if (i == VALUE && aindex == 0)
1316 t = scalar_tp, subname = "rt_int";
1317 else if (i == DEBUG_EXPR && aindex == 0)
1318 t = tree_tp, subname = "rt_tree";
1319 else if (i == REG && aindex == 1)
1320 t = reg_attrs_tp, subname = "rt_reg";
1321 else if (i == SYMBOL_REF && aindex == 1)
1322 t = symbol_union_tp, subname = "";
1323 else if (i == JUMP_TABLE_DATA && aindex >= 4)
1324 t = scalar_tp, subname = "rt_int";
1325 else if (i == BARRIER && aindex >= 2)
1326 t = scalar_tp, subname = "rt_int";
1327 else if (i == ENTRY_VALUE && aindex == 0)
1328 t = rtx_tp, subname = "rt_rtx";
1329 else
1331 error_at_line
1332 (&lexer_line,
1333 "rtx type `%s' has `0' in position %lu, can't handle",
1334 rtx_name[i], (unsigned long) aindex);
1335 t = &string_type;
1336 subname = "rt_int";
1338 break;
1340 case 's':
1341 case 'S':
1342 case 'T':
1343 t = &string_type;
1344 subname = "rt_str";
1345 break;
1347 case 'e':
1348 case 'u':
1349 t = rtx_tp;
1350 subname = "rt_rtx";
1351 break;
1353 case 'E':
1354 case 'V':
1355 t = rtvec_tp;
1356 subname = "rt_rtvec";
1357 break;
1359 case 't':
1360 t = tree_tp;
1361 subname = "rt_tree";
1362 break;
1364 case 'B':
1365 t = basic_block_tp;
1366 subname = "rt_bb";
1367 break;
1369 default:
1370 error_at_line
1371 (&lexer_line,
1372 "rtx type `%s' has `%c' in position %lu, can't handle",
1373 rtx_name[i], rtx_format[i][aindex],
1374 (unsigned long) aindex);
1375 t = &string_type;
1376 subname = "rt_int";
1377 break;
1380 subfields = create_field (subfields, t,
1381 xasprintf (".fld[%lu].%s",
1382 (unsigned long) aindex,
1383 subname));
1384 subfields->opt = nodot;
1385 if (t == note_union_tp)
1386 subfields->opt =
1387 create_string_option (subfields->opt, "desc",
1388 "NOTE_KIND (&%0)");
1389 if (t == symbol_union_tp)
1390 subfields->opt =
1391 create_string_option (subfields->opt, "desc",
1392 "CONSTANT_POOL_ADDRESS_P (&%0)");
1395 if (i == SYMBOL_REF)
1397 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1398 holds. */
1399 type_p field_tp = find_structure ("block_symbol", TYPE_STRUCT);
1400 subfields
1401 = create_optional_field (subfields, field_tp, "block_sym",
1402 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1405 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1406 substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
1407 NULL, NULL);
1409 ftag = xstrdup (rtx_name[i]);
1410 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1411 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1412 flds = create_field (flds, substruct, "");
1413 flds->opt = create_string_option (nodot, "tag", ftag);
1415 return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
1416 nodot, NULL);
1419 /* Handle `special("tree_exp")'. This is a special case for
1420 field `operands' of struct tree_exp, which although it claims to contain
1421 pointers to trees, actually sometimes contains pointers to RTL too.
1422 Passed T, the old type of the field, and OPT its options. Returns
1423 a new type for the field. */
1425 static type_p
1426 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1428 pair_p flds;
1429 options_p nodot;
1431 if (t->kind != TYPE_ARRAY)
1433 error_at_line (&lexer_line,
1434 "special `tree_exp' must be applied to an array");
1435 return &string_type;
1438 nodot = create_string_option (NULL, "dot", "");
1440 flds = create_field (NULL, t, "");
1441 flds->opt = create_string_option (nodot, "length",
1442 "TREE_OPERAND_LENGTH ((tree) &%0)");
1443 flds->opt = create_string_option (flds->opt, "default", "");
1445 return new_structure ("tree_exp_subunion", TYPE_UNION, &lexer_line, flds,
1446 nodot, NULL);
1449 /* Perform any special processing on a type T, about to become the type
1450 of a field. Return the appropriate type for the field.
1451 At present:
1452 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1453 - Similarly for arrays of pointer-to-char;
1454 - Converts structures for which a parameter is provided to
1455 TYPE_PARAM_STRUCT;
1456 - Handles "special" options.
1459 type_p
1460 adjust_field_type (type_p t, options_p opt)
1462 int length_p = 0;
1463 const int pointer_p = t->kind == TYPE_POINTER;
1464 type_p params[NUM_PARAM];
1465 int params_p = 0;
1466 int i;
1468 for (i = 0; i < NUM_PARAM; i++)
1469 params[i] = NULL;
1471 for (; opt; opt = opt->next)
1472 if (strcmp (opt->name, "length") == 0)
1474 if (length_p)
1475 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1476 if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
1478 error_at_line (&lexer_line,
1479 "option `%s' may not be applied to "
1480 "arrays of atomic types", opt->name);
1482 length_p = 1;
1484 else if ((strcmp (opt->name, "param_is") == 0
1485 || (strncmp (opt->name, "param", 5) == 0
1486 && ISDIGIT (opt->name[5])
1487 && strcmp (opt->name + 6, "_is") == 0))
1488 && opt->kind == OPTION_TYPE)
1490 int num = ISDIGIT (opt->name[5]) ? opt->name[5] - '0' : 0;
1492 if (!union_or_struct_p (t)
1493 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
1495 error_at_line (&lexer_line,
1496 "option `%s' may only be applied to structures or structure pointers",
1497 opt->name);
1498 return t;
1501 params_p = 1;
1502 if (params[num] != NULL)
1503 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1504 if (!ISDIGIT (opt->name[5]))
1505 params[num] = create_pointer (opt->info.type);
1506 else
1507 params[num] = opt->info.type;
1509 else if (strcmp (opt->name, "special") == 0
1510 && opt->kind == OPTION_STRING)
1512 const char *special_name = opt->info.string;
1513 if (strcmp (special_name, "tree_exp") == 0)
1514 t = adjust_field_tree_exp (t, opt);
1515 else if (strcmp (special_name, "rtx_def") == 0)
1516 t = adjust_field_rtx_def (t, opt);
1517 else
1518 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1521 if (params_p)
1523 type_p realt;
1525 if (pointer_p)
1526 t = t->u.p;
1527 realt = find_param_structure (t, params);
1528 t = pointer_p ? create_pointer (realt) : realt;
1531 if (!length_p
1532 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1533 return &string_type;
1534 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1535 && t->u.a.p->u.p->kind == TYPE_SCALAR
1536 && t->u.a.p->u.p->u.scalar_is_char)
1537 return create_array (&string_type, t->u.a.len);
1539 return t;
1543 static void set_gc_used_type (type_p, enum gc_used_enum, type_p *,
1544 bool = false);
1545 static void set_gc_used (pair_p);
1547 /* Handle OPT for set_gc_used_type. */
1549 static void
1550 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1551 int *pass_param, int *length, int *skip,
1552 type_p *nested_ptr)
1554 options_p o;
1555 for (o = opt; o; o = o->next)
1556 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1557 && o->kind == OPTION_TYPE)
1558 set_gc_used_type (o->info.type,
1559 GC_POINTED_TO, NULL);
1560 else if (strcmp (o->name, "maybe_undef") == 0)
1561 *maybe_undef = 1;
1562 else if (strcmp (o->name, "use_params") == 0)
1563 *pass_param = 1;
1564 else if (strcmp (o->name, "length") == 0)
1565 *length = 1;
1566 else if (strcmp (o->name, "skip") == 0)
1567 *skip = 1;
1568 else if (strcmp (o->name, "nested_ptr") == 0
1569 && o->kind == OPTION_NESTED)
1570 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1574 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1576 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1577 are set to GC_UNUSED. Otherwise, an error is emitted for
1578 TYPE_UNDEFINED types. This is used to support user-defined
1579 template types with non-type arguments.
1581 For instance, when we parse a template type with enum arguments
1582 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1583 artificial fields for 'MyType', one for 'AnotherType', the other
1584 one for 'EnumValue'.
1586 At the time that we parse this type we don't know that 'EnumValue'
1587 is really an enum value, so the parser creates a TYPE_UNDEFINED
1588 type for it. Since 'EnumValue' is never resolved to a known
1589 structure, it will stay with TYPE_UNDEFINED.
1591 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1592 'EnumValue'. Generating marking code for it would cause
1593 compilation failures since the marking routines assumes that
1594 'EnumValue' is a type. */
1596 static void
1597 set_gc_used_type (type_p t, enum gc_used_enum level, type_p param[NUM_PARAM],
1598 bool allow_undefined_types)
1600 if (t->gc_used >= level)
1601 return;
1603 t->gc_used = level;
1605 switch (t->kind)
1607 case TYPE_STRUCT:
1608 case TYPE_UNION:
1609 case TYPE_USER_STRUCT:
1611 pair_p f;
1612 int dummy;
1613 type_p dummy2;
1614 bool allow_undefined_field_types = (t->kind == TYPE_USER_STRUCT);
1616 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
1617 &dummy2);
1619 if (t->u.s.base_class)
1620 set_gc_used_type (t->u.s.base_class, level, param,
1621 allow_undefined_types);
1622 /* Anything pointing to a base class might actually be pointing
1623 to a subclass. */
1624 for (type_p subclass = t->u.s.first_subclass; subclass;
1625 subclass = subclass->u.s.next_sibling_class)
1626 set_gc_used_type (subclass, level, param,
1627 allow_undefined_types);
1629 FOR_ALL_INHERITED_FIELDS(t, f)
1631 int maybe_undef = 0;
1632 int pass_param = 0;
1633 int length = 0;
1634 int skip = 0;
1635 type_p nested_ptr = NULL;
1636 process_gc_options (f->opt, level, &maybe_undef, &pass_param,
1637 &length, &skip, &nested_ptr);
1639 if (nested_ptr && f->type->kind == TYPE_POINTER)
1640 set_gc_used_type (nested_ptr, GC_POINTED_TO,
1641 pass_param ? param : NULL);
1642 else if (length && f->type->kind == TYPE_POINTER)
1643 set_gc_used_type (f->type->u.p, GC_USED, NULL);
1644 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1645 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO, NULL);
1646 else if (pass_param && f->type->kind == TYPE_POINTER && param)
1647 set_gc_used_type (find_param_structure (f->type->u.p, param),
1648 GC_POINTED_TO, NULL);
1649 else if (skip)
1650 ; /* target type is not used through this field */
1651 else
1652 set_gc_used_type (f->type, GC_USED, pass_param ? param : NULL,
1653 allow_undefined_field_types);
1655 break;
1658 case TYPE_UNDEFINED:
1659 if (level > GC_UNUSED)
1661 if (!allow_undefined_types)
1662 error_at_line (&t->u.s.line, "undefined type `%s'", t->u.s.tag);
1663 t->gc_used = GC_UNUSED;
1665 break;
1667 case TYPE_POINTER:
1668 set_gc_used_type (t->u.p, GC_POINTED_TO, NULL);
1669 break;
1671 case TYPE_ARRAY:
1672 set_gc_used_type (t->u.a.p, GC_USED, param);
1673 break;
1675 case TYPE_LANG_STRUCT:
1676 for (t = t->u.s.lang_struct; t; t = t->next)
1677 set_gc_used_type (t, level, param);
1678 break;
1680 case TYPE_PARAM_STRUCT:
1682 int i;
1683 for (i = 0; i < NUM_PARAM; i++)
1684 if (t->u.param_struct.param[i] != 0)
1685 set_gc_used_type (t->u.param_struct.param[i], GC_USED, NULL);
1687 if (t->u.param_struct.stru->gc_used == GC_POINTED_TO)
1688 level = GC_POINTED_TO;
1689 else
1690 level = GC_USED;
1691 t->u.param_struct.stru->gc_used = GC_UNUSED;
1692 set_gc_used_type (t->u.param_struct.stru, level,
1693 t->u.param_struct.param);
1694 break;
1696 default:
1697 break;
1701 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1703 static void
1704 set_gc_used (pair_p variables)
1706 int nbvars = 0;
1707 pair_p p;
1708 for (p = variables; p; p = p->next)
1710 set_gc_used_type (p->type, GC_USED, NULL);
1711 nbvars++;
1713 if (verbosity_level >= 2)
1714 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1717 /* File mapping routines. For each input file, there is one output .c file
1718 (but some output files have many input files), and there is one .h file
1719 for the whole build. */
1721 /* Output file handling. */
1723 /* Create and return an outf_p for a new file for NAME, to be called
1724 ONAME. */
1726 static outf_p
1727 create_file (const char *name, const char *oname)
1729 static const char *const hdr[] = {
1730 " Copyright (C) 2004-2014 Free Software Foundation, Inc.\n",
1731 "\n",
1732 "This file is part of GCC.\n",
1733 "\n",
1734 "GCC is free software; you can redistribute it and/or modify it under\n",
1735 "the terms of the GNU General Public License as published by the Free\n",
1736 "Software Foundation; either version 3, or (at your option) any later\n",
1737 "version.\n",
1738 "\n",
1739 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1740 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1741 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1742 "for more details.\n",
1743 "\n",
1744 "You should have received a copy of the GNU General Public License\n",
1745 "along with GCC; see the file COPYING3. If not see\n",
1746 "<http://www.gnu.org/licenses/>. */\n",
1747 "\n",
1748 "/* This file is machine generated. Do not edit. */\n"
1750 outf_p f;
1751 size_t i;
1753 gcc_assert (name != NULL);
1754 gcc_assert (oname != NULL);
1755 f = XCNEW (struct outf);
1756 f->next = output_files;
1757 f->name = oname;
1758 output_files = f;
1760 oprintf (f, "/* Type information for %s.\n", name);
1761 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1762 oprintf (f, "%s", hdr[i]);
1763 return f;
1766 /* Print, like fprintf, to O.
1767 N.B. You might think this could be implemented more efficiently
1768 with vsnprintf(). Unfortunately, there are C libraries that
1769 provide that function but without the C99 semantics for its return
1770 value, making it impossible to know how much space is required. */
1771 void
1772 oprintf (outf_p o, const char *format, ...)
1774 char *s;
1775 size_t slength;
1776 va_list ap;
1778 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1779 in that case. */
1780 if (!o)
1781 return;
1783 va_start (ap, format);
1784 slength = vasprintf (&s, format, ap);
1785 if (s == NULL || (int) slength < 0)
1786 fatal ("out of memory");
1787 va_end (ap);
1789 if (o->bufused + slength > o->buflength)
1791 size_t new_len = o->buflength;
1792 if (new_len == 0)
1793 new_len = 1024;
1796 new_len *= 2;
1798 while (o->bufused + slength >= new_len);
1799 o->buf = XRESIZEVEC (char, o->buf, new_len);
1800 o->buflength = new_len;
1802 memcpy (o->buf + o->bufused, s, slength);
1803 o->bufused += slength;
1804 free (s);
1807 /* Open the global header file and the language-specific header files. */
1809 static void
1810 open_base_files (void)
1812 size_t i;
1814 if (nb_plugin_files > 0 && plugin_files)
1815 return;
1817 header_file = create_file ("GCC", "gtype-desc.h");
1819 base_files = XNEWVEC (outf_p, num_lang_dirs);
1821 for (i = 0; i < num_lang_dirs; i++)
1822 base_files[i] = create_file (lang_dir_names[i],
1823 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1825 /* gtype-desc.c is a little special, so we create it here. */
1827 /* The order of files here matters very much. */
1828 static const char *const ifiles[] = {
1829 "config.h", "system.h", "coretypes.h", "tm.h",
1830 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1831 "tree.h", "rtl.h", "wide-int.h", "function.h", "insn-config.h", "expr.h",
1832 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1833 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1834 "hash-table.h", "vec.h", "ggc.h", "basic-block.h",
1835 "tree-ssa-alias.h", "internal-fn.h", "gimple-fold.h", "tree-eh.h",
1836 "gimple-expr.h", "is-a.h",
1837 "gimple.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
1838 "tree-phinodes.h", "ssa-iterators.h", "stringpool.h", "tree-ssanames.h",
1839 "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h", "tree-ssa-loop-manip.h",
1840 "tree-ssa-loop-niter.h", "tree-into-ssa.h", "tree-dfa.h",
1841 "tree-ssa.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1842 "except.h", "output.h", "cfgloop.h",
1843 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h",
1844 "ipa-inline.h", "dwarf2out.h", NULL
1846 const char *const *ifp;
1847 outf_p gtype_desc_c;
1849 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1850 for (ifp = ifiles; *ifp; ifp++)
1851 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1853 /* Make sure we handle "cfun" specially. */
1854 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1855 oprintf (gtype_desc_c, "#undef cfun\n");
1857 oprintf (gtype_desc_c,
1858 "\n"
1859 "/* Types with a \"gcc::\" namespace have it stripped\n"
1860 " during gengtype parsing. Provide a \"using\" directive\n"
1861 " to ensure that the fully-qualified types are found. */\n"
1862 "using namespace gcc;\n");
1866 /* For INPF an input file, return the real basename of INPF, with all
1867 the directory components skipped. */
1869 static const char *
1870 get_file_realbasename (const input_file *inpf)
1872 return lbasename (get_input_file_name (inpf));
1875 /* For INPF a filename, return the relative path to INPF from
1876 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1878 const char *
1879 get_file_srcdir_relative_path (const input_file *inpf)
1881 const char *f = get_input_file_name (inpf);
1882 if (strlen (f) > srcdir_len
1883 && IS_DIR_SEPARATOR (f[srcdir_len])
1884 && strncmp (f, srcdir, srcdir_len) == 0)
1885 return f + srcdir_len + 1;
1886 else
1887 return NULL;
1890 /* For INPF an input_file, return the relative path to INPF from
1891 $(srcdir) if the latter is a prefix in INPF, or the real basename
1892 of INPF otherwise. */
1894 static const char *
1895 get_file_basename (const input_file *inpf)
1897 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1899 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1902 /* For F a filename, return the lang_dir_names relative index of the language
1903 directory that is a prefix in F, if any, -1 otherwise. */
1905 static int
1906 get_prefix_langdir_index (const char *f)
1908 size_t f_len = strlen (f);
1909 size_t lang_index;
1911 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1913 const char *langdir = lang_dir_names[lang_index];
1914 size_t langdir_len = strlen (langdir);
1916 if (f_len > langdir_len
1917 && IS_DIR_SEPARATOR (f[langdir_len])
1918 && memcmp (f, langdir, langdir_len) == 0)
1919 return lang_index;
1922 return -1;
1925 /* For INPF an input file, return the name of language directory where
1926 F is located, if any, NULL otherwise. */
1928 static const char *
1929 get_file_langdir (const input_file *inpf)
1931 /* Get the relative path to INPF from $(srcdir) and find the
1932 language by comparing the prefix with language directory names.
1933 If INPF is not even srcdir relative, no point in looking
1934 further. */
1936 int lang_index;
1937 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1938 const char *r;
1940 if (!srcdir_relative_path)
1941 return NULL;
1943 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1944 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1945 r = "c-family";
1946 else if (lang_index >= 0)
1947 r = lang_dir_names[lang_index];
1948 else
1949 r = NULL;
1951 return r;
1954 /* The gt- output file name for INPF. */
1956 static const char *
1957 get_file_gtfilename (const input_file *inpf)
1959 /* Cook up an initial version of the gt- file name from the file real
1960 basename and the language name, if any. */
1962 const char *basename = get_file_realbasename (inpf);
1963 const char *langdir = get_file_langdir (inpf);
1965 char *result =
1966 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1967 : xasprintf ("gt-%s", basename));
1969 /* Then replace all non alphanumerics characters by '-' and change the
1970 extension to ".h". We expect the input filename extension was at least
1971 one character long. */
1973 char *s = result;
1975 for (; *s != '.'; s++)
1976 if (!ISALNUM (*s) && *s != '-')
1977 *s = '-';
1979 memcpy (s, ".h", sizeof (".h"));
1981 return result;
1984 /* Each input_file has its associated output file outf_p. The
1985 association is computed by the function
1986 get_output_file_with_visibility. The associated file is cached
1987 inside input_file in its inpoutf field, so is really computed only
1988 once. Associated output file paths (i.e. output_name-s) are
1989 computed by a rule based regexp machinery, using the files_rules
1990 array of struct file_rule_st. A for_name is also computed, giving
1991 the source file name for which the output_file is generated; it is
1992 often the last component of the input_file path. */
1996 Regexpr machinery to compute the output_name and for_name-s of each
1997 input_file. We have a sequence of file rules which gives the POSIX
1998 extended regular expression to match an input file path, and two
1999 transformed strings for the corresponding output_name and the
2000 corresponding for_name. The transformed string contain dollars: $0
2001 is replaced by the entire match, $1 is replaced by the substring
2002 matching the first parenthesis in the regexp, etc. And $$ is replaced
2003 by a single verbatim dollar. The rule order is important. The
2004 general case is last, and the particular cases should come before.
2005 An action routine can, when needed, update the out_name & for_name
2006 and/or return the appropriate output file. It is invoked only when a
2007 rule is triggered. When a rule is triggered, the output_name and
2008 for_name are computed using their transform string in while $$, $0,
2009 $1, ... are suitably replaced. If there is an action, it is called.
2010 In some few cases, the action can directly return the outf_p, but
2011 usually it just updates the output_name and for_name so should free
2012 them before replacing them. The get_output_file_with_visibility
2013 function creates an outf_p only once per each output_name, so it
2014 scans the output_files list for previously seen output file names.
2017 /* Signature of actions in file rules. */
2018 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
2021 struct file_rule_st {
2022 const char* frul_srcexpr; /* Source string for regexp. */
2023 int frul_rflags; /* Flags passed to regcomp, usually
2024 * REG_EXTENDED. */
2025 regex_t* frul_re; /* Compiled regular expression
2026 obtained by regcomp. */
2027 const char* frul_tr_out; /* Transformation string for making
2028 * the output_name, with $1 ... $9 for
2029 * subpatterns and $0 for the whole
2030 * matched filename. */
2031 const char* frul_tr_for; /* Tranformation string for making the
2032 for_name. */
2033 frul_actionrout_t* frul_action; /* The action, if non null, is
2034 * called once the rule matches, on
2035 * the transformed out_name &
2036 * for_name. It could change them
2037 * and/or give the output file. */
2040 /* File rule action handling *.h files. */
2041 static outf_p header_dot_h_frul (input_file*, char**, char**);
2043 /* File rule action handling *.c files. */
2044 static outf_p source_dot_c_frul (input_file*, char**, char**);
2046 #define NULL_REGEX (regex_t*)0
2048 /* The prefix in our regexp-s matching the directory. */
2049 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
2051 #define NULL_FRULACT (frul_actionrout_t*)0
2053 /* The array of our rules governing file name generation. Rules order
2054 matters, so change with extreme care! */
2056 struct file_rule_st files_rules[] = {
2057 /* The general rule assumes that files in subdirectories belong to a
2058 particular front-end, and files not in subdirectories are shared.
2059 The following rules deal with exceptions - files that are in
2060 subdirectories and yet are shared, and files that are top-level,
2061 but are not shared. */
2063 /* the c-family/ source directory is special. */
2064 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
2065 REG_EXTENDED, NULL_REGEX,
2066 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
2068 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
2069 REG_EXTENDED, NULL_REGEX,
2070 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
2072 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
2073 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
2074 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
2076 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
2077 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
2079 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
2080 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
2081 REG_EXTENDED, NULL_REGEX,
2082 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
2084 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
2085 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
2086 REG_EXTENDED, NULL_REGEX,
2087 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
2089 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
2090 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
2091 REG_EXTENDED, NULL_REGEX,
2092 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
2094 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
2095 { DIR_PREFIX_REGEX "cp/parser\\.h$",
2096 REG_EXTENDED, NULL_REGEX,
2097 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
2099 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
2100 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
2101 REG_EXTENDED, NULL_REGEX,
2102 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
2104 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
2105 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
2106 REG_EXTENDED, NULL_REGEX,
2107 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
2109 /* General cases. For header *.h and source *.c or *.cc files, we
2110 * need special actions to handle the language. */
2112 /* Source *.c files are using get_file_gtfilename to compute their
2113 output_name and get_file_basename to compute their for_name
2114 through the source_dot_c_frul action. */
2115 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
2116 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
2118 /* Source *.cc files are using get_file_gtfilename to compute their
2119 output_name and get_file_basename to compute their for_name
2120 through the source_dot_c_frul action. */
2121 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.cc$",
2122 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.cc", source_dot_c_frul},
2124 /* Common header files get "gtype-desc.c" as their output_name,
2125 * while language specific header files are handled specially. So
2126 * we need the header_dot_h_frul action. */
2127 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
2128 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
2130 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
2131 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
2133 /* Mandatory null last entry signaling end of rules. */
2134 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
2137 /* Special file rules action for handling *.h header files. It gives
2138 "gtype-desc.c" for common headers and corresponding output
2139 files for language-specific header files. */
2140 static outf_p
2141 header_dot_h_frul (input_file* inpf, char**poutname,
2142 char**pforname ATTRIBUTE_UNUSED)
2144 const char *basename = 0;
2145 int lang_index = 0;
2146 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2147 (void*) inpf, get_input_file_name (inpf),
2148 *poutname, *pforname);
2149 basename = get_file_basename (inpf);
2150 lang_index = get_prefix_langdir_index (basename);
2151 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
2153 if (lang_index >= 0)
2155 /* The header is language specific. Given output_name &
2156 for_name remains unchanged. The base_files array gives the
2157 outf_p. */
2158 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2159 (void*) base_files[lang_index],
2160 (base_files[lang_index])->name);
2161 return base_files[lang_index];
2163 else
2165 /* The header is common to all front-end languages. So
2166 output_name is "gtype-desc.c" file. The calling function
2167 get_output_file_with_visibility will find its outf_p. */
2168 free (*poutname);
2169 *poutname = xstrdup ("gtype-desc.c");
2170 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2171 get_input_file_name (inpf));
2172 return NULL;
2177 /* Special file rules action for handling *.c source files using
2178 * get_file_gtfilename to compute their output_name and
2179 * get_file_basename to compute their for_name. The output_name is
2180 * gt-<LANG>-<BASE>.h for language specific source files, and
2181 * gt-<BASE>.h for common source files. */
2182 static outf_p
2183 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
2185 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
2186 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
2187 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2188 (void*) inpf, get_input_file_name (inpf),
2189 *poutname, *pforname);
2190 DBGPRINTF ("newoutname %s", newoutname);
2191 DBGPRINTF ("newbasename %s", newbasename);
2192 free (*poutname);
2193 free (*pforname);
2194 *poutname = newoutname;
2195 *pforname = newbasename;
2196 return NULL;
2199 /* Utility function for get_output_file_with_visibility which returns
2200 * a malloc-ed substituted string using TRS on matching of the FILNAM
2201 * file name, using the PMATCH array. */
2202 static char*
2203 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
2204 const char *trs)
2206 struct obstack str_obstack;
2207 char *str = NULL;
2208 char *rawstr = NULL;
2209 const char *pt = NULL;
2210 DBGPRINTF ("filnam %s", filnam);
2211 obstack_init (&str_obstack);
2212 for (pt = trs; *pt; pt++) {
2213 char c = *pt;
2214 if (c == '$')
2216 if (pt[1] == '$')
2218 /* A double dollar $$ is substituted by a single verbatim
2219 dollar, but who really uses dollar signs in file
2220 paths? */
2221 obstack_1grow (&str_obstack, '$');
2223 else if (ISDIGIT (pt[1]))
2225 /* Handle $0 $1 ... $9 by appropriate substitution. */
2226 int dolnum = pt[1] - '0';
2227 int so = pmatch[dolnum].rm_so;
2228 int eo = pmatch[dolnum].rm_eo;
2229 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2230 if (so>=0 && eo>=so)
2231 obstack_grow (&str_obstack, filnam + so, eo - so);
2233 else
2235 /* This can happen only when files_rules is buggy! */
2236 gcc_unreachable ();
2238 /* Always skip the character after the dollar. */
2239 pt++;
2241 else
2242 obstack_1grow (&str_obstack, c);
2244 obstack_1grow (&str_obstack, '\0');
2245 rawstr = XOBFINISH (&str_obstack, char *);
2246 str = xstrdup (rawstr);
2247 obstack_free (&str_obstack, NULL);
2248 DBGPRINTF ("matched replacement %s", str);
2249 rawstr = NULL;
2250 return str;
2254 /* An output file, suitable for definitions, that can see declarations
2255 made in INPF and is linked into every language that uses INPF.
2256 Since the result is cached inside INPF, that argument cannot be
2257 declared constant, but is "almost" constant. */
2259 outf_p
2260 get_output_file_with_visibility (input_file *inpf)
2262 outf_p r;
2263 char *for_name = NULL;
2264 char *output_name = NULL;
2265 const char* inpfname;
2267 /* This can happen when we need a file with visibility on a
2268 structure that we've never seen. We have to just hope that it's
2269 globally visible. */
2270 if (inpf == NULL)
2271 inpf = system_h_file;
2273 /* The result is cached in INPF, so return it if already known. */
2274 if (inpf->inpoutf)
2275 return inpf->inpoutf;
2277 /* In plugin mode, return NULL unless the input_file is one of the
2278 plugin_files. */
2279 if (plugin_files)
2281 size_t i;
2282 for (i = 0; i < nb_plugin_files; i++)
2283 if (inpf == plugin_files[i])
2285 inpf->inpoutf = plugin_output;
2286 return plugin_output;
2289 return NULL;
2292 inpfname = get_input_file_name (inpf);
2294 /* Try each rule in sequence in files_rules until one is triggered. */
2296 int rulix = 0;
2297 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2298 (void*) inpf, inpfname);
2300 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2302 DBGPRINTF ("rulix#%d srcexpr %s",
2303 rulix, files_rules[rulix].frul_srcexpr);
2305 if (!files_rules[rulix].frul_re)
2307 /* Compile the regexpr lazily. */
2308 int err = 0;
2309 files_rules[rulix].frul_re = XCNEW (regex_t);
2310 err = regcomp (files_rules[rulix].frul_re,
2311 files_rules[rulix].frul_srcexpr,
2312 files_rules[rulix].frul_rflags);
2313 if (err)
2315 /* The regular expression compilation fails only when
2316 file_rules is buggy. */
2317 gcc_unreachable ();
2321 output_name = NULL;
2322 for_name = NULL;
2324 /* Match the regexpr and trigger the rule if matched. */
2326 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2327 $3, ... $9. */
2328 regmatch_t pmatch[10];
2329 memset (pmatch, 0, sizeof (pmatch));
2330 if (!regexec (files_rules[rulix].frul_re,
2331 inpfname, 10, pmatch, 0))
2333 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2334 (void*) inpf, inpfname, rulix,
2335 files_rules[rulix].frul_srcexpr);
2336 for_name =
2337 matching_file_name_substitute (inpfname, pmatch,
2338 files_rules[rulix].frul_tr_for);
2339 DBGPRINTF ("for_name %s", for_name);
2340 output_name =
2341 matching_file_name_substitute (inpfname, pmatch,
2342 files_rules[rulix].frul_tr_out);
2343 DBGPRINTF ("output_name %s", output_name);
2344 if (files_rules[rulix].frul_action)
2346 /* Invoke our action routine. */
2347 outf_p of = NULL;
2348 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2349 rulix, output_name, for_name);
2350 of =
2351 (files_rules[rulix].frul_action) (inpf,
2352 &output_name, &for_name);
2353 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2354 rulix, (void*)of, output_name, for_name);
2355 /* If the action routine returned something, give it back
2356 immediately and cache it in inpf. */
2357 if (of)
2359 inpf->inpoutf = of;
2360 return of;
2363 /* The rule matched, and had no action, or that action did
2364 not return any output file but could have changed the
2365 output_name or for_name. We break out of the loop on the
2366 files_rules. */
2367 break;
2369 else
2371 /* The regexpr did not match. */
2372 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2373 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2374 continue;
2379 if (!output_name || !for_name)
2381 /* This should not be possible, and could only happen if the
2382 files_rules is incomplete or buggy. */
2383 fatal ("failed to compute output name for %s", inpfname);
2386 /* Look through to see if we've ever seen this output filename
2387 before. If found, cache the result in inpf. */
2388 for (r = output_files; r; r = r->next)
2389 if (filename_cmp (r->name, output_name) == 0)
2391 inpf->inpoutf = r;
2392 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2393 output_name, for_name);
2394 return r;
2397 /* If not found, create it, and cache it in inpf. */
2398 r = create_file (for_name, output_name);
2400 gcc_assert (r && r->name);
2401 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2402 output_name, for_name);
2403 inpf->inpoutf = r;
2404 return r;
2409 /* The name of an output file, suitable for definitions, that can see
2410 declarations made in INPF and is linked into every language that
2411 uses INPF. */
2413 const char *
2414 get_output_file_name (input_file* inpf)
2416 outf_p o = get_output_file_with_visibility (inpf);
2417 if (o)
2418 return o->name;
2419 return NULL;
2422 /* Check if existing file is equal to the in memory buffer. */
2424 static bool
2425 is_file_equal (outf_p of)
2427 FILE *newfile = fopen (of->name, "r");
2428 size_t i;
2429 bool equal;
2430 if (newfile == NULL)
2431 return false;
2433 equal = true;
2434 for (i = 0; i < of->bufused; i++)
2436 int ch;
2437 ch = fgetc (newfile);
2438 if (ch == EOF || ch != (unsigned char) of->buf[i])
2440 equal = false;
2441 break;
2444 if (equal && EOF != fgetc (newfile))
2445 equal = false;
2446 fclose (newfile);
2447 return equal;
2450 /* Copy the output to its final destination,
2451 but don't unnecessarily change modification times. */
2453 static void
2454 close_output_files (void)
2456 int nbwrittenfiles = 0;
2457 outf_p of;
2459 for (of = output_files; of; of = of->next)
2461 if (!is_file_equal (of))
2463 FILE *newfile = NULL;
2464 char *backupname = NULL;
2465 /* Back up the old version of the output file gt-FOO.c as
2466 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2467 if (backup_dir)
2469 backupname = concat (backup_dir, "/",
2470 lbasename (of->name), "~", NULL);
2471 if (!access (of->name, F_OK) && rename (of->name, backupname))
2472 fatal ("failed to back up %s as %s: %s",
2473 of->name, backupname, xstrerror (errno));
2476 newfile = fopen (of->name, "w");
2477 if (newfile == NULL)
2478 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2479 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2480 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2481 if (fclose (newfile) != 0)
2482 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2483 nbwrittenfiles++;
2484 if (verbosity_level >= 2 && backupname)
2485 printf ("%s wrote #%-3d %s backed-up in %s\n",
2486 progname, nbwrittenfiles, of->name, backupname);
2487 else if (verbosity_level >= 1)
2488 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2489 free (backupname);
2491 else
2493 /* output file remains unchanged. */
2494 if (verbosity_level >= 2)
2495 printf ("%s keep %s\n", progname, of->name);
2497 free (of->buf);
2498 of->buf = NULL;
2499 of->bufused = of->buflength = 0;
2501 if (verbosity_level >= 1)
2502 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2505 struct flist
2507 struct flist *next;
2508 int started_p;
2509 const input_file* file;
2510 outf_p f;
2513 struct walk_type_data;
2515 /* For scalars and strings, given the item in 'val'.
2516 For structures, given a pointer to the item in 'val'.
2517 For misc. pointers, given the item in 'val'.
2519 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2520 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2522 /* Parameters for write_types. */
2524 struct write_types_data
2526 const char *prefix;
2527 const char *param_prefix;
2528 const char *subfield_marker_routine;
2529 const char *marker_routine;
2530 const char *reorder_note_routine;
2531 const char *comment;
2532 int skip_hooks; /* skip hook generation if non zero */
2533 enum write_types_kinds kind;
2536 static void output_escaped_param (struct walk_type_data *d,
2537 const char *, const char *);
2538 static void output_mangled_typename (outf_p, const_type_p);
2539 static void walk_type (type_p t, struct walk_type_data *d);
2540 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2541 const struct write_types_data *wtd);
2542 static void write_types_process_field
2543 (type_p f, const struct walk_type_data *d);
2544 static void write_types (outf_p output_header,
2545 type_p structures,
2546 type_p param_structs,
2547 const struct write_types_data *wtd);
2548 static void write_types_local_process_field
2549 (type_p f, const struct walk_type_data *d);
2550 static void write_local_func_for_structure
2551 (const_type_p orig_s, type_p s, type_p *param);
2552 static void write_local (outf_p output_header,
2553 type_p structures, type_p param_structs);
2554 static int contains_scalar_p (type_p t);
2555 static void put_mangled_filename (outf_p, const input_file *);
2556 static void finish_root_table (struct flist *flp, const char *pfx,
2557 const char *tname, const char *lastname,
2558 const char *name);
2559 static void write_root (outf_p, pair_p, type_p, const char *, int,
2560 struct fileloc *, const char *, bool);
2561 static void write_array (outf_p f, pair_p v,
2562 const struct write_types_data *wtd);
2563 static void write_roots (pair_p, bool);
2565 /* Parameters for walk_type. */
2567 struct walk_type_data
2569 process_field_fn process_field;
2570 const void *cookie;
2571 outf_p of;
2572 options_p opt;
2573 const char *val;
2574 const char *prev_val[4];
2575 int indent;
2576 int counter;
2577 const struct fileloc *line;
2578 lang_bitmap bitmap;
2579 type_p *param;
2580 int used_length;
2581 type_p orig_s;
2582 const char *reorder_fn;
2583 bool needs_cast_p;
2584 bool fn_wants_lvalue;
2585 bool in_record_p;
2586 int loopcounter;
2587 bool in_ptr_field;
2588 bool have_this_obj;
2592 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2593 pre-processor identifier to use in a #define directive. This replaces
2594 special characters used in C++ identifiers like '>', '<' and ':' with
2595 '_'.
2597 If no C++ special characters are found in TYPE_NAME, return
2598 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2599 characters replaced with '_'. In this case, the caller is
2600 responsible for freeing the allocated string. */
2602 static const char *
2603 filter_type_name (const char *type_name)
2605 if (strchr (type_name, '<') || strchr (type_name, ':'))
2607 size_t i;
2608 char *s = xstrdup (type_name);
2609 for (i = 0; i < strlen (s); i++)
2610 if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ','
2611 || s[i] == '*')
2612 s[i] = '_';
2613 return s;
2615 else
2616 return type_name;
2620 /* Print a mangled name representing T to OF. */
2622 static void
2623 output_mangled_typename (outf_p of, const_type_p t)
2625 if (t == NULL)
2626 oprintf (of, "Z");
2627 else
2628 switch (t->kind)
2630 case TYPE_NONE:
2631 case TYPE_UNDEFINED:
2632 gcc_unreachable ();
2633 break;
2634 case TYPE_POINTER:
2635 oprintf (of, "P");
2636 output_mangled_typename (of, t->u.p);
2637 break;
2638 case TYPE_SCALAR:
2639 oprintf (of, "I");
2640 break;
2641 case TYPE_STRING:
2642 oprintf (of, "S");
2643 break;
2644 case TYPE_STRUCT:
2645 case TYPE_UNION:
2646 case TYPE_LANG_STRUCT:
2647 case TYPE_USER_STRUCT:
2649 /* For references to classes within an inheritance hierarchy,
2650 only ever reference the ultimate base class, since only
2651 it will have gt_ functions. */
2652 t = get_ultimate_base_class (t);
2653 const char *id_for_tag = filter_type_name (t->u.s.tag);
2654 oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2655 id_for_tag);
2656 if (id_for_tag != t->u.s.tag)
2657 free (CONST_CAST (char *, id_for_tag));
2659 break;
2660 case TYPE_PARAM_STRUCT:
2662 int i;
2663 for (i = 0; i < NUM_PARAM; i++)
2664 if (t->u.param_struct.param[i] != NULL)
2665 output_mangled_typename (of, t->u.param_struct.param[i]);
2666 output_mangled_typename (of, t->u.param_struct.stru);
2668 break;
2669 case TYPE_ARRAY:
2670 gcc_unreachable ();
2674 /* Print PARAM to D->OF processing escapes. D->VAL references the
2675 current object, D->PREV_VAL the object containing the current
2676 object, ONAME is the name of the option and D->LINE is used to
2677 print error messages. */
2679 static void
2680 output_escaped_param (struct walk_type_data *d, const char *param,
2681 const char *oname)
2683 const char *p;
2685 for (p = param; *p; p++)
2686 if (*p != '%')
2687 oprintf (d->of, "%c", *p);
2688 else
2689 switch (*++p)
2691 case 'h':
2692 oprintf (d->of, "(%s)", d->prev_val[2]);
2693 break;
2694 case '0':
2695 oprintf (d->of, "(%s)", d->prev_val[0]);
2696 break;
2697 case '1':
2698 oprintf (d->of, "(%s)", d->prev_val[1]);
2699 break;
2700 case 'a':
2702 const char *pp = d->val + strlen (d->val);
2703 while (pp[-1] == ']')
2704 while (*pp != '[')
2705 pp--;
2706 oprintf (d->of, "%s", pp);
2708 break;
2709 default:
2710 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2711 oname, '%', *p);
2715 const char *
2716 get_string_option (options_p opt, const char *key)
2718 for (; opt; opt = opt->next)
2719 if (strcmp (opt->name, key) == 0)
2720 return opt->info.string;
2721 return NULL;
2724 /* Machinery for avoiding duplicate tags within switch statements. */
2725 struct seen_tag
2727 const char *tag;
2728 struct seen_tag *next;
2732 already_seen_tag (struct seen_tag *seen_tags, const char *tag)
2734 /* Linear search, so O(n^2), but n is currently small. */
2735 while (seen_tags)
2737 if (!strcmp (seen_tags->tag, tag))
2738 return 1;
2739 seen_tags = seen_tags->next;
2741 /* Not yet seen this tag. */
2742 return 0;
2745 void
2746 mark_tag_as_seen (struct seen_tag **seen_tags, const char *tag)
2748 /* Add to front of linked list. */
2749 struct seen_tag *new_node = XCNEW (struct seen_tag);
2750 new_node->tag = tag;
2751 new_node->next = *seen_tags;
2752 *seen_tags = new_node;
2755 static void
2756 walk_subclasses (type_p base, struct walk_type_data *d,
2757 struct seen_tag **seen_tags)
2759 for (type_p sub = base->u.s.first_subclass; sub != NULL;
2760 sub = sub->u.s.next_sibling_class)
2762 const char *type_tag = get_string_option (sub->u.s.opt, "tag");
2763 if (type_tag && !already_seen_tag (*seen_tags, type_tag))
2765 mark_tag_as_seen (seen_tags, type_tag);
2766 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
2767 d->indent += 2;
2768 oprintf (d->of, "%*s{\n", d->indent, "");
2769 d->indent += 2;
2770 oprintf (d->of, "%*s%s *sub = static_cast <%s *> (x);\n",
2771 d->indent, "", sub->u.s.tag, sub->u.s.tag);
2772 const char *old_val = d->val;
2773 d->val = "(*sub)";
2774 walk_type (sub, d);
2775 d->val = old_val;
2776 d->indent -= 2;
2777 oprintf (d->of, "%*s}\n", d->indent, "");
2778 oprintf (d->of, "%*sbreak;\n", d->indent, "");
2779 d->indent -= 2;
2781 walk_subclasses (sub, d, seen_tags);
2785 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2786 which is of type T. Write code to D->OF to constrain execution (at
2787 the point that D->PROCESS_FIELD is called) to the appropriate
2788 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2789 pointers to those objects. D->PREV_VAL lists the objects
2790 containing the current object, D->OPT is a list of options to
2791 apply, D->INDENT is the current indentation level, D->LINE is used
2792 to print error messages, D->BITMAP indicates which languages to
2793 print the structure for, and D->PARAM is the current parameter
2794 (from an enclosing param_is option). */
2796 static void
2797 walk_type (type_p t, struct walk_type_data *d)
2799 const char *length = NULL;
2800 const char *desc = NULL;
2801 const char *type_tag = NULL;
2802 int maybe_undef_p = 0;
2803 int use_param_num = -1;
2804 int use_params_p = 0;
2805 int atomic_p = 0;
2806 options_p oo;
2807 const struct nested_ptr_data *nested_ptr_d = NULL;
2809 d->needs_cast_p = false;
2810 for (oo = d->opt; oo; oo = oo->next)
2811 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2812 length = oo->info.string;
2813 else if (strcmp (oo->name, "maybe_undef") == 0)
2814 maybe_undef_p = 1;
2815 else if (strncmp (oo->name, "use_param", 9) == 0
2816 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2817 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2818 else if (strcmp (oo->name, "use_params") == 0)
2819 use_params_p = 1;
2820 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2821 desc = oo->info.string;
2822 else if (strcmp (oo->name, "mark_hook") == 0)
2824 else if (strcmp (oo->name, "nested_ptr") == 0
2825 && oo->kind == OPTION_NESTED)
2826 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2827 else if (strcmp (oo->name, "dot") == 0)
2829 else if (strcmp (oo->name, "tag") == 0)
2830 type_tag = oo->info.string;
2831 else if (strcmp (oo->name, "special") == 0)
2833 else if (strcmp (oo->name, "skip") == 0)
2835 else if (strcmp (oo->name, "atomic") == 0)
2836 atomic_p = 1;
2837 else if (strcmp (oo->name, "default") == 0)
2839 else if (strcmp (oo->name, "param_is") == 0)
2841 else if (strncmp (oo->name, "param", 5) == 0
2842 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2844 else if (strcmp (oo->name, "chain_next") == 0)
2846 else if (strcmp (oo->name, "chain_prev") == 0)
2848 else if (strcmp (oo->name, "chain_circular") == 0)
2850 else if (strcmp (oo->name, "reorder") == 0)
2852 else if (strcmp (oo->name, "variable_size") == 0)
2854 else if (strcmp (oo->name, "for_user") == 0)
2856 else
2857 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2859 if (d->used_length)
2860 length = NULL;
2862 if (use_params_p)
2864 int pointer_p = t->kind == TYPE_POINTER;
2866 if (pointer_p)
2867 t = t->u.p;
2868 if (!union_or_struct_p (t))
2869 error_at_line (d->line, "`use_params' option on unimplemented type");
2870 else
2871 t = find_param_structure (t, d->param);
2872 if (pointer_p)
2873 t = create_pointer (t);
2876 if (use_param_num != -1)
2878 if (d->param != NULL && d->param[use_param_num] != NULL)
2880 type_p nt = d->param[use_param_num];
2882 if (t->kind == TYPE_ARRAY)
2883 nt = create_array (nt, t->u.a.len);
2884 else if (length != NULL && t->kind == TYPE_POINTER)
2885 nt = create_pointer (nt);
2886 d->needs_cast_p = (t->kind != TYPE_POINTER
2887 && (nt->kind == TYPE_POINTER
2888 || nt->kind == TYPE_STRING));
2889 t = nt;
2891 else
2892 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2895 if (maybe_undef_p
2896 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
2898 error_at_line (d->line,
2899 "field `%s' has invalid option `maybe_undef_p'\n",
2900 d->val);
2901 return;
2904 if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2906 error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2907 return;
2910 switch (t->kind)
2912 case TYPE_SCALAR:
2913 case TYPE_STRING:
2914 d->process_field (t, d);
2915 break;
2917 case TYPE_POINTER:
2919 d->in_ptr_field = true;
2920 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2922 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2923 break;
2926 /* If a pointer type is marked as "atomic", we process the
2927 field itself, but we don't walk the data that they point to.
2929 There are two main cases where we walk types: to mark
2930 pointers that are reachable, and to relocate pointers when
2931 writing a PCH file. In both cases, an atomic pointer is
2932 itself marked or relocated, but the memory that it points
2933 to is left untouched. In the case of PCH, that memory will
2934 be read/written unchanged to the PCH file. */
2935 if (atomic_p)
2937 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2938 d->indent += 2;
2939 d->process_field (t, d);
2940 d->indent -= 2;
2941 oprintf (d->of, "%*s}\n", d->indent, "");
2942 break;
2945 if (!length)
2947 if (!union_or_struct_p (t->u.p)
2948 && t->u.p->kind != TYPE_PARAM_STRUCT)
2950 error_at_line (d->line,
2951 "field `%s' is pointer to unimplemented type",
2952 d->val);
2953 break;
2956 if (nested_ptr_d)
2958 const char *oldprevval2 = d->prev_val[2];
2960 if (!union_or_struct_p (nested_ptr_d->type))
2962 error_at_line (d->line,
2963 "field `%s' has invalid "
2964 "option `nested_ptr'\n", d->val);
2965 return;
2968 d->prev_val[2] = d->val;
2969 oprintf (d->of, "%*s{\n", d->indent, "");
2970 d->indent += 2;
2971 d->val = xasprintf ("x%d", d->counter++);
2972 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2973 (nested_ptr_d->type->kind == TYPE_UNION
2974 ? "union" : "struct"),
2975 nested_ptr_d->type->u.s.tag,
2976 d->fn_wants_lvalue ? "" : "const ", d->val);
2977 oprintf (d->of, "%*s", d->indent + 2, "");
2978 output_escaped_param (d, nested_ptr_d->convert_from,
2979 "nested_ptr");
2980 oprintf (d->of, ";\n");
2982 d->process_field (nested_ptr_d->type, d);
2984 if (d->fn_wants_lvalue)
2986 oprintf (d->of, "%*s%s = ", d->indent, "",
2987 d->prev_val[2]);
2988 d->prev_val[2] = d->val;
2989 output_escaped_param (d, nested_ptr_d->convert_to,
2990 "nested_ptr");
2991 oprintf (d->of, ";\n");
2994 d->indent -= 2;
2995 oprintf (d->of, "%*s}\n", d->indent, "");
2996 d->val = d->prev_val[2];
2997 d->prev_val[2] = oldprevval2;
2999 else
3000 d->process_field (t->u.p, d);
3002 else
3004 int loopcounter = d->loopcounter;
3005 const char *oldval = d->val;
3006 const char *oldprevval3 = d->prev_val[3];
3007 char *newval;
3009 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
3010 d->indent += 2;
3011 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
3012 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
3013 "", loopcounter, loopcounter);
3014 if (!d->in_record_p)
3015 output_escaped_param (d, length, "length");
3016 else
3017 oprintf (d->of, "l%d", loopcounter);
3018 if (d->have_this_obj)
3019 /* Try to unswitch loops (see PR53880). */
3020 oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
3021 oprintf (d->of, "); i%d++) {\n", loopcounter);
3022 d->indent += 2;
3023 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
3024 d->used_length = 1;
3025 d->prev_val[3] = oldval;
3026 walk_type (t->u.p, d);
3027 free (newval);
3028 d->val = oldval;
3029 d->prev_val[3] = oldprevval3;
3030 d->used_length = 0;
3031 d->indent -= 2;
3032 oprintf (d->of, "%*s}\n", d->indent, "");
3033 d->process_field (t, d);
3034 d->indent -= 2;
3035 oprintf (d->of, "%*s}\n", d->indent, "");
3037 d->in_ptr_field = false;
3039 break;
3041 case TYPE_ARRAY:
3043 int loopcounter;
3044 const char *oldval = d->val;
3045 char *newval;
3047 /* If it's an array of scalars, we optimize by not generating
3048 any code. */
3049 if (t->u.a.p->kind == TYPE_SCALAR)
3050 break;
3052 if (length)
3053 loopcounter = d->loopcounter;
3054 else
3055 loopcounter = d->counter++;
3057 /* When walking an array, compute the length and store it in a
3058 local variable before walking the array elements, instead of
3059 recomputing the length expression each time through the loop.
3060 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
3061 where the length is stored in the first array element,
3062 because otherwise that operand can get overwritten on the
3063 first iteration. */
3064 oprintf (d->of, "%*s{\n", d->indent, "");
3065 d->indent += 2;
3066 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
3067 if (!d->in_record_p || !length)
3069 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
3070 d->indent, "", loopcounter);
3071 if (length)
3072 output_escaped_param (d, length, "length");
3073 else
3074 oprintf (d->of, "%s", t->u.a.len);
3075 oprintf (d->of, ");\n");
3078 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
3079 d->indent, "",
3080 loopcounter, loopcounter, loopcounter, loopcounter);
3081 d->indent += 2;
3082 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
3083 d->used_length = 1;
3084 walk_type (t->u.a.p, d);
3085 free (newval);
3086 d->used_length = 0;
3087 d->val = oldval;
3088 d->indent -= 2;
3089 oprintf (d->of, "%*s}\n", d->indent, "");
3090 d->indent -= 2;
3091 oprintf (d->of, "%*s}\n", d->indent, "");
3093 break;
3095 case TYPE_STRUCT:
3096 case TYPE_UNION:
3098 pair_p f;
3099 const char *oldval = d->val;
3100 const char *oldprevval1 = d->prev_val[1];
3101 const char *oldprevval2 = d->prev_val[2];
3102 const char *struct_mark_hook = NULL;
3103 const int union_p = t->kind == TYPE_UNION;
3104 int seen_default_p = 0;
3105 options_p o;
3106 int lengths_seen = 0;
3107 int endcounter;
3108 bool any_length_seen = false;
3110 if (!t->u.s.line.file)
3111 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
3113 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
3115 error_at_line (d->line,
3116 "structure `%s' defined for mismatching languages",
3117 t->u.s.tag);
3118 error_at_line (&t->u.s.line, "one structure defined here");
3121 /* Some things may also be defined in the structure's options. */
3122 for (o = t->u.s.opt; o; o = o->next)
3123 if (!desc && strcmp (o->name, "desc") == 0
3124 && o->kind == OPTION_STRING)
3125 desc = o->info.string;
3126 else if (!struct_mark_hook && strcmp (o->name, "mark_hook") == 0
3127 && o->kind == OPTION_STRING)
3128 struct_mark_hook = o->info.string;
3130 if (struct_mark_hook)
3131 oprintf (d->of, "%*s%s (&%s);\n",
3132 d->indent, "", struct_mark_hook, oldval);
3134 d->prev_val[2] = oldval;
3135 d->prev_val[1] = oldprevval2;
3136 if (union_p)
3138 if (desc == NULL)
3140 error_at_line (d->line,
3141 "missing `desc' option for union `%s'",
3142 t->u.s.tag);
3143 desc = "1";
3145 oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
3146 output_escaped_param (d, desc, "desc");
3147 oprintf (d->of, "))\n");
3148 d->indent += 2;
3149 oprintf (d->of, "%*s{\n", d->indent, "");
3151 else if (desc)
3153 /* We have a "desc" option on a struct, signifying the
3154 base class within a GC-managed inheritance hierarchy.
3155 The current code specialcases the base class, then walks
3156 into subclasses, recursing into this routine to handle them.
3157 This organization requires the base class to have a case in
3158 the switch statement, and hence a tag value is mandatory
3159 for the base class. This restriction could be removed, but
3160 it would require some restructing of this code. */
3161 if (!type_tag)
3163 error_at_line (d->line,
3164 "missing `tag' option for type `%s'",
3165 t->u.s.tag);
3167 oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
3168 output_escaped_param (d, desc, "desc");
3169 oprintf (d->of, "))\n");
3170 d->indent += 2;
3171 oprintf (d->of, "%*s{\n", d->indent, "");
3172 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
3173 d->indent += 2;
3176 FOR_ALL_INHERITED_FIELDS (t, f)
3178 options_p oo;
3179 int skip_p = 0;
3180 const char *fieldlength = NULL;
3182 d->reorder_fn = NULL;
3183 for (oo = f->opt; oo; oo = oo->next)
3184 if (strcmp (oo->name, "skip") == 0)
3185 skip_p = 1;
3186 else if (strcmp (oo->name, "length") == 0
3187 && oo->kind == OPTION_STRING)
3188 fieldlength = oo->info.string;
3190 if (skip_p)
3191 continue;
3192 if (fieldlength)
3194 lengths_seen++;
3195 d->counter++;
3196 if (!union_p)
3198 if (!any_length_seen)
3200 oprintf (d->of, "%*s{\n", d->indent, "");
3201 d->indent += 2;
3203 any_length_seen = true;
3205 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
3206 d->indent, "", d->counter - 1);
3207 output_escaped_param (d, fieldlength, "length");
3208 oprintf (d->of, ");\n");
3212 endcounter = d->counter;
3214 FOR_ALL_INHERITED_FIELDS (t, f)
3216 options_p oo;
3217 const char *dot = ".";
3218 const char *tagid = NULL;
3219 int skip_p = 0;
3220 int default_p = 0;
3221 int use_param_p = 0;
3222 const char *fieldlength = NULL;
3223 char *newval;
3225 d->reorder_fn = NULL;
3226 for (oo = f->opt; oo; oo = oo->next)
3227 if (strcmp (oo->name, "dot") == 0
3228 && oo->kind == OPTION_STRING)
3229 dot = oo->info.string;
3230 else if (strcmp (oo->name, "tag") == 0
3231 && oo->kind == OPTION_STRING)
3232 tagid = oo->info.string;
3233 else if (strcmp (oo->name, "skip") == 0)
3234 skip_p = 1;
3235 else if (strcmp (oo->name, "default") == 0)
3236 default_p = 1;
3237 else if (strcmp (oo->name, "reorder") == 0
3238 && oo->kind == OPTION_STRING)
3239 d->reorder_fn = oo->info.string;
3240 else if (strncmp (oo->name, "use_param", 9) == 0
3241 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
3242 use_param_p = 1;
3243 else if (strcmp (oo->name, "length") == 0
3244 && oo->kind == OPTION_STRING)
3245 fieldlength = oo->info.string;
3247 if (skip_p)
3248 continue;
3250 if (union_p && tagid)
3252 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
3253 d->indent += 2;
3255 else if (union_p && default_p)
3257 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3258 d->indent += 2;
3259 seen_default_p = 1;
3261 else if (!union_p && (default_p || tagid))
3262 error_at_line (d->line,
3263 "can't use `%s' outside a union on field `%s'",
3264 default_p ? "default" : "tag", f->name);
3265 else if (union_p && !(default_p || tagid)
3266 && f->type->kind == TYPE_SCALAR)
3268 fprintf (stderr,
3269 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3270 get_input_file_name (d->line->file), d->line->line,
3271 f->name);
3272 continue;
3274 else if (union_p && !(default_p || tagid))
3275 error_at_line (d->line,
3276 "field `%s' is missing `tag' or `default' option",
3277 f->name);
3279 if (fieldlength)
3281 d->loopcounter = endcounter - lengths_seen--;
3284 d->line = &f->line;
3285 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
3286 d->opt = f->opt;
3287 d->used_length = false;
3288 d->in_record_p = !union_p;
3290 if (union_p && use_param_p && d->param == NULL)
3291 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
3292 else
3293 walk_type (f->type, d);
3295 d->in_record_p = false;
3297 free (newval);
3299 if (union_p)
3301 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3302 d->indent -= 2;
3305 d->reorder_fn = NULL;
3307 d->val = oldval;
3308 d->prev_val[1] = oldprevval1;
3309 d->prev_val[2] = oldprevval2;
3311 if (union_p && !seen_default_p)
3313 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3314 oprintf (d->of, "%*s break;\n", d->indent, "");
3317 if (desc && !union_p)
3319 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3320 d->indent -= 2;
3322 if (union_p)
3324 oprintf (d->of, "%*s}\n", d->indent, "");
3325 d->indent -= 2;
3327 else if (desc)
3329 /* Add cases to handle subclasses. */
3330 struct seen_tag *tags = NULL;
3331 walk_subclasses (t, d, &tags);
3333 /* Ensure that if someone forgets a "tag" option that we don't
3334 silent fail to traverse that subclass's fields. */
3335 if (!seen_default_p)
3337 oprintf (d->of, "%*s/* Unrecognized tag value. */\n",
3338 d->indent, "");
3339 oprintf (d->of, "%*sdefault: gcc_unreachable (); \n",
3340 d->indent, "");
3343 /* End of the switch statement */
3344 oprintf (d->of, "%*s}\n", d->indent, "");
3345 d->indent -= 2;
3347 if (any_length_seen)
3349 d->indent -= 2;
3350 oprintf (d->of, "%*s}\n", d->indent, "");
3353 break;
3355 case TYPE_LANG_STRUCT:
3357 type_p nt;
3358 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3359 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3360 break;
3361 if (nt == NULL)
3362 error_at_line (d->line, "structure `%s' differs between languages",
3363 t->u.s.tag);
3364 else
3365 walk_type (nt, d);
3367 break;
3369 case TYPE_PARAM_STRUCT:
3371 type_p *oldparam = d->param;
3373 d->param = t->u.param_struct.param;
3374 walk_type (t->u.param_struct.stru, d);
3375 d->param = oldparam;
3377 break;
3379 case TYPE_USER_STRUCT:
3380 d->process_field (t, d);
3381 break;
3383 case TYPE_NONE:
3384 case TYPE_UNDEFINED:
3385 gcc_unreachable ();
3389 /* process_field routine for marking routines. */
3391 static void
3392 write_types_process_field (type_p f, const struct walk_type_data *d)
3394 const struct write_types_data *wtd;
3395 const char *cast = d->needs_cast_p ? "(void *)" : "";
3396 wtd = (const struct write_types_data *) d->cookie;
3398 switch (f->kind)
3400 case TYPE_NONE:
3401 case TYPE_UNDEFINED:
3402 gcc_unreachable ();
3403 case TYPE_POINTER:
3404 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
3405 wtd->subfield_marker_routine, cast, d->val);
3406 if (wtd->param_prefix)
3408 if (f->u.p->kind == TYPE_SCALAR)
3409 /* The current type is a pointer to a scalar (so not
3410 considered like a pointer to instances of user defined
3411 types) and we are seeing it; it means we must be even
3412 more careful about the second argument of the
3413 SUBFIELD_MARKER_ROUTINE call. That argument must
3414 always be the instance of the type for which
3415 write_func_for_structure was called - this really is
3416 what the function SUBFIELD_MARKER_ROUTINE expects.
3417 That is, it must be an instance of the ORIG_S type
3418 parameter of write_func_for_structure. The convention
3419 is that that argument must be "x" in that case (as set
3420 by write_func_for_structure). The problem is, we can't
3421 count on d->prev_val[3] to be always set to "x" in that
3422 case. Sometimes walk_type can set it to something else
3423 (to e.g cooperate with write_array when called from
3424 write_roots). So let's set it to "x" here then. */
3425 oprintf (d->of, ", x");
3426 else
3427 oprintf (d->of, ", %s", d->prev_val[3]);
3428 if (d->orig_s)
3430 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3431 output_mangled_typename (d->of, d->orig_s);
3433 else
3434 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3436 oprintf (d->of, ");\n");
3437 if (d->reorder_fn && wtd->reorder_note_routine)
3438 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
3439 wtd->reorder_note_routine, cast, d->val,
3440 d->prev_val[3], d->reorder_fn);
3441 break;
3443 case TYPE_STRING:
3444 case TYPE_STRUCT:
3445 case TYPE_UNION:
3446 case TYPE_LANG_STRUCT:
3447 case TYPE_PARAM_STRUCT:
3448 case TYPE_USER_STRUCT:
3449 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3451 /* If F is a user-defined type and the field is not a
3452 pointer to the type, then we should not generate the
3453 standard pointer-marking code. All we need to do is call
3454 the user-provided marking function to process the fields
3455 of F. */
3456 oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3457 d->val);
3459 else
3461 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3462 output_mangled_typename (d->of, f);
3463 oprintf (d->of, " (%s%s);\n", cast, d->val);
3464 if (d->reorder_fn && wtd->reorder_note_routine)
3465 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
3466 wtd->reorder_note_routine, cast, d->val, cast, d->val,
3467 d->reorder_fn);
3469 break;
3471 case TYPE_SCALAR:
3472 break;
3474 case TYPE_ARRAY:
3475 gcc_unreachable ();
3479 /* Return an output file that is suitable for definitions which can
3480 reference struct S */
3482 static outf_p
3483 get_output_file_for_structure (const_type_p s, type_p *param)
3485 const input_file *fn;
3486 int i;
3488 gcc_assert (union_or_struct_p (s));
3489 fn = s->u.s.line.file;
3491 /* This is a hack, and not the good kind either. */
3492 for (i = NUM_PARAM - 1; i >= 0; i--)
3493 if (param && param[i] && param[i]->kind == TYPE_POINTER
3494 && union_or_struct_p (param[i]->u.p))
3495 fn = param[i]->u.p->u.s.line.file;
3497 /* The call to get_output_file_with_visibility may update fn by
3498 caching its result inside, so we need the CONST_CAST. */
3499 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3503 /* Returns the specifier keyword for a string or union type S, empty string
3504 otherwise. */
3506 static const char *
3507 get_type_specifier (const type_p s)
3509 if (s->kind == TYPE_STRUCT)
3510 return "struct ";
3511 else if (s->kind == TYPE_LANG_STRUCT)
3512 return get_type_specifier (s->u.s.lang_struct);
3513 else if (s->kind == TYPE_UNION)
3514 return "union ";
3515 return "";
3519 /* Emits a declaration for type TY (assumed to be a union or a
3520 structure) on stream OUT. */
3522 static void
3523 write_type_decl (outf_p out, type_p ty)
3525 if (union_or_struct_p (ty))
3526 oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3527 else if (ty->kind == TYPE_SCALAR)
3529 if (ty->u.scalar_is_char)
3530 oprintf (out, "const char");
3531 else
3532 oprintf (out, "void");
3534 else if (ty->kind == TYPE_POINTER)
3536 write_type_decl (out, ty->u.p);
3537 oprintf (out, " *");
3539 else if (ty->kind == TYPE_ARRAY)
3541 write_type_decl (out, ty->u.a.p);
3542 oprintf (out, " *");
3544 else if (ty->kind == TYPE_STRING)
3546 oprintf (out, "const char *");
3548 else
3549 gcc_unreachable ();
3553 /* Write on OF the name of the marker function for structure S. PREFIX
3554 is the prefix to use (to distinguish ggc from pch markers). */
3556 static void
3557 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3559 if (union_or_struct_p (s))
3561 const char *id_for_tag = filter_type_name (s->u.s.tag);
3562 oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3563 if (id_for_tag != s->u.s.tag)
3564 free (CONST_CAST (char *, id_for_tag));
3566 else if (s->kind == TYPE_PARAM_STRUCT)
3568 oprintf (of, "gt_%s_", prefix);
3569 output_mangled_typename (of, s);
3571 else
3572 gcc_unreachable ();
3575 /* Write on OF a user-callable routine to act as an entry point for
3576 the marking routine for S, generated by write_func_for_structure.
3577 WTD distinguishes between ggc and pch markers. */
3579 static void
3580 write_user_func_for_structure_ptr (outf_p of, type_p s, const write_types_data *wtd)
3582 /* Parameterized structures are not supported in user markers. There
3583 is no way for the marker function to know which specific type
3584 to use to generate the call to the void * entry point. For
3585 instance, a marker for struct htab may need to call different
3586 routines to mark the fields, depending on the paramN_is attributes.
3588 A user-defined marker that accepts 'struct htab' as its argument
3589 would not know which variant to call. Generating several entry
3590 points accepting 'struct htab' would cause multiply-defined
3591 errors during compilation. */
3592 gcc_assert (union_or_struct_p (s));
3594 type_p alias_of = NULL;
3595 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3596 if (strcmp (opt->name, "ptr_alias") == 0)
3598 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3599 we do not generate marking code for ORIG_S here. Instead, a
3600 forwarder #define in gtype-desc.h will cause every call to its
3601 marker to call the target of this alias.
3603 However, we still want to create a user entry code for the
3604 aliased type. So, if ALIAS_OF is set, we only generate the
3605 user-callable marker function. */
3606 alias_of = opt->info.type;
3607 break;
3610 DBGPRINTF ("write_user_func_for_structure_ptr: %s %s", s->u.s.tag,
3611 wtd->prefix);
3613 /* Only write the function once. */
3614 if (s->u.s.wrote_user_func_for_ptr[wtd->kind])
3615 return;
3616 s->u.s.wrote_user_func_for_ptr[wtd->kind] = true;
3618 oprintf (of, "\nvoid\n");
3619 oprintf (of, "gt_%sx (", wtd->prefix);
3620 write_type_decl (of, s);
3621 oprintf (of, " *& x)\n");
3622 oprintf (of, "{\n");
3623 oprintf (of, " if (x)\n ");
3624 write_marker_function_name (of,
3625 alias_of ? alias_of : get_ultimate_base_class (s),
3626 wtd->prefix);
3627 oprintf (of, " ((void *) x);\n");
3628 oprintf (of, "}\n");
3632 /* Write a function to mark all the fields of type S on OF. PREFIX
3633 and D are as in write_user_marking_functions. */
3635 static void
3636 write_user_func_for_structure_body (type_p s, const char *prefix,
3637 struct walk_type_data *d)
3639 oprintf (d->of, "\nvoid\n");
3640 oprintf (d->of, "gt_%sx (", prefix);
3641 write_type_decl (d->of, s);
3642 oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3643 oprintf (d->of, "{\n");
3644 oprintf (d->of, " ");
3645 write_type_decl (d->of, s);
3646 oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3647 d->val = "(*x)";
3648 d->indent = 2;
3649 walk_type (s, d);
3650 oprintf (d->of, "}\n");
3653 /* Emit the user-callable functions needed to mark all the types used
3654 by the user structure S. PREFIX is the prefix to use to
3655 distinguish ggc and pch markers. D contains data needed to pass to
3656 walk_type when traversing the fields of a type.
3658 For every type T referenced by S, two routines are generated: one
3659 that takes 'T *', marks the pointer and calls the second routine,
3660 which just marks the fields of T. */
3662 static void
3663 write_user_marking_functions (type_p s,
3664 const write_types_data *w,
3665 struct walk_type_data *d)
3667 gcc_assert (s->kind == TYPE_USER_STRUCT);
3669 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3671 type_p fld_type = fld->type;
3672 if (fld_type->kind == TYPE_POINTER)
3674 type_p pointed_to_type = fld_type->u.p;
3675 if (union_or_struct_p (pointed_to_type))
3676 write_user_func_for_structure_ptr (d->of, pointed_to_type, w);
3678 else if (union_or_struct_p (fld_type))
3679 write_user_func_for_structure_body (fld_type, w->prefix, d);
3684 /* For S, a structure that's part of ORIG_S, and using parameters
3685 PARAM, write out a routine that:
3686 - Takes a parameter, a void * but actually of type *S
3687 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3688 field of S or its substructures and (in some cases) things
3689 that are pointed to by S. */
3691 static void
3692 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
3693 const struct write_types_data *wtd)
3695 const char *chain_next = NULL;
3696 const char *chain_prev = NULL;
3697 const char *chain_circular = NULL;
3698 const char *mark_hook_name = NULL;
3699 options_p opt;
3700 struct walk_type_data d;
3702 if (s->u.s.base_class)
3704 /* Verify that the base class has a "desc", since otherwise
3705 the traversal hooks there won't attempt to visit fields of
3706 subclasses such as this one. */
3707 const_type_p ubc = get_ultimate_base_class (s);
3708 if ((!opts_have (ubc->u.s.opt, "user")
3709 && !opts_have (ubc->u.s.opt, "desc")))
3710 error_at_line (&s->u.s.line,
3711 ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
3712 ", but '%s' lacks a discriminator 'desc' option"),
3713 s->u.s.tag, ubc->u.s.tag, ubc->u.s.tag);
3715 /* Don't write fns for subclasses, only for the ultimate base class
3716 within an inheritance hierarchy. */
3717 return;
3720 memset (&d, 0, sizeof (d));
3721 d.of = get_output_file_for_structure (s, param);
3723 bool for_user = false;
3724 for (opt = s->u.s.opt; opt; opt = opt->next)
3725 if (strcmp (opt->name, "chain_next") == 0
3726 && opt->kind == OPTION_STRING)
3727 chain_next = opt->info.string;
3728 else if (strcmp (opt->name, "chain_prev") == 0
3729 && opt->kind == OPTION_STRING)
3730 chain_prev = opt->info.string;
3731 else if (strcmp (opt->name, "chain_circular") == 0
3732 && opt->kind == OPTION_STRING)
3733 chain_circular = opt->info.string;
3734 else if (strcmp (opt->name, "mark_hook") == 0
3735 && opt->kind == OPTION_STRING)
3736 mark_hook_name = opt->info.string;
3737 else if (strcmp (opt->name, "for_user") == 0)
3738 for_user = true;
3739 if (chain_prev != NULL && chain_next == NULL)
3740 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3741 if (chain_circular != NULL && chain_next != NULL)
3742 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3743 if (chain_circular != NULL)
3744 chain_next = chain_circular;
3746 d.process_field = write_types_process_field;
3747 d.cookie = wtd;
3748 d.orig_s = orig_s;
3749 d.opt = s->u.s.opt;
3750 d.line = &s->u.s.line;
3751 d.bitmap = s->u.s.bitmap;
3752 d.param = param;
3753 d.prev_val[0] = "*x";
3754 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3755 d.prev_val[3] = "x";
3756 d.val = "(*x)";
3757 d.have_this_obj = false;
3759 oprintf (d.of, "\n");
3760 oprintf (d.of, "void\n");
3761 write_marker_function_name (d.of, orig_s, wtd->prefix);
3762 oprintf (d.of, " (void *x_p)\n");
3763 oprintf (d.of, "{\n ");
3764 write_type_decl (d.of, s);
3765 oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3766 write_type_decl (d.of, s);
3767 oprintf (d.of, " *)x_p;\n");
3768 if (chain_next != NULL)
3770 /* TYPE_USER_STRUCTs should not occur here. These structures
3771 are completely handled by user code. */
3772 gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
3774 oprintf (d.of, " ");
3775 write_type_decl (d.of, s);
3776 oprintf (d.of, " * xlimit = x;\n");
3778 if (chain_next == NULL)
3780 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3781 if (wtd->param_prefix)
3783 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3784 output_mangled_typename (d.of, orig_s);
3786 oprintf (d.of, "))\n");
3788 else
3790 if (chain_circular != NULL)
3791 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3792 else
3793 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3794 if (wtd->param_prefix)
3796 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3797 output_mangled_typename (d.of, orig_s);
3799 oprintf (d.of, "))\n");
3800 if (chain_circular != NULL)
3801 oprintf (d.of, " return;\n do\n");
3802 if (mark_hook_name && !wtd->skip_hooks)
3804 oprintf (d.of, " {\n");
3805 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
3807 oprintf (d.of, " xlimit = (");
3808 d.prev_val[2] = "*xlimit";
3809 output_escaped_param (&d, chain_next, "chain_next");
3810 oprintf (d.of, ");\n");
3811 if (mark_hook_name && !wtd->skip_hooks)
3812 oprintf (d.of, " }\n");
3813 if (chain_prev != NULL)
3815 oprintf (d.of, " if (x != xlimit)\n");
3816 oprintf (d.of, " for (;;)\n");
3817 oprintf (d.of, " {\n");
3818 oprintf (d.of, " %s %s * const xprev = (",
3819 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3821 d.prev_val[2] = "*x";
3822 output_escaped_param (&d, chain_prev, "chain_prev");
3823 oprintf (d.of, ");\n");
3824 oprintf (d.of, " if (xprev == NULL) break;\n");
3825 oprintf (d.of, " x = xprev;\n");
3826 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3827 if (wtd->param_prefix)
3829 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3830 output_mangled_typename (d.of, orig_s);
3832 oprintf (d.of, ");\n");
3833 oprintf (d.of, " }\n");
3835 if (chain_circular != NULL)
3837 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3838 if (wtd->param_prefix)
3840 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3841 output_mangled_typename (d.of, orig_s);
3843 oprintf (d.of, "));\n");
3844 if (mark_hook_name && !wtd->skip_hooks)
3845 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
3846 oprintf (d.of, " do\n");
3848 else
3849 oprintf (d.of, " while (x != xlimit)\n");
3851 oprintf (d.of, " {\n");
3852 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3854 oprintf (d.of, " %s (x);\n", mark_hook_name);
3857 d.prev_val[2] = "*x";
3858 d.indent = 6;
3859 if (orig_s->kind != TYPE_USER_STRUCT)
3860 walk_type (s, &d);
3861 else
3863 /* User structures have no fields to walk. Simply generate a call
3864 to the user-provided structure marker. */
3865 oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3868 if (chain_next != NULL)
3870 oprintf (d.of, " x = (");
3871 output_escaped_param (&d, chain_next, "chain_next");
3872 oprintf (d.of, ");\n");
3875 oprintf (d.of, " }\n");
3876 if (chain_circular != NULL)
3877 oprintf (d.of, " while (x != xlimit);\n");
3878 oprintf (d.of, "}\n");
3880 if (orig_s->kind == TYPE_USER_STRUCT)
3881 write_user_marking_functions (orig_s, wtd, &d);
3883 if (for_user)
3885 write_user_func_for_structure_body (orig_s, wtd->prefix, &d);
3886 write_user_func_for_structure_ptr (d.of, orig_s, wtd);
3891 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3893 static void
3894 write_types (outf_p output_header, type_p structures, type_p param_structs,
3895 const struct write_types_data *wtd)
3897 int nbfun = 0; /* Count the emitted functions. */
3898 type_p s;
3900 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3902 /* We first emit the macros and the declarations. Functions' code is
3903 emitted afterwards. This is needed in plugin mode. */
3904 oprintf (output_header, "/* Macros and declarations. */\n");
3905 for (s = structures; s; s = s->next)
3906 /* Do not emit handlers for derived classes; we only ever deal with
3907 the ultimate base class within an inheritance hierarchy. */
3908 if ((s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3909 && !s->u.s.base_class)
3911 options_p opt;
3913 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3914 continue;
3916 const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3918 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3919 output_mangled_typename (output_header, s);
3920 oprintf (output_header, "(X) do { \\\n");
3921 oprintf (output_header,
3922 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3923 s_id_for_tag);
3924 oprintf (output_header, " } while (0)\n");
3926 for (opt = s->u.s.opt; opt; opt = opt->next)
3927 if (strcmp (opt->name, "ptr_alias") == 0
3928 && opt->kind == OPTION_TYPE)
3930 const_type_p const t = (const_type_p) opt->info.type;
3931 if (t->kind == TYPE_STRUCT
3932 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3934 const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3935 oprintf (output_header,
3936 "#define gt_%sx_%s gt_%sx_%s\n",
3937 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3938 if (t_id_for_tag != t->u.s.tag)
3939 free (CONST_CAST (char *, t_id_for_tag));
3941 else
3942 error_at_line (&s->u.s.line,
3943 "structure alias is not a structure");
3944 break;
3946 if (opt)
3947 continue;
3949 /* Declare the marker procedure only once. */
3950 oprintf (output_header,
3951 "extern void gt_%sx_%s (void *);\n",
3952 wtd->prefix, s_id_for_tag);
3954 if (s_id_for_tag != s->u.s.tag)
3955 free (CONST_CAST (char *, s_id_for_tag));
3957 if (s->u.s.line.file == NULL)
3959 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3960 s->u.s.tag);
3961 continue;
3965 for (s = param_structs; s; s = s->next)
3966 if (s->gc_used == GC_POINTED_TO)
3968 type_p stru = s->u.param_struct.stru;
3970 /* Declare the marker procedure. */
3971 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
3972 output_mangled_typename (output_header, s);
3973 oprintf (output_header, " (void *);\n");
3975 if (stru->u.s.line.file == NULL)
3977 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3978 stru->u.s.tag);
3979 continue;
3983 /* At last we emit the functions code. */
3984 oprintf (output_header, "\n/* functions code */\n");
3985 for (s = structures; s; s = s->next)
3986 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3988 options_p opt;
3990 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3991 continue;
3992 for (opt = s->u.s.opt; opt; opt = opt->next)
3993 if (strcmp (opt->name, "ptr_alias") == 0)
3994 break;
3995 if (opt)
3996 continue;
3998 if (s->kind == TYPE_LANG_STRUCT)
4000 type_p ss;
4001 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4003 nbfun++;
4004 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
4005 nbfun, (void*) ss, ss->u.s.tag);
4006 write_func_for_structure (s, ss, NULL, wtd);
4009 else
4011 nbfun++;
4012 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
4013 nbfun, (void*) s, s->u.s.tag);
4014 write_func_for_structure (s, s, NULL, wtd);
4017 else
4019 /* Structure s is not possibly pointed to, so can be ignored. */
4020 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
4021 (void*)s, s->u.s.tag,
4022 (int) s->gc_used);
4025 for (s = param_structs; s; s = s->next)
4026 if (s->gc_used == GC_POINTED_TO)
4028 type_p *param = s->u.param_struct.param;
4029 type_p stru = s->u.param_struct.stru;
4030 if (stru->u.s.line.file == NULL)
4031 continue;
4032 if (stru->kind == TYPE_LANG_STRUCT)
4034 type_p ss;
4035 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
4037 nbfun++;
4038 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
4039 nbfun, (void*) ss, ss->u.s.tag);
4040 write_func_for_structure (s, ss, param, wtd);
4043 else
4045 nbfun++;
4046 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
4047 nbfun, (void*) s,
4048 (void*) stru, stru->u.s.tag);
4049 write_func_for_structure (s, stru, param, wtd);
4052 else
4054 /* Param structure s is not pointed to, so should be ignored. */
4055 DBGPRINTF ("ignored s @ %p", (void*)s);
4057 if (verbosity_level >= 2)
4058 printf ("%s emitted %d routines for %s\n",
4059 progname, nbfun, wtd->comment);
4062 static const struct write_types_data ggc_wtd = {
4063 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
4064 "GC marker procedures. ",
4065 FALSE, WTK_GGC
4068 static const struct write_types_data pch_wtd = {
4069 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
4070 "gt_pch_note_reorder",
4071 "PCH type-walking procedures. ",
4072 TRUE, WTK_PCH
4075 /* Write out the local pointer-walking routines. */
4077 /* process_field routine for local pointer-walking for user-callable
4078 routines. The difference between this and
4079 write_types_local_process_field is that, in this case, we do not
4080 need to check whether the given pointer matches the address of the
4081 parent structure. This check was already generated by the call
4082 to gt_pch_nx in the main gt_pch_p_*() function that is calling
4083 this code. */
4085 static void
4086 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
4088 switch (f->kind)
4090 case TYPE_POINTER:
4091 case TYPE_STRUCT:
4092 case TYPE_UNION:
4093 case TYPE_LANG_STRUCT:
4094 case TYPE_PARAM_STRUCT:
4095 case TYPE_STRING:
4096 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4097 break;
4099 case TYPE_USER_STRUCT:
4100 if (d->in_ptr_field)
4101 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4102 else
4103 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4104 d->indent, "", d->val);
4105 break;
4107 case TYPE_SCALAR:
4108 break;
4110 case TYPE_ARRAY:
4111 case TYPE_NONE:
4112 case TYPE_UNDEFINED:
4113 gcc_unreachable ();
4118 /* Write a function to PCH walk all the fields of type S on OF.
4119 D contains data needed by walk_type to recurse into the fields of S. */
4121 static void
4122 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
4124 oprintf (d->of, "\nvoid\n");
4125 oprintf (d->of, "gt_pch_nx (");
4126 write_type_decl (d->of, s);
4127 oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
4128 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4129 "\tATTRIBUTE_UNUSED void *cookie)\n");
4130 oprintf (d->of, "{\n");
4131 d->val = "(*x)";
4132 d->indent = 2;
4133 d->process_field = write_types_local_user_process_field;
4134 walk_type (s, d);
4135 oprintf (d->of, "}\n");
4139 /* Emit the user-callable functions needed to mark all the types used
4140 by the user structure S. PREFIX is the prefix to use to
4141 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
4142 chain_next option defined. D contains data needed to pass to
4143 walk_type when traversing the fields of a type.
4145 For every type T referenced by S, two routines are generated: one
4146 that takes 'T *', marks the pointer and calls the second routine,
4147 which just marks the fields of T. */
4149 static void
4150 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
4152 gcc_assert (s->kind == TYPE_USER_STRUCT);
4154 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
4156 type_p fld_type = fld->type;
4157 if (union_or_struct_p (fld_type))
4158 write_pch_user_walking_for_structure_body (fld_type, d);
4163 /* process_field routine for local pointer-walking. */
4165 static void
4166 write_types_local_process_field (type_p f, const struct walk_type_data *d)
4168 gcc_assert (d->have_this_obj);
4169 switch (f->kind)
4171 case TYPE_POINTER:
4172 case TYPE_STRUCT:
4173 case TYPE_UNION:
4174 case TYPE_LANG_STRUCT:
4175 case TYPE_PARAM_STRUCT:
4176 case TYPE_STRING:
4177 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
4178 d->prev_val[3]);
4179 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4180 break;
4182 case TYPE_USER_STRUCT:
4183 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
4184 d->prev_val[3]);
4185 if (d->in_ptr_field)
4186 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4187 else
4188 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4189 d->indent, "", d->val);
4190 break;
4192 case TYPE_SCALAR:
4193 break;
4195 case TYPE_ARRAY:
4196 case TYPE_NONE:
4197 case TYPE_UNDEFINED:
4198 gcc_unreachable ();
4203 /* For S, a structure that's part of ORIG_S, and using parameters
4204 PARAM, write out a routine that:
4205 - Is of type gt_note_pointers
4206 - Calls PROCESS_FIELD on each field of S or its substructures.
4209 static void
4210 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
4212 struct walk_type_data d;
4214 /* Don't write fns for subclasses, only for the ultimate base class
4215 within an inheritance hierarchy. */
4216 if (s->u.s.base_class)
4217 return;
4219 memset (&d, 0, sizeof (d));
4220 d.of = get_output_file_for_structure (s, param);
4221 d.process_field = write_types_local_process_field;
4222 d.opt = s->u.s.opt;
4223 d.line = &s->u.s.line;
4224 d.bitmap = s->u.s.bitmap;
4225 d.param = param;
4226 d.prev_val[0] = d.prev_val[2] = "*x";
4227 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
4228 d.prev_val[3] = "x";
4229 d.val = "(*x)";
4230 d.fn_wants_lvalue = true;
4232 oprintf (d.of, "\n");
4233 oprintf (d.of, "void\n");
4234 oprintf (d.of, "gt_pch_p_");
4235 output_mangled_typename (d.of, orig_s);
4236 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
4237 "\tvoid *x_p,\n"
4238 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4239 "\tATTRIBUTE_UNUSED void *cookie)\n");
4240 oprintf (d.of, "{\n");
4241 oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
4242 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
4243 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
4244 d.indent = 2;
4245 d.have_this_obj = true;
4247 if (s->kind != TYPE_USER_STRUCT)
4248 walk_type (s, &d);
4249 else
4251 /* User structures have no fields to walk. Simply generate a
4252 call to the user-provided PCH walker. */
4253 oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
4254 d.prev_val[3]);
4255 oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4256 d.indent, "", d.val);
4259 oprintf (d.of, "}\n");
4261 /* Write user-callable entry points for the PCH walking routines. */
4262 if (orig_s->kind == TYPE_USER_STRUCT)
4263 write_pch_user_walking_functions (s, &d);
4265 for (options_p o = s->u.s.opt; o; o = o->next)
4266 if (strcmp (o->name, "for_user") == 0)
4268 write_pch_user_walking_for_structure_body (s, &d);
4269 break;
4273 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
4275 static void
4276 write_local (outf_p output_header, type_p structures, type_p param_structs)
4278 type_p s;
4280 if (!output_header)
4281 return;
4283 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
4284 for (s = structures; s; s = s->next)
4285 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
4287 options_p opt;
4289 if (s->u.s.line.file == NULL)
4290 continue;
4291 for (opt = s->u.s.opt; opt; opt = opt->next)
4292 if (strcmp (opt->name, "ptr_alias") == 0
4293 && opt->kind == OPTION_TYPE)
4295 const_type_p const t = (const_type_p) opt->info.type;
4296 if (t->kind == TYPE_STRUCT
4297 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
4299 oprintf (output_header, "#define gt_pch_p_");
4300 output_mangled_typename (output_header, s);
4301 oprintf (output_header, " gt_pch_p_");
4302 output_mangled_typename (output_header, t);
4303 oprintf (output_header, "\n");
4305 else
4306 error_at_line (&s->u.s.line,
4307 "structure alias is not a structure");
4308 break;
4310 if (opt)
4311 continue;
4313 /* Declare the marker procedure only once. */
4314 oprintf (output_header, "extern void gt_pch_p_");
4315 output_mangled_typename (output_header, s);
4316 oprintf (output_header,
4317 "\n (void *, void *, gt_pointer_operator, void *);\n");
4319 if (s->kind == TYPE_LANG_STRUCT)
4321 type_p ss;
4322 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4323 write_local_func_for_structure (s, ss, NULL);
4325 else
4326 write_local_func_for_structure (s, s, NULL);
4329 for (s = param_structs; s; s = s->next)
4330 if (s->gc_used == GC_POINTED_TO)
4332 type_p *param = s->u.param_struct.param;
4333 type_p stru = s->u.param_struct.stru;
4335 /* Declare the marker procedure. */
4336 oprintf (output_header, "extern void gt_pch_p_");
4337 output_mangled_typename (output_header, s);
4338 oprintf (output_header,
4339 "\n (void *, void *, gt_pointer_operator, void *);\n");
4341 if (stru->u.s.line.file == NULL)
4343 fprintf (stderr, "warning: structure `%s' used but not defined\n",
4344 stru->u.s.tag);
4345 continue;
4348 if (stru->kind == TYPE_LANG_STRUCT)
4350 type_p ss;
4351 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
4352 write_local_func_for_structure (s, ss, param);
4354 else
4355 write_local_func_for_structure (s, stru, param);
4359 /* Nonzero if S is a type for which typed GC allocators should be output. */
4361 #define USED_BY_TYPED_GC_P(s) \
4362 ((s->kind == TYPE_POINTER \
4363 && (s->u.p->gc_used == GC_POINTED_TO \
4364 || s->u.p->gc_used == GC_USED)) \
4365 || (union_or_struct_p (s) \
4366 && ((s)->gc_used == GC_POINTED_TO \
4367 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4368 && s->u.s.line.file != NULL) \
4369 || ((s)->gc_used == GC_USED \
4370 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))) \
4371 || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
4375 /* Might T contain any non-pointer elements? */
4377 static int
4378 contains_scalar_p (type_p t)
4380 switch (t->kind)
4382 case TYPE_STRING:
4383 case TYPE_POINTER:
4384 return 0;
4385 case TYPE_ARRAY:
4386 return contains_scalar_p (t->u.a.p);
4387 case TYPE_USER_STRUCT:
4388 /* User-marked structures will typically contain pointers. */
4389 return 0;
4390 default:
4391 /* Could also check for structures that have no non-pointer
4392 fields, but there aren't enough of those to worry about. */
4393 return 1;
4397 /* Mangle INPF and print it to F. */
4399 static void
4400 put_mangled_filename (outf_p f, const input_file *inpf)
4402 /* The call to get_output_file_name may indirectly update fn since
4403 get_output_file_with_visibility caches its result inside, so we
4404 need the CONST_CAST. */
4405 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4406 if (!f || !name)
4407 return;
4408 for (; *name != 0; name++)
4409 if (ISALNUM (*name))
4410 oprintf (f, "%c", *name);
4411 else
4412 oprintf (f, "%c", '_');
4415 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4416 LASTNAME, and NAME are all strings to insert in various places in
4417 the resulting code. */
4419 static void
4420 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4421 const char *tname, const char *name)
4423 struct flist *fli2;
4425 for (fli2 = flp; fli2; fli2 = fli2->next)
4426 if (fli2->started_p)
4428 oprintf (fli2->f, " %s\n", lastname);
4429 oprintf (fli2->f, "};\n\n");
4432 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4433 if (fli2->started_p)
4435 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4436 int fnum;
4438 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4439 if (bitmap & 1)
4441 oprintf (base_files[fnum],
4442 "extern const struct %s gt_%s_", tname, pfx);
4443 put_mangled_filename (base_files[fnum], fli2->file);
4444 oprintf (base_files[fnum], "[];\n");
4449 size_t fnum;
4450 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4451 oprintf (base_files[fnum],
4452 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4456 for (fli2 = flp; fli2; fli2 = fli2->next)
4457 if (fli2->started_p)
4459 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4460 int fnum;
4462 fli2->started_p = 0;
4464 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4465 if (bitmap & 1)
4467 oprintf (base_files[fnum], " gt_%s_", pfx);
4468 put_mangled_filename (base_files[fnum], fli2->file);
4469 oprintf (base_files[fnum], ",\n");
4474 size_t fnum;
4475 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4477 oprintf (base_files[fnum], " NULL\n");
4478 oprintf (base_files[fnum], "};\n");
4483 /* Write the first three fields (pointer, count and stride) for
4484 root NAME to F. V and LINE are as for write_root.
4486 Return true if the entry could be written; return false on error. */
4488 static bool
4489 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4491 type_p ap;
4493 if (!v)
4495 error_at_line (line, "`%s' is too complex to be a root", name);
4496 return false;
4499 oprintf (f, " {\n");
4500 oprintf (f, " &%s,\n", name);
4501 oprintf (f, " 1");
4503 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4504 if (ap->u.a.len[0])
4505 oprintf (f, " * (%s)", ap->u.a.len);
4506 else if (ap == v->type)
4507 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4508 oprintf (f, ",\n");
4509 oprintf (f, " sizeof (%s", v->name);
4510 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4511 oprintf (f, "[0]");
4512 oprintf (f, "),\n");
4513 return true;
4516 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4517 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4518 of the caller. */
4520 static void
4521 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4522 int has_length, struct fileloc *line, const char *if_marked,
4523 bool emit_pch, type_p field_type, const char *field_name)
4525 struct pair newv;
4526 /* If the field reference is relative to V, rather than to some
4527 subcomponent of V, we can mark any subarrays with a single stride.
4528 We're effectively treating the field as a global variable in its
4529 own right. */
4530 if (v && type == v->type)
4532 newv = *v;
4533 newv.type = field_type;
4534 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4535 v = &newv;
4537 /* Otherwise, any arrays nested in the structure are too complex to
4538 handle. */
4539 else if (field_type->kind == TYPE_ARRAY)
4540 v = NULL;
4541 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4542 has_length, line, if_marked, emit_pch);
4545 /* Write out to F the table entry and any marker routines needed to
4546 mark NAME as TYPE. V can be one of three values:
4548 - null, if NAME is too complex to represent using a single
4549 count and stride. In this case, it is an error for NAME to
4550 contain any gc-ed data.
4552 - the outermost array that contains NAME, if NAME is part of an array.
4554 - the C variable that contains NAME, if NAME is not part of an array.
4556 LINE is the line of the C source that declares the root variable.
4557 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
4558 is nonzero iff we are building the root table for hash table caches. */
4560 static void
4561 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4562 struct fileloc *line, const char *if_marked, bool emit_pch)
4564 switch (type->kind)
4566 case TYPE_STRUCT:
4568 pair_p fld;
4569 for (fld = type->u.s.fields; fld; fld = fld->next)
4571 int skip_p = 0;
4572 const char *desc = NULL;
4573 options_p o;
4575 for (o = fld->opt; o; o = o->next)
4576 if (strcmp (o->name, "skip") == 0)
4577 skip_p = 1;
4578 else if (strcmp (o->name, "desc") == 0
4579 && o->kind == OPTION_STRING)
4580 desc = o->info.string;
4581 else if (strcmp (o->name, "param_is") == 0)
4583 else
4584 error_at_line (line,
4585 "field `%s' of global `%s' has unknown option `%s'",
4586 fld->name, name, o->name);
4588 if (skip_p)
4589 continue;
4590 else if (desc && fld->type->kind == TYPE_UNION)
4592 pair_p validf = NULL;
4593 pair_p ufld;
4595 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4597 const char *tag = NULL;
4598 options_p oo;
4599 for (oo = ufld->opt; oo; oo = oo->next)
4600 if (strcmp (oo->name, "tag") == 0
4601 && oo->kind == OPTION_STRING)
4602 tag = oo->info.string;
4603 if (tag == NULL || strcmp (tag, desc) != 0)
4604 continue;
4605 if (validf != NULL)
4606 error_at_line (line,
4607 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4608 name, fld->name, validf->name,
4609 name, fld->name, ufld->name, tag);
4610 validf = ufld;
4612 if (validf != NULL)
4613 write_field_root (f, v, type, name, 0, line, if_marked,
4614 emit_pch, validf->type,
4615 ACONCAT ((fld->name, ".",
4616 validf->name, NULL)));
4618 else if (desc)
4619 error_at_line (line,
4620 "global `%s.%s' has `desc' option but is not union",
4621 name, fld->name);
4622 else
4623 write_field_root (f, v, type, name, 0, line, if_marked,
4624 emit_pch, fld->type, fld->name);
4627 break;
4629 case TYPE_ARRAY:
4631 char *newname;
4632 newname = xasprintf ("%s[0]", name);
4633 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
4634 emit_pch);
4635 free (newname);
4637 break;
4639 case TYPE_USER_STRUCT:
4640 error_at_line (line, "`%s' must be a pointer type, because it is "
4641 "a GC root and its type is marked with GTY((user))",
4642 v->name);
4643 break;
4645 case TYPE_POINTER:
4647 const_type_p tp;
4649 if (!start_root_entry (f, v, name, line))
4650 return;
4652 tp = type->u.p;
4654 if (!has_length && union_or_struct_p (tp))
4656 tp = get_ultimate_base_class (tp);
4657 const char *id_for_tag = filter_type_name (tp->u.s.tag);
4658 oprintf (f, " &gt_ggc_mx_%s,\n", id_for_tag);
4659 if (emit_pch)
4660 oprintf (f, " &gt_pch_nx_%s", id_for_tag);
4661 else
4662 oprintf (f, " NULL");
4663 if (id_for_tag != tp->u.s.tag)
4664 free (CONST_CAST (char *, id_for_tag));
4666 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
4668 oprintf (f, " &gt_ggc_m_");
4669 output_mangled_typename (f, tp);
4670 if (emit_pch)
4672 oprintf (f, ",\n &gt_pch_n_");
4673 output_mangled_typename (f, tp);
4675 else
4676 oprintf (f, ",\n NULL");
4678 else if (has_length
4679 && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
4681 oprintf (f, " &gt_ggc_ma_%s,\n", name);
4682 if (emit_pch)
4683 oprintf (f, " &gt_pch_na_%s", name);
4684 else
4685 oprintf (f, " NULL");
4687 else
4689 error_at_line (line,
4690 "global `%s' is pointer to unimplemented type",
4691 name);
4693 if (if_marked)
4694 oprintf (f, ",\n &%s", if_marked);
4695 oprintf (f, "\n },\n");
4697 break;
4699 case TYPE_STRING:
4701 if (!start_root_entry (f, v, name, line))
4702 return;
4704 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
4705 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
4706 oprintf (f, " },\n");
4708 break;
4710 case TYPE_SCALAR:
4711 break;
4713 case TYPE_NONE:
4714 case TYPE_UNDEFINED:
4715 case TYPE_UNION:
4716 case TYPE_LANG_STRUCT:
4717 case TYPE_PARAM_STRUCT:
4718 error_at_line (line, "global `%s' is unimplemented type", name);
4722 /* This generates a routine to walk an array. */
4724 static void
4725 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4727 struct walk_type_data d;
4728 char *prevval3;
4730 memset (&d, 0, sizeof (d));
4731 d.of = f;
4732 d.cookie = wtd;
4733 d.indent = 2;
4734 d.line = &v->line;
4735 d.opt = v->opt;
4736 d.bitmap = get_lang_bitmap (v->line.file);
4737 d.param = NULL;
4739 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4741 if (wtd->param_prefix)
4743 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4744 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
4745 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4746 wtd->param_prefix, v->name);
4747 oprintf (d.of,
4748 " ATTRIBUTE_UNUSED void *x_p,\n"
4749 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4750 " ATTRIBUTE_UNUSED void * cookie)\n");
4751 oprintf (d.of, "{\n");
4752 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4753 d.process_field = write_types_local_process_field;
4754 d.have_this_obj = true;
4755 walk_type (v->type, &d);
4756 oprintf (f, "}\n\n");
4759 d.opt = v->opt;
4760 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4761 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4762 wtd->prefix, v->name);
4763 oprintf (f, "{\n");
4764 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4765 d.process_field = write_types_process_field;
4766 d.have_this_obj = false;
4767 walk_type (v->type, &d);
4768 free (prevval3);
4769 oprintf (f, "}\n\n");
4772 /* Output a table describing the locations and types of VARIABLES. */
4774 static void
4775 write_roots (pair_p variables, bool emit_pch)
4777 pair_p v;
4778 struct flist *flp = NULL;
4780 for (v = variables; v; v = v->next)
4782 outf_p f =
4783 get_output_file_with_visibility (CONST_CAST (input_file*,
4784 v->line.file));
4785 struct flist *fli;
4786 const char *length = NULL;
4787 int deletable_p = 0;
4788 options_p o;
4789 for (o = v->opt; o; o = o->next)
4790 if (strcmp (o->name, "length") == 0
4791 && o->kind == OPTION_STRING)
4792 length = o->info.string;
4793 else if (strcmp (o->name, "deletable") == 0)
4794 deletable_p = 1;
4795 else if (strcmp (o->name, "param_is") == 0)
4797 else if (strncmp (o->name, "param", 5) == 0
4798 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
4800 else if (strcmp (o->name, "if_marked") == 0)
4802 else
4803 error_at_line (&v->line,
4804 "global `%s' has unknown option `%s'",
4805 v->name, o->name);
4807 for (fli = flp; fli; fli = fli->next)
4808 if (fli->f == f && f)
4809 break;
4810 if (fli == NULL)
4812 fli = XNEW (struct flist);
4813 fli->f = f;
4814 fli->next = flp;
4815 fli->started_p = 0;
4816 fli->file = v->line.file;
4817 gcc_assert (fli->file);
4818 flp = fli;
4820 oprintf (f, "\n/* GC roots. */\n\n");
4823 if (!deletable_p
4824 && length
4825 && v->type->kind == TYPE_POINTER
4826 && (v->type->u.p->kind == TYPE_POINTER
4827 || v->type->u.p->kind == TYPE_STRUCT))
4829 write_array (f, v, &ggc_wtd);
4830 write_array (f, v, &pch_wtd);
4834 for (v = variables; v; v = v->next)
4836 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4837 v->line.file));
4838 struct flist *fli;
4839 int skip_p = 0;
4840 int length_p = 0;
4841 options_p o;
4843 for (o = v->opt; o; o = o->next)
4844 if (strcmp (o->name, "length") == 0)
4845 length_p = 1;
4846 else if (strcmp (o->name, "deletable") == 0
4847 || strcmp (o->name, "if_marked") == 0)
4848 skip_p = 1;
4850 if (skip_p)
4851 continue;
4853 for (fli = flp; fli; fli = fli->next)
4854 if (fli->f == f)
4855 break;
4856 if (!fli->started_p)
4858 fli->started_p = 1;
4860 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4861 put_mangled_filename (f, v->line.file);
4862 oprintf (f, "[] = {\n");
4865 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4868 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4869 "gt_ggc_rtab");
4871 for (v = variables; v; v = v->next)
4873 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4874 v->line.file));
4875 struct flist *fli;
4876 int skip_p = 1;
4877 options_p o;
4879 for (o = v->opt; o; o = o->next)
4880 if (strcmp (o->name, "deletable") == 0)
4881 skip_p = 0;
4882 else if (strcmp (o->name, "if_marked") == 0)
4883 skip_p = 1;
4885 if (skip_p)
4886 continue;
4888 for (fli = flp; fli; fli = fli->next)
4889 if (fli->f == f)
4890 break;
4891 if (!fli->started_p)
4893 fli->started_p = 1;
4895 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4896 put_mangled_filename (f, v->line.file);
4897 oprintf (f, "[] = {\n");
4900 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4901 v->name, v->name);
4904 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4905 "gt_ggc_deletable_rtab");
4907 for (v = variables; v; v = v->next)
4909 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4910 v->line.file));
4911 struct flist *fli;
4912 const char *if_marked = NULL;
4913 int length_p = 0;
4914 options_p o;
4916 for (o = v->opt; o; o = o->next)
4917 if (strcmp (o->name, "length") == 0)
4918 length_p = 1;
4919 else if (strcmp (o->name, "if_marked") == 0
4920 && o->kind == OPTION_STRING)
4921 if_marked = o->info.string;
4922 if (if_marked == NULL)
4923 continue;
4924 if (v->type->kind != TYPE_POINTER
4925 || v->type->u.p->kind != TYPE_PARAM_STRUCT
4926 || v->type->u.p->u.param_struct.stru != find_structure ("htab",
4927 TYPE_STRUCT))
4929 error_at_line (&v->line,
4930 "if_marked option used but not hash table");
4931 continue;
4934 for (fli = flp; fli; fli = fli->next)
4935 if (fli->f == f)
4936 break;
4937 if (!fli->started_p)
4939 fli->started_p = 1;
4941 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4942 put_mangled_filename (f, v->line.file);
4943 oprintf (f, "[] = {\n");
4946 write_root (f, v, v->type->u.p->u.param_struct.param[0],
4947 v->name, length_p, &v->line, if_marked, emit_pch);
4950 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4951 "gt_ggc_cache_rtab");
4953 if (!emit_pch)
4954 return;
4956 for (v = variables; v; v = v->next)
4958 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4959 v->line.file));
4960 struct flist *fli;
4961 int length_p = 0;
4962 int if_marked_p = 0;
4963 options_p o;
4965 for (o = v->opt; o; o = o->next)
4966 if (strcmp (o->name, "length") == 0)
4967 length_p = 1;
4968 else if (strcmp (o->name, "if_marked") == 0)
4969 if_marked_p = 1;
4971 if (!if_marked_p)
4972 continue;
4974 for (fli = flp; fli; fli = fli->next)
4975 if (fli->f == f)
4976 break;
4977 if (!fli->started_p)
4979 fli->started_p = 1;
4981 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4982 put_mangled_filename (f, v->line.file);
4983 oprintf (f, "[] = {\n");
4986 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4989 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4990 "gt_pch_cache_rtab");
4992 for (v = variables; v; v = v->next)
4994 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4995 v->line.file));
4996 struct flist *fli;
4997 int skip_p = 0;
4998 options_p o;
5000 for (o = v->opt; o; o = o->next)
5001 if (strcmp (o->name, "deletable") == 0
5002 || strcmp (o->name, "if_marked") == 0)
5004 skip_p = 1;
5005 break;
5008 if (skip_p)
5009 continue;
5011 if (!contains_scalar_p (v->type))
5012 continue;
5014 for (fli = flp; fli; fli = fli->next)
5015 if (fli->f == f)
5016 break;
5017 if (!fli->started_p)
5019 fli->started_p = 1;
5021 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
5022 put_mangled_filename (f, v->line.file);
5023 oprintf (f, "[] = {\n");
5026 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
5027 v->name, v->name);
5030 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
5031 "gt_pch_scalar_rtab");
5034 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
5035 guaranteee for somewhat increased readability. If name conflicts do happen,
5036 this funcion will have to be adjusted to be more like
5037 output_mangled_typename. */
5039 static void
5040 output_typename (outf_p of, const_type_p t)
5042 switch (t->kind)
5044 case TYPE_STRING:
5045 oprintf (of, "str");
5046 break;
5047 case TYPE_SCALAR:
5048 oprintf (of, "scalar");
5049 break;
5050 case TYPE_POINTER:
5051 output_typename (of, t->u.p);
5052 break;
5053 case TYPE_STRUCT:
5054 case TYPE_USER_STRUCT:
5055 case TYPE_UNION:
5056 case TYPE_LANG_STRUCT:
5057 oprintf (of, "%s", t->u.s.tag);
5058 break;
5059 case TYPE_PARAM_STRUCT:
5061 int i;
5062 for (i = 0; i < NUM_PARAM; i++)
5063 if (t->u.param_struct.param[i] != NULL)
5065 output_typename (of, t->u.param_struct.param[i]);
5066 oprintf (of, "_");
5068 output_typename (of, t->u.param_struct.stru);
5069 break;
5071 case TYPE_NONE:
5072 case TYPE_UNDEFINED:
5073 case TYPE_ARRAY:
5074 gcc_unreachable ();
5078 /* Writes a typed GC allocator for type S that is suitable as a callback for
5079 the splay tree implementation in libiberty. */
5081 static void
5082 write_splay_tree_allocator_def (const_type_p s)
5084 outf_p of = get_output_file_with_visibility (NULL);
5085 oprintf (of, "void * ggc_alloc_splay_tree_");
5086 output_typename (of, s);
5087 oprintf (of, " (int sz, void * nl)\n");
5088 oprintf (of, "{\n");
5089 oprintf (of, " return ggc_splay_alloc (sz, nl);\n");
5090 oprintf (of, "}\n\n");
5093 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
5094 for the splay tree implementation in libiberty. */
5096 static void
5097 write_splay_tree_allocators (const_type_p param_structs)
5099 const_type_p s;
5101 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
5102 for (s = param_structs; s; s = s->next)
5103 if (s->gc_used == GC_POINTED_TO)
5105 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
5106 output_typename (header_file, s);
5107 oprintf (header_file, " (int, void *);\n");
5108 write_splay_tree_allocator_def (s);
5112 #define INDENT 2
5114 /* Dumps the value of typekind KIND. */
5116 static void
5117 dump_typekind (int indent, enum typekind kind)
5119 printf ("%*ckind = ", indent, ' ');
5120 switch (kind)
5122 case TYPE_SCALAR:
5123 printf ("TYPE_SCALAR");
5124 break;
5125 case TYPE_STRING:
5126 printf ("TYPE_STRING");
5127 break;
5128 case TYPE_STRUCT:
5129 printf ("TYPE_STRUCT");
5130 break;
5131 case TYPE_UNDEFINED:
5132 printf ("TYPE_UNDEFINED");
5133 break;
5134 case TYPE_USER_STRUCT:
5135 printf ("TYPE_USER_STRUCT");
5136 break;
5137 case TYPE_UNION:
5138 printf ("TYPE_UNION");
5139 break;
5140 case TYPE_POINTER:
5141 printf ("TYPE_POINTER");
5142 break;
5143 case TYPE_ARRAY:
5144 printf ("TYPE_ARRAY");
5145 break;
5146 case TYPE_LANG_STRUCT:
5147 printf ("TYPE_LANG_STRUCT");
5148 break;
5149 case TYPE_PARAM_STRUCT:
5150 printf ("TYPE_PARAM_STRUCT");
5151 break;
5152 default:
5153 gcc_unreachable ();
5155 printf ("\n");
5158 /* Dumps the value of GC_USED flag. */
5160 static void
5161 dump_gc_used (int indent, enum gc_used_enum gc_used)
5163 printf ("%*cgc_used = ", indent, ' ');
5164 switch (gc_used)
5166 case GC_UNUSED:
5167 printf ("GC_UNUSED");
5168 break;
5169 case GC_USED:
5170 printf ("GC_USED");
5171 break;
5172 case GC_MAYBE_POINTED_TO:
5173 printf ("GC_MAYBE_POINTED_TO");
5174 break;
5175 case GC_POINTED_TO:
5176 printf ("GC_POINTED_TO");
5177 break;
5178 default:
5179 gcc_unreachable ();
5181 printf ("\n");
5184 /* Dumps the type options OPT. */
5186 static void
5187 dump_options (int indent, options_p opt)
5189 options_p o;
5190 printf ("%*coptions = ", indent, ' ');
5191 o = opt;
5192 while (o)
5194 switch (o->kind)
5196 case OPTION_STRING:
5197 printf ("%s:string %s ", o->name, o->info.string);
5198 break;
5199 case OPTION_TYPE:
5200 printf ("%s:type ", o->name);
5201 dump_type (indent+1, o->info.type);
5202 break;
5203 case OPTION_NESTED:
5204 printf ("%s:nested ", o->name);
5205 break;
5206 case OPTION_NONE:
5207 gcc_unreachable ();
5209 o = o->next;
5211 printf ("\n");
5214 /* Dumps the source file location in LINE. */
5216 static void
5217 dump_fileloc (int indent, struct fileloc line)
5219 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
5220 get_input_file_name (line.file),
5221 line.line);
5224 /* Recursively dumps the struct, union, or a language-specific
5225 struct T. */
5227 static void
5228 dump_type_u_s (int indent, type_p t)
5230 pair_p fields;
5232 gcc_assert (union_or_struct_p (t));
5233 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
5234 dump_fileloc (indent, t->u.s.line);
5235 printf ("%*cu.s.fields =\n", indent, ' ');
5236 fields = t->u.s.fields;
5237 while (fields)
5239 dump_pair (indent + INDENT, fields);
5240 fields = fields->next;
5242 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
5243 dump_options (indent, t->u.s.opt);
5244 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
5245 if (t->kind == TYPE_LANG_STRUCT)
5247 printf ("%*cu.s.lang_struct:\n", indent, ' ');
5248 dump_type_list (indent + INDENT, t->u.s.lang_struct);
5252 /* Recursively dumps the array T. */
5254 static void
5255 dump_type_u_a (int indent, type_p t)
5257 gcc_assert (t->kind == TYPE_ARRAY);
5258 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
5259 dump_type_list (indent + INDENT, t->u.a.p);
5262 /* Recursively dumps the parameterized struct T. */
5264 static void
5265 dump_type_u_param_struct (int indent, type_p t)
5267 int i;
5268 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
5269 printf ("%*cu.param_struct.stru:\n", indent, ' ');
5270 dump_type_list (indent, t->u.param_struct.stru);
5271 dump_fileloc (indent, t->u.param_struct.line);
5272 for (i = 0; i < NUM_PARAM; i++)
5274 if (t->u.param_struct.param[i] == NULL)
5275 continue;
5276 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
5277 dump_type (indent + INDENT, t->u.param_struct.param[i]);
5281 /* Recursively dumps the type list T. */
5283 static void
5284 dump_type_list (int indent, type_p t)
5286 type_p p = t;
5287 while (p)
5289 dump_type (indent, p);
5290 p = p->next;
5294 static htab_t seen_types;
5296 /* Recursively dumps the type T if it was not dumped previously. */
5298 static void
5299 dump_type (int indent, type_p t)
5301 PTR *slot;
5303 if (seen_types == NULL)
5304 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
5306 printf ("%*cType at %p: ", indent, ' ', (void *) t);
5307 slot = htab_find_slot (seen_types, t, INSERT);
5308 if (*slot != NULL)
5310 printf ("already seen.\n");
5311 return;
5313 *slot = t;
5314 printf ("\n");
5316 dump_typekind (indent, t->kind);
5317 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
5318 (void *) t->pointer_to);
5319 dump_gc_used (indent + INDENT, t->gc_used);
5320 switch (t->kind)
5322 case TYPE_SCALAR:
5323 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
5324 t->u.scalar_is_char ? "true" : "false");
5325 break;
5326 case TYPE_STRING:
5327 break;
5328 case TYPE_STRUCT:
5329 case TYPE_UNION:
5330 case TYPE_LANG_STRUCT:
5331 case TYPE_USER_STRUCT:
5332 dump_type_u_s (indent + INDENT, t);
5333 break;
5334 case TYPE_POINTER:
5335 printf ("%*cp:\n", indent + INDENT, ' ');
5336 dump_type (indent + INDENT, t->u.p);
5337 break;
5338 case TYPE_ARRAY:
5339 dump_type_u_a (indent + INDENT, t);
5340 break;
5341 case TYPE_PARAM_STRUCT:
5342 dump_type_u_param_struct (indent + INDENT, t);
5343 break;
5344 default:
5345 gcc_unreachable ();
5347 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
5350 /* Dumps the pair P. */
5352 static void
5353 dump_pair (int indent, pair_p p)
5355 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
5356 dump_type (indent, p->type);
5357 dump_fileloc (indent, p->line);
5358 dump_options (indent, p->opt);
5359 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
5362 /* Dumps the list of pairs PP. */
5364 static void
5365 dump_pair_list (const char *name, pair_p pp)
5367 pair_p p;
5368 printf ("%s:\n", name);
5369 for (p = pp; p != NULL; p = p->next)
5370 dump_pair (0, p);
5371 printf ("End of %s\n\n", name);
5374 /* Dumps the STRUCTURES. */
5376 static void
5377 dump_structures (const char *name, type_p structures)
5379 printf ("%s:\n", name);
5380 dump_type_list (0, structures);
5381 printf ("End of %s\n\n", name);
5384 /* Dumps the internal structures of gengtype. This is useful to debug
5385 gengtype itself, or to understand what it does, e.g. for plugin
5386 developers. */
5388 static void
5389 dump_everything (void)
5391 dump_pair_list ("typedefs", typedefs);
5392 dump_structures ("structures", structures);
5393 dump_structures ("param_structs", param_structs);
5394 dump_pair_list ("variables", variables);
5396 /* Allocated with the first call to dump_type. */
5397 htab_delete (seen_types);
5402 /* Option specification for getopt_long. */
5403 static const struct option gengtype_long_options[] = {
5404 {"help", no_argument, NULL, 'h'},
5405 {"version", no_argument, NULL, 'V'},
5406 {"verbose", no_argument, NULL, 'v'},
5407 {"dump", no_argument, NULL, 'd'},
5408 {"debug", no_argument, NULL, 'D'},
5409 {"plugin", required_argument, NULL, 'P'},
5410 {"srcdir", required_argument, NULL, 'S'},
5411 {"backupdir", required_argument, NULL, 'B'},
5412 {"inputs", required_argument, NULL, 'I'},
5413 {"read-state", required_argument, NULL, 'r'},
5414 {"write-state", required_argument, NULL, 'w'},
5415 /* Terminating NULL placeholder. */
5416 {NULL, no_argument, NULL, 0},
5420 static void
5421 print_usage (void)
5423 printf ("Usage: %s\n", progname);
5424 printf ("\t -h | --help " " \t# Give this help.\n");
5425 printf ("\t -D | --debug "
5426 " \t# Give debug output to debug %s itself.\n", progname);
5427 printf ("\t -V | --version " " \t# Give version information.\n");
5428 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5429 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5430 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5431 " \t# Generate for plugin.\n");
5432 printf ("\t -S | --srcdir <GCC-directory> "
5433 " \t# Specify the GCC source directory.\n");
5434 printf ("\t -B | --backupdir <directory> "
5435 " \t# Specify the backup directory for updated files.\n");
5436 printf ("\t -I | --inputs <input-list> "
5437 " \t# Specify the file with source files list.\n");
5438 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5439 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5442 static void
5443 print_version (void)
5445 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5446 printf ("Report bugs: %s\n", bug_report_url);
5449 /* Parse the program options using getopt_long... */
5450 static void
5451 parse_program_options (int argc, char **argv)
5453 int opt = -1;
5454 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5455 gengtype_long_options, NULL)) >= 0)
5457 switch (opt)
5459 case 'h': /* --help */
5460 print_usage ();
5461 break;
5462 case 'V': /* --version */
5463 print_version ();
5464 break;
5465 case 'd': /* --dump */
5466 do_dump = 1;
5467 break;
5468 case 'D': /* --debug */
5469 do_debug = 1;
5470 break;
5471 case 'v': /* --verbose */
5472 verbosity_level++;
5473 break;
5474 case 'P': /* --plugin */
5475 if (optarg)
5476 plugin_output_filename = optarg;
5477 else
5478 fatal ("missing plugin output file name");
5479 break;
5480 case 'S': /* --srcdir */
5481 if (optarg)
5482 srcdir = optarg;
5483 else
5484 fatal ("missing source directory");
5485 srcdir_len = strlen (srcdir);
5486 break;
5487 case 'B': /* --backupdir */
5488 if (optarg)
5489 backup_dir = optarg;
5490 else
5491 fatal ("missing backup directory");
5492 break;
5493 case 'I': /* --inputs */
5494 if (optarg)
5495 inputlist = optarg;
5496 else
5497 fatal ("missing input list");
5498 break;
5499 case 'r': /* --read-state */
5500 if (optarg)
5501 read_state_filename = optarg;
5502 else
5503 fatal ("missing read state file");
5504 DBGPRINTF ("read state %s\n", optarg);
5505 break;
5506 case 'w': /* --write-state */
5507 DBGPRINTF ("write state %s\n", optarg);
5508 if (optarg)
5509 write_state_filename = optarg;
5510 else
5511 fatal ("missing write state file");
5512 break;
5513 default:
5514 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5515 print_usage ();
5516 fatal ("unexpected flag");
5519 if (plugin_output_filename)
5521 /* In plugin mode we require some input files. */
5522 int i = 0;
5523 if (optind >= argc)
5524 fatal ("no source files given in plugin mode");
5525 nb_plugin_files = argc - optind;
5526 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5527 for (i = 0; i < (int) nb_plugin_files; i++)
5529 char *name = argv[i + optind];
5530 plugin_files[i] = input_file_by_name (name);
5537 /******* Manage input files. ******/
5539 /* Hash table of unique input file names. */
5540 static htab_t input_file_htab;
5542 /* Find or allocate a new input_file by hash-consing it. */
5543 input_file*
5544 input_file_by_name (const char* name)
5546 PTR* slot;
5547 input_file* f = NULL;
5548 int namlen = 0;
5549 if (!name)
5550 return NULL;
5551 namlen = strlen (name);
5552 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5553 f->inpbitmap = 0;
5554 f->inpoutf = NULL;
5555 f->inpisplugin = false;
5556 strcpy (f->inpname, name);
5557 slot = htab_find_slot (input_file_htab, f, INSERT);
5558 gcc_assert (slot != NULL);
5559 if (*slot)
5561 /* Already known input file. */
5562 free (f);
5563 return (input_file*)(*slot);
5565 /* New input file. */
5566 *slot = f;
5567 return f;
5570 /* Hash table support routines for input_file-s. */
5571 static hashval_t
5572 htab_hash_inputfile (const void *p)
5574 const input_file *inpf = (const input_file *) p;
5575 gcc_assert (inpf);
5576 return htab_hash_string (get_input_file_name (inpf));
5579 static int
5580 htab_eq_inputfile (const void *x, const void *y)
5582 const input_file *inpfx = (const input_file *) x;
5583 const input_file *inpfy = (const input_file *) y;
5584 gcc_assert (inpfx != NULL && inpfy != NULL);
5585 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5590 main (int argc, char **argv)
5592 size_t i;
5593 static struct fileloc pos = { NULL, 0 };
5594 outf_p output_header;
5596 /* Mandatory common initializations. */
5597 progname = "gengtype"; /* For fatal and messages. */
5598 /* Create the hash-table used to hash-cons input files. */
5599 input_file_htab =
5600 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5601 /* Initialize our special input files. */
5602 this_file = input_file_by_name (__FILE__);
5603 system_h_file = input_file_by_name ("system.h");
5604 /* Set the scalar_is_char union number for predefined scalar types. */
5605 scalar_nonchar.u.scalar_is_char = FALSE;
5606 scalar_char.u.scalar_is_char = TRUE;
5608 parse_program_options (argc, argv);
5610 #if ENABLE_CHECKING
5611 if (do_debug)
5613 time_t now = (time_t) 0;
5614 time (&now);
5615 DBGPRINTF ("gengtype started pid %d at %s",
5616 (int) getpid (), ctime (&now));
5618 #endif /* ENABLE_CHECKING */
5620 /* Parse the input list and the input files. */
5621 DBGPRINTF ("inputlist %s", inputlist);
5622 if (read_state_filename)
5624 if (inputlist)
5625 fatal ("input list %s cannot be given with a read state file %s",
5626 inputlist, read_state_filename);
5627 read_state (read_state_filename);
5628 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5629 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs);
5631 else if (inputlist)
5633 /* These types are set up with #define or else outside of where
5634 we can see them. We should initialize them before calling
5635 read_input_list. */
5636 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5637 Call;} while (0)
5638 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5639 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5640 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5641 POS_HERE (do_scalar_typedef ("double_int", &pos));
5642 POS_HERE (do_scalar_typedef ("offset_int", &pos));
5643 POS_HERE (do_scalar_typedef ("widest_int", &pos));
5644 POS_HERE (do_scalar_typedef ("int64_t", &pos));
5645 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5646 POS_HERE (do_scalar_typedef ("uint8", &pos));
5647 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5648 POS_HERE (do_scalar_typedef ("jword", &pos));
5649 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5650 POS_HERE (do_scalar_typedef ("void", &pos));
5651 POS_HERE (do_typedef ("PTR",
5652 create_pointer (resolve_typedef ("void", &pos)),
5653 &pos));
5654 #undef POS_HERE
5655 read_input_list (inputlist);
5656 for (i = 0; i < num_gt_files; i++)
5658 parse_file (get_input_file_name (gt_files[i]));
5659 DBGPRINTF ("parsed file #%d %s",
5660 (int) i, get_input_file_name (gt_files[i]));
5662 if (verbosity_level >= 1)
5663 printf ("%s parsed %d files with %d GTY types\n",
5664 progname, (int) num_gt_files, type_count);
5666 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5667 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
5670 else
5671 fatal ("either an input list or a read state file should be given");
5672 if (hit_error)
5673 return 1;
5676 if (plugin_output_filename)
5678 size_t ix = 0;
5679 /* In plugin mode, we should have read a state file, and have
5680 given at least one plugin file. */
5681 if (!read_state_filename)
5682 fatal ("No read state given in plugin mode for %s",
5683 plugin_output_filename);
5685 if (nb_plugin_files == 0 || !plugin_files)
5686 fatal ("No plugin files given in plugin mode for %s",
5687 plugin_output_filename);
5689 /* Parse our plugin files and augment the state. */
5690 for (ix = 0; ix < nb_plugin_files; ix++)
5692 input_file* pluginput = plugin_files [ix];
5693 pluginput->inpisplugin = true;
5694 parse_file (get_input_file_name (pluginput));
5696 if (hit_error)
5697 return 1;
5699 plugin_output = create_file ("GCC", plugin_output_filename);
5700 DBGPRINTF ("created plugin_output %p named %s",
5701 (void *) plugin_output, plugin_output->name);
5703 else
5704 { /* No plugin files, we are in normal mode. */
5705 if (!srcdir)
5706 fatal ("gengtype needs a source directory in normal mode");
5708 if (hit_error)
5709 return 1;
5711 gen_rtx_next ();
5713 /* The call to set_gc_used may indirectly call find_param_structure
5714 hence enlarge the param_structs list of types. */
5715 set_gc_used (variables);
5717 for (type_p t = structures; t; t = t->next)
5719 bool for_user = false;
5720 for (options_p o = t->u.s.opt; o; o = o->next)
5721 if (strcmp (o->name, "for_user") == 0)
5723 for_user = true;
5724 break;
5727 if (for_user)
5728 set_gc_used_type (t, GC_POINTED_TO, NULL);
5730 /* The state at this point is read from the state input file or by
5731 parsing source files and optionally augmented by parsing plugin
5732 source files. Write it now. */
5733 if (write_state_filename)
5735 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5736 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs);
5738 if (hit_error)
5739 fatal ("didn't write state file %s after errors",
5740 write_state_filename);
5742 DBGPRINTF ("before write_state %s", write_state_filename);
5743 write_state (write_state_filename);
5745 if (do_dump)
5746 dump_everything ();
5748 /* After having written the state file we return immediately to
5749 avoid generating any output file. */
5750 if (hit_error)
5751 return 1;
5752 else
5753 return 0;
5757 open_base_files ();
5759 output_header = plugin_output ? plugin_output : header_file;
5760 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5761 structures);
5762 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5763 param_structs);
5765 write_types (output_header, structures, param_structs, &ggc_wtd);
5766 if (plugin_files == NULL)
5768 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5769 structures);
5770 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5771 param_structs);
5772 write_types (header_file, structures, param_structs, &pch_wtd);
5773 write_local (header_file, structures, param_structs);
5775 write_splay_tree_allocators (param_structs);
5776 write_roots (variables, plugin_files == NULL);
5777 write_rtx_next ();
5778 close_output_files ();
5780 if (do_dump)
5781 dump_everything ();
5783 /* Don't bother about free-ing any input or plugin file, etc. */
5785 if (hit_error)
5786 return 1;
5787 return 0;