Optimize powerpc*-*-linux* e500 hardfp/soft-fp use.
[official-gcc.git] / gcc / gengtype.c
blobd55d53041f6238d8c7ea87f9a6c03f7635063cba
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", "hashtab.h", "hash-set.h", "vec.h",
1832 "machmode.h", "tm.h", "hard-reg-set.h", "input.h", "predict.h",
1833 "function.h", "insn-config.h", "expr.h", "alloc-pool.h",
1834 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1835 "optabs.h", "libfuncs.h", "debug.h", "ggc.h",
1836 "hash-table.h", "vec.h", "ggc.h", "dominance.h", "cfg.h", "basic-block.h",
1837 "tree-ssa-alias.h", "internal-fn.h", "gimple-fold.h", "tree-eh.h",
1838 "gimple-expr.h", "is-a.h",
1839 "gimple.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
1840 "tree-phinodes.h", "ssa-iterators.h", "stringpool.h", "tree-ssanames.h",
1841 "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h", "tree-ssa-loop-manip.h",
1842 "tree-ssa-loop-niter.h", "tree-into-ssa.h", "tree-dfa.h",
1843 "tree-ssa.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1844 "except.h", "output.h", "cfgloop.h", "target.h", "lto-streamer.h",
1845 "target-globals.h", "ipa-ref.h", "cgraph.h", "ipa-prop.h",
1846 "ipa-inline.h", "dwarf2out.h", NULL
1848 const char *const *ifp;
1849 outf_p gtype_desc_c;
1851 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1852 for (ifp = ifiles; *ifp; ifp++)
1853 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1855 /* Make sure we handle "cfun" specially. */
1856 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1857 oprintf (gtype_desc_c, "#undef cfun\n");
1859 oprintf (gtype_desc_c,
1860 "\n"
1861 "/* Types with a \"gcc::\" namespace have it stripped\n"
1862 " during gengtype parsing. Provide a \"using\" directive\n"
1863 " to ensure that the fully-qualified types are found. */\n"
1864 "using namespace gcc;\n");
1868 /* For INPF an input file, return the real basename of INPF, with all
1869 the directory components skipped. */
1871 static const char *
1872 get_file_realbasename (const input_file *inpf)
1874 return lbasename (get_input_file_name (inpf));
1877 /* For INPF a filename, return the relative path to INPF from
1878 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1880 const char *
1881 get_file_srcdir_relative_path (const input_file *inpf)
1883 const char *f = get_input_file_name (inpf);
1884 if (strlen (f) > srcdir_len
1885 && IS_DIR_SEPARATOR (f[srcdir_len])
1886 && strncmp (f, srcdir, srcdir_len) == 0)
1887 return f + srcdir_len + 1;
1888 else
1889 return NULL;
1892 /* For INPF an input_file, return the relative path to INPF from
1893 $(srcdir) if the latter is a prefix in INPF, or the real basename
1894 of INPF otherwise. */
1896 static const char *
1897 get_file_basename (const input_file *inpf)
1899 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1901 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1904 /* For F a filename, return the lang_dir_names relative index of the language
1905 directory that is a prefix in F, if any, -1 otherwise. */
1907 static int
1908 get_prefix_langdir_index (const char *f)
1910 size_t f_len = strlen (f);
1911 size_t lang_index;
1913 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1915 const char *langdir = lang_dir_names[lang_index];
1916 size_t langdir_len = strlen (langdir);
1918 if (f_len > langdir_len
1919 && IS_DIR_SEPARATOR (f[langdir_len])
1920 && memcmp (f, langdir, langdir_len) == 0)
1921 return lang_index;
1924 return -1;
1927 /* For INPF an input file, return the name of language directory where
1928 F is located, if any, NULL otherwise. */
1930 static const char *
1931 get_file_langdir (const input_file *inpf)
1933 /* Get the relative path to INPF from $(srcdir) and find the
1934 language by comparing the prefix with language directory names.
1935 If INPF is not even srcdir relative, no point in looking
1936 further. */
1938 int lang_index;
1939 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1940 const char *r;
1942 if (!srcdir_relative_path)
1943 return NULL;
1945 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1946 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1947 r = "c-family";
1948 else if (lang_index >= 0)
1949 r = lang_dir_names[lang_index];
1950 else
1951 r = NULL;
1953 return r;
1956 /* The gt- output file name for INPF. */
1958 static const char *
1959 get_file_gtfilename (const input_file *inpf)
1961 /* Cook up an initial version of the gt- file name from the file real
1962 basename and the language name, if any. */
1964 const char *basename = get_file_realbasename (inpf);
1965 const char *langdir = get_file_langdir (inpf);
1967 char *result =
1968 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1969 : xasprintf ("gt-%s", basename));
1971 /* Then replace all non alphanumerics characters by '-' and change the
1972 extension to ".h". We expect the input filename extension was at least
1973 one character long. */
1975 char *s = result;
1977 for (; *s != '.'; s++)
1978 if (!ISALNUM (*s) && *s != '-')
1979 *s = '-';
1981 memcpy (s, ".h", sizeof (".h"));
1983 return result;
1986 /* Each input_file has its associated output file outf_p. The
1987 association is computed by the function
1988 get_output_file_with_visibility. The associated file is cached
1989 inside input_file in its inpoutf field, so is really computed only
1990 once. Associated output file paths (i.e. output_name-s) are
1991 computed by a rule based regexp machinery, using the files_rules
1992 array of struct file_rule_st. A for_name is also computed, giving
1993 the source file name for which the output_file is generated; it is
1994 often the last component of the input_file path. */
1998 Regexpr machinery to compute the output_name and for_name-s of each
1999 input_file. We have a sequence of file rules which gives the POSIX
2000 extended regular expression to match an input file path, and two
2001 transformed strings for the corresponding output_name and the
2002 corresponding for_name. The transformed string contain dollars: $0
2003 is replaced by the entire match, $1 is replaced by the substring
2004 matching the first parenthesis in the regexp, etc. And $$ is replaced
2005 by a single verbatim dollar. The rule order is important. The
2006 general case is last, and the particular cases should come before.
2007 An action routine can, when needed, update the out_name & for_name
2008 and/or return the appropriate output file. It is invoked only when a
2009 rule is triggered. When a rule is triggered, the output_name and
2010 for_name are computed using their transform string in while $$, $0,
2011 $1, ... are suitably replaced. If there is an action, it is called.
2012 In some few cases, the action can directly return the outf_p, but
2013 usually it just updates the output_name and for_name so should free
2014 them before replacing them. The get_output_file_with_visibility
2015 function creates an outf_p only once per each output_name, so it
2016 scans the output_files list for previously seen output file names.
2019 /* Signature of actions in file rules. */
2020 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
2023 struct file_rule_st {
2024 const char* frul_srcexpr; /* Source string for regexp. */
2025 int frul_rflags; /* Flags passed to regcomp, usually
2026 * REG_EXTENDED. */
2027 regex_t* frul_re; /* Compiled regular expression
2028 obtained by regcomp. */
2029 const char* frul_tr_out; /* Transformation string for making
2030 * the output_name, with $1 ... $9 for
2031 * subpatterns and $0 for the whole
2032 * matched filename. */
2033 const char* frul_tr_for; /* Tranformation string for making the
2034 for_name. */
2035 frul_actionrout_t* frul_action; /* The action, if non null, is
2036 * called once the rule matches, on
2037 * the transformed out_name &
2038 * for_name. It could change them
2039 * and/or give the output file. */
2042 /* File rule action handling *.h files. */
2043 static outf_p header_dot_h_frul (input_file*, char**, char**);
2045 /* File rule action handling *.c files. */
2046 static outf_p source_dot_c_frul (input_file*, char**, char**);
2048 #define NULL_REGEX (regex_t*)0
2050 /* The prefix in our regexp-s matching the directory. */
2051 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
2053 #define NULL_FRULACT (frul_actionrout_t*)0
2055 /* The array of our rules governing file name generation. Rules order
2056 matters, so change with extreme care! */
2058 struct file_rule_st files_rules[] = {
2059 /* The general rule assumes that files in subdirectories belong to a
2060 particular front-end, and files not in subdirectories are shared.
2061 The following rules deal with exceptions - files that are in
2062 subdirectories and yet are shared, and files that are top-level,
2063 but are not shared. */
2065 /* the c-family/ source directory is special. */
2066 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
2067 REG_EXTENDED, NULL_REGEX,
2068 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
2070 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
2071 REG_EXTENDED, NULL_REGEX,
2072 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
2074 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
2075 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
2076 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
2078 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
2079 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
2081 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
2082 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
2083 REG_EXTENDED, NULL_REGEX,
2084 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
2086 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
2087 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
2088 REG_EXTENDED, NULL_REGEX,
2089 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
2091 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
2092 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
2093 REG_EXTENDED, NULL_REGEX,
2094 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
2096 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
2097 { DIR_PREFIX_REGEX "cp/parser\\.h$",
2098 REG_EXTENDED, NULL_REGEX,
2099 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
2101 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
2102 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
2103 REG_EXTENDED, NULL_REGEX,
2104 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
2106 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
2107 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
2108 REG_EXTENDED, NULL_REGEX,
2109 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
2111 /* General cases. For header *.h and source *.c or *.cc files, we
2112 * need special actions to handle the language. */
2114 /* Source *.c files are using get_file_gtfilename to compute their
2115 output_name and get_file_basename to compute their for_name
2116 through the source_dot_c_frul action. */
2117 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
2118 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
2120 /* Source *.cc files are using get_file_gtfilename to compute their
2121 output_name and get_file_basename to compute their for_name
2122 through the source_dot_c_frul action. */
2123 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.cc$",
2124 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.cc", source_dot_c_frul},
2126 /* Common header files get "gtype-desc.c" as their output_name,
2127 * while language specific header files are handled specially. So
2128 * we need the header_dot_h_frul action. */
2129 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
2130 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
2132 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
2133 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
2135 /* Mandatory null last entry signaling end of rules. */
2136 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
2139 /* Special file rules action for handling *.h header files. It gives
2140 "gtype-desc.c" for common headers and corresponding output
2141 files for language-specific header files. */
2142 static outf_p
2143 header_dot_h_frul (input_file* inpf, char**poutname,
2144 char**pforname ATTRIBUTE_UNUSED)
2146 const char *basename = 0;
2147 int lang_index = 0;
2148 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2149 (void*) inpf, get_input_file_name (inpf),
2150 *poutname, *pforname);
2151 basename = get_file_basename (inpf);
2152 lang_index = get_prefix_langdir_index (basename);
2153 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
2155 if (lang_index >= 0)
2157 /* The header is language specific. Given output_name &
2158 for_name remains unchanged. The base_files array gives the
2159 outf_p. */
2160 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2161 (void*) base_files[lang_index],
2162 (base_files[lang_index])->name);
2163 return base_files[lang_index];
2165 else
2167 /* The header is common to all front-end languages. So
2168 output_name is "gtype-desc.c" file. The calling function
2169 get_output_file_with_visibility will find its outf_p. */
2170 free (*poutname);
2171 *poutname = xstrdup ("gtype-desc.c");
2172 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2173 get_input_file_name (inpf));
2174 return NULL;
2179 /* Special file rules action for handling *.c source files using
2180 * get_file_gtfilename to compute their output_name and
2181 * get_file_basename to compute their for_name. The output_name is
2182 * gt-<LANG>-<BASE>.h for language specific source files, and
2183 * gt-<BASE>.h for common source files. */
2184 static outf_p
2185 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
2187 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
2188 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
2189 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2190 (void*) inpf, get_input_file_name (inpf),
2191 *poutname, *pforname);
2192 DBGPRINTF ("newoutname %s", newoutname);
2193 DBGPRINTF ("newbasename %s", newbasename);
2194 free (*poutname);
2195 free (*pforname);
2196 *poutname = newoutname;
2197 *pforname = newbasename;
2198 return NULL;
2201 /* Utility function for get_output_file_with_visibility which returns
2202 * a malloc-ed substituted string using TRS on matching of the FILNAM
2203 * file name, using the PMATCH array. */
2204 static char*
2205 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
2206 const char *trs)
2208 struct obstack str_obstack;
2209 char *str = NULL;
2210 char *rawstr = NULL;
2211 const char *pt = NULL;
2212 DBGPRINTF ("filnam %s", filnam);
2213 obstack_init (&str_obstack);
2214 for (pt = trs; *pt; pt++) {
2215 char c = *pt;
2216 if (c == '$')
2218 if (pt[1] == '$')
2220 /* A double dollar $$ is substituted by a single verbatim
2221 dollar, but who really uses dollar signs in file
2222 paths? */
2223 obstack_1grow (&str_obstack, '$');
2225 else if (ISDIGIT (pt[1]))
2227 /* Handle $0 $1 ... $9 by appropriate substitution. */
2228 int dolnum = pt[1] - '0';
2229 int so = pmatch[dolnum].rm_so;
2230 int eo = pmatch[dolnum].rm_eo;
2231 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2232 if (so>=0 && eo>=so)
2233 obstack_grow (&str_obstack, filnam + so, eo - so);
2235 else
2237 /* This can happen only when files_rules is buggy! */
2238 gcc_unreachable ();
2240 /* Always skip the character after the dollar. */
2241 pt++;
2243 else
2244 obstack_1grow (&str_obstack, c);
2246 obstack_1grow (&str_obstack, '\0');
2247 rawstr = XOBFINISH (&str_obstack, char *);
2248 str = xstrdup (rawstr);
2249 obstack_free (&str_obstack, NULL);
2250 DBGPRINTF ("matched replacement %s", str);
2251 rawstr = NULL;
2252 return str;
2256 /* An output file, suitable for definitions, that can see declarations
2257 made in INPF and is linked into every language that uses INPF.
2258 Since the result is cached inside INPF, that argument cannot be
2259 declared constant, but is "almost" constant. */
2261 outf_p
2262 get_output_file_with_visibility (input_file *inpf)
2264 outf_p r;
2265 char *for_name = NULL;
2266 char *output_name = NULL;
2267 const char* inpfname;
2269 /* This can happen when we need a file with visibility on a
2270 structure that we've never seen. We have to just hope that it's
2271 globally visible. */
2272 if (inpf == NULL)
2273 inpf = system_h_file;
2275 /* The result is cached in INPF, so return it if already known. */
2276 if (inpf->inpoutf)
2277 return inpf->inpoutf;
2279 /* In plugin mode, return NULL unless the input_file is one of the
2280 plugin_files. */
2281 if (plugin_files)
2283 size_t i;
2284 for (i = 0; i < nb_plugin_files; i++)
2285 if (inpf == plugin_files[i])
2287 inpf->inpoutf = plugin_output;
2288 return plugin_output;
2291 return NULL;
2294 inpfname = get_input_file_name (inpf);
2296 /* Try each rule in sequence in files_rules until one is triggered. */
2298 int rulix = 0;
2299 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2300 (void*) inpf, inpfname);
2302 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2304 DBGPRINTF ("rulix#%d srcexpr %s",
2305 rulix, files_rules[rulix].frul_srcexpr);
2307 if (!files_rules[rulix].frul_re)
2309 /* Compile the regexpr lazily. */
2310 int err = 0;
2311 files_rules[rulix].frul_re = XCNEW (regex_t);
2312 err = regcomp (files_rules[rulix].frul_re,
2313 files_rules[rulix].frul_srcexpr,
2314 files_rules[rulix].frul_rflags);
2315 if (err)
2317 /* The regular expression compilation fails only when
2318 file_rules is buggy. */
2319 gcc_unreachable ();
2323 output_name = NULL;
2324 for_name = NULL;
2326 /* Match the regexpr and trigger the rule if matched. */
2328 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2329 $3, ... $9. */
2330 regmatch_t pmatch[10];
2331 memset (pmatch, 0, sizeof (pmatch));
2332 if (!regexec (files_rules[rulix].frul_re,
2333 inpfname, 10, pmatch, 0))
2335 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2336 (void*) inpf, inpfname, rulix,
2337 files_rules[rulix].frul_srcexpr);
2338 for_name =
2339 matching_file_name_substitute (inpfname, pmatch,
2340 files_rules[rulix].frul_tr_for);
2341 DBGPRINTF ("for_name %s", for_name);
2342 output_name =
2343 matching_file_name_substitute (inpfname, pmatch,
2344 files_rules[rulix].frul_tr_out);
2345 DBGPRINTF ("output_name %s", output_name);
2346 if (files_rules[rulix].frul_action)
2348 /* Invoke our action routine. */
2349 outf_p of = NULL;
2350 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2351 rulix, output_name, for_name);
2352 of =
2353 (files_rules[rulix].frul_action) (inpf,
2354 &output_name, &for_name);
2355 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2356 rulix, (void*)of, output_name, for_name);
2357 /* If the action routine returned something, give it back
2358 immediately and cache it in inpf. */
2359 if (of)
2361 inpf->inpoutf = of;
2362 return of;
2365 /* The rule matched, and had no action, or that action did
2366 not return any output file but could have changed the
2367 output_name or for_name. We break out of the loop on the
2368 files_rules. */
2369 break;
2371 else
2373 /* The regexpr did not match. */
2374 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2375 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2376 continue;
2381 if (!output_name || !for_name)
2383 /* This should not be possible, and could only happen if the
2384 files_rules is incomplete or buggy. */
2385 fatal ("failed to compute output name for %s", inpfname);
2388 /* Look through to see if we've ever seen this output filename
2389 before. If found, cache the result in inpf. */
2390 for (r = output_files; r; r = r->next)
2391 if (filename_cmp (r->name, output_name) == 0)
2393 inpf->inpoutf = r;
2394 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2395 output_name, for_name);
2396 return r;
2399 /* If not found, create it, and cache it in inpf. */
2400 r = create_file (for_name, output_name);
2402 gcc_assert (r && r->name);
2403 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2404 output_name, for_name);
2405 inpf->inpoutf = r;
2406 return r;
2411 /* The name of an output file, suitable for definitions, that can see
2412 declarations made in INPF and is linked into every language that
2413 uses INPF. */
2415 const char *
2416 get_output_file_name (input_file* inpf)
2418 outf_p o = get_output_file_with_visibility (inpf);
2419 if (o)
2420 return o->name;
2421 return NULL;
2424 /* Check if existing file is equal to the in memory buffer. */
2426 static bool
2427 is_file_equal (outf_p of)
2429 FILE *newfile = fopen (of->name, "r");
2430 size_t i;
2431 bool equal;
2432 if (newfile == NULL)
2433 return false;
2435 equal = true;
2436 for (i = 0; i < of->bufused; i++)
2438 int ch;
2439 ch = fgetc (newfile);
2440 if (ch == EOF || ch != (unsigned char) of->buf[i])
2442 equal = false;
2443 break;
2446 if (equal && EOF != fgetc (newfile))
2447 equal = false;
2448 fclose (newfile);
2449 return equal;
2452 /* Copy the output to its final destination,
2453 but don't unnecessarily change modification times. */
2455 static void
2456 close_output_files (void)
2458 int nbwrittenfiles = 0;
2459 outf_p of;
2461 for (of = output_files; of; of = of->next)
2463 if (!is_file_equal (of))
2465 FILE *newfile = NULL;
2466 char *backupname = NULL;
2467 /* Back up the old version of the output file gt-FOO.c as
2468 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2469 if (backup_dir)
2471 backupname = concat (backup_dir, "/",
2472 lbasename (of->name), "~", NULL);
2473 if (!access (of->name, F_OK) && rename (of->name, backupname))
2474 fatal ("failed to back up %s as %s: %s",
2475 of->name, backupname, xstrerror (errno));
2478 newfile = fopen (of->name, "w");
2479 if (newfile == NULL)
2480 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2481 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2482 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2483 if (fclose (newfile) != 0)
2484 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2485 nbwrittenfiles++;
2486 if (verbosity_level >= 2 && backupname)
2487 printf ("%s wrote #%-3d %s backed-up in %s\n",
2488 progname, nbwrittenfiles, of->name, backupname);
2489 else if (verbosity_level >= 1)
2490 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2491 free (backupname);
2493 else
2495 /* output file remains unchanged. */
2496 if (verbosity_level >= 2)
2497 printf ("%s keep %s\n", progname, of->name);
2499 free (of->buf);
2500 of->buf = NULL;
2501 of->bufused = of->buflength = 0;
2503 if (verbosity_level >= 1)
2504 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2507 struct flist
2509 struct flist *next;
2510 int started_p;
2511 const input_file* file;
2512 outf_p f;
2515 struct walk_type_data;
2517 /* For scalars and strings, given the item in 'val'.
2518 For structures, given a pointer to the item in 'val'.
2519 For misc. pointers, given the item in 'val'.
2521 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2522 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2524 /* Parameters for write_types. */
2526 struct write_types_data
2528 const char *prefix;
2529 const char *param_prefix;
2530 const char *subfield_marker_routine;
2531 const char *marker_routine;
2532 const char *reorder_note_routine;
2533 const char *comment;
2534 int skip_hooks; /* skip hook generation if non zero */
2535 enum write_types_kinds kind;
2538 static void output_escaped_param (struct walk_type_data *d,
2539 const char *, const char *);
2540 static void output_mangled_typename (outf_p, const_type_p);
2541 static void walk_type (type_p t, struct walk_type_data *d);
2542 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2543 const struct write_types_data *wtd);
2544 static void write_types_process_field
2545 (type_p f, const struct walk_type_data *d);
2546 static void write_types (outf_p output_header,
2547 type_p structures,
2548 type_p param_structs,
2549 const struct write_types_data *wtd);
2550 static void write_types_local_process_field
2551 (type_p f, const struct walk_type_data *d);
2552 static void write_local_func_for_structure
2553 (const_type_p orig_s, type_p s, type_p *param);
2554 static void write_local (outf_p output_header,
2555 type_p structures, type_p param_structs);
2556 static int contains_scalar_p (type_p t);
2557 static void put_mangled_filename (outf_p, const input_file *);
2558 static void finish_root_table (struct flist *flp, const char *pfx,
2559 const char *tname, const char *lastname,
2560 const char *name);
2561 static void write_root (outf_p, pair_p, type_p, const char *, int,
2562 struct fileloc *, const char *, bool);
2563 static void write_array (outf_p f, pair_p v,
2564 const struct write_types_data *wtd);
2565 static void write_roots (pair_p, bool);
2567 /* Parameters for walk_type. */
2569 struct walk_type_data
2571 process_field_fn process_field;
2572 const void *cookie;
2573 outf_p of;
2574 options_p opt;
2575 const char *val;
2576 const char *prev_val[4];
2577 int indent;
2578 int counter;
2579 const struct fileloc *line;
2580 lang_bitmap bitmap;
2581 type_p *param;
2582 int used_length;
2583 type_p orig_s;
2584 const char *reorder_fn;
2585 bool needs_cast_p;
2586 bool fn_wants_lvalue;
2587 bool in_record_p;
2588 int loopcounter;
2589 bool in_ptr_field;
2590 bool have_this_obj;
2594 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2595 pre-processor identifier to use in a #define directive. This replaces
2596 special characters used in C++ identifiers like '>', '<' and ':' with
2597 '_'.
2599 If no C++ special characters are found in TYPE_NAME, return
2600 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2601 characters replaced with '_'. In this case, the caller is
2602 responsible for freeing the allocated string. */
2604 static const char *
2605 filter_type_name (const char *type_name)
2607 if (strchr (type_name, '<') || strchr (type_name, ':'))
2609 size_t i;
2610 char *s = xstrdup (type_name);
2611 for (i = 0; i < strlen (s); i++)
2612 if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ','
2613 || s[i] == '*')
2614 s[i] = '_';
2615 return s;
2617 else
2618 return type_name;
2622 /* Print a mangled name representing T to OF. */
2624 static void
2625 output_mangled_typename (outf_p of, const_type_p t)
2627 if (t == NULL)
2628 oprintf (of, "Z");
2629 else
2630 switch (t->kind)
2632 case TYPE_NONE:
2633 case TYPE_UNDEFINED:
2634 gcc_unreachable ();
2635 break;
2636 case TYPE_POINTER:
2637 oprintf (of, "P");
2638 output_mangled_typename (of, t->u.p);
2639 break;
2640 case TYPE_SCALAR:
2641 oprintf (of, "I");
2642 break;
2643 case TYPE_STRING:
2644 oprintf (of, "S");
2645 break;
2646 case TYPE_STRUCT:
2647 case TYPE_UNION:
2648 case TYPE_LANG_STRUCT:
2649 case TYPE_USER_STRUCT:
2651 /* For references to classes within an inheritance hierarchy,
2652 only ever reference the ultimate base class, since only
2653 it will have gt_ functions. */
2654 t = get_ultimate_base_class (t);
2655 const char *id_for_tag = filter_type_name (t->u.s.tag);
2656 oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2657 id_for_tag);
2658 if (id_for_tag != t->u.s.tag)
2659 free (CONST_CAST (char *, id_for_tag));
2661 break;
2662 case TYPE_PARAM_STRUCT:
2664 int i;
2665 for (i = 0; i < NUM_PARAM; i++)
2666 if (t->u.param_struct.param[i] != NULL)
2667 output_mangled_typename (of, t->u.param_struct.param[i]);
2668 output_mangled_typename (of, t->u.param_struct.stru);
2670 break;
2671 case TYPE_ARRAY:
2672 gcc_unreachable ();
2676 /* Print PARAM to D->OF processing escapes. D->VAL references the
2677 current object, D->PREV_VAL the object containing the current
2678 object, ONAME is the name of the option and D->LINE is used to
2679 print error messages. */
2681 static void
2682 output_escaped_param (struct walk_type_data *d, const char *param,
2683 const char *oname)
2685 const char *p;
2687 for (p = param; *p; p++)
2688 if (*p != '%')
2689 oprintf (d->of, "%c", *p);
2690 else
2691 switch (*++p)
2693 case 'h':
2694 oprintf (d->of, "(%s)", d->prev_val[2]);
2695 break;
2696 case '0':
2697 oprintf (d->of, "(%s)", d->prev_val[0]);
2698 break;
2699 case '1':
2700 oprintf (d->of, "(%s)", d->prev_val[1]);
2701 break;
2702 case 'a':
2704 const char *pp = d->val + strlen (d->val);
2705 while (pp[-1] == ']')
2706 while (*pp != '[')
2707 pp--;
2708 oprintf (d->of, "%s", pp);
2710 break;
2711 default:
2712 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2713 oname, '%', *p);
2717 const char *
2718 get_string_option (options_p opt, const char *key)
2720 for (; opt; opt = opt->next)
2721 if (strcmp (opt->name, key) == 0)
2722 return opt->info.string;
2723 return NULL;
2726 /* Machinery for avoiding duplicate tags within switch statements. */
2727 struct seen_tag
2729 const char *tag;
2730 struct seen_tag *next;
2734 already_seen_tag (struct seen_tag *seen_tags, const char *tag)
2736 /* Linear search, so O(n^2), but n is currently small. */
2737 while (seen_tags)
2739 if (!strcmp (seen_tags->tag, tag))
2740 return 1;
2741 seen_tags = seen_tags->next;
2743 /* Not yet seen this tag. */
2744 return 0;
2747 void
2748 mark_tag_as_seen (struct seen_tag **seen_tags, const char *tag)
2750 /* Add to front of linked list. */
2751 struct seen_tag *new_node = XCNEW (struct seen_tag);
2752 new_node->tag = tag;
2753 new_node->next = *seen_tags;
2754 *seen_tags = new_node;
2757 static void
2758 walk_subclasses (type_p base, struct walk_type_data *d,
2759 struct seen_tag **seen_tags)
2761 for (type_p sub = base->u.s.first_subclass; sub != NULL;
2762 sub = sub->u.s.next_sibling_class)
2764 const char *type_tag = get_string_option (sub->u.s.opt, "tag");
2765 if (type_tag && !already_seen_tag (*seen_tags, type_tag))
2767 mark_tag_as_seen (seen_tags, type_tag);
2768 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
2769 d->indent += 2;
2770 oprintf (d->of, "%*s{\n", d->indent, "");
2771 d->indent += 2;
2772 oprintf (d->of, "%*s%s *sub = static_cast <%s *> (x);\n",
2773 d->indent, "", sub->u.s.tag, sub->u.s.tag);
2774 const char *old_val = d->val;
2775 d->val = "(*sub)";
2776 walk_type (sub, d);
2777 d->val = old_val;
2778 d->indent -= 2;
2779 oprintf (d->of, "%*s}\n", d->indent, "");
2780 oprintf (d->of, "%*sbreak;\n", d->indent, "");
2781 d->indent -= 2;
2783 walk_subclasses (sub, d, seen_tags);
2787 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2788 which is of type T. Write code to D->OF to constrain execution (at
2789 the point that D->PROCESS_FIELD is called) to the appropriate
2790 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2791 pointers to those objects. D->PREV_VAL lists the objects
2792 containing the current object, D->OPT is a list of options to
2793 apply, D->INDENT is the current indentation level, D->LINE is used
2794 to print error messages, D->BITMAP indicates which languages to
2795 print the structure for, and D->PARAM is the current parameter
2796 (from an enclosing param_is option). */
2798 static void
2799 walk_type (type_p t, struct walk_type_data *d)
2801 const char *length = NULL;
2802 const char *desc = NULL;
2803 const char *type_tag = NULL;
2804 int maybe_undef_p = 0;
2805 int use_param_num = -1;
2806 int use_params_p = 0;
2807 int atomic_p = 0;
2808 options_p oo;
2809 const struct nested_ptr_data *nested_ptr_d = NULL;
2811 d->needs_cast_p = false;
2812 for (oo = d->opt; oo; oo = oo->next)
2813 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2814 length = oo->info.string;
2815 else if (strcmp (oo->name, "maybe_undef") == 0)
2816 maybe_undef_p = 1;
2817 else if (strncmp (oo->name, "use_param", 9) == 0
2818 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2819 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2820 else if (strcmp (oo->name, "use_params") == 0)
2821 use_params_p = 1;
2822 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2823 desc = oo->info.string;
2824 else if (strcmp (oo->name, "mark_hook") == 0)
2826 else if (strcmp (oo->name, "nested_ptr") == 0
2827 && oo->kind == OPTION_NESTED)
2828 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2829 else if (strcmp (oo->name, "dot") == 0)
2831 else if (strcmp (oo->name, "tag") == 0)
2832 type_tag = oo->info.string;
2833 else if (strcmp (oo->name, "special") == 0)
2835 else if (strcmp (oo->name, "skip") == 0)
2837 else if (strcmp (oo->name, "atomic") == 0)
2838 atomic_p = 1;
2839 else if (strcmp (oo->name, "default") == 0)
2841 else if (strcmp (oo->name, "param_is") == 0)
2843 else if (strncmp (oo->name, "param", 5) == 0
2844 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2846 else if (strcmp (oo->name, "chain_next") == 0)
2848 else if (strcmp (oo->name, "chain_prev") == 0)
2850 else if (strcmp (oo->name, "chain_circular") == 0)
2852 else if (strcmp (oo->name, "reorder") == 0)
2854 else if (strcmp (oo->name, "variable_size") == 0)
2856 else if (strcmp (oo->name, "for_user") == 0)
2858 else
2859 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2861 if (d->used_length)
2862 length = NULL;
2864 if (use_params_p)
2866 int pointer_p = t->kind == TYPE_POINTER;
2868 if (pointer_p)
2869 t = t->u.p;
2870 if (!union_or_struct_p (t))
2871 error_at_line (d->line, "`use_params' option on unimplemented type");
2872 else
2873 t = find_param_structure (t, d->param);
2874 if (pointer_p)
2875 t = create_pointer (t);
2878 if (use_param_num != -1)
2880 if (d->param != NULL && d->param[use_param_num] != NULL)
2882 type_p nt = d->param[use_param_num];
2884 if (t->kind == TYPE_ARRAY)
2885 nt = create_array (nt, t->u.a.len);
2886 else if (length != NULL && t->kind == TYPE_POINTER)
2887 nt = create_pointer (nt);
2888 d->needs_cast_p = (t->kind != TYPE_POINTER
2889 && (nt->kind == TYPE_POINTER
2890 || nt->kind == TYPE_STRING));
2891 t = nt;
2893 else
2894 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2897 if (maybe_undef_p
2898 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
2900 error_at_line (d->line,
2901 "field `%s' has invalid option `maybe_undef_p'\n",
2902 d->val);
2903 return;
2906 if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2908 error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2909 return;
2912 switch (t->kind)
2914 case TYPE_SCALAR:
2915 case TYPE_STRING:
2916 d->process_field (t, d);
2917 break;
2919 case TYPE_POINTER:
2921 d->in_ptr_field = true;
2922 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2924 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2925 break;
2928 /* If a pointer type is marked as "atomic", we process the
2929 field itself, but we don't walk the data that they point to.
2931 There are two main cases where we walk types: to mark
2932 pointers that are reachable, and to relocate pointers when
2933 writing a PCH file. In both cases, an atomic pointer is
2934 itself marked or relocated, but the memory that it points
2935 to is left untouched. In the case of PCH, that memory will
2936 be read/written unchanged to the PCH file. */
2937 if (atomic_p)
2939 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2940 d->indent += 2;
2941 d->process_field (t, d);
2942 d->indent -= 2;
2943 oprintf (d->of, "%*s}\n", d->indent, "");
2944 break;
2947 if (!length)
2949 if (!union_or_struct_p (t->u.p)
2950 && t->u.p->kind != TYPE_PARAM_STRUCT)
2952 error_at_line (d->line,
2953 "field `%s' is pointer to unimplemented type",
2954 d->val);
2955 break;
2958 if (nested_ptr_d)
2960 const char *oldprevval2 = d->prev_val[2];
2962 if (!union_or_struct_p (nested_ptr_d->type))
2964 error_at_line (d->line,
2965 "field `%s' has invalid "
2966 "option `nested_ptr'\n", d->val);
2967 return;
2970 d->prev_val[2] = d->val;
2971 oprintf (d->of, "%*s{\n", d->indent, "");
2972 d->indent += 2;
2973 d->val = xasprintf ("x%d", d->counter++);
2974 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2975 (nested_ptr_d->type->kind == TYPE_UNION
2976 ? "union" : "struct"),
2977 nested_ptr_d->type->u.s.tag,
2978 d->fn_wants_lvalue ? "" : "const ", d->val);
2979 oprintf (d->of, "%*s", d->indent + 2, "");
2980 output_escaped_param (d, nested_ptr_d->convert_from,
2981 "nested_ptr");
2982 oprintf (d->of, ";\n");
2984 d->process_field (nested_ptr_d->type, d);
2986 if (d->fn_wants_lvalue)
2988 oprintf (d->of, "%*s%s = ", d->indent, "",
2989 d->prev_val[2]);
2990 d->prev_val[2] = d->val;
2991 output_escaped_param (d, nested_ptr_d->convert_to,
2992 "nested_ptr");
2993 oprintf (d->of, ";\n");
2996 d->indent -= 2;
2997 oprintf (d->of, "%*s}\n", d->indent, "");
2998 d->val = d->prev_val[2];
2999 d->prev_val[2] = oldprevval2;
3001 else
3002 d->process_field (t->u.p, d);
3004 else
3006 int loopcounter = d->loopcounter;
3007 const char *oldval = d->val;
3008 const char *oldprevval3 = d->prev_val[3];
3009 char *newval;
3011 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
3012 d->indent += 2;
3013 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
3014 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
3015 "", loopcounter, loopcounter);
3016 if (!d->in_record_p)
3017 output_escaped_param (d, length, "length");
3018 else
3019 oprintf (d->of, "l%d", loopcounter);
3020 if (d->have_this_obj)
3021 /* Try to unswitch loops (see PR53880). */
3022 oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
3023 oprintf (d->of, "); i%d++) {\n", loopcounter);
3024 d->indent += 2;
3025 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
3026 d->used_length = 1;
3027 d->prev_val[3] = oldval;
3028 walk_type (t->u.p, d);
3029 free (newval);
3030 d->val = oldval;
3031 d->prev_val[3] = oldprevval3;
3032 d->used_length = 0;
3033 d->indent -= 2;
3034 oprintf (d->of, "%*s}\n", d->indent, "");
3035 d->process_field (t, d);
3036 d->indent -= 2;
3037 oprintf (d->of, "%*s}\n", d->indent, "");
3039 d->in_ptr_field = false;
3041 break;
3043 case TYPE_ARRAY:
3045 int loopcounter;
3046 const char *oldval = d->val;
3047 char *newval;
3049 /* If it's an array of scalars, we optimize by not generating
3050 any code. */
3051 if (t->u.a.p->kind == TYPE_SCALAR)
3052 break;
3054 if (length)
3055 loopcounter = d->loopcounter;
3056 else
3057 loopcounter = d->counter++;
3059 /* When walking an array, compute the length and store it in a
3060 local variable before walking the array elements, instead of
3061 recomputing the length expression each time through the loop.
3062 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
3063 where the length is stored in the first array element,
3064 because otherwise that operand can get overwritten on the
3065 first iteration. */
3066 oprintf (d->of, "%*s{\n", d->indent, "");
3067 d->indent += 2;
3068 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
3069 if (!d->in_record_p || !length)
3071 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
3072 d->indent, "", loopcounter);
3073 if (length)
3074 output_escaped_param (d, length, "length");
3075 else
3076 oprintf (d->of, "%s", t->u.a.len);
3077 oprintf (d->of, ");\n");
3080 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
3081 d->indent, "",
3082 loopcounter, loopcounter, loopcounter, loopcounter);
3083 d->indent += 2;
3084 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
3085 d->used_length = 1;
3086 walk_type (t->u.a.p, d);
3087 free (newval);
3088 d->used_length = 0;
3089 d->val = oldval;
3090 d->indent -= 2;
3091 oprintf (d->of, "%*s}\n", d->indent, "");
3092 d->indent -= 2;
3093 oprintf (d->of, "%*s}\n", d->indent, "");
3095 break;
3097 case TYPE_STRUCT:
3098 case TYPE_UNION:
3100 pair_p f;
3101 const char *oldval = d->val;
3102 const char *oldprevval1 = d->prev_val[1];
3103 const char *oldprevval2 = d->prev_val[2];
3104 const char *struct_mark_hook = NULL;
3105 const int union_p = t->kind == TYPE_UNION;
3106 int seen_default_p = 0;
3107 options_p o;
3108 int lengths_seen = 0;
3109 int endcounter;
3110 bool any_length_seen = false;
3112 if (!t->u.s.line.file)
3113 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
3115 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
3117 error_at_line (d->line,
3118 "structure `%s' defined for mismatching languages",
3119 t->u.s.tag);
3120 error_at_line (&t->u.s.line, "one structure defined here");
3123 /* Some things may also be defined in the structure's options. */
3124 for (o = t->u.s.opt; o; o = o->next)
3125 if (!desc && strcmp (o->name, "desc") == 0
3126 && o->kind == OPTION_STRING)
3127 desc = o->info.string;
3128 else if (!struct_mark_hook && strcmp (o->name, "mark_hook") == 0
3129 && o->kind == OPTION_STRING)
3130 struct_mark_hook = o->info.string;
3132 if (struct_mark_hook)
3133 oprintf (d->of, "%*s%s (&%s);\n",
3134 d->indent, "", struct_mark_hook, oldval);
3136 d->prev_val[2] = oldval;
3137 d->prev_val[1] = oldprevval2;
3138 if (union_p)
3140 if (desc == NULL)
3142 error_at_line (d->line,
3143 "missing `desc' option for union `%s'",
3144 t->u.s.tag);
3145 desc = "1";
3147 oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
3148 output_escaped_param (d, desc, "desc");
3149 oprintf (d->of, "))\n");
3150 d->indent += 2;
3151 oprintf (d->of, "%*s{\n", d->indent, "");
3153 else if (desc)
3155 /* We have a "desc" option on a struct, signifying the
3156 base class within a GC-managed inheritance hierarchy.
3157 The current code specialcases the base class, then walks
3158 into subclasses, recursing into this routine to handle them.
3159 This organization requires the base class to have a case in
3160 the switch statement, and hence a tag value is mandatory
3161 for the base class. This restriction could be removed, but
3162 it would require some restructing of this code. */
3163 if (!type_tag)
3165 error_at_line (d->line,
3166 "missing `tag' option for type `%s'",
3167 t->u.s.tag);
3169 oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
3170 output_escaped_param (d, desc, "desc");
3171 oprintf (d->of, "))\n");
3172 d->indent += 2;
3173 oprintf (d->of, "%*s{\n", d->indent, "");
3174 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
3175 d->indent += 2;
3178 FOR_ALL_INHERITED_FIELDS (t, f)
3180 options_p oo;
3181 int skip_p = 0;
3182 const char *fieldlength = NULL;
3184 d->reorder_fn = NULL;
3185 for (oo = f->opt; oo; oo = oo->next)
3186 if (strcmp (oo->name, "skip") == 0)
3187 skip_p = 1;
3188 else if (strcmp (oo->name, "length") == 0
3189 && oo->kind == OPTION_STRING)
3190 fieldlength = oo->info.string;
3192 if (skip_p)
3193 continue;
3194 if (fieldlength)
3196 lengths_seen++;
3197 d->counter++;
3198 if (!union_p)
3200 if (!any_length_seen)
3202 oprintf (d->of, "%*s{\n", d->indent, "");
3203 d->indent += 2;
3205 any_length_seen = true;
3207 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
3208 d->indent, "", d->counter - 1);
3209 output_escaped_param (d, fieldlength, "length");
3210 oprintf (d->of, ");\n");
3214 endcounter = d->counter;
3216 FOR_ALL_INHERITED_FIELDS (t, f)
3218 options_p oo;
3219 const char *dot = ".";
3220 const char *tagid = NULL;
3221 int skip_p = 0;
3222 int default_p = 0;
3223 int use_param_p = 0;
3224 const char *fieldlength = NULL;
3225 char *newval;
3227 d->reorder_fn = NULL;
3228 for (oo = f->opt; oo; oo = oo->next)
3229 if (strcmp (oo->name, "dot") == 0
3230 && oo->kind == OPTION_STRING)
3231 dot = oo->info.string;
3232 else if (strcmp (oo->name, "tag") == 0
3233 && oo->kind == OPTION_STRING)
3234 tagid = oo->info.string;
3235 else if (strcmp (oo->name, "skip") == 0)
3236 skip_p = 1;
3237 else if (strcmp (oo->name, "default") == 0)
3238 default_p = 1;
3239 else if (strcmp (oo->name, "reorder") == 0
3240 && oo->kind == OPTION_STRING)
3241 d->reorder_fn = oo->info.string;
3242 else if (strncmp (oo->name, "use_param", 9) == 0
3243 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
3244 use_param_p = 1;
3245 else if (strcmp (oo->name, "length") == 0
3246 && oo->kind == OPTION_STRING)
3247 fieldlength = oo->info.string;
3249 if (skip_p)
3250 continue;
3252 if (union_p && tagid)
3254 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
3255 d->indent += 2;
3257 else if (union_p && default_p)
3259 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3260 d->indent += 2;
3261 seen_default_p = 1;
3263 else if (!union_p && (default_p || tagid))
3264 error_at_line (d->line,
3265 "can't use `%s' outside a union on field `%s'",
3266 default_p ? "default" : "tag", f->name);
3267 else if (union_p && !(default_p || tagid)
3268 && f->type->kind == TYPE_SCALAR)
3270 fprintf (stderr,
3271 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3272 get_input_file_name (d->line->file), d->line->line,
3273 f->name);
3274 continue;
3276 else if (union_p && !(default_p || tagid))
3277 error_at_line (d->line,
3278 "field `%s' is missing `tag' or `default' option",
3279 f->name);
3281 if (fieldlength)
3283 d->loopcounter = endcounter - lengths_seen--;
3286 d->line = &f->line;
3287 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
3288 d->opt = f->opt;
3289 d->used_length = false;
3290 d->in_record_p = !union_p;
3292 if (union_p && use_param_p && d->param == NULL)
3293 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
3294 else
3295 walk_type (f->type, d);
3297 d->in_record_p = false;
3299 free (newval);
3301 if (union_p)
3303 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3304 d->indent -= 2;
3307 d->reorder_fn = NULL;
3309 d->val = oldval;
3310 d->prev_val[1] = oldprevval1;
3311 d->prev_val[2] = oldprevval2;
3313 if (union_p && !seen_default_p)
3315 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3316 oprintf (d->of, "%*s break;\n", d->indent, "");
3319 if (desc && !union_p)
3321 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3322 d->indent -= 2;
3324 if (union_p)
3326 oprintf (d->of, "%*s}\n", d->indent, "");
3327 d->indent -= 2;
3329 else if (desc)
3331 /* Add cases to handle subclasses. */
3332 struct seen_tag *tags = NULL;
3333 walk_subclasses (t, d, &tags);
3335 /* Ensure that if someone forgets a "tag" option that we don't
3336 silent fail to traverse that subclass's fields. */
3337 if (!seen_default_p)
3339 oprintf (d->of, "%*s/* Unrecognized tag value. */\n",
3340 d->indent, "");
3341 oprintf (d->of, "%*sdefault: gcc_unreachable (); \n",
3342 d->indent, "");
3345 /* End of the switch statement */
3346 oprintf (d->of, "%*s}\n", d->indent, "");
3347 d->indent -= 2;
3349 if (any_length_seen)
3351 d->indent -= 2;
3352 oprintf (d->of, "%*s}\n", d->indent, "");
3355 break;
3357 case TYPE_LANG_STRUCT:
3359 type_p nt;
3360 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3361 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3362 break;
3363 if (nt == NULL)
3364 error_at_line (d->line, "structure `%s' differs between languages",
3365 t->u.s.tag);
3366 else
3367 walk_type (nt, d);
3369 break;
3371 case TYPE_PARAM_STRUCT:
3373 type_p *oldparam = d->param;
3375 d->param = t->u.param_struct.param;
3376 walk_type (t->u.param_struct.stru, d);
3377 d->param = oldparam;
3379 break;
3381 case TYPE_USER_STRUCT:
3382 d->process_field (t, d);
3383 break;
3385 case TYPE_NONE:
3386 case TYPE_UNDEFINED:
3387 gcc_unreachable ();
3391 /* process_field routine for marking routines. */
3393 static void
3394 write_types_process_field (type_p f, const struct walk_type_data *d)
3396 const struct write_types_data *wtd;
3397 const char *cast = d->needs_cast_p ? "(void *)" : "";
3398 wtd = (const struct write_types_data *) d->cookie;
3400 switch (f->kind)
3402 case TYPE_NONE:
3403 case TYPE_UNDEFINED:
3404 gcc_unreachable ();
3405 case TYPE_POINTER:
3406 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
3407 wtd->subfield_marker_routine, cast, d->val);
3408 if (wtd->param_prefix)
3410 if (f->u.p->kind == TYPE_SCALAR)
3411 /* The current type is a pointer to a scalar (so not
3412 considered like a pointer to instances of user defined
3413 types) and we are seeing it; it means we must be even
3414 more careful about the second argument of the
3415 SUBFIELD_MARKER_ROUTINE call. That argument must
3416 always be the instance of the type for which
3417 write_func_for_structure was called - this really is
3418 what the function SUBFIELD_MARKER_ROUTINE expects.
3419 That is, it must be an instance of the ORIG_S type
3420 parameter of write_func_for_structure. The convention
3421 is that that argument must be "x" in that case (as set
3422 by write_func_for_structure). The problem is, we can't
3423 count on d->prev_val[3] to be always set to "x" in that
3424 case. Sometimes walk_type can set it to something else
3425 (to e.g cooperate with write_array when called from
3426 write_roots). So let's set it to "x" here then. */
3427 oprintf (d->of, ", x");
3428 else
3429 oprintf (d->of, ", %s", d->prev_val[3]);
3430 if (d->orig_s)
3432 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3433 output_mangled_typename (d->of, d->orig_s);
3435 else
3436 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3438 oprintf (d->of, ");\n");
3439 if (d->reorder_fn && wtd->reorder_note_routine)
3440 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
3441 wtd->reorder_note_routine, cast, d->val,
3442 d->prev_val[3], d->reorder_fn);
3443 break;
3445 case TYPE_STRING:
3446 case TYPE_STRUCT:
3447 case TYPE_UNION:
3448 case TYPE_LANG_STRUCT:
3449 case TYPE_PARAM_STRUCT:
3450 case TYPE_USER_STRUCT:
3451 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3453 /* If F is a user-defined type and the field is not a
3454 pointer to the type, then we should not generate the
3455 standard pointer-marking code. All we need to do is call
3456 the user-provided marking function to process the fields
3457 of F. */
3458 oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3459 d->val);
3461 else
3463 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3464 output_mangled_typename (d->of, f);
3465 oprintf (d->of, " (%s%s);\n", cast, d->val);
3466 if (d->reorder_fn && wtd->reorder_note_routine)
3467 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
3468 wtd->reorder_note_routine, cast, d->val, cast, d->val,
3469 d->reorder_fn);
3471 break;
3473 case TYPE_SCALAR:
3474 break;
3476 case TYPE_ARRAY:
3477 gcc_unreachable ();
3481 /* Return an output file that is suitable for definitions which can
3482 reference struct S */
3484 static outf_p
3485 get_output_file_for_structure (const_type_p s, type_p *param)
3487 const input_file *fn;
3488 int i;
3490 gcc_assert (union_or_struct_p (s));
3491 fn = s->u.s.line.file;
3493 /* This is a hack, and not the good kind either. */
3494 for (i = NUM_PARAM - 1; i >= 0; i--)
3495 if (param && param[i] && param[i]->kind == TYPE_POINTER
3496 && union_or_struct_p (param[i]->u.p))
3497 fn = param[i]->u.p->u.s.line.file;
3499 /* The call to get_output_file_with_visibility may update fn by
3500 caching its result inside, so we need the CONST_CAST. */
3501 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3505 /* Returns the specifier keyword for a string or union type S, empty string
3506 otherwise. */
3508 static const char *
3509 get_type_specifier (const type_p s)
3511 if (s->kind == TYPE_STRUCT)
3512 return "struct ";
3513 else if (s->kind == TYPE_LANG_STRUCT)
3514 return get_type_specifier (s->u.s.lang_struct);
3515 else if (s->kind == TYPE_UNION)
3516 return "union ";
3517 return "";
3521 /* Emits a declaration for type TY (assumed to be a union or a
3522 structure) on stream OUT. */
3524 static void
3525 write_type_decl (outf_p out, type_p ty)
3527 if (union_or_struct_p (ty))
3528 oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3529 else if (ty->kind == TYPE_SCALAR)
3531 if (ty->u.scalar_is_char)
3532 oprintf (out, "const char");
3533 else
3534 oprintf (out, "void");
3536 else if (ty->kind == TYPE_POINTER)
3538 write_type_decl (out, ty->u.p);
3539 oprintf (out, " *");
3541 else if (ty->kind == TYPE_ARRAY)
3543 write_type_decl (out, ty->u.a.p);
3544 oprintf (out, " *");
3546 else if (ty->kind == TYPE_STRING)
3548 oprintf (out, "const char *");
3550 else
3551 gcc_unreachable ();
3555 /* Write on OF the name of the marker function for structure S. PREFIX
3556 is the prefix to use (to distinguish ggc from pch markers). */
3558 static void
3559 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3561 if (union_or_struct_p (s))
3563 const char *id_for_tag = filter_type_name (s->u.s.tag);
3564 oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3565 if (id_for_tag != s->u.s.tag)
3566 free (CONST_CAST (char *, id_for_tag));
3568 else if (s->kind == TYPE_PARAM_STRUCT)
3570 oprintf (of, "gt_%s_", prefix);
3571 output_mangled_typename (of, s);
3573 else
3574 gcc_unreachable ();
3577 /* Write on OF a user-callable routine to act as an entry point for
3578 the marking routine for S, generated by write_func_for_structure.
3579 WTD distinguishes between ggc and pch markers. */
3581 static void
3582 write_user_func_for_structure_ptr (outf_p of, type_p s, const write_types_data *wtd)
3584 /* Parameterized structures are not supported in user markers. There
3585 is no way for the marker function to know which specific type
3586 to use to generate the call to the void * entry point. For
3587 instance, a marker for struct htab may need to call different
3588 routines to mark the fields, depending on the paramN_is attributes.
3590 A user-defined marker that accepts 'struct htab' as its argument
3591 would not know which variant to call. Generating several entry
3592 points accepting 'struct htab' would cause multiply-defined
3593 errors during compilation. */
3594 gcc_assert (union_or_struct_p (s));
3596 type_p alias_of = NULL;
3597 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3598 if (strcmp (opt->name, "ptr_alias") == 0)
3600 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3601 we do not generate marking code for ORIG_S here. Instead, a
3602 forwarder #define in gtype-desc.h will cause every call to its
3603 marker to call the target of this alias.
3605 However, we still want to create a user entry code for the
3606 aliased type. So, if ALIAS_OF is set, we only generate the
3607 user-callable marker function. */
3608 alias_of = opt->info.type;
3609 break;
3612 DBGPRINTF ("write_user_func_for_structure_ptr: %s %s", s->u.s.tag,
3613 wtd->prefix);
3615 /* Only write the function once. */
3616 if (s->u.s.wrote_user_func_for_ptr[wtd->kind])
3617 return;
3618 s->u.s.wrote_user_func_for_ptr[wtd->kind] = true;
3620 oprintf (of, "\nvoid\n");
3621 oprintf (of, "gt_%sx (", wtd->prefix);
3622 write_type_decl (of, s);
3623 oprintf (of, " *& x)\n");
3624 oprintf (of, "{\n");
3625 oprintf (of, " if (x)\n ");
3626 write_marker_function_name (of,
3627 alias_of ? alias_of : get_ultimate_base_class (s),
3628 wtd->prefix);
3629 oprintf (of, " ((void *) x);\n");
3630 oprintf (of, "}\n");
3634 /* Write a function to mark all the fields of type S on OF. PREFIX
3635 and D are as in write_user_marking_functions. */
3637 static void
3638 write_user_func_for_structure_body (type_p s, const char *prefix,
3639 struct walk_type_data *d)
3641 oprintf (d->of, "\nvoid\n");
3642 oprintf (d->of, "gt_%sx (", prefix);
3643 write_type_decl (d->of, s);
3644 oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3645 oprintf (d->of, "{\n");
3646 oprintf (d->of, " ");
3647 write_type_decl (d->of, s);
3648 oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3649 d->val = "(*x)";
3650 d->indent = 2;
3651 walk_type (s, d);
3652 oprintf (d->of, "}\n");
3655 /* Emit the user-callable functions needed to mark all the types used
3656 by the user structure S. PREFIX is the prefix to use to
3657 distinguish ggc and pch markers. D contains data needed to pass to
3658 walk_type when traversing the fields of a type.
3660 For every type T referenced by S, two routines are generated: one
3661 that takes 'T *', marks the pointer and calls the second routine,
3662 which just marks the fields of T. */
3664 static void
3665 write_user_marking_functions (type_p s,
3666 const write_types_data *w,
3667 struct walk_type_data *d)
3669 gcc_assert (s->kind == TYPE_USER_STRUCT);
3671 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3673 type_p fld_type = fld->type;
3674 if (fld_type->kind == TYPE_POINTER)
3676 type_p pointed_to_type = fld_type->u.p;
3677 if (union_or_struct_p (pointed_to_type))
3678 write_user_func_for_structure_ptr (d->of, pointed_to_type, w);
3680 else if (union_or_struct_p (fld_type))
3681 write_user_func_for_structure_body (fld_type, w->prefix, d);
3686 /* For S, a structure that's part of ORIG_S, and using parameters
3687 PARAM, write out a routine that:
3688 - Takes a parameter, a void * but actually of type *S
3689 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3690 field of S or its substructures and (in some cases) things
3691 that are pointed to by S. */
3693 static void
3694 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
3695 const struct write_types_data *wtd)
3697 const char *chain_next = NULL;
3698 const char *chain_prev = NULL;
3699 const char *chain_circular = NULL;
3700 const char *mark_hook_name = NULL;
3701 options_p opt;
3702 struct walk_type_data d;
3704 if (s->u.s.base_class)
3706 /* Verify that the base class has a "desc", since otherwise
3707 the traversal hooks there won't attempt to visit fields of
3708 subclasses such as this one. */
3709 const_type_p ubc = get_ultimate_base_class (s);
3710 if ((!opts_have (ubc->u.s.opt, "user")
3711 && !opts_have (ubc->u.s.opt, "desc")))
3712 error_at_line (&s->u.s.line,
3713 ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
3714 ", but '%s' lacks a discriminator 'desc' option"),
3715 s->u.s.tag, ubc->u.s.tag, ubc->u.s.tag);
3717 /* Don't write fns for subclasses, only for the ultimate base class
3718 within an inheritance hierarchy. */
3719 return;
3722 memset (&d, 0, sizeof (d));
3723 d.of = get_output_file_for_structure (s, param);
3725 bool for_user = false;
3726 for (opt = s->u.s.opt; opt; opt = opt->next)
3727 if (strcmp (opt->name, "chain_next") == 0
3728 && opt->kind == OPTION_STRING)
3729 chain_next = opt->info.string;
3730 else if (strcmp (opt->name, "chain_prev") == 0
3731 && opt->kind == OPTION_STRING)
3732 chain_prev = opt->info.string;
3733 else if (strcmp (opt->name, "chain_circular") == 0
3734 && opt->kind == OPTION_STRING)
3735 chain_circular = opt->info.string;
3736 else if (strcmp (opt->name, "mark_hook") == 0
3737 && opt->kind == OPTION_STRING)
3738 mark_hook_name = opt->info.string;
3739 else if (strcmp (opt->name, "for_user") == 0)
3740 for_user = true;
3741 if (chain_prev != NULL && chain_next == NULL)
3742 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3743 if (chain_circular != NULL && chain_next != NULL)
3744 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3745 if (chain_circular != NULL)
3746 chain_next = chain_circular;
3748 d.process_field = write_types_process_field;
3749 d.cookie = wtd;
3750 d.orig_s = orig_s;
3751 d.opt = s->u.s.opt;
3752 d.line = &s->u.s.line;
3753 d.bitmap = s->u.s.bitmap;
3754 d.param = param;
3755 d.prev_val[0] = "*x";
3756 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3757 d.prev_val[3] = "x";
3758 d.val = "(*x)";
3759 d.have_this_obj = false;
3761 oprintf (d.of, "\n");
3762 oprintf (d.of, "void\n");
3763 write_marker_function_name (d.of, orig_s, wtd->prefix);
3764 oprintf (d.of, " (void *x_p)\n");
3765 oprintf (d.of, "{\n ");
3766 write_type_decl (d.of, s);
3767 oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3768 write_type_decl (d.of, s);
3769 oprintf (d.of, " *)x_p;\n");
3770 if (chain_next != NULL)
3772 /* TYPE_USER_STRUCTs should not occur here. These structures
3773 are completely handled by user code. */
3774 gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
3776 oprintf (d.of, " ");
3777 write_type_decl (d.of, s);
3778 oprintf (d.of, " * xlimit = x;\n");
3780 if (chain_next == NULL)
3782 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3783 if (wtd->param_prefix)
3785 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3786 output_mangled_typename (d.of, orig_s);
3788 oprintf (d.of, "))\n");
3790 else
3792 if (chain_circular != NULL)
3793 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3794 else
3795 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3796 if (wtd->param_prefix)
3798 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3799 output_mangled_typename (d.of, orig_s);
3801 oprintf (d.of, "))\n");
3802 if (chain_circular != NULL)
3803 oprintf (d.of, " return;\n do\n");
3804 if (mark_hook_name && !wtd->skip_hooks)
3806 oprintf (d.of, " {\n");
3807 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
3809 oprintf (d.of, " xlimit = (");
3810 d.prev_val[2] = "*xlimit";
3811 output_escaped_param (&d, chain_next, "chain_next");
3812 oprintf (d.of, ");\n");
3813 if (mark_hook_name && !wtd->skip_hooks)
3814 oprintf (d.of, " }\n");
3815 if (chain_prev != NULL)
3817 oprintf (d.of, " if (x != xlimit)\n");
3818 oprintf (d.of, " for (;;)\n");
3819 oprintf (d.of, " {\n");
3820 oprintf (d.of, " %s %s * const xprev = (",
3821 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3823 d.prev_val[2] = "*x";
3824 output_escaped_param (&d, chain_prev, "chain_prev");
3825 oprintf (d.of, ");\n");
3826 oprintf (d.of, " if (xprev == NULL) break;\n");
3827 oprintf (d.of, " x = xprev;\n");
3828 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3829 if (wtd->param_prefix)
3831 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3832 output_mangled_typename (d.of, orig_s);
3834 oprintf (d.of, ");\n");
3835 oprintf (d.of, " }\n");
3837 if (chain_circular != NULL)
3839 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3840 if (wtd->param_prefix)
3842 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3843 output_mangled_typename (d.of, orig_s);
3845 oprintf (d.of, "));\n");
3846 if (mark_hook_name && !wtd->skip_hooks)
3847 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
3848 oprintf (d.of, " do\n");
3850 else
3851 oprintf (d.of, " while (x != xlimit)\n");
3853 oprintf (d.of, " {\n");
3854 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3856 oprintf (d.of, " %s (x);\n", mark_hook_name);
3859 d.prev_val[2] = "*x";
3860 d.indent = 6;
3861 if (orig_s->kind != TYPE_USER_STRUCT)
3862 walk_type (s, &d);
3863 else
3865 /* User structures have no fields to walk. Simply generate a call
3866 to the user-provided structure marker. */
3867 oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3870 if (chain_next != NULL)
3872 oprintf (d.of, " x = (");
3873 output_escaped_param (&d, chain_next, "chain_next");
3874 oprintf (d.of, ");\n");
3877 oprintf (d.of, " }\n");
3878 if (chain_circular != NULL)
3879 oprintf (d.of, " while (x != xlimit);\n");
3880 oprintf (d.of, "}\n");
3882 if (orig_s->kind == TYPE_USER_STRUCT)
3883 write_user_marking_functions (orig_s, wtd, &d);
3885 if (for_user)
3887 write_user_func_for_structure_body (orig_s, wtd->prefix, &d);
3888 write_user_func_for_structure_ptr (d.of, orig_s, wtd);
3893 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3895 static void
3896 write_types (outf_p output_header, type_p structures, type_p param_structs,
3897 const struct write_types_data *wtd)
3899 int nbfun = 0; /* Count the emitted functions. */
3900 type_p s;
3902 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3904 /* We first emit the macros and the declarations. Functions' code is
3905 emitted afterwards. This is needed in plugin mode. */
3906 oprintf (output_header, "/* Macros and declarations. */\n");
3907 for (s = structures; s; s = s->next)
3908 /* Do not emit handlers for derived classes; we only ever deal with
3909 the ultimate base class within an inheritance hierarchy. */
3910 if ((s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3911 && !s->u.s.base_class)
3913 options_p opt;
3915 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3916 continue;
3918 const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3920 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3921 output_mangled_typename (output_header, s);
3922 oprintf (output_header, "(X) do { \\\n");
3923 oprintf (output_header,
3924 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3925 s_id_for_tag);
3926 oprintf (output_header, " } while (0)\n");
3928 for (opt = s->u.s.opt; opt; opt = opt->next)
3929 if (strcmp (opt->name, "ptr_alias") == 0
3930 && opt->kind == OPTION_TYPE)
3932 const_type_p const t = (const_type_p) opt->info.type;
3933 if (t->kind == TYPE_STRUCT
3934 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3936 const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3937 oprintf (output_header,
3938 "#define gt_%sx_%s gt_%sx_%s\n",
3939 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3940 if (t_id_for_tag != t->u.s.tag)
3941 free (CONST_CAST (char *, t_id_for_tag));
3943 else
3944 error_at_line (&s->u.s.line,
3945 "structure alias is not a structure");
3946 break;
3948 if (opt)
3949 continue;
3951 /* Declare the marker procedure only once. */
3952 oprintf (output_header,
3953 "extern void gt_%sx_%s (void *);\n",
3954 wtd->prefix, s_id_for_tag);
3956 if (s_id_for_tag != s->u.s.tag)
3957 free (CONST_CAST (char *, s_id_for_tag));
3959 if (s->u.s.line.file == NULL)
3961 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3962 s->u.s.tag);
3963 continue;
3967 for (s = param_structs; s; s = s->next)
3968 if (s->gc_used == GC_POINTED_TO)
3970 type_p stru = s->u.param_struct.stru;
3972 /* Declare the marker procedure. */
3973 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
3974 output_mangled_typename (output_header, s);
3975 oprintf (output_header, " (void *);\n");
3977 if (stru->u.s.line.file == NULL)
3979 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3980 stru->u.s.tag);
3981 continue;
3985 /* At last we emit the functions code. */
3986 oprintf (output_header, "\n/* functions code */\n");
3987 for (s = structures; s; s = s->next)
3988 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3990 options_p opt;
3992 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3993 continue;
3994 for (opt = s->u.s.opt; opt; opt = opt->next)
3995 if (strcmp (opt->name, "ptr_alias") == 0)
3996 break;
3997 if (opt)
3998 continue;
4000 if (s->kind == TYPE_LANG_STRUCT)
4002 type_p ss;
4003 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4005 nbfun++;
4006 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
4007 nbfun, (void*) ss, ss->u.s.tag);
4008 write_func_for_structure (s, ss, NULL, wtd);
4011 else
4013 nbfun++;
4014 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
4015 nbfun, (void*) s, s->u.s.tag);
4016 write_func_for_structure (s, s, NULL, wtd);
4019 else
4021 /* Structure s is not possibly pointed to, so can be ignored. */
4022 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
4023 (void*)s, s->u.s.tag,
4024 (int) s->gc_used);
4027 for (s = param_structs; s; s = s->next)
4028 if (s->gc_used == GC_POINTED_TO)
4030 type_p *param = s->u.param_struct.param;
4031 type_p stru = s->u.param_struct.stru;
4032 if (stru->u.s.line.file == NULL)
4033 continue;
4034 if (stru->kind == TYPE_LANG_STRUCT)
4036 type_p ss;
4037 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
4039 nbfun++;
4040 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
4041 nbfun, (void*) ss, ss->u.s.tag);
4042 write_func_for_structure (s, ss, param, wtd);
4045 else
4047 nbfun++;
4048 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
4049 nbfun, (void*) s,
4050 (void*) stru, stru->u.s.tag);
4051 write_func_for_structure (s, stru, param, wtd);
4054 else
4056 /* Param structure s is not pointed to, so should be ignored. */
4057 DBGPRINTF ("ignored s @ %p", (void*)s);
4059 if (verbosity_level >= 2)
4060 printf ("%s emitted %d routines for %s\n",
4061 progname, nbfun, wtd->comment);
4064 static const struct write_types_data ggc_wtd = {
4065 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
4066 "GC marker procedures. ",
4067 FALSE, WTK_GGC
4070 static const struct write_types_data pch_wtd = {
4071 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
4072 "gt_pch_note_reorder",
4073 "PCH type-walking procedures. ",
4074 TRUE, WTK_PCH
4077 /* Write out the local pointer-walking routines. */
4079 /* process_field routine for local pointer-walking for user-callable
4080 routines. The difference between this and
4081 write_types_local_process_field is that, in this case, we do not
4082 need to check whether the given pointer matches the address of the
4083 parent structure. This check was already generated by the call
4084 to gt_pch_nx in the main gt_pch_p_*() function that is calling
4085 this code. */
4087 static void
4088 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
4090 switch (f->kind)
4092 case TYPE_POINTER:
4093 case TYPE_STRUCT:
4094 case TYPE_UNION:
4095 case TYPE_LANG_STRUCT:
4096 case TYPE_PARAM_STRUCT:
4097 case TYPE_STRING:
4098 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4099 break;
4101 case TYPE_USER_STRUCT:
4102 if (d->in_ptr_field)
4103 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4104 else
4105 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4106 d->indent, "", d->val);
4107 break;
4109 case TYPE_SCALAR:
4110 break;
4112 case TYPE_ARRAY:
4113 case TYPE_NONE:
4114 case TYPE_UNDEFINED:
4115 gcc_unreachable ();
4120 /* Write a function to PCH walk all the fields of type S on OF.
4121 D contains data needed by walk_type to recurse into the fields of S. */
4123 static void
4124 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
4126 oprintf (d->of, "\nvoid\n");
4127 oprintf (d->of, "gt_pch_nx (");
4128 write_type_decl (d->of, s);
4129 oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
4130 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4131 "\tATTRIBUTE_UNUSED void *cookie)\n");
4132 oprintf (d->of, "{\n");
4133 d->val = "(*x)";
4134 d->indent = 2;
4135 d->process_field = write_types_local_user_process_field;
4136 walk_type (s, d);
4137 oprintf (d->of, "}\n");
4141 /* Emit the user-callable functions needed to mark all the types used
4142 by the user structure S. PREFIX is the prefix to use to
4143 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
4144 chain_next option defined. D contains data needed to pass to
4145 walk_type when traversing the fields of a type.
4147 For every type T referenced by S, two routines are generated: one
4148 that takes 'T *', marks the pointer and calls the second routine,
4149 which just marks the fields of T. */
4151 static void
4152 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
4154 gcc_assert (s->kind == TYPE_USER_STRUCT);
4156 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
4158 type_p fld_type = fld->type;
4159 if (union_or_struct_p (fld_type))
4160 write_pch_user_walking_for_structure_body (fld_type, d);
4165 /* process_field routine for local pointer-walking. */
4167 static void
4168 write_types_local_process_field (type_p f, const struct walk_type_data *d)
4170 gcc_assert (d->have_this_obj);
4171 switch (f->kind)
4173 case TYPE_POINTER:
4174 case TYPE_STRUCT:
4175 case TYPE_UNION:
4176 case TYPE_LANG_STRUCT:
4177 case TYPE_PARAM_STRUCT:
4178 case TYPE_STRING:
4179 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
4180 d->prev_val[3]);
4181 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4182 break;
4184 case TYPE_USER_STRUCT:
4185 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
4186 d->prev_val[3]);
4187 if (d->in_ptr_field)
4188 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4189 else
4190 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4191 d->indent, "", d->val);
4192 break;
4194 case TYPE_SCALAR:
4195 break;
4197 case TYPE_ARRAY:
4198 case TYPE_NONE:
4199 case TYPE_UNDEFINED:
4200 gcc_unreachable ();
4205 /* For S, a structure that's part of ORIG_S, and using parameters
4206 PARAM, write out a routine that:
4207 - Is of type gt_note_pointers
4208 - Calls PROCESS_FIELD on each field of S or its substructures.
4211 static void
4212 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
4214 struct walk_type_data d;
4216 /* Don't write fns for subclasses, only for the ultimate base class
4217 within an inheritance hierarchy. */
4218 if (s->u.s.base_class)
4219 return;
4221 memset (&d, 0, sizeof (d));
4222 d.of = get_output_file_for_structure (s, param);
4223 d.process_field = write_types_local_process_field;
4224 d.opt = s->u.s.opt;
4225 d.line = &s->u.s.line;
4226 d.bitmap = s->u.s.bitmap;
4227 d.param = param;
4228 d.prev_val[0] = d.prev_val[2] = "*x";
4229 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
4230 d.prev_val[3] = "x";
4231 d.val = "(*x)";
4232 d.fn_wants_lvalue = true;
4234 oprintf (d.of, "\n");
4235 oprintf (d.of, "void\n");
4236 oprintf (d.of, "gt_pch_p_");
4237 output_mangled_typename (d.of, orig_s);
4238 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
4239 "\tvoid *x_p,\n"
4240 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4241 "\tATTRIBUTE_UNUSED void *cookie)\n");
4242 oprintf (d.of, "{\n");
4243 oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
4244 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
4245 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
4246 d.indent = 2;
4247 d.have_this_obj = true;
4249 if (s->kind != TYPE_USER_STRUCT)
4250 walk_type (s, &d);
4251 else
4253 /* User structures have no fields to walk. Simply generate a
4254 call to the user-provided PCH walker. */
4255 oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
4256 d.prev_val[3]);
4257 oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4258 d.indent, "", d.val);
4261 oprintf (d.of, "}\n");
4263 /* Write user-callable entry points for the PCH walking routines. */
4264 if (orig_s->kind == TYPE_USER_STRUCT)
4265 write_pch_user_walking_functions (s, &d);
4267 for (options_p o = s->u.s.opt; o; o = o->next)
4268 if (strcmp (o->name, "for_user") == 0)
4270 write_pch_user_walking_for_structure_body (s, &d);
4271 break;
4275 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
4277 static void
4278 write_local (outf_p output_header, type_p structures, type_p param_structs)
4280 type_p s;
4282 if (!output_header)
4283 return;
4285 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
4286 for (s = structures; s; s = s->next)
4287 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
4289 options_p opt;
4291 if (s->u.s.line.file == NULL)
4292 continue;
4293 for (opt = s->u.s.opt; opt; opt = opt->next)
4294 if (strcmp (opt->name, "ptr_alias") == 0
4295 && opt->kind == OPTION_TYPE)
4297 const_type_p const t = (const_type_p) opt->info.type;
4298 if (t->kind == TYPE_STRUCT
4299 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
4301 oprintf (output_header, "#define gt_pch_p_");
4302 output_mangled_typename (output_header, s);
4303 oprintf (output_header, " gt_pch_p_");
4304 output_mangled_typename (output_header, t);
4305 oprintf (output_header, "\n");
4307 else
4308 error_at_line (&s->u.s.line,
4309 "structure alias is not a structure");
4310 break;
4312 if (opt)
4313 continue;
4315 /* Declare the marker procedure only once. */
4316 oprintf (output_header, "extern void gt_pch_p_");
4317 output_mangled_typename (output_header, s);
4318 oprintf (output_header,
4319 "\n (void *, void *, gt_pointer_operator, void *);\n");
4321 if (s->kind == TYPE_LANG_STRUCT)
4323 type_p ss;
4324 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4325 write_local_func_for_structure (s, ss, NULL);
4327 else
4328 write_local_func_for_structure (s, s, NULL);
4331 for (s = param_structs; s; s = s->next)
4332 if (s->gc_used == GC_POINTED_TO)
4334 type_p *param = s->u.param_struct.param;
4335 type_p stru = s->u.param_struct.stru;
4337 /* Declare the marker procedure. */
4338 oprintf (output_header, "extern void gt_pch_p_");
4339 output_mangled_typename (output_header, s);
4340 oprintf (output_header,
4341 "\n (void *, void *, gt_pointer_operator, void *);\n");
4343 if (stru->u.s.line.file == NULL)
4345 fprintf (stderr, "warning: structure `%s' used but not defined\n",
4346 stru->u.s.tag);
4347 continue;
4350 if (stru->kind == TYPE_LANG_STRUCT)
4352 type_p ss;
4353 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
4354 write_local_func_for_structure (s, ss, param);
4356 else
4357 write_local_func_for_structure (s, stru, param);
4361 /* Nonzero if S is a type for which typed GC allocators should be output. */
4363 #define USED_BY_TYPED_GC_P(s) \
4364 ((s->kind == TYPE_POINTER \
4365 && (s->u.p->gc_used == GC_POINTED_TO \
4366 || s->u.p->gc_used == GC_USED)) \
4367 || (union_or_struct_p (s) \
4368 && ((s)->gc_used == GC_POINTED_TO \
4369 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4370 && s->u.s.line.file != NULL) \
4371 || ((s)->gc_used == GC_USED \
4372 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))) \
4373 || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
4377 /* Might T contain any non-pointer elements? */
4379 static int
4380 contains_scalar_p (type_p t)
4382 switch (t->kind)
4384 case TYPE_STRING:
4385 case TYPE_POINTER:
4386 return 0;
4387 case TYPE_ARRAY:
4388 return contains_scalar_p (t->u.a.p);
4389 case TYPE_USER_STRUCT:
4390 /* User-marked structures will typically contain pointers. */
4391 return 0;
4392 default:
4393 /* Could also check for structures that have no non-pointer
4394 fields, but there aren't enough of those to worry about. */
4395 return 1;
4399 /* Mangle INPF and print it to F. */
4401 static void
4402 put_mangled_filename (outf_p f, const input_file *inpf)
4404 /* The call to get_output_file_name may indirectly update fn since
4405 get_output_file_with_visibility caches its result inside, so we
4406 need the CONST_CAST. */
4407 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4408 if (!f || !name)
4409 return;
4410 for (; *name != 0; name++)
4411 if (ISALNUM (*name))
4412 oprintf (f, "%c", *name);
4413 else
4414 oprintf (f, "%c", '_');
4417 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4418 LASTNAME, and NAME are all strings to insert in various places in
4419 the resulting code. */
4421 static void
4422 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4423 const char *tname, const char *name)
4425 struct flist *fli2;
4427 for (fli2 = flp; fli2; fli2 = fli2->next)
4428 if (fli2->started_p)
4430 oprintf (fli2->f, " %s\n", lastname);
4431 oprintf (fli2->f, "};\n\n");
4434 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4435 if (fli2->started_p)
4437 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4438 int fnum;
4440 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4441 if (bitmap & 1)
4443 oprintf (base_files[fnum],
4444 "extern const struct %s gt_%s_", tname, pfx);
4445 put_mangled_filename (base_files[fnum], fli2->file);
4446 oprintf (base_files[fnum], "[];\n");
4451 size_t fnum;
4452 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4453 oprintf (base_files[fnum],
4454 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4458 for (fli2 = flp; fli2; fli2 = fli2->next)
4459 if (fli2->started_p)
4461 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4462 int fnum;
4464 fli2->started_p = 0;
4466 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4467 if (bitmap & 1)
4469 oprintf (base_files[fnum], " gt_%s_", pfx);
4470 put_mangled_filename (base_files[fnum], fli2->file);
4471 oprintf (base_files[fnum], ",\n");
4476 size_t fnum;
4477 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4479 oprintf (base_files[fnum], " NULL\n");
4480 oprintf (base_files[fnum], "};\n");
4485 /* Write the first three fields (pointer, count and stride) for
4486 root NAME to F. V and LINE are as for write_root.
4488 Return true if the entry could be written; return false on error. */
4490 static bool
4491 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4493 type_p ap;
4495 if (!v)
4497 error_at_line (line, "`%s' is too complex to be a root", name);
4498 return false;
4501 oprintf (f, " {\n");
4502 oprintf (f, " &%s,\n", name);
4503 oprintf (f, " 1");
4505 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4506 if (ap->u.a.len[0])
4507 oprintf (f, " * (%s)", ap->u.a.len);
4508 else if (ap == v->type)
4509 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4510 oprintf (f, ",\n");
4511 oprintf (f, " sizeof (%s", v->name);
4512 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4513 oprintf (f, "[0]");
4514 oprintf (f, "),\n");
4515 return true;
4518 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4519 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4520 of the caller. */
4522 static void
4523 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4524 int has_length, struct fileloc *line, const char *if_marked,
4525 bool emit_pch, type_p field_type, const char *field_name)
4527 struct pair newv;
4528 /* If the field reference is relative to V, rather than to some
4529 subcomponent of V, we can mark any subarrays with a single stride.
4530 We're effectively treating the field as a global variable in its
4531 own right. */
4532 if (v && type == v->type)
4534 newv = *v;
4535 newv.type = field_type;
4536 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4537 v = &newv;
4539 /* Otherwise, any arrays nested in the structure are too complex to
4540 handle. */
4541 else if (field_type->kind == TYPE_ARRAY)
4542 v = NULL;
4543 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4544 has_length, line, if_marked, emit_pch);
4547 /* Write out to F the table entry and any marker routines needed to
4548 mark NAME as TYPE. V can be one of three values:
4550 - null, if NAME is too complex to represent using a single
4551 count and stride. In this case, it is an error for NAME to
4552 contain any gc-ed data.
4554 - the outermost array that contains NAME, if NAME is part of an array.
4556 - the C variable that contains NAME, if NAME is not part of an array.
4558 LINE is the line of the C source that declares the root variable.
4559 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
4560 is nonzero iff we are building the root table for hash table caches. */
4562 static void
4563 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4564 struct fileloc *line, const char *if_marked, bool emit_pch)
4566 switch (type->kind)
4568 case TYPE_STRUCT:
4570 pair_p fld;
4571 for (fld = type->u.s.fields; fld; fld = fld->next)
4573 int skip_p = 0;
4574 const char *desc = NULL;
4575 options_p o;
4577 for (o = fld->opt; o; o = o->next)
4578 if (strcmp (o->name, "skip") == 0)
4579 skip_p = 1;
4580 else if (strcmp (o->name, "desc") == 0
4581 && o->kind == OPTION_STRING)
4582 desc = o->info.string;
4583 else if (strcmp (o->name, "param_is") == 0)
4585 else
4586 error_at_line (line,
4587 "field `%s' of global `%s' has unknown option `%s'",
4588 fld->name, name, o->name);
4590 if (skip_p)
4591 continue;
4592 else if (desc && fld->type->kind == TYPE_UNION)
4594 pair_p validf = NULL;
4595 pair_p ufld;
4597 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4599 const char *tag = NULL;
4600 options_p oo;
4601 for (oo = ufld->opt; oo; oo = oo->next)
4602 if (strcmp (oo->name, "tag") == 0
4603 && oo->kind == OPTION_STRING)
4604 tag = oo->info.string;
4605 if (tag == NULL || strcmp (tag, desc) != 0)
4606 continue;
4607 if (validf != NULL)
4608 error_at_line (line,
4609 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4610 name, fld->name, validf->name,
4611 name, fld->name, ufld->name, tag);
4612 validf = ufld;
4614 if (validf != NULL)
4615 write_field_root (f, v, type, name, 0, line, if_marked,
4616 emit_pch, validf->type,
4617 ACONCAT ((fld->name, ".",
4618 validf->name, NULL)));
4620 else if (desc)
4621 error_at_line (line,
4622 "global `%s.%s' has `desc' option but is not union",
4623 name, fld->name);
4624 else
4625 write_field_root (f, v, type, name, 0, line, if_marked,
4626 emit_pch, fld->type, fld->name);
4629 break;
4631 case TYPE_ARRAY:
4633 char *newname;
4634 newname = xasprintf ("%s[0]", name);
4635 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
4636 emit_pch);
4637 free (newname);
4639 break;
4641 case TYPE_USER_STRUCT:
4642 error_at_line (line, "`%s' must be a pointer type, because it is "
4643 "a GC root and its type is marked with GTY((user))",
4644 v->name);
4645 break;
4647 case TYPE_POINTER:
4649 const_type_p tp;
4651 if (!start_root_entry (f, v, name, line))
4652 return;
4654 tp = type->u.p;
4656 if (!has_length && union_or_struct_p (tp))
4658 tp = get_ultimate_base_class (tp);
4659 const char *id_for_tag = filter_type_name (tp->u.s.tag);
4660 oprintf (f, " &gt_ggc_mx_%s,\n", id_for_tag);
4661 if (emit_pch)
4662 oprintf (f, " &gt_pch_nx_%s", id_for_tag);
4663 else
4664 oprintf (f, " NULL");
4665 if (id_for_tag != tp->u.s.tag)
4666 free (CONST_CAST (char *, id_for_tag));
4668 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
4670 oprintf (f, " &gt_ggc_m_");
4671 output_mangled_typename (f, tp);
4672 if (emit_pch)
4674 oprintf (f, ",\n &gt_pch_n_");
4675 output_mangled_typename (f, tp);
4677 else
4678 oprintf (f, ",\n NULL");
4680 else if (has_length
4681 && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
4683 oprintf (f, " &gt_ggc_ma_%s,\n", name);
4684 if (emit_pch)
4685 oprintf (f, " &gt_pch_na_%s", name);
4686 else
4687 oprintf (f, " NULL");
4689 else
4691 error_at_line (line,
4692 "global `%s' is pointer to unimplemented type",
4693 name);
4695 if (if_marked)
4696 oprintf (f, ",\n &%s", if_marked);
4697 oprintf (f, "\n },\n");
4699 break;
4701 case TYPE_STRING:
4703 if (!start_root_entry (f, v, name, line))
4704 return;
4706 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
4707 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
4708 oprintf (f, " },\n");
4710 break;
4712 case TYPE_SCALAR:
4713 break;
4715 case TYPE_NONE:
4716 case TYPE_UNDEFINED:
4717 case TYPE_UNION:
4718 case TYPE_LANG_STRUCT:
4719 case TYPE_PARAM_STRUCT:
4720 error_at_line (line, "global `%s' is unimplemented type", name);
4724 /* This generates a routine to walk an array. */
4726 static void
4727 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4729 struct walk_type_data d;
4730 char *prevval3;
4732 memset (&d, 0, sizeof (d));
4733 d.of = f;
4734 d.cookie = wtd;
4735 d.indent = 2;
4736 d.line = &v->line;
4737 d.opt = v->opt;
4738 d.bitmap = get_lang_bitmap (v->line.file);
4739 d.param = NULL;
4741 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4743 if (wtd->param_prefix)
4745 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4746 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
4747 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4748 wtd->param_prefix, v->name);
4749 oprintf (d.of,
4750 " ATTRIBUTE_UNUSED void *x_p,\n"
4751 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4752 " ATTRIBUTE_UNUSED void * cookie)\n");
4753 oprintf (d.of, "{\n");
4754 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4755 d.process_field = write_types_local_process_field;
4756 d.have_this_obj = true;
4757 walk_type (v->type, &d);
4758 oprintf (f, "}\n\n");
4761 d.opt = v->opt;
4762 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4763 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4764 wtd->prefix, v->name);
4765 oprintf (f, "{\n");
4766 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4767 d.process_field = write_types_process_field;
4768 d.have_this_obj = false;
4769 walk_type (v->type, &d);
4770 free (prevval3);
4771 oprintf (f, "}\n\n");
4774 /* Output a table describing the locations and types of VARIABLES. */
4776 static void
4777 write_roots (pair_p variables, bool emit_pch)
4779 pair_p v;
4780 struct flist *flp = NULL;
4782 for (v = variables; v; v = v->next)
4784 outf_p f =
4785 get_output_file_with_visibility (CONST_CAST (input_file*,
4786 v->line.file));
4787 struct flist *fli;
4788 const char *length = NULL;
4789 int deletable_p = 0;
4790 options_p o;
4791 for (o = v->opt; o; o = o->next)
4792 if (strcmp (o->name, "length") == 0
4793 && o->kind == OPTION_STRING)
4794 length = o->info.string;
4795 else if (strcmp (o->name, "deletable") == 0)
4796 deletable_p = 1;
4797 else if (strcmp (o->name, "param_is") == 0)
4799 else if (strncmp (o->name, "param", 5) == 0
4800 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
4802 else if (strcmp (o->name, "if_marked") == 0)
4804 else
4805 error_at_line (&v->line,
4806 "global `%s' has unknown option `%s'",
4807 v->name, o->name);
4809 for (fli = flp; fli; fli = fli->next)
4810 if (fli->f == f && f)
4811 break;
4812 if (fli == NULL)
4814 fli = XNEW (struct flist);
4815 fli->f = f;
4816 fli->next = flp;
4817 fli->started_p = 0;
4818 fli->file = v->line.file;
4819 gcc_assert (fli->file);
4820 flp = fli;
4822 oprintf (f, "\n/* GC roots. */\n\n");
4825 if (!deletable_p
4826 && length
4827 && v->type->kind == TYPE_POINTER
4828 && (v->type->u.p->kind == TYPE_POINTER
4829 || v->type->u.p->kind == TYPE_STRUCT))
4831 write_array (f, v, &ggc_wtd);
4832 write_array (f, v, &pch_wtd);
4836 for (v = variables; v; v = v->next)
4838 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4839 v->line.file));
4840 struct flist *fli;
4841 int skip_p = 0;
4842 int length_p = 0;
4843 options_p o;
4845 for (o = v->opt; o; o = o->next)
4846 if (strcmp (o->name, "length") == 0)
4847 length_p = 1;
4848 else if (strcmp (o->name, "deletable") == 0
4849 || strcmp (o->name, "if_marked") == 0)
4850 skip_p = 1;
4852 if (skip_p)
4853 continue;
4855 for (fli = flp; fli; fli = fli->next)
4856 if (fli->f == f)
4857 break;
4858 if (!fli->started_p)
4860 fli->started_p = 1;
4862 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4863 put_mangled_filename (f, v->line.file);
4864 oprintf (f, "[] = {\n");
4867 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4870 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4871 "gt_ggc_rtab");
4873 for (v = variables; v; v = v->next)
4875 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4876 v->line.file));
4877 struct flist *fli;
4878 int skip_p = 1;
4879 options_p o;
4881 for (o = v->opt; o; o = o->next)
4882 if (strcmp (o->name, "deletable") == 0)
4883 skip_p = 0;
4884 else if (strcmp (o->name, "if_marked") == 0)
4885 skip_p = 1;
4887 if (skip_p)
4888 continue;
4890 for (fli = flp; fli; fli = fli->next)
4891 if (fli->f == f)
4892 break;
4893 if (!fli->started_p)
4895 fli->started_p = 1;
4897 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4898 put_mangled_filename (f, v->line.file);
4899 oprintf (f, "[] = {\n");
4902 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4903 v->name, v->name);
4906 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4907 "gt_ggc_deletable_rtab");
4909 for (v = variables; v; v = v->next)
4911 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4912 v->line.file));
4913 struct flist *fli;
4914 const char *if_marked = NULL;
4915 int length_p = 0;
4916 options_p o;
4918 for (o = v->opt; o; o = o->next)
4919 if (strcmp (o->name, "length") == 0)
4920 length_p = 1;
4921 else if (strcmp (o->name, "if_marked") == 0
4922 && o->kind == OPTION_STRING)
4923 if_marked = o->info.string;
4924 if (if_marked == NULL)
4925 continue;
4926 if (v->type->kind != TYPE_POINTER
4927 || v->type->u.p->kind != TYPE_PARAM_STRUCT
4928 || v->type->u.p->u.param_struct.stru != find_structure ("htab",
4929 TYPE_STRUCT))
4931 error_at_line (&v->line,
4932 "if_marked option used but not hash table");
4933 continue;
4936 for (fli = flp; fli; fli = fli->next)
4937 if (fli->f == f)
4938 break;
4939 if (!fli->started_p)
4941 fli->started_p = 1;
4943 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4944 put_mangled_filename (f, v->line.file);
4945 oprintf (f, "[] = {\n");
4948 write_root (f, v, v->type->u.p->u.param_struct.param[0],
4949 v->name, length_p, &v->line, if_marked, emit_pch);
4952 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4953 "gt_ggc_cache_rtab");
4955 if (!emit_pch)
4956 return;
4958 for (v = variables; v; v = v->next)
4960 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4961 v->line.file));
4962 struct flist *fli;
4963 int length_p = 0;
4964 int if_marked_p = 0;
4965 options_p o;
4967 for (o = v->opt; o; o = o->next)
4968 if (strcmp (o->name, "length") == 0)
4969 length_p = 1;
4970 else if (strcmp (o->name, "if_marked") == 0)
4971 if_marked_p = 1;
4973 if (!if_marked_p)
4974 continue;
4976 for (fli = flp; fli; fli = fli->next)
4977 if (fli->f == f)
4978 break;
4979 if (!fli->started_p)
4981 fli->started_p = 1;
4983 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4984 put_mangled_filename (f, v->line.file);
4985 oprintf (f, "[] = {\n");
4988 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4991 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4992 "gt_pch_cache_rtab");
4994 for (v = variables; v; v = v->next)
4996 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4997 v->line.file));
4998 struct flist *fli;
4999 int skip_p = 0;
5000 options_p o;
5002 for (o = v->opt; o; o = o->next)
5003 if (strcmp (o->name, "deletable") == 0
5004 || strcmp (o->name, "if_marked") == 0)
5006 skip_p = 1;
5007 break;
5010 if (skip_p)
5011 continue;
5013 if (!contains_scalar_p (v->type))
5014 continue;
5016 for (fli = flp; fli; fli = fli->next)
5017 if (fli->f == f)
5018 break;
5019 if (!fli->started_p)
5021 fli->started_p = 1;
5023 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
5024 put_mangled_filename (f, v->line.file);
5025 oprintf (f, "[] = {\n");
5028 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
5029 v->name, v->name);
5032 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
5033 "gt_pch_scalar_rtab");
5036 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
5037 guaranteee for somewhat increased readability. If name conflicts do happen,
5038 this funcion will have to be adjusted to be more like
5039 output_mangled_typename. */
5041 static void
5042 output_typename (outf_p of, const_type_p t)
5044 switch (t->kind)
5046 case TYPE_STRING:
5047 oprintf (of, "str");
5048 break;
5049 case TYPE_SCALAR:
5050 oprintf (of, "scalar");
5051 break;
5052 case TYPE_POINTER:
5053 output_typename (of, t->u.p);
5054 break;
5055 case TYPE_STRUCT:
5056 case TYPE_USER_STRUCT:
5057 case TYPE_UNION:
5058 case TYPE_LANG_STRUCT:
5059 oprintf (of, "%s", t->u.s.tag);
5060 break;
5061 case TYPE_PARAM_STRUCT:
5063 int i;
5064 for (i = 0; i < NUM_PARAM; i++)
5065 if (t->u.param_struct.param[i] != NULL)
5067 output_typename (of, t->u.param_struct.param[i]);
5068 oprintf (of, "_");
5070 output_typename (of, t->u.param_struct.stru);
5071 break;
5073 case TYPE_NONE:
5074 case TYPE_UNDEFINED:
5075 case TYPE_ARRAY:
5076 gcc_unreachable ();
5080 /* Writes a typed GC allocator for type S that is suitable as a callback for
5081 the splay tree implementation in libiberty. */
5083 static void
5084 write_splay_tree_allocator_def (const_type_p s)
5086 outf_p of = get_output_file_with_visibility (NULL);
5087 oprintf (of, "void * ggc_alloc_splay_tree_");
5088 output_typename (of, s);
5089 oprintf (of, " (int sz, void * nl)\n");
5090 oprintf (of, "{\n");
5091 oprintf (of, " return ggc_splay_alloc (sz, nl);\n");
5092 oprintf (of, "}\n\n");
5095 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
5096 for the splay tree implementation in libiberty. */
5098 static void
5099 write_splay_tree_allocators (const_type_p param_structs)
5101 const_type_p s;
5103 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
5104 for (s = param_structs; s; s = s->next)
5105 if (s->gc_used == GC_POINTED_TO)
5107 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
5108 output_typename (header_file, s);
5109 oprintf (header_file, " (int, void *);\n");
5110 write_splay_tree_allocator_def (s);
5114 #define INDENT 2
5116 /* Dumps the value of typekind KIND. */
5118 static void
5119 dump_typekind (int indent, enum typekind kind)
5121 printf ("%*ckind = ", indent, ' ');
5122 switch (kind)
5124 case TYPE_SCALAR:
5125 printf ("TYPE_SCALAR");
5126 break;
5127 case TYPE_STRING:
5128 printf ("TYPE_STRING");
5129 break;
5130 case TYPE_STRUCT:
5131 printf ("TYPE_STRUCT");
5132 break;
5133 case TYPE_UNDEFINED:
5134 printf ("TYPE_UNDEFINED");
5135 break;
5136 case TYPE_USER_STRUCT:
5137 printf ("TYPE_USER_STRUCT");
5138 break;
5139 case TYPE_UNION:
5140 printf ("TYPE_UNION");
5141 break;
5142 case TYPE_POINTER:
5143 printf ("TYPE_POINTER");
5144 break;
5145 case TYPE_ARRAY:
5146 printf ("TYPE_ARRAY");
5147 break;
5148 case TYPE_LANG_STRUCT:
5149 printf ("TYPE_LANG_STRUCT");
5150 break;
5151 case TYPE_PARAM_STRUCT:
5152 printf ("TYPE_PARAM_STRUCT");
5153 break;
5154 default:
5155 gcc_unreachable ();
5157 printf ("\n");
5160 /* Dumps the value of GC_USED flag. */
5162 static void
5163 dump_gc_used (int indent, enum gc_used_enum gc_used)
5165 printf ("%*cgc_used = ", indent, ' ');
5166 switch (gc_used)
5168 case GC_UNUSED:
5169 printf ("GC_UNUSED");
5170 break;
5171 case GC_USED:
5172 printf ("GC_USED");
5173 break;
5174 case GC_MAYBE_POINTED_TO:
5175 printf ("GC_MAYBE_POINTED_TO");
5176 break;
5177 case GC_POINTED_TO:
5178 printf ("GC_POINTED_TO");
5179 break;
5180 default:
5181 gcc_unreachable ();
5183 printf ("\n");
5186 /* Dumps the type options OPT. */
5188 static void
5189 dump_options (int indent, options_p opt)
5191 options_p o;
5192 printf ("%*coptions = ", indent, ' ');
5193 o = opt;
5194 while (o)
5196 switch (o->kind)
5198 case OPTION_STRING:
5199 printf ("%s:string %s ", o->name, o->info.string);
5200 break;
5201 case OPTION_TYPE:
5202 printf ("%s:type ", o->name);
5203 dump_type (indent+1, o->info.type);
5204 break;
5205 case OPTION_NESTED:
5206 printf ("%s:nested ", o->name);
5207 break;
5208 case OPTION_NONE:
5209 gcc_unreachable ();
5211 o = o->next;
5213 printf ("\n");
5216 /* Dumps the source file location in LINE. */
5218 static void
5219 dump_fileloc (int indent, struct fileloc line)
5221 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
5222 get_input_file_name (line.file),
5223 line.line);
5226 /* Recursively dumps the struct, union, or a language-specific
5227 struct T. */
5229 static void
5230 dump_type_u_s (int indent, type_p t)
5232 pair_p fields;
5234 gcc_assert (union_or_struct_p (t));
5235 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
5236 dump_fileloc (indent, t->u.s.line);
5237 printf ("%*cu.s.fields =\n", indent, ' ');
5238 fields = t->u.s.fields;
5239 while (fields)
5241 dump_pair (indent + INDENT, fields);
5242 fields = fields->next;
5244 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
5245 dump_options (indent, t->u.s.opt);
5246 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
5247 if (t->kind == TYPE_LANG_STRUCT)
5249 printf ("%*cu.s.lang_struct:\n", indent, ' ');
5250 dump_type_list (indent + INDENT, t->u.s.lang_struct);
5254 /* Recursively dumps the array T. */
5256 static void
5257 dump_type_u_a (int indent, type_p t)
5259 gcc_assert (t->kind == TYPE_ARRAY);
5260 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
5261 dump_type_list (indent + INDENT, t->u.a.p);
5264 /* Recursively dumps the parameterized struct T. */
5266 static void
5267 dump_type_u_param_struct (int indent, type_p t)
5269 int i;
5270 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
5271 printf ("%*cu.param_struct.stru:\n", indent, ' ');
5272 dump_type_list (indent, t->u.param_struct.stru);
5273 dump_fileloc (indent, t->u.param_struct.line);
5274 for (i = 0; i < NUM_PARAM; i++)
5276 if (t->u.param_struct.param[i] == NULL)
5277 continue;
5278 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
5279 dump_type (indent + INDENT, t->u.param_struct.param[i]);
5283 /* Recursively dumps the type list T. */
5285 static void
5286 dump_type_list (int indent, type_p t)
5288 type_p p = t;
5289 while (p)
5291 dump_type (indent, p);
5292 p = p->next;
5296 static htab_t seen_types;
5298 /* Recursively dumps the type T if it was not dumped previously. */
5300 static void
5301 dump_type (int indent, type_p t)
5303 PTR *slot;
5305 if (seen_types == NULL)
5306 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
5308 printf ("%*cType at %p: ", indent, ' ', (void *) t);
5309 slot = htab_find_slot (seen_types, t, INSERT);
5310 if (*slot != NULL)
5312 printf ("already seen.\n");
5313 return;
5315 *slot = t;
5316 printf ("\n");
5318 dump_typekind (indent, t->kind);
5319 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
5320 (void *) t->pointer_to);
5321 dump_gc_used (indent + INDENT, t->gc_used);
5322 switch (t->kind)
5324 case TYPE_SCALAR:
5325 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
5326 t->u.scalar_is_char ? "true" : "false");
5327 break;
5328 case TYPE_STRING:
5329 break;
5330 case TYPE_STRUCT:
5331 case TYPE_UNION:
5332 case TYPE_LANG_STRUCT:
5333 case TYPE_USER_STRUCT:
5334 dump_type_u_s (indent + INDENT, t);
5335 break;
5336 case TYPE_POINTER:
5337 printf ("%*cp:\n", indent + INDENT, ' ');
5338 dump_type (indent + INDENT, t->u.p);
5339 break;
5340 case TYPE_ARRAY:
5341 dump_type_u_a (indent + INDENT, t);
5342 break;
5343 case TYPE_PARAM_STRUCT:
5344 dump_type_u_param_struct (indent + INDENT, t);
5345 break;
5346 default:
5347 gcc_unreachable ();
5349 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
5352 /* Dumps the pair P. */
5354 static void
5355 dump_pair (int indent, pair_p p)
5357 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
5358 dump_type (indent, p->type);
5359 dump_fileloc (indent, p->line);
5360 dump_options (indent, p->opt);
5361 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
5364 /* Dumps the list of pairs PP. */
5366 static void
5367 dump_pair_list (const char *name, pair_p pp)
5369 pair_p p;
5370 printf ("%s:\n", name);
5371 for (p = pp; p != NULL; p = p->next)
5372 dump_pair (0, p);
5373 printf ("End of %s\n\n", name);
5376 /* Dumps the STRUCTURES. */
5378 static void
5379 dump_structures (const char *name, type_p structures)
5381 printf ("%s:\n", name);
5382 dump_type_list (0, structures);
5383 printf ("End of %s\n\n", name);
5386 /* Dumps the internal structures of gengtype. This is useful to debug
5387 gengtype itself, or to understand what it does, e.g. for plugin
5388 developers. */
5390 static void
5391 dump_everything (void)
5393 dump_pair_list ("typedefs", typedefs);
5394 dump_structures ("structures", structures);
5395 dump_structures ("param_structs", param_structs);
5396 dump_pair_list ("variables", variables);
5398 /* Allocated with the first call to dump_type. */
5399 htab_delete (seen_types);
5404 /* Option specification for getopt_long. */
5405 static const struct option gengtype_long_options[] = {
5406 {"help", no_argument, NULL, 'h'},
5407 {"version", no_argument, NULL, 'V'},
5408 {"verbose", no_argument, NULL, 'v'},
5409 {"dump", no_argument, NULL, 'd'},
5410 {"debug", no_argument, NULL, 'D'},
5411 {"plugin", required_argument, NULL, 'P'},
5412 {"srcdir", required_argument, NULL, 'S'},
5413 {"backupdir", required_argument, NULL, 'B'},
5414 {"inputs", required_argument, NULL, 'I'},
5415 {"read-state", required_argument, NULL, 'r'},
5416 {"write-state", required_argument, NULL, 'w'},
5417 /* Terminating NULL placeholder. */
5418 {NULL, no_argument, NULL, 0},
5422 static void
5423 print_usage (void)
5425 printf ("Usage: %s\n", progname);
5426 printf ("\t -h | --help " " \t# Give this help.\n");
5427 printf ("\t -D | --debug "
5428 " \t# Give debug output to debug %s itself.\n", progname);
5429 printf ("\t -V | --version " " \t# Give version information.\n");
5430 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5431 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5432 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5433 " \t# Generate for plugin.\n");
5434 printf ("\t -S | --srcdir <GCC-directory> "
5435 " \t# Specify the GCC source directory.\n");
5436 printf ("\t -B | --backupdir <directory> "
5437 " \t# Specify the backup directory for updated files.\n");
5438 printf ("\t -I | --inputs <input-list> "
5439 " \t# Specify the file with source files list.\n");
5440 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5441 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5444 static void
5445 print_version (void)
5447 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5448 printf ("Report bugs: %s\n", bug_report_url);
5451 /* Parse the program options using getopt_long... */
5452 static void
5453 parse_program_options (int argc, char **argv)
5455 int opt = -1;
5456 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5457 gengtype_long_options, NULL)) >= 0)
5459 switch (opt)
5461 case 'h': /* --help */
5462 print_usage ();
5463 break;
5464 case 'V': /* --version */
5465 print_version ();
5466 break;
5467 case 'd': /* --dump */
5468 do_dump = 1;
5469 break;
5470 case 'D': /* --debug */
5471 do_debug = 1;
5472 break;
5473 case 'v': /* --verbose */
5474 verbosity_level++;
5475 break;
5476 case 'P': /* --plugin */
5477 if (optarg)
5478 plugin_output_filename = optarg;
5479 else
5480 fatal ("missing plugin output file name");
5481 break;
5482 case 'S': /* --srcdir */
5483 if (optarg)
5484 srcdir = optarg;
5485 else
5486 fatal ("missing source directory");
5487 srcdir_len = strlen (srcdir);
5488 break;
5489 case 'B': /* --backupdir */
5490 if (optarg)
5491 backup_dir = optarg;
5492 else
5493 fatal ("missing backup directory");
5494 break;
5495 case 'I': /* --inputs */
5496 if (optarg)
5497 inputlist = optarg;
5498 else
5499 fatal ("missing input list");
5500 break;
5501 case 'r': /* --read-state */
5502 if (optarg)
5503 read_state_filename = optarg;
5504 else
5505 fatal ("missing read state file");
5506 DBGPRINTF ("read state %s\n", optarg);
5507 break;
5508 case 'w': /* --write-state */
5509 DBGPRINTF ("write state %s\n", optarg);
5510 if (optarg)
5511 write_state_filename = optarg;
5512 else
5513 fatal ("missing write state file");
5514 break;
5515 default:
5516 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5517 print_usage ();
5518 fatal ("unexpected flag");
5521 if (plugin_output_filename)
5523 /* In plugin mode we require some input files. */
5524 int i = 0;
5525 if (optind >= argc)
5526 fatal ("no source files given in plugin mode");
5527 nb_plugin_files = argc - optind;
5528 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5529 for (i = 0; i < (int) nb_plugin_files; i++)
5531 char *name = argv[i + optind];
5532 plugin_files[i] = input_file_by_name (name);
5539 /******* Manage input files. ******/
5541 /* Hash table of unique input file names. */
5542 static htab_t input_file_htab;
5544 /* Find or allocate a new input_file by hash-consing it. */
5545 input_file*
5546 input_file_by_name (const char* name)
5548 PTR* slot;
5549 input_file* f = NULL;
5550 int namlen = 0;
5551 if (!name)
5552 return NULL;
5553 namlen = strlen (name);
5554 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5555 f->inpbitmap = 0;
5556 f->inpoutf = NULL;
5557 f->inpisplugin = false;
5558 strcpy (f->inpname, name);
5559 slot = htab_find_slot (input_file_htab, f, INSERT);
5560 gcc_assert (slot != NULL);
5561 if (*slot)
5563 /* Already known input file. */
5564 free (f);
5565 return (input_file*)(*slot);
5567 /* New input file. */
5568 *slot = f;
5569 return f;
5572 /* Hash table support routines for input_file-s. */
5573 static hashval_t
5574 htab_hash_inputfile (const void *p)
5576 const input_file *inpf = (const input_file *) p;
5577 gcc_assert (inpf);
5578 return htab_hash_string (get_input_file_name (inpf));
5581 static int
5582 htab_eq_inputfile (const void *x, const void *y)
5584 const input_file *inpfx = (const input_file *) x;
5585 const input_file *inpfy = (const input_file *) y;
5586 gcc_assert (inpfx != NULL && inpfy != NULL);
5587 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5592 main (int argc, char **argv)
5594 size_t i;
5595 static struct fileloc pos = { NULL, 0 };
5596 outf_p output_header;
5598 /* Mandatory common initializations. */
5599 progname = "gengtype"; /* For fatal and messages. */
5600 /* Create the hash-table used to hash-cons input files. */
5601 input_file_htab =
5602 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5603 /* Initialize our special input files. */
5604 this_file = input_file_by_name (__FILE__);
5605 system_h_file = input_file_by_name ("system.h");
5606 /* Set the scalar_is_char union number for predefined scalar types. */
5607 scalar_nonchar.u.scalar_is_char = FALSE;
5608 scalar_char.u.scalar_is_char = TRUE;
5610 parse_program_options (argc, argv);
5612 #if ENABLE_CHECKING
5613 if (do_debug)
5615 time_t now = (time_t) 0;
5616 time (&now);
5617 DBGPRINTF ("gengtype started pid %d at %s",
5618 (int) getpid (), ctime (&now));
5620 #endif /* ENABLE_CHECKING */
5622 /* Parse the input list and the input files. */
5623 DBGPRINTF ("inputlist %s", inputlist);
5624 if (read_state_filename)
5626 if (inputlist)
5627 fatal ("input list %s cannot be given with a read state file %s",
5628 inputlist, read_state_filename);
5629 read_state (read_state_filename);
5630 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5631 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs);
5633 else if (inputlist)
5635 /* These types are set up with #define or else outside of where
5636 we can see them. We should initialize them before calling
5637 read_input_list. */
5638 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5639 Call;} while (0)
5640 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5641 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5642 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5643 POS_HERE (do_scalar_typedef ("double_int", &pos));
5644 POS_HERE (do_scalar_typedef ("offset_int", &pos));
5645 POS_HERE (do_scalar_typedef ("widest_int", &pos));
5646 POS_HERE (do_scalar_typedef ("int64_t", &pos));
5647 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5648 POS_HERE (do_scalar_typedef ("uint8", &pos));
5649 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5650 POS_HERE (do_scalar_typedef ("jword", &pos));
5651 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5652 POS_HERE (do_scalar_typedef ("void", &pos));
5653 POS_HERE (do_scalar_typedef ("machine_mode", &pos));
5654 POS_HERE (do_typedef ("PTR",
5655 create_pointer (resolve_typedef ("void", &pos)),
5656 &pos));
5657 #undef POS_HERE
5658 read_input_list (inputlist);
5659 for (i = 0; i < num_gt_files; i++)
5661 parse_file (get_input_file_name (gt_files[i]));
5662 DBGPRINTF ("parsed file #%d %s",
5663 (int) i, get_input_file_name (gt_files[i]));
5665 if (verbosity_level >= 1)
5666 printf ("%s parsed %d files with %d GTY types\n",
5667 progname, (int) num_gt_files, type_count);
5669 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5670 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
5673 else
5674 fatal ("either an input list or a read state file should be given");
5675 if (hit_error)
5676 return 1;
5679 if (plugin_output_filename)
5681 size_t ix = 0;
5682 /* In plugin mode, we should have read a state file, and have
5683 given at least one plugin file. */
5684 if (!read_state_filename)
5685 fatal ("No read state given in plugin mode for %s",
5686 plugin_output_filename);
5688 if (nb_plugin_files == 0 || !plugin_files)
5689 fatal ("No plugin files given in plugin mode for %s",
5690 plugin_output_filename);
5692 /* Parse our plugin files and augment the state. */
5693 for (ix = 0; ix < nb_plugin_files; ix++)
5695 input_file* pluginput = plugin_files [ix];
5696 pluginput->inpisplugin = true;
5697 parse_file (get_input_file_name (pluginput));
5699 if (hit_error)
5700 return 1;
5702 plugin_output = create_file ("GCC", plugin_output_filename);
5703 DBGPRINTF ("created plugin_output %p named %s",
5704 (void *) plugin_output, plugin_output->name);
5706 else
5707 { /* No plugin files, we are in normal mode. */
5708 if (!srcdir)
5709 fatal ("gengtype needs a source directory in normal mode");
5711 if (hit_error)
5712 return 1;
5714 gen_rtx_next ();
5716 /* The call to set_gc_used may indirectly call find_param_structure
5717 hence enlarge the param_structs list of types. */
5718 set_gc_used (variables);
5720 for (type_p t = structures; t; t = t->next)
5722 bool for_user = false;
5723 for (options_p o = t->u.s.opt; o; o = o->next)
5724 if (strcmp (o->name, "for_user") == 0)
5726 for_user = true;
5727 break;
5730 if (for_user)
5731 set_gc_used_type (t, GC_POINTED_TO, NULL);
5733 /* The state at this point is read from the state input file or by
5734 parsing source files and optionally augmented by parsing plugin
5735 source files. Write it now. */
5736 if (write_state_filename)
5738 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5739 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs);
5741 if (hit_error)
5742 fatal ("didn't write state file %s after errors",
5743 write_state_filename);
5745 DBGPRINTF ("before write_state %s", write_state_filename);
5746 write_state (write_state_filename);
5748 if (do_dump)
5749 dump_everything ();
5751 /* After having written the state file we return immediately to
5752 avoid generating any output file. */
5753 if (hit_error)
5754 return 1;
5755 else
5756 return 0;
5760 open_base_files ();
5762 output_header = plugin_output ? plugin_output : header_file;
5763 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5764 structures);
5765 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5766 param_structs);
5768 write_types (output_header, structures, param_structs, &ggc_wtd);
5769 if (plugin_files == NULL)
5771 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5772 structures);
5773 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5774 param_structs);
5775 write_types (header_file, structures, param_structs, &pch_wtd);
5776 write_local (header_file, structures, param_structs);
5778 write_splay_tree_allocators (param_structs);
5779 write_roots (variables, plugin_files == NULL);
5780 write_rtx_next ();
5781 close_output_files ();
5783 if (do_dump)
5784 dump_everything ();
5786 /* Don't bother about free-ing any input or plugin file, etc. */
5788 if (hit_error)
5789 return 1;
5790 return 0;