1 /* Process source files and output type information.
2 Copyright (C) 2002-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #ifdef HOST_GENERATOR_FILE
22 #define GENERATOR_FILE 1
27 #include "errors.h" /* for fatal */
29 #include "version.h" /* for version_string & pkgversion_string. */
33 #include "filenames.h"
35 /* Data types, macros, etc. used only in this file. */
38 /* The list of output files. */
41 /* The output header file that is included into pretty much every
46 /* The name of the file containing the list of input files. */
47 static char *inputlist
;
49 /* The plugin input files and their number; in that case only
50 a single file is produced. */
51 static input_file
**plugin_files
;
52 static size_t nb_plugin_files
;
54 /* The generated plugin output file and name. */
55 static outf_p plugin_output
;
56 static char *plugin_output_filename
;
58 /* Our source directory and its length. */
62 /* Variables used for reading and writing the state. */
63 const char *read_state_filename
;
64 const char *write_state_filename
;
66 /* Variables to help debugging. */
70 /* Level for verbose messages. */
73 /* We have a type count and use it to set the state_number of newly
74 allocated types to some unique negative number. */
75 static int type_count
;
77 /* The backup directory should be in the same file system as the
78 generated files, otherwise the rename(2) system call would fail.
79 If NULL, no backup is made when overwriting a generated file. */
80 static const char* backup_dir
; /* (-B) program option. */
83 static outf_p
create_file (const char *, const char *);
85 static const char *get_file_basename (const input_file
*);
86 static const char *get_file_realbasename (const input_file
*);
88 static int get_prefix_langdir_index (const char *);
89 static const char *get_file_langdir (const input_file
*);
91 static void dump_pair (int indent
, pair_p p
);
92 static void dump_type (int indent
, type_p p
);
93 static void dump_type_list (int indent
, type_p p
);
96 /* Nonzero iff an error has occurred. */
97 bool hit_error
= false;
99 static void gen_rtx_next (void);
100 static void write_rtx_next (void);
101 static void open_base_files (void);
102 static void close_output_files (void);
104 /* Report an error at POS, printing MSG. */
107 error_at_line (const struct fileloc
*pos
, const char *msg
, ...)
111 gcc_assert (pos
!= NULL
&& pos
->file
!= NULL
);
114 fprintf (stderr
, "%s:%d: ", get_input_file_name (pos
->file
), pos
->line
);
115 vfprintf (stderr
, msg
, ap
);
116 fputc ('\n', stderr
);
122 /* Locate the ultimate base class of struct S. */
125 get_ultimate_base_class (const_type_p s
)
127 while (s
->u
.s
.base_class
)
128 s
= s
->u
.s
.base_class
;
133 get_ultimate_base_class (type_p s
)
135 while (s
->u
.s
.base_class
)
136 s
= s
->u
.s
.base_class
;
140 /* Input file handling. */
142 /* Table of all input files. */
143 const input_file
**gt_files
;
146 /* Table of headers to be included in gtype-desc.c that are generated
147 during the build. These are identified as "./<filename>.h". */
148 const char **build_headers
;
149 size_t num_build_headers
;
151 /* A number of places use the name of this "gengtype.c" file for a
152 location for things that we can't rely on the source to define.
153 Make sure we can still use pointer comparison on filenames. */
154 input_file
* this_file
;
155 /* The "system.h" file is likewise specially useful. */
156 input_file
* system_h_file
;
158 /* Vector of per-language directories. */
159 const char **lang_dir_names
;
160 size_t num_lang_dirs
;
162 /* An array of output files suitable for definitions. There is one
163 BASE_FILES entry for each language. */
164 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;
174 int nb_user_struct
= 0, nb_undefined
= 0;
176 for (p
= t
; p
; p
= p
->next
)
193 case TYPE_USER_STRUCT
:
205 case TYPE_LANG_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)
221 fprintf (stderr
, "@@%%@@ %d lang_structs\n", nb_lang_struct
);
222 if (nb_user_struct
> 0)
223 fprintf (stderr
, "@@%%@@ %d user_structs\n", nb_user_struct
);
224 if (nb_undefined
> 0)
225 fprintf (stderr
, "@@%%@@ %d undefined types\n", nb_undefined
);
226 fprintf (stderr
, "\n");
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. */
500 pair_p typedefs
= NULL
;
501 type_p structures
= NULL
;
502 pair_p variables
= NULL
;
504 static type_p
adjust_field_tree_exp (type_p t
, options_p opt
);
505 static type_p
adjust_field_rtx_def (type_p t
, options_p opt
);
507 /* Define S as a typedef to T at POS. */
510 do_typedef (const char *s
, type_p t
, struct fileloc
*pos
)
514 /* temporary kludge - gengtype doesn't handle conditionals or
515 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
516 is coming from this file (main() sets them up with safe dummy
518 if (!strcmp (s
, "CUMULATIVE_ARGS") && pos
->file
!= this_file
)
521 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
522 if (strcmp (p
->name
, s
) == 0)
524 if (p
->type
!= t
&& strcmp (s
, "result_type") != 0)
526 error_at_line (pos
, "type `%s' previously defined", s
);
527 error_at_line (&p
->line
, "previously defined here");
532 p
= XNEW (struct pair
);
541 /* Define S as a typename of a scalar. Cannot be used to define
542 typedefs of 'char'. Note: is also used for pointer-to-function
543 typedefs (which are therefore not treated as pointers). */
546 do_scalar_typedef (const char *s
, struct fileloc
*pos
)
548 do_typedef (s
, &scalar_nonchar
, pos
);
551 /* Similar to strtok_r. */
554 strtoken (char *str
, const char *delim
, char **next
)
561 /* Skip the leading delimiters. */
562 str
+= strspn (str
, delim
);
564 /* This is an empty token. */
567 /* The current token. */
570 /* Find the next delimiter. */
571 str
+= strcspn (str
, delim
);
573 /* This is the last token. */
577 /* Terminate the current token. */
579 /* Advance to the next token. */
586 /* Define TYPE_NAME to be a user defined type at location POS. */
589 create_user_defined_type (const char *type_name
, struct fileloc
*pos
)
591 type_p ty
= find_structure (type_name
, TYPE_USER_STRUCT
);
593 /* We might have already seen an incomplete decl of the given type,
594 in which case we won't have yet seen a GTY((user)), and the type will
595 only have kind "TYPE_STRUCT". Mark it as a user struct. */
596 ty
->kind
= TYPE_USER_STRUCT
;
599 ty
->u
.s
.bitmap
= get_lang_bitmap (pos
->file
);
600 do_typedef (type_name
, ty
, pos
);
602 /* If TYPE_NAME specifies a template, create references to the types
603 in the template by pretending that each type is a field of TY.
604 This is needed to make sure that the types referenced by the
605 template are marked as used. */
606 char *str
= xstrdup (type_name
);
607 char *open_bracket
= strchr (str
, '<');
610 /* We only accept simple template declarations (see
611 require_template_declaration), so we only need to parse a
612 comma-separated list of strings, implicitly assumed to
613 be type names, potentially with "*" characters. */
614 char *arg
= open_bracket
+ 1;
615 /* Workaround -Wmaybe-uninitialized false positive during
616 profiledbootstrap by initializing it. */
618 char *type_id
= strtoken (arg
, ",>", &next
);
622 /* Create a new field for every type found inside the template
625 /* Support a single trailing "*" character. */
626 const char *star
= strchr (type_id
, '*');
627 int is_ptr
= (star
!= NULL
);
628 size_t offset_to_star
= star
- type_id
;
630 offset_to_star
= star
- type_id
;
632 if (strstr (type_id
, "char*"))
634 type_id
= strtoken (0, ",>", &next
);
638 char *field_name
= xstrdup (type_id
);
643 /* Strip off the first '*' character (and any subsequent text). */
644 *(field_name
+ offset_to_star
) = '\0';
646 arg_type
= find_structure (field_name
, TYPE_STRUCT
);
647 arg_type
= create_pointer (arg_type
);
650 arg_type
= resolve_typedef (field_name
, pos
);
652 fields
= create_field_at (fields
, arg_type
, field_name
, 0, pos
);
653 type_id
= strtoken (0, ",>", &next
);
656 /* Associate the field list to TY. */
657 ty
->u
.s
.fields
= fields
;
665 /* Given a typedef name S, return its associated type. Return NULL if
666 S is not a registered type name. */
669 type_for_name (const char *s
)
673 /* Special-case support for types within a "gcc::" namespace. Rather
674 than fully-supporting namespaces, simply strip off the "gcc::" prefix
675 where present. This allows us to have GTY roots of this form:
676 extern GTY(()) gcc::some_type *some_ptr;
677 where the autogenerated functions will refer to simply "some_type",
678 where they can be resolved into their namespace. */
679 if (startswith (s
, "gcc::"))
682 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
683 if (strcmp (p
->name
, s
) == 0)
689 /* Create an undefined type with name S and location POS. Return the
690 newly created type. */
693 create_undefined_type (const char *s
, struct fileloc
*pos
)
695 type_p ty
= find_structure (s
, TYPE_UNDEFINED
);
697 ty
->u
.s
.bitmap
= get_lang_bitmap (pos
->file
);
698 do_typedef (s
, ty
, pos
);
703 /* Return the type previously defined for S. Use POS to report errors. */
706 resolve_typedef (const char *s
, struct fileloc
*pos
)
708 bool is_template_instance
= (strchr (s
, '<') != NULL
);
709 type_p p
= type_for_name (s
);
711 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
712 type for regular type identifiers. If the type identifier S is a
713 template instantiation, however, we treat it as a user defined
716 FIXME, this is actually a limitation in gengtype. Supporting
717 template types and their instances would require keeping separate
718 track of the basic types definition and its instances. This
719 essentially forces all template classes in GC to be marked
722 p
= (is_template_instance
)
723 ? create_user_defined_type (s
, pos
)
724 : create_undefined_type (s
, pos
);
729 /* Add SUBCLASS to head of linked list of BASE's subclasses. */
731 void add_subclass (type_p base
, type_p subclass
)
733 gcc_assert (union_or_struct_p (base
));
734 gcc_assert (union_or_struct_p (subclass
));
736 subclass
->u
.s
.next_sibling_class
= base
->u
.s
.first_subclass
;
737 base
->u
.s
.first_subclass
= subclass
;
740 /* Create and return a new structure with tag NAME at POS with fields
741 FIELDS and options O. The KIND of structure must be one of
742 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
745 new_structure (const char *name
, enum typekind kind
, struct fileloc
*pos
,
746 pair_p fields
, options_p o
, type_p base_class
)
750 lang_bitmap bitmap
= get_lang_bitmap (pos
->file
);
751 bool isunion
= (kind
== TYPE_UNION
);
752 type_p
*p
= &structures
;
754 gcc_assert (union_or_struct_p (kind
));
756 for (si
= structures
; si
!= NULL
; p
= &si
->next
, si
= *p
)
757 if (strcmp (name
, si
->u
.s
.tag
) == 0 && UNION_P (si
) == isunion
)
760 if (si
->kind
== TYPE_LANG_STRUCT
)
764 for (si
= ls
->u
.s
.lang_struct
; si
!= NULL
; si
= si
->next
)
765 if (si
->u
.s
.bitmap
== bitmap
)
768 else if (si
->u
.s
.line
.file
!= NULL
&& si
->u
.s
.bitmap
!= bitmap
)
772 si
= XCNEW (struct type
);
773 memcpy (si
, ls
, sizeof (struct type
));
774 ls
->kind
= TYPE_LANG_STRUCT
;
775 ls
->u
.s
.lang_struct
= si
;
776 ls
->u
.s
.fields
= NULL
;
778 si
->state_number
= -type_count
;
779 si
->pointer_to
= NULL
;
780 si
->u
.s
.lang_struct
= ls
;
785 if (ls
!= NULL
&& s
== NULL
)
788 s
= XCNEW (struct type
);
789 s
->state_number
= -type_count
;
790 s
->next
= ls
->u
.s
.lang_struct
;
791 ls
->u
.s
.lang_struct
= s
;
792 s
->u
.s
.lang_struct
= ls
;
800 s
= XCNEW (struct type
);
801 s
->state_number
= -type_count
;
805 if (s
->u
.s
.lang_struct
&& (s
->u
.s
.lang_struct
->u
.s
.bitmap
& bitmap
))
807 error_at_line (pos
, "duplicate definition of '%s %s'",
808 isunion
? "union" : "struct", s
->u
.s
.tag
);
809 error_at_line (&s
->u
.s
.line
, "previous definition here");
815 s
->u
.s
.fields
= fields
;
817 s
->u
.s
.bitmap
= bitmap
;
818 if (s
->u
.s
.lang_struct
)
819 s
->u
.s
.lang_struct
->u
.s
.bitmap
|= bitmap
;
820 s
->u
.s
.base_class
= base_class
;
822 add_subclass (base_class
, s
);
827 /* Return the previously-defined structure or union with tag NAME,
828 or a new empty structure or union if none was defined previously.
829 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
833 find_structure (const char *name
, enum typekind kind
)
836 bool isunion
= (kind
== TYPE_UNION
);
837 type_p
*p
= &structures
;
839 gcc_assert (kind
== TYPE_UNDEFINED
|| union_or_struct_p (kind
));
841 for (s
= structures
; s
!= NULL
; p
= &s
->next
, s
= *p
)
842 if (strcmp (name
, s
->u
.s
.tag
) == 0 && UNION_P (s
) == isunion
)
846 s
= XCNEW (struct type
);
847 s
->state_number
= -type_count
;
854 /* Return a scalar type with name NAME. */
857 create_scalar_type (const char *name
)
859 if (!strcmp (name
, "char") || !strcmp (name
, "unsigned char"))
862 return &scalar_nonchar
;
866 /* Return a pointer to T. */
869 create_pointer (type_p t
)
873 type_p r
= XCNEW (struct type
);
875 r
->state_number
= -type_count
;
876 r
->kind
= TYPE_POINTER
;
880 return t
->pointer_to
;
883 /* Return an array of length LEN. */
886 create_array (type_p t
, const char *len
)
891 v
= XCNEW (struct type
);
892 v
->kind
= TYPE_ARRAY
;
893 v
->state_number
= -type_count
;
899 /* Return a string options structure with name NAME and info INFO.
900 NEXT is the next option in the chain. */
902 create_string_option (options_p next
, const char *name
, const char *info
)
904 options_p o
= XNEW (struct options
);
905 o
->kind
= OPTION_STRING
;
908 o
->info
.string
= info
;
912 /* Create a type options structure with name NAME and info INFO. NEXT
913 is the next option in the chain. */
915 create_type_option (options_p next
, const char* name
, type_p info
)
917 options_p o
= XNEW (struct options
);
920 o
->kind
= OPTION_TYPE
;
925 /* Create a nested pointer options structure with name NAME and info
926 INFO. NEXT is the next option in the chain. */
928 create_nested_option (options_p next
, const char* name
,
929 struct nested_ptr_data
* info
)
932 o
= XNEW (struct options
);
935 o
->kind
= OPTION_NESTED
;
936 o
->info
.nested
= info
;
940 /* Return an options structure for a "nested_ptr" option. */
942 create_nested_ptr_option (options_p next
, type_p t
,
943 const char *to
, const char *from
)
945 struct nested_ptr_data
*d
= XNEW (struct nested_ptr_data
);
947 d
->type
= adjust_field_type (t
, 0);
949 d
->convert_from
= from
;
950 return create_nested_option (next
, "nested_ptr", d
);
953 /* Add a variable named S of type T with options O defined at POS,
956 note_variable (const char *s
, type_p t
, options_p o
, struct fileloc
*pos
)
959 n
= XNEW (struct pair
);
968 /* Most-general structure field creator. */
970 create_field_all (pair_p next
, type_p type
, const char *name
, options_p opt
,
971 const input_file
*inpf
, int line
)
975 field
= XNEW (struct pair
);
980 field
->line
.file
= inpf
;
981 field
->line
.line
= line
;
985 /* Create a field that came from the source code we are scanning,
986 i.e. we have a 'struct fileloc', and possibly options; also,
987 adjust_field_type should be called. */
989 create_field_at (pair_p next
, type_p type
, const char *name
, options_p opt
,
992 return create_field_all (next
, adjust_field_type (type
, opt
),
993 name
, opt
, pos
->file
, pos
->line
);
996 /* Create a fake field with the given type and name. NEXT is the next
997 field in the chain. */
998 #define create_field(next,type,name) \
999 create_field_all (next,type,name, 0, this_file, __LINE__)
1001 /* Like create_field, but the field is only valid when condition COND
1005 create_optional_field_ (pair_p next
, type_p type
, const char *name
,
1006 const char *cond
, int line
)
1009 pair_p union_fields
;
1012 /* Create a fake union type with a single nameless field of type TYPE.
1013 The field has a tag of "1". This allows us to make the presence
1014 of a field of type TYPE depend on some boolean "desc" being true. */
1015 union_fields
= create_field (NULL
, type
, "");
1017 create_string_option (union_fields
->opt
, "dot", "");
1019 create_string_option (union_fields
->opt
, "tag", "1");
1021 new_structure (xasprintf ("%s_%d", "fake_union", id
++), TYPE_UNION
,
1022 &lexer_line
, union_fields
, NULL
, NULL
);
1024 /* Create the field and give it the new fake union type. Add a "desc"
1025 tag that specifies the condition under which the field is valid. */
1026 return create_field_all (next
, union_type
, name
,
1027 create_string_option (0, "desc", cond
),
1031 #define create_optional_field(next,type,name,cond) \
1032 create_optional_field_(next,type,name,cond,__LINE__)
1034 /* Reverse a linked list of 'struct pair's in place. */
1036 nreverse_pairs (pair_p list
)
1038 pair_p prev
= 0, p
, next
;
1039 for (p
= list
; p
; p
= next
)
1049 /* We don't care how long a CONST_DOUBLE is. */
1050 #define CONST_DOUBLE_FORMAT "ww"
1051 /* We don't want to see codes that are only for generator files. */
1052 #undef GENERATOR_FILE
1056 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1062 static const char *const rtx_name
[NUM_RTX_CODE
] = {
1063 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1068 static const char *const rtx_format
[NUM_RTX_CODE
] = {
1069 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1074 static int rtx_next_new
[NUM_RTX_CODE
];
1076 /* We also need codes and names for insn notes (not register notes).
1077 Note that we do *not* bias the note values here. */
1080 #define DEF_INSN_NOTE(NAME) NAME,
1081 #include "insn-notes.def"
1082 #undef DEF_INSN_NOTE
1087 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1088 default field for line number notes. */
1089 static const char *const note_insn_name
[NOTE_INSN_MAX
+ 1] = {
1090 #define DEF_INSN_NOTE(NAME) #NAME,
1091 #include "insn-notes.def"
1092 #undef DEF_INSN_NOTE
1095 #undef CONST_DOUBLE_FORMAT
1096 #define GENERATOR_FILE
1098 /* Generate the contents of the rtx_next array. This really doesn't belong
1099 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1105 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1109 rtx_next_new
[i
] = -1;
1110 if (startswith (rtx_format
[i
], "uu"))
1111 rtx_next_new
[i
] = 1;
1112 else if (i
== COND_EXEC
|| i
== SET
|| i
== EXPR_LIST
|| i
== INSN_LIST
)
1113 rtx_next_new
[i
] = 1;
1115 for (k
= strlen (rtx_format
[i
]) - 1; k
>= 0; k
--)
1116 if (rtx_format
[i
][k
] == 'e' || rtx_format
[i
][k
] == 'u')
1117 rtx_next_new
[i
] = k
;
1121 /* Write out the contents of the rtx_next array. */
1123 write_rtx_next (void)
1125 outf_p f
= get_output_file_with_visibility (NULL
);
1130 oprintf (f
, "\n/* Used to implement the RTX_NEXT macro. */\n");
1131 oprintf (f
, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1132 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1133 if (rtx_next_new
[i
] == -1)
1134 oprintf (f
, " 0,\n");
1137 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new
[i
]);
1138 oprintf (f
, "};\n");
1141 /* Handle `special("rtx_def")'. This is a special case for field
1142 `fld' of struct rtx_def, which is an array of unions whose values
1143 are based in a complex way on the type of RTL. */
1146 adjust_field_rtx_def (type_p t
, options_p
ARG_UNUSED (opt
))
1151 type_p rtx_tp
, rtvec_tp
, tree_tp
, mem_attrs_tp
, note_union_tp
, scalar_tp
;
1152 type_p basic_block_tp
, reg_attrs_tp
, constant_tp
, symbol_union_tp
;
1154 if (t
->kind
!= TYPE_UNION
)
1156 error_at_line (&lexer_line
,
1157 "special `rtx_def' must be applied to a union");
1158 return &string_type
;
1161 nodot
= create_string_option (NULL
, "dot", "");
1163 rtx_tp
= create_pointer (find_structure ("rtx_def", TYPE_STRUCT
));
1164 rtvec_tp
= create_pointer (find_structure ("rtvec_def", TYPE_STRUCT
));
1165 tree_tp
= create_pointer (find_structure ("tree_node", TYPE_UNION
));
1166 mem_attrs_tp
= create_pointer (find_structure ("mem_attrs", TYPE_STRUCT
));
1168 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT
));
1170 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT
));
1172 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT
));
1173 scalar_tp
= &scalar_nonchar
; /* rtunion int */
1176 pair_p note_flds
= NULL
;
1179 for (c
= 0; c
<= NOTE_INSN_MAX
; c
++)
1184 case NOTE_INSN_DELETED_LABEL
:
1185 case NOTE_INSN_DELETED_DEBUG_LABEL
:
1186 note_flds
= create_field (note_flds
, &string_type
, "rt_str");
1189 case NOTE_INSN_BLOCK_BEG
:
1190 case NOTE_INSN_BLOCK_END
:
1191 note_flds
= create_field (note_flds
, tree_tp
, "rt_tree");
1194 case NOTE_INSN_VAR_LOCATION
:
1195 note_flds
= create_field (note_flds
, rtx_tp
, "rt_rtx");
1199 note_flds
= create_field (note_flds
, scalar_tp
, "rt_int");
1202 /* NOTE_INSN_MAX is used as the default field for line
1204 if (c
== NOTE_INSN_MAX
)
1206 create_string_option (nodot
, "default", "");
1209 create_string_option (nodot
, "tag", note_insn_name
[c
]);
1211 note_union_tp
= new_structure ("rtx_def_note_subunion", TYPE_UNION
,
1212 &lexer_line
, note_flds
, NULL
, NULL
);
1214 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1217 sym_flds
= create_field (NULL
, tree_tp
, "rt_tree");
1218 sym_flds
->opt
= create_string_option (nodot
, "default", "");
1219 sym_flds
= create_field (sym_flds
, constant_tp
, "rt_constant");
1220 sym_flds
->opt
= create_string_option (nodot
, "tag", "1");
1221 symbol_union_tp
= new_structure ("rtx_def_symbol_subunion", TYPE_UNION
,
1222 &lexer_line
, sym_flds
, NULL
, NULL
);
1224 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1226 pair_p subfields
= NULL
;
1227 size_t aindex
, nmindex
;
1232 for (aindex
= 0; aindex
< strlen (rtx_format
[i
]); aindex
++)
1235 const char *subname
;
1237 switch (rtx_format
[i
][aindex
])
1250 subname
= "rt_subreg";
1254 if (i
== MEM
&& aindex
== 1)
1255 t
= mem_attrs_tp
, subname
= "rt_mem";
1256 else if (i
== JUMP_INSN
&& aindex
== 7)
1257 t
= rtx_tp
, subname
= "rt_rtx";
1258 else if (i
== CODE_LABEL
&& aindex
== 4)
1259 t
= scalar_tp
, subname
= "rt_int";
1260 else if (i
== CODE_LABEL
&& aindex
== 3)
1261 t
= rtx_tp
, subname
= "rt_rtx";
1262 else if (i
== LABEL_REF
&& (aindex
== 1 || aindex
== 2))
1263 t
= rtx_tp
, subname
= "rt_rtx";
1264 else if (i
== NOTE
&& aindex
== 3)
1265 t
= note_union_tp
, subname
= "";
1266 else if (i
== NOTE
&& aindex
== 4)
1267 t
= scalar_tp
, subname
= "rt_int";
1268 else if (i
== NOTE
&& aindex
>= 6)
1269 t
= scalar_tp
, subname
= "rt_int";
1270 else if (i
== ADDR_DIFF_VEC
&& aindex
== 4)
1271 t
= scalar_tp
, subname
= "rt_int";
1272 else if (i
== VALUE
&& aindex
== 0)
1273 t
= scalar_tp
, subname
= "rt_int";
1274 else if (i
== DEBUG_EXPR
&& aindex
== 0)
1275 t
= tree_tp
, subname
= "rt_tree";
1276 else if (i
== SYMBOL_REF
&& aindex
== 1)
1277 t
= symbol_union_tp
, subname
= "";
1278 else if (i
== JUMP_TABLE_DATA
&& aindex
>= 4)
1279 t
= scalar_tp
, subname
= "rt_int";
1280 else if (i
== BARRIER
&& aindex
>= 2)
1281 t
= scalar_tp
, subname
= "rt_int";
1282 else if (i
== ENTRY_VALUE
&& aindex
== 0)
1283 t
= rtx_tp
, subname
= "rt_rtx";
1288 "rtx type `%s' has `0' in position %lu, can't handle",
1289 rtx_name
[i
], (unsigned long) aindex
);
1311 subname
= "rt_rtvec";
1316 subname
= "rt_tree";
1327 "rtx type `%s' has `%c' in position %lu, can't handle",
1328 rtx_name
[i
], rtx_format
[i
][aindex
],
1329 (unsigned long) aindex
);
1335 subfields
= create_field (subfields
, t
,
1336 xasprintf (".fld[%lu].%s",
1337 (unsigned long) aindex
,
1339 subfields
->opt
= nodot
;
1340 if (t
== note_union_tp
)
1342 create_string_option (subfields
->opt
, "desc",
1344 if (t
== symbol_union_tp
)
1346 create_string_option (subfields
->opt
, "desc",
1347 "CONSTANT_POOL_ADDRESS_P (&%0)");
1351 subfields
= create_field (subfields
, reg_attrs_tp
, "reg.attrs");
1353 if (i
== SYMBOL_REF
)
1355 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1357 type_p field_tp
= find_structure ("block_symbol", TYPE_STRUCT
);
1359 = create_optional_field (subfields
, field_tp
, "block_sym",
1360 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1363 sname
= xasprintf ("rtx_def_%s", rtx_name
[i
]);
1364 substruct
= new_structure (sname
, TYPE_STRUCT
, &lexer_line
, subfields
,
1367 ftag
= xstrdup (rtx_name
[i
]);
1368 for (nmindex
= 0; nmindex
< strlen (ftag
); nmindex
++)
1369 ftag
[nmindex
] = TOUPPER (ftag
[nmindex
]);
1370 flds
= create_field (flds
, substruct
, "");
1371 flds
->opt
= create_string_option (nodot
, "tag", ftag
);
1373 return new_structure ("rtx_def_subunion", TYPE_UNION
, &lexer_line
, flds
,
1377 /* Handle `special("tree_exp")'. This is a special case for
1378 field `operands' of struct tree_exp, which although it claims to contain
1379 pointers to trees, actually sometimes contains pointers to RTL too.
1380 Passed T, the old type of the field, and OPT its options. Returns
1381 a new type for the field. */
1384 adjust_field_tree_exp (type_p t
, options_p opt ATTRIBUTE_UNUSED
)
1389 if (t
->kind
!= TYPE_ARRAY
)
1391 error_at_line (&lexer_line
,
1392 "special `tree_exp' must be applied to an array");
1393 return &string_type
;
1396 nodot
= create_string_option (NULL
, "dot", "");
1398 flds
= create_field (NULL
, t
, "");
1399 flds
->opt
= create_string_option (nodot
, "length",
1400 "TREE_OPERAND_LENGTH ((tree) &%0)");
1401 flds
->opt
= create_string_option (flds
->opt
, "default", "");
1403 return new_structure ("tree_exp_subunion", TYPE_UNION
, &lexer_line
, flds
,
1407 /* Perform any special processing on a type T, about to become the type
1408 of a field. Return the appropriate type for the field.
1410 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1411 - Similarly for arrays of pointer-to-char;
1412 - Converts structures for which a parameter is provided to
1414 - Handles "special" options.
1418 adjust_field_type (type_p t
, options_p opt
)
1421 const int pointer_p
= t
->kind
== TYPE_POINTER
;
1423 for (; opt
; opt
= opt
->next
)
1424 if (strcmp (opt
->name
, "length") == 0)
1427 error_at_line (&lexer_line
, "duplicate `%s' option", opt
->name
);
1428 if (t
->u
.p
->kind
== TYPE_SCALAR
|| t
->u
.p
->kind
== TYPE_STRING
)
1430 error_at_line (&lexer_line
,
1431 "option `%s' may not be applied to "
1432 "arrays of atomic types", opt
->name
);
1436 else if (strcmp (opt
->name
, "special") == 0
1437 && opt
->kind
== OPTION_STRING
)
1439 const char *special_name
= opt
->info
.string
;
1440 if (strcmp (special_name
, "tree_exp") == 0)
1441 t
= adjust_field_tree_exp (t
, opt
);
1442 else if (strcmp (special_name
, "rtx_def") == 0)
1443 t
= adjust_field_rtx_def (t
, opt
);
1445 error_at_line (&lexer_line
, "unknown special `%s'", special_name
);
1449 && pointer_p
&& t
->u
.p
->kind
== TYPE_SCALAR
&& t
->u
.p
->u
.scalar_is_char
)
1450 return &string_type
;
1451 if (t
->kind
== TYPE_ARRAY
&& t
->u
.a
.p
->kind
== TYPE_POINTER
1452 && t
->u
.a
.p
->u
.p
->kind
== TYPE_SCALAR
1453 && t
->u
.a
.p
->u
.p
->u
.scalar_is_char
)
1454 return create_array (&string_type
, t
->u
.a
.len
);
1460 static void set_gc_used_type (type_p
, enum gc_used_enum
, bool = false);
1461 static void set_gc_used (pair_p
);
1463 /* Handle OPT for set_gc_used_type. */
1466 process_gc_options (options_p opt
, enum gc_used_enum level
, int *maybe_undef
,
1467 int *length
, int *skip
, type_p
*nested_ptr
)
1470 for (o
= opt
; o
; o
= o
->next
)
1471 if (strcmp (o
->name
, "ptr_alias") == 0 && level
== GC_POINTED_TO
1472 && o
->kind
== OPTION_TYPE
)
1473 set_gc_used_type (o
->info
.type
,
1475 else if (strcmp (o
->name
, "maybe_undef") == 0)
1477 else if (strcmp (o
->name
, "length") == 0)
1479 else if (strcmp (o
->name
, "skip") == 0)
1481 else if (strcmp (o
->name
, "nested_ptr") == 0
1482 && o
->kind
== OPTION_NESTED
)
1483 *nested_ptr
= ((const struct nested_ptr_data
*) o
->info
.nested
)->type
;
1487 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1489 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1490 are set to GC_UNUSED. Otherwise, an error is emitted for
1491 TYPE_UNDEFINED types. This is used to support user-defined
1492 template types with non-type arguments.
1494 For instance, when we parse a template type with enum arguments
1495 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1496 artificial fields for 'MyType', one for 'AnotherType', the other
1497 one for 'EnumValue'.
1499 At the time that we parse this type we don't know that 'EnumValue'
1500 is really an enum value, so the parser creates a TYPE_UNDEFINED
1501 type for it. Since 'EnumValue' is never resolved to a known
1502 structure, it will stay with TYPE_UNDEFINED.
1504 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1505 'EnumValue'. Generating marking code for it would cause
1506 compilation failures since the marking routines assumes that
1507 'EnumValue' is a type. */
1510 set_gc_used_type (type_p t
, enum gc_used_enum level
,
1511 bool allow_undefined_types
)
1513 if (t
->gc_used
>= level
)
1522 case TYPE_USER_STRUCT
:
1527 bool allow_undefined_field_types
= (t
->kind
== TYPE_USER_STRUCT
);
1529 process_gc_options (t
->u
.s
.opt
, level
, &dummy
, &dummy
, &dummy
,
1532 if (t
->u
.s
.base_class
)
1533 set_gc_used_type (t
->u
.s
.base_class
, level
, allow_undefined_types
);
1534 /* Anything pointing to a base class might actually be pointing
1536 for (type_p subclass
= t
->u
.s
.first_subclass
; subclass
;
1537 subclass
= subclass
->u
.s
.next_sibling_class
)
1538 set_gc_used_type (subclass
, level
, allow_undefined_types
);
1540 FOR_ALL_INHERITED_FIELDS(t
, f
)
1542 int maybe_undef
= 0;
1545 type_p nested_ptr
= NULL
;
1546 process_gc_options (f
->opt
, level
, &maybe_undef
, &length
, &skip
,
1549 if (nested_ptr
&& f
->type
->kind
== TYPE_POINTER
)
1550 set_gc_used_type (nested_ptr
, GC_POINTED_TO
);
1551 else if (length
&& f
->type
->kind
== TYPE_POINTER
)
1552 set_gc_used_type (f
->type
->u
.p
, GC_USED
);
1553 else if (maybe_undef
&& f
->type
->kind
== TYPE_POINTER
)
1554 set_gc_used_type (f
->type
->u
.p
, GC_MAYBE_POINTED_TO
);
1556 ; /* target type is not used through this field */
1558 set_gc_used_type (f
->type
, GC_USED
, allow_undefined_field_types
);
1563 case TYPE_UNDEFINED
:
1564 if (level
> GC_UNUSED
)
1566 if (!allow_undefined_types
)
1567 error_at_line (&t
->u
.s
.line
, "undefined type `%s'", t
->u
.s
.tag
);
1568 t
->gc_used
= GC_UNUSED
;
1573 set_gc_used_type (t
->u
.p
, GC_POINTED_TO
);
1577 set_gc_used_type (t
->u
.a
.p
, GC_USED
);
1580 case TYPE_LANG_STRUCT
:
1581 for (t
= t
->u
.s
.lang_struct
; t
; t
= t
->next
)
1582 set_gc_used_type (t
, level
);
1590 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1593 set_gc_used (pair_p variables
)
1597 for (p
= variables
; p
; p
= p
->next
)
1599 set_gc_used_type (p
->type
, GC_USED
);
1602 if (verbosity_level
>= 2)
1603 printf ("%s used %d GTY-ed variables\n", progname
, nbvars
);
1606 /* File mapping routines. For each input file, there is one output .c file
1607 (but some output files have many input files), and there is one .h file
1608 for the whole build. */
1610 /* Output file handling. */
1612 /* Create and return an outf_p for a new file for NAME, to be called
1616 create_file (const char *name
, const char *oname
)
1618 static const char *const hdr
[] = {
1619 " Copyright (C) 2004-2021 Free Software Foundation, Inc.\n",
1621 "This file is part of GCC.\n",
1623 "GCC is free software; you can redistribute it and/or modify it under\n",
1624 "the terms of the GNU General Public License as published by the Free\n",
1625 "Software Foundation; either version 3, or (at your option) any later\n",
1628 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1629 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1630 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1631 "for more details.\n",
1633 "You should have received a copy of the GNU General Public License\n",
1634 "along with GCC; see the file COPYING3. If not see\n",
1635 "<http://www.gnu.org/licenses/>. */\n",
1637 "/* This file is machine generated. Do not edit. */\n"
1642 gcc_assert (name
!= NULL
);
1643 gcc_assert (oname
!= NULL
);
1644 f
= XCNEW (struct outf
);
1645 f
->next
= output_files
;
1649 oprintf (f
, "/* Type information for %s.\n", name
);
1650 for (i
= 0; i
< ARRAY_SIZE (hdr
); i
++)
1651 oprintf (f
, "%s", hdr
[i
]);
1655 /* Print, like fprintf, to O.
1656 N.B. You might think this could be implemented more efficiently
1657 with vsnprintf(). Unfortunately, there are C libraries that
1658 provide that function but without the C99 semantics for its return
1659 value, making it impossible to know how much space is required. */
1661 oprintf (outf_p o
, const char *format
, ...)
1667 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1672 va_start (ap
, format
);
1673 slength
= vasprintf (&s
, format
, ap
);
1674 if (s
== NULL
|| (int) slength
< 0)
1675 fatal ("out of memory");
1678 if (o
->bufused
+ slength
> o
->buflength
)
1680 size_t new_len
= o
->buflength
;
1687 while (o
->bufused
+ slength
>= new_len
);
1688 o
->buf
= XRESIZEVEC (char, o
->buf
, new_len
);
1689 o
->buflength
= new_len
;
1691 memcpy (o
->buf
+ o
->bufused
, s
, slength
);
1692 o
->bufused
+= slength
;
1696 /* Open the global header file and the language-specific header files. */
1699 open_base_files (void)
1703 if (nb_plugin_files
> 0 && plugin_files
)
1706 header_file
= create_file ("GCC", "gtype-desc.h");
1708 base_files
= XNEWVEC (outf_p
, num_lang_dirs
);
1710 for (i
= 0; i
< num_lang_dirs
; i
++)
1711 base_files
[i
] = create_file (lang_dir_names
[i
],
1712 xasprintf ("gtype-%s.h", lang_dir_names
[i
]));
1714 /* gtype-desc.c is a little special, so we create it here. */
1716 /* The order of files here matters very much. */
1717 static const char *const ifiles
[] = {
1718 "config.h", "system.h", "coretypes.h",
1719 "backend.h", "predict.h", "tree.h",
1720 "rtl.h", "gimple.h", "fold-const.h", "insn-codes.h", "splay-tree.h",
1721 "alias.h", "insn-config.h", "flags.h", "expmed.h", "dojump.h",
1722 "explow.h", "calls.h", "memmodel.h", "emit-rtl.h", "varasm.h",
1723 "stmt.h", "expr.h", "alloc-pool.h", "cselib.h", "insn-addr.h",
1724 "optabs.h", "libfuncs.h", "debug.h", "internal-fn.h", "gimple-fold.h",
1726 "tree-eh.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
1727 "tree-vrp.h", "tree-phinodes.h", "ssa-iterators.h", "stringpool.h",
1728 "tree-ssanames.h", "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h",
1729 "tree-ssa-loop-manip.h", "tree-ssa-loop-niter.h", "tree-into-ssa.h",
1730 "tree-dfa.h", "tree-ssa.h", "reload.h", "cpplib.h", "tree-chrec.h",
1731 "except.h", "output.h", "cfgloop.h", "target.h", "lto-streamer.h",
1732 "target-globals.h", "ipa-ref.h", "cgraph.h", "symbol-summary.h",
1733 "ipa-prop.h", "ipa-fnsummary.h", "dwarf2out.h", "omp-general.h",
1734 "omp-offload.h", "ipa-modref-tree.h", "ipa-modref.h", "symtab-thunks.h",
1735 "symtab-clones.h", "diagnostic-spec.h", "ctfc.h",
1738 const char *const *ifp
;
1739 outf_p gtype_desc_c
;
1741 gtype_desc_c
= create_file ("GCC", "gtype-desc.c");
1742 for (ifp
= ifiles
; *ifp
; ifp
++)
1743 oprintf (gtype_desc_c
, "#include \"%s\"\n", *ifp
);
1744 for (int j
= 0; j
< (int) num_build_headers
; j
++)
1745 oprintf (gtype_desc_c
, "#include \"%s\"\n", build_headers
[j
]);
1747 /* Make sure we handle "cfun" specially. */
1748 oprintf (gtype_desc_c
, "\n/* See definition in function.h. */\n");
1749 oprintf (gtype_desc_c
, "#undef cfun\n");
1751 oprintf (gtype_desc_c
,
1753 "/* Types with a \"gcc::\" namespace have it stripped\n"
1754 " during gengtype parsing. Provide a \"using\" directive\n"
1755 " to ensure that the fully-qualified types are found. */\n"
1756 "using namespace gcc;\n");
1760 /* For INPF an input file, return the real basename of INPF, with all
1761 the directory components skipped. */
1764 get_file_realbasename (const input_file
*inpf
)
1766 return lbasename (get_input_file_name (inpf
));
1769 /* For INPF a filename, return the relative path to INPF from
1770 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1773 get_file_srcdir_relative_path (const input_file
*inpf
)
1775 const char *f
= get_input_file_name (inpf
);
1776 if (strlen (f
) > srcdir_len
1777 && IS_DIR_SEPARATOR (f
[srcdir_len
])
1778 && strncmp (f
, srcdir
, srcdir_len
) == 0)
1779 return f
+ srcdir_len
+ 1;
1784 /* For INPF an input_file, return the relative path to INPF from
1785 $(srcdir) if the latter is a prefix in INPF, or the real basename
1786 of INPF otherwise. */
1789 get_file_basename (const input_file
*inpf
)
1791 const char *srcdir_path
= get_file_srcdir_relative_path (inpf
);
1793 return (srcdir_path
!= NULL
) ? srcdir_path
: get_file_realbasename (inpf
);
1796 /* For F a filename, return the lang_dir_names relative index of the language
1797 directory that is a prefix in F, if any, -1 otherwise. */
1800 get_prefix_langdir_index (const char *f
)
1802 size_t f_len
= strlen (f
);
1805 for (lang_index
= 0; lang_index
< num_lang_dirs
; lang_index
++)
1807 const char *langdir
= lang_dir_names
[lang_index
];
1808 size_t langdir_len
= strlen (langdir
);
1810 if (f_len
> langdir_len
1811 && IS_DIR_SEPARATOR (f
[langdir_len
])
1812 && memcmp (f
, langdir
, langdir_len
) == 0)
1819 /* For INPF an input file, return the name of language directory where
1820 F is located, if any, NULL otherwise. */
1823 get_file_langdir (const input_file
*inpf
)
1825 /* Get the relative path to INPF from $(srcdir) and find the
1826 language by comparing the prefix with language directory names.
1827 If INPF is not even srcdir relative, no point in looking
1831 const char *srcdir_relative_path
= get_file_srcdir_relative_path (inpf
);
1834 if (!srcdir_relative_path
)
1837 lang_index
= get_prefix_langdir_index (srcdir_relative_path
);
1838 if (lang_index
< 0 && startswith (srcdir_relative_path
, "c-family"))
1840 else if (lang_index
>= 0)
1841 r
= lang_dir_names
[lang_index
];
1848 /* The gt- output file name for INPF. */
1851 get_file_gtfilename (const input_file
*inpf
)
1853 /* Cook up an initial version of the gt- file name from the file real
1854 basename and the language name, if any. */
1856 const char *basename
= get_file_realbasename (inpf
);
1857 const char *langdir
= get_file_langdir (inpf
);
1860 (langdir
? xasprintf ("gt-%s-%s", langdir
, basename
)
1861 : xasprintf ("gt-%s", basename
));
1863 /* Then replace all non alphanumerics characters by '-' and change the
1864 extension to ".h". We expect the input filename extension was at least
1865 one character long. */
1869 for (; *s
!= '.'; s
++)
1870 if (!ISALNUM (*s
) && *s
!= '-')
1873 memcpy (s
, ".h", sizeof (".h"));
1878 /* Each input_file has its associated output file outf_p. The
1879 association is computed by the function
1880 get_output_file_with_visibility. The associated file is cached
1881 inside input_file in its inpoutf field, so is really computed only
1882 once. Associated output file paths (i.e. output_name-s) are
1883 computed by a rule based regexp machinery, using the files_rules
1884 array of struct file_rule_st. A for_name is also computed, giving
1885 the source file name for which the output_file is generated; it is
1886 often the last component of the input_file path. */
1890 Regexpr machinery to compute the output_name and for_name-s of each
1891 input_file. We have a sequence of file rules which gives the POSIX
1892 extended regular expression to match an input file path, and two
1893 transformed strings for the corresponding output_name and the
1894 corresponding for_name. The transformed string contain dollars: $0
1895 is replaced by the entire match, $1 is replaced by the substring
1896 matching the first parenthesis in the regexp, etc. And $$ is replaced
1897 by a single verbatim dollar. The rule order is important. The
1898 general case is last, and the particular cases should come before.
1899 An action routine can, when needed, update the out_name & for_name
1900 and/or return the appropriate output file. It is invoked only when a
1901 rule is triggered. When a rule is triggered, the output_name and
1902 for_name are computed using their transform string in while $$, $0,
1903 $1, ... are suitably replaced. If there is an action, it is called.
1904 In some few cases, the action can directly return the outf_p, but
1905 usually it just updates the output_name and for_name so should free
1906 them before replacing them. The get_output_file_with_visibility
1907 function creates an outf_p only once per each output_name, so it
1908 scans the output_files list for previously seen output file names.
1911 /* Signature of actions in file rules. */
1912 typedef outf_p (frul_actionrout_t
) (input_file
*, char**, char**);
1915 struct file_rule_st
{
1916 const char* frul_srcexpr
; /* Source string for regexp. */
1917 int frul_rflags
; /* Flags passed to regcomp, usually
1919 regex_t
* frul_re
; /* Compiled regular expression
1920 obtained by regcomp. */
1921 const char* frul_tr_out
; /* Transformation string for making
1922 * the output_name, with $1 ... $9 for
1923 * subpatterns and $0 for the whole
1924 * matched filename. */
1925 const char* frul_tr_for
; /* Tranformation string for making the
1927 frul_actionrout_t
* frul_action
; /* The action, if non null, is
1928 * called once the rule matches, on
1929 * the transformed out_name &
1930 * for_name. It could change them
1931 * and/or give the output file. */
1934 /* File rule action handling *.h files. */
1935 static outf_p
header_dot_h_frul (input_file
*, char**, char**);
1937 /* File rule action handling *.c files. */
1938 static outf_p
source_dot_c_frul (input_file
*, char**, char**);
1940 #define NULL_REGEX (regex_t*)0
1942 /* The prefix in our regexp-s matching the directory. */
1943 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1945 #define NULL_FRULACT (frul_actionrout_t*)0
1947 /* The array of our rules governing file name generation. Rules order
1948 matters, so change with extreme care! */
1950 struct file_rule_st files_rules
[] = {
1951 /* The general rule assumes that files in subdirectories belong to a
1952 particular front-end, and files not in subdirectories are shared.
1953 The following rules deal with exceptions - files that are in
1954 subdirectories and yet are shared, and files that are top-level,
1955 but are not shared. */
1957 /* the c-family/ source directory is special. */
1958 { DIR_PREFIX_REGEX
"c-family/([[:alnum:]_-]*)\\.c$",
1959 REG_EXTENDED
, NULL_REGEX
,
1960 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT
},
1962 { DIR_PREFIX_REGEX
"c-family/([[:alnum:]_-]*)\\.h$",
1963 REG_EXTENDED
, NULL_REGEX
,
1964 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT
},
1966 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
1967 { DIR_PREFIX_REGEX
"c/c-lang\\.h$",
1968 REG_EXTENDED
, NULL_REGEX
, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT
},
1970 { DIR_PREFIX_REGEX
"c/c-tree\\.h$",
1971 REG_EXTENDED
, NULL_REGEX
, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT
},
1973 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
1974 { DIR_PREFIX_REGEX
"cp/cp-tree\\.h$",
1975 REG_EXTENDED
, NULL_REGEX
,
1976 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT
},
1978 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
1979 { DIR_PREFIX_REGEX
"cp/decl\\.[ch]$",
1980 REG_EXTENDED
, NULL_REGEX
,
1981 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT
},
1983 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
1984 { DIR_PREFIX_REGEX
"cp/name-lookup\\.h$",
1985 REG_EXTENDED
, NULL_REGEX
,
1986 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT
},
1988 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
1989 { DIR_PREFIX_REGEX
"cp/parser\\.h$",
1990 REG_EXTENDED
, NULL_REGEX
,
1991 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT
},
1993 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
1994 { DIR_PREFIX_REGEX
"objc/objc-act\\.h$",
1995 REG_EXTENDED
, NULL_REGEX
,
1996 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT
},
1998 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
1999 { DIR_PREFIX_REGEX
"objc/objc-map\\.h$",
2000 REG_EXTENDED
, NULL_REGEX
,
2001 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT
},
2003 /* General cases. For header *.h and source *.c or *.cc files, we
2004 * need special actions to handle the language. */
2006 /* Source *.c files are using get_file_gtfilename to compute their
2007 output_name and get_file_basename to compute their for_name
2008 through the source_dot_c_frul action. */
2009 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.c$",
2010 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.c", source_dot_c_frul
},
2012 /* Source *.cc files are using get_file_gtfilename to compute their
2013 output_name and get_file_basename to compute their for_name
2014 through the source_dot_c_frul action. */
2015 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.cc$",
2016 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.cc", source_dot_c_frul
},
2018 /* Common header files get "gtype-desc.c" as their output_name,
2019 * while language specific header files are handled specially. So
2020 * we need the header_dot_h_frul action. */
2021 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.h$",
2022 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.h", header_dot_h_frul
},
2024 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.in$",
2025 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.in", NULL_FRULACT
},
2027 /* Mandatory null last entry signaling end of rules. */
2028 {NULL
, 0, NULL_REGEX
, NULL
, NULL
, NULL_FRULACT
}
2031 /* Special file rules action for handling *.h header files. It gives
2032 "gtype-desc.c" for common headers and corresponding output
2033 files for language-specific header files. */
2035 header_dot_h_frul (input_file
* inpf
, char**poutname
,
2036 char**pforname ATTRIBUTE_UNUSED
)
2038 const char *basename
= 0;
2040 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2041 (void*) inpf
, get_input_file_name (inpf
),
2042 *poutname
, *pforname
);
2043 basename
= get_file_basename (inpf
);
2044 lang_index
= get_prefix_langdir_index (basename
);
2045 DBGPRINTF ("basename %s lang_index %d", basename
, lang_index
);
2047 if (lang_index
>= 0)
2049 /* The header is language specific. Given output_name &
2050 for_name remains unchanged. The base_files array gives the
2052 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2053 (void*) base_files
[lang_index
],
2054 (base_files
[lang_index
])->name
);
2055 return base_files
[lang_index
];
2059 /* The header is common to all front-end languages. So
2060 output_name is "gtype-desc.c" file. The calling function
2061 get_output_file_with_visibility will find its outf_p. */
2063 *poutname
= xstrdup ("gtype-desc.c");
2064 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2065 get_input_file_name (inpf
));
2071 /* Special file rules action for handling *.c source files using
2072 * get_file_gtfilename to compute their output_name and
2073 * get_file_basename to compute their for_name. The output_name is
2074 * gt-<LANG>-<BASE>.h for language specific source files, and
2075 * gt-<BASE>.h for common source files. */
2077 source_dot_c_frul (input_file
* inpf
, char**poutname
, char**pforname
)
2079 char *newbasename
= CONST_CAST (char*, get_file_basename (inpf
));
2080 char *newoutname
= CONST_CAST (char*, get_file_gtfilename (inpf
));
2081 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2082 (void*) inpf
, get_input_file_name (inpf
),
2083 *poutname
, *pforname
);
2084 DBGPRINTF ("newoutname %s", newoutname
);
2085 DBGPRINTF ("newbasename %s", newbasename
);
2088 *poutname
= newoutname
;
2089 *pforname
= newbasename
;
2093 /* Utility function for get_output_file_with_visibility which returns
2094 * a malloc-ed substituted string using TRS on matching of the FILNAM
2095 * file name, using the PMATCH array. */
2097 matching_file_name_substitute (const char *filnam
, regmatch_t pmatch
[10],
2100 struct obstack str_obstack
;
2102 char *rawstr
= NULL
;
2103 const char *pt
= NULL
;
2104 DBGPRINTF ("filnam %s", filnam
);
2105 obstack_init (&str_obstack
);
2106 for (pt
= trs
; *pt
; pt
++) {
2112 /* A double dollar $$ is substituted by a single verbatim
2113 dollar, but who really uses dollar signs in file
2115 obstack_1grow (&str_obstack
, '$');
2117 else if (ISDIGIT (pt
[1]))
2119 /* Handle $0 $1 ... $9 by appropriate substitution. */
2120 int dolnum
= pt
[1] - '0';
2121 int so
= pmatch
[dolnum
].rm_so
;
2122 int eo
= pmatch
[dolnum
].rm_eo
;
2123 DBGPRINTF ("so=%d eo=%d dolnum=%d", so
, eo
, dolnum
);
2124 if (so
>=0 && eo
>=so
)
2125 obstack_grow (&str_obstack
, filnam
+ so
, eo
- so
);
2129 /* This can happen only when files_rules is buggy! */
2132 /* Always skip the character after the dollar. */
2136 obstack_1grow (&str_obstack
, c
);
2138 obstack_1grow (&str_obstack
, '\0');
2139 rawstr
= XOBFINISH (&str_obstack
, char *);
2140 str
= xstrdup (rawstr
);
2141 obstack_free (&str_obstack
, NULL
);
2142 DBGPRINTF ("matched replacement %s", str
);
2148 /* An output file, suitable for definitions, that can see declarations
2149 made in INPF and is linked into every language that uses INPF.
2150 Since the result is cached inside INPF, that argument cannot be
2151 declared constant, but is "almost" constant. */
2154 get_output_file_with_visibility (input_file
*inpf
)
2157 char *for_name
= NULL
;
2158 char *output_name
= NULL
;
2159 const char* inpfname
;
2161 /* This can happen when we need a file with visibility on a
2162 structure that we've never seen. We have to just hope that it's
2163 globally visible. */
2165 inpf
= system_h_file
;
2167 /* The result is cached in INPF, so return it if already known. */
2169 return inpf
->inpoutf
;
2171 /* In plugin mode, return NULL unless the input_file is one of the
2176 for (i
= 0; i
< nb_plugin_files
; i
++)
2177 if (inpf
== plugin_files
[i
])
2179 inpf
->inpoutf
= plugin_output
;
2180 return plugin_output
;
2186 inpfname
= get_input_file_name (inpf
);
2188 /* Try each rule in sequence in files_rules until one is triggered. */
2191 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2192 (void*) inpf
, inpfname
);
2194 for (; files_rules
[rulix
].frul_srcexpr
!= NULL
; rulix
++)
2196 DBGPRINTF ("rulix#%d srcexpr %s",
2197 rulix
, files_rules
[rulix
].frul_srcexpr
);
2199 if (!files_rules
[rulix
].frul_re
)
2201 /* Compile the regexpr lazily. */
2203 files_rules
[rulix
].frul_re
= XCNEW (regex_t
);
2204 err
= regcomp (files_rules
[rulix
].frul_re
,
2205 files_rules
[rulix
].frul_srcexpr
,
2206 files_rules
[rulix
].frul_rflags
);
2209 /* The regular expression compilation fails only when
2210 file_rules is buggy. */
2218 /* Match the regexpr and trigger the rule if matched. */
2220 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2222 regmatch_t pmatch
[10];
2223 memset (pmatch
, 0, sizeof (pmatch
));
2224 if (!regexec (files_rules
[rulix
].frul_re
,
2225 inpfname
, 10, pmatch
, 0))
2227 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2228 (void*) inpf
, inpfname
, rulix
,
2229 files_rules
[rulix
].frul_srcexpr
);
2231 matching_file_name_substitute (inpfname
, pmatch
,
2232 files_rules
[rulix
].frul_tr_for
);
2233 DBGPRINTF ("for_name %s", for_name
);
2235 matching_file_name_substitute (inpfname
, pmatch
,
2236 files_rules
[rulix
].frul_tr_out
);
2237 DBGPRINTF ("output_name %s", output_name
);
2238 if (files_rules
[rulix
].frul_action
)
2240 /* Invoke our action routine. */
2242 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2243 rulix
, output_name
, for_name
);
2245 (files_rules
[rulix
].frul_action
) (inpf
,
2246 &output_name
, &for_name
);
2247 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2248 rulix
, (void*)of
, output_name
, for_name
);
2249 /* If the action routine returned something, give it back
2250 immediately and cache it in inpf. */
2257 /* The rule matched, and had no action, or that action did
2258 not return any output file but could have changed the
2259 output_name or for_name. We break out of the loop on the
2265 /* The regexpr did not match. */
2266 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2267 rulix
, inpfname
, files_rules
[rulix
].frul_srcexpr
);
2273 if (!output_name
|| !for_name
)
2275 /* This should not be possible, and could only happen if the
2276 files_rules is incomplete or buggy. */
2277 fatal ("failed to compute output name for %s", inpfname
);
2280 /* Look through to see if we've ever seen this output filename
2281 before. If found, cache the result in inpf. */
2282 for (r
= output_files
; r
; r
= r
->next
)
2283 if (filename_cmp (r
->name
, output_name
) == 0)
2286 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r
,
2287 output_name
, for_name
);
2291 /* If not found, create it, and cache it in inpf. */
2292 r
= create_file (for_name
, output_name
);
2294 gcc_assert (r
&& r
->name
);
2295 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r
,
2296 output_name
, for_name
);
2303 /* The name of an output file, suitable for definitions, that can see
2304 declarations made in INPF and is linked into every language that
2308 get_output_file_name (input_file
* inpf
)
2310 outf_p o
= get_output_file_with_visibility (inpf
);
2316 /* Check if existing file is equal to the in memory buffer. */
2319 is_file_equal (outf_p of
)
2321 FILE *newfile
= fopen (of
->name
, "r");
2324 if (newfile
== NULL
)
2328 for (i
= 0; i
< of
->bufused
; i
++)
2331 ch
= fgetc (newfile
);
2332 if (ch
== EOF
|| ch
!= (unsigned char) of
->buf
[i
])
2338 if (equal
&& EOF
!= fgetc (newfile
))
2344 /* Copy the output to its final destination,
2345 but don't unnecessarily change modification times. */
2348 close_output_files (void)
2350 int nbwrittenfiles
= 0;
2353 for (of
= output_files
; of
; of
= of
->next
)
2355 if (!is_file_equal (of
))
2357 FILE *newfile
= NULL
;
2358 char *backupname
= NULL
;
2359 /* Back up the old version of the output file gt-FOO.c as
2360 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2363 backupname
= concat (backup_dir
, "/",
2364 lbasename (of
->name
), "~", NULL
);
2365 if (!access (of
->name
, F_OK
) && rename (of
->name
, backupname
))
2366 fatal ("failed to back up %s as %s: %s",
2367 of
->name
, backupname
, xstrerror (errno
));
2370 newfile
= fopen (of
->name
, "w");
2371 if (newfile
== NULL
)
2372 fatal ("opening output file %s: %s", of
->name
, xstrerror (errno
));
2373 if (fwrite (of
->buf
, 1, of
->bufused
, newfile
) != of
->bufused
)
2374 fatal ("writing output file %s: %s", of
->name
, xstrerror (errno
));
2375 if (fclose (newfile
) != 0)
2376 fatal ("closing output file %s: %s", of
->name
, xstrerror (errno
));
2378 if (verbosity_level
>= 2 && backupname
)
2379 printf ("%s wrote #%-3d %s backed-up in %s\n",
2380 progname
, nbwrittenfiles
, of
->name
, backupname
);
2381 else if (verbosity_level
>= 1)
2382 printf ("%s write #%-3d %s\n", progname
, nbwrittenfiles
, of
->name
);
2387 /* output file remains unchanged. */
2388 if (verbosity_level
>= 2)
2389 printf ("%s keep %s\n", progname
, of
->name
);
2393 of
->bufused
= of
->buflength
= 0;
2395 if (verbosity_level
>= 1)
2396 printf ("%s wrote %d files.\n", progname
, nbwrittenfiles
);
2403 const input_file
* file
;
2407 struct walk_type_data
;
2409 /* For scalars and strings, given the item in 'val'.
2410 For structures, given a pointer to the item in 'val'.
2411 For misc. pointers, given the item in 'val'.
2413 typedef void (*process_field_fn
) (type_p f
, const struct walk_type_data
* p
);
2414 typedef void (*func_name_fn
) (type_p s
, const struct walk_type_data
* p
);
2416 /* Parameters for write_types. */
2418 struct write_types_data
2421 const char *param_prefix
;
2422 const char *subfield_marker_routine
;
2423 const char *marker_routine
;
2424 const char *reorder_note_routine
;
2425 const char *comment
;
2426 enum write_types_kinds kind
;
2429 static void output_escaped_param (struct walk_type_data
*d
,
2430 const char *, const char *);
2431 static void output_mangled_typename (outf_p
, const_type_p
);
2432 static void walk_type (type_p t
, struct walk_type_data
*d
);
2433 static void write_func_for_structure (type_p orig_s
, type_p s
,
2434 const struct write_types_data
*wtd
);
2435 static void write_types_process_field
2436 (type_p f
, const struct walk_type_data
*d
);
2437 static void write_types (outf_p output_header
,
2439 const struct write_types_data
*wtd
);
2440 static void write_types_local_process_field
2441 (type_p f
, const struct walk_type_data
*d
);
2442 static void write_local_func_for_structure (const_type_p orig_s
, type_p s
);
2443 static void write_local (outf_p output_header
,
2445 static int contains_scalar_p (type_p t
);
2446 static void put_mangled_filename (outf_p
, const input_file
*);
2447 static void finish_root_table (struct flist
*flp
, const char *pfx
,
2448 const char *lastname
,
2449 const char *tname
, const char *name
);
2450 static void write_root (outf_p
, pair_p
, type_p
, const char *, int,
2451 struct fileloc
*, bool);
2452 static void write_array (outf_p f
, pair_p v
,
2453 const struct write_types_data
*wtd
);
2454 static void write_roots (pair_p
, bool);
2456 /* Parameters for walk_type. */
2458 struct walk_type_data
2460 process_field_fn process_field
;
2465 const char *prev_val
[4];
2468 const struct fileloc
*line
;
2472 const char *reorder_fn
;
2474 bool fn_wants_lvalue
;
2482 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2483 pre-processor identifier to use in a #define directive. This replaces
2484 special characters used in C++ identifiers like '>', '<' and ':' with
2487 If no C++ special characters are found in TYPE_NAME, return
2488 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2489 characters replaced with '_'. In this case, the caller is
2490 responsible for freeing the allocated string. */
2493 filter_type_name (const char *type_name
)
2495 if (strchr (type_name
, '<') || strchr (type_name
, ':'))
2498 char *s
= xstrdup (type_name
);
2499 for (i
= 0; i
< strlen (s
); i
++)
2500 if (s
[i
] == '<' || s
[i
] == '>' || s
[i
] == ':' || s
[i
] == ','
2510 /* Print a mangled name representing T to OF. */
2513 output_mangled_typename (outf_p of
, const_type_p t
)
2521 case TYPE_UNDEFINED
:
2526 output_mangled_typename (of
, t
->u
.p
);
2536 case TYPE_LANG_STRUCT
:
2537 case TYPE_USER_STRUCT
:
2539 /* For references to classes within an inheritance hierarchy,
2540 only ever reference the ultimate base class, since only
2541 it will have gt_ functions. */
2542 t
= get_ultimate_base_class (t
);
2543 const char *id_for_tag
= filter_type_name (t
->u
.s
.tag
);
2544 oprintf (of
, "%lu%s", (unsigned long) strlen (id_for_tag
),
2546 if (id_for_tag
!= t
->u
.s
.tag
)
2547 free (CONST_CAST (char *, id_for_tag
));
2555 /* Print PARAM to D->OF processing escapes. D->VAL references the
2556 current object, D->PREV_VAL the object containing the current
2557 object, ONAME is the name of the option and D->LINE is used to
2558 print error messages. */
2561 output_escaped_param (struct walk_type_data
*d
, const char *param
,
2566 for (p
= param
; *p
; p
++)
2568 oprintf (d
->of
, "%c", *p
);
2573 oprintf (d
->of
, "(%s)", d
->prev_val
[2]);
2576 oprintf (d
->of
, "(%s)", d
->prev_val
[0]);
2579 oprintf (d
->of
, "(%s)", d
->prev_val
[1]);
2583 const char *pp
= d
->val
+ strlen (d
->val
);
2584 while (pp
[-1] == ']')
2587 oprintf (d
->of
, "%s", pp
);
2591 error_at_line (d
->line
, "`%s' option contains bad escape %c%c",
2597 get_string_option (options_p opt
, const char *key
)
2599 for (; opt
; opt
= opt
->next
)
2600 if (strcmp (opt
->name
, key
) == 0)
2601 return opt
->info
.string
;
2605 /* Machinery for avoiding duplicate tags within switch statements. */
2609 struct seen_tag
*next
;
2613 already_seen_tag (struct seen_tag
*seen_tags
, const char *tag
)
2615 /* Linear search, so O(n^2), but n is currently small. */
2618 if (!strcmp (seen_tags
->tag
, tag
))
2620 seen_tags
= seen_tags
->next
;
2622 /* Not yet seen this tag. */
2627 mark_tag_as_seen (struct seen_tag
**seen_tags
, const char *tag
)
2629 /* Add to front of linked list. */
2630 struct seen_tag
*new_node
= XCNEW (struct seen_tag
);
2631 new_node
->tag
= tag
;
2632 new_node
->next
= *seen_tags
;
2633 *seen_tags
= new_node
;
2637 walk_subclasses (type_p base
, struct walk_type_data
*d
,
2638 struct seen_tag
**seen_tags
)
2640 for (type_p sub
= base
->u
.s
.first_subclass
; sub
!= NULL
;
2641 sub
= sub
->u
.s
.next_sibling_class
)
2643 const char *type_tag
= get_string_option (sub
->u
.s
.opt
, "tag");
2644 if (type_tag
&& !already_seen_tag (*seen_tags
, type_tag
))
2646 mark_tag_as_seen (seen_tags
, type_tag
);
2647 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", type_tag
);
2649 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2651 oprintf (d
->of
, "%*s%s *sub = static_cast <%s *> (x);\n",
2652 d
->indent
, "", sub
->u
.s
.tag
, sub
->u
.s
.tag
);
2653 const char *old_val
= d
->val
;
2658 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2659 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
2662 walk_subclasses (sub
, d
, seen_tags
);
2666 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2667 which is of type T. Write code to D->OF to constrain execution (at
2668 the point that D->PROCESS_FIELD is called) to the appropriate
2669 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2670 pointers to those objects. D->PREV_VAL lists the objects
2671 containing the current object, D->OPT is a list of options to
2672 apply, D->INDENT is the current indentation level, D->LINE is used
2673 to print error messages, D->BITMAP indicates which languages to
2674 print the structure for. */
2677 walk_type (type_p t
, struct walk_type_data
*d
)
2679 const char *length
= NULL
;
2680 const char *desc
= NULL
;
2681 const char *type_tag
= NULL
;
2682 int maybe_undef_p
= 0;
2685 const struct nested_ptr_data
*nested_ptr_d
= NULL
;
2687 d
->needs_cast_p
= false;
2688 for (oo
= d
->opt
; oo
; oo
= oo
->next
)
2689 if (strcmp (oo
->name
, "length") == 0 && oo
->kind
== OPTION_STRING
)
2690 length
= oo
->info
.string
;
2691 else if (strcmp (oo
->name
, "maybe_undef") == 0)
2693 else if (strcmp (oo
->name
, "desc") == 0 && oo
->kind
== OPTION_STRING
)
2694 desc
= oo
->info
.string
;
2695 else if (strcmp (oo
->name
, "nested_ptr") == 0
2696 && oo
->kind
== OPTION_NESTED
)
2697 nested_ptr_d
= (const struct nested_ptr_data
*) oo
->info
.nested
;
2698 else if (strcmp (oo
->name
, "dot") == 0)
2700 else if (strcmp (oo
->name
, "tag") == 0)
2701 type_tag
= oo
->info
.string
;
2702 else if (strcmp (oo
->name
, "special") == 0)
2704 else if (strcmp (oo
->name
, "skip") == 0)
2706 else if (strcmp (oo
->name
, "atomic") == 0)
2708 else if (strcmp (oo
->name
, "default") == 0)
2710 else if (strcmp (oo
->name
, "chain_next") == 0)
2712 else if (strcmp (oo
->name
, "chain_prev") == 0)
2714 else if (strcmp (oo
->name
, "chain_circular") == 0)
2716 else if (strcmp (oo
->name
, "reorder") == 0)
2718 else if (strcmp (oo
->name
, "variable_size") == 0)
2720 else if (strcmp (oo
->name
, "for_user") == 0)
2723 error_at_line (d
->line
, "unknown option `%s'\n", oo
->name
);
2729 && (t
->kind
!= TYPE_POINTER
|| !union_or_struct_p (t
->u
.p
)))
2731 error_at_line (d
->line
,
2732 "field `%s' has invalid option `maybe_undef_p'\n",
2737 if (atomic_p
&& (t
->kind
!= TYPE_POINTER
) && (t
->kind
!= TYPE_STRING
))
2739 error_at_line (d
->line
, "field `%s' has invalid option `atomic'\n", d
->val
);
2747 d
->process_field (t
, d
);
2752 d
->in_ptr_field
= true;
2753 if (maybe_undef_p
&& t
->u
.p
->u
.s
.line
.file
== NULL
)
2755 oprintf (d
->of
, "%*sgcc_assert (!%s);\n", d
->indent
, "", d
->val
);
2759 /* If a pointer type is marked as "atomic", we process the
2760 field itself, but we don't walk the data that they point to.
2762 There are two main cases where we walk types: to mark
2763 pointers that are reachable, and to relocate pointers when
2764 writing a PCH file. In both cases, an atomic pointer is
2765 itself marked or relocated, but the memory that it points
2766 to is left untouched. In the case of PCH, that memory will
2767 be read/written unchanged to the PCH file. */
2770 oprintf (d
->of
, "%*sif (%s != NULL) {\n", d
->indent
, "", d
->val
);
2772 d
->process_field (t
, d
);
2774 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2780 if (!union_or_struct_p (t
->u
.p
))
2782 error_at_line (d
->line
,
2783 "field `%s' is pointer to unimplemented type",
2790 const char *oldprevval2
= d
->prev_val
[2];
2792 if (!union_or_struct_p (nested_ptr_d
->type
))
2794 error_at_line (d
->line
,
2795 "field `%s' has invalid "
2796 "option `nested_ptr'\n", d
->val
);
2800 d
->prev_val
[2] = d
->val
;
2801 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2803 d
->val
= xasprintf ("x%d", d
->counter
++);
2804 oprintf (d
->of
, "%*s%s %s * %s%s =\n", d
->indent
, "",
2805 (nested_ptr_d
->type
->kind
== TYPE_UNION
2806 ? "union" : "struct"),
2807 nested_ptr_d
->type
->u
.s
.tag
,
2808 d
->fn_wants_lvalue
? "" : "const ", d
->val
);
2809 oprintf (d
->of
, "%*s", d
->indent
+ 2, "");
2810 output_escaped_param (d
, nested_ptr_d
->convert_from
,
2812 oprintf (d
->of
, ";\n");
2814 d
->process_field (nested_ptr_d
->type
, d
);
2816 if (d
->fn_wants_lvalue
)
2818 oprintf (d
->of
, "%*s%s = ", d
->indent
, "",
2820 d
->prev_val
[2] = d
->val
;
2821 output_escaped_param (d
, nested_ptr_d
->convert_to
,
2823 oprintf (d
->of
, ";\n");
2827 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2828 d
->val
= d
->prev_val
[2];
2829 d
->prev_val
[2] = oldprevval2
;
2832 d
->process_field (t
->u
.p
, d
);
2836 int loopcounter
= d
->loopcounter
;
2837 const char *oldval
= d
->val
;
2838 const char *oldprevval3
= d
->prev_val
[3];
2841 oprintf (d
->of
, "%*sif (%s != NULL) {\n", d
->indent
, "", d
->val
);
2843 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
2844 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != (size_t)(", d
->indent
,
2845 "", loopcounter
, loopcounter
);
2846 if (!d
->in_record_p
)
2847 output_escaped_param (d
, length
, "length");
2849 oprintf (d
->of
, "l%d", loopcounter
);
2850 if (d
->have_this_obj
)
2851 /* Try to unswitch loops (see PR53880). */
2852 oprintf (d
->of
, ") && ((void *)%s == this_obj", oldval
);
2853 oprintf (d
->of
, "); i%d++) {\n", loopcounter
);
2855 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
2857 d
->prev_val
[3] = oldval
;
2858 walk_type (t
->u
.p
, d
);
2861 d
->prev_val
[3] = oldprevval3
;
2864 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2865 d
->process_field (t
, d
);
2867 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2869 d
->in_ptr_field
= false;
2876 const char *oldval
= d
->val
;
2879 /* If it's an array of scalars, we optimize by not generating
2881 if (t
->u
.a
.p
->kind
== TYPE_SCALAR
)
2885 loopcounter
= d
->loopcounter
;
2887 loopcounter
= d
->counter
++;
2889 /* When walking an array, compute the length and store it in a
2890 local variable before walking the array elements, instead of
2891 recomputing the length expression each time through the loop.
2892 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2893 where the length is stored in the first array element,
2894 because otherwise that operand can get overwritten on the
2896 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2898 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
2899 if (!d
->in_record_p
|| !length
)
2901 oprintf (d
->of
, "%*ssize_t l%d = (size_t)(",
2902 d
->indent
, "", loopcounter
);
2904 output_escaped_param (d
, length
, "length");
2906 oprintf (d
->of
, "%s", t
->u
.a
.len
);
2907 oprintf (d
->of
, ");\n");
2910 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2912 loopcounter
, loopcounter
, loopcounter
, loopcounter
);
2914 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
2916 walk_type (t
->u
.a
.p
, d
);
2921 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2923 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2931 const char *oldval
= d
->val
;
2932 const char *oldprevval1
= d
->prev_val
[1];
2933 const char *oldprevval2
= d
->prev_val
[2];
2934 const int union_p
= t
->kind
== TYPE_UNION
;
2935 int seen_default_p
= 0;
2937 int lengths_seen
= 0;
2939 bool any_length_seen
= false;
2941 if (!t
->u
.s
.line
.file
)
2942 error_at_line (d
->line
, "incomplete structure `%s'", t
->u
.s
.tag
);
2944 if ((d
->bitmap
& t
->u
.s
.bitmap
) != d
->bitmap
)
2946 error_at_line (d
->line
,
2947 "structure `%s' defined for mismatching languages",
2949 error_at_line (&t
->u
.s
.line
, "one structure defined here");
2952 /* Some things may also be defined in the structure's options. */
2953 for (o
= t
->u
.s
.opt
; o
; o
= o
->next
)
2954 if (!desc
&& strcmp (o
->name
, "desc") == 0
2955 && o
->kind
== OPTION_STRING
)
2956 desc
= o
->info
.string
;
2958 d
->prev_val
[2] = oldval
;
2959 d
->prev_val
[1] = oldprevval2
;
2964 error_at_line (d
->line
,
2965 "missing `desc' option for union `%s'",
2969 oprintf (d
->of
, "%*sswitch ((int) (", d
->indent
, "");
2970 output_escaped_param (d
, desc
, "desc");
2971 oprintf (d
->of
, "))\n");
2973 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2977 /* We have a "desc" option on a struct, signifying the
2978 base class within a GC-managed inheritance hierarchy.
2979 The current code specialcases the base class, then walks
2980 into subclasses, recursing into this routine to handle them.
2981 This organization requires the base class to have a case in
2982 the switch statement, and hence a tag value is mandatory
2983 for the base class. This restriction could be removed, but
2984 it would require some restructing of this code. */
2987 error_at_line (d
->line
,
2988 "missing `tag' option for type `%s'",
2991 oprintf (d
->of
, "%*sswitch ((int) (", d
->indent
, "");
2992 output_escaped_param (d
, desc
, "desc");
2993 oprintf (d
->of
, "))\n");
2995 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2996 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", type_tag
);
3000 FOR_ALL_INHERITED_FIELDS (t
, f
)
3004 const char *fieldlength
= NULL
;
3006 d
->reorder_fn
= NULL
;
3007 for (oo
= f
->opt
; oo
; oo
= oo
->next
)
3008 if (strcmp (oo
->name
, "skip") == 0)
3010 else if (strcmp (oo
->name
, "length") == 0
3011 && oo
->kind
== OPTION_STRING
)
3012 fieldlength
= oo
->info
.string
;
3022 if (!any_length_seen
)
3024 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
3027 any_length_seen
= true;
3029 oprintf (d
->of
, "%*ssize_t l%d = (size_t)(",
3030 d
->indent
, "", d
->counter
- 1);
3031 output_escaped_param (d
, fieldlength
, "length");
3032 oprintf (d
->of
, ");\n");
3036 endcounter
= d
->counter
;
3038 FOR_ALL_INHERITED_FIELDS (t
, f
)
3041 const char *dot
= ".";
3042 const char *tagid
= NULL
;
3045 const char *fieldlength
= NULL
;
3048 d
->reorder_fn
= NULL
;
3049 for (oo
= f
->opt
; oo
; oo
= oo
->next
)
3050 if (strcmp (oo
->name
, "dot") == 0
3051 && oo
->kind
== OPTION_STRING
)
3052 dot
= oo
->info
.string
;
3053 else if (strcmp (oo
->name
, "tag") == 0
3054 && oo
->kind
== OPTION_STRING
)
3055 tagid
= oo
->info
.string
;
3056 else if (strcmp (oo
->name
, "skip") == 0)
3058 else if (strcmp (oo
->name
, "default") == 0)
3060 else if (strcmp (oo
->name
, "reorder") == 0
3061 && oo
->kind
== OPTION_STRING
)
3062 d
->reorder_fn
= oo
->info
.string
;
3063 else if (strcmp (oo
->name
, "length") == 0
3064 && oo
->kind
== OPTION_STRING
)
3065 fieldlength
= oo
->info
.string
;
3070 if (union_p
&& tagid
)
3072 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", tagid
);
3075 else if (union_p
&& default_p
)
3077 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
3081 else if (!union_p
&& (default_p
|| tagid
))
3082 error_at_line (d
->line
,
3083 "can't use `%s' outside a union on field `%s'",
3084 default_p
? "default" : "tag", f
->name
);
3085 else if (union_p
&& !(default_p
|| tagid
)
3086 && f
->type
->kind
== TYPE_SCALAR
)
3089 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3090 get_input_file_name (d
->line
->file
), d
->line
->line
,
3094 else if (union_p
&& !(default_p
|| tagid
))
3095 error_at_line (d
->line
,
3096 "field `%s' is missing `tag' or `default' option",
3101 d
->loopcounter
= endcounter
- lengths_seen
--;
3105 d
->val
= newval
= xasprintf ("%s%s%s", oldval
, dot
, f
->name
);
3107 d
->used_length
= false;
3108 d
->in_record_p
= !union_p
;
3110 walk_type (f
->type
, d
);
3112 d
->in_record_p
= false;
3118 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
3122 d
->reorder_fn
= NULL
;
3125 d
->prev_val
[1] = oldprevval1
;
3126 d
->prev_val
[2] = oldprevval2
;
3128 if (union_p
&& !seen_default_p
)
3130 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
3131 oprintf (d
->of
, "%*s break;\n", d
->indent
, "");
3134 if (desc
&& !union_p
)
3136 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
3141 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3146 /* Add cases to handle subclasses. */
3147 struct seen_tag
*tags
= NULL
;
3148 walk_subclasses (t
, d
, &tags
);
3150 /* Ensure that if someone forgets a "tag" option that we don't
3151 silent fail to traverse that subclass's fields. */
3152 if (!seen_default_p
)
3154 oprintf (d
->of
, "%*s/* Unrecognized tag value. */\n",
3156 oprintf (d
->of
, "%*sdefault: gcc_unreachable (); \n",
3160 /* End of the switch statement */
3161 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3164 if (any_length_seen
)
3167 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3172 case TYPE_LANG_STRUCT
:
3175 for (nt
= t
->u
.s
.lang_struct
; nt
; nt
= nt
->next
)
3176 if ((d
->bitmap
& nt
->u
.s
.bitmap
) == d
->bitmap
)
3179 error_at_line (d
->line
, "structure `%s' differs between languages",
3186 case TYPE_USER_STRUCT
:
3187 d
->process_field (t
, d
);
3191 case TYPE_UNDEFINED
:
3196 /* process_field routine for marking routines. */
3199 write_types_process_field (type_p f
, const struct walk_type_data
*d
)
3201 const struct write_types_data
*wtd
;
3202 const char *cast
= d
->needs_cast_p
? "(void *)" : "";
3203 wtd
= (const struct write_types_data
*) d
->cookie
;
3208 case TYPE_UNDEFINED
:
3211 oprintf (d
->of
, "%*s%s (%s%s", d
->indent
, "",
3212 wtd
->subfield_marker_routine
, cast
, d
->val
);
3213 if (wtd
->param_prefix
)
3215 if (f
->u
.p
->kind
== TYPE_SCALAR
)
3216 /* The current type is a pointer to a scalar (so not
3217 considered like a pointer to instances of user defined
3218 types) and we are seeing it; it means we must be even
3219 more careful about the second argument of the
3220 SUBFIELD_MARKER_ROUTINE call. That argument must
3221 always be the instance of the type for which
3222 write_func_for_structure was called - this really is
3223 what the function SUBFIELD_MARKER_ROUTINE expects.
3224 That is, it must be an instance of the ORIG_S type
3225 parameter of write_func_for_structure. The convention
3226 is that that argument must be "x" in that case (as set
3227 by write_func_for_structure). The problem is, we can't
3228 count on d->prev_val[3] to be always set to "x" in that
3229 case. Sometimes walk_type can set it to something else
3230 (to e.g cooperate with write_array when called from
3231 write_roots). So let's set it to "x" here then. */
3232 oprintf (d
->of
, ", x");
3234 oprintf (d
->of
, ", %s", d
->prev_val
[3]);
3237 oprintf (d
->of
, ", gt_%s_", wtd
->param_prefix
);
3238 output_mangled_typename (d
->of
, d
->orig_s
);
3241 oprintf (d
->of
, ", gt_%sa_%s", wtd
->param_prefix
, d
->prev_val
[0]);
3243 oprintf (d
->of
, ");\n");
3244 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
3245 oprintf (d
->of
, "%*s%s (%s%s, %s, %s);\n", d
->indent
, "",
3246 wtd
->reorder_note_routine
, cast
, d
->val
,
3247 d
->prev_val
[3], d
->reorder_fn
);
3253 case TYPE_LANG_STRUCT
:
3254 case TYPE_USER_STRUCT
:
3255 if (f
->kind
== TYPE_USER_STRUCT
&& !d
->in_ptr_field
)
3257 /* If F is a user-defined type and the field is not a
3258 pointer to the type, then we should not generate the
3259 standard pointer-marking code. All we need to do is call
3260 the user-provided marking function to process the fields
3262 oprintf (d
->of
, "%*sgt_%sx (&(%s));\n", d
->indent
, "", wtd
->prefix
,
3267 oprintf (d
->of
, "%*sgt_%s_", d
->indent
, "", wtd
->prefix
);
3268 output_mangled_typename (d
->of
, f
);
3269 oprintf (d
->of
, " (%s%s);\n", cast
, d
->val
);
3270 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
3271 oprintf (d
->of
, "%*s%s (%s%s, %s%s, %s);\n", d
->indent
, "",
3272 wtd
->reorder_note_routine
, cast
, d
->val
, cast
, d
->val
,
3285 /* Return an output file that is suitable for definitions which can
3286 reference struct S */
3289 get_output_file_for_structure (const_type_p s
)
3291 const input_file
*fn
;
3293 gcc_assert (union_or_struct_p (s
));
3294 fn
= s
->u
.s
.line
.file
;
3296 /* The call to get_output_file_with_visibility may update fn by
3297 caching its result inside, so we need the CONST_CAST. */
3298 return get_output_file_with_visibility (CONST_CAST (input_file
*, fn
));
3302 /* Returns the specifier keyword for a string or union type S, empty string
3306 get_type_specifier (const type_p s
)
3308 if (s
->kind
== TYPE_STRUCT
)
3310 else if (s
->kind
== TYPE_LANG_STRUCT
)
3311 return get_type_specifier (s
->u
.s
.lang_struct
);
3312 else if (s
->kind
== TYPE_UNION
)
3318 /* Emits a declaration for type TY (assumed to be a union or a
3319 structure) on stream OUT. */
3322 write_type_decl (outf_p out
, type_p ty
)
3324 if (union_or_struct_p (ty
))
3325 oprintf (out
, "%s%s", get_type_specifier (ty
), ty
->u
.s
.tag
);
3326 else if (ty
->kind
== TYPE_SCALAR
)
3328 if (ty
->u
.scalar_is_char
)
3329 oprintf (out
, "const char");
3331 oprintf (out
, "void");
3333 else if (ty
->kind
== TYPE_POINTER
)
3335 write_type_decl (out
, ty
->u
.p
);
3336 oprintf (out
, " *");
3338 else if (ty
->kind
== TYPE_ARRAY
)
3340 write_type_decl (out
, ty
->u
.a
.p
);
3341 oprintf (out
, " *");
3343 else if (ty
->kind
== TYPE_STRING
)
3345 oprintf (out
, "const char *");
3352 /* Write on OF the name of the marker function for structure S. PREFIX
3353 is the prefix to use (to distinguish ggc from pch markers). */
3356 write_marker_function_name (outf_p of
, type_p s
, const char *prefix
)
3358 if (union_or_struct_p (s
))
3360 const char *id_for_tag
= filter_type_name (s
->u
.s
.tag
);
3361 oprintf (of
, "gt_%sx_%s", prefix
, id_for_tag
);
3362 if (id_for_tag
!= s
->u
.s
.tag
)
3363 free (CONST_CAST (char *, id_for_tag
));
3369 /* Write on OF a user-callable routine to act as an entry point for
3370 the marking routine for S, generated by write_func_for_structure.
3371 WTD distinguishes between ggc and pch markers. */
3374 write_user_func_for_structure_ptr (outf_p of
, type_p s
, const write_types_data
*wtd
)
3376 gcc_assert (union_or_struct_p (s
));
3378 type_p alias_of
= NULL
;
3379 for (options_p opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3380 if (strcmp (opt
->name
, "ptr_alias") == 0)
3382 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3383 we do not generate marking code for ORIG_S here. Instead, a
3384 forwarder #define in gtype-desc.h will cause every call to its
3385 marker to call the target of this alias.
3387 However, we still want to create a user entry code for the
3388 aliased type. So, if ALIAS_OF is set, we only generate the
3389 user-callable marker function. */
3390 alias_of
= opt
->info
.type
;
3394 DBGPRINTF ("write_user_func_for_structure_ptr: %s %s", s
->u
.s
.tag
,
3397 /* Only write the function once. */
3398 if (s
->u
.s
.wrote_user_func_for_ptr
[wtd
->kind
])
3400 s
->u
.s
.wrote_user_func_for_ptr
[wtd
->kind
] = true;
3402 oprintf (of
, "\nvoid\n");
3403 oprintf (of
, "gt_%sx (", wtd
->prefix
);
3404 write_type_decl (of
, s
);
3405 oprintf (of
, " *& x)\n");
3406 oprintf (of
, "{\n");
3407 oprintf (of
, " if (x)\n ");
3408 write_marker_function_name (of
,
3409 alias_of
? alias_of
: get_ultimate_base_class (s
),
3411 oprintf (of
, " ((void *) x);\n");
3412 oprintf (of
, "}\n");
3416 /* Write a function to mark all the fields of type S on OF. PREFIX
3417 and D are as in write_user_marking_functions. */
3420 write_user_func_for_structure_body (type_p s
, const char *prefix
,
3421 struct walk_type_data
*d
)
3423 oprintf (d
->of
, "\nvoid\n");
3424 oprintf (d
->of
, "gt_%sx (", prefix
);
3425 write_type_decl (d
->of
, s
);
3426 oprintf (d
->of
, "& x_r ATTRIBUTE_UNUSED)\n");
3427 oprintf (d
->of
, "{\n");
3428 oprintf (d
->of
, " ");
3429 write_type_decl (d
->of
, s
);
3430 oprintf (d
->of
, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3434 oprintf (d
->of
, "}\n");
3437 /* Emit the user-callable functions needed to mark all the types used
3438 by the user structure S. PREFIX is the prefix to use to
3439 distinguish ggc and pch markers. D contains data needed to pass to
3440 walk_type when traversing the fields of a type.
3442 For every type T referenced by S, two routines are generated: one
3443 that takes 'T *', marks the pointer and calls the second routine,
3444 which just marks the fields of T. */
3447 write_user_marking_functions (type_p s
,
3448 const write_types_data
*w
,
3449 struct walk_type_data
*d
)
3451 gcc_assert (s
->kind
== TYPE_USER_STRUCT
);
3453 for (pair_p fld
= s
->u
.s
.fields
; fld
; fld
= fld
->next
)
3455 type_p fld_type
= fld
->type
;
3456 if (fld_type
->kind
== TYPE_POINTER
)
3458 type_p pointed_to_type
= fld_type
->u
.p
;
3459 if (union_or_struct_p (pointed_to_type
))
3460 write_user_func_for_structure_ptr (d
->of
, pointed_to_type
, w
);
3462 else if (union_or_struct_p (fld_type
))
3463 write_user_func_for_structure_body (fld_type
, w
->prefix
, d
);
3468 /* For S, a structure that's part of ORIG_S write out a routine that:
3469 - Takes a parameter, a void * but actually of type *S
3470 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3471 field of S or its substructures and (in some cases) things
3472 that are pointed to by S. */
3475 write_func_for_structure (type_p orig_s
, type_p s
,
3476 const struct write_types_data
*wtd
)
3478 const char *chain_next
= NULL
;
3479 const char *chain_prev
= NULL
;
3480 const char *chain_circular
= NULL
;
3482 struct walk_type_data d
;
3484 if (s
->u
.s
.base_class
)
3486 /* Verify that the base class has a "desc", since otherwise
3487 the traversal hooks there won't attempt to visit fields of
3488 subclasses such as this one. */
3489 const_type_p ubc
= get_ultimate_base_class (s
);
3490 if ((!opts_have (ubc
->u
.s
.opt
, "user")
3491 && !opts_have (ubc
->u
.s
.opt
, "desc")))
3492 error_at_line (&s
->u
.s
.line
,
3493 ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
3494 ", but '%s' lacks a discriminator 'desc' option"),
3495 s
->u
.s
.tag
, ubc
->u
.s
.tag
, ubc
->u
.s
.tag
);
3497 /* Don't write fns for subclasses, only for the ultimate base class
3498 within an inheritance hierarchy. */
3502 memset (&d
, 0, sizeof (d
));
3503 d
.of
= get_output_file_for_structure (s
);
3505 bool for_user
= false;
3506 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3507 if (strcmp (opt
->name
, "chain_next") == 0
3508 && opt
->kind
== OPTION_STRING
)
3509 chain_next
= opt
->info
.string
;
3510 else if (strcmp (opt
->name
, "chain_prev") == 0
3511 && opt
->kind
== OPTION_STRING
)
3512 chain_prev
= opt
->info
.string
;
3513 else if (strcmp (opt
->name
, "chain_circular") == 0
3514 && opt
->kind
== OPTION_STRING
)
3515 chain_circular
= opt
->info
.string
;
3516 else if (strcmp (opt
->name
, "for_user") == 0)
3518 if (chain_prev
!= NULL
&& chain_next
== NULL
)
3519 error_at_line (&s
->u
.s
.line
, "chain_prev without chain_next");
3520 if (chain_circular
!= NULL
&& chain_next
!= NULL
)
3521 error_at_line (&s
->u
.s
.line
, "chain_circular with chain_next");
3522 if (chain_circular
!= NULL
)
3523 chain_next
= chain_circular
;
3525 d
.process_field
= write_types_process_field
;
3529 d
.line
= &s
->u
.s
.line
;
3530 d
.bitmap
= s
->u
.s
.bitmap
;
3531 d
.prev_val
[0] = "*x";
3532 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
3533 d
.prev_val
[3] = "x";
3535 d
.have_this_obj
= false;
3537 oprintf (d
.of
, "\n");
3538 oprintf (d
.of
, "void\n");
3539 write_marker_function_name (d
.of
, orig_s
, wtd
->prefix
);
3540 oprintf (d
.of
, " (void *x_p)\n");
3541 oprintf (d
.of
, "{\n ");
3542 write_type_decl (d
.of
, s
);
3543 oprintf (d
.of
, " * %sx = (", chain_next
== NULL
? "const " : "");
3544 write_type_decl (d
.of
, s
);
3545 oprintf (d
.of
, " *)x_p;\n");
3546 if (chain_next
!= NULL
)
3548 /* TYPE_USER_STRUCTs should not occur here. These structures
3549 are completely handled by user code. */
3550 gcc_assert (orig_s
->kind
!= TYPE_USER_STRUCT
);
3552 oprintf (d
.of
, " ");
3553 write_type_decl (d
.of
, s
);
3554 oprintf (d
.of
, " * xlimit = x;\n");
3556 if (chain_next
== NULL
)
3558 oprintf (d
.of
, " if (%s (x", wtd
->marker_routine
);
3559 if (wtd
->param_prefix
)
3561 oprintf (d
.of
, ", x, gt_%s_", wtd
->param_prefix
);
3562 output_mangled_typename (d
.of
, orig_s
);
3564 oprintf (d
.of
, "))\n");
3568 if (chain_circular
!= NULL
)
3569 oprintf (d
.of
, " if (!%s (xlimit", wtd
->marker_routine
);
3571 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
3572 if (wtd
->param_prefix
)
3574 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
3575 output_mangled_typename (d
.of
, orig_s
);
3577 oprintf (d
.of
, "))\n");
3578 if (chain_circular
!= NULL
)
3579 oprintf (d
.of
, " return;\n do\n");
3581 oprintf (d
.of
, " xlimit = (");
3582 d
.prev_val
[2] = "*xlimit";
3583 output_escaped_param (&d
, chain_next
, "chain_next");
3584 oprintf (d
.of
, ");\n");
3585 if (chain_prev
!= NULL
)
3587 oprintf (d
.of
, " if (x != xlimit)\n");
3588 oprintf (d
.of
, " for (;;)\n");
3589 oprintf (d
.of
, " {\n");
3590 oprintf (d
.of
, " %s %s * const xprev = (",
3591 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
3593 d
.prev_val
[2] = "*x";
3594 output_escaped_param (&d
, chain_prev
, "chain_prev");
3595 oprintf (d
.of
, ");\n");
3596 oprintf (d
.of
, " if (xprev == NULL) break;\n");
3597 oprintf (d
.of
, " x = xprev;\n");
3598 oprintf (d
.of
, " (void) %s (xprev", wtd
->marker_routine
);
3599 if (wtd
->param_prefix
)
3601 oprintf (d
.of
, ", xprev, gt_%s_", wtd
->param_prefix
);
3602 output_mangled_typename (d
.of
, orig_s
);
3604 oprintf (d
.of
, ");\n");
3605 oprintf (d
.of
, " }\n");
3607 if (chain_circular
!= NULL
)
3609 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
3610 if (wtd
->param_prefix
)
3612 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
3613 output_mangled_typename (d
.of
, orig_s
);
3615 oprintf (d
.of
, "));\n");
3616 oprintf (d
.of
, " do\n");
3619 oprintf (d
.of
, " while (x != xlimit)\n");
3621 oprintf (d
.of
, " {\n");
3623 d
.prev_val
[2] = "*x";
3625 if (orig_s
->kind
!= TYPE_USER_STRUCT
)
3629 /* User structures have no fields to walk. Simply generate a call
3630 to the user-provided structure marker. */
3631 oprintf (d
.of
, "%*sgt_%sx (x);\n", d
.indent
, "", wtd
->prefix
);
3634 if (chain_next
!= NULL
)
3636 oprintf (d
.of
, " x = (");
3637 output_escaped_param (&d
, chain_next
, "chain_next");
3638 oprintf (d
.of
, ");\n");
3641 oprintf (d
.of
, " }\n");
3642 if (chain_circular
!= NULL
)
3643 oprintf (d
.of
, " while (x != xlimit);\n");
3644 oprintf (d
.of
, "}\n");
3646 if (orig_s
->kind
== TYPE_USER_STRUCT
)
3647 write_user_marking_functions (orig_s
, wtd
, &d
);
3651 write_user_func_for_structure_body (orig_s
, wtd
->prefix
, &d
);
3652 write_user_func_for_structure_ptr (d
.of
, orig_s
, wtd
);
3657 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3660 write_types (outf_p output_header
, type_p structures
,
3661 const struct write_types_data
*wtd
)
3663 int nbfun
= 0; /* Count the emitted functions. */
3666 oprintf (output_header
, "\n/* %s*/\n", wtd
->comment
);
3668 /* We first emit the macros and the declarations. Functions' code is
3669 emitted afterwards. This is needed in plugin mode. */
3670 oprintf (output_header
, "/* Macros and declarations. */\n");
3671 for (s
= structures
; s
; s
= s
->next
)
3672 /* Do not emit handlers for derived classes; we only ever deal with
3673 the ultimate base class within an inheritance hierarchy. */
3674 if ((s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
3675 && !s
->u
.s
.base_class
)
3679 if (s
->gc_used
== GC_MAYBE_POINTED_TO
&& s
->u
.s
.line
.file
== NULL
)
3682 const char *s_id_for_tag
= filter_type_name (s
->u
.s
.tag
);
3684 oprintf (output_header
, "#define gt_%s_", wtd
->prefix
);
3685 output_mangled_typename (output_header
, s
);
3686 oprintf (output_header
, "(X) do { \\\n");
3687 oprintf (output_header
,
3688 " if ((intptr_t)(X) != 0) gt_%sx_%s (X);\\\n",
3689 wtd
->prefix
, s_id_for_tag
);
3690 oprintf (output_header
, " } while (0)\n");
3692 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3693 if (strcmp (opt
->name
, "ptr_alias") == 0
3694 && opt
->kind
== OPTION_TYPE
)
3696 const_type_p
const t
= (const_type_p
) opt
->info
.type
;
3697 if (t
->kind
== TYPE_STRUCT
3698 || t
->kind
== TYPE_UNION
|| t
->kind
== TYPE_LANG_STRUCT
)
3700 const char *t_id_for_tag
= filter_type_name (t
->u
.s
.tag
);
3701 oprintf (output_header
,
3702 "#define gt_%sx_%s gt_%sx_%s\n",
3703 wtd
->prefix
, s
->u
.s
.tag
, wtd
->prefix
, t_id_for_tag
);
3704 if (t_id_for_tag
!= t
->u
.s
.tag
)
3705 free (CONST_CAST (char *, t_id_for_tag
));
3708 error_at_line (&s
->u
.s
.line
,
3709 "structure alias is not a structure");
3715 /* Declare the marker procedure only once. */
3716 oprintf (output_header
,
3717 "extern void gt_%sx_%s (void *);\n",
3718 wtd
->prefix
, s_id_for_tag
);
3720 if (s_id_for_tag
!= s
->u
.s
.tag
)
3721 free (CONST_CAST (char *, s_id_for_tag
));
3723 if (s
->u
.s
.line
.file
== NULL
)
3725 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
3731 /* At last we emit the functions code. */
3732 oprintf (output_header
, "\n/* functions code */\n");
3733 for (s
= structures
; s
; s
= s
->next
)
3734 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
3738 if (s
->gc_used
== GC_MAYBE_POINTED_TO
&& s
->u
.s
.line
.file
== NULL
)
3740 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3741 if (strcmp (opt
->name
, "ptr_alias") == 0)
3746 if (s
->kind
== TYPE_LANG_STRUCT
)
3749 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
3752 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3753 nbfun
, (void*) ss
, ss
->u
.s
.tag
);
3754 write_func_for_structure (s
, ss
, wtd
);
3760 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3761 nbfun
, (void*) s
, s
->u
.s
.tag
);
3762 write_func_for_structure (s
, s
, wtd
);
3767 /* Structure s is not possibly pointed to, so can be ignored. */
3768 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3769 (void*)s
, s
->u
.s
.tag
,
3773 if (verbosity_level
>= 2)
3774 printf ("%s emitted %d routines for %s\n",
3775 progname
, nbfun
, wtd
->comment
);
3778 static const struct write_types_data ggc_wtd
= {
3779 "ggc_m", NULL
, "ggc_mark", "ggc_test_and_set_mark", NULL
,
3780 "GC marker procedures. ",
3784 static const struct write_types_data pch_wtd
= {
3785 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3786 "gt_pch_note_reorder",
3787 "PCH type-walking procedures. ",
3791 /* Write out the local pointer-walking routines. */
3793 /* process_field routine for local pointer-walking for user-callable
3794 routines. The difference between this and
3795 write_types_local_process_field is that, in this case, we do not
3796 need to check whether the given pointer matches the address of the
3797 parent structure. This check was already generated by the call
3798 to gt_pch_nx in the main gt_pch_p_*() function that is calling
3802 write_types_local_user_process_field (type_p f
, const struct walk_type_data
*d
)
3809 case TYPE_LANG_STRUCT
:
3811 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
3814 case TYPE_USER_STRUCT
:
3815 if (d
->in_ptr_field
)
3816 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
3818 oprintf (d
->of
, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3819 d
->indent
, "", d
->val
);
3827 case TYPE_UNDEFINED
:
3833 /* Write a function to PCH walk all the fields of type S on OF.
3834 D contains data needed by walk_type to recurse into the fields of S. */
3837 write_pch_user_walking_for_structure_body (type_p s
, struct walk_type_data
*d
)
3839 oprintf (d
->of
, "\nvoid\n");
3840 oprintf (d
->of
, "gt_pch_nx (");
3841 write_type_decl (d
->of
, s
);
3842 oprintf (d
->of
, "* x ATTRIBUTE_UNUSED,\n"
3843 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3844 "\tATTRIBUTE_UNUSED void *cookie)\n");
3845 oprintf (d
->of
, "{\n");
3848 d
->process_field
= write_types_local_user_process_field
;
3850 oprintf (d
->of
, "}\n");
3854 /* Emit the user-callable functions needed to mark all the types used
3855 by the user structure S. PREFIX is the prefix to use to
3856 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
3857 chain_next option defined. D contains data needed to pass to
3858 walk_type when traversing the fields of a type.
3860 For every type T referenced by S, two routines are generated: one
3861 that takes 'T *', marks the pointer and calls the second routine,
3862 which just marks the fields of T. */
3865 write_pch_user_walking_functions (type_p s
, struct walk_type_data
*d
)
3867 gcc_assert (s
->kind
== TYPE_USER_STRUCT
);
3869 for (pair_p fld
= s
->u
.s
.fields
; fld
; fld
= fld
->next
)
3871 type_p fld_type
= fld
->type
;
3872 if (union_or_struct_p (fld_type
))
3873 write_pch_user_walking_for_structure_body (fld_type
, d
);
3878 /* process_field routine for local pointer-walking. */
3881 write_types_local_process_field (type_p f
, const struct walk_type_data
*d
)
3883 gcc_assert (d
->have_this_obj
);
3889 case TYPE_LANG_STRUCT
:
3891 oprintf (d
->of
, "%*sif ((void *)(%s) == this_obj)\n", d
->indent
, "",
3893 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
3896 case TYPE_USER_STRUCT
:
3897 oprintf (d
->of
, "%*sif ((void *)(%s) == this_obj)\n", d
->indent
, "",
3899 if (d
->in_ptr_field
)
3900 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
3902 oprintf (d
->of
, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3903 d
->indent
, "", d
->val
);
3911 case TYPE_UNDEFINED
:
3917 /* For S, a structure that's part of ORIG_S, and using parameters
3918 PARAM, write out a routine that:
3919 - Is of type gt_note_pointers
3920 - Calls PROCESS_FIELD on each field of S or its substructures.
3924 write_local_func_for_structure (const_type_p orig_s
, type_p s
)
3926 struct walk_type_data d
;
3928 /* Don't write fns for subclasses, only for the ultimate base class
3929 within an inheritance hierarchy. */
3930 if (s
->u
.s
.base_class
)
3933 memset (&d
, 0, sizeof (d
));
3934 d
.of
= get_output_file_for_structure (s
);
3935 d
.process_field
= write_types_local_process_field
;
3937 d
.line
= &s
->u
.s
.line
;
3938 d
.bitmap
= s
->u
.s
.bitmap
;
3939 d
.prev_val
[0] = d
.prev_val
[2] = "*x";
3940 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
3941 d
.prev_val
[3] = "x";
3943 d
.fn_wants_lvalue
= true;
3945 oprintf (d
.of
, "\n");
3946 oprintf (d
.of
, "void\n");
3947 oprintf (d
.of
, "gt_pch_p_");
3948 output_mangled_typename (d
.of
, orig_s
);
3949 oprintf (d
.of
, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3951 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3952 "\tATTRIBUTE_UNUSED void *cookie)\n");
3953 oprintf (d
.of
, "{\n");
3954 oprintf (d
.of
, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3955 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
,
3956 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
3958 d
.have_this_obj
= true;
3960 if (s
->kind
!= TYPE_USER_STRUCT
)
3964 /* User structures have no fields to walk. Simply generate a
3965 call to the user-provided PCH walker. */
3966 oprintf (d
.of
, "%*sif ((void *)(%s) == this_obj)\n", d
.indent
, "",
3968 oprintf (d
.of
, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3969 d
.indent
, "", d
.val
);
3972 oprintf (d
.of
, "}\n");
3974 /* Write user-callable entry points for the PCH walking routines. */
3975 if (orig_s
->kind
== TYPE_USER_STRUCT
)
3976 write_pch_user_walking_functions (s
, &d
);
3978 for (options_p o
= s
->u
.s
.opt
; o
; o
= o
->next
)
3979 if (strcmp (o
->name
, "for_user") == 0)
3981 write_pch_user_walking_for_structure_body (s
, &d
);
3986 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
3989 write_local (outf_p output_header
, type_p structures
)
3996 oprintf (output_header
, "\n/* Local pointer-walking routines. */\n");
3997 for (s
= structures
; s
; s
= s
->next
)
3998 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
4002 if (s
->u
.s
.line
.file
== NULL
)
4004 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
4005 if (strcmp (opt
->name
, "ptr_alias") == 0
4006 && opt
->kind
== OPTION_TYPE
)
4008 const_type_p
const t
= (const_type_p
) opt
->info
.type
;
4009 if (t
->kind
== TYPE_STRUCT
4010 || t
->kind
== TYPE_UNION
|| t
->kind
== TYPE_LANG_STRUCT
)
4012 oprintf (output_header
, "#define gt_pch_p_");
4013 output_mangled_typename (output_header
, s
);
4014 oprintf (output_header
, " gt_pch_p_");
4015 output_mangled_typename (output_header
, t
);
4016 oprintf (output_header
, "\n");
4019 error_at_line (&s
->u
.s
.line
,
4020 "structure alias is not a structure");
4026 /* Declare the marker procedure only once. */
4027 oprintf (output_header
, "extern void gt_pch_p_");
4028 output_mangled_typename (output_header
, s
);
4029 oprintf (output_header
,
4030 "\n (void *, void *, gt_pointer_operator, void *);\n");
4032 if (s
->kind
== TYPE_LANG_STRUCT
)
4035 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
4036 write_local_func_for_structure (s
, ss
);
4039 write_local_func_for_structure (s
, s
);
4043 /* Nonzero if S is a type for which typed GC allocators should be output. */
4045 #define USED_BY_TYPED_GC_P(s) \
4046 ((s->kind == TYPE_POINTER \
4047 && (s->u.p->gc_used == GC_POINTED_TO \
4048 || s->u.p->gc_used == GC_USED)) \
4049 || (union_or_struct_p (s) \
4050 && ((s)->gc_used == GC_POINTED_TO \
4051 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4052 && s->u.s.line.file != NULL) \
4053 || ((s)->gc_used == GC_USED \
4054 && !startswith (s->u.s.tag, "anonymous")) \
4055 || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
4059 /* Might T contain any non-pointer elements? */
4062 contains_scalar_p (type_p t
)
4070 return contains_scalar_p (t
->u
.a
.p
);
4071 case TYPE_USER_STRUCT
:
4072 /* User-marked structures will typically contain pointers. */
4075 /* Could also check for structures that have no non-pointer
4076 fields, but there aren't enough of those to worry about. */
4081 /* Mangle INPF and print it to F. */
4084 put_mangled_filename (outf_p f
, const input_file
*inpf
)
4086 /* The call to get_output_file_name may indirectly update fn since
4087 get_output_file_with_visibility caches its result inside, so we
4088 need the CONST_CAST. */
4089 const char *name
= get_output_file_name (CONST_CAST (input_file
*, inpf
));
4092 for (; *name
!= 0; name
++)
4093 if (ISALNUM (*name
))
4094 oprintf (f
, "%c", *name
);
4096 oprintf (f
, "%c", '_');
4099 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4100 LASTNAME, and NAME are all strings to insert in various places in
4101 the resulting code. */
4104 finish_root_table (struct flist
*flp
, const char *pfx
, const char *lastname
,
4105 const char *tname
, const char *name
)
4109 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
4110 if (fli2
->started_p
)
4112 oprintf (fli2
->f
, " %s\n", lastname
);
4113 oprintf (fli2
->f
, "};\n\n");
4116 for (fli2
= flp
; fli2
&& base_files
; fli2
= fli2
->next
)
4117 if (fli2
->started_p
)
4119 lang_bitmap bitmap
= get_lang_bitmap (fli2
->file
);
4122 for (fnum
= 0; bitmap
!= 0; fnum
++, bitmap
>>= 1)
4125 oprintf (base_files
[fnum
],
4126 "extern const struct %s gt_%s_", tname
, pfx
);
4127 put_mangled_filename (base_files
[fnum
], fli2
->file
);
4128 oprintf (base_files
[fnum
], "[];\n");
4134 for (fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
4135 oprintf (base_files
[fnum
],
4136 "EXPORTED_CONST struct %s * const %s[] = {\n", tname
, name
);
4140 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
4141 if (fli2
->started_p
)
4143 lang_bitmap bitmap
= get_lang_bitmap (fli2
->file
);
4146 fli2
->started_p
= 0;
4148 for (fnum
= 0; base_files
&& bitmap
!= 0; fnum
++, bitmap
>>= 1)
4151 oprintf (base_files
[fnum
], " gt_%s_", pfx
);
4152 put_mangled_filename (base_files
[fnum
], fli2
->file
);
4153 oprintf (base_files
[fnum
], ",\n");
4159 for (fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
4161 oprintf (base_files
[fnum
], " NULL\n");
4162 oprintf (base_files
[fnum
], "};\n");
4167 /* Finish off the created gt_clear_caches_file_c functions. */
4170 finish_cache_funcs (flist
*flp
)
4174 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
4175 if (fli2
->started_p
)
4177 oprintf (fli2
->f
, "}\n\n");
4180 for (fli2
= flp
; fli2
&& base_files
; fli2
= fli2
->next
)
4181 if (fli2
->started_p
)
4183 lang_bitmap bitmap
= get_lang_bitmap (fli2
->file
);
4186 for (fnum
= 0; bitmap
!= 0; fnum
++, bitmap
>>= 1)
4189 oprintf (base_files
[fnum
], "extern void gt_clear_caches_");
4190 put_mangled_filename (base_files
[fnum
], fli2
->file
);
4191 oprintf (base_files
[fnum
], " ();\n");
4195 for (size_t fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
4196 oprintf (base_files
[fnum
], "void\ngt_clear_caches ()\n{\n");
4198 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
4199 if (fli2
->started_p
)
4201 lang_bitmap bitmap
= get_lang_bitmap (fli2
->file
);
4204 fli2
->started_p
= 0;
4206 for (fnum
= 0; base_files
&& bitmap
!= 0; fnum
++, bitmap
>>= 1)
4209 oprintf (base_files
[fnum
], " gt_clear_caches_");
4210 put_mangled_filename (base_files
[fnum
], fli2
->file
);
4211 oprintf (base_files
[fnum
], " ();\n");
4215 for (size_t fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
4217 oprintf (base_files
[fnum
], "}\n");
4221 /* Write the first three fields (pointer, count and stride) for
4222 root NAME to F. V and LINE are as for write_root.
4224 Return true if the entry could be written; return false on error. */
4227 start_root_entry (outf_p f
, pair_p v
, const char *name
, struct fileloc
*line
)
4233 error_at_line (line
, "`%s' is too complex to be a root", name
);
4237 oprintf (f
, " {\n");
4238 oprintf (f
, " &%s,\n", name
);
4241 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
4243 oprintf (f
, " * (%s)", ap
->u
.a
.len
);
4244 else if (ap
== v
->type
)
4245 oprintf (f
, " * ARRAY_SIZE (%s)", v
->name
);
4247 oprintf (f
, " sizeof (%s", v
->name
);
4248 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
4250 oprintf (f
, "),\n");
4254 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4255 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4259 write_field_root (outf_p f
, pair_p v
, type_p type
, const char *name
,
4260 int has_length
, struct fileloc
*line
,
4261 bool emit_pch
, type_p field_type
, const char *field_name
)
4264 /* If the field reference is relative to V, rather than to some
4265 subcomponent of V, we can mark any subarrays with a single stride.
4266 We're effectively treating the field as a global variable in its
4268 if (v
&& type
== v
->type
)
4271 newv
.type
= field_type
;
4272 newv
.name
= ACONCAT ((v
->name
, ".", field_name
, NULL
));
4275 /* Otherwise, any arrays nested in the structure are too complex to
4277 else if (field_type
->kind
== TYPE_ARRAY
)
4279 write_root (f
, v
, field_type
, ACONCAT ((name
, ".", field_name
, NULL
)),
4280 has_length
, line
, emit_pch
);
4283 /* Write out to F the table entry and any marker routines needed to
4284 mark NAME as TYPE. V can be one of three values:
4286 - null, if NAME is too complex to represent using a single
4287 count and stride. In this case, it is an error for NAME to
4288 contain any gc-ed data.
4290 - the outermost array that contains NAME, if NAME is part of an array.
4292 - the C variable that contains NAME, if NAME is not part of an array.
4294 LINE is the line of the C source that declares the root variable.
4295 HAS_LENGTH is nonzero iff V was a variable-length array. */
4298 write_root (outf_p f
, pair_p v
, type_p type
, const char *name
, int has_length
,
4299 struct fileloc
*line
, bool emit_pch
)
4306 for (fld
= type
->u
.s
.fields
; fld
; fld
= fld
->next
)
4309 const char *desc
= NULL
;
4312 for (o
= fld
->opt
; o
; o
= o
->next
)
4313 if (strcmp (o
->name
, "skip") == 0)
4315 else if (strcmp (o
->name
, "desc") == 0
4316 && o
->kind
== OPTION_STRING
)
4317 desc
= o
->info
.string
;
4319 error_at_line (line
,
4320 "field `%s' of global `%s' has unknown option `%s'",
4321 fld
->name
, name
, o
->name
);
4325 else if (desc
&& fld
->type
->kind
== TYPE_UNION
)
4327 pair_p validf
= NULL
;
4330 for (ufld
= fld
->type
->u
.s
.fields
; ufld
; ufld
= ufld
->next
)
4332 const char *tag
= NULL
;
4334 for (oo
= ufld
->opt
; oo
; oo
= oo
->next
)
4335 if (strcmp (oo
->name
, "tag") == 0
4336 && oo
->kind
== OPTION_STRING
)
4337 tag
= oo
->info
.string
;
4338 if (tag
== NULL
|| strcmp (tag
, desc
) != 0)
4341 error_at_line (line
,
4342 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4343 name
, fld
->name
, validf
->name
,
4344 name
, fld
->name
, ufld
->name
, tag
);
4348 write_field_root (f
, v
, type
, name
, 0, line
, emit_pch
,
4350 ACONCAT ((fld
->name
, ".",
4351 validf
->name
, NULL
)));
4354 error_at_line (line
,
4355 "global `%s.%s' has `desc' option but is not union",
4358 write_field_root (f
, v
, type
, name
, 0, line
, emit_pch
, fld
->type
,
4367 newname
= xasprintf ("%s[0]", name
);
4368 write_root (f
, v
, type
->u
.a
.p
, newname
, has_length
, line
, emit_pch
);
4373 case TYPE_USER_STRUCT
:
4374 error_at_line (line
, "`%s' must be a pointer type, because it is "
4375 "a GC root and its type is marked with GTY((user))",
4383 if (!start_root_entry (f
, v
, name
, line
))
4388 if (!has_length
&& union_or_struct_p (tp
))
4390 tp
= get_ultimate_base_class (tp
);
4391 const char *id_for_tag
= filter_type_name (tp
->u
.s
.tag
);
4392 oprintf (f
, " >_ggc_mx_%s,\n", id_for_tag
);
4394 oprintf (f
, " >_pch_nx_%s", id_for_tag
);
4396 oprintf (f
, " NULL");
4397 if (id_for_tag
!= tp
->u
.s
.tag
)
4398 free (CONST_CAST (char *, id_for_tag
));
4401 && (tp
->kind
== TYPE_POINTER
|| union_or_struct_p (tp
)))
4403 oprintf (f
, " >_ggc_ma_%s,\n", name
);
4405 oprintf (f
, " >_pch_na_%s", name
);
4407 oprintf (f
, " NULL");
4411 error_at_line (line
,
4412 "global `%s' is pointer to unimplemented type",
4415 oprintf (f
, "\n },\n");
4421 if (!start_root_entry (f
, v
, name
, line
))
4424 oprintf (f
, " (gt_pointer_walker) >_ggc_m_S,\n");
4425 oprintf (f
, " (gt_pointer_walker) >_pch_n_S\n");
4426 oprintf (f
, " },\n");
4434 case TYPE_UNDEFINED
:
4436 case TYPE_LANG_STRUCT
:
4437 error_at_line (line
, "global `%s' is unimplemented type", name
);
4441 /* This generates a routine to walk an array. */
4444 write_array (outf_p f
, pair_p v
, const struct write_types_data
*wtd
)
4446 struct walk_type_data d
;
4449 memset (&d
, 0, sizeof (d
));
4455 d
.bitmap
= get_lang_bitmap (v
->line
.file
);
4457 d
.prev_val
[3] = prevval3
= xasprintf ("&%s", v
->name
);
4459 if (wtd
->param_prefix
)
4461 oprintf (f
, "static void gt_%sa_%s\n", wtd
->param_prefix
, v
->name
);
4462 oprintf (f
, " (void *, void *, gt_pointer_operator, void *);\n");
4463 oprintf (f
, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4464 wtd
->param_prefix
, v
->name
);
4466 " ATTRIBUTE_UNUSED void *x_p,\n"
4467 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4468 " ATTRIBUTE_UNUSED void * cookie)\n");
4469 oprintf (d
.of
, "{\n");
4470 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
4471 d
.process_field
= write_types_local_process_field
;
4472 d
.have_this_obj
= true;
4473 walk_type (v
->type
, &d
);
4474 oprintf (f
, "}\n\n");
4478 oprintf (f
, "static void gt_%sa_%s (void *);\n", wtd
->prefix
, v
->name
);
4479 oprintf (f
, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4480 wtd
->prefix
, v
->name
);
4482 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
4483 d
.process_field
= write_types_process_field
;
4484 d
.have_this_obj
= false;
4485 walk_type (v
->type
, &d
);
4487 oprintf (f
, "}\n\n");
4490 /* Output a table describing the locations and types of VARIABLES. */
4493 write_roots (pair_p variables
, bool emit_pch
)
4496 struct flist
*flp
= NULL
;
4498 for (v
= variables
; v
; v
= v
->next
)
4501 get_output_file_with_visibility (CONST_CAST (input_file
*,
4504 const char *length
= NULL
;
4505 int deletable_p
= 0;
4507 for (o
= v
->opt
; o
; o
= o
->next
)
4508 if (strcmp (o
->name
, "length") == 0
4509 && o
->kind
== OPTION_STRING
)
4510 length
= o
->info
.string
;
4511 else if (strcmp (o
->name
, "deletable") == 0)
4513 else if (strcmp (o
->name
, "cache") == 0)
4516 error_at_line (&v
->line
,
4517 "global `%s' has unknown option `%s'",
4520 for (fli
= flp
; fli
; fli
= fli
->next
)
4521 if (fli
->f
== f
&& f
)
4525 fli
= XNEW (struct flist
);
4529 fli
->file
= v
->line
.file
;
4530 gcc_assert (fli
->file
);
4533 oprintf (f
, "\n/* GC roots. */\n\n");
4538 && v
->type
->kind
== TYPE_POINTER
4539 && (v
->type
->u
.p
->kind
== TYPE_POINTER
4540 || v
->type
->u
.p
->kind
== TYPE_STRUCT
))
4542 write_array (f
, v
, &ggc_wtd
);
4543 write_array (f
, v
, &pch_wtd
);
4547 for (v
= variables
; v
; v
= v
->next
)
4549 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4556 for (o
= v
->opt
; o
; o
= o
->next
)
4557 if (strcmp (o
->name
, "length") == 0)
4559 else if (strcmp (o
->name
, "deletable") == 0)
4565 for (fli
= flp
; fli
; fli
= fli
->next
)
4568 if (!fli
->started_p
)
4572 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4573 put_mangled_filename (f
, v
->line
.file
);
4574 oprintf (f
, "[] = {\n");
4577 write_root (f
, v
, v
->type
, v
->name
, length_p
, &v
->line
, emit_pch
);
4580 finish_root_table (flp
, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4583 for (v
= variables
; v
; v
= v
->next
)
4585 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4591 for (o
= v
->opt
; o
; o
= o
->next
)
4592 if (strcmp (o
->name
, "deletable") == 0)
4598 for (fli
= flp
; fli
; fli
= fli
->next
)
4601 if (!fli
->started_p
)
4605 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4606 put_mangled_filename (f
, v
->line
.file
);
4607 oprintf (f
, "[] = {\n");
4610 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4614 finish_root_table (flp
, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4615 "gt_ggc_deletable_rtab");
4617 for (v
= variables
; v
; v
= v
->next
)
4619 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4625 for (o
= v
->opt
; o
; o
= o
->next
)
4626 if (strcmp (o
->name
, "cache") == 0)
4631 for (fli
= flp
; fli
; fli
= fli
->next
)
4634 if (!fli
->started_p
)
4638 oprintf (f
, "void\ngt_clear_caches_");
4639 put_mangled_filename (f
, v
->line
.file
);
4640 oprintf (f
, " ()\n{\n");
4643 oprintf (f
, " gt_cleare_cache (%s);\n", v
->name
);
4646 finish_cache_funcs (flp
);
4651 for (v
= variables
; v
; v
= v
->next
)
4653 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4659 for (o
= v
->opt
; o
; o
= o
->next
)
4660 if (strcmp (o
->name
, "deletable") == 0)
4669 if (!contains_scalar_p (v
->type
))
4672 for (fli
= flp
; fli
; fli
= fli
->next
)
4675 if (!fli
->started_p
)
4679 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4680 put_mangled_filename (f
, v
->line
.file
);
4681 oprintf (f
, "[] = {\n");
4684 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4688 finish_root_table (flp
, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4689 "gt_pch_scalar_rtab");
4692 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4693 guaranteee for somewhat increased readability. If name conflicts do happen,
4694 this funcion will have to be adjusted to be more like
4695 output_mangled_typename. */
4699 /* Dumps the value of typekind KIND. */
4702 dump_typekind (int indent
, enum typekind kind
)
4704 printf ("%*ckind = ", indent
, ' ');
4708 printf ("TYPE_SCALAR");
4711 printf ("TYPE_STRING");
4714 printf ("TYPE_STRUCT");
4716 case TYPE_UNDEFINED
:
4717 printf ("TYPE_UNDEFINED");
4719 case TYPE_USER_STRUCT
:
4720 printf ("TYPE_USER_STRUCT");
4723 printf ("TYPE_UNION");
4726 printf ("TYPE_POINTER");
4729 printf ("TYPE_ARRAY");
4731 case TYPE_LANG_STRUCT
:
4732 printf ("TYPE_LANG_STRUCT");
4740 /* Dumps the value of GC_USED flag. */
4743 dump_gc_used (int indent
, enum gc_used_enum gc_used
)
4745 printf ("%*cgc_used = ", indent
, ' ');
4749 printf ("GC_UNUSED");
4754 case GC_MAYBE_POINTED_TO
:
4755 printf ("GC_MAYBE_POINTED_TO");
4758 printf ("GC_POINTED_TO");
4766 /* Dumps the type options OPT. */
4769 dump_options (int indent
, options_p opt
)
4772 printf ("%*coptions = ", indent
, ' ');
4779 printf ("%s:string %s ", o
->name
, o
->info
.string
);
4782 printf ("%s:type ", o
->name
);
4783 dump_type (indent
+1, o
->info
.type
);
4786 printf ("%s:nested ", o
->name
);
4796 /* Dumps the source file location in LINE. */
4799 dump_fileloc (int indent
, struct fileloc line
)
4801 printf ("%*cfileloc: file = %s, line = %d\n", indent
, ' ',
4802 get_input_file_name (line
.file
),
4806 /* Recursively dumps the struct, union, or a language-specific
4810 dump_type_u_s (int indent
, type_p t
)
4814 gcc_assert (union_or_struct_p (t
));
4815 printf ("%*cu.s.tag = %s\n", indent
, ' ', t
->u
.s
.tag
);
4816 dump_fileloc (indent
, t
->u
.s
.line
);
4817 printf ("%*cu.s.fields =\n", indent
, ' ');
4818 fields
= t
->u
.s
.fields
;
4821 dump_pair (indent
+ INDENT
, fields
);
4822 fields
= fields
->next
;
4824 printf ("%*cend of fields of type %p\n", indent
, ' ', (void *) t
);
4825 dump_options (indent
, t
->u
.s
.opt
);
4826 printf ("%*cu.s.bitmap = %X\n", indent
, ' ', t
->u
.s
.bitmap
);
4827 if (t
->kind
== TYPE_LANG_STRUCT
)
4829 printf ("%*cu.s.lang_struct:\n", indent
, ' ');
4830 dump_type_list (indent
+ INDENT
, t
->u
.s
.lang_struct
);
4834 /* Recursively dumps the array T. */
4837 dump_type_u_a (int indent
, type_p t
)
4839 gcc_assert (t
->kind
== TYPE_ARRAY
);
4840 printf ("%*clen = %s, u.a.p:\n", indent
, ' ', t
->u
.a
.len
);
4841 dump_type_list (indent
+ INDENT
, t
->u
.a
.p
);
4844 /* Recursively dumps the type list T. */
4847 dump_type_list (int indent
, type_p t
)
4852 dump_type (indent
, p
);
4857 static htab_t seen_types
;
4859 /* Recursively dumps the type T if it was not dumped previously. */
4862 dump_type (int indent
, type_p t
)
4866 printf ("%*cType at %p: ", indent
, ' ', (void *) t
);
4867 if (t
->kind
== TYPE_UNDEFINED
)
4869 gcc_assert (t
->gc_used
== GC_UNUSED
);
4870 printf ("undefined.\n");
4874 if (seen_types
== NULL
)
4875 seen_types
= htab_create (100, htab_hash_pointer
, htab_eq_pointer
, NULL
);
4877 slot
= htab_find_slot (seen_types
, t
, INSERT
);
4880 printf ("already seen.\n");
4886 dump_typekind (indent
, t
->kind
);
4887 printf ("%*cpointer_to = %p\n", indent
+ INDENT
, ' ',
4888 (void *) t
->pointer_to
);
4889 dump_gc_used (indent
+ INDENT
, t
->gc_used
);
4893 printf ("%*cscalar_is_char = %s\n", indent
+ INDENT
, ' ',
4894 t
->u
.scalar_is_char
? "true" : "false");
4900 case TYPE_LANG_STRUCT
:
4901 case TYPE_USER_STRUCT
:
4902 dump_type_u_s (indent
+ INDENT
, t
);
4905 printf ("%*cp:\n", indent
+ INDENT
, ' ');
4906 dump_type (indent
+ INDENT
, t
->u
.p
);
4909 dump_type_u_a (indent
+ INDENT
, t
);
4914 printf ("%*cEnd of type at %p\n", indent
, ' ', (void *) t
);
4917 /* Dumps the pair P. */
4920 dump_pair (int indent
, pair_p p
)
4922 printf ("%*cpair: name = %s\n", indent
, ' ', p
->name
);
4923 dump_type (indent
, p
->type
);
4924 dump_fileloc (indent
, p
->line
);
4925 dump_options (indent
, p
->opt
);
4926 printf ("%*cEnd of pair %s\n", indent
, ' ', p
->name
);
4929 /* Dumps the list of pairs PP. */
4932 dump_pair_list (const char *name
, pair_p pp
)
4935 printf ("%s:\n", name
);
4936 for (p
= pp
; p
!= NULL
; p
= p
->next
)
4938 printf ("End of %s\n\n", name
);
4941 /* Dumps the STRUCTURES. */
4944 dump_structures (const char *name
, type_p structures
)
4946 printf ("%s:\n", name
);
4947 dump_type_list (0, structures
);
4948 printf ("End of %s\n\n", name
);
4951 /* Dumps the internal structures of gengtype. This is useful to debug
4952 gengtype itself, or to understand what it does, e.g. for plugin
4956 dump_everything (void)
4958 dump_pair_list ("typedefs", typedefs
);
4959 dump_structures ("structures", structures
);
4960 dump_pair_list ("variables", variables
);
4962 /* Allocated with the first call to dump_type. */
4963 htab_delete (seen_types
);
4968 /* Option specification for getopt_long. */
4969 static const struct option gengtype_long_options
[] = {
4970 {"help", no_argument
, NULL
, 'h'},
4971 {"version", no_argument
, NULL
, 'V'},
4972 {"verbose", no_argument
, NULL
, 'v'},
4973 {"dump", no_argument
, NULL
, 'd'},
4974 {"debug", no_argument
, NULL
, 'D'},
4975 {"plugin", required_argument
, NULL
, 'P'},
4976 {"srcdir", required_argument
, NULL
, 'S'},
4977 {"backupdir", required_argument
, NULL
, 'B'},
4978 {"inputs", required_argument
, NULL
, 'I'},
4979 {"read-state", required_argument
, NULL
, 'r'},
4980 {"write-state", required_argument
, NULL
, 'w'},
4981 /* Terminating NULL placeholder. */
4982 {NULL
, no_argument
, NULL
, 0},
4989 printf ("Usage: %s\n", progname
);
4990 printf ("\t -h | --help " " \t# Give this help.\n");
4991 printf ("\t -D | --debug "
4992 " \t# Give debug output to debug %s itself.\n", progname
);
4993 printf ("\t -V | --version " " \t# Give version information.\n");
4994 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
4995 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
4996 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
4997 " \t# Generate for plugin.\n");
4998 printf ("\t -S | --srcdir <GCC-directory> "
4999 " \t# Specify the GCC source directory.\n");
5000 printf ("\t -B | --backupdir <directory> "
5001 " \t# Specify the backup directory for updated files.\n");
5002 printf ("\t -I | --inputs <input-list> "
5003 " \t# Specify the file with source files list.\n");
5004 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5005 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5009 print_version (void)
5011 printf ("%s %s%s\n", progname
, pkgversion_string
, version_string
);
5012 printf ("Report bugs: %s\n", bug_report_url
);
5015 /* Parse the program options using getopt_long... */
5017 parse_program_options (int argc
, char **argv
)
5020 while ((opt
= getopt_long (argc
, argv
, "hVvdP:S:B:I:w:r:D",
5021 gengtype_long_options
, NULL
)) >= 0)
5025 case 'h': /* --help */
5028 case 'V': /* --version */
5031 case 'd': /* --dump */
5034 case 'D': /* --debug */
5037 case 'v': /* --verbose */
5040 case 'P': /* --plugin */
5042 plugin_output_filename
= optarg
;
5044 fatal ("missing plugin output file name");
5046 case 'S': /* --srcdir */
5050 fatal ("missing source directory");
5051 srcdir_len
= strlen (srcdir
);
5053 case 'B': /* --backupdir */
5055 backup_dir
= optarg
;
5057 fatal ("missing backup directory");
5059 case 'I': /* --inputs */
5063 fatal ("missing input list");
5065 case 'r': /* --read-state */
5067 read_state_filename
= optarg
;
5069 fatal ("missing read state file");
5070 DBGPRINTF ("read state %s\n", optarg
);
5072 case 'w': /* --write-state */
5073 DBGPRINTF ("write state %s\n", optarg
);
5075 write_state_filename
= optarg
;
5077 fatal ("missing write state file");
5080 fprintf (stderr
, "%s: unknown flag '%c'\n", progname
, opt
);
5082 fatal ("unexpected flag");
5085 if (plugin_output_filename
)
5087 /* In plugin mode we require some input files. */
5090 fatal ("no source files given in plugin mode");
5091 nb_plugin_files
= argc
- optind
;
5092 plugin_files
= XNEWVEC (input_file
*, nb_plugin_files
);
5093 for (i
= 0; i
< (int) nb_plugin_files
; i
++)
5095 char *name
= argv
[i
+ optind
];
5096 plugin_files
[i
] = input_file_by_name (name
);
5103 /******* Manage input files. ******/
5105 /* Hash table of unique input file names. */
5106 static htab_t input_file_htab
;
5108 /* Find or allocate a new input_file by hash-consing it. */
5110 input_file_by_name (const char* name
)
5113 input_file
* f
= NULL
;
5117 namlen
= strlen (name
);
5118 f
= XCNEWVAR (input_file
, sizeof (input_file
)+namlen
+2);
5121 f
->inpisplugin
= false;
5122 strcpy (f
->inpname
, name
);
5123 slot
= htab_find_slot (input_file_htab
, f
, INSERT
);
5124 gcc_assert (slot
!= NULL
);
5127 /* Already known input file. */
5129 return (input_file
*)(*slot
);
5131 /* New input file. */
5136 /* Hash table support routines for input_file-s. */
5138 htab_hash_inputfile (const void *p
)
5140 const input_file
*inpf
= (const input_file
*) p
;
5142 return htab_hash_string (get_input_file_name (inpf
));
5146 htab_eq_inputfile (const void *x
, const void *y
)
5148 const input_file
*inpfx
= (const input_file
*) x
;
5149 const input_file
*inpfy
= (const input_file
*) y
;
5150 gcc_assert (inpfx
!= NULL
&& inpfy
!= NULL
);
5151 return !filename_cmp (get_input_file_name (inpfx
), get_input_file_name (inpfy
));
5156 main (int argc
, char **argv
)
5159 static struct fileloc pos
= { NULL
, 0 };
5160 outf_p output_header
;
5162 /* Mandatory common initializations. */
5163 progname
= "gengtype"; /* For fatal and messages. */
5164 /* Create the hash-table used to hash-cons input files. */
5166 htab_create (800, htab_hash_inputfile
, htab_eq_inputfile
, NULL
);
5167 /* Initialize our special input files. */
5168 this_file
= input_file_by_name (__FILE__
);
5169 system_h_file
= input_file_by_name ("system.h");
5170 /* Set the scalar_is_char union number for predefined scalar types. */
5171 scalar_nonchar
.u
.scalar_is_char
= FALSE
;
5172 scalar_char
.u
.scalar_is_char
= TRUE
;
5174 parse_program_options (argc
, argv
);
5178 time_t now
= (time_t) 0;
5180 DBGPRINTF ("gengtype started pid %d at %s",
5181 (int) getpid (), ctime (&now
));
5184 /* Parse the input list and the input files. */
5185 DBGPRINTF ("inputlist %s", inputlist
);
5186 if (read_state_filename
)
5189 fatal ("input list %s cannot be given with a read state file %s",
5190 inputlist
, read_state_filename
);
5191 read_state (read_state_filename
);
5192 DBGPRINT_COUNT_TYPE ("structures after read_state", structures
);
5196 /* These types are set up with #define or else outside of where
5197 we can see them. We should initialize them before calling
5199 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5201 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos
));
5202 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos
));
5203 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos
));
5204 POS_HERE (do_scalar_typedef ("double_int", &pos
));
5205 POS_HERE (do_scalar_typedef ("poly_int64_pod", &pos
));
5206 POS_HERE (do_scalar_typedef ("offset_int", &pos
));
5207 POS_HERE (do_scalar_typedef ("widest_int", &pos
));
5208 POS_HERE (do_scalar_typedef ("int64_t", &pos
));
5209 POS_HERE (do_scalar_typedef ("poly_int64", &pos
));
5210 POS_HERE (do_scalar_typedef ("poly_uint64", &pos
));
5211 POS_HERE (do_scalar_typedef ("uint64_t", &pos
));
5212 POS_HERE (do_scalar_typedef ("uint32_t", &pos
));
5213 POS_HERE (do_scalar_typedef ("uint8", &pos
));
5214 POS_HERE (do_scalar_typedef ("uintptr_t", &pos
));
5215 POS_HERE (do_scalar_typedef ("jword", &pos
));
5216 POS_HERE (do_scalar_typedef ("JCF_u2", &pos
));
5217 POS_HERE (do_scalar_typedef ("void", &pos
));
5218 POS_HERE (do_scalar_typedef ("machine_mode", &pos
));
5219 POS_HERE (do_scalar_typedef ("fixed_size_mode", &pos
));
5220 POS_HERE (do_scalar_typedef ("CONSTEXPR", &pos
));
5221 POS_HERE (do_typedef ("PTR",
5222 create_pointer (resolve_typedef ("void", &pos
)),
5225 read_input_list (inputlist
);
5226 num_build_headers
= 0;
5227 for (i
= 0; i
< num_gt_files
; i
++)
5229 const char *fname
= get_input_file_name (gt_files
[i
]);
5231 DBGPRINTF ("parsed file #%d %s", (int) i
, fname
);
5232 /* Check if this is a header file generated during the build. */
5233 int len
= strlen (fname
);
5236 && IS_DIR_SEPARATOR (fname
[1])
5237 && fname
[len
-2] == '.'
5238 && fname
[len
-1] == 'h')
5239 num_build_headers
++;
5241 if (verbosity_level
>= 1)
5242 printf ("%s parsed %d files with %d GTY types\n",
5243 progname
, (int) num_gt_files
, type_count
);
5245 DBGPRINT_COUNT_TYPE ("structures after parsing", structures
);
5248 fatal ("either an input list or a read state file should be given");
5253 if (plugin_output_filename
)
5256 /* In plugin mode, we should have read a state file, and have
5257 given at least one plugin file. */
5258 if (!read_state_filename
)
5259 fatal ("No read state given in plugin mode for %s",
5260 plugin_output_filename
);
5262 if (nb_plugin_files
== 0 || !plugin_files
)
5263 fatal ("No plugin files given in plugin mode for %s",
5264 plugin_output_filename
);
5266 /* Parse our plugin files and augment the state. */
5267 for (ix
= 0; ix
< nb_plugin_files
; ix
++)
5269 input_file
* pluginput
= plugin_files
[ix
];
5270 pluginput
->inpisplugin
= true;
5271 parse_file (get_input_file_name (pluginput
));
5276 plugin_output
= create_file ("GCC", plugin_output_filename
);
5277 DBGPRINTF ("created plugin_output %p named %s",
5278 (void *) plugin_output
, plugin_output
->name
);
5281 { /* No plugin files, we are in normal mode. */
5283 fatal ("gengtype needs a source directory in normal mode");
5290 set_gc_used (variables
);
5292 for (type_p t
= structures
; t
; t
= t
->next
)
5294 bool for_user
= false;
5295 for (options_p o
= t
->u
.s
.opt
; o
; o
= o
->next
)
5296 if (strcmp (o
->name
, "for_user") == 0)
5303 set_gc_used_type (t
, GC_POINTED_TO
);
5305 /* The state at this point is read from the state input file or by
5306 parsing source files and optionally augmented by parsing plugin
5307 source files. Write it now. */
5308 if (write_state_filename
)
5310 DBGPRINT_COUNT_TYPE ("structures before write_state", structures
);
5313 fatal ("didn't write state file %s after errors",
5314 write_state_filename
);
5316 DBGPRINTF ("before write_state %s", write_state_filename
);
5317 write_state (write_state_filename
);
5322 /* After having written the state file we return immediately to
5323 avoid generating any output file. */
5333 output_header
= plugin_output
? plugin_output
: header_file
;
5334 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5337 write_types (output_header
, structures
, &ggc_wtd
);
5338 if (plugin_files
== NULL
)
5340 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5342 write_types (header_file
, structures
, &pch_wtd
);
5343 write_local (header_file
, structures
);
5345 write_roots (variables
, plugin_files
== NULL
);
5347 close_output_files ();
5352 /* Don't bother about free-ing any input or plugin file, etc. */