* gcc.dg/c11-complex-1.c: Use dg-add-options ieee.
[official-gcc.git] / gcc / gengtype.c
blobecf1ef00385a7638b0050acff8e8144ca630970b
1 /* Process source files and output type information.
2 Copyright (C) 2002-2013 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 "double-int.h"
29 #include "version.h" /* for version_string & pkgversion_string. */
30 #include "hashtab.h"
31 #include "xregex.h"
32 #include "obstack.h"
33 #include "gengtype.h"
34 #include "filenames.h"
36 /* Data types, macros, etc. used only in this file. */
39 /* The list of output files. */
40 outf_p output_files;
42 /* The output header file that is included into pretty much every
43 source file. */
44 outf_p header_file;
47 /* The name of the file containing the list of input files. */
48 static char *inputlist;
50 /* The plugin input files and their number; in that case only
51 a single file is produced. */
52 static input_file **plugin_files;
53 static size_t nb_plugin_files;
55 /* The generated plugin output file and name. */
56 static outf_p plugin_output;
57 static char *plugin_output_filename;
59 /* Our source directory and its length. */
60 const char *srcdir;
61 size_t srcdir_len;
63 /* Variables used for reading and writing the state. */
64 const char *read_state_filename;
65 const char *write_state_filename;
67 /* Variables to help debugging. */
68 int do_dump;
69 int do_debug;
71 /* Level for verbose messages. */
72 int verbosity_level;
74 /* We have a type count and use it to set the state_number of newly
75 allocated types to some unique negative number. */
76 static int type_count;
78 /* The backup directory should be in the same file system as the
79 generated files, otherwise the rename(2) system call would fail.
80 If NULL, no backup is made when overwriting a generated file. */
81 static const char* backup_dir; /* (-B) program option. */
84 static outf_p create_file (const char *, const char *);
86 static const char *get_file_basename (const input_file *);
87 static const char *get_file_realbasename (const input_file *);
89 static int get_prefix_langdir_index (const char *);
90 static const char *get_file_langdir (const input_file *);
92 static void dump_pair (int indent, pair_p p);
93 static void dump_type (int indent, type_p p);
94 static void dump_type_list (int indent, type_p p);
97 /* Nonzero iff an error has occurred. */
98 bool hit_error = false;
100 static void gen_rtx_next (void);
101 static void write_rtx_next (void);
102 static void open_base_files (void);
103 static void close_output_files (void);
105 /* Report an error at POS, printing MSG. */
107 void
108 error_at_line (const struct fileloc *pos, const char *msg, ...)
110 va_list ap;
112 gcc_assert (pos != NULL && pos->file != NULL);
113 va_start (ap, msg);
115 fprintf (stderr, "%s:%d: ", get_input_file_name (pos->file), pos->line);
116 vfprintf (stderr, msg, ap);
117 fputc ('\n', stderr);
118 hit_error = true;
120 va_end (ap);
123 /* asprintf, but produces fatal message on out-of-memory. */
124 char *
125 xasprintf (const char *format, ...)
127 int n;
128 char *result;
129 va_list ap;
131 va_start (ap, format);
132 n = vasprintf (&result, format, ap);
133 if (result == NULL || n < 0)
134 fatal ("out of memory");
135 va_end (ap);
137 return result;
140 /* Locate the ultimate base class of struct S. */
142 static const_type_p
143 get_ultimate_base_class (const_type_p s)
145 while (s->u.s.base_class)
146 s = s->u.s.base_class;
147 return s;
150 /* Input file handling. */
152 /* Table of all input files. */
153 const input_file **gt_files;
154 size_t num_gt_files;
156 /* A number of places use the name of this "gengtype.c" file for a
157 location for things that we can't rely on the source to define.
158 Make sure we can still use pointer comparison on filenames. */
159 input_file* this_file;
160 /* The "system.h" file is likewise specially useful. */
161 input_file* system_h_file;
163 /* Vector of per-language directories. */
164 const char **lang_dir_names;
165 size_t num_lang_dirs;
167 /* An array of output files suitable for definitions. There is one
168 BASE_FILES entry for each language. */
169 static outf_p *base_files;
173 #if ENABLE_CHECKING
174 /* Utility debugging function, printing the various type counts within
175 a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
176 void
177 dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
179 int nb_types = 0, nb_scalar = 0, nb_string = 0;
180 int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
181 int nb_lang_struct = 0, nb_param_struct = 0;
182 int nb_user_struct = 0, nb_undefined = 0;
183 type_p p = NULL;
184 for (p = t; p; p = p->next)
186 nb_types++;
187 switch (p->kind)
189 case TYPE_UNDEFINED:
190 nb_undefined++;
191 case TYPE_SCALAR:
192 nb_scalar++;
193 break;
194 case TYPE_STRING:
195 nb_string++;
196 break;
197 case TYPE_STRUCT:
198 nb_struct++;
199 break;
200 case TYPE_USER_STRUCT:
201 nb_user_struct++;
202 break;
203 case TYPE_UNION:
204 nb_union++;
205 break;
206 case TYPE_POINTER:
207 nb_pointer++;
208 break;
209 case TYPE_ARRAY:
210 nb_array++;
211 break;
212 case TYPE_LANG_STRUCT:
213 nb_lang_struct++;
214 break;
215 case TYPE_PARAM_STRUCT:
216 nb_param_struct++;
217 break;
218 case TYPE_NONE:
219 gcc_unreachable ();
222 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
223 lbasename (fil), lin, msg, nb_types);
224 if (nb_scalar > 0 || nb_string > 0)
225 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
226 if (nb_struct > 0 || nb_union > 0)
227 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
228 if (nb_pointer > 0 || nb_array > 0)
229 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
230 if (nb_lang_struct > 0 || nb_param_struct > 0)
231 fprintf (stderr, "@@%%@@ %d lang_structs, %d param_structs\n",
232 nb_lang_struct, nb_param_struct);
233 if (nb_user_struct > 0)
234 fprintf (stderr, "@@%%@@ %d user_structs\n", nb_user_struct);
235 if (nb_undefined > 0)
236 fprintf (stderr, "@@%%@@ %d undefined types\n", nb_undefined);
237 fprintf (stderr, "\n");
239 #endif /* ENABLE_CHECKING */
241 /* Scan the input file, LIST, and determine how much space we need to
242 store strings in. Also, count the number of language directories
243 and files. The numbers returned are overestimates as they does not
244 consider repeated files. */
245 static size_t
246 measure_input_list (FILE *list)
248 size_t n = 0;
249 int c;
250 bool atbol = true;
251 num_lang_dirs = 0;
252 num_gt_files = plugin_files ? nb_plugin_files : 0;
253 while ((c = getc (list)) != EOF)
255 n++;
256 if (atbol)
258 if (c == '[')
259 num_lang_dirs++;
260 else
262 /* Add space for a lang_bitmap before the input file name. */
263 n += sizeof (lang_bitmap);
264 num_gt_files++;
266 atbol = false;
269 if (c == '\n')
270 atbol = true;
273 rewind (list);
274 return n;
277 /* Read one input line from LIST to HEREP (which is updated). A
278 pointer to the string is returned via LINEP. If it was a language
279 subdirectory in square brackets, strip off the square brackets and
280 return true. Otherwise, leave space before the string for a
281 lang_bitmap, and return false. At EOF, returns false, does not
282 touch *HEREP, and sets *LINEP to NULL. POS is used for
283 diagnostics. */
284 static bool
285 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
287 char *here = *herep;
288 char *line;
289 int c = getc (list);
291 /* Read over whitespace. */
292 while (c == '\n' || c == ' ')
293 c = getc (list);
295 if (c == EOF)
297 *linep = 0;
298 return false;
300 else if (c == '[')
302 /* No space for a lang_bitmap is necessary. Discard the '['. */
303 c = getc (list);
304 line = here;
305 while (c != ']' && c != '\n' && c != EOF)
307 *here++ = c;
308 c = getc (list);
310 *here++ = '\0';
312 if (c == ']')
314 c = getc (list); /* eat what should be a newline */
315 if (c != '\n' && c != EOF)
316 error_at_line (pos, "junk on line after language tag [%s]", line);
318 else
319 error_at_line (pos, "missing close bracket for language tag [%s",
320 line);
322 *herep = here;
323 *linep = line;
324 return true;
326 else
328 /* Leave space for a lang_bitmap. */
329 memset (here, 0, sizeof (lang_bitmap));
330 here += sizeof (lang_bitmap);
331 line = here;
334 *here++ = c;
335 c = getc (list);
337 while (c != EOF && c != '\n');
338 *here++ = '\0';
339 *herep = here;
340 *linep = line;
341 return false;
345 /* Read the list of input files from LIST and compute all of the
346 relevant tables. There is one file per line of the list. At
347 first, all the files on the list are language-generic, but
348 eventually a line will appear which is the name of a language
349 subdirectory in square brackets, like this: [cp]. All subsequent
350 files are specific to that language, until another language
351 subdirectory tag appears. Files can appear more than once, if
352 they apply to more than one language. */
353 static void
354 read_input_list (const char *listname)
356 FILE *list = fopen (listname, "r");
357 if (!list)
358 fatal ("cannot open %s: %s", listname, xstrerror (errno));
359 else
361 struct fileloc epos;
362 size_t bufsz = measure_input_list (list);
363 char *buf = XNEWVEC (char, bufsz);
364 char *here = buf;
365 char *committed = buf;
366 char *limit = buf + bufsz;
367 char *line;
368 bool is_language;
369 size_t langno = 0;
370 size_t nfiles = 0;
371 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
373 epos.file = input_file_by_name (listname);
374 epos.line = 0;
376 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
377 gt_files = XNEWVEC (const input_file *, num_gt_files);
379 for (;;)
381 next_line:
382 epos.line++;
383 committed = here;
384 is_language = read_input_line (list, &here, &line, &epos);
385 gcc_assert (here <= limit);
386 if (line == 0)
387 break;
388 else if (is_language)
390 size_t i;
391 gcc_assert (langno <= num_lang_dirs);
392 for (i = 0; i < langno; i++)
393 if (strcmp (lang_dir_names[i], line) == 0)
395 error_at_line (&epos, "duplicate language tag [%s]",
396 line);
397 curlangs = 1 << i;
398 here = committed;
399 goto next_line;
402 curlangs = 1 << langno;
403 lang_dir_names[langno++] = line;
405 else
407 size_t i;
408 input_file *inpf = input_file_by_name (line);
409 gcc_assert (nfiles <= num_gt_files);
410 for (i = 0; i < nfiles; i++)
411 /* Since the input_file-s are uniquely hash-consed, we
412 can just compare pointers! */
413 if (gt_files[i] == inpf)
415 /* Throw away the string we just read, and add the
416 current language to the existing string's bitmap. */
417 lang_bitmap bmap = get_lang_bitmap (inpf);
418 if (bmap & curlangs)
419 error_at_line (&epos,
420 "file %s specified more than once "
421 "for language %s", line,
422 langno ==
423 0 ? "(all)" : lang_dir_names[langno -
424 1]);
426 bmap |= curlangs;
427 set_lang_bitmap (inpf, bmap);
428 here = committed;
429 goto next_line;
432 set_lang_bitmap (inpf, curlangs);
433 gt_files[nfiles++] = inpf;
436 /* Update the global counts now that we know accurately how many
437 things there are. (We do not bother resizing the arrays down.) */
438 num_lang_dirs = langno;
439 /* Add the plugin files if provided. */
440 if (plugin_files)
442 size_t i;
443 for (i = 0; i < nb_plugin_files; i++)
444 gt_files[nfiles++] = plugin_files[i];
446 num_gt_files = nfiles;
449 /* Sanity check: any file that resides in a language subdirectory
450 (e.g. 'cp') ought to belong to the corresponding language.
451 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
452 (Can you even do that? Should you be allowed to?) */
454 size_t f;
455 for (f = 0; f < num_gt_files; f++)
457 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
458 const char *basename = get_file_basename (gt_files[f]);
459 const char *slashpos = strchr (basename, '/');
460 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
461 const char *slashpos2 = strchr (basename, '\\');
463 if (!slashpos || (slashpos2 && slashpos2 < slashpos))
464 slashpos = slashpos2;
465 #endif
467 if (slashpos)
469 size_t l;
470 for (l = 0; l < num_lang_dirs; l++)
471 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
472 && memcmp (basename, lang_dir_names[l],
473 strlen (lang_dir_names[l])) == 0)
475 if (!(bitmap & (1 << l)))
476 error ("%s is in language directory '%s' but is not "
477 "tagged for that language",
478 basename, lang_dir_names[l]);
479 break;
485 if (ferror (list))
486 fatal ("error reading %s: %s", listname, xstrerror (errno));
488 fclose (list);
493 /* The one and only TYPE_STRING. */
495 struct type string_type = {
496 TYPE_STRING, 0, 0, 0, GC_USED, {0}
499 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
500 set early in main. */
502 struct type scalar_nonchar = {
503 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
506 struct type scalar_char = {
507 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
510 /* Lists of various things. */
512 pair_p typedefs = NULL;
513 type_p structures = NULL;
514 type_p param_structs = NULL;
515 pair_p variables = NULL;
517 static type_p find_param_structure (type_p t, type_p param[NUM_PARAM]);
518 static type_p adjust_field_tree_exp (type_p t, options_p opt);
519 static type_p adjust_field_rtx_def (type_p t, options_p opt);
521 /* Define S as a typedef to T at POS. */
523 void
524 do_typedef (const char *s, type_p t, struct fileloc *pos)
526 pair_p p;
528 /* temporary kludge - gengtype doesn't handle conditionals or
529 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
530 is coming from this file (main() sets them up with safe dummy
531 definitions). */
532 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
533 return;
535 for (p = typedefs; p != NULL; p = p->next)
536 if (strcmp (p->name, s) == 0)
538 if (p->type != t)
540 error_at_line (pos, "type `%s' previously defined", s);
541 error_at_line (&p->line, "previously defined here");
543 return;
546 p = XNEW (struct pair);
547 p->next = typedefs;
548 p->name = s;
549 p->type = t;
550 p->line = *pos;
551 p->opt = NULL;
552 typedefs = p;
555 /* Define S as a typename of a scalar. Cannot be used to define
556 typedefs of 'char'. Note: is also used for pointer-to-function
557 typedefs (which are therefore not treated as pointers). */
559 void
560 do_scalar_typedef (const char *s, struct fileloc *pos)
562 do_typedef (s, &scalar_nonchar, pos);
566 /* Define TYPE_NAME to be a user defined type at location POS. */
568 type_p
569 create_user_defined_type (const char *type_name, struct fileloc *pos)
571 type_p ty = find_structure (type_name, TYPE_USER_STRUCT);
573 /* We might have already seen an incomplete decl of the given type,
574 in which case we won't have yet seen a GTY((user)), and the type will
575 only have kind "TYPE_STRUCT". Mark it as a user struct. */
576 ty->kind = TYPE_USER_STRUCT;
578 ty->u.s.line = *pos;
579 ty->u.s.bitmap = get_lang_bitmap (pos->file);
580 do_typedef (type_name, ty, pos);
582 /* If TYPE_NAME specifies a template, create references to the types
583 in the template by pretending that each type is a field of TY.
584 This is needed to make sure that the types referenced by the
585 template are marked as used. */
586 char *str = xstrdup (type_name);
587 char *open_bracket = strchr (str, '<');
588 if (open_bracket)
590 /* We only accept simple template declarations (see
591 require_template_declaration), so we only need to parse a
592 comma-separated list of strings, implicitly assumed to
593 be type names. */
594 char *arg = open_bracket + 1;
595 char *type_id = strtok (arg, ",>");
596 pair_p fields = 0;
597 while (type_id)
599 /* Create a new field for every type found inside the template
600 parameter list. */
601 const char *field_name = xstrdup (type_id);
602 type_p arg_type = resolve_typedef (field_name, pos);
603 fields = create_field_at (fields, arg_type, field_name, 0, pos);
604 type_id = strtok (0, ",>");
607 /* Associate the field list to TY. */
608 ty->u.s.fields = fields;
610 free (str);
612 return ty;
616 /* Given a typedef name S, return its associated type. Return NULL if
617 S is not a registered type name. */
619 static type_p
620 type_for_name (const char *s)
622 pair_p p;
624 /* Special-case support for types within a "gcc::" namespace. Rather
625 than fully-supporting namespaces, simply strip off the "gcc::" prefix
626 where present. This allows us to have GTY roots of this form:
627 extern GTY(()) gcc::some_type *some_ptr;
628 where the autogenerated functions will refer to simply "some_type",
629 where they can be resolved into their namespace. */
630 if (0 == strncmp (s, "gcc::", 5))
631 s += 5;
633 for (p = typedefs; p != NULL; p = p->next)
634 if (strcmp (p->name, s) == 0)
635 return p->type;
636 return NULL;
640 /* Create an undefined type with name S and location POS. Return the
641 newly created type. */
643 static type_p
644 create_undefined_type (const char *s, struct fileloc *pos)
646 type_p ty = find_structure (s, TYPE_UNDEFINED);
647 ty->u.s.line = *pos;
648 ty->u.s.bitmap = get_lang_bitmap (pos->file);
649 do_typedef (s, ty, pos);
650 return ty;
654 /* Return the type previously defined for S. Use POS to report errors. */
656 type_p
657 resolve_typedef (const char *s, struct fileloc *pos)
659 bool is_template_instance = (strchr (s, '<') != NULL);
660 type_p p = type_for_name (s);
662 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
663 type for regular type identifiers. If the type identifier S is a
664 template instantiation, however, we treat it as a user defined
665 type.
667 FIXME, this is actually a limitation in gengtype. Supporting
668 template types and their instances would require keeping separate
669 track of the basic types definition and its instances. This
670 essentially forces all template classes in GC to be marked
671 GTY((user)). */
672 if (!p)
673 p = (is_template_instance)
674 ? create_user_defined_type (s, pos)
675 : create_undefined_type (s, pos);
677 return p;
680 /* Add SUBCLASS to head of linked list of BASE's subclasses. */
682 void add_subclass (type_p base, type_p subclass)
684 gcc_assert (union_or_struct_p (base));
685 gcc_assert (union_or_struct_p (subclass));
687 subclass->u.s.next_sibling_class = base->u.s.first_subclass;
688 base->u.s.first_subclass = subclass;
691 /* Create and return a new structure with tag NAME at POS with fields
692 FIELDS and options O. The KIND of structure must be one of
693 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
695 type_p
696 new_structure (const char *name, enum typekind kind, struct fileloc *pos,
697 pair_p fields, options_p o, type_p base_class)
699 type_p si;
700 type_p s = NULL;
701 lang_bitmap bitmap = get_lang_bitmap (pos->file);
702 bool isunion = (kind == TYPE_UNION);
704 gcc_assert (union_or_struct_p (kind));
706 for (si = structures; si != NULL; si = si->next)
707 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
709 type_p ls = NULL;
710 if (si->kind == TYPE_LANG_STRUCT)
712 ls = si;
714 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
715 if (si->u.s.bitmap == bitmap)
716 s = si;
718 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
720 ls = si;
721 type_count++;
722 si = XCNEW (struct type);
723 memcpy (si, ls, sizeof (struct type));
724 ls->kind = TYPE_LANG_STRUCT;
725 ls->u.s.lang_struct = si;
726 ls->u.s.fields = NULL;
727 si->next = NULL;
728 si->state_number = -type_count;
729 si->pointer_to = NULL;
730 si->u.s.lang_struct = ls;
732 else
733 s = si;
735 if (ls != NULL && s == NULL)
737 type_count++;
738 s = XCNEW (struct type);
739 s->state_number = -type_count;
740 s->next = ls->u.s.lang_struct;
741 ls->u.s.lang_struct = s;
742 s->u.s.lang_struct = ls;
744 break;
747 if (s == NULL)
749 type_count++;
750 s = XCNEW (struct type);
751 s->state_number = -type_count;
752 s->next = structures;
753 structures = s;
756 if (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap))
758 error_at_line (pos, "duplicate definition of '%s %s'",
759 isunion ? "union" : "struct", s->u.s.tag);
760 error_at_line (&s->u.s.line, "previous definition here");
763 s->kind = kind;
764 s->u.s.tag = name;
765 s->u.s.line = *pos;
766 s->u.s.fields = fields;
767 s->u.s.opt = o;
768 s->u.s.bitmap = bitmap;
769 if (s->u.s.lang_struct)
770 s->u.s.lang_struct->u.s.bitmap |= bitmap;
771 s->u.s.base_class = base_class;
772 if (base_class)
773 add_subclass (base_class, s);
775 return s;
778 /* Return the previously-defined structure or union with tag NAME,
779 or a new empty structure or union if none was defined previously.
780 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
781 TYPE_USER_STRUCT. */
783 type_p
784 find_structure (const char *name, enum typekind kind)
786 type_p s;
787 bool isunion = (kind == TYPE_UNION);
789 gcc_assert (kind == TYPE_UNDEFINED || union_or_struct_p (kind));
791 for (s = structures; s != NULL; s = s->next)
792 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
793 return s;
795 type_count++;
796 s = XCNEW (struct type);
797 s->next = structures;
798 s->state_number = -type_count;
799 structures = s;
800 s->kind = kind;
801 s->u.s.tag = name;
802 structures = s;
803 return s;
806 /* Return the previously-defined parameterized structure for structure
807 T and parameters PARAM, or a new parameterized empty structure or
808 union if none was defined previously. */
810 static type_p
811 find_param_structure (type_p t, type_p param[NUM_PARAM])
813 type_p res;
815 for (res = param_structs; res; res = res->next)
816 if (res->u.param_struct.stru == t
817 && memcmp (res->u.param_struct.param, param,
818 sizeof (type_p) * NUM_PARAM) == 0)
819 break;
820 if (res == NULL)
822 type_count++;
823 res = XCNEW (struct type);
824 res->kind = TYPE_PARAM_STRUCT;
825 res->next = param_structs;
826 res->state_number = -type_count;
827 param_structs = res;
828 res->u.param_struct.stru = t;
829 memcpy (res->u.param_struct.param, param, sizeof (type_p) * NUM_PARAM);
831 return res;
834 /* Return a scalar type with name NAME. */
836 type_p
837 create_scalar_type (const char *name)
839 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
840 return &scalar_char;
841 else
842 return &scalar_nonchar;
846 /* Return a pointer to T. */
848 type_p
849 create_pointer (type_p t)
851 if (!t->pointer_to)
853 type_p r = XCNEW (struct type);
854 type_count++;
855 r->state_number = -type_count;
856 r->kind = TYPE_POINTER;
857 r->u.p = t;
858 t->pointer_to = r;
860 return t->pointer_to;
863 /* Return an array of length LEN. */
865 type_p
866 create_array (type_p t, const char *len)
868 type_p v;
870 type_count++;
871 v = XCNEW (struct type);
872 v->kind = TYPE_ARRAY;
873 v->state_number = -type_count;
874 v->u.a.p = t;
875 v->u.a.len = len;
876 return v;
879 /* Return a string options structure with name NAME and info INFO.
880 NEXT is the next option in the chain. */
881 options_p
882 create_string_option (options_p next, const char *name, const char *info)
884 options_p o = XNEW (struct options);
885 o->kind = OPTION_STRING;
886 o->next = next;
887 o->name = name;
888 o->info.string = info;
889 return o;
892 /* Create a type options structure with name NAME and info INFO. NEXT
893 is the next option in the chain. */
894 options_p
895 create_type_option (options_p next, const char* name, type_p info)
897 options_p o = XNEW (struct options);
898 o->next = next;
899 o->name = name;
900 o->kind = OPTION_TYPE;
901 o->info.type = info;
902 return o;
905 /* Create a nested pointer options structure with name NAME and info
906 INFO. NEXT is the next option in the chain. */
907 options_p
908 create_nested_option (options_p next, const char* name,
909 struct nested_ptr_data* info)
911 options_p o;
912 o = XNEW (struct options);
913 o->next = next;
914 o->name = name;
915 o->kind = OPTION_NESTED;
916 o->info.nested = info;
917 return o;
920 /* Return an options structure for a "nested_ptr" option. */
921 options_p
922 create_nested_ptr_option (options_p next, type_p t,
923 const char *to, const char *from)
925 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
927 d->type = adjust_field_type (t, 0);
928 d->convert_to = to;
929 d->convert_from = from;
930 return create_nested_option (next, "nested_ptr", d);
933 /* Add a variable named S of type T with options O defined at POS,
934 to `variables'. */
935 void
936 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
938 pair_p n;
939 n = XNEW (struct pair);
940 n->name = s;
941 n->type = t;
942 n->line = *pos;
943 n->opt = o;
944 n->next = variables;
945 variables = n;
948 /* Most-general structure field creator. */
949 static pair_p
950 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
951 const input_file *inpf, int line)
953 pair_p field;
955 field = XNEW (struct pair);
956 field->next = next;
957 field->type = type;
958 field->name = name;
959 field->opt = opt;
960 field->line.file = inpf;
961 field->line.line = line;
962 return field;
965 /* Create a field that came from the source code we are scanning,
966 i.e. we have a 'struct fileloc', and possibly options; also,
967 adjust_field_type should be called. */
968 pair_p
969 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
970 struct fileloc *pos)
972 return create_field_all (next, adjust_field_type (type, opt),
973 name, opt, pos->file, pos->line);
976 /* Create a fake field with the given type and name. NEXT is the next
977 field in the chain. */
978 #define create_field(next,type,name) \
979 create_field_all (next,type,name, 0, this_file, __LINE__)
981 /* Like create_field, but the field is only valid when condition COND
982 is true. */
984 static pair_p
985 create_optional_field_ (pair_p next, type_p type, const char *name,
986 const char *cond, int line)
988 static int id = 1;
989 pair_p union_fields;
990 type_p union_type;
992 /* Create a fake union type with a single nameless field of type TYPE.
993 The field has a tag of "1". This allows us to make the presence
994 of a field of type TYPE depend on some boolean "desc" being true. */
995 union_fields = create_field (NULL, type, "");
996 union_fields->opt =
997 create_string_option (union_fields->opt, "dot", "");
998 union_fields->opt =
999 create_string_option (union_fields->opt, "tag", "1");
1000 union_type =
1001 new_structure (xasprintf ("%s_%d", "fake_union", id++), TYPE_UNION,
1002 &lexer_line, union_fields, NULL, NULL);
1004 /* Create the field and give it the new fake union type. Add a "desc"
1005 tag that specifies the condition under which the field is valid. */
1006 return create_field_all (next, union_type, name,
1007 create_string_option (0, "desc", cond),
1008 this_file, line);
1011 #define create_optional_field(next,type,name,cond) \
1012 create_optional_field_(next,type,name,cond,__LINE__)
1014 /* Reverse a linked list of 'struct pair's in place. */
1015 pair_p
1016 nreverse_pairs (pair_p list)
1018 pair_p prev = 0, p, next;
1019 for (p = list; p; p = next)
1021 next = p->next;
1022 p->next = prev;
1023 prev = p;
1025 return prev;
1029 /* We don't care how long a CONST_DOUBLE is. */
1030 #define CONST_DOUBLE_FORMAT "ww"
1031 /* We don't want to see codes that are only for generator files. */
1032 #undef GENERATOR_FILE
1034 enum rtx_code
1036 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1037 #include "rtl.def"
1038 #undef DEF_RTL_EXPR
1039 NUM_RTX_CODE
1042 static const char *const rtx_name[NUM_RTX_CODE] = {
1043 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1044 #include "rtl.def"
1045 #undef DEF_RTL_EXPR
1048 static const char *const rtx_format[NUM_RTX_CODE] = {
1049 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1050 #include "rtl.def"
1051 #undef DEF_RTL_EXPR
1054 static int rtx_next_new[NUM_RTX_CODE];
1056 /* We also need codes and names for insn notes (not register notes).
1057 Note that we do *not* bias the note values here. */
1058 enum insn_note
1060 #define DEF_INSN_NOTE(NAME) NAME,
1061 #include "insn-notes.def"
1062 #undef DEF_INSN_NOTE
1064 NOTE_INSN_MAX
1067 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1068 default field for line number notes. */
1069 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
1070 #define DEF_INSN_NOTE(NAME) #NAME,
1071 #include "insn-notes.def"
1072 #undef DEF_INSN_NOTE
1075 #undef CONST_DOUBLE_FORMAT
1076 #define GENERATOR_FILE
1078 /* Generate the contents of the rtx_next array. This really doesn't belong
1079 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1081 static void
1082 gen_rtx_next (void)
1084 int i;
1085 for (i = 0; i < NUM_RTX_CODE; i++)
1087 int k;
1089 rtx_next_new[i] = -1;
1090 if (strncmp (rtx_format[i], "iuu", 3) == 0)
1091 rtx_next_new[i] = 2;
1092 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1093 rtx_next_new[i] = 1;
1094 else
1095 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
1096 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1097 rtx_next_new[i] = k;
1101 /* Write out the contents of the rtx_next array. */
1102 static void
1103 write_rtx_next (void)
1105 outf_p f = get_output_file_with_visibility (NULL);
1106 int i;
1107 if (!f)
1108 return;
1110 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
1111 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1112 for (i = 0; i < NUM_RTX_CODE; i++)
1113 if (rtx_next_new[i] == -1)
1114 oprintf (f, " 0,\n");
1115 else
1116 oprintf (f,
1117 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1118 oprintf (f, "};\n");
1121 /* Handle `special("rtx_def")'. This is a special case for field
1122 `fld' of struct rtx_def, which is an array of unions whose values
1123 are based in a complex way on the type of RTL. */
1125 static type_p
1126 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1128 pair_p flds = NULL;
1129 options_p nodot;
1130 int i;
1131 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1132 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1134 if (t->kind != TYPE_UNION)
1136 error_at_line (&lexer_line,
1137 "special `rtx_def' must be applied to a union");
1138 return &string_type;
1141 nodot = create_string_option (NULL, "dot", "");
1143 rtx_tp = create_pointer (find_structure ("rtx_def", TYPE_STRUCT));
1144 rtvec_tp = create_pointer (find_structure ("rtvec_def", TYPE_STRUCT));
1145 tree_tp = create_pointer (find_structure ("tree_node", TYPE_UNION));
1146 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", TYPE_STRUCT));
1147 reg_attrs_tp =
1148 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT));
1149 basic_block_tp =
1150 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT));
1151 constant_tp =
1152 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT));
1153 scalar_tp = &scalar_nonchar; /* rtunion int */
1156 pair_p note_flds = NULL;
1157 int c;
1159 for (c = 0; c <= NOTE_INSN_MAX; c++)
1161 switch (c)
1163 case NOTE_INSN_MAX:
1164 case NOTE_INSN_DELETED_LABEL:
1165 case NOTE_INSN_DELETED_DEBUG_LABEL:
1166 note_flds = create_field (note_flds, &string_type, "rt_str");
1167 break;
1169 case NOTE_INSN_BLOCK_BEG:
1170 case NOTE_INSN_BLOCK_END:
1171 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1172 break;
1174 case NOTE_INSN_VAR_LOCATION:
1175 case NOTE_INSN_CALL_ARG_LOCATION:
1176 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1177 break;
1179 default:
1180 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1181 break;
1183 /* NOTE_INSN_MAX is used as the default field for line
1184 number notes. */
1185 if (c == NOTE_INSN_MAX)
1186 note_flds->opt =
1187 create_string_option (nodot, "default", "");
1188 else
1189 note_flds->opt =
1190 create_string_option (nodot, "tag", note_insn_name[c]);
1192 note_union_tp = new_structure ("rtx_def_note_subunion", TYPE_UNION,
1193 &lexer_line, note_flds, NULL, NULL);
1195 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1197 pair_p sym_flds;
1198 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1199 sym_flds->opt = create_string_option (nodot, "default", "");
1200 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1201 sym_flds->opt = create_string_option (nodot, "tag", "1");
1202 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", TYPE_UNION,
1203 &lexer_line, sym_flds, NULL, NULL);
1205 for (i = 0; i < NUM_RTX_CODE; i++)
1207 pair_p subfields = NULL;
1208 size_t aindex, nmindex;
1209 const char *sname;
1210 type_p substruct;
1211 char *ftag;
1213 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1215 type_p t;
1216 const char *subname;
1218 switch (rtx_format[i][aindex])
1220 case '*':
1221 case 'i':
1222 case 'n':
1223 case 'w':
1224 t = scalar_tp;
1225 subname = "rt_int";
1226 break;
1228 case '0':
1229 if (i == MEM && aindex == 1)
1230 t = mem_attrs_tp, subname = "rt_mem";
1231 else if (i == JUMP_INSN && aindex == 8)
1232 t = rtx_tp, subname = "rt_rtx";
1233 else if (i == CODE_LABEL && aindex == 5)
1234 t = scalar_tp, subname = "rt_int";
1235 else if (i == CODE_LABEL && aindex == 4)
1236 t = rtx_tp, subname = "rt_rtx";
1237 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1238 t = rtx_tp, subname = "rt_rtx";
1239 else if (i == NOTE && aindex == 4)
1240 t = note_union_tp, subname = "";
1241 else if (i == NOTE && aindex == 5)
1242 t = scalar_tp, subname = "rt_int";
1243 else if (i == NOTE && aindex >= 7)
1244 t = scalar_tp, subname = "rt_int";
1245 else if (i == ADDR_DIFF_VEC && aindex == 4)
1246 t = scalar_tp, subname = "rt_int";
1247 else if (i == VALUE && aindex == 0)
1248 t = scalar_tp, subname = "rt_int";
1249 else if (i == DEBUG_EXPR && aindex == 0)
1250 t = tree_tp, subname = "rt_tree";
1251 else if (i == REG && aindex == 1)
1252 t = scalar_tp, subname = "rt_int";
1253 else if (i == REG && aindex == 2)
1254 t = reg_attrs_tp, subname = "rt_reg";
1255 else if (i == SCRATCH && aindex == 0)
1256 t = scalar_tp, subname = "rt_int";
1257 else if (i == SYMBOL_REF && aindex == 1)
1258 t = scalar_tp, subname = "rt_int";
1259 else if (i == SYMBOL_REF && aindex == 2)
1260 t = symbol_union_tp, subname = "";
1261 else if (i == JUMP_TABLE_DATA && aindex >= 5)
1262 t = scalar_tp, subname = "rt_int";
1263 else if (i == BARRIER && aindex >= 3)
1264 t = scalar_tp, subname = "rt_int";
1265 else if (i == ENTRY_VALUE && aindex == 0)
1266 t = rtx_tp, subname = "rt_rtx";
1267 else
1269 error_at_line
1270 (&lexer_line,
1271 "rtx type `%s' has `0' in position %lu, can't handle",
1272 rtx_name[i], (unsigned long) aindex);
1273 t = &string_type;
1274 subname = "rt_int";
1276 break;
1278 case 's':
1279 case 'S':
1280 case 'T':
1281 t = &string_type;
1282 subname = "rt_str";
1283 break;
1285 case 'e':
1286 case 'u':
1287 t = rtx_tp;
1288 subname = "rt_rtx";
1289 break;
1291 case 'E':
1292 case 'V':
1293 t = rtvec_tp;
1294 subname = "rt_rtvec";
1295 break;
1297 case 't':
1298 t = tree_tp;
1299 subname = "rt_tree";
1300 break;
1302 case 'B':
1303 t = basic_block_tp;
1304 subname = "rt_bb";
1305 break;
1307 default:
1308 error_at_line
1309 (&lexer_line,
1310 "rtx type `%s' has `%c' in position %lu, can't handle",
1311 rtx_name[i], rtx_format[i][aindex],
1312 (unsigned long) aindex);
1313 t = &string_type;
1314 subname = "rt_int";
1315 break;
1318 subfields = create_field (subfields, t,
1319 xasprintf (".fld[%lu].%s",
1320 (unsigned long) aindex,
1321 subname));
1322 subfields->opt = nodot;
1323 if (t == note_union_tp)
1324 subfields->opt =
1325 create_string_option (subfields->opt, "desc",
1326 "NOTE_KIND (&%0)");
1327 if (t == symbol_union_tp)
1328 subfields->opt =
1329 create_string_option (subfields->opt, "desc",
1330 "CONSTANT_POOL_ADDRESS_P (&%0)");
1333 if (i == SYMBOL_REF)
1335 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1336 holds. */
1337 type_p field_tp = find_structure ("block_symbol", TYPE_STRUCT);
1338 subfields
1339 = create_optional_field (subfields, field_tp, "block_sym",
1340 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1343 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1344 substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
1345 NULL, NULL);
1347 ftag = xstrdup (rtx_name[i]);
1348 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1349 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1350 flds = create_field (flds, substruct, "");
1351 flds->opt = create_string_option (nodot, "tag", ftag);
1353 return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
1354 nodot, NULL);
1357 /* Handle `special("tree_exp")'. This is a special case for
1358 field `operands' of struct tree_exp, which although it claims to contain
1359 pointers to trees, actually sometimes contains pointers to RTL too.
1360 Passed T, the old type of the field, and OPT its options. Returns
1361 a new type for the field. */
1363 static type_p
1364 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1366 pair_p flds;
1367 options_p nodot;
1369 if (t->kind != TYPE_ARRAY)
1371 error_at_line (&lexer_line,
1372 "special `tree_exp' must be applied to an array");
1373 return &string_type;
1376 nodot = create_string_option (NULL, "dot", "");
1378 flds = create_field (NULL, t, "");
1379 flds->opt = create_string_option (nodot, "length",
1380 "TREE_OPERAND_LENGTH ((tree) &%0)");
1381 flds->opt = create_string_option (flds->opt, "default", "");
1383 return new_structure ("tree_exp_subunion", TYPE_UNION, &lexer_line, flds,
1384 nodot, NULL);
1387 /* Perform any special processing on a type T, about to become the type
1388 of a field. Return the appropriate type for the field.
1389 At present:
1390 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1391 - Similarly for arrays of pointer-to-char;
1392 - Converts structures for which a parameter is provided to
1393 TYPE_PARAM_STRUCT;
1394 - Handles "special" options.
1397 type_p
1398 adjust_field_type (type_p t, options_p opt)
1400 int length_p = 0;
1401 const int pointer_p = t->kind == TYPE_POINTER;
1402 type_p params[NUM_PARAM];
1403 int params_p = 0;
1404 int i;
1406 for (i = 0; i < NUM_PARAM; i++)
1407 params[i] = NULL;
1409 for (; opt; opt = opt->next)
1410 if (strcmp (opt->name, "length") == 0)
1412 if (length_p)
1413 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1414 if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
1416 error_at_line (&lexer_line,
1417 "option `%s' may not be applied to "
1418 "arrays of atomic types", opt->name);
1420 length_p = 1;
1422 else if ((strcmp (opt->name, "param_is") == 0
1423 || (strncmp (opt->name, "param", 5) == 0
1424 && ISDIGIT (opt->name[5])
1425 && strcmp (opt->name + 6, "_is") == 0))
1426 && opt->kind == OPTION_TYPE)
1428 int num = ISDIGIT (opt->name[5]) ? opt->name[5] - '0' : 0;
1430 if (!union_or_struct_p (t)
1431 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
1433 error_at_line (&lexer_line,
1434 "option `%s' may only be applied to structures or structure pointers",
1435 opt->name);
1436 return t;
1439 params_p = 1;
1440 if (params[num] != NULL)
1441 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1442 if (!ISDIGIT (opt->name[5]))
1443 params[num] = create_pointer (opt->info.type);
1444 else
1445 params[num] = opt->info.type;
1447 else if (strcmp (opt->name, "special") == 0
1448 && opt->kind == OPTION_STRING)
1450 const char *special_name = opt->info.string;
1451 if (strcmp (special_name, "tree_exp") == 0)
1452 t = adjust_field_tree_exp (t, opt);
1453 else if (strcmp (special_name, "rtx_def") == 0)
1454 t = adjust_field_rtx_def (t, opt);
1455 else
1456 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1459 if (params_p)
1461 type_p realt;
1463 if (pointer_p)
1464 t = t->u.p;
1465 realt = find_param_structure (t, params);
1466 t = pointer_p ? create_pointer (realt) : realt;
1469 if (!length_p
1470 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1471 return &string_type;
1472 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1473 && t->u.a.p->u.p->kind == TYPE_SCALAR
1474 && t->u.a.p->u.p->u.scalar_is_char)
1475 return create_array (&string_type, t->u.a.len);
1477 return t;
1481 static void set_gc_used_type (type_p, enum gc_used_enum, type_p *,
1482 bool = false);
1483 static void set_gc_used (pair_p);
1485 /* Handle OPT for set_gc_used_type. */
1487 static void
1488 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1489 int *pass_param, int *length, int *skip,
1490 type_p *nested_ptr)
1492 options_p o;
1493 for (o = opt; o; o = o->next)
1494 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1495 && o->kind == OPTION_TYPE)
1496 set_gc_used_type (o->info.type,
1497 GC_POINTED_TO, NULL);
1498 else if (strcmp (o->name, "maybe_undef") == 0)
1499 *maybe_undef = 1;
1500 else if (strcmp (o->name, "use_params") == 0)
1501 *pass_param = 1;
1502 else if (strcmp (o->name, "length") == 0)
1503 *length = 1;
1504 else if (strcmp (o->name, "skip") == 0)
1505 *skip = 1;
1506 else if (strcmp (o->name, "nested_ptr") == 0
1507 && o->kind == OPTION_NESTED)
1508 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1512 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1514 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1515 are set to GC_UNUSED. Otherwise, an error is emitted for
1516 TYPE_UNDEFINED types. This is used to support user-defined
1517 template types with non-type arguments.
1519 For instance, when we parse a template type with enum arguments
1520 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1521 artificial fields for 'MyType', one for 'AnotherType', the other
1522 one for 'EnumValue'.
1524 At the time that we parse this type we don't know that 'EnumValue'
1525 is really an enum value, so the parser creates a TYPE_UNDEFINED
1526 type for it. Since 'EnumValue' is never resolved to a known
1527 structure, it will stay with TYPE_UNDEFINED.
1529 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1530 'EnumValue'. Generating marking code for it would cause
1531 compilation failures since the marking routines assumes that
1532 'EnumValue' is a type. */
1534 static void
1535 set_gc_used_type (type_p t, enum gc_used_enum level, type_p param[NUM_PARAM],
1536 bool allow_undefined_types)
1538 if (t->gc_used >= level)
1539 return;
1541 t->gc_used = level;
1543 switch (t->kind)
1545 case TYPE_STRUCT:
1546 case TYPE_UNION:
1547 case TYPE_USER_STRUCT:
1549 pair_p f;
1550 int dummy;
1551 type_p dummy2;
1552 bool allow_undefined_field_types = (t->kind == TYPE_USER_STRUCT);
1554 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
1555 &dummy2);
1557 if (t->u.s.base_class)
1558 set_gc_used_type (t->u.s.base_class, level, param,
1559 allow_undefined_types);
1560 /* Anything pointing to a base class might actually be pointing
1561 to a subclass. */
1562 for (type_p subclass = t->u.s.first_subclass; subclass;
1563 subclass = subclass->u.s.next_sibling_class)
1564 set_gc_used_type (subclass, level, param,
1565 allow_undefined_types);
1567 FOR_ALL_INHERITED_FIELDS(t, f)
1569 int maybe_undef = 0;
1570 int pass_param = 0;
1571 int length = 0;
1572 int skip = 0;
1573 type_p nested_ptr = NULL;
1574 process_gc_options (f->opt, level, &maybe_undef, &pass_param,
1575 &length, &skip, &nested_ptr);
1577 if (nested_ptr && f->type->kind == TYPE_POINTER)
1578 set_gc_used_type (nested_ptr, GC_POINTED_TO,
1579 pass_param ? param : NULL);
1580 else if (length && f->type->kind == TYPE_POINTER)
1581 set_gc_used_type (f->type->u.p, GC_USED, NULL);
1582 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1583 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO, NULL);
1584 else if (pass_param && f->type->kind == TYPE_POINTER && param)
1585 set_gc_used_type (find_param_structure (f->type->u.p, param),
1586 GC_POINTED_TO, NULL);
1587 else if (skip)
1588 ; /* target type is not used through this field */
1589 else
1590 set_gc_used_type (f->type, GC_USED, pass_param ? param : NULL,
1591 allow_undefined_field_types);
1593 break;
1596 case TYPE_UNDEFINED:
1597 if (level > GC_UNUSED)
1599 if (!allow_undefined_types)
1600 error_at_line (&t->u.s.line, "undefined type `%s'", t->u.s.tag);
1601 t->gc_used = GC_UNUSED;
1603 break;
1605 case TYPE_POINTER:
1606 set_gc_used_type (t->u.p, GC_POINTED_TO, NULL);
1607 break;
1609 case TYPE_ARRAY:
1610 set_gc_used_type (t->u.a.p, GC_USED, param);
1611 break;
1613 case TYPE_LANG_STRUCT:
1614 for (t = t->u.s.lang_struct; t; t = t->next)
1615 set_gc_used_type (t, level, param);
1616 break;
1618 case TYPE_PARAM_STRUCT:
1620 int i;
1621 for (i = 0; i < NUM_PARAM; i++)
1622 if (t->u.param_struct.param[i] != 0)
1623 set_gc_used_type (t->u.param_struct.param[i], GC_USED, NULL);
1625 if (t->u.param_struct.stru->gc_used == GC_POINTED_TO)
1626 level = GC_POINTED_TO;
1627 else
1628 level = GC_USED;
1629 t->u.param_struct.stru->gc_used = GC_UNUSED;
1630 set_gc_used_type (t->u.param_struct.stru, level,
1631 t->u.param_struct.param);
1632 break;
1634 default:
1635 break;
1639 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1641 static void
1642 set_gc_used (pair_p variables)
1644 int nbvars = 0;
1645 pair_p p;
1646 for (p = variables; p; p = p->next)
1648 set_gc_used_type (p->type, GC_USED, NULL);
1649 nbvars++;
1651 if (verbosity_level >= 2)
1652 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1655 /* File mapping routines. For each input file, there is one output .c file
1656 (but some output files have many input files), and there is one .h file
1657 for the whole build. */
1659 /* Output file handling. */
1661 /* Create and return an outf_p for a new file for NAME, to be called
1662 ONAME. */
1664 static outf_p
1665 create_file (const char *name, const char *oname)
1667 static const char *const hdr[] = {
1668 " Copyright (C) 2004-2013 Free Software Foundation, Inc.\n",
1669 "\n",
1670 "This file is part of GCC.\n",
1671 "\n",
1672 "GCC is free software; you can redistribute it and/or modify it under\n",
1673 "the terms of the GNU General Public License as published by the Free\n",
1674 "Software Foundation; either version 3, or (at your option) any later\n",
1675 "version.\n",
1676 "\n",
1677 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1678 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1679 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1680 "for more details.\n",
1681 "\n",
1682 "You should have received a copy of the GNU General Public License\n",
1683 "along with GCC; see the file COPYING3. If not see\n",
1684 "<http://www.gnu.org/licenses/>. */\n",
1685 "\n",
1686 "/* This file is machine generated. Do not edit. */\n"
1688 outf_p f;
1689 size_t i;
1691 gcc_assert (name != NULL);
1692 gcc_assert (oname != NULL);
1693 f = XCNEW (struct outf);
1694 f->next = output_files;
1695 f->name = oname;
1696 output_files = f;
1698 oprintf (f, "/* Type information for %s.\n", name);
1699 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1700 oprintf (f, "%s", hdr[i]);
1701 return f;
1704 /* Print, like fprintf, to O.
1705 N.B. You might think this could be implemented more efficiently
1706 with vsnprintf(). Unfortunately, there are C libraries that
1707 provide that function but without the C99 semantics for its return
1708 value, making it impossible to know how much space is required. */
1709 void
1710 oprintf (outf_p o, const char *format, ...)
1712 char *s;
1713 size_t slength;
1714 va_list ap;
1716 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1717 in that case. */
1718 if (!o)
1719 return;
1721 va_start (ap, format);
1722 slength = vasprintf (&s, format, ap);
1723 if (s == NULL || (int) slength < 0)
1724 fatal ("out of memory");
1725 va_end (ap);
1727 if (o->bufused + slength > o->buflength)
1729 size_t new_len = o->buflength;
1730 if (new_len == 0)
1731 new_len = 1024;
1734 new_len *= 2;
1736 while (o->bufused + slength >= new_len);
1737 o->buf = XRESIZEVEC (char, o->buf, new_len);
1738 o->buflength = new_len;
1740 memcpy (o->buf + o->bufused, s, slength);
1741 o->bufused += slength;
1742 free (s);
1745 /* Open the global header file and the language-specific header files. */
1747 static void
1748 open_base_files (void)
1750 size_t i;
1752 if (nb_plugin_files > 0 && plugin_files)
1753 return;
1755 header_file = create_file ("GCC", "gtype-desc.h");
1757 base_files = XNEWVEC (outf_p, num_lang_dirs);
1759 for (i = 0; i < num_lang_dirs; i++)
1760 base_files[i] = create_file (lang_dir_names[i],
1761 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1763 /* gtype-desc.c is a little special, so we create it here. */
1765 /* The order of files here matters very much. */
1766 static const char *const ifiles[] = {
1767 "config.h", "system.h", "coretypes.h", "tm.h",
1768 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1769 "tree.h", "rtl.h", "function.h", "insn-config.h", "expr.h",
1770 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1771 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1772 "gimple.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
1773 "tree-phinodes.h", "ssa-iterators.h", "tree-ssanames.h",
1774 "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h", "tree-ssa-loop-manip.h",
1775 "tree-ssa-loop-niter.h", "tree-into-ssa.h", "tree-dfa.h",
1776 "tree-ssa.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1777 "except.h", "output.h", "cfgloop.h",
1778 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h",
1779 "ipa-inline.h", "dwarf2out.h", NULL
1781 const char *const *ifp;
1782 outf_p gtype_desc_c;
1784 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1785 for (ifp = ifiles; *ifp; ifp++)
1786 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1788 /* Make sure we handle "cfun" specially. */
1789 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1790 oprintf (gtype_desc_c, "#undef cfun\n");
1792 oprintf (gtype_desc_c,
1793 "\n"
1794 "/* Types with a \"gcc::\" namespace have it stripped\n"
1795 " during gengtype parsing. Provide a \"using\" directive\n"
1796 " to ensure that the fully-qualified types are found. */\n"
1797 "using namespace gcc;\n");
1801 /* For INPF an input file, return the real basename of INPF, with all
1802 the directory components skipped. */
1804 static const char *
1805 get_file_realbasename (const input_file *inpf)
1807 return lbasename (get_input_file_name (inpf));
1810 /* For INPF a filename, return the relative path to INPF from
1811 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1813 const char *
1814 get_file_srcdir_relative_path (const input_file *inpf)
1816 const char *f = get_input_file_name (inpf);
1817 if (strlen (f) > srcdir_len
1818 && IS_DIR_SEPARATOR (f[srcdir_len])
1819 && strncmp (f, srcdir, srcdir_len) == 0)
1820 return f + srcdir_len + 1;
1821 else
1822 return NULL;
1825 /* For INPF an input_file, return the relative path to INPF from
1826 $(srcdir) if the latter is a prefix in INPF, or the real basename
1827 of INPF otherwise. */
1829 static const char *
1830 get_file_basename (const input_file *inpf)
1832 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1834 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1837 /* For F a filename, return the lang_dir_names relative index of the language
1838 directory that is a prefix in F, if any, -1 otherwise. */
1840 static int
1841 get_prefix_langdir_index (const char *f)
1843 size_t f_len = strlen (f);
1844 size_t lang_index;
1846 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1848 const char *langdir = lang_dir_names[lang_index];
1849 size_t langdir_len = strlen (langdir);
1851 if (f_len > langdir_len
1852 && IS_DIR_SEPARATOR (f[langdir_len])
1853 && memcmp (f, langdir, langdir_len) == 0)
1854 return lang_index;
1857 return -1;
1860 /* For INPF an input file, return the name of language directory where
1861 F is located, if any, NULL otherwise. */
1863 static const char *
1864 get_file_langdir (const input_file *inpf)
1866 /* Get the relative path to INPF from $(srcdir) and find the
1867 language by comparing the prefix with language directory names.
1868 If INPF is not even srcdir relative, no point in looking
1869 further. */
1871 int lang_index;
1872 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1873 const char *r;
1875 if (!srcdir_relative_path)
1876 return NULL;
1878 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1879 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1880 r = "c-family";
1881 else if (lang_index >= 0)
1882 r = lang_dir_names[lang_index];
1883 else
1884 r = NULL;
1886 return r;
1889 /* The gt- output file name for INPF. */
1891 static const char *
1892 get_file_gtfilename (const input_file *inpf)
1894 /* Cook up an initial version of the gt- file name from the file real
1895 basename and the language name, if any. */
1897 const char *basename = get_file_realbasename (inpf);
1898 const char *langdir = get_file_langdir (inpf);
1900 char *result =
1901 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1902 : xasprintf ("gt-%s", basename));
1904 /* Then replace all non alphanumerics characters by '-' and change the
1905 extension to ".h". We expect the input filename extension was at least
1906 one character long. */
1908 char *s = result;
1910 for (; *s != '.'; s++)
1911 if (!ISALNUM (*s) && *s != '-')
1912 *s = '-';
1914 memcpy (s, ".h", sizeof (".h"));
1916 return result;
1919 /* Each input_file has its associated output file outf_p. The
1920 association is computed by the function
1921 get_output_file_with_visibility. The associated file is cached
1922 inside input_file in its inpoutf field, so is really computed only
1923 once. Associated output file paths (i.e. output_name-s) are
1924 computed by a rule based regexp machinery, using the files_rules
1925 array of struct file_rule_st. A for_name is also computed, giving
1926 the source file name for which the output_file is generated; it is
1927 often the last component of the input_file path. */
1931 Regexpr machinery to compute the output_name and for_name-s of each
1932 input_file. We have a sequence of file rules which gives the POSIX
1933 extended regular expression to match an input file path, and two
1934 transformed strings for the corresponding output_name and the
1935 corresponding for_name. The transformed string contain dollars: $0
1936 is replaced by the entire match, $1 is replaced by the substring
1937 matching the first parenthesis in the regexp, etc. And $$ is replaced
1938 by a single verbatim dollar. The rule order is important. The
1939 general case is last, and the particular cases should come before.
1940 An action routine can, when needed, update the out_name & for_name
1941 and/or return the appropriate output file. It is invoked only when a
1942 rule is triggered. When a rule is triggered, the output_name and
1943 for_name are computed using their transform string in while $$, $0,
1944 $1, ... are suitably replaced. If there is an action, it is called.
1945 In some few cases, the action can directly return the outf_p, but
1946 usually it just updates the output_name and for_name so should free
1947 them before replacing them. The get_output_file_with_visibility
1948 function creates an outf_p only once per each output_name, so it
1949 scans the output_files list for previously seen output file names.
1952 /* Signature of actions in file rules. */
1953 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
1956 struct file_rule_st {
1957 const char* frul_srcexpr; /* Source string for regexp. */
1958 int frul_rflags; /* Flags passed to regcomp, usually
1959 * REG_EXTENDED. */
1960 regex_t* frul_re; /* Compiled regular expression
1961 obtained by regcomp. */
1962 const char* frul_tr_out; /* Transformation string for making
1963 * the output_name, with $1 ... $9 for
1964 * subpatterns and $0 for the whole
1965 * matched filename. */
1966 const char* frul_tr_for; /* Tranformation string for making the
1967 for_name. */
1968 frul_actionrout_t* frul_action; /* The action, if non null, is
1969 * called once the rule matches, on
1970 * the transformed out_name &
1971 * for_name. It could change them
1972 * and/or give the output file. */
1975 /* File rule action handling *.h files. */
1976 static outf_p header_dot_h_frul (input_file*, char**, char**);
1978 /* File rule action handling *.c files. */
1979 static outf_p source_dot_c_frul (input_file*, char**, char**);
1981 #define NULL_REGEX (regex_t*)0
1983 /* The prefix in our regexp-s matching the directory. */
1984 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1986 #define NULL_FRULACT (frul_actionrout_t*)0
1988 /* The array of our rules governing file name generation. Rules order
1989 matters, so change with extreme care! */
1991 struct file_rule_st files_rules[] = {
1992 /* The general rule assumes that files in subdirectories belong to a
1993 particular front-end, and files not in subdirectories are shared.
1994 The following rules deal with exceptions - files that are in
1995 subdirectories and yet are shared, and files that are top-level,
1996 but are not shared. */
1998 /* the c-family/ source directory is special. */
1999 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
2000 REG_EXTENDED, NULL_REGEX,
2001 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
2003 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
2004 REG_EXTENDED, NULL_REGEX,
2005 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
2007 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
2008 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
2009 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
2011 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
2012 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
2014 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
2015 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
2016 REG_EXTENDED, NULL_REGEX,
2017 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
2019 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
2020 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
2021 REG_EXTENDED, NULL_REGEX,
2022 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
2024 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
2025 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
2026 REG_EXTENDED, NULL_REGEX,
2027 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
2029 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
2030 { DIR_PREFIX_REGEX "cp/parser\\.h$",
2031 REG_EXTENDED, NULL_REGEX,
2032 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
2034 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
2035 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
2036 REG_EXTENDED, NULL_REGEX,
2037 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
2039 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
2040 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
2041 REG_EXTENDED, NULL_REGEX,
2042 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
2044 /* General cases. For header *.h and source *.c or *.cc files, we
2045 * need special actions to handle the language. */
2047 /* Source *.c files are using get_file_gtfilename to compute their
2048 output_name and get_file_basename to compute their for_name
2049 through the source_dot_c_frul action. */
2050 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
2051 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
2053 /* Source *.cc files are using get_file_gtfilename to compute their
2054 output_name and get_file_basename to compute their for_name
2055 through the source_dot_c_frul action. */
2056 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.cc$",
2057 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.cc", source_dot_c_frul},
2059 /* Common header files get "gtype-desc.c" as their output_name,
2060 * while language specific header files are handled specially. So
2061 * we need the header_dot_h_frul action. */
2062 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
2063 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
2065 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
2066 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
2068 /* Mandatory null last entry signaling end of rules. */
2069 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
2072 /* Special file rules action for handling *.h header files. It gives
2073 "gtype-desc.c" for common headers and corresponding output
2074 files for language-specific header files. */
2075 static outf_p
2076 header_dot_h_frul (input_file* inpf, char**poutname,
2077 char**pforname ATTRIBUTE_UNUSED)
2079 const char *basename = 0;
2080 int lang_index = 0;
2081 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2082 (void*) inpf, get_input_file_name (inpf),
2083 *poutname, *pforname);
2084 basename = get_file_basename (inpf);
2085 lang_index = get_prefix_langdir_index (basename);
2086 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
2088 if (lang_index >= 0)
2090 /* The header is language specific. Given output_name &
2091 for_name remains unchanged. The base_files array gives the
2092 outf_p. */
2093 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2094 (void*) base_files[lang_index],
2095 (base_files[lang_index])->name);
2096 return base_files[lang_index];
2098 else
2100 /* The header is common to all front-end languages. So
2101 output_name is "gtype-desc.c" file. The calling function
2102 get_output_file_with_visibility will find its outf_p. */
2103 free (*poutname);
2104 *poutname = xstrdup ("gtype-desc.c");
2105 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2106 get_input_file_name (inpf));
2107 return NULL;
2112 /* Special file rules action for handling *.c source files using
2113 * get_file_gtfilename to compute their output_name and
2114 * get_file_basename to compute their for_name. The output_name is
2115 * gt-<LANG>-<BASE>.h for language specific source files, and
2116 * gt-<BASE>.h for common source files. */
2117 static outf_p
2118 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
2120 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
2121 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
2122 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2123 (void*) inpf, get_input_file_name (inpf),
2124 *poutname, *pforname);
2125 DBGPRINTF ("newoutname %s", newoutname);
2126 DBGPRINTF ("newbasename %s", newbasename);
2127 free (*poutname);
2128 free (*pforname);
2129 *poutname = newoutname;
2130 *pforname = newbasename;
2131 return NULL;
2134 /* Utility function for get_output_file_with_visibility which returns
2135 * a malloc-ed substituted string using TRS on matching of the FILNAM
2136 * file name, using the PMATCH array. */
2137 static char*
2138 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
2139 const char *trs)
2141 struct obstack str_obstack;
2142 char *str = NULL;
2143 char *rawstr = NULL;
2144 const char *pt = NULL;
2145 DBGPRINTF ("filnam %s", filnam);
2146 obstack_init (&str_obstack);
2147 for (pt = trs; *pt; pt++) {
2148 char c = *pt;
2149 if (c == '$')
2151 if (pt[1] == '$')
2153 /* A double dollar $$ is substituted by a single verbatim
2154 dollar, but who really uses dollar signs in file
2155 paths? */
2156 obstack_1grow (&str_obstack, '$');
2158 else if (ISDIGIT (pt[1]))
2160 /* Handle $0 $1 ... $9 by appropriate substitution. */
2161 int dolnum = pt[1] - '0';
2162 int so = pmatch[dolnum].rm_so;
2163 int eo = pmatch[dolnum].rm_eo;
2164 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2165 if (so>=0 && eo>=so)
2166 obstack_grow (&str_obstack, filnam + so, eo - so);
2168 else
2170 /* This can happen only when files_rules is buggy! */
2171 gcc_unreachable ();
2173 /* Always skip the character after the dollar. */
2174 pt++;
2176 else
2177 obstack_1grow (&str_obstack, c);
2179 obstack_1grow (&str_obstack, '\0');
2180 rawstr = XOBFINISH (&str_obstack, char *);
2181 str = xstrdup (rawstr);
2182 obstack_free (&str_obstack, NULL);
2183 DBGPRINTF ("matched replacement %s", str);
2184 rawstr = NULL;
2185 return str;
2189 /* An output file, suitable for definitions, that can see declarations
2190 made in INPF and is linked into every language that uses INPF.
2191 Since the result is cached inside INPF, that argument cannot be
2192 declared constant, but is "almost" constant. */
2194 outf_p
2195 get_output_file_with_visibility (input_file *inpf)
2197 outf_p r;
2198 char *for_name = NULL;
2199 char *output_name = NULL;
2200 const char* inpfname;
2202 /* This can happen when we need a file with visibility on a
2203 structure that we've never seen. We have to just hope that it's
2204 globally visible. */
2205 if (inpf == NULL)
2206 inpf = system_h_file;
2208 /* The result is cached in INPF, so return it if already known. */
2209 if (inpf->inpoutf)
2210 return inpf->inpoutf;
2212 /* In plugin mode, return NULL unless the input_file is one of the
2213 plugin_files. */
2214 if (plugin_files)
2216 size_t i;
2217 for (i = 0; i < nb_plugin_files; i++)
2218 if (inpf == plugin_files[i])
2220 inpf->inpoutf = plugin_output;
2221 return plugin_output;
2224 return NULL;
2227 inpfname = get_input_file_name (inpf);
2229 /* Try each rule in sequence in files_rules until one is triggered. */
2231 int rulix = 0;
2232 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2233 (void*) inpf, inpfname);
2235 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2237 DBGPRINTF ("rulix#%d srcexpr %s",
2238 rulix, files_rules[rulix].frul_srcexpr);
2240 if (!files_rules[rulix].frul_re)
2242 /* Compile the regexpr lazily. */
2243 int err = 0;
2244 files_rules[rulix].frul_re = XCNEW (regex_t);
2245 err = regcomp (files_rules[rulix].frul_re,
2246 files_rules[rulix].frul_srcexpr,
2247 files_rules[rulix].frul_rflags);
2248 if (err)
2250 /* The regular expression compilation fails only when
2251 file_rules is buggy. */
2252 gcc_unreachable ();
2256 output_name = NULL;
2257 for_name = NULL;
2259 /* Match the regexpr and trigger the rule if matched. */
2261 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2262 $3, ... $9. */
2263 regmatch_t pmatch[10];
2264 memset (pmatch, 0, sizeof (pmatch));
2265 if (!regexec (files_rules[rulix].frul_re,
2266 inpfname, 10, pmatch, 0))
2268 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2269 (void*) inpf, inpfname, rulix,
2270 files_rules[rulix].frul_srcexpr);
2271 for_name =
2272 matching_file_name_substitute (inpfname, pmatch,
2273 files_rules[rulix].frul_tr_for);
2274 DBGPRINTF ("for_name %s", for_name);
2275 output_name =
2276 matching_file_name_substitute (inpfname, pmatch,
2277 files_rules[rulix].frul_tr_out);
2278 DBGPRINTF ("output_name %s", output_name);
2279 if (files_rules[rulix].frul_action)
2281 /* Invoke our action routine. */
2282 outf_p of = NULL;
2283 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2284 rulix, output_name, for_name);
2285 of =
2286 (files_rules[rulix].frul_action) (inpf,
2287 &output_name, &for_name);
2288 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2289 rulix, (void*)of, output_name, for_name);
2290 /* If the action routine returned something, give it back
2291 immediately and cache it in inpf. */
2292 if (of)
2294 inpf->inpoutf = of;
2295 return of;
2298 /* The rule matched, and had no action, or that action did
2299 not return any output file but could have changed the
2300 output_name or for_name. We break out of the loop on the
2301 files_rules. */
2302 break;
2304 else
2306 /* The regexpr did not match. */
2307 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2308 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2309 continue;
2314 if (!output_name || !for_name)
2316 /* This should not be possible, and could only happen if the
2317 files_rules is incomplete or buggy. */
2318 fatal ("failed to compute output name for %s", inpfname);
2321 /* Look through to see if we've ever seen this output filename
2322 before. If found, cache the result in inpf. */
2323 for (r = output_files; r; r = r->next)
2324 if (filename_cmp (r->name, output_name) == 0)
2326 inpf->inpoutf = r;
2327 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2328 output_name, for_name);
2329 return r;
2332 /* If not found, create it, and cache it in inpf. */
2333 r = create_file (for_name, output_name);
2335 gcc_assert (r && r->name);
2336 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2337 output_name, for_name);
2338 inpf->inpoutf = r;
2339 return r;
2344 /* The name of an output file, suitable for definitions, that can see
2345 declarations made in INPF and is linked into every language that
2346 uses INPF. */
2348 const char *
2349 get_output_file_name (input_file* inpf)
2351 outf_p o = get_output_file_with_visibility (inpf);
2352 if (o)
2353 return o->name;
2354 return NULL;
2357 /* Check if existing file is equal to the in memory buffer. */
2359 static bool
2360 is_file_equal (outf_p of)
2362 FILE *newfile = fopen (of->name, "r");
2363 size_t i;
2364 bool equal;
2365 if (newfile == NULL)
2366 return false;
2368 equal = true;
2369 for (i = 0; i < of->bufused; i++)
2371 int ch;
2372 ch = fgetc (newfile);
2373 if (ch == EOF || ch != (unsigned char) of->buf[i])
2375 equal = false;
2376 break;
2379 if (equal && EOF != fgetc (newfile))
2380 equal = false;
2381 fclose (newfile);
2382 return equal;
2385 /* Copy the output to its final destination,
2386 but don't unnecessarily change modification times. */
2388 static void
2389 close_output_files (void)
2391 int nbwrittenfiles = 0;
2392 outf_p of;
2394 for (of = output_files; of; of = of->next)
2396 if (!is_file_equal (of))
2398 FILE *newfile = NULL;
2399 char *backupname = NULL;
2400 /* Back up the old version of the output file gt-FOO.c as
2401 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2402 if (backup_dir)
2404 backupname = concat (backup_dir, "/",
2405 lbasename (of->name), "~", NULL);
2406 if (!access (of->name, F_OK) && rename (of->name, backupname))
2407 fatal ("failed to back up %s as %s: %s",
2408 of->name, backupname, xstrerror (errno));
2411 newfile = fopen (of->name, "w");
2412 if (newfile == NULL)
2413 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2414 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2415 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2416 if (fclose (newfile) != 0)
2417 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2418 nbwrittenfiles++;
2419 if (verbosity_level >= 2 && backupname)
2420 printf ("%s wrote #%-3d %s backed-up in %s\n",
2421 progname, nbwrittenfiles, of->name, backupname);
2422 else if (verbosity_level >= 1)
2423 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2424 free (backupname);
2426 else
2428 /* output file remains unchanged. */
2429 if (verbosity_level >= 2)
2430 printf ("%s keep %s\n", progname, of->name);
2432 free (of->buf);
2433 of->buf = NULL;
2434 of->bufused = of->buflength = 0;
2436 if (verbosity_level >= 1)
2437 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2440 struct flist
2442 struct flist *next;
2443 int started_p;
2444 const input_file* file;
2445 outf_p f;
2448 struct walk_type_data;
2450 /* For scalars and strings, given the item in 'val'.
2451 For structures, given a pointer to the item in 'val'.
2452 For misc. pointers, given the item in 'val'.
2454 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2455 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2457 /* Parameters for write_types. */
2459 struct write_types_data
2461 const char *prefix;
2462 const char *param_prefix;
2463 const char *subfield_marker_routine;
2464 const char *marker_routine;
2465 const char *reorder_note_routine;
2466 const char *comment;
2467 int skip_hooks; /* skip hook generation if non zero */
2470 static void output_escaped_param (struct walk_type_data *d,
2471 const char *, const char *);
2472 static void output_mangled_typename (outf_p, const_type_p);
2473 static void walk_type (type_p t, struct walk_type_data *d);
2474 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2475 const struct write_types_data *wtd);
2476 static void write_types_process_field
2477 (type_p f, const struct walk_type_data *d);
2478 static void write_types (outf_p output_header,
2479 type_p structures,
2480 type_p param_structs,
2481 const struct write_types_data *wtd);
2482 static void write_types_local_process_field
2483 (type_p f, const struct walk_type_data *d);
2484 static void write_local_func_for_structure
2485 (const_type_p orig_s, type_p s, type_p *param);
2486 static void write_local (outf_p output_header,
2487 type_p structures, type_p param_structs);
2488 static int contains_scalar_p (type_p t);
2489 static void put_mangled_filename (outf_p, const input_file *);
2490 static void finish_root_table (struct flist *flp, const char *pfx,
2491 const char *tname, const char *lastname,
2492 const char *name);
2493 static void write_root (outf_p, pair_p, type_p, const char *, int,
2494 struct fileloc *, const char *, bool);
2495 static void write_array (outf_p f, pair_p v,
2496 const struct write_types_data *wtd);
2497 static void write_roots (pair_p, bool);
2499 /* Parameters for walk_type. */
2501 struct walk_type_data
2503 process_field_fn process_field;
2504 const void *cookie;
2505 outf_p of;
2506 options_p opt;
2507 const char *val;
2508 const char *prev_val[4];
2509 int indent;
2510 int counter;
2511 const struct fileloc *line;
2512 lang_bitmap bitmap;
2513 type_p *param;
2514 int used_length;
2515 type_p orig_s;
2516 const char *reorder_fn;
2517 bool needs_cast_p;
2518 bool fn_wants_lvalue;
2519 bool in_record_p;
2520 int loopcounter;
2521 bool in_ptr_field;
2522 bool have_this_obj;
2526 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2527 pre-processor identifier to use in a #define directive. This replaces
2528 special characters used in C++ identifiers like '>', '<' and ':' with
2529 '_'.
2531 If no C++ special characters are found in TYPE_NAME, return
2532 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2533 characters replaced with '_'. In this case, the caller is
2534 responsible for freeing the allocated string. */
2536 static const char *
2537 filter_type_name (const char *type_name)
2539 if (strchr (type_name, '<') || strchr (type_name, ':'))
2541 size_t i;
2542 char *s = xstrdup (type_name);
2543 for (i = 0; i < strlen (s); i++)
2544 if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ',')
2545 s[i] = '_';
2546 return s;
2548 else
2549 return type_name;
2553 /* Print a mangled name representing T to OF. */
2555 static void
2556 output_mangled_typename (outf_p of, const_type_p t)
2558 if (t == NULL)
2559 oprintf (of, "Z");
2560 else
2561 switch (t->kind)
2563 case TYPE_NONE:
2564 case TYPE_UNDEFINED:
2565 gcc_unreachable ();
2566 break;
2567 case TYPE_POINTER:
2568 oprintf (of, "P");
2569 output_mangled_typename (of, t->u.p);
2570 break;
2571 case TYPE_SCALAR:
2572 oprintf (of, "I");
2573 break;
2574 case TYPE_STRING:
2575 oprintf (of, "S");
2576 break;
2577 case TYPE_STRUCT:
2578 case TYPE_UNION:
2579 case TYPE_LANG_STRUCT:
2580 case TYPE_USER_STRUCT:
2582 /* For references to classes within an inheritance hierarchy,
2583 only ever reference the ultimate base class, since only
2584 it will have gt_ functions. */
2585 t = get_ultimate_base_class (t);
2586 const char *id_for_tag = filter_type_name (t->u.s.tag);
2587 oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2588 id_for_tag);
2589 if (id_for_tag != t->u.s.tag)
2590 free (CONST_CAST (char *, id_for_tag));
2592 break;
2593 case TYPE_PARAM_STRUCT:
2595 int i;
2596 for (i = 0; i < NUM_PARAM; i++)
2597 if (t->u.param_struct.param[i] != NULL)
2598 output_mangled_typename (of, t->u.param_struct.param[i]);
2599 output_mangled_typename (of, t->u.param_struct.stru);
2601 break;
2602 case TYPE_ARRAY:
2603 gcc_unreachable ();
2607 /* Print PARAM to D->OF processing escapes. D->VAL references the
2608 current object, D->PREV_VAL the object containing the current
2609 object, ONAME is the name of the option and D->LINE is used to
2610 print error messages. */
2612 static void
2613 output_escaped_param (struct walk_type_data *d, const char *param,
2614 const char *oname)
2616 const char *p;
2618 for (p = param; *p; p++)
2619 if (*p != '%')
2620 oprintf (d->of, "%c", *p);
2621 else
2622 switch (*++p)
2624 case 'h':
2625 oprintf (d->of, "(%s)", d->prev_val[2]);
2626 break;
2627 case '0':
2628 oprintf (d->of, "(%s)", d->prev_val[0]);
2629 break;
2630 case '1':
2631 oprintf (d->of, "(%s)", d->prev_val[1]);
2632 break;
2633 case 'a':
2635 const char *pp = d->val + strlen (d->val);
2636 while (pp[-1] == ']')
2637 while (*pp != '[')
2638 pp--;
2639 oprintf (d->of, "%s", pp);
2641 break;
2642 default:
2643 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2644 oname, '%', *p);
2648 const char *
2649 get_string_option (options_p opt, const char *key)
2651 for (; opt; opt = opt->next)
2652 if (strcmp (opt->name, key) == 0)
2653 return opt->info.string;
2654 return NULL;
2657 static void
2658 walk_subclasses (type_p base, struct walk_type_data *d)
2660 for (type_p sub = base->u.s.first_subclass; sub != NULL;
2661 sub = sub->u.s.next_sibling_class)
2663 const char *type_tag = get_string_option (sub->u.s.opt, "tag");
2664 if (type_tag)
2666 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
2667 d->indent += 2;
2668 oprintf (d->of, "%*s{\n", d->indent, "");
2669 d->indent += 2;
2670 oprintf (d->of, "%*s%s *sub = static_cast <%s *> (x);\n",
2671 d->indent, "", sub->u.s.tag, sub->u.s.tag);
2672 const char *old_val = d->val;
2673 d->val = "(*sub)";
2674 walk_type (sub, d);
2675 d->val = old_val;
2676 d->indent -= 2;
2677 oprintf (d->of, "%*s}\n", d->indent, "");
2678 oprintf (d->of, "%*sbreak;\n", d->indent, "");
2679 d->indent -= 2;
2681 walk_subclasses (sub, d);
2685 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2686 which is of type T. Write code to D->OF to constrain execution (at
2687 the point that D->PROCESS_FIELD is called) to the appropriate
2688 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2689 pointers to those objects. D->PREV_VAL lists the objects
2690 containing the current object, D->OPT is a list of options to
2691 apply, D->INDENT is the current indentation level, D->LINE is used
2692 to print error messages, D->BITMAP indicates which languages to
2693 print the structure for, and D->PARAM is the current parameter
2694 (from an enclosing param_is option). */
2696 static void
2697 walk_type (type_p t, struct walk_type_data *d)
2699 const char *length = NULL;
2700 const char *desc = NULL;
2701 const char *type_tag = NULL;
2702 int maybe_undef_p = 0;
2703 int use_param_num = -1;
2704 int use_params_p = 0;
2705 int atomic_p = 0;
2706 options_p oo;
2707 const struct nested_ptr_data *nested_ptr_d = NULL;
2709 d->needs_cast_p = false;
2710 for (oo = d->opt; oo; oo = oo->next)
2711 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2712 length = oo->info.string;
2713 else if (strcmp (oo->name, "maybe_undef") == 0)
2714 maybe_undef_p = 1;
2715 else if (strncmp (oo->name, "use_param", 9) == 0
2716 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2717 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2718 else if (strcmp (oo->name, "use_params") == 0)
2719 use_params_p = 1;
2720 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2721 desc = oo->info.string;
2722 else if (strcmp (oo->name, "mark_hook") == 0)
2724 else if (strcmp (oo->name, "nested_ptr") == 0
2725 && oo->kind == OPTION_NESTED)
2726 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2727 else if (strcmp (oo->name, "dot") == 0)
2729 else if (strcmp (oo->name, "tag") == 0)
2730 type_tag = oo->info.string;
2731 else if (strcmp (oo->name, "special") == 0)
2733 else if (strcmp (oo->name, "skip") == 0)
2735 else if (strcmp (oo->name, "atomic") == 0)
2736 atomic_p = 1;
2737 else if (strcmp (oo->name, "default") == 0)
2739 else if (strcmp (oo->name, "param_is") == 0)
2741 else if (strncmp (oo->name, "param", 5) == 0
2742 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2744 else if (strcmp (oo->name, "chain_next") == 0)
2746 else if (strcmp (oo->name, "chain_prev") == 0)
2748 else if (strcmp (oo->name, "chain_circular") == 0)
2750 else if (strcmp (oo->name, "reorder") == 0)
2752 else if (strcmp (oo->name, "variable_size") == 0)
2754 else
2755 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2757 if (d->used_length)
2758 length = NULL;
2760 if (use_params_p)
2762 int pointer_p = t->kind == TYPE_POINTER;
2764 if (pointer_p)
2765 t = t->u.p;
2766 if (!union_or_struct_p (t))
2767 error_at_line (d->line, "`use_params' option on unimplemented type");
2768 else
2769 t = find_param_structure (t, d->param);
2770 if (pointer_p)
2771 t = create_pointer (t);
2774 if (use_param_num != -1)
2776 if (d->param != NULL && d->param[use_param_num] != NULL)
2778 type_p nt = d->param[use_param_num];
2780 if (t->kind == TYPE_ARRAY)
2781 nt = create_array (nt, t->u.a.len);
2782 else if (length != NULL && t->kind == TYPE_POINTER)
2783 nt = create_pointer (nt);
2784 d->needs_cast_p = (t->kind != TYPE_POINTER
2785 && (nt->kind == TYPE_POINTER
2786 || nt->kind == TYPE_STRING));
2787 t = nt;
2789 else
2790 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2793 if (maybe_undef_p
2794 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
2796 error_at_line (d->line,
2797 "field `%s' has invalid option `maybe_undef_p'\n",
2798 d->val);
2799 return;
2802 if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2804 error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2805 return;
2808 switch (t->kind)
2810 case TYPE_SCALAR:
2811 case TYPE_STRING:
2812 d->process_field (t, d);
2813 break;
2815 case TYPE_POINTER:
2817 d->in_ptr_field = true;
2818 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2820 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2821 break;
2824 /* If a pointer type is marked as "atomic", we process the
2825 field itself, but we don't walk the data that they point to.
2827 There are two main cases where we walk types: to mark
2828 pointers that are reachable, and to relocate pointers when
2829 writing a PCH file. In both cases, an atomic pointer is
2830 itself marked or relocated, but the memory that it points
2831 to is left untouched. In the case of PCH, that memory will
2832 be read/written unchanged to the PCH file. */
2833 if (atomic_p)
2835 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2836 d->indent += 2;
2837 d->process_field (t, d);
2838 d->indent -= 2;
2839 oprintf (d->of, "%*s}\n", d->indent, "");
2840 break;
2843 if (!length)
2845 if (!union_or_struct_p (t->u.p)
2846 && t->u.p->kind != TYPE_PARAM_STRUCT)
2848 error_at_line (d->line,
2849 "field `%s' is pointer to unimplemented type",
2850 d->val);
2851 break;
2854 if (nested_ptr_d)
2856 const char *oldprevval2 = d->prev_val[2];
2858 if (!union_or_struct_p (nested_ptr_d->type))
2860 error_at_line (d->line,
2861 "field `%s' has invalid "
2862 "option `nested_ptr'\n", d->val);
2863 return;
2866 d->prev_val[2] = d->val;
2867 oprintf (d->of, "%*s{\n", d->indent, "");
2868 d->indent += 2;
2869 d->val = xasprintf ("x%d", d->counter++);
2870 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2871 (nested_ptr_d->type->kind == TYPE_UNION
2872 ? "union" : "struct"),
2873 nested_ptr_d->type->u.s.tag,
2874 d->fn_wants_lvalue ? "" : "const ", d->val);
2875 oprintf (d->of, "%*s", d->indent + 2, "");
2876 output_escaped_param (d, nested_ptr_d->convert_from,
2877 "nested_ptr");
2878 oprintf (d->of, ";\n");
2880 d->process_field (nested_ptr_d->type, d);
2882 if (d->fn_wants_lvalue)
2884 oprintf (d->of, "%*s%s = ", d->indent, "",
2885 d->prev_val[2]);
2886 d->prev_val[2] = d->val;
2887 output_escaped_param (d, nested_ptr_d->convert_to,
2888 "nested_ptr");
2889 oprintf (d->of, ";\n");
2892 d->indent -= 2;
2893 oprintf (d->of, "%*s}\n", d->indent, "");
2894 d->val = d->prev_val[2];
2895 d->prev_val[2] = oldprevval2;
2897 else
2898 d->process_field (t->u.p, d);
2900 else
2902 int loopcounter = d->loopcounter;
2903 const char *oldval = d->val;
2904 const char *oldprevval3 = d->prev_val[3];
2905 char *newval;
2907 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2908 d->indent += 2;
2909 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2910 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2911 "", loopcounter, loopcounter);
2912 if (!d->in_record_p)
2913 output_escaped_param (d, length, "length");
2914 else
2915 oprintf (d->of, "l%d", loopcounter);
2916 if (d->have_this_obj)
2917 /* Try to unswitch loops (see PR53880). */
2918 oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
2919 oprintf (d->of, "); i%d++) {\n", loopcounter);
2920 d->indent += 2;
2921 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2922 d->used_length = 1;
2923 d->prev_val[3] = oldval;
2924 walk_type (t->u.p, d);
2925 free (newval);
2926 d->val = oldval;
2927 d->prev_val[3] = oldprevval3;
2928 d->used_length = 0;
2929 d->indent -= 2;
2930 oprintf (d->of, "%*s}\n", d->indent, "");
2931 d->process_field (t, d);
2932 d->indent -= 2;
2933 oprintf (d->of, "%*s}\n", d->indent, "");
2935 d->in_ptr_field = false;
2937 break;
2939 case TYPE_ARRAY:
2941 int loopcounter;
2942 const char *oldval = d->val;
2943 char *newval;
2945 /* If it's an array of scalars, we optimize by not generating
2946 any code. */
2947 if (t->u.a.p->kind == TYPE_SCALAR)
2948 break;
2950 if (length)
2951 loopcounter = d->loopcounter;
2952 else
2953 loopcounter = d->counter++;
2955 /* When walking an array, compute the length and store it in a
2956 local variable before walking the array elements, instead of
2957 recomputing the length expression each time through the loop.
2958 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2959 where the length is stored in the first array element,
2960 because otherwise that operand can get overwritten on the
2961 first iteration. */
2962 oprintf (d->of, "%*s{\n", d->indent, "");
2963 d->indent += 2;
2964 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2965 if (!d->in_record_p || !length)
2967 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2968 d->indent, "", loopcounter);
2969 if (length)
2970 output_escaped_param (d, length, "length");
2971 else
2972 oprintf (d->of, "%s", t->u.a.len);
2973 oprintf (d->of, ");\n");
2976 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2977 d->indent, "",
2978 loopcounter, loopcounter, loopcounter, loopcounter);
2979 d->indent += 2;
2980 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2981 d->used_length = 1;
2982 walk_type (t->u.a.p, d);
2983 free (newval);
2984 d->used_length = 0;
2985 d->val = oldval;
2986 d->indent -= 2;
2987 oprintf (d->of, "%*s}\n", d->indent, "");
2988 d->indent -= 2;
2989 oprintf (d->of, "%*s}\n", d->indent, "");
2991 break;
2993 case TYPE_STRUCT:
2994 case TYPE_UNION:
2996 pair_p f;
2997 const char *oldval = d->val;
2998 const char *oldprevval1 = d->prev_val[1];
2999 const char *oldprevval2 = d->prev_val[2];
3000 const char *struct_mark_hook = NULL;
3001 const int union_p = t->kind == TYPE_UNION;
3002 int seen_default_p = 0;
3003 options_p o;
3004 int lengths_seen = 0;
3005 int endcounter;
3006 bool any_length_seen = false;
3008 if (!t->u.s.line.file)
3009 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
3011 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
3013 error_at_line (d->line,
3014 "structure `%s' defined for mismatching languages",
3015 t->u.s.tag);
3016 error_at_line (&t->u.s.line, "one structure defined here");
3019 /* Some things may also be defined in the structure's options. */
3020 for (o = t->u.s.opt; o; o = o->next)
3021 if (!desc && strcmp (o->name, "desc") == 0
3022 && o->kind == OPTION_STRING)
3023 desc = o->info.string;
3024 else if (!struct_mark_hook && strcmp (o->name, "mark_hook") == 0
3025 && o->kind == OPTION_STRING)
3026 struct_mark_hook = o->info.string;
3028 if (struct_mark_hook)
3029 oprintf (d->of, "%*s%s (&%s);\n",
3030 d->indent, "", struct_mark_hook, oldval);
3032 d->prev_val[2] = oldval;
3033 d->prev_val[1] = oldprevval2;
3034 if (union_p)
3036 if (desc == NULL)
3038 error_at_line (d->line,
3039 "missing `desc' option for union `%s'",
3040 t->u.s.tag);
3041 desc = "1";
3043 oprintf (d->of, "%*sswitch (", d->indent, "");
3044 output_escaped_param (d, desc, "desc");
3045 oprintf (d->of, ")\n");
3046 d->indent += 2;
3047 oprintf (d->of, "%*s{\n", d->indent, "");
3049 else if (desc)
3051 /* We have a "desc" option on a struct, signifying the
3052 base class within a GC-managed inheritance hierarchy.
3053 The current code specialcases the base class, then walks
3054 into subclasses, recursing into this routine to handle them.
3055 This organization requires the base class to have a case in
3056 the switch statement, and hence a tag value is mandatory
3057 for the base class. This restriction could be removed, but
3058 it would require some restructing of this code. */
3059 if (!type_tag)
3061 error_at_line (d->line,
3062 "missing `tag' option for type `%s'",
3063 t->u.s.tag);
3065 oprintf (d->of, "%*sswitch (", d->indent, "");
3066 output_escaped_param (d, desc, "desc");
3067 oprintf (d->of, ")\n");
3068 d->indent += 2;
3069 oprintf (d->of, "%*s{\n", d->indent, "");
3070 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
3071 d->indent += 2;
3074 FOR_ALL_INHERITED_FIELDS (t, f)
3076 options_p oo;
3077 int skip_p = 0;
3078 const char *fieldlength = NULL;
3080 d->reorder_fn = NULL;
3081 for (oo = f->opt; oo; oo = oo->next)
3082 if (strcmp (oo->name, "skip") == 0)
3083 skip_p = 1;
3084 else if (strcmp (oo->name, "length") == 0
3085 && oo->kind == OPTION_STRING)
3086 fieldlength = oo->info.string;
3088 if (skip_p)
3089 continue;
3090 if (fieldlength)
3092 lengths_seen++;
3093 d->counter++;
3094 if (!union_p)
3096 if (!any_length_seen)
3098 oprintf (d->of, "%*s{\n", d->indent, "");
3099 d->indent += 2;
3101 any_length_seen = true;
3103 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
3104 d->indent, "", d->counter - 1);
3105 output_escaped_param (d, fieldlength, "length");
3106 oprintf (d->of, ");\n");
3110 endcounter = d->counter;
3112 FOR_ALL_INHERITED_FIELDS (t, f)
3114 options_p oo;
3115 const char *dot = ".";
3116 const char *tagid = NULL;
3117 int skip_p = 0;
3118 int default_p = 0;
3119 int use_param_p = 0;
3120 const char *fieldlength = NULL;
3121 char *newval;
3123 d->reorder_fn = NULL;
3124 for (oo = f->opt; oo; oo = oo->next)
3125 if (strcmp (oo->name, "dot") == 0
3126 && oo->kind == OPTION_STRING)
3127 dot = oo->info.string;
3128 else if (strcmp (oo->name, "tag") == 0
3129 && oo->kind == OPTION_STRING)
3130 tagid = oo->info.string;
3131 else if (strcmp (oo->name, "skip") == 0)
3132 skip_p = 1;
3133 else if (strcmp (oo->name, "default") == 0)
3134 default_p = 1;
3135 else if (strcmp (oo->name, "reorder") == 0
3136 && oo->kind == OPTION_STRING)
3137 d->reorder_fn = oo->info.string;
3138 else if (strncmp (oo->name, "use_param", 9) == 0
3139 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
3140 use_param_p = 1;
3141 else if (strcmp (oo->name, "length") == 0
3142 && oo->kind == OPTION_STRING)
3143 fieldlength = oo->info.string;
3145 if (skip_p)
3146 continue;
3148 if (union_p && tagid)
3150 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
3151 d->indent += 2;
3153 else if (union_p && default_p)
3155 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3156 d->indent += 2;
3157 seen_default_p = 1;
3159 else if (!union_p && (default_p || tagid))
3160 error_at_line (d->line,
3161 "can't use `%s' outside a union on field `%s'",
3162 default_p ? "default" : "tag", f->name);
3163 else if (union_p && !(default_p || tagid)
3164 && f->type->kind == TYPE_SCALAR)
3166 fprintf (stderr,
3167 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3168 get_input_file_name (d->line->file), d->line->line,
3169 f->name);
3170 continue;
3172 else if (union_p && !(default_p || tagid))
3173 error_at_line (d->line,
3174 "field `%s' is missing `tag' or `default' option",
3175 f->name);
3177 if (fieldlength)
3179 d->loopcounter = endcounter - lengths_seen--;
3182 d->line = &f->line;
3183 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
3184 d->opt = f->opt;
3185 d->used_length = false;
3186 d->in_record_p = !union_p;
3188 if (union_p && use_param_p && d->param == NULL)
3189 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
3190 else
3191 walk_type (f->type, d);
3193 d->in_record_p = false;
3195 free (newval);
3197 if (union_p)
3199 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3200 d->indent -= 2;
3203 d->reorder_fn = NULL;
3205 d->val = oldval;
3206 d->prev_val[1] = oldprevval1;
3207 d->prev_val[2] = oldprevval2;
3209 if (union_p && !seen_default_p)
3211 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3212 oprintf (d->of, "%*s break;\n", d->indent, "");
3215 if (desc && !union_p)
3217 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3218 d->indent -= 2;
3220 if (union_p)
3222 oprintf (d->of, "%*s}\n", d->indent, "");
3223 d->indent -= 2;
3225 else if (desc)
3227 /* Add cases to handle subclasses. */
3228 walk_subclasses (t, d);
3230 /* Ensure that if someone forgets a "tag" option that we don't
3231 silent fail to traverse that subclass's fields. */
3232 if (!seen_default_p)
3234 oprintf (d->of, "%*s/* Unrecognized tag value. */\n",
3235 d->indent, "");
3236 oprintf (d->of, "%*sdefault: gcc_unreachable (); \n",
3237 d->indent, "");
3240 /* End of the switch statement */
3241 oprintf (d->of, "%*s}\n", d->indent, "");
3242 d->indent -= 2;
3244 if (any_length_seen)
3246 d->indent -= 2;
3247 oprintf (d->of, "%*s}\n", d->indent, "");
3250 break;
3252 case TYPE_LANG_STRUCT:
3254 type_p nt;
3255 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3256 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3257 break;
3258 if (nt == NULL)
3259 error_at_line (d->line, "structure `%s' differs between languages",
3260 t->u.s.tag);
3261 else
3262 walk_type (nt, d);
3264 break;
3266 case TYPE_PARAM_STRUCT:
3268 type_p *oldparam = d->param;
3270 d->param = t->u.param_struct.param;
3271 walk_type (t->u.param_struct.stru, d);
3272 d->param = oldparam;
3274 break;
3276 case TYPE_USER_STRUCT:
3277 d->process_field (t, d);
3278 break;
3280 case TYPE_NONE:
3281 case TYPE_UNDEFINED:
3282 gcc_unreachable ();
3286 /* process_field routine for marking routines. */
3288 static void
3289 write_types_process_field (type_p f, const struct walk_type_data *d)
3291 const struct write_types_data *wtd;
3292 const char *cast = d->needs_cast_p ? "(void *)" : "";
3293 wtd = (const struct write_types_data *) d->cookie;
3295 switch (f->kind)
3297 case TYPE_NONE:
3298 case TYPE_UNDEFINED:
3299 gcc_unreachable ();
3300 case TYPE_POINTER:
3301 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
3302 wtd->subfield_marker_routine, cast, d->val);
3303 if (wtd->param_prefix)
3305 if (f->u.p->kind == TYPE_SCALAR)
3306 /* The current type is a pointer to a scalar (so not
3307 considered like a pointer to instances of user defined
3308 types) and we are seeing it; it means we must be even
3309 more careful about the second argument of the
3310 SUBFIELD_MARKER_ROUTINE call. That argument must
3311 always be the instance of the type for which
3312 write_func_for_structure was called - this really is
3313 what the function SUBFIELD_MARKER_ROUTINE expects.
3314 That is, it must be an instance of the ORIG_S type
3315 parameter of write_func_for_structure. The convention
3316 is that that argument must be "x" in that case (as set
3317 by write_func_for_structure). The problem is, we can't
3318 count on d->prev_val[3] to be always set to "x" in that
3319 case. Sometimes walk_type can set it to something else
3320 (to e.g cooperate with write_array when called from
3321 write_roots). So let's set it to "x" here then. */
3322 oprintf (d->of, ", x");
3323 else
3324 oprintf (d->of, ", %s", d->prev_val[3]);
3325 if (d->orig_s)
3327 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3328 output_mangled_typename (d->of, d->orig_s);
3330 else
3331 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3333 oprintf (d->of, ");\n");
3334 if (d->reorder_fn && wtd->reorder_note_routine)
3335 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
3336 wtd->reorder_note_routine, cast, d->val,
3337 d->prev_val[3], d->reorder_fn);
3338 break;
3340 case TYPE_STRING:
3341 case TYPE_STRUCT:
3342 case TYPE_UNION:
3343 case TYPE_LANG_STRUCT:
3344 case TYPE_PARAM_STRUCT:
3345 case TYPE_USER_STRUCT:
3346 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3348 /* If F is a user-defined type and the field is not a
3349 pointer to the type, then we should not generate the
3350 standard pointer-marking code. All we need to do is call
3351 the user-provided marking function to process the fields
3352 of F. */
3353 oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3354 d->val);
3356 else
3358 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3359 output_mangled_typename (d->of, f);
3360 oprintf (d->of, " (%s%s);\n", cast, d->val);
3361 if (d->reorder_fn && wtd->reorder_note_routine)
3362 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
3363 wtd->reorder_note_routine, cast, d->val, cast, d->val,
3364 d->reorder_fn);
3366 break;
3368 case TYPE_SCALAR:
3369 break;
3371 case TYPE_ARRAY:
3372 gcc_unreachable ();
3376 /* Return an output file that is suitable for definitions which can
3377 reference struct S */
3379 static outf_p
3380 get_output_file_for_structure (const_type_p s, type_p *param)
3382 const input_file *fn;
3383 int i;
3385 gcc_assert (union_or_struct_p (s));
3386 fn = s->u.s.line.file;
3388 /* This is a hack, and not the good kind either. */
3389 for (i = NUM_PARAM - 1; i >= 0; i--)
3390 if (param && param[i] && param[i]->kind == TYPE_POINTER
3391 && union_or_struct_p (param[i]->u.p))
3392 fn = param[i]->u.p->u.s.line.file;
3394 /* The call to get_output_file_with_visibility may update fn by
3395 caching its result inside, so we need the CONST_CAST. */
3396 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3400 /* Returns the specifier keyword for a string or union type S, empty string
3401 otherwise. */
3403 static const char *
3404 get_type_specifier (const type_p s)
3406 if (s->kind == TYPE_STRUCT)
3407 return "struct ";
3408 else if (s->kind == TYPE_LANG_STRUCT)
3409 return get_type_specifier (s->u.s.lang_struct);
3410 else if (s->kind == TYPE_UNION)
3411 return "union ";
3412 return "";
3416 /* Emits a declaration for type TY (assumed to be a union or a
3417 structure) on stream OUT. */
3419 static void
3420 write_type_decl (outf_p out, type_p ty)
3422 if (union_or_struct_p (ty))
3423 oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3424 else if (ty->kind == TYPE_SCALAR)
3426 if (ty->u.scalar_is_char)
3427 oprintf (out, "const char");
3428 else
3429 oprintf (out, "void");
3431 else if (ty->kind == TYPE_POINTER)
3433 write_type_decl (out, ty->u.p);
3434 oprintf (out, " *");
3436 else if (ty->kind == TYPE_ARRAY)
3438 write_type_decl (out, ty->u.a.p);
3439 oprintf (out, " *");
3441 else if (ty->kind == TYPE_STRING)
3443 oprintf (out, "const char *");
3445 else
3446 gcc_unreachable ();
3450 /* Write on OF the name of the marker function for structure S. PREFIX
3451 is the prefix to use (to distinguish ggc from pch markers). */
3453 static void
3454 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3456 if (union_or_struct_p (s))
3458 const char *id_for_tag = filter_type_name (s->u.s.tag);
3459 oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3460 if (id_for_tag != s->u.s.tag)
3461 free (CONST_CAST (char *, id_for_tag));
3463 else if (s->kind == TYPE_PARAM_STRUCT)
3465 oprintf (of, "gt_%s_", prefix);
3466 output_mangled_typename (of, s);
3468 else
3469 gcc_unreachable ();
3472 /* Write on OF a user-callable routine to act as an entry point for
3473 the marking routine for S, generated by write_func_for_structure.
3474 PREFIX is the prefix to use to distinguish ggc and pch markers. */
3476 static void
3477 write_user_func_for_structure_ptr (outf_p of, type_p s, const char *prefix)
3479 /* Parameterized structures are not supported in user markers. There
3480 is no way for the marker function to know which specific type
3481 to use to generate the call to the void * entry point. For
3482 instance, a marker for struct htab may need to call different
3483 routines to mark the fields, depending on the paramN_is attributes.
3485 A user-defined marker that accepts 'struct htab' as its argument
3486 would not know which variant to call. Generating several entry
3487 points accepting 'struct htab' would cause multiply-defined
3488 errors during compilation. */
3489 gcc_assert (union_or_struct_p (s));
3491 type_p alias_of = NULL;
3492 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3493 if (strcmp (opt->name, "ptr_alias") == 0)
3495 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3496 we do not generate marking code for ORIG_S here. Instead, a
3497 forwarder #define in gtype-desc.h will cause every call to its
3498 marker to call the target of this alias.
3500 However, we still want to create a user entry code for the
3501 aliased type. So, if ALIAS_OF is set, we only generate the
3502 user-callable marker function. */
3503 alias_of = opt->info.type;
3504 break;
3507 oprintf (of, "\nvoid\n");
3508 oprintf (of, "gt_%sx (", prefix);
3509 write_type_decl (of, s);
3510 oprintf (of, " *& x)\n");
3511 oprintf (of, "{\n");
3512 oprintf (of, " if (x)\n ");
3513 write_marker_function_name (of, alias_of ? alias_of : s, prefix);
3514 oprintf (of, " ((void *) x);\n");
3515 oprintf (of, "}\n");
3519 /* Write a function to mark all the fields of type S on OF. PREFIX
3520 and D are as in write_user_marking_functions. */
3522 static void
3523 write_user_func_for_structure_body (type_p s, const char *prefix,
3524 struct walk_type_data *d)
3526 oprintf (d->of, "\nvoid\n");
3527 oprintf (d->of, "gt_%sx (", prefix);
3528 write_type_decl (d->of, s);
3529 oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3530 oprintf (d->of, "{\n");
3531 oprintf (d->of, " ");
3532 write_type_decl (d->of, s);
3533 oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3534 d->val = "(*x)";
3535 d->indent = 2;
3536 walk_type (s, d);
3537 oprintf (d->of, "}\n");
3541 /* Emit the user-callable functions needed to mark all the types used
3542 by the user structure S. PREFIX is the prefix to use to
3543 distinguish ggc and pch markers. D contains data needed to pass to
3544 walk_type when traversing the fields of a type.
3546 For every type T referenced by S, two routines are generated: one
3547 that takes 'T *', marks the pointer and calls the second routine,
3548 which just marks the fields of T. */
3550 static void
3551 write_user_marking_functions (type_p s, const char *prefix,
3552 struct walk_type_data *d)
3554 gcc_assert (s->kind == TYPE_USER_STRUCT);
3556 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3558 type_p fld_type = fld->type;
3559 if (fld_type->kind == TYPE_POINTER)
3561 type_p pointed_to_type = fld_type->u.p;
3562 if (union_or_struct_p (pointed_to_type))
3563 write_user_func_for_structure_ptr (d->of, pointed_to_type, prefix);
3565 else if (union_or_struct_p (fld_type))
3566 write_user_func_for_structure_body (fld_type, prefix, d);
3571 /* For S, a structure that's part of ORIG_S, and using parameters
3572 PARAM, write out a routine that:
3573 - Takes a parameter, a void * but actually of type *S
3574 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3575 field of S or its substructures and (in some cases) things
3576 that are pointed to by S. */
3578 static void
3579 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
3580 const struct write_types_data *wtd)
3582 const char *chain_next = NULL;
3583 const char *chain_prev = NULL;
3584 const char *chain_circular = NULL;
3585 const char *mark_hook_name = NULL;
3586 options_p opt;
3587 struct walk_type_data d;
3589 if (s->u.s.base_class)
3591 /* Verify that the base class has a "desc", since otherwise
3592 the traversal hooks there won't attempt to visit fields of
3593 subclasses such as this one. */
3594 const_type_p ubc = get_ultimate_base_class (s);
3595 if ((!opts_have (ubc->u.s.opt, "user")
3596 && !opts_have (ubc->u.s.opt, "desc")))
3597 error_at_line (&s->u.s.line,
3598 ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
3599 ", but '%s' lacks a discriminator 'desc' option"),
3600 s->u.s.tag, ubc->u.s.tag, ubc->u.s.tag);
3602 /* Don't write fns for subclasses, only for the ultimate base class
3603 within an inheritance hierarchy. */
3604 return;
3607 memset (&d, 0, sizeof (d));
3608 d.of = get_output_file_for_structure (s, param);
3609 for (opt = s->u.s.opt; opt; opt = opt->next)
3610 if (strcmp (opt->name, "chain_next") == 0
3611 && opt->kind == OPTION_STRING)
3612 chain_next = opt->info.string;
3613 else if (strcmp (opt->name, "chain_prev") == 0
3614 && opt->kind == OPTION_STRING)
3615 chain_prev = opt->info.string;
3616 else if (strcmp (opt->name, "chain_circular") == 0
3617 && opt->kind == OPTION_STRING)
3618 chain_circular = opt->info.string;
3619 else if (strcmp (opt->name, "mark_hook") == 0
3620 && opt->kind == OPTION_STRING)
3621 mark_hook_name = opt->info.string;
3622 if (chain_prev != NULL && chain_next == NULL)
3623 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3624 if (chain_circular != NULL && chain_next != NULL)
3625 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3626 if (chain_circular != NULL)
3627 chain_next = chain_circular;
3629 d.process_field = write_types_process_field;
3630 d.cookie = wtd;
3631 d.orig_s = orig_s;
3632 d.opt = s->u.s.opt;
3633 d.line = &s->u.s.line;
3634 d.bitmap = s->u.s.bitmap;
3635 d.param = param;
3636 d.prev_val[0] = "*x";
3637 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3638 d.prev_val[3] = "x";
3639 d.val = "(*x)";
3640 d.have_this_obj = false;
3642 oprintf (d.of, "\n");
3643 oprintf (d.of, "void\n");
3644 write_marker_function_name (d.of, orig_s, wtd->prefix);
3645 oprintf (d.of, " (void *x_p)\n");
3646 oprintf (d.of, "{\n ");
3647 write_type_decl (d.of, s);
3648 oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3649 write_type_decl (d.of, s);
3650 oprintf (d.of, " *)x_p;\n");
3651 if (chain_next != NULL)
3653 /* TYPE_USER_STRUCTs should not occur here. These structures
3654 are completely handled by user code. */
3655 gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
3657 oprintf (d.of, " ");
3658 write_type_decl (d.of, s);
3659 oprintf (d.of, " * xlimit = x;\n");
3661 if (chain_next == NULL)
3663 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3664 if (wtd->param_prefix)
3666 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3667 output_mangled_typename (d.of, orig_s);
3669 oprintf (d.of, "))\n");
3671 else
3673 if (chain_circular != NULL)
3674 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3675 else
3676 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3677 if (wtd->param_prefix)
3679 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3680 output_mangled_typename (d.of, orig_s);
3682 oprintf (d.of, "))\n");
3683 if (chain_circular != NULL)
3684 oprintf (d.of, " return;\n do\n");
3685 if (mark_hook_name && !wtd->skip_hooks)
3687 oprintf (d.of, " {\n");
3688 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
3690 oprintf (d.of, " xlimit = (");
3691 d.prev_val[2] = "*xlimit";
3692 output_escaped_param (&d, chain_next, "chain_next");
3693 oprintf (d.of, ");\n");
3694 if (mark_hook_name && !wtd->skip_hooks)
3695 oprintf (d.of, " }\n");
3696 if (chain_prev != NULL)
3698 oprintf (d.of, " if (x != xlimit)\n");
3699 oprintf (d.of, " for (;;)\n");
3700 oprintf (d.of, " {\n");
3701 oprintf (d.of, " %s %s * const xprev = (",
3702 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3704 d.prev_val[2] = "*x";
3705 output_escaped_param (&d, chain_prev, "chain_prev");
3706 oprintf (d.of, ");\n");
3707 oprintf (d.of, " if (xprev == NULL) break;\n");
3708 oprintf (d.of, " x = xprev;\n");
3709 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3710 if (wtd->param_prefix)
3712 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3713 output_mangled_typename (d.of, orig_s);
3715 oprintf (d.of, ");\n");
3716 oprintf (d.of, " }\n");
3718 if (chain_circular != NULL)
3720 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3721 if (wtd->param_prefix)
3723 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3724 output_mangled_typename (d.of, orig_s);
3726 oprintf (d.of, "));\n");
3727 if (mark_hook_name && !wtd->skip_hooks)
3728 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
3729 oprintf (d.of, " do\n");
3731 else
3732 oprintf (d.of, " while (x != xlimit)\n");
3734 oprintf (d.of, " {\n");
3735 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3737 oprintf (d.of, " %s (x);\n", mark_hook_name);
3740 d.prev_val[2] = "*x";
3741 d.indent = 6;
3742 if (orig_s->kind != TYPE_USER_STRUCT)
3743 walk_type (s, &d);
3744 else
3746 /* User structures have no fields to walk. Simply generate a call
3747 to the user-provided structure marker. */
3748 oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3751 if (chain_next != NULL)
3753 oprintf (d.of, " x = (");
3754 output_escaped_param (&d, chain_next, "chain_next");
3755 oprintf (d.of, ");\n");
3758 oprintf (d.of, " }\n");
3759 if (chain_circular != NULL)
3760 oprintf (d.of, " while (x != xlimit);\n");
3761 oprintf (d.of, "}\n");
3763 if (orig_s->kind == TYPE_USER_STRUCT)
3764 write_user_marking_functions (orig_s, wtd->prefix, &d);
3768 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3770 static void
3771 write_types (outf_p output_header, type_p structures, type_p param_structs,
3772 const struct write_types_data *wtd)
3774 int nbfun = 0; /* Count the emitted functions. */
3775 type_p s;
3777 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3779 /* We first emit the macros and the declarations. Functions' code is
3780 emitted afterwards. This is needed in plugin mode. */
3781 oprintf (output_header, "/* Macros and declarations. */\n");
3782 for (s = structures; s; s = s->next)
3783 /* Do not emit handlers for derived classes; we only ever deal with
3784 the ultimate base class within an inheritance hierarchy. */
3785 if ((s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3786 && !s->u.s.base_class)
3788 options_p opt;
3790 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3791 continue;
3793 const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3795 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3796 output_mangled_typename (output_header, s);
3797 oprintf (output_header, "(X) do { \\\n");
3798 oprintf (output_header,
3799 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3800 s_id_for_tag);
3801 oprintf (output_header, " } while (0)\n");
3803 for (opt = s->u.s.opt; opt; opt = opt->next)
3804 if (strcmp (opt->name, "ptr_alias") == 0
3805 && opt->kind == OPTION_TYPE)
3807 const_type_p const t = (const_type_p) opt->info.type;
3808 if (t->kind == TYPE_STRUCT
3809 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3811 const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3812 oprintf (output_header,
3813 "#define gt_%sx_%s gt_%sx_%s\n",
3814 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3815 if (t_id_for_tag != t->u.s.tag)
3816 free (CONST_CAST (char *, t_id_for_tag));
3818 else
3819 error_at_line (&s->u.s.line,
3820 "structure alias is not a structure");
3821 break;
3823 if (opt)
3824 continue;
3826 /* Declare the marker procedure only once. */
3827 oprintf (output_header,
3828 "extern void gt_%sx_%s (void *);\n",
3829 wtd->prefix, s_id_for_tag);
3831 if (s_id_for_tag != s->u.s.tag)
3832 free (CONST_CAST (char *, s_id_for_tag));
3834 if (s->u.s.line.file == NULL)
3836 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3837 s->u.s.tag);
3838 continue;
3842 for (s = param_structs; s; s = s->next)
3843 if (s->gc_used == GC_POINTED_TO)
3845 type_p stru = s->u.param_struct.stru;
3847 /* Declare the marker procedure. */
3848 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
3849 output_mangled_typename (output_header, s);
3850 oprintf (output_header, " (void *);\n");
3852 if (stru->u.s.line.file == NULL)
3854 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3855 stru->u.s.tag);
3856 continue;
3860 /* At last we emit the functions code. */
3861 oprintf (output_header, "\n/* functions code */\n");
3862 for (s = structures; s; s = s->next)
3863 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3865 options_p opt;
3867 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3868 continue;
3869 for (opt = s->u.s.opt; opt; opt = opt->next)
3870 if (strcmp (opt->name, "ptr_alias") == 0)
3871 break;
3872 if (opt)
3873 continue;
3875 if (s->kind == TYPE_LANG_STRUCT)
3877 type_p ss;
3878 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3880 nbfun++;
3881 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3882 nbfun, (void*) ss, ss->u.s.tag);
3883 write_func_for_structure (s, ss, NULL, wtd);
3886 else
3888 nbfun++;
3889 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3890 nbfun, (void*) s, s->u.s.tag);
3891 write_func_for_structure (s, s, NULL, wtd);
3894 else
3896 /* Structure s is not possibly pointed to, so can be ignored. */
3897 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3898 (void*)s, s->u.s.tag,
3899 (int) s->gc_used);
3902 for (s = param_structs; s; s = s->next)
3903 if (s->gc_used == GC_POINTED_TO)
3905 type_p *param = s->u.param_struct.param;
3906 type_p stru = s->u.param_struct.stru;
3907 if (stru->u.s.line.file == NULL)
3908 continue;
3909 if (stru->kind == TYPE_LANG_STRUCT)
3911 type_p ss;
3912 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3914 nbfun++;
3915 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
3916 nbfun, (void*) ss, ss->u.s.tag);
3917 write_func_for_structure (s, ss, param, wtd);
3920 else
3922 nbfun++;
3923 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
3924 nbfun, (void*) s,
3925 (void*) stru, stru->u.s.tag);
3926 write_func_for_structure (s, stru, param, wtd);
3929 else
3931 /* Param structure s is not pointed to, so should be ignored. */
3932 DBGPRINTF ("ignored s @ %p", (void*)s);
3934 if (verbosity_level >= 2)
3935 printf ("%s emitted %d routines for %s\n",
3936 progname, nbfun, wtd->comment);
3939 static const struct write_types_data ggc_wtd = {
3940 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
3941 "GC marker procedures. ",
3942 FALSE
3945 static const struct write_types_data pch_wtd = {
3946 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3947 "gt_pch_note_reorder",
3948 "PCH type-walking procedures. ",
3949 TRUE
3952 /* Write out the local pointer-walking routines. */
3954 /* process_field routine for local pointer-walking for user-callable
3955 routines. The difference between this and
3956 write_types_local_process_field is that, in this case, we do not
3957 need to check whether the given pointer matches the address of the
3958 parent structure. This check was already generated by the call
3959 to gt_pch_nx in the main gt_pch_p_*() function that is calling
3960 this code. */
3962 static void
3963 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
3965 switch (f->kind)
3967 case TYPE_POINTER:
3968 case TYPE_STRUCT:
3969 case TYPE_UNION:
3970 case TYPE_LANG_STRUCT:
3971 case TYPE_PARAM_STRUCT:
3972 case TYPE_STRING:
3973 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3974 break;
3976 case TYPE_USER_STRUCT:
3977 if (d->in_ptr_field)
3978 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3979 else
3980 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3981 d->indent, "", d->val);
3982 break;
3984 case TYPE_SCALAR:
3985 break;
3987 case TYPE_ARRAY:
3988 case TYPE_NONE:
3989 case TYPE_UNDEFINED:
3990 gcc_unreachable ();
3995 /* Write a function to PCH walk all the fields of type S on OF.
3996 D contains data needed by walk_type to recurse into the fields of S. */
3998 static void
3999 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
4001 oprintf (d->of, "\nvoid\n");
4002 oprintf (d->of, "gt_pch_nx (");
4003 write_type_decl (d->of, s);
4004 oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
4005 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4006 "\tATTRIBUTE_UNUSED void *cookie)\n");
4007 oprintf (d->of, "{\n");
4008 d->val = "(*x)";
4009 d->indent = 2;
4010 d->process_field = write_types_local_user_process_field;
4011 walk_type (s, d);
4012 oprintf (d->of, "}\n");
4016 /* Emit the user-callable functions needed to mark all the types used
4017 by the user structure S. PREFIX is the prefix to use to
4018 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
4019 chain_next option defined. D contains data needed to pass to
4020 walk_type when traversing the fields of a type.
4022 For every type T referenced by S, two routines are generated: one
4023 that takes 'T *', marks the pointer and calls the second routine,
4024 which just marks the fields of T. */
4026 static void
4027 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
4029 gcc_assert (s->kind == TYPE_USER_STRUCT);
4031 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
4033 type_p fld_type = fld->type;
4034 if (union_or_struct_p (fld_type))
4035 write_pch_user_walking_for_structure_body (fld_type, d);
4040 /* process_field routine for local pointer-walking. */
4042 static void
4043 write_types_local_process_field (type_p f, const struct walk_type_data *d)
4045 gcc_assert (d->have_this_obj);
4046 switch (f->kind)
4048 case TYPE_POINTER:
4049 case TYPE_STRUCT:
4050 case TYPE_UNION:
4051 case TYPE_LANG_STRUCT:
4052 case TYPE_PARAM_STRUCT:
4053 case TYPE_STRING:
4054 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
4055 d->prev_val[3]);
4056 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4057 break;
4059 case TYPE_USER_STRUCT:
4060 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
4061 d->prev_val[3]);
4062 if (d->in_ptr_field)
4063 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4064 else
4065 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4066 d->indent, "", d->val);
4067 break;
4069 case TYPE_SCALAR:
4070 break;
4072 case TYPE_ARRAY:
4073 case TYPE_NONE:
4074 case TYPE_UNDEFINED:
4075 gcc_unreachable ();
4080 /* For S, a structure that's part of ORIG_S, and using parameters
4081 PARAM, write out a routine that:
4082 - Is of type gt_note_pointers
4083 - Calls PROCESS_FIELD on each field of S or its substructures.
4086 static void
4087 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
4089 struct walk_type_data d;
4091 /* Don't write fns for subclasses, only for the ultimate base class
4092 within an inheritance hierarchy. */
4093 if (s->u.s.base_class)
4094 return;
4096 memset (&d, 0, sizeof (d));
4097 d.of = get_output_file_for_structure (s, param);
4098 d.process_field = write_types_local_process_field;
4099 d.opt = s->u.s.opt;
4100 d.line = &s->u.s.line;
4101 d.bitmap = s->u.s.bitmap;
4102 d.param = param;
4103 d.prev_val[0] = d.prev_val[2] = "*x";
4104 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
4105 d.prev_val[3] = "x";
4106 d.val = "(*x)";
4107 d.fn_wants_lvalue = true;
4109 oprintf (d.of, "\n");
4110 oprintf (d.of, "void\n");
4111 oprintf (d.of, "gt_pch_p_");
4112 output_mangled_typename (d.of, orig_s);
4113 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
4114 "\tvoid *x_p,\n"
4115 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4116 "\tATTRIBUTE_UNUSED void *cookie)\n");
4117 oprintf (d.of, "{\n");
4118 oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
4119 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
4120 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
4121 d.indent = 2;
4122 d.have_this_obj = true;
4124 if (s->kind != TYPE_USER_STRUCT)
4125 walk_type (s, &d);
4126 else
4128 /* User structures have no fields to walk. Simply generate a
4129 call to the user-provided PCH walker. */
4130 oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
4131 d.prev_val[3]);
4132 oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4133 d.indent, "", d.val);
4136 oprintf (d.of, "}\n");
4138 /* Write user-callable entry points for the PCH walking routines. */
4139 if (orig_s->kind == TYPE_USER_STRUCT)
4140 write_pch_user_walking_functions (s, &d);
4143 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
4145 static void
4146 write_local (outf_p output_header, type_p structures, type_p param_structs)
4148 type_p s;
4150 if (!output_header)
4151 return;
4153 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
4154 for (s = structures; s; s = s->next)
4155 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
4157 options_p opt;
4159 if (s->u.s.line.file == NULL)
4160 continue;
4161 for (opt = s->u.s.opt; opt; opt = opt->next)
4162 if (strcmp (opt->name, "ptr_alias") == 0
4163 && opt->kind == OPTION_TYPE)
4165 const_type_p const t = (const_type_p) opt->info.type;
4166 if (t->kind == TYPE_STRUCT
4167 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
4169 oprintf (output_header, "#define gt_pch_p_");
4170 output_mangled_typename (output_header, s);
4171 oprintf (output_header, " gt_pch_p_");
4172 output_mangled_typename (output_header, t);
4173 oprintf (output_header, "\n");
4175 else
4176 error_at_line (&s->u.s.line,
4177 "structure alias is not a structure");
4178 break;
4180 if (opt)
4181 continue;
4183 /* Declare the marker procedure only once. */
4184 oprintf (output_header, "extern void gt_pch_p_");
4185 output_mangled_typename (output_header, s);
4186 oprintf (output_header,
4187 "\n (void *, void *, gt_pointer_operator, void *);\n");
4189 if (s->kind == TYPE_LANG_STRUCT)
4191 type_p ss;
4192 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4193 write_local_func_for_structure (s, ss, NULL);
4195 else
4196 write_local_func_for_structure (s, s, NULL);
4199 for (s = param_structs; s; s = s->next)
4200 if (s->gc_used == GC_POINTED_TO)
4202 type_p *param = s->u.param_struct.param;
4203 type_p stru = s->u.param_struct.stru;
4205 /* Declare the marker procedure. */
4206 oprintf (output_header, "extern void gt_pch_p_");
4207 output_mangled_typename (output_header, s);
4208 oprintf (output_header,
4209 "\n (void *, void *, gt_pointer_operator, void *);\n");
4211 if (stru->u.s.line.file == NULL)
4213 fprintf (stderr, "warning: structure `%s' used but not defined\n",
4214 stru->u.s.tag);
4215 continue;
4218 if (stru->kind == TYPE_LANG_STRUCT)
4220 type_p ss;
4221 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
4222 write_local_func_for_structure (s, ss, param);
4224 else
4225 write_local_func_for_structure (s, stru, param);
4229 /* Nonzero if S is a type for which typed GC allocators should be output. */
4231 #define USED_BY_TYPED_GC_P(s) \
4232 ((s->kind == TYPE_POINTER \
4233 && (s->u.p->gc_used == GC_POINTED_TO \
4234 || s->u.p->gc_used == GC_USED)) \
4235 || (union_or_struct_p (s) \
4236 && ((s)->gc_used == GC_POINTED_TO \
4237 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4238 && s->u.s.line.file != NULL) \
4239 || ((s)->gc_used == GC_USED \
4240 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))) \
4241 || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
4245 /* Might T contain any non-pointer elements? */
4247 static int
4248 contains_scalar_p (type_p t)
4250 switch (t->kind)
4252 case TYPE_STRING:
4253 case TYPE_POINTER:
4254 return 0;
4255 case TYPE_ARRAY:
4256 return contains_scalar_p (t->u.a.p);
4257 case TYPE_USER_STRUCT:
4258 /* User-marked structures will typically contain pointers. */
4259 return 0;
4260 default:
4261 /* Could also check for structures that have no non-pointer
4262 fields, but there aren't enough of those to worry about. */
4263 return 1;
4267 /* Mangle INPF and print it to F. */
4269 static void
4270 put_mangled_filename (outf_p f, const input_file *inpf)
4272 /* The call to get_output_file_name may indirectly update fn since
4273 get_output_file_with_visibility caches its result inside, so we
4274 need the CONST_CAST. */
4275 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4276 if (!f || !name)
4277 return;
4278 for (; *name != 0; name++)
4279 if (ISALNUM (*name))
4280 oprintf (f, "%c", *name);
4281 else
4282 oprintf (f, "%c", '_');
4285 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4286 LASTNAME, and NAME are all strings to insert in various places in
4287 the resulting code. */
4289 static void
4290 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4291 const char *tname, const char *name)
4293 struct flist *fli2;
4295 for (fli2 = flp; fli2; fli2 = fli2->next)
4296 if (fli2->started_p)
4298 oprintf (fli2->f, " %s\n", lastname);
4299 oprintf (fli2->f, "};\n\n");
4302 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4303 if (fli2->started_p)
4305 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4306 int fnum;
4308 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4309 if (bitmap & 1)
4311 oprintf (base_files[fnum],
4312 "extern const struct %s gt_%s_", tname, pfx);
4313 put_mangled_filename (base_files[fnum], fli2->file);
4314 oprintf (base_files[fnum], "[];\n");
4319 size_t fnum;
4320 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4321 oprintf (base_files[fnum],
4322 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4326 for (fli2 = flp; fli2; fli2 = fli2->next)
4327 if (fli2->started_p)
4329 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4330 int fnum;
4332 fli2->started_p = 0;
4334 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4335 if (bitmap & 1)
4337 oprintf (base_files[fnum], " gt_%s_", pfx);
4338 put_mangled_filename (base_files[fnum], fli2->file);
4339 oprintf (base_files[fnum], ",\n");
4344 size_t fnum;
4345 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4347 oprintf (base_files[fnum], " NULL\n");
4348 oprintf (base_files[fnum], "};\n");
4353 /* Write the first three fields (pointer, count and stride) for
4354 root NAME to F. V and LINE are as for write_root.
4356 Return true if the entry could be written; return false on error. */
4358 static bool
4359 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4361 type_p ap;
4363 if (!v)
4365 error_at_line (line, "`%s' is too complex to be a root", name);
4366 return false;
4369 oprintf (f, " {\n");
4370 oprintf (f, " &%s,\n", name);
4371 oprintf (f, " 1");
4373 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4374 if (ap->u.a.len[0])
4375 oprintf (f, " * (%s)", ap->u.a.len);
4376 else if (ap == v->type)
4377 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4378 oprintf (f, ",\n");
4379 oprintf (f, " sizeof (%s", v->name);
4380 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4381 oprintf (f, "[0]");
4382 oprintf (f, "),\n");
4383 return true;
4386 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4387 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4388 of the caller. */
4390 static void
4391 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4392 int has_length, struct fileloc *line, const char *if_marked,
4393 bool emit_pch, type_p field_type, const char *field_name)
4395 struct pair newv;
4396 /* If the field reference is relative to V, rather than to some
4397 subcomponent of V, we can mark any subarrays with a single stride.
4398 We're effectively treating the field as a global variable in its
4399 own right. */
4400 if (v && type == v->type)
4402 newv = *v;
4403 newv.type = field_type;
4404 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4405 v = &newv;
4407 /* Otherwise, any arrays nested in the structure are too complex to
4408 handle. */
4409 else if (field_type->kind == TYPE_ARRAY)
4410 v = NULL;
4411 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4412 has_length, line, if_marked, emit_pch);
4415 /* Write out to F the table entry and any marker routines needed to
4416 mark NAME as TYPE. V can be one of three values:
4418 - null, if NAME is too complex to represent using a single
4419 count and stride. In this case, it is an error for NAME to
4420 contain any gc-ed data.
4422 - the outermost array that contains NAME, if NAME is part of an array.
4424 - the C variable that contains NAME, if NAME is not part of an array.
4426 LINE is the line of the C source that declares the root variable.
4427 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
4428 is nonzero iff we are building the root table for hash table caches. */
4430 static void
4431 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4432 struct fileloc *line, const char *if_marked, bool emit_pch)
4434 switch (type->kind)
4436 case TYPE_STRUCT:
4438 pair_p fld;
4439 for (fld = type->u.s.fields; fld; fld = fld->next)
4441 int skip_p = 0;
4442 const char *desc = NULL;
4443 options_p o;
4445 for (o = fld->opt; o; o = o->next)
4446 if (strcmp (o->name, "skip") == 0)
4447 skip_p = 1;
4448 else if (strcmp (o->name, "desc") == 0
4449 && o->kind == OPTION_STRING)
4450 desc = o->info.string;
4451 else if (strcmp (o->name, "param_is") == 0)
4453 else
4454 error_at_line (line,
4455 "field `%s' of global `%s' has unknown option `%s'",
4456 fld->name, name, o->name);
4458 if (skip_p)
4459 continue;
4460 else if (desc && fld->type->kind == TYPE_UNION)
4462 pair_p validf = NULL;
4463 pair_p ufld;
4465 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4467 const char *tag = NULL;
4468 options_p oo;
4469 for (oo = ufld->opt; oo; oo = oo->next)
4470 if (strcmp (oo->name, "tag") == 0
4471 && oo->kind == OPTION_STRING)
4472 tag = oo->info.string;
4473 if (tag == NULL || strcmp (tag, desc) != 0)
4474 continue;
4475 if (validf != NULL)
4476 error_at_line (line,
4477 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4478 name, fld->name, validf->name,
4479 name, fld->name, ufld->name, tag);
4480 validf = ufld;
4482 if (validf != NULL)
4483 write_field_root (f, v, type, name, 0, line, if_marked,
4484 emit_pch, validf->type,
4485 ACONCAT ((fld->name, ".",
4486 validf->name, NULL)));
4488 else if (desc)
4489 error_at_line (line,
4490 "global `%s.%s' has `desc' option but is not union",
4491 name, fld->name);
4492 else
4493 write_field_root (f, v, type, name, 0, line, if_marked,
4494 emit_pch, fld->type, fld->name);
4497 break;
4499 case TYPE_ARRAY:
4501 char *newname;
4502 newname = xasprintf ("%s[0]", name);
4503 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
4504 emit_pch);
4505 free (newname);
4507 break;
4509 case TYPE_USER_STRUCT:
4510 error_at_line (line, "`%s' must be a pointer type, because it is "
4511 "a GC root and its type is marked with GTY((user))",
4512 v->name);
4513 break;
4515 case TYPE_POINTER:
4517 const_type_p tp;
4519 if (!start_root_entry (f, v, name, line))
4520 return;
4522 tp = type->u.p;
4524 if (!has_length && union_or_struct_p (tp))
4526 tp = get_ultimate_base_class (tp);
4527 const char *id_for_tag = filter_type_name (tp->u.s.tag);
4528 oprintf (f, " &gt_ggc_mx_%s,\n", id_for_tag);
4529 if (emit_pch)
4530 oprintf (f, " &gt_pch_nx_%s", id_for_tag);
4531 else
4532 oprintf (f, " NULL");
4533 if (id_for_tag != tp->u.s.tag)
4534 free (CONST_CAST (char *, id_for_tag));
4536 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
4538 oprintf (f, " &gt_ggc_m_");
4539 output_mangled_typename (f, tp);
4540 if (emit_pch)
4542 oprintf (f, ",\n &gt_pch_n_");
4543 output_mangled_typename (f, tp);
4545 else
4546 oprintf (f, ",\n NULL");
4548 else if (has_length
4549 && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
4551 oprintf (f, " &gt_ggc_ma_%s,\n", name);
4552 if (emit_pch)
4553 oprintf (f, " &gt_pch_na_%s", name);
4554 else
4555 oprintf (f, " NULL");
4557 else
4559 error_at_line (line,
4560 "global `%s' is pointer to unimplemented type",
4561 name);
4563 if (if_marked)
4564 oprintf (f, ",\n &%s", if_marked);
4565 oprintf (f, "\n },\n");
4567 break;
4569 case TYPE_STRING:
4571 if (!start_root_entry (f, v, name, line))
4572 return;
4574 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
4575 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
4576 oprintf (f, " },\n");
4578 break;
4580 case TYPE_SCALAR:
4581 break;
4583 case TYPE_NONE:
4584 case TYPE_UNDEFINED:
4585 case TYPE_UNION:
4586 case TYPE_LANG_STRUCT:
4587 case TYPE_PARAM_STRUCT:
4588 error_at_line (line, "global `%s' is unimplemented type", name);
4592 /* This generates a routine to walk an array. */
4594 static void
4595 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4597 struct walk_type_data d;
4598 char *prevval3;
4600 memset (&d, 0, sizeof (d));
4601 d.of = f;
4602 d.cookie = wtd;
4603 d.indent = 2;
4604 d.line = &v->line;
4605 d.opt = v->opt;
4606 d.bitmap = get_lang_bitmap (v->line.file);
4607 d.param = NULL;
4609 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4611 if (wtd->param_prefix)
4613 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4614 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
4615 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4616 wtd->param_prefix, v->name);
4617 oprintf (d.of,
4618 " ATTRIBUTE_UNUSED void *x_p,\n"
4619 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4620 " ATTRIBUTE_UNUSED void * cookie)\n");
4621 oprintf (d.of, "{\n");
4622 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4623 d.process_field = write_types_local_process_field;
4624 d.have_this_obj = true;
4625 walk_type (v->type, &d);
4626 oprintf (f, "}\n\n");
4629 d.opt = v->opt;
4630 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4631 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4632 wtd->prefix, v->name);
4633 oprintf (f, "{\n");
4634 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4635 d.process_field = write_types_process_field;
4636 d.have_this_obj = false;
4637 walk_type (v->type, &d);
4638 free (prevval3);
4639 oprintf (f, "}\n\n");
4642 /* Output a table describing the locations and types of VARIABLES. */
4644 static void
4645 write_roots (pair_p variables, bool emit_pch)
4647 pair_p v;
4648 struct flist *flp = NULL;
4650 for (v = variables; v; v = v->next)
4652 outf_p f =
4653 get_output_file_with_visibility (CONST_CAST (input_file*,
4654 v->line.file));
4655 struct flist *fli;
4656 const char *length = NULL;
4657 int deletable_p = 0;
4658 options_p o;
4659 for (o = v->opt; o; o = o->next)
4660 if (strcmp (o->name, "length") == 0
4661 && o->kind == OPTION_STRING)
4662 length = o->info.string;
4663 else if (strcmp (o->name, "deletable") == 0)
4664 deletable_p = 1;
4665 else if (strcmp (o->name, "param_is") == 0)
4667 else if (strncmp (o->name, "param", 5) == 0
4668 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
4670 else if (strcmp (o->name, "if_marked") == 0)
4672 else
4673 error_at_line (&v->line,
4674 "global `%s' has unknown option `%s'",
4675 v->name, o->name);
4677 for (fli = flp; fli; fli = fli->next)
4678 if (fli->f == f && f)
4679 break;
4680 if (fli == NULL)
4682 fli = XNEW (struct flist);
4683 fli->f = f;
4684 fli->next = flp;
4685 fli->started_p = 0;
4686 fli->file = v->line.file;
4687 gcc_assert (fli->file);
4688 flp = fli;
4690 oprintf (f, "\n/* GC roots. */\n\n");
4693 if (!deletable_p
4694 && length
4695 && v->type->kind == TYPE_POINTER
4696 && (v->type->u.p->kind == TYPE_POINTER
4697 || v->type->u.p->kind == TYPE_STRUCT))
4699 write_array (f, v, &ggc_wtd);
4700 write_array (f, v, &pch_wtd);
4704 for (v = variables; v; v = v->next)
4706 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4707 v->line.file));
4708 struct flist *fli;
4709 int skip_p = 0;
4710 int length_p = 0;
4711 options_p o;
4713 for (o = v->opt; o; o = o->next)
4714 if (strcmp (o->name, "length") == 0)
4715 length_p = 1;
4716 else if (strcmp (o->name, "deletable") == 0
4717 || strcmp (o->name, "if_marked") == 0)
4718 skip_p = 1;
4720 if (skip_p)
4721 continue;
4723 for (fli = flp; fli; fli = fli->next)
4724 if (fli->f == f)
4725 break;
4726 if (!fli->started_p)
4728 fli->started_p = 1;
4730 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4731 put_mangled_filename (f, v->line.file);
4732 oprintf (f, "[] = {\n");
4735 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4738 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4739 "gt_ggc_rtab");
4741 for (v = variables; v; v = v->next)
4743 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4744 v->line.file));
4745 struct flist *fli;
4746 int skip_p = 1;
4747 options_p o;
4749 for (o = v->opt; o; o = o->next)
4750 if (strcmp (o->name, "deletable") == 0)
4751 skip_p = 0;
4752 else if (strcmp (o->name, "if_marked") == 0)
4753 skip_p = 1;
4755 if (skip_p)
4756 continue;
4758 for (fli = flp; fli; fli = fli->next)
4759 if (fli->f == f)
4760 break;
4761 if (!fli->started_p)
4763 fli->started_p = 1;
4765 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4766 put_mangled_filename (f, v->line.file);
4767 oprintf (f, "[] = {\n");
4770 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4771 v->name, v->name);
4774 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4775 "gt_ggc_deletable_rtab");
4777 for (v = variables; v; v = v->next)
4779 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4780 v->line.file));
4781 struct flist *fli;
4782 const char *if_marked = NULL;
4783 int length_p = 0;
4784 options_p o;
4786 for (o = v->opt; o; o = o->next)
4787 if (strcmp (o->name, "length") == 0)
4788 length_p = 1;
4789 else if (strcmp (o->name, "if_marked") == 0
4790 && o->kind == OPTION_STRING)
4791 if_marked = o->info.string;
4792 if (if_marked == NULL)
4793 continue;
4794 if (v->type->kind != TYPE_POINTER
4795 || v->type->u.p->kind != TYPE_PARAM_STRUCT
4796 || v->type->u.p->u.param_struct.stru != find_structure ("htab",
4797 TYPE_STRUCT))
4799 error_at_line (&v->line,
4800 "if_marked option used but not hash table");
4801 continue;
4804 for (fli = flp; fli; fli = fli->next)
4805 if (fli->f == f)
4806 break;
4807 if (!fli->started_p)
4809 fli->started_p = 1;
4811 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4812 put_mangled_filename (f, v->line.file);
4813 oprintf (f, "[] = {\n");
4816 write_root (f, v, v->type->u.p->u.param_struct.param[0],
4817 v->name, length_p, &v->line, if_marked, emit_pch);
4820 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4821 "gt_ggc_cache_rtab");
4823 if (!emit_pch)
4824 return;
4826 for (v = variables; v; v = v->next)
4828 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4829 v->line.file));
4830 struct flist *fli;
4831 int length_p = 0;
4832 int if_marked_p = 0;
4833 options_p o;
4835 for (o = v->opt; o; o = o->next)
4836 if (strcmp (o->name, "length") == 0)
4837 length_p = 1;
4838 else if (strcmp (o->name, "if_marked") == 0)
4839 if_marked_p = 1;
4841 if (!if_marked_p)
4842 continue;
4844 for (fli = flp; fli; fli = fli->next)
4845 if (fli->f == f)
4846 break;
4847 if (!fli->started_p)
4849 fli->started_p = 1;
4851 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4852 put_mangled_filename (f, v->line.file);
4853 oprintf (f, "[] = {\n");
4856 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4859 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4860 "gt_pch_cache_rtab");
4862 for (v = variables; v; v = v->next)
4864 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4865 v->line.file));
4866 struct flist *fli;
4867 int skip_p = 0;
4868 options_p o;
4870 for (o = v->opt; o; o = o->next)
4871 if (strcmp (o->name, "deletable") == 0
4872 || strcmp (o->name, "if_marked") == 0)
4874 skip_p = 1;
4875 break;
4878 if (skip_p)
4879 continue;
4881 if (!contains_scalar_p (v->type))
4882 continue;
4884 for (fli = flp; fli; fli = fli->next)
4885 if (fli->f == f)
4886 break;
4887 if (!fli->started_p)
4889 fli->started_p = 1;
4891 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4892 put_mangled_filename (f, v->line.file);
4893 oprintf (f, "[] = {\n");
4896 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4897 v->name, v->name);
4900 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4901 "gt_pch_scalar_rtab");
4904 /* TRUE if type S has the GTY variable_size annotation. */
4906 static bool
4907 variable_size_p (const type_p s)
4909 options_p o;
4910 for (o = s->u.s.opt; o; o = o->next)
4911 if (strcmp (o->name, "variable_size") == 0)
4912 return true;
4913 return false;
4916 enum alloc_quantity
4917 { single, vector };
4919 /* Writes one typed allocator definition into output F for type
4920 identifier TYPE_NAME with optional type specifier TYPE_SPECIFIER.
4921 The allocator name will contain ALLOCATOR_TYPE. If VARIABLE_SIZE
4922 is true, the allocator will have an extra parameter specifying
4923 number of bytes to allocate. If QUANTITY is set to VECTOR, a
4924 vector allocator will be output. */
4926 static void
4927 write_typed_alloc_def (outf_p f,
4928 bool variable_size, const char *type_specifier,
4929 const char *type_name, const char *allocator_type,
4930 enum alloc_quantity quantity)
4932 bool two_args = variable_size && (quantity == vector);
4933 gcc_assert (f != NULL);
4934 const char *type_name_as_id = filter_type_name (type_name);
4935 oprintf (f, "#define ggc_alloc_%s%s", allocator_type, type_name_as_id);
4936 oprintf (f, "(%s%s%s) ",
4937 (variable_size ? "SIZE" : ""),
4938 (two_args ? ", " : ""),
4939 (quantity == vector) ? "n" : "");
4940 oprintf (f, "((%s%s *)", type_specifier, type_name);
4941 oprintf (f, "(ggc_internal_%salloc_stat (", allocator_type);
4942 if (variable_size)
4943 oprintf (f, "SIZE");
4944 else
4945 oprintf (f, "sizeof (%s%s)", type_specifier, type_name);
4946 if (quantity == vector)
4947 oprintf (f, ", n");
4948 oprintf (f, " MEM_STAT_INFO)))\n");
4949 if (type_name_as_id != type_name)
4950 free (CONST_CAST (char *, type_name_as_id));
4953 /* Writes a typed allocator definition into output F for a struct or
4954 union S, with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4956 static void
4957 write_typed_struct_alloc_def (outf_p f,
4958 const type_p s, const char *allocator_type,
4959 enum alloc_quantity quantity)
4961 gcc_assert (union_or_struct_p (s));
4962 write_typed_alloc_def (f, variable_size_p (s), get_type_specifier (s),
4963 s->u.s.tag, allocator_type, quantity);
4966 /* Writes a typed allocator definition into output F for a typedef P,
4967 with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4969 static void
4970 write_typed_typedef_alloc_def (outf_p f,
4971 const pair_p p, const char *allocator_type,
4972 enum alloc_quantity quantity)
4974 write_typed_alloc_def (f, variable_size_p (p->type), "", p->name,
4975 allocator_type, quantity);
4978 /* Writes typed allocator definitions into output F for the types in
4979 STRUCTURES and TYPEDEFS that are used by GC. */
4981 static void
4982 write_typed_alloc_defns (outf_p f,
4983 const type_p structures, const pair_p typedefs)
4985 type_p s;
4986 pair_p p;
4988 gcc_assert (f != NULL);
4989 oprintf (f,
4990 "\n/* Allocators for known structs and unions. */\n\n");
4991 for (s = structures; s; s = s->next)
4993 if (!USED_BY_TYPED_GC_P (s))
4994 continue;
4995 gcc_assert (union_or_struct_p (s));
4996 /* In plugin mode onput output ggc_alloc macro definitions
4997 relevant to plugin input files. */
4998 if (nb_plugin_files > 0
4999 && ((s->u.s.line.file == NULL) || !s->u.s.line.file->inpisplugin))
5000 continue;
5001 write_typed_struct_alloc_def (f, s, "", single);
5002 write_typed_struct_alloc_def (f, s, "cleared_", single);
5003 write_typed_struct_alloc_def (f, s, "vec_", vector);
5004 write_typed_struct_alloc_def (f, s, "cleared_vec_", vector);
5007 oprintf (f, "\n/* Allocators for known typedefs. */\n");
5008 for (p = typedefs; p; p = p->next)
5010 s = p->type;
5011 if (!USED_BY_TYPED_GC_P (s) || (strcmp (p->name, s->u.s.tag) == 0))
5012 continue;
5013 /* In plugin mode onput output ggc_alloc macro definitions
5014 relevant to plugin input files. */
5015 if (nb_plugin_files > 0)
5017 struct fileloc* filoc = type_fileloc (s);
5018 if (!filoc || !filoc->file->inpisplugin)
5019 continue;
5021 write_typed_typedef_alloc_def (f, p, "", single);
5022 write_typed_typedef_alloc_def (f, p, "cleared_", single);
5023 write_typed_typedef_alloc_def (f, p, "vec_", vector);
5024 write_typed_typedef_alloc_def (f, p, "cleared_vec_", vector);
5028 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
5029 guaranteee for somewhat increased readability. If name conflicts do happen,
5030 this funcion will have to be adjusted to be more like
5031 output_mangled_typename. */
5033 static void
5034 output_typename (outf_p of, const_type_p t)
5036 switch (t->kind)
5038 case TYPE_STRING:
5039 oprintf (of, "str");
5040 break;
5041 case TYPE_SCALAR:
5042 oprintf (of, "scalar");
5043 break;
5044 case TYPE_POINTER:
5045 output_typename (of, t->u.p);
5046 break;
5047 case TYPE_STRUCT:
5048 case TYPE_USER_STRUCT:
5049 case TYPE_UNION:
5050 case TYPE_LANG_STRUCT:
5051 oprintf (of, "%s", t->u.s.tag);
5052 break;
5053 case TYPE_PARAM_STRUCT:
5055 int i;
5056 for (i = 0; i < NUM_PARAM; i++)
5057 if (t->u.param_struct.param[i] != NULL)
5059 output_typename (of, t->u.param_struct.param[i]);
5060 oprintf (of, "_");
5062 output_typename (of, t->u.param_struct.stru);
5063 break;
5065 case TYPE_NONE:
5066 case TYPE_UNDEFINED:
5067 case TYPE_ARRAY:
5068 gcc_unreachable ();
5072 /* Writes a typed GC allocator for type S that is suitable as a callback for
5073 the splay tree implementation in libiberty. */
5075 static void
5076 write_splay_tree_allocator_def (const_type_p s)
5078 outf_p of = get_output_file_with_visibility (NULL);
5079 oprintf (of, "void * ggc_alloc_splay_tree_");
5080 output_typename (of, s);
5081 oprintf (of, " (int sz, void * nl)\n");
5082 oprintf (of, "{\n");
5083 oprintf (of, " return ggc_splay_alloc (sz, nl);\n");
5084 oprintf (of, "}\n\n");
5087 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
5088 for the splay tree implementation in libiberty. */
5090 static void
5091 write_splay_tree_allocators (const_type_p param_structs)
5093 const_type_p s;
5095 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
5096 for (s = param_structs; s; s = s->next)
5097 if (s->gc_used == GC_POINTED_TO)
5099 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
5100 output_typename (header_file, s);
5101 oprintf (header_file, " (int, void *);\n");
5102 write_splay_tree_allocator_def (s);
5106 #define INDENT 2
5108 /* Dumps the value of typekind KIND. */
5110 static void
5111 dump_typekind (int indent, enum typekind kind)
5113 printf ("%*ckind = ", indent, ' ');
5114 switch (kind)
5116 case TYPE_SCALAR:
5117 printf ("TYPE_SCALAR");
5118 break;
5119 case TYPE_STRING:
5120 printf ("TYPE_STRING");
5121 break;
5122 case TYPE_STRUCT:
5123 printf ("TYPE_STRUCT");
5124 break;
5125 case TYPE_UNDEFINED:
5126 printf ("TYPE_UNDEFINED");
5127 break;
5128 case TYPE_USER_STRUCT:
5129 printf ("TYPE_USER_STRUCT");
5130 break;
5131 case TYPE_UNION:
5132 printf ("TYPE_UNION");
5133 break;
5134 case TYPE_POINTER:
5135 printf ("TYPE_POINTER");
5136 break;
5137 case TYPE_ARRAY:
5138 printf ("TYPE_ARRAY");
5139 break;
5140 case TYPE_LANG_STRUCT:
5141 printf ("TYPE_LANG_STRUCT");
5142 break;
5143 case TYPE_PARAM_STRUCT:
5144 printf ("TYPE_PARAM_STRUCT");
5145 break;
5146 default:
5147 gcc_unreachable ();
5149 printf ("\n");
5152 /* Dumps the value of GC_USED flag. */
5154 static void
5155 dump_gc_used (int indent, enum gc_used_enum gc_used)
5157 printf ("%*cgc_used = ", indent, ' ');
5158 switch (gc_used)
5160 case GC_UNUSED:
5161 printf ("GC_UNUSED");
5162 break;
5163 case GC_USED:
5164 printf ("GC_USED");
5165 break;
5166 case GC_MAYBE_POINTED_TO:
5167 printf ("GC_MAYBE_POINTED_TO");
5168 break;
5169 case GC_POINTED_TO:
5170 printf ("GC_POINTED_TO");
5171 break;
5172 default:
5173 gcc_unreachable ();
5175 printf ("\n");
5178 /* Dumps the type options OPT. */
5180 static void
5181 dump_options (int indent, options_p opt)
5183 options_p o;
5184 printf ("%*coptions = ", indent, ' ');
5185 o = opt;
5186 while (o)
5188 switch (o->kind)
5190 case OPTION_STRING:
5191 printf ("%s:string %s ", o->name, o->info.string);
5192 break;
5193 case OPTION_TYPE:
5194 printf ("%s:type ", o->name);
5195 dump_type (indent+1, o->info.type);
5196 break;
5197 case OPTION_NESTED:
5198 printf ("%s:nested ", o->name);
5199 break;
5200 case OPTION_NONE:
5201 gcc_unreachable ();
5203 o = o->next;
5205 printf ("\n");
5208 /* Dumps the source file location in LINE. */
5210 static void
5211 dump_fileloc (int indent, struct fileloc line)
5213 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
5214 get_input_file_name (line.file),
5215 line.line);
5218 /* Recursively dumps the struct, union, or a language-specific
5219 struct T. */
5221 static void
5222 dump_type_u_s (int indent, type_p t)
5224 pair_p fields;
5226 gcc_assert (union_or_struct_p (t));
5227 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
5228 dump_fileloc (indent, t->u.s.line);
5229 printf ("%*cu.s.fields =\n", indent, ' ');
5230 fields = t->u.s.fields;
5231 while (fields)
5233 dump_pair (indent + INDENT, fields);
5234 fields = fields->next;
5236 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
5237 dump_options (indent, t->u.s.opt);
5238 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
5239 if (t->kind == TYPE_LANG_STRUCT)
5241 printf ("%*cu.s.lang_struct:\n", indent, ' ');
5242 dump_type_list (indent + INDENT, t->u.s.lang_struct);
5246 /* Recursively dumps the array T. */
5248 static void
5249 dump_type_u_a (int indent, type_p t)
5251 gcc_assert (t->kind == TYPE_ARRAY);
5252 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
5253 dump_type_list (indent + INDENT, t->u.a.p);
5256 /* Recursively dumps the parameterized struct T. */
5258 static void
5259 dump_type_u_param_struct (int indent, type_p t)
5261 int i;
5262 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
5263 printf ("%*cu.param_struct.stru:\n", indent, ' ');
5264 dump_type_list (indent, t->u.param_struct.stru);
5265 dump_fileloc (indent, t->u.param_struct.line);
5266 for (i = 0; i < NUM_PARAM; i++)
5268 if (t->u.param_struct.param[i] == NULL)
5269 continue;
5270 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
5271 dump_type (indent + INDENT, t->u.param_struct.param[i]);
5275 /* Recursively dumps the type list T. */
5277 static void
5278 dump_type_list (int indent, type_p t)
5280 type_p p = t;
5281 while (p)
5283 dump_type (indent, p);
5284 p = p->next;
5288 static htab_t seen_types;
5290 /* Recursively dumps the type T if it was not dumped previously. */
5292 static void
5293 dump_type (int indent, type_p t)
5295 PTR *slot;
5297 if (seen_types == NULL)
5298 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
5300 printf ("%*cType at %p: ", indent, ' ', (void *) t);
5301 slot = htab_find_slot (seen_types, t, INSERT);
5302 if (*slot != NULL)
5304 printf ("already seen.\n");
5305 return;
5307 *slot = t;
5308 printf ("\n");
5310 dump_typekind (indent, t->kind);
5311 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
5312 (void *) t->pointer_to);
5313 dump_gc_used (indent + INDENT, t->gc_used);
5314 switch (t->kind)
5316 case TYPE_SCALAR:
5317 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
5318 t->u.scalar_is_char ? "true" : "false");
5319 break;
5320 case TYPE_STRING:
5321 break;
5322 case TYPE_STRUCT:
5323 case TYPE_UNION:
5324 case TYPE_LANG_STRUCT:
5325 case TYPE_USER_STRUCT:
5326 dump_type_u_s (indent + INDENT, t);
5327 break;
5328 case TYPE_POINTER:
5329 printf ("%*cp:\n", indent + INDENT, ' ');
5330 dump_type (indent + INDENT, t->u.p);
5331 break;
5332 case TYPE_ARRAY:
5333 dump_type_u_a (indent + INDENT, t);
5334 break;
5335 case TYPE_PARAM_STRUCT:
5336 dump_type_u_param_struct (indent + INDENT, t);
5337 break;
5338 default:
5339 gcc_unreachable ();
5341 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
5344 /* Dumps the pair P. */
5346 static void
5347 dump_pair (int indent, pair_p p)
5349 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
5350 dump_type (indent, p->type);
5351 dump_fileloc (indent, p->line);
5352 dump_options (indent, p->opt);
5353 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
5356 /* Dumps the list of pairs PP. */
5358 static void
5359 dump_pair_list (const char *name, pair_p pp)
5361 pair_p p;
5362 printf ("%s:\n", name);
5363 for (p = pp; p != NULL; p = p->next)
5364 dump_pair (0, p);
5365 printf ("End of %s\n\n", name);
5368 /* Dumps the STRUCTURES. */
5370 static void
5371 dump_structures (const char *name, type_p structures)
5373 printf ("%s:\n", name);
5374 dump_type_list (0, structures);
5375 printf ("End of %s\n\n", name);
5378 /* Dumps the internal structures of gengtype. This is useful to debug
5379 gengtype itself, or to understand what it does, e.g. for plugin
5380 developers. */
5382 static void
5383 dump_everything (void)
5385 dump_pair_list ("typedefs", typedefs);
5386 dump_structures ("structures", structures);
5387 dump_structures ("param_structs", param_structs);
5388 dump_pair_list ("variables", variables);
5390 /* Allocated with the first call to dump_type. */
5391 htab_delete (seen_types);
5396 /* Option specification for getopt_long. */
5397 static const struct option gengtype_long_options[] = {
5398 {"help", no_argument, NULL, 'h'},
5399 {"version", no_argument, NULL, 'V'},
5400 {"verbose", no_argument, NULL, 'v'},
5401 {"dump", no_argument, NULL, 'd'},
5402 {"debug", no_argument, NULL, 'D'},
5403 {"plugin", required_argument, NULL, 'P'},
5404 {"srcdir", required_argument, NULL, 'S'},
5405 {"backupdir", required_argument, NULL, 'B'},
5406 {"inputs", required_argument, NULL, 'I'},
5407 {"read-state", required_argument, NULL, 'r'},
5408 {"write-state", required_argument, NULL, 'w'},
5409 /* Terminating NULL placeholder. */
5410 {NULL, no_argument, NULL, 0},
5414 static void
5415 print_usage (void)
5417 printf ("Usage: %s\n", progname);
5418 printf ("\t -h | --help " " \t# Give this help.\n");
5419 printf ("\t -D | --debug "
5420 " \t# Give debug output to debug %s itself.\n", progname);
5421 printf ("\t -V | --version " " \t# Give version information.\n");
5422 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5423 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5424 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5425 " \t# Generate for plugin.\n");
5426 printf ("\t -S | --srcdir <GCC-directory> "
5427 " \t# Specify the GCC source directory.\n");
5428 printf ("\t -B | --backupdir <directory> "
5429 " \t# Specify the backup directory for updated files.\n");
5430 printf ("\t -I | --inputs <input-list> "
5431 " \t# Specify the file with source files list.\n");
5432 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5433 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5436 static void
5437 print_version (void)
5439 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5440 printf ("Report bugs: %s\n", bug_report_url);
5443 /* Parse the program options using getopt_long... */
5444 static void
5445 parse_program_options (int argc, char **argv)
5447 int opt = -1;
5448 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5449 gengtype_long_options, NULL)) >= 0)
5451 switch (opt)
5453 case 'h': /* --help */
5454 print_usage ();
5455 break;
5456 case 'V': /* --version */
5457 print_version ();
5458 break;
5459 case 'd': /* --dump */
5460 do_dump = 1;
5461 break;
5462 case 'D': /* --debug */
5463 do_debug = 1;
5464 break;
5465 case 'v': /* --verbose */
5466 verbosity_level++;
5467 break;
5468 case 'P': /* --plugin */
5469 if (optarg)
5470 plugin_output_filename = optarg;
5471 else
5472 fatal ("missing plugin output file name");
5473 break;
5474 case 'S': /* --srcdir */
5475 if (optarg)
5476 srcdir = optarg;
5477 else
5478 fatal ("missing source directory");
5479 srcdir_len = strlen (srcdir);
5480 break;
5481 case 'B': /* --backupdir */
5482 if (optarg)
5483 backup_dir = optarg;
5484 else
5485 fatal ("missing backup directory");
5486 break;
5487 case 'I': /* --inputs */
5488 if (optarg)
5489 inputlist = optarg;
5490 else
5491 fatal ("missing input list");
5492 break;
5493 case 'r': /* --read-state */
5494 if (optarg)
5495 read_state_filename = optarg;
5496 else
5497 fatal ("missing read state file");
5498 DBGPRINTF ("read state %s\n", optarg);
5499 break;
5500 case 'w': /* --write-state */
5501 DBGPRINTF ("write state %s\n", optarg);
5502 if (optarg)
5503 write_state_filename = optarg;
5504 else
5505 fatal ("missing write state file");
5506 break;
5507 default:
5508 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5509 print_usage ();
5510 fatal ("unexpected flag");
5513 if (plugin_output_filename)
5515 /* In plugin mode we require some input files. */
5516 int i = 0;
5517 if (optind >= argc)
5518 fatal ("no source files given in plugin mode");
5519 nb_plugin_files = argc - optind;
5520 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5521 for (i = 0; i < (int) nb_plugin_files; i++)
5523 char *name = argv[i + optind];
5524 plugin_files[i] = input_file_by_name (name);
5531 /******* Manage input files. ******/
5533 /* Hash table of unique input file names. */
5534 static htab_t input_file_htab;
5536 /* Find or allocate a new input_file by hash-consing it. */
5537 input_file*
5538 input_file_by_name (const char* name)
5540 PTR* slot;
5541 input_file* f = NULL;
5542 int namlen = 0;
5543 if (!name)
5544 return NULL;
5545 namlen = strlen (name);
5546 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5547 f->inpbitmap = 0;
5548 f->inpoutf = NULL;
5549 f->inpisplugin = false;
5550 strcpy (f->inpname, name);
5551 slot = htab_find_slot (input_file_htab, f, INSERT);
5552 gcc_assert (slot != NULL);
5553 if (*slot)
5555 /* Already known input file. */
5556 free (f);
5557 return (input_file*)(*slot);
5559 /* New input file. */
5560 *slot = f;
5561 return f;
5564 /* Hash table support routines for input_file-s. */
5565 static hashval_t
5566 htab_hash_inputfile (const void *p)
5568 const input_file *inpf = (const input_file *) p;
5569 gcc_assert (inpf);
5570 return htab_hash_string (get_input_file_name (inpf));
5573 static int
5574 htab_eq_inputfile (const void *x, const void *y)
5576 const input_file *inpfx = (const input_file *) x;
5577 const input_file *inpfy = (const input_file *) y;
5578 gcc_assert (inpfx != NULL && inpfy != NULL);
5579 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5584 main (int argc, char **argv)
5586 size_t i;
5587 static struct fileloc pos = { NULL, 0 };
5588 outf_p output_header;
5590 /* Mandatory common initializations. */
5591 progname = "gengtype"; /* For fatal and messages. */
5592 /* Create the hash-table used to hash-cons input files. */
5593 input_file_htab =
5594 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5595 /* Initialize our special input files. */
5596 this_file = input_file_by_name (__FILE__);
5597 system_h_file = input_file_by_name ("system.h");
5598 /* Set the scalar_is_char union number for predefined scalar types. */
5599 scalar_nonchar.u.scalar_is_char = FALSE;
5600 scalar_char.u.scalar_is_char = TRUE;
5602 parse_program_options (argc, argv);
5604 #if ENABLE_CHECKING
5605 if (do_debug)
5607 time_t now = (time_t) 0;
5608 time (&now);
5609 DBGPRINTF ("gengtype started pid %d at %s",
5610 (int) getpid (), ctime (&now));
5612 #endif /* ENABLE_CHECKING */
5614 /* Parse the input list and the input files. */
5615 DBGPRINTF ("inputlist %s", inputlist);
5616 if (read_state_filename)
5618 if (inputlist)
5619 fatal ("input list %s cannot be given with a read state file %s",
5620 inputlist, read_state_filename);
5621 read_state (read_state_filename);
5622 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5623 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs);
5625 else if (inputlist)
5627 /* These types are set up with #define or else outside of where
5628 we can see them. We should initialize them before calling
5629 read_input_list. */
5630 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5631 Call;} while (0)
5632 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5633 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5634 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5635 POS_HERE (do_scalar_typedef ("double_int", &pos));
5636 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5637 POS_HERE (do_scalar_typedef ("uint8", &pos));
5638 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5639 POS_HERE (do_scalar_typedef ("jword", &pos));
5640 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5641 POS_HERE (do_scalar_typedef ("void", &pos));
5642 POS_HERE (do_typedef ("PTR",
5643 create_pointer (resolve_typedef ("void", &pos)),
5644 &pos));
5645 #undef POS_HERE
5646 read_input_list (inputlist);
5647 for (i = 0; i < num_gt_files; i++)
5649 parse_file (get_input_file_name (gt_files[i]));
5650 DBGPRINTF ("parsed file #%d %s",
5651 (int) i, get_input_file_name (gt_files[i]));
5653 if (verbosity_level >= 1)
5654 printf ("%s parsed %d files with %d GTY types\n",
5655 progname, (int) num_gt_files, type_count);
5657 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5658 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
5661 else
5662 fatal ("either an input list or a read state file should be given");
5663 if (hit_error)
5664 return 1;
5667 if (plugin_output_filename)
5669 size_t ix = 0;
5670 /* In plugin mode, we should have read a state file, and have
5671 given at least one plugin file. */
5672 if (!read_state_filename)
5673 fatal ("No read state given in plugin mode for %s",
5674 plugin_output_filename);
5676 if (nb_plugin_files == 0 || !plugin_files)
5677 fatal ("No plugin files given in plugin mode for %s",
5678 plugin_output_filename);
5680 /* Parse our plugin files and augment the state. */
5681 for (ix = 0; ix < nb_plugin_files; ix++)
5683 input_file* pluginput = plugin_files [ix];
5684 pluginput->inpisplugin = true;
5685 parse_file (get_input_file_name (pluginput));
5687 if (hit_error)
5688 return 1;
5690 plugin_output = create_file ("GCC", plugin_output_filename);
5691 DBGPRINTF ("created plugin_output %p named %s",
5692 (void *) plugin_output, plugin_output->name);
5694 else
5695 { /* No plugin files, we are in normal mode. */
5696 if (!srcdir)
5697 fatal ("gengtype needs a source directory in normal mode");
5699 if (hit_error)
5700 return 1;
5702 gen_rtx_next ();
5704 /* The call to set_gc_used may indirectly call find_param_structure
5705 hence enlarge the param_structs list of types. */
5706 set_gc_used (variables);
5708 /* The state at this point is read from the state input file or by
5709 parsing source files and optionally augmented by parsing plugin
5710 source files. Write it now. */
5711 if (write_state_filename)
5713 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5714 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs);
5716 if (hit_error)
5717 fatal ("didn't write state file %s after errors",
5718 write_state_filename);
5720 DBGPRINTF ("before write_state %s", write_state_filename);
5721 write_state (write_state_filename);
5723 if (do_dump)
5724 dump_everything ();
5726 /* After having written the state file we return immediately to
5727 avoid generating any output file. */
5728 if (hit_error)
5729 return 1;
5730 else
5731 return 0;
5735 open_base_files ();
5737 output_header = plugin_output ? plugin_output : header_file;
5738 write_typed_alloc_defns (output_header, structures, typedefs);
5739 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5740 structures);
5741 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5742 param_structs);
5744 write_types (output_header, structures, param_structs, &ggc_wtd);
5745 if (plugin_files == NULL)
5747 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5748 structures);
5749 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5750 param_structs);
5751 write_types (header_file, structures, param_structs, &pch_wtd);
5752 write_local (header_file, structures, param_structs);
5754 write_splay_tree_allocators (param_structs);
5755 write_roots (variables, plugin_files == NULL);
5756 write_rtx_next ();
5757 close_output_files ();
5759 if (do_dump)
5760 dump_everything ();
5762 /* Don't bother about free-ing any input or plugin file, etc. */
5764 if (hit_error)
5765 return 1;
5766 return 0;