2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / gengtype.c
blob137e0ffdb96e2724917923c4353258af722a2a85
1 /* Process source files and output type information.
2 Copyright (C) 2002-2015 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 HOST_GENERATOR_FILE
21 #include "config.h"
22 #define GENERATOR_FILE 1
23 #else
24 #include "bconfig.h"
25 #endif
26 #include "system.h"
27 #include "errors.h" /* for fatal */
28 #include "getopt.h"
29 #include "version.h" /* for version_string & pkgversion_string. */
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 /* Locate the ultimate base class of struct S. */
124 static const_type_p
125 get_ultimate_base_class (const_type_p s)
127 while (s->u.s.base_class)
128 s = s->u.s.base_class;
129 return s;
132 static type_p
133 get_ultimate_base_class (type_p s)
135 while (s->u.s.base_class)
136 s = s->u.s.base_class;
137 return s;
140 /* Input file handling. */
142 /* Table of all input files. */
143 const input_file **gt_files;
144 size_t num_gt_files;
146 /* A number of places use the name of this "gengtype.c" file for a
147 location for things that we can't rely on the source to define.
148 Make sure we can still use pointer comparison on filenames. */
149 input_file* this_file;
150 /* The "system.h" file is likewise specially useful. */
151 input_file* system_h_file;
153 /* Vector of per-language directories. */
154 const char **lang_dir_names;
155 size_t num_lang_dirs;
157 /* An array of output files suitable for definitions. There is one
158 BASE_FILES entry for each language. */
159 static outf_p *base_files;
163 #if ENABLE_CHECKING
164 /* Utility debugging function, printing the various type counts within
165 a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
166 void
167 dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
169 int nb_types = 0, nb_scalar = 0, nb_string = 0;
170 int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
171 int nb_lang_struct = 0;
172 int nb_user_struct = 0, nb_undefined = 0;
173 type_p p = NULL;
174 for (p = t; p; p = p->next)
176 nb_types++;
177 switch (p->kind)
179 case TYPE_UNDEFINED:
180 nb_undefined++;
181 case TYPE_SCALAR:
182 nb_scalar++;
183 break;
184 case TYPE_STRING:
185 nb_string++;
186 break;
187 case TYPE_STRUCT:
188 nb_struct++;
189 break;
190 case TYPE_USER_STRUCT:
191 nb_user_struct++;
192 break;
193 case TYPE_UNION:
194 nb_union++;
195 break;
196 case TYPE_POINTER:
197 nb_pointer++;
198 break;
199 case TYPE_ARRAY:
200 nb_array++;
201 break;
202 case TYPE_LANG_STRUCT:
203 nb_lang_struct++;
204 break;
205 case TYPE_NONE:
206 gcc_unreachable ();
209 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
210 lbasename (fil), lin, msg, nb_types);
211 if (nb_scalar > 0 || nb_string > 0)
212 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
213 if (nb_struct > 0 || nb_union > 0)
214 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
215 if (nb_pointer > 0 || nb_array > 0)
216 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
217 if (nb_lang_struct > 0)
218 fprintf (stderr, "@@%%@@ %d lang_structs\n", nb_lang_struct);
219 if (nb_user_struct > 0)
220 fprintf (stderr, "@@%%@@ %d user_structs\n", nb_user_struct);
221 if (nb_undefined > 0)
222 fprintf (stderr, "@@%%@@ %d undefined types\n", nb_undefined);
223 fprintf (stderr, "\n");
225 #endif /* ENABLE_CHECKING */
227 /* Scan the input file, LIST, and determine how much space we need to
228 store strings in. Also, count the number of language directories
229 and files. The numbers returned are overestimates as they does not
230 consider repeated files. */
231 static size_t
232 measure_input_list (FILE *list)
234 size_t n = 0;
235 int c;
236 bool atbol = true;
237 num_lang_dirs = 0;
238 num_gt_files = plugin_files ? nb_plugin_files : 0;
239 while ((c = getc (list)) != EOF)
241 n++;
242 if (atbol)
244 if (c == '[')
245 num_lang_dirs++;
246 else
248 /* Add space for a lang_bitmap before the input file name. */
249 n += sizeof (lang_bitmap);
250 num_gt_files++;
252 atbol = false;
255 if (c == '\n')
256 atbol = true;
259 rewind (list);
260 return n;
263 /* Read one input line from LIST to HEREP (which is updated). A
264 pointer to the string is returned via LINEP. If it was a language
265 subdirectory in square brackets, strip off the square brackets and
266 return true. Otherwise, leave space before the string for a
267 lang_bitmap, and return false. At EOF, returns false, does not
268 touch *HEREP, and sets *LINEP to NULL. POS is used for
269 diagnostics. */
270 static bool
271 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
273 char *here = *herep;
274 char *line;
275 int c = getc (list);
277 /* Read over whitespace. */
278 while (c == '\n' || c == ' ')
279 c = getc (list);
281 if (c == EOF)
283 *linep = 0;
284 return false;
286 else if (c == '[')
288 /* No space for a lang_bitmap is necessary. Discard the '['. */
289 c = getc (list);
290 line = here;
291 while (c != ']' && c != '\n' && c != EOF)
293 *here++ = c;
294 c = getc (list);
296 *here++ = '\0';
298 if (c == ']')
300 c = getc (list); /* eat what should be a newline */
301 if (c != '\n' && c != EOF)
302 error_at_line (pos, "junk on line after language tag [%s]", line);
304 else
305 error_at_line (pos, "missing close bracket for language tag [%s",
306 line);
308 *herep = here;
309 *linep = line;
310 return true;
312 else
314 /* Leave space for a lang_bitmap. */
315 memset (here, 0, sizeof (lang_bitmap));
316 here += sizeof (lang_bitmap);
317 line = here;
320 *here++ = c;
321 c = getc (list);
323 while (c != EOF && c != '\n');
324 *here++ = '\0';
325 *herep = here;
326 *linep = line;
327 return false;
331 /* Read the list of input files from LIST and compute all of the
332 relevant tables. There is one file per line of the list. At
333 first, all the files on the list are language-generic, but
334 eventually a line will appear which is the name of a language
335 subdirectory in square brackets, like this: [cp]. All subsequent
336 files are specific to that language, until another language
337 subdirectory tag appears. Files can appear more than once, if
338 they apply to more than one language. */
339 static void
340 read_input_list (const char *listname)
342 FILE *list = fopen (listname, "r");
343 if (!list)
344 fatal ("cannot open %s: %s", listname, xstrerror (errno));
345 else
347 struct fileloc epos;
348 size_t bufsz = measure_input_list (list);
349 char *buf = XNEWVEC (char, bufsz);
350 char *here = buf;
351 char *committed = buf;
352 char *limit = buf + bufsz;
353 char *line;
354 bool is_language;
355 size_t langno = 0;
356 size_t nfiles = 0;
357 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
359 epos.file = input_file_by_name (listname);
360 epos.line = 0;
362 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
363 gt_files = XNEWVEC (const input_file *, num_gt_files);
365 for (;;)
367 next_line:
368 epos.line++;
369 committed = here;
370 is_language = read_input_line (list, &here, &line, &epos);
371 gcc_assert (here <= limit);
372 if (line == 0)
373 break;
374 else if (is_language)
376 size_t i;
377 gcc_assert (langno <= num_lang_dirs);
378 for (i = 0; i < langno; i++)
379 if (strcmp (lang_dir_names[i], line) == 0)
381 error_at_line (&epos, "duplicate language tag [%s]",
382 line);
383 curlangs = 1 << i;
384 here = committed;
385 goto next_line;
388 curlangs = 1 << langno;
389 lang_dir_names[langno++] = line;
391 else
393 size_t i;
394 input_file *inpf = input_file_by_name (line);
395 gcc_assert (nfiles <= num_gt_files);
396 for (i = 0; i < nfiles; i++)
397 /* Since the input_file-s are uniquely hash-consed, we
398 can just compare pointers! */
399 if (gt_files[i] == inpf)
401 /* Throw away the string we just read, and add the
402 current language to the existing string's bitmap. */
403 lang_bitmap bmap = get_lang_bitmap (inpf);
404 if (bmap & curlangs)
405 error_at_line (&epos,
406 "file %s specified more than once "
407 "for language %s", line,
408 langno ==
409 0 ? "(all)" : lang_dir_names[langno -
410 1]);
412 bmap |= curlangs;
413 set_lang_bitmap (inpf, bmap);
414 here = committed;
415 goto next_line;
418 set_lang_bitmap (inpf, curlangs);
419 gt_files[nfiles++] = inpf;
422 /* Update the global counts now that we know accurately how many
423 things there are. (We do not bother resizing the arrays down.) */
424 num_lang_dirs = langno;
425 /* Add the plugin files if provided. */
426 if (plugin_files)
428 size_t i;
429 for (i = 0; i < nb_plugin_files; i++)
430 gt_files[nfiles++] = plugin_files[i];
432 num_gt_files = nfiles;
435 /* Sanity check: any file that resides in a language subdirectory
436 (e.g. 'cp') ought to belong to the corresponding language.
437 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
438 (Can you even do that? Should you be allowed to?) */
440 size_t f;
441 for (f = 0; f < num_gt_files; f++)
443 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
444 const char *basename = get_file_basename (gt_files[f]);
445 const char *slashpos = strchr (basename, '/');
446 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
447 const char *slashpos2 = strchr (basename, '\\');
449 if (!slashpos || (slashpos2 && slashpos2 < slashpos))
450 slashpos = slashpos2;
451 #endif
453 if (slashpos)
455 size_t l;
456 for (l = 0; l < num_lang_dirs; l++)
457 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
458 && memcmp (basename, lang_dir_names[l],
459 strlen (lang_dir_names[l])) == 0)
461 if (!(bitmap & (1 << l)))
462 error ("%s is in language directory '%s' but is not "
463 "tagged for that language",
464 basename, lang_dir_names[l]);
465 break;
471 if (ferror (list))
472 fatal ("error reading %s: %s", listname, xstrerror (errno));
474 fclose (list);
479 /* The one and only TYPE_STRING. */
481 struct type string_type = {
482 TYPE_STRING, 0, 0, 0, GC_USED, {0}
485 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
486 set early in main. */
488 struct type scalar_nonchar = {
489 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
492 struct type scalar_char = {
493 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
496 /* Lists of various things. */
498 pair_p typedefs = NULL;
499 type_p structures = NULL;
500 pair_p variables = NULL;
502 static type_p adjust_field_tree_exp (type_p t, options_p opt);
503 static type_p adjust_field_rtx_def (type_p t, options_p opt);
505 /* Define S as a typedef to T at POS. */
507 void
508 do_typedef (const char *s, type_p t, struct fileloc *pos)
510 pair_p p;
512 /* temporary kludge - gengtype doesn't handle conditionals or
513 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
514 is coming from this file (main() sets them up with safe dummy
515 definitions). */
516 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
517 return;
519 for (p = typedefs; p != NULL; p = p->next)
520 if (strcmp (p->name, s) == 0)
522 if (p->type != t && strcmp (s, "result_type") != 0)
524 error_at_line (pos, "type `%s' previously defined", s);
525 error_at_line (&p->line, "previously defined here");
527 return;
530 p = XNEW (struct pair);
531 p->next = typedefs;
532 p->name = s;
533 p->type = t;
534 p->line = *pos;
535 p->opt = NULL;
536 typedefs = p;
539 /* Define S as a typename of a scalar. Cannot be used to define
540 typedefs of 'char'. Note: is also used for pointer-to-function
541 typedefs (which are therefore not treated as pointers). */
543 void
544 do_scalar_typedef (const char *s, struct fileloc *pos)
546 do_typedef (s, &scalar_nonchar, pos);
549 /* Similar to strtok_r. */
551 static char *
552 strtoken (char *str, const char *delim, char **next)
554 char *p;
556 if (str == NULL)
557 str = *next;
559 /* Skip the leading delimiters. */
560 str += strspn (str, delim);
561 if (*str == '\0')
562 /* This is an empty token. */
563 return NULL;
565 /* The current token. */
566 p = str;
568 /* Find the next delimiter. */
569 str += strcspn (str, delim);
570 if (*str == '\0')
571 /* This is the last token. */
572 *next = str;
573 else
575 /* Terminate the current token. */
576 *str = '\0';
577 /* Advance to the next token. */
578 *next = str + 1;
581 return p;
584 /* Define TYPE_NAME to be a user defined type at location POS. */
586 type_p
587 create_user_defined_type (const char *type_name, struct fileloc *pos)
589 type_p ty = find_structure (type_name, TYPE_USER_STRUCT);
591 /* We might have already seen an incomplete decl of the given type,
592 in which case we won't have yet seen a GTY((user)), and the type will
593 only have kind "TYPE_STRUCT". Mark it as a user struct. */
594 ty->kind = TYPE_USER_STRUCT;
596 ty->u.s.line = *pos;
597 ty->u.s.bitmap = get_lang_bitmap (pos->file);
598 do_typedef (type_name, ty, pos);
600 /* If TYPE_NAME specifies a template, create references to the types
601 in the template by pretending that each type is a field of TY.
602 This is needed to make sure that the types referenced by the
603 template are marked as used. */
604 char *str = xstrdup (type_name);
605 char *open_bracket = strchr (str, '<');
606 if (open_bracket)
608 /* We only accept simple template declarations (see
609 require_template_declaration), so we only need to parse a
610 comma-separated list of strings, implicitly assumed to
611 be type names, potentially with "*" characters. */
612 char *arg = open_bracket + 1;
613 /* Workaround -Wmaybe-uninitialized false positive during
614 profiledbootstrap by initializing it. */
615 char *next = NULL;
616 char *type_id = strtoken (arg, ",>", &next);
617 pair_p fields = 0;
618 while (type_id)
620 /* Create a new field for every type found inside the template
621 parameter list. */
623 /* Support a single trailing "*" character. */
624 const char *star = strchr (type_id, '*');
625 int is_ptr = (star != NULL);
626 size_t offset_to_star = star - type_id;
627 if (is_ptr)
628 offset_to_star = star - type_id;
630 if (strstr (type_id, "char*"))
632 type_id = strtoken (0, ",>", &next);
633 continue;
636 char *field_name = xstrdup (type_id);
638 type_p arg_type;
639 if (is_ptr)
641 /* Strip off the first '*' character (and any subsequent text). */
642 *(field_name + offset_to_star) = '\0';
644 arg_type = find_structure (field_name, TYPE_STRUCT);
645 arg_type = create_pointer (arg_type);
647 else
648 arg_type = resolve_typedef (field_name, pos);
650 fields = create_field_at (fields, arg_type, field_name, 0, pos);
651 type_id = strtoken (0, ",>", &next);
654 /* Associate the field list to TY. */
655 ty->u.s.fields = fields;
657 free (str);
659 return ty;
663 /* Given a typedef name S, return its associated type. Return NULL if
664 S is not a registered type name. */
666 static type_p
667 type_for_name (const char *s)
669 pair_p p;
671 /* Special-case support for types within a "gcc::" namespace. Rather
672 than fully-supporting namespaces, simply strip off the "gcc::" prefix
673 where present. This allows us to have GTY roots of this form:
674 extern GTY(()) gcc::some_type *some_ptr;
675 where the autogenerated functions will refer to simply "some_type",
676 where they can be resolved into their namespace. */
677 if (0 == strncmp (s, "gcc::", 5))
678 s += 5;
680 for (p = typedefs; p != NULL; p = p->next)
681 if (strcmp (p->name, s) == 0)
682 return p->type;
683 return NULL;
687 /* Create an undefined type with name S and location POS. Return the
688 newly created type. */
690 static type_p
691 create_undefined_type (const char *s, struct fileloc *pos)
693 type_p ty = find_structure (s, TYPE_UNDEFINED);
694 ty->u.s.line = *pos;
695 ty->u.s.bitmap = get_lang_bitmap (pos->file);
696 do_typedef (s, ty, pos);
697 return ty;
701 /* Return the type previously defined for S. Use POS to report errors. */
703 type_p
704 resolve_typedef (const char *s, struct fileloc *pos)
706 bool is_template_instance = (strchr (s, '<') != NULL);
707 type_p p = type_for_name (s);
709 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
710 type for regular type identifiers. If the type identifier S is a
711 template instantiation, however, we treat it as a user defined
712 type.
714 FIXME, this is actually a limitation in gengtype. Supporting
715 template types and their instances would require keeping separate
716 track of the basic types definition and its instances. This
717 essentially forces all template classes in GC to be marked
718 GTY((user)). */
719 if (!p)
720 p = (is_template_instance)
721 ? create_user_defined_type (s, pos)
722 : create_undefined_type (s, pos);
724 return p;
727 /* Add SUBCLASS to head of linked list of BASE's subclasses. */
729 void add_subclass (type_p base, type_p subclass)
731 gcc_assert (union_or_struct_p (base));
732 gcc_assert (union_or_struct_p (subclass));
734 subclass->u.s.next_sibling_class = base->u.s.first_subclass;
735 base->u.s.first_subclass = subclass;
738 /* Create and return a new structure with tag NAME at POS with fields
739 FIELDS and options O. The KIND of structure must be one of
740 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
742 type_p
743 new_structure (const char *name, enum typekind kind, struct fileloc *pos,
744 pair_p fields, options_p o, type_p base_class)
746 type_p si;
747 type_p s = NULL;
748 lang_bitmap bitmap = get_lang_bitmap (pos->file);
749 bool isunion = (kind == TYPE_UNION);
751 gcc_assert (union_or_struct_p (kind));
753 for (si = structures; si != NULL; si = si->next)
754 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
756 type_p ls = NULL;
757 if (si->kind == TYPE_LANG_STRUCT)
759 ls = si;
761 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
762 if (si->u.s.bitmap == bitmap)
763 s = si;
765 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
767 ls = si;
768 type_count++;
769 si = XCNEW (struct type);
770 memcpy (si, ls, sizeof (struct type));
771 ls->kind = TYPE_LANG_STRUCT;
772 ls->u.s.lang_struct = si;
773 ls->u.s.fields = NULL;
774 si->next = NULL;
775 si->state_number = -type_count;
776 si->pointer_to = NULL;
777 si->u.s.lang_struct = ls;
779 else
780 s = si;
782 if (ls != NULL && s == NULL)
784 type_count++;
785 s = XCNEW (struct type);
786 s->state_number = -type_count;
787 s->next = ls->u.s.lang_struct;
788 ls->u.s.lang_struct = s;
789 s->u.s.lang_struct = ls;
791 break;
794 if (s == NULL)
796 type_count++;
797 s = XCNEW (struct type);
798 s->state_number = -type_count;
799 s->next = structures;
800 structures = s;
803 if (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap))
805 error_at_line (pos, "duplicate definition of '%s %s'",
806 isunion ? "union" : "struct", s->u.s.tag);
807 error_at_line (&s->u.s.line, "previous definition here");
810 s->kind = kind;
811 s->u.s.tag = name;
812 s->u.s.line = *pos;
813 s->u.s.fields = fields;
814 s->u.s.opt = o;
815 s->u.s.bitmap = bitmap;
816 if (s->u.s.lang_struct)
817 s->u.s.lang_struct->u.s.bitmap |= bitmap;
818 s->u.s.base_class = base_class;
819 if (base_class)
820 add_subclass (base_class, s);
822 return s;
825 /* Return the previously-defined structure or union with tag NAME,
826 or a new empty structure or union if none was defined previously.
827 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
828 TYPE_USER_STRUCT. */
830 type_p
831 find_structure (const char *name, enum typekind kind)
833 type_p s;
834 bool isunion = (kind == TYPE_UNION);
836 gcc_assert (kind == TYPE_UNDEFINED || union_or_struct_p (kind));
838 for (s = structures; s != NULL; s = s->next)
839 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
840 return s;
842 type_count++;
843 s = XCNEW (struct type);
844 s->next = structures;
845 s->state_number = -type_count;
846 structures = s;
847 s->kind = kind;
848 s->u.s.tag = name;
849 structures = s;
850 return s;
853 /* Return a scalar type with name NAME. */
855 type_p
856 create_scalar_type (const char *name)
858 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
859 return &scalar_char;
860 else
861 return &scalar_nonchar;
865 /* Return a pointer to T. */
867 type_p
868 create_pointer (type_p t)
870 if (!t->pointer_to)
872 type_p r = XCNEW (struct type);
873 type_count++;
874 r->state_number = -type_count;
875 r->kind = TYPE_POINTER;
876 r->u.p = t;
877 t->pointer_to = r;
879 return t->pointer_to;
882 /* Return an array of length LEN. */
884 type_p
885 create_array (type_p t, const char *len)
887 type_p v;
889 type_count++;
890 v = XCNEW (struct type);
891 v->kind = TYPE_ARRAY;
892 v->state_number = -type_count;
893 v->u.a.p = t;
894 v->u.a.len = len;
895 return v;
898 /* Return a string options structure with name NAME and info INFO.
899 NEXT is the next option in the chain. */
900 options_p
901 create_string_option (options_p next, const char *name, const char *info)
903 options_p o = XNEW (struct options);
904 o->kind = OPTION_STRING;
905 o->next = next;
906 o->name = name;
907 o->info.string = info;
908 return o;
911 /* Create a type options structure with name NAME and info INFO. NEXT
912 is the next option in the chain. */
913 options_p
914 create_type_option (options_p next, const char* name, type_p info)
916 options_p o = XNEW (struct options);
917 o->next = next;
918 o->name = name;
919 o->kind = OPTION_TYPE;
920 o->info.type = info;
921 return o;
924 /* Create a nested pointer options structure with name NAME and info
925 INFO. NEXT is the next option in the chain. */
926 options_p
927 create_nested_option (options_p next, const char* name,
928 struct nested_ptr_data* info)
930 options_p o;
931 o = XNEW (struct options);
932 o->next = next;
933 o->name = name;
934 o->kind = OPTION_NESTED;
935 o->info.nested = info;
936 return o;
939 /* Return an options structure for a "nested_ptr" option. */
940 options_p
941 create_nested_ptr_option (options_p next, type_p t,
942 const char *to, const char *from)
944 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
946 d->type = adjust_field_type (t, 0);
947 d->convert_to = to;
948 d->convert_from = from;
949 return create_nested_option (next, "nested_ptr", d);
952 /* Add a variable named S of type T with options O defined at POS,
953 to `variables'. */
954 void
955 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
957 pair_p n;
958 n = XNEW (struct pair);
959 n->name = s;
960 n->type = t;
961 n->line = *pos;
962 n->opt = o;
963 n->next = variables;
964 variables = n;
967 /* Most-general structure field creator. */
968 static pair_p
969 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
970 const input_file *inpf, int line)
972 pair_p field;
974 field = XNEW (struct pair);
975 field->next = next;
976 field->type = type;
977 field->name = name;
978 field->opt = opt;
979 field->line.file = inpf;
980 field->line.line = line;
981 return field;
984 /* Create a field that came from the source code we are scanning,
985 i.e. we have a 'struct fileloc', and possibly options; also,
986 adjust_field_type should be called. */
987 pair_p
988 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
989 struct fileloc *pos)
991 return create_field_all (next, adjust_field_type (type, opt),
992 name, opt, pos->file, pos->line);
995 /* Create a fake field with the given type and name. NEXT is the next
996 field in the chain. */
997 #define create_field(next,type,name) \
998 create_field_all (next,type,name, 0, this_file, __LINE__)
1000 /* Like create_field, but the field is only valid when condition COND
1001 is true. */
1003 static pair_p
1004 create_optional_field_ (pair_p next, type_p type, const char *name,
1005 const char *cond, int line)
1007 static int id = 1;
1008 pair_p union_fields;
1009 type_p union_type;
1011 /* Create a fake union type with a single nameless field of type TYPE.
1012 The field has a tag of "1". This allows us to make the presence
1013 of a field of type TYPE depend on some boolean "desc" being true. */
1014 union_fields = create_field (NULL, type, "");
1015 union_fields->opt =
1016 create_string_option (union_fields->opt, "dot", "");
1017 union_fields->opt =
1018 create_string_option (union_fields->opt, "tag", "1");
1019 union_type =
1020 new_structure (xasprintf ("%s_%d", "fake_union", id++), TYPE_UNION,
1021 &lexer_line, union_fields, NULL, NULL);
1023 /* Create the field and give it the new fake union type. Add a "desc"
1024 tag that specifies the condition under which the field is valid. */
1025 return create_field_all (next, union_type, name,
1026 create_string_option (0, "desc", cond),
1027 this_file, line);
1030 #define create_optional_field(next,type,name,cond) \
1031 create_optional_field_(next,type,name,cond,__LINE__)
1033 /* Reverse a linked list of 'struct pair's in place. */
1034 pair_p
1035 nreverse_pairs (pair_p list)
1037 pair_p prev = 0, p, next;
1038 for (p = list; p; p = next)
1040 next = p->next;
1041 p->next = prev;
1042 prev = p;
1044 return prev;
1048 /* We don't care how long a CONST_DOUBLE is. */
1049 #define CONST_DOUBLE_FORMAT "ww"
1050 /* We don't want to see codes that are only for generator files. */
1051 #undef GENERATOR_FILE
1053 enum rtx_code
1055 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1056 #include "rtl.def"
1057 #undef DEF_RTL_EXPR
1058 NUM_RTX_CODE
1061 static const char *const rtx_name[NUM_RTX_CODE] = {
1062 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1063 #include "rtl.def"
1064 #undef DEF_RTL_EXPR
1067 static const char *const rtx_format[NUM_RTX_CODE] = {
1068 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1069 #include "rtl.def"
1070 #undef DEF_RTL_EXPR
1073 static int rtx_next_new[NUM_RTX_CODE];
1075 /* We also need codes and names for insn notes (not register notes).
1076 Note that we do *not* bias the note values here. */
1077 enum insn_note
1079 #define DEF_INSN_NOTE(NAME) NAME,
1080 #include "insn-notes.def"
1081 #undef DEF_INSN_NOTE
1083 NOTE_INSN_MAX
1086 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1087 default field for line number notes. */
1088 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
1089 #define DEF_INSN_NOTE(NAME) #NAME,
1090 #include "insn-notes.def"
1091 #undef DEF_INSN_NOTE
1094 #undef CONST_DOUBLE_FORMAT
1095 #define GENERATOR_FILE
1097 /* Generate the contents of the rtx_next array. This really doesn't belong
1098 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1100 static void
1101 gen_rtx_next (void)
1103 int i;
1104 for (i = 0; i < NUM_RTX_CODE; i++)
1106 int k;
1108 rtx_next_new[i] = -1;
1109 if (strncmp (rtx_format[i], "uu", 2) == 0)
1110 rtx_next_new[i] = 1;
1111 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1112 rtx_next_new[i] = 1;
1113 else
1114 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
1115 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1116 rtx_next_new[i] = k;
1120 /* Write out the contents of the rtx_next array. */
1121 static void
1122 write_rtx_next (void)
1124 outf_p f = get_output_file_with_visibility (NULL);
1125 int i;
1126 if (!f)
1127 return;
1129 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
1130 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1131 for (i = 0; i < NUM_RTX_CODE; i++)
1132 if (rtx_next_new[i] == -1)
1133 oprintf (f, " 0,\n");
1134 else
1135 oprintf (f,
1136 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1137 oprintf (f, "};\n");
1140 /* Handle `special("rtx_def")'. This is a special case for field
1141 `fld' of struct rtx_def, which is an array of unions whose values
1142 are based in a complex way on the type of RTL. */
1144 static type_p
1145 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1147 pair_p flds = NULL;
1148 options_p nodot;
1149 int i;
1150 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1151 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1153 if (t->kind != TYPE_UNION)
1155 error_at_line (&lexer_line,
1156 "special `rtx_def' must be applied to a union");
1157 return &string_type;
1160 nodot = create_string_option (NULL, "dot", "");
1162 rtx_tp = create_pointer (find_structure ("rtx_def", TYPE_STRUCT));
1163 rtvec_tp = create_pointer (find_structure ("rtvec_def", TYPE_STRUCT));
1164 tree_tp = create_pointer (find_structure ("tree_node", TYPE_UNION));
1165 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", TYPE_STRUCT));
1166 reg_attrs_tp =
1167 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT));
1168 basic_block_tp =
1169 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT));
1170 constant_tp =
1171 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT));
1172 scalar_tp = &scalar_nonchar; /* rtunion int */
1175 pair_p note_flds = NULL;
1176 int c;
1178 for (c = 0; c <= NOTE_INSN_MAX; c++)
1180 switch (c)
1182 case NOTE_INSN_MAX:
1183 case NOTE_INSN_DELETED_LABEL:
1184 case NOTE_INSN_DELETED_DEBUG_LABEL:
1185 note_flds = create_field (note_flds, &string_type, "rt_str");
1186 break;
1188 case NOTE_INSN_BLOCK_BEG:
1189 case NOTE_INSN_BLOCK_END:
1190 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1191 break;
1193 case NOTE_INSN_VAR_LOCATION:
1194 case NOTE_INSN_CALL_ARG_LOCATION:
1195 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1196 break;
1198 default:
1199 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1200 break;
1202 /* NOTE_INSN_MAX is used as the default field for line
1203 number notes. */
1204 if (c == NOTE_INSN_MAX)
1205 note_flds->opt =
1206 create_string_option (nodot, "default", "");
1207 else
1208 note_flds->opt =
1209 create_string_option (nodot, "tag", note_insn_name[c]);
1211 note_union_tp = new_structure ("rtx_def_note_subunion", TYPE_UNION,
1212 &lexer_line, note_flds, NULL, NULL);
1214 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1216 pair_p sym_flds;
1217 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1218 sym_flds->opt = create_string_option (nodot, "default", "");
1219 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1220 sym_flds->opt = create_string_option (nodot, "tag", "1");
1221 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", TYPE_UNION,
1222 &lexer_line, sym_flds, NULL, NULL);
1224 for (i = 0; i < NUM_RTX_CODE; i++)
1226 pair_p subfields = NULL;
1227 size_t aindex, nmindex;
1228 const char *sname;
1229 type_p substruct;
1230 char *ftag;
1232 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1234 type_p t;
1235 const char *subname;
1237 switch (rtx_format[i][aindex])
1239 case '*':
1240 case 'i':
1241 case 'n':
1242 case 'w':
1243 case 'r':
1244 t = scalar_tp;
1245 subname = "rt_int";
1246 break;
1248 case '0':
1249 if (i == MEM && aindex == 1)
1250 t = mem_attrs_tp, subname = "rt_mem";
1251 else if (i == JUMP_INSN && aindex == 7)
1252 t = rtx_tp, subname = "rt_rtx";
1253 else if (i == CODE_LABEL && aindex == 4)
1254 t = scalar_tp, subname = "rt_int";
1255 else if (i == CODE_LABEL && aindex == 3)
1256 t = rtx_tp, subname = "rt_rtx";
1257 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1258 t = rtx_tp, subname = "rt_rtx";
1259 else if (i == NOTE && aindex == 3)
1260 t = note_union_tp, subname = "";
1261 else if (i == NOTE && aindex == 4)
1262 t = scalar_tp, subname = "rt_int";
1263 else if (i == NOTE && aindex >= 6)
1264 t = scalar_tp, subname = "rt_int";
1265 else if (i == ADDR_DIFF_VEC && aindex == 4)
1266 t = scalar_tp, subname = "rt_int";
1267 else if (i == VALUE && aindex == 0)
1268 t = scalar_tp, subname = "rt_int";
1269 else if (i == DEBUG_EXPR && aindex == 0)
1270 t = tree_tp, subname = "rt_tree";
1271 else if (i == SYMBOL_REF && aindex == 1)
1272 t = symbol_union_tp, subname = "";
1273 else if (i == JUMP_TABLE_DATA && aindex >= 4)
1274 t = scalar_tp, subname = "rt_int";
1275 else if (i == BARRIER && aindex >= 2)
1276 t = scalar_tp, subname = "rt_int";
1277 else if (i == ENTRY_VALUE && aindex == 0)
1278 t = rtx_tp, subname = "rt_rtx";
1279 else
1281 error_at_line
1282 (&lexer_line,
1283 "rtx type `%s' has `0' in position %lu, can't handle",
1284 rtx_name[i], (unsigned long) aindex);
1285 t = &string_type;
1286 subname = "rt_int";
1288 break;
1290 case 's':
1291 case 'S':
1292 case 'T':
1293 t = &string_type;
1294 subname = "rt_str";
1295 break;
1297 case 'e':
1298 case 'u':
1299 t = rtx_tp;
1300 subname = "rt_rtx";
1301 break;
1303 case 'E':
1304 case 'V':
1305 t = rtvec_tp;
1306 subname = "rt_rtvec";
1307 break;
1309 case 't':
1310 t = tree_tp;
1311 subname = "rt_tree";
1312 break;
1314 case 'B':
1315 t = basic_block_tp;
1316 subname = "rt_bb";
1317 break;
1319 default:
1320 error_at_line
1321 (&lexer_line,
1322 "rtx type `%s' has `%c' in position %lu, can't handle",
1323 rtx_name[i], rtx_format[i][aindex],
1324 (unsigned long) aindex);
1325 t = &string_type;
1326 subname = "rt_int";
1327 break;
1330 subfields = create_field (subfields, t,
1331 xasprintf (".fld[%lu].%s",
1332 (unsigned long) aindex,
1333 subname));
1334 subfields->opt = nodot;
1335 if (t == note_union_tp)
1336 subfields->opt =
1337 create_string_option (subfields->opt, "desc",
1338 "NOTE_KIND (&%0)");
1339 if (t == symbol_union_tp)
1340 subfields->opt =
1341 create_string_option (subfields->opt, "desc",
1342 "CONSTANT_POOL_ADDRESS_P (&%0)");
1345 if (i == REG)
1346 subfields = create_field (subfields, reg_attrs_tp, "reg.attrs");
1348 if (i == SYMBOL_REF)
1350 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1351 holds. */
1352 type_p field_tp = find_structure ("block_symbol", TYPE_STRUCT);
1353 subfields
1354 = create_optional_field (subfields, field_tp, "block_sym",
1355 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1358 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1359 substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
1360 NULL, NULL);
1362 ftag = xstrdup (rtx_name[i]);
1363 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1364 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1365 flds = create_field (flds, substruct, "");
1366 flds->opt = create_string_option (nodot, "tag", ftag);
1368 return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
1369 nodot, NULL);
1372 /* Handle `special("tree_exp")'. This is a special case for
1373 field `operands' of struct tree_exp, which although it claims to contain
1374 pointers to trees, actually sometimes contains pointers to RTL too.
1375 Passed T, the old type of the field, and OPT its options. Returns
1376 a new type for the field. */
1378 static type_p
1379 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1381 pair_p flds;
1382 options_p nodot;
1384 if (t->kind != TYPE_ARRAY)
1386 error_at_line (&lexer_line,
1387 "special `tree_exp' must be applied to an array");
1388 return &string_type;
1391 nodot = create_string_option (NULL, "dot", "");
1393 flds = create_field (NULL, t, "");
1394 flds->opt = create_string_option (nodot, "length",
1395 "TREE_OPERAND_LENGTH ((tree) &%0)");
1396 flds->opt = create_string_option (flds->opt, "default", "");
1398 return new_structure ("tree_exp_subunion", TYPE_UNION, &lexer_line, flds,
1399 nodot, NULL);
1402 /* Perform any special processing on a type T, about to become the type
1403 of a field. Return the appropriate type for the field.
1404 At present:
1405 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1406 - Similarly for arrays of pointer-to-char;
1407 - Converts structures for which a parameter is provided to
1408 TYPE_PARAM_STRUCT;
1409 - Handles "special" options.
1412 type_p
1413 adjust_field_type (type_p t, options_p opt)
1415 int length_p = 0;
1416 const int pointer_p = t->kind == TYPE_POINTER;
1418 for (; opt; opt = opt->next)
1419 if (strcmp (opt->name, "length") == 0)
1421 if (length_p)
1422 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1423 if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
1425 error_at_line (&lexer_line,
1426 "option `%s' may not be applied to "
1427 "arrays of atomic types", opt->name);
1429 length_p = 1;
1431 else if (strcmp (opt->name, "special") == 0
1432 && opt->kind == OPTION_STRING)
1434 const char *special_name = opt->info.string;
1435 if (strcmp (special_name, "tree_exp") == 0)
1436 t = adjust_field_tree_exp (t, opt);
1437 else if (strcmp (special_name, "rtx_def") == 0)
1438 t = adjust_field_rtx_def (t, opt);
1439 else
1440 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1443 if (!length_p
1444 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1445 return &string_type;
1446 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1447 && t->u.a.p->u.p->kind == TYPE_SCALAR
1448 && t->u.a.p->u.p->u.scalar_is_char)
1449 return create_array (&string_type, t->u.a.len);
1451 return t;
1455 static void set_gc_used_type (type_p, enum gc_used_enum, bool = false);
1456 static void set_gc_used (pair_p);
1458 /* Handle OPT for set_gc_used_type. */
1460 static void
1461 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1462 int *length, int *skip, type_p *nested_ptr)
1464 options_p o;
1465 for (o = opt; o; o = o->next)
1466 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1467 && o->kind == OPTION_TYPE)
1468 set_gc_used_type (o->info.type,
1469 GC_POINTED_TO);
1470 else if (strcmp (o->name, "maybe_undef") == 0)
1471 *maybe_undef = 1;
1472 else if (strcmp (o->name, "length") == 0)
1473 *length = 1;
1474 else if (strcmp (o->name, "skip") == 0)
1475 *skip = 1;
1476 else if (strcmp (o->name, "nested_ptr") == 0
1477 && o->kind == OPTION_NESTED)
1478 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1482 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1484 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1485 are set to GC_UNUSED. Otherwise, an error is emitted for
1486 TYPE_UNDEFINED types. This is used to support user-defined
1487 template types with non-type arguments.
1489 For instance, when we parse a template type with enum arguments
1490 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1491 artificial fields for 'MyType', one for 'AnotherType', the other
1492 one for 'EnumValue'.
1494 At the time that we parse this type we don't know that 'EnumValue'
1495 is really an enum value, so the parser creates a TYPE_UNDEFINED
1496 type for it. Since 'EnumValue' is never resolved to a known
1497 structure, it will stay with TYPE_UNDEFINED.
1499 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1500 'EnumValue'. Generating marking code for it would cause
1501 compilation failures since the marking routines assumes that
1502 'EnumValue' is a type. */
1504 static void
1505 set_gc_used_type (type_p t, enum gc_used_enum level,
1506 bool allow_undefined_types)
1508 if (t->gc_used >= level)
1509 return;
1511 t->gc_used = level;
1513 switch (t->kind)
1515 case TYPE_STRUCT:
1516 case TYPE_UNION:
1517 case TYPE_USER_STRUCT:
1519 pair_p f;
1520 int dummy;
1521 type_p dummy2;
1522 bool allow_undefined_field_types = (t->kind == TYPE_USER_STRUCT);
1524 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy,
1525 &dummy2);
1527 if (t->u.s.base_class)
1528 set_gc_used_type (t->u.s.base_class, level, allow_undefined_types);
1529 /* Anything pointing to a base class might actually be pointing
1530 to a subclass. */
1531 for (type_p subclass = t->u.s.first_subclass; subclass;
1532 subclass = subclass->u.s.next_sibling_class)
1533 set_gc_used_type (subclass, level, allow_undefined_types);
1535 FOR_ALL_INHERITED_FIELDS(t, f)
1537 int maybe_undef = 0;
1538 int length = 0;
1539 int skip = 0;
1540 type_p nested_ptr = NULL;
1541 process_gc_options (f->opt, level, &maybe_undef, &length, &skip,
1542 &nested_ptr);
1544 if (nested_ptr && f->type->kind == TYPE_POINTER)
1545 set_gc_used_type (nested_ptr, GC_POINTED_TO);
1546 else if (length && f->type->kind == TYPE_POINTER)
1547 set_gc_used_type (f->type->u.p, GC_USED);
1548 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1549 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO);
1550 else if (skip)
1551 ; /* target type is not used through this field */
1552 else
1553 set_gc_used_type (f->type, GC_USED, allow_undefined_field_types);
1555 break;
1558 case TYPE_UNDEFINED:
1559 if (level > GC_UNUSED)
1561 if (!allow_undefined_types)
1562 error_at_line (&t->u.s.line, "undefined type `%s'", t->u.s.tag);
1563 t->gc_used = GC_UNUSED;
1565 break;
1567 case TYPE_POINTER:
1568 set_gc_used_type (t->u.p, GC_POINTED_TO);
1569 break;
1571 case TYPE_ARRAY:
1572 set_gc_used_type (t->u.a.p, GC_USED);
1573 break;
1575 case TYPE_LANG_STRUCT:
1576 for (t = t->u.s.lang_struct; t; t = t->next)
1577 set_gc_used_type (t, level);
1578 break;
1580 default:
1581 break;
1585 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1587 static void
1588 set_gc_used (pair_p variables)
1590 int nbvars = 0;
1591 pair_p p;
1592 for (p = variables; p; p = p->next)
1594 set_gc_used_type (p->type, GC_USED);
1595 nbvars++;
1597 if (verbosity_level >= 2)
1598 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1601 /* File mapping routines. For each input file, there is one output .c file
1602 (but some output files have many input files), and there is one .h file
1603 for the whole build. */
1605 /* Output file handling. */
1607 /* Create and return an outf_p for a new file for NAME, to be called
1608 ONAME. */
1610 static outf_p
1611 create_file (const char *name, const char *oname)
1613 static const char *const hdr[] = {
1614 " Copyright (C) 2004-2015 Free Software Foundation, Inc.\n",
1615 "\n",
1616 "This file is part of GCC.\n",
1617 "\n",
1618 "GCC is free software; you can redistribute it and/or modify it under\n",
1619 "the terms of the GNU General Public License as published by the Free\n",
1620 "Software Foundation; either version 3, or (at your option) any later\n",
1621 "version.\n",
1622 "\n",
1623 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1624 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1625 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1626 "for more details.\n",
1627 "\n",
1628 "You should have received a copy of the GNU General Public License\n",
1629 "along with GCC; see the file COPYING3. If not see\n",
1630 "<http://www.gnu.org/licenses/>. */\n",
1631 "\n",
1632 "/* This file is machine generated. Do not edit. */\n"
1634 outf_p f;
1635 size_t i;
1637 gcc_assert (name != NULL);
1638 gcc_assert (oname != NULL);
1639 f = XCNEW (struct outf);
1640 f->next = output_files;
1641 f->name = oname;
1642 output_files = f;
1644 oprintf (f, "/* Type information for %s.\n", name);
1645 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1646 oprintf (f, "%s", hdr[i]);
1647 return f;
1650 /* Print, like fprintf, to O.
1651 N.B. You might think this could be implemented more efficiently
1652 with vsnprintf(). Unfortunately, there are C libraries that
1653 provide that function but without the C99 semantics for its return
1654 value, making it impossible to know how much space is required. */
1655 void
1656 oprintf (outf_p o, const char *format, ...)
1658 char *s;
1659 size_t slength;
1660 va_list ap;
1662 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1663 in that case. */
1664 if (!o)
1665 return;
1667 va_start (ap, format);
1668 slength = vasprintf (&s, format, ap);
1669 if (s == NULL || (int) slength < 0)
1670 fatal ("out of memory");
1671 va_end (ap);
1673 if (o->bufused + slength > o->buflength)
1675 size_t new_len = o->buflength;
1676 if (new_len == 0)
1677 new_len = 1024;
1680 new_len *= 2;
1682 while (o->bufused + slength >= new_len);
1683 o->buf = XRESIZEVEC (char, o->buf, new_len);
1684 o->buflength = new_len;
1686 memcpy (o->buf + o->bufused, s, slength);
1687 o->bufused += slength;
1688 free (s);
1691 /* Open the global header file and the language-specific header files. */
1693 static void
1694 open_base_files (void)
1696 size_t i;
1698 if (nb_plugin_files > 0 && plugin_files)
1699 return;
1701 header_file = create_file ("GCC", "gtype-desc.h");
1703 base_files = XNEWVEC (outf_p, num_lang_dirs);
1705 for (i = 0; i < num_lang_dirs; i++)
1706 base_files[i] = create_file (lang_dir_names[i],
1707 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1709 /* gtype-desc.c is a little special, so we create it here. */
1711 /* The order of files here matters very much. */
1712 static const char *const ifiles[] = {
1713 "config.h", "system.h", "coretypes.h", "tm.h", "insn-codes.h",
1714 "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1715 "alias.h", "symtab.h", "options.h",
1716 "tree.h", "fold-const.h", "rtl.h",
1717 "hard-reg-set.h", "predict.h",
1718 "function.h", "insn-config.h", "flags.h",
1719 "tree.h", "expmed.h", "dojump.h",
1720 "explow.h", "calls.h", "emit-rtl.h", "varasm.h", "stmt.h",
1721 "expr.h", "alloc-pool.h",
1722 "basic-block.h", "cselib.h", "insn-addr.h",
1723 "optabs.h", "libfuncs.h", "debug.h",
1724 "dominance.h", "cfg.h", "basic-block.h",
1725 "tree-ssa-alias.h", "internal-fn.h", "gimple-fold.h", "tree-eh.h",
1726 "gimple-expr.h", "is-a.h",
1727 "gimple.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
1728 "tree-phinodes.h", "ssa-iterators.h", "stringpool.h", "tree-ssanames.h",
1729 "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h", "tree-ssa-loop-manip.h",
1730 "tree-ssa-loop-niter.h", "tree-into-ssa.h", "tree-dfa.h",
1731 "tree-ssa.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1732 "except.h", "output.h", "cfgloop.h", "target.h", "lto-streamer.h",
1733 "target-globals.h", "ipa-ref.h", "cgraph.h", "symbol-summary.h",
1734 "ipa-prop.h", "ipa-inline.h", "dwarf2out.h", "omp-low.h", NULL
1736 const char *const *ifp;
1737 outf_p gtype_desc_c;
1739 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1740 for (ifp = ifiles; *ifp; ifp++)
1741 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1743 /* Make sure we handle "cfun" specially. */
1744 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1745 oprintf (gtype_desc_c, "#undef cfun\n");
1747 oprintf (gtype_desc_c,
1748 "\n"
1749 "/* Types with a \"gcc::\" namespace have it stripped\n"
1750 " during gengtype parsing. Provide a \"using\" directive\n"
1751 " to ensure that the fully-qualified types are found. */\n"
1752 "using namespace gcc;\n");
1756 /* For INPF an input file, return the real basename of INPF, with all
1757 the directory components skipped. */
1759 static const char *
1760 get_file_realbasename (const input_file *inpf)
1762 return lbasename (get_input_file_name (inpf));
1765 /* For INPF a filename, return the relative path to INPF from
1766 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1768 const char *
1769 get_file_srcdir_relative_path (const input_file *inpf)
1771 const char *f = get_input_file_name (inpf);
1772 if (strlen (f) > srcdir_len
1773 && IS_DIR_SEPARATOR (f[srcdir_len])
1774 && strncmp (f, srcdir, srcdir_len) == 0)
1775 return f + srcdir_len + 1;
1776 else
1777 return NULL;
1780 /* For INPF an input_file, return the relative path to INPF from
1781 $(srcdir) if the latter is a prefix in INPF, or the real basename
1782 of INPF otherwise. */
1784 static const char *
1785 get_file_basename (const input_file *inpf)
1787 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1789 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1792 /* For F a filename, return the lang_dir_names relative index of the language
1793 directory that is a prefix in F, if any, -1 otherwise. */
1795 static int
1796 get_prefix_langdir_index (const char *f)
1798 size_t f_len = strlen (f);
1799 size_t lang_index;
1801 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1803 const char *langdir = lang_dir_names[lang_index];
1804 size_t langdir_len = strlen (langdir);
1806 if (f_len > langdir_len
1807 && IS_DIR_SEPARATOR (f[langdir_len])
1808 && memcmp (f, langdir, langdir_len) == 0)
1809 return lang_index;
1812 return -1;
1815 /* For INPF an input file, return the name of language directory where
1816 F is located, if any, NULL otherwise. */
1818 static const char *
1819 get_file_langdir (const input_file *inpf)
1821 /* Get the relative path to INPF from $(srcdir) and find the
1822 language by comparing the prefix with language directory names.
1823 If INPF is not even srcdir relative, no point in looking
1824 further. */
1826 int lang_index;
1827 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1828 const char *r;
1830 if (!srcdir_relative_path)
1831 return NULL;
1833 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1834 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1835 r = "c-family";
1836 else if (lang_index >= 0)
1837 r = lang_dir_names[lang_index];
1838 else
1839 r = NULL;
1841 return r;
1844 /* The gt- output file name for INPF. */
1846 static const char *
1847 get_file_gtfilename (const input_file *inpf)
1849 /* Cook up an initial version of the gt- file name from the file real
1850 basename and the language name, if any. */
1852 const char *basename = get_file_realbasename (inpf);
1853 const char *langdir = get_file_langdir (inpf);
1855 char *result =
1856 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1857 : xasprintf ("gt-%s", basename));
1859 /* Then replace all non alphanumerics characters by '-' and change the
1860 extension to ".h". We expect the input filename extension was at least
1861 one character long. */
1863 char *s = result;
1865 for (; *s != '.'; s++)
1866 if (!ISALNUM (*s) && *s != '-')
1867 *s = '-';
1869 memcpy (s, ".h", sizeof (".h"));
1871 return result;
1874 /* Each input_file has its associated output file outf_p. The
1875 association is computed by the function
1876 get_output_file_with_visibility. The associated file is cached
1877 inside input_file in its inpoutf field, so is really computed only
1878 once. Associated output file paths (i.e. output_name-s) are
1879 computed by a rule based regexp machinery, using the files_rules
1880 array of struct file_rule_st. A for_name is also computed, giving
1881 the source file name for which the output_file is generated; it is
1882 often the last component of the input_file path. */
1886 Regexpr machinery to compute the output_name and for_name-s of each
1887 input_file. We have a sequence of file rules which gives the POSIX
1888 extended regular expression to match an input file path, and two
1889 transformed strings for the corresponding output_name and the
1890 corresponding for_name. The transformed string contain dollars: $0
1891 is replaced by the entire match, $1 is replaced by the substring
1892 matching the first parenthesis in the regexp, etc. And $$ is replaced
1893 by a single verbatim dollar. The rule order is important. The
1894 general case is last, and the particular cases should come before.
1895 An action routine can, when needed, update the out_name & for_name
1896 and/or return the appropriate output file. It is invoked only when a
1897 rule is triggered. When a rule is triggered, the output_name and
1898 for_name are computed using their transform string in while $$, $0,
1899 $1, ... are suitably replaced. If there is an action, it is called.
1900 In some few cases, the action can directly return the outf_p, but
1901 usually it just updates the output_name and for_name so should free
1902 them before replacing them. The get_output_file_with_visibility
1903 function creates an outf_p only once per each output_name, so it
1904 scans the output_files list for previously seen output file names.
1907 /* Signature of actions in file rules. */
1908 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
1911 struct file_rule_st {
1912 const char* frul_srcexpr; /* Source string for regexp. */
1913 int frul_rflags; /* Flags passed to regcomp, usually
1914 * REG_EXTENDED. */
1915 regex_t* frul_re; /* Compiled regular expression
1916 obtained by regcomp. */
1917 const char* frul_tr_out; /* Transformation string for making
1918 * the output_name, with $1 ... $9 for
1919 * subpatterns and $0 for the whole
1920 * matched filename. */
1921 const char* frul_tr_for; /* Tranformation string for making the
1922 for_name. */
1923 frul_actionrout_t* frul_action; /* The action, if non null, is
1924 * called once the rule matches, on
1925 * the transformed out_name &
1926 * for_name. It could change them
1927 * and/or give the output file. */
1930 /* File rule action handling *.h files. */
1931 static outf_p header_dot_h_frul (input_file*, char**, char**);
1933 /* File rule action handling *.c files. */
1934 static outf_p source_dot_c_frul (input_file*, char**, char**);
1936 #define NULL_REGEX (regex_t*)0
1938 /* The prefix in our regexp-s matching the directory. */
1939 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1941 #define NULL_FRULACT (frul_actionrout_t*)0
1943 /* The array of our rules governing file name generation. Rules order
1944 matters, so change with extreme care! */
1946 struct file_rule_st files_rules[] = {
1947 /* The general rule assumes that files in subdirectories belong to a
1948 particular front-end, and files not in subdirectories are shared.
1949 The following rules deal with exceptions - files that are in
1950 subdirectories and yet are shared, and files that are top-level,
1951 but are not shared. */
1953 /* the c-family/ source directory is special. */
1954 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
1955 REG_EXTENDED, NULL_REGEX,
1956 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
1958 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
1959 REG_EXTENDED, NULL_REGEX,
1960 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
1962 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
1963 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
1964 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1966 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
1967 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1969 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
1970 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
1971 REG_EXTENDED, NULL_REGEX,
1972 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
1974 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
1975 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
1976 REG_EXTENDED, NULL_REGEX,
1977 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
1979 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
1980 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
1981 REG_EXTENDED, NULL_REGEX,
1982 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
1984 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
1985 { DIR_PREFIX_REGEX "cp/parser\\.h$",
1986 REG_EXTENDED, NULL_REGEX,
1987 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
1989 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
1990 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
1991 REG_EXTENDED, NULL_REGEX,
1992 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
1994 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
1995 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
1996 REG_EXTENDED, NULL_REGEX,
1997 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
1999 /* General cases. For header *.h and source *.c or *.cc files, we
2000 * need special actions to handle the language. */
2002 /* Source *.c files are using get_file_gtfilename to compute their
2003 output_name and get_file_basename to compute their for_name
2004 through the source_dot_c_frul action. */
2005 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
2006 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
2008 /* Source *.cc files are using get_file_gtfilename to compute their
2009 output_name and get_file_basename to compute their for_name
2010 through the source_dot_c_frul action. */
2011 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.cc$",
2012 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.cc", source_dot_c_frul},
2014 /* Common header files get "gtype-desc.c" as their output_name,
2015 * while language specific header files are handled specially. So
2016 * we need the header_dot_h_frul action. */
2017 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
2018 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
2020 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
2021 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
2023 /* Mandatory null last entry signaling end of rules. */
2024 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
2027 /* Special file rules action for handling *.h header files. It gives
2028 "gtype-desc.c" for common headers and corresponding output
2029 files for language-specific header files. */
2030 static outf_p
2031 header_dot_h_frul (input_file* inpf, char**poutname,
2032 char**pforname ATTRIBUTE_UNUSED)
2034 const char *basename = 0;
2035 int lang_index = 0;
2036 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2037 (void*) inpf, get_input_file_name (inpf),
2038 *poutname, *pforname);
2039 basename = get_file_basename (inpf);
2040 lang_index = get_prefix_langdir_index (basename);
2041 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
2043 if (lang_index >= 0)
2045 /* The header is language specific. Given output_name &
2046 for_name remains unchanged. The base_files array gives the
2047 outf_p. */
2048 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2049 (void*) base_files[lang_index],
2050 (base_files[lang_index])->name);
2051 return base_files[lang_index];
2053 else
2055 /* The header is common to all front-end languages. So
2056 output_name is "gtype-desc.c" file. The calling function
2057 get_output_file_with_visibility will find its outf_p. */
2058 free (*poutname);
2059 *poutname = xstrdup ("gtype-desc.c");
2060 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2061 get_input_file_name (inpf));
2062 return NULL;
2067 /* Special file rules action for handling *.c source files using
2068 * get_file_gtfilename to compute their output_name and
2069 * get_file_basename to compute their for_name. The output_name is
2070 * gt-<LANG>-<BASE>.h for language specific source files, and
2071 * gt-<BASE>.h for common source files. */
2072 static outf_p
2073 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
2075 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
2076 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
2077 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2078 (void*) inpf, get_input_file_name (inpf),
2079 *poutname, *pforname);
2080 DBGPRINTF ("newoutname %s", newoutname);
2081 DBGPRINTF ("newbasename %s", newbasename);
2082 free (*poutname);
2083 free (*pforname);
2084 *poutname = newoutname;
2085 *pforname = newbasename;
2086 return NULL;
2089 /* Utility function for get_output_file_with_visibility which returns
2090 * a malloc-ed substituted string using TRS on matching of the FILNAM
2091 * file name, using the PMATCH array. */
2092 static char*
2093 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
2094 const char *trs)
2096 struct obstack str_obstack;
2097 char *str = NULL;
2098 char *rawstr = NULL;
2099 const char *pt = NULL;
2100 DBGPRINTF ("filnam %s", filnam);
2101 obstack_init (&str_obstack);
2102 for (pt = trs; *pt; pt++) {
2103 char c = *pt;
2104 if (c == '$')
2106 if (pt[1] == '$')
2108 /* A double dollar $$ is substituted by a single verbatim
2109 dollar, but who really uses dollar signs in file
2110 paths? */
2111 obstack_1grow (&str_obstack, '$');
2113 else if (ISDIGIT (pt[1]))
2115 /* Handle $0 $1 ... $9 by appropriate substitution. */
2116 int dolnum = pt[1] - '0';
2117 int so = pmatch[dolnum].rm_so;
2118 int eo = pmatch[dolnum].rm_eo;
2119 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2120 if (so>=0 && eo>=so)
2121 obstack_grow (&str_obstack, filnam + so, eo - so);
2123 else
2125 /* This can happen only when files_rules is buggy! */
2126 gcc_unreachable ();
2128 /* Always skip the character after the dollar. */
2129 pt++;
2131 else
2132 obstack_1grow (&str_obstack, c);
2134 obstack_1grow (&str_obstack, '\0');
2135 rawstr = XOBFINISH (&str_obstack, char *);
2136 str = xstrdup (rawstr);
2137 obstack_free (&str_obstack, NULL);
2138 DBGPRINTF ("matched replacement %s", str);
2139 rawstr = NULL;
2140 return str;
2144 /* An output file, suitable for definitions, that can see declarations
2145 made in INPF and is linked into every language that uses INPF.
2146 Since the result is cached inside INPF, that argument cannot be
2147 declared constant, but is "almost" constant. */
2149 outf_p
2150 get_output_file_with_visibility (input_file *inpf)
2152 outf_p r;
2153 char *for_name = NULL;
2154 char *output_name = NULL;
2155 const char* inpfname;
2157 /* This can happen when we need a file with visibility on a
2158 structure that we've never seen. We have to just hope that it's
2159 globally visible. */
2160 if (inpf == NULL)
2161 inpf = system_h_file;
2163 /* The result is cached in INPF, so return it if already known. */
2164 if (inpf->inpoutf)
2165 return inpf->inpoutf;
2167 /* In plugin mode, return NULL unless the input_file is one of the
2168 plugin_files. */
2169 if (plugin_files)
2171 size_t i;
2172 for (i = 0; i < nb_plugin_files; i++)
2173 if (inpf == plugin_files[i])
2175 inpf->inpoutf = plugin_output;
2176 return plugin_output;
2179 return NULL;
2182 inpfname = get_input_file_name (inpf);
2184 /* Try each rule in sequence in files_rules until one is triggered. */
2186 int rulix = 0;
2187 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2188 (void*) inpf, inpfname);
2190 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2192 DBGPRINTF ("rulix#%d srcexpr %s",
2193 rulix, files_rules[rulix].frul_srcexpr);
2195 if (!files_rules[rulix].frul_re)
2197 /* Compile the regexpr lazily. */
2198 int err = 0;
2199 files_rules[rulix].frul_re = XCNEW (regex_t);
2200 err = regcomp (files_rules[rulix].frul_re,
2201 files_rules[rulix].frul_srcexpr,
2202 files_rules[rulix].frul_rflags);
2203 if (err)
2205 /* The regular expression compilation fails only when
2206 file_rules is buggy. */
2207 gcc_unreachable ();
2211 output_name = NULL;
2212 for_name = NULL;
2214 /* Match the regexpr and trigger the rule if matched. */
2216 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2217 $3, ... $9. */
2218 regmatch_t pmatch[10];
2219 memset (pmatch, 0, sizeof (pmatch));
2220 if (!regexec (files_rules[rulix].frul_re,
2221 inpfname, 10, pmatch, 0))
2223 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2224 (void*) inpf, inpfname, rulix,
2225 files_rules[rulix].frul_srcexpr);
2226 for_name =
2227 matching_file_name_substitute (inpfname, pmatch,
2228 files_rules[rulix].frul_tr_for);
2229 DBGPRINTF ("for_name %s", for_name);
2230 output_name =
2231 matching_file_name_substitute (inpfname, pmatch,
2232 files_rules[rulix].frul_tr_out);
2233 DBGPRINTF ("output_name %s", output_name);
2234 if (files_rules[rulix].frul_action)
2236 /* Invoke our action routine. */
2237 outf_p of = NULL;
2238 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2239 rulix, output_name, for_name);
2240 of =
2241 (files_rules[rulix].frul_action) (inpf,
2242 &output_name, &for_name);
2243 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2244 rulix, (void*)of, output_name, for_name);
2245 /* If the action routine returned something, give it back
2246 immediately and cache it in inpf. */
2247 if (of)
2249 inpf->inpoutf = of;
2250 return of;
2253 /* The rule matched, and had no action, or that action did
2254 not return any output file but could have changed the
2255 output_name or for_name. We break out of the loop on the
2256 files_rules. */
2257 break;
2259 else
2261 /* The regexpr did not match. */
2262 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2263 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2264 continue;
2269 if (!output_name || !for_name)
2271 /* This should not be possible, and could only happen if the
2272 files_rules is incomplete or buggy. */
2273 fatal ("failed to compute output name for %s", inpfname);
2276 /* Look through to see if we've ever seen this output filename
2277 before. If found, cache the result in inpf. */
2278 for (r = output_files; r; r = r->next)
2279 if (filename_cmp (r->name, output_name) == 0)
2281 inpf->inpoutf = r;
2282 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2283 output_name, for_name);
2284 return r;
2287 /* If not found, create it, and cache it in inpf. */
2288 r = create_file (for_name, output_name);
2290 gcc_assert (r && r->name);
2291 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2292 output_name, for_name);
2293 inpf->inpoutf = r;
2294 return r;
2299 /* The name of an output file, suitable for definitions, that can see
2300 declarations made in INPF and is linked into every language that
2301 uses INPF. */
2303 const char *
2304 get_output_file_name (input_file* inpf)
2306 outf_p o = get_output_file_with_visibility (inpf);
2307 if (o)
2308 return o->name;
2309 return NULL;
2312 /* Check if existing file is equal to the in memory buffer. */
2314 static bool
2315 is_file_equal (outf_p of)
2317 FILE *newfile = fopen (of->name, "r");
2318 size_t i;
2319 bool equal;
2320 if (newfile == NULL)
2321 return false;
2323 equal = true;
2324 for (i = 0; i < of->bufused; i++)
2326 int ch;
2327 ch = fgetc (newfile);
2328 if (ch == EOF || ch != (unsigned char) of->buf[i])
2330 equal = false;
2331 break;
2334 if (equal && EOF != fgetc (newfile))
2335 equal = false;
2336 fclose (newfile);
2337 return equal;
2340 /* Copy the output to its final destination,
2341 but don't unnecessarily change modification times. */
2343 static void
2344 close_output_files (void)
2346 int nbwrittenfiles = 0;
2347 outf_p of;
2349 for (of = output_files; of; of = of->next)
2351 if (!is_file_equal (of))
2353 FILE *newfile = NULL;
2354 char *backupname = NULL;
2355 /* Back up the old version of the output file gt-FOO.c as
2356 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2357 if (backup_dir)
2359 backupname = concat (backup_dir, "/",
2360 lbasename (of->name), "~", NULL);
2361 if (!access (of->name, F_OK) && rename (of->name, backupname))
2362 fatal ("failed to back up %s as %s: %s",
2363 of->name, backupname, xstrerror (errno));
2366 newfile = fopen (of->name, "w");
2367 if (newfile == NULL)
2368 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2369 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2370 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2371 if (fclose (newfile) != 0)
2372 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2373 nbwrittenfiles++;
2374 if (verbosity_level >= 2 && backupname)
2375 printf ("%s wrote #%-3d %s backed-up in %s\n",
2376 progname, nbwrittenfiles, of->name, backupname);
2377 else if (verbosity_level >= 1)
2378 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2379 free (backupname);
2381 else
2383 /* output file remains unchanged. */
2384 if (verbosity_level >= 2)
2385 printf ("%s keep %s\n", progname, of->name);
2387 free (of->buf);
2388 of->buf = NULL;
2389 of->bufused = of->buflength = 0;
2391 if (verbosity_level >= 1)
2392 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2395 struct flist
2397 struct flist *next;
2398 int started_p;
2399 const input_file* file;
2400 outf_p f;
2403 struct walk_type_data;
2405 /* For scalars and strings, given the item in 'val'.
2406 For structures, given a pointer to the item in 'val'.
2407 For misc. pointers, given the item in 'val'.
2409 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2410 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2412 /* Parameters for write_types. */
2414 struct write_types_data
2416 const char *prefix;
2417 const char *param_prefix;
2418 const char *subfield_marker_routine;
2419 const char *marker_routine;
2420 const char *reorder_note_routine;
2421 const char *comment;
2422 int skip_hooks; /* skip hook generation if non zero */
2423 enum write_types_kinds kind;
2426 static void output_escaped_param (struct walk_type_data *d,
2427 const char *, const char *);
2428 static void output_mangled_typename (outf_p, const_type_p);
2429 static void walk_type (type_p t, struct walk_type_data *d);
2430 static void write_func_for_structure (type_p orig_s, type_p s,
2431 const struct write_types_data *wtd);
2432 static void write_types_process_field
2433 (type_p f, const struct walk_type_data *d);
2434 static void write_types (outf_p output_header,
2435 type_p structures,
2436 const struct write_types_data *wtd);
2437 static void write_types_local_process_field
2438 (type_p f, const struct walk_type_data *d);
2439 static void write_local_func_for_structure (const_type_p orig_s, type_p s);
2440 static void write_local (outf_p output_header,
2441 type_p structures);
2442 static int contains_scalar_p (type_p t);
2443 static void put_mangled_filename (outf_p, const input_file *);
2444 static void finish_root_table (struct flist *flp, const char *pfx,
2445 const char *tname, const char *lastname,
2446 const char *name);
2447 static void write_root (outf_p, pair_p, type_p, const char *, int,
2448 struct fileloc *, bool);
2449 static void write_array (outf_p f, pair_p v,
2450 const struct write_types_data *wtd);
2451 static void write_roots (pair_p, bool);
2453 /* Parameters for walk_type. */
2455 struct walk_type_data
2457 process_field_fn process_field;
2458 const void *cookie;
2459 outf_p of;
2460 options_p opt;
2461 const char *val;
2462 const char *prev_val[4];
2463 int indent;
2464 int counter;
2465 const struct fileloc *line;
2466 lang_bitmap bitmap;
2467 int used_length;
2468 type_p orig_s;
2469 const char *reorder_fn;
2470 bool needs_cast_p;
2471 bool fn_wants_lvalue;
2472 bool in_record_p;
2473 int loopcounter;
2474 bool in_ptr_field;
2475 bool have_this_obj;
2479 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2480 pre-processor identifier to use in a #define directive. This replaces
2481 special characters used in C++ identifiers like '>', '<' and ':' with
2482 '_'.
2484 If no C++ special characters are found in TYPE_NAME, return
2485 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2486 characters replaced with '_'. In this case, the caller is
2487 responsible for freeing the allocated string. */
2489 static const char *
2490 filter_type_name (const char *type_name)
2492 if (strchr (type_name, '<') || strchr (type_name, ':'))
2494 size_t i;
2495 char *s = xstrdup (type_name);
2496 for (i = 0; i < strlen (s); i++)
2497 if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ','
2498 || s[i] == '*')
2499 s[i] = '_';
2500 return s;
2502 else
2503 return type_name;
2507 /* Print a mangled name representing T to OF. */
2509 static void
2510 output_mangled_typename (outf_p of, const_type_p t)
2512 if (t == NULL)
2513 oprintf (of, "Z");
2514 else
2515 switch (t->kind)
2517 case TYPE_NONE:
2518 case TYPE_UNDEFINED:
2519 gcc_unreachable ();
2520 break;
2521 case TYPE_POINTER:
2522 oprintf (of, "P");
2523 output_mangled_typename (of, t->u.p);
2524 break;
2525 case TYPE_SCALAR:
2526 oprintf (of, "I");
2527 break;
2528 case TYPE_STRING:
2529 oprintf (of, "S");
2530 break;
2531 case TYPE_STRUCT:
2532 case TYPE_UNION:
2533 case TYPE_LANG_STRUCT:
2534 case TYPE_USER_STRUCT:
2536 /* For references to classes within an inheritance hierarchy,
2537 only ever reference the ultimate base class, since only
2538 it will have gt_ functions. */
2539 t = get_ultimate_base_class (t);
2540 const char *id_for_tag = filter_type_name (t->u.s.tag);
2541 oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2542 id_for_tag);
2543 if (id_for_tag != t->u.s.tag)
2544 free (CONST_CAST (char *, id_for_tag));
2546 break;
2547 case TYPE_ARRAY:
2548 gcc_unreachable ();
2552 /* Print PARAM to D->OF processing escapes. D->VAL references the
2553 current object, D->PREV_VAL the object containing the current
2554 object, ONAME is the name of the option and D->LINE is used to
2555 print error messages. */
2557 static void
2558 output_escaped_param (struct walk_type_data *d, const char *param,
2559 const char *oname)
2561 const char *p;
2563 for (p = param; *p; p++)
2564 if (*p != '%')
2565 oprintf (d->of, "%c", *p);
2566 else
2567 switch (*++p)
2569 case 'h':
2570 oprintf (d->of, "(%s)", d->prev_val[2]);
2571 break;
2572 case '0':
2573 oprintf (d->of, "(%s)", d->prev_val[0]);
2574 break;
2575 case '1':
2576 oprintf (d->of, "(%s)", d->prev_val[1]);
2577 break;
2578 case 'a':
2580 const char *pp = d->val + strlen (d->val);
2581 while (pp[-1] == ']')
2582 while (*pp != '[')
2583 pp--;
2584 oprintf (d->of, "%s", pp);
2586 break;
2587 default:
2588 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2589 oname, '%', *p);
2593 const char *
2594 get_string_option (options_p opt, const char *key)
2596 for (; opt; opt = opt->next)
2597 if (strcmp (opt->name, key) == 0)
2598 return opt->info.string;
2599 return NULL;
2602 /* Machinery for avoiding duplicate tags within switch statements. */
2603 struct seen_tag
2605 const char *tag;
2606 struct seen_tag *next;
2610 already_seen_tag (struct seen_tag *seen_tags, const char *tag)
2612 /* Linear search, so O(n^2), but n is currently small. */
2613 while (seen_tags)
2615 if (!strcmp (seen_tags->tag, tag))
2616 return 1;
2617 seen_tags = seen_tags->next;
2619 /* Not yet seen this tag. */
2620 return 0;
2623 void
2624 mark_tag_as_seen (struct seen_tag **seen_tags, const char *tag)
2626 /* Add to front of linked list. */
2627 struct seen_tag *new_node = XCNEW (struct seen_tag);
2628 new_node->tag = tag;
2629 new_node->next = *seen_tags;
2630 *seen_tags = new_node;
2633 static void
2634 walk_subclasses (type_p base, struct walk_type_data *d,
2635 struct seen_tag **seen_tags)
2637 for (type_p sub = base->u.s.first_subclass; sub != NULL;
2638 sub = sub->u.s.next_sibling_class)
2640 const char *type_tag = get_string_option (sub->u.s.opt, "tag");
2641 if (type_tag && !already_seen_tag (*seen_tags, type_tag))
2643 mark_tag_as_seen (seen_tags, type_tag);
2644 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
2645 d->indent += 2;
2646 oprintf (d->of, "%*s{\n", d->indent, "");
2647 d->indent += 2;
2648 oprintf (d->of, "%*s%s *sub = static_cast <%s *> (x);\n",
2649 d->indent, "", sub->u.s.tag, sub->u.s.tag);
2650 const char *old_val = d->val;
2651 d->val = "(*sub)";
2652 walk_type (sub, d);
2653 d->val = old_val;
2654 d->indent -= 2;
2655 oprintf (d->of, "%*s}\n", d->indent, "");
2656 oprintf (d->of, "%*sbreak;\n", d->indent, "");
2657 d->indent -= 2;
2659 walk_subclasses (sub, d, seen_tags);
2663 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2664 which is of type T. Write code to D->OF to constrain execution (at
2665 the point that D->PROCESS_FIELD is called) to the appropriate
2666 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2667 pointers to those objects. D->PREV_VAL lists the objects
2668 containing the current object, D->OPT is a list of options to
2669 apply, D->INDENT is the current indentation level, D->LINE is used
2670 to print error messages, D->BITMAP indicates which languages to
2671 print the structure for. */
2673 static void
2674 walk_type (type_p t, struct walk_type_data *d)
2676 const char *length = NULL;
2677 const char *desc = NULL;
2678 const char *type_tag = NULL;
2679 int maybe_undef_p = 0;
2680 int atomic_p = 0;
2681 options_p oo;
2682 const struct nested_ptr_data *nested_ptr_d = NULL;
2684 d->needs_cast_p = false;
2685 for (oo = d->opt; oo; oo = oo->next)
2686 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2687 length = oo->info.string;
2688 else if (strcmp (oo->name, "maybe_undef") == 0)
2689 maybe_undef_p = 1;
2690 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2691 desc = oo->info.string;
2692 else if (strcmp (oo->name, "mark_hook") == 0)
2694 else if (strcmp (oo->name, "nested_ptr") == 0
2695 && oo->kind == OPTION_NESTED)
2696 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2697 else if (strcmp (oo->name, "dot") == 0)
2699 else if (strcmp (oo->name, "tag") == 0)
2700 type_tag = oo->info.string;
2701 else if (strcmp (oo->name, "special") == 0)
2703 else if (strcmp (oo->name, "skip") == 0)
2705 else if (strcmp (oo->name, "atomic") == 0)
2706 atomic_p = 1;
2707 else if (strcmp (oo->name, "default") == 0)
2709 else if (strcmp (oo->name, "chain_next") == 0)
2711 else if (strcmp (oo->name, "chain_prev") == 0)
2713 else if (strcmp (oo->name, "chain_circular") == 0)
2715 else if (strcmp (oo->name, "reorder") == 0)
2717 else if (strcmp (oo->name, "variable_size") == 0)
2719 else if (strcmp (oo->name, "for_user") == 0)
2721 else
2722 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2724 if (d->used_length)
2725 length = NULL;
2727 if (maybe_undef_p
2728 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
2730 error_at_line (d->line,
2731 "field `%s' has invalid option `maybe_undef_p'\n",
2732 d->val);
2733 return;
2736 if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2738 error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2739 return;
2742 switch (t->kind)
2744 case TYPE_SCALAR:
2745 case TYPE_STRING:
2746 d->process_field (t, d);
2747 break;
2749 case TYPE_POINTER:
2751 d->in_ptr_field = true;
2752 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2754 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2755 break;
2758 /* If a pointer type is marked as "atomic", we process the
2759 field itself, but we don't walk the data that they point to.
2761 There are two main cases where we walk types: to mark
2762 pointers that are reachable, and to relocate pointers when
2763 writing a PCH file. In both cases, an atomic pointer is
2764 itself marked or relocated, but the memory that it points
2765 to is left untouched. In the case of PCH, that memory will
2766 be read/written unchanged to the PCH file. */
2767 if (atomic_p)
2769 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2770 d->indent += 2;
2771 d->process_field (t, d);
2772 d->indent -= 2;
2773 oprintf (d->of, "%*s}\n", d->indent, "");
2774 break;
2777 if (!length)
2779 if (!union_or_struct_p (t->u.p))
2781 error_at_line (d->line,
2782 "field `%s' is pointer to unimplemented type",
2783 d->val);
2784 break;
2787 if (nested_ptr_d)
2789 const char *oldprevval2 = d->prev_val[2];
2791 if (!union_or_struct_p (nested_ptr_d->type))
2793 error_at_line (d->line,
2794 "field `%s' has invalid "
2795 "option `nested_ptr'\n", d->val);
2796 return;
2799 d->prev_val[2] = d->val;
2800 oprintf (d->of, "%*s{\n", d->indent, "");
2801 d->indent += 2;
2802 d->val = xasprintf ("x%d", d->counter++);
2803 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2804 (nested_ptr_d->type->kind == TYPE_UNION
2805 ? "union" : "struct"),
2806 nested_ptr_d->type->u.s.tag,
2807 d->fn_wants_lvalue ? "" : "const ", d->val);
2808 oprintf (d->of, "%*s", d->indent + 2, "");
2809 output_escaped_param (d, nested_ptr_d->convert_from,
2810 "nested_ptr");
2811 oprintf (d->of, ";\n");
2813 d->process_field (nested_ptr_d->type, d);
2815 if (d->fn_wants_lvalue)
2817 oprintf (d->of, "%*s%s = ", d->indent, "",
2818 d->prev_val[2]);
2819 d->prev_val[2] = d->val;
2820 output_escaped_param (d, nested_ptr_d->convert_to,
2821 "nested_ptr");
2822 oprintf (d->of, ";\n");
2825 d->indent -= 2;
2826 oprintf (d->of, "%*s}\n", d->indent, "");
2827 d->val = d->prev_val[2];
2828 d->prev_val[2] = oldprevval2;
2830 else
2831 d->process_field (t->u.p, d);
2833 else
2835 int loopcounter = d->loopcounter;
2836 const char *oldval = d->val;
2837 const char *oldprevval3 = d->prev_val[3];
2838 char *newval;
2840 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2841 d->indent += 2;
2842 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2843 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2844 "", loopcounter, loopcounter);
2845 if (!d->in_record_p)
2846 output_escaped_param (d, length, "length");
2847 else
2848 oprintf (d->of, "l%d", loopcounter);
2849 if (d->have_this_obj)
2850 /* Try to unswitch loops (see PR53880). */
2851 oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
2852 oprintf (d->of, "); i%d++) {\n", loopcounter);
2853 d->indent += 2;
2854 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2855 d->used_length = 1;
2856 d->prev_val[3] = oldval;
2857 walk_type (t->u.p, d);
2858 free (newval);
2859 d->val = oldval;
2860 d->prev_val[3] = oldprevval3;
2861 d->used_length = 0;
2862 d->indent -= 2;
2863 oprintf (d->of, "%*s}\n", d->indent, "");
2864 d->process_field (t, d);
2865 d->indent -= 2;
2866 oprintf (d->of, "%*s}\n", d->indent, "");
2868 d->in_ptr_field = false;
2870 break;
2872 case TYPE_ARRAY:
2874 int loopcounter;
2875 const char *oldval = d->val;
2876 char *newval;
2878 /* If it's an array of scalars, we optimize by not generating
2879 any code. */
2880 if (t->u.a.p->kind == TYPE_SCALAR)
2881 break;
2883 if (length)
2884 loopcounter = d->loopcounter;
2885 else
2886 loopcounter = d->counter++;
2888 /* When walking an array, compute the length and store it in a
2889 local variable before walking the array elements, instead of
2890 recomputing the length expression each time through the loop.
2891 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2892 where the length is stored in the first array element,
2893 because otherwise that operand can get overwritten on the
2894 first iteration. */
2895 oprintf (d->of, "%*s{\n", d->indent, "");
2896 d->indent += 2;
2897 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2898 if (!d->in_record_p || !length)
2900 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2901 d->indent, "", loopcounter);
2902 if (length)
2903 output_escaped_param (d, length, "length");
2904 else
2905 oprintf (d->of, "%s", t->u.a.len);
2906 oprintf (d->of, ");\n");
2909 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2910 d->indent, "",
2911 loopcounter, loopcounter, loopcounter, loopcounter);
2912 d->indent += 2;
2913 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2914 d->used_length = 1;
2915 walk_type (t->u.a.p, d);
2916 free (newval);
2917 d->used_length = 0;
2918 d->val = oldval;
2919 d->indent -= 2;
2920 oprintf (d->of, "%*s}\n", d->indent, "");
2921 d->indent -= 2;
2922 oprintf (d->of, "%*s}\n", d->indent, "");
2924 break;
2926 case TYPE_STRUCT:
2927 case TYPE_UNION:
2929 pair_p f;
2930 const char *oldval = d->val;
2931 const char *oldprevval1 = d->prev_val[1];
2932 const char *oldprevval2 = d->prev_val[2];
2933 const char *struct_mark_hook = NULL;
2934 const int union_p = t->kind == TYPE_UNION;
2935 int seen_default_p = 0;
2936 options_p o;
2937 int lengths_seen = 0;
2938 int endcounter;
2939 bool any_length_seen = false;
2941 if (!t->u.s.line.file)
2942 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
2944 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2946 error_at_line (d->line,
2947 "structure `%s' defined for mismatching languages",
2948 t->u.s.tag);
2949 error_at_line (&t->u.s.line, "one structure defined here");
2952 /* Some things may also be defined in the structure's options. */
2953 for (o = t->u.s.opt; o; o = o->next)
2954 if (!desc && strcmp (o->name, "desc") == 0
2955 && o->kind == OPTION_STRING)
2956 desc = o->info.string;
2957 else if (!struct_mark_hook && strcmp (o->name, "mark_hook") == 0
2958 && o->kind == OPTION_STRING)
2959 struct_mark_hook = o->info.string;
2961 if (struct_mark_hook)
2962 oprintf (d->of, "%*s%s (&%s);\n",
2963 d->indent, "", struct_mark_hook, oldval);
2965 d->prev_val[2] = oldval;
2966 d->prev_val[1] = oldprevval2;
2967 if (union_p)
2969 if (desc == NULL)
2971 error_at_line (d->line,
2972 "missing `desc' option for union `%s'",
2973 t->u.s.tag);
2974 desc = "1";
2976 oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
2977 output_escaped_param (d, desc, "desc");
2978 oprintf (d->of, "))\n");
2979 d->indent += 2;
2980 oprintf (d->of, "%*s{\n", d->indent, "");
2982 else if (desc)
2984 /* We have a "desc" option on a struct, signifying the
2985 base class within a GC-managed inheritance hierarchy.
2986 The current code specialcases the base class, then walks
2987 into subclasses, recursing into this routine to handle them.
2988 This organization requires the base class to have a case in
2989 the switch statement, and hence a tag value is mandatory
2990 for the base class. This restriction could be removed, but
2991 it would require some restructing of this code. */
2992 if (!type_tag)
2994 error_at_line (d->line,
2995 "missing `tag' option for type `%s'",
2996 t->u.s.tag);
2998 oprintf (d->of, "%*sswitch ((int) (", d->indent, "");
2999 output_escaped_param (d, desc, "desc");
3000 oprintf (d->of, "))\n");
3001 d->indent += 2;
3002 oprintf (d->of, "%*s{\n", d->indent, "");
3003 oprintf (d->of, "%*scase %s:\n", d->indent, "", type_tag);
3004 d->indent += 2;
3007 FOR_ALL_INHERITED_FIELDS (t, f)
3009 options_p oo;
3010 int skip_p = 0;
3011 const char *fieldlength = NULL;
3013 d->reorder_fn = NULL;
3014 for (oo = f->opt; oo; oo = oo->next)
3015 if (strcmp (oo->name, "skip") == 0)
3016 skip_p = 1;
3017 else if (strcmp (oo->name, "length") == 0
3018 && oo->kind == OPTION_STRING)
3019 fieldlength = oo->info.string;
3021 if (skip_p)
3022 continue;
3023 if (fieldlength)
3025 lengths_seen++;
3026 d->counter++;
3027 if (!union_p)
3029 if (!any_length_seen)
3031 oprintf (d->of, "%*s{\n", d->indent, "");
3032 d->indent += 2;
3034 any_length_seen = true;
3036 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
3037 d->indent, "", d->counter - 1);
3038 output_escaped_param (d, fieldlength, "length");
3039 oprintf (d->of, ");\n");
3043 endcounter = d->counter;
3045 FOR_ALL_INHERITED_FIELDS (t, f)
3047 options_p oo;
3048 const char *dot = ".";
3049 const char *tagid = NULL;
3050 int skip_p = 0;
3051 int default_p = 0;
3052 const char *fieldlength = NULL;
3053 char *newval;
3055 d->reorder_fn = NULL;
3056 for (oo = f->opt; oo; oo = oo->next)
3057 if (strcmp (oo->name, "dot") == 0
3058 && oo->kind == OPTION_STRING)
3059 dot = oo->info.string;
3060 else if (strcmp (oo->name, "tag") == 0
3061 && oo->kind == OPTION_STRING)
3062 tagid = oo->info.string;
3063 else if (strcmp (oo->name, "skip") == 0)
3064 skip_p = 1;
3065 else if (strcmp (oo->name, "default") == 0)
3066 default_p = 1;
3067 else if (strcmp (oo->name, "reorder") == 0
3068 && oo->kind == OPTION_STRING)
3069 d->reorder_fn = oo->info.string;
3070 else if (strcmp (oo->name, "length") == 0
3071 && oo->kind == OPTION_STRING)
3072 fieldlength = oo->info.string;
3074 if (skip_p)
3075 continue;
3077 if (union_p && tagid)
3079 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
3080 d->indent += 2;
3082 else if (union_p && default_p)
3084 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3085 d->indent += 2;
3086 seen_default_p = 1;
3088 else if (!union_p && (default_p || tagid))
3089 error_at_line (d->line,
3090 "can't use `%s' outside a union on field `%s'",
3091 default_p ? "default" : "tag", f->name);
3092 else if (union_p && !(default_p || tagid)
3093 && f->type->kind == TYPE_SCALAR)
3095 fprintf (stderr,
3096 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3097 get_input_file_name (d->line->file), d->line->line,
3098 f->name);
3099 continue;
3101 else if (union_p && !(default_p || tagid))
3102 error_at_line (d->line,
3103 "field `%s' is missing `tag' or `default' option",
3104 f->name);
3106 if (fieldlength)
3108 d->loopcounter = endcounter - lengths_seen--;
3111 d->line = &f->line;
3112 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
3113 d->opt = f->opt;
3114 d->used_length = false;
3115 d->in_record_p = !union_p;
3117 walk_type (f->type, d);
3119 d->in_record_p = false;
3121 free (newval);
3123 if (union_p)
3125 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3126 d->indent -= 2;
3129 d->reorder_fn = NULL;
3131 d->val = oldval;
3132 d->prev_val[1] = oldprevval1;
3133 d->prev_val[2] = oldprevval2;
3135 if (union_p && !seen_default_p)
3137 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3138 oprintf (d->of, "%*s break;\n", d->indent, "");
3141 if (desc && !union_p)
3143 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3144 d->indent -= 2;
3146 if (union_p)
3148 oprintf (d->of, "%*s}\n", d->indent, "");
3149 d->indent -= 2;
3151 else if (desc)
3153 /* Add cases to handle subclasses. */
3154 struct seen_tag *tags = NULL;
3155 walk_subclasses (t, d, &tags);
3157 /* Ensure that if someone forgets a "tag" option that we don't
3158 silent fail to traverse that subclass's fields. */
3159 if (!seen_default_p)
3161 oprintf (d->of, "%*s/* Unrecognized tag value. */\n",
3162 d->indent, "");
3163 oprintf (d->of, "%*sdefault: gcc_unreachable (); \n",
3164 d->indent, "");
3167 /* End of the switch statement */
3168 oprintf (d->of, "%*s}\n", d->indent, "");
3169 d->indent -= 2;
3171 if (any_length_seen)
3173 d->indent -= 2;
3174 oprintf (d->of, "%*s}\n", d->indent, "");
3177 break;
3179 case TYPE_LANG_STRUCT:
3181 type_p nt;
3182 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3183 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3184 break;
3185 if (nt == NULL)
3186 error_at_line (d->line, "structure `%s' differs between languages",
3187 t->u.s.tag);
3188 else
3189 walk_type (nt, d);
3191 break;
3193 case TYPE_USER_STRUCT:
3194 d->process_field (t, d);
3195 break;
3197 case TYPE_NONE:
3198 case TYPE_UNDEFINED:
3199 gcc_unreachable ();
3203 /* process_field routine for marking routines. */
3205 static void
3206 write_types_process_field (type_p f, const struct walk_type_data *d)
3208 const struct write_types_data *wtd;
3209 const char *cast = d->needs_cast_p ? "(void *)" : "";
3210 wtd = (const struct write_types_data *) d->cookie;
3212 switch (f->kind)
3214 case TYPE_NONE:
3215 case TYPE_UNDEFINED:
3216 gcc_unreachable ();
3217 case TYPE_POINTER:
3218 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
3219 wtd->subfield_marker_routine, cast, d->val);
3220 if (wtd->param_prefix)
3222 if (f->u.p->kind == TYPE_SCALAR)
3223 /* The current type is a pointer to a scalar (so not
3224 considered like a pointer to instances of user defined
3225 types) and we are seeing it; it means we must be even
3226 more careful about the second argument of the
3227 SUBFIELD_MARKER_ROUTINE call. That argument must
3228 always be the instance of the type for which
3229 write_func_for_structure was called - this really is
3230 what the function SUBFIELD_MARKER_ROUTINE expects.
3231 That is, it must be an instance of the ORIG_S type
3232 parameter of write_func_for_structure. The convention
3233 is that that argument must be "x" in that case (as set
3234 by write_func_for_structure). The problem is, we can't
3235 count on d->prev_val[3] to be always set to "x" in that
3236 case. Sometimes walk_type can set it to something else
3237 (to e.g cooperate with write_array when called from
3238 write_roots). So let's set it to "x" here then. */
3239 oprintf (d->of, ", x");
3240 else
3241 oprintf (d->of, ", %s", d->prev_val[3]);
3242 if (d->orig_s)
3244 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3245 output_mangled_typename (d->of, d->orig_s);
3247 else
3248 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3250 oprintf (d->of, ");\n");
3251 if (d->reorder_fn && wtd->reorder_note_routine)
3252 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
3253 wtd->reorder_note_routine, cast, d->val,
3254 d->prev_val[3], d->reorder_fn);
3255 break;
3257 case TYPE_STRING:
3258 case TYPE_STRUCT:
3259 case TYPE_UNION:
3260 case TYPE_LANG_STRUCT:
3261 case TYPE_USER_STRUCT:
3262 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3264 /* If F is a user-defined type and the field is not a
3265 pointer to the type, then we should not generate the
3266 standard pointer-marking code. All we need to do is call
3267 the user-provided marking function to process the fields
3268 of F. */
3269 oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3270 d->val);
3272 else
3274 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3275 output_mangled_typename (d->of, f);
3276 oprintf (d->of, " (%s%s);\n", cast, d->val);
3277 if (d->reorder_fn && wtd->reorder_note_routine)
3278 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
3279 wtd->reorder_note_routine, cast, d->val, cast, d->val,
3280 d->reorder_fn);
3282 break;
3284 case TYPE_SCALAR:
3285 break;
3287 case TYPE_ARRAY:
3288 gcc_unreachable ();
3292 /* Return an output file that is suitable for definitions which can
3293 reference struct S */
3295 static outf_p
3296 get_output_file_for_structure (const_type_p s)
3298 const input_file *fn;
3300 gcc_assert (union_or_struct_p (s));
3301 fn = s->u.s.line.file;
3303 /* The call to get_output_file_with_visibility may update fn by
3304 caching its result inside, so we need the CONST_CAST. */
3305 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3309 /* Returns the specifier keyword for a string or union type S, empty string
3310 otherwise. */
3312 static const char *
3313 get_type_specifier (const type_p s)
3315 if (s->kind == TYPE_STRUCT)
3316 return "struct ";
3317 else if (s->kind == TYPE_LANG_STRUCT)
3318 return get_type_specifier (s->u.s.lang_struct);
3319 else if (s->kind == TYPE_UNION)
3320 return "union ";
3321 return "";
3325 /* Emits a declaration for type TY (assumed to be a union or a
3326 structure) on stream OUT. */
3328 static void
3329 write_type_decl (outf_p out, type_p ty)
3331 if (union_or_struct_p (ty))
3332 oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3333 else if (ty->kind == TYPE_SCALAR)
3335 if (ty->u.scalar_is_char)
3336 oprintf (out, "const char");
3337 else
3338 oprintf (out, "void");
3340 else if (ty->kind == TYPE_POINTER)
3342 write_type_decl (out, ty->u.p);
3343 oprintf (out, " *");
3345 else if (ty->kind == TYPE_ARRAY)
3347 write_type_decl (out, ty->u.a.p);
3348 oprintf (out, " *");
3350 else if (ty->kind == TYPE_STRING)
3352 oprintf (out, "const char *");
3354 else
3355 gcc_unreachable ();
3359 /* Write on OF the name of the marker function for structure S. PREFIX
3360 is the prefix to use (to distinguish ggc from pch markers). */
3362 static void
3363 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3365 if (union_or_struct_p (s))
3367 const char *id_for_tag = filter_type_name (s->u.s.tag);
3368 oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3369 if (id_for_tag != s->u.s.tag)
3370 free (CONST_CAST (char *, id_for_tag));
3372 else
3373 gcc_unreachable ();
3376 /* Write on OF a user-callable routine to act as an entry point for
3377 the marking routine for S, generated by write_func_for_structure.
3378 WTD distinguishes between ggc and pch markers. */
3380 static void
3381 write_user_func_for_structure_ptr (outf_p of, type_p s, const write_types_data *wtd)
3383 gcc_assert (union_or_struct_p (s));
3385 type_p alias_of = NULL;
3386 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3387 if (strcmp (opt->name, "ptr_alias") == 0)
3389 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3390 we do not generate marking code for ORIG_S here. Instead, a
3391 forwarder #define in gtype-desc.h will cause every call to its
3392 marker to call the target of this alias.
3394 However, we still want to create a user entry code for the
3395 aliased type. So, if ALIAS_OF is set, we only generate the
3396 user-callable marker function. */
3397 alias_of = opt->info.type;
3398 break;
3401 DBGPRINTF ("write_user_func_for_structure_ptr: %s %s", s->u.s.tag,
3402 wtd->prefix);
3404 /* Only write the function once. */
3405 if (s->u.s.wrote_user_func_for_ptr[wtd->kind])
3406 return;
3407 s->u.s.wrote_user_func_for_ptr[wtd->kind] = true;
3409 oprintf (of, "\nvoid\n");
3410 oprintf (of, "gt_%sx (", wtd->prefix);
3411 write_type_decl (of, s);
3412 oprintf (of, " *& x)\n");
3413 oprintf (of, "{\n");
3414 oprintf (of, " if (x)\n ");
3415 write_marker_function_name (of,
3416 alias_of ? alias_of : get_ultimate_base_class (s),
3417 wtd->prefix);
3418 oprintf (of, " ((void *) x);\n");
3419 oprintf (of, "}\n");
3423 /* Write a function to mark all the fields of type S on OF. PREFIX
3424 and D are as in write_user_marking_functions. */
3426 static void
3427 write_user_func_for_structure_body (type_p s, const char *prefix,
3428 struct walk_type_data *d)
3430 oprintf (d->of, "\nvoid\n");
3431 oprintf (d->of, "gt_%sx (", prefix);
3432 write_type_decl (d->of, s);
3433 oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3434 oprintf (d->of, "{\n");
3435 oprintf (d->of, " ");
3436 write_type_decl (d->of, s);
3437 oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3438 d->val = "(*x)";
3439 d->indent = 2;
3440 walk_type (s, d);
3441 oprintf (d->of, "}\n");
3444 /* Emit the user-callable functions needed to mark all the types used
3445 by the user structure S. PREFIX is the prefix to use to
3446 distinguish ggc and pch markers. D contains data needed to pass to
3447 walk_type when traversing the fields of a type.
3449 For every type T referenced by S, two routines are generated: one
3450 that takes 'T *', marks the pointer and calls the second routine,
3451 which just marks the fields of T. */
3453 static void
3454 write_user_marking_functions (type_p s,
3455 const write_types_data *w,
3456 struct walk_type_data *d)
3458 gcc_assert (s->kind == TYPE_USER_STRUCT);
3460 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3462 type_p fld_type = fld->type;
3463 if (fld_type->kind == TYPE_POINTER)
3465 type_p pointed_to_type = fld_type->u.p;
3466 if (union_or_struct_p (pointed_to_type))
3467 write_user_func_for_structure_ptr (d->of, pointed_to_type, w);
3469 else if (union_or_struct_p (fld_type))
3470 write_user_func_for_structure_body (fld_type, w->prefix, d);
3475 /* For S, a structure that's part of ORIG_S write out a routine that:
3476 - Takes a parameter, a void * but actually of type *S
3477 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3478 field of S or its substructures and (in some cases) things
3479 that are pointed to by S. */
3481 static void
3482 write_func_for_structure (type_p orig_s, type_p s,
3483 const struct write_types_data *wtd)
3485 const char *chain_next = NULL;
3486 const char *chain_prev = NULL;
3487 const char *chain_circular = NULL;
3488 const char *mark_hook_name = NULL;
3489 options_p opt;
3490 struct walk_type_data d;
3492 if (s->u.s.base_class)
3494 /* Verify that the base class has a "desc", since otherwise
3495 the traversal hooks there won't attempt to visit fields of
3496 subclasses such as this one. */
3497 const_type_p ubc = get_ultimate_base_class (s);
3498 if ((!opts_have (ubc->u.s.opt, "user")
3499 && !opts_have (ubc->u.s.opt, "desc")))
3500 error_at_line (&s->u.s.line,
3501 ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
3502 ", but '%s' lacks a discriminator 'desc' option"),
3503 s->u.s.tag, ubc->u.s.tag, ubc->u.s.tag);
3505 /* Don't write fns for subclasses, only for the ultimate base class
3506 within an inheritance hierarchy. */
3507 return;
3510 memset (&d, 0, sizeof (d));
3511 d.of = get_output_file_for_structure (s);
3513 bool for_user = false;
3514 for (opt = s->u.s.opt; opt; opt = opt->next)
3515 if (strcmp (opt->name, "chain_next") == 0
3516 && opt->kind == OPTION_STRING)
3517 chain_next = opt->info.string;
3518 else if (strcmp (opt->name, "chain_prev") == 0
3519 && opt->kind == OPTION_STRING)
3520 chain_prev = opt->info.string;
3521 else if (strcmp (opt->name, "chain_circular") == 0
3522 && opt->kind == OPTION_STRING)
3523 chain_circular = opt->info.string;
3524 else if (strcmp (opt->name, "mark_hook") == 0
3525 && opt->kind == OPTION_STRING)
3526 mark_hook_name = opt->info.string;
3527 else if (strcmp (opt->name, "for_user") == 0)
3528 for_user = true;
3529 if (chain_prev != NULL && chain_next == NULL)
3530 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3531 if (chain_circular != NULL && chain_next != NULL)
3532 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3533 if (chain_circular != NULL)
3534 chain_next = chain_circular;
3536 d.process_field = write_types_process_field;
3537 d.cookie = wtd;
3538 d.orig_s = orig_s;
3539 d.opt = s->u.s.opt;
3540 d.line = &s->u.s.line;
3541 d.bitmap = s->u.s.bitmap;
3542 d.prev_val[0] = "*x";
3543 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3544 d.prev_val[3] = "x";
3545 d.val = "(*x)";
3546 d.have_this_obj = false;
3548 oprintf (d.of, "\n");
3549 oprintf (d.of, "void\n");
3550 write_marker_function_name (d.of, orig_s, wtd->prefix);
3551 oprintf (d.of, " (void *x_p)\n");
3552 oprintf (d.of, "{\n ");
3553 write_type_decl (d.of, s);
3554 oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3555 write_type_decl (d.of, s);
3556 oprintf (d.of, " *)x_p;\n");
3557 if (chain_next != NULL)
3559 /* TYPE_USER_STRUCTs should not occur here. These structures
3560 are completely handled by user code. */
3561 gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
3563 oprintf (d.of, " ");
3564 write_type_decl (d.of, s);
3565 oprintf (d.of, " * xlimit = x;\n");
3567 if (chain_next == NULL)
3569 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3570 if (wtd->param_prefix)
3572 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3573 output_mangled_typename (d.of, orig_s);
3575 oprintf (d.of, "))\n");
3577 else
3579 if (chain_circular != NULL)
3580 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3581 else
3582 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3583 if (wtd->param_prefix)
3585 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3586 output_mangled_typename (d.of, orig_s);
3588 oprintf (d.of, "))\n");
3589 if (chain_circular != NULL)
3590 oprintf (d.of, " return;\n do\n");
3591 if (mark_hook_name && !wtd->skip_hooks)
3593 oprintf (d.of, " {\n");
3594 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
3596 oprintf (d.of, " xlimit = (");
3597 d.prev_val[2] = "*xlimit";
3598 output_escaped_param (&d, chain_next, "chain_next");
3599 oprintf (d.of, ");\n");
3600 if (mark_hook_name && !wtd->skip_hooks)
3601 oprintf (d.of, " }\n");
3602 if (chain_prev != NULL)
3604 oprintf (d.of, " if (x != xlimit)\n");
3605 oprintf (d.of, " for (;;)\n");
3606 oprintf (d.of, " {\n");
3607 oprintf (d.of, " %s %s * const xprev = (",
3608 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3610 d.prev_val[2] = "*x";
3611 output_escaped_param (&d, chain_prev, "chain_prev");
3612 oprintf (d.of, ");\n");
3613 oprintf (d.of, " if (xprev == NULL) break;\n");
3614 oprintf (d.of, " x = xprev;\n");
3615 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3616 if (wtd->param_prefix)
3618 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3619 output_mangled_typename (d.of, orig_s);
3621 oprintf (d.of, ");\n");
3622 oprintf (d.of, " }\n");
3624 if (chain_circular != NULL)
3626 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3627 if (wtd->param_prefix)
3629 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3630 output_mangled_typename (d.of, orig_s);
3632 oprintf (d.of, "));\n");
3633 if (mark_hook_name && !wtd->skip_hooks)
3634 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
3635 oprintf (d.of, " do\n");
3637 else
3638 oprintf (d.of, " while (x != xlimit)\n");
3640 oprintf (d.of, " {\n");
3641 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3643 oprintf (d.of, " %s (x);\n", mark_hook_name);
3646 d.prev_val[2] = "*x";
3647 d.indent = 6;
3648 if (orig_s->kind != TYPE_USER_STRUCT)
3649 walk_type (s, &d);
3650 else
3652 /* User structures have no fields to walk. Simply generate a call
3653 to the user-provided structure marker. */
3654 oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3657 if (chain_next != NULL)
3659 oprintf (d.of, " x = (");
3660 output_escaped_param (&d, chain_next, "chain_next");
3661 oprintf (d.of, ");\n");
3664 oprintf (d.of, " }\n");
3665 if (chain_circular != NULL)
3666 oprintf (d.of, " while (x != xlimit);\n");
3667 oprintf (d.of, "}\n");
3669 if (orig_s->kind == TYPE_USER_STRUCT)
3670 write_user_marking_functions (orig_s, wtd, &d);
3672 if (for_user)
3674 write_user_func_for_structure_body (orig_s, wtd->prefix, &d);
3675 write_user_func_for_structure_ptr (d.of, orig_s, wtd);
3680 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3682 static void
3683 write_types (outf_p output_header, type_p structures,
3684 const struct write_types_data *wtd)
3686 int nbfun = 0; /* Count the emitted functions. */
3687 type_p s;
3689 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3691 /* We first emit the macros and the declarations. Functions' code is
3692 emitted afterwards. This is needed in plugin mode. */
3693 oprintf (output_header, "/* Macros and declarations. */\n");
3694 for (s = structures; s; s = s->next)
3695 /* Do not emit handlers for derived classes; we only ever deal with
3696 the ultimate base class within an inheritance hierarchy. */
3697 if ((s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3698 && !s->u.s.base_class)
3700 options_p opt;
3702 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3703 continue;
3705 const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3707 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3708 output_mangled_typename (output_header, s);
3709 oprintf (output_header, "(X) do { \\\n");
3710 oprintf (output_header,
3711 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3712 s_id_for_tag);
3713 oprintf (output_header, " } while (0)\n");
3715 for (opt = s->u.s.opt; opt; opt = opt->next)
3716 if (strcmp (opt->name, "ptr_alias") == 0
3717 && opt->kind == OPTION_TYPE)
3719 const_type_p const t = (const_type_p) opt->info.type;
3720 if (t->kind == TYPE_STRUCT
3721 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3723 const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3724 oprintf (output_header,
3725 "#define gt_%sx_%s gt_%sx_%s\n",
3726 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3727 if (t_id_for_tag != t->u.s.tag)
3728 free (CONST_CAST (char *, t_id_for_tag));
3730 else
3731 error_at_line (&s->u.s.line,
3732 "structure alias is not a structure");
3733 break;
3735 if (opt)
3736 continue;
3738 /* Declare the marker procedure only once. */
3739 oprintf (output_header,
3740 "extern void gt_%sx_%s (void *);\n",
3741 wtd->prefix, s_id_for_tag);
3743 if (s_id_for_tag != s->u.s.tag)
3744 free (CONST_CAST (char *, s_id_for_tag));
3746 if (s->u.s.line.file == NULL)
3748 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3749 s->u.s.tag);
3750 continue;
3754 /* At last we emit the functions code. */
3755 oprintf (output_header, "\n/* functions code */\n");
3756 for (s = structures; s; s = s->next)
3757 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3759 options_p opt;
3761 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3762 continue;
3763 for (opt = s->u.s.opt; opt; opt = opt->next)
3764 if (strcmp (opt->name, "ptr_alias") == 0)
3765 break;
3766 if (opt)
3767 continue;
3769 if (s->kind == TYPE_LANG_STRUCT)
3771 type_p ss;
3772 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3774 nbfun++;
3775 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3776 nbfun, (void*) ss, ss->u.s.tag);
3777 write_func_for_structure (s, ss, wtd);
3780 else
3782 nbfun++;
3783 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3784 nbfun, (void*) s, s->u.s.tag);
3785 write_func_for_structure (s, s, wtd);
3788 else
3790 /* Structure s is not possibly pointed to, so can be ignored. */
3791 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3792 (void*)s, s->u.s.tag,
3793 (int) s->gc_used);
3796 if (verbosity_level >= 2)
3797 printf ("%s emitted %d routines for %s\n",
3798 progname, nbfun, wtd->comment);
3801 static const struct write_types_data ggc_wtd = {
3802 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
3803 "GC marker procedures. ",
3804 FALSE, WTK_GGC
3807 static const struct write_types_data pch_wtd = {
3808 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3809 "gt_pch_note_reorder",
3810 "PCH type-walking procedures. ",
3811 TRUE, WTK_PCH
3814 /* Write out the local pointer-walking routines. */
3816 /* process_field routine for local pointer-walking for user-callable
3817 routines. The difference between this and
3818 write_types_local_process_field is that, in this case, we do not
3819 need to check whether the given pointer matches the address of the
3820 parent structure. This check was already generated by the call
3821 to gt_pch_nx in the main gt_pch_p_*() function that is calling
3822 this code. */
3824 static void
3825 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
3827 switch (f->kind)
3829 case TYPE_POINTER:
3830 case TYPE_STRUCT:
3831 case TYPE_UNION:
3832 case TYPE_LANG_STRUCT:
3833 case TYPE_STRING:
3834 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3835 break;
3837 case TYPE_USER_STRUCT:
3838 if (d->in_ptr_field)
3839 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3840 else
3841 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3842 d->indent, "", d->val);
3843 break;
3845 case TYPE_SCALAR:
3846 break;
3848 case TYPE_ARRAY:
3849 case TYPE_NONE:
3850 case TYPE_UNDEFINED:
3851 gcc_unreachable ();
3856 /* Write a function to PCH walk all the fields of type S on OF.
3857 D contains data needed by walk_type to recurse into the fields of S. */
3859 static void
3860 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
3862 oprintf (d->of, "\nvoid\n");
3863 oprintf (d->of, "gt_pch_nx (");
3864 write_type_decl (d->of, s);
3865 oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
3866 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3867 "\tATTRIBUTE_UNUSED void *cookie)\n");
3868 oprintf (d->of, "{\n");
3869 d->val = "(*x)";
3870 d->indent = 2;
3871 d->process_field = write_types_local_user_process_field;
3872 walk_type (s, d);
3873 oprintf (d->of, "}\n");
3877 /* Emit the user-callable functions needed to mark all the types used
3878 by the user structure S. PREFIX is the prefix to use to
3879 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
3880 chain_next option defined. D contains data needed to pass to
3881 walk_type when traversing the fields of a type.
3883 For every type T referenced by S, two routines are generated: one
3884 that takes 'T *', marks the pointer and calls the second routine,
3885 which just marks the fields of T. */
3887 static void
3888 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
3890 gcc_assert (s->kind == TYPE_USER_STRUCT);
3892 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3894 type_p fld_type = fld->type;
3895 if (union_or_struct_p (fld_type))
3896 write_pch_user_walking_for_structure_body (fld_type, d);
3901 /* process_field routine for local pointer-walking. */
3903 static void
3904 write_types_local_process_field (type_p f, const struct walk_type_data *d)
3906 gcc_assert (d->have_this_obj);
3907 switch (f->kind)
3909 case TYPE_POINTER:
3910 case TYPE_STRUCT:
3911 case TYPE_UNION:
3912 case TYPE_LANG_STRUCT:
3913 case TYPE_STRING:
3914 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3915 d->prev_val[3]);
3916 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3917 break;
3919 case TYPE_USER_STRUCT:
3920 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3921 d->prev_val[3]);
3922 if (d->in_ptr_field)
3923 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3924 else
3925 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3926 d->indent, "", d->val);
3927 break;
3929 case TYPE_SCALAR:
3930 break;
3932 case TYPE_ARRAY:
3933 case TYPE_NONE:
3934 case TYPE_UNDEFINED:
3935 gcc_unreachable ();
3940 /* For S, a structure that's part of ORIG_S, and using parameters
3941 PARAM, write out a routine that:
3942 - Is of type gt_note_pointers
3943 - Calls PROCESS_FIELD on each field of S or its substructures.
3946 static void
3947 write_local_func_for_structure (const_type_p orig_s, type_p s)
3949 struct walk_type_data d;
3951 /* Don't write fns for subclasses, only for the ultimate base class
3952 within an inheritance hierarchy. */
3953 if (s->u.s.base_class)
3954 return;
3956 memset (&d, 0, sizeof (d));
3957 d.of = get_output_file_for_structure (s);
3958 d.process_field = write_types_local_process_field;
3959 d.opt = s->u.s.opt;
3960 d.line = &s->u.s.line;
3961 d.bitmap = s->u.s.bitmap;
3962 d.prev_val[0] = d.prev_val[2] = "*x";
3963 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3964 d.prev_val[3] = "x";
3965 d.val = "(*x)";
3966 d.fn_wants_lvalue = true;
3968 oprintf (d.of, "\n");
3969 oprintf (d.of, "void\n");
3970 oprintf (d.of, "gt_pch_p_");
3971 output_mangled_typename (d.of, orig_s);
3972 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3973 "\tvoid *x_p,\n"
3974 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3975 "\tATTRIBUTE_UNUSED void *cookie)\n");
3976 oprintf (d.of, "{\n");
3977 oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3978 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3979 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3980 d.indent = 2;
3981 d.have_this_obj = true;
3983 if (s->kind != TYPE_USER_STRUCT)
3984 walk_type (s, &d);
3985 else
3987 /* User structures have no fields to walk. Simply generate a
3988 call to the user-provided PCH walker. */
3989 oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
3990 d.prev_val[3]);
3991 oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3992 d.indent, "", d.val);
3995 oprintf (d.of, "}\n");
3997 /* Write user-callable entry points for the PCH walking routines. */
3998 if (orig_s->kind == TYPE_USER_STRUCT)
3999 write_pch_user_walking_functions (s, &d);
4001 for (options_p o = s->u.s.opt; o; o = o->next)
4002 if (strcmp (o->name, "for_user") == 0)
4004 write_pch_user_walking_for_structure_body (s, &d);
4005 break;
4009 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
4011 static void
4012 write_local (outf_p output_header, type_p structures)
4014 type_p s;
4016 if (!output_header)
4017 return;
4019 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
4020 for (s = structures; s; s = s->next)
4021 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
4023 options_p opt;
4025 if (s->u.s.line.file == NULL)
4026 continue;
4027 for (opt = s->u.s.opt; opt; opt = opt->next)
4028 if (strcmp (opt->name, "ptr_alias") == 0
4029 && opt->kind == OPTION_TYPE)
4031 const_type_p const t = (const_type_p) opt->info.type;
4032 if (t->kind == TYPE_STRUCT
4033 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
4035 oprintf (output_header, "#define gt_pch_p_");
4036 output_mangled_typename (output_header, s);
4037 oprintf (output_header, " gt_pch_p_");
4038 output_mangled_typename (output_header, t);
4039 oprintf (output_header, "\n");
4041 else
4042 error_at_line (&s->u.s.line,
4043 "structure alias is not a structure");
4044 break;
4046 if (opt)
4047 continue;
4049 /* Declare the marker procedure only once. */
4050 oprintf (output_header, "extern void gt_pch_p_");
4051 output_mangled_typename (output_header, s);
4052 oprintf (output_header,
4053 "\n (void *, void *, gt_pointer_operator, void *);\n");
4055 if (s->kind == TYPE_LANG_STRUCT)
4057 type_p ss;
4058 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4059 write_local_func_for_structure (s, ss);
4061 else
4062 write_local_func_for_structure (s, s);
4066 /* Nonzero if S is a type for which typed GC allocators should be output. */
4068 #define USED_BY_TYPED_GC_P(s) \
4069 ((s->kind == TYPE_POINTER \
4070 && (s->u.p->gc_used == GC_POINTED_TO \
4071 || s->u.p->gc_used == GC_USED)) \
4072 || (union_or_struct_p (s) \
4073 && ((s)->gc_used == GC_POINTED_TO \
4074 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4075 && s->u.s.line.file != NULL) \
4076 || ((s)->gc_used == GC_USED \
4077 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))) \
4078 || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
4082 /* Might T contain any non-pointer elements? */
4084 static int
4085 contains_scalar_p (type_p t)
4087 switch (t->kind)
4089 case TYPE_STRING:
4090 case TYPE_POINTER:
4091 return 0;
4092 case TYPE_ARRAY:
4093 return contains_scalar_p (t->u.a.p);
4094 case TYPE_USER_STRUCT:
4095 /* User-marked structures will typically contain pointers. */
4096 return 0;
4097 default:
4098 /* Could also check for structures that have no non-pointer
4099 fields, but there aren't enough of those to worry about. */
4100 return 1;
4104 /* Mangle INPF and print it to F. */
4106 static void
4107 put_mangled_filename (outf_p f, const input_file *inpf)
4109 /* The call to get_output_file_name may indirectly update fn since
4110 get_output_file_with_visibility caches its result inside, so we
4111 need the CONST_CAST. */
4112 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4113 if (!f || !name)
4114 return;
4115 for (; *name != 0; name++)
4116 if (ISALNUM (*name))
4117 oprintf (f, "%c", *name);
4118 else
4119 oprintf (f, "%c", '_');
4122 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4123 LASTNAME, and NAME are all strings to insert in various places in
4124 the resulting code. */
4126 static void
4127 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4128 const char *tname, const char *name)
4130 struct flist *fli2;
4132 for (fli2 = flp; fli2; fli2 = fli2->next)
4133 if (fli2->started_p)
4135 oprintf (fli2->f, " %s\n", lastname);
4136 oprintf (fli2->f, "};\n\n");
4139 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4140 if (fli2->started_p)
4142 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4143 int fnum;
4145 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4146 if (bitmap & 1)
4148 oprintf (base_files[fnum],
4149 "extern const struct %s gt_%s_", tname, pfx);
4150 put_mangled_filename (base_files[fnum], fli2->file);
4151 oprintf (base_files[fnum], "[];\n");
4156 size_t fnum;
4157 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4158 oprintf (base_files[fnum],
4159 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4163 for (fli2 = flp; fli2; fli2 = fli2->next)
4164 if (fli2->started_p)
4166 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4167 int fnum;
4169 fli2->started_p = 0;
4171 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4172 if (bitmap & 1)
4174 oprintf (base_files[fnum], " gt_%s_", pfx);
4175 put_mangled_filename (base_files[fnum], fli2->file);
4176 oprintf (base_files[fnum], ",\n");
4181 size_t fnum;
4182 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4184 oprintf (base_files[fnum], " NULL\n");
4185 oprintf (base_files[fnum], "};\n");
4190 /* Finish off the created gt_clear_caches_file_c functions. */
4192 static void
4193 finish_cache_funcs (flist *flp)
4195 struct flist *fli2;
4197 for (fli2 = flp; fli2; fli2 = fli2->next)
4198 if (fli2->started_p)
4200 oprintf (fli2->f, "}\n\n");
4203 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4204 if (fli2->started_p)
4206 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4207 int fnum;
4209 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4210 if (bitmap & 1)
4212 oprintf (base_files[fnum], "extern void gt_clear_caches_");
4213 put_mangled_filename (base_files[fnum], fli2->file);
4214 oprintf (base_files[fnum], " ();\n");
4218 for (size_t fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4219 oprintf (base_files[fnum], "void\ngt_clear_caches ()\n{\n");
4221 for (fli2 = flp; fli2; fli2 = fli2->next)
4222 if (fli2->started_p)
4224 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4225 int fnum;
4227 fli2->started_p = 0;
4229 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4230 if (bitmap & 1)
4232 oprintf (base_files[fnum], " gt_clear_caches_");
4233 put_mangled_filename (base_files[fnum], fli2->file);
4234 oprintf (base_files[fnum], " ();\n");
4238 for (size_t fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4240 oprintf (base_files[fnum], "}\n");
4244 /* Write the first three fields (pointer, count and stride) for
4245 root NAME to F. V and LINE are as for write_root.
4247 Return true if the entry could be written; return false on error. */
4249 static bool
4250 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4252 type_p ap;
4254 if (!v)
4256 error_at_line (line, "`%s' is too complex to be a root", name);
4257 return false;
4260 oprintf (f, " {\n");
4261 oprintf (f, " &%s,\n", name);
4262 oprintf (f, " 1");
4264 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4265 if (ap->u.a.len[0])
4266 oprintf (f, " * (%s)", ap->u.a.len);
4267 else if (ap == v->type)
4268 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4269 oprintf (f, ",\n");
4270 oprintf (f, " sizeof (%s", v->name);
4271 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4272 oprintf (f, "[0]");
4273 oprintf (f, "),\n");
4274 return true;
4277 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4278 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4279 of the caller. */
4281 static void
4282 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4283 int has_length, struct fileloc *line,
4284 bool emit_pch, type_p field_type, const char *field_name)
4286 struct pair newv;
4287 /* If the field reference is relative to V, rather than to some
4288 subcomponent of V, we can mark any subarrays with a single stride.
4289 We're effectively treating the field as a global variable in its
4290 own right. */
4291 if (v && type == v->type)
4293 newv = *v;
4294 newv.type = field_type;
4295 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4296 v = &newv;
4298 /* Otherwise, any arrays nested in the structure are too complex to
4299 handle. */
4300 else if (field_type->kind == TYPE_ARRAY)
4301 v = NULL;
4302 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4303 has_length, line, emit_pch);
4306 /* Write out to F the table entry and any marker routines needed to
4307 mark NAME as TYPE. V can be one of three values:
4309 - null, if NAME is too complex to represent using a single
4310 count and stride. In this case, it is an error for NAME to
4311 contain any gc-ed data.
4313 - the outermost array that contains NAME, if NAME is part of an array.
4315 - the C variable that contains NAME, if NAME is not part of an array.
4317 LINE is the line of the C source that declares the root variable.
4318 HAS_LENGTH is nonzero iff V was a variable-length array. */
4320 static void
4321 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4322 struct fileloc *line, bool emit_pch)
4324 switch (type->kind)
4326 case TYPE_STRUCT:
4328 pair_p fld;
4329 for (fld = type->u.s.fields; fld; fld = fld->next)
4331 int skip_p = 0;
4332 const char *desc = NULL;
4333 options_p o;
4335 for (o = fld->opt; o; o = o->next)
4336 if (strcmp (o->name, "skip") == 0)
4337 skip_p = 1;
4338 else if (strcmp (o->name, "desc") == 0
4339 && o->kind == OPTION_STRING)
4340 desc = o->info.string;
4341 else
4342 error_at_line (line,
4343 "field `%s' of global `%s' has unknown option `%s'",
4344 fld->name, name, o->name);
4346 if (skip_p)
4347 continue;
4348 else if (desc && fld->type->kind == TYPE_UNION)
4350 pair_p validf = NULL;
4351 pair_p ufld;
4353 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4355 const char *tag = NULL;
4356 options_p oo;
4357 for (oo = ufld->opt; oo; oo = oo->next)
4358 if (strcmp (oo->name, "tag") == 0
4359 && oo->kind == OPTION_STRING)
4360 tag = oo->info.string;
4361 if (tag == NULL || strcmp (tag, desc) != 0)
4362 continue;
4363 if (validf != NULL)
4364 error_at_line (line,
4365 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4366 name, fld->name, validf->name,
4367 name, fld->name, ufld->name, tag);
4368 validf = ufld;
4370 if (validf != NULL)
4371 write_field_root (f, v, type, name, 0, line, emit_pch,
4372 validf->type,
4373 ACONCAT ((fld->name, ".",
4374 validf->name, NULL)));
4376 else if (desc)
4377 error_at_line (line,
4378 "global `%s.%s' has `desc' option but is not union",
4379 name, fld->name);
4380 else
4381 write_field_root (f, v, type, name, 0, line, emit_pch, fld->type,
4382 fld->name);
4385 break;
4387 case TYPE_ARRAY:
4389 char *newname;
4390 newname = xasprintf ("%s[0]", name);
4391 write_root (f, v, type->u.a.p, newname, has_length, line, emit_pch);
4392 free (newname);
4394 break;
4396 case TYPE_USER_STRUCT:
4397 error_at_line (line, "`%s' must be a pointer type, because it is "
4398 "a GC root and its type is marked with GTY((user))",
4399 v->name);
4400 break;
4402 case TYPE_POINTER:
4404 const_type_p tp;
4406 if (!start_root_entry (f, v, name, line))
4407 return;
4409 tp = type->u.p;
4411 if (!has_length && union_or_struct_p (tp))
4413 tp = get_ultimate_base_class (tp);
4414 const char *id_for_tag = filter_type_name (tp->u.s.tag);
4415 oprintf (f, " &gt_ggc_mx_%s,\n", id_for_tag);
4416 if (emit_pch)
4417 oprintf (f, " &gt_pch_nx_%s", id_for_tag);
4418 else
4419 oprintf (f, " NULL");
4420 if (id_for_tag != tp->u.s.tag)
4421 free (CONST_CAST (char *, id_for_tag));
4423 else if (has_length
4424 && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
4426 oprintf (f, " &gt_ggc_ma_%s,\n", name);
4427 if (emit_pch)
4428 oprintf (f, " &gt_pch_na_%s", name);
4429 else
4430 oprintf (f, " NULL");
4432 else
4434 error_at_line (line,
4435 "global `%s' is pointer to unimplemented type",
4436 name);
4438 oprintf (f, "\n },\n");
4440 break;
4442 case TYPE_STRING:
4444 if (!start_root_entry (f, v, name, line))
4445 return;
4447 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
4448 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
4449 oprintf (f, " },\n");
4451 break;
4453 case TYPE_SCALAR:
4454 break;
4456 case TYPE_NONE:
4457 case TYPE_UNDEFINED:
4458 case TYPE_UNION:
4459 case TYPE_LANG_STRUCT:
4460 error_at_line (line, "global `%s' is unimplemented type", name);
4464 /* This generates a routine to walk an array. */
4466 static void
4467 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4469 struct walk_type_data d;
4470 char *prevval3;
4472 memset (&d, 0, sizeof (d));
4473 d.of = f;
4474 d.cookie = wtd;
4475 d.indent = 2;
4476 d.line = &v->line;
4477 d.opt = v->opt;
4478 d.bitmap = get_lang_bitmap (v->line.file);
4480 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4482 if (wtd->param_prefix)
4484 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4485 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
4486 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4487 wtd->param_prefix, v->name);
4488 oprintf (d.of,
4489 " ATTRIBUTE_UNUSED void *x_p,\n"
4490 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4491 " ATTRIBUTE_UNUSED void * cookie)\n");
4492 oprintf (d.of, "{\n");
4493 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4494 d.process_field = write_types_local_process_field;
4495 d.have_this_obj = true;
4496 walk_type (v->type, &d);
4497 oprintf (f, "}\n\n");
4500 d.opt = v->opt;
4501 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4502 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4503 wtd->prefix, v->name);
4504 oprintf (f, "{\n");
4505 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4506 d.process_field = write_types_process_field;
4507 d.have_this_obj = false;
4508 walk_type (v->type, &d);
4509 free (prevval3);
4510 oprintf (f, "}\n\n");
4513 /* Output a table describing the locations and types of VARIABLES. */
4515 static void
4516 write_roots (pair_p variables, bool emit_pch)
4518 pair_p v;
4519 struct flist *flp = NULL;
4521 for (v = variables; v; v = v->next)
4523 outf_p f =
4524 get_output_file_with_visibility (CONST_CAST (input_file*,
4525 v->line.file));
4526 struct flist *fli;
4527 const char *length = NULL;
4528 int deletable_p = 0;
4529 options_p o;
4530 for (o = v->opt; o; o = o->next)
4531 if (strcmp (o->name, "length") == 0
4532 && o->kind == OPTION_STRING)
4533 length = o->info.string;
4534 else if (strcmp (o->name, "deletable") == 0)
4535 deletable_p = 1;
4536 else if (strcmp (o->name, "cache") == 0)
4538 else
4539 error_at_line (&v->line,
4540 "global `%s' has unknown option `%s'",
4541 v->name, o->name);
4543 for (fli = flp; fli; fli = fli->next)
4544 if (fli->f == f && f)
4545 break;
4546 if (fli == NULL)
4548 fli = XNEW (struct flist);
4549 fli->f = f;
4550 fli->next = flp;
4551 fli->started_p = 0;
4552 fli->file = v->line.file;
4553 gcc_assert (fli->file);
4554 flp = fli;
4556 oprintf (f, "\n/* GC roots. */\n\n");
4559 if (!deletable_p
4560 && length
4561 && v->type->kind == TYPE_POINTER
4562 && (v->type->u.p->kind == TYPE_POINTER
4563 || v->type->u.p->kind == TYPE_STRUCT))
4565 write_array (f, v, &ggc_wtd);
4566 write_array (f, v, &pch_wtd);
4570 for (v = variables; v; v = v->next)
4572 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4573 v->line.file));
4574 struct flist *fli;
4575 int skip_p = 0;
4576 int length_p = 0;
4577 options_p o;
4579 for (o = v->opt; o; o = o->next)
4580 if (strcmp (o->name, "length") == 0)
4581 length_p = 1;
4582 else if (strcmp (o->name, "deletable") == 0)
4583 skip_p = 1;
4585 if (skip_p)
4586 continue;
4588 for (fli = flp; fli; fli = fli->next)
4589 if (fli->f == f)
4590 break;
4591 if (!fli->started_p)
4593 fli->started_p = 1;
4595 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4596 put_mangled_filename (f, v->line.file);
4597 oprintf (f, "[] = {\n");
4600 write_root (f, v, v->type, v->name, length_p, &v->line, emit_pch);
4603 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4604 "gt_ggc_rtab");
4606 for (v = variables; v; v = v->next)
4608 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4609 v->line.file));
4610 struct flist *fli;
4611 int skip_p = 1;
4612 options_p o;
4614 for (o = v->opt; o; o = o->next)
4615 if (strcmp (o->name, "deletable") == 0)
4616 skip_p = 0;
4618 if (skip_p)
4619 continue;
4621 for (fli = flp; fli; fli = fli->next)
4622 if (fli->f == f)
4623 break;
4624 if (!fli->started_p)
4626 fli->started_p = 1;
4628 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4629 put_mangled_filename (f, v->line.file);
4630 oprintf (f, "[] = {\n");
4633 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4634 v->name, v->name);
4637 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4638 "gt_ggc_deletable_rtab");
4640 for (v = variables; v; v = v->next)
4642 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4643 v->line.file));
4644 struct flist *fli;
4645 bool cache = false;
4646 options_p o;
4648 for (o = v->opt; o; o = o->next)
4649 if (strcmp (o->name, "cache") == 0)
4650 cache = true;
4651 if (!cache)
4652 continue;
4654 for (fli = flp; fli; fli = fli->next)
4655 if (fli->f == f)
4656 break;
4657 if (!fli->started_p)
4659 fli->started_p = 1;
4661 oprintf (f, "void\ngt_clear_caches_");
4662 put_mangled_filename (f, v->line.file);
4663 oprintf (f, " ()\n{\n");
4666 oprintf (f, " gt_cleare_cache (%s);\n", v->name);
4669 finish_cache_funcs (flp);
4671 if (!emit_pch)
4672 return;
4674 for (v = variables; v; v = v->next)
4676 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4677 v->line.file));
4678 struct flist *fli;
4679 int skip_p = 0;
4680 options_p o;
4682 for (o = v->opt; o; o = o->next)
4683 if (strcmp (o->name, "deletable") == 0)
4685 skip_p = 1;
4686 break;
4689 if (skip_p)
4690 continue;
4692 if (!contains_scalar_p (v->type))
4693 continue;
4695 for (fli = flp; fli; fli = fli->next)
4696 if (fli->f == f)
4697 break;
4698 if (!fli->started_p)
4700 fli->started_p = 1;
4702 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4703 put_mangled_filename (f, v->line.file);
4704 oprintf (f, "[] = {\n");
4707 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4708 v->name, v->name);
4711 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4712 "gt_pch_scalar_rtab");
4715 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4716 guaranteee for somewhat increased readability. If name conflicts do happen,
4717 this funcion will have to be adjusted to be more like
4718 output_mangled_typename. */
4720 #define INDENT 2
4722 /* Dumps the value of typekind KIND. */
4724 static void
4725 dump_typekind (int indent, enum typekind kind)
4727 printf ("%*ckind = ", indent, ' ');
4728 switch (kind)
4730 case TYPE_SCALAR:
4731 printf ("TYPE_SCALAR");
4732 break;
4733 case TYPE_STRING:
4734 printf ("TYPE_STRING");
4735 break;
4736 case TYPE_STRUCT:
4737 printf ("TYPE_STRUCT");
4738 break;
4739 case TYPE_UNDEFINED:
4740 printf ("TYPE_UNDEFINED");
4741 break;
4742 case TYPE_USER_STRUCT:
4743 printf ("TYPE_USER_STRUCT");
4744 break;
4745 case TYPE_UNION:
4746 printf ("TYPE_UNION");
4747 break;
4748 case TYPE_POINTER:
4749 printf ("TYPE_POINTER");
4750 break;
4751 case TYPE_ARRAY:
4752 printf ("TYPE_ARRAY");
4753 break;
4754 case TYPE_LANG_STRUCT:
4755 printf ("TYPE_LANG_STRUCT");
4756 break;
4757 default:
4758 gcc_unreachable ();
4760 printf ("\n");
4763 /* Dumps the value of GC_USED flag. */
4765 static void
4766 dump_gc_used (int indent, enum gc_used_enum gc_used)
4768 printf ("%*cgc_used = ", indent, ' ');
4769 switch (gc_used)
4771 case GC_UNUSED:
4772 printf ("GC_UNUSED");
4773 break;
4774 case GC_USED:
4775 printf ("GC_USED");
4776 break;
4777 case GC_MAYBE_POINTED_TO:
4778 printf ("GC_MAYBE_POINTED_TO");
4779 break;
4780 case GC_POINTED_TO:
4781 printf ("GC_POINTED_TO");
4782 break;
4783 default:
4784 gcc_unreachable ();
4786 printf ("\n");
4789 /* Dumps the type options OPT. */
4791 static void
4792 dump_options (int indent, options_p opt)
4794 options_p o;
4795 printf ("%*coptions = ", indent, ' ');
4796 o = opt;
4797 while (o)
4799 switch (o->kind)
4801 case OPTION_STRING:
4802 printf ("%s:string %s ", o->name, o->info.string);
4803 break;
4804 case OPTION_TYPE:
4805 printf ("%s:type ", o->name);
4806 dump_type (indent+1, o->info.type);
4807 break;
4808 case OPTION_NESTED:
4809 printf ("%s:nested ", o->name);
4810 break;
4811 case OPTION_NONE:
4812 gcc_unreachable ();
4814 o = o->next;
4816 printf ("\n");
4819 /* Dumps the source file location in LINE. */
4821 static void
4822 dump_fileloc (int indent, struct fileloc line)
4824 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
4825 get_input_file_name (line.file),
4826 line.line);
4829 /* Recursively dumps the struct, union, or a language-specific
4830 struct T. */
4832 static void
4833 dump_type_u_s (int indent, type_p t)
4835 pair_p fields;
4837 gcc_assert (union_or_struct_p (t));
4838 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
4839 dump_fileloc (indent, t->u.s.line);
4840 printf ("%*cu.s.fields =\n", indent, ' ');
4841 fields = t->u.s.fields;
4842 while (fields)
4844 dump_pair (indent + INDENT, fields);
4845 fields = fields->next;
4847 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
4848 dump_options (indent, t->u.s.opt);
4849 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
4850 if (t->kind == TYPE_LANG_STRUCT)
4852 printf ("%*cu.s.lang_struct:\n", indent, ' ');
4853 dump_type_list (indent + INDENT, t->u.s.lang_struct);
4857 /* Recursively dumps the array T. */
4859 static void
4860 dump_type_u_a (int indent, type_p t)
4862 gcc_assert (t->kind == TYPE_ARRAY);
4863 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
4864 dump_type_list (indent + INDENT, t->u.a.p);
4867 /* Recursively dumps the type list T. */
4869 static void
4870 dump_type_list (int indent, type_p t)
4872 type_p p = t;
4873 while (p)
4875 dump_type (indent, p);
4876 p = p->next;
4880 static htab_t seen_types;
4882 /* Recursively dumps the type T if it was not dumped previously. */
4884 static void
4885 dump_type (int indent, type_p t)
4887 PTR *slot;
4889 if (seen_types == NULL)
4890 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
4892 printf ("%*cType at %p: ", indent, ' ', (void *) t);
4893 slot = htab_find_slot (seen_types, t, INSERT);
4894 if (*slot != NULL)
4896 printf ("already seen.\n");
4897 return;
4899 *slot = t;
4900 printf ("\n");
4902 dump_typekind (indent, t->kind);
4903 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
4904 (void *) t->pointer_to);
4905 dump_gc_used (indent + INDENT, t->gc_used);
4906 switch (t->kind)
4908 case TYPE_SCALAR:
4909 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
4910 t->u.scalar_is_char ? "true" : "false");
4911 break;
4912 case TYPE_STRING:
4913 break;
4914 case TYPE_STRUCT:
4915 case TYPE_UNION:
4916 case TYPE_LANG_STRUCT:
4917 case TYPE_USER_STRUCT:
4918 dump_type_u_s (indent + INDENT, t);
4919 break;
4920 case TYPE_POINTER:
4921 printf ("%*cp:\n", indent + INDENT, ' ');
4922 dump_type (indent + INDENT, t->u.p);
4923 break;
4924 case TYPE_ARRAY:
4925 dump_type_u_a (indent + INDENT, t);
4926 break;
4927 default:
4928 gcc_unreachable ();
4930 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
4933 /* Dumps the pair P. */
4935 static void
4936 dump_pair (int indent, pair_p p)
4938 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
4939 dump_type (indent, p->type);
4940 dump_fileloc (indent, p->line);
4941 dump_options (indent, p->opt);
4942 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
4945 /* Dumps the list of pairs PP. */
4947 static void
4948 dump_pair_list (const char *name, pair_p pp)
4950 pair_p p;
4951 printf ("%s:\n", name);
4952 for (p = pp; p != NULL; p = p->next)
4953 dump_pair (0, p);
4954 printf ("End of %s\n\n", name);
4957 /* Dumps the STRUCTURES. */
4959 static void
4960 dump_structures (const char *name, type_p structures)
4962 printf ("%s:\n", name);
4963 dump_type_list (0, structures);
4964 printf ("End of %s\n\n", name);
4967 /* Dumps the internal structures of gengtype. This is useful to debug
4968 gengtype itself, or to understand what it does, e.g. for plugin
4969 developers. */
4971 static void
4972 dump_everything (void)
4974 dump_pair_list ("typedefs", typedefs);
4975 dump_structures ("structures", structures);
4976 dump_pair_list ("variables", variables);
4978 /* Allocated with the first call to dump_type. */
4979 htab_delete (seen_types);
4984 /* Option specification for getopt_long. */
4985 static const struct option gengtype_long_options[] = {
4986 {"help", no_argument, NULL, 'h'},
4987 {"version", no_argument, NULL, 'V'},
4988 {"verbose", no_argument, NULL, 'v'},
4989 {"dump", no_argument, NULL, 'd'},
4990 {"debug", no_argument, NULL, 'D'},
4991 {"plugin", required_argument, NULL, 'P'},
4992 {"srcdir", required_argument, NULL, 'S'},
4993 {"backupdir", required_argument, NULL, 'B'},
4994 {"inputs", required_argument, NULL, 'I'},
4995 {"read-state", required_argument, NULL, 'r'},
4996 {"write-state", required_argument, NULL, 'w'},
4997 /* Terminating NULL placeholder. */
4998 {NULL, no_argument, NULL, 0},
5002 static void
5003 print_usage (void)
5005 printf ("Usage: %s\n", progname);
5006 printf ("\t -h | --help " " \t# Give this help.\n");
5007 printf ("\t -D | --debug "
5008 " \t# Give debug output to debug %s itself.\n", progname);
5009 printf ("\t -V | --version " " \t# Give version information.\n");
5010 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5011 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5012 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5013 " \t# Generate for plugin.\n");
5014 printf ("\t -S | --srcdir <GCC-directory> "
5015 " \t# Specify the GCC source directory.\n");
5016 printf ("\t -B | --backupdir <directory> "
5017 " \t# Specify the backup directory for updated files.\n");
5018 printf ("\t -I | --inputs <input-list> "
5019 " \t# Specify the file with source files list.\n");
5020 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5021 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5024 static void
5025 print_version (void)
5027 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5028 printf ("Report bugs: %s\n", bug_report_url);
5031 /* Parse the program options using getopt_long... */
5032 static void
5033 parse_program_options (int argc, char **argv)
5035 int opt = -1;
5036 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5037 gengtype_long_options, NULL)) >= 0)
5039 switch (opt)
5041 case 'h': /* --help */
5042 print_usage ();
5043 break;
5044 case 'V': /* --version */
5045 print_version ();
5046 break;
5047 case 'd': /* --dump */
5048 do_dump = 1;
5049 break;
5050 case 'D': /* --debug */
5051 do_debug = 1;
5052 break;
5053 case 'v': /* --verbose */
5054 verbosity_level++;
5055 break;
5056 case 'P': /* --plugin */
5057 if (optarg)
5058 plugin_output_filename = optarg;
5059 else
5060 fatal ("missing plugin output file name");
5061 break;
5062 case 'S': /* --srcdir */
5063 if (optarg)
5064 srcdir = optarg;
5065 else
5066 fatal ("missing source directory");
5067 srcdir_len = strlen (srcdir);
5068 break;
5069 case 'B': /* --backupdir */
5070 if (optarg)
5071 backup_dir = optarg;
5072 else
5073 fatal ("missing backup directory");
5074 break;
5075 case 'I': /* --inputs */
5076 if (optarg)
5077 inputlist = optarg;
5078 else
5079 fatal ("missing input list");
5080 break;
5081 case 'r': /* --read-state */
5082 if (optarg)
5083 read_state_filename = optarg;
5084 else
5085 fatal ("missing read state file");
5086 DBGPRINTF ("read state %s\n", optarg);
5087 break;
5088 case 'w': /* --write-state */
5089 DBGPRINTF ("write state %s\n", optarg);
5090 if (optarg)
5091 write_state_filename = optarg;
5092 else
5093 fatal ("missing write state file");
5094 break;
5095 default:
5096 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5097 print_usage ();
5098 fatal ("unexpected flag");
5101 if (plugin_output_filename)
5103 /* In plugin mode we require some input files. */
5104 int i = 0;
5105 if (optind >= argc)
5106 fatal ("no source files given in plugin mode");
5107 nb_plugin_files = argc - optind;
5108 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5109 for (i = 0; i < (int) nb_plugin_files; i++)
5111 char *name = argv[i + optind];
5112 plugin_files[i] = input_file_by_name (name);
5119 /******* Manage input files. ******/
5121 /* Hash table of unique input file names. */
5122 static htab_t input_file_htab;
5124 /* Find or allocate a new input_file by hash-consing it. */
5125 input_file*
5126 input_file_by_name (const char* name)
5128 PTR* slot;
5129 input_file* f = NULL;
5130 int namlen = 0;
5131 if (!name)
5132 return NULL;
5133 namlen = strlen (name);
5134 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5135 f->inpbitmap = 0;
5136 f->inpoutf = NULL;
5137 f->inpisplugin = false;
5138 strcpy (f->inpname, name);
5139 slot = htab_find_slot (input_file_htab, f, INSERT);
5140 gcc_assert (slot != NULL);
5141 if (*slot)
5143 /* Already known input file. */
5144 free (f);
5145 return (input_file*)(*slot);
5147 /* New input file. */
5148 *slot = f;
5149 return f;
5152 /* Hash table support routines for input_file-s. */
5153 static hashval_t
5154 htab_hash_inputfile (const void *p)
5156 const input_file *inpf = (const input_file *) p;
5157 gcc_assert (inpf);
5158 return htab_hash_string (get_input_file_name (inpf));
5161 static int
5162 htab_eq_inputfile (const void *x, const void *y)
5164 const input_file *inpfx = (const input_file *) x;
5165 const input_file *inpfy = (const input_file *) y;
5166 gcc_assert (inpfx != NULL && inpfy != NULL);
5167 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5172 main (int argc, char **argv)
5174 size_t i;
5175 static struct fileloc pos = { NULL, 0 };
5176 outf_p output_header;
5178 /* Mandatory common initializations. */
5179 progname = "gengtype"; /* For fatal and messages. */
5180 /* Create the hash-table used to hash-cons input files. */
5181 input_file_htab =
5182 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5183 /* Initialize our special input files. */
5184 this_file = input_file_by_name (__FILE__);
5185 system_h_file = input_file_by_name ("system.h");
5186 /* Set the scalar_is_char union number for predefined scalar types. */
5187 scalar_nonchar.u.scalar_is_char = FALSE;
5188 scalar_char.u.scalar_is_char = TRUE;
5190 parse_program_options (argc, argv);
5192 #if ENABLE_CHECKING
5193 if (do_debug)
5195 time_t now = (time_t) 0;
5196 time (&now);
5197 DBGPRINTF ("gengtype started pid %d at %s",
5198 (int) getpid (), ctime (&now));
5200 #endif /* ENABLE_CHECKING */
5202 /* Parse the input list and the input files. */
5203 DBGPRINTF ("inputlist %s", inputlist);
5204 if (read_state_filename)
5206 if (inputlist)
5207 fatal ("input list %s cannot be given with a read state file %s",
5208 inputlist, read_state_filename);
5209 read_state (read_state_filename);
5210 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5212 else if (inputlist)
5214 /* These types are set up with #define or else outside of where
5215 we can see them. We should initialize them before calling
5216 read_input_list. */
5217 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5218 Call;} while (0)
5219 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5220 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5221 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5222 POS_HERE (do_scalar_typedef ("double_int", &pos));
5223 POS_HERE (do_scalar_typedef ("offset_int", &pos));
5224 POS_HERE (do_scalar_typedef ("widest_int", &pos));
5225 POS_HERE (do_scalar_typedef ("int64_t", &pos));
5226 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5227 POS_HERE (do_scalar_typedef ("uint8", &pos));
5228 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5229 POS_HERE (do_scalar_typedef ("jword", &pos));
5230 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5231 POS_HERE (do_scalar_typedef ("void", &pos));
5232 POS_HERE (do_scalar_typedef ("machine_mode", &pos));
5233 POS_HERE (do_typedef ("PTR",
5234 create_pointer (resolve_typedef ("void", &pos)),
5235 &pos));
5236 #undef POS_HERE
5237 read_input_list (inputlist);
5238 for (i = 0; i < num_gt_files; i++)
5240 parse_file (get_input_file_name (gt_files[i]));
5241 DBGPRINTF ("parsed file #%d %s",
5242 (int) i, get_input_file_name (gt_files[i]));
5244 if (verbosity_level >= 1)
5245 printf ("%s parsed %d files with %d GTY types\n",
5246 progname, (int) num_gt_files, type_count);
5248 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5250 else
5251 fatal ("either an input list or a read state file should be given");
5252 if (hit_error)
5253 return 1;
5256 if (plugin_output_filename)
5258 size_t ix = 0;
5259 /* In plugin mode, we should have read a state file, and have
5260 given at least one plugin file. */
5261 if (!read_state_filename)
5262 fatal ("No read state given in plugin mode for %s",
5263 plugin_output_filename);
5265 if (nb_plugin_files == 0 || !plugin_files)
5266 fatal ("No plugin files given in plugin mode for %s",
5267 plugin_output_filename);
5269 /* Parse our plugin files and augment the state. */
5270 for (ix = 0; ix < nb_plugin_files; ix++)
5272 input_file* pluginput = plugin_files [ix];
5273 pluginput->inpisplugin = true;
5274 parse_file (get_input_file_name (pluginput));
5276 if (hit_error)
5277 return 1;
5279 plugin_output = create_file ("GCC", plugin_output_filename);
5280 DBGPRINTF ("created plugin_output %p named %s",
5281 (void *) plugin_output, plugin_output->name);
5283 else
5284 { /* No plugin files, we are in normal mode. */
5285 if (!srcdir)
5286 fatal ("gengtype needs a source directory in normal mode");
5288 if (hit_error)
5289 return 1;
5291 gen_rtx_next ();
5293 set_gc_used (variables);
5295 for (type_p t = structures; t; t = t->next)
5297 bool for_user = false;
5298 for (options_p o = t->u.s.opt; o; o = o->next)
5299 if (strcmp (o->name, "for_user") == 0)
5301 for_user = true;
5302 break;
5305 if (for_user)
5306 set_gc_used_type (t, GC_POINTED_TO);
5308 /* The state at this point is read from the state input file or by
5309 parsing source files and optionally augmented by parsing plugin
5310 source files. Write it now. */
5311 if (write_state_filename)
5313 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5315 if (hit_error)
5316 fatal ("didn't write state file %s after errors",
5317 write_state_filename);
5319 DBGPRINTF ("before write_state %s", write_state_filename);
5320 write_state (write_state_filename);
5322 if (do_dump)
5323 dump_everything ();
5325 /* After having written the state file we return immediately to
5326 avoid generating any output file. */
5327 if (hit_error)
5328 return 1;
5329 else
5330 return 0;
5334 open_base_files ();
5336 output_header = plugin_output ? plugin_output : header_file;
5337 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5338 structures);
5340 write_types (output_header, structures, &ggc_wtd);
5341 if (plugin_files == NULL)
5343 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5344 structures);
5345 write_types (header_file, structures, &pch_wtd);
5346 write_local (header_file, structures);
5348 write_roots (variables, plugin_files == NULL);
5349 write_rtx_next ();
5350 close_output_files ();
5352 if (do_dump)
5353 dump_everything ();
5355 /* Don't bother about free-ing any input or plugin file, etc. */
5357 if (hit_error)
5358 return 1;
5359 return 0;