1 /* Process source files and output type information.
2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
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. */
34 #include "filenames.h"
36 /* Data types, macros, etc. used only in this file. */
39 /* The list of output files. */
42 /* The output header file that is included into pretty much every
47 /* The name of the file containing the list of input files. */
48 static char *inputlist
;
50 /* The plugin input files and their number; in that case only
51 a single file is produced. */
52 static input_file
**plugin_files
;
53 static size_t nb_plugin_files
;
55 /* The generated plugin output file and name. */
56 static outf_p plugin_output
;
57 static char *plugin_output_filename
;
59 /* Our source directory and its length. */
63 /* Variables used for reading and writing the state. */
64 const char *read_state_filename
;
65 const char *write_state_filename
;
67 /* Variables to help debugging. */
71 /* Level for verbose messages. */
74 /* We have a type count and use it to set the state_number of newly
75 allocated types to some unique negative number. */
76 static int type_count
;
78 /* The backup directory should be in the same file system as the
79 generated files, otherwise the rename(2) system call would fail.
80 If NULL, no backup is made when overwriting a generated file. */
81 static const char* backup_dir
; /* (-B) program option. */
84 static outf_p
create_file (const char *, const char *);
86 static const char *get_file_basename (const input_file
*);
87 static const char *get_file_realbasename (const input_file
*);
89 static int get_prefix_langdir_index (const char *);
90 static const char *get_file_langdir (const input_file
*);
92 static void dump_pair (int indent
, pair_p p
);
93 static void dump_type (int indent
, type_p p
);
94 static void dump_type_list (int indent
, type_p p
);
97 /* Nonzero iff an error has occurred. */
98 bool hit_error
= false;
100 static void gen_rtx_next (void);
101 static void write_rtx_next (void);
102 static void open_base_files (void);
103 static void close_output_files (void);
105 /* Report an error at POS, printing MSG. */
108 error_at_line (const struct fileloc
*pos
, const char *msg
, ...)
112 gcc_assert (pos
!= NULL
&& pos
->file
!= NULL
);
115 fprintf (stderr
, "%s:%d: ", get_input_file_name (pos
->file
), pos
->line
);
116 vfprintf (stderr
, msg
, ap
);
117 fputc ('\n', stderr
);
123 /* Locate the ultimate base class of struct S. */
126 get_ultimate_base_class (const_type_p s
)
128 while (s
->u
.s
.base_class
)
129 s
= s
->u
.s
.base_class
;
134 get_ultimate_base_class (type_p s
)
136 while (s
->u
.s
.base_class
)
137 s
= s
->u
.s
.base_class
;
141 /* Input file handling. */
143 /* Table of all input files. */
144 const input_file
**gt_files
;
147 /* A number of places use the name of this "gengtype.c" file for a
148 location for things that we can't rely on the source to define.
149 Make sure we can still use pointer comparison on filenames. */
150 input_file
* this_file
;
151 /* The "system.h" file is likewise specially useful. */
152 input_file
* system_h_file
;
154 /* Vector of per-language directories. */
155 const char **lang_dir_names
;
156 size_t num_lang_dirs
;
158 /* An array of output files suitable for definitions. There is one
159 BASE_FILES entry for each language. */
160 static outf_p
*base_files
;
165 /* Utility debugging function, printing the various type counts within
166 a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
168 dbgprint_count_type_at (const char *fil
, int lin
, const char *msg
, type_p t
)
170 int nb_types
= 0, nb_scalar
= 0, nb_string
= 0;
171 int nb_struct
= 0, nb_union
= 0, nb_array
= 0, nb_pointer
= 0;
172 int nb_lang_struct
= 0;
173 int nb_user_struct
= 0, nb_undefined
= 0;
175 for (p
= t
; p
; p
= p
->next
)
191 case TYPE_USER_STRUCT
:
203 case TYPE_LANG_STRUCT
:
210 fprintf (stderr
, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
211 lbasename (fil
), lin
, msg
, nb_types
);
212 if (nb_scalar
> 0 || nb_string
> 0)
213 fprintf (stderr
, "@@%%@@ %d scalars, %d strings\n", nb_scalar
, nb_string
);
214 if (nb_struct
> 0 || nb_union
> 0)
215 fprintf (stderr
, "@@%%@@ %d structs, %d unions\n", nb_struct
, nb_union
);
216 if (nb_pointer
> 0 || nb_array
> 0)
217 fprintf (stderr
, "@@%%@@ %d pointers, %d arrays\n", nb_pointer
, nb_array
);
218 if (nb_lang_struct
> 0)
219 fprintf (stderr
, "@@%%@@ %d lang_structs\n", nb_lang_struct
);
220 if (nb_user_struct
> 0)
221 fprintf (stderr
, "@@%%@@ %d user_structs\n", nb_user_struct
);
222 if (nb_undefined
> 0)
223 fprintf (stderr
, "@@%%@@ %d undefined types\n", nb_undefined
);
224 fprintf (stderr
, "\n");
226 #endif /* ENABLE_CHECKING */
228 /* Scan the input file, LIST, and determine how much space we need to
229 store strings in. Also, count the number of language directories
230 and files. The numbers returned are overestimates as they does not
231 consider repeated files. */
233 measure_input_list (FILE *list
)
239 num_gt_files
= plugin_files
? nb_plugin_files
: 0;
240 while ((c
= getc (list
)) != EOF
)
249 /* Add space for a lang_bitmap before the input file name. */
250 n
+= sizeof (lang_bitmap
);
264 /* Read one input line from LIST to HEREP (which is updated). A
265 pointer to the string is returned via LINEP. If it was a language
266 subdirectory in square brackets, strip off the square brackets and
267 return true. Otherwise, leave space before the string for a
268 lang_bitmap, and return false. At EOF, returns false, does not
269 touch *HEREP, and sets *LINEP to NULL. POS is used for
272 read_input_line (FILE *list
, char **herep
, char **linep
, struct fileloc
*pos
)
278 /* Read over whitespace. */
279 while (c
== '\n' || c
== ' ')
289 /* No space for a lang_bitmap is necessary. Discard the '['. */
292 while (c
!= ']' && c
!= '\n' && c
!= EOF
)
301 c
= getc (list
); /* eat what should be a newline */
302 if (c
!= '\n' && c
!= EOF
)
303 error_at_line (pos
, "junk on line after language tag [%s]", line
);
306 error_at_line (pos
, "missing close bracket for language tag [%s",
315 /* Leave space for a lang_bitmap. */
316 memset (here
, 0, sizeof (lang_bitmap
));
317 here
+= sizeof (lang_bitmap
);
324 while (c
!= EOF
&& c
!= '\n');
332 /* Read the list of input files from LIST and compute all of the
333 relevant tables. There is one file per line of the list. At
334 first, all the files on the list are language-generic, but
335 eventually a line will appear which is the name of a language
336 subdirectory in square brackets, like this: [cp]. All subsequent
337 files are specific to that language, until another language
338 subdirectory tag appears. Files can appear more than once, if
339 they apply to more than one language. */
341 read_input_list (const char *listname
)
343 FILE *list
= fopen (listname
, "r");
345 fatal ("cannot open %s: %s", listname
, xstrerror (errno
));
349 size_t bufsz
= measure_input_list (list
);
350 char *buf
= XNEWVEC (char, bufsz
);
352 char *committed
= buf
;
353 char *limit
= buf
+ bufsz
;
358 lang_bitmap curlangs
= (1 << num_lang_dirs
) - 1;
360 epos
.file
= input_file_by_name (listname
);
363 lang_dir_names
= XNEWVEC (const char *, num_lang_dirs
);
364 gt_files
= XNEWVEC (const input_file
*, num_gt_files
);
371 is_language
= read_input_line (list
, &here
, &line
, &epos
);
372 gcc_assert (here
<= limit
);
375 else if (is_language
)
378 gcc_assert (langno
<= num_lang_dirs
);
379 for (i
= 0; i
< langno
; i
++)
380 if (strcmp (lang_dir_names
[i
], line
) == 0)
382 error_at_line (&epos
, "duplicate language tag [%s]",
389 curlangs
= 1 << langno
;
390 lang_dir_names
[langno
++] = line
;
395 input_file
*inpf
= input_file_by_name (line
);
396 gcc_assert (nfiles
<= num_gt_files
);
397 for (i
= 0; i
< nfiles
; i
++)
398 /* Since the input_file-s are uniquely hash-consed, we
399 can just compare pointers! */
400 if (gt_files
[i
] == inpf
)
402 /* Throw away the string we just read, and add the
403 current language to the existing string's bitmap. */
404 lang_bitmap bmap
= get_lang_bitmap (inpf
);
406 error_at_line (&epos
,
407 "file %s specified more than once "
408 "for language %s", line
,
410 0 ? "(all)" : lang_dir_names
[langno
-
414 set_lang_bitmap (inpf
, bmap
);
419 set_lang_bitmap (inpf
, curlangs
);
420 gt_files
[nfiles
++] = inpf
;
423 /* Update the global counts now that we know accurately how many
424 things there are. (We do not bother resizing the arrays down.) */
425 num_lang_dirs
= langno
;
426 /* Add the plugin files if provided. */
430 for (i
= 0; i
< nb_plugin_files
; i
++)
431 gt_files
[nfiles
++] = plugin_files
[i
];
433 num_gt_files
= nfiles
;
436 /* Sanity check: any file that resides in a language subdirectory
437 (e.g. 'cp') ought to belong to the corresponding language.
438 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
439 (Can you even do that? Should you be allowed to?) */
442 for (f
= 0; f
< num_gt_files
; f
++)
444 lang_bitmap bitmap
= get_lang_bitmap (gt_files
[f
]);
445 const char *basename
= get_file_basename (gt_files
[f
]);
446 const char *slashpos
= strchr (basename
, '/');
447 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
448 const char *slashpos2
= strchr (basename
, '\\');
450 if (!slashpos
|| (slashpos2
&& slashpos2
< slashpos
))
451 slashpos
= slashpos2
;
457 for (l
= 0; l
< num_lang_dirs
; l
++)
458 if ((size_t) (slashpos
- basename
) == strlen (lang_dir_names
[l
])
459 && memcmp (basename
, lang_dir_names
[l
],
460 strlen (lang_dir_names
[l
])) == 0)
462 if (!(bitmap
& (1 << l
)))
463 error ("%s is in language directory '%s' but is not "
464 "tagged for that language",
465 basename
, lang_dir_names
[l
]);
473 fatal ("error reading %s: %s", listname
, xstrerror (errno
));
480 /* The one and only TYPE_STRING. */
482 struct type string_type
= {
483 TYPE_STRING
, 0, 0, 0, GC_USED
, {0}
486 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
487 set early in main. */
489 struct type scalar_nonchar
= {
490 TYPE_SCALAR
, 0, 0, 0, GC_USED
, {0}
493 struct type scalar_char
= {
494 TYPE_SCALAR
, 0, 0, 0, GC_USED
, {0}
497 /* Lists of various things. */
499 pair_p typedefs
= NULL
;
500 type_p structures
= NULL
;
501 pair_p variables
= NULL
;
503 static type_p
adjust_field_tree_exp (type_p t
, options_p opt
);
504 static type_p
adjust_field_rtx_def (type_p t
, options_p opt
);
506 /* Define S as a typedef to T at POS. */
509 do_typedef (const char *s
, type_p t
, struct fileloc
*pos
)
513 /* temporary kludge - gengtype doesn't handle conditionals or
514 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
515 is coming from this file (main() sets them up with safe dummy
517 if (!strcmp (s
, "CUMULATIVE_ARGS") && pos
->file
!= this_file
)
520 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
521 if (strcmp (p
->name
, s
) == 0)
523 if (p
->type
!= t
&& strcmp (s
, "result_type") != 0)
525 error_at_line (pos
, "type `%s' previously defined", s
);
526 error_at_line (&p
->line
, "previously defined here");
531 p
= XNEW (struct pair
);
540 /* Define S as a typename of a scalar. Cannot be used to define
541 typedefs of 'char'. Note: is also used for pointer-to-function
542 typedefs (which are therefore not treated as pointers). */
545 do_scalar_typedef (const char *s
, struct fileloc
*pos
)
547 do_typedef (s
, &scalar_nonchar
, pos
);
550 /* Similar to strtok_r. */
553 strtoken (char *str
, const char *delim
, char **next
)
560 /* Skip the leading delimiters. */
561 str
+= strspn (str
, delim
);
563 /* This is an empty token. */
566 /* The current token. */
569 /* Find the next delimiter. */
570 str
+= strcspn (str
, delim
);
572 /* This is the last token. */
576 /* Terminate the current token. */
578 /* Advance to the next token. */
585 /* Define TYPE_NAME to be a user defined type at location POS. */
588 create_user_defined_type (const char *type_name
, struct fileloc
*pos
)
590 type_p ty
= find_structure (type_name
, TYPE_USER_STRUCT
);
592 /* We might have already seen an incomplete decl of the given type,
593 in which case we won't have yet seen a GTY((user)), and the type will
594 only have kind "TYPE_STRUCT". Mark it as a user struct. */
595 ty
->kind
= TYPE_USER_STRUCT
;
598 ty
->u
.s
.bitmap
= get_lang_bitmap (pos
->file
);
599 do_typedef (type_name
, ty
, pos
);
601 /* If TYPE_NAME specifies a template, create references to the types
602 in the template by pretending that each type is a field of TY.
603 This is needed to make sure that the types referenced by the
604 template are marked as used. */
605 char *str
= xstrdup (type_name
);
606 char *open_bracket
= strchr (str
, '<');
609 /* We only accept simple template declarations (see
610 require_template_declaration), so we only need to parse a
611 comma-separated list of strings, implicitly assumed to
612 be type names, potentially with "*" characters. */
613 char *arg
= open_bracket
+ 1;
614 /* Workaround -Wmaybe-uninitialized false positive during
615 profiledbootstrap by initializing it. */
617 char *type_id
= strtoken (arg
, ",>", &next
);
621 /* Create a new field for every type found inside the template
624 /* Support a single trailing "*" character. */
625 const char *star
= strchr (type_id
, '*');
626 int is_ptr
= (star
!= NULL
);
627 size_t offset_to_star
= star
- type_id
;
629 offset_to_star
= star
- type_id
;
631 if (strstr (type_id
, "char*"))
633 type_id
= strtoken (0, ",>", &next
);
637 char *field_name
= xstrdup (type_id
);
642 /* Strip off the first '*' character (and any subsequent text). */
643 *(field_name
+ offset_to_star
) = '\0';
645 arg_type
= find_structure (field_name
, TYPE_STRUCT
);
646 arg_type
= create_pointer (arg_type
);
649 arg_type
= resolve_typedef (field_name
, pos
);
651 fields
= create_field_at (fields
, arg_type
, field_name
, 0, pos
);
652 type_id
= strtoken (0, ",>", &next
);
655 /* Associate the field list to TY. */
656 ty
->u
.s
.fields
= fields
;
664 /* Given a typedef name S, return its associated type. Return NULL if
665 S is not a registered type name. */
668 type_for_name (const char *s
)
672 /* Special-case support for types within a "gcc::" namespace. Rather
673 than fully-supporting namespaces, simply strip off the "gcc::" prefix
674 where present. This allows us to have GTY roots of this form:
675 extern GTY(()) gcc::some_type *some_ptr;
676 where the autogenerated functions will refer to simply "some_type",
677 where they can be resolved into their namespace. */
678 if (0 == strncmp (s
, "gcc::", 5))
681 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
682 if (strcmp (p
->name
, s
) == 0)
688 /* Create an undefined type with name S and location POS. Return the
689 newly created type. */
692 create_undefined_type (const char *s
, struct fileloc
*pos
)
694 type_p ty
= find_structure (s
, TYPE_UNDEFINED
);
696 ty
->u
.s
.bitmap
= get_lang_bitmap (pos
->file
);
697 do_typedef (s
, ty
, pos
);
702 /* Return the type previously defined for S. Use POS to report errors. */
705 resolve_typedef (const char *s
, struct fileloc
*pos
)
707 bool is_template_instance
= (strchr (s
, '<') != NULL
);
708 type_p p
= type_for_name (s
);
710 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
711 type for regular type identifiers. If the type identifier S is a
712 template instantiation, however, we treat it as a user defined
715 FIXME, this is actually a limitation in gengtype. Supporting
716 template types and their instances would require keeping separate
717 track of the basic types definition and its instances. This
718 essentially forces all template classes in GC to be marked
721 p
= (is_template_instance
)
722 ? create_user_defined_type (s
, pos
)
723 : create_undefined_type (s
, pos
);
728 /* Add SUBCLASS to head of linked list of BASE's subclasses. */
730 void add_subclass (type_p base
, type_p subclass
)
732 gcc_assert (union_or_struct_p (base
));
733 gcc_assert (union_or_struct_p (subclass
));
735 subclass
->u
.s
.next_sibling_class
= base
->u
.s
.first_subclass
;
736 base
->u
.s
.first_subclass
= subclass
;
739 /* Create and return a new structure with tag NAME at POS with fields
740 FIELDS and options O. The KIND of structure must be one of
741 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
744 new_structure (const char *name
, enum typekind kind
, struct fileloc
*pos
,
745 pair_p fields
, options_p o
, type_p base_class
)
749 lang_bitmap bitmap
= get_lang_bitmap (pos
->file
);
750 bool isunion
= (kind
== TYPE_UNION
);
752 gcc_assert (union_or_struct_p (kind
));
754 for (si
= structures
; si
!= NULL
; si
= si
->next
)
755 if (strcmp (name
, si
->u
.s
.tag
) == 0 && UNION_P (si
) == isunion
)
758 if (si
->kind
== TYPE_LANG_STRUCT
)
762 for (si
= ls
->u
.s
.lang_struct
; si
!= NULL
; si
= si
->next
)
763 if (si
->u
.s
.bitmap
== bitmap
)
766 else if (si
->u
.s
.line
.file
!= NULL
&& si
->u
.s
.bitmap
!= bitmap
)
770 si
= XCNEW (struct type
);
771 memcpy (si
, ls
, sizeof (struct type
));
772 ls
->kind
= TYPE_LANG_STRUCT
;
773 ls
->u
.s
.lang_struct
= si
;
774 ls
->u
.s
.fields
= NULL
;
776 si
->state_number
= -type_count
;
777 si
->pointer_to
= NULL
;
778 si
->u
.s
.lang_struct
= ls
;
783 if (ls
!= NULL
&& s
== NULL
)
786 s
= XCNEW (struct type
);
787 s
->state_number
= -type_count
;
788 s
->next
= ls
->u
.s
.lang_struct
;
789 ls
->u
.s
.lang_struct
= s
;
790 s
->u
.s
.lang_struct
= ls
;
798 s
= XCNEW (struct type
);
799 s
->state_number
= -type_count
;
800 s
->next
= structures
;
804 if (s
->u
.s
.lang_struct
&& (s
->u
.s
.lang_struct
->u
.s
.bitmap
& bitmap
))
806 error_at_line (pos
, "duplicate definition of '%s %s'",
807 isunion
? "union" : "struct", s
->u
.s
.tag
);
808 error_at_line (&s
->u
.s
.line
, "previous definition here");
814 s
->u
.s
.fields
= fields
;
816 s
->u
.s
.bitmap
= bitmap
;
817 if (s
->u
.s
.lang_struct
)
818 s
->u
.s
.lang_struct
->u
.s
.bitmap
|= bitmap
;
819 s
->u
.s
.base_class
= base_class
;
821 add_subclass (base_class
, s
);
826 /* Return the previously-defined structure or union with tag NAME,
827 or a new empty structure or union if none was defined previously.
828 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
832 find_structure (const char *name
, enum typekind kind
)
835 bool isunion
= (kind
== TYPE_UNION
);
837 gcc_assert (kind
== TYPE_UNDEFINED
|| union_or_struct_p (kind
));
839 for (s
= structures
; s
!= NULL
; s
= s
->next
)
840 if (strcmp (name
, s
->u
.s
.tag
) == 0 && UNION_P (s
) == isunion
)
844 s
= XCNEW (struct type
);
845 s
->next
= structures
;
846 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 (strncmp (rtx_format
[i
], "uu", 2) == 0)
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 case NOTE_INSN_CALL_ARG_LOCATION
:
1196 note_flds
= create_field (note_flds
, rtx_tp
, "rt_rtx");
1200 note_flds
= create_field (note_flds
, scalar_tp
, "rt_int");
1203 /* NOTE_INSN_MAX is used as the default field for line
1205 if (c
== NOTE_INSN_MAX
)
1207 create_string_option (nodot
, "default", "");
1210 create_string_option (nodot
, "tag", note_insn_name
[c
]);
1212 note_union_tp
= new_structure ("rtx_def_note_subunion", TYPE_UNION
,
1213 &lexer_line
, note_flds
, NULL
, NULL
);
1215 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1218 sym_flds
= create_field (NULL
, tree_tp
, "rt_tree");
1219 sym_flds
->opt
= create_string_option (nodot
, "default", "");
1220 sym_flds
= create_field (sym_flds
, constant_tp
, "rt_constant");
1221 sym_flds
->opt
= create_string_option (nodot
, "tag", "1");
1222 symbol_union_tp
= new_structure ("rtx_def_symbol_subunion", TYPE_UNION
,
1223 &lexer_line
, sym_flds
, NULL
, NULL
);
1225 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1227 pair_p subfields
= NULL
;
1228 size_t aindex
, nmindex
;
1233 for (aindex
= 0; aindex
< strlen (rtx_format
[i
]); aindex
++)
1236 const char *subname
;
1238 switch (rtx_format
[i
][aindex
])
1250 if (i
== MEM
&& aindex
== 1)
1251 t
= mem_attrs_tp
, subname
= "rt_mem";
1252 else if (i
== JUMP_INSN
&& aindex
== 7)
1253 t
= rtx_tp
, subname
= "rt_rtx";
1254 else if (i
== CODE_LABEL
&& aindex
== 4)
1255 t
= scalar_tp
, subname
= "rt_int";
1256 else if (i
== CODE_LABEL
&& aindex
== 3)
1257 t
= rtx_tp
, subname
= "rt_rtx";
1258 else if (i
== LABEL_REF
&& (aindex
== 1 || aindex
== 2))
1259 t
= rtx_tp
, subname
= "rt_rtx";
1260 else if (i
== NOTE
&& aindex
== 3)
1261 t
= note_union_tp
, subname
= "";
1262 else if (i
== NOTE
&& aindex
== 4)
1263 t
= scalar_tp
, subname
= "rt_int";
1264 else if (i
== NOTE
&& aindex
>= 6)
1265 t
= scalar_tp
, subname
= "rt_int";
1266 else if (i
== ADDR_DIFF_VEC
&& aindex
== 4)
1267 t
= scalar_tp
, subname
= "rt_int";
1268 else if (i
== VALUE
&& aindex
== 0)
1269 t
= scalar_tp
, subname
= "rt_int";
1270 else if (i
== DEBUG_EXPR
&& aindex
== 0)
1271 t
= tree_tp
, subname
= "rt_tree";
1272 else if (i
== SYMBOL_REF
&& aindex
== 1)
1273 t
= symbol_union_tp
, subname
= "";
1274 else if (i
== JUMP_TABLE_DATA
&& aindex
>= 4)
1275 t
= scalar_tp
, subname
= "rt_int";
1276 else if (i
== BARRIER
&& aindex
>= 2)
1277 t
= scalar_tp
, subname
= "rt_int";
1278 else if (i
== ENTRY_VALUE
&& aindex
== 0)
1279 t
= rtx_tp
, subname
= "rt_rtx";
1284 "rtx type `%s' has `0' in position %lu, can't handle",
1285 rtx_name
[i
], (unsigned long) aindex
);
1307 subname
= "rt_rtvec";
1312 subname
= "rt_tree";
1323 "rtx type `%s' has `%c' in position %lu, can't handle",
1324 rtx_name
[i
], rtx_format
[i
][aindex
],
1325 (unsigned long) aindex
);
1331 subfields
= create_field (subfields
, t
,
1332 xasprintf (".fld[%lu].%s",
1333 (unsigned long) aindex
,
1335 subfields
->opt
= nodot
;
1336 if (t
== note_union_tp
)
1338 create_string_option (subfields
->opt
, "desc",
1340 if (t
== symbol_union_tp
)
1342 create_string_option (subfields
->opt
, "desc",
1343 "CONSTANT_POOL_ADDRESS_P (&%0)");
1347 subfields
= create_field (subfields
, reg_attrs_tp
, "reg.attrs");
1349 if (i
== SYMBOL_REF
)
1351 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1353 type_p field_tp
= find_structure ("block_symbol", TYPE_STRUCT
);
1355 = create_optional_field (subfields
, field_tp
, "block_sym",
1356 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1359 sname
= xasprintf ("rtx_def_%s", rtx_name
[i
]);
1360 substruct
= new_structure (sname
, TYPE_STRUCT
, &lexer_line
, subfields
,
1363 ftag
= xstrdup (rtx_name
[i
]);
1364 for (nmindex
= 0; nmindex
< strlen (ftag
); nmindex
++)
1365 ftag
[nmindex
] = TOUPPER (ftag
[nmindex
]);
1366 flds
= create_field (flds
, substruct
, "");
1367 flds
->opt
= create_string_option (nodot
, "tag", ftag
);
1369 return new_structure ("rtx_def_subunion", TYPE_UNION
, &lexer_line
, flds
,
1373 /* Handle `special("tree_exp")'. This is a special case for
1374 field `operands' of struct tree_exp, which although it claims to contain
1375 pointers to trees, actually sometimes contains pointers to RTL too.
1376 Passed T, the old type of the field, and OPT its options. Returns
1377 a new type for the field. */
1380 adjust_field_tree_exp (type_p t
, options_p opt ATTRIBUTE_UNUSED
)
1385 if (t
->kind
!= TYPE_ARRAY
)
1387 error_at_line (&lexer_line
,
1388 "special `tree_exp' must be applied to an array");
1389 return &string_type
;
1392 nodot
= create_string_option (NULL
, "dot", "");
1394 flds
= create_field (NULL
, t
, "");
1395 flds
->opt
= create_string_option (nodot
, "length",
1396 "TREE_OPERAND_LENGTH ((tree) &%0)");
1397 flds
->opt
= create_string_option (flds
->opt
, "default", "");
1399 return new_structure ("tree_exp_subunion", TYPE_UNION
, &lexer_line
, flds
,
1403 /* Perform any special processing on a type T, about to become the type
1404 of a field. Return the appropriate type for the field.
1406 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1407 - Similarly for arrays of pointer-to-char;
1408 - Converts structures for which a parameter is provided to
1410 - Handles "special" options.
1414 adjust_field_type (type_p t
, options_p opt
)
1417 const int pointer_p
= t
->kind
== TYPE_POINTER
;
1419 for (; opt
; opt
= opt
->next
)
1420 if (strcmp (opt
->name
, "length") == 0)
1423 error_at_line (&lexer_line
, "duplicate `%s' option", opt
->name
);
1424 if (t
->u
.p
->kind
== TYPE_SCALAR
|| t
->u
.p
->kind
== TYPE_STRING
)
1426 error_at_line (&lexer_line
,
1427 "option `%s' may not be applied to "
1428 "arrays of atomic types", opt
->name
);
1432 else if (strcmp (opt
->name
, "special") == 0
1433 && opt
->kind
== OPTION_STRING
)
1435 const char *special_name
= opt
->info
.string
;
1436 if (strcmp (special_name
, "tree_exp") == 0)
1437 t
= adjust_field_tree_exp (t
, opt
);
1438 else if (strcmp (special_name
, "rtx_def") == 0)
1439 t
= adjust_field_rtx_def (t
, opt
);
1441 error_at_line (&lexer_line
, "unknown special `%s'", special_name
);
1445 && pointer_p
&& t
->u
.p
->kind
== TYPE_SCALAR
&& t
->u
.p
->u
.scalar_is_char
)
1446 return &string_type
;
1447 if (t
->kind
== TYPE_ARRAY
&& t
->u
.a
.p
->kind
== TYPE_POINTER
1448 && t
->u
.a
.p
->u
.p
->kind
== TYPE_SCALAR
1449 && t
->u
.a
.p
->u
.p
->u
.scalar_is_char
)
1450 return create_array (&string_type
, t
->u
.a
.len
);
1456 static void set_gc_used_type (type_p
, enum gc_used_enum
, bool = false);
1457 static void set_gc_used (pair_p
);
1459 /* Handle OPT for set_gc_used_type. */
1462 process_gc_options (options_p opt
, enum gc_used_enum level
, int *maybe_undef
,
1463 int *length
, int *skip
, type_p
*nested_ptr
)
1466 for (o
= opt
; o
; o
= o
->next
)
1467 if (strcmp (o
->name
, "ptr_alias") == 0 && level
== GC_POINTED_TO
1468 && o
->kind
== OPTION_TYPE
)
1469 set_gc_used_type (o
->info
.type
,
1471 else if (strcmp (o
->name
, "maybe_undef") == 0)
1473 else if (strcmp (o
->name
, "length") == 0)
1475 else if (strcmp (o
->name
, "skip") == 0)
1477 else if (strcmp (o
->name
, "nested_ptr") == 0
1478 && o
->kind
== OPTION_NESTED
)
1479 *nested_ptr
= ((const struct nested_ptr_data
*) o
->info
.nested
)->type
;
1483 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1485 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1486 are set to GC_UNUSED. Otherwise, an error is emitted for
1487 TYPE_UNDEFINED types. This is used to support user-defined
1488 template types with non-type arguments.
1490 For instance, when we parse a template type with enum arguments
1491 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1492 artificial fields for 'MyType', one for 'AnotherType', the other
1493 one for 'EnumValue'.
1495 At the time that we parse this type we don't know that 'EnumValue'
1496 is really an enum value, so the parser creates a TYPE_UNDEFINED
1497 type for it. Since 'EnumValue' is never resolved to a known
1498 structure, it will stay with TYPE_UNDEFINED.
1500 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1501 'EnumValue'. Generating marking code for it would cause
1502 compilation failures since the marking routines assumes that
1503 'EnumValue' is a type. */
1506 set_gc_used_type (type_p t
, enum gc_used_enum level
,
1507 bool allow_undefined_types
)
1509 if (t
->gc_used
>= level
)
1518 case TYPE_USER_STRUCT
:
1523 bool allow_undefined_field_types
= (t
->kind
== TYPE_USER_STRUCT
);
1525 process_gc_options (t
->u
.s
.opt
, level
, &dummy
, &dummy
, &dummy
,
1528 if (t
->u
.s
.base_class
)
1529 set_gc_used_type (t
->u
.s
.base_class
, level
, allow_undefined_types
);
1530 /* Anything pointing to a base class might actually be pointing
1532 for (type_p subclass
= t
->u
.s
.first_subclass
; subclass
;
1533 subclass
= subclass
->u
.s
.next_sibling_class
)
1534 set_gc_used_type (subclass
, level
, allow_undefined_types
);
1536 FOR_ALL_INHERITED_FIELDS(t
, f
)
1538 int maybe_undef
= 0;
1541 type_p nested_ptr
= NULL
;
1542 process_gc_options (f
->opt
, level
, &maybe_undef
, &length
, &skip
,
1545 if (nested_ptr
&& f
->type
->kind
== TYPE_POINTER
)
1546 set_gc_used_type (nested_ptr
, GC_POINTED_TO
);
1547 else if (length
&& f
->type
->kind
== TYPE_POINTER
)
1548 set_gc_used_type (f
->type
->u
.p
, GC_USED
);
1549 else if (maybe_undef
&& f
->type
->kind
== TYPE_POINTER
)
1550 set_gc_used_type (f
->type
->u
.p
, GC_MAYBE_POINTED_TO
);
1552 ; /* target type is not used through this field */
1554 set_gc_used_type (f
->type
, GC_USED
, allow_undefined_field_types
);
1559 case TYPE_UNDEFINED
:
1560 if (level
> GC_UNUSED
)
1562 if (!allow_undefined_types
)
1563 error_at_line (&t
->u
.s
.line
, "undefined type `%s'", t
->u
.s
.tag
);
1564 t
->gc_used
= GC_UNUSED
;
1569 set_gc_used_type (t
->u
.p
, GC_POINTED_TO
);
1573 set_gc_used_type (t
->u
.a
.p
, GC_USED
);
1576 case TYPE_LANG_STRUCT
:
1577 for (t
= t
->u
.s
.lang_struct
; t
; t
= t
->next
)
1578 set_gc_used_type (t
, level
);
1586 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1589 set_gc_used (pair_p variables
)
1593 for (p
= variables
; p
; p
= p
->next
)
1595 set_gc_used_type (p
->type
, GC_USED
);
1598 if (verbosity_level
>= 2)
1599 printf ("%s used %d GTY-ed variables\n", progname
, nbvars
);
1602 /* File mapping routines. For each input file, there is one output .c file
1603 (but some output files have many input files), and there is one .h file
1604 for the whole build. */
1606 /* Output file handling. */
1608 /* Create and return an outf_p for a new file for NAME, to be called
1612 create_file (const char *name
, const char *oname
)
1614 static const char *const hdr
[] = {
1615 " Copyright (C) 2004-2015 Free Software Foundation, Inc.\n",
1617 "This file is part of GCC.\n",
1619 "GCC is free software; you can redistribute it and/or modify it under\n",
1620 "the terms of the GNU General Public License as published by the Free\n",
1621 "Software Foundation; either version 3, or (at your option) any later\n",
1624 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1625 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1626 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1627 "for more details.\n",
1629 "You should have received a copy of the GNU General Public License\n",
1630 "along with GCC; see the file COPYING3. If not see\n",
1631 "<http://www.gnu.org/licenses/>. */\n",
1633 "/* This file is machine generated. Do not edit. */\n"
1638 gcc_assert (name
!= NULL
);
1639 gcc_assert (oname
!= NULL
);
1640 f
= XCNEW (struct outf
);
1641 f
->next
= output_files
;
1645 oprintf (f
, "/* Type information for %s.\n", name
);
1646 for (i
= 0; i
< ARRAY_SIZE (hdr
); i
++)
1647 oprintf (f
, "%s", hdr
[i
]);
1651 /* Print, like fprintf, to O.
1652 N.B. You might think this could be implemented more efficiently
1653 with vsnprintf(). Unfortunately, there are C libraries that
1654 provide that function but without the C99 semantics for its return
1655 value, making it impossible to know how much space is required. */
1657 oprintf (outf_p o
, const char *format
, ...)
1663 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1668 va_start (ap
, format
);
1669 slength
= vasprintf (&s
, format
, ap
);
1670 if (s
== NULL
|| (int) slength
< 0)
1671 fatal ("out of memory");
1674 if (o
->bufused
+ slength
> o
->buflength
)
1676 size_t new_len
= o
->buflength
;
1683 while (o
->bufused
+ slength
>= new_len
);
1684 o
->buf
= XRESIZEVEC (char, o
->buf
, new_len
);
1685 o
->buflength
= new_len
;
1687 memcpy (o
->buf
+ o
->bufused
, s
, slength
);
1688 o
->bufused
+= slength
;
1692 /* Open the global header file and the language-specific header files. */
1695 open_base_files (void)
1699 if (nb_plugin_files
> 0 && plugin_files
)
1702 header_file
= create_file ("GCC", "gtype-desc.h");
1704 base_files
= XNEWVEC (outf_p
, num_lang_dirs
);
1706 for (i
= 0; i
< num_lang_dirs
; i
++)
1707 base_files
[i
] = create_file (lang_dir_names
[i
],
1708 xasprintf ("gtype-%s.h", lang_dir_names
[i
]));
1710 /* gtype-desc.c is a little special, so we create it here. */
1712 /* The order of files here matters very much. */
1713 static const char *const ifiles
[] = {
1714 "config.h", "system.h", "coretypes.h", "tm.h", "insn-codes.h",
1715 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1716 "hash-set.h", "machmode.h", "vec.h", "double-int.h", "input.h",
1717 "alias.h", "symtab.h", "options.h",
1718 "wide-int.h", "inchash.h",
1719 "tree.h", "fold-const.h", "rtl.h",
1720 "machmode.h", "tm.h", "hard-reg-set.h", "input.h", "predict.h",
1721 "function.h", "insn-config.h", "flags.h", "statistics.h",
1722 "real.h", "fixed-value.h", "tree.h", "expmed.h", "dojump.h",
1723 "explow.h", "calls.h", "emit-rtl.h", "varasm.h", "stmt.h",
1724 "expr.h", "alloc-pool.h",
1725 "basic-block.h", "cselib.h", "insn-addr.h",
1726 "optabs.h", "libfuncs.h", "debug.h", "ggc.h",
1727 "ggc.h", "dominance.h", "cfg.h", "basic-block.h",
1728 "tree-ssa-alias.h", "internal-fn.h", "gimple-fold.h", "tree-eh.h",
1729 "gimple-expr.h", "is-a.h",
1730 "gimple.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
1731 "tree-phinodes.h", "ssa-iterators.h", "stringpool.h", "tree-ssanames.h",
1732 "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h", "tree-ssa-loop-manip.h",
1733 "tree-ssa-loop-niter.h", "tree-into-ssa.h", "tree-dfa.h",
1734 "tree-ssa.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1735 "except.h", "output.h", "cfgloop.h", "target.h", "lto-streamer.h",
1736 "target-globals.h", "ipa-ref.h", "cgraph.h", "symbol-summary.h",
1737 "ipa-prop.h", "ipa-inline.h", "dwarf2out.h", "omp-low.h", NULL
1739 const char *const *ifp
;
1740 outf_p gtype_desc_c
;
1742 gtype_desc_c
= create_file ("GCC", "gtype-desc.c");
1743 for (ifp
= ifiles
; *ifp
; ifp
++)
1744 oprintf (gtype_desc_c
, "#include \"%s\"\n", *ifp
);
1746 /* Make sure we handle "cfun" specially. */
1747 oprintf (gtype_desc_c
, "\n/* See definition in function.h. */\n");
1748 oprintf (gtype_desc_c
, "#undef cfun\n");
1750 oprintf (gtype_desc_c
,
1752 "/* Types with a \"gcc::\" namespace have it stripped\n"
1753 " during gengtype parsing. Provide a \"using\" directive\n"
1754 " to ensure that the fully-qualified types are found. */\n"
1755 "using namespace gcc;\n");
1759 /* For INPF an input file, return the real basename of INPF, with all
1760 the directory components skipped. */
1763 get_file_realbasename (const input_file
*inpf
)
1765 return lbasename (get_input_file_name (inpf
));
1768 /* For INPF a filename, return the relative path to INPF from
1769 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1772 get_file_srcdir_relative_path (const input_file
*inpf
)
1774 const char *f
= get_input_file_name (inpf
);
1775 if (strlen (f
) > srcdir_len
1776 && IS_DIR_SEPARATOR (f
[srcdir_len
])
1777 && strncmp (f
, srcdir
, srcdir_len
) == 0)
1778 return f
+ srcdir_len
+ 1;
1783 /* For INPF an input_file, return the relative path to INPF from
1784 $(srcdir) if the latter is a prefix in INPF, or the real basename
1785 of INPF otherwise. */
1788 get_file_basename (const input_file
*inpf
)
1790 const char *srcdir_path
= get_file_srcdir_relative_path (inpf
);
1792 return (srcdir_path
!= NULL
) ? srcdir_path
: get_file_realbasename (inpf
);
1795 /* For F a filename, return the lang_dir_names relative index of the language
1796 directory that is a prefix in F, if any, -1 otherwise. */
1799 get_prefix_langdir_index (const char *f
)
1801 size_t f_len
= strlen (f
);
1804 for (lang_index
= 0; lang_index
< num_lang_dirs
; lang_index
++)
1806 const char *langdir
= lang_dir_names
[lang_index
];
1807 size_t langdir_len
= strlen (langdir
);
1809 if (f_len
> langdir_len
1810 && IS_DIR_SEPARATOR (f
[langdir_len
])
1811 && memcmp (f
, langdir
, langdir_len
) == 0)
1818 /* For INPF an input file, return the name of language directory where
1819 F is located, if any, NULL otherwise. */
1822 get_file_langdir (const input_file
*inpf
)
1824 /* Get the relative path to INPF from $(srcdir) and find the
1825 language by comparing the prefix with language directory names.
1826 If INPF is not even srcdir relative, no point in looking
1830 const char *srcdir_relative_path
= get_file_srcdir_relative_path (inpf
);
1833 if (!srcdir_relative_path
)
1836 lang_index
= get_prefix_langdir_index (srcdir_relative_path
);
1837 if (lang_index
< 0 && strncmp (srcdir_relative_path
, "c-family", 8) == 0)
1839 else if (lang_index
>= 0)
1840 r
= lang_dir_names
[lang_index
];
1847 /* The gt- output file name for INPF. */
1850 get_file_gtfilename (const input_file
*inpf
)
1852 /* Cook up an initial version of the gt- file name from the file real
1853 basename and the language name, if any. */
1855 const char *basename
= get_file_realbasename (inpf
);
1856 const char *langdir
= get_file_langdir (inpf
);
1859 (langdir
? xasprintf ("gt-%s-%s", langdir
, basename
)
1860 : xasprintf ("gt-%s", basename
));
1862 /* Then replace all non alphanumerics characters by '-' and change the
1863 extension to ".h". We expect the input filename extension was at least
1864 one character long. */
1868 for (; *s
!= '.'; s
++)
1869 if (!ISALNUM (*s
) && *s
!= '-')
1872 memcpy (s
, ".h", sizeof (".h"));
1877 /* Each input_file has its associated output file outf_p. The
1878 association is computed by the function
1879 get_output_file_with_visibility. The associated file is cached
1880 inside input_file in its inpoutf field, so is really computed only
1881 once. Associated output file paths (i.e. output_name-s) are
1882 computed by a rule based regexp machinery, using the files_rules
1883 array of struct file_rule_st. A for_name is also computed, giving
1884 the source file name for which the output_file is generated; it is
1885 often the last component of the input_file path. */
1889 Regexpr machinery to compute the output_name and for_name-s of each
1890 input_file. We have a sequence of file rules which gives the POSIX
1891 extended regular expression to match an input file path, and two
1892 transformed strings for the corresponding output_name and the
1893 corresponding for_name. The transformed string contain dollars: $0
1894 is replaced by the entire match, $1 is replaced by the substring
1895 matching the first parenthesis in the regexp, etc. And $$ is replaced
1896 by a single verbatim dollar. The rule order is important. The
1897 general case is last, and the particular cases should come before.
1898 An action routine can, when needed, update the out_name & for_name
1899 and/or return the appropriate output file. It is invoked only when a
1900 rule is triggered. When a rule is triggered, the output_name and
1901 for_name are computed using their transform string in while $$, $0,
1902 $1, ... are suitably replaced. If there is an action, it is called.
1903 In some few cases, the action can directly return the outf_p, but
1904 usually it just updates the output_name and for_name so should free
1905 them before replacing them. The get_output_file_with_visibility
1906 function creates an outf_p only once per each output_name, so it
1907 scans the output_files list for previously seen output file names.
1910 /* Signature of actions in file rules. */
1911 typedef outf_p (frul_actionrout_t
) (input_file
*, char**, char**);
1914 struct file_rule_st
{
1915 const char* frul_srcexpr
; /* Source string for regexp. */
1916 int frul_rflags
; /* Flags passed to regcomp, usually
1918 regex_t
* frul_re
; /* Compiled regular expression
1919 obtained by regcomp. */
1920 const char* frul_tr_out
; /* Transformation string for making
1921 * the output_name, with $1 ... $9 for
1922 * subpatterns and $0 for the whole
1923 * matched filename. */
1924 const char* frul_tr_for
; /* Tranformation string for making the
1926 frul_actionrout_t
* frul_action
; /* The action, if non null, is
1927 * called once the rule matches, on
1928 * the transformed out_name &
1929 * for_name. It could change them
1930 * and/or give the output file. */
1933 /* File rule action handling *.h files. */
1934 static outf_p
header_dot_h_frul (input_file
*, char**, char**);
1936 /* File rule action handling *.c files. */
1937 static outf_p
source_dot_c_frul (input_file
*, char**, char**);
1939 #define NULL_REGEX (regex_t*)0
1941 /* The prefix in our regexp-s matching the directory. */
1942 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1944 #define NULL_FRULACT (frul_actionrout_t*)0
1946 /* The array of our rules governing file name generation. Rules order
1947 matters, so change with extreme care! */
1949 struct file_rule_st files_rules
[] = {
1950 /* The general rule assumes that files in subdirectories belong to a
1951 particular front-end, and files not in subdirectories are shared.
1952 The following rules deal with exceptions - files that are in
1953 subdirectories and yet are shared, and files that are top-level,
1954 but are not shared. */
1956 /* the c-family/ source directory is special. */
1957 { DIR_PREFIX_REGEX
"c-family/([[:alnum:]_-]*)\\.c$",
1958 REG_EXTENDED
, NULL_REGEX
,
1959 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT
},
1961 { DIR_PREFIX_REGEX
"c-family/([[:alnum:]_-]*)\\.h$",
1962 REG_EXTENDED
, NULL_REGEX
,
1963 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT
},
1965 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
1966 { DIR_PREFIX_REGEX
"c/c-lang\\.h$",
1967 REG_EXTENDED
, NULL_REGEX
, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT
},
1969 { DIR_PREFIX_REGEX
"c/c-tree\\.h$",
1970 REG_EXTENDED
, NULL_REGEX
, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT
},
1972 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
1973 { DIR_PREFIX_REGEX
"cp/cp-tree\\.h$",
1974 REG_EXTENDED
, NULL_REGEX
,
1975 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT
},
1977 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
1978 { DIR_PREFIX_REGEX
"cp/decl\\.[ch]$",
1979 REG_EXTENDED
, NULL_REGEX
,
1980 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT
},
1982 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
1983 { DIR_PREFIX_REGEX
"cp/name-lookup\\.h$",
1984 REG_EXTENDED
, NULL_REGEX
,
1985 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT
},
1987 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
1988 { DIR_PREFIX_REGEX
"cp/parser\\.h$",
1989 REG_EXTENDED
, NULL_REGEX
,
1990 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT
},
1992 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
1993 { DIR_PREFIX_REGEX
"objc/objc-act\\.h$",
1994 REG_EXTENDED
, NULL_REGEX
,
1995 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT
},
1997 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
1998 { DIR_PREFIX_REGEX
"objc/objc-map\\.h$",
1999 REG_EXTENDED
, NULL_REGEX
,
2000 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT
},
2002 /* General cases. For header *.h and source *.c or *.cc files, we
2003 * need special actions to handle the language. */
2005 /* Source *.c files are using get_file_gtfilename to compute their
2006 output_name and get_file_basename to compute their for_name
2007 through the source_dot_c_frul action. */
2008 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.c$",
2009 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.c", source_dot_c_frul
},
2011 /* Source *.cc files are using get_file_gtfilename to compute their
2012 output_name and get_file_basename to compute their for_name
2013 through the source_dot_c_frul action. */
2014 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.cc$",
2015 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.cc", source_dot_c_frul
},
2017 /* Common header files get "gtype-desc.c" as their output_name,
2018 * while language specific header files are handled specially. So
2019 * we need the header_dot_h_frul action. */
2020 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.h$",
2021 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.h", header_dot_h_frul
},
2023 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.in$",
2024 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.in", NULL_FRULACT
},
2026 /* Mandatory null last entry signaling end of rules. */
2027 {NULL
, 0, NULL_REGEX
, NULL
, NULL
, NULL_FRULACT
}
2030 /* Special file rules action for handling *.h header files. It gives
2031 "gtype-desc.c" for common headers and corresponding output
2032 files for language-specific header files. */
2034 header_dot_h_frul (input_file
* inpf
, char**poutname
,
2035 char**pforname ATTRIBUTE_UNUSED
)
2037 const char *basename
= 0;
2039 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2040 (void*) inpf
, get_input_file_name (inpf
),
2041 *poutname
, *pforname
);
2042 basename
= get_file_basename (inpf
);
2043 lang_index
= get_prefix_langdir_index (basename
);
2044 DBGPRINTF ("basename %s lang_index %d", basename
, lang_index
);
2046 if (lang_index
>= 0)
2048 /* The header is language specific. Given output_name &
2049 for_name remains unchanged. The base_files array gives the
2051 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2052 (void*) base_files
[lang_index
],
2053 (base_files
[lang_index
])->name
);
2054 return base_files
[lang_index
];
2058 /* The header is common to all front-end languages. So
2059 output_name is "gtype-desc.c" file. The calling function
2060 get_output_file_with_visibility will find its outf_p. */
2062 *poutname
= xstrdup ("gtype-desc.c");
2063 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2064 get_input_file_name (inpf
));
2070 /* Special file rules action for handling *.c source files using
2071 * get_file_gtfilename to compute their output_name and
2072 * get_file_basename to compute their for_name. The output_name is
2073 * gt-<LANG>-<BASE>.h for language specific source files, and
2074 * gt-<BASE>.h for common source files. */
2076 source_dot_c_frul (input_file
* inpf
, char**poutname
, char**pforname
)
2078 char *newbasename
= CONST_CAST (char*, get_file_basename (inpf
));
2079 char *newoutname
= CONST_CAST (char*, get_file_gtfilename (inpf
));
2080 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2081 (void*) inpf
, get_input_file_name (inpf
),
2082 *poutname
, *pforname
);
2083 DBGPRINTF ("newoutname %s", newoutname
);
2084 DBGPRINTF ("newbasename %s", newbasename
);
2087 *poutname
= newoutname
;
2088 *pforname
= newbasename
;
2092 /* Utility function for get_output_file_with_visibility which returns
2093 * a malloc-ed substituted string using TRS on matching of the FILNAM
2094 * file name, using the PMATCH array. */
2096 matching_file_name_substitute (const char *filnam
, regmatch_t pmatch
[10],
2099 struct obstack str_obstack
;
2101 char *rawstr
= NULL
;
2102 const char *pt
= NULL
;
2103 DBGPRINTF ("filnam %s", filnam
);
2104 obstack_init (&str_obstack
);
2105 for (pt
= trs
; *pt
; pt
++) {
2111 /* A double dollar $$ is substituted by a single verbatim
2112 dollar, but who really uses dollar signs in file
2114 obstack_1grow (&str_obstack
, '$');
2116 else if (ISDIGIT (pt
[1]))
2118 /* Handle $0 $1 ... $9 by appropriate substitution. */
2119 int dolnum
= pt
[1] - '0';
2120 int so
= pmatch
[dolnum
].rm_so
;
2121 int eo
= pmatch
[dolnum
].rm_eo
;
2122 DBGPRINTF ("so=%d eo=%d dolnum=%d", so
, eo
, dolnum
);
2123 if (so
>=0 && eo
>=so
)
2124 obstack_grow (&str_obstack
, filnam
+ so
, eo
- so
);
2128 /* This can happen only when files_rules is buggy! */
2131 /* Always skip the character after the dollar. */
2135 obstack_1grow (&str_obstack
, c
);
2137 obstack_1grow (&str_obstack
, '\0');
2138 rawstr
= XOBFINISH (&str_obstack
, char *);
2139 str
= xstrdup (rawstr
);
2140 obstack_free (&str_obstack
, NULL
);
2141 DBGPRINTF ("matched replacement %s", str
);
2147 /* An output file, suitable for definitions, that can see declarations
2148 made in INPF and is linked into every language that uses INPF.
2149 Since the result is cached inside INPF, that argument cannot be
2150 declared constant, but is "almost" constant. */
2153 get_output_file_with_visibility (input_file
*inpf
)
2156 char *for_name
= NULL
;
2157 char *output_name
= NULL
;
2158 const char* inpfname
;
2160 /* This can happen when we need a file with visibility on a
2161 structure that we've never seen. We have to just hope that it's
2162 globally visible. */
2164 inpf
= system_h_file
;
2166 /* The result is cached in INPF, so return it if already known. */
2168 return inpf
->inpoutf
;
2170 /* In plugin mode, return NULL unless the input_file is one of the
2175 for (i
= 0; i
< nb_plugin_files
; i
++)
2176 if (inpf
== plugin_files
[i
])
2178 inpf
->inpoutf
= plugin_output
;
2179 return plugin_output
;
2185 inpfname
= get_input_file_name (inpf
);
2187 /* Try each rule in sequence in files_rules until one is triggered. */
2190 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2191 (void*) inpf
, inpfname
);
2193 for (; files_rules
[rulix
].frul_srcexpr
!= NULL
; rulix
++)
2195 DBGPRINTF ("rulix#%d srcexpr %s",
2196 rulix
, files_rules
[rulix
].frul_srcexpr
);
2198 if (!files_rules
[rulix
].frul_re
)
2200 /* Compile the regexpr lazily. */
2202 files_rules
[rulix
].frul_re
= XCNEW (regex_t
);
2203 err
= regcomp (files_rules
[rulix
].frul_re
,
2204 files_rules
[rulix
].frul_srcexpr
,
2205 files_rules
[rulix
].frul_rflags
);
2208 /* The regular expression compilation fails only when
2209 file_rules is buggy. */
2217 /* Match the regexpr and trigger the rule if matched. */
2219 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2221 regmatch_t pmatch
[10];
2222 memset (pmatch
, 0, sizeof (pmatch
));
2223 if (!regexec (files_rules
[rulix
].frul_re
,
2224 inpfname
, 10, pmatch
, 0))
2226 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2227 (void*) inpf
, inpfname
, rulix
,
2228 files_rules
[rulix
].frul_srcexpr
);
2230 matching_file_name_substitute (inpfname
, pmatch
,
2231 files_rules
[rulix
].frul_tr_for
);
2232 DBGPRINTF ("for_name %s", for_name
);
2234 matching_file_name_substitute (inpfname
, pmatch
,
2235 files_rules
[rulix
].frul_tr_out
);
2236 DBGPRINTF ("output_name %s", output_name
);
2237 if (files_rules
[rulix
].frul_action
)
2239 /* Invoke our action routine. */
2241 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2242 rulix
, output_name
, for_name
);
2244 (files_rules
[rulix
].frul_action
) (inpf
,
2245 &output_name
, &for_name
);
2246 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2247 rulix
, (void*)of
, output_name
, for_name
);
2248 /* If the action routine returned something, give it back
2249 immediately and cache it in inpf. */
2256 /* The rule matched, and had no action, or that action did
2257 not return any output file but could have changed the
2258 output_name or for_name. We break out of the loop on the
2264 /* The regexpr did not match. */
2265 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2266 rulix
, inpfname
, files_rules
[rulix
].frul_srcexpr
);
2272 if (!output_name
|| !for_name
)
2274 /* This should not be possible, and could only happen if the
2275 files_rules is incomplete or buggy. */
2276 fatal ("failed to compute output name for %s", inpfname
);
2279 /* Look through to see if we've ever seen this output filename
2280 before. If found, cache the result in inpf. */
2281 for (r
= output_files
; r
; r
= r
->next
)
2282 if (filename_cmp (r
->name
, output_name
) == 0)
2285 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r
,
2286 output_name
, for_name
);
2290 /* If not found, create it, and cache it in inpf. */
2291 r
= create_file (for_name
, output_name
);
2293 gcc_assert (r
&& r
->name
);
2294 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r
,
2295 output_name
, for_name
);
2302 /* The name of an output file, suitable for definitions, that can see
2303 declarations made in INPF and is linked into every language that
2307 get_output_file_name (input_file
* inpf
)
2309 outf_p o
= get_output_file_with_visibility (inpf
);
2315 /* Check if existing file is equal to the in memory buffer. */
2318 is_file_equal (outf_p of
)
2320 FILE *newfile
= fopen (of
->name
, "r");
2323 if (newfile
== NULL
)
2327 for (i
= 0; i
< of
->bufused
; i
++)
2330 ch
= fgetc (newfile
);
2331 if (ch
== EOF
|| ch
!= (unsigned char) of
->buf
[i
])
2337 if (equal
&& EOF
!= fgetc (newfile
))
2343 /* Copy the output to its final destination,
2344 but don't unnecessarily change modification times. */
2347 close_output_files (void)
2349 int nbwrittenfiles
= 0;
2352 for (of
= output_files
; of
; of
= of
->next
)
2354 if (!is_file_equal (of
))
2356 FILE *newfile
= NULL
;
2357 char *backupname
= NULL
;
2358 /* Back up the old version of the output file gt-FOO.c as
2359 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2362 backupname
= concat (backup_dir
, "/",
2363 lbasename (of
->name
), "~", NULL
);
2364 if (!access (of
->name
, F_OK
) && rename (of
->name
, backupname
))
2365 fatal ("failed to back up %s as %s: %s",
2366 of
->name
, backupname
, xstrerror (errno
));
2369 newfile
= fopen (of
->name
, "w");
2370 if (newfile
== NULL
)
2371 fatal ("opening output file %s: %s", of
->name
, xstrerror (errno
));
2372 if (fwrite (of
->buf
, 1, of
->bufused
, newfile
) != of
->bufused
)
2373 fatal ("writing output file %s: %s", of
->name
, xstrerror (errno
));
2374 if (fclose (newfile
) != 0)
2375 fatal ("closing output file %s: %s", of
->name
, xstrerror (errno
));
2377 if (verbosity_level
>= 2 && backupname
)
2378 printf ("%s wrote #%-3d %s backed-up in %s\n",
2379 progname
, nbwrittenfiles
, of
->name
, backupname
);
2380 else if (verbosity_level
>= 1)
2381 printf ("%s write #%-3d %s\n", progname
, nbwrittenfiles
, of
->name
);
2386 /* output file remains unchanged. */
2387 if (verbosity_level
>= 2)
2388 printf ("%s keep %s\n", progname
, of
->name
);
2392 of
->bufused
= of
->buflength
= 0;
2394 if (verbosity_level
>= 1)
2395 printf ("%s wrote %d files.\n", progname
, nbwrittenfiles
);
2402 const input_file
* file
;
2406 struct walk_type_data
;
2408 /* For scalars and strings, given the item in 'val'.
2409 For structures, given a pointer to the item in 'val'.
2410 For misc. pointers, given the item in 'val'.
2412 typedef void (*process_field_fn
) (type_p f
, const struct walk_type_data
* p
);
2413 typedef void (*func_name_fn
) (type_p s
, const struct walk_type_data
* p
);
2415 /* Parameters for write_types. */
2417 struct write_types_data
2420 const char *param_prefix
;
2421 const char *subfield_marker_routine
;
2422 const char *marker_routine
;
2423 const char *reorder_note_routine
;
2424 const char *comment
;
2425 int skip_hooks
; /* skip hook generation if non zero */
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 *tname
, const char *lastname
,
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
, "mark_hook") == 0)
2697 else if (strcmp (oo
->name
, "nested_ptr") == 0
2698 && oo
->kind
== OPTION_NESTED
)
2699 nested_ptr_d
= (const struct nested_ptr_data
*) oo
->info
.nested
;
2700 else if (strcmp (oo
->name
, "dot") == 0)
2702 else if (strcmp (oo
->name
, "tag") == 0)
2703 type_tag
= oo
->info
.string
;
2704 else if (strcmp (oo
->name
, "special") == 0)
2706 else if (strcmp (oo
->name
, "skip") == 0)
2708 else if (strcmp (oo
->name
, "atomic") == 0)
2710 else if (strcmp (oo
->name
, "default") == 0)
2712 else if (strcmp (oo
->name
, "chain_next") == 0)
2714 else if (strcmp (oo
->name
, "chain_prev") == 0)
2716 else if (strcmp (oo
->name
, "chain_circular") == 0)
2718 else if (strcmp (oo
->name
, "reorder") == 0)
2720 else if (strcmp (oo
->name
, "variable_size") == 0)
2722 else if (strcmp (oo
->name
, "for_user") == 0)
2725 error_at_line (d
->line
, "unknown option `%s'\n", oo
->name
);
2731 && (t
->kind
!= TYPE_POINTER
|| !union_or_struct_p (t
->u
.p
)))
2733 error_at_line (d
->line
,
2734 "field `%s' has invalid option `maybe_undef_p'\n",
2739 if (atomic_p
&& (t
->kind
!= TYPE_POINTER
) && (t
->kind
!= TYPE_STRING
))
2741 error_at_line (d
->line
, "field `%s' has invalid option `atomic'\n", d
->val
);
2749 d
->process_field (t
, d
);
2754 d
->in_ptr_field
= true;
2755 if (maybe_undef_p
&& t
->u
.p
->u
.s
.line
.file
== NULL
)
2757 oprintf (d
->of
, "%*sgcc_assert (!%s);\n", d
->indent
, "", d
->val
);
2761 /* If a pointer type is marked as "atomic", we process the
2762 field itself, but we don't walk the data that they point to.
2764 There are two main cases where we walk types: to mark
2765 pointers that are reachable, and to relocate pointers when
2766 writing a PCH file. In both cases, an atomic pointer is
2767 itself marked or relocated, but the memory that it points
2768 to is left untouched. In the case of PCH, that memory will
2769 be read/written unchanged to the PCH file. */
2772 oprintf (d
->of
, "%*sif (%s != NULL) {\n", d
->indent
, "", d
->val
);
2774 d
->process_field (t
, d
);
2776 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2782 if (!union_or_struct_p (t
->u
.p
))
2784 error_at_line (d
->line
,
2785 "field `%s' is pointer to unimplemented type",
2792 const char *oldprevval2
= d
->prev_val
[2];
2794 if (!union_or_struct_p (nested_ptr_d
->type
))
2796 error_at_line (d
->line
,
2797 "field `%s' has invalid "
2798 "option `nested_ptr'\n", d
->val
);
2802 d
->prev_val
[2] = d
->val
;
2803 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2805 d
->val
= xasprintf ("x%d", d
->counter
++);
2806 oprintf (d
->of
, "%*s%s %s * %s%s =\n", d
->indent
, "",
2807 (nested_ptr_d
->type
->kind
== TYPE_UNION
2808 ? "union" : "struct"),
2809 nested_ptr_d
->type
->u
.s
.tag
,
2810 d
->fn_wants_lvalue
? "" : "const ", d
->val
);
2811 oprintf (d
->of
, "%*s", d
->indent
+ 2, "");
2812 output_escaped_param (d
, nested_ptr_d
->convert_from
,
2814 oprintf (d
->of
, ";\n");
2816 d
->process_field (nested_ptr_d
->type
, d
);
2818 if (d
->fn_wants_lvalue
)
2820 oprintf (d
->of
, "%*s%s = ", d
->indent
, "",
2822 d
->prev_val
[2] = d
->val
;
2823 output_escaped_param (d
, nested_ptr_d
->convert_to
,
2825 oprintf (d
->of
, ";\n");
2829 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2830 d
->val
= d
->prev_val
[2];
2831 d
->prev_val
[2] = oldprevval2
;
2834 d
->process_field (t
->u
.p
, d
);
2838 int loopcounter
= d
->loopcounter
;
2839 const char *oldval
= d
->val
;
2840 const char *oldprevval3
= d
->prev_val
[3];
2843 oprintf (d
->of
, "%*sif (%s != NULL) {\n", d
->indent
, "", d
->val
);
2845 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
2846 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != (size_t)(", d
->indent
,
2847 "", loopcounter
, loopcounter
);
2848 if (!d
->in_record_p
)
2849 output_escaped_param (d
, length
, "length");
2851 oprintf (d
->of
, "l%d", loopcounter
);
2852 if (d
->have_this_obj
)
2853 /* Try to unswitch loops (see PR53880). */
2854 oprintf (d
->of
, ") && ((void *)%s == this_obj", oldval
);
2855 oprintf (d
->of
, "); i%d++) {\n", loopcounter
);
2857 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
2859 d
->prev_val
[3] = oldval
;
2860 walk_type (t
->u
.p
, d
);
2863 d
->prev_val
[3] = oldprevval3
;
2866 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2867 d
->process_field (t
, d
);
2869 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2871 d
->in_ptr_field
= false;
2878 const char *oldval
= d
->val
;
2881 /* If it's an array of scalars, we optimize by not generating
2883 if (t
->u
.a
.p
->kind
== TYPE_SCALAR
)
2887 loopcounter
= d
->loopcounter
;
2889 loopcounter
= d
->counter
++;
2891 /* When walking an array, compute the length and store it in a
2892 local variable before walking the array elements, instead of
2893 recomputing the length expression each time through the loop.
2894 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2895 where the length is stored in the first array element,
2896 because otherwise that operand can get overwritten on the
2898 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2900 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
2901 if (!d
->in_record_p
|| !length
)
2903 oprintf (d
->of
, "%*ssize_t l%d = (size_t)(",
2904 d
->indent
, "", loopcounter
);
2906 output_escaped_param (d
, length
, "length");
2908 oprintf (d
->of
, "%s", t
->u
.a
.len
);
2909 oprintf (d
->of
, ");\n");
2912 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2914 loopcounter
, loopcounter
, loopcounter
, loopcounter
);
2916 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
2918 walk_type (t
->u
.a
.p
, d
);
2923 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2925 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2933 const char *oldval
= d
->val
;
2934 const char *oldprevval1
= d
->prev_val
[1];
2935 const char *oldprevval2
= d
->prev_val
[2];
2936 const char *struct_mark_hook
= NULL
;
2937 const int union_p
= t
->kind
== TYPE_UNION
;
2938 int seen_default_p
= 0;
2940 int lengths_seen
= 0;
2942 bool any_length_seen
= false;
2944 if (!t
->u
.s
.line
.file
)
2945 error_at_line (d
->line
, "incomplete structure `%s'", t
->u
.s
.tag
);
2947 if ((d
->bitmap
& t
->u
.s
.bitmap
) != d
->bitmap
)
2949 error_at_line (d
->line
,
2950 "structure `%s' defined for mismatching languages",
2952 error_at_line (&t
->u
.s
.line
, "one structure defined here");
2955 /* Some things may also be defined in the structure's options. */
2956 for (o
= t
->u
.s
.opt
; o
; o
= o
->next
)
2957 if (!desc
&& strcmp (o
->name
, "desc") == 0
2958 && o
->kind
== OPTION_STRING
)
2959 desc
= o
->info
.string
;
2960 else if (!struct_mark_hook
&& strcmp (o
->name
, "mark_hook") == 0
2961 && o
->kind
== OPTION_STRING
)
2962 struct_mark_hook
= o
->info
.string
;
2964 if (struct_mark_hook
)
2965 oprintf (d
->of
, "%*s%s (&%s);\n",
2966 d
->indent
, "", struct_mark_hook
, oldval
);
2968 d
->prev_val
[2] = oldval
;
2969 d
->prev_val
[1] = oldprevval2
;
2974 error_at_line (d
->line
,
2975 "missing `desc' option for union `%s'",
2979 oprintf (d
->of
, "%*sswitch ((int) (", d
->indent
, "");
2980 output_escaped_param (d
, desc
, "desc");
2981 oprintf (d
->of
, "))\n");
2983 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2987 /* We have a "desc" option on a struct, signifying the
2988 base class within a GC-managed inheritance hierarchy.
2989 The current code specialcases the base class, then walks
2990 into subclasses, recursing into this routine to handle them.
2991 This organization requires the base class to have a case in
2992 the switch statement, and hence a tag value is mandatory
2993 for the base class. This restriction could be removed, but
2994 it would require some restructing of this code. */
2997 error_at_line (d
->line
,
2998 "missing `tag' option for type `%s'",
3001 oprintf (d
->of
, "%*sswitch ((int) (", d
->indent
, "");
3002 output_escaped_param (d
, desc
, "desc");
3003 oprintf (d
->of
, "))\n");
3005 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
3006 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", type_tag
);
3010 FOR_ALL_INHERITED_FIELDS (t
, f
)
3014 const char *fieldlength
= NULL
;
3016 d
->reorder_fn
= NULL
;
3017 for (oo
= f
->opt
; oo
; oo
= oo
->next
)
3018 if (strcmp (oo
->name
, "skip") == 0)
3020 else if (strcmp (oo
->name
, "length") == 0
3021 && oo
->kind
== OPTION_STRING
)
3022 fieldlength
= oo
->info
.string
;
3032 if (!any_length_seen
)
3034 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
3037 any_length_seen
= true;
3039 oprintf (d
->of
, "%*ssize_t l%d = (size_t)(",
3040 d
->indent
, "", d
->counter
- 1);
3041 output_escaped_param (d
, fieldlength
, "length");
3042 oprintf (d
->of
, ");\n");
3046 endcounter
= d
->counter
;
3048 FOR_ALL_INHERITED_FIELDS (t
, f
)
3051 const char *dot
= ".";
3052 const char *tagid
= NULL
;
3055 const char *fieldlength
= NULL
;
3058 d
->reorder_fn
= NULL
;
3059 for (oo
= f
->opt
; oo
; oo
= oo
->next
)
3060 if (strcmp (oo
->name
, "dot") == 0
3061 && oo
->kind
== OPTION_STRING
)
3062 dot
= oo
->info
.string
;
3063 else if (strcmp (oo
->name
, "tag") == 0
3064 && oo
->kind
== OPTION_STRING
)
3065 tagid
= oo
->info
.string
;
3066 else if (strcmp (oo
->name
, "skip") == 0)
3068 else if (strcmp (oo
->name
, "default") == 0)
3070 else if (strcmp (oo
->name
, "reorder") == 0
3071 && oo
->kind
== OPTION_STRING
)
3072 d
->reorder_fn
= oo
->info
.string
;
3073 else if (strcmp (oo
->name
, "length") == 0
3074 && oo
->kind
== OPTION_STRING
)
3075 fieldlength
= oo
->info
.string
;
3080 if (union_p
&& tagid
)
3082 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", tagid
);
3085 else if (union_p
&& default_p
)
3087 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
3091 else if (!union_p
&& (default_p
|| tagid
))
3092 error_at_line (d
->line
,
3093 "can't use `%s' outside a union on field `%s'",
3094 default_p
? "default" : "tag", f
->name
);
3095 else if (union_p
&& !(default_p
|| tagid
)
3096 && f
->type
->kind
== TYPE_SCALAR
)
3099 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3100 get_input_file_name (d
->line
->file
), d
->line
->line
,
3104 else if (union_p
&& !(default_p
|| tagid
))
3105 error_at_line (d
->line
,
3106 "field `%s' is missing `tag' or `default' option",
3111 d
->loopcounter
= endcounter
- lengths_seen
--;
3115 d
->val
= newval
= xasprintf ("%s%s%s", oldval
, dot
, f
->name
);
3117 d
->used_length
= false;
3118 d
->in_record_p
= !union_p
;
3120 walk_type (f
->type
, d
);
3122 d
->in_record_p
= false;
3128 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
3132 d
->reorder_fn
= NULL
;
3135 d
->prev_val
[1] = oldprevval1
;
3136 d
->prev_val
[2] = oldprevval2
;
3138 if (union_p
&& !seen_default_p
)
3140 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
3141 oprintf (d
->of
, "%*s break;\n", d
->indent
, "");
3144 if (desc
&& !union_p
)
3146 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
3151 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3156 /* Add cases to handle subclasses. */
3157 struct seen_tag
*tags
= NULL
;
3158 walk_subclasses (t
, d
, &tags
);
3160 /* Ensure that if someone forgets a "tag" option that we don't
3161 silent fail to traverse that subclass's fields. */
3162 if (!seen_default_p
)
3164 oprintf (d
->of
, "%*s/* Unrecognized tag value. */\n",
3166 oprintf (d
->of
, "%*sdefault: gcc_unreachable (); \n",
3170 /* End of the switch statement */
3171 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3174 if (any_length_seen
)
3177 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3182 case TYPE_LANG_STRUCT
:
3185 for (nt
= t
->u
.s
.lang_struct
; nt
; nt
= nt
->next
)
3186 if ((d
->bitmap
& nt
->u
.s
.bitmap
) == d
->bitmap
)
3189 error_at_line (d
->line
, "structure `%s' differs between languages",
3196 case TYPE_USER_STRUCT
:
3197 d
->process_field (t
, d
);
3201 case TYPE_UNDEFINED
:
3206 /* process_field routine for marking routines. */
3209 write_types_process_field (type_p f
, const struct walk_type_data
*d
)
3211 const struct write_types_data
*wtd
;
3212 const char *cast
= d
->needs_cast_p
? "(void *)" : "";
3213 wtd
= (const struct write_types_data
*) d
->cookie
;
3218 case TYPE_UNDEFINED
:
3221 oprintf (d
->of
, "%*s%s (%s%s", d
->indent
, "",
3222 wtd
->subfield_marker_routine
, cast
, d
->val
);
3223 if (wtd
->param_prefix
)
3225 if (f
->u
.p
->kind
== TYPE_SCALAR
)
3226 /* The current type is a pointer to a scalar (so not
3227 considered like a pointer to instances of user defined
3228 types) and we are seeing it; it means we must be even
3229 more careful about the second argument of the
3230 SUBFIELD_MARKER_ROUTINE call. That argument must
3231 always be the instance of the type for which
3232 write_func_for_structure was called - this really is
3233 what the function SUBFIELD_MARKER_ROUTINE expects.
3234 That is, it must be an instance of the ORIG_S type
3235 parameter of write_func_for_structure. The convention
3236 is that that argument must be "x" in that case (as set
3237 by write_func_for_structure). The problem is, we can't
3238 count on d->prev_val[3] to be always set to "x" in that
3239 case. Sometimes walk_type can set it to something else
3240 (to e.g cooperate with write_array when called from
3241 write_roots). So let's set it to "x" here then. */
3242 oprintf (d
->of
, ", x");
3244 oprintf (d
->of
, ", %s", d
->prev_val
[3]);
3247 oprintf (d
->of
, ", gt_%s_", wtd
->param_prefix
);
3248 output_mangled_typename (d
->of
, d
->orig_s
);
3251 oprintf (d
->of
, ", gt_%sa_%s", wtd
->param_prefix
, d
->prev_val
[0]);
3253 oprintf (d
->of
, ");\n");
3254 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
3255 oprintf (d
->of
, "%*s%s (%s%s, %s, %s);\n", d
->indent
, "",
3256 wtd
->reorder_note_routine
, cast
, d
->val
,
3257 d
->prev_val
[3], d
->reorder_fn
);
3263 case TYPE_LANG_STRUCT
:
3264 case TYPE_USER_STRUCT
:
3265 if (f
->kind
== TYPE_USER_STRUCT
&& !d
->in_ptr_field
)
3267 /* If F is a user-defined type and the field is not a
3268 pointer to the type, then we should not generate the
3269 standard pointer-marking code. All we need to do is call
3270 the user-provided marking function to process the fields
3272 oprintf (d
->of
, "%*sgt_%sx (&(%s));\n", d
->indent
, "", wtd
->prefix
,
3277 oprintf (d
->of
, "%*sgt_%s_", d
->indent
, "", wtd
->prefix
);
3278 output_mangled_typename (d
->of
, f
);
3279 oprintf (d
->of
, " (%s%s);\n", cast
, d
->val
);
3280 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
3281 oprintf (d
->of
, "%*s%s (%s%s, %s%s, %s);\n", d
->indent
, "",
3282 wtd
->reorder_note_routine
, cast
, d
->val
, cast
, d
->val
,
3295 /* Return an output file that is suitable for definitions which can
3296 reference struct S */
3299 get_output_file_for_structure (const_type_p s
)
3301 const input_file
*fn
;
3303 gcc_assert (union_or_struct_p (s
));
3304 fn
= s
->u
.s
.line
.file
;
3306 /* The call to get_output_file_with_visibility may update fn by
3307 caching its result inside, so we need the CONST_CAST. */
3308 return get_output_file_with_visibility (CONST_CAST (input_file
*, fn
));
3312 /* Returns the specifier keyword for a string or union type S, empty string
3316 get_type_specifier (const type_p s
)
3318 if (s
->kind
== TYPE_STRUCT
)
3320 else if (s
->kind
== TYPE_LANG_STRUCT
)
3321 return get_type_specifier (s
->u
.s
.lang_struct
);
3322 else if (s
->kind
== TYPE_UNION
)
3328 /* Emits a declaration for type TY (assumed to be a union or a
3329 structure) on stream OUT. */
3332 write_type_decl (outf_p out
, type_p ty
)
3334 if (union_or_struct_p (ty
))
3335 oprintf (out
, "%s%s", get_type_specifier (ty
), ty
->u
.s
.tag
);
3336 else if (ty
->kind
== TYPE_SCALAR
)
3338 if (ty
->u
.scalar_is_char
)
3339 oprintf (out
, "const char");
3341 oprintf (out
, "void");
3343 else if (ty
->kind
== TYPE_POINTER
)
3345 write_type_decl (out
, ty
->u
.p
);
3346 oprintf (out
, " *");
3348 else if (ty
->kind
== TYPE_ARRAY
)
3350 write_type_decl (out
, ty
->u
.a
.p
);
3351 oprintf (out
, " *");
3353 else if (ty
->kind
== TYPE_STRING
)
3355 oprintf (out
, "const char *");
3362 /* Write on OF the name of the marker function for structure S. PREFIX
3363 is the prefix to use (to distinguish ggc from pch markers). */
3366 write_marker_function_name (outf_p of
, type_p s
, const char *prefix
)
3368 if (union_or_struct_p (s
))
3370 const char *id_for_tag
= filter_type_name (s
->u
.s
.tag
);
3371 oprintf (of
, "gt_%sx_%s", prefix
, id_for_tag
);
3372 if (id_for_tag
!= s
->u
.s
.tag
)
3373 free (CONST_CAST (char *, id_for_tag
));
3379 /* Write on OF a user-callable routine to act as an entry point for
3380 the marking routine for S, generated by write_func_for_structure.
3381 WTD distinguishes between ggc and pch markers. */
3384 write_user_func_for_structure_ptr (outf_p of
, type_p s
, const write_types_data
*wtd
)
3386 gcc_assert (union_or_struct_p (s
));
3388 type_p alias_of
= NULL
;
3389 for (options_p opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3390 if (strcmp (opt
->name
, "ptr_alias") == 0)
3392 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3393 we do not generate marking code for ORIG_S here. Instead, a
3394 forwarder #define in gtype-desc.h will cause every call to its
3395 marker to call the target of this alias.
3397 However, we still want to create a user entry code for the
3398 aliased type. So, if ALIAS_OF is set, we only generate the
3399 user-callable marker function. */
3400 alias_of
= opt
->info
.type
;
3404 DBGPRINTF ("write_user_func_for_structure_ptr: %s %s", s
->u
.s
.tag
,
3407 /* Only write the function once. */
3408 if (s
->u
.s
.wrote_user_func_for_ptr
[wtd
->kind
])
3410 s
->u
.s
.wrote_user_func_for_ptr
[wtd
->kind
] = true;
3412 oprintf (of
, "\nvoid\n");
3413 oprintf (of
, "gt_%sx (", wtd
->prefix
);
3414 write_type_decl (of
, s
);
3415 oprintf (of
, " *& x)\n");
3416 oprintf (of
, "{\n");
3417 oprintf (of
, " if (x)\n ");
3418 write_marker_function_name (of
,
3419 alias_of
? alias_of
: get_ultimate_base_class (s
),
3421 oprintf (of
, " ((void *) x);\n");
3422 oprintf (of
, "}\n");
3426 /* Write a function to mark all the fields of type S on OF. PREFIX
3427 and D are as in write_user_marking_functions. */
3430 write_user_func_for_structure_body (type_p s
, const char *prefix
,
3431 struct walk_type_data
*d
)
3433 oprintf (d
->of
, "\nvoid\n");
3434 oprintf (d
->of
, "gt_%sx (", prefix
);
3435 write_type_decl (d
->of
, s
);
3436 oprintf (d
->of
, "& x_r ATTRIBUTE_UNUSED)\n");
3437 oprintf (d
->of
, "{\n");
3438 oprintf (d
->of
, " ");
3439 write_type_decl (d
->of
, s
);
3440 oprintf (d
->of
, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3444 oprintf (d
->of
, "}\n");
3447 /* Emit the user-callable functions needed to mark all the types used
3448 by the user structure S. PREFIX is the prefix to use to
3449 distinguish ggc and pch markers. D contains data needed to pass to
3450 walk_type when traversing the fields of a type.
3452 For every type T referenced by S, two routines are generated: one
3453 that takes 'T *', marks the pointer and calls the second routine,
3454 which just marks the fields of T. */
3457 write_user_marking_functions (type_p s
,
3458 const write_types_data
*w
,
3459 struct walk_type_data
*d
)
3461 gcc_assert (s
->kind
== TYPE_USER_STRUCT
);
3463 for (pair_p fld
= s
->u
.s
.fields
; fld
; fld
= fld
->next
)
3465 type_p fld_type
= fld
->type
;
3466 if (fld_type
->kind
== TYPE_POINTER
)
3468 type_p pointed_to_type
= fld_type
->u
.p
;
3469 if (union_or_struct_p (pointed_to_type
))
3470 write_user_func_for_structure_ptr (d
->of
, pointed_to_type
, w
);
3472 else if (union_or_struct_p (fld_type
))
3473 write_user_func_for_structure_body (fld_type
, w
->prefix
, d
);
3478 /* For S, a structure that's part of ORIG_S write out a routine that:
3479 - Takes a parameter, a void * but actually of type *S
3480 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3481 field of S or its substructures and (in some cases) things
3482 that are pointed to by S. */
3485 write_func_for_structure (type_p orig_s
, type_p s
,
3486 const struct write_types_data
*wtd
)
3488 const char *chain_next
= NULL
;
3489 const char *chain_prev
= NULL
;
3490 const char *chain_circular
= NULL
;
3491 const char *mark_hook_name
= NULL
;
3493 struct walk_type_data d
;
3495 if (s
->u
.s
.base_class
)
3497 /* Verify that the base class has a "desc", since otherwise
3498 the traversal hooks there won't attempt to visit fields of
3499 subclasses such as this one. */
3500 const_type_p ubc
= get_ultimate_base_class (s
);
3501 if ((!opts_have (ubc
->u
.s
.opt
, "user")
3502 && !opts_have (ubc
->u
.s
.opt
, "desc")))
3503 error_at_line (&s
->u
.s
.line
,
3504 ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
3505 ", but '%s' lacks a discriminator 'desc' option"),
3506 s
->u
.s
.tag
, ubc
->u
.s
.tag
, ubc
->u
.s
.tag
);
3508 /* Don't write fns for subclasses, only for the ultimate base class
3509 within an inheritance hierarchy. */
3513 memset (&d
, 0, sizeof (d
));
3514 d
.of
= get_output_file_for_structure (s
);
3516 bool for_user
= false;
3517 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3518 if (strcmp (opt
->name
, "chain_next") == 0
3519 && opt
->kind
== OPTION_STRING
)
3520 chain_next
= opt
->info
.string
;
3521 else if (strcmp (opt
->name
, "chain_prev") == 0
3522 && opt
->kind
== OPTION_STRING
)
3523 chain_prev
= opt
->info
.string
;
3524 else if (strcmp (opt
->name
, "chain_circular") == 0
3525 && opt
->kind
== OPTION_STRING
)
3526 chain_circular
= opt
->info
.string
;
3527 else if (strcmp (opt
->name
, "mark_hook") == 0
3528 && opt
->kind
== OPTION_STRING
)
3529 mark_hook_name
= opt
->info
.string
;
3530 else if (strcmp (opt
->name
, "for_user") == 0)
3532 if (chain_prev
!= NULL
&& chain_next
== NULL
)
3533 error_at_line (&s
->u
.s
.line
, "chain_prev without chain_next");
3534 if (chain_circular
!= NULL
&& chain_next
!= NULL
)
3535 error_at_line (&s
->u
.s
.line
, "chain_circular with chain_next");
3536 if (chain_circular
!= NULL
)
3537 chain_next
= chain_circular
;
3539 d
.process_field
= write_types_process_field
;
3543 d
.line
= &s
->u
.s
.line
;
3544 d
.bitmap
= s
->u
.s
.bitmap
;
3545 d
.prev_val
[0] = "*x";
3546 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
3547 d
.prev_val
[3] = "x";
3549 d
.have_this_obj
= false;
3551 oprintf (d
.of
, "\n");
3552 oprintf (d
.of
, "void\n");
3553 write_marker_function_name (d
.of
, orig_s
, wtd
->prefix
);
3554 oprintf (d
.of
, " (void *x_p)\n");
3555 oprintf (d
.of
, "{\n ");
3556 write_type_decl (d
.of
, s
);
3557 oprintf (d
.of
, " * %sx = (", chain_next
== NULL
? "const " : "");
3558 write_type_decl (d
.of
, s
);
3559 oprintf (d
.of
, " *)x_p;\n");
3560 if (chain_next
!= NULL
)
3562 /* TYPE_USER_STRUCTs should not occur here. These structures
3563 are completely handled by user code. */
3564 gcc_assert (orig_s
->kind
!= TYPE_USER_STRUCT
);
3566 oprintf (d
.of
, " ");
3567 write_type_decl (d
.of
, s
);
3568 oprintf (d
.of
, " * xlimit = x;\n");
3570 if (chain_next
== NULL
)
3572 oprintf (d
.of
, " if (%s (x", wtd
->marker_routine
);
3573 if (wtd
->param_prefix
)
3575 oprintf (d
.of
, ", x, gt_%s_", wtd
->param_prefix
);
3576 output_mangled_typename (d
.of
, orig_s
);
3578 oprintf (d
.of
, "))\n");
3582 if (chain_circular
!= NULL
)
3583 oprintf (d
.of
, " if (!%s (xlimit", wtd
->marker_routine
);
3585 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
3586 if (wtd
->param_prefix
)
3588 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
3589 output_mangled_typename (d
.of
, orig_s
);
3591 oprintf (d
.of
, "))\n");
3592 if (chain_circular
!= NULL
)
3593 oprintf (d
.of
, " return;\n do\n");
3594 if (mark_hook_name
&& !wtd
->skip_hooks
)
3596 oprintf (d
.of
, " {\n");
3597 oprintf (d
.of
, " %s (xlimit);\n ", mark_hook_name
);
3599 oprintf (d
.of
, " xlimit = (");
3600 d
.prev_val
[2] = "*xlimit";
3601 output_escaped_param (&d
, chain_next
, "chain_next");
3602 oprintf (d
.of
, ");\n");
3603 if (mark_hook_name
&& !wtd
->skip_hooks
)
3604 oprintf (d
.of
, " }\n");
3605 if (chain_prev
!= NULL
)
3607 oprintf (d
.of
, " if (x != xlimit)\n");
3608 oprintf (d
.of
, " for (;;)\n");
3609 oprintf (d
.of
, " {\n");
3610 oprintf (d
.of
, " %s %s * const xprev = (",
3611 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
3613 d
.prev_val
[2] = "*x";
3614 output_escaped_param (&d
, chain_prev
, "chain_prev");
3615 oprintf (d
.of
, ");\n");
3616 oprintf (d
.of
, " if (xprev == NULL) break;\n");
3617 oprintf (d
.of
, " x = xprev;\n");
3618 oprintf (d
.of
, " (void) %s (xprev", wtd
->marker_routine
);
3619 if (wtd
->param_prefix
)
3621 oprintf (d
.of
, ", xprev, gt_%s_", wtd
->param_prefix
);
3622 output_mangled_typename (d
.of
, orig_s
);
3624 oprintf (d
.of
, ");\n");
3625 oprintf (d
.of
, " }\n");
3627 if (chain_circular
!= NULL
)
3629 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
3630 if (wtd
->param_prefix
)
3632 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
3633 output_mangled_typename (d
.of
, orig_s
);
3635 oprintf (d
.of
, "));\n");
3636 if (mark_hook_name
&& !wtd
->skip_hooks
)
3637 oprintf (d
.of
, " %s (xlimit);\n", mark_hook_name
);
3638 oprintf (d
.of
, " do\n");
3641 oprintf (d
.of
, " while (x != xlimit)\n");
3643 oprintf (d
.of
, " {\n");
3644 if (mark_hook_name
&& chain_next
== NULL
&& !wtd
->skip_hooks
)
3646 oprintf (d
.of
, " %s (x);\n", mark_hook_name
);
3649 d
.prev_val
[2] = "*x";
3651 if (orig_s
->kind
!= TYPE_USER_STRUCT
)
3655 /* User structures have no fields to walk. Simply generate a call
3656 to the user-provided structure marker. */
3657 oprintf (d
.of
, "%*sgt_%sx (x);\n", d
.indent
, "", wtd
->prefix
);
3660 if (chain_next
!= NULL
)
3662 oprintf (d
.of
, " x = (");
3663 output_escaped_param (&d
, chain_next
, "chain_next");
3664 oprintf (d
.of
, ");\n");
3667 oprintf (d
.of
, " }\n");
3668 if (chain_circular
!= NULL
)
3669 oprintf (d
.of
, " while (x != xlimit);\n");
3670 oprintf (d
.of
, "}\n");
3672 if (orig_s
->kind
== TYPE_USER_STRUCT
)
3673 write_user_marking_functions (orig_s
, wtd
, &d
);
3677 write_user_func_for_structure_body (orig_s
, wtd
->prefix
, &d
);
3678 write_user_func_for_structure_ptr (d
.of
, orig_s
, wtd
);
3683 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3686 write_types (outf_p output_header
, type_p structures
,
3687 const struct write_types_data
*wtd
)
3689 int nbfun
= 0; /* Count the emitted functions. */
3692 oprintf (output_header
, "\n/* %s*/\n", wtd
->comment
);
3694 /* We first emit the macros and the declarations. Functions' code is
3695 emitted afterwards. This is needed in plugin mode. */
3696 oprintf (output_header
, "/* Macros and declarations. */\n");
3697 for (s
= structures
; s
; s
= s
->next
)
3698 /* Do not emit handlers for derived classes; we only ever deal with
3699 the ultimate base class within an inheritance hierarchy. */
3700 if ((s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
3701 && !s
->u
.s
.base_class
)
3705 if (s
->gc_used
== GC_MAYBE_POINTED_TO
&& s
->u
.s
.line
.file
== NULL
)
3708 const char *s_id_for_tag
= filter_type_name (s
->u
.s
.tag
);
3710 oprintf (output_header
, "#define gt_%s_", wtd
->prefix
);
3711 output_mangled_typename (output_header
, s
);
3712 oprintf (output_header
, "(X) do { \\\n");
3713 oprintf (output_header
,
3714 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd
->prefix
,
3716 oprintf (output_header
, " } while (0)\n");
3718 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3719 if (strcmp (opt
->name
, "ptr_alias") == 0
3720 && opt
->kind
== OPTION_TYPE
)
3722 const_type_p
const t
= (const_type_p
) opt
->info
.type
;
3723 if (t
->kind
== TYPE_STRUCT
3724 || t
->kind
== TYPE_UNION
|| t
->kind
== TYPE_LANG_STRUCT
)
3726 const char *t_id_for_tag
= filter_type_name (t
->u
.s
.tag
);
3727 oprintf (output_header
,
3728 "#define gt_%sx_%s gt_%sx_%s\n",
3729 wtd
->prefix
, s
->u
.s
.tag
, wtd
->prefix
, t_id_for_tag
);
3730 if (t_id_for_tag
!= t
->u
.s
.tag
)
3731 free (CONST_CAST (char *, t_id_for_tag
));
3734 error_at_line (&s
->u
.s
.line
,
3735 "structure alias is not a structure");
3741 /* Declare the marker procedure only once. */
3742 oprintf (output_header
,
3743 "extern void gt_%sx_%s (void *);\n",
3744 wtd
->prefix
, s_id_for_tag
);
3746 if (s_id_for_tag
!= s
->u
.s
.tag
)
3747 free (CONST_CAST (char *, s_id_for_tag
));
3749 if (s
->u
.s
.line
.file
== NULL
)
3751 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
3757 /* At last we emit the functions code. */
3758 oprintf (output_header
, "\n/* functions code */\n");
3759 for (s
= structures
; s
; s
= s
->next
)
3760 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
3764 if (s
->gc_used
== GC_MAYBE_POINTED_TO
&& s
->u
.s
.line
.file
== NULL
)
3766 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3767 if (strcmp (opt
->name
, "ptr_alias") == 0)
3772 if (s
->kind
== TYPE_LANG_STRUCT
)
3775 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
3778 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3779 nbfun
, (void*) ss
, ss
->u
.s
.tag
);
3780 write_func_for_structure (s
, ss
, wtd
);
3786 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3787 nbfun
, (void*) s
, s
->u
.s
.tag
);
3788 write_func_for_structure (s
, s
, wtd
);
3793 /* Structure s is not possibly pointed to, so can be ignored. */
3794 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3795 (void*)s
, s
->u
.s
.tag
,
3799 if (verbosity_level
>= 2)
3800 printf ("%s emitted %d routines for %s\n",
3801 progname
, nbfun
, wtd
->comment
);
3804 static const struct write_types_data ggc_wtd
= {
3805 "ggc_m", NULL
, "ggc_mark", "ggc_test_and_set_mark", NULL
,
3806 "GC marker procedures. ",
3810 static const struct write_types_data pch_wtd
= {
3811 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3812 "gt_pch_note_reorder",
3813 "PCH type-walking procedures. ",
3817 /* Write out the local pointer-walking routines. */
3819 /* process_field routine for local pointer-walking for user-callable
3820 routines. The difference between this and
3821 write_types_local_process_field is that, in this case, we do not
3822 need to check whether the given pointer matches the address of the
3823 parent structure. This check was already generated by the call
3824 to gt_pch_nx in the main gt_pch_p_*() function that is calling
3828 write_types_local_user_process_field (type_p f
, const struct walk_type_data
*d
)
3835 case TYPE_LANG_STRUCT
:
3837 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
3840 case TYPE_USER_STRUCT
:
3841 if (d
->in_ptr_field
)
3842 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
3844 oprintf (d
->of
, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3845 d
->indent
, "", d
->val
);
3853 case TYPE_UNDEFINED
:
3859 /* Write a function to PCH walk all the fields of type S on OF.
3860 D contains data needed by walk_type to recurse into the fields of S. */
3863 write_pch_user_walking_for_structure_body (type_p s
, struct walk_type_data
*d
)
3865 oprintf (d
->of
, "\nvoid\n");
3866 oprintf (d
->of
, "gt_pch_nx (");
3867 write_type_decl (d
->of
, s
);
3868 oprintf (d
->of
, "* x ATTRIBUTE_UNUSED,\n"
3869 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3870 "\tATTRIBUTE_UNUSED void *cookie)\n");
3871 oprintf (d
->of
, "{\n");
3874 d
->process_field
= write_types_local_user_process_field
;
3876 oprintf (d
->of
, "}\n");
3880 /* Emit the user-callable functions needed to mark all the types used
3881 by the user structure S. PREFIX is the prefix to use to
3882 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
3883 chain_next option defined. D contains data needed to pass to
3884 walk_type when traversing the fields of a type.
3886 For every type T referenced by S, two routines are generated: one
3887 that takes 'T *', marks the pointer and calls the second routine,
3888 which just marks the fields of T. */
3891 write_pch_user_walking_functions (type_p s
, struct walk_type_data
*d
)
3893 gcc_assert (s
->kind
== TYPE_USER_STRUCT
);
3895 for (pair_p fld
= s
->u
.s
.fields
; fld
; fld
= fld
->next
)
3897 type_p fld_type
= fld
->type
;
3898 if (union_or_struct_p (fld_type
))
3899 write_pch_user_walking_for_structure_body (fld_type
, d
);
3904 /* process_field routine for local pointer-walking. */
3907 write_types_local_process_field (type_p f
, const struct walk_type_data
*d
)
3909 gcc_assert (d
->have_this_obj
);
3915 case TYPE_LANG_STRUCT
:
3917 oprintf (d
->of
, "%*sif ((void *)(%s) == this_obj)\n", d
->indent
, "",
3919 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
3922 case TYPE_USER_STRUCT
:
3923 oprintf (d
->of
, "%*sif ((void *)(%s) == this_obj)\n", d
->indent
, "",
3925 if (d
->in_ptr_field
)
3926 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
3928 oprintf (d
->of
, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3929 d
->indent
, "", d
->val
);
3937 case TYPE_UNDEFINED
:
3943 /* For S, a structure that's part of ORIG_S, and using parameters
3944 PARAM, write out a routine that:
3945 - Is of type gt_note_pointers
3946 - Calls PROCESS_FIELD on each field of S or its substructures.
3950 write_local_func_for_structure (const_type_p orig_s
, type_p s
)
3952 struct walk_type_data d
;
3954 /* Don't write fns for subclasses, only for the ultimate base class
3955 within an inheritance hierarchy. */
3956 if (s
->u
.s
.base_class
)
3959 memset (&d
, 0, sizeof (d
));
3960 d
.of
= get_output_file_for_structure (s
);
3961 d
.process_field
= write_types_local_process_field
;
3963 d
.line
= &s
->u
.s
.line
;
3964 d
.bitmap
= s
->u
.s
.bitmap
;
3965 d
.prev_val
[0] = d
.prev_val
[2] = "*x";
3966 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
3967 d
.prev_val
[3] = "x";
3969 d
.fn_wants_lvalue
= true;
3971 oprintf (d
.of
, "\n");
3972 oprintf (d
.of
, "void\n");
3973 oprintf (d
.of
, "gt_pch_p_");
3974 output_mangled_typename (d
.of
, orig_s
);
3975 oprintf (d
.of
, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3977 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3978 "\tATTRIBUTE_UNUSED void *cookie)\n");
3979 oprintf (d
.of
, "{\n");
3980 oprintf (d
.of
, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3981 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
,
3982 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
3984 d
.have_this_obj
= true;
3986 if (s
->kind
!= TYPE_USER_STRUCT
)
3990 /* User structures have no fields to walk. Simply generate a
3991 call to the user-provided PCH walker. */
3992 oprintf (d
.of
, "%*sif ((void *)(%s) == this_obj)\n", d
.indent
, "",
3994 oprintf (d
.of
, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3995 d
.indent
, "", d
.val
);
3998 oprintf (d
.of
, "}\n");
4000 /* Write user-callable entry points for the PCH walking routines. */
4001 if (orig_s
->kind
== TYPE_USER_STRUCT
)
4002 write_pch_user_walking_functions (s
, &d
);
4004 for (options_p o
= s
->u
.s
.opt
; o
; o
= o
->next
)
4005 if (strcmp (o
->name
, "for_user") == 0)
4007 write_pch_user_walking_for_structure_body (s
, &d
);
4012 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
4015 write_local (outf_p output_header
, type_p structures
)
4022 oprintf (output_header
, "\n/* Local pointer-walking routines. */\n");
4023 for (s
= structures
; s
; s
= s
->next
)
4024 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
4028 if (s
->u
.s
.line
.file
== NULL
)
4030 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
4031 if (strcmp (opt
->name
, "ptr_alias") == 0
4032 && opt
->kind
== OPTION_TYPE
)
4034 const_type_p
const t
= (const_type_p
) opt
->info
.type
;
4035 if (t
->kind
== TYPE_STRUCT
4036 || t
->kind
== TYPE_UNION
|| t
->kind
== TYPE_LANG_STRUCT
)
4038 oprintf (output_header
, "#define gt_pch_p_");
4039 output_mangled_typename (output_header
, s
);
4040 oprintf (output_header
, " gt_pch_p_");
4041 output_mangled_typename (output_header
, t
);
4042 oprintf (output_header
, "\n");
4045 error_at_line (&s
->u
.s
.line
,
4046 "structure alias is not a structure");
4052 /* Declare the marker procedure only once. */
4053 oprintf (output_header
, "extern void gt_pch_p_");
4054 output_mangled_typename (output_header
, s
);
4055 oprintf (output_header
,
4056 "\n (void *, void *, gt_pointer_operator, void *);\n");
4058 if (s
->kind
== TYPE_LANG_STRUCT
)
4061 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
4062 write_local_func_for_structure (s
, ss
);
4065 write_local_func_for_structure (s
, s
);
4069 /* Nonzero if S is a type for which typed GC allocators should be output. */
4071 #define USED_BY_TYPED_GC_P(s) \
4072 ((s->kind == TYPE_POINTER \
4073 && (s->u.p->gc_used == GC_POINTED_TO \
4074 || s->u.p->gc_used == GC_USED)) \
4075 || (union_or_struct_p (s) \
4076 && ((s)->gc_used == GC_POINTED_TO \
4077 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4078 && s->u.s.line.file != NULL) \
4079 || ((s)->gc_used == GC_USED \
4080 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))) \
4081 || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
4085 /* Might T contain any non-pointer elements? */
4088 contains_scalar_p (type_p t
)
4096 return contains_scalar_p (t
->u
.a
.p
);
4097 case TYPE_USER_STRUCT
:
4098 /* User-marked structures will typically contain pointers. */
4101 /* Could also check for structures that have no non-pointer
4102 fields, but there aren't enough of those to worry about. */
4107 /* Mangle INPF and print it to F. */
4110 put_mangled_filename (outf_p f
, const input_file
*inpf
)
4112 /* The call to get_output_file_name may indirectly update fn since
4113 get_output_file_with_visibility caches its result inside, so we
4114 need the CONST_CAST. */
4115 const char *name
= get_output_file_name (CONST_CAST (input_file
*, inpf
));
4118 for (; *name
!= 0; name
++)
4119 if (ISALNUM (*name
))
4120 oprintf (f
, "%c", *name
);
4122 oprintf (f
, "%c", '_');
4125 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4126 LASTNAME, and NAME are all strings to insert in various places in
4127 the resulting code. */
4130 finish_root_table (struct flist
*flp
, const char *pfx
, const char *lastname
,
4131 const char *tname
, const char *name
)
4135 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
4136 if (fli2
->started_p
)
4138 oprintf (fli2
->f
, " %s\n", lastname
);
4139 oprintf (fli2
->f
, "};\n\n");
4142 for (fli2
= flp
; fli2
&& base_files
; fli2
= fli2
->next
)
4143 if (fli2
->started_p
)
4145 lang_bitmap bitmap
= get_lang_bitmap (fli2
->file
);
4148 for (fnum
= 0; bitmap
!= 0; fnum
++, bitmap
>>= 1)
4151 oprintf (base_files
[fnum
],
4152 "extern const struct %s gt_%s_", tname
, pfx
);
4153 put_mangled_filename (base_files
[fnum
], fli2
->file
);
4154 oprintf (base_files
[fnum
], "[];\n");
4160 for (fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
4161 oprintf (base_files
[fnum
],
4162 "EXPORTED_CONST struct %s * const %s[] = {\n", tname
, name
);
4166 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
4167 if (fli2
->started_p
)
4169 lang_bitmap bitmap
= get_lang_bitmap (fli2
->file
);
4172 fli2
->started_p
= 0;
4174 for (fnum
= 0; base_files
&& bitmap
!= 0; fnum
++, bitmap
>>= 1)
4177 oprintf (base_files
[fnum
], " gt_%s_", pfx
);
4178 put_mangled_filename (base_files
[fnum
], fli2
->file
);
4179 oprintf (base_files
[fnum
], ",\n");
4185 for (fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
4187 oprintf (base_files
[fnum
], " NULL\n");
4188 oprintf (base_files
[fnum
], "};\n");
4193 /* Finish off the created gt_clear_caches_file_c functions. */
4196 finish_cache_funcs (flist
*flp
)
4200 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
4201 if (fli2
->started_p
)
4203 oprintf (fli2
->f
, "}\n\n");
4206 for (fli2
= flp
; fli2
&& base_files
; fli2
= fli2
->next
)
4207 if (fli2
->started_p
)
4209 lang_bitmap bitmap
= get_lang_bitmap (fli2
->file
);
4212 for (fnum
= 0; bitmap
!= 0; fnum
++, bitmap
>>= 1)
4215 oprintf (base_files
[fnum
], "extern void gt_clear_caches_");
4216 put_mangled_filename (base_files
[fnum
], fli2
->file
);
4217 oprintf (base_files
[fnum
], " ();\n");
4221 for (size_t fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
4222 oprintf (base_files
[fnum
], "void\ngt_clear_caches ()\n{\n");
4224 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
4225 if (fli2
->started_p
)
4227 lang_bitmap bitmap
= get_lang_bitmap (fli2
->file
);
4230 fli2
->started_p
= 0;
4232 for (fnum
= 0; base_files
&& bitmap
!= 0; fnum
++, bitmap
>>= 1)
4235 oprintf (base_files
[fnum
], " gt_clear_caches_");
4236 put_mangled_filename (base_files
[fnum
], fli2
->file
);
4237 oprintf (base_files
[fnum
], " ();\n");
4241 for (size_t fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
4243 oprintf (base_files
[fnum
], "}\n");
4247 /* Write the first three fields (pointer, count and stride) for
4248 root NAME to F. V and LINE are as for write_root.
4250 Return true if the entry could be written; return false on error. */
4253 start_root_entry (outf_p f
, pair_p v
, const char *name
, struct fileloc
*line
)
4259 error_at_line (line
, "`%s' is too complex to be a root", name
);
4263 oprintf (f
, " {\n");
4264 oprintf (f
, " &%s,\n", name
);
4267 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
4269 oprintf (f
, " * (%s)", ap
->u
.a
.len
);
4270 else if (ap
== v
->type
)
4271 oprintf (f
, " * ARRAY_SIZE (%s)", v
->name
);
4273 oprintf (f
, " sizeof (%s", v
->name
);
4274 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
4276 oprintf (f
, "),\n");
4280 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4281 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4285 write_field_root (outf_p f
, pair_p v
, type_p type
, const char *name
,
4286 int has_length
, struct fileloc
*line
,
4287 bool emit_pch
, type_p field_type
, const char *field_name
)
4290 /* If the field reference is relative to V, rather than to some
4291 subcomponent of V, we can mark any subarrays with a single stride.
4292 We're effectively treating the field as a global variable in its
4294 if (v
&& type
== v
->type
)
4297 newv
.type
= field_type
;
4298 newv
.name
= ACONCAT ((v
->name
, ".", field_name
, NULL
));
4301 /* Otherwise, any arrays nested in the structure are too complex to
4303 else if (field_type
->kind
== TYPE_ARRAY
)
4305 write_root (f
, v
, field_type
, ACONCAT ((name
, ".", field_name
, NULL
)),
4306 has_length
, line
, emit_pch
);
4309 /* Write out to F the table entry and any marker routines needed to
4310 mark NAME as TYPE. V can be one of three values:
4312 - null, if NAME is too complex to represent using a single
4313 count and stride. In this case, it is an error for NAME to
4314 contain any gc-ed data.
4316 - the outermost array that contains NAME, if NAME is part of an array.
4318 - the C variable that contains NAME, if NAME is not part of an array.
4320 LINE is the line of the C source that declares the root variable.
4321 HAS_LENGTH is nonzero iff V was a variable-length array. */
4324 write_root (outf_p f
, pair_p v
, type_p type
, const char *name
, int has_length
,
4325 struct fileloc
*line
, bool emit_pch
)
4332 for (fld
= type
->u
.s
.fields
; fld
; fld
= fld
->next
)
4335 const char *desc
= NULL
;
4338 for (o
= fld
->opt
; o
; o
= o
->next
)
4339 if (strcmp (o
->name
, "skip") == 0)
4341 else if (strcmp (o
->name
, "desc") == 0
4342 && o
->kind
== OPTION_STRING
)
4343 desc
= o
->info
.string
;
4345 error_at_line (line
,
4346 "field `%s' of global `%s' has unknown option `%s'",
4347 fld
->name
, name
, o
->name
);
4351 else if (desc
&& fld
->type
->kind
== TYPE_UNION
)
4353 pair_p validf
= NULL
;
4356 for (ufld
= fld
->type
->u
.s
.fields
; ufld
; ufld
= ufld
->next
)
4358 const char *tag
= NULL
;
4360 for (oo
= ufld
->opt
; oo
; oo
= oo
->next
)
4361 if (strcmp (oo
->name
, "tag") == 0
4362 && oo
->kind
== OPTION_STRING
)
4363 tag
= oo
->info
.string
;
4364 if (tag
== NULL
|| strcmp (tag
, desc
) != 0)
4367 error_at_line (line
,
4368 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4369 name
, fld
->name
, validf
->name
,
4370 name
, fld
->name
, ufld
->name
, tag
);
4374 write_field_root (f
, v
, type
, name
, 0, line
, emit_pch
,
4376 ACONCAT ((fld
->name
, ".",
4377 validf
->name
, NULL
)));
4380 error_at_line (line
,
4381 "global `%s.%s' has `desc' option but is not union",
4384 write_field_root (f
, v
, type
, name
, 0, line
, emit_pch
, fld
->type
,
4393 newname
= xasprintf ("%s[0]", name
);
4394 write_root (f
, v
, type
->u
.a
.p
, newname
, has_length
, line
, emit_pch
);
4399 case TYPE_USER_STRUCT
:
4400 error_at_line (line
, "`%s' must be a pointer type, because it is "
4401 "a GC root and its type is marked with GTY((user))",
4409 if (!start_root_entry (f
, v
, name
, line
))
4414 if (!has_length
&& union_or_struct_p (tp
))
4416 tp
= get_ultimate_base_class (tp
);
4417 const char *id_for_tag
= filter_type_name (tp
->u
.s
.tag
);
4418 oprintf (f
, " >_ggc_mx_%s,\n", id_for_tag
);
4420 oprintf (f
, " >_pch_nx_%s", id_for_tag
);
4422 oprintf (f
, " NULL");
4423 if (id_for_tag
!= tp
->u
.s
.tag
)
4424 free (CONST_CAST (char *, id_for_tag
));
4427 && (tp
->kind
== TYPE_POINTER
|| union_or_struct_p (tp
)))
4429 oprintf (f
, " >_ggc_ma_%s,\n", name
);
4431 oprintf (f
, " >_pch_na_%s", name
);
4433 oprintf (f
, " NULL");
4437 error_at_line (line
,
4438 "global `%s' is pointer to unimplemented type",
4441 oprintf (f
, "\n },\n");
4447 if (!start_root_entry (f
, v
, name
, line
))
4450 oprintf (f
, " (gt_pointer_walker) >_ggc_m_S,\n");
4451 oprintf (f
, " (gt_pointer_walker) >_pch_n_S\n");
4452 oprintf (f
, " },\n");
4460 case TYPE_UNDEFINED
:
4462 case TYPE_LANG_STRUCT
:
4463 error_at_line (line
, "global `%s' is unimplemented type", name
);
4467 /* This generates a routine to walk an array. */
4470 write_array (outf_p f
, pair_p v
, const struct write_types_data
*wtd
)
4472 struct walk_type_data d
;
4475 memset (&d
, 0, sizeof (d
));
4481 d
.bitmap
= get_lang_bitmap (v
->line
.file
);
4483 d
.prev_val
[3] = prevval3
= xasprintf ("&%s", v
->name
);
4485 if (wtd
->param_prefix
)
4487 oprintf (f
, "static void gt_%sa_%s\n", wtd
->param_prefix
, v
->name
);
4488 oprintf (f
, " (void *, void *, gt_pointer_operator, void *);\n");
4489 oprintf (f
, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4490 wtd
->param_prefix
, v
->name
);
4492 " ATTRIBUTE_UNUSED void *x_p,\n"
4493 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4494 " ATTRIBUTE_UNUSED void * cookie)\n");
4495 oprintf (d
.of
, "{\n");
4496 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
4497 d
.process_field
= write_types_local_process_field
;
4498 d
.have_this_obj
= true;
4499 walk_type (v
->type
, &d
);
4500 oprintf (f
, "}\n\n");
4504 oprintf (f
, "static void gt_%sa_%s (void *);\n", wtd
->prefix
, v
->name
);
4505 oprintf (f
, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4506 wtd
->prefix
, v
->name
);
4508 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
4509 d
.process_field
= write_types_process_field
;
4510 d
.have_this_obj
= false;
4511 walk_type (v
->type
, &d
);
4513 oprintf (f
, "}\n\n");
4516 /* Output a table describing the locations and types of VARIABLES. */
4519 write_roots (pair_p variables
, bool emit_pch
)
4522 struct flist
*flp
= NULL
;
4524 for (v
= variables
; v
; v
= v
->next
)
4527 get_output_file_with_visibility (CONST_CAST (input_file
*,
4530 const char *length
= NULL
;
4531 int deletable_p
= 0;
4533 for (o
= v
->opt
; o
; o
= o
->next
)
4534 if (strcmp (o
->name
, "length") == 0
4535 && o
->kind
== OPTION_STRING
)
4536 length
= o
->info
.string
;
4537 else if (strcmp (o
->name
, "deletable") == 0)
4539 else if (strcmp (o
->name
, "cache") == 0)
4542 error_at_line (&v
->line
,
4543 "global `%s' has unknown option `%s'",
4546 for (fli
= flp
; fli
; fli
= fli
->next
)
4547 if (fli
->f
== f
&& f
)
4551 fli
= XNEW (struct flist
);
4555 fli
->file
= v
->line
.file
;
4556 gcc_assert (fli
->file
);
4559 oprintf (f
, "\n/* GC roots. */\n\n");
4564 && v
->type
->kind
== TYPE_POINTER
4565 && (v
->type
->u
.p
->kind
== TYPE_POINTER
4566 || v
->type
->u
.p
->kind
== TYPE_STRUCT
))
4568 write_array (f
, v
, &ggc_wtd
);
4569 write_array (f
, v
, &pch_wtd
);
4573 for (v
= variables
; v
; v
= v
->next
)
4575 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4582 for (o
= v
->opt
; o
; o
= o
->next
)
4583 if (strcmp (o
->name
, "length") == 0)
4585 else if (strcmp (o
->name
, "deletable") == 0)
4591 for (fli
= flp
; fli
; fli
= fli
->next
)
4594 if (!fli
->started_p
)
4598 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4599 put_mangled_filename (f
, v
->line
.file
);
4600 oprintf (f
, "[] = {\n");
4603 write_root (f
, v
, v
->type
, v
->name
, length_p
, &v
->line
, emit_pch
);
4606 finish_root_table (flp
, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4609 for (v
= variables
; v
; v
= v
->next
)
4611 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4617 for (o
= v
->opt
; o
; o
= o
->next
)
4618 if (strcmp (o
->name
, "deletable") == 0)
4624 for (fli
= flp
; fli
; fli
= fli
->next
)
4627 if (!fli
->started_p
)
4631 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4632 put_mangled_filename (f
, v
->line
.file
);
4633 oprintf (f
, "[] = {\n");
4636 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4640 finish_root_table (flp
, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4641 "gt_ggc_deletable_rtab");
4643 for (v
= variables
; v
; v
= v
->next
)
4645 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4651 for (o
= v
->opt
; o
; o
= o
->next
)
4652 if (strcmp (o
->name
, "cache") == 0)
4657 for (fli
= flp
; fli
; fli
= fli
->next
)
4660 if (!fli
->started_p
)
4664 oprintf (f
, "void\ngt_clear_caches_");
4665 put_mangled_filename (f
, v
->line
.file
);
4666 oprintf (f
, " ()\n{\n");
4669 oprintf (f
, " gt_cleare_cache (%s);\n", v
->name
);
4672 finish_cache_funcs (flp
);
4677 for (v
= variables
; v
; v
= v
->next
)
4679 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4685 for (o
= v
->opt
; o
; o
= o
->next
)
4686 if (strcmp (o
->name
, "deletable") == 0)
4695 if (!contains_scalar_p (v
->type
))
4698 for (fli
= flp
; fli
; fli
= fli
->next
)
4701 if (!fli
->started_p
)
4705 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4706 put_mangled_filename (f
, v
->line
.file
);
4707 oprintf (f
, "[] = {\n");
4710 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4714 finish_root_table (flp
, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4715 "gt_pch_scalar_rtab");
4718 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4719 guaranteee for somewhat increased readability. If name conflicts do happen,
4720 this funcion will have to be adjusted to be more like
4721 output_mangled_typename. */
4725 /* Dumps the value of typekind KIND. */
4728 dump_typekind (int indent
, enum typekind kind
)
4730 printf ("%*ckind = ", indent
, ' ');
4734 printf ("TYPE_SCALAR");
4737 printf ("TYPE_STRING");
4740 printf ("TYPE_STRUCT");
4742 case TYPE_UNDEFINED
:
4743 printf ("TYPE_UNDEFINED");
4745 case TYPE_USER_STRUCT
:
4746 printf ("TYPE_USER_STRUCT");
4749 printf ("TYPE_UNION");
4752 printf ("TYPE_POINTER");
4755 printf ("TYPE_ARRAY");
4757 case TYPE_LANG_STRUCT
:
4758 printf ("TYPE_LANG_STRUCT");
4766 /* Dumps the value of GC_USED flag. */
4769 dump_gc_used (int indent
, enum gc_used_enum gc_used
)
4771 printf ("%*cgc_used = ", indent
, ' ');
4775 printf ("GC_UNUSED");
4780 case GC_MAYBE_POINTED_TO
:
4781 printf ("GC_MAYBE_POINTED_TO");
4784 printf ("GC_POINTED_TO");
4792 /* Dumps the type options OPT. */
4795 dump_options (int indent
, options_p opt
)
4798 printf ("%*coptions = ", indent
, ' ');
4805 printf ("%s:string %s ", o
->name
, o
->info
.string
);
4808 printf ("%s:type ", o
->name
);
4809 dump_type (indent
+1, o
->info
.type
);
4812 printf ("%s:nested ", o
->name
);
4822 /* Dumps the source file location in LINE. */
4825 dump_fileloc (int indent
, struct fileloc line
)
4827 printf ("%*cfileloc: file = %s, line = %d\n", indent
, ' ',
4828 get_input_file_name (line
.file
),
4832 /* Recursively dumps the struct, union, or a language-specific
4836 dump_type_u_s (int indent
, type_p t
)
4840 gcc_assert (union_or_struct_p (t
));
4841 printf ("%*cu.s.tag = %s\n", indent
, ' ', t
->u
.s
.tag
);
4842 dump_fileloc (indent
, t
->u
.s
.line
);
4843 printf ("%*cu.s.fields =\n", indent
, ' ');
4844 fields
= t
->u
.s
.fields
;
4847 dump_pair (indent
+ INDENT
, fields
);
4848 fields
= fields
->next
;
4850 printf ("%*cend of fields of type %p\n", indent
, ' ', (void *) t
);
4851 dump_options (indent
, t
->u
.s
.opt
);
4852 printf ("%*cu.s.bitmap = %X\n", indent
, ' ', t
->u
.s
.bitmap
);
4853 if (t
->kind
== TYPE_LANG_STRUCT
)
4855 printf ("%*cu.s.lang_struct:\n", indent
, ' ');
4856 dump_type_list (indent
+ INDENT
, t
->u
.s
.lang_struct
);
4860 /* Recursively dumps the array T. */
4863 dump_type_u_a (int indent
, type_p t
)
4865 gcc_assert (t
->kind
== TYPE_ARRAY
);
4866 printf ("%*clen = %s, u.a.p:\n", indent
, ' ', t
->u
.a
.len
);
4867 dump_type_list (indent
+ INDENT
, t
->u
.a
.p
);
4870 /* Recursively dumps the type list T. */
4873 dump_type_list (int indent
, type_p t
)
4878 dump_type (indent
, p
);
4883 static htab_t seen_types
;
4885 /* Recursively dumps the type T if it was not dumped previously. */
4888 dump_type (int indent
, type_p t
)
4892 if (seen_types
== NULL
)
4893 seen_types
= htab_create (100, htab_hash_pointer
, htab_eq_pointer
, NULL
);
4895 printf ("%*cType at %p: ", indent
, ' ', (void *) t
);
4896 slot
= htab_find_slot (seen_types
, t
, INSERT
);
4899 printf ("already seen.\n");
4905 dump_typekind (indent
, t
->kind
);
4906 printf ("%*cpointer_to = %p\n", indent
+ INDENT
, ' ',
4907 (void *) t
->pointer_to
);
4908 dump_gc_used (indent
+ INDENT
, t
->gc_used
);
4912 printf ("%*cscalar_is_char = %s\n", indent
+ INDENT
, ' ',
4913 t
->u
.scalar_is_char
? "true" : "false");
4919 case TYPE_LANG_STRUCT
:
4920 case TYPE_USER_STRUCT
:
4921 dump_type_u_s (indent
+ INDENT
, t
);
4924 printf ("%*cp:\n", indent
+ INDENT
, ' ');
4925 dump_type (indent
+ INDENT
, t
->u
.p
);
4928 dump_type_u_a (indent
+ INDENT
, t
);
4933 printf ("%*cEnd of type at %p\n", indent
, ' ', (void *) t
);
4936 /* Dumps the pair P. */
4939 dump_pair (int indent
, pair_p p
)
4941 printf ("%*cpair: name = %s\n", indent
, ' ', p
->name
);
4942 dump_type (indent
, p
->type
);
4943 dump_fileloc (indent
, p
->line
);
4944 dump_options (indent
, p
->opt
);
4945 printf ("%*cEnd of pair %s\n", indent
, ' ', p
->name
);
4948 /* Dumps the list of pairs PP. */
4951 dump_pair_list (const char *name
, pair_p pp
)
4954 printf ("%s:\n", name
);
4955 for (p
= pp
; p
!= NULL
; p
= p
->next
)
4957 printf ("End of %s\n\n", name
);
4960 /* Dumps the STRUCTURES. */
4963 dump_structures (const char *name
, type_p structures
)
4965 printf ("%s:\n", name
);
4966 dump_type_list (0, structures
);
4967 printf ("End of %s\n\n", name
);
4970 /* Dumps the internal structures of gengtype. This is useful to debug
4971 gengtype itself, or to understand what it does, e.g. for plugin
4975 dump_everything (void)
4977 dump_pair_list ("typedefs", typedefs
);
4978 dump_structures ("structures", structures
);
4979 dump_pair_list ("variables", variables
);
4981 /* Allocated with the first call to dump_type. */
4982 htab_delete (seen_types
);
4987 /* Option specification for getopt_long. */
4988 static const struct option gengtype_long_options
[] = {
4989 {"help", no_argument
, NULL
, 'h'},
4990 {"version", no_argument
, NULL
, 'V'},
4991 {"verbose", no_argument
, NULL
, 'v'},
4992 {"dump", no_argument
, NULL
, 'd'},
4993 {"debug", no_argument
, NULL
, 'D'},
4994 {"plugin", required_argument
, NULL
, 'P'},
4995 {"srcdir", required_argument
, NULL
, 'S'},
4996 {"backupdir", required_argument
, NULL
, 'B'},
4997 {"inputs", required_argument
, NULL
, 'I'},
4998 {"read-state", required_argument
, NULL
, 'r'},
4999 {"write-state", required_argument
, NULL
, 'w'},
5000 /* Terminating NULL placeholder. */
5001 {NULL
, no_argument
, NULL
, 0},
5008 printf ("Usage: %s\n", progname
);
5009 printf ("\t -h | --help " " \t# Give this help.\n");
5010 printf ("\t -D | --debug "
5011 " \t# Give debug output to debug %s itself.\n", progname
);
5012 printf ("\t -V | --version " " \t# Give version information.\n");
5013 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5014 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5015 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5016 " \t# Generate for plugin.\n");
5017 printf ("\t -S | --srcdir <GCC-directory> "
5018 " \t# Specify the GCC source directory.\n");
5019 printf ("\t -B | --backupdir <directory> "
5020 " \t# Specify the backup directory for updated files.\n");
5021 printf ("\t -I | --inputs <input-list> "
5022 " \t# Specify the file with source files list.\n");
5023 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5024 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5028 print_version (void)
5030 printf ("%s %s%s\n", progname
, pkgversion_string
, version_string
);
5031 printf ("Report bugs: %s\n", bug_report_url
);
5034 /* Parse the program options using getopt_long... */
5036 parse_program_options (int argc
, char **argv
)
5039 while ((opt
= getopt_long (argc
, argv
, "hVvdP:S:B:I:w:r:D",
5040 gengtype_long_options
, NULL
)) >= 0)
5044 case 'h': /* --help */
5047 case 'V': /* --version */
5050 case 'd': /* --dump */
5053 case 'D': /* --debug */
5056 case 'v': /* --verbose */
5059 case 'P': /* --plugin */
5061 plugin_output_filename
= optarg
;
5063 fatal ("missing plugin output file name");
5065 case 'S': /* --srcdir */
5069 fatal ("missing source directory");
5070 srcdir_len
= strlen (srcdir
);
5072 case 'B': /* --backupdir */
5074 backup_dir
= optarg
;
5076 fatal ("missing backup directory");
5078 case 'I': /* --inputs */
5082 fatal ("missing input list");
5084 case 'r': /* --read-state */
5086 read_state_filename
= optarg
;
5088 fatal ("missing read state file");
5089 DBGPRINTF ("read state %s\n", optarg
);
5091 case 'w': /* --write-state */
5092 DBGPRINTF ("write state %s\n", optarg
);
5094 write_state_filename
= optarg
;
5096 fatal ("missing write state file");
5099 fprintf (stderr
, "%s: unknown flag '%c'\n", progname
, opt
);
5101 fatal ("unexpected flag");
5104 if (plugin_output_filename
)
5106 /* In plugin mode we require some input files. */
5109 fatal ("no source files given in plugin mode");
5110 nb_plugin_files
= argc
- optind
;
5111 plugin_files
= XNEWVEC (input_file
*, nb_plugin_files
);
5112 for (i
= 0; i
< (int) nb_plugin_files
; i
++)
5114 char *name
= argv
[i
+ optind
];
5115 plugin_files
[i
] = input_file_by_name (name
);
5122 /******* Manage input files. ******/
5124 /* Hash table of unique input file names. */
5125 static htab_t input_file_htab
;
5127 /* Find or allocate a new input_file by hash-consing it. */
5129 input_file_by_name (const char* name
)
5132 input_file
* f
= NULL
;
5136 namlen
= strlen (name
);
5137 f
= XCNEWVAR (input_file
, sizeof (input_file
)+namlen
+2);
5140 f
->inpisplugin
= false;
5141 strcpy (f
->inpname
, name
);
5142 slot
= htab_find_slot (input_file_htab
, f
, INSERT
);
5143 gcc_assert (slot
!= NULL
);
5146 /* Already known input file. */
5148 return (input_file
*)(*slot
);
5150 /* New input file. */
5155 /* Hash table support routines for input_file-s. */
5157 htab_hash_inputfile (const void *p
)
5159 const input_file
*inpf
= (const input_file
*) p
;
5161 return htab_hash_string (get_input_file_name (inpf
));
5165 htab_eq_inputfile (const void *x
, const void *y
)
5167 const input_file
*inpfx
= (const input_file
*) x
;
5168 const input_file
*inpfy
= (const input_file
*) y
;
5169 gcc_assert (inpfx
!= NULL
&& inpfy
!= NULL
);
5170 return !filename_cmp (get_input_file_name (inpfx
), get_input_file_name (inpfy
));
5175 main (int argc
, char **argv
)
5178 static struct fileloc pos
= { NULL
, 0 };
5179 outf_p output_header
;
5181 /* Mandatory common initializations. */
5182 progname
= "gengtype"; /* For fatal and messages. */
5183 /* Create the hash-table used to hash-cons input files. */
5185 htab_create (800, htab_hash_inputfile
, htab_eq_inputfile
, NULL
);
5186 /* Initialize our special input files. */
5187 this_file
= input_file_by_name (__FILE__
);
5188 system_h_file
= input_file_by_name ("system.h");
5189 /* Set the scalar_is_char union number for predefined scalar types. */
5190 scalar_nonchar
.u
.scalar_is_char
= FALSE
;
5191 scalar_char
.u
.scalar_is_char
= TRUE
;
5193 parse_program_options (argc
, argv
);
5198 time_t now
= (time_t) 0;
5200 DBGPRINTF ("gengtype started pid %d at %s",
5201 (int) getpid (), ctime (&now
));
5203 #endif /* ENABLE_CHECKING */
5205 /* Parse the input list and the input files. */
5206 DBGPRINTF ("inputlist %s", inputlist
);
5207 if (read_state_filename
)
5210 fatal ("input list %s cannot be given with a read state file %s",
5211 inputlist
, read_state_filename
);
5212 read_state (read_state_filename
);
5213 DBGPRINT_COUNT_TYPE ("structures after read_state", structures
);
5217 /* These types are set up with #define or else outside of where
5218 we can see them. We should initialize them before calling
5220 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5222 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos
));
5223 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos
));
5224 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos
));
5225 POS_HERE (do_scalar_typedef ("double_int", &pos
));
5226 POS_HERE (do_scalar_typedef ("offset_int", &pos
));
5227 POS_HERE (do_scalar_typedef ("widest_int", &pos
));
5228 POS_HERE (do_scalar_typedef ("int64_t", &pos
));
5229 POS_HERE (do_scalar_typedef ("uint64_t", &pos
));
5230 POS_HERE (do_scalar_typedef ("uint8", &pos
));
5231 POS_HERE (do_scalar_typedef ("uintptr_t", &pos
));
5232 POS_HERE (do_scalar_typedef ("jword", &pos
));
5233 POS_HERE (do_scalar_typedef ("JCF_u2", &pos
));
5234 POS_HERE (do_scalar_typedef ("void", &pos
));
5235 POS_HERE (do_scalar_typedef ("machine_mode", &pos
));
5236 POS_HERE (do_typedef ("PTR",
5237 create_pointer (resolve_typedef ("void", &pos
)),
5240 read_input_list (inputlist
);
5241 for (i
= 0; i
< num_gt_files
; i
++)
5243 parse_file (get_input_file_name (gt_files
[i
]));
5244 DBGPRINTF ("parsed file #%d %s",
5245 (int) i
, get_input_file_name (gt_files
[i
]));
5247 if (verbosity_level
>= 1)
5248 printf ("%s parsed %d files with %d GTY types\n",
5249 progname
, (int) num_gt_files
, type_count
);
5251 DBGPRINT_COUNT_TYPE ("structures after parsing", structures
);
5254 fatal ("either an input list or a read state file should be given");
5259 if (plugin_output_filename
)
5262 /* In plugin mode, we should have read a state file, and have
5263 given at least one plugin file. */
5264 if (!read_state_filename
)
5265 fatal ("No read state given in plugin mode for %s",
5266 plugin_output_filename
);
5268 if (nb_plugin_files
== 0 || !plugin_files
)
5269 fatal ("No plugin files given in plugin mode for %s",
5270 plugin_output_filename
);
5272 /* Parse our plugin files and augment the state. */
5273 for (ix
= 0; ix
< nb_plugin_files
; ix
++)
5275 input_file
* pluginput
= plugin_files
[ix
];
5276 pluginput
->inpisplugin
= true;
5277 parse_file (get_input_file_name (pluginput
));
5282 plugin_output
= create_file ("GCC", plugin_output_filename
);
5283 DBGPRINTF ("created plugin_output %p named %s",
5284 (void *) plugin_output
, plugin_output
->name
);
5287 { /* No plugin files, we are in normal mode. */
5289 fatal ("gengtype needs a source directory in normal mode");
5296 set_gc_used (variables
);
5298 for (type_p t
= structures
; t
; t
= t
->next
)
5300 bool for_user
= false;
5301 for (options_p o
= t
->u
.s
.opt
; o
; o
= o
->next
)
5302 if (strcmp (o
->name
, "for_user") == 0)
5309 set_gc_used_type (t
, GC_POINTED_TO
);
5311 /* The state at this point is read from the state input file or by
5312 parsing source files and optionally augmented by parsing plugin
5313 source files. Write it now. */
5314 if (write_state_filename
)
5316 DBGPRINT_COUNT_TYPE ("structures before write_state", structures
);
5319 fatal ("didn't write state file %s after errors",
5320 write_state_filename
);
5322 DBGPRINTF ("before write_state %s", write_state_filename
);
5323 write_state (write_state_filename
);
5328 /* After having written the state file we return immediately to
5329 avoid generating any output file. */
5339 output_header
= plugin_output
? plugin_output
: header_file
;
5340 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5343 write_types (output_header
, structures
, &ggc_wtd
);
5344 if (plugin_files
== NULL
)
5346 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5348 write_types (header_file
, structures
, &pch_wtd
);
5349 write_local (header_file
, structures
);
5351 write_roots (variables
, plugin_files
== NULL
);
5353 close_output_files ();
5358 /* Don't bother about free-ing any input or plugin file, etc. */