2014-07-29 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / gengtype.c
blobffe3f94a6ae5c05177aeabc206a8d7b363b85e7f
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);
573 /* Define TYPE_NAME to be a user defined type at location POS. */
575 type_p
576 create_user_defined_type (const char *type_name, struct fileloc *pos)
578 type_p ty = find_structure (type_name, TYPE_USER_STRUCT);
580 /* We might have already seen an incomplete decl of the given type,
581 in which case we won't have yet seen a GTY((user)), and the type will
582 only have kind "TYPE_STRUCT". Mark it as a user struct. */
583 ty->kind = TYPE_USER_STRUCT;
585 ty->u.s.line = *pos;
586 ty->u.s.bitmap = get_lang_bitmap (pos->file);
587 do_typedef (type_name, ty, pos);
589 /* If TYPE_NAME specifies a template, create references to the types
590 in the template by pretending that each type is a field of TY.
591 This is needed to make sure that the types referenced by the
592 template are marked as used. */
593 char *str = xstrdup (type_name);
594 char *open_bracket = strchr (str, '<');
595 if (open_bracket)
597 /* We only accept simple template declarations (see
598 require_template_declaration), so we only need to parse a
599 comma-separated list of strings, implicitly assumed to
600 be type names, potentially with "*" characters. */
601 char *arg = open_bracket + 1;
602 char *type_id = strtok (arg, ",>");
603 pair_p fields = 0;
604 while (type_id)
606 /* Create a new field for every type found inside the template
607 parameter list. */
609 /* Support a single trailing "*" character. */
610 const char *star = strchr (type_id, '*');
611 int is_ptr = (star != NULL);
612 size_t offset_to_star = star - type_id;
613 if (is_ptr)
614 offset_to_star = star - type_id;
616 char *field_name = xstrdup (type_id);
618 type_p arg_type;
619 if (is_ptr)
621 /* Strip off the first '*' character (and any subsequent text). */
622 *(field_name + offset_to_star) = '\0';
624 arg_type = find_structure (field_name, TYPE_STRUCT);
625 arg_type = create_pointer (arg_type);
627 else
628 arg_type = resolve_typedef (field_name, pos);
630 fields = create_field_at (fields, arg_type, field_name, 0, pos);
631 type_id = strtok (0, ",>");
634 /* Associate the field list to TY. */
635 ty->u.s.fields = fields;
637 free (str);
639 return ty;
643 /* Given a typedef name S, return its associated type. Return NULL if
644 S is not a registered type name. */
646 static type_p
647 type_for_name (const char *s)
649 pair_p p;
651 /* Special-case support for types within a "gcc::" namespace. Rather
652 than fully-supporting namespaces, simply strip off the "gcc::" prefix
653 where present. This allows us to have GTY roots of this form:
654 extern GTY(()) gcc::some_type *some_ptr;
655 where the autogenerated functions will refer to simply "some_type",
656 where they can be resolved into their namespace. */
657 if (0 == strncmp (s, "gcc::", 5))
658 s += 5;
660 for (p = typedefs; p != NULL; p = p->next)
661 if (strcmp (p->name, s) == 0)
662 return p->type;
663 return NULL;
667 /* Create an undefined type with name S and location POS. Return the
668 newly created type. */
670 static type_p
671 create_undefined_type (const char *s, struct fileloc *pos)
673 type_p ty = find_structure (s, TYPE_UNDEFINED);
674 ty->u.s.line = *pos;
675 ty->u.s.bitmap = get_lang_bitmap (pos->file);
676 do_typedef (s, ty, pos);
677 return ty;
681 /* Return the type previously defined for S. Use POS to report errors. */
683 type_p
684 resolve_typedef (const char *s, struct fileloc *pos)
686 bool is_template_instance = (strchr (s, '<') != NULL);
687 type_p p = type_for_name (s);
689 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
690 type for regular type identifiers. If the type identifier S is a
691 template instantiation, however, we treat it as a user defined
692 type.
694 FIXME, this is actually a limitation in gengtype. Supporting
695 template types and their instances would require keeping separate
696 track of the basic types definition and its instances. This
697 essentially forces all template classes in GC to be marked
698 GTY((user)). */
699 if (!p)
700 p = (is_template_instance)
701 ? create_user_defined_type (s, pos)
702 : create_undefined_type (s, pos);
704 return p;
707 /* Add SUBCLASS to head of linked list of BASE's subclasses. */
709 void add_subclass (type_p base, type_p subclass)
711 gcc_assert (union_or_struct_p (base));
712 gcc_assert (union_or_struct_p (subclass));
714 subclass->u.s.next_sibling_class = base->u.s.first_subclass;
715 base->u.s.first_subclass = subclass;
718 /* Create and return a new structure with tag NAME at POS with fields
719 FIELDS and options O. The KIND of structure must be one of
720 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
722 type_p
723 new_structure (const char *name, enum typekind kind, struct fileloc *pos,
724 pair_p fields, options_p o, type_p base_class)
726 type_p si;
727 type_p s = NULL;
728 lang_bitmap bitmap = get_lang_bitmap (pos->file);
729 bool isunion = (kind == TYPE_UNION);
731 gcc_assert (union_or_struct_p (kind));
733 for (si = structures; si != NULL; si = si->next)
734 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
736 type_p ls = NULL;
737 if (si->kind == TYPE_LANG_STRUCT)
739 ls = si;
741 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
742 if (si->u.s.bitmap == bitmap)
743 s = si;
745 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
747 ls = si;
748 type_count++;
749 si = XCNEW (struct type);
750 memcpy (si, ls, sizeof (struct type));
751 ls->kind = TYPE_LANG_STRUCT;
752 ls->u.s.lang_struct = si;
753 ls->u.s.fields = NULL;
754 si->next = NULL;
755 si->state_number = -type_count;
756 si->pointer_to = NULL;
757 si->u.s.lang_struct = ls;
759 else
760 s = si;
762 if (ls != NULL && s == NULL)
764 type_count++;
765 s = XCNEW (struct type);
766 s->state_number = -type_count;
767 s->next = ls->u.s.lang_struct;
768 ls->u.s.lang_struct = s;
769 s->u.s.lang_struct = ls;
771 break;
774 if (s == NULL)
776 type_count++;
777 s = XCNEW (struct type);
778 s->state_number = -type_count;
779 s->next = structures;
780 structures = s;
783 if (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap))
785 error_at_line (pos, "duplicate definition of '%s %s'",
786 isunion ? "union" : "struct", s->u.s.tag);
787 error_at_line (&s->u.s.line, "previous definition here");
790 s->kind = kind;
791 s->u.s.tag = name;
792 s->u.s.line = *pos;
793 s->u.s.fields = fields;
794 s->u.s.opt = o;
795 s->u.s.bitmap = bitmap;
796 if (s->u.s.lang_struct)
797 s->u.s.lang_struct->u.s.bitmap |= bitmap;
798 s->u.s.base_class = base_class;
799 if (base_class)
800 add_subclass (base_class, s);
802 return s;
805 /* Return the previously-defined structure or union with tag NAME,
806 or a new empty structure or union if none was defined previously.
807 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
808 TYPE_USER_STRUCT. */
810 type_p
811 find_structure (const char *name, enum typekind kind)
813 type_p s;
814 bool isunion = (kind == TYPE_UNION);
816 gcc_assert (kind == TYPE_UNDEFINED || union_or_struct_p (kind));
818 for (s = structures; s != NULL; s = s->next)
819 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
820 return s;
822 type_count++;
823 s = XCNEW (struct type);
824 s->next = structures;
825 s->state_number = -type_count;
826 structures = s;
827 s->kind = kind;
828 s->u.s.tag = name;
829 structures = s;
830 return s;
833 /* Return the previously-defined parameterized structure for structure
834 T and parameters PARAM, or a new parameterized empty structure or
835 union if none was defined previously. */
837 static type_p
838 find_param_structure (type_p t, type_p param[NUM_PARAM])
840 type_p res;
842 for (res = param_structs; res; res = res->next)
843 if (res->u.param_struct.stru == t
844 && memcmp (res->u.param_struct.param, param,
845 sizeof (type_p) * NUM_PARAM) == 0)
846 break;
847 if (res == NULL)
849 type_count++;
850 res = XCNEW (struct type);
851 res->kind = TYPE_PARAM_STRUCT;
852 res->next = param_structs;
853 res->state_number = -type_count;
854 param_structs = res;
855 res->u.param_struct.stru = t;
856 memcpy (res->u.param_struct.param, param, sizeof (type_p) * NUM_PARAM);
858 return res;
861 /* Return a scalar type with name NAME. */
863 type_p
864 create_scalar_type (const char *name)
866 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
867 return &scalar_char;
868 else
869 return &scalar_nonchar;
873 /* Return a pointer to T. */
875 type_p
876 create_pointer (type_p t)
878 if (!t->pointer_to)
880 type_p r = XCNEW (struct type);
881 type_count++;
882 r->state_number = -type_count;
883 r->kind = TYPE_POINTER;
884 r->u.p = t;
885 t->pointer_to = r;
887 return t->pointer_to;
890 /* Return an array of length LEN. */
892 type_p
893 create_array (type_p t, const char *len)
895 type_p v;
897 type_count++;
898 v = XCNEW (struct type);
899 v->kind = TYPE_ARRAY;
900 v->state_number = -type_count;
901 v->u.a.p = t;
902 v->u.a.len = len;
903 return v;
906 /* Return a string options structure with name NAME and info INFO.
907 NEXT is the next option in the chain. */
908 options_p
909 create_string_option (options_p next, const char *name, const char *info)
911 options_p o = XNEW (struct options);
912 o->kind = OPTION_STRING;
913 o->next = next;
914 o->name = name;
915 o->info.string = info;
916 return o;
919 /* Create a type options structure with name NAME and info INFO. NEXT
920 is the next option in the chain. */
921 options_p
922 create_type_option (options_p next, const char* name, type_p info)
924 options_p o = XNEW (struct options);
925 o->next = next;
926 o->name = name;
927 o->kind = OPTION_TYPE;
928 o->info.type = info;
929 return o;
932 /* Create a nested pointer options structure with name NAME and info
933 INFO. NEXT is the next option in the chain. */
934 options_p
935 create_nested_option (options_p next, const char* name,
936 struct nested_ptr_data* info)
938 options_p o;
939 o = XNEW (struct options);
940 o->next = next;
941 o->name = name;
942 o->kind = OPTION_NESTED;
943 o->info.nested = info;
944 return o;
947 /* Return an options structure for a "nested_ptr" option. */
948 options_p
949 create_nested_ptr_option (options_p next, type_p t,
950 const char *to, const char *from)
952 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
954 d->type = adjust_field_type (t, 0);
955 d->convert_to = to;
956 d->convert_from = from;
957 return create_nested_option (next, "nested_ptr", d);
960 /* Add a variable named S of type T with options O defined at POS,
961 to `variables'. */
962 void
963 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
965 pair_p n;
966 n = XNEW (struct pair);
967 n->name = s;
968 n->type = t;
969 n->line = *pos;
970 n->opt = o;
971 n->next = variables;
972 variables = n;
975 /* Most-general structure field creator. */
976 static pair_p
977 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
978 const input_file *inpf, int line)
980 pair_p field;
982 field = XNEW (struct pair);
983 field->next = next;
984 field->type = type;
985 field->name = name;
986 field->opt = opt;
987 field->line.file = inpf;
988 field->line.line = line;
989 return field;
992 /* Create a field that came from the source code we are scanning,
993 i.e. we have a 'struct fileloc', and possibly options; also,
994 adjust_field_type should be called. */
995 pair_p
996 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
997 struct fileloc *pos)
999 return create_field_all (next, adjust_field_type (type, opt),
1000 name, opt, pos->file, pos->line);
1003 /* Create a fake field with the given type and name. NEXT is the next
1004 field in the chain. */
1005 #define create_field(next,type,name) \
1006 create_field_all (next,type,name, 0, this_file, __LINE__)
1008 /* Like create_field, but the field is only valid when condition COND
1009 is true. */
1011 static pair_p
1012 create_optional_field_ (pair_p next, type_p type, const char *name,
1013 const char *cond, int line)
1015 static int id = 1;
1016 pair_p union_fields;
1017 type_p union_type;
1019 /* Create a fake union type with a single nameless field of type TYPE.
1020 The field has a tag of "1". This allows us to make the presence
1021 of a field of type TYPE depend on some boolean "desc" being true. */
1022 union_fields = create_field (NULL, type, "");
1023 union_fields->opt =
1024 create_string_option (union_fields->opt, "dot", "");
1025 union_fields->opt =
1026 create_string_option (union_fields->opt, "tag", "1");
1027 union_type =
1028 new_structure (xasprintf ("%s_%d", "fake_union", id++), TYPE_UNION,
1029 &lexer_line, union_fields, NULL, NULL);
1031 /* Create the field and give it the new fake union type. Add a "desc"
1032 tag that specifies the condition under which the field is valid. */
1033 return create_field_all (next, union_type, name,
1034 create_string_option (0, "desc", cond),
1035 this_file, line);
1038 #define create_optional_field(next,type,name,cond) \
1039 create_optional_field_(next,type,name,cond,__LINE__)
1041 /* Reverse a linked list of 'struct pair's in place. */
1042 pair_p
1043 nreverse_pairs (pair_p list)
1045 pair_p prev = 0, p, next;
1046 for (p = list; p; p = next)
1048 next = p->next;
1049 p->next = prev;
1050 prev = p;
1052 return prev;
1056 /* We don't care how long a CONST_DOUBLE is. */
1057 #define CONST_DOUBLE_FORMAT "ww"
1058 /* We don't want to see codes that are only for generator files. */
1059 #undef GENERATOR_FILE
1061 enum rtx_code
1063 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1064 #include "rtl.def"
1065 #undef DEF_RTL_EXPR
1066 NUM_RTX_CODE
1069 static const char *const rtx_name[NUM_RTX_CODE] = {
1070 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1071 #include "rtl.def"
1072 #undef DEF_RTL_EXPR
1075 static const char *const rtx_format[NUM_RTX_CODE] = {
1076 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1077 #include "rtl.def"
1078 #undef DEF_RTL_EXPR
1081 static int rtx_next_new[NUM_RTX_CODE];
1083 /* We also need codes and names for insn notes (not register notes).
1084 Note that we do *not* bias the note values here. */
1085 enum insn_note
1087 #define DEF_INSN_NOTE(NAME) NAME,
1088 #include "insn-notes.def"
1089 #undef DEF_INSN_NOTE
1091 NOTE_INSN_MAX
1094 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1095 default field for line number notes. */
1096 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
1097 #define DEF_INSN_NOTE(NAME) #NAME,
1098 #include "insn-notes.def"
1099 #undef DEF_INSN_NOTE
1102 #undef CONST_DOUBLE_FORMAT
1103 #define GENERATOR_FILE
1105 /* Generate the contents of the rtx_next array. This really doesn't belong
1106 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1108 static void
1109 gen_rtx_next (void)
1111 int i;
1112 for (i = 0; i < NUM_RTX_CODE; i++)
1114 int k;
1116 rtx_next_new[i] = -1;
1117 if (strncmp (rtx_format[i], "uu", 2) == 0)
1118 rtx_next_new[i] = 1;
1119 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1120 rtx_next_new[i] = 1;
1121 else
1122 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
1123 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1124 rtx_next_new[i] = k;
1128 /* Write out the contents of the rtx_next array. */
1129 static void
1130 write_rtx_next (void)
1132 outf_p f = get_output_file_with_visibility (NULL);
1133 int i;
1134 if (!f)
1135 return;
1137 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
1138 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1139 for (i = 0; i < NUM_RTX_CODE; i++)
1140 if (rtx_next_new[i] == -1)
1141 oprintf (f, " 0,\n");
1142 else
1143 oprintf (f,
1144 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1145 oprintf (f, "};\n");
1148 /* Handle `special("rtx_def")'. This is a special case for field
1149 `fld' of struct rtx_def, which is an array of unions whose values
1150 are based in a complex way on the type of RTL. */
1152 static type_p
1153 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1155 pair_p flds = NULL;
1156 options_p nodot;
1157 int i;
1158 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1159 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1161 if (t->kind != TYPE_UNION)
1163 error_at_line (&lexer_line,
1164 "special `rtx_def' must be applied to a union");
1165 return &string_type;
1168 nodot = create_string_option (NULL, "dot", "");
1170 rtx_tp = create_pointer (find_structure ("rtx_def", TYPE_STRUCT));
1171 rtvec_tp = create_pointer (find_structure ("rtvec_def", TYPE_STRUCT));
1172 tree_tp = create_pointer (find_structure ("tree_node", TYPE_UNION));
1173 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", TYPE_STRUCT));
1174 reg_attrs_tp =
1175 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT));
1176 basic_block_tp =
1177 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT));
1178 constant_tp =
1179 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT));
1180 scalar_tp = &scalar_nonchar; /* rtunion int */
1183 pair_p note_flds = NULL;
1184 int c;
1186 for (c = 0; c <= NOTE_INSN_MAX; c++)
1188 switch (c)
1190 case NOTE_INSN_MAX:
1191 case NOTE_INSN_DELETED_LABEL:
1192 case NOTE_INSN_DELETED_DEBUG_LABEL:
1193 note_flds = create_field (note_flds, &string_type, "rt_str");
1194 break;
1196 case NOTE_INSN_BLOCK_BEG:
1197 case NOTE_INSN_BLOCK_END:
1198 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1199 break;
1201 case NOTE_INSN_VAR_LOCATION:
1202 case NOTE_INSN_CALL_ARG_LOCATION:
1203 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1204 break;
1206 default:
1207 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1208 break;
1210 /* NOTE_INSN_MAX is used as the default field for line
1211 number notes. */
1212 if (c == NOTE_INSN_MAX)
1213 note_flds->opt =
1214 create_string_option (nodot, "default", "");
1215 else
1216 note_flds->opt =
1217 create_string_option (nodot, "tag", note_insn_name[c]);
1219 note_union_tp = new_structure ("rtx_def_note_subunion", TYPE_UNION,
1220 &lexer_line, note_flds, NULL, NULL);
1222 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1224 pair_p sym_flds;
1225 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1226 sym_flds->opt = create_string_option (nodot, "default", "");
1227 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1228 sym_flds->opt = create_string_option (nodot, "tag", "1");
1229 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", TYPE_UNION,
1230 &lexer_line, sym_flds, NULL, NULL);
1232 for (i = 0; i < NUM_RTX_CODE; i++)
1234 pair_p subfields = NULL;
1235 size_t aindex, nmindex;
1236 const char *sname;
1237 type_p substruct;
1238 char *ftag;
1240 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1242 type_p t;
1243 const char *subname;
1245 switch (rtx_format[i][aindex])
1247 case '*':
1248 case 'i':
1249 case 'n':
1250 case 'w':
1251 t = scalar_tp;
1252 subname = "rt_int";
1253 break;
1255 case '0':
1256 if (i == MEM && aindex == 1)
1257 t = mem_attrs_tp, subname = "rt_mem";
1258 else if (i == JUMP_INSN && aindex == 7)
1259 t = rtx_tp, subname = "rt_rtx";
1260 else if (i == CODE_LABEL && aindex == 4)
1261 t = scalar_tp, subname = "rt_int";
1262 else if (i == CODE_LABEL && aindex == 3)
1263 t = rtx_tp, subname = "rt_rtx";
1264 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1265 t = rtx_tp, subname = "rt_rtx";
1266 else if (i == NOTE && aindex == 3)
1267 t = note_union_tp, subname = "";
1268 else if (i == NOTE && aindex == 4)
1269 t = scalar_tp, subname = "rt_int";
1270 else if (i == NOTE && aindex >= 6)
1271 t = scalar_tp, subname = "rt_int";
1272 else if (i == ADDR_DIFF_VEC && aindex == 4)
1273 t = scalar_tp, subname = "rt_int";
1274 else if (i == VALUE && aindex == 0)
1275 t = scalar_tp, subname = "rt_int";
1276 else if (i == DEBUG_EXPR && aindex == 0)
1277 t = tree_tp, subname = "rt_tree";
1278 else if (i == REG && aindex == 1)
1279 t = reg_attrs_tp, subname = "rt_reg";
1280 else if (i == SYMBOL_REF && aindex == 1)
1281 t = symbol_union_tp, subname = "";
1282 else if (i == JUMP_TABLE_DATA && aindex >= 4)
1283 t = scalar_tp, subname = "rt_int";
1284 else if (i == BARRIER && aindex >= 2)
1285 t = scalar_tp, subname = "rt_int";
1286 else if (i == ENTRY_VALUE && aindex == 0)
1287 t = rtx_tp, subname = "rt_rtx";
1288 else
1290 error_at_line
1291 (&lexer_line,
1292 "rtx type `%s' has `0' in position %lu, can't handle",
1293 rtx_name[i], (unsigned long) aindex);
1294 t = &string_type;
1295 subname = "rt_int";
1297 break;
1299 case 's':
1300 case 'S':
1301 case 'T':
1302 t = &string_type;
1303 subname = "rt_str";
1304 break;
1306 case 'e':
1307 case 'u':
1308 t = rtx_tp;
1309 subname = "rt_rtx";
1310 break;
1312 case 'E':
1313 case 'V':
1314 t = rtvec_tp;
1315 subname = "rt_rtvec";
1316 break;
1318 case 't':
1319 t = tree_tp;
1320 subname = "rt_tree";
1321 break;
1323 case 'B':
1324 t = basic_block_tp;
1325 subname = "rt_bb";
1326 break;
1328 default:
1329 error_at_line
1330 (&lexer_line,
1331 "rtx type `%s' has `%c' in position %lu, can't handle",
1332 rtx_name[i], rtx_format[i][aindex],
1333 (unsigned long) aindex);
1334 t = &string_type;
1335 subname = "rt_int";
1336 break;
1339 subfields = create_field (subfields, t,
1340 xasprintf (".fld[%lu].%s",
1341 (unsigned long) aindex,
1342 subname));
1343 subfields->opt = nodot;
1344 if (t == note_union_tp)
1345 subfields->opt =
1346 create_string_option (subfields->opt, "desc",
1347 "NOTE_KIND (&%0)");
1348 if (t == symbol_union_tp)
1349 subfields->opt =
1350 create_string_option (subfields->opt, "desc",
1351 "CONSTANT_POOL_ADDRESS_P (&%0)");
1354 if (i == SYMBOL_REF)
1356 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1357 holds. */
1358 type_p field_tp = find_structure ("block_symbol", TYPE_STRUCT);
1359 subfields
1360 = create_optional_field (subfields, field_tp, "block_sym",
1361 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1364 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1365 substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
1366 NULL, NULL);
1368 ftag = xstrdup (rtx_name[i]);
1369 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1370 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1371 flds = create_field (flds, substruct, "");
1372 flds->opt = create_string_option (nodot, "tag", ftag);
1374 return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
1375 nodot, NULL);
1378 /* Handle `special("tree_exp")'. This is a special case for
1379 field `operands' of struct tree_exp, which although it claims to contain
1380 pointers to trees, actually sometimes contains pointers to RTL too.
1381 Passed T, the old type of the field, and OPT its options. Returns
1382 a new type for the field. */
1384 static type_p
1385 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1387 pair_p flds;
1388 options_p nodot;
1390 if (t->kind != TYPE_ARRAY)
1392 error_at_line (&lexer_line,
1393 "special `tree_exp' must be applied to an array");
1394 return &string_type;
1397 nodot = create_string_option (NULL, "dot", "");
1399 flds = create_field (NULL, t, "");
1400 flds->opt = create_string_option (nodot, "length",
1401 "TREE_OPERAND_LENGTH ((tree) &%0)");
1402 flds->opt = create_string_option (flds->opt, "default", "");
1404 return new_structure ("tree_exp_subunion", TYPE_UNION, &lexer_line, flds,
1405 nodot, NULL);
1408 /* Perform any special processing on a type T, about to become the type
1409 of a field. Return the appropriate type for the field.
1410 At present:
1411 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1412 - Similarly for arrays of pointer-to-char;
1413 - Converts structures for which a parameter is provided to
1414 TYPE_PARAM_STRUCT;
1415 - Handles "special" options.
1418 type_p
1419 adjust_field_type (type_p t, options_p opt)
1421 int length_p = 0;
1422 const int pointer_p = t->kind == TYPE_POINTER;
1423 type_p params[NUM_PARAM];
1424 int params_p = 0;
1425 int i;
1427 for (i = 0; i < NUM_PARAM; i++)
1428 params[i] = NULL;
1430 for (; opt; opt = opt->next)
1431 if (strcmp (opt->name, "length") == 0)
1433 if (length_p)
1434 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1435 if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
1437 error_at_line (&lexer_line,
1438 "option `%s' may not be applied to "
1439 "arrays of atomic types", opt->name);
1441 length_p = 1;
1443 else if ((strcmp (opt->name, "param_is") == 0
1444 || (strncmp (opt->name, "param", 5) == 0
1445 && ISDIGIT (opt->name[5])
1446 && strcmp (opt->name + 6, "_is") == 0))
1447 && opt->kind == OPTION_TYPE)
1449 int num = ISDIGIT (opt->name[5]) ? opt->name[5] - '0' : 0;
1451 if (!union_or_struct_p (t)
1452 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
1454 error_at_line (&lexer_line,
1455 "option `%s' may only be applied to structures or structure pointers",
1456 opt->name);
1457 return t;
1460 params_p = 1;
1461 if (params[num] != NULL)
1462 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1463 if (!ISDIGIT (opt->name[5]))
1464 params[num] = create_pointer (opt->info.type);
1465 else
1466 params[num] = opt->info.type;
1468 else if (strcmp (opt->name, "special") == 0
1469 && opt->kind == OPTION_STRING)
1471 const char *special_name = opt->info.string;
1472 if (strcmp (special_name, "tree_exp") == 0)
1473 t = adjust_field_tree_exp (t, opt);
1474 else if (strcmp (special_name, "rtx_def") == 0)
1475 t = adjust_field_rtx_def (t, opt);
1476 else
1477 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1480 if (params_p)
1482 type_p realt;
1484 if (pointer_p)
1485 t = t->u.p;
1486 realt = find_param_structure (t, params);
1487 t = pointer_p ? create_pointer (realt) : realt;
1490 if (!length_p
1491 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1492 return &string_type;
1493 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1494 && t->u.a.p->u.p->kind == TYPE_SCALAR
1495 && t->u.a.p->u.p->u.scalar_is_char)
1496 return create_array (&string_type, t->u.a.len);
1498 return t;
1502 static void set_gc_used_type (type_p, enum gc_used_enum, type_p *,
1503 bool = false);
1504 static void set_gc_used (pair_p);
1506 /* Handle OPT for set_gc_used_type. */
1508 static void
1509 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1510 int *pass_param, int *length, int *skip,
1511 type_p *nested_ptr)
1513 options_p o;
1514 for (o = opt; o; o = o->next)
1515 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1516 && o->kind == OPTION_TYPE)
1517 set_gc_used_type (o->info.type,
1518 GC_POINTED_TO, NULL);
1519 else if (strcmp (o->name, "maybe_undef") == 0)
1520 *maybe_undef = 1;
1521 else if (strcmp (o->name, "use_params") == 0)
1522 *pass_param = 1;
1523 else if (strcmp (o->name, "length") == 0)
1524 *length = 1;
1525 else if (strcmp (o->name, "skip") == 0)
1526 *skip = 1;
1527 else if (strcmp (o->name, "nested_ptr") == 0
1528 && o->kind == OPTION_NESTED)
1529 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1533 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1535 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1536 are set to GC_UNUSED. Otherwise, an error is emitted for
1537 TYPE_UNDEFINED types. This is used to support user-defined
1538 template types with non-type arguments.
1540 For instance, when we parse a template type with enum arguments
1541 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1542 artificial fields for 'MyType', one for 'AnotherType', the other
1543 one for 'EnumValue'.
1545 At the time that we parse this type we don't know that 'EnumValue'
1546 is really an enum value, so the parser creates a TYPE_UNDEFINED
1547 type for it. Since 'EnumValue' is never resolved to a known
1548 structure, it will stay with TYPE_UNDEFINED.
1550 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1551 'EnumValue'. Generating marking code for it would cause
1552 compilation failures since the marking routines assumes that
1553 'EnumValue' is a type. */
1555 static void
1556 set_gc_used_type (type_p t, enum gc_used_enum level, type_p param[NUM_PARAM],
1557 bool allow_undefined_types)
1559 if (t->gc_used >= level)
1560 return;
1562 t->gc_used = level;
1564 switch (t->kind)
1566 case TYPE_STRUCT:
1567 case TYPE_UNION:
1568 case TYPE_USER_STRUCT:
1570 pair_p f;
1571 int dummy;
1572 type_p dummy2;
1573 bool allow_undefined_field_types = (t->kind == TYPE_USER_STRUCT);
1575 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
1576 &dummy2);
1578 if (t->u.s.base_class)
1579 set_gc_used_type (t->u.s.base_class, level, param,
1580 allow_undefined_types);
1581 /* Anything pointing to a base class might actually be pointing
1582 to a subclass. */
1583 for (type_p subclass = t->u.s.first_subclass; subclass;
1584 subclass = subclass->u.s.next_sibling_class)
1585 set_gc_used_type (subclass, level, param,
1586 allow_undefined_types);
1588 FOR_ALL_INHERITED_FIELDS(t, f)
1590 int maybe_undef = 0;
1591 int pass_param = 0;
1592 int length = 0;
1593 int skip = 0;
1594 type_p nested_ptr = NULL;
1595 process_gc_options (f->opt, level, &maybe_undef, &pass_param,
1596 &length, &skip, &nested_ptr);
1598 if (nested_ptr && f->type->kind == TYPE_POINTER)
1599 set_gc_used_type (nested_ptr, GC_POINTED_TO,
1600 pass_param ? param : NULL);
1601 else if (length && f->type->kind == TYPE_POINTER)
1602 set_gc_used_type (f->type->u.p, GC_USED, NULL);
1603 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1604 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO, NULL);
1605 else if (pass_param && f->type->kind == TYPE_POINTER && param)
1606 set_gc_used_type (find_param_structure (f->type->u.p, param),
1607 GC_POINTED_TO, NULL);
1608 else if (skip)
1609 ; /* target type is not used through this field */
1610 else
1611 set_gc_used_type (f->type, GC_USED, pass_param ? param : NULL,
1612 allow_undefined_field_types);
1614 break;
1617 case TYPE_UNDEFINED:
1618 if (level > GC_UNUSED)
1620 if (!allow_undefined_types)
1621 error_at_line (&t->u.s.line, "undefined type `%s'", t->u.s.tag);
1622 t->gc_used = GC_UNUSED;
1624 break;
1626 case TYPE_POINTER:
1627 set_gc_used_type (t->u.p, GC_POINTED_TO, NULL);
1628 break;
1630 case TYPE_ARRAY:
1631 set_gc_used_type (t->u.a.p, GC_USED, param);
1632 break;
1634 case TYPE_LANG_STRUCT:
1635 for (t = t->u.s.lang_struct; t; t = t->next)
1636 set_gc_used_type (t, level, param);
1637 break;
1639 case TYPE_PARAM_STRUCT:
1641 int i;
1642 for (i = 0; i < NUM_PARAM; i++)
1643 if (t->u.param_struct.param[i] != 0)
1644 set_gc_used_type (t->u.param_struct.param[i], GC_USED, NULL);
1646 if (t->u.param_struct.stru->gc_used == GC_POINTED_TO)
1647 level = GC_POINTED_TO;
1648 else
1649 level = GC_USED;
1650 t->u.param_struct.stru->gc_used = GC_UNUSED;
1651 set_gc_used_type (t->u.param_struct.stru, level,
1652 t->u.param_struct.param);
1653 break;
1655 default:
1656 break;
1660 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1662 static void
1663 set_gc_used (pair_p variables)
1665 int nbvars = 0;
1666 pair_p p;
1667 for (p = variables; p; p = p->next)
1669 set_gc_used_type (p->type, GC_USED, NULL);
1670 nbvars++;
1672 if (verbosity_level >= 2)
1673 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1676 /* File mapping routines. For each input file, there is one output .c file
1677 (but some output files have many input files), and there is one .h file
1678 for the whole build. */
1680 /* Output file handling. */
1682 /* Create and return an outf_p for a new file for NAME, to be called
1683 ONAME. */
1685 static outf_p
1686 create_file (const char *name, const char *oname)
1688 static const char *const hdr[] = {
1689 " Copyright (C) 2004-2014 Free Software Foundation, Inc.\n",
1690 "\n",
1691 "This file is part of GCC.\n",
1692 "\n",
1693 "GCC is free software; you can redistribute it and/or modify it under\n",
1694 "the terms of the GNU General Public License as published by the Free\n",
1695 "Software Foundation; either version 3, or (at your option) any later\n",
1696 "version.\n",
1697 "\n",
1698 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1699 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1700 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1701 "for more details.\n",
1702 "\n",
1703 "You should have received a copy of the GNU General Public License\n",
1704 "along with GCC; see the file COPYING3. If not see\n",
1705 "<http://www.gnu.org/licenses/>. */\n",
1706 "\n",
1707 "/* This file is machine generated. Do not edit. */\n"
1709 outf_p f;
1710 size_t i;
1712 gcc_assert (name != NULL);
1713 gcc_assert (oname != NULL);
1714 f = XCNEW (struct outf);
1715 f->next = output_files;
1716 f->name = oname;
1717 output_files = f;
1719 oprintf (f, "/* Type information for %s.\n", name);
1720 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1721 oprintf (f, "%s", hdr[i]);
1722 return f;
1725 /* Print, like fprintf, to O.
1726 N.B. You might think this could be implemented more efficiently
1727 with vsnprintf(). Unfortunately, there are C libraries that
1728 provide that function but without the C99 semantics for its return
1729 value, making it impossible to know how much space is required. */
1730 void
1731 oprintf (outf_p o, const char *format, ...)
1733 char *s;
1734 size_t slength;
1735 va_list ap;
1737 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1738 in that case. */
1739 if (!o)
1740 return;
1742 va_start (ap, format);
1743 slength = vasprintf (&s, format, ap);
1744 if (s == NULL || (int) slength < 0)
1745 fatal ("out of memory");
1746 va_end (ap);
1748 if (o->bufused + slength > o->buflength)
1750 size_t new_len = o->buflength;
1751 if (new_len == 0)
1752 new_len = 1024;
1755 new_len *= 2;
1757 while (o->bufused + slength >= new_len);
1758 o->buf = XRESIZEVEC (char, o->buf, new_len);
1759 o->buflength = new_len;
1761 memcpy (o->buf + o->bufused, s, slength);
1762 o->bufused += slength;
1763 free (s);
1766 /* Open the global header file and the language-specific header files. */
1768 static void
1769 open_base_files (void)
1771 size_t i;
1773 if (nb_plugin_files > 0 && plugin_files)
1774 return;
1776 header_file = create_file ("GCC", "gtype-desc.h");
1778 base_files = XNEWVEC (outf_p, num_lang_dirs);
1780 for (i = 0; i < num_lang_dirs; i++)
1781 base_files[i] = create_file (lang_dir_names[i],
1782 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1784 /* gtype-desc.c is a little special, so we create it here. */
1786 /* The order of files here matters very much. */
1787 static const char *const ifiles[] = {
1788 "config.h", "system.h", "coretypes.h", "tm.h",
1789 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1790 "tree.h", "rtl.h", "wide-int.h", "function.h", "insn-config.h", "expr.h",
1791 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1792 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1793 "pointer-set.h", "hash-table.h", "vec.h", "ggc.h", "basic-block.h",
1794 "tree-ssa-alias.h", "internal-fn.h", "gimple-fold.h", "tree-eh.h",
1795 "gimple-expr.h", "is-a.h",
1796 "gimple.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
1797 "tree-phinodes.h", "ssa-iterators.h", "stringpool.h", "tree-ssanames.h",
1798 "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h", "tree-ssa-loop-manip.h",
1799 "tree-ssa-loop-niter.h", "tree-into-ssa.h", "tree-dfa.h",
1800 "tree-ssa.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1801 "except.h", "output.h", "cfgloop.h",
1802 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h",
1803 "ipa-inline.h", "dwarf2out.h", NULL
1805 const char *const *ifp;
1806 outf_p gtype_desc_c;
1808 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1809 for (ifp = ifiles; *ifp; ifp++)
1810 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1812 /* Make sure we handle "cfun" specially. */
1813 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1814 oprintf (gtype_desc_c, "#undef cfun\n");
1816 oprintf (gtype_desc_c,
1817 "\n"
1818 "/* Types with a \"gcc::\" namespace have it stripped\n"
1819 " during gengtype parsing. Provide a \"using\" directive\n"
1820 " to ensure that the fully-qualified types are found. */\n"
1821 "using namespace gcc;\n");
1825 /* For INPF an input file, return the real basename of INPF, with all
1826 the directory components skipped. */
1828 static const char *
1829 get_file_realbasename (const input_file *inpf)
1831 return lbasename (get_input_file_name (inpf));
1834 /* For INPF a filename, return the relative path to INPF from
1835 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1837 const char *
1838 get_file_srcdir_relative_path (const input_file *inpf)
1840 const char *f = get_input_file_name (inpf);
1841 if (strlen (f) > srcdir_len
1842 && IS_DIR_SEPARATOR (f[srcdir_len])
1843 && strncmp (f, srcdir, srcdir_len) == 0)
1844 return f + srcdir_len + 1;
1845 else
1846 return NULL;
1849 /* For INPF an input_file, return the relative path to INPF from
1850 $(srcdir) if the latter is a prefix in INPF, or the real basename
1851 of INPF otherwise. */
1853 static const char *
1854 get_file_basename (const input_file *inpf)
1856 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1858 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1861 /* For F a filename, return the lang_dir_names relative index of the language
1862 directory that is a prefix in F, if any, -1 otherwise. */
1864 static int
1865 get_prefix_langdir_index (const char *f)
1867 size_t f_len = strlen (f);
1868 size_t lang_index;
1870 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1872 const char *langdir = lang_dir_names[lang_index];
1873 size_t langdir_len = strlen (langdir);
1875 if (f_len > langdir_len
1876 && IS_DIR_SEPARATOR (f[langdir_len])
1877 && memcmp (f, langdir, langdir_len) == 0)
1878 return lang_index;
1881 return -1;
1884 /* For INPF an input file, return the name of language directory where
1885 F is located, if any, NULL otherwise. */
1887 static const char *
1888 get_file_langdir (const input_file *inpf)
1890 /* Get the relative path to INPF from $(srcdir) and find the
1891 language by comparing the prefix with language directory names.
1892 If INPF is not even srcdir relative, no point in looking
1893 further. */
1895 int lang_index;
1896 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1897 const char *r;
1899 if (!srcdir_relative_path)
1900 return NULL;
1902 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1903 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1904 r = "c-family";
1905 else if (lang_index >= 0)
1906 r = lang_dir_names[lang_index];
1907 else
1908 r = NULL;
1910 return r;
1913 /* The gt- output file name for INPF. */
1915 static const char *
1916 get_file_gtfilename (const input_file *inpf)
1918 /* Cook up an initial version of the gt- file name from the file real
1919 basename and the language name, if any. */
1921 const char *basename = get_file_realbasename (inpf);
1922 const char *langdir = get_file_langdir (inpf);
1924 char *result =
1925 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1926 : xasprintf ("gt-%s", basename));
1928 /* Then replace all non alphanumerics characters by '-' and change the
1929 extension to ".h". We expect the input filename extension was at least
1930 one character long. */
1932 char *s = result;
1934 for (; *s != '.'; s++)
1935 if (!ISALNUM (*s) && *s != '-')
1936 *s = '-';
1938 memcpy (s, ".h", sizeof (".h"));
1940 return result;
1943 /* Each input_file has its associated output file outf_p. The
1944 association is computed by the function
1945 get_output_file_with_visibility. The associated file is cached
1946 inside input_file in its inpoutf field, so is really computed only
1947 once. Associated output file paths (i.e. output_name-s) are
1948 computed by a rule based regexp machinery, using the files_rules
1949 array of struct file_rule_st. A for_name is also computed, giving
1950 the source file name for which the output_file is generated; it is
1951 often the last component of the input_file path. */
1955 Regexpr machinery to compute the output_name and for_name-s of each
1956 input_file. We have a sequence of file rules which gives the POSIX
1957 extended regular expression to match an input file path, and two
1958 transformed strings for the corresponding output_name and the
1959 corresponding for_name. The transformed string contain dollars: $0
1960 is replaced by the entire match, $1 is replaced by the substring
1961 matching the first parenthesis in the regexp, etc. And $$ is replaced
1962 by a single verbatim dollar. The rule order is important. The
1963 general case is last, and the particular cases should come before.
1964 An action routine can, when needed, update the out_name & for_name
1965 and/or return the appropriate output file. It is invoked only when a
1966 rule is triggered. When a rule is triggered, the output_name and
1967 for_name are computed using their transform string in while $$, $0,
1968 $1, ... are suitably replaced. If there is an action, it is called.
1969 In some few cases, the action can directly return the outf_p, but
1970 usually it just updates the output_name and for_name so should free
1971 them before replacing them. The get_output_file_with_visibility
1972 function creates an outf_p only once per each output_name, so it
1973 scans the output_files list for previously seen output file names.
1976 /* Signature of actions in file rules. */
1977 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
1980 struct file_rule_st {
1981 const char* frul_srcexpr; /* Source string for regexp. */
1982 int frul_rflags; /* Flags passed to regcomp, usually
1983 * REG_EXTENDED. */
1984 regex_t* frul_re; /* Compiled regular expression
1985 obtained by regcomp. */
1986 const char* frul_tr_out; /* Transformation string for making
1987 * the output_name, with $1 ... $9 for
1988 * subpatterns and $0 for the whole
1989 * matched filename. */
1990 const char* frul_tr_for; /* Tranformation string for making the
1991 for_name. */
1992 frul_actionrout_t* frul_action; /* The action, if non null, is
1993 * called once the rule matches, on
1994 * the transformed out_name &
1995 * for_name. It could change them
1996 * and/or give the output file. */
1999 /* File rule action handling *.h files. */
2000 static outf_p header_dot_h_frul (input_file*, char**, char**);
2002 /* File rule action handling *.c files. */
2003 static outf_p source_dot_c_frul (input_file*, char**, char**);
2005 #define NULL_REGEX (regex_t*)0
2007 /* The prefix in our regexp-s matching the directory. */
2008 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
2010 #define NULL_FRULACT (frul_actionrout_t*)0
2012 /* The array of our rules governing file name generation. Rules order
2013 matters, so change with extreme care! */
2015 struct file_rule_st files_rules[] = {
2016 /* The general rule assumes that files in subdirectories belong to a
2017 particular front-end, and files not in subdirectories are shared.
2018 The following rules deal with exceptions - files that are in
2019 subdirectories and yet are shared, and files that are top-level,
2020 but are not shared. */
2022 /* the c-family/ source directory is special. */
2023 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
2024 REG_EXTENDED, NULL_REGEX,
2025 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
2027 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
2028 REG_EXTENDED, NULL_REGEX,
2029 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
2031 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
2032 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
2033 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
2035 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
2036 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
2038 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
2039 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
2040 REG_EXTENDED, NULL_REGEX,
2041 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
2043 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
2044 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
2045 REG_EXTENDED, NULL_REGEX,
2046 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
2048 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
2049 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
2050 REG_EXTENDED, NULL_REGEX,
2051 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
2053 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
2054 { DIR_PREFIX_REGEX "cp/parser\\.h$",
2055 REG_EXTENDED, NULL_REGEX,
2056 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
2058 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
2059 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
2060 REG_EXTENDED, NULL_REGEX,
2061 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
2063 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
2064 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
2065 REG_EXTENDED, NULL_REGEX,
2066 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
2068 /* General cases. For header *.h and source *.c or *.cc files, we
2069 * need special actions to handle the language. */
2071 /* Source *.c files are using get_file_gtfilename to compute their
2072 output_name and get_file_basename to compute their for_name
2073 through the source_dot_c_frul action. */
2074 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
2075 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
2077 /* Source *.cc files are using get_file_gtfilename to compute their
2078 output_name and get_file_basename to compute their for_name
2079 through the source_dot_c_frul action. */
2080 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.cc$",
2081 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.cc", source_dot_c_frul},
2083 /* Common header files get "gtype-desc.c" as their output_name,
2084 * while language specific header files are handled specially. So
2085 * we need the header_dot_h_frul action. */
2086 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
2087 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
2089 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
2090 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
2092 /* Mandatory null last entry signaling end of rules. */
2093 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
2096 /* Special file rules action for handling *.h header files. It gives
2097 "gtype-desc.c" for common headers and corresponding output
2098 files for language-specific header files. */
2099 static outf_p
2100 header_dot_h_frul (input_file* inpf, char**poutname,
2101 char**pforname ATTRIBUTE_UNUSED)
2103 const char *basename = 0;
2104 int lang_index = 0;
2105 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2106 (void*) inpf, get_input_file_name (inpf),
2107 *poutname, *pforname);
2108 basename = get_file_basename (inpf);
2109 lang_index = get_prefix_langdir_index (basename);
2110 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
2112 if (lang_index >= 0)
2114 /* The header is language specific. Given output_name &
2115 for_name remains unchanged. The base_files array gives the
2116 outf_p. */
2117 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2118 (void*) base_files[lang_index],
2119 (base_files[lang_index])->name);
2120 return base_files[lang_index];
2122 else
2124 /* The header is common to all front-end languages. So
2125 output_name is "gtype-desc.c" file. The calling function
2126 get_output_file_with_visibility will find its outf_p. */
2127 free (*poutname);
2128 *poutname = xstrdup ("gtype-desc.c");
2129 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2130 get_input_file_name (inpf));
2131 return NULL;
2136 /* Special file rules action for handling *.c source files using
2137 * get_file_gtfilename to compute their output_name and
2138 * get_file_basename to compute their for_name. The output_name is
2139 * gt-<LANG>-<BASE>.h for language specific source files, and
2140 * gt-<BASE>.h for common source files. */
2141 static outf_p
2142 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
2144 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
2145 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
2146 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2147 (void*) inpf, get_input_file_name (inpf),
2148 *poutname, *pforname);
2149 DBGPRINTF ("newoutname %s", newoutname);
2150 DBGPRINTF ("newbasename %s", newbasename);
2151 free (*poutname);
2152 free (*pforname);
2153 *poutname = newoutname;
2154 *pforname = newbasename;
2155 return NULL;
2158 /* Utility function for get_output_file_with_visibility which returns
2159 * a malloc-ed substituted string using TRS on matching of the FILNAM
2160 * file name, using the PMATCH array. */
2161 static char*
2162 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
2163 const char *trs)
2165 struct obstack str_obstack;
2166 char *str = NULL;
2167 char *rawstr = NULL;
2168 const char *pt = NULL;
2169 DBGPRINTF ("filnam %s", filnam);
2170 obstack_init (&str_obstack);
2171 for (pt = trs; *pt; pt++) {
2172 char c = *pt;
2173 if (c == '$')
2175 if (pt[1] == '$')
2177 /* A double dollar $$ is substituted by a single verbatim
2178 dollar, but who really uses dollar signs in file
2179 paths? */
2180 obstack_1grow (&str_obstack, '$');
2182 else if (ISDIGIT (pt[1]))
2184 /* Handle $0 $1 ... $9 by appropriate substitution. */
2185 int dolnum = pt[1] - '0';
2186 int so = pmatch[dolnum].rm_so;
2187 int eo = pmatch[dolnum].rm_eo;
2188 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2189 if (so>=0 && eo>=so)
2190 obstack_grow (&str_obstack, filnam + so, eo - so);
2192 else
2194 /* This can happen only when files_rules is buggy! */
2195 gcc_unreachable ();
2197 /* Always skip the character after the dollar. */
2198 pt++;
2200 else
2201 obstack_1grow (&str_obstack, c);
2203 obstack_1grow (&str_obstack, '\0');
2204 rawstr = XOBFINISH (&str_obstack, char *);
2205 str = xstrdup (rawstr);
2206 obstack_free (&str_obstack, NULL);
2207 DBGPRINTF ("matched replacement %s", str);
2208 rawstr = NULL;
2209 return str;
2213 /* An output file, suitable for definitions, that can see declarations
2214 made in INPF and is linked into every language that uses INPF.
2215 Since the result is cached inside INPF, that argument cannot be
2216 declared constant, but is "almost" constant. */
2218 outf_p
2219 get_output_file_with_visibility (input_file *inpf)
2221 outf_p r;
2222 char *for_name = NULL;
2223 char *output_name = NULL;
2224 const char* inpfname;
2226 /* This can happen when we need a file with visibility on a
2227 structure that we've never seen. We have to just hope that it's
2228 globally visible. */
2229 if (inpf == NULL)
2230 inpf = system_h_file;
2232 /* The result is cached in INPF, so return it if already known. */
2233 if (inpf->inpoutf)
2234 return inpf->inpoutf;
2236 /* In plugin mode, return NULL unless the input_file is one of the
2237 plugin_files. */
2238 if (plugin_files)
2240 size_t i;
2241 for (i = 0; i < nb_plugin_files; i++)
2242 if (inpf == plugin_files[i])
2244 inpf->inpoutf = plugin_output;
2245 return plugin_output;
2248 return NULL;
2251 inpfname = get_input_file_name (inpf);
2253 /* Try each rule in sequence in files_rules until one is triggered. */
2255 int rulix = 0;
2256 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2257 (void*) inpf, inpfname);
2259 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2261 DBGPRINTF ("rulix#%d srcexpr %s",
2262 rulix, files_rules[rulix].frul_srcexpr);
2264 if (!files_rules[rulix].frul_re)
2266 /* Compile the regexpr lazily. */
2267 int err = 0;
2268 files_rules[rulix].frul_re = XCNEW (regex_t);
2269 err = regcomp (files_rules[rulix].frul_re,
2270 files_rules[rulix].frul_srcexpr,
2271 files_rules[rulix].frul_rflags);
2272 if (err)
2274 /* The regular expression compilation fails only when
2275 file_rules is buggy. */
2276 gcc_unreachable ();
2280 output_name = NULL;
2281 for_name = NULL;
2283 /* Match the regexpr and trigger the rule if matched. */
2285 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2286 $3, ... $9. */
2287 regmatch_t pmatch[10];
2288 memset (pmatch, 0, sizeof (pmatch));
2289 if (!regexec (files_rules[rulix].frul_re,
2290 inpfname, 10, pmatch, 0))
2292 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2293 (void*) inpf, inpfname, rulix,
2294 files_rules[rulix].frul_srcexpr);
2295 for_name =
2296 matching_file_name_substitute (inpfname, pmatch,
2297 files_rules[rulix].frul_tr_for);
2298 DBGPRINTF ("for_name %s", for_name);
2299 output_name =
2300 matching_file_name_substitute (inpfname, pmatch,
2301 files_rules[rulix].frul_tr_out);
2302 DBGPRINTF ("output_name %s", output_name);
2303 if (files_rules[rulix].frul_action)
2305 /* Invoke our action routine. */
2306 outf_p of = NULL;
2307 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2308 rulix, output_name, for_name);
2309 of =
2310 (files_rules[rulix].frul_action) (inpf,
2311 &output_name, &for_name);
2312 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2313 rulix, (void*)of, output_name, for_name);
2314 /* If the action routine returned something, give it back
2315 immediately and cache it in inpf. */
2316 if (of)
2318 inpf->inpoutf = of;
2319 return of;
2322 /* The rule matched, and had no action, or that action did
2323 not return any output file but could have changed the
2324 output_name or for_name. We break out of the loop on the
2325 files_rules. */
2326 break;
2328 else
2330 /* The regexpr did not match. */
2331 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2332 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2333 continue;
2338 if (!output_name || !for_name)
2340 /* This should not be possible, and could only happen if the
2341 files_rules is incomplete or buggy. */
2342 fatal ("failed to compute output name for %s", inpfname);
2345 /* Look through to see if we've ever seen this output filename
2346 before. If found, cache the result in inpf. */
2347 for (r = output_files; r; r = r->next)
2348 if (filename_cmp (r->name, output_name) == 0)
2350 inpf->inpoutf = r;
2351 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2352 output_name, for_name);
2353 return r;
2356 /* If not found, create it, and cache it in inpf. */
2357 r = create_file (for_name, output_name);
2359 gcc_assert (r && r->name);
2360 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2361 output_name, for_name);
2362 inpf->inpoutf = r;
2363 return r;
2368 /* The name of an output file, suitable for definitions, that can see
2369 declarations made in INPF and is linked into every language that
2370 uses INPF. */
2372 const char *
2373 get_output_file_name (input_file* inpf)
2375 outf_p o = get_output_file_with_visibility (inpf);
2376 if (o)
2377 return o->name;
2378 return NULL;
2381 /* Check if existing file is equal to the in memory buffer. */
2383 static bool
2384 is_file_equal (outf_p of)
2386 FILE *newfile = fopen (of->name, "r");
2387 size_t i;
2388 bool equal;
2389 if (newfile == NULL)
2390 return false;
2392 equal = true;
2393 for (i = 0; i < of->bufused; i++)
2395 int ch;
2396 ch = fgetc (newfile);
2397 if (ch == EOF || ch != (unsigned char) of->buf[i])
2399 equal = false;
2400 break;
2403 if (equal && EOF != fgetc (newfile))
2404 equal = false;
2405 fclose (newfile);
2406 return equal;
2409 /* Copy the output to its final destination,
2410 but don't unnecessarily change modification times. */
2412 static void
2413 close_output_files (void)
2415 int nbwrittenfiles = 0;
2416 outf_p of;
2418 for (of = output_files; of; of = of->next)
2420 if (!is_file_equal (of))
2422 FILE *newfile = NULL;
2423 char *backupname = NULL;
2424 /* Back up the old version of the output file gt-FOO.c as
2425 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2426 if (backup_dir)
2428 backupname = concat (backup_dir, "/",
2429 lbasename (of->name), "~", NULL);
2430 if (!access (of->name, F_OK) && rename (of->name, backupname))
2431 fatal ("failed to back up %s as %s: %s",
2432 of->name, backupname, xstrerror (errno));
2435 newfile = fopen (of->name, "w");
2436 if (newfile == NULL)
2437 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2438 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2439 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2440 if (fclose (newfile) != 0)
2441 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2442 nbwrittenfiles++;
2443 if (verbosity_level >= 2 && backupname)
2444 printf ("%s wrote #%-3d %s backed-up in %s\n",
2445 progname, nbwrittenfiles, of->name, backupname);
2446 else if (verbosity_level >= 1)
2447 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2448 free (backupname);
2450 else
2452 /* output file remains unchanged. */
2453 if (verbosity_level >= 2)
2454 printf ("%s keep %s\n", progname, of->name);
2456 free (of->buf);
2457 of->buf = NULL;
2458 of->bufused = of->buflength = 0;
2460 if (verbosity_level >= 1)
2461 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2464 struct flist
2466 struct flist *next;
2467 int started_p;
2468 const input_file* file;
2469 outf_p f;
2472 struct walk_type_data;
2474 /* For scalars and strings, given the item in 'val'.
2475 For structures, given a pointer to the item in 'val'.
2476 For misc. pointers, given the item in 'val'.
2478 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2479 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2481 /* Parameters for write_types. */
2483 struct write_types_data
2485 const char *prefix;
2486 const char *param_prefix;
2487 const char *subfield_marker_routine;
2488 const char *marker_routine;
2489 const char *reorder_note_routine;
2490 const char *comment;
2491 int skip_hooks; /* skip hook generation if non zero */
2492 enum write_types_kinds kind;
2495 static void output_escaped_param (struct walk_type_data *d,
2496 const char *, const char *);
2497 static void output_mangled_typename (outf_p, const_type_p);
2498 static void walk_type (type_p t, struct walk_type_data *d);
2499 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2500 const struct write_types_data *wtd);
2501 static void write_types_process_field
2502 (type_p f, const struct walk_type_data *d);
2503 static void write_types (outf_p output_header,
2504 type_p structures,
2505 type_p param_structs,
2506 const struct write_types_data *wtd);
2507 static void write_types_local_process_field
2508 (type_p f, const struct walk_type_data *d);
2509 static void write_local_func_for_structure
2510 (const_type_p orig_s, type_p s, type_p *param);
2511 static void write_local (outf_p output_header,
2512 type_p structures, type_p param_structs);
2513 static int contains_scalar_p (type_p t);
2514 static void put_mangled_filename (outf_p, const input_file *);
2515 static void finish_root_table (struct flist *flp, const char *pfx,
2516 const char *tname, const char *lastname,
2517 const char *name);
2518 static void write_root (outf_p, pair_p, type_p, const char *, int,
2519 struct fileloc *, const char *, bool);
2520 static void write_array (outf_p f, pair_p v,
2521 const struct write_types_data *wtd);
2522 static void write_roots (pair_p, bool);
2524 /* Parameters for walk_type. */
2526 struct walk_type_data
2528 process_field_fn process_field;
2529 const void *cookie;
2530 outf_p of;
2531 options_p opt;
2532 const char *val;
2533 const char *prev_val[4];
2534 int indent;
2535 int counter;
2536 const struct fileloc *line;
2537 lang_bitmap bitmap;
2538 type_p *param;
2539 int used_length;
2540 type_p orig_s;
2541 const char *reorder_fn;
2542 bool needs_cast_p;
2543 bool fn_wants_lvalue;
2544 bool in_record_p;
2545 int loopcounter;
2546 bool in_ptr_field;
2547 bool have_this_obj;
2551 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2552 pre-processor identifier to use in a #define directive. This replaces
2553 special characters used in C++ identifiers like '>', '<' and ':' with
2554 '_'.
2556 If no C++ special characters are found in TYPE_NAME, return
2557 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2558 characters replaced with '_'. In this case, the caller is
2559 responsible for freeing the allocated string. */
2561 static const char *
2562 filter_type_name (const char *type_name)
2564 if (strchr (type_name, '<') || strchr (type_name, ':'))
2566 size_t i;
2567 char *s = xstrdup (type_name);
2568 for (i = 0; i < strlen (s); i++)
2569 if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ','
2570 || s[i] == '*')
2571 s[i] = '_';
2572 return s;
2574 else
2575 return type_name;
2579 /* Print a mangled name representing T to OF. */
2581 static void
2582 output_mangled_typename (outf_p of, const_type_p t)
2584 if (t == NULL)
2585 oprintf (of, "Z");
2586 else
2587 switch (t->kind)
2589 case TYPE_NONE:
2590 case TYPE_UNDEFINED:
2591 gcc_unreachable ();
2592 break;
2593 case TYPE_POINTER:
2594 oprintf (of, "P");
2595 output_mangled_typename (of, t->u.p);
2596 break;
2597 case TYPE_SCALAR:
2598 oprintf (of, "I");
2599 break;
2600 case TYPE_STRING:
2601 oprintf (of, "S");
2602 break;
2603 case TYPE_STRUCT:
2604 case TYPE_UNION:
2605 case TYPE_LANG_STRUCT:
2606 case TYPE_USER_STRUCT:
2608 /* For references to classes within an inheritance hierarchy,
2609 only ever reference the ultimate base class, since only
2610 it will have gt_ functions. */
2611 t = get_ultimate_base_class (t);
2612 const char *id_for_tag = filter_type_name (t->u.s.tag);
2613 oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2614 id_for_tag);
2615 if (id_for_tag != t->u.s.tag)
2616 free (CONST_CAST (char *, id_for_tag));
2618 break;
2619 case TYPE_PARAM_STRUCT:
2621 int i;
2622 for (i = 0; i < NUM_PARAM; i++)
2623 if (t->u.param_struct.param[i] != NULL)
2624 output_mangled_typename (of, t->u.param_struct.param[i]);
2625 output_mangled_typename (of, t->u.param_struct.stru);
2627 break;
2628 case TYPE_ARRAY:
2629 gcc_unreachable ();
2633 /* Print PARAM to D->OF processing escapes. D->VAL references the
2634 current object, D->PREV_VAL the object containing the current
2635 object, ONAME is the name of the option and D->LINE is used to
2636 print error messages. */
2638 static void
2639 output_escaped_param (struct walk_type_data *d, const char *param,
2640 const char *oname)
2642 const char *p;
2644 for (p = param; *p; p++)
2645 if (*p != '%')
2646 oprintf (d->of, "%c", *p);
2647 else
2648 switch (*++p)
2650 case 'h':
2651 oprintf (d->of, "(%s)", d->prev_val[2]);
2652 break;
2653 case '0':
2654 oprintf (d->of, "(%s)", d->prev_val[0]);
2655 break;
2656 case '1':
2657 oprintf (d->of, "(%s)", d->prev_val[1]);
2658 break;
2659 case 'a':
2661 const char *pp = d->val + strlen (d->val);
2662 while (pp[-1] == ']')
2663 while (*pp != '[')
2664 pp--;
2665 oprintf (d->of, "%s", pp);
2667 break;
2668 default:
2669 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2670 oname, '%', *p);
2674 const char *
2675 get_string_option (options_p opt, const char *key)
2677 for (; opt; opt = opt->next)
2678 if (strcmp (opt->name, key) == 0)
2679 return opt->info.string;
2680 return NULL;
2683 /* Machinery for avoiding duplicate tags within switch statements. */
2684 struct seen_tag
2686 const char *tag;
2687 struct seen_tag *next;
2691 already_seen_tag (struct seen_tag *seen_tags, const char *tag)
2693 /* Linear search, so O(n^2), but n is currently small. */
2694 while (seen_tags)
2696 if (!strcmp (seen_tags->tag, tag))
2697 return 1;
2698 seen_tags = seen_tags->next;
2700 /* Not yet seen this tag. */
2701 return 0;
2704 void
2705 mark_tag_as_seen (struct seen_tag **seen_tags, const char *tag)
2707 /* Add to front of linked list. */
2708 struct seen_tag *new_node = XCNEW (struct seen_tag);
2709 new_node->tag = tag;
2710 new_node->next = *seen_tags;
2711 *seen_tags = new_node;
2714 static void
2715 walk_subclasses (type_p base, struct walk_type_data *d,
2716 struct seen_tag **seen_tags)
2718 for (type_p sub = base->u.s.first_subclass; sub != NULL;
2719 sub = sub->u.s.next_sibling_class)
2721 const char *type_tag = get_string_option (sub->u.s.opt, "tag");
2722 if (type_tag && !already_seen_tag (*seen_tags, type_tag))
2724 mark_tag_as_seen (seen_tags, type_tag);
2725 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
2726 d->indent += 2;
2727 oprintf (d->of, "%*s{\n", d->indent, "");
2728 d->indent += 2;
2729 oprintf (d->of, "%*s%s *sub = static_cast <%s *> (x);\n",
2730 d->indent, "", sub->u.s.tag, sub->u.s.tag);
2731 const char *old_val = d->val;
2732 d->val = "(*sub)";
2733 walk_type (sub, d);
2734 d->val = old_val;
2735 d->indent -= 2;
2736 oprintf (d->of, "%*s}\n", d->indent, "");
2737 oprintf (d->of, "%*sbreak;\n", d->indent, "");
2738 d->indent -= 2;
2740 walk_subclasses (sub, d, seen_tags);
2744 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2745 which is of type T. Write code to D->OF to constrain execution (at
2746 the point that D->PROCESS_FIELD is called) to the appropriate
2747 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2748 pointers to those objects. D->PREV_VAL lists the objects
2749 containing the current object, D->OPT is a list of options to
2750 apply, D->INDENT is the current indentation level, D->LINE is used
2751 to print error messages, D->BITMAP indicates which languages to
2752 print the structure for, and D->PARAM is the current parameter
2753 (from an enclosing param_is option). */
2755 static void
2756 walk_type (type_p t, struct walk_type_data *d)
2758 const char *length = NULL;
2759 const char *desc = NULL;
2760 const char *type_tag = NULL;
2761 int maybe_undef_p = 0;
2762 int use_param_num = -1;
2763 int use_params_p = 0;
2764 int atomic_p = 0;
2765 options_p oo;
2766 const struct nested_ptr_data *nested_ptr_d = NULL;
2768 d->needs_cast_p = false;
2769 for (oo = d->opt; oo; oo = oo->next)
2770 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2771 length = oo->info.string;
2772 else if (strcmp (oo->name, "maybe_undef") == 0)
2773 maybe_undef_p = 1;
2774 else if (strncmp (oo->name, "use_param", 9) == 0
2775 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2776 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2777 else if (strcmp (oo->name, "use_params") == 0)
2778 use_params_p = 1;
2779 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2780 desc = oo->info.string;
2781 else if (strcmp (oo->name, "mark_hook") == 0)
2783 else if (strcmp (oo->name, "nested_ptr") == 0
2784 && oo->kind == OPTION_NESTED)
2785 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2786 else if (strcmp (oo->name, "dot") == 0)
2788 else if (strcmp (oo->name, "tag") == 0)
2789 type_tag = oo->info.string;
2790 else if (strcmp (oo->name, "special") == 0)
2792 else if (strcmp (oo->name, "skip") == 0)
2794 else if (strcmp (oo->name, "atomic") == 0)
2795 atomic_p = 1;
2796 else if (strcmp (oo->name, "default") == 0)
2798 else if (strcmp (oo->name, "param_is") == 0)
2800 else if (strncmp (oo->name, "param", 5) == 0
2801 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2803 else if (strcmp (oo->name, "chain_next") == 0)
2805 else if (strcmp (oo->name, "chain_prev") == 0)
2807 else if (strcmp (oo->name, "chain_circular") == 0)
2809 else if (strcmp (oo->name, "reorder") == 0)
2811 else if (strcmp (oo->name, "variable_size") == 0)
2813 else
2814 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2816 if (d->used_length)
2817 length = NULL;
2819 if (use_params_p)
2821 int pointer_p = t->kind == TYPE_POINTER;
2823 if (pointer_p)
2824 t = t->u.p;
2825 if (!union_or_struct_p (t))
2826 error_at_line (d->line, "`use_params' option on unimplemented type");
2827 else
2828 t = find_param_structure (t, d->param);
2829 if (pointer_p)
2830 t = create_pointer (t);
2833 if (use_param_num != -1)
2835 if (d->param != NULL && d->param[use_param_num] != NULL)
2837 type_p nt = d->param[use_param_num];
2839 if (t->kind == TYPE_ARRAY)
2840 nt = create_array (nt, t->u.a.len);
2841 else if (length != NULL && t->kind == TYPE_POINTER)
2842 nt = create_pointer (nt);
2843 d->needs_cast_p = (t->kind != TYPE_POINTER
2844 && (nt->kind == TYPE_POINTER
2845 || nt->kind == TYPE_STRING));
2846 t = nt;
2848 else
2849 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2852 if (maybe_undef_p
2853 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
2855 error_at_line (d->line,
2856 "field `%s' has invalid option `maybe_undef_p'\n",
2857 d->val);
2858 return;
2861 if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2863 error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2864 return;
2867 switch (t->kind)
2869 case TYPE_SCALAR:
2870 case TYPE_STRING:
2871 d->process_field (t, d);
2872 break;
2874 case TYPE_POINTER:
2876 d->in_ptr_field = true;
2877 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2879 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2880 break;
2883 /* If a pointer type is marked as "atomic", we process the
2884 field itself, but we don't walk the data that they point to.
2886 There are two main cases where we walk types: to mark
2887 pointers that are reachable, and to relocate pointers when
2888 writing a PCH file. In both cases, an atomic pointer is
2889 itself marked or relocated, but the memory that it points
2890 to is left untouched. In the case of PCH, that memory will
2891 be read/written unchanged to the PCH file. */
2892 if (atomic_p)
2894 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2895 d->indent += 2;
2896 d->process_field (t, d);
2897 d->indent -= 2;
2898 oprintf (d->of, "%*s}\n", d->indent, "");
2899 break;
2902 if (!length)
2904 if (!union_or_struct_p (t->u.p)
2905 && t->u.p->kind != TYPE_PARAM_STRUCT)
2907 error_at_line (d->line,
2908 "field `%s' is pointer to unimplemented type",
2909 d->val);
2910 break;
2913 if (nested_ptr_d)
2915 const char *oldprevval2 = d->prev_val[2];
2917 if (!union_or_struct_p (nested_ptr_d->type))
2919 error_at_line (d->line,
2920 "field `%s' has invalid "
2921 "option `nested_ptr'\n", d->val);
2922 return;
2925 d->prev_val[2] = d->val;
2926 oprintf (d->of, "%*s{\n", d->indent, "");
2927 d->indent += 2;
2928 d->val = xasprintf ("x%d", d->counter++);
2929 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2930 (nested_ptr_d->type->kind == TYPE_UNION
2931 ? "union" : "struct"),
2932 nested_ptr_d->type->u.s.tag,
2933 d->fn_wants_lvalue ? "" : "const ", d->val);
2934 oprintf (d->of, "%*s", d->indent + 2, "");
2935 output_escaped_param (d, nested_ptr_d->convert_from,
2936 "nested_ptr");
2937 oprintf (d->of, ";\n");
2939 d->process_field (nested_ptr_d->type, d);
2941 if (d->fn_wants_lvalue)
2943 oprintf (d->of, "%*s%s = ", d->indent, "",
2944 d->prev_val[2]);
2945 d->prev_val[2] = d->val;
2946 output_escaped_param (d, nested_ptr_d->convert_to,
2947 "nested_ptr");
2948 oprintf (d->of, ";\n");
2951 d->indent -= 2;
2952 oprintf (d->of, "%*s}\n", d->indent, "");
2953 d->val = d->prev_val[2];
2954 d->prev_val[2] = oldprevval2;
2956 else
2957 d->process_field (t->u.p, d);
2959 else
2961 int loopcounter = d->loopcounter;
2962 const char *oldval = d->val;
2963 const char *oldprevval3 = d->prev_val[3];
2964 char *newval;
2966 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2967 d->indent += 2;
2968 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2969 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2970 "", loopcounter, loopcounter);
2971 if (!d->in_record_p)
2972 output_escaped_param (d, length, "length");
2973 else
2974 oprintf (d->of, "l%d", loopcounter);
2975 if (d->have_this_obj)
2976 /* Try to unswitch loops (see PR53880). */
2977 oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
2978 oprintf (d->of, "); i%d++) {\n", loopcounter);
2979 d->indent += 2;
2980 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2981 d->used_length = 1;
2982 d->prev_val[3] = oldval;
2983 walk_type (t->u.p, d);
2984 free (newval);
2985 d->val = oldval;
2986 d->prev_val[3] = oldprevval3;
2987 d->used_length = 0;
2988 d->indent -= 2;
2989 oprintf (d->of, "%*s}\n", d->indent, "");
2990 d->process_field (t, d);
2991 d->indent -= 2;
2992 oprintf (d->of, "%*s}\n", d->indent, "");
2994 d->in_ptr_field = false;
2996 break;
2998 case TYPE_ARRAY:
3000 int loopcounter;
3001 const char *oldval = d->val;
3002 char *newval;
3004 /* If it's an array of scalars, we optimize by not generating
3005 any code. */
3006 if (t->u.a.p->kind == TYPE_SCALAR)
3007 break;
3009 if (length)
3010 loopcounter = d->loopcounter;
3011 else
3012 loopcounter = d->counter++;
3014 /* When walking an array, compute the length and store it in a
3015 local variable before walking the array elements, instead of
3016 recomputing the length expression each time through the loop.
3017 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
3018 where the length is stored in the first array element,
3019 because otherwise that operand can get overwritten on the
3020 first iteration. */
3021 oprintf (d->of, "%*s{\n", d->indent, "");
3022 d->indent += 2;
3023 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
3024 if (!d->in_record_p || !length)
3026 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
3027 d->indent, "", loopcounter);
3028 if (length)
3029 output_escaped_param (d, length, "length");
3030 else
3031 oprintf (d->of, "%s", t->u.a.len);
3032 oprintf (d->of, ");\n");
3035 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
3036 d->indent, "",
3037 loopcounter, loopcounter, loopcounter, loopcounter);
3038 d->indent += 2;
3039 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
3040 d->used_length = 1;
3041 walk_type (t->u.a.p, d);
3042 free (newval);
3043 d->used_length = 0;
3044 d->val = oldval;
3045 d->indent -= 2;
3046 oprintf (d->of, "%*s}\n", d->indent, "");
3047 d->indent -= 2;
3048 oprintf (d->of, "%*s}\n", d->indent, "");
3050 break;
3052 case TYPE_STRUCT:
3053 case TYPE_UNION:
3055 pair_p f;
3056 const char *oldval = d->val;
3057 const char *oldprevval1 = d->prev_val[1];
3058 const char *oldprevval2 = d->prev_val[2];
3059 const char *struct_mark_hook = NULL;
3060 const int union_p = t->kind == TYPE_UNION;
3061 int seen_default_p = 0;
3062 options_p o;
3063 int lengths_seen = 0;
3064 int endcounter;
3065 bool any_length_seen = false;
3067 if (!t->u.s.line.file)
3068 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
3070 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
3072 error_at_line (d->line,
3073 "structure `%s' defined for mismatching languages",
3074 t->u.s.tag);
3075 error_at_line (&t->u.s.line, "one structure defined here");
3078 /* Some things may also be defined in the structure's options. */
3079 for (o = t->u.s.opt; o; o = o->next)
3080 if (!desc && strcmp (o->name, "desc") == 0
3081 && o->kind == OPTION_STRING)
3082 desc = o->info.string;
3083 else if (!struct_mark_hook && strcmp (o->name, "mark_hook") == 0
3084 && o->kind == OPTION_STRING)
3085 struct_mark_hook = o->info.string;
3087 if (struct_mark_hook)
3088 oprintf (d->of, "%*s%s (&%s);\n",
3089 d->indent, "", struct_mark_hook, oldval);
3091 d->prev_val[2] = oldval;
3092 d->prev_val[1] = oldprevval2;
3093 if (union_p)
3095 if (desc == NULL)
3097 error_at_line (d->line,
3098 "missing `desc' option for union `%s'",
3099 t->u.s.tag);
3100 desc = "1";
3102 oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
3103 output_escaped_param (d, desc, "desc");
3104 oprintf (d->of, "))\n");
3105 d->indent += 2;
3106 oprintf (d->of, "%*s{\n", d->indent, "");
3108 else if (desc)
3110 /* We have a "desc" option on a struct, signifying the
3111 base class within a GC-managed inheritance hierarchy.
3112 The current code specialcases the base class, then walks
3113 into subclasses, recursing into this routine to handle them.
3114 This organization requires the base class to have a case in
3115 the switch statement, and hence a tag value is mandatory
3116 for the base class. This restriction could be removed, but
3117 it would require some restructing of this code. */
3118 if (!type_tag)
3120 error_at_line (d->line,
3121 "missing `tag' option for type `%s'",
3122 t->u.s.tag);
3124 oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
3125 output_escaped_param (d, desc, "desc");
3126 oprintf (d->of, "))\n");
3127 d->indent += 2;
3128 oprintf (d->of, "%*s{\n", d->indent, "");
3129 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
3130 d->indent += 2;
3133 FOR_ALL_INHERITED_FIELDS (t, f)
3135 options_p oo;
3136 int skip_p = 0;
3137 const char *fieldlength = NULL;
3139 d->reorder_fn = NULL;
3140 for (oo = f->opt; oo; oo = oo->next)
3141 if (strcmp (oo->name, "skip") == 0)
3142 skip_p = 1;
3143 else if (strcmp (oo->name, "length") == 0
3144 && oo->kind == OPTION_STRING)
3145 fieldlength = oo->info.string;
3147 if (skip_p)
3148 continue;
3149 if (fieldlength)
3151 lengths_seen++;
3152 d->counter++;
3153 if (!union_p)
3155 if (!any_length_seen)
3157 oprintf (d->of, "%*s{\n", d->indent, "");
3158 d->indent += 2;
3160 any_length_seen = true;
3162 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
3163 d->indent, "", d->counter - 1);
3164 output_escaped_param (d, fieldlength, "length");
3165 oprintf (d->of, ");\n");
3169 endcounter = d->counter;
3171 FOR_ALL_INHERITED_FIELDS (t, f)
3173 options_p oo;
3174 const char *dot = ".";
3175 const char *tagid = NULL;
3176 int skip_p = 0;
3177 int default_p = 0;
3178 int use_param_p = 0;
3179 const char *fieldlength = NULL;
3180 char *newval;
3182 d->reorder_fn = NULL;
3183 for (oo = f->opt; oo; oo = oo->next)
3184 if (strcmp (oo->name, "dot") == 0
3185 && oo->kind == OPTION_STRING)
3186 dot = oo->info.string;
3187 else if (strcmp (oo->name, "tag") == 0
3188 && oo->kind == OPTION_STRING)
3189 tagid = oo->info.string;
3190 else if (strcmp (oo->name, "skip") == 0)
3191 skip_p = 1;
3192 else if (strcmp (oo->name, "default") == 0)
3193 default_p = 1;
3194 else if (strcmp (oo->name, "reorder") == 0
3195 && oo->kind == OPTION_STRING)
3196 d->reorder_fn = oo->info.string;
3197 else if (strncmp (oo->name, "use_param", 9) == 0
3198 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
3199 use_param_p = 1;
3200 else if (strcmp (oo->name, "length") == 0
3201 && oo->kind == OPTION_STRING)
3202 fieldlength = oo->info.string;
3204 if (skip_p)
3205 continue;
3207 if (union_p && tagid)
3209 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
3210 d->indent += 2;
3212 else if (union_p && default_p)
3214 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3215 d->indent += 2;
3216 seen_default_p = 1;
3218 else if (!union_p && (default_p || tagid))
3219 error_at_line (d->line,
3220 "can't use `%s' outside a union on field `%s'",
3221 default_p ? "default" : "tag", f->name);
3222 else if (union_p && !(default_p || tagid)
3223 && f->type->kind == TYPE_SCALAR)
3225 fprintf (stderr,
3226 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3227 get_input_file_name (d->line->file), d->line->line,
3228 f->name);
3229 continue;
3231 else if (union_p && !(default_p || tagid))
3232 error_at_line (d->line,
3233 "field `%s' is missing `tag' or `default' option",
3234 f->name);
3236 if (fieldlength)
3238 d->loopcounter = endcounter - lengths_seen--;
3241 d->line = &f->line;
3242 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
3243 d->opt = f->opt;
3244 d->used_length = false;
3245 d->in_record_p = !union_p;
3247 if (union_p && use_param_p && d->param == NULL)
3248 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
3249 else
3250 walk_type (f->type, d);
3252 d->in_record_p = false;
3254 free (newval);
3256 if (union_p)
3258 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3259 d->indent -= 2;
3262 d->reorder_fn = NULL;
3264 d->val = oldval;
3265 d->prev_val[1] = oldprevval1;
3266 d->prev_val[2] = oldprevval2;
3268 if (union_p && !seen_default_p)
3270 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3271 oprintf (d->of, "%*s break;\n", d->indent, "");
3274 if (desc && !union_p)
3276 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3277 d->indent -= 2;
3279 if (union_p)
3281 oprintf (d->of, "%*s}\n", d->indent, "");
3282 d->indent -= 2;
3284 else if (desc)
3286 /* Add cases to handle subclasses. */
3287 struct seen_tag *tags = NULL;
3288 walk_subclasses (t, d, &tags);
3290 /* Ensure that if someone forgets a "tag" option that we don't
3291 silent fail to traverse that subclass's fields. */
3292 if (!seen_default_p)
3294 oprintf (d->of, "%*s/* Unrecognized tag value. */\n",
3295 d->indent, "");
3296 oprintf (d->of, "%*sdefault: gcc_unreachable (); \n",
3297 d->indent, "");
3300 /* End of the switch statement */
3301 oprintf (d->of, "%*s}\n", d->indent, "");
3302 d->indent -= 2;
3304 if (any_length_seen)
3306 d->indent -= 2;
3307 oprintf (d->of, "%*s}\n", d->indent, "");
3310 break;
3312 case TYPE_LANG_STRUCT:
3314 type_p nt;
3315 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3316 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3317 break;
3318 if (nt == NULL)
3319 error_at_line (d->line, "structure `%s' differs between languages",
3320 t->u.s.tag);
3321 else
3322 walk_type (nt, d);
3324 break;
3326 case TYPE_PARAM_STRUCT:
3328 type_p *oldparam = d->param;
3330 d->param = t->u.param_struct.param;
3331 walk_type (t->u.param_struct.stru, d);
3332 d->param = oldparam;
3334 break;
3336 case TYPE_USER_STRUCT:
3337 d->process_field (t, d);
3338 break;
3340 case TYPE_NONE:
3341 case TYPE_UNDEFINED:
3342 gcc_unreachable ();
3346 /* process_field routine for marking routines. */
3348 static void
3349 write_types_process_field (type_p f, const struct walk_type_data *d)
3351 const struct write_types_data *wtd;
3352 const char *cast = d->needs_cast_p ? "(void *)" : "";
3353 wtd = (const struct write_types_data *) d->cookie;
3355 switch (f->kind)
3357 case TYPE_NONE:
3358 case TYPE_UNDEFINED:
3359 gcc_unreachable ();
3360 case TYPE_POINTER:
3361 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
3362 wtd->subfield_marker_routine, cast, d->val);
3363 if (wtd->param_prefix)
3365 if (f->u.p->kind == TYPE_SCALAR)
3366 /* The current type is a pointer to a scalar (so not
3367 considered like a pointer to instances of user defined
3368 types) and we are seeing it; it means we must be even
3369 more careful about the second argument of the
3370 SUBFIELD_MARKER_ROUTINE call. That argument must
3371 always be the instance of the type for which
3372 write_func_for_structure was called - this really is
3373 what the function SUBFIELD_MARKER_ROUTINE expects.
3374 That is, it must be an instance of the ORIG_S type
3375 parameter of write_func_for_structure. The convention
3376 is that that argument must be "x" in that case (as set
3377 by write_func_for_structure). The problem is, we can't
3378 count on d->prev_val[3] to be always set to "x" in that
3379 case. Sometimes walk_type can set it to something else
3380 (to e.g cooperate with write_array when called from
3381 write_roots). So let's set it to "x" here then. */
3382 oprintf (d->of, ", x");
3383 else
3384 oprintf (d->of, ", %s", d->prev_val[3]);
3385 if (d->orig_s)
3387 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3388 output_mangled_typename (d->of, d->orig_s);
3390 else
3391 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3393 oprintf (d->of, ");\n");
3394 if (d->reorder_fn && wtd->reorder_note_routine)
3395 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
3396 wtd->reorder_note_routine, cast, d->val,
3397 d->prev_val[3], d->reorder_fn);
3398 break;
3400 case TYPE_STRING:
3401 case TYPE_STRUCT:
3402 case TYPE_UNION:
3403 case TYPE_LANG_STRUCT:
3404 case TYPE_PARAM_STRUCT:
3405 case TYPE_USER_STRUCT:
3406 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3408 /* If F is a user-defined type and the field is not a
3409 pointer to the type, then we should not generate the
3410 standard pointer-marking code. All we need to do is call
3411 the user-provided marking function to process the fields
3412 of F. */
3413 oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3414 d->val);
3416 else
3418 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3419 output_mangled_typename (d->of, f);
3420 oprintf (d->of, " (%s%s);\n", cast, d->val);
3421 if (d->reorder_fn && wtd->reorder_note_routine)
3422 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
3423 wtd->reorder_note_routine, cast, d->val, cast, d->val,
3424 d->reorder_fn);
3426 break;
3428 case TYPE_SCALAR:
3429 break;
3431 case TYPE_ARRAY:
3432 gcc_unreachable ();
3436 /* Return an output file that is suitable for definitions which can
3437 reference struct S */
3439 static outf_p
3440 get_output_file_for_structure (const_type_p s, type_p *param)
3442 const input_file *fn;
3443 int i;
3445 gcc_assert (union_or_struct_p (s));
3446 fn = s->u.s.line.file;
3448 /* This is a hack, and not the good kind either. */
3449 for (i = NUM_PARAM - 1; i >= 0; i--)
3450 if (param && param[i] && param[i]->kind == TYPE_POINTER
3451 && union_or_struct_p (param[i]->u.p))
3452 fn = param[i]->u.p->u.s.line.file;
3454 /* The call to get_output_file_with_visibility may update fn by
3455 caching its result inside, so we need the CONST_CAST. */
3456 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3460 /* Returns the specifier keyword for a string or union type S, empty string
3461 otherwise. */
3463 static const char *
3464 get_type_specifier (const type_p s)
3466 if (s->kind == TYPE_STRUCT)
3467 return "struct ";
3468 else if (s->kind == TYPE_LANG_STRUCT)
3469 return get_type_specifier (s->u.s.lang_struct);
3470 else if (s->kind == TYPE_UNION)
3471 return "union ";
3472 return "";
3476 /* Emits a declaration for type TY (assumed to be a union or a
3477 structure) on stream OUT. */
3479 static void
3480 write_type_decl (outf_p out, type_p ty)
3482 if (union_or_struct_p (ty))
3483 oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3484 else if (ty->kind == TYPE_SCALAR)
3486 if (ty->u.scalar_is_char)
3487 oprintf (out, "const char");
3488 else
3489 oprintf (out, "void");
3491 else if (ty->kind == TYPE_POINTER)
3493 write_type_decl (out, ty->u.p);
3494 oprintf (out, " *");
3496 else if (ty->kind == TYPE_ARRAY)
3498 write_type_decl (out, ty->u.a.p);
3499 oprintf (out, " *");
3501 else if (ty->kind == TYPE_STRING)
3503 oprintf (out, "const char *");
3505 else
3506 gcc_unreachable ();
3510 /* Write on OF the name of the marker function for structure S. PREFIX
3511 is the prefix to use (to distinguish ggc from pch markers). */
3513 static void
3514 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3516 if (union_or_struct_p (s))
3518 const char *id_for_tag = filter_type_name (s->u.s.tag);
3519 oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3520 if (id_for_tag != s->u.s.tag)
3521 free (CONST_CAST (char *, id_for_tag));
3523 else if (s->kind == TYPE_PARAM_STRUCT)
3525 oprintf (of, "gt_%s_", prefix);
3526 output_mangled_typename (of, s);
3528 else
3529 gcc_unreachable ();
3532 /* Write on OF a user-callable routine to act as an entry point for
3533 the marking routine for S, generated by write_func_for_structure.
3534 WTD distinguishes between ggc and pch markers. */
3536 static void
3537 write_user_func_for_structure_ptr (outf_p of, type_p s, const write_types_data *wtd)
3539 /* Parameterized structures are not supported in user markers. There
3540 is no way for the marker function to know which specific type
3541 to use to generate the call to the void * entry point. For
3542 instance, a marker for struct htab may need to call different
3543 routines to mark the fields, depending on the paramN_is attributes.
3545 A user-defined marker that accepts 'struct htab' as its argument
3546 would not know which variant to call. Generating several entry
3547 points accepting 'struct htab' would cause multiply-defined
3548 errors during compilation. */
3549 gcc_assert (union_or_struct_p (s));
3551 type_p alias_of = NULL;
3552 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3553 if (strcmp (opt->name, "ptr_alias") == 0)
3555 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3556 we do not generate marking code for ORIG_S here. Instead, a
3557 forwarder #define in gtype-desc.h will cause every call to its
3558 marker to call the target of this alias.
3560 However, we still want to create a user entry code for the
3561 aliased type. So, if ALIAS_OF is set, we only generate the
3562 user-callable marker function. */
3563 alias_of = opt->info.type;
3564 break;
3567 DBGPRINTF ("write_user_func_for_structure_ptr: %s %s", s->u.s.tag,
3568 wtd->prefix);
3570 /* Only write the function once. */
3571 if (s->u.s.wrote_user_func_for_ptr[wtd->kind])
3572 return;
3573 s->u.s.wrote_user_func_for_ptr[wtd->kind] = true;
3575 oprintf (of, "\nvoid\n");
3576 oprintf (of, "gt_%sx (", wtd->prefix);
3577 write_type_decl (of, s);
3578 oprintf (of, " *& x)\n");
3579 oprintf (of, "{\n");
3580 oprintf (of, " if (x)\n ");
3581 write_marker_function_name (of,
3582 alias_of ? alias_of : get_ultimate_base_class (s),
3583 wtd->prefix);
3584 oprintf (of, " ((void *) x);\n");
3585 oprintf (of, "}\n");
3589 /* Write a function to mark all the fields of type S on OF. PREFIX
3590 and D are as in write_user_marking_functions. */
3592 static void
3593 write_user_func_for_structure_body (type_p s, const char *prefix,
3594 struct walk_type_data *d)
3596 oprintf (d->of, "\nvoid\n");
3597 oprintf (d->of, "gt_%sx (", prefix);
3598 write_type_decl (d->of, s);
3599 oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3600 oprintf (d->of, "{\n");
3601 oprintf (d->of, " ");
3602 write_type_decl (d->of, s);
3603 oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3604 d->val = "(*x)";
3605 d->indent = 2;
3606 walk_type (s, d);
3607 oprintf (d->of, "}\n");
3611 /* Emit the user-callable functions needed to mark all the types used
3612 by the user structure S. PREFIX is the prefix to use to
3613 distinguish ggc and pch markers. D contains data needed to pass to
3614 walk_type when traversing the fields of a type.
3616 For every type T referenced by S, two routines are generated: one
3617 that takes 'T *', marks the pointer and calls the second routine,
3618 which just marks the fields of T. */
3620 static void
3621 write_user_marking_functions (type_p s,
3622 const write_types_data *w,
3623 struct walk_type_data *d)
3625 gcc_assert (s->kind == TYPE_USER_STRUCT);
3627 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3629 type_p fld_type = fld->type;
3630 if (fld_type->kind == TYPE_POINTER)
3632 type_p pointed_to_type = fld_type->u.p;
3633 if (union_or_struct_p (pointed_to_type))
3634 write_user_func_for_structure_ptr (d->of, pointed_to_type, w);
3636 else if (union_or_struct_p (fld_type))
3637 write_user_func_for_structure_body (fld_type, w->prefix, d);
3642 /* For S, a structure that's part of ORIG_S, and using parameters
3643 PARAM, write out a routine that:
3644 - Takes a parameter, a void * but actually of type *S
3645 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3646 field of S or its substructures and (in some cases) things
3647 that are pointed to by S. */
3649 static void
3650 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
3651 const struct write_types_data *wtd)
3653 const char *chain_next = NULL;
3654 const char *chain_prev = NULL;
3655 const char *chain_circular = NULL;
3656 const char *mark_hook_name = NULL;
3657 options_p opt;
3658 struct walk_type_data d;
3660 if (s->u.s.base_class)
3662 /* Verify that the base class has a "desc", since otherwise
3663 the traversal hooks there won't attempt to visit fields of
3664 subclasses such as this one. */
3665 const_type_p ubc = get_ultimate_base_class (s);
3666 if ((!opts_have (ubc->u.s.opt, "user")
3667 && !opts_have (ubc->u.s.opt, "desc")))
3668 error_at_line (&s->u.s.line,
3669 ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
3670 ", but '%s' lacks a discriminator 'desc' option"),
3671 s->u.s.tag, ubc->u.s.tag, ubc->u.s.tag);
3673 /* Don't write fns for subclasses, only for the ultimate base class
3674 within an inheritance hierarchy. */
3675 return;
3678 memset (&d, 0, sizeof (d));
3679 d.of = get_output_file_for_structure (s, param);
3680 for (opt = s->u.s.opt; opt; opt = opt->next)
3681 if (strcmp (opt->name, "chain_next") == 0
3682 && opt->kind == OPTION_STRING)
3683 chain_next = opt->info.string;
3684 else if (strcmp (opt->name, "chain_prev") == 0
3685 && opt->kind == OPTION_STRING)
3686 chain_prev = opt->info.string;
3687 else if (strcmp (opt->name, "chain_circular") == 0
3688 && opt->kind == OPTION_STRING)
3689 chain_circular = opt->info.string;
3690 else if (strcmp (opt->name, "mark_hook") == 0
3691 && opt->kind == OPTION_STRING)
3692 mark_hook_name = opt->info.string;
3693 if (chain_prev != NULL && chain_next == NULL)
3694 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3695 if (chain_circular != NULL && chain_next != NULL)
3696 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3697 if (chain_circular != NULL)
3698 chain_next = chain_circular;
3700 d.process_field = write_types_process_field;
3701 d.cookie = wtd;
3702 d.orig_s = orig_s;
3703 d.opt = s->u.s.opt;
3704 d.line = &s->u.s.line;
3705 d.bitmap = s->u.s.bitmap;
3706 d.param = param;
3707 d.prev_val[0] = "*x";
3708 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3709 d.prev_val[3] = "x";
3710 d.val = "(*x)";
3711 d.have_this_obj = false;
3713 oprintf (d.of, "\n");
3714 oprintf (d.of, "void\n");
3715 write_marker_function_name (d.of, orig_s, wtd->prefix);
3716 oprintf (d.of, " (void *x_p)\n");
3717 oprintf (d.of, "{\n ");
3718 write_type_decl (d.of, s);
3719 oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3720 write_type_decl (d.of, s);
3721 oprintf (d.of, " *)x_p;\n");
3722 if (chain_next != NULL)
3724 /* TYPE_USER_STRUCTs should not occur here. These structures
3725 are completely handled by user code. */
3726 gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
3728 oprintf (d.of, " ");
3729 write_type_decl (d.of, s);
3730 oprintf (d.of, " * xlimit = x;\n");
3732 if (chain_next == NULL)
3734 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3735 if (wtd->param_prefix)
3737 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3738 output_mangled_typename (d.of, orig_s);
3740 oprintf (d.of, "))\n");
3742 else
3744 if (chain_circular != NULL)
3745 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3746 else
3747 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3748 if (wtd->param_prefix)
3750 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3751 output_mangled_typename (d.of, orig_s);
3753 oprintf (d.of, "))\n");
3754 if (chain_circular != NULL)
3755 oprintf (d.of, " return;\n do\n");
3756 if (mark_hook_name && !wtd->skip_hooks)
3758 oprintf (d.of, " {\n");
3759 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
3761 oprintf (d.of, " xlimit = (");
3762 d.prev_val[2] = "*xlimit";
3763 output_escaped_param (&d, chain_next, "chain_next");
3764 oprintf (d.of, ");\n");
3765 if (mark_hook_name && !wtd->skip_hooks)
3766 oprintf (d.of, " }\n");
3767 if (chain_prev != NULL)
3769 oprintf (d.of, " if (x != xlimit)\n");
3770 oprintf (d.of, " for (;;)\n");
3771 oprintf (d.of, " {\n");
3772 oprintf (d.of, " %s %s * const xprev = (",
3773 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3775 d.prev_val[2] = "*x";
3776 output_escaped_param (&d, chain_prev, "chain_prev");
3777 oprintf (d.of, ");\n");
3778 oprintf (d.of, " if (xprev == NULL) break;\n");
3779 oprintf (d.of, " x = xprev;\n");
3780 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3781 if (wtd->param_prefix)
3783 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3784 output_mangled_typename (d.of, orig_s);
3786 oprintf (d.of, ");\n");
3787 oprintf (d.of, " }\n");
3789 if (chain_circular != NULL)
3791 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3792 if (wtd->param_prefix)
3794 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3795 output_mangled_typename (d.of, orig_s);
3797 oprintf (d.of, "));\n");
3798 if (mark_hook_name && !wtd->skip_hooks)
3799 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
3800 oprintf (d.of, " do\n");
3802 else
3803 oprintf (d.of, " while (x != xlimit)\n");
3805 oprintf (d.of, " {\n");
3806 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3808 oprintf (d.of, " %s (x);\n", mark_hook_name);
3811 d.prev_val[2] = "*x";
3812 d.indent = 6;
3813 if (orig_s->kind != TYPE_USER_STRUCT)
3814 walk_type (s, &d);
3815 else
3817 /* User structures have no fields to walk. Simply generate a call
3818 to the user-provided structure marker. */
3819 oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3822 if (chain_next != NULL)
3824 oprintf (d.of, " x = (");
3825 output_escaped_param (&d, chain_next, "chain_next");
3826 oprintf (d.of, ");\n");
3829 oprintf (d.of, " }\n");
3830 if (chain_circular != NULL)
3831 oprintf (d.of, " while (x != xlimit);\n");
3832 oprintf (d.of, "}\n");
3834 if (orig_s->kind == TYPE_USER_STRUCT)
3835 write_user_marking_functions (orig_s, wtd, &d);
3839 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3841 static void
3842 write_types (outf_p output_header, type_p structures, type_p param_structs,
3843 const struct write_types_data *wtd)
3845 int nbfun = 0; /* Count the emitted functions. */
3846 type_p s;
3848 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3850 /* We first emit the macros and the declarations. Functions' code is
3851 emitted afterwards. This is needed in plugin mode. */
3852 oprintf (output_header, "/* Macros and declarations. */\n");
3853 for (s = structures; s; s = s->next)
3854 /* Do not emit handlers for derived classes; we only ever deal with
3855 the ultimate base class within an inheritance hierarchy. */
3856 if ((s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3857 && !s->u.s.base_class)
3859 options_p opt;
3861 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3862 continue;
3864 const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3866 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3867 output_mangled_typename (output_header, s);
3868 oprintf (output_header, "(X) do { \\\n");
3869 oprintf (output_header,
3870 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3871 s_id_for_tag);
3872 oprintf (output_header, " } while (0)\n");
3874 for (opt = s->u.s.opt; opt; opt = opt->next)
3875 if (strcmp (opt->name, "ptr_alias") == 0
3876 && opt->kind == OPTION_TYPE)
3878 const_type_p const t = (const_type_p) opt->info.type;
3879 if (t->kind == TYPE_STRUCT
3880 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3882 const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3883 oprintf (output_header,
3884 "#define gt_%sx_%s gt_%sx_%s\n",
3885 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3886 if (t_id_for_tag != t->u.s.tag)
3887 free (CONST_CAST (char *, t_id_for_tag));
3889 else
3890 error_at_line (&s->u.s.line,
3891 "structure alias is not a structure");
3892 break;
3894 if (opt)
3895 continue;
3897 /* Declare the marker procedure only once. */
3898 oprintf (output_header,
3899 "extern void gt_%sx_%s (void *);\n",
3900 wtd->prefix, s_id_for_tag);
3902 if (s_id_for_tag != s->u.s.tag)
3903 free (CONST_CAST (char *, s_id_for_tag));
3905 if (s->u.s.line.file == NULL)
3907 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3908 s->u.s.tag);
3909 continue;
3913 for (s = param_structs; s; s = s->next)
3914 if (s->gc_used == GC_POINTED_TO)
3916 type_p stru = s->u.param_struct.stru;
3918 /* Declare the marker procedure. */
3919 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
3920 output_mangled_typename (output_header, s);
3921 oprintf (output_header, " (void *);\n");
3923 if (stru->u.s.line.file == NULL)
3925 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3926 stru->u.s.tag);
3927 continue;
3931 /* At last we emit the functions code. */
3932 oprintf (output_header, "\n/* functions code */\n");
3933 for (s = structures; s; s = s->next)
3934 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3936 options_p opt;
3938 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3939 continue;
3940 for (opt = s->u.s.opt; opt; opt = opt->next)
3941 if (strcmp (opt->name, "ptr_alias") == 0)
3942 break;
3943 if (opt)
3944 continue;
3946 if (s->kind == TYPE_LANG_STRUCT)
3948 type_p ss;
3949 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3951 nbfun++;
3952 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3953 nbfun, (void*) ss, ss->u.s.tag);
3954 write_func_for_structure (s, ss, NULL, wtd);
3957 else
3959 nbfun++;
3960 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3961 nbfun, (void*) s, s->u.s.tag);
3962 write_func_for_structure (s, s, NULL, wtd);
3965 else
3967 /* Structure s is not possibly pointed to, so can be ignored. */
3968 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3969 (void*)s, s->u.s.tag,
3970 (int) s->gc_used);
3973 for (s = param_structs; s; s = s->next)
3974 if (s->gc_used == GC_POINTED_TO)
3976 type_p *param = s->u.param_struct.param;
3977 type_p stru = s->u.param_struct.stru;
3978 if (stru->u.s.line.file == NULL)
3979 continue;
3980 if (stru->kind == TYPE_LANG_STRUCT)
3982 type_p ss;
3983 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3985 nbfun++;
3986 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
3987 nbfun, (void*) ss, ss->u.s.tag);
3988 write_func_for_structure (s, ss, param, wtd);
3991 else
3993 nbfun++;
3994 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
3995 nbfun, (void*) s,
3996 (void*) stru, stru->u.s.tag);
3997 write_func_for_structure (s, stru, param, wtd);
4000 else
4002 /* Param structure s is not pointed to, so should be ignored. */
4003 DBGPRINTF ("ignored s @ %p", (void*)s);
4005 if (verbosity_level >= 2)
4006 printf ("%s emitted %d routines for %s\n",
4007 progname, nbfun, wtd->comment);
4010 static const struct write_types_data ggc_wtd = {
4011 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
4012 "GC marker procedures. ",
4013 FALSE, WTK_GGC
4016 static const struct write_types_data pch_wtd = {
4017 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
4018 "gt_pch_note_reorder",
4019 "PCH type-walking procedures. ",
4020 TRUE, WTK_PCH
4023 /* Write out the local pointer-walking routines. */
4025 /* process_field routine for local pointer-walking for user-callable
4026 routines. The difference between this and
4027 write_types_local_process_field is that, in this case, we do not
4028 need to check whether the given pointer matches the address of the
4029 parent structure. This check was already generated by the call
4030 to gt_pch_nx in the main gt_pch_p_*() function that is calling
4031 this code. */
4033 static void
4034 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
4036 switch (f->kind)
4038 case TYPE_POINTER:
4039 case TYPE_STRUCT:
4040 case TYPE_UNION:
4041 case TYPE_LANG_STRUCT:
4042 case TYPE_PARAM_STRUCT:
4043 case TYPE_STRING:
4044 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4045 break;
4047 case TYPE_USER_STRUCT:
4048 if (d->in_ptr_field)
4049 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4050 else
4051 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4052 d->indent, "", d->val);
4053 break;
4055 case TYPE_SCALAR:
4056 break;
4058 case TYPE_ARRAY:
4059 case TYPE_NONE:
4060 case TYPE_UNDEFINED:
4061 gcc_unreachable ();
4066 /* Write a function to PCH walk all the fields of type S on OF.
4067 D contains data needed by walk_type to recurse into the fields of S. */
4069 static void
4070 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
4072 oprintf (d->of, "\nvoid\n");
4073 oprintf (d->of, "gt_pch_nx (");
4074 write_type_decl (d->of, s);
4075 oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
4076 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4077 "\tATTRIBUTE_UNUSED void *cookie)\n");
4078 oprintf (d->of, "{\n");
4079 d->val = "(*x)";
4080 d->indent = 2;
4081 d->process_field = write_types_local_user_process_field;
4082 walk_type (s, d);
4083 oprintf (d->of, "}\n");
4087 /* Emit the user-callable functions needed to mark all the types used
4088 by the user structure S. PREFIX is the prefix to use to
4089 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
4090 chain_next option defined. D contains data needed to pass to
4091 walk_type when traversing the fields of a type.
4093 For every type T referenced by S, two routines are generated: one
4094 that takes 'T *', marks the pointer and calls the second routine,
4095 which just marks the fields of T. */
4097 static void
4098 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
4100 gcc_assert (s->kind == TYPE_USER_STRUCT);
4102 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
4104 type_p fld_type = fld->type;
4105 if (union_or_struct_p (fld_type))
4106 write_pch_user_walking_for_structure_body (fld_type, d);
4111 /* process_field routine for local pointer-walking. */
4113 static void
4114 write_types_local_process_field (type_p f, const struct walk_type_data *d)
4116 gcc_assert (d->have_this_obj);
4117 switch (f->kind)
4119 case TYPE_POINTER:
4120 case TYPE_STRUCT:
4121 case TYPE_UNION:
4122 case TYPE_LANG_STRUCT:
4123 case TYPE_PARAM_STRUCT:
4124 case TYPE_STRING:
4125 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
4126 d->prev_val[3]);
4127 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4128 break;
4130 case TYPE_USER_STRUCT:
4131 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
4132 d->prev_val[3]);
4133 if (d->in_ptr_field)
4134 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4135 else
4136 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4137 d->indent, "", d->val);
4138 break;
4140 case TYPE_SCALAR:
4141 break;
4143 case TYPE_ARRAY:
4144 case TYPE_NONE:
4145 case TYPE_UNDEFINED:
4146 gcc_unreachable ();
4151 /* For S, a structure that's part of ORIG_S, and using parameters
4152 PARAM, write out a routine that:
4153 - Is of type gt_note_pointers
4154 - Calls PROCESS_FIELD on each field of S or its substructures.
4157 static void
4158 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
4160 struct walk_type_data d;
4162 /* Don't write fns for subclasses, only for the ultimate base class
4163 within an inheritance hierarchy. */
4164 if (s->u.s.base_class)
4165 return;
4167 memset (&d, 0, sizeof (d));
4168 d.of = get_output_file_for_structure (s, param);
4169 d.process_field = write_types_local_process_field;
4170 d.opt = s->u.s.opt;
4171 d.line = &s->u.s.line;
4172 d.bitmap = s->u.s.bitmap;
4173 d.param = param;
4174 d.prev_val[0] = d.prev_val[2] = "*x";
4175 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
4176 d.prev_val[3] = "x";
4177 d.val = "(*x)";
4178 d.fn_wants_lvalue = true;
4180 oprintf (d.of, "\n");
4181 oprintf (d.of, "void\n");
4182 oprintf (d.of, "gt_pch_p_");
4183 output_mangled_typename (d.of, orig_s);
4184 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
4185 "\tvoid *x_p,\n"
4186 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4187 "\tATTRIBUTE_UNUSED void *cookie)\n");
4188 oprintf (d.of, "{\n");
4189 oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
4190 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
4191 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
4192 d.indent = 2;
4193 d.have_this_obj = true;
4195 if (s->kind != TYPE_USER_STRUCT)
4196 walk_type (s, &d);
4197 else
4199 /* User structures have no fields to walk. Simply generate a
4200 call to the user-provided PCH walker. */
4201 oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
4202 d.prev_val[3]);
4203 oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4204 d.indent, "", d.val);
4207 oprintf (d.of, "}\n");
4209 /* Write user-callable entry points for the PCH walking routines. */
4210 if (orig_s->kind == TYPE_USER_STRUCT)
4211 write_pch_user_walking_functions (s, &d);
4214 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
4216 static void
4217 write_local (outf_p output_header, type_p structures, type_p param_structs)
4219 type_p s;
4221 if (!output_header)
4222 return;
4224 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
4225 for (s = structures; s; s = s->next)
4226 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
4228 options_p opt;
4230 if (s->u.s.line.file == NULL)
4231 continue;
4232 for (opt = s->u.s.opt; opt; opt = opt->next)
4233 if (strcmp (opt->name, "ptr_alias") == 0
4234 && opt->kind == OPTION_TYPE)
4236 const_type_p const t = (const_type_p) opt->info.type;
4237 if (t->kind == TYPE_STRUCT
4238 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
4240 oprintf (output_header, "#define gt_pch_p_");
4241 output_mangled_typename (output_header, s);
4242 oprintf (output_header, " gt_pch_p_");
4243 output_mangled_typename (output_header, t);
4244 oprintf (output_header, "\n");
4246 else
4247 error_at_line (&s->u.s.line,
4248 "structure alias is not a structure");
4249 break;
4251 if (opt)
4252 continue;
4254 /* Declare the marker procedure only once. */
4255 oprintf (output_header, "extern void gt_pch_p_");
4256 output_mangled_typename (output_header, s);
4257 oprintf (output_header,
4258 "\n (void *, void *, gt_pointer_operator, void *);\n");
4260 if (s->kind == TYPE_LANG_STRUCT)
4262 type_p ss;
4263 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4264 write_local_func_for_structure (s, ss, NULL);
4266 else
4267 write_local_func_for_structure (s, s, NULL);
4270 for (s = param_structs; s; s = s->next)
4271 if (s->gc_used == GC_POINTED_TO)
4273 type_p *param = s->u.param_struct.param;
4274 type_p stru = s->u.param_struct.stru;
4276 /* Declare the marker procedure. */
4277 oprintf (output_header, "extern void gt_pch_p_");
4278 output_mangled_typename (output_header, s);
4279 oprintf (output_header,
4280 "\n (void *, void *, gt_pointer_operator, void *);\n");
4282 if (stru->u.s.line.file == NULL)
4284 fprintf (stderr, "warning: structure `%s' used but not defined\n",
4285 stru->u.s.tag);
4286 continue;
4289 if (stru->kind == TYPE_LANG_STRUCT)
4291 type_p ss;
4292 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
4293 write_local_func_for_structure (s, ss, param);
4295 else
4296 write_local_func_for_structure (s, stru, param);
4300 /* Nonzero if S is a type for which typed GC allocators should be output. */
4302 #define USED_BY_TYPED_GC_P(s) \
4303 ((s->kind == TYPE_POINTER \
4304 && (s->u.p->gc_used == GC_POINTED_TO \
4305 || s->u.p->gc_used == GC_USED)) \
4306 || (union_or_struct_p (s) \
4307 && ((s)->gc_used == GC_POINTED_TO \
4308 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4309 && s->u.s.line.file != NULL) \
4310 || ((s)->gc_used == GC_USED \
4311 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))) \
4312 || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
4316 /* Might T contain any non-pointer elements? */
4318 static int
4319 contains_scalar_p (type_p t)
4321 switch (t->kind)
4323 case TYPE_STRING:
4324 case TYPE_POINTER:
4325 return 0;
4326 case TYPE_ARRAY:
4327 return contains_scalar_p (t->u.a.p);
4328 case TYPE_USER_STRUCT:
4329 /* User-marked structures will typically contain pointers. */
4330 return 0;
4331 default:
4332 /* Could also check for structures that have no non-pointer
4333 fields, but there aren't enough of those to worry about. */
4334 return 1;
4338 /* Mangle INPF and print it to F. */
4340 static void
4341 put_mangled_filename (outf_p f, const input_file *inpf)
4343 /* The call to get_output_file_name may indirectly update fn since
4344 get_output_file_with_visibility caches its result inside, so we
4345 need the CONST_CAST. */
4346 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4347 if (!f || !name)
4348 return;
4349 for (; *name != 0; name++)
4350 if (ISALNUM (*name))
4351 oprintf (f, "%c", *name);
4352 else
4353 oprintf (f, "%c", '_');
4356 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4357 LASTNAME, and NAME are all strings to insert in various places in
4358 the resulting code. */
4360 static void
4361 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4362 const char *tname, const char *name)
4364 struct flist *fli2;
4366 for (fli2 = flp; fli2; fli2 = fli2->next)
4367 if (fli2->started_p)
4369 oprintf (fli2->f, " %s\n", lastname);
4370 oprintf (fli2->f, "};\n\n");
4373 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4374 if (fli2->started_p)
4376 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4377 int fnum;
4379 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4380 if (bitmap & 1)
4382 oprintf (base_files[fnum],
4383 "extern const struct %s gt_%s_", tname, pfx);
4384 put_mangled_filename (base_files[fnum], fli2->file);
4385 oprintf (base_files[fnum], "[];\n");
4390 size_t fnum;
4391 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4392 oprintf (base_files[fnum],
4393 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4397 for (fli2 = flp; fli2; fli2 = fli2->next)
4398 if (fli2->started_p)
4400 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4401 int fnum;
4403 fli2->started_p = 0;
4405 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4406 if (bitmap & 1)
4408 oprintf (base_files[fnum], " gt_%s_", pfx);
4409 put_mangled_filename (base_files[fnum], fli2->file);
4410 oprintf (base_files[fnum], ",\n");
4415 size_t fnum;
4416 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4418 oprintf (base_files[fnum], " NULL\n");
4419 oprintf (base_files[fnum], "};\n");
4424 /* Write the first three fields (pointer, count and stride) for
4425 root NAME to F. V and LINE are as for write_root.
4427 Return true if the entry could be written; return false on error. */
4429 static bool
4430 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4432 type_p ap;
4434 if (!v)
4436 error_at_line (line, "`%s' is too complex to be a root", name);
4437 return false;
4440 oprintf (f, " {\n");
4441 oprintf (f, " &%s,\n", name);
4442 oprintf (f, " 1");
4444 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4445 if (ap->u.a.len[0])
4446 oprintf (f, " * (%s)", ap->u.a.len);
4447 else if (ap == v->type)
4448 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4449 oprintf (f, ",\n");
4450 oprintf (f, " sizeof (%s", v->name);
4451 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4452 oprintf (f, "[0]");
4453 oprintf (f, "),\n");
4454 return true;
4457 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4458 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4459 of the caller. */
4461 static void
4462 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4463 int has_length, struct fileloc *line, const char *if_marked,
4464 bool emit_pch, type_p field_type, const char *field_name)
4466 struct pair newv;
4467 /* If the field reference is relative to V, rather than to some
4468 subcomponent of V, we can mark any subarrays with a single stride.
4469 We're effectively treating the field as a global variable in its
4470 own right. */
4471 if (v && type == v->type)
4473 newv = *v;
4474 newv.type = field_type;
4475 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4476 v = &newv;
4478 /* Otherwise, any arrays nested in the structure are too complex to
4479 handle. */
4480 else if (field_type->kind == TYPE_ARRAY)
4481 v = NULL;
4482 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4483 has_length, line, if_marked, emit_pch);
4486 /* Write out to F the table entry and any marker routines needed to
4487 mark NAME as TYPE. V can be one of three values:
4489 - null, if NAME is too complex to represent using a single
4490 count and stride. In this case, it is an error for NAME to
4491 contain any gc-ed data.
4493 - the outermost array that contains NAME, if NAME is part of an array.
4495 - the C variable that contains NAME, if NAME is not part of an array.
4497 LINE is the line of the C source that declares the root variable.
4498 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
4499 is nonzero iff we are building the root table for hash table caches. */
4501 static void
4502 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4503 struct fileloc *line, const char *if_marked, bool emit_pch)
4505 switch (type->kind)
4507 case TYPE_STRUCT:
4509 pair_p fld;
4510 for (fld = type->u.s.fields; fld; fld = fld->next)
4512 int skip_p = 0;
4513 const char *desc = NULL;
4514 options_p o;
4516 for (o = fld->opt; o; o = o->next)
4517 if (strcmp (o->name, "skip") == 0)
4518 skip_p = 1;
4519 else if (strcmp (o->name, "desc") == 0
4520 && o->kind == OPTION_STRING)
4521 desc = o->info.string;
4522 else if (strcmp (o->name, "param_is") == 0)
4524 else
4525 error_at_line (line,
4526 "field `%s' of global `%s' has unknown option `%s'",
4527 fld->name, name, o->name);
4529 if (skip_p)
4530 continue;
4531 else if (desc && fld->type->kind == TYPE_UNION)
4533 pair_p validf = NULL;
4534 pair_p ufld;
4536 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4538 const char *tag = NULL;
4539 options_p oo;
4540 for (oo = ufld->opt; oo; oo = oo->next)
4541 if (strcmp (oo->name, "tag") == 0
4542 && oo->kind == OPTION_STRING)
4543 tag = oo->info.string;
4544 if (tag == NULL || strcmp (tag, desc) != 0)
4545 continue;
4546 if (validf != NULL)
4547 error_at_line (line,
4548 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4549 name, fld->name, validf->name,
4550 name, fld->name, ufld->name, tag);
4551 validf = ufld;
4553 if (validf != NULL)
4554 write_field_root (f, v, type, name, 0, line, if_marked,
4555 emit_pch, validf->type,
4556 ACONCAT ((fld->name, ".",
4557 validf->name, NULL)));
4559 else if (desc)
4560 error_at_line (line,
4561 "global `%s.%s' has `desc' option but is not union",
4562 name, fld->name);
4563 else
4564 write_field_root (f, v, type, name, 0, line, if_marked,
4565 emit_pch, fld->type, fld->name);
4568 break;
4570 case TYPE_ARRAY:
4572 char *newname;
4573 newname = xasprintf ("%s[0]", name);
4574 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
4575 emit_pch);
4576 free (newname);
4578 break;
4580 case TYPE_USER_STRUCT:
4581 error_at_line (line, "`%s' must be a pointer type, because it is "
4582 "a GC root and its type is marked with GTY((user))",
4583 v->name);
4584 break;
4586 case TYPE_POINTER:
4588 const_type_p tp;
4590 if (!start_root_entry (f, v, name, line))
4591 return;
4593 tp = type->u.p;
4595 if (!has_length && union_or_struct_p (tp))
4597 tp = get_ultimate_base_class (tp);
4598 const char *id_for_tag = filter_type_name (tp->u.s.tag);
4599 oprintf (f, " &gt_ggc_mx_%s,\n", id_for_tag);
4600 if (emit_pch)
4601 oprintf (f, " &gt_pch_nx_%s", id_for_tag);
4602 else
4603 oprintf (f, " NULL");
4604 if (id_for_tag != tp->u.s.tag)
4605 free (CONST_CAST (char *, id_for_tag));
4607 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
4609 oprintf (f, " &gt_ggc_m_");
4610 output_mangled_typename (f, tp);
4611 if (emit_pch)
4613 oprintf (f, ",\n &gt_pch_n_");
4614 output_mangled_typename (f, tp);
4616 else
4617 oprintf (f, ",\n NULL");
4619 else if (has_length
4620 && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
4622 oprintf (f, " &gt_ggc_ma_%s,\n", name);
4623 if (emit_pch)
4624 oprintf (f, " &gt_pch_na_%s", name);
4625 else
4626 oprintf (f, " NULL");
4628 else
4630 error_at_line (line,
4631 "global `%s' is pointer to unimplemented type",
4632 name);
4634 if (if_marked)
4635 oprintf (f, ",\n &%s", if_marked);
4636 oprintf (f, "\n },\n");
4638 break;
4640 case TYPE_STRING:
4642 if (!start_root_entry (f, v, name, line))
4643 return;
4645 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
4646 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
4647 oprintf (f, " },\n");
4649 break;
4651 case TYPE_SCALAR:
4652 break;
4654 case TYPE_NONE:
4655 case TYPE_UNDEFINED:
4656 case TYPE_UNION:
4657 case TYPE_LANG_STRUCT:
4658 case TYPE_PARAM_STRUCT:
4659 error_at_line (line, "global `%s' is unimplemented type", name);
4663 /* This generates a routine to walk an array. */
4665 static void
4666 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4668 struct walk_type_data d;
4669 char *prevval3;
4671 memset (&d, 0, sizeof (d));
4672 d.of = f;
4673 d.cookie = wtd;
4674 d.indent = 2;
4675 d.line = &v->line;
4676 d.opt = v->opt;
4677 d.bitmap = get_lang_bitmap (v->line.file);
4678 d.param = NULL;
4680 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4682 if (wtd->param_prefix)
4684 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4685 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
4686 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4687 wtd->param_prefix, v->name);
4688 oprintf (d.of,
4689 " ATTRIBUTE_UNUSED void *x_p,\n"
4690 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4691 " ATTRIBUTE_UNUSED void * cookie)\n");
4692 oprintf (d.of, "{\n");
4693 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4694 d.process_field = write_types_local_process_field;
4695 d.have_this_obj = true;
4696 walk_type (v->type, &d);
4697 oprintf (f, "}\n\n");
4700 d.opt = v->opt;
4701 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4702 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4703 wtd->prefix, v->name);
4704 oprintf (f, "{\n");
4705 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4706 d.process_field = write_types_process_field;
4707 d.have_this_obj = false;
4708 walk_type (v->type, &d);
4709 free (prevval3);
4710 oprintf (f, "}\n\n");
4713 /* Output a table describing the locations and types of VARIABLES. */
4715 static void
4716 write_roots (pair_p variables, bool emit_pch)
4718 pair_p v;
4719 struct flist *flp = NULL;
4721 for (v = variables; v; v = v->next)
4723 outf_p f =
4724 get_output_file_with_visibility (CONST_CAST (input_file*,
4725 v->line.file));
4726 struct flist *fli;
4727 const char *length = NULL;
4728 int deletable_p = 0;
4729 options_p o;
4730 for (o = v->opt; o; o = o->next)
4731 if (strcmp (o->name, "length") == 0
4732 && o->kind == OPTION_STRING)
4733 length = o->info.string;
4734 else if (strcmp (o->name, "deletable") == 0)
4735 deletable_p = 1;
4736 else if (strcmp (o->name, "param_is") == 0)
4738 else if (strncmp (o->name, "param", 5) == 0
4739 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
4741 else if (strcmp (o->name, "if_marked") == 0)
4743 else
4744 error_at_line (&v->line,
4745 "global `%s' has unknown option `%s'",
4746 v->name, o->name);
4748 for (fli = flp; fli; fli = fli->next)
4749 if (fli->f == f && f)
4750 break;
4751 if (fli == NULL)
4753 fli = XNEW (struct flist);
4754 fli->f = f;
4755 fli->next = flp;
4756 fli->started_p = 0;
4757 fli->file = v->line.file;
4758 gcc_assert (fli->file);
4759 flp = fli;
4761 oprintf (f, "\n/* GC roots. */\n\n");
4764 if (!deletable_p
4765 && length
4766 && v->type->kind == TYPE_POINTER
4767 && (v->type->u.p->kind == TYPE_POINTER
4768 || v->type->u.p->kind == TYPE_STRUCT))
4770 write_array (f, v, &ggc_wtd);
4771 write_array (f, v, &pch_wtd);
4775 for (v = variables; v; v = v->next)
4777 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4778 v->line.file));
4779 struct flist *fli;
4780 int skip_p = 0;
4781 int length_p = 0;
4782 options_p o;
4784 for (o = v->opt; o; o = o->next)
4785 if (strcmp (o->name, "length") == 0)
4786 length_p = 1;
4787 else if (strcmp (o->name, "deletable") == 0
4788 || strcmp (o->name, "if_marked") == 0)
4789 skip_p = 1;
4791 if (skip_p)
4792 continue;
4794 for (fli = flp; fli; fli = fli->next)
4795 if (fli->f == f)
4796 break;
4797 if (!fli->started_p)
4799 fli->started_p = 1;
4801 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4802 put_mangled_filename (f, v->line.file);
4803 oprintf (f, "[] = {\n");
4806 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4809 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4810 "gt_ggc_rtab");
4812 for (v = variables; v; v = v->next)
4814 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4815 v->line.file));
4816 struct flist *fli;
4817 int skip_p = 1;
4818 options_p o;
4820 for (o = v->opt; o; o = o->next)
4821 if (strcmp (o->name, "deletable") == 0)
4822 skip_p = 0;
4823 else if (strcmp (o->name, "if_marked") == 0)
4824 skip_p = 1;
4826 if (skip_p)
4827 continue;
4829 for (fli = flp; fli; fli = fli->next)
4830 if (fli->f == f)
4831 break;
4832 if (!fli->started_p)
4834 fli->started_p = 1;
4836 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4837 put_mangled_filename (f, v->line.file);
4838 oprintf (f, "[] = {\n");
4841 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4842 v->name, v->name);
4845 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4846 "gt_ggc_deletable_rtab");
4848 for (v = variables; v; v = v->next)
4850 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4851 v->line.file));
4852 struct flist *fli;
4853 const char *if_marked = NULL;
4854 int length_p = 0;
4855 options_p o;
4857 for (o = v->opt; o; o = o->next)
4858 if (strcmp (o->name, "length") == 0)
4859 length_p = 1;
4860 else if (strcmp (o->name, "if_marked") == 0
4861 && o->kind == OPTION_STRING)
4862 if_marked = o->info.string;
4863 if (if_marked == NULL)
4864 continue;
4865 if (v->type->kind != TYPE_POINTER
4866 || v->type->u.p->kind != TYPE_PARAM_STRUCT
4867 || v->type->u.p->u.param_struct.stru != find_structure ("htab",
4868 TYPE_STRUCT))
4870 error_at_line (&v->line,
4871 "if_marked option used but not hash table");
4872 continue;
4875 for (fli = flp; fli; fli = fli->next)
4876 if (fli->f == f)
4877 break;
4878 if (!fli->started_p)
4880 fli->started_p = 1;
4882 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4883 put_mangled_filename (f, v->line.file);
4884 oprintf (f, "[] = {\n");
4887 write_root (f, v, v->type->u.p->u.param_struct.param[0],
4888 v->name, length_p, &v->line, if_marked, emit_pch);
4891 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4892 "gt_ggc_cache_rtab");
4894 if (!emit_pch)
4895 return;
4897 for (v = variables; v; v = v->next)
4899 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4900 v->line.file));
4901 struct flist *fli;
4902 int length_p = 0;
4903 int if_marked_p = 0;
4904 options_p o;
4906 for (o = v->opt; o; o = o->next)
4907 if (strcmp (o->name, "length") == 0)
4908 length_p = 1;
4909 else if (strcmp (o->name, "if_marked") == 0)
4910 if_marked_p = 1;
4912 if (!if_marked_p)
4913 continue;
4915 for (fli = flp; fli; fli = fli->next)
4916 if (fli->f == f)
4917 break;
4918 if (!fli->started_p)
4920 fli->started_p = 1;
4922 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4923 put_mangled_filename (f, v->line.file);
4924 oprintf (f, "[] = {\n");
4927 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4930 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4931 "gt_pch_cache_rtab");
4933 for (v = variables; v; v = v->next)
4935 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4936 v->line.file));
4937 struct flist *fli;
4938 int skip_p = 0;
4939 options_p o;
4941 for (o = v->opt; o; o = o->next)
4942 if (strcmp (o->name, "deletable") == 0
4943 || strcmp (o->name, "if_marked") == 0)
4945 skip_p = 1;
4946 break;
4949 if (skip_p)
4950 continue;
4952 if (!contains_scalar_p (v->type))
4953 continue;
4955 for (fli = flp; fli; fli = fli->next)
4956 if (fli->f == f)
4957 break;
4958 if (!fli->started_p)
4960 fli->started_p = 1;
4962 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4963 put_mangled_filename (f, v->line.file);
4964 oprintf (f, "[] = {\n");
4967 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4968 v->name, v->name);
4971 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4972 "gt_pch_scalar_rtab");
4975 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4976 guaranteee for somewhat increased readability. If name conflicts do happen,
4977 this funcion will have to be adjusted to be more like
4978 output_mangled_typename. */
4980 static void
4981 output_typename (outf_p of, const_type_p t)
4983 switch (t->kind)
4985 case TYPE_STRING:
4986 oprintf (of, "str");
4987 break;
4988 case TYPE_SCALAR:
4989 oprintf (of, "scalar");
4990 break;
4991 case TYPE_POINTER:
4992 output_typename (of, t->u.p);
4993 break;
4994 case TYPE_STRUCT:
4995 case TYPE_USER_STRUCT:
4996 case TYPE_UNION:
4997 case TYPE_LANG_STRUCT:
4998 oprintf (of, "%s", t->u.s.tag);
4999 break;
5000 case TYPE_PARAM_STRUCT:
5002 int i;
5003 for (i = 0; i < NUM_PARAM; i++)
5004 if (t->u.param_struct.param[i] != NULL)
5006 output_typename (of, t->u.param_struct.param[i]);
5007 oprintf (of, "_");
5009 output_typename (of, t->u.param_struct.stru);
5010 break;
5012 case TYPE_NONE:
5013 case TYPE_UNDEFINED:
5014 case TYPE_ARRAY:
5015 gcc_unreachable ();
5019 /* Writes a typed GC allocator for type S that is suitable as a callback for
5020 the splay tree implementation in libiberty. */
5022 static void
5023 write_splay_tree_allocator_def (const_type_p s)
5025 outf_p of = get_output_file_with_visibility (NULL);
5026 oprintf (of, "void * ggc_alloc_splay_tree_");
5027 output_typename (of, s);
5028 oprintf (of, " (int sz, void * nl)\n");
5029 oprintf (of, "{\n");
5030 oprintf (of, " return ggc_splay_alloc (sz, nl);\n");
5031 oprintf (of, "}\n\n");
5034 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
5035 for the splay tree implementation in libiberty. */
5037 static void
5038 write_splay_tree_allocators (const_type_p param_structs)
5040 const_type_p s;
5042 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
5043 for (s = param_structs; s; s = s->next)
5044 if (s->gc_used == GC_POINTED_TO)
5046 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
5047 output_typename (header_file, s);
5048 oprintf (header_file, " (int, void *);\n");
5049 write_splay_tree_allocator_def (s);
5053 #define INDENT 2
5055 /* Dumps the value of typekind KIND. */
5057 static void
5058 dump_typekind (int indent, enum typekind kind)
5060 printf ("%*ckind = ", indent, ' ');
5061 switch (kind)
5063 case TYPE_SCALAR:
5064 printf ("TYPE_SCALAR");
5065 break;
5066 case TYPE_STRING:
5067 printf ("TYPE_STRING");
5068 break;
5069 case TYPE_STRUCT:
5070 printf ("TYPE_STRUCT");
5071 break;
5072 case TYPE_UNDEFINED:
5073 printf ("TYPE_UNDEFINED");
5074 break;
5075 case TYPE_USER_STRUCT:
5076 printf ("TYPE_USER_STRUCT");
5077 break;
5078 case TYPE_UNION:
5079 printf ("TYPE_UNION");
5080 break;
5081 case TYPE_POINTER:
5082 printf ("TYPE_POINTER");
5083 break;
5084 case TYPE_ARRAY:
5085 printf ("TYPE_ARRAY");
5086 break;
5087 case TYPE_LANG_STRUCT:
5088 printf ("TYPE_LANG_STRUCT");
5089 break;
5090 case TYPE_PARAM_STRUCT:
5091 printf ("TYPE_PARAM_STRUCT");
5092 break;
5093 default:
5094 gcc_unreachable ();
5096 printf ("\n");
5099 /* Dumps the value of GC_USED flag. */
5101 static void
5102 dump_gc_used (int indent, enum gc_used_enum gc_used)
5104 printf ("%*cgc_used = ", indent, ' ');
5105 switch (gc_used)
5107 case GC_UNUSED:
5108 printf ("GC_UNUSED");
5109 break;
5110 case GC_USED:
5111 printf ("GC_USED");
5112 break;
5113 case GC_MAYBE_POINTED_TO:
5114 printf ("GC_MAYBE_POINTED_TO");
5115 break;
5116 case GC_POINTED_TO:
5117 printf ("GC_POINTED_TO");
5118 break;
5119 default:
5120 gcc_unreachable ();
5122 printf ("\n");
5125 /* Dumps the type options OPT. */
5127 static void
5128 dump_options (int indent, options_p opt)
5130 options_p o;
5131 printf ("%*coptions = ", indent, ' ');
5132 o = opt;
5133 while (o)
5135 switch (o->kind)
5137 case OPTION_STRING:
5138 printf ("%s:string %s ", o->name, o->info.string);
5139 break;
5140 case OPTION_TYPE:
5141 printf ("%s:type ", o->name);
5142 dump_type (indent+1, o->info.type);
5143 break;
5144 case OPTION_NESTED:
5145 printf ("%s:nested ", o->name);
5146 break;
5147 case OPTION_NONE:
5148 gcc_unreachable ();
5150 o = o->next;
5152 printf ("\n");
5155 /* Dumps the source file location in LINE. */
5157 static void
5158 dump_fileloc (int indent, struct fileloc line)
5160 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
5161 get_input_file_name (line.file),
5162 line.line);
5165 /* Recursively dumps the struct, union, or a language-specific
5166 struct T. */
5168 static void
5169 dump_type_u_s (int indent, type_p t)
5171 pair_p fields;
5173 gcc_assert (union_or_struct_p (t));
5174 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
5175 dump_fileloc (indent, t->u.s.line);
5176 printf ("%*cu.s.fields =\n", indent, ' ');
5177 fields = t->u.s.fields;
5178 while (fields)
5180 dump_pair (indent + INDENT, fields);
5181 fields = fields->next;
5183 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
5184 dump_options (indent, t->u.s.opt);
5185 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
5186 if (t->kind == TYPE_LANG_STRUCT)
5188 printf ("%*cu.s.lang_struct:\n", indent, ' ');
5189 dump_type_list (indent + INDENT, t->u.s.lang_struct);
5193 /* Recursively dumps the array T. */
5195 static void
5196 dump_type_u_a (int indent, type_p t)
5198 gcc_assert (t->kind == TYPE_ARRAY);
5199 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
5200 dump_type_list (indent + INDENT, t->u.a.p);
5203 /* Recursively dumps the parameterized struct T. */
5205 static void
5206 dump_type_u_param_struct (int indent, type_p t)
5208 int i;
5209 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
5210 printf ("%*cu.param_struct.stru:\n", indent, ' ');
5211 dump_type_list (indent, t->u.param_struct.stru);
5212 dump_fileloc (indent, t->u.param_struct.line);
5213 for (i = 0; i < NUM_PARAM; i++)
5215 if (t->u.param_struct.param[i] == NULL)
5216 continue;
5217 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
5218 dump_type (indent + INDENT, t->u.param_struct.param[i]);
5222 /* Recursively dumps the type list T. */
5224 static void
5225 dump_type_list (int indent, type_p t)
5227 type_p p = t;
5228 while (p)
5230 dump_type (indent, p);
5231 p = p->next;
5235 static htab_t seen_types;
5237 /* Recursively dumps the type T if it was not dumped previously. */
5239 static void
5240 dump_type (int indent, type_p t)
5242 PTR *slot;
5244 if (seen_types == NULL)
5245 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
5247 printf ("%*cType at %p: ", indent, ' ', (void *) t);
5248 slot = htab_find_slot (seen_types, t, INSERT);
5249 if (*slot != NULL)
5251 printf ("already seen.\n");
5252 return;
5254 *slot = t;
5255 printf ("\n");
5257 dump_typekind (indent, t->kind);
5258 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
5259 (void *) t->pointer_to);
5260 dump_gc_used (indent + INDENT, t->gc_used);
5261 switch (t->kind)
5263 case TYPE_SCALAR:
5264 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
5265 t->u.scalar_is_char ? "true" : "false");
5266 break;
5267 case TYPE_STRING:
5268 break;
5269 case TYPE_STRUCT:
5270 case TYPE_UNION:
5271 case TYPE_LANG_STRUCT:
5272 case TYPE_USER_STRUCT:
5273 dump_type_u_s (indent + INDENT, t);
5274 break;
5275 case TYPE_POINTER:
5276 printf ("%*cp:\n", indent + INDENT, ' ');
5277 dump_type (indent + INDENT, t->u.p);
5278 break;
5279 case TYPE_ARRAY:
5280 dump_type_u_a (indent + INDENT, t);
5281 break;
5282 case TYPE_PARAM_STRUCT:
5283 dump_type_u_param_struct (indent + INDENT, t);
5284 break;
5285 default:
5286 gcc_unreachable ();
5288 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
5291 /* Dumps the pair P. */
5293 static void
5294 dump_pair (int indent, pair_p p)
5296 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
5297 dump_type (indent, p->type);
5298 dump_fileloc (indent, p->line);
5299 dump_options (indent, p->opt);
5300 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
5303 /* Dumps the list of pairs PP. */
5305 static void
5306 dump_pair_list (const char *name, pair_p pp)
5308 pair_p p;
5309 printf ("%s:\n", name);
5310 for (p = pp; p != NULL; p = p->next)
5311 dump_pair (0, p);
5312 printf ("End of %s\n\n", name);
5315 /* Dumps the STRUCTURES. */
5317 static void
5318 dump_structures (const char *name, type_p structures)
5320 printf ("%s:\n", name);
5321 dump_type_list (0, structures);
5322 printf ("End of %s\n\n", name);
5325 /* Dumps the internal structures of gengtype. This is useful to debug
5326 gengtype itself, or to understand what it does, e.g. for plugin
5327 developers. */
5329 static void
5330 dump_everything (void)
5332 dump_pair_list ("typedefs", typedefs);
5333 dump_structures ("structures", structures);
5334 dump_structures ("param_structs", param_structs);
5335 dump_pair_list ("variables", variables);
5337 /* Allocated with the first call to dump_type. */
5338 htab_delete (seen_types);
5343 /* Option specification for getopt_long. */
5344 static const struct option gengtype_long_options[] = {
5345 {"help", no_argument, NULL, 'h'},
5346 {"version", no_argument, NULL, 'V'},
5347 {"verbose", no_argument, NULL, 'v'},
5348 {"dump", no_argument, NULL, 'd'},
5349 {"debug", no_argument, NULL, 'D'},
5350 {"plugin", required_argument, NULL, 'P'},
5351 {"srcdir", required_argument, NULL, 'S'},
5352 {"backupdir", required_argument, NULL, 'B'},
5353 {"inputs", required_argument, NULL, 'I'},
5354 {"read-state", required_argument, NULL, 'r'},
5355 {"write-state", required_argument, NULL, 'w'},
5356 /* Terminating NULL placeholder. */
5357 {NULL, no_argument, NULL, 0},
5361 static void
5362 print_usage (void)
5364 printf ("Usage: %s\n", progname);
5365 printf ("\t -h | --help " " \t# Give this help.\n");
5366 printf ("\t -D | --debug "
5367 " \t# Give debug output to debug %s itself.\n", progname);
5368 printf ("\t -V | --version " " \t# Give version information.\n");
5369 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5370 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5371 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5372 " \t# Generate for plugin.\n");
5373 printf ("\t -S | --srcdir <GCC-directory> "
5374 " \t# Specify the GCC source directory.\n");
5375 printf ("\t -B | --backupdir <directory> "
5376 " \t# Specify the backup directory for updated files.\n");
5377 printf ("\t -I | --inputs <input-list> "
5378 " \t# Specify the file with source files list.\n");
5379 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5380 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5383 static void
5384 print_version (void)
5386 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5387 printf ("Report bugs: %s\n", bug_report_url);
5390 /* Parse the program options using getopt_long... */
5391 static void
5392 parse_program_options (int argc, char **argv)
5394 int opt = -1;
5395 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5396 gengtype_long_options, NULL)) >= 0)
5398 switch (opt)
5400 case 'h': /* --help */
5401 print_usage ();
5402 break;
5403 case 'V': /* --version */
5404 print_version ();
5405 break;
5406 case 'd': /* --dump */
5407 do_dump = 1;
5408 break;
5409 case 'D': /* --debug */
5410 do_debug = 1;
5411 break;
5412 case 'v': /* --verbose */
5413 verbosity_level++;
5414 break;
5415 case 'P': /* --plugin */
5416 if (optarg)
5417 plugin_output_filename = optarg;
5418 else
5419 fatal ("missing plugin output file name");
5420 break;
5421 case 'S': /* --srcdir */
5422 if (optarg)
5423 srcdir = optarg;
5424 else
5425 fatal ("missing source directory");
5426 srcdir_len = strlen (srcdir);
5427 break;
5428 case 'B': /* --backupdir */
5429 if (optarg)
5430 backup_dir = optarg;
5431 else
5432 fatal ("missing backup directory");
5433 break;
5434 case 'I': /* --inputs */
5435 if (optarg)
5436 inputlist = optarg;
5437 else
5438 fatal ("missing input list");
5439 break;
5440 case 'r': /* --read-state */
5441 if (optarg)
5442 read_state_filename = optarg;
5443 else
5444 fatal ("missing read state file");
5445 DBGPRINTF ("read state %s\n", optarg);
5446 break;
5447 case 'w': /* --write-state */
5448 DBGPRINTF ("write state %s\n", optarg);
5449 if (optarg)
5450 write_state_filename = optarg;
5451 else
5452 fatal ("missing write state file");
5453 break;
5454 default:
5455 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5456 print_usage ();
5457 fatal ("unexpected flag");
5460 if (plugin_output_filename)
5462 /* In plugin mode we require some input files. */
5463 int i = 0;
5464 if (optind >= argc)
5465 fatal ("no source files given in plugin mode");
5466 nb_plugin_files = argc - optind;
5467 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5468 for (i = 0; i < (int) nb_plugin_files; i++)
5470 char *name = argv[i + optind];
5471 plugin_files[i] = input_file_by_name (name);
5478 /******* Manage input files. ******/
5480 /* Hash table of unique input file names. */
5481 static htab_t input_file_htab;
5483 /* Find or allocate a new input_file by hash-consing it. */
5484 input_file*
5485 input_file_by_name (const char* name)
5487 PTR* slot;
5488 input_file* f = NULL;
5489 int namlen = 0;
5490 if (!name)
5491 return NULL;
5492 namlen = strlen (name);
5493 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5494 f->inpbitmap = 0;
5495 f->inpoutf = NULL;
5496 f->inpisplugin = false;
5497 strcpy (f->inpname, name);
5498 slot = htab_find_slot (input_file_htab, f, INSERT);
5499 gcc_assert (slot != NULL);
5500 if (*slot)
5502 /* Already known input file. */
5503 free (f);
5504 return (input_file*)(*slot);
5506 /* New input file. */
5507 *slot = f;
5508 return f;
5511 /* Hash table support routines for input_file-s. */
5512 static hashval_t
5513 htab_hash_inputfile (const void *p)
5515 const input_file *inpf = (const input_file *) p;
5516 gcc_assert (inpf);
5517 return htab_hash_string (get_input_file_name (inpf));
5520 static int
5521 htab_eq_inputfile (const void *x, const void *y)
5523 const input_file *inpfx = (const input_file *) x;
5524 const input_file *inpfy = (const input_file *) y;
5525 gcc_assert (inpfx != NULL && inpfy != NULL);
5526 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5531 main (int argc, char **argv)
5533 size_t i;
5534 static struct fileloc pos = { NULL, 0 };
5535 outf_p output_header;
5537 /* Mandatory common initializations. */
5538 progname = "gengtype"; /* For fatal and messages. */
5539 /* Create the hash-table used to hash-cons input files. */
5540 input_file_htab =
5541 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5542 /* Initialize our special input files. */
5543 this_file = input_file_by_name (__FILE__);
5544 system_h_file = input_file_by_name ("system.h");
5545 /* Set the scalar_is_char union number for predefined scalar types. */
5546 scalar_nonchar.u.scalar_is_char = FALSE;
5547 scalar_char.u.scalar_is_char = TRUE;
5549 parse_program_options (argc, argv);
5551 #if ENABLE_CHECKING
5552 if (do_debug)
5554 time_t now = (time_t) 0;
5555 time (&now);
5556 DBGPRINTF ("gengtype started pid %d at %s",
5557 (int) getpid (), ctime (&now));
5559 #endif /* ENABLE_CHECKING */
5561 /* Parse the input list and the input files. */
5562 DBGPRINTF ("inputlist %s", inputlist);
5563 if (read_state_filename)
5565 if (inputlist)
5566 fatal ("input list %s cannot be given with a read state file %s",
5567 inputlist, read_state_filename);
5568 read_state (read_state_filename);
5569 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5570 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs);
5572 else if (inputlist)
5574 /* These types are set up with #define or else outside of where
5575 we can see them. We should initialize them before calling
5576 read_input_list. */
5577 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5578 Call;} while (0)
5579 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5580 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5581 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5582 POS_HERE (do_scalar_typedef ("double_int", &pos));
5583 POS_HERE (do_scalar_typedef ("offset_int", &pos));
5584 POS_HERE (do_scalar_typedef ("widest_int", &pos));
5585 POS_HERE (do_scalar_typedef ("int64_t", &pos));
5586 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5587 POS_HERE (do_scalar_typedef ("uint8", &pos));
5588 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5589 POS_HERE (do_scalar_typedef ("jword", &pos));
5590 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5591 POS_HERE (do_scalar_typedef ("void", &pos));
5592 POS_HERE (do_typedef ("PTR",
5593 create_pointer (resolve_typedef ("void", &pos)),
5594 &pos));
5595 #undef POS_HERE
5596 read_input_list (inputlist);
5597 for (i = 0; i < num_gt_files; i++)
5599 parse_file (get_input_file_name (gt_files[i]));
5600 DBGPRINTF ("parsed file #%d %s",
5601 (int) i, get_input_file_name (gt_files[i]));
5603 if (verbosity_level >= 1)
5604 printf ("%s parsed %d files with %d GTY types\n",
5605 progname, (int) num_gt_files, type_count);
5607 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5608 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
5611 else
5612 fatal ("either an input list or a read state file should be given");
5613 if (hit_error)
5614 return 1;
5617 if (plugin_output_filename)
5619 size_t ix = 0;
5620 /* In plugin mode, we should have read a state file, and have
5621 given at least one plugin file. */
5622 if (!read_state_filename)
5623 fatal ("No read state given in plugin mode for %s",
5624 plugin_output_filename);
5626 if (nb_plugin_files == 0 || !plugin_files)
5627 fatal ("No plugin files given in plugin mode for %s",
5628 plugin_output_filename);
5630 /* Parse our plugin files and augment the state. */
5631 for (ix = 0; ix < nb_plugin_files; ix++)
5633 input_file* pluginput = plugin_files [ix];
5634 pluginput->inpisplugin = true;
5635 parse_file (get_input_file_name (pluginput));
5637 if (hit_error)
5638 return 1;
5640 plugin_output = create_file ("GCC", plugin_output_filename);
5641 DBGPRINTF ("created plugin_output %p named %s",
5642 (void *) plugin_output, plugin_output->name);
5644 else
5645 { /* No plugin files, we are in normal mode. */
5646 if (!srcdir)
5647 fatal ("gengtype needs a source directory in normal mode");
5649 if (hit_error)
5650 return 1;
5652 gen_rtx_next ();
5654 /* The call to set_gc_used may indirectly call find_param_structure
5655 hence enlarge the param_structs list of types. */
5656 set_gc_used (variables);
5658 /* The state at this point is read from the state input file or by
5659 parsing source files and optionally augmented by parsing plugin
5660 source files. Write it now. */
5661 if (write_state_filename)
5663 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5664 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs);
5666 if (hit_error)
5667 fatal ("didn't write state file %s after errors",
5668 write_state_filename);
5670 DBGPRINTF ("before write_state %s", write_state_filename);
5671 write_state (write_state_filename);
5673 if (do_dump)
5674 dump_everything ();
5676 /* After having written the state file we return immediately to
5677 avoid generating any output file. */
5678 if (hit_error)
5679 return 1;
5680 else
5681 return 0;
5685 open_base_files ();
5687 output_header = plugin_output ? plugin_output : header_file;
5688 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5689 structures);
5690 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5691 param_structs);
5693 write_types (output_header, structures, param_structs, &ggc_wtd);
5694 if (plugin_files == NULL)
5696 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5697 structures);
5698 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5699 param_structs);
5700 write_types (header_file, structures, param_structs, &pch_wtd);
5701 write_local (header_file, structures, param_structs);
5703 write_splay_tree_allocators (param_structs);
5704 write_roots (variables, plugin_files == NULL);
5705 write_rtx_next ();
5706 close_output_files ();
5708 if (do_dump)
5709 dump_everything ();
5711 /* Don't bother about free-ing any input or plugin file, etc. */
5713 if (hit_error)
5714 return 1;
5715 return 0;