Daily bump.
[official-gcc.git] / gcc / gengtype.c
blobc1f9e693c22f9d9d1b200942dd99ea5aff53f0f7
1 /* Process source files and output type information.
2 Copyright (C) 2002-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #ifdef GENERATOR_FILE
21 #include "bconfig.h"
22 #else
23 #include "config.h"
24 #endif
25 #include "system.h"
26 #include "errors.h" /* for fatal */
27 #include "getopt.h"
28 #include "version.h" /* for version_string & pkgversion_string. */
29 #include "hashtab.h"
30 #include "xregex.h"
31 #include "obstack.h"
32 #include "gengtype.h"
33 #include "filenames.h"
35 /* Data types, macros, etc. used only in this file. */
38 /* The list of output files. */
39 outf_p output_files;
41 /* The output header file that is included into pretty much every
42 source file. */
43 outf_p header_file;
46 /* The name of the file containing the list of input files. */
47 static char *inputlist;
49 /* The plugin input files and their number; in that case only
50 a single file is produced. */
51 static input_file **plugin_files;
52 static size_t nb_plugin_files;
54 /* The generated plugin output file and name. */
55 static outf_p plugin_output;
56 static char *plugin_output_filename;
58 /* Our source directory and its length. */
59 const char *srcdir;
60 size_t srcdir_len;
62 /* Variables used for reading and writing the state. */
63 const char *read_state_filename;
64 const char *write_state_filename;
66 /* Variables to help debugging. */
67 int do_dump;
68 int do_debug;
70 /* Level for verbose messages. */
71 int verbosity_level;
73 /* We have a type count and use it to set the state_number of newly
74 allocated types to some unique negative number. */
75 static int type_count;
77 /* The backup directory should be in the same file system as the
78 generated files, otherwise the rename(2) system call would fail.
79 If NULL, no backup is made when overwriting a generated file. */
80 static const char* backup_dir; /* (-B) program option. */
83 static outf_p create_file (const char *, const char *);
85 static const char *get_file_basename (const input_file *);
86 static const char *get_file_realbasename (const input_file *);
88 static int get_prefix_langdir_index (const char *);
89 static const char *get_file_langdir (const input_file *);
91 static void dump_pair (int indent, pair_p p);
92 static void dump_type (int indent, type_p p);
93 static void dump_type_list (int indent, type_p p);
96 /* Nonzero iff an error has occurred. */
97 bool hit_error = false;
99 static void gen_rtx_next (void);
100 static void write_rtx_next (void);
101 static void open_base_files (void);
102 static void close_output_files (void);
104 /* Report an error at POS, printing MSG. */
106 void
107 error_at_line (const struct fileloc *pos, const char *msg, ...)
109 va_list ap;
111 gcc_assert (pos != NULL && pos->file != NULL);
112 va_start (ap, msg);
114 fprintf (stderr, "%s:%d: ", get_input_file_name (pos->file), pos->line);
115 vfprintf (stderr, msg, ap);
116 fputc ('\n', stderr);
117 hit_error = true;
119 va_end (ap);
122 /* asprintf, but produces fatal message on out-of-memory. */
123 char *
124 xasprintf (const char *format, ...)
126 int n;
127 char *result;
128 va_list ap;
130 va_start (ap, format);
131 n = vasprintf (&result, format, ap);
132 if (result == NULL || n < 0)
133 fatal ("out of memory");
134 va_end (ap);
136 return result;
139 /* Locate the ultimate base class of struct S. */
141 static const_type_p
142 get_ultimate_base_class (const_type_p s)
144 while (s->u.s.base_class)
145 s = s->u.s.base_class;
146 return s;
149 static type_p
150 get_ultimate_base_class (type_p s)
152 while (s->u.s.base_class)
153 s = s->u.s.base_class;
154 return s;
157 /* Input file handling. */
159 /* Table of all input files. */
160 const input_file **gt_files;
161 size_t num_gt_files;
163 /* A number of places use the name of this "gengtype.c" file for a
164 location for things that we can't rely on the source to define.
165 Make sure we can still use pointer comparison on filenames. */
166 input_file* this_file;
167 /* The "system.h" file is likewise specially useful. */
168 input_file* system_h_file;
170 /* Vector of per-language directories. */
171 const char **lang_dir_names;
172 size_t num_lang_dirs;
174 /* An array of output files suitable for definitions. There is one
175 BASE_FILES entry for each language. */
176 static outf_p *base_files;
180 #if ENABLE_CHECKING
181 /* Utility debugging function, printing the various type counts within
182 a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
183 void
184 dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
186 int nb_types = 0, nb_scalar = 0, nb_string = 0;
187 int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
188 int nb_lang_struct = 0, nb_param_struct = 0;
189 int nb_user_struct = 0, nb_undefined = 0;
190 type_p p = NULL;
191 for (p = t; p; p = p->next)
193 nb_types++;
194 switch (p->kind)
196 case TYPE_UNDEFINED:
197 nb_undefined++;
198 case TYPE_SCALAR:
199 nb_scalar++;
200 break;
201 case TYPE_STRING:
202 nb_string++;
203 break;
204 case TYPE_STRUCT:
205 nb_struct++;
206 break;
207 case TYPE_USER_STRUCT:
208 nb_user_struct++;
209 break;
210 case TYPE_UNION:
211 nb_union++;
212 break;
213 case TYPE_POINTER:
214 nb_pointer++;
215 break;
216 case TYPE_ARRAY:
217 nb_array++;
218 break;
219 case TYPE_LANG_STRUCT:
220 nb_lang_struct++;
221 break;
222 case TYPE_PARAM_STRUCT:
223 nb_param_struct++;
224 break;
225 case TYPE_NONE:
226 gcc_unreachable ();
229 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
230 lbasename (fil), lin, msg, nb_types);
231 if (nb_scalar > 0 || nb_string > 0)
232 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
233 if (nb_struct > 0 || nb_union > 0)
234 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
235 if (nb_pointer > 0 || nb_array > 0)
236 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
237 if (nb_lang_struct > 0 || nb_param_struct > 0)
238 fprintf (stderr, "@@%%@@ %d lang_structs, %d param_structs\n",
239 nb_lang_struct, nb_param_struct);
240 if (nb_user_struct > 0)
241 fprintf (stderr, "@@%%@@ %d user_structs\n", nb_user_struct);
242 if (nb_undefined > 0)
243 fprintf (stderr, "@@%%@@ %d undefined types\n", nb_undefined);
244 fprintf (stderr, "\n");
246 #endif /* ENABLE_CHECKING */
248 /* Scan the input file, LIST, and determine how much space we need to
249 store strings in. Also, count the number of language directories
250 and files. The numbers returned are overestimates as they does not
251 consider repeated files. */
252 static size_t
253 measure_input_list (FILE *list)
255 size_t n = 0;
256 int c;
257 bool atbol = true;
258 num_lang_dirs = 0;
259 num_gt_files = plugin_files ? nb_plugin_files : 0;
260 while ((c = getc (list)) != EOF)
262 n++;
263 if (atbol)
265 if (c == '[')
266 num_lang_dirs++;
267 else
269 /* Add space for a lang_bitmap before the input file name. */
270 n += sizeof (lang_bitmap);
271 num_gt_files++;
273 atbol = false;
276 if (c == '\n')
277 atbol = true;
280 rewind (list);
281 return n;
284 /* Read one input line from LIST to HEREP (which is updated). A
285 pointer to the string is returned via LINEP. If it was a language
286 subdirectory in square brackets, strip off the square brackets and
287 return true. Otherwise, leave space before the string for a
288 lang_bitmap, and return false. At EOF, returns false, does not
289 touch *HEREP, and sets *LINEP to NULL. POS is used for
290 diagnostics. */
291 static bool
292 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
294 char *here = *herep;
295 char *line;
296 int c = getc (list);
298 /* Read over whitespace. */
299 while (c == '\n' || c == ' ')
300 c = getc (list);
302 if (c == EOF)
304 *linep = 0;
305 return false;
307 else if (c == '[')
309 /* No space for a lang_bitmap is necessary. Discard the '['. */
310 c = getc (list);
311 line = here;
312 while (c != ']' && c != '\n' && c != EOF)
314 *here++ = c;
315 c = getc (list);
317 *here++ = '\0';
319 if (c == ']')
321 c = getc (list); /* eat what should be a newline */
322 if (c != '\n' && c != EOF)
323 error_at_line (pos, "junk on line after language tag [%s]", line);
325 else
326 error_at_line (pos, "missing close bracket for language tag [%s",
327 line);
329 *herep = here;
330 *linep = line;
331 return true;
333 else
335 /* Leave space for a lang_bitmap. */
336 memset (here, 0, sizeof (lang_bitmap));
337 here += sizeof (lang_bitmap);
338 line = here;
341 *here++ = c;
342 c = getc (list);
344 while (c != EOF && c != '\n');
345 *here++ = '\0';
346 *herep = here;
347 *linep = line;
348 return false;
352 /* Read the list of input files from LIST and compute all of the
353 relevant tables. There is one file per line of the list. At
354 first, all the files on the list are language-generic, but
355 eventually a line will appear which is the name of a language
356 subdirectory in square brackets, like this: [cp]. All subsequent
357 files are specific to that language, until another language
358 subdirectory tag appears. Files can appear more than once, if
359 they apply to more than one language. */
360 static void
361 read_input_list (const char *listname)
363 FILE *list = fopen (listname, "r");
364 if (!list)
365 fatal ("cannot open %s: %s", listname, xstrerror (errno));
366 else
368 struct fileloc epos;
369 size_t bufsz = measure_input_list (list);
370 char *buf = XNEWVEC (char, bufsz);
371 char *here = buf;
372 char *committed = buf;
373 char *limit = buf + bufsz;
374 char *line;
375 bool is_language;
376 size_t langno = 0;
377 size_t nfiles = 0;
378 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
380 epos.file = input_file_by_name (listname);
381 epos.line = 0;
383 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
384 gt_files = XNEWVEC (const input_file *, num_gt_files);
386 for (;;)
388 next_line:
389 epos.line++;
390 committed = here;
391 is_language = read_input_line (list, &here, &line, &epos);
392 gcc_assert (here <= limit);
393 if (line == 0)
394 break;
395 else if (is_language)
397 size_t i;
398 gcc_assert (langno <= num_lang_dirs);
399 for (i = 0; i < langno; i++)
400 if (strcmp (lang_dir_names[i], line) == 0)
402 error_at_line (&epos, "duplicate language tag [%s]",
403 line);
404 curlangs = 1 << i;
405 here = committed;
406 goto next_line;
409 curlangs = 1 << langno;
410 lang_dir_names[langno++] = line;
412 else
414 size_t i;
415 input_file *inpf = input_file_by_name (line);
416 gcc_assert (nfiles <= num_gt_files);
417 for (i = 0; i < nfiles; i++)
418 /* Since the input_file-s are uniquely hash-consed, we
419 can just compare pointers! */
420 if (gt_files[i] == inpf)
422 /* Throw away the string we just read, and add the
423 current language to the existing string's bitmap. */
424 lang_bitmap bmap = get_lang_bitmap (inpf);
425 if (bmap & curlangs)
426 error_at_line (&epos,
427 "file %s specified more than once "
428 "for language %s", line,
429 langno ==
430 0 ? "(all)" : lang_dir_names[langno -
431 1]);
433 bmap |= curlangs;
434 set_lang_bitmap (inpf, bmap);
435 here = committed;
436 goto next_line;
439 set_lang_bitmap (inpf, curlangs);
440 gt_files[nfiles++] = inpf;
443 /* Update the global counts now that we know accurately how many
444 things there are. (We do not bother resizing the arrays down.) */
445 num_lang_dirs = langno;
446 /* Add the plugin files if provided. */
447 if (plugin_files)
449 size_t i;
450 for (i = 0; i < nb_plugin_files; i++)
451 gt_files[nfiles++] = plugin_files[i];
453 num_gt_files = nfiles;
456 /* Sanity check: any file that resides in a language subdirectory
457 (e.g. 'cp') ought to belong to the corresponding language.
458 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
459 (Can you even do that? Should you be allowed to?) */
461 size_t f;
462 for (f = 0; f < num_gt_files; f++)
464 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
465 const char *basename = get_file_basename (gt_files[f]);
466 const char *slashpos = strchr (basename, '/');
467 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
468 const char *slashpos2 = strchr (basename, '\\');
470 if (!slashpos || (slashpos2 && slashpos2 < slashpos))
471 slashpos = slashpos2;
472 #endif
474 if (slashpos)
476 size_t l;
477 for (l = 0; l < num_lang_dirs; l++)
478 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
479 && memcmp (basename, lang_dir_names[l],
480 strlen (lang_dir_names[l])) == 0)
482 if (!(bitmap & (1 << l)))
483 error ("%s is in language directory '%s' but is not "
484 "tagged for that language",
485 basename, lang_dir_names[l]);
486 break;
492 if (ferror (list))
493 fatal ("error reading %s: %s", listname, xstrerror (errno));
495 fclose (list);
500 /* The one and only TYPE_STRING. */
502 struct type string_type = {
503 TYPE_STRING, 0, 0, 0, GC_USED, {0}
506 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
507 set early in main. */
509 struct type scalar_nonchar = {
510 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
513 struct type scalar_char = {
514 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
517 /* Lists of various things. */
519 pair_p typedefs = NULL;
520 type_p structures = NULL;
521 type_p param_structs = NULL;
522 pair_p variables = NULL;
524 static type_p find_param_structure (type_p t, type_p param[NUM_PARAM]);
525 static type_p adjust_field_tree_exp (type_p t, options_p opt);
526 static type_p adjust_field_rtx_def (type_p t, options_p opt);
528 /* Define S as a typedef to T at POS. */
530 void
531 do_typedef (const char *s, type_p t, struct fileloc *pos)
533 pair_p p;
535 /* temporary kludge - gengtype doesn't handle conditionals or
536 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
537 is coming from this file (main() sets them up with safe dummy
538 definitions). */
539 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
540 return;
542 for (p = typedefs; p != NULL; p = p->next)
543 if (strcmp (p->name, s) == 0)
545 if (p->type != t && strcmp (s, "result_type") != 0)
547 error_at_line (pos, "type `%s' previously defined", s);
548 error_at_line (&p->line, "previously defined here");
550 return;
553 p = XNEW (struct pair);
554 p->next = typedefs;
555 p->name = s;
556 p->type = t;
557 p->line = *pos;
558 p->opt = NULL;
559 typedefs = p;
562 /* Define S as a typename of a scalar. Cannot be used to define
563 typedefs of 'char'. Note: is also used for pointer-to-function
564 typedefs (which are therefore not treated as pointers). */
566 void
567 do_scalar_typedef (const char *s, struct fileloc *pos)
569 do_typedef (s, &scalar_nonchar, pos);
572 /* Similar to strtok_r. */
574 static char *
575 strtoken (char *str, const char *delim, char **next)
577 char *p;
579 if (str == NULL)
580 str = *next;
582 /* Skip the leading delimiters. */
583 str += strspn (str, delim);
584 if (*str == '\0')
585 /* This is an empty token. */
586 return NULL;
588 /* The current token. */
589 p = str;
591 /* Find the next delimiter. */
592 str += strcspn (str, delim);
593 if (*str == '\0')
594 /* This is the last token. */
595 *next = str;
596 else
598 /* Terminate the current token. */
599 *str = '\0';
600 /* Advance to the next token. */
601 *next = str + 1;
604 return p;
607 /* Define TYPE_NAME to be a user defined type at location POS. */
609 type_p
610 create_user_defined_type (const char *type_name, struct fileloc *pos)
612 type_p ty = find_structure (type_name, TYPE_USER_STRUCT);
614 /* We might have already seen an incomplete decl of the given type,
615 in which case we won't have yet seen a GTY((user)), and the type will
616 only have kind "TYPE_STRUCT". Mark it as a user struct. */
617 ty->kind = TYPE_USER_STRUCT;
619 ty->u.s.line = *pos;
620 ty->u.s.bitmap = get_lang_bitmap (pos->file);
621 do_typedef (type_name, ty, pos);
623 /* If TYPE_NAME specifies a template, create references to the types
624 in the template by pretending that each type is a field of TY.
625 This is needed to make sure that the types referenced by the
626 template are marked as used. */
627 char *str = xstrdup (type_name);
628 char *open_bracket = strchr (str, '<');
629 if (open_bracket)
631 /* We only accept simple template declarations (see
632 require_template_declaration), so we only need to parse a
633 comma-separated list of strings, implicitly assumed to
634 be type names, potentially with "*" characters. */
635 char *arg = open_bracket + 1;
636 char *next;
637 char *type_id = strtoken (arg, ",>", &next);
638 pair_p fields = 0;
639 while (type_id)
641 /* Create a new field for every type found inside the template
642 parameter list. */
644 /* Support a single trailing "*" character. */
645 const char *star = strchr (type_id, '*');
646 int is_ptr = (star != NULL);
647 size_t offset_to_star = star - type_id;
648 if (is_ptr)
649 offset_to_star = star - type_id;
651 char *field_name = xstrdup (type_id);
653 type_p arg_type;
654 if (is_ptr)
656 /* Strip off the first '*' character (and any subsequent text). */
657 *(field_name + offset_to_star) = '\0';
659 arg_type = find_structure (field_name, TYPE_STRUCT);
660 arg_type = create_pointer (arg_type);
662 else
663 arg_type = resolve_typedef (field_name, pos);
665 fields = create_field_at (fields, arg_type, field_name, 0, pos);
666 type_id = strtoken (0, ",>", &next);
669 /* Associate the field list to TY. */
670 ty->u.s.fields = fields;
672 free (str);
674 return ty;
678 /* Given a typedef name S, return its associated type. Return NULL if
679 S is not a registered type name. */
681 static type_p
682 type_for_name (const char *s)
684 pair_p p;
686 /* Special-case support for types within a "gcc::" namespace. Rather
687 than fully-supporting namespaces, simply strip off the "gcc::" prefix
688 where present. This allows us to have GTY roots of this form:
689 extern GTY(()) gcc::some_type *some_ptr;
690 where the autogenerated functions will refer to simply "some_type",
691 where they can be resolved into their namespace. */
692 if (0 == strncmp (s, "gcc::", 5))
693 s += 5;
695 for (p = typedefs; p != NULL; p = p->next)
696 if (strcmp (p->name, s) == 0)
697 return p->type;
698 return NULL;
702 /* Create an undefined type with name S and location POS. Return the
703 newly created type. */
705 static type_p
706 create_undefined_type (const char *s, struct fileloc *pos)
708 type_p ty = find_structure (s, TYPE_UNDEFINED);
709 ty->u.s.line = *pos;
710 ty->u.s.bitmap = get_lang_bitmap (pos->file);
711 do_typedef (s, ty, pos);
712 return ty;
716 /* Return the type previously defined for S. Use POS to report errors. */
718 type_p
719 resolve_typedef (const char *s, struct fileloc *pos)
721 bool is_template_instance = (strchr (s, '<') != NULL);
722 type_p p = type_for_name (s);
724 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
725 type for regular type identifiers. If the type identifier S is a
726 template instantiation, however, we treat it as a user defined
727 type.
729 FIXME, this is actually a limitation in gengtype. Supporting
730 template types and their instances would require keeping separate
731 track of the basic types definition and its instances. This
732 essentially forces all template classes in GC to be marked
733 GTY((user)). */
734 if (!p)
735 p = (is_template_instance)
736 ? create_user_defined_type (s, pos)
737 : create_undefined_type (s, pos);
739 return p;
742 /* Add SUBCLASS to head of linked list of BASE's subclasses. */
744 void add_subclass (type_p base, type_p subclass)
746 gcc_assert (union_or_struct_p (base));
747 gcc_assert (union_or_struct_p (subclass));
749 subclass->u.s.next_sibling_class = base->u.s.first_subclass;
750 base->u.s.first_subclass = subclass;
753 /* Create and return a new structure with tag NAME at POS with fields
754 FIELDS and options O. The KIND of structure must be one of
755 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
757 type_p
758 new_structure (const char *name, enum typekind kind, struct fileloc *pos,
759 pair_p fields, options_p o, type_p base_class)
761 type_p si;
762 type_p s = NULL;
763 lang_bitmap bitmap = get_lang_bitmap (pos->file);
764 bool isunion = (kind == TYPE_UNION);
766 gcc_assert (union_or_struct_p (kind));
768 for (si = structures; si != NULL; si = si->next)
769 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
771 type_p ls = NULL;
772 if (si->kind == TYPE_LANG_STRUCT)
774 ls = si;
776 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
777 if (si->u.s.bitmap == bitmap)
778 s = si;
780 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
782 ls = si;
783 type_count++;
784 si = XCNEW (struct type);
785 memcpy (si, ls, sizeof (struct type));
786 ls->kind = TYPE_LANG_STRUCT;
787 ls->u.s.lang_struct = si;
788 ls->u.s.fields = NULL;
789 si->next = NULL;
790 si->state_number = -type_count;
791 si->pointer_to = NULL;
792 si->u.s.lang_struct = ls;
794 else
795 s = si;
797 if (ls != NULL && s == NULL)
799 type_count++;
800 s = XCNEW (struct type);
801 s->state_number = -type_count;
802 s->next = ls->u.s.lang_struct;
803 ls->u.s.lang_struct = s;
804 s->u.s.lang_struct = ls;
806 break;
809 if (s == NULL)
811 type_count++;
812 s = XCNEW (struct type);
813 s->state_number = -type_count;
814 s->next = structures;
815 structures = s;
818 if (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap))
820 error_at_line (pos, "duplicate definition of '%s %s'",
821 isunion ? "union" : "struct", s->u.s.tag);
822 error_at_line (&s->u.s.line, "previous definition here");
825 s->kind = kind;
826 s->u.s.tag = name;
827 s->u.s.line = *pos;
828 s->u.s.fields = fields;
829 s->u.s.opt = o;
830 s->u.s.bitmap = bitmap;
831 if (s->u.s.lang_struct)
832 s->u.s.lang_struct->u.s.bitmap |= bitmap;
833 s->u.s.base_class = base_class;
834 if (base_class)
835 add_subclass (base_class, s);
837 return s;
840 /* Return the previously-defined structure or union with tag NAME,
841 or a new empty structure or union if none was defined previously.
842 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
843 TYPE_USER_STRUCT. */
845 type_p
846 find_structure (const char *name, enum typekind kind)
848 type_p s;
849 bool isunion = (kind == TYPE_UNION);
851 gcc_assert (kind == TYPE_UNDEFINED || union_or_struct_p (kind));
853 for (s = structures; s != NULL; s = s->next)
854 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
855 return s;
857 type_count++;
858 s = XCNEW (struct type);
859 s->next = structures;
860 s->state_number = -type_count;
861 structures = s;
862 s->kind = kind;
863 s->u.s.tag = name;
864 structures = s;
865 return s;
868 /* Return the previously-defined parameterized structure for structure
869 T and parameters PARAM, or a new parameterized empty structure or
870 union if none was defined previously. */
872 static type_p
873 find_param_structure (type_p t, type_p param[NUM_PARAM])
875 type_p res;
877 for (res = param_structs; res; res = res->next)
878 if (res->u.param_struct.stru == t
879 && memcmp (res->u.param_struct.param, param,
880 sizeof (type_p) * NUM_PARAM) == 0)
881 break;
882 if (res == NULL)
884 type_count++;
885 res = XCNEW (struct type);
886 res->kind = TYPE_PARAM_STRUCT;
887 res->next = param_structs;
888 res->state_number = -type_count;
889 param_structs = res;
890 res->u.param_struct.stru = t;
891 memcpy (res->u.param_struct.param, param, sizeof (type_p) * NUM_PARAM);
893 return res;
896 /* Return a scalar type with name NAME. */
898 type_p
899 create_scalar_type (const char *name)
901 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
902 return &scalar_char;
903 else
904 return &scalar_nonchar;
908 /* Return a pointer to T. */
910 type_p
911 create_pointer (type_p t)
913 if (!t->pointer_to)
915 type_p r = XCNEW (struct type);
916 type_count++;
917 r->state_number = -type_count;
918 r->kind = TYPE_POINTER;
919 r->u.p = t;
920 t->pointer_to = r;
922 return t->pointer_to;
925 /* Return an array of length LEN. */
927 type_p
928 create_array (type_p t, const char *len)
930 type_p v;
932 type_count++;
933 v = XCNEW (struct type);
934 v->kind = TYPE_ARRAY;
935 v->state_number = -type_count;
936 v->u.a.p = t;
937 v->u.a.len = len;
938 return v;
941 /* Return a string options structure with name NAME and info INFO.
942 NEXT is the next option in the chain. */
943 options_p
944 create_string_option (options_p next, const char *name, const char *info)
946 options_p o = XNEW (struct options);
947 o->kind = OPTION_STRING;
948 o->next = next;
949 o->name = name;
950 o->info.string = info;
951 return o;
954 /* Create a type options structure with name NAME and info INFO. NEXT
955 is the next option in the chain. */
956 options_p
957 create_type_option (options_p next, const char* name, type_p info)
959 options_p o = XNEW (struct options);
960 o->next = next;
961 o->name = name;
962 o->kind = OPTION_TYPE;
963 o->info.type = info;
964 return o;
967 /* Create a nested pointer options structure with name NAME and info
968 INFO. NEXT is the next option in the chain. */
969 options_p
970 create_nested_option (options_p next, const char* name,
971 struct nested_ptr_data* info)
973 options_p o;
974 o = XNEW (struct options);
975 o->next = next;
976 o->name = name;
977 o->kind = OPTION_NESTED;
978 o->info.nested = info;
979 return o;
982 /* Return an options structure for a "nested_ptr" option. */
983 options_p
984 create_nested_ptr_option (options_p next, type_p t,
985 const char *to, const char *from)
987 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
989 d->type = adjust_field_type (t, 0);
990 d->convert_to = to;
991 d->convert_from = from;
992 return create_nested_option (next, "nested_ptr", d);
995 /* Add a variable named S of type T with options O defined at POS,
996 to `variables'. */
997 void
998 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
1000 pair_p n;
1001 n = XNEW (struct pair);
1002 n->name = s;
1003 n->type = t;
1004 n->line = *pos;
1005 n->opt = o;
1006 n->next = variables;
1007 variables = n;
1010 /* Most-general structure field creator. */
1011 static pair_p
1012 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
1013 const input_file *inpf, int line)
1015 pair_p field;
1017 field = XNEW (struct pair);
1018 field->next = next;
1019 field->type = type;
1020 field->name = name;
1021 field->opt = opt;
1022 field->line.file = inpf;
1023 field->line.line = line;
1024 return field;
1027 /* Create a field that came from the source code we are scanning,
1028 i.e. we have a 'struct fileloc', and possibly options; also,
1029 adjust_field_type should be called. */
1030 pair_p
1031 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
1032 struct fileloc *pos)
1034 return create_field_all (next, adjust_field_type (type, opt),
1035 name, opt, pos->file, pos->line);
1038 /* Create a fake field with the given type and name. NEXT is the next
1039 field in the chain. */
1040 #define create_field(next,type,name) \
1041 create_field_all (next,type,name, 0, this_file, __LINE__)
1043 /* Like create_field, but the field is only valid when condition COND
1044 is true. */
1046 static pair_p
1047 create_optional_field_ (pair_p next, type_p type, const char *name,
1048 const char *cond, int line)
1050 static int id = 1;
1051 pair_p union_fields;
1052 type_p union_type;
1054 /* Create a fake union type with a single nameless field of type TYPE.
1055 The field has a tag of "1". This allows us to make the presence
1056 of a field of type TYPE depend on some boolean "desc" being true. */
1057 union_fields = create_field (NULL, type, "");
1058 union_fields->opt =
1059 create_string_option (union_fields->opt, "dot", "");
1060 union_fields->opt =
1061 create_string_option (union_fields->opt, "tag", "1");
1062 union_type =
1063 new_structure (xasprintf ("%s_%d", "fake_union", id++), TYPE_UNION,
1064 &lexer_line, union_fields, NULL, NULL);
1066 /* Create the field and give it the new fake union type. Add a "desc"
1067 tag that specifies the condition under which the field is valid. */
1068 return create_field_all (next, union_type, name,
1069 create_string_option (0, "desc", cond),
1070 this_file, line);
1073 #define create_optional_field(next,type,name,cond) \
1074 create_optional_field_(next,type,name,cond,__LINE__)
1076 /* Reverse a linked list of 'struct pair's in place. */
1077 pair_p
1078 nreverse_pairs (pair_p list)
1080 pair_p prev = 0, p, next;
1081 for (p = list; p; p = next)
1083 next = p->next;
1084 p->next = prev;
1085 prev = p;
1087 return prev;
1091 /* We don't care how long a CONST_DOUBLE is. */
1092 #define CONST_DOUBLE_FORMAT "ww"
1093 /* We don't want to see codes that are only for generator files. */
1094 #undef GENERATOR_FILE
1096 enum rtx_code
1098 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1099 #include "rtl.def"
1100 #undef DEF_RTL_EXPR
1101 NUM_RTX_CODE
1104 static const char *const rtx_name[NUM_RTX_CODE] = {
1105 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1106 #include "rtl.def"
1107 #undef DEF_RTL_EXPR
1110 static const char *const rtx_format[NUM_RTX_CODE] = {
1111 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1112 #include "rtl.def"
1113 #undef DEF_RTL_EXPR
1116 static int rtx_next_new[NUM_RTX_CODE];
1118 /* We also need codes and names for insn notes (not register notes).
1119 Note that we do *not* bias the note values here. */
1120 enum insn_note
1122 #define DEF_INSN_NOTE(NAME) NAME,
1123 #include "insn-notes.def"
1124 #undef DEF_INSN_NOTE
1126 NOTE_INSN_MAX
1129 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1130 default field for line number notes. */
1131 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
1132 #define DEF_INSN_NOTE(NAME) #NAME,
1133 #include "insn-notes.def"
1134 #undef DEF_INSN_NOTE
1137 #undef CONST_DOUBLE_FORMAT
1138 #define GENERATOR_FILE
1140 /* Generate the contents of the rtx_next array. This really doesn't belong
1141 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1143 static void
1144 gen_rtx_next (void)
1146 int i;
1147 for (i = 0; i < NUM_RTX_CODE; i++)
1149 int k;
1151 rtx_next_new[i] = -1;
1152 if (strncmp (rtx_format[i], "uu", 2) == 0)
1153 rtx_next_new[i] = 1;
1154 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1155 rtx_next_new[i] = 1;
1156 else
1157 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
1158 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1159 rtx_next_new[i] = k;
1163 /* Write out the contents of the rtx_next array. */
1164 static void
1165 write_rtx_next (void)
1167 outf_p f = get_output_file_with_visibility (NULL);
1168 int i;
1169 if (!f)
1170 return;
1172 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
1173 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1174 for (i = 0; i < NUM_RTX_CODE; i++)
1175 if (rtx_next_new[i] == -1)
1176 oprintf (f, " 0,\n");
1177 else
1178 oprintf (f,
1179 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1180 oprintf (f, "};\n");
1183 /* Handle `special("rtx_def")'. This is a special case for field
1184 `fld' of struct rtx_def, which is an array of unions whose values
1185 are based in a complex way on the type of RTL. */
1187 static type_p
1188 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1190 pair_p flds = NULL;
1191 options_p nodot;
1192 int i;
1193 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1194 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1196 if (t->kind != TYPE_UNION)
1198 error_at_line (&lexer_line,
1199 "special `rtx_def' must be applied to a union");
1200 return &string_type;
1203 nodot = create_string_option (NULL, "dot", "");
1205 rtx_tp = create_pointer (find_structure ("rtx_def", TYPE_STRUCT));
1206 rtvec_tp = create_pointer (find_structure ("rtvec_def", TYPE_STRUCT));
1207 tree_tp = create_pointer (find_structure ("tree_node", TYPE_UNION));
1208 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", TYPE_STRUCT));
1209 reg_attrs_tp =
1210 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT));
1211 basic_block_tp =
1212 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT));
1213 constant_tp =
1214 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT));
1215 scalar_tp = &scalar_nonchar; /* rtunion int */
1218 pair_p note_flds = NULL;
1219 int c;
1221 for (c = 0; c <= NOTE_INSN_MAX; c++)
1223 switch (c)
1225 case NOTE_INSN_MAX:
1226 case NOTE_INSN_DELETED_LABEL:
1227 case NOTE_INSN_DELETED_DEBUG_LABEL:
1228 note_flds = create_field (note_flds, &string_type, "rt_str");
1229 break;
1231 case NOTE_INSN_BLOCK_BEG:
1232 case NOTE_INSN_BLOCK_END:
1233 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1234 break;
1236 case NOTE_INSN_VAR_LOCATION:
1237 case NOTE_INSN_CALL_ARG_LOCATION:
1238 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1239 break;
1241 default:
1242 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1243 break;
1245 /* NOTE_INSN_MAX is used as the default field for line
1246 number notes. */
1247 if (c == NOTE_INSN_MAX)
1248 note_flds->opt =
1249 create_string_option (nodot, "default", "");
1250 else
1251 note_flds->opt =
1252 create_string_option (nodot, "tag", note_insn_name[c]);
1254 note_union_tp = new_structure ("rtx_def_note_subunion", TYPE_UNION,
1255 &lexer_line, note_flds, NULL, NULL);
1257 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1259 pair_p sym_flds;
1260 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1261 sym_flds->opt = create_string_option (nodot, "default", "");
1262 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1263 sym_flds->opt = create_string_option (nodot, "tag", "1");
1264 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", TYPE_UNION,
1265 &lexer_line, sym_flds, NULL, NULL);
1267 for (i = 0; i < NUM_RTX_CODE; i++)
1269 pair_p subfields = NULL;
1270 size_t aindex, nmindex;
1271 const char *sname;
1272 type_p substruct;
1273 char *ftag;
1275 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1277 type_p t;
1278 const char *subname;
1280 switch (rtx_format[i][aindex])
1282 case '*':
1283 case 'i':
1284 case 'n':
1285 case 'w':
1286 t = scalar_tp;
1287 subname = "rt_int";
1288 break;
1290 case '0':
1291 if (i == MEM && aindex == 1)
1292 t = mem_attrs_tp, subname = "rt_mem";
1293 else if (i == JUMP_INSN && aindex == 7)
1294 t = rtx_tp, subname = "rt_rtx";
1295 else if (i == CODE_LABEL && aindex == 4)
1296 t = scalar_tp, subname = "rt_int";
1297 else if (i == CODE_LABEL && aindex == 3)
1298 t = rtx_tp, subname = "rt_rtx";
1299 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1300 t = rtx_tp, subname = "rt_rtx";
1301 else if (i == NOTE && aindex == 3)
1302 t = note_union_tp, subname = "";
1303 else if (i == NOTE && aindex == 4)
1304 t = scalar_tp, subname = "rt_int";
1305 else if (i == NOTE && aindex >= 6)
1306 t = scalar_tp, subname = "rt_int";
1307 else if (i == ADDR_DIFF_VEC && aindex == 4)
1308 t = scalar_tp, subname = "rt_int";
1309 else if (i == VALUE && aindex == 0)
1310 t = scalar_tp, subname = "rt_int";
1311 else if (i == DEBUG_EXPR && aindex == 0)
1312 t = tree_tp, subname = "rt_tree";
1313 else if (i == REG && aindex == 1)
1314 t = reg_attrs_tp, subname = "rt_reg";
1315 else if (i == SYMBOL_REF && aindex == 1)
1316 t = symbol_union_tp, subname = "";
1317 else if (i == JUMP_TABLE_DATA && aindex >= 4)
1318 t = scalar_tp, subname = "rt_int";
1319 else if (i == BARRIER && aindex >= 2)
1320 t = scalar_tp, subname = "rt_int";
1321 else if (i == ENTRY_VALUE && aindex == 0)
1322 t = rtx_tp, subname = "rt_rtx";
1323 else
1325 error_at_line
1326 (&lexer_line,
1327 "rtx type `%s' has `0' in position %lu, can't handle",
1328 rtx_name[i], (unsigned long) aindex);
1329 t = &string_type;
1330 subname = "rt_int";
1332 break;
1334 case 's':
1335 case 'S':
1336 case 'T':
1337 t = &string_type;
1338 subname = "rt_str";
1339 break;
1341 case 'e':
1342 case 'u':
1343 t = rtx_tp;
1344 subname = "rt_rtx";
1345 break;
1347 case 'E':
1348 case 'V':
1349 t = rtvec_tp;
1350 subname = "rt_rtvec";
1351 break;
1353 case 't':
1354 t = tree_tp;
1355 subname = "rt_tree";
1356 break;
1358 case 'B':
1359 t = basic_block_tp;
1360 subname = "rt_bb";
1361 break;
1363 default:
1364 error_at_line
1365 (&lexer_line,
1366 "rtx type `%s' has `%c' in position %lu, can't handle",
1367 rtx_name[i], rtx_format[i][aindex],
1368 (unsigned long) aindex);
1369 t = &string_type;
1370 subname = "rt_int";
1371 break;
1374 subfields = create_field (subfields, t,
1375 xasprintf (".fld[%lu].%s",
1376 (unsigned long) aindex,
1377 subname));
1378 subfields->opt = nodot;
1379 if (t == note_union_tp)
1380 subfields->opt =
1381 create_string_option (subfields->opt, "desc",
1382 "NOTE_KIND (&%0)");
1383 if (t == symbol_union_tp)
1384 subfields->opt =
1385 create_string_option (subfields->opt, "desc",
1386 "CONSTANT_POOL_ADDRESS_P (&%0)");
1389 if (i == SYMBOL_REF)
1391 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1392 holds. */
1393 type_p field_tp = find_structure ("block_symbol", TYPE_STRUCT);
1394 subfields
1395 = create_optional_field (subfields, field_tp, "block_sym",
1396 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1399 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1400 substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
1401 NULL, NULL);
1403 ftag = xstrdup (rtx_name[i]);
1404 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1405 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1406 flds = create_field (flds, substruct, "");
1407 flds->opt = create_string_option (nodot, "tag", ftag);
1409 return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
1410 nodot, NULL);
1413 /* Handle `special("tree_exp")'. This is a special case for
1414 field `operands' of struct tree_exp, which although it claims to contain
1415 pointers to trees, actually sometimes contains pointers to RTL too.
1416 Passed T, the old type of the field, and OPT its options. Returns
1417 a new type for the field. */
1419 static type_p
1420 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1422 pair_p flds;
1423 options_p nodot;
1425 if (t->kind != TYPE_ARRAY)
1427 error_at_line (&lexer_line,
1428 "special `tree_exp' must be applied to an array");
1429 return &string_type;
1432 nodot = create_string_option (NULL, "dot", "");
1434 flds = create_field (NULL, t, "");
1435 flds->opt = create_string_option (nodot, "length",
1436 "TREE_OPERAND_LENGTH ((tree) &%0)");
1437 flds->opt = create_string_option (flds->opt, "default", "");
1439 return new_structure ("tree_exp_subunion", TYPE_UNION, &lexer_line, flds,
1440 nodot, NULL);
1443 /* Perform any special processing on a type T, about to become the type
1444 of a field. Return the appropriate type for the field.
1445 At present:
1446 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1447 - Similarly for arrays of pointer-to-char;
1448 - Converts structures for which a parameter is provided to
1449 TYPE_PARAM_STRUCT;
1450 - Handles "special" options.
1453 type_p
1454 adjust_field_type (type_p t, options_p opt)
1456 int length_p = 0;
1457 const int pointer_p = t->kind == TYPE_POINTER;
1458 type_p params[NUM_PARAM];
1459 int params_p = 0;
1460 int i;
1462 for (i = 0; i < NUM_PARAM; i++)
1463 params[i] = NULL;
1465 for (; opt; opt = opt->next)
1466 if (strcmp (opt->name, "length") == 0)
1468 if (length_p)
1469 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1470 if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
1472 error_at_line (&lexer_line,
1473 "option `%s' may not be applied to "
1474 "arrays of atomic types", opt->name);
1476 length_p = 1;
1478 else if ((strcmp (opt->name, "param_is") == 0
1479 || (strncmp (opt->name, "param", 5) == 0
1480 && ISDIGIT (opt->name[5])
1481 && strcmp (opt->name + 6, "_is") == 0))
1482 && opt->kind == OPTION_TYPE)
1484 int num = ISDIGIT (opt->name[5]) ? opt->name[5] - '0' : 0;
1486 if (!union_or_struct_p (t)
1487 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
1489 error_at_line (&lexer_line,
1490 "option `%s' may only be applied to structures or structure pointers",
1491 opt->name);
1492 return t;
1495 params_p = 1;
1496 if (params[num] != NULL)
1497 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1498 if (!ISDIGIT (opt->name[5]))
1499 params[num] = create_pointer (opt->info.type);
1500 else
1501 params[num] = opt->info.type;
1503 else if (strcmp (opt->name, "special") == 0
1504 && opt->kind == OPTION_STRING)
1506 const char *special_name = opt->info.string;
1507 if (strcmp (special_name, "tree_exp") == 0)
1508 t = adjust_field_tree_exp (t, opt);
1509 else if (strcmp (special_name, "rtx_def") == 0)
1510 t = adjust_field_rtx_def (t, opt);
1511 else
1512 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1515 if (params_p)
1517 type_p realt;
1519 if (pointer_p)
1520 t = t->u.p;
1521 realt = find_param_structure (t, params);
1522 t = pointer_p ? create_pointer (realt) : realt;
1525 if (!length_p
1526 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1527 return &string_type;
1528 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1529 && t->u.a.p->u.p->kind == TYPE_SCALAR
1530 && t->u.a.p->u.p->u.scalar_is_char)
1531 return create_array (&string_type, t->u.a.len);
1533 return t;
1537 static void set_gc_used_type (type_p, enum gc_used_enum, type_p *,
1538 bool = false);
1539 static void set_gc_used (pair_p);
1541 /* Handle OPT for set_gc_used_type. */
1543 static void
1544 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1545 int *pass_param, int *length, int *skip,
1546 type_p *nested_ptr)
1548 options_p o;
1549 for (o = opt; o; o = o->next)
1550 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1551 && o->kind == OPTION_TYPE)
1552 set_gc_used_type (o->info.type,
1553 GC_POINTED_TO, NULL);
1554 else if (strcmp (o->name, "maybe_undef") == 0)
1555 *maybe_undef = 1;
1556 else if (strcmp (o->name, "use_params") == 0)
1557 *pass_param = 1;
1558 else if (strcmp (o->name, "length") == 0)
1559 *length = 1;
1560 else if (strcmp (o->name, "skip") == 0)
1561 *skip = 1;
1562 else if (strcmp (o->name, "nested_ptr") == 0
1563 && o->kind == OPTION_NESTED)
1564 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1568 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1570 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1571 are set to GC_UNUSED. Otherwise, an error is emitted for
1572 TYPE_UNDEFINED types. This is used to support user-defined
1573 template types with non-type arguments.
1575 For instance, when we parse a template type with enum arguments
1576 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1577 artificial fields for 'MyType', one for 'AnotherType', the other
1578 one for 'EnumValue'.
1580 At the time that we parse this type we don't know that 'EnumValue'
1581 is really an enum value, so the parser creates a TYPE_UNDEFINED
1582 type for it. Since 'EnumValue' is never resolved to a known
1583 structure, it will stay with TYPE_UNDEFINED.
1585 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1586 'EnumValue'. Generating marking code for it would cause
1587 compilation failures since the marking routines assumes that
1588 'EnumValue' is a type. */
1590 static void
1591 set_gc_used_type (type_p t, enum gc_used_enum level, type_p param[NUM_PARAM],
1592 bool allow_undefined_types)
1594 if (t->gc_used >= level)
1595 return;
1597 t->gc_used = level;
1599 switch (t->kind)
1601 case TYPE_STRUCT:
1602 case TYPE_UNION:
1603 case TYPE_USER_STRUCT:
1605 pair_p f;
1606 int dummy;
1607 type_p dummy2;
1608 bool allow_undefined_field_types = (t->kind == TYPE_USER_STRUCT);
1610 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
1611 &dummy2);
1613 if (t->u.s.base_class)
1614 set_gc_used_type (t->u.s.base_class, level, param,
1615 allow_undefined_types);
1616 /* Anything pointing to a base class might actually be pointing
1617 to a subclass. */
1618 for (type_p subclass = t->u.s.first_subclass; subclass;
1619 subclass = subclass->u.s.next_sibling_class)
1620 set_gc_used_type (subclass, level, param,
1621 allow_undefined_types);
1623 FOR_ALL_INHERITED_FIELDS(t, f)
1625 int maybe_undef = 0;
1626 int pass_param = 0;
1627 int length = 0;
1628 int skip = 0;
1629 type_p nested_ptr = NULL;
1630 process_gc_options (f->opt, level, &maybe_undef, &pass_param,
1631 &length, &skip, &nested_ptr);
1633 if (nested_ptr && f->type->kind == TYPE_POINTER)
1634 set_gc_used_type (nested_ptr, GC_POINTED_TO,
1635 pass_param ? param : NULL);
1636 else if (length && f->type->kind == TYPE_POINTER)
1637 set_gc_used_type (f->type->u.p, GC_USED, NULL);
1638 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1639 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO, NULL);
1640 else if (pass_param && f->type->kind == TYPE_POINTER && param)
1641 set_gc_used_type (find_param_structure (f->type->u.p, param),
1642 GC_POINTED_TO, NULL);
1643 else if (skip)
1644 ; /* target type is not used through this field */
1645 else
1646 set_gc_used_type (f->type, GC_USED, pass_param ? param : NULL,
1647 allow_undefined_field_types);
1649 break;
1652 case TYPE_UNDEFINED:
1653 if (level > GC_UNUSED)
1655 if (!allow_undefined_types)
1656 error_at_line (&t->u.s.line, "undefined type `%s'", t->u.s.tag);
1657 t->gc_used = GC_UNUSED;
1659 break;
1661 case TYPE_POINTER:
1662 set_gc_used_type (t->u.p, GC_POINTED_TO, NULL);
1663 break;
1665 case TYPE_ARRAY:
1666 set_gc_used_type (t->u.a.p, GC_USED, param);
1667 break;
1669 case TYPE_LANG_STRUCT:
1670 for (t = t->u.s.lang_struct; t; t = t->next)
1671 set_gc_used_type (t, level, param);
1672 break;
1674 case TYPE_PARAM_STRUCT:
1676 int i;
1677 for (i = 0; i < NUM_PARAM; i++)
1678 if (t->u.param_struct.param[i] != 0)
1679 set_gc_used_type (t->u.param_struct.param[i], GC_USED, NULL);
1681 if (t->u.param_struct.stru->gc_used == GC_POINTED_TO)
1682 level = GC_POINTED_TO;
1683 else
1684 level = GC_USED;
1685 t->u.param_struct.stru->gc_used = GC_UNUSED;
1686 set_gc_used_type (t->u.param_struct.stru, level,
1687 t->u.param_struct.param);
1688 break;
1690 default:
1691 break;
1695 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1697 static void
1698 set_gc_used (pair_p variables)
1700 int nbvars = 0;
1701 pair_p p;
1702 for (p = variables; p; p = p->next)
1704 set_gc_used_type (p->type, GC_USED, NULL);
1705 nbvars++;
1707 if (verbosity_level >= 2)
1708 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1711 /* File mapping routines. For each input file, there is one output .c file
1712 (but some output files have many input files), and there is one .h file
1713 for the whole build. */
1715 /* Output file handling. */
1717 /* Create and return an outf_p for a new file for NAME, to be called
1718 ONAME. */
1720 static outf_p
1721 create_file (const char *name, const char *oname)
1723 static const char *const hdr[] = {
1724 " Copyright (C) 2004-2014 Free Software Foundation, Inc.\n",
1725 "\n",
1726 "This file is part of GCC.\n",
1727 "\n",
1728 "GCC is free software; you can redistribute it and/or modify it under\n",
1729 "the terms of the GNU General Public License as published by the Free\n",
1730 "Software Foundation; either version 3, or (at your option) any later\n",
1731 "version.\n",
1732 "\n",
1733 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1734 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1735 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1736 "for more details.\n",
1737 "\n",
1738 "You should have received a copy of the GNU General Public License\n",
1739 "along with GCC; see the file COPYING3. If not see\n",
1740 "<http://www.gnu.org/licenses/>. */\n",
1741 "\n",
1742 "/* This file is machine generated. Do not edit. */\n"
1744 outf_p f;
1745 size_t i;
1747 gcc_assert (name != NULL);
1748 gcc_assert (oname != NULL);
1749 f = XCNEW (struct outf);
1750 f->next = output_files;
1751 f->name = oname;
1752 output_files = f;
1754 oprintf (f, "/* Type information for %s.\n", name);
1755 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1756 oprintf (f, "%s", hdr[i]);
1757 return f;
1760 /* Print, like fprintf, to O.
1761 N.B. You might think this could be implemented more efficiently
1762 with vsnprintf(). Unfortunately, there are C libraries that
1763 provide that function but without the C99 semantics for its return
1764 value, making it impossible to know how much space is required. */
1765 void
1766 oprintf (outf_p o, const char *format, ...)
1768 char *s;
1769 size_t slength;
1770 va_list ap;
1772 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1773 in that case. */
1774 if (!o)
1775 return;
1777 va_start (ap, format);
1778 slength = vasprintf (&s, format, ap);
1779 if (s == NULL || (int) slength < 0)
1780 fatal ("out of memory");
1781 va_end (ap);
1783 if (o->bufused + slength > o->buflength)
1785 size_t new_len = o->buflength;
1786 if (new_len == 0)
1787 new_len = 1024;
1790 new_len *= 2;
1792 while (o->bufused + slength >= new_len);
1793 o->buf = XRESIZEVEC (char, o->buf, new_len);
1794 o->buflength = new_len;
1796 memcpy (o->buf + o->bufused, s, slength);
1797 o->bufused += slength;
1798 free (s);
1801 /* Open the global header file and the language-specific header files. */
1803 static void
1804 open_base_files (void)
1806 size_t i;
1808 if (nb_plugin_files > 0 && plugin_files)
1809 return;
1811 header_file = create_file ("GCC", "gtype-desc.h");
1813 base_files = XNEWVEC (outf_p, num_lang_dirs);
1815 for (i = 0; i < num_lang_dirs; i++)
1816 base_files[i] = create_file (lang_dir_names[i],
1817 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1819 /* gtype-desc.c is a little special, so we create it here. */
1821 /* The order of files here matters very much. */
1822 static const char *const ifiles[] = {
1823 "config.h", "system.h", "coretypes.h", "tm.h",
1824 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1825 "tree.h", "rtl.h", "wide-int.h", "function.h", "insn-config.h", "expr.h",
1826 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1827 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1828 "hash-table.h", "vec.h", "ggc.h", "basic-block.h",
1829 "tree-ssa-alias.h", "internal-fn.h", "gimple-fold.h", "tree-eh.h",
1830 "gimple-expr.h", "is-a.h",
1831 "gimple.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
1832 "tree-phinodes.h", "ssa-iterators.h", "stringpool.h", "tree-ssanames.h",
1833 "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h", "tree-ssa-loop-manip.h",
1834 "tree-ssa-loop-niter.h", "tree-into-ssa.h", "tree-dfa.h",
1835 "tree-ssa.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1836 "except.h", "output.h", "cfgloop.h",
1837 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h",
1838 "ipa-inline.h", "dwarf2out.h", NULL
1840 const char *const *ifp;
1841 outf_p gtype_desc_c;
1843 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1844 for (ifp = ifiles; *ifp; ifp++)
1845 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1847 /* Make sure we handle "cfun" specially. */
1848 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1849 oprintf (gtype_desc_c, "#undef cfun\n");
1851 oprintf (gtype_desc_c,
1852 "\n"
1853 "/* Types with a \"gcc::\" namespace have it stripped\n"
1854 " during gengtype parsing. Provide a \"using\" directive\n"
1855 " to ensure that the fully-qualified types are found. */\n"
1856 "using namespace gcc;\n");
1860 /* For INPF an input file, return the real basename of INPF, with all
1861 the directory components skipped. */
1863 static const char *
1864 get_file_realbasename (const input_file *inpf)
1866 return lbasename (get_input_file_name (inpf));
1869 /* For INPF a filename, return the relative path to INPF from
1870 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1872 const char *
1873 get_file_srcdir_relative_path (const input_file *inpf)
1875 const char *f = get_input_file_name (inpf);
1876 if (strlen (f) > srcdir_len
1877 && IS_DIR_SEPARATOR (f[srcdir_len])
1878 && strncmp (f, srcdir, srcdir_len) == 0)
1879 return f + srcdir_len + 1;
1880 else
1881 return NULL;
1884 /* For INPF an input_file, return the relative path to INPF from
1885 $(srcdir) if the latter is a prefix in INPF, or the real basename
1886 of INPF otherwise. */
1888 static const char *
1889 get_file_basename (const input_file *inpf)
1891 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1893 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1896 /* For F a filename, return the lang_dir_names relative index of the language
1897 directory that is a prefix in F, if any, -1 otherwise. */
1899 static int
1900 get_prefix_langdir_index (const char *f)
1902 size_t f_len = strlen (f);
1903 size_t lang_index;
1905 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1907 const char *langdir = lang_dir_names[lang_index];
1908 size_t langdir_len = strlen (langdir);
1910 if (f_len > langdir_len
1911 && IS_DIR_SEPARATOR (f[langdir_len])
1912 && memcmp (f, langdir, langdir_len) == 0)
1913 return lang_index;
1916 return -1;
1919 /* For INPF an input file, return the name of language directory where
1920 F is located, if any, NULL otherwise. */
1922 static const char *
1923 get_file_langdir (const input_file *inpf)
1925 /* Get the relative path to INPF from $(srcdir) and find the
1926 language by comparing the prefix with language directory names.
1927 If INPF is not even srcdir relative, no point in looking
1928 further. */
1930 int lang_index;
1931 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1932 const char *r;
1934 if (!srcdir_relative_path)
1935 return NULL;
1937 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1938 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1939 r = "c-family";
1940 else if (lang_index >= 0)
1941 r = lang_dir_names[lang_index];
1942 else
1943 r = NULL;
1945 return r;
1948 /* The gt- output file name for INPF. */
1950 static const char *
1951 get_file_gtfilename (const input_file *inpf)
1953 /* Cook up an initial version of the gt- file name from the file real
1954 basename and the language name, if any. */
1956 const char *basename = get_file_realbasename (inpf);
1957 const char *langdir = get_file_langdir (inpf);
1959 char *result =
1960 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1961 : xasprintf ("gt-%s", basename));
1963 /* Then replace all non alphanumerics characters by '-' and change the
1964 extension to ".h". We expect the input filename extension was at least
1965 one character long. */
1967 char *s = result;
1969 for (; *s != '.'; s++)
1970 if (!ISALNUM (*s) && *s != '-')
1971 *s = '-';
1973 memcpy (s, ".h", sizeof (".h"));
1975 return result;
1978 /* Each input_file has its associated output file outf_p. The
1979 association is computed by the function
1980 get_output_file_with_visibility. The associated file is cached
1981 inside input_file in its inpoutf field, so is really computed only
1982 once. Associated output file paths (i.e. output_name-s) are
1983 computed by a rule based regexp machinery, using the files_rules
1984 array of struct file_rule_st. A for_name is also computed, giving
1985 the source file name for which the output_file is generated; it is
1986 often the last component of the input_file path. */
1990 Regexpr machinery to compute the output_name and for_name-s of each
1991 input_file. We have a sequence of file rules which gives the POSIX
1992 extended regular expression to match an input file path, and two
1993 transformed strings for the corresponding output_name and the
1994 corresponding for_name. The transformed string contain dollars: $0
1995 is replaced by the entire match, $1 is replaced by the substring
1996 matching the first parenthesis in the regexp, etc. And $$ is replaced
1997 by a single verbatim dollar. The rule order is important. The
1998 general case is last, and the particular cases should come before.
1999 An action routine can, when needed, update the out_name & for_name
2000 and/or return the appropriate output file. It is invoked only when a
2001 rule is triggered. When a rule is triggered, the output_name and
2002 for_name are computed using their transform string in while $$, $0,
2003 $1, ... are suitably replaced. If there is an action, it is called.
2004 In some few cases, the action can directly return the outf_p, but
2005 usually it just updates the output_name and for_name so should free
2006 them before replacing them. The get_output_file_with_visibility
2007 function creates an outf_p only once per each output_name, so it
2008 scans the output_files list for previously seen output file names.
2011 /* Signature of actions in file rules. */
2012 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
2015 struct file_rule_st {
2016 const char* frul_srcexpr; /* Source string for regexp. */
2017 int frul_rflags; /* Flags passed to regcomp, usually
2018 * REG_EXTENDED. */
2019 regex_t* frul_re; /* Compiled regular expression
2020 obtained by regcomp. */
2021 const char* frul_tr_out; /* Transformation string for making
2022 * the output_name, with $1 ... $9 for
2023 * subpatterns and $0 for the whole
2024 * matched filename. */
2025 const char* frul_tr_for; /* Tranformation string for making the
2026 for_name. */
2027 frul_actionrout_t* frul_action; /* The action, if non null, is
2028 * called once the rule matches, on
2029 * the transformed out_name &
2030 * for_name. It could change them
2031 * and/or give the output file. */
2034 /* File rule action handling *.h files. */
2035 static outf_p header_dot_h_frul (input_file*, char**, char**);
2037 /* File rule action handling *.c files. */
2038 static outf_p source_dot_c_frul (input_file*, char**, char**);
2040 #define NULL_REGEX (regex_t*)0
2042 /* The prefix in our regexp-s matching the directory. */
2043 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
2045 #define NULL_FRULACT (frul_actionrout_t*)0
2047 /* The array of our rules governing file name generation. Rules order
2048 matters, so change with extreme care! */
2050 struct file_rule_st files_rules[] = {
2051 /* The general rule assumes that files in subdirectories belong to a
2052 particular front-end, and files not in subdirectories are shared.
2053 The following rules deal with exceptions - files that are in
2054 subdirectories and yet are shared, and files that are top-level,
2055 but are not shared. */
2057 /* the c-family/ source directory is special. */
2058 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
2059 REG_EXTENDED, NULL_REGEX,
2060 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
2062 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
2063 REG_EXTENDED, NULL_REGEX,
2064 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
2066 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
2067 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
2068 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
2070 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
2071 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
2073 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
2074 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
2075 REG_EXTENDED, NULL_REGEX,
2076 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
2078 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
2079 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
2080 REG_EXTENDED, NULL_REGEX,
2081 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
2083 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
2084 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
2085 REG_EXTENDED, NULL_REGEX,
2086 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
2088 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
2089 { DIR_PREFIX_REGEX "cp/parser\\.h$",
2090 REG_EXTENDED, NULL_REGEX,
2091 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
2093 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
2094 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
2095 REG_EXTENDED, NULL_REGEX,
2096 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
2098 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
2099 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
2100 REG_EXTENDED, NULL_REGEX,
2101 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
2103 /* General cases. For header *.h and source *.c or *.cc files, we
2104 * need special actions to handle the language. */
2106 /* Source *.c files are using get_file_gtfilename to compute their
2107 output_name and get_file_basename to compute their for_name
2108 through the source_dot_c_frul action. */
2109 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
2110 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
2112 /* Source *.cc files are using get_file_gtfilename to compute their
2113 output_name and get_file_basename to compute their for_name
2114 through the source_dot_c_frul action. */
2115 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.cc$",
2116 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.cc", source_dot_c_frul},
2118 /* Common header files get "gtype-desc.c" as their output_name,
2119 * while language specific header files are handled specially. So
2120 * we need the header_dot_h_frul action. */
2121 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
2122 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
2124 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
2125 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
2127 /* Mandatory null last entry signaling end of rules. */
2128 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
2131 /* Special file rules action for handling *.h header files. It gives
2132 "gtype-desc.c" for common headers and corresponding output
2133 files for language-specific header files. */
2134 static outf_p
2135 header_dot_h_frul (input_file* inpf, char**poutname,
2136 char**pforname ATTRIBUTE_UNUSED)
2138 const char *basename = 0;
2139 int lang_index = 0;
2140 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2141 (void*) inpf, get_input_file_name (inpf),
2142 *poutname, *pforname);
2143 basename = get_file_basename (inpf);
2144 lang_index = get_prefix_langdir_index (basename);
2145 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
2147 if (lang_index >= 0)
2149 /* The header is language specific. Given output_name &
2150 for_name remains unchanged. The base_files array gives the
2151 outf_p. */
2152 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2153 (void*) base_files[lang_index],
2154 (base_files[lang_index])->name);
2155 return base_files[lang_index];
2157 else
2159 /* The header is common to all front-end languages. So
2160 output_name is "gtype-desc.c" file. The calling function
2161 get_output_file_with_visibility will find its outf_p. */
2162 free (*poutname);
2163 *poutname = xstrdup ("gtype-desc.c");
2164 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2165 get_input_file_name (inpf));
2166 return NULL;
2171 /* Special file rules action for handling *.c source files using
2172 * get_file_gtfilename to compute their output_name and
2173 * get_file_basename to compute their for_name. The output_name is
2174 * gt-<LANG>-<BASE>.h for language specific source files, and
2175 * gt-<BASE>.h for common source files. */
2176 static outf_p
2177 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
2179 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
2180 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
2181 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2182 (void*) inpf, get_input_file_name (inpf),
2183 *poutname, *pforname);
2184 DBGPRINTF ("newoutname %s", newoutname);
2185 DBGPRINTF ("newbasename %s", newbasename);
2186 free (*poutname);
2187 free (*pforname);
2188 *poutname = newoutname;
2189 *pforname = newbasename;
2190 return NULL;
2193 /* Utility function for get_output_file_with_visibility which returns
2194 * a malloc-ed substituted string using TRS on matching of the FILNAM
2195 * file name, using the PMATCH array. */
2196 static char*
2197 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
2198 const char *trs)
2200 struct obstack str_obstack;
2201 char *str = NULL;
2202 char *rawstr = NULL;
2203 const char *pt = NULL;
2204 DBGPRINTF ("filnam %s", filnam);
2205 obstack_init (&str_obstack);
2206 for (pt = trs; *pt; pt++) {
2207 char c = *pt;
2208 if (c == '$')
2210 if (pt[1] == '$')
2212 /* A double dollar $$ is substituted by a single verbatim
2213 dollar, but who really uses dollar signs in file
2214 paths? */
2215 obstack_1grow (&str_obstack, '$');
2217 else if (ISDIGIT (pt[1]))
2219 /* Handle $0 $1 ... $9 by appropriate substitution. */
2220 int dolnum = pt[1] - '0';
2221 int so = pmatch[dolnum].rm_so;
2222 int eo = pmatch[dolnum].rm_eo;
2223 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2224 if (so>=0 && eo>=so)
2225 obstack_grow (&str_obstack, filnam + so, eo - so);
2227 else
2229 /* This can happen only when files_rules is buggy! */
2230 gcc_unreachable ();
2232 /* Always skip the character after the dollar. */
2233 pt++;
2235 else
2236 obstack_1grow (&str_obstack, c);
2238 obstack_1grow (&str_obstack, '\0');
2239 rawstr = XOBFINISH (&str_obstack, char *);
2240 str = xstrdup (rawstr);
2241 obstack_free (&str_obstack, NULL);
2242 DBGPRINTF ("matched replacement %s", str);
2243 rawstr = NULL;
2244 return str;
2248 /* An output file, suitable for definitions, that can see declarations
2249 made in INPF and is linked into every language that uses INPF.
2250 Since the result is cached inside INPF, that argument cannot be
2251 declared constant, but is "almost" constant. */
2253 outf_p
2254 get_output_file_with_visibility (input_file *inpf)
2256 outf_p r;
2257 char *for_name = NULL;
2258 char *output_name = NULL;
2259 const char* inpfname;
2261 /* This can happen when we need a file with visibility on a
2262 structure that we've never seen. We have to just hope that it's
2263 globally visible. */
2264 if (inpf == NULL)
2265 inpf = system_h_file;
2267 /* The result is cached in INPF, so return it if already known. */
2268 if (inpf->inpoutf)
2269 return inpf->inpoutf;
2271 /* In plugin mode, return NULL unless the input_file is one of the
2272 plugin_files. */
2273 if (plugin_files)
2275 size_t i;
2276 for (i = 0; i < nb_plugin_files; i++)
2277 if (inpf == plugin_files[i])
2279 inpf->inpoutf = plugin_output;
2280 return plugin_output;
2283 return NULL;
2286 inpfname = get_input_file_name (inpf);
2288 /* Try each rule in sequence in files_rules until one is triggered. */
2290 int rulix = 0;
2291 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2292 (void*) inpf, inpfname);
2294 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2296 DBGPRINTF ("rulix#%d srcexpr %s",
2297 rulix, files_rules[rulix].frul_srcexpr);
2299 if (!files_rules[rulix].frul_re)
2301 /* Compile the regexpr lazily. */
2302 int err = 0;
2303 files_rules[rulix].frul_re = XCNEW (regex_t);
2304 err = regcomp (files_rules[rulix].frul_re,
2305 files_rules[rulix].frul_srcexpr,
2306 files_rules[rulix].frul_rflags);
2307 if (err)
2309 /* The regular expression compilation fails only when
2310 file_rules is buggy. */
2311 gcc_unreachable ();
2315 output_name = NULL;
2316 for_name = NULL;
2318 /* Match the regexpr and trigger the rule if matched. */
2320 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2321 $3, ... $9. */
2322 regmatch_t pmatch[10];
2323 memset (pmatch, 0, sizeof (pmatch));
2324 if (!regexec (files_rules[rulix].frul_re,
2325 inpfname, 10, pmatch, 0))
2327 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2328 (void*) inpf, inpfname, rulix,
2329 files_rules[rulix].frul_srcexpr);
2330 for_name =
2331 matching_file_name_substitute (inpfname, pmatch,
2332 files_rules[rulix].frul_tr_for);
2333 DBGPRINTF ("for_name %s", for_name);
2334 output_name =
2335 matching_file_name_substitute (inpfname, pmatch,
2336 files_rules[rulix].frul_tr_out);
2337 DBGPRINTF ("output_name %s", output_name);
2338 if (files_rules[rulix].frul_action)
2340 /* Invoke our action routine. */
2341 outf_p of = NULL;
2342 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2343 rulix, output_name, for_name);
2344 of =
2345 (files_rules[rulix].frul_action) (inpf,
2346 &output_name, &for_name);
2347 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2348 rulix, (void*)of, output_name, for_name);
2349 /* If the action routine returned something, give it back
2350 immediately and cache it in inpf. */
2351 if (of)
2353 inpf->inpoutf = of;
2354 return of;
2357 /* The rule matched, and had no action, or that action did
2358 not return any output file but could have changed the
2359 output_name or for_name. We break out of the loop on the
2360 files_rules. */
2361 break;
2363 else
2365 /* The regexpr did not match. */
2366 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2367 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2368 continue;
2373 if (!output_name || !for_name)
2375 /* This should not be possible, and could only happen if the
2376 files_rules is incomplete or buggy. */
2377 fatal ("failed to compute output name for %s", inpfname);
2380 /* Look through to see if we've ever seen this output filename
2381 before. If found, cache the result in inpf. */
2382 for (r = output_files; r; r = r->next)
2383 if (filename_cmp (r->name, output_name) == 0)
2385 inpf->inpoutf = r;
2386 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2387 output_name, for_name);
2388 return r;
2391 /* If not found, create it, and cache it in inpf. */
2392 r = create_file (for_name, output_name);
2394 gcc_assert (r && r->name);
2395 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2396 output_name, for_name);
2397 inpf->inpoutf = r;
2398 return r;
2403 /* The name of an output file, suitable for definitions, that can see
2404 declarations made in INPF and is linked into every language that
2405 uses INPF. */
2407 const char *
2408 get_output_file_name (input_file* inpf)
2410 outf_p o = get_output_file_with_visibility (inpf);
2411 if (o)
2412 return o->name;
2413 return NULL;
2416 /* Check if existing file is equal to the in memory buffer. */
2418 static bool
2419 is_file_equal (outf_p of)
2421 FILE *newfile = fopen (of->name, "r");
2422 size_t i;
2423 bool equal;
2424 if (newfile == NULL)
2425 return false;
2427 equal = true;
2428 for (i = 0; i < of->bufused; i++)
2430 int ch;
2431 ch = fgetc (newfile);
2432 if (ch == EOF || ch != (unsigned char) of->buf[i])
2434 equal = false;
2435 break;
2438 if (equal && EOF != fgetc (newfile))
2439 equal = false;
2440 fclose (newfile);
2441 return equal;
2444 /* Copy the output to its final destination,
2445 but don't unnecessarily change modification times. */
2447 static void
2448 close_output_files (void)
2450 int nbwrittenfiles = 0;
2451 outf_p of;
2453 for (of = output_files; of; of = of->next)
2455 if (!is_file_equal (of))
2457 FILE *newfile = NULL;
2458 char *backupname = NULL;
2459 /* Back up the old version of the output file gt-FOO.c as
2460 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2461 if (backup_dir)
2463 backupname = concat (backup_dir, "/",
2464 lbasename (of->name), "~", NULL);
2465 if (!access (of->name, F_OK) && rename (of->name, backupname))
2466 fatal ("failed to back up %s as %s: %s",
2467 of->name, backupname, xstrerror (errno));
2470 newfile = fopen (of->name, "w");
2471 if (newfile == NULL)
2472 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2473 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2474 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2475 if (fclose (newfile) != 0)
2476 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2477 nbwrittenfiles++;
2478 if (verbosity_level >= 2 && backupname)
2479 printf ("%s wrote #%-3d %s backed-up in %s\n",
2480 progname, nbwrittenfiles, of->name, backupname);
2481 else if (verbosity_level >= 1)
2482 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2483 free (backupname);
2485 else
2487 /* output file remains unchanged. */
2488 if (verbosity_level >= 2)
2489 printf ("%s keep %s\n", progname, of->name);
2491 free (of->buf);
2492 of->buf = NULL;
2493 of->bufused = of->buflength = 0;
2495 if (verbosity_level >= 1)
2496 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2499 struct flist
2501 struct flist *next;
2502 int started_p;
2503 const input_file* file;
2504 outf_p f;
2507 struct walk_type_data;
2509 /* For scalars and strings, given the item in 'val'.
2510 For structures, given a pointer to the item in 'val'.
2511 For misc. pointers, given the item in 'val'.
2513 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2514 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2516 /* Parameters for write_types. */
2518 struct write_types_data
2520 const char *prefix;
2521 const char *param_prefix;
2522 const char *subfield_marker_routine;
2523 const char *marker_routine;
2524 const char *reorder_note_routine;
2525 const char *comment;
2526 int skip_hooks; /* skip hook generation if non zero */
2527 enum write_types_kinds kind;
2530 static void output_escaped_param (struct walk_type_data *d,
2531 const char *, const char *);
2532 static void output_mangled_typename (outf_p, const_type_p);
2533 static void walk_type (type_p t, struct walk_type_data *d);
2534 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2535 const struct write_types_data *wtd);
2536 static void write_types_process_field
2537 (type_p f, const struct walk_type_data *d);
2538 static void write_types (outf_p output_header,
2539 type_p structures,
2540 type_p param_structs,
2541 const struct write_types_data *wtd);
2542 static void write_types_local_process_field
2543 (type_p f, const struct walk_type_data *d);
2544 static void write_local_func_for_structure
2545 (const_type_p orig_s, type_p s, type_p *param);
2546 static void write_local (outf_p output_header,
2547 type_p structures, type_p param_structs);
2548 static int contains_scalar_p (type_p t);
2549 static void put_mangled_filename (outf_p, const input_file *);
2550 static void finish_root_table (struct flist *flp, const char *pfx,
2551 const char *tname, const char *lastname,
2552 const char *name);
2553 static void write_root (outf_p, pair_p, type_p, const char *, int,
2554 struct fileloc *, const char *, bool);
2555 static void write_array (outf_p f, pair_p v,
2556 const struct write_types_data *wtd);
2557 static void write_roots (pair_p, bool);
2559 /* Parameters for walk_type. */
2561 struct walk_type_data
2563 process_field_fn process_field;
2564 const void *cookie;
2565 outf_p of;
2566 options_p opt;
2567 const char *val;
2568 const char *prev_val[4];
2569 int indent;
2570 int counter;
2571 const struct fileloc *line;
2572 lang_bitmap bitmap;
2573 type_p *param;
2574 int used_length;
2575 type_p orig_s;
2576 const char *reorder_fn;
2577 bool needs_cast_p;
2578 bool fn_wants_lvalue;
2579 bool in_record_p;
2580 int loopcounter;
2581 bool in_ptr_field;
2582 bool have_this_obj;
2586 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2587 pre-processor identifier to use in a #define directive. This replaces
2588 special characters used in C++ identifiers like '>', '<' and ':' with
2589 '_'.
2591 If no C++ special characters are found in TYPE_NAME, return
2592 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2593 characters replaced with '_'. In this case, the caller is
2594 responsible for freeing the allocated string. */
2596 static const char *
2597 filter_type_name (const char *type_name)
2599 if (strchr (type_name, '<') || strchr (type_name, ':'))
2601 size_t i;
2602 char *s = xstrdup (type_name);
2603 for (i = 0; i < strlen (s); i++)
2604 if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ','
2605 || s[i] == '*')
2606 s[i] = '_';
2607 return s;
2609 else
2610 return type_name;
2614 /* Print a mangled name representing T to OF. */
2616 static void
2617 output_mangled_typename (outf_p of, const_type_p t)
2619 if (t == NULL)
2620 oprintf (of, "Z");
2621 else
2622 switch (t->kind)
2624 case TYPE_NONE:
2625 case TYPE_UNDEFINED:
2626 gcc_unreachable ();
2627 break;
2628 case TYPE_POINTER:
2629 oprintf (of, "P");
2630 output_mangled_typename (of, t->u.p);
2631 break;
2632 case TYPE_SCALAR:
2633 oprintf (of, "I");
2634 break;
2635 case TYPE_STRING:
2636 oprintf (of, "S");
2637 break;
2638 case TYPE_STRUCT:
2639 case TYPE_UNION:
2640 case TYPE_LANG_STRUCT:
2641 case TYPE_USER_STRUCT:
2643 /* For references to classes within an inheritance hierarchy,
2644 only ever reference the ultimate base class, since only
2645 it will have gt_ functions. */
2646 t = get_ultimate_base_class (t);
2647 const char *id_for_tag = filter_type_name (t->u.s.tag);
2648 oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2649 id_for_tag);
2650 if (id_for_tag != t->u.s.tag)
2651 free (CONST_CAST (char *, id_for_tag));
2653 break;
2654 case TYPE_PARAM_STRUCT:
2656 int i;
2657 for (i = 0; i < NUM_PARAM; i++)
2658 if (t->u.param_struct.param[i] != NULL)
2659 output_mangled_typename (of, t->u.param_struct.param[i]);
2660 output_mangled_typename (of, t->u.param_struct.stru);
2662 break;
2663 case TYPE_ARRAY:
2664 gcc_unreachable ();
2668 /* Print PARAM to D->OF processing escapes. D->VAL references the
2669 current object, D->PREV_VAL the object containing the current
2670 object, ONAME is the name of the option and D->LINE is used to
2671 print error messages. */
2673 static void
2674 output_escaped_param (struct walk_type_data *d, const char *param,
2675 const char *oname)
2677 const char *p;
2679 for (p = param; *p; p++)
2680 if (*p != '%')
2681 oprintf (d->of, "%c", *p);
2682 else
2683 switch (*++p)
2685 case 'h':
2686 oprintf (d->of, "(%s)", d->prev_val[2]);
2687 break;
2688 case '0':
2689 oprintf (d->of, "(%s)", d->prev_val[0]);
2690 break;
2691 case '1':
2692 oprintf (d->of, "(%s)", d->prev_val[1]);
2693 break;
2694 case 'a':
2696 const char *pp = d->val + strlen (d->val);
2697 while (pp[-1] == ']')
2698 while (*pp != '[')
2699 pp--;
2700 oprintf (d->of, "%s", pp);
2702 break;
2703 default:
2704 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2705 oname, '%', *p);
2709 const char *
2710 get_string_option (options_p opt, const char *key)
2712 for (; opt; opt = opt->next)
2713 if (strcmp (opt->name, key) == 0)
2714 return opt->info.string;
2715 return NULL;
2718 /* Machinery for avoiding duplicate tags within switch statements. */
2719 struct seen_tag
2721 const char *tag;
2722 struct seen_tag *next;
2726 already_seen_tag (struct seen_tag *seen_tags, const char *tag)
2728 /* Linear search, so O(n^2), but n is currently small. */
2729 while (seen_tags)
2731 if (!strcmp (seen_tags->tag, tag))
2732 return 1;
2733 seen_tags = seen_tags->next;
2735 /* Not yet seen this tag. */
2736 return 0;
2739 void
2740 mark_tag_as_seen (struct seen_tag **seen_tags, const char *tag)
2742 /* Add to front of linked list. */
2743 struct seen_tag *new_node = XCNEW (struct seen_tag);
2744 new_node->tag = tag;
2745 new_node->next = *seen_tags;
2746 *seen_tags = new_node;
2749 static void
2750 walk_subclasses (type_p base, struct walk_type_data *d,
2751 struct seen_tag **seen_tags)
2753 for (type_p sub = base->u.s.first_subclass; sub != NULL;
2754 sub = sub->u.s.next_sibling_class)
2756 const char *type_tag = get_string_option (sub->u.s.opt, "tag");
2757 if (type_tag && !already_seen_tag (*seen_tags, type_tag))
2759 mark_tag_as_seen (seen_tags, type_tag);
2760 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
2761 d->indent += 2;
2762 oprintf (d->of, "%*s{\n", d->indent, "");
2763 d->indent += 2;
2764 oprintf (d->of, "%*s%s *sub = static_cast <%s *> (x);\n",
2765 d->indent, "", sub->u.s.tag, sub->u.s.tag);
2766 const char *old_val = d->val;
2767 d->val = "(*sub)";
2768 walk_type (sub, d);
2769 d->val = old_val;
2770 d->indent -= 2;
2771 oprintf (d->of, "%*s}\n", d->indent, "");
2772 oprintf (d->of, "%*sbreak;\n", d->indent, "");
2773 d->indent -= 2;
2775 walk_subclasses (sub, d, seen_tags);
2779 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2780 which is of type T. Write code to D->OF to constrain execution (at
2781 the point that D->PROCESS_FIELD is called) to the appropriate
2782 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2783 pointers to those objects. D->PREV_VAL lists the objects
2784 containing the current object, D->OPT is a list of options to
2785 apply, D->INDENT is the current indentation level, D->LINE is used
2786 to print error messages, D->BITMAP indicates which languages to
2787 print the structure for, and D->PARAM is the current parameter
2788 (from an enclosing param_is option). */
2790 static void
2791 walk_type (type_p t, struct walk_type_data *d)
2793 const char *length = NULL;
2794 const char *desc = NULL;
2795 const char *type_tag = NULL;
2796 int maybe_undef_p = 0;
2797 int use_param_num = -1;
2798 int use_params_p = 0;
2799 int atomic_p = 0;
2800 options_p oo;
2801 const struct nested_ptr_data *nested_ptr_d = NULL;
2803 d->needs_cast_p = false;
2804 for (oo = d->opt; oo; oo = oo->next)
2805 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2806 length = oo->info.string;
2807 else if (strcmp (oo->name, "maybe_undef") == 0)
2808 maybe_undef_p = 1;
2809 else if (strncmp (oo->name, "use_param", 9) == 0
2810 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2811 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2812 else if (strcmp (oo->name, "use_params") == 0)
2813 use_params_p = 1;
2814 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2815 desc = oo->info.string;
2816 else if (strcmp (oo->name, "mark_hook") == 0)
2818 else if (strcmp (oo->name, "nested_ptr") == 0
2819 && oo->kind == OPTION_NESTED)
2820 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2821 else if (strcmp (oo->name, "dot") == 0)
2823 else if (strcmp (oo->name, "tag") == 0)
2824 type_tag = oo->info.string;
2825 else if (strcmp (oo->name, "special") == 0)
2827 else if (strcmp (oo->name, "skip") == 0)
2829 else if (strcmp (oo->name, "atomic") == 0)
2830 atomic_p = 1;
2831 else if (strcmp (oo->name, "default") == 0)
2833 else if (strcmp (oo->name, "param_is") == 0)
2835 else if (strncmp (oo->name, "param", 5) == 0
2836 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2838 else if (strcmp (oo->name, "chain_next") == 0)
2840 else if (strcmp (oo->name, "chain_prev") == 0)
2842 else if (strcmp (oo->name, "chain_circular") == 0)
2844 else if (strcmp (oo->name, "reorder") == 0)
2846 else if (strcmp (oo->name, "variable_size") == 0)
2848 else
2849 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2851 if (d->used_length)
2852 length = NULL;
2854 if (use_params_p)
2856 int pointer_p = t->kind == TYPE_POINTER;
2858 if (pointer_p)
2859 t = t->u.p;
2860 if (!union_or_struct_p (t))
2861 error_at_line (d->line, "`use_params' option on unimplemented type");
2862 else
2863 t = find_param_structure (t, d->param);
2864 if (pointer_p)
2865 t = create_pointer (t);
2868 if (use_param_num != -1)
2870 if (d->param != NULL && d->param[use_param_num] != NULL)
2872 type_p nt = d->param[use_param_num];
2874 if (t->kind == TYPE_ARRAY)
2875 nt = create_array (nt, t->u.a.len);
2876 else if (length != NULL && t->kind == TYPE_POINTER)
2877 nt = create_pointer (nt);
2878 d->needs_cast_p = (t->kind != TYPE_POINTER
2879 && (nt->kind == TYPE_POINTER
2880 || nt->kind == TYPE_STRING));
2881 t = nt;
2883 else
2884 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2887 if (maybe_undef_p
2888 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
2890 error_at_line (d->line,
2891 "field `%s' has invalid option `maybe_undef_p'\n",
2892 d->val);
2893 return;
2896 if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2898 error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2899 return;
2902 switch (t->kind)
2904 case TYPE_SCALAR:
2905 case TYPE_STRING:
2906 d->process_field (t, d);
2907 break;
2909 case TYPE_POINTER:
2911 d->in_ptr_field = true;
2912 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2914 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2915 break;
2918 /* If a pointer type is marked as "atomic", we process the
2919 field itself, but we don't walk the data that they point to.
2921 There are two main cases where we walk types: to mark
2922 pointers that are reachable, and to relocate pointers when
2923 writing a PCH file. In both cases, an atomic pointer is
2924 itself marked or relocated, but the memory that it points
2925 to is left untouched. In the case of PCH, that memory will
2926 be read/written unchanged to the PCH file. */
2927 if (atomic_p)
2929 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2930 d->indent += 2;
2931 d->process_field (t, d);
2932 d->indent -= 2;
2933 oprintf (d->of, "%*s}\n", d->indent, "");
2934 break;
2937 if (!length)
2939 if (!union_or_struct_p (t->u.p)
2940 && t->u.p->kind != TYPE_PARAM_STRUCT)
2942 error_at_line (d->line,
2943 "field `%s' is pointer to unimplemented type",
2944 d->val);
2945 break;
2948 if (nested_ptr_d)
2950 const char *oldprevval2 = d->prev_val[2];
2952 if (!union_or_struct_p (nested_ptr_d->type))
2954 error_at_line (d->line,
2955 "field `%s' has invalid "
2956 "option `nested_ptr'\n", d->val);
2957 return;
2960 d->prev_val[2] = d->val;
2961 oprintf (d->of, "%*s{\n", d->indent, "");
2962 d->indent += 2;
2963 d->val = xasprintf ("x%d", d->counter++);
2964 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2965 (nested_ptr_d->type->kind == TYPE_UNION
2966 ? "union" : "struct"),
2967 nested_ptr_d->type->u.s.tag,
2968 d->fn_wants_lvalue ? "" : "const ", d->val);
2969 oprintf (d->of, "%*s", d->indent + 2, "");
2970 output_escaped_param (d, nested_ptr_d->convert_from,
2971 "nested_ptr");
2972 oprintf (d->of, ";\n");
2974 d->process_field (nested_ptr_d->type, d);
2976 if (d->fn_wants_lvalue)
2978 oprintf (d->of, "%*s%s = ", d->indent, "",
2979 d->prev_val[2]);
2980 d->prev_val[2] = d->val;
2981 output_escaped_param (d, nested_ptr_d->convert_to,
2982 "nested_ptr");
2983 oprintf (d->of, ";\n");
2986 d->indent -= 2;
2987 oprintf (d->of, "%*s}\n", d->indent, "");
2988 d->val = d->prev_val[2];
2989 d->prev_val[2] = oldprevval2;
2991 else
2992 d->process_field (t->u.p, d);
2994 else
2996 int loopcounter = d->loopcounter;
2997 const char *oldval = d->val;
2998 const char *oldprevval3 = d->prev_val[3];
2999 char *newval;
3001 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
3002 d->indent += 2;
3003 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
3004 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
3005 "", loopcounter, loopcounter);
3006 if (!d->in_record_p)
3007 output_escaped_param (d, length, "length");
3008 else
3009 oprintf (d->of, "l%d", loopcounter);
3010 if (d->have_this_obj)
3011 /* Try to unswitch loops (see PR53880). */
3012 oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
3013 oprintf (d->of, "); i%d++) {\n", loopcounter);
3014 d->indent += 2;
3015 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
3016 d->used_length = 1;
3017 d->prev_val[3] = oldval;
3018 walk_type (t->u.p, d);
3019 free (newval);
3020 d->val = oldval;
3021 d->prev_val[3] = oldprevval3;
3022 d->used_length = 0;
3023 d->indent -= 2;
3024 oprintf (d->of, "%*s}\n", d->indent, "");
3025 d->process_field (t, d);
3026 d->indent -= 2;
3027 oprintf (d->of, "%*s}\n", d->indent, "");
3029 d->in_ptr_field = false;
3031 break;
3033 case TYPE_ARRAY:
3035 int loopcounter;
3036 const char *oldval = d->val;
3037 char *newval;
3039 /* If it's an array of scalars, we optimize by not generating
3040 any code. */
3041 if (t->u.a.p->kind == TYPE_SCALAR)
3042 break;
3044 if (length)
3045 loopcounter = d->loopcounter;
3046 else
3047 loopcounter = d->counter++;
3049 /* When walking an array, compute the length and store it in a
3050 local variable before walking the array elements, instead of
3051 recomputing the length expression each time through the loop.
3052 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
3053 where the length is stored in the first array element,
3054 because otherwise that operand can get overwritten on the
3055 first iteration. */
3056 oprintf (d->of, "%*s{\n", d->indent, "");
3057 d->indent += 2;
3058 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
3059 if (!d->in_record_p || !length)
3061 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
3062 d->indent, "", loopcounter);
3063 if (length)
3064 output_escaped_param (d, length, "length");
3065 else
3066 oprintf (d->of, "%s", t->u.a.len);
3067 oprintf (d->of, ");\n");
3070 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
3071 d->indent, "",
3072 loopcounter, loopcounter, loopcounter, loopcounter);
3073 d->indent += 2;
3074 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
3075 d->used_length = 1;
3076 walk_type (t->u.a.p, d);
3077 free (newval);
3078 d->used_length = 0;
3079 d->val = oldval;
3080 d->indent -= 2;
3081 oprintf (d->of, "%*s}\n", d->indent, "");
3082 d->indent -= 2;
3083 oprintf (d->of, "%*s}\n", d->indent, "");
3085 break;
3087 case TYPE_STRUCT:
3088 case TYPE_UNION:
3090 pair_p f;
3091 const char *oldval = d->val;
3092 const char *oldprevval1 = d->prev_val[1];
3093 const char *oldprevval2 = d->prev_val[2];
3094 const char *struct_mark_hook = NULL;
3095 const int union_p = t->kind == TYPE_UNION;
3096 int seen_default_p = 0;
3097 options_p o;
3098 int lengths_seen = 0;
3099 int endcounter;
3100 bool any_length_seen = false;
3102 if (!t->u.s.line.file)
3103 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
3105 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
3107 error_at_line (d->line,
3108 "structure `%s' defined for mismatching languages",
3109 t->u.s.tag);
3110 error_at_line (&t->u.s.line, "one structure defined here");
3113 /* Some things may also be defined in the structure's options. */
3114 for (o = t->u.s.opt; o; o = o->next)
3115 if (!desc && strcmp (o->name, "desc") == 0
3116 && o->kind == OPTION_STRING)
3117 desc = o->info.string;
3118 else if (!struct_mark_hook && strcmp (o->name, "mark_hook") == 0
3119 && o->kind == OPTION_STRING)
3120 struct_mark_hook = o->info.string;
3122 if (struct_mark_hook)
3123 oprintf (d->of, "%*s%s (&%s);\n",
3124 d->indent, "", struct_mark_hook, oldval);
3126 d->prev_val[2] = oldval;
3127 d->prev_val[1] = oldprevval2;
3128 if (union_p)
3130 if (desc == NULL)
3132 error_at_line (d->line,
3133 "missing `desc' option for union `%s'",
3134 t->u.s.tag);
3135 desc = "1";
3137 oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
3138 output_escaped_param (d, desc, "desc");
3139 oprintf (d->of, "))\n");
3140 d->indent += 2;
3141 oprintf (d->of, "%*s{\n", d->indent, "");
3143 else if (desc)
3145 /* We have a "desc" option on a struct, signifying the
3146 base class within a GC-managed inheritance hierarchy.
3147 The current code specialcases the base class, then walks
3148 into subclasses, recursing into this routine to handle them.
3149 This organization requires the base class to have a case in
3150 the switch statement, and hence a tag value is mandatory
3151 for the base class. This restriction could be removed, but
3152 it would require some restructing of this code. */
3153 if (!type_tag)
3155 error_at_line (d->line,
3156 "missing `tag' option for type `%s'",
3157 t->u.s.tag);
3159 oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
3160 output_escaped_param (d, desc, "desc");
3161 oprintf (d->of, "))\n");
3162 d->indent += 2;
3163 oprintf (d->of, "%*s{\n", d->indent, "");
3164 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
3165 d->indent += 2;
3168 FOR_ALL_INHERITED_FIELDS (t, f)
3170 options_p oo;
3171 int skip_p = 0;
3172 const char *fieldlength = NULL;
3174 d->reorder_fn = NULL;
3175 for (oo = f->opt; oo; oo = oo->next)
3176 if (strcmp (oo->name, "skip") == 0)
3177 skip_p = 1;
3178 else if (strcmp (oo->name, "length") == 0
3179 && oo->kind == OPTION_STRING)
3180 fieldlength = oo->info.string;
3182 if (skip_p)
3183 continue;
3184 if (fieldlength)
3186 lengths_seen++;
3187 d->counter++;
3188 if (!union_p)
3190 if (!any_length_seen)
3192 oprintf (d->of, "%*s{\n", d->indent, "");
3193 d->indent += 2;
3195 any_length_seen = true;
3197 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
3198 d->indent, "", d->counter - 1);
3199 output_escaped_param (d, fieldlength, "length");
3200 oprintf (d->of, ");\n");
3204 endcounter = d->counter;
3206 FOR_ALL_INHERITED_FIELDS (t, f)
3208 options_p oo;
3209 const char *dot = ".";
3210 const char *tagid = NULL;
3211 int skip_p = 0;
3212 int default_p = 0;
3213 int use_param_p = 0;
3214 const char *fieldlength = NULL;
3215 char *newval;
3217 d->reorder_fn = NULL;
3218 for (oo = f->opt; oo; oo = oo->next)
3219 if (strcmp (oo->name, "dot") == 0
3220 && oo->kind == OPTION_STRING)
3221 dot = oo->info.string;
3222 else if (strcmp (oo->name, "tag") == 0
3223 && oo->kind == OPTION_STRING)
3224 tagid = oo->info.string;
3225 else if (strcmp (oo->name, "skip") == 0)
3226 skip_p = 1;
3227 else if (strcmp (oo->name, "default") == 0)
3228 default_p = 1;
3229 else if (strcmp (oo->name, "reorder") == 0
3230 && oo->kind == OPTION_STRING)
3231 d->reorder_fn = oo->info.string;
3232 else if (strncmp (oo->name, "use_param", 9) == 0
3233 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
3234 use_param_p = 1;
3235 else if (strcmp (oo->name, "length") == 0
3236 && oo->kind == OPTION_STRING)
3237 fieldlength = oo->info.string;
3239 if (skip_p)
3240 continue;
3242 if (union_p && tagid)
3244 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
3245 d->indent += 2;
3247 else if (union_p && default_p)
3249 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3250 d->indent += 2;
3251 seen_default_p = 1;
3253 else if (!union_p && (default_p || tagid))
3254 error_at_line (d->line,
3255 "can't use `%s' outside a union on field `%s'",
3256 default_p ? "default" : "tag", f->name);
3257 else if (union_p && !(default_p || tagid)
3258 && f->type->kind == TYPE_SCALAR)
3260 fprintf (stderr,
3261 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3262 get_input_file_name (d->line->file), d->line->line,
3263 f->name);
3264 continue;
3266 else if (union_p && !(default_p || tagid))
3267 error_at_line (d->line,
3268 "field `%s' is missing `tag' or `default' option",
3269 f->name);
3271 if (fieldlength)
3273 d->loopcounter = endcounter - lengths_seen--;
3276 d->line = &f->line;
3277 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
3278 d->opt = f->opt;
3279 d->used_length = false;
3280 d->in_record_p = !union_p;
3282 if (union_p && use_param_p && d->param == NULL)
3283 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
3284 else
3285 walk_type (f->type, d);
3287 d->in_record_p = false;
3289 free (newval);
3291 if (union_p)
3293 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3294 d->indent -= 2;
3297 d->reorder_fn = NULL;
3299 d->val = oldval;
3300 d->prev_val[1] = oldprevval1;
3301 d->prev_val[2] = oldprevval2;
3303 if (union_p && !seen_default_p)
3305 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3306 oprintf (d->of, "%*s break;\n", d->indent, "");
3309 if (desc && !union_p)
3311 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3312 d->indent -= 2;
3314 if (union_p)
3316 oprintf (d->of, "%*s}\n", d->indent, "");
3317 d->indent -= 2;
3319 else if (desc)
3321 /* Add cases to handle subclasses. */
3322 struct seen_tag *tags = NULL;
3323 walk_subclasses (t, d, &tags);
3325 /* Ensure that if someone forgets a "tag" option that we don't
3326 silent fail to traverse that subclass's fields. */
3327 if (!seen_default_p)
3329 oprintf (d->of, "%*s/* Unrecognized tag value. */\n",
3330 d->indent, "");
3331 oprintf (d->of, "%*sdefault: gcc_unreachable (); \n",
3332 d->indent, "");
3335 /* End of the switch statement */
3336 oprintf (d->of, "%*s}\n", d->indent, "");
3337 d->indent -= 2;
3339 if (any_length_seen)
3341 d->indent -= 2;
3342 oprintf (d->of, "%*s}\n", d->indent, "");
3345 break;
3347 case TYPE_LANG_STRUCT:
3349 type_p nt;
3350 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3351 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3352 break;
3353 if (nt == NULL)
3354 error_at_line (d->line, "structure `%s' differs between languages",
3355 t->u.s.tag);
3356 else
3357 walk_type (nt, d);
3359 break;
3361 case TYPE_PARAM_STRUCT:
3363 type_p *oldparam = d->param;
3365 d->param = t->u.param_struct.param;
3366 walk_type (t->u.param_struct.stru, d);
3367 d->param = oldparam;
3369 break;
3371 case TYPE_USER_STRUCT:
3372 d->process_field (t, d);
3373 break;
3375 case TYPE_NONE:
3376 case TYPE_UNDEFINED:
3377 gcc_unreachable ();
3381 /* process_field routine for marking routines. */
3383 static void
3384 write_types_process_field (type_p f, const struct walk_type_data *d)
3386 const struct write_types_data *wtd;
3387 const char *cast = d->needs_cast_p ? "(void *)" : "";
3388 wtd = (const struct write_types_data *) d->cookie;
3390 switch (f->kind)
3392 case TYPE_NONE:
3393 case TYPE_UNDEFINED:
3394 gcc_unreachable ();
3395 case TYPE_POINTER:
3396 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
3397 wtd->subfield_marker_routine, cast, d->val);
3398 if (wtd->param_prefix)
3400 if (f->u.p->kind == TYPE_SCALAR)
3401 /* The current type is a pointer to a scalar (so not
3402 considered like a pointer to instances of user defined
3403 types) and we are seeing it; it means we must be even
3404 more careful about the second argument of the
3405 SUBFIELD_MARKER_ROUTINE call. That argument must
3406 always be the instance of the type for which
3407 write_func_for_structure was called - this really is
3408 what the function SUBFIELD_MARKER_ROUTINE expects.
3409 That is, it must be an instance of the ORIG_S type
3410 parameter of write_func_for_structure. The convention
3411 is that that argument must be "x" in that case (as set
3412 by write_func_for_structure). The problem is, we can't
3413 count on d->prev_val[3] to be always set to "x" in that
3414 case. Sometimes walk_type can set it to something else
3415 (to e.g cooperate with write_array when called from
3416 write_roots). So let's set it to "x" here then. */
3417 oprintf (d->of, ", x");
3418 else
3419 oprintf (d->of, ", %s", d->prev_val[3]);
3420 if (d->orig_s)
3422 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3423 output_mangled_typename (d->of, d->orig_s);
3425 else
3426 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3428 oprintf (d->of, ");\n");
3429 if (d->reorder_fn && wtd->reorder_note_routine)
3430 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
3431 wtd->reorder_note_routine, cast, d->val,
3432 d->prev_val[3], d->reorder_fn);
3433 break;
3435 case TYPE_STRING:
3436 case TYPE_STRUCT:
3437 case TYPE_UNION:
3438 case TYPE_LANG_STRUCT:
3439 case TYPE_PARAM_STRUCT:
3440 case TYPE_USER_STRUCT:
3441 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3443 /* If F is a user-defined type and the field is not a
3444 pointer to the type, then we should not generate the
3445 standard pointer-marking code. All we need to do is call
3446 the user-provided marking function to process the fields
3447 of F. */
3448 oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3449 d->val);
3451 else
3453 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3454 output_mangled_typename (d->of, f);
3455 oprintf (d->of, " (%s%s);\n", cast, d->val);
3456 if (d->reorder_fn && wtd->reorder_note_routine)
3457 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
3458 wtd->reorder_note_routine, cast, d->val, cast, d->val,
3459 d->reorder_fn);
3461 break;
3463 case TYPE_SCALAR:
3464 break;
3466 case TYPE_ARRAY:
3467 gcc_unreachable ();
3471 /* Return an output file that is suitable for definitions which can
3472 reference struct S */
3474 static outf_p
3475 get_output_file_for_structure (const_type_p s, type_p *param)
3477 const input_file *fn;
3478 int i;
3480 gcc_assert (union_or_struct_p (s));
3481 fn = s->u.s.line.file;
3483 /* This is a hack, and not the good kind either. */
3484 for (i = NUM_PARAM - 1; i >= 0; i--)
3485 if (param && param[i] && param[i]->kind == TYPE_POINTER
3486 && union_or_struct_p (param[i]->u.p))
3487 fn = param[i]->u.p->u.s.line.file;
3489 /* The call to get_output_file_with_visibility may update fn by
3490 caching its result inside, so we need the CONST_CAST. */
3491 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3495 /* Returns the specifier keyword for a string or union type S, empty string
3496 otherwise. */
3498 static const char *
3499 get_type_specifier (const type_p s)
3501 if (s->kind == TYPE_STRUCT)
3502 return "struct ";
3503 else if (s->kind == TYPE_LANG_STRUCT)
3504 return get_type_specifier (s->u.s.lang_struct);
3505 else if (s->kind == TYPE_UNION)
3506 return "union ";
3507 return "";
3511 /* Emits a declaration for type TY (assumed to be a union or a
3512 structure) on stream OUT. */
3514 static void
3515 write_type_decl (outf_p out, type_p ty)
3517 if (union_or_struct_p (ty))
3518 oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3519 else if (ty->kind == TYPE_SCALAR)
3521 if (ty->u.scalar_is_char)
3522 oprintf (out, "const char");
3523 else
3524 oprintf (out, "void");
3526 else if (ty->kind == TYPE_POINTER)
3528 write_type_decl (out, ty->u.p);
3529 oprintf (out, " *");
3531 else if (ty->kind == TYPE_ARRAY)
3533 write_type_decl (out, ty->u.a.p);
3534 oprintf (out, " *");
3536 else if (ty->kind == TYPE_STRING)
3538 oprintf (out, "const char *");
3540 else
3541 gcc_unreachable ();
3545 /* Write on OF the name of the marker function for structure S. PREFIX
3546 is the prefix to use (to distinguish ggc from pch markers). */
3548 static void
3549 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3551 if (union_or_struct_p (s))
3553 const char *id_for_tag = filter_type_name (s->u.s.tag);
3554 oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3555 if (id_for_tag != s->u.s.tag)
3556 free (CONST_CAST (char *, id_for_tag));
3558 else if (s->kind == TYPE_PARAM_STRUCT)
3560 oprintf (of, "gt_%s_", prefix);
3561 output_mangled_typename (of, s);
3563 else
3564 gcc_unreachable ();
3567 /* Write on OF a user-callable routine to act as an entry point for
3568 the marking routine for S, generated by write_func_for_structure.
3569 WTD distinguishes between ggc and pch markers. */
3571 static void
3572 write_user_func_for_structure_ptr (outf_p of, type_p s, const write_types_data *wtd)
3574 /* Parameterized structures are not supported in user markers. There
3575 is no way for the marker function to know which specific type
3576 to use to generate the call to the void * entry point. For
3577 instance, a marker for struct htab may need to call different
3578 routines to mark the fields, depending on the paramN_is attributes.
3580 A user-defined marker that accepts 'struct htab' as its argument
3581 would not know which variant to call. Generating several entry
3582 points accepting 'struct htab' would cause multiply-defined
3583 errors during compilation. */
3584 gcc_assert (union_or_struct_p (s));
3586 type_p alias_of = NULL;
3587 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3588 if (strcmp (opt->name, "ptr_alias") == 0)
3590 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3591 we do not generate marking code for ORIG_S here. Instead, a
3592 forwarder #define in gtype-desc.h will cause every call to its
3593 marker to call the target of this alias.
3595 However, we still want to create a user entry code for the
3596 aliased type. So, if ALIAS_OF is set, we only generate the
3597 user-callable marker function. */
3598 alias_of = opt->info.type;
3599 break;
3602 DBGPRINTF ("write_user_func_for_structure_ptr: %s %s", s->u.s.tag,
3603 wtd->prefix);
3605 /* Only write the function once. */
3606 if (s->u.s.wrote_user_func_for_ptr[wtd->kind])
3607 return;
3608 s->u.s.wrote_user_func_for_ptr[wtd->kind] = true;
3610 oprintf (of, "\nvoid\n");
3611 oprintf (of, "gt_%sx (", wtd->prefix);
3612 write_type_decl (of, s);
3613 oprintf (of, " *& x)\n");
3614 oprintf (of, "{\n");
3615 oprintf (of, " if (x)\n ");
3616 write_marker_function_name (of,
3617 alias_of ? alias_of : get_ultimate_base_class (s),
3618 wtd->prefix);
3619 oprintf (of, " ((void *) x);\n");
3620 oprintf (of, "}\n");
3624 /* Write a function to mark all the fields of type S on OF. PREFIX
3625 and D are as in write_user_marking_functions. */
3627 static void
3628 write_user_func_for_structure_body (type_p s, const char *prefix,
3629 struct walk_type_data *d)
3631 oprintf (d->of, "\nvoid\n");
3632 oprintf (d->of, "gt_%sx (", prefix);
3633 write_type_decl (d->of, s);
3634 oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3635 oprintf (d->of, "{\n");
3636 oprintf (d->of, " ");
3637 write_type_decl (d->of, s);
3638 oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3639 d->val = "(*x)";
3640 d->indent = 2;
3641 walk_type (s, d);
3642 oprintf (d->of, "}\n");
3646 /* Emit the user-callable functions needed to mark all the types used
3647 by the user structure S. PREFIX is the prefix to use to
3648 distinguish ggc and pch markers. D contains data needed to pass to
3649 walk_type when traversing the fields of a type.
3651 For every type T referenced by S, two routines are generated: one
3652 that takes 'T *', marks the pointer and calls the second routine,
3653 which just marks the fields of T. */
3655 static void
3656 write_user_marking_functions (type_p s,
3657 const write_types_data *w,
3658 struct walk_type_data *d)
3660 gcc_assert (s->kind == TYPE_USER_STRUCT);
3662 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3664 type_p fld_type = fld->type;
3665 if (fld_type->kind == TYPE_POINTER)
3667 type_p pointed_to_type = fld_type->u.p;
3668 if (union_or_struct_p (pointed_to_type))
3669 write_user_func_for_structure_ptr (d->of, pointed_to_type, w);
3671 else if (union_or_struct_p (fld_type))
3672 write_user_func_for_structure_body (fld_type, w->prefix, d);
3677 /* For S, a structure that's part of ORIG_S, and using parameters
3678 PARAM, write out a routine that:
3679 - Takes a parameter, a void * but actually of type *S
3680 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3681 field of S or its substructures and (in some cases) things
3682 that are pointed to by S. */
3684 static void
3685 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
3686 const struct write_types_data *wtd)
3688 const char *chain_next = NULL;
3689 const char *chain_prev = NULL;
3690 const char *chain_circular = NULL;
3691 const char *mark_hook_name = NULL;
3692 options_p opt;
3693 struct walk_type_data d;
3695 if (s->u.s.base_class)
3697 /* Verify that the base class has a "desc", since otherwise
3698 the traversal hooks there won't attempt to visit fields of
3699 subclasses such as this one. */
3700 const_type_p ubc = get_ultimate_base_class (s);
3701 if ((!opts_have (ubc->u.s.opt, "user")
3702 && !opts_have (ubc->u.s.opt, "desc")))
3703 error_at_line (&s->u.s.line,
3704 ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
3705 ", but '%s' lacks a discriminator 'desc' option"),
3706 s->u.s.tag, ubc->u.s.tag, ubc->u.s.tag);
3708 /* Don't write fns for subclasses, only for the ultimate base class
3709 within an inheritance hierarchy. */
3710 return;
3713 memset (&d, 0, sizeof (d));
3714 d.of = get_output_file_for_structure (s, param);
3715 for (opt = s->u.s.opt; opt; opt = opt->next)
3716 if (strcmp (opt->name, "chain_next") == 0
3717 && opt->kind == OPTION_STRING)
3718 chain_next = opt->info.string;
3719 else if (strcmp (opt->name, "chain_prev") == 0
3720 && opt->kind == OPTION_STRING)
3721 chain_prev = opt->info.string;
3722 else if (strcmp (opt->name, "chain_circular") == 0
3723 && opt->kind == OPTION_STRING)
3724 chain_circular = opt->info.string;
3725 else if (strcmp (opt->name, "mark_hook") == 0
3726 && opt->kind == OPTION_STRING)
3727 mark_hook_name = opt->info.string;
3728 if (chain_prev != NULL && chain_next == NULL)
3729 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3730 if (chain_circular != NULL && chain_next != NULL)
3731 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3732 if (chain_circular != NULL)
3733 chain_next = chain_circular;
3735 d.process_field = write_types_process_field;
3736 d.cookie = wtd;
3737 d.orig_s = orig_s;
3738 d.opt = s->u.s.opt;
3739 d.line = &s->u.s.line;
3740 d.bitmap = s->u.s.bitmap;
3741 d.param = param;
3742 d.prev_val[0] = "*x";
3743 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3744 d.prev_val[3] = "x";
3745 d.val = "(*x)";
3746 d.have_this_obj = false;
3748 oprintf (d.of, "\n");
3749 oprintf (d.of, "void\n");
3750 write_marker_function_name (d.of, orig_s, wtd->prefix);
3751 oprintf (d.of, " (void *x_p)\n");
3752 oprintf (d.of, "{\n ");
3753 write_type_decl (d.of, s);
3754 oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3755 write_type_decl (d.of, s);
3756 oprintf (d.of, " *)x_p;\n");
3757 if (chain_next != NULL)
3759 /* TYPE_USER_STRUCTs should not occur here. These structures
3760 are completely handled by user code. */
3761 gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
3763 oprintf (d.of, " ");
3764 write_type_decl (d.of, s);
3765 oprintf (d.of, " * xlimit = x;\n");
3767 if (chain_next == NULL)
3769 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3770 if (wtd->param_prefix)
3772 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3773 output_mangled_typename (d.of, orig_s);
3775 oprintf (d.of, "))\n");
3777 else
3779 if (chain_circular != NULL)
3780 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3781 else
3782 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3783 if (wtd->param_prefix)
3785 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3786 output_mangled_typename (d.of, orig_s);
3788 oprintf (d.of, "))\n");
3789 if (chain_circular != NULL)
3790 oprintf (d.of, " return;\n do\n");
3791 if (mark_hook_name && !wtd->skip_hooks)
3793 oprintf (d.of, " {\n");
3794 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
3796 oprintf (d.of, " xlimit = (");
3797 d.prev_val[2] = "*xlimit";
3798 output_escaped_param (&d, chain_next, "chain_next");
3799 oprintf (d.of, ");\n");
3800 if (mark_hook_name && !wtd->skip_hooks)
3801 oprintf (d.of, " }\n");
3802 if (chain_prev != NULL)
3804 oprintf (d.of, " if (x != xlimit)\n");
3805 oprintf (d.of, " for (;;)\n");
3806 oprintf (d.of, " {\n");
3807 oprintf (d.of, " %s %s * const xprev = (",
3808 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3810 d.prev_val[2] = "*x";
3811 output_escaped_param (&d, chain_prev, "chain_prev");
3812 oprintf (d.of, ");\n");
3813 oprintf (d.of, " if (xprev == NULL) break;\n");
3814 oprintf (d.of, " x = xprev;\n");
3815 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3816 if (wtd->param_prefix)
3818 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3819 output_mangled_typename (d.of, orig_s);
3821 oprintf (d.of, ");\n");
3822 oprintf (d.of, " }\n");
3824 if (chain_circular != NULL)
3826 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3827 if (wtd->param_prefix)
3829 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3830 output_mangled_typename (d.of, orig_s);
3832 oprintf (d.of, "));\n");
3833 if (mark_hook_name && !wtd->skip_hooks)
3834 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
3835 oprintf (d.of, " do\n");
3837 else
3838 oprintf (d.of, " while (x != xlimit)\n");
3840 oprintf (d.of, " {\n");
3841 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3843 oprintf (d.of, " %s (x);\n", mark_hook_name);
3846 d.prev_val[2] = "*x";
3847 d.indent = 6;
3848 if (orig_s->kind != TYPE_USER_STRUCT)
3849 walk_type (s, &d);
3850 else
3852 /* User structures have no fields to walk. Simply generate a call
3853 to the user-provided structure marker. */
3854 oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3857 if (chain_next != NULL)
3859 oprintf (d.of, " x = (");
3860 output_escaped_param (&d, chain_next, "chain_next");
3861 oprintf (d.of, ");\n");
3864 oprintf (d.of, " }\n");
3865 if (chain_circular != NULL)
3866 oprintf (d.of, " while (x != xlimit);\n");
3867 oprintf (d.of, "}\n");
3869 if (orig_s->kind == TYPE_USER_STRUCT)
3870 write_user_marking_functions (orig_s, wtd, &d);
3874 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3876 static void
3877 write_types (outf_p output_header, type_p structures, type_p param_structs,
3878 const struct write_types_data *wtd)
3880 int nbfun = 0; /* Count the emitted functions. */
3881 type_p s;
3883 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3885 /* We first emit the macros and the declarations. Functions' code is
3886 emitted afterwards. This is needed in plugin mode. */
3887 oprintf (output_header, "/* Macros and declarations. */\n");
3888 for (s = structures; s; s = s->next)
3889 /* Do not emit handlers for derived classes; we only ever deal with
3890 the ultimate base class within an inheritance hierarchy. */
3891 if ((s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3892 && !s->u.s.base_class)
3894 options_p opt;
3896 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3897 continue;
3899 const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3901 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3902 output_mangled_typename (output_header, s);
3903 oprintf (output_header, "(X) do { \\\n");
3904 oprintf (output_header,
3905 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3906 s_id_for_tag);
3907 oprintf (output_header, " } while (0)\n");
3909 for (opt = s->u.s.opt; opt; opt = opt->next)
3910 if (strcmp (opt->name, "ptr_alias") == 0
3911 && opt->kind == OPTION_TYPE)
3913 const_type_p const t = (const_type_p) opt->info.type;
3914 if (t->kind == TYPE_STRUCT
3915 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3917 const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3918 oprintf (output_header,
3919 "#define gt_%sx_%s gt_%sx_%s\n",
3920 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3921 if (t_id_for_tag != t->u.s.tag)
3922 free (CONST_CAST (char *, t_id_for_tag));
3924 else
3925 error_at_line (&s->u.s.line,
3926 "structure alias is not a structure");
3927 break;
3929 if (opt)
3930 continue;
3932 /* Declare the marker procedure only once. */
3933 oprintf (output_header,
3934 "extern void gt_%sx_%s (void *);\n",
3935 wtd->prefix, s_id_for_tag);
3937 if (s_id_for_tag != s->u.s.tag)
3938 free (CONST_CAST (char *, s_id_for_tag));
3940 if (s->u.s.line.file == NULL)
3942 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3943 s->u.s.tag);
3944 continue;
3948 for (s = param_structs; s; s = s->next)
3949 if (s->gc_used == GC_POINTED_TO)
3951 type_p stru = s->u.param_struct.stru;
3953 /* Declare the marker procedure. */
3954 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
3955 output_mangled_typename (output_header, s);
3956 oprintf (output_header, " (void *);\n");
3958 if (stru->u.s.line.file == NULL)
3960 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3961 stru->u.s.tag);
3962 continue;
3966 /* At last we emit the functions code. */
3967 oprintf (output_header, "\n/* functions code */\n");
3968 for (s = structures; s; s = s->next)
3969 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3971 options_p opt;
3973 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3974 continue;
3975 for (opt = s->u.s.opt; opt; opt = opt->next)
3976 if (strcmp (opt->name, "ptr_alias") == 0)
3977 break;
3978 if (opt)
3979 continue;
3981 if (s->kind == TYPE_LANG_STRUCT)
3983 type_p ss;
3984 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3986 nbfun++;
3987 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3988 nbfun, (void*) ss, ss->u.s.tag);
3989 write_func_for_structure (s, ss, NULL, wtd);
3992 else
3994 nbfun++;
3995 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3996 nbfun, (void*) s, s->u.s.tag);
3997 write_func_for_structure (s, s, NULL, wtd);
4000 else
4002 /* Structure s is not possibly pointed to, so can be ignored. */
4003 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
4004 (void*)s, s->u.s.tag,
4005 (int) s->gc_used);
4008 for (s = param_structs; s; s = s->next)
4009 if (s->gc_used == GC_POINTED_TO)
4011 type_p *param = s->u.param_struct.param;
4012 type_p stru = s->u.param_struct.stru;
4013 if (stru->u.s.line.file == NULL)
4014 continue;
4015 if (stru->kind == TYPE_LANG_STRUCT)
4017 type_p ss;
4018 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
4020 nbfun++;
4021 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
4022 nbfun, (void*) ss, ss->u.s.tag);
4023 write_func_for_structure (s, ss, param, wtd);
4026 else
4028 nbfun++;
4029 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
4030 nbfun, (void*) s,
4031 (void*) stru, stru->u.s.tag);
4032 write_func_for_structure (s, stru, param, wtd);
4035 else
4037 /* Param structure s is not pointed to, so should be ignored. */
4038 DBGPRINTF ("ignored s @ %p", (void*)s);
4040 if (verbosity_level >= 2)
4041 printf ("%s emitted %d routines for %s\n",
4042 progname, nbfun, wtd->comment);
4045 static const struct write_types_data ggc_wtd = {
4046 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
4047 "GC marker procedures. ",
4048 FALSE, WTK_GGC
4051 static const struct write_types_data pch_wtd = {
4052 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
4053 "gt_pch_note_reorder",
4054 "PCH type-walking procedures. ",
4055 TRUE, WTK_PCH
4058 /* Write out the local pointer-walking routines. */
4060 /* process_field routine for local pointer-walking for user-callable
4061 routines. The difference between this and
4062 write_types_local_process_field is that, in this case, we do not
4063 need to check whether the given pointer matches the address of the
4064 parent structure. This check was already generated by the call
4065 to gt_pch_nx in the main gt_pch_p_*() function that is calling
4066 this code. */
4068 static void
4069 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
4071 switch (f->kind)
4073 case TYPE_POINTER:
4074 case TYPE_STRUCT:
4075 case TYPE_UNION:
4076 case TYPE_LANG_STRUCT:
4077 case TYPE_PARAM_STRUCT:
4078 case TYPE_STRING:
4079 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4080 break;
4082 case TYPE_USER_STRUCT:
4083 if (d->in_ptr_field)
4084 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4085 else
4086 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4087 d->indent, "", d->val);
4088 break;
4090 case TYPE_SCALAR:
4091 break;
4093 case TYPE_ARRAY:
4094 case TYPE_NONE:
4095 case TYPE_UNDEFINED:
4096 gcc_unreachable ();
4101 /* Write a function to PCH walk all the fields of type S on OF.
4102 D contains data needed by walk_type to recurse into the fields of S. */
4104 static void
4105 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
4107 oprintf (d->of, "\nvoid\n");
4108 oprintf (d->of, "gt_pch_nx (");
4109 write_type_decl (d->of, s);
4110 oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
4111 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4112 "\tATTRIBUTE_UNUSED void *cookie)\n");
4113 oprintf (d->of, "{\n");
4114 d->val = "(*x)";
4115 d->indent = 2;
4116 d->process_field = write_types_local_user_process_field;
4117 walk_type (s, d);
4118 oprintf (d->of, "}\n");
4122 /* Emit the user-callable functions needed to mark all the types used
4123 by the user structure S. PREFIX is the prefix to use to
4124 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
4125 chain_next option defined. D contains data needed to pass to
4126 walk_type when traversing the fields of a type.
4128 For every type T referenced by S, two routines are generated: one
4129 that takes 'T *', marks the pointer and calls the second routine,
4130 which just marks the fields of T. */
4132 static void
4133 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
4135 gcc_assert (s->kind == TYPE_USER_STRUCT);
4137 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
4139 type_p fld_type = fld->type;
4140 if (union_or_struct_p (fld_type))
4141 write_pch_user_walking_for_structure_body (fld_type, d);
4146 /* process_field routine for local pointer-walking. */
4148 static void
4149 write_types_local_process_field (type_p f, const struct walk_type_data *d)
4151 gcc_assert (d->have_this_obj);
4152 switch (f->kind)
4154 case TYPE_POINTER:
4155 case TYPE_STRUCT:
4156 case TYPE_UNION:
4157 case TYPE_LANG_STRUCT:
4158 case TYPE_PARAM_STRUCT:
4159 case TYPE_STRING:
4160 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
4161 d->prev_val[3]);
4162 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4163 break;
4165 case TYPE_USER_STRUCT:
4166 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
4167 d->prev_val[3]);
4168 if (d->in_ptr_field)
4169 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
4170 else
4171 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4172 d->indent, "", d->val);
4173 break;
4175 case TYPE_SCALAR:
4176 break;
4178 case TYPE_ARRAY:
4179 case TYPE_NONE:
4180 case TYPE_UNDEFINED:
4181 gcc_unreachable ();
4186 /* For S, a structure that's part of ORIG_S, and using parameters
4187 PARAM, write out a routine that:
4188 - Is of type gt_note_pointers
4189 - Calls PROCESS_FIELD on each field of S or its substructures.
4192 static void
4193 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
4195 struct walk_type_data d;
4197 /* Don't write fns for subclasses, only for the ultimate base class
4198 within an inheritance hierarchy. */
4199 if (s->u.s.base_class)
4200 return;
4202 memset (&d, 0, sizeof (d));
4203 d.of = get_output_file_for_structure (s, param);
4204 d.process_field = write_types_local_process_field;
4205 d.opt = s->u.s.opt;
4206 d.line = &s->u.s.line;
4207 d.bitmap = s->u.s.bitmap;
4208 d.param = param;
4209 d.prev_val[0] = d.prev_val[2] = "*x";
4210 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
4211 d.prev_val[3] = "x";
4212 d.val = "(*x)";
4213 d.fn_wants_lvalue = true;
4215 oprintf (d.of, "\n");
4216 oprintf (d.of, "void\n");
4217 oprintf (d.of, "gt_pch_p_");
4218 output_mangled_typename (d.of, orig_s);
4219 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
4220 "\tvoid *x_p,\n"
4221 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4222 "\tATTRIBUTE_UNUSED void *cookie)\n");
4223 oprintf (d.of, "{\n");
4224 oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
4225 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
4226 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
4227 d.indent = 2;
4228 d.have_this_obj = true;
4230 if (s->kind != TYPE_USER_STRUCT)
4231 walk_type (s, &d);
4232 else
4234 /* User structures have no fields to walk. Simply generate a
4235 call to the user-provided PCH walker. */
4236 oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
4237 d.prev_val[3]);
4238 oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4239 d.indent, "", d.val);
4242 oprintf (d.of, "}\n");
4244 /* Write user-callable entry points for the PCH walking routines. */
4245 if (orig_s->kind == TYPE_USER_STRUCT)
4246 write_pch_user_walking_functions (s, &d);
4249 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
4251 static void
4252 write_local (outf_p output_header, type_p structures, type_p param_structs)
4254 type_p s;
4256 if (!output_header)
4257 return;
4259 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
4260 for (s = structures; s; s = s->next)
4261 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
4263 options_p opt;
4265 if (s->u.s.line.file == NULL)
4266 continue;
4267 for (opt = s->u.s.opt; opt; opt = opt->next)
4268 if (strcmp (opt->name, "ptr_alias") == 0
4269 && opt->kind == OPTION_TYPE)
4271 const_type_p const t = (const_type_p) opt->info.type;
4272 if (t->kind == TYPE_STRUCT
4273 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
4275 oprintf (output_header, "#define gt_pch_p_");
4276 output_mangled_typename (output_header, s);
4277 oprintf (output_header, " gt_pch_p_");
4278 output_mangled_typename (output_header, t);
4279 oprintf (output_header, "\n");
4281 else
4282 error_at_line (&s->u.s.line,
4283 "structure alias is not a structure");
4284 break;
4286 if (opt)
4287 continue;
4289 /* Declare the marker procedure only once. */
4290 oprintf (output_header, "extern void gt_pch_p_");
4291 output_mangled_typename (output_header, s);
4292 oprintf (output_header,
4293 "\n (void *, void *, gt_pointer_operator, void *);\n");
4295 if (s->kind == TYPE_LANG_STRUCT)
4297 type_p ss;
4298 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4299 write_local_func_for_structure (s, ss, NULL);
4301 else
4302 write_local_func_for_structure (s, s, NULL);
4305 for (s = param_structs; s; s = s->next)
4306 if (s->gc_used == GC_POINTED_TO)
4308 type_p *param = s->u.param_struct.param;
4309 type_p stru = s->u.param_struct.stru;
4311 /* Declare the marker procedure. */
4312 oprintf (output_header, "extern void gt_pch_p_");
4313 output_mangled_typename (output_header, s);
4314 oprintf (output_header,
4315 "\n (void *, void *, gt_pointer_operator, void *);\n");
4317 if (stru->u.s.line.file == NULL)
4319 fprintf (stderr, "warning: structure `%s' used but not defined\n",
4320 stru->u.s.tag);
4321 continue;
4324 if (stru->kind == TYPE_LANG_STRUCT)
4326 type_p ss;
4327 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
4328 write_local_func_for_structure (s, ss, param);
4330 else
4331 write_local_func_for_structure (s, stru, param);
4335 /* Nonzero if S is a type for which typed GC allocators should be output. */
4337 #define USED_BY_TYPED_GC_P(s) \
4338 ((s->kind == TYPE_POINTER \
4339 && (s->u.p->gc_used == GC_POINTED_TO \
4340 || s->u.p->gc_used == GC_USED)) \
4341 || (union_or_struct_p (s) \
4342 && ((s)->gc_used == GC_POINTED_TO \
4343 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4344 && s->u.s.line.file != NULL) \
4345 || ((s)->gc_used == GC_USED \
4346 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))) \
4347 || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
4351 /* Might T contain any non-pointer elements? */
4353 static int
4354 contains_scalar_p (type_p t)
4356 switch (t->kind)
4358 case TYPE_STRING:
4359 case TYPE_POINTER:
4360 return 0;
4361 case TYPE_ARRAY:
4362 return contains_scalar_p (t->u.a.p);
4363 case TYPE_USER_STRUCT:
4364 /* User-marked structures will typically contain pointers. */
4365 return 0;
4366 default:
4367 /* Could also check for structures that have no non-pointer
4368 fields, but there aren't enough of those to worry about. */
4369 return 1;
4373 /* Mangle INPF and print it to F. */
4375 static void
4376 put_mangled_filename (outf_p f, const input_file *inpf)
4378 /* The call to get_output_file_name may indirectly update fn since
4379 get_output_file_with_visibility caches its result inside, so we
4380 need the CONST_CAST. */
4381 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4382 if (!f || !name)
4383 return;
4384 for (; *name != 0; name++)
4385 if (ISALNUM (*name))
4386 oprintf (f, "%c", *name);
4387 else
4388 oprintf (f, "%c", '_');
4391 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4392 LASTNAME, and NAME are all strings to insert in various places in
4393 the resulting code. */
4395 static void
4396 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4397 const char *tname, const char *name)
4399 struct flist *fli2;
4401 for (fli2 = flp; fli2; fli2 = fli2->next)
4402 if (fli2->started_p)
4404 oprintf (fli2->f, " %s\n", lastname);
4405 oprintf (fli2->f, "};\n\n");
4408 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4409 if (fli2->started_p)
4411 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4412 int fnum;
4414 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4415 if (bitmap & 1)
4417 oprintf (base_files[fnum],
4418 "extern const struct %s gt_%s_", tname, pfx);
4419 put_mangled_filename (base_files[fnum], fli2->file);
4420 oprintf (base_files[fnum], "[];\n");
4425 size_t fnum;
4426 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4427 oprintf (base_files[fnum],
4428 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4432 for (fli2 = flp; fli2; fli2 = fli2->next)
4433 if (fli2->started_p)
4435 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4436 int fnum;
4438 fli2->started_p = 0;
4440 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4441 if (bitmap & 1)
4443 oprintf (base_files[fnum], " gt_%s_", pfx);
4444 put_mangled_filename (base_files[fnum], fli2->file);
4445 oprintf (base_files[fnum], ",\n");
4450 size_t fnum;
4451 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4453 oprintf (base_files[fnum], " NULL\n");
4454 oprintf (base_files[fnum], "};\n");
4459 /* Write the first three fields (pointer, count and stride) for
4460 root NAME to F. V and LINE are as for write_root.
4462 Return true if the entry could be written; return false on error. */
4464 static bool
4465 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4467 type_p ap;
4469 if (!v)
4471 error_at_line (line, "`%s' is too complex to be a root", name);
4472 return false;
4475 oprintf (f, " {\n");
4476 oprintf (f, " &%s,\n", name);
4477 oprintf (f, " 1");
4479 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4480 if (ap->u.a.len[0])
4481 oprintf (f, " * (%s)", ap->u.a.len);
4482 else if (ap == v->type)
4483 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4484 oprintf (f, ",\n");
4485 oprintf (f, " sizeof (%s", v->name);
4486 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4487 oprintf (f, "[0]");
4488 oprintf (f, "),\n");
4489 return true;
4492 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4493 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4494 of the caller. */
4496 static void
4497 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4498 int has_length, struct fileloc *line, const char *if_marked,
4499 bool emit_pch, type_p field_type, const char *field_name)
4501 struct pair newv;
4502 /* If the field reference is relative to V, rather than to some
4503 subcomponent of V, we can mark any subarrays with a single stride.
4504 We're effectively treating the field as a global variable in its
4505 own right. */
4506 if (v && type == v->type)
4508 newv = *v;
4509 newv.type = field_type;
4510 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4511 v = &newv;
4513 /* Otherwise, any arrays nested in the structure are too complex to
4514 handle. */
4515 else if (field_type->kind == TYPE_ARRAY)
4516 v = NULL;
4517 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4518 has_length, line, if_marked, emit_pch);
4521 /* Write out to F the table entry and any marker routines needed to
4522 mark NAME as TYPE. V can be one of three values:
4524 - null, if NAME is too complex to represent using a single
4525 count and stride. In this case, it is an error for NAME to
4526 contain any gc-ed data.
4528 - the outermost array that contains NAME, if NAME is part of an array.
4530 - the C variable that contains NAME, if NAME is not part of an array.
4532 LINE is the line of the C source that declares the root variable.
4533 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
4534 is nonzero iff we are building the root table for hash table caches. */
4536 static void
4537 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4538 struct fileloc *line, const char *if_marked, bool emit_pch)
4540 switch (type->kind)
4542 case TYPE_STRUCT:
4544 pair_p fld;
4545 for (fld = type->u.s.fields; fld; fld = fld->next)
4547 int skip_p = 0;
4548 const char *desc = NULL;
4549 options_p o;
4551 for (o = fld->opt; o; o = o->next)
4552 if (strcmp (o->name, "skip") == 0)
4553 skip_p = 1;
4554 else if (strcmp (o->name, "desc") == 0
4555 && o->kind == OPTION_STRING)
4556 desc = o->info.string;
4557 else if (strcmp (o->name, "param_is") == 0)
4559 else
4560 error_at_line (line,
4561 "field `%s' of global `%s' has unknown option `%s'",
4562 fld->name, name, o->name);
4564 if (skip_p)
4565 continue;
4566 else if (desc && fld->type->kind == TYPE_UNION)
4568 pair_p validf = NULL;
4569 pair_p ufld;
4571 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4573 const char *tag = NULL;
4574 options_p oo;
4575 for (oo = ufld->opt; oo; oo = oo->next)
4576 if (strcmp (oo->name, "tag") == 0
4577 && oo->kind == OPTION_STRING)
4578 tag = oo->info.string;
4579 if (tag == NULL || strcmp (tag, desc) != 0)
4580 continue;
4581 if (validf != NULL)
4582 error_at_line (line,
4583 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4584 name, fld->name, validf->name,
4585 name, fld->name, ufld->name, tag);
4586 validf = ufld;
4588 if (validf != NULL)
4589 write_field_root (f, v, type, name, 0, line, if_marked,
4590 emit_pch, validf->type,
4591 ACONCAT ((fld->name, ".",
4592 validf->name, NULL)));
4594 else if (desc)
4595 error_at_line (line,
4596 "global `%s.%s' has `desc' option but is not union",
4597 name, fld->name);
4598 else
4599 write_field_root (f, v, type, name, 0, line, if_marked,
4600 emit_pch, fld->type, fld->name);
4603 break;
4605 case TYPE_ARRAY:
4607 char *newname;
4608 newname = xasprintf ("%s[0]", name);
4609 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
4610 emit_pch);
4611 free (newname);
4613 break;
4615 case TYPE_USER_STRUCT:
4616 error_at_line (line, "`%s' must be a pointer type, because it is "
4617 "a GC root and its type is marked with GTY((user))",
4618 v->name);
4619 break;
4621 case TYPE_POINTER:
4623 const_type_p tp;
4625 if (!start_root_entry (f, v, name, line))
4626 return;
4628 tp = type->u.p;
4630 if (!has_length && union_or_struct_p (tp))
4632 tp = get_ultimate_base_class (tp);
4633 const char *id_for_tag = filter_type_name (tp->u.s.tag);
4634 oprintf (f, " &gt_ggc_mx_%s,\n", id_for_tag);
4635 if (emit_pch)
4636 oprintf (f, " &gt_pch_nx_%s", id_for_tag);
4637 else
4638 oprintf (f, " NULL");
4639 if (id_for_tag != tp->u.s.tag)
4640 free (CONST_CAST (char *, id_for_tag));
4642 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
4644 oprintf (f, " &gt_ggc_m_");
4645 output_mangled_typename (f, tp);
4646 if (emit_pch)
4648 oprintf (f, ",\n &gt_pch_n_");
4649 output_mangled_typename (f, tp);
4651 else
4652 oprintf (f, ",\n NULL");
4654 else if (has_length
4655 && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
4657 oprintf (f, " &gt_ggc_ma_%s,\n", name);
4658 if (emit_pch)
4659 oprintf (f, " &gt_pch_na_%s", name);
4660 else
4661 oprintf (f, " NULL");
4663 else
4665 error_at_line (line,
4666 "global `%s' is pointer to unimplemented type",
4667 name);
4669 if (if_marked)
4670 oprintf (f, ",\n &%s", if_marked);
4671 oprintf (f, "\n },\n");
4673 break;
4675 case TYPE_STRING:
4677 if (!start_root_entry (f, v, name, line))
4678 return;
4680 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
4681 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
4682 oprintf (f, " },\n");
4684 break;
4686 case TYPE_SCALAR:
4687 break;
4689 case TYPE_NONE:
4690 case TYPE_UNDEFINED:
4691 case TYPE_UNION:
4692 case TYPE_LANG_STRUCT:
4693 case TYPE_PARAM_STRUCT:
4694 error_at_line (line, "global `%s' is unimplemented type", name);
4698 /* This generates a routine to walk an array. */
4700 static void
4701 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4703 struct walk_type_data d;
4704 char *prevval3;
4706 memset (&d, 0, sizeof (d));
4707 d.of = f;
4708 d.cookie = wtd;
4709 d.indent = 2;
4710 d.line = &v->line;
4711 d.opt = v->opt;
4712 d.bitmap = get_lang_bitmap (v->line.file);
4713 d.param = NULL;
4715 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4717 if (wtd->param_prefix)
4719 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4720 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
4721 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4722 wtd->param_prefix, v->name);
4723 oprintf (d.of,
4724 " ATTRIBUTE_UNUSED void *x_p,\n"
4725 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4726 " ATTRIBUTE_UNUSED void * cookie)\n");
4727 oprintf (d.of, "{\n");
4728 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4729 d.process_field = write_types_local_process_field;
4730 d.have_this_obj = true;
4731 walk_type (v->type, &d);
4732 oprintf (f, "}\n\n");
4735 d.opt = v->opt;
4736 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4737 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4738 wtd->prefix, v->name);
4739 oprintf (f, "{\n");
4740 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4741 d.process_field = write_types_process_field;
4742 d.have_this_obj = false;
4743 walk_type (v->type, &d);
4744 free (prevval3);
4745 oprintf (f, "}\n\n");
4748 /* Output a table describing the locations and types of VARIABLES. */
4750 static void
4751 write_roots (pair_p variables, bool emit_pch)
4753 pair_p v;
4754 struct flist *flp = NULL;
4756 for (v = variables; v; v = v->next)
4758 outf_p f =
4759 get_output_file_with_visibility (CONST_CAST (input_file*,
4760 v->line.file));
4761 struct flist *fli;
4762 const char *length = NULL;
4763 int deletable_p = 0;
4764 options_p o;
4765 for (o = v->opt; o; o = o->next)
4766 if (strcmp (o->name, "length") == 0
4767 && o->kind == OPTION_STRING)
4768 length = o->info.string;
4769 else if (strcmp (o->name, "deletable") == 0)
4770 deletable_p = 1;
4771 else if (strcmp (o->name, "param_is") == 0)
4773 else if (strncmp (o->name, "param", 5) == 0
4774 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
4776 else if (strcmp (o->name, "if_marked") == 0)
4778 else
4779 error_at_line (&v->line,
4780 "global `%s' has unknown option `%s'",
4781 v->name, o->name);
4783 for (fli = flp; fli; fli = fli->next)
4784 if (fli->f == f && f)
4785 break;
4786 if (fli == NULL)
4788 fli = XNEW (struct flist);
4789 fli->f = f;
4790 fli->next = flp;
4791 fli->started_p = 0;
4792 fli->file = v->line.file;
4793 gcc_assert (fli->file);
4794 flp = fli;
4796 oprintf (f, "\n/* GC roots. */\n\n");
4799 if (!deletable_p
4800 && length
4801 && v->type->kind == TYPE_POINTER
4802 && (v->type->u.p->kind == TYPE_POINTER
4803 || v->type->u.p->kind == TYPE_STRUCT))
4805 write_array (f, v, &ggc_wtd);
4806 write_array (f, v, &pch_wtd);
4810 for (v = variables; v; v = v->next)
4812 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4813 v->line.file));
4814 struct flist *fli;
4815 int skip_p = 0;
4816 int length_p = 0;
4817 options_p o;
4819 for (o = v->opt; o; o = o->next)
4820 if (strcmp (o->name, "length") == 0)
4821 length_p = 1;
4822 else if (strcmp (o->name, "deletable") == 0
4823 || strcmp (o->name, "if_marked") == 0)
4824 skip_p = 1;
4826 if (skip_p)
4827 continue;
4829 for (fli = flp; fli; fli = fli->next)
4830 if (fli->f == f)
4831 break;
4832 if (!fli->started_p)
4834 fli->started_p = 1;
4836 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4837 put_mangled_filename (f, v->line.file);
4838 oprintf (f, "[] = {\n");
4841 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4844 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4845 "gt_ggc_rtab");
4847 for (v = variables; v; v = v->next)
4849 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4850 v->line.file));
4851 struct flist *fli;
4852 int skip_p = 1;
4853 options_p o;
4855 for (o = v->opt; o; o = o->next)
4856 if (strcmp (o->name, "deletable") == 0)
4857 skip_p = 0;
4858 else if (strcmp (o->name, "if_marked") == 0)
4859 skip_p = 1;
4861 if (skip_p)
4862 continue;
4864 for (fli = flp; fli; fli = fli->next)
4865 if (fli->f == f)
4866 break;
4867 if (!fli->started_p)
4869 fli->started_p = 1;
4871 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4872 put_mangled_filename (f, v->line.file);
4873 oprintf (f, "[] = {\n");
4876 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4877 v->name, v->name);
4880 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4881 "gt_ggc_deletable_rtab");
4883 for (v = variables; v; v = v->next)
4885 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4886 v->line.file));
4887 struct flist *fli;
4888 const char *if_marked = NULL;
4889 int length_p = 0;
4890 options_p o;
4892 for (o = v->opt; o; o = o->next)
4893 if (strcmp (o->name, "length") == 0)
4894 length_p = 1;
4895 else if (strcmp (o->name, "if_marked") == 0
4896 && o->kind == OPTION_STRING)
4897 if_marked = o->info.string;
4898 if (if_marked == NULL)
4899 continue;
4900 if (v->type->kind != TYPE_POINTER
4901 || v->type->u.p->kind != TYPE_PARAM_STRUCT
4902 || v->type->u.p->u.param_struct.stru != find_structure ("htab",
4903 TYPE_STRUCT))
4905 error_at_line (&v->line,
4906 "if_marked option used but not hash table");
4907 continue;
4910 for (fli = flp; fli; fli = fli->next)
4911 if (fli->f == f)
4912 break;
4913 if (!fli->started_p)
4915 fli->started_p = 1;
4917 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4918 put_mangled_filename (f, v->line.file);
4919 oprintf (f, "[] = {\n");
4922 write_root (f, v, v->type->u.p->u.param_struct.param[0],
4923 v->name, length_p, &v->line, if_marked, emit_pch);
4926 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4927 "gt_ggc_cache_rtab");
4929 if (!emit_pch)
4930 return;
4932 for (v = variables; v; v = v->next)
4934 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4935 v->line.file));
4936 struct flist *fli;
4937 int length_p = 0;
4938 int if_marked_p = 0;
4939 options_p o;
4941 for (o = v->opt; o; o = o->next)
4942 if (strcmp (o->name, "length") == 0)
4943 length_p = 1;
4944 else if (strcmp (o->name, "if_marked") == 0)
4945 if_marked_p = 1;
4947 if (!if_marked_p)
4948 continue;
4950 for (fli = flp; fli; fli = fli->next)
4951 if (fli->f == f)
4952 break;
4953 if (!fli->started_p)
4955 fli->started_p = 1;
4957 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4958 put_mangled_filename (f, v->line.file);
4959 oprintf (f, "[] = {\n");
4962 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4965 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4966 "gt_pch_cache_rtab");
4968 for (v = variables; v; v = v->next)
4970 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4971 v->line.file));
4972 struct flist *fli;
4973 int skip_p = 0;
4974 options_p o;
4976 for (o = v->opt; o; o = o->next)
4977 if (strcmp (o->name, "deletable") == 0
4978 || strcmp (o->name, "if_marked") == 0)
4980 skip_p = 1;
4981 break;
4984 if (skip_p)
4985 continue;
4987 if (!contains_scalar_p (v->type))
4988 continue;
4990 for (fli = flp; fli; fli = fli->next)
4991 if (fli->f == f)
4992 break;
4993 if (!fli->started_p)
4995 fli->started_p = 1;
4997 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4998 put_mangled_filename (f, v->line.file);
4999 oprintf (f, "[] = {\n");
5002 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
5003 v->name, v->name);
5006 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
5007 "gt_pch_scalar_rtab");
5010 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
5011 guaranteee for somewhat increased readability. If name conflicts do happen,
5012 this funcion will have to be adjusted to be more like
5013 output_mangled_typename. */
5015 static void
5016 output_typename (outf_p of, const_type_p t)
5018 switch (t->kind)
5020 case TYPE_STRING:
5021 oprintf (of, "str");
5022 break;
5023 case TYPE_SCALAR:
5024 oprintf (of, "scalar");
5025 break;
5026 case TYPE_POINTER:
5027 output_typename (of, t->u.p);
5028 break;
5029 case TYPE_STRUCT:
5030 case TYPE_USER_STRUCT:
5031 case TYPE_UNION:
5032 case TYPE_LANG_STRUCT:
5033 oprintf (of, "%s", t->u.s.tag);
5034 break;
5035 case TYPE_PARAM_STRUCT:
5037 int i;
5038 for (i = 0; i < NUM_PARAM; i++)
5039 if (t->u.param_struct.param[i] != NULL)
5041 output_typename (of, t->u.param_struct.param[i]);
5042 oprintf (of, "_");
5044 output_typename (of, t->u.param_struct.stru);
5045 break;
5047 case TYPE_NONE:
5048 case TYPE_UNDEFINED:
5049 case TYPE_ARRAY:
5050 gcc_unreachable ();
5054 /* Writes a typed GC allocator for type S that is suitable as a callback for
5055 the splay tree implementation in libiberty. */
5057 static void
5058 write_splay_tree_allocator_def (const_type_p s)
5060 outf_p of = get_output_file_with_visibility (NULL);
5061 oprintf (of, "void * ggc_alloc_splay_tree_");
5062 output_typename (of, s);
5063 oprintf (of, " (int sz, void * nl)\n");
5064 oprintf (of, "{\n");
5065 oprintf (of, " return ggc_splay_alloc (sz, nl);\n");
5066 oprintf (of, "}\n\n");
5069 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
5070 for the splay tree implementation in libiberty. */
5072 static void
5073 write_splay_tree_allocators (const_type_p param_structs)
5075 const_type_p s;
5077 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
5078 for (s = param_structs; s; s = s->next)
5079 if (s->gc_used == GC_POINTED_TO)
5081 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
5082 output_typename (header_file, s);
5083 oprintf (header_file, " (int, void *);\n");
5084 write_splay_tree_allocator_def (s);
5088 #define INDENT 2
5090 /* Dumps the value of typekind KIND. */
5092 static void
5093 dump_typekind (int indent, enum typekind kind)
5095 printf ("%*ckind = ", indent, ' ');
5096 switch (kind)
5098 case TYPE_SCALAR:
5099 printf ("TYPE_SCALAR");
5100 break;
5101 case TYPE_STRING:
5102 printf ("TYPE_STRING");
5103 break;
5104 case TYPE_STRUCT:
5105 printf ("TYPE_STRUCT");
5106 break;
5107 case TYPE_UNDEFINED:
5108 printf ("TYPE_UNDEFINED");
5109 break;
5110 case TYPE_USER_STRUCT:
5111 printf ("TYPE_USER_STRUCT");
5112 break;
5113 case TYPE_UNION:
5114 printf ("TYPE_UNION");
5115 break;
5116 case TYPE_POINTER:
5117 printf ("TYPE_POINTER");
5118 break;
5119 case TYPE_ARRAY:
5120 printf ("TYPE_ARRAY");
5121 break;
5122 case TYPE_LANG_STRUCT:
5123 printf ("TYPE_LANG_STRUCT");
5124 break;
5125 case TYPE_PARAM_STRUCT:
5126 printf ("TYPE_PARAM_STRUCT");
5127 break;
5128 default:
5129 gcc_unreachable ();
5131 printf ("\n");
5134 /* Dumps the value of GC_USED flag. */
5136 static void
5137 dump_gc_used (int indent, enum gc_used_enum gc_used)
5139 printf ("%*cgc_used = ", indent, ' ');
5140 switch (gc_used)
5142 case GC_UNUSED:
5143 printf ("GC_UNUSED");
5144 break;
5145 case GC_USED:
5146 printf ("GC_USED");
5147 break;
5148 case GC_MAYBE_POINTED_TO:
5149 printf ("GC_MAYBE_POINTED_TO");
5150 break;
5151 case GC_POINTED_TO:
5152 printf ("GC_POINTED_TO");
5153 break;
5154 default:
5155 gcc_unreachable ();
5157 printf ("\n");
5160 /* Dumps the type options OPT. */
5162 static void
5163 dump_options (int indent, options_p opt)
5165 options_p o;
5166 printf ("%*coptions = ", indent, ' ');
5167 o = opt;
5168 while (o)
5170 switch (o->kind)
5172 case OPTION_STRING:
5173 printf ("%s:string %s ", o->name, o->info.string);
5174 break;
5175 case OPTION_TYPE:
5176 printf ("%s:type ", o->name);
5177 dump_type (indent+1, o->info.type);
5178 break;
5179 case OPTION_NESTED:
5180 printf ("%s:nested ", o->name);
5181 break;
5182 case OPTION_NONE:
5183 gcc_unreachable ();
5185 o = o->next;
5187 printf ("\n");
5190 /* Dumps the source file location in LINE. */
5192 static void
5193 dump_fileloc (int indent, struct fileloc line)
5195 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
5196 get_input_file_name (line.file),
5197 line.line);
5200 /* Recursively dumps the struct, union, or a language-specific
5201 struct T. */
5203 static void
5204 dump_type_u_s (int indent, type_p t)
5206 pair_p fields;
5208 gcc_assert (union_or_struct_p (t));
5209 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
5210 dump_fileloc (indent, t->u.s.line);
5211 printf ("%*cu.s.fields =\n", indent, ' ');
5212 fields = t->u.s.fields;
5213 while (fields)
5215 dump_pair (indent + INDENT, fields);
5216 fields = fields->next;
5218 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
5219 dump_options (indent, t->u.s.opt);
5220 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
5221 if (t->kind == TYPE_LANG_STRUCT)
5223 printf ("%*cu.s.lang_struct:\n", indent, ' ');
5224 dump_type_list (indent + INDENT, t->u.s.lang_struct);
5228 /* Recursively dumps the array T. */
5230 static void
5231 dump_type_u_a (int indent, type_p t)
5233 gcc_assert (t->kind == TYPE_ARRAY);
5234 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
5235 dump_type_list (indent + INDENT, t->u.a.p);
5238 /* Recursively dumps the parameterized struct T. */
5240 static void
5241 dump_type_u_param_struct (int indent, type_p t)
5243 int i;
5244 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
5245 printf ("%*cu.param_struct.stru:\n", indent, ' ');
5246 dump_type_list (indent, t->u.param_struct.stru);
5247 dump_fileloc (indent, t->u.param_struct.line);
5248 for (i = 0; i < NUM_PARAM; i++)
5250 if (t->u.param_struct.param[i] == NULL)
5251 continue;
5252 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
5253 dump_type (indent + INDENT, t->u.param_struct.param[i]);
5257 /* Recursively dumps the type list T. */
5259 static void
5260 dump_type_list (int indent, type_p t)
5262 type_p p = t;
5263 while (p)
5265 dump_type (indent, p);
5266 p = p->next;
5270 static htab_t seen_types;
5272 /* Recursively dumps the type T if it was not dumped previously. */
5274 static void
5275 dump_type (int indent, type_p t)
5277 PTR *slot;
5279 if (seen_types == NULL)
5280 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
5282 printf ("%*cType at %p: ", indent, ' ', (void *) t);
5283 slot = htab_find_slot (seen_types, t, INSERT);
5284 if (*slot != NULL)
5286 printf ("already seen.\n");
5287 return;
5289 *slot = t;
5290 printf ("\n");
5292 dump_typekind (indent, t->kind);
5293 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
5294 (void *) t->pointer_to);
5295 dump_gc_used (indent + INDENT, t->gc_used);
5296 switch (t->kind)
5298 case TYPE_SCALAR:
5299 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
5300 t->u.scalar_is_char ? "true" : "false");
5301 break;
5302 case TYPE_STRING:
5303 break;
5304 case TYPE_STRUCT:
5305 case TYPE_UNION:
5306 case TYPE_LANG_STRUCT:
5307 case TYPE_USER_STRUCT:
5308 dump_type_u_s (indent + INDENT, t);
5309 break;
5310 case TYPE_POINTER:
5311 printf ("%*cp:\n", indent + INDENT, ' ');
5312 dump_type (indent + INDENT, t->u.p);
5313 break;
5314 case TYPE_ARRAY:
5315 dump_type_u_a (indent + INDENT, t);
5316 break;
5317 case TYPE_PARAM_STRUCT:
5318 dump_type_u_param_struct (indent + INDENT, t);
5319 break;
5320 default:
5321 gcc_unreachable ();
5323 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
5326 /* Dumps the pair P. */
5328 static void
5329 dump_pair (int indent, pair_p p)
5331 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
5332 dump_type (indent, p->type);
5333 dump_fileloc (indent, p->line);
5334 dump_options (indent, p->opt);
5335 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
5338 /* Dumps the list of pairs PP. */
5340 static void
5341 dump_pair_list (const char *name, pair_p pp)
5343 pair_p p;
5344 printf ("%s:\n", name);
5345 for (p = pp; p != NULL; p = p->next)
5346 dump_pair (0, p);
5347 printf ("End of %s\n\n", name);
5350 /* Dumps the STRUCTURES. */
5352 static void
5353 dump_structures (const char *name, type_p structures)
5355 printf ("%s:\n", name);
5356 dump_type_list (0, structures);
5357 printf ("End of %s\n\n", name);
5360 /* Dumps the internal structures of gengtype. This is useful to debug
5361 gengtype itself, or to understand what it does, e.g. for plugin
5362 developers. */
5364 static void
5365 dump_everything (void)
5367 dump_pair_list ("typedefs", typedefs);
5368 dump_structures ("structures", structures);
5369 dump_structures ("param_structs", param_structs);
5370 dump_pair_list ("variables", variables);
5372 /* Allocated with the first call to dump_type. */
5373 htab_delete (seen_types);
5378 /* Option specification for getopt_long. */
5379 static const struct option gengtype_long_options[] = {
5380 {"help", no_argument, NULL, 'h'},
5381 {"version", no_argument, NULL, 'V'},
5382 {"verbose", no_argument, NULL, 'v'},
5383 {"dump", no_argument, NULL, 'd'},
5384 {"debug", no_argument, NULL, 'D'},
5385 {"plugin", required_argument, NULL, 'P'},
5386 {"srcdir", required_argument, NULL, 'S'},
5387 {"backupdir", required_argument, NULL, 'B'},
5388 {"inputs", required_argument, NULL, 'I'},
5389 {"read-state", required_argument, NULL, 'r'},
5390 {"write-state", required_argument, NULL, 'w'},
5391 /* Terminating NULL placeholder. */
5392 {NULL, no_argument, NULL, 0},
5396 static void
5397 print_usage (void)
5399 printf ("Usage: %s\n", progname);
5400 printf ("\t -h | --help " " \t# Give this help.\n");
5401 printf ("\t -D | --debug "
5402 " \t# Give debug output to debug %s itself.\n", progname);
5403 printf ("\t -V | --version " " \t# Give version information.\n");
5404 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5405 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5406 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5407 " \t# Generate for plugin.\n");
5408 printf ("\t -S | --srcdir <GCC-directory> "
5409 " \t# Specify the GCC source directory.\n");
5410 printf ("\t -B | --backupdir <directory> "
5411 " \t# Specify the backup directory for updated files.\n");
5412 printf ("\t -I | --inputs <input-list> "
5413 " \t# Specify the file with source files list.\n");
5414 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5415 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5418 static void
5419 print_version (void)
5421 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5422 printf ("Report bugs: %s\n", bug_report_url);
5425 /* Parse the program options using getopt_long... */
5426 static void
5427 parse_program_options (int argc, char **argv)
5429 int opt = -1;
5430 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5431 gengtype_long_options, NULL)) >= 0)
5433 switch (opt)
5435 case 'h': /* --help */
5436 print_usage ();
5437 break;
5438 case 'V': /* --version */
5439 print_version ();
5440 break;
5441 case 'd': /* --dump */
5442 do_dump = 1;
5443 break;
5444 case 'D': /* --debug */
5445 do_debug = 1;
5446 break;
5447 case 'v': /* --verbose */
5448 verbosity_level++;
5449 break;
5450 case 'P': /* --plugin */
5451 if (optarg)
5452 plugin_output_filename = optarg;
5453 else
5454 fatal ("missing plugin output file name");
5455 break;
5456 case 'S': /* --srcdir */
5457 if (optarg)
5458 srcdir = optarg;
5459 else
5460 fatal ("missing source directory");
5461 srcdir_len = strlen (srcdir);
5462 break;
5463 case 'B': /* --backupdir */
5464 if (optarg)
5465 backup_dir = optarg;
5466 else
5467 fatal ("missing backup directory");
5468 break;
5469 case 'I': /* --inputs */
5470 if (optarg)
5471 inputlist = optarg;
5472 else
5473 fatal ("missing input list");
5474 break;
5475 case 'r': /* --read-state */
5476 if (optarg)
5477 read_state_filename = optarg;
5478 else
5479 fatal ("missing read state file");
5480 DBGPRINTF ("read state %s\n", optarg);
5481 break;
5482 case 'w': /* --write-state */
5483 DBGPRINTF ("write state %s\n", optarg);
5484 if (optarg)
5485 write_state_filename = optarg;
5486 else
5487 fatal ("missing write state file");
5488 break;
5489 default:
5490 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5491 print_usage ();
5492 fatal ("unexpected flag");
5495 if (plugin_output_filename)
5497 /* In plugin mode we require some input files. */
5498 int i = 0;
5499 if (optind >= argc)
5500 fatal ("no source files given in plugin mode");
5501 nb_plugin_files = argc - optind;
5502 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5503 for (i = 0; i < (int) nb_plugin_files; i++)
5505 char *name = argv[i + optind];
5506 plugin_files[i] = input_file_by_name (name);
5513 /******* Manage input files. ******/
5515 /* Hash table of unique input file names. */
5516 static htab_t input_file_htab;
5518 /* Find or allocate a new input_file by hash-consing it. */
5519 input_file*
5520 input_file_by_name (const char* name)
5522 PTR* slot;
5523 input_file* f = NULL;
5524 int namlen = 0;
5525 if (!name)
5526 return NULL;
5527 namlen = strlen (name);
5528 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5529 f->inpbitmap = 0;
5530 f->inpoutf = NULL;
5531 f->inpisplugin = false;
5532 strcpy (f->inpname, name);
5533 slot = htab_find_slot (input_file_htab, f, INSERT);
5534 gcc_assert (slot != NULL);
5535 if (*slot)
5537 /* Already known input file. */
5538 free (f);
5539 return (input_file*)(*slot);
5541 /* New input file. */
5542 *slot = f;
5543 return f;
5546 /* Hash table support routines for input_file-s. */
5547 static hashval_t
5548 htab_hash_inputfile (const void *p)
5550 const input_file *inpf = (const input_file *) p;
5551 gcc_assert (inpf);
5552 return htab_hash_string (get_input_file_name (inpf));
5555 static int
5556 htab_eq_inputfile (const void *x, const void *y)
5558 const input_file *inpfx = (const input_file *) x;
5559 const input_file *inpfy = (const input_file *) y;
5560 gcc_assert (inpfx != NULL && inpfy != NULL);
5561 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5566 main (int argc, char **argv)
5568 size_t i;
5569 static struct fileloc pos = { NULL, 0 };
5570 outf_p output_header;
5572 /* Mandatory common initializations. */
5573 progname = "gengtype"; /* For fatal and messages. */
5574 /* Create the hash-table used to hash-cons input files. */
5575 input_file_htab =
5576 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5577 /* Initialize our special input files. */
5578 this_file = input_file_by_name (__FILE__);
5579 system_h_file = input_file_by_name ("system.h");
5580 /* Set the scalar_is_char union number for predefined scalar types. */
5581 scalar_nonchar.u.scalar_is_char = FALSE;
5582 scalar_char.u.scalar_is_char = TRUE;
5584 parse_program_options (argc, argv);
5586 #if ENABLE_CHECKING
5587 if (do_debug)
5589 time_t now = (time_t) 0;
5590 time (&now);
5591 DBGPRINTF ("gengtype started pid %d at %s",
5592 (int) getpid (), ctime (&now));
5594 #endif /* ENABLE_CHECKING */
5596 /* Parse the input list and the input files. */
5597 DBGPRINTF ("inputlist %s", inputlist);
5598 if (read_state_filename)
5600 if (inputlist)
5601 fatal ("input list %s cannot be given with a read state file %s",
5602 inputlist, read_state_filename);
5603 read_state (read_state_filename);
5604 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5605 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs);
5607 else if (inputlist)
5609 /* These types are set up with #define or else outside of where
5610 we can see them. We should initialize them before calling
5611 read_input_list. */
5612 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5613 Call;} while (0)
5614 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5615 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5616 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5617 POS_HERE (do_scalar_typedef ("double_int", &pos));
5618 POS_HERE (do_scalar_typedef ("offset_int", &pos));
5619 POS_HERE (do_scalar_typedef ("widest_int", &pos));
5620 POS_HERE (do_scalar_typedef ("int64_t", &pos));
5621 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5622 POS_HERE (do_scalar_typedef ("uint8", &pos));
5623 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5624 POS_HERE (do_scalar_typedef ("jword", &pos));
5625 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5626 POS_HERE (do_scalar_typedef ("void", &pos));
5627 POS_HERE (do_typedef ("PTR",
5628 create_pointer (resolve_typedef ("void", &pos)),
5629 &pos));
5630 #undef POS_HERE
5631 read_input_list (inputlist);
5632 for (i = 0; i < num_gt_files; i++)
5634 parse_file (get_input_file_name (gt_files[i]));
5635 DBGPRINTF ("parsed file #%d %s",
5636 (int) i, get_input_file_name (gt_files[i]));
5638 if (verbosity_level >= 1)
5639 printf ("%s parsed %d files with %d GTY types\n",
5640 progname, (int) num_gt_files, type_count);
5642 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5643 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
5646 else
5647 fatal ("either an input list or a read state file should be given");
5648 if (hit_error)
5649 return 1;
5652 if (plugin_output_filename)
5654 size_t ix = 0;
5655 /* In plugin mode, we should have read a state file, and have
5656 given at least one plugin file. */
5657 if (!read_state_filename)
5658 fatal ("No read state given in plugin mode for %s",
5659 plugin_output_filename);
5661 if (nb_plugin_files == 0 || !plugin_files)
5662 fatal ("No plugin files given in plugin mode for %s",
5663 plugin_output_filename);
5665 /* Parse our plugin files and augment the state. */
5666 for (ix = 0; ix < nb_plugin_files; ix++)
5668 input_file* pluginput = plugin_files [ix];
5669 pluginput->inpisplugin = true;
5670 parse_file (get_input_file_name (pluginput));
5672 if (hit_error)
5673 return 1;
5675 plugin_output = create_file ("GCC", plugin_output_filename);
5676 DBGPRINTF ("created plugin_output %p named %s",
5677 (void *) plugin_output, plugin_output->name);
5679 else
5680 { /* No plugin files, we are in normal mode. */
5681 if (!srcdir)
5682 fatal ("gengtype needs a source directory in normal mode");
5684 if (hit_error)
5685 return 1;
5687 gen_rtx_next ();
5689 /* The call to set_gc_used may indirectly call find_param_structure
5690 hence enlarge the param_structs list of types. */
5691 set_gc_used (variables);
5693 /* The state at this point is read from the state input file or by
5694 parsing source files and optionally augmented by parsing plugin
5695 source files. Write it now. */
5696 if (write_state_filename)
5698 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5699 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs);
5701 if (hit_error)
5702 fatal ("didn't write state file %s after errors",
5703 write_state_filename);
5705 DBGPRINTF ("before write_state %s", write_state_filename);
5706 write_state (write_state_filename);
5708 if (do_dump)
5709 dump_everything ();
5711 /* After having written the state file we return immediately to
5712 avoid generating any output file. */
5713 if (hit_error)
5714 return 1;
5715 else
5716 return 0;
5720 open_base_files ();
5722 output_header = plugin_output ? plugin_output : header_file;
5723 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5724 structures);
5725 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5726 param_structs);
5728 write_types (output_header, structures, param_structs, &ggc_wtd);
5729 if (plugin_files == NULL)
5731 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5732 structures);
5733 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5734 param_structs);
5735 write_types (header_file, structures, param_structs, &pch_wtd);
5736 write_local (header_file, structures, param_structs);
5738 write_splay_tree_allocators (param_structs);
5739 write_roots (variables, plugin_files == NULL);
5740 write_rtx_next ();
5741 close_output_files ();
5743 if (do_dump)
5744 dump_everything ();
5746 /* Don't bother about free-ing any input or plugin file, etc. */
5748 if (hit_error)
5749 return 1;
5750 return 0;