1 /* Process source files and output type information.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
28 #include "errors.h" /* for fatal */
30 #include "double-int.h"
31 #include "version.h" /* for version_string & pkgversion_string. */
36 #include "filenames.h"
38 /* Data types, macros, etc. used only in this file. */
41 /* The list of output files. */
44 /* The output header file that is included into pretty much every
49 /* The name of the file containing the list of input files. */
50 static char *inputlist;
52 /* The plugin input files and their number; in that case only
53 a single file is produced. */
54 static input_file **plugin_files;
55 static size_t nb_plugin_files;
57 /* The generated plugin output file and name. */
58 static outf_p plugin_output;
59 static char *plugin_output_filename;
61 /* Our source directory and its length. */
65 /* Variables used for reading and writing the state. */
66 const char *read_state_filename;
67 const char *write_state_filename;
69 /* Variables to help debugging. */
73 /* Level for verbose messages. */
76 /* We have a type count and use it to set the state_number of newly
77 allocated types to some unique negative number. */
78 static int type_count;
80 /* The backup directory should be in the same file system as the
81 generated files, otherwise the rename(2) system call would fail.
82 If NULL, no backup is made when overwriting a generated file. */
83 static const char* backup_dir; /* (-B) program option. */
86 static outf_p create_file (const char *, const char *);
88 static const char *get_file_basename (const input_file *);
89 static const char *get_file_realbasename (const input_file *);
91 static int get_prefix_langdir_index (const char *);
92 static const char *get_file_langdir (const input_file *);
94 static void dump_pair (int indent, pair_p p);
95 static void dump_type (int indent, type_p p);
96 static void dump_type_list (int indent, type_p p);
99 /* Nonzero iff an error has occurred. */
100 bool hit_error = false;
102 static void gen_rtx_next (void);
103 static void write_rtx_next (void);
104 static void open_base_files (void);
105 static void close_output_files (void);
107 /* Report an error at POS, printing MSG. */
110 error_at_line (const struct fileloc *pos, const char *msg, ...)
114 gcc_assert (pos != NULL && pos->file != NULL);
117 fprintf (stderr, "%s:%d: ", get_input_file_name (pos->file), pos->line);
118 vfprintf (stderr, msg, ap);
119 fputc ('\n', stderr);
125 /* asprintf, but produces fatal message on out-of-memory. */
127 xasprintf (const char *format, ...)
133 va_start (ap, format);
134 n = vasprintf (&result, format, ap);
135 if (result == NULL || n < 0)
136 fatal ("out of memory");
142 /* Input file handling. */
144 /* Table of all input files. */
145 const input_file **gt_files;
148 /* A number of places use the name of this "gengtype.c" file for a
149 location for things that we can't rely on the source to define.
150 Make sure we can still use pointer comparison on filenames. */
151 input_file* this_file;
152 /* The "system.h" file is likewise specially useful. */
153 input_file* system_h_file;
155 /* Vector of per-language directories. */
156 const char **lang_dir_names;
157 size_t num_lang_dirs;
159 /* An array of output files suitable for definitions. There is one
160 BASE_FILES entry for each language. */
161 static outf_p *base_files;
166 /* Utility debugging function, printing the various type counts within
167 a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
169 dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
171 int nb_types = 0, nb_scalar = 0, nb_string = 0;
172 int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
173 int nb_lang_struct = 0, nb_param_struct = 0;
174 int nb_user_struct = 0;
176 for (p = t; p; p = p->next)
190 case TYPE_USER_STRUCT:
202 case TYPE_LANG_STRUCT:
205 case TYPE_PARAM_STRUCT:
212 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
213 lbasename (fil), lin, msg, nb_types);
214 if (nb_scalar > 0 || nb_string > 0)
215 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
216 if (nb_struct > 0 || nb_union > 0)
217 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
218 if (nb_pointer > 0 || nb_array > 0)
219 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
220 if (nb_lang_struct > 0 || nb_param_struct > 0)
221 fprintf (stderr, "@@%%@@ %d lang_structs, %d param_structs\n",
222 nb_lang_struct, nb_param_struct);
223 if (nb_user_struct > 0)
224 fprintf (stderr, "@@%%@@ %d user_structs\n", nb_user_struct);
225 fprintf (stderr, "\n");
227 #endif /* ENABLE_CHECKING */
229 /* Scan the input file, LIST, and determine how much space we need to
230 store strings in. Also, count the number of language directories
231 and files. The numbers returned are overestimates as they does not
232 consider repeated files. */
234 measure_input_list (FILE *list)
240 num_gt_files = plugin_files ? nb_plugin_files : 0;
241 while ((c = getc (list)) != EOF)
250 /* Add space for a lang_bitmap before the input file name. */
251 n += sizeof (lang_bitmap);
265 /* Read one input line from LIST to HEREP (which is updated). A
266 pointer to the string is returned via LINEP. If it was a language
267 subdirectory in square brackets, strip off the square brackets and
268 return true. Otherwise, leave space before the string for a
269 lang_bitmap, and return false. At EOF, returns false, does not
270 touch *HEREP, and sets *LINEP to NULL. POS is used for
273 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
279 /* Read over whitespace. */
280 while (c == '\n' || c == ' ')
290 /* No space for a lang_bitmap is necessary. Discard the '['. */
293 while (c != ']' && c != '\n' && c != EOF)
302 c = getc (list); /* eat what should be a newline */
303 if (c != '\n' && c != EOF)
304 error_at_line (pos, "junk on line after language tag [%s]", line);
307 error_at_line (pos, "missing close bracket for language tag [%s",
316 /* Leave space for a lang_bitmap. */
317 memset (here, 0, sizeof (lang_bitmap));
318 here += sizeof (lang_bitmap);
325 while (c != EOF && c != '\n');
333 /* Read the list of input files from LIST and compute all of the
334 relevant tables. There is one file per line of the list. At
335 first, all the files on the list are language-generic, but
336 eventually a line will appear which is the name of a language
337 subdirectory in square brackets, like this: [cp]. All subsequent
338 files are specific to that language, until another language
339 subdirectory tag appears. Files can appear more than once, if
340 they apply to more than one language. */
342 read_input_list (const char *listname)
344 FILE *list = fopen (listname, "r");
346 fatal ("cannot open %s: %s", listname, xstrerror (errno));
350 size_t bufsz = measure_input_list (list);
351 char *buf = XNEWVEC (char, bufsz);
353 char *committed = buf;
354 char *limit = buf + bufsz;
359 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
361 epos.file = input_file_by_name (listname);
364 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
365 gt_files = XNEWVEC (const input_file *, num_gt_files);
372 is_language = read_input_line (list, &here, &line, &epos);
373 gcc_assert (here <= limit);
376 else if (is_language)
379 gcc_assert (langno <= num_lang_dirs);
380 for (i = 0; i < langno; i++)
381 if (strcmp (lang_dir_names[i], line) == 0)
383 error_at_line (&epos, "duplicate language tag [%s]",
390 curlangs = 1 << langno;
391 lang_dir_names[langno++] = line;
396 input_file *inpf = input_file_by_name (line);
397 gcc_assert (nfiles <= num_gt_files);
398 for (i = 0; i < nfiles; i++)
399 /* Since the input_file-s are uniquely hash-consed, we
400 can just compare pointers! */
401 if (gt_files[i] == inpf)
403 /* Throw away the string we just read, and add the
404 current language to the existing string's bitmap. */
405 lang_bitmap bmap = get_lang_bitmap (inpf);
407 error_at_line (&epos,
408 "file %s specified more than once "
409 "for language %s", line,
411 0 ? "(all)" : lang_dir_names[langno -
415 set_lang_bitmap (inpf, bmap);
420 set_lang_bitmap (inpf, curlangs);
421 gt_files[nfiles++] = inpf;
424 /* Update the global counts now that we know accurately how many
425 things there are. (We do not bother resizing the arrays down.) */
426 num_lang_dirs = langno;
427 /* Add the plugin files if provided. */
431 for (i = 0; i < nb_plugin_files; i++)
432 gt_files[nfiles++] = plugin_files[i];
434 num_gt_files = nfiles;
437 /* Sanity check: any file that resides in a language subdirectory
438 (e.g. 'cp') ought to belong to the corresponding language.
439 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
440 (Can you even do that? Should you be allowed to?) */
443 for (f = 0; f < num_gt_files; f++)
445 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
446 const char *basename = get_file_basename (gt_files[f]);
447 const char *slashpos = strchr (basename, '/');
448 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
449 const char *slashpos2 = strchr (basename, '\\');
451 if (!slashpos || (slashpos2 && slashpos2 < slashpos))
452 slashpos = slashpos2;
458 for (l = 0; l < num_lang_dirs; l++)
459 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
460 && memcmp (basename, lang_dir_names[l],
461 strlen (lang_dir_names[l])) == 0)
463 if (!(bitmap & (1 << l)))
464 error ("%s is in language directory '%s' but is not "
465 "tagged for that language",
466 basename, lang_dir_names[l]);
474 fatal ("error reading %s: %s", listname, xstrerror (errno));
481 /* The one and only TYPE_STRING. */
483 struct type string_type = {
484 TYPE_STRING, 0, 0, 0, GC_USED, {0}
487 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
488 set early in main. */
490 struct type scalar_nonchar = {
491 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
494 struct type scalar_char = {
495 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
498 /* Lists of various things. */
502 type_p param_structs;
505 static type_p find_param_structure (type_p t, type_p param[NUM_PARAM]);
506 static type_p adjust_field_tree_exp (type_p t, options_p opt);
507 static type_p adjust_field_rtx_def (type_p t, options_p opt);
509 /* Define S as a typedef to T at POS. */
512 do_typedef (const char *s, type_p t, struct fileloc *pos)
516 /* temporary kludge - gengtype doesn't handle conditionals or
517 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
518 is coming from this file (main() sets them up with safe dummy
520 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
523 for (p = typedefs; p != NULL; p = p->next)
524 if (strcmp (p->name, s) == 0)
528 error_at_line (pos, "type `%s' previously defined", s);
529 error_at_line (&p->line, "previously defined here");
534 p = XNEW (struct pair);
543 /* Define S as a typename of a scalar. Cannot be used to define
544 typedefs of 'char'. Note: is also used for pointer-to-function
545 typedefs (which are therefore not treated as pointers). */
548 do_scalar_typedef (const char *s, struct fileloc *pos)
550 do_typedef (s, &scalar_nonchar, pos);
554 /* Define TYPE_NAME to be a user defined type at location POS. */
557 create_user_defined_type (const char *type_name, struct fileloc *pos)
559 type_p ty = find_structure (type_name, TYPE_USER_STRUCT);
561 ty->u.s.bitmap = get_lang_bitmap (pos->file);
562 do_typedef (type_name, ty, pos);
564 /* If TYPE_NAME specifies a template, create references to the types
565 in the template by pretending that each type is a field of TY.
566 This is needed to make sure that the types referenced by the
567 template are marked as used. */
568 char *str = xstrdup (type_name);
569 char *open_bracket = strchr (str, '<');
572 /* We only accept simple template declarations (see
573 require_template_declaration), so we only need to parse a
574 comma-separated list of strings, implicitly assumed to
576 char *arg = open_bracket + 1;
577 char *type_id = strtok (arg, ",>");
581 /* Create a new field for every type found inside the template
583 const char *field_name = xstrdup (type_id);
584 type_p arg_type = resolve_typedef (field_name, pos);
585 fields = create_field_at (fields, arg_type, field_name, 0, pos);
586 type_id = strtok (0, ",>");
589 /* Associate the field list to TY. */
590 ty->u.s.fields = fields;
598 /* Return the type previously defined for S. Use POS to report errors. */
601 resolve_typedef (const char *s, struct fileloc *pos)
604 for (p = typedefs; p != NULL; p = p->next)
605 if (strcmp (p->name, s) == 0)
608 /* If we did not find a typedef registered, assume this is a name
609 for a user-defined type which will need to provide its own
610 marking functions. */
611 return create_user_defined_type (s, pos);
614 /* Create and return a new structure with tag NAME at POS with fields
615 FIELDS and options O. The KIND of structure must be one of
616 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
619 new_structure (const char *name, enum typekind kind, struct fileloc *pos,
620 pair_p fields, options_p o)
624 lang_bitmap bitmap = get_lang_bitmap (pos->file);
625 bool isunion = (kind == TYPE_UNION);
627 gcc_assert (union_or_struct_p (kind));
629 for (si = structures; si != NULL; si = si->next)
630 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
633 if (si->kind == TYPE_LANG_STRUCT)
637 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
638 if (si->u.s.bitmap == bitmap)
641 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
645 si = XCNEW (struct type);
646 memcpy (si, ls, sizeof (struct type));
647 ls->kind = TYPE_LANG_STRUCT;
648 ls->u.s.lang_struct = si;
649 ls->u.s.fields = NULL;
651 si->state_number = -type_count;
652 si->pointer_to = NULL;
653 si->u.s.lang_struct = ls;
658 if (ls != NULL && s == NULL)
661 s = XCNEW (struct type);
662 s->state_number = -type_count;
663 s->next = ls->u.s.lang_struct;
664 ls->u.s.lang_struct = s;
665 s->u.s.lang_struct = ls;
673 s = XCNEW (struct type);
674 s->state_number = -type_count;
675 s->next = structures;
679 if (s->u.s.line.file != NULL
680 || (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap)))
682 error_at_line (pos, "duplicate definition of '%s %s'",
683 isunion ? "union" : "struct", s->u.s.tag);
684 error_at_line (&s->u.s.line, "previous definition here");
690 s->u.s.fields = fields;
692 s->u.s.bitmap = bitmap;
693 if (s->u.s.lang_struct)
694 s->u.s.lang_struct->u.s.bitmap |= bitmap;
699 /* Return the previously-defined structure or union with tag NAME,
700 or a new empty structure or union if none was defined previously.
701 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
705 find_structure (const char *name, enum typekind kind)
708 bool isunion = (kind == TYPE_UNION);
710 gcc_assert (union_or_struct_p (kind));
712 for (s = structures; s != NULL; s = s->next)
713 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
717 s = XCNEW (struct type);
718 s->next = structures;
719 s->state_number = -type_count;
727 /* Return the previously-defined parameterized structure for structure
728 T and parameters PARAM, or a new parameterized empty structure or
729 union if none was defined previously. */
732 find_param_structure (type_p t, type_p param[NUM_PARAM])
736 for (res = param_structs; res; res = res->next)
737 if (res->u.param_struct.stru == t
738 && memcmp (res->u.param_struct.param, param,
739 sizeof (type_p) * NUM_PARAM) == 0)
744 res = XCNEW (struct type);
745 res->kind = TYPE_PARAM_STRUCT;
746 res->next = param_structs;
747 res->state_number = -type_count;
749 res->u.param_struct.stru = t;
750 memcpy (res->u.param_struct.param, param, sizeof (type_p) * NUM_PARAM);
755 /* Return a scalar type with name NAME. */
758 create_scalar_type (const char *name)
760 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
763 return &scalar_nonchar;
766 /* Return a pointer to T. */
769 create_pointer (type_p t)
773 type_p r = XCNEW (struct type);
775 r->state_number = -type_count;
776 r->kind = TYPE_POINTER;
780 return t->pointer_to;
783 /* Return an array of length LEN. */
786 create_array (type_p t, const char *len)
791 v = XCNEW (struct type);
792 v->kind = TYPE_ARRAY;
793 v->state_number = -type_count;
799 /* Return a string options structure with name NAME and info INFO.
800 NEXT is the next option in the chain. */
802 create_string_option (options_p next, const char *name, const char *info)
804 options_p o = XNEW (struct options);
805 o->kind = OPTION_STRING;
808 o->info.string = info;
812 /* Create a type options structure with name NAME and info INFO. NEXT
813 is the next option in the chain. */
815 create_type_option (options_p next, const char* name, type_p info)
817 options_p o = XNEW (struct options);
820 o->kind = OPTION_TYPE;
825 /* Create a nested pointer options structure with name NAME and info
826 INFO. NEXT is the next option in the chain. */
828 create_nested_option (options_p next, const char* name,
829 struct nested_ptr_data* info)
832 o = XNEW (struct options);
835 o->kind = OPTION_NESTED;
836 o->info.nested = info;
840 /* Return an options structure for a "nested_ptr" option. */
842 create_nested_ptr_option (options_p next, type_p t,
843 const char *to, const char *from)
845 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
847 d->type = adjust_field_type (t, 0);
849 d->convert_from = from;
850 return create_nested_option (next, "nested_ptr", d);
853 /* Add a variable named S of type T with options O defined at POS,
856 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
859 n = XNEW (struct pair);
868 /* Most-general structure field creator. */
870 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
871 const input_file *inpf, int line)
875 field = XNEW (struct pair);
880 field->line.file = inpf;
881 field->line.line = line;
885 /* Create a field that came from the source code we are scanning,
886 i.e. we have a 'struct fileloc', and possibly options; also,
887 adjust_field_type should be called. */
889 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
892 return create_field_all (next, adjust_field_type (type, opt),
893 name, opt, pos->file, pos->line);
896 /* Create a fake field with the given type and name. NEXT is the next
897 field in the chain. */
898 #define create_field(next,type,name) \
899 create_field_all(next,type,name, 0, this_file, __LINE__)
901 /* Like create_field, but the field is only valid when condition COND
905 create_optional_field_ (pair_p next, type_p type, const char *name,
906 const char *cond, int line)
912 /* Create a fake union type with a single nameless field of type TYPE.
913 The field has a tag of "1". This allows us to make the presence
914 of a field of type TYPE depend on some boolean "desc" being true. */
915 union_fields = create_field (NULL, type, "");
917 create_string_option (union_fields->opt, "dot", "");
919 create_string_option (union_fields->opt, "tag", "1");
921 new_structure (xasprintf ("%s_%d", "fake_union", id++), TYPE_UNION,
922 &lexer_line, union_fields, NULL);
924 /* Create the field and give it the new fake union type. Add a "desc"
925 tag that specifies the condition under which the field is valid. */
926 return create_field_all (next, union_type, name,
927 create_string_option (0, "desc", cond),
931 #define create_optional_field(next,type,name,cond) \
932 create_optional_field_(next,type,name,cond,__LINE__)
934 /* Reverse a linked list of 'struct pair's in place. */
936 nreverse_pairs (pair_p list)
938 pair_p prev = 0, p, next;
939 for (p = list; p; p = next)
949 /* We don't care how long a CONST_DOUBLE is. */
950 #define CONST_DOUBLE_FORMAT "ww"
951 /* We don't want to see codes that are only for generator files. */
952 #undef GENERATOR_FILE
956 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
962 static const char *const rtx_name[NUM_RTX_CODE] = {
963 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
968 static const char *const rtx_format[NUM_RTX_CODE] = {
969 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
974 static int rtx_next_new[NUM_RTX_CODE];
976 /* We also need codes and names for insn notes (not register notes).
977 Note that we do *not* bias the note values here. */
980 #define DEF_INSN_NOTE(NAME) NAME,
981 #include "insn-notes.def"
987 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
988 default field for line number notes. */
989 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
990 #define DEF_INSN_NOTE(NAME) #NAME,
991 #include "insn-notes.def"
995 #undef CONST_DOUBLE_FORMAT
996 #define GENERATOR_FILE
998 /* Generate the contents of the rtx_next array. This really doesn't belong
999 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1005 for (i = 0; i < NUM_RTX_CODE; i++)
1009 rtx_next_new[i] = -1;
1010 if (strncmp (rtx_format[i], "iuu", 3) == 0)
1011 rtx_next_new[i] = 2;
1012 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1013 rtx_next_new[i] = 1;
1015 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
1016 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1017 rtx_next_new[i] = k;
1021 /* Write out the contents of the rtx_next array. */
1023 write_rtx_next (void)
1025 outf_p f = get_output_file_with_visibility (NULL);
1030 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
1031 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1032 for (i = 0; i < NUM_RTX_CODE; i++)
1033 if (rtx_next_new[i] == -1)
1034 oprintf (f, " 0,\n");
1037 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1038 oprintf (f, "};\n");
1041 /* Handle `special("rtx_def")'. This is a special case for field
1042 `fld' of struct rtx_def, which is an array of unions whose values
1043 are based in a complex way on the type of RTL. */
1046 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1051 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1052 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1054 if (t->kind != TYPE_UNION)
1056 error_at_line (&lexer_line,
1057 "special `rtx_def' must be applied to a union");
1058 return &string_type;
1061 nodot = create_string_option (NULL, "dot", "");
1063 rtx_tp = create_pointer (find_structure ("rtx_def", TYPE_STRUCT));
1064 rtvec_tp = create_pointer (find_structure ("rtvec_def", TYPE_STRUCT));
1065 tree_tp = create_pointer (find_structure ("tree_node", TYPE_UNION));
1066 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", TYPE_STRUCT));
1068 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT));
1070 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT));
1072 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT));
1073 scalar_tp = &scalar_nonchar; /* rtunion int */
1076 pair_p note_flds = NULL;
1079 for (c = 0; c <= NOTE_INSN_MAX; c++)
1084 case NOTE_INSN_DELETED_LABEL:
1085 case NOTE_INSN_DELETED_DEBUG_LABEL:
1086 note_flds = create_field (note_flds, &string_type, "rt_str");
1089 case NOTE_INSN_BLOCK_BEG:
1090 case NOTE_INSN_BLOCK_END:
1091 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1094 case NOTE_INSN_VAR_LOCATION:
1095 case NOTE_INSN_CALL_ARG_LOCATION:
1096 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1100 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1103 /* NOTE_INSN_MAX is used as the default field for line
1105 if (c == NOTE_INSN_MAX)
1107 create_string_option (nodot, "default", "");
1110 create_string_option (nodot, "tag", note_insn_name[c]);
1112 note_union_tp = new_structure ("rtx_def_note_subunion", TYPE_UNION,
1113 &lexer_line, note_flds, NULL);
1115 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1118 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1119 sym_flds->opt = create_string_option (nodot, "default", "");
1120 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1121 sym_flds->opt = create_string_option (nodot, "tag", "1");
1122 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", TYPE_UNION,
1123 &lexer_line, sym_flds, NULL);
1125 for (i = 0; i < NUM_RTX_CODE; i++)
1127 pair_p subfields = NULL;
1128 size_t aindex, nmindex;
1133 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1136 const char *subname;
1138 switch (rtx_format[i][aindex])
1149 if (i == MEM && aindex == 1)
1150 t = mem_attrs_tp, subname = "rt_mem";
1151 else if (i == JUMP_INSN && aindex == 8)
1152 t = rtx_tp, subname = "rt_rtx";
1153 else if (i == CODE_LABEL && aindex == 5)
1154 t = scalar_tp, subname = "rt_int";
1155 else if (i == CODE_LABEL && aindex == 4)
1156 t = rtx_tp, subname = "rt_rtx";
1157 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1158 t = rtx_tp, subname = "rt_rtx";
1159 else if (i == NOTE && aindex == 4)
1160 t = note_union_tp, subname = "";
1161 else if (i == NOTE && aindex == 5)
1162 t = scalar_tp, subname = "rt_int";
1163 else if (i == NOTE && aindex >= 7)
1164 t = scalar_tp, subname = "rt_int";
1165 else if (i == ADDR_DIFF_VEC && aindex == 4)
1166 t = scalar_tp, subname = "rt_int";
1167 else if (i == VALUE && aindex == 0)
1168 t = scalar_tp, subname = "rt_int";
1169 else if (i == DEBUG_EXPR && aindex == 0)
1170 t = tree_tp, subname = "rt_tree";
1171 else if (i == REG && aindex == 1)
1172 t = scalar_tp, subname = "rt_int";
1173 else if (i == REG && aindex == 2)
1174 t = reg_attrs_tp, subname = "rt_reg";
1175 else if (i == SCRATCH && aindex == 0)
1176 t = scalar_tp, subname = "rt_int";
1177 else if (i == SYMBOL_REF && aindex == 1)
1178 t = scalar_tp, subname = "rt_int";
1179 else if (i == SYMBOL_REF && aindex == 2)
1180 t = symbol_union_tp, subname = "";
1181 else if (i == BARRIER && aindex >= 3)
1182 t = scalar_tp, subname = "rt_int";
1183 else if (i == ENTRY_VALUE && aindex == 0)
1184 t = rtx_tp, subname = "rt_rtx";
1189 "rtx type `%s' has `0' in position %lu, can't handle",
1190 rtx_name[i], (unsigned long) aindex);
1212 subname = "rt_rtvec";
1217 subname = "rt_tree";
1228 "rtx type `%s' has `%c' in position %lu, can't handle",
1229 rtx_name[i], rtx_format[i][aindex],
1230 (unsigned long) aindex);
1236 subfields = create_field (subfields, t,
1237 xasprintf (".fld[%lu].%s",
1238 (unsigned long) aindex,
1240 subfields->opt = nodot;
1241 if (t == note_union_tp)
1243 create_string_option (subfields->opt, "desc",
1245 if (t == symbol_union_tp)
1247 create_string_option (subfields->opt, "desc",
1248 "CONSTANT_POOL_ADDRESS_P (&%0)");
1251 if (i == SYMBOL_REF)
1253 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1255 type_p field_tp = find_structure ("block_symbol", TYPE_STRUCT);
1257 = create_optional_field (subfields, field_tp, "block_sym",
1258 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1261 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1262 substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
1265 ftag = xstrdup (rtx_name[i]);
1266 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1267 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1268 flds = create_field (flds, substruct, "");
1269 flds->opt = create_string_option (nodot, "tag", ftag);
1271 return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
1275 /* Handle `special("tree_exp")'. This is a special case for
1276 field `operands' of struct tree_exp, which although it claims to contain
1277 pointers to trees, actually sometimes contains pointers to RTL too.
1278 Passed T, the old type of the field, and OPT its options. Returns
1279 a new type for the field. */
1282 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1287 if (t->kind != TYPE_ARRAY)
1289 error_at_line (&lexer_line,
1290 "special `tree_exp' must be applied to an array");
1291 return &string_type;
1294 nodot = create_string_option (NULL, "dot", "");
1296 flds = create_field (NULL, t, "");
1297 flds->opt = create_string_option (nodot, "length",
1298 "TREE_OPERAND_LENGTH ((tree) &%0)");
1299 flds->opt = create_string_option (flds->opt, "default", "");
1301 return new_structure ("tree_exp_subunion", TYPE_UNION, &lexer_line, flds,
1305 /* Perform any special processing on a type T, about to become the type
1306 of a field. Return the appropriate type for the field.
1308 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1309 - Similarly for arrays of pointer-to-char;
1310 - Converts structures for which a parameter is provided to
1312 - Handles "special" options.
1316 adjust_field_type (type_p t, options_p opt)
1319 const int pointer_p = t->kind == TYPE_POINTER;
1320 type_p params[NUM_PARAM];
1324 for (i = 0; i < NUM_PARAM; i++)
1327 for (; opt; opt = opt->next)
1328 if (strcmp (opt->name, "length") == 0)
1331 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1332 if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
1334 error_at_line (&lexer_line,
1335 "option `%s' may not be applied to "
1336 "arrays of atomic types", opt->name);
1340 else if ((strcmp (opt->name, "param_is") == 0
1341 || (strncmp (opt->name, "param", 5) == 0
1342 && ISDIGIT (opt->name[5])
1343 && strcmp (opt->name + 6, "_is") == 0))
1344 && opt->kind == OPTION_TYPE)
1346 int num = ISDIGIT (opt->name[5]) ? opt->name[5] - '0' : 0;
1348 if (!union_or_struct_p (t)
1349 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
1351 error_at_line (&lexer_line,
1352 "option `%s' may only be applied to structures or structure pointers",
1358 if (params[num] != NULL)
1359 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1360 if (!ISDIGIT (opt->name[5]))
1361 params[num] = create_pointer (opt->info.type);
1363 params[num] = opt->info.type;
1365 else if (strcmp (opt->name, "special") == 0
1366 && opt->kind == OPTION_STRING)
1368 const char *special_name = opt->info.string;
1369 if (strcmp (special_name, "tree_exp") == 0)
1370 t = adjust_field_tree_exp (t, opt);
1371 else if (strcmp (special_name, "rtx_def") == 0)
1372 t = adjust_field_rtx_def (t, opt);
1374 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1383 realt = find_param_structure (t, params);
1384 t = pointer_p ? create_pointer (realt) : realt;
1388 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1389 return &string_type;
1390 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1391 && t->u.a.p->u.p->kind == TYPE_SCALAR
1392 && t->u.a.p->u.p->u.scalar_is_char)
1393 return create_array (&string_type, t->u.a.len);
1399 static void set_gc_used_type (type_p, enum gc_used_enum, type_p *);
1400 static void set_gc_used (pair_p);
1402 /* Handle OPT for set_gc_used_type. */
1405 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1406 int *pass_param, int *length, int *skip,
1410 for (o = opt; o; o = o->next)
1411 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1412 && o->kind == OPTION_TYPE)
1413 set_gc_used_type (o->info.type,
1414 GC_POINTED_TO, NULL);
1415 else if (strcmp (o->name, "maybe_undef") == 0)
1417 else if (strcmp (o->name, "use_params") == 0)
1419 else if (strcmp (o->name, "length") == 0)
1421 else if (strcmp (o->name, "skip") == 0)
1423 else if (strcmp (o->name, "nested_ptr") == 0
1424 && o->kind == OPTION_NESTED)
1425 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1429 /* Set the gc_used field of T to LEVEL, and handle the types it references. */
1431 set_gc_used_type (type_p t, enum gc_used_enum level, type_p param[NUM_PARAM])
1433 if (t->gc_used >= level)
1442 case TYPE_USER_STRUCT:
1448 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
1451 for (f = t->u.s.fields; f; f = f->next)
1453 int maybe_undef = 0;
1457 type_p nested_ptr = NULL;
1458 process_gc_options (f->opt, level, &maybe_undef, &pass_param,
1459 &length, &skip, &nested_ptr);
1461 if (nested_ptr && f->type->kind == TYPE_POINTER)
1462 set_gc_used_type (nested_ptr, GC_POINTED_TO,
1463 pass_param ? param : NULL);
1464 else if (length && f->type->kind == TYPE_POINTER)
1465 set_gc_used_type (f->type->u.p, GC_USED, NULL);
1466 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1467 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO, NULL);
1468 else if (pass_param && f->type->kind == TYPE_POINTER && param)
1469 set_gc_used_type (find_param_structure (f->type->u.p, param),
1470 GC_POINTED_TO, NULL);
1472 ; /* target type is not used through this field */
1474 set_gc_used_type (f->type, GC_USED, pass_param ? param : NULL);
1480 set_gc_used_type (t->u.p, GC_POINTED_TO, NULL);
1484 set_gc_used_type (t->u.a.p, GC_USED, param);
1487 case TYPE_LANG_STRUCT:
1488 for (t = t->u.s.lang_struct; t; t = t->next)
1489 set_gc_used_type (t, level, param);
1492 case TYPE_PARAM_STRUCT:
1495 for (i = 0; i < NUM_PARAM; i++)
1496 if (t->u.param_struct.param[i] != 0)
1497 set_gc_used_type (t->u.param_struct.param[i], GC_USED, NULL);
1499 if (t->u.param_struct.stru->gc_used == GC_POINTED_TO)
1500 level = GC_POINTED_TO;
1503 t->u.param_struct.stru->gc_used = GC_UNUSED;
1504 set_gc_used_type (t->u.param_struct.stru, level,
1505 t->u.param_struct.param);
1513 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1516 set_gc_used (pair_p variables)
1520 for (p = variables; p; p = p->next)
1522 set_gc_used_type (p->type, GC_USED, NULL);
1525 if (verbosity_level >= 2)
1526 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1529 /* File mapping routines. For each input file, there is one output .c file
1530 (but some output files have many input files), and there is one .h file
1531 for the whole build. */
1533 /* Output file handling. */
1535 /* Create and return an outf_p for a new file for NAME, to be called
1539 create_file (const char *name, const char *oname)
1541 static const char *const hdr[] = {
1542 " Copyright (C) 2004, 2007, 2009, 2012 Free Software Foundation, Inc.\n",
1544 "This file is part of GCC.\n",
1546 "GCC is free software; you can redistribute it and/or modify it under\n",
1547 "the terms of the GNU General Public License as published by the Free\n",
1548 "Software Foundation; either version 3, or (at your option) any later\n",
1551 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1552 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1553 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1554 "for more details.\n",
1556 "You should have received a copy of the GNU General Public License\n",
1557 "along with GCC; see the file COPYING3. If not see\n",
1558 "<http://www.gnu.org/licenses/>. */\n",
1560 "/* This file is machine generated. Do not edit. */\n"
1565 gcc_assert (name != NULL);
1566 gcc_assert (oname != NULL);
1567 f = XCNEW (struct outf);
1568 f->next = output_files;
1572 oprintf (f, "/* Type information for %s.\n", name);
1573 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1574 oprintf (f, "%s", hdr[i]);
1578 /* Print, like fprintf, to O.
1579 N.B. You might think this could be implemented more efficiently
1580 with vsnprintf(). Unfortunately, there are C libraries that
1581 provide that function but without the C99 semantics for its return
1582 value, making it impossible to know how much space is required. */
1584 oprintf (outf_p o, const char *format, ...)
1590 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1595 va_start (ap, format);
1596 slength = vasprintf (&s, format, ap);
1597 if (s == NULL || (int) slength < 0)
1598 fatal ("out of memory");
1601 if (o->bufused + slength > o->buflength)
1603 size_t new_len = o->buflength;
1610 while (o->bufused + slength >= new_len);
1611 o->buf = XRESIZEVEC (char, o->buf, new_len);
1612 o->buflength = new_len;
1614 memcpy (o->buf + o->bufused, s, slength);
1615 o->bufused += slength;
1619 /* Open the global header file and the language-specific header files. */
1622 open_base_files (void)
1626 if (nb_plugin_files > 0 && plugin_files)
1629 header_file = create_file ("GCC", "gtype-desc.h");
1631 base_files = XNEWVEC (outf_p, num_lang_dirs);
1633 for (i = 0; i < num_lang_dirs; i++)
1634 base_files[i] = create_file (lang_dir_names[i],
1635 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1637 /* gtype-desc.c is a little special, so we create it here. */
1639 /* The order of files here matters very much. */
1640 static const char *const ifiles[] = {
1641 "config.h", "system.h", "coretypes.h", "tm.h",
1642 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1643 "tree.h", "rtl.h", "function.h", "insn-config.h", "expr.h",
1644 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1645 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1646 "tree-flow.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1647 "except.h", "output.h", "gimple.h", "cfgloop.h",
1648 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h",
1649 "ipa-inline.h", "dwarf2out.h", NULL
1651 const char *const *ifp;
1652 outf_p gtype_desc_c;
1654 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1655 for (ifp = ifiles; *ifp; ifp++)
1656 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1658 /* Make sure we handle "cfun" specially. */
1659 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1660 oprintf (gtype_desc_c, "#undef cfun\n");
1664 /* For INPF an input file, return the real basename of INPF, with all
1665 the directory components skipped. */
1668 get_file_realbasename (const input_file *inpf)
1670 return lbasename (get_input_file_name (inpf));
1673 /* For INPF a filename, return the relative path to INPF from
1674 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1677 get_file_srcdir_relative_path (const input_file *inpf)
1679 const char *f = get_input_file_name (inpf);
1680 if (strlen (f) > srcdir_len
1681 && IS_DIR_SEPARATOR (f[srcdir_len])
1682 && strncmp (f, srcdir, srcdir_len) == 0)
1683 return f + srcdir_len + 1;
1688 /* For INPF an input_file, return the relative path to INPF from
1689 $(srcdir) if the latter is a prefix in INPF, or the real basename
1690 of INPF otherwise. */
1693 get_file_basename (const input_file *inpf)
1695 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1697 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1700 /* For F a filename, return the lang_dir_names relative index of the language
1701 directory that is a prefix in F, if any, -1 otherwise. */
1704 get_prefix_langdir_index (const char *f)
1706 size_t f_len = strlen (f);
1709 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1711 const char *langdir = lang_dir_names[lang_index];
1712 size_t langdir_len = strlen (langdir);
1714 if (f_len > langdir_len
1715 && IS_DIR_SEPARATOR (f[langdir_len])
1716 && memcmp (f, langdir, langdir_len) == 0)
1723 /* For INPF an input file, return the name of language directory where
1724 F is located, if any, NULL otherwise. */
1727 get_file_langdir (const input_file *inpf)
1729 /* Get the relative path to INPF from $(srcdir) and find the
1730 language by comparing the prefix with language directory names.
1731 If INPF is not even srcdir relative, no point in looking
1735 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1738 if (!srcdir_relative_path)
1741 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1742 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1744 else if (lang_index >= 0)
1745 r = lang_dir_names[lang_index];
1752 /* The gt- output file name for INPF. */
1755 get_file_gtfilename (const input_file *inpf)
1757 /* Cook up an initial version of the gt- file name from the file real
1758 basename and the language name, if any. */
1760 const char *basename = get_file_realbasename (inpf);
1761 const char *langdir = get_file_langdir (inpf);
1764 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1765 : xasprintf ("gt-%s", basename));
1767 /* Then replace all non alphanumerics characters by '-' and change the
1768 extension to ".h". We expect the input filename extension was at least
1769 one character long. */
1773 for (; *s != '.'; s++)
1774 if (!ISALNUM (*s) && *s != '-')
1777 memcpy (s, ".h", sizeof (".h"));
1782 /* Each input_file has its associated output file outf_p. The
1783 association is computed by the function
1784 get_output_file_with_visibility. The associated file is cached
1785 inside input_file in its inpoutf field, so is really computed only
1786 once. Associated output file paths (i.e. output_name-s) are
1787 computed by a rule based regexp machinery, using the files_rules
1788 array of struct file_rule_st. A for_name is also computed, giving
1789 the source file name for which the output_file is generated; it is
1790 often the last component of the input_file path. */
1794 Regexpr machinery to compute the output_name and for_name-s of each
1795 input_file. We have a sequence of file rules which gives the POSIX
1796 extended regular expression to match an input file path, and two
1797 transformed strings for the corresponding output_name and the
1798 corresponding for_name. The transformed string contain dollars: $0
1799 is replaced by the entire match, $1 is replaced by the substring
1800 matching the first parenthesis in the regexp, etc. And $$ is replaced
1801 by a single verbatim dollar. The rule order is important. The
1802 general case is last, and the particular cases should come before.
1803 An action routine can, when needed, update the out_name & for_name
1804 and/or return the appropriate output file. It is invoked only when a
1805 rule is triggered. When a rule is triggered, the output_name and
1806 for_name are computed using their transform string in while $$, $0,
1807 $1, ... are suitably replaced. If there is an action, it is called.
1808 In some few cases, the action can directly return the outf_p, but
1809 usually it just updates the output_name and for_name so should free
1810 them before replacing them. The get_output_file_with_visibility
1811 function creates an outf_p only once per each output_name, so it
1812 scans the output_files list for previously seen output file names.
1815 /* Signature of actions in file rules. */
1816 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
1819 struct file_rule_st {
1820 const char* frul_srcexpr; /* Source string for regexp. */
1821 int frul_rflags; /* Flags passed to regcomp, usually
1823 regex_t* frul_re; /* Compiled regular expression
1824 obtained by regcomp. */
1825 const char* frul_tr_out; /* Transformation string for making
1826 * the output_name, with $1 ... $9 for
1827 * subpatterns and $0 for the whole
1828 * matched filename. */
1829 const char* frul_tr_for; /* Tranformation string for making the
1831 frul_actionrout_t* frul_action; /* The action, if non null, is
1832 * called once the rule matches, on
1833 * the transformed out_name &
1834 * for_name. It could change them
1835 * and/or give the output file. */
1838 /* File rule action handling *.h files. */
1839 static outf_p header_dot_h_frul (input_file*, char**, char**);
1841 /* File rule action handling *.c files. */
1842 static outf_p source_dot_c_frul (input_file*, char**, char**);
1844 #define NULL_REGEX (regex_t*)0
1846 /* The prefix in our regexp-s matching the directory. */
1847 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1849 #define NULL_FRULACT (frul_actionrout_t*)0
1851 /* The array of our rules governing file name generation. Rules order
1852 matters, so change with extreme care! */
1854 struct file_rule_st files_rules[] = {
1855 /* The general rule assumes that files in subdirectories belong to a
1856 particular front-end, and files not in subdirectories are shared.
1857 The following rules deal with exceptions - files that are in
1858 subdirectories and yet are shared, and files that are top-level,
1859 but are not shared. */
1861 /* the c-family/ source directory is special. */
1862 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
1863 REG_EXTENDED, NULL_REGEX,
1864 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
1866 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
1867 REG_EXTENDED, NULL_REGEX,
1868 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
1870 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
1871 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
1872 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1874 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
1875 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1877 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
1878 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
1879 REG_EXTENDED, NULL_REGEX,
1880 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
1882 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
1883 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
1884 REG_EXTENDED, NULL_REGEX,
1885 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
1887 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
1888 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
1889 REG_EXTENDED, NULL_REGEX,
1890 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
1892 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
1893 { DIR_PREFIX_REGEX "cp/parser\\.h$",
1894 REG_EXTENDED, NULL_REGEX,
1895 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
1897 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
1898 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
1899 REG_EXTENDED, NULL_REGEX,
1900 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
1902 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
1903 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
1904 REG_EXTENDED, NULL_REGEX,
1905 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
1907 /* General cases. For header *.h and source *.c files, we need
1908 * special actions to handle the language. */
1910 /* Source *.c files are using get_file_gtfilename to compute their
1911 output_name and get_file_basename to compute their for_name
1912 through the source_dot_c_frul action. */
1913 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
1914 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
1915 /* Common header files get "gtype-desc.c" as their output_name,
1916 * while language specific header files are handled specially. So
1917 * we need the header_dot_h_frul action. */
1918 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
1919 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
1921 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
1922 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
1924 /* Mandatory null last entry signaling end of rules. */
1925 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
1928 /* Special file rules action for handling *.h header files. It gives
1929 "gtype-desc.c" for common headers and corresponding output
1930 files for language-specific header files. */
1932 header_dot_h_frul (input_file* inpf, char**poutname,
1933 char**pforname ATTRIBUTE_UNUSED)
1935 const char *basename = 0;
1937 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
1938 (void*) inpf, get_input_file_name (inpf),
1939 *poutname, *pforname);
1940 basename = get_file_basename (inpf);
1941 lang_index = get_prefix_langdir_index (basename);
1942 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
1944 if (lang_index >= 0)
1946 /* The header is language specific. Given output_name &
1947 for_name remains unchanged. The base_files array gives the
1949 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
1950 (void*) base_files[lang_index],
1951 (base_files[lang_index])->name);
1952 return base_files[lang_index];
1956 /* The header is common to all front-end languages. So
1957 output_name is "gtype-desc.c" file. The calling function
1958 get_output_file_with_visibility will find its outf_p. */
1960 *poutname = xstrdup ("gtype-desc.c");
1961 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
1962 get_input_file_name (inpf));
1968 /* Special file rules action for handling *.c source files using
1969 * get_file_gtfilename to compute their output_name and
1970 * get_file_basename to compute their for_name. The output_name is
1971 * gt-<LANG>-<BASE>.h for language specific source files, and
1972 * gt-<BASE>.h for common source files. */
1974 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
1976 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
1977 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
1978 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
1979 (void*) inpf, get_input_file_name (inpf),
1980 *poutname, *pforname);
1981 DBGPRINTF ("newoutname %s", newoutname);
1982 DBGPRINTF ("newbasename %s", newbasename);
1985 *poutname = newoutname;
1986 *pforname = newbasename;
1990 /* Utility function for get_output_file_with_visibility which returns
1991 * a malloc-ed substituted string using TRS on matching of the FILNAM
1992 * file name, using the PMATCH array. */
1994 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
1997 struct obstack str_obstack;
1999 char *rawstr = NULL;
2000 const char *pt = NULL;
2001 DBGPRINTF ("filnam %s", filnam);
2002 obstack_init (&str_obstack);
2003 for (pt = trs; *pt; pt++) {
2009 /* A double dollar $$ is substituted by a single verbatim
2010 dollar, but who really uses dollar signs in file
2012 obstack_1grow (&str_obstack, '$');
2014 else if (ISDIGIT (pt[1]))
2016 /* Handle $0 $1 ... $9 by appropriate substitution. */
2017 int dolnum = pt[1] - '0';
2018 int so = pmatch[dolnum].rm_so;
2019 int eo = pmatch[dolnum].rm_eo;
2020 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2021 if (so>=0 && eo>=so)
2022 obstack_grow (&str_obstack, filnam + so, eo - so);
2026 /* This can happen only when files_rules is buggy! */
2029 /* Always skip the character after the dollar. */
2033 obstack_1grow (&str_obstack, c);
2035 obstack_1grow (&str_obstack, '\0');
2036 rawstr = XOBFINISH (&str_obstack, char *);
2037 str = xstrdup (rawstr);
2038 obstack_free (&str_obstack, NULL);
2039 DBGPRINTF ("matched replacement %s", str);
2045 /* An output file, suitable for definitions, that can see declarations
2046 made in INPF and is linked into every language that uses INPF.
2047 Since the result is cached inside INPF, that argument cannot be
2048 declared constant, but is "almost" constant. */
2051 get_output_file_with_visibility (input_file *inpf)
2054 char *for_name = NULL;
2055 char *output_name = NULL;
2056 const char* inpfname;
2058 /* This can happen when we need a file with visibility on a
2059 structure that we've never seen. We have to just hope that it's
2060 globally visible. */
2062 inpf = system_h_file;
2064 /* The result is cached in INPF, so return it if already known. */
2066 return inpf->inpoutf;
2068 /* In plugin mode, return NULL unless the input_file is one of the
2073 for (i = 0; i < nb_plugin_files; i++)
2074 if (inpf == plugin_files[i])
2076 inpf->inpoutf = plugin_output;
2077 return plugin_output;
2083 inpfname = get_input_file_name (inpf);
2085 /* Try each rule in sequence in files_rules until one is triggered. */
2088 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2089 (void*) inpf, inpfname);
2091 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2093 DBGPRINTF ("rulix#%d srcexpr %s",
2094 rulix, files_rules[rulix].frul_srcexpr);
2096 if (!files_rules[rulix].frul_re)
2098 /* Compile the regexpr lazily. */
2100 files_rules[rulix].frul_re = XCNEW (regex_t);
2101 err = regcomp (files_rules[rulix].frul_re,
2102 files_rules[rulix].frul_srcexpr,
2103 files_rules[rulix].frul_rflags);
2106 /* The regular expression compilation fails only when
2107 file_rules is buggy. */
2115 /* Match the regexpr and trigger the rule if matched. */
2117 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2119 regmatch_t pmatch[10];
2120 memset (pmatch, 0, sizeof (pmatch));
2121 if (!regexec (files_rules[rulix].frul_re,
2122 inpfname, 10, pmatch, 0))
2124 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2125 (void*) inpf, inpfname, rulix,
2126 files_rules[rulix].frul_srcexpr);
2128 matching_file_name_substitute (inpfname, pmatch,
2129 files_rules[rulix].frul_tr_for);
2130 DBGPRINTF ("for_name %s", for_name);
2132 matching_file_name_substitute (inpfname, pmatch,
2133 files_rules[rulix].frul_tr_out);
2134 DBGPRINTF ("output_name %s", output_name);
2135 if (files_rules[rulix].frul_action)
2137 /* Invoke our action routine. */
2139 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2140 rulix, output_name, for_name);
2142 (files_rules[rulix].frul_action) (inpf,
2143 &output_name, &for_name);
2144 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2145 rulix, (void*)of, output_name, for_name);
2146 /* If the action routine returned something, give it back
2147 immediately and cache it in inpf. */
2154 /* The rule matched, and had no action, or that action did
2155 not return any output file but could have changed the
2156 output_name or for_name. We break out of the loop on the
2162 /* The regexpr did not match. */
2163 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2164 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2170 if (!output_name || !for_name)
2172 /* This is impossible, and could only happen if the files_rules is
2173 incomplete or buggy. */
2177 /* Look through to see if we've ever seen this output filename
2178 before. If found, cache the result in inpf. */
2179 for (r = output_files; r; r = r->next)
2180 if (filename_cmp (r->name, output_name) == 0)
2183 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2184 output_name, for_name);
2188 /* If not found, create it, and cache it in inpf. */
2189 r = create_file (for_name, output_name);
2191 gcc_assert (r && r->name);
2192 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2193 output_name, for_name);
2200 /* The name of an output file, suitable for definitions, that can see
2201 declarations made in INPF and is linked into every language that
2205 get_output_file_name (input_file* inpf)
2207 outf_p o = get_output_file_with_visibility (inpf);
2213 /* Check if existing file is equal to the in memory buffer. */
2216 is_file_equal (outf_p of)
2218 FILE *newfile = fopen (of->name, "r");
2221 if (newfile == NULL)
2225 for (i = 0; i < of->bufused; i++)
2228 ch = fgetc (newfile);
2229 if (ch == EOF || ch != (unsigned char) of->buf[i])
2239 /* Copy the output to its final destination,
2240 but don't unnecessarily change modification times. */
2243 close_output_files (void)
2245 int nbwrittenfiles = 0;
2248 for (of = output_files; of; of = of->next)
2250 if (!is_file_equal (of))
2252 FILE *newfile = NULL;
2253 char *backupname = NULL;
2254 /* Back up the old version of the output file gt-FOO.c as
2255 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2258 backupname = concat (backup_dir, "/",
2259 lbasename (of->name), "~", NULL);
2260 if (!access (of->name, F_OK) && rename (of->name, backupname))
2261 fatal ("failed to back up %s as %s: %s",
2262 of->name, backupname, xstrerror (errno));
2265 newfile = fopen (of->name, "w");
2266 if (newfile == NULL)
2267 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2268 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2269 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2270 if (fclose (newfile) != 0)
2271 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2273 if (verbosity_level >= 2 && backupname)
2274 printf ("%s wrote #%-3d %s backed-up in %s\n",
2275 progname, nbwrittenfiles, of->name, backupname);
2276 else if (verbosity_level >= 1)
2277 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2282 /* output file remains unchanged. */
2283 if (verbosity_level >= 2)
2284 printf ("%s keep %s\n", progname, of->name);
2288 of->bufused = of->buflength = 0;
2290 if (verbosity_level >= 1)
2291 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2298 const input_file* file;
2302 struct walk_type_data;
2304 /* For scalars and strings, given the item in 'val'.
2305 For structures, given a pointer to the item in 'val'.
2306 For misc. pointers, given the item in 'val'.
2308 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2309 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2311 /* Parameters for write_types. */
2313 struct write_types_data
2316 const char *param_prefix;
2317 const char *subfield_marker_routine;
2318 const char *marker_routine;
2319 const char *reorder_note_routine;
2320 const char *comment;
2321 int skip_hooks; /* skip hook generation if non zero */
2324 static void output_escaped_param (struct walk_type_data *d,
2325 const char *, const char *);
2326 static void output_mangled_typename (outf_p, const_type_p);
2327 static void walk_type (type_p t, struct walk_type_data *d);
2328 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2329 const struct write_types_data *wtd);
2330 static void write_types_process_field
2331 (type_p f, const struct walk_type_data *d);
2332 static void write_types (outf_p output_header,
2334 type_p param_structs,
2335 const struct write_types_data *wtd);
2336 static void write_types_local_process_field
2337 (type_p f, const struct walk_type_data *d);
2338 static void write_local_func_for_structure
2339 (const_type_p orig_s, type_p s, type_p *param);
2340 static void write_local (outf_p output_header,
2341 type_p structures, type_p param_structs);
2342 static void write_enum_defn (type_p structures, type_p param_structs);
2343 static int contains_scalar_p (type_p t);
2344 static void put_mangled_filename (outf_p, const input_file *);
2345 static void finish_root_table (struct flist *flp, const char *pfx,
2346 const char *tname, const char *lastname,
2348 static void write_root (outf_p, pair_p, type_p, const char *, int,
2349 struct fileloc *, const char *, bool);
2350 static void write_array (outf_p f, pair_p v,
2351 const struct write_types_data *wtd);
2352 static void write_roots (pair_p, bool);
2354 /* Parameters for walk_type. */
2356 struct walk_type_data
2358 process_field_fn process_field;
2363 const char *prev_val[4];
2366 const struct fileloc *line;
2371 const char *reorder_fn;
2373 bool fn_wants_lvalue;
2381 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2382 pre-processor identifier to use in a #define directive. This replaces
2383 special characters used in C++ identifiers like '>', '<' and ':' with
2386 If no C++ special characters are found in TYPE_NAME, return
2387 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2388 characters replaced with '_'. In this case, the caller is
2389 responsible for freeing the allocated string. */
2392 filter_type_name (const char *type_name)
2394 if (strchr (type_name, '<') || strchr (type_name, ':'))
2397 char *s = xstrdup (type_name);
2398 for (i = 0; i < strlen (s); i++)
2399 if (s[i] == '<' || s[i] == '>' || s[i] == ':')
2408 /* Print a mangled name representing T to OF. */
2411 output_mangled_typename (outf_p of, const_type_p t)
2423 output_mangled_typename (of, t->u.p);
2433 case TYPE_LANG_STRUCT:
2434 case TYPE_USER_STRUCT:
2436 const char *id_for_tag = filter_type_name (t->u.s.tag);
2437 oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2439 if (id_for_tag != t->u.s.tag)
2440 free (CONST_CAST(char *, id_for_tag));
2443 case TYPE_PARAM_STRUCT:
2446 for (i = 0; i < NUM_PARAM; i++)
2447 if (t->u.param_struct.param[i] != NULL)
2448 output_mangled_typename (of, t->u.param_struct.param[i]);
2449 output_mangled_typename (of, t->u.param_struct.stru);
2457 /* Print PARAM to D->OF processing escapes. D->VAL references the
2458 current object, D->PREV_VAL the object containing the current
2459 object, ONAME is the name of the option and D->LINE is used to
2460 print error messages. */
2463 output_escaped_param (struct walk_type_data *d, const char *param,
2468 for (p = param; *p; p++)
2470 oprintf (d->of, "%c", *p);
2475 oprintf (d->of, "(%s)", d->prev_val[2]);
2478 oprintf (d->of, "(%s)", d->prev_val[0]);
2481 oprintf (d->of, "(%s)", d->prev_val[1]);
2485 const char *pp = d->val + strlen (d->val);
2486 while (pp[-1] == ']')
2489 oprintf (d->of, "%s", pp);
2493 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2499 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2500 which is of type T. Write code to D->OF to constrain execution (at
2501 the point that D->PROCESS_FIELD is called) to the appropriate
2502 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2503 pointers to those objects. D->PREV_VAL lists the objects
2504 containing the current object, D->OPT is a list of options to
2505 apply, D->INDENT is the current indentation level, D->LINE is used
2506 to print error messages, D->BITMAP indicates which languages to
2507 print the structure for, and D->PARAM is the current parameter
2508 (from an enclosing param_is option). */
2511 walk_type (type_p t, struct walk_type_data *d)
2513 const char *length = NULL;
2514 const char *desc = NULL;
2515 int maybe_undef_p = 0;
2516 int use_param_num = -1;
2517 int use_params_p = 0;
2520 const struct nested_ptr_data *nested_ptr_d = NULL;
2522 d->needs_cast_p = false;
2523 for (oo = d->opt; oo; oo = oo->next)
2524 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2525 length = oo->info.string;
2526 else if (strcmp (oo->name, "maybe_undef") == 0)
2528 else if (strncmp (oo->name, "use_param", 9) == 0
2529 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2530 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2531 else if (strcmp (oo->name, "use_params") == 0)
2533 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2534 desc = oo->info.string;
2535 else if (strcmp (oo->name, "mark_hook") == 0)
2537 else if (strcmp (oo->name, "nested_ptr") == 0
2538 && oo->kind == OPTION_NESTED)
2539 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2540 else if (strcmp (oo->name, "dot") == 0)
2542 else if (strcmp (oo->name, "tag") == 0)
2544 else if (strcmp (oo->name, "special") == 0)
2546 else if (strcmp (oo->name, "skip") == 0)
2548 else if (strcmp (oo->name, "atomic") == 0)
2550 else if (strcmp (oo->name, "default") == 0)
2552 else if (strcmp (oo->name, "param_is") == 0)
2554 else if (strncmp (oo->name, "param", 5) == 0
2555 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2557 else if (strcmp (oo->name, "chain_next") == 0)
2559 else if (strcmp (oo->name, "chain_prev") == 0)
2561 else if (strcmp (oo->name, "chain_circular") == 0)
2563 else if (strcmp (oo->name, "reorder") == 0)
2565 else if (strcmp (oo->name, "variable_size") == 0)
2568 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2575 int pointer_p = t->kind == TYPE_POINTER;
2579 if (!union_or_struct_p (t))
2580 error_at_line (d->line, "`use_params' option on unimplemented type");
2582 t = find_param_structure (t, d->param);
2584 t = create_pointer (t);
2587 if (use_param_num != -1)
2589 if (d->param != NULL && d->param[use_param_num] != NULL)
2591 type_p nt = d->param[use_param_num];
2593 if (t->kind == TYPE_ARRAY)
2594 nt = create_array (nt, t->u.a.len);
2595 else if (length != NULL && t->kind == TYPE_POINTER)
2596 nt = create_pointer (nt);
2597 d->needs_cast_p = (t->kind != TYPE_POINTER
2598 && (nt->kind == TYPE_POINTER
2599 || nt->kind == TYPE_STRING));
2603 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2607 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
2609 error_at_line (d->line,
2610 "field `%s' has invalid option `maybe_undef_p'\n",
2615 if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2617 error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2625 d->process_field (t, d);
2630 d->in_ptr_field = true;
2631 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2633 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2637 /* If a pointer type is marked as "atomic", we process the
2638 field itself, but we don't walk the data that they point to.
2640 There are two main cases where we walk types: to mark
2641 pointers that are reachable, and to relocate pointers when
2642 writing a PCH file. In both cases, an atomic pointer is
2643 itself marked or relocated, but the memory that it points
2644 to is left untouched. In the case of PCH, that memory will
2645 be read/written unchanged to the PCH file. */
2648 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2650 d->process_field (t, d);
2652 oprintf (d->of, "%*s}\n", d->indent, "");
2658 if (!union_or_struct_p (t->u.p)
2659 && t->u.p->kind != TYPE_PARAM_STRUCT)
2661 error_at_line (d->line,
2662 "field `%s' is pointer to unimplemented type",
2669 const char *oldprevval2 = d->prev_val[2];
2671 if (!union_or_struct_p (nested_ptr_d->type))
2673 error_at_line (d->line,
2674 "field `%s' has invalid "
2675 "option `nested_ptr'\n", d->val);
2679 d->prev_val[2] = d->val;
2680 oprintf (d->of, "%*s{\n", d->indent, "");
2682 d->val = xasprintf ("x%d", d->counter++);
2683 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2684 (nested_ptr_d->type->kind == TYPE_UNION
2685 ? "union" : "struct"),
2686 nested_ptr_d->type->u.s.tag,
2687 d->fn_wants_lvalue ? "" : "const ", d->val);
2688 oprintf (d->of, "%*s", d->indent + 2, "");
2689 output_escaped_param (d, nested_ptr_d->convert_from,
2691 oprintf (d->of, ";\n");
2693 d->process_field (nested_ptr_d->type, d);
2695 if (d->fn_wants_lvalue)
2697 oprintf (d->of, "%*s%s = ", d->indent, "",
2699 d->prev_val[2] = d->val;
2700 output_escaped_param (d, nested_ptr_d->convert_to,
2702 oprintf (d->of, ";\n");
2706 oprintf (d->of, "%*s}\n", d->indent, "");
2707 d->val = d->prev_val[2];
2708 d->prev_val[2] = oldprevval2;
2711 d->process_field (t->u.p, d);
2715 int loopcounter = d->loopcounter;
2716 const char *oldval = d->val;
2717 const char *oldprevval3 = d->prev_val[3];
2720 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2722 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2723 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2724 "", loopcounter, loopcounter);
2725 if (!d->in_record_p)
2726 output_escaped_param (d, length, "length");
2728 oprintf (d->of, "l%d", loopcounter);
2729 if (d->have_this_obj)
2730 /* Try to unswitch loops (see PR53880). */
2731 oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
2732 oprintf (d->of, "); i%d++) {\n", loopcounter);
2734 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2736 d->prev_val[3] = oldval;
2737 walk_type (t->u.p, d);
2740 d->prev_val[3] = oldprevval3;
2743 oprintf (d->of, "%*s}\n", d->indent, "");
2744 d->process_field (t, d);
2746 oprintf (d->of, "%*s}\n", d->indent, "");
2748 d->in_ptr_field = false;
2755 const char *oldval = d->val;
2758 /* If it's an array of scalars, we optimize by not generating
2760 if (t->u.a.p->kind == TYPE_SCALAR)
2764 loopcounter = d->loopcounter;
2766 loopcounter = d->counter++;
2768 /* When walking an array, compute the length and store it in a
2769 local variable before walking the array elements, instead of
2770 recomputing the length expression each time through the loop.
2771 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2772 where the length is stored in the first array element,
2773 because otherwise that operand can get overwritten on the
2775 oprintf (d->of, "%*s{\n", d->indent, "");
2777 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2778 if (!d->in_record_p || !length)
2780 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2781 d->indent, "", loopcounter);
2783 output_escaped_param (d, length, "length");
2785 oprintf (d->of, "%s", t->u.a.len);
2786 oprintf (d->of, ");\n");
2789 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2791 loopcounter, loopcounter, loopcounter, loopcounter);
2793 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2795 walk_type (t->u.a.p, d);
2800 oprintf (d->of, "%*s}\n", d->indent, "");
2802 oprintf (d->of, "%*s}\n", d->indent, "");
2810 const char *oldval = d->val;
2811 const char *oldprevval1 = d->prev_val[1];
2812 const char *oldprevval2 = d->prev_val[2];
2813 const char *struct_mark_hook = NULL;
2814 const int union_p = t->kind == TYPE_UNION;
2815 int seen_default_p = 0;
2817 int lengths_seen = 0;
2819 bool any_length_seen = false;
2821 if (!t->u.s.line.file)
2822 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
2824 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2826 error_at_line (d->line,
2827 "structure `%s' defined for mismatching languages",
2829 error_at_line (&t->u.s.line, "one structure defined here");
2832 /* Some things may also be defined in the structure's options. */
2833 for (o = t->u.s.opt; o; o = o->next)
2834 if (!desc && strcmp (o->name, "desc") == 0
2835 && o->kind == OPTION_STRING)
2836 desc = o->info.string;
2837 else if (!struct_mark_hook && strcmp (o->name, "mark_hook") == 0
2838 && o->kind == OPTION_STRING)
2839 struct_mark_hook = o->info.string;
2841 if (struct_mark_hook)
2842 oprintf (d->of, "%*s%s (&%s);\n",
2843 d->indent, "", struct_mark_hook, oldval);
2845 d->prev_val[2] = oldval;
2846 d->prev_val[1] = oldprevval2;
2851 error_at_line (d->line,
2852 "missing `desc' option for union `%s'",
2856 oprintf (d->of, "%*sswitch (", d->indent, "");
2857 output_escaped_param (d, desc, "desc");
2858 oprintf (d->of, ")\n");
2860 oprintf (d->of, "%*s{\n", d->indent, "");
2863 for (f = t->u.s.fields; f; f = f->next)
2867 const char *fieldlength = NULL;
2869 d->reorder_fn = NULL;
2870 for (oo = f->opt; oo; oo = oo->next)
2871 if (strcmp (oo->name, "skip") == 0)
2873 else if (strcmp (oo->name, "length") == 0
2874 && oo->kind == OPTION_STRING)
2875 fieldlength = oo->info.string;
2885 if (!any_length_seen)
2887 oprintf (d->of, "%*s{\n", d->indent, "");
2890 any_length_seen = true;
2892 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2893 d->indent, "", d->counter - 1);
2894 output_escaped_param (d, fieldlength, "length");
2895 oprintf (d->of, ");\n");
2899 endcounter = d->counter;
2901 for (f = t->u.s.fields; f; f = f->next)
2904 const char *dot = ".";
2905 const char *tagid = NULL;
2908 int use_param_p = 0;
2909 const char *fieldlength = NULL;
2912 d->reorder_fn = NULL;
2913 for (oo = f->opt; oo; oo = oo->next)
2914 if (strcmp (oo->name, "dot") == 0
2915 && oo->kind == OPTION_STRING)
2916 dot = oo->info.string;
2917 else if (strcmp (oo->name, "tag") == 0
2918 && oo->kind == OPTION_STRING)
2919 tagid = oo->info.string;
2920 else if (strcmp (oo->name, "skip") == 0)
2922 else if (strcmp (oo->name, "default") == 0)
2924 else if (strcmp (oo->name, "reorder") == 0
2925 && oo->kind == OPTION_STRING)
2926 d->reorder_fn = oo->info.string;
2927 else if (strncmp (oo->name, "use_param", 9) == 0
2928 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2930 else if (strcmp (oo->name, "length") == 0
2931 && oo->kind == OPTION_STRING)
2932 fieldlength = oo->info.string;
2937 if (union_p && tagid)
2939 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
2942 else if (union_p && default_p)
2944 oprintf (d->of, "%*sdefault:\n", d->indent, "");
2948 else if (!union_p && (default_p || tagid))
2949 error_at_line (d->line,
2950 "can't use `%s' outside a union on field `%s'",
2951 default_p ? "default" : "tag", f->name);
2952 else if (union_p && !(default_p || tagid)
2953 && f->type->kind == TYPE_SCALAR)
2956 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
2957 get_input_file_name (d->line->file), d->line->line,
2961 else if (union_p && !(default_p || tagid))
2962 error_at_line (d->line,
2963 "field `%s' is missing `tag' or `default' option",
2968 d->loopcounter = endcounter - lengths_seen--;
2972 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
2974 d->used_length = false;
2975 d->in_record_p = !union_p;
2977 if (union_p && use_param_p && d->param == NULL)
2978 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
2980 walk_type (f->type, d);
2982 d->in_record_p = false;
2988 oprintf (d->of, "%*sbreak;\n", d->indent, "");
2992 d->reorder_fn = NULL;
2995 d->prev_val[1] = oldprevval1;
2996 d->prev_val[2] = oldprevval2;
2998 if (union_p && !seen_default_p)
3000 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3001 oprintf (d->of, "%*s break;\n", d->indent, "");
3005 oprintf (d->of, "%*s}\n", d->indent, "");
3008 if (any_length_seen)
3011 oprintf (d->of, "%*s}\n", d->indent, "");
3016 case TYPE_LANG_STRUCT:
3019 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3020 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3023 error_at_line (d->line, "structure `%s' differs between languages",
3030 case TYPE_PARAM_STRUCT:
3032 type_p *oldparam = d->param;
3034 d->param = t->u.param_struct.param;
3035 walk_type (t->u.param_struct.stru, d);
3036 d->param = oldparam;
3040 case TYPE_USER_STRUCT:
3041 d->process_field (t, d);
3049 /* process_field routine for marking routines. */
3052 write_types_process_field (type_p f, const struct walk_type_data *d)
3054 const struct write_types_data *wtd;
3055 const char *cast = d->needs_cast_p ? "(void *)" : "";
3056 wtd = (const struct write_types_data *) d->cookie;
3063 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
3064 wtd->subfield_marker_routine, cast, d->val);
3065 if (wtd->param_prefix)
3067 if (f->u.p->kind == TYPE_SCALAR)
3068 /* The current type is a pointer to a scalar (so not
3069 considered like a pointer to instances of user defined
3070 types) and we are seeing it; it means we must be even
3071 more careful about the second argument of the
3072 SUBFIELD_MARKER_ROUTINE call. That argument must
3073 always be the instance of the type for which
3074 write_func_for_structure was called - this really is
3075 what the function SUBFIELD_MARKER_ROUTINE expects.
3076 That is, it must be an instance of the ORIG_S type
3077 parameter of write_func_for_structure. The convention
3078 is that that argument must be "x" in that case (as set
3079 by write_func_for_structure). The problem is, we can't
3080 count on d->prev_val[3] to be always set to "x" in that
3081 case. Sometimes walk_type can set it to something else
3082 (to e.g cooperate with write_array when called from
3083 write_roots). So let's set it to "x" here then. */
3084 oprintf (d->of, ", x");
3086 oprintf (d->of, ", %s", d->prev_val[3]);
3089 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3090 output_mangled_typename (d->of, d->orig_s);
3093 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3095 if (f->u.p->kind == TYPE_PARAM_STRUCT
3096 && f->u.p->u.s.line.file != NULL)
3098 oprintf (d->of, ", gt_e_");
3099 output_mangled_typename (d->of, f);
3101 else if (union_or_struct_p (f) && f->u.p->u.s.line.file != NULL)
3103 oprintf (d->of, ", gt_ggc_e_");
3104 output_mangled_typename (d->of, f);
3107 oprintf (d->of, ", gt_types_enum_last");
3109 oprintf (d->of, ");\n");
3110 if (d->reorder_fn && wtd->reorder_note_routine)
3111 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
3112 wtd->reorder_note_routine, cast, d->val,
3113 d->prev_val[3], d->reorder_fn);
3119 case TYPE_LANG_STRUCT:
3120 case TYPE_PARAM_STRUCT:
3121 case TYPE_USER_STRUCT:
3122 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3124 /* If F is a user-defined type and the field is not a
3125 pointer to the type, then we should not generate the
3126 standard pointer-marking code. All we need to do is call
3127 the user-provided marking function to process the fields
3129 oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3134 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3135 output_mangled_typename (d->of, f);
3136 oprintf (d->of, " (%s%s);\n", cast, d->val);
3137 if (d->reorder_fn && wtd->reorder_note_routine)
3138 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
3139 wtd->reorder_note_routine, cast, d->val, cast, d->val,
3152 /* A subroutine of write_func_for_structure. Write the enum tag for S. */
3155 output_type_enum (outf_p of, type_p s)
3157 if (s->kind == TYPE_PARAM_STRUCT && s->u.param_struct.line.file != NULL)
3159 oprintf (of, ", gt_e_");
3160 output_mangled_typename (of, s);
3162 else if (union_or_struct_p (s) && s->u.s.line.file != NULL)
3164 oprintf (of, ", gt_ggc_e_");
3165 output_mangled_typename (of, s);
3168 oprintf (of, ", gt_types_enum_last");
3171 /* Return an output file that is suitable for definitions which can
3172 reference struct S */
3175 get_output_file_for_structure (const_type_p s, type_p *param)
3177 const input_file *fn;
3180 gcc_assert (union_or_struct_p (s));
3181 fn = s->u.s.line.file;
3183 /* This is a hack, and not the good kind either. */
3184 for (i = NUM_PARAM - 1; i >= 0; i--)
3185 if (param && param[i] && param[i]->kind == TYPE_POINTER
3186 && union_or_struct_p (param[i]->u.p))
3187 fn = param[i]->u.p->u.s.line.file;
3189 /* The call to get_output_file_with_visibility may update fn by
3190 caching its result inside, so we need the CONST_CAST. */
3191 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3195 /* Returns the specifier keyword for a string or union type S, empty string
3199 get_type_specifier (const type_p s)
3201 if (s->kind == TYPE_STRUCT)
3203 else if (s->kind == TYPE_LANG_STRUCT)
3204 return get_type_specifier (s->u.s.lang_struct);
3205 else if (s->kind == TYPE_UNION)
3211 /* Emits a declaration for type TY (assumed to be a union or a
3212 structure) on stream OUT. */
3215 write_type_decl (outf_p out, type_p ty)
3217 if (union_or_struct_p (ty))
3218 oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3219 else if (ty->kind == TYPE_SCALAR)
3221 if (ty->u.scalar_is_char)
3222 oprintf (out, "const char");
3224 oprintf (out, "void");
3226 else if (ty->kind == TYPE_POINTER)
3228 write_type_decl (out, ty->u.p);
3229 oprintf (out, " *");
3231 else if (ty->kind == TYPE_ARRAY)
3233 write_type_decl (out, ty->u.a.p);
3234 oprintf (out, " *");
3236 else if (ty->kind == TYPE_STRING)
3238 oprintf (out, "const char *");
3245 /* Write on OF the name of the marker function for structure S. PREFIX
3246 is the prefix to use (to distinguish ggc from pch markers). */
3249 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3251 if (union_or_struct_p (s))
3253 const char *id_for_tag = filter_type_name (s->u.s.tag);
3254 oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3255 if (id_for_tag != s->u.s.tag)
3256 free (CONST_CAST(char *, id_for_tag));
3258 else if (s->kind == TYPE_PARAM_STRUCT)
3260 oprintf (of, "gt_%s_", prefix);
3261 output_mangled_typename (of, s);
3268 /* Write on OF a user-callable routine to act as an entry point for
3269 the marking routine for S, generated by write_func_for_structure.
3270 PREFIX is the prefix to use to distinguish ggc and pch markers. */
3273 write_user_func_for_structure_ptr (outf_p of, type_p s, const char *prefix)
3275 /* Parameterized structures are not supported in user markers. There
3276 is no way for the marker function to know which specific type
3277 to use to generate the call to the void * entry point. For
3278 instance, a marker for struct htab may need to call different
3279 routines to mark the fields, depending on the paramN_is attributes.
3281 A user-defined marker that accepts 'struct htab' as its argument
3282 would not know which variant to call. Generating several entry
3283 points accepting 'struct htab' would cause multiply-defined
3284 errors during compilation. */
3285 gcc_assert (union_or_struct_p (s));
3287 type_p alias_of = NULL;
3288 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3289 if (strcmp (opt->name, "ptr_alias") == 0)
3291 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3292 we do not generate marking code for ORIG_S here. Instead, a
3293 forwarder #define in gtype-desc.h will cause every call to its
3294 marker to call the target of this alias.
3296 However, we still want to create a user entry code for the
3297 aliased type. So, if ALIAS_OF is set, we only generate the
3298 user-callable marker function. */
3299 alias_of = opt->info.type;
3303 oprintf (of, "\nvoid\n");
3304 oprintf (of, "gt_%sx (", prefix);
3305 write_type_decl (of, s);
3306 oprintf (of, " *& x)\n");
3307 oprintf (of, "{\n");
3308 oprintf (of, " if (x)\n ");
3309 write_marker_function_name (of, alias_of ? alias_of : s, prefix);
3310 oprintf (of, " ((void *) x);\n");
3311 oprintf (of, "}\n");
3315 /* Write a function to mark all the fields of type S on OF. PREFIX
3316 and D are as in write_user_marking_functions. */
3319 write_user_func_for_structure_body (type_p s, const char *prefix,
3320 struct walk_type_data *d)
3322 oprintf (d->of, "\nvoid\n");
3323 oprintf (d->of, "gt_%sx (", prefix);
3324 write_type_decl (d->of, s);
3325 oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3326 oprintf (d->of, "{\n");
3327 oprintf (d->of, " ");
3328 write_type_decl (d->of, s);
3329 oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3333 oprintf (d->of, "}\n");
3337 /* Emit the user-callable functions needed to mark all the types used
3338 by the user structure S. PREFIX is the prefix to use to
3339 distinguish ggc and pch markers. D contains data needed to pass to
3340 walk_type when traversing the fields of a type.
3342 For every type T referenced by S, two routines are generated: one
3343 that takes 'T *', marks the pointer and calls the second routine,
3344 which just marks the fields of T. */
3347 write_user_marking_functions (type_p s, const char *prefix,
3348 struct walk_type_data *d)
3350 gcc_assert (s->kind == TYPE_USER_STRUCT);
3352 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3354 type_p fld_type = fld->type;
3355 if (fld_type->kind == TYPE_POINTER)
3357 type_p pointed_to_type = fld_type->u.p;
3358 if (union_or_struct_p (pointed_to_type))
3359 write_user_func_for_structure_ptr (d->of, pointed_to_type, prefix);
3361 else if (union_or_struct_p (fld_type))
3362 write_user_func_for_structure_body (fld_type, prefix, d);
3367 /* For S, a structure that's part of ORIG_S, and using parameters
3368 PARAM, write out a routine that:
3369 - Takes a parameter, a void * but actually of type *S
3370 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3371 field of S or its substructures and (in some cases) things
3372 that are pointed to by S. */
3375 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
3376 const struct write_types_data *wtd)
3378 const char *chain_next = NULL;
3379 const char *chain_prev = NULL;
3380 const char *chain_circular = NULL;
3381 const char *mark_hook_name = NULL;
3383 struct walk_type_data d;
3385 memset (&d, 0, sizeof (d));
3386 d.of = get_output_file_for_structure (s, param);
3387 for (opt = s->u.s.opt; opt; opt = opt->next)
3388 if (strcmp (opt->name, "chain_next") == 0
3389 && opt->kind == OPTION_STRING)
3390 chain_next = opt->info.string;
3391 else if (strcmp (opt->name, "chain_prev") == 0
3392 && opt->kind == OPTION_STRING)
3393 chain_prev = opt->info.string;
3394 else if (strcmp (opt->name, "chain_circular") == 0
3395 && opt->kind == OPTION_STRING)
3396 chain_circular = opt->info.string;
3397 else if (strcmp (opt->name, "mark_hook") == 0
3398 && opt->kind == OPTION_STRING)
3399 mark_hook_name = opt->info.string;
3400 if (chain_prev != NULL && chain_next == NULL)
3401 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3402 if (chain_circular != NULL && chain_next != NULL)
3403 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3404 if (chain_circular != NULL)
3405 chain_next = chain_circular;
3407 d.process_field = write_types_process_field;
3411 d.line = &s->u.s.line;
3412 d.bitmap = s->u.s.bitmap;
3414 d.prev_val[0] = "*x";
3415 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3416 d.prev_val[3] = "x";
3418 d.have_this_obj = false;
3420 oprintf (d.of, "\n");
3421 oprintf (d.of, "void\n");
3422 write_marker_function_name (d.of, orig_s, wtd->prefix);
3423 oprintf (d.of, " (void *x_p)\n");
3424 oprintf (d.of, "{\n ");
3425 write_type_decl (d.of, s);
3426 oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3427 write_type_decl (d.of, s);
3428 oprintf (d.of, " *)x_p;\n");
3429 if (chain_next != NULL)
3431 oprintf (d.of, " ");
3432 write_type_decl (d.of, s);
3433 oprintf (d.of, " * xlimit = x;\n");
3435 if (chain_next == NULL)
3437 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3438 if (wtd->param_prefix)
3440 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3441 output_mangled_typename (d.of, orig_s);
3442 output_type_enum (d.of, orig_s);
3444 oprintf (d.of, "))\n");
3448 if (chain_circular != NULL)
3449 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3451 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3452 if (wtd->param_prefix)
3454 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3455 output_mangled_typename (d.of, orig_s);
3456 output_type_enum (d.of, orig_s);
3458 oprintf (d.of, "))\n");
3459 if (chain_circular != NULL)
3460 oprintf (d.of, " return;\n do\n");
3461 if (mark_hook_name && !wtd->skip_hooks)
3463 oprintf (d.of, " {\n");
3464 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
3466 oprintf (d.of, " xlimit = (");
3467 d.prev_val[2] = "*xlimit";
3468 output_escaped_param (&d, chain_next, "chain_next");
3469 oprintf (d.of, ");\n");
3470 if (mark_hook_name && !wtd->skip_hooks)
3471 oprintf (d.of, " }\n");
3472 if (chain_prev != NULL)
3474 oprintf (d.of, " if (x != xlimit)\n");
3475 oprintf (d.of, " for (;;)\n");
3476 oprintf (d.of, " {\n");
3477 oprintf (d.of, " %s %s * const xprev = (",
3478 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3480 d.prev_val[2] = "*x";
3481 output_escaped_param (&d, chain_prev, "chain_prev");
3482 oprintf (d.of, ");\n");
3483 oprintf (d.of, " if (xprev == NULL) break;\n");
3484 oprintf (d.of, " x = xprev;\n");
3485 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3486 if (wtd->param_prefix)
3488 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3489 output_mangled_typename (d.of, orig_s);
3490 output_type_enum (d.of, orig_s);
3492 oprintf (d.of, ");\n");
3493 oprintf (d.of, " }\n");
3495 if (chain_circular != NULL)
3497 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3498 if (wtd->param_prefix)
3500 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3501 output_mangled_typename (d.of, orig_s);
3502 output_type_enum (d.of, orig_s);
3504 oprintf (d.of, "));\n");
3505 if (mark_hook_name && !wtd->skip_hooks)
3506 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
3507 oprintf (d.of, " do\n");
3510 oprintf (d.of, " while (x != xlimit)\n");
3512 oprintf (d.of, " {\n");
3513 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3515 oprintf (d.of, " %s (x);\n", mark_hook_name);
3518 d.prev_val[2] = "*x";
3520 if (orig_s->kind != TYPE_USER_STRUCT)
3524 /* User structures have no fields to walk. Simply generate a call
3525 to the user-provided structure marker. */
3526 oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3529 if (chain_next != NULL)
3531 oprintf (d.of, " x = (");
3532 output_escaped_param (&d, chain_next, "chain_next");
3533 oprintf (d.of, ");\n");
3536 oprintf (d.of, " }\n");
3537 if (chain_circular != NULL)
3538 oprintf (d.of, " while (x != xlimit);\n");
3539 oprintf (d.of, "}\n");
3541 if (orig_s->kind == TYPE_USER_STRUCT)
3542 write_user_marking_functions (orig_s, wtd->prefix, &d);
3546 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3549 write_types (outf_p output_header, type_p structures, type_p param_structs,
3550 const struct write_types_data *wtd)
3552 int nbfun = 0; /* Count the emitted functions. */
3555 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3557 /* We first emit the macros and the declarations. Functions' code is
3558 emitted afterwards. This is needed in plugin mode. */
3559 oprintf (output_header, "/* Macros and declarations. */\n");
3560 for (s = structures; s; s = s->next)
3561 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3565 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3568 const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3570 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3571 output_mangled_typename (output_header, s);
3572 oprintf (output_header, "(X) do { \\\n");
3573 oprintf (output_header,
3574 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3576 oprintf (output_header, " } while (0)\n");
3578 for (opt = s->u.s.opt; opt; opt = opt->next)
3579 if (strcmp (opt->name, "ptr_alias") == 0
3580 && opt->kind == OPTION_TYPE)
3582 const_type_p const t = (const_type_p) opt->info.type;
3583 if (t->kind == TYPE_STRUCT
3584 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3586 const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3587 oprintf (output_header,
3588 "#define gt_%sx_%s gt_%sx_%s\n",
3589 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3590 if (t_id_for_tag != t->u.s.tag)
3591 free (CONST_CAST(char *, t_id_for_tag));
3594 error_at_line (&s->u.s.line,
3595 "structure alias is not a structure");
3601 /* Declare the marker procedure only once. */
3602 oprintf (output_header,
3603 "extern void gt_%sx_%s (void *);\n",
3604 wtd->prefix, s_id_for_tag);
3606 if (s_id_for_tag != s->u.s.tag)
3607 free (CONST_CAST(char *, s_id_for_tag));
3609 if (s->u.s.line.file == NULL)
3611 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3617 for (s = param_structs; s; s = s->next)
3618 if (s->gc_used == GC_POINTED_TO)
3620 type_p stru = s->u.param_struct.stru;
3622 /* Declare the marker procedure. */
3623 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
3624 output_mangled_typename (output_header, s);
3625 oprintf (output_header, " (void *);\n");
3627 if (stru->u.s.line.file == NULL)
3629 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3635 /* At last we emit the functions code. */
3636 oprintf (output_header, "\n/* functions code */\n");
3637 for (s = structures; s; s = s->next)
3638 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3642 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3644 for (opt = s->u.s.opt; opt; opt = opt->next)
3645 if (strcmp (opt->name, "ptr_alias") == 0)
3650 if (s->kind == TYPE_LANG_STRUCT)
3653 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3656 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3657 nbfun, (void*) ss, ss->u.s.tag);
3658 write_func_for_structure (s, ss, NULL, wtd);
3664 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3665 nbfun, (void*) s, s->u.s.tag);
3666 write_func_for_structure (s, s, NULL, wtd);
3671 /* Structure s is not possibly pointed to, so can be ignored. */
3672 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3673 (void*)s, s->u.s.tag,
3677 for (s = param_structs; s; s = s->next)
3678 if (s->gc_used == GC_POINTED_TO)
3680 type_p *param = s->u.param_struct.param;
3681 type_p stru = s->u.param_struct.stru;
3682 if (stru->u.s.line.file == NULL)
3684 if (stru->kind == TYPE_LANG_STRUCT)
3687 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3690 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
3691 nbfun, (void*) ss, ss->u.s.tag);
3692 write_func_for_structure (s, ss, param, wtd);
3698 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
3700 (void*) stru, stru->u.s.tag);
3701 write_func_for_structure (s, stru, param, wtd);
3706 /* Param structure s is not pointed to, so should be ignored. */
3707 DBGPRINTF ("ignored s @ %p", (void*)s);
3709 if (verbosity_level >= 2)
3710 printf ("%s emitted %d routines for %s\n",
3711 progname, nbfun, wtd->comment);
3714 static const struct write_types_data ggc_wtd = {
3715 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
3716 "GC marker procedures. ",
3720 static const struct write_types_data pch_wtd = {
3721 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3722 "gt_pch_note_reorder",
3723 "PCH type-walking procedures. ",
3727 /* Write out the local pointer-walking routines. */
3729 /* process_field routine for local pointer-walking for user-callable
3730 routines. The difference between this and
3731 write_types_local_process_field is that, in this case, we do not
3732 need to check whether the given pointer matches the address of the
3733 parent structure. This check was already generated by the call
3734 to gt_pch_nx in the main gt_pch_p_*() function that is calling
3738 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
3745 case TYPE_LANG_STRUCT:
3746 case TYPE_PARAM_STRUCT:
3748 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3751 case TYPE_USER_STRUCT:
3752 if (d->in_ptr_field)
3753 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3755 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3756 d->indent, "", d->val);
3768 /* Write a function to PCH walk all the fields of type S on OF.
3769 D contains data needed by walk_type to recurse into the fields of S. */
3772 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
3774 oprintf (d->of, "\nvoid\n");
3775 oprintf (d->of, "gt_pch_nx (");
3776 write_type_decl (d->of, s);
3777 oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
3778 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3779 "\tATTRIBUTE_UNUSED void *cookie)\n");
3780 oprintf (d->of, "{\n");
3783 d->process_field = write_types_local_user_process_field;
3785 oprintf (d->of, "}\n");
3789 /* Emit the user-callable functions needed to mark all the types used
3790 by the user structure S. PREFIX is the prefix to use to
3791 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
3792 chain_next option defined. D contains data needed to pass to
3793 walk_type when traversing the fields of a type.
3795 For every type T referenced by S, two routines are generated: one
3796 that takes 'T *', marks the pointer and calls the second routine,
3797 which just marks the fields of T. */
3800 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
3802 gcc_assert (s->kind == TYPE_USER_STRUCT);
3804 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3806 type_p fld_type = fld->type;
3807 if (union_or_struct_p (fld_type))
3808 write_pch_user_walking_for_structure_body (fld_type, d);
3813 /* process_field routine for local pointer-walking. */
3816 write_types_local_process_field (type_p f, const struct walk_type_data *d)
3818 gcc_assert (d->have_this_obj);
3824 case TYPE_LANG_STRUCT:
3825 case TYPE_PARAM_STRUCT:
3827 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3829 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3832 case TYPE_USER_STRUCT:
3833 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3835 if (d->in_ptr_field)
3836 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3838 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3839 d->indent, "", d->val);
3851 /* For S, a structure that's part of ORIG_S, and using parameters
3852 PARAM, write out a routine that:
3853 - Is of type gt_note_pointers
3854 - Calls PROCESS_FIELD on each field of S or its substructures.
3858 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
3860 struct walk_type_data d;
3862 memset (&d, 0, sizeof (d));
3863 d.of = get_output_file_for_structure (s, param);
3864 d.process_field = write_types_local_process_field;
3866 d.line = &s->u.s.line;
3867 d.bitmap = s->u.s.bitmap;
3869 d.prev_val[0] = d.prev_val[2] = "*x";
3870 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3871 d.prev_val[3] = "x";
3873 d.fn_wants_lvalue = true;
3875 oprintf (d.of, "\n");
3876 oprintf (d.of, "void\n");
3877 oprintf (d.of, "gt_pch_p_");
3878 output_mangled_typename (d.of, orig_s);
3879 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3881 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3882 "\tATTRIBUTE_UNUSED void *cookie)\n");
3883 oprintf (d.of, "{\n");
3884 oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3885 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3886 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3888 d.have_this_obj = true;
3890 if (s->kind != TYPE_USER_STRUCT)
3894 /* User structures have no fields to walk. Simply generate a
3895 call to the user-provided PCH walker. */
3896 oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
3898 oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3899 d.indent, "", d.val);
3902 oprintf (d.of, "}\n");
3904 /* Write user-callable entry points for the PCH walking routines. */
3905 if (orig_s->kind == TYPE_USER_STRUCT)
3906 write_pch_user_walking_functions (s, &d);
3909 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
3912 write_local (outf_p output_header, type_p structures, type_p param_structs)
3919 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
3920 for (s = structures; s; s = s->next)
3921 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3925 if (s->u.s.line.file == NULL)
3927 for (opt = s->u.s.opt; opt; opt = opt->next)
3928 if (strcmp (opt->name, "ptr_alias") == 0
3929 && opt->kind == OPTION_TYPE)
3931 const_type_p const t = (const_type_p) opt->info.type;
3932 if (t->kind == TYPE_STRUCT
3933 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3935 oprintf (output_header, "#define gt_pch_p_");
3936 output_mangled_typename (output_header, s);
3937 oprintf (output_header, " gt_pch_p_");
3938 output_mangled_typename (output_header, t);
3939 oprintf (output_header, "\n");
3942 error_at_line (&s->u.s.line,
3943 "structure alias is not a structure");
3949 /* Declare the marker procedure only once. */
3950 oprintf (output_header, "extern void gt_pch_p_");
3951 output_mangled_typename (output_header, s);
3952 oprintf (output_header,
3953 "\n (void *, void *, gt_pointer_operator, void *);\n");
3955 if (s->kind == TYPE_LANG_STRUCT)
3958 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3959 write_local_func_for_structure (s, ss, NULL);
3962 write_local_func_for_structure (s, s, NULL);
3965 for (s = param_structs; s; s = s->next)
3966 if (s->gc_used == GC_POINTED_TO)
3968 type_p *param = s->u.param_struct.param;
3969 type_p stru = s->u.param_struct.stru;
3971 /* Declare the marker procedure. */
3972 oprintf (output_header, "extern void gt_pch_p_");
3973 output_mangled_typename (output_header, s);
3974 oprintf (output_header,
3975 "\n (void *, void *, gt_pointer_operator, void *);\n");
3977 if (stru->u.s.line.file == NULL)
3979 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3984 if (stru->kind == TYPE_LANG_STRUCT)
3987 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3988 write_local_func_for_structure (s, ss, param);
3991 write_local_func_for_structure (s, stru, param);
3995 /* Nonzero if S is a type for which typed GC allocators should be output. */
3997 #define USED_BY_TYPED_GC_P(s) \
3998 ((s->kind == TYPE_POINTER \
3999 && (s->u.p->gc_used == GC_POINTED_TO \
4000 || s->u.p->gc_used == GC_USED)) \
4001 || (union_or_struct_p (s) \
4002 && ((s)->gc_used == GC_POINTED_TO \
4003 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4004 && s->u.s.line.file != NULL) \
4005 || ((s)->gc_used == GC_USED \
4006 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))))))
4009 /* Write out the 'enum' definition for gt_types_enum. */
4012 write_enum_defn (type_p structures, type_p param_structs)
4016 int nbparamstruct = 0;
4020 oprintf (header_file, "\n/* Enumeration of types known. */\n");
4021 oprintf (header_file, "enum gt_types_enum {\n");
4022 for (s = structures; s; s = s->next)
4023 if (USED_BY_TYPED_GC_P (s))
4026 DBGPRINTF ("write_enum_defn s @ %p nbstruct %d",
4027 (void*) s, nbstruct);
4028 if (union_or_struct_p (s))
4029 DBGPRINTF ("write_enum_defn s %p #%d is unionorstruct tagged %s",
4030 (void*) s, nbstruct, s->u.s.tag);
4031 oprintf (header_file, " gt_ggc_e_");
4032 output_mangled_typename (header_file, s);
4033 oprintf (header_file, ",\n");
4035 for (s = param_structs; s; s = s->next)
4036 if (s->gc_used == GC_POINTED_TO)
4039 DBGPRINTF ("write_enum_defn s %p nbparamstruct %d",
4040 (void*) s, nbparamstruct);
4041 oprintf (header_file, " gt_e_");
4042 output_mangled_typename (header_file, s);
4043 oprintf (header_file, ",\n");
4045 oprintf (header_file, " gt_types_enum_last\n");
4046 oprintf (header_file, "};\n");
4047 if (verbosity_level >= 2)
4048 printf ("%s handled %d GTY-ed structures & %d parameterized structures.\n",
4049 progname, nbstruct, nbparamstruct);
4053 /* Might T contain any non-pointer elements? */
4056 contains_scalar_p (type_p t)
4064 return contains_scalar_p (t->u.a.p);
4066 /* Could also check for structures that have no non-pointer
4067 fields, but there aren't enough of those to worry about. */
4072 /* Mangle INPF and print it to F. */
4075 put_mangled_filename (outf_p f, const input_file *inpf)
4077 /* The call to get_output_file_name may indirectly update fn since
4078 get_output_file_with_visibility caches its result inside, so we
4079 need the CONST_CAST. */
4080 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4083 for (; *name != 0; name++)
4084 if (ISALNUM (*name))
4085 oprintf (f, "%c", *name);
4087 oprintf (f, "%c", '_');
4090 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4091 LASTNAME, and NAME are all strings to insert in various places in
4092 the resulting code. */
4095 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4096 const char *tname, const char *name)
4100 for (fli2 = flp; fli2; fli2 = fli2->next)
4101 if (fli2->started_p)
4103 oprintf (fli2->f, " %s\n", lastname);
4104 oprintf (fli2->f, "};\n\n");
4107 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4108 if (fli2->started_p)
4110 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4113 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4116 oprintf (base_files[fnum],
4117 "extern const struct %s gt_%s_", tname, pfx);
4118 put_mangled_filename (base_files[fnum], fli2->file);
4119 oprintf (base_files[fnum], "[];\n");
4125 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4126 oprintf (base_files[fnum],
4127 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4131 for (fli2 = flp; fli2; fli2 = fli2->next)
4132 if (fli2->started_p)
4134 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4137 fli2->started_p = 0;
4139 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4142 oprintf (base_files[fnum], " gt_%s_", pfx);
4143 put_mangled_filename (base_files[fnum], fli2->file);
4144 oprintf (base_files[fnum], ",\n");
4150 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4152 oprintf (base_files[fnum], " NULL\n");
4153 oprintf (base_files[fnum], "};\n");
4158 /* Write the first three fields (pointer, count and stride) for
4159 root NAME to F. V and LINE are as for write_root.
4161 Return true if the entry could be written; return false on error. */
4164 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4170 error_at_line (line, "`%s' is too complex to be a root", name);
4174 oprintf (f, " {\n");
4175 oprintf (f, " &%s,\n", name);
4178 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4180 oprintf (f, " * (%s)", ap->u.a.len);
4181 else if (ap == v->type)
4182 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4184 oprintf (f, " sizeof (%s", v->name);
4185 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4187 oprintf (f, "),\n");
4191 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4192 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4196 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4197 int has_length, struct fileloc *line, const char *if_marked,
4198 bool emit_pch, type_p field_type, const char *field_name)
4201 /* If the field reference is relative to V, rather than to some
4202 subcomponent of V, we can mark any subarrays with a single stride.
4203 We're effectively treating the field as a global variable in its
4205 if (v && type == v->type)
4208 newv.type = field_type;
4209 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4212 /* Otherwise, any arrays nested in the structure are too complex to
4214 else if (field_type->kind == TYPE_ARRAY)
4216 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4217 has_length, line, if_marked, emit_pch);
4220 /* Write out to F the table entry and any marker routines needed to
4221 mark NAME as TYPE. V can be one of three values:
4223 - null, if NAME is too complex to represent using a single
4224 count and stride. In this case, it is an error for NAME to
4225 contain any gc-ed data.
4227 - the outermost array that contains NAME, if NAME is part of an array.
4229 - the C variable that contains NAME, if NAME is not part of an array.
4231 LINE is the line of the C source that declares the root variable.
4232 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
4233 is nonzero iff we are building the root table for hash table caches. */
4236 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4237 struct fileloc *line, const char *if_marked, bool emit_pch)
4244 for (fld = type->u.s.fields; fld; fld = fld->next)
4247 const char *desc = NULL;
4250 for (o = fld->opt; o; o = o->next)
4251 if (strcmp (o->name, "skip") == 0)
4253 else if (strcmp (o->name, "desc") == 0
4254 && o->kind == OPTION_STRING)
4255 desc = o->info.string;
4256 else if (strcmp (o->name, "param_is") == 0)
4259 error_at_line (line,
4260 "field `%s' of global `%s' has unknown option `%s'",
4261 fld->name, name, o->name);
4265 else if (desc && fld->type->kind == TYPE_UNION)
4267 pair_p validf = NULL;
4270 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4272 const char *tag = NULL;
4274 for (oo = ufld->opt; oo; oo = oo->next)
4275 if (strcmp (oo->name, "tag") == 0
4276 && oo->kind == OPTION_STRING)
4277 tag = oo->info.string;
4278 if (tag == NULL || strcmp (tag, desc) != 0)
4281 error_at_line (line,
4282 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4283 name, fld->name, validf->name,
4284 name, fld->name, ufld->name, tag);
4288 write_field_root (f, v, type, name, 0, line, if_marked,
4289 emit_pch, validf->type,
4290 ACONCAT ((fld->name, ".",
4291 validf->name, NULL)));
4294 error_at_line (line,
4295 "global `%s.%s' has `desc' option but is not union",
4298 write_field_root (f, v, type, name, 0, line, if_marked,
4299 emit_pch, fld->type, fld->name);
4307 newname = xasprintf ("%s[0]", name);
4308 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
4314 case TYPE_USER_STRUCT:
4315 write_root (f, v, type->u.a.p, name, has_length, line, if_marked,
4323 if (!start_root_entry (f, v, name, line))
4328 if (!has_length && union_or_struct_p (tp))
4330 const char *id_for_tag = filter_type_name (tp->u.s.tag);
4331 oprintf (f, " >_ggc_mx_%s,\n", id_for_tag);
4333 oprintf (f, " >_pch_nx_%s", id_for_tag);
4335 oprintf (f, " NULL");
4336 if (id_for_tag != tp->u.s.tag)
4337 free (CONST_CAST(char *, id_for_tag));
4339 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
4341 oprintf (f, " >_ggc_m_");
4342 output_mangled_typename (f, tp);
4345 oprintf (f, ",\n >_pch_n_");
4346 output_mangled_typename (f, tp);
4349 oprintf (f, ",\n NULL");
4352 && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
4354 oprintf (f, " >_ggc_ma_%s,\n", name);
4356 oprintf (f, " >_pch_na_%s", name);
4358 oprintf (f, " NULL");
4362 error_at_line (line,
4363 "global `%s' is pointer to unimplemented type",
4367 oprintf (f, ",\n &%s", if_marked);
4368 oprintf (f, "\n },\n");
4374 if (!start_root_entry (f, v, name, line))
4377 oprintf (f, " (gt_pointer_walker) >_ggc_m_S,\n");
4378 oprintf (f, " (gt_pointer_walker) >_pch_n_S\n");
4379 oprintf (f, " },\n");
4387 error_at_line (line, "global `%s' is unimplemented type", name);
4391 /* This generates a routine to walk an array. */
4394 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4396 struct walk_type_data d;
4399 memset (&d, 0, sizeof (d));
4405 d.bitmap = get_lang_bitmap (v->line.file);
4408 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4410 if (wtd->param_prefix)
4412 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4413 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
4414 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4415 wtd->param_prefix, v->name);
4417 " ATTRIBUTE_UNUSED void *x_p,\n"
4418 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4419 " ATTRIBUTE_UNUSED void * cookie)\n");
4420 oprintf (d.of, "{\n");
4421 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4422 d.process_field = write_types_local_process_field;
4423 d.have_this_obj = true;
4424 walk_type (v->type, &d);
4425 oprintf (f, "}\n\n");
4429 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4430 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4431 wtd->prefix, v->name);
4433 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4434 d.process_field = write_types_process_field;
4435 d.have_this_obj = false;
4436 walk_type (v->type, &d);
4438 oprintf (f, "}\n\n");
4441 /* Output a table describing the locations and types of VARIABLES. */
4444 write_roots (pair_p variables, bool emit_pch)
4447 struct flist *flp = NULL;
4449 for (v = variables; v; v = v->next)
4452 get_output_file_with_visibility (CONST_CAST (input_file*,
4455 const char *length = NULL;
4456 int deletable_p = 0;
4458 for (o = v->opt; o; o = o->next)
4459 if (strcmp (o->name, "length") == 0
4460 && o->kind == OPTION_STRING)
4461 length = o->info.string;
4462 else if (strcmp (o->name, "deletable") == 0)
4464 else if (strcmp (o->name, "param_is") == 0)
4466 else if (strncmp (o->name, "param", 5) == 0
4467 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
4469 else if (strcmp (o->name, "if_marked") == 0)
4472 error_at_line (&v->line,
4473 "global `%s' has unknown option `%s'",
4476 for (fli = flp; fli; fli = fli->next)
4477 if (fli->f == f && f)
4481 fli = XNEW (struct flist);
4485 fli->file = v->line.file;
4486 gcc_assert (fli->file);
4489 oprintf (f, "\n/* GC roots. */\n\n");
4494 && v->type->kind == TYPE_POINTER
4495 && (v->type->u.p->kind == TYPE_POINTER
4496 || v->type->u.p->kind == TYPE_STRUCT))
4498 write_array (f, v, &ggc_wtd);
4499 write_array (f, v, &pch_wtd);
4503 for (v = variables; v; v = v->next)
4505 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4512 for (o = v->opt; o; o = o->next)
4513 if (strcmp (o->name, "length") == 0)
4515 else if (strcmp (o->name, "deletable") == 0
4516 || strcmp (o->name, "if_marked") == 0)
4522 for (fli = flp; fli; fli = fli->next)
4525 if (!fli->started_p)
4529 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4530 put_mangled_filename (f, v->line.file);
4531 oprintf (f, "[] = {\n");
4534 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4537 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4540 for (v = variables; v; v = v->next)
4542 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4548 for (o = v->opt; o; o = o->next)
4549 if (strcmp (o->name, "deletable") == 0)
4551 else if (strcmp (o->name, "if_marked") == 0)
4557 for (fli = flp; fli; fli = fli->next)
4560 if (!fli->started_p)
4564 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4565 put_mangled_filename (f, v->line.file);
4566 oprintf (f, "[] = {\n");
4569 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4573 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4574 "gt_ggc_deletable_rtab");
4576 for (v = variables; v; v = v->next)
4578 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4581 const char *if_marked = NULL;
4585 for (o = v->opt; o; o = o->next)
4586 if (strcmp (o->name, "length") == 0)
4588 else if (strcmp (o->name, "if_marked") == 0
4589 && o->kind == OPTION_STRING)
4590 if_marked = o->info.string;
4591 if (if_marked == NULL)
4593 if (v->type->kind != TYPE_POINTER
4594 || v->type->u.p->kind != TYPE_PARAM_STRUCT
4595 || v->type->u.p->u.param_struct.stru != find_structure ("htab",
4598 error_at_line (&v->line,
4599 "if_marked option used but not hash table");
4603 for (fli = flp; fli; fli = fli->next)
4606 if (!fli->started_p)
4610 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4611 put_mangled_filename (f, v->line.file);
4612 oprintf (f, "[] = {\n");
4615 write_root (f, v, v->type->u.p->u.param_struct.param[0],
4616 v->name, length_p, &v->line, if_marked, emit_pch);
4619 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4620 "gt_ggc_cache_rtab");
4625 for (v = variables; v; v = v->next)
4627 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4631 int if_marked_p = 0;
4634 for (o = v->opt; o; o = o->next)
4635 if (strcmp (o->name, "length") == 0)
4637 else if (strcmp (o->name, "if_marked") == 0)
4643 for (fli = flp; fli; fli = fli->next)
4646 if (!fli->started_p)
4650 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4651 put_mangled_filename (f, v->line.file);
4652 oprintf (f, "[] = {\n");
4655 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4658 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4659 "gt_pch_cache_rtab");
4661 for (v = variables; v; v = v->next)
4663 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4669 for (o = v->opt; o; o = o->next)
4670 if (strcmp (o->name, "deletable") == 0
4671 || strcmp (o->name, "if_marked") == 0)
4677 if (!contains_scalar_p (v->type))
4680 for (fli = flp; fli; fli = fli->next)
4683 if (!fli->started_p)
4687 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4688 put_mangled_filename (f, v->line.file);
4689 oprintf (f, "[] = {\n");
4692 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4696 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4697 "gt_pch_scalar_rtab");
4700 /* TRUE if type S has the GTY variable_size annotation. */
4703 variable_size_p (const type_p s)
4706 for (o = s->u.s.opt; o; o = o->next)
4707 if (strcmp (o->name, "variable_size") == 0)
4715 { any_zone, specific_zone };
4717 /* Writes one typed allocator definition into output F for type
4718 identifier TYPE_NAME with optional type specifier TYPE_SPECIFIER.
4719 The allocator name will contain ALLOCATOR_TYPE. If VARIABLE_SIZE
4720 is true, the allocator will have an extra parameter specifying
4721 number of bytes to allocate. If QUANTITY is set to VECTOR, a
4722 vector allocator will be output, if ZONE is set to SPECIFIC_ZONE,
4723 the allocator will be zone-specific. */
4726 write_typed_alloc_def (outf_p f,
4727 bool variable_size, const char *type_specifier,
4728 const char *type_name, const char *allocator_type,
4729 enum alloc_quantity quantity, enum alloc_zone zone)
4731 bool two_args = variable_size && (quantity == vector);
4732 bool third_arg = ((zone == specific_zone)
4733 && (variable_size || (quantity == vector)));
4734 gcc_assert (f != NULL);
4735 const char *type_name_as_id = filter_type_name (type_name);
4736 oprintf (f, "#define ggc_alloc_%s%s", allocator_type, type_name_as_id);
4737 oprintf (f, "(%s%s%s%s%s) ",
4738 (variable_size ? "SIZE" : ""),
4739 (two_args ? ", " : ""),
4740 (quantity == vector) ? "n" : "",
4741 (third_arg ? ", " : ""), (zone == specific_zone) ? "z" : "");
4742 oprintf (f, "((%s%s *)", type_specifier, type_name);
4743 oprintf (f, "(ggc_internal_%salloc_stat (", allocator_type);
4744 if (zone == specific_zone)
4747 oprintf (f, "SIZE");
4749 oprintf (f, "sizeof (%s%s)", type_specifier, type_name);
4750 if (quantity == vector)
4752 oprintf (f, " MEM_STAT_INFO)))\n");
4753 if (type_name_as_id != type_name)
4754 free (CONST_CAST(char *, type_name_as_id));
4757 /* Writes a typed allocator definition into output F for a struct or
4758 union S, with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4761 write_typed_struct_alloc_def (outf_p f,
4762 const type_p s, const char *allocator_type,
4763 enum alloc_quantity quantity,
4764 enum alloc_zone zone)
4766 gcc_assert (union_or_struct_p (s));
4767 write_typed_alloc_def (f, variable_size_p (s), get_type_specifier (s),
4768 s->u.s.tag, allocator_type, quantity, zone);
4771 /* Writes a typed allocator definition into output F for a typedef P,
4772 with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4775 write_typed_typedef_alloc_def (outf_p f,
4776 const pair_p p, const char *allocator_type,
4777 enum alloc_quantity quantity,
4778 enum alloc_zone zone)
4780 write_typed_alloc_def (f, variable_size_p (p->type), "", p->name,
4781 allocator_type, quantity, zone);
4784 /* Writes typed allocator definitions into output F for the types in
4785 STRUCTURES and TYPEDEFS that are used by GC. */
4788 write_typed_alloc_defns (outf_p f,
4789 const type_p structures, const pair_p typedefs)
4794 gcc_assert (f != NULL);
4796 "\n/* Allocators for known structs and unions. */\n\n");
4797 for (s = structures; s; s = s->next)
4799 if (!USED_BY_TYPED_GC_P (s))
4801 gcc_assert (union_or_struct_p (s));
4802 /* In plugin mode onput output ggc_alloc macro definitions
4803 relevant to plugin input files. */
4804 if (nb_plugin_files > 0
4805 && ((s->u.s.line.file == NULL) || !s->u.s.line.file->inpisplugin))
4807 write_typed_struct_alloc_def (f, s, "", single, any_zone);
4808 write_typed_struct_alloc_def (f, s, "cleared_", single, any_zone);
4809 write_typed_struct_alloc_def (f, s, "vec_", vector, any_zone);
4810 write_typed_struct_alloc_def (f, s, "cleared_vec_", vector, any_zone);
4811 write_typed_struct_alloc_def (f, s, "zone_", single, specific_zone);
4812 write_typed_struct_alloc_def (f, s, "zone_cleared_", single,
4814 write_typed_struct_alloc_def (f, s, "zone_vec_", vector, specific_zone);
4815 write_typed_struct_alloc_def (f, s, "zone_cleared_vec_", vector,
4819 oprintf (f, "\n/* Allocators for known typedefs. */\n");
4820 for (p = typedefs; p; p = p->next)
4823 if (!USED_BY_TYPED_GC_P (s) || (strcmp (p->name, s->u.s.tag) == 0))
4825 /* In plugin mode onput output ggc_alloc macro definitions
4826 relevant to plugin input files. */
4827 if (nb_plugin_files > 0)
4829 struct fileloc* filoc = type_fileloc(s);
4830 if (!filoc || !filoc->file->inpisplugin)
4833 write_typed_typedef_alloc_def (f, p, "", single, any_zone);
4834 write_typed_typedef_alloc_def (f, p, "cleared_", single, any_zone);
4835 write_typed_typedef_alloc_def (f, p, "vec_", vector, any_zone);
4836 write_typed_typedef_alloc_def (f, p, "cleared_vec_", vector, any_zone);
4837 write_typed_typedef_alloc_def (f, p, "zone_", single, specific_zone);
4838 write_typed_typedef_alloc_def (f, p, "zone_cleared_", single,
4840 write_typed_typedef_alloc_def (f, p, "zone_cleared_vec_", vector,
4845 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4846 guaranteee for somewhat increased readability. If name conflicts do happen,
4847 this funcion will have to be adjusted to be more like
4848 output_mangled_typename. */
4851 output_typename (outf_p of, const_type_p t)
4856 oprintf (of, "str");
4859 oprintf (of, "scalar");
4862 output_typename (of, t->u.p);
4865 case TYPE_USER_STRUCT:
4867 case TYPE_LANG_STRUCT:
4868 oprintf (of, "%s", t->u.s.tag);
4870 case TYPE_PARAM_STRUCT:
4873 for (i = 0; i < NUM_PARAM; i++)
4874 if (t->u.param_struct.param[i] != NULL)
4876 output_typename (of, t->u.param_struct.param[i]);
4879 output_typename (of, t->u.param_struct.stru);
4887 /* Writes a typed GC allocator for type S that is suitable as a callback for
4888 the splay tree implementation in libiberty. */
4891 write_splay_tree_allocator_def (const_type_p s)
4893 outf_p of = get_output_file_with_visibility (NULL);
4894 oprintf (of, "void * ggc_alloc_splay_tree_");
4895 output_typename (of, s);
4896 oprintf (of, " (int sz, void * nl)\n");
4897 oprintf (of, "{\n");
4898 oprintf (of, " return ggc_splay_alloc (");
4899 oprintf (of, "gt_e_");
4900 output_mangled_typename (of, s);
4901 oprintf (of, ", sz, nl);\n");
4902 oprintf (of, "}\n\n");
4905 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
4906 for the splay tree implementation in libiberty. */
4909 write_splay_tree_allocators (const_type_p param_structs)
4913 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
4914 for (s = param_structs; s; s = s->next)
4915 if (s->gc_used == GC_POINTED_TO)
4917 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
4918 output_typename (header_file, s);
4919 oprintf (header_file, " (int, void *);\n");
4920 write_splay_tree_allocator_def (s);
4926 /* Dumps the value of typekind KIND. */
4929 dump_typekind (int indent, enum typekind kind)
4931 printf ("%*ckind = ", indent, ' ');
4935 printf ("TYPE_SCALAR");
4938 printf ("TYPE_STRING");
4941 printf ("TYPE_STRUCT");
4943 case TYPE_USER_STRUCT:
4944 printf ("TYPE_USER_STRUCT");
4947 printf ("TYPE_UNION");
4950 printf ("TYPE_POINTER");
4953 printf ("TYPE_ARRAY");
4955 case TYPE_LANG_STRUCT:
4956 printf ("TYPE_LANG_STRUCT");
4958 case TYPE_PARAM_STRUCT:
4959 printf ("TYPE_PARAM_STRUCT");
4967 /* Dumps the value of GC_USED flag. */
4970 dump_gc_used (int indent, enum gc_used_enum gc_used)
4972 printf ("%*cgc_used = ", indent, ' ');
4976 printf ("GC_UNUSED");
4981 case GC_MAYBE_POINTED_TO:
4982 printf ("GC_MAYBE_POINTED_TO");
4985 printf ("GC_POINTED_TO");
4993 /* Dumps the type options OPT. */
4996 dump_options (int indent, options_p opt)
4999 printf ("%*coptions = ", indent, ' ');
5006 printf ("%s:string %s ", o->name, o->info.string);
5009 printf ("%s:type ", o->name);
5010 dump_type (indent+1, o->info.type);
5013 printf ("%s:nested ", o->name);
5023 /* Dumps the source file location in LINE. */
5026 dump_fileloc (int indent, struct fileloc line)
5028 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
5029 get_input_file_name (line.file),
5033 /* Recursively dumps the struct, union, or a language-specific
5037 dump_type_u_s (int indent, type_p t)
5041 gcc_assert (union_or_struct_p (t));
5042 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
5043 dump_fileloc (indent, t->u.s.line);
5044 printf ("%*cu.s.fields =\n", indent, ' ');
5045 fields = t->u.s.fields;
5048 dump_pair (indent + INDENT, fields);
5049 fields = fields->next;
5051 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
5052 dump_options (indent, t->u.s.opt);
5053 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
5054 if (t->kind == TYPE_LANG_STRUCT)
5056 printf ("%*cu.s.lang_struct:\n", indent, ' ');
5057 dump_type_list (indent + INDENT, t->u.s.lang_struct);
5061 /* Recursively dumps the array T. */
5064 dump_type_u_a (int indent, type_p t)
5066 gcc_assert (t->kind == TYPE_ARRAY);
5067 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
5068 dump_type_list (indent + INDENT, t->u.a.p);
5071 /* Recursively dumps the parameterized struct T. */
5074 dump_type_u_param_struct (int indent, type_p t)
5077 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
5078 printf ("%*cu.param_struct.stru:\n", indent, ' ');
5079 dump_type_list (indent, t->u.param_struct.stru);
5080 dump_fileloc (indent, t->u.param_struct.line);
5081 for (i = 0; i < NUM_PARAM; i++)
5083 if (t->u.param_struct.param[i] == NULL)
5085 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
5086 dump_type (indent + INDENT, t->u.param_struct.param[i]);
5090 /* Recursively dumps the type list T. */
5093 dump_type_list (int indent, type_p t)
5098 dump_type (indent, p);
5103 static htab_t seen_types;
5105 /* Recursively dumps the type T if it was not dumped previously. */
5108 dump_type (int indent, type_p t)
5112 if (seen_types == NULL)
5113 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
5115 printf ("%*cType at %p: ", indent, ' ', (void *) t);
5116 slot = htab_find_slot (seen_types, t, INSERT);
5119 printf ("already seen.\n");
5125 dump_typekind (indent, t->kind);
5126 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
5127 (void *) t->pointer_to);
5128 dump_gc_used (indent + INDENT, t->gc_used);
5132 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
5133 t->u.scalar_is_char ? "true" : "false");
5139 case TYPE_LANG_STRUCT:
5140 case TYPE_USER_STRUCT:
5141 dump_type_u_s (indent + INDENT, t);
5144 printf ("%*cp:\n", indent + INDENT, ' ');
5145 dump_type (indent + INDENT, t->u.p);
5148 dump_type_u_a (indent + INDENT, t);
5150 case TYPE_PARAM_STRUCT:
5151 dump_type_u_param_struct (indent + INDENT, t);
5156 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
5159 /* Dumps the pair P. */
5162 dump_pair (int indent, pair_p p)
5164 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
5165 dump_type (indent, p->type);
5166 dump_fileloc (indent, p->line);
5167 dump_options (indent, p->opt);
5168 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
5171 /* Dumps the list of pairs PP. */
5174 dump_pair_list (const char *name, pair_p pp)
5177 printf ("%s:\n", name);
5178 for (p = pp; p != NULL; p = p->next)
5180 printf ("End of %s\n\n", name);
5183 /* Dumps the STRUCTURES. */
5186 dump_structures (const char *name, type_p structures)
5188 printf ("%s:\n", name);
5189 dump_type_list (0, structures);
5190 printf ("End of %s\n\n", name);
5193 /* Dumps the internal structures of gengtype. This is useful to debug
5194 gengtype itself, or to understand what it does, e.g. for plugin
5198 dump_everything (void)
5200 dump_pair_list ("typedefs", typedefs);
5201 dump_structures ("structures", structures);
5202 dump_structures ("param_structs", param_structs);
5203 dump_pair_list ("variables", variables);
5205 /* Allocated with the first call to dump_type. */
5206 htab_delete (seen_types);
5211 /* Option specification for getopt_long. */
5212 static const struct option gengtype_long_options[] = {
5213 {"help", no_argument, NULL, 'h'},
5214 {"version", no_argument, NULL, 'V'},
5215 {"verbose", no_argument, NULL, 'v'},
5216 {"dump", no_argument, NULL, 'd'},
5217 {"debug", no_argument, NULL, 'D'},
5218 {"plugin", required_argument, NULL, 'P'},
5219 {"srcdir", required_argument, NULL, 'S'},
5220 {"backupdir", required_argument, NULL, 'B'},
5221 {"inputs", required_argument, NULL, 'I'},
5222 {"read-state", required_argument, NULL, 'r'},
5223 {"write-state", required_argument, NULL, 'w'},
5224 /* Terminating NULL placeholder. */
5225 {NULL, no_argument, NULL, 0},
5232 printf ("Usage: %s\n", progname);
5233 printf ("\t -h | --help " " \t# Give this help.\n");
5234 printf ("\t -D | --debug "
5235 " \t# Give debug output to debug %s itself.\n", progname);
5236 printf ("\t -V | --version " " \t# Give version information.\n");
5237 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5238 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5239 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5240 " \t# Generate for plugin.\n");
5241 printf ("\t -S | --srcdir <GCC-directory> "
5242 " \t# Specify the GCC source directory.\n");
5243 printf ("\t -B | --backupdir <directory> "
5244 " \t# Specify the backup directory for updated files.\n");
5245 printf ("\t -I | --inputs <input-list> "
5246 " \t# Specify the file with source files list.\n");
5247 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5248 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5252 print_version (void)
5254 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5255 printf ("Report bugs: %s\n", bug_report_url);
5258 /* Parse the program options using getopt_long... */
5260 parse_program_options (int argc, char **argv)
5263 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5264 gengtype_long_options, NULL)) >= 0)
5268 case 'h': /* --help */
5271 case 'V': /* --version */
5274 case 'd': /* --dump */
5277 case 'D': /* --debug */
5280 case 'v': /* --verbose */
5283 case 'P': /* --plugin */
5285 plugin_output_filename = optarg;
5287 fatal ("missing plugin output file name");
5289 case 'S': /* --srcdir */
5293 fatal ("missing source directory");
5294 srcdir_len = strlen (srcdir);
5296 case 'B': /* --backupdir */
5298 backup_dir = optarg;
5300 fatal ("missing backup directory");
5302 case 'I': /* --inputs */
5306 fatal ("missing input list");
5308 case 'r': /* --read-state */
5310 read_state_filename = optarg;
5312 fatal ("missing read state file");
5313 DBGPRINTF ("read state %s\n", optarg);
5315 case 'w': /* --write-state */
5316 DBGPRINTF ("write state %s\n", optarg);
5318 write_state_filename = optarg;
5320 fatal ("missing write state file");
5323 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5325 fatal ("unexpected flag");
5328 if (plugin_output_filename)
5330 /* In plugin mode we require some input files. */
5333 fatal ("no source files given in plugin mode");
5334 nb_plugin_files = argc - optind;
5335 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5336 for (i = 0; i < (int) nb_plugin_files; i++)
5338 char *name = argv[i + optind];
5339 plugin_files[i] = input_file_by_name (name);
5346 /******* Manage input files. ******/
5348 /* Hash table of unique input file names. */
5349 static htab_t input_file_htab;
5351 /* Find or allocate a new input_file by hash-consing it. */
5353 input_file_by_name (const char* name)
5356 input_file* f = NULL;
5360 namlen = strlen (name);
5361 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5364 f->inpisplugin = false;
5365 strcpy (f->inpname, name);
5366 slot = htab_find_slot (input_file_htab, f, INSERT);
5367 gcc_assert (slot != NULL);
5370 /* Already known input file. */
5372 return (input_file*)(*slot);
5374 /* New input file. */
5379 /* Hash table support routines for input_file-s. */
5381 htab_hash_inputfile (const void *p)
5383 const input_file *inpf = (const input_file *) p;
5385 return htab_hash_string (get_input_file_name (inpf));
5389 htab_eq_inputfile (const void *x, const void *y)
5391 const input_file *inpfx = (const input_file *) x;
5392 const input_file *inpfy = (const input_file *) y;
5393 gcc_assert (inpfx != NULL && inpfy != NULL);
5394 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5399 main (int argc, char **argv)
5402 static struct fileloc pos = { NULL, 0 };
5403 outf_p output_header;
5405 /* Mandatory common initializations. */
5406 progname = "gengtype"; /* For fatal and messages. */
5407 /* Create the hash-table used to hash-cons input files. */
5409 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5410 /* Initialize our special input files. */
5411 this_file = input_file_by_name (__FILE__);
5412 system_h_file = input_file_by_name ("system.h");
5413 /* Set the scalar_is_char union number for predefined scalar types. */
5414 scalar_nonchar.u.scalar_is_char = FALSE;
5415 scalar_char.u.scalar_is_char = TRUE;
5417 parse_program_options (argc, argv);
5422 time_t now = (time_t) 0;
5424 DBGPRINTF ("gengtype started pid %d at %s",
5425 (int) getpid (), ctime (&now));
5427 #endif /* ENABLE_CHECKING */
5429 /* Parse the input list and the input files. */
5430 DBGPRINTF ("inputlist %s", inputlist);
5431 if (read_state_filename)
5434 fatal ("input list %s cannot be given with a read state file %s",
5435 inputlist, read_state_filename);
5436 read_state (read_state_filename);
5437 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5438 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs);
5442 /* These types are set up with #define or else outside of where
5443 we can see them. We should initialize them before calling
5445 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5447 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5448 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5449 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5450 POS_HERE (do_scalar_typedef ("double_int", &pos));
5451 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5452 POS_HERE (do_scalar_typedef ("uint8", &pos));
5453 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5454 POS_HERE (do_scalar_typedef ("jword", &pos));
5455 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5456 POS_HERE (do_scalar_typedef ("void", &pos));
5457 POS_HERE (do_typedef ("PTR",
5458 create_pointer (resolve_typedef ("void", &pos)),
5461 read_input_list (inputlist);
5462 for (i = 0; i < num_gt_files; i++)
5464 parse_file (get_input_file_name (gt_files[i]));
5465 DBGPRINTF ("parsed file #%d %s",
5466 (int) i, get_input_file_name (gt_files[i]));
5468 if (verbosity_level >= 1)
5469 printf ("%s parsed %d files with %d GTY types\n",
5470 progname, (int) num_gt_files, type_count);
5472 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5473 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
5477 fatal ("either an input list or a read state file should be given");
5482 if (plugin_output_filename)
5485 /* In plugin mode, we should have read a state file, and have
5486 given at least one plugin file. */
5487 if (!read_state_filename)
5488 fatal ("No read state given in plugin mode for %s",
5489 plugin_output_filename);
5491 if (nb_plugin_files == 0 || !plugin_files)
5492 fatal ("No plugin files given in plugin mode for %s",
5493 plugin_output_filename);
5495 /* Parse our plugin files and augment the state. */
5496 for (ix = 0; ix < nb_plugin_files; ix++)
5498 input_file* pluginput = plugin_files [ix];
5499 pluginput->inpisplugin = true;
5500 parse_file (get_input_file_name (pluginput));
5505 plugin_output = create_file ("GCC", plugin_output_filename);
5506 DBGPRINTF ("created plugin_output %p named %s",
5507 (void *) plugin_output, plugin_output->name);
5510 { /* No plugin files, we are in normal mode. */
5512 fatal ("gengtype needs a source directory in normal mode");
5519 /* The call to set_gc_used may indirectly call find_param_structure
5520 hence enlarge the param_structs list of types. */
5521 set_gc_used (variables);
5523 /* The state at this point is read from the state input file or by
5524 parsing source files and optionally augmented by parsing plugin
5525 source files. Write it now. */
5526 if (write_state_filename)
5528 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5529 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs);
5532 fatal ("didn't write state file %s after errors",
5533 write_state_filename);
5535 DBGPRINTF ("before write_state %s", write_state_filename);
5536 write_state (write_state_filename);
5541 /* After having written the state file we return immediately to
5542 avoid generating any output file. */
5552 write_enum_defn (structures, param_structs);
5553 output_header = plugin_output ? plugin_output : header_file;
5554 write_typed_alloc_defns (output_header, structures, typedefs);
5555 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5557 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5560 write_types (output_header, structures, param_structs, &ggc_wtd);
5561 if (plugin_files == NULL)
5563 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5565 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5567 write_types (header_file, structures, param_structs, &pch_wtd);
5568 write_local (header_file, structures, param_structs);
5570 write_splay_tree_allocators (param_structs);
5571 write_roots (variables, plugin_files == NULL);
5573 close_output_files ();
5578 /* Don't bother about free-ing any input or plugin file, etc. */