1 /* Process source files and output type information.
2 Copyright (C) 2002-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
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/>. */
26 #include "errors.h" /* for fatal */
28 #include "version.h" /* for version_string & pkgversion_string. */
33 #include "filenames.h"
35 /* Data types, macros, etc. used only in this file. */
38 /* The list of output files. */
41 /* The output header file that is included into pretty much every
46 /* The name of the file containing the list of input files. */
47 static char *inputlist
;
49 /* The plugin input files and their number; in that case only
50 a single file is produced. */
51 static input_file
**plugin_files
;
52 static size_t nb_plugin_files
;
54 /* The generated plugin output file and name. */
55 static outf_p plugin_output
;
56 static char *plugin_output_filename
;
58 /* Our source directory and its length. */
62 /* Variables used for reading and writing the state. */
63 const char *read_state_filename
;
64 const char *write_state_filename
;
66 /* Variables to help debugging. */
70 /* Level for verbose messages. */
73 /* We have a type count and use it to set the state_number of newly
74 allocated types to some unique negative number. */
75 static int type_count
;
77 /* The backup directory should be in the same file system as the
78 generated files, otherwise the rename(2) system call would fail.
79 If NULL, no backup is made when overwriting a generated file. */
80 static const char* backup_dir
; /* (-B) program option. */
83 static outf_p
create_file (const char *, const char *);
85 static const char *get_file_basename (const input_file
*);
86 static const char *get_file_realbasename (const input_file
*);
88 static int get_prefix_langdir_index (const char *);
89 static const char *get_file_langdir (const input_file
*);
91 static void dump_pair (int indent
, pair_p p
);
92 static void dump_type (int indent
, type_p p
);
93 static void dump_type_list (int indent
, type_p p
);
96 /* Nonzero iff an error has occurred. */
97 bool hit_error
= false;
99 static void gen_rtx_next (void);
100 static void write_rtx_next (void);
101 static void open_base_files (void);
102 static void close_output_files (void);
104 /* Report an error at POS, printing MSG. */
107 error_at_line (const struct fileloc
*pos
, const char *msg
, ...)
111 gcc_assert (pos
!= NULL
&& pos
->file
!= NULL
);
114 fprintf (stderr
, "%s:%d: ", get_input_file_name (pos
->file
), pos
->line
);
115 vfprintf (stderr
, msg
, ap
);
116 fputc ('\n', stderr
);
122 /* asprintf, but produces fatal message on out-of-memory. */
124 xasprintf (const char *format
, ...)
130 va_start (ap
, format
);
131 n
= vasprintf (&result
, format
, ap
);
132 if (result
== NULL
|| n
< 0)
133 fatal ("out of memory");
139 /* Locate the ultimate base class of struct S. */
142 get_ultimate_base_class (const_type_p s
)
144 while (s
->u
.s
.base_class
)
145 s
= s
->u
.s
.base_class
;
150 get_ultimate_base_class (type_p s
)
152 while (s
->u
.s
.base_class
)
153 s
= s
->u
.s
.base_class
;
157 /* Input file handling. */
159 /* Table of all input files. */
160 const input_file
**gt_files
;
163 /* A number of places use the name of this "gengtype.c" file for a
164 location for things that we can't rely on the source to define.
165 Make sure we can still use pointer comparison on filenames. */
166 input_file
* this_file
;
167 /* The "system.h" file is likewise specially useful. */
168 input_file
* system_h_file
;
170 /* Vector of per-language directories. */
171 const char **lang_dir_names
;
172 size_t num_lang_dirs
;
174 /* An array of output files suitable for definitions. There is one
175 BASE_FILES entry for each language. */
176 static outf_p
*base_files
;
181 /* Utility debugging function, printing the various type counts within
182 a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
184 dbgprint_count_type_at (const char *fil
, int lin
, const char *msg
, type_p t
)
186 int nb_types
= 0, nb_scalar
= 0, nb_string
= 0;
187 int nb_struct
= 0, nb_union
= 0, nb_array
= 0, nb_pointer
= 0;
188 int nb_lang_struct
= 0, nb_param_struct
= 0;
189 int nb_user_struct
= 0, nb_undefined
= 0;
191 for (p
= t
; p
; p
= p
->next
)
207 case TYPE_USER_STRUCT
:
219 case TYPE_LANG_STRUCT
:
222 case TYPE_PARAM_STRUCT
:
229 fprintf (stderr
, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
230 lbasename (fil
), lin
, msg
, nb_types
);
231 if (nb_scalar
> 0 || nb_string
> 0)
232 fprintf (stderr
, "@@%%@@ %d scalars, %d strings\n", nb_scalar
, nb_string
);
233 if (nb_struct
> 0 || nb_union
> 0)
234 fprintf (stderr
, "@@%%@@ %d structs, %d unions\n", nb_struct
, nb_union
);
235 if (nb_pointer
> 0 || nb_array
> 0)
236 fprintf (stderr
, "@@%%@@ %d pointers, %d arrays\n", nb_pointer
, nb_array
);
237 if (nb_lang_struct
> 0 || nb_param_struct
> 0)
238 fprintf (stderr
, "@@%%@@ %d lang_structs, %d param_structs\n",
239 nb_lang_struct
, nb_param_struct
);
240 if (nb_user_struct
> 0)
241 fprintf (stderr
, "@@%%@@ %d user_structs\n", nb_user_struct
);
242 if (nb_undefined
> 0)
243 fprintf (stderr
, "@@%%@@ %d undefined types\n", nb_undefined
);
244 fprintf (stderr
, "\n");
246 #endif /* ENABLE_CHECKING */
248 /* Scan the input file, LIST, and determine how much space we need to
249 store strings in. Also, count the number of language directories
250 and files. The numbers returned are overestimates as they does not
251 consider repeated files. */
253 measure_input_list (FILE *list
)
259 num_gt_files
= plugin_files
? nb_plugin_files
: 0;
260 while ((c
= getc (list
)) != EOF
)
269 /* Add space for a lang_bitmap before the input file name. */
270 n
+= sizeof (lang_bitmap
);
284 /* Read one input line from LIST to HEREP (which is updated). A
285 pointer to the string is returned via LINEP. If it was a language
286 subdirectory in square brackets, strip off the square brackets and
287 return true. Otherwise, leave space before the string for a
288 lang_bitmap, and return false. At EOF, returns false, does not
289 touch *HEREP, and sets *LINEP to NULL. POS is used for
292 read_input_line (FILE *list
, char **herep
, char **linep
, struct fileloc
*pos
)
298 /* Read over whitespace. */
299 while (c
== '\n' || c
== ' ')
309 /* No space for a lang_bitmap is necessary. Discard the '['. */
312 while (c
!= ']' && c
!= '\n' && c
!= EOF
)
321 c
= getc (list
); /* eat what should be a newline */
322 if (c
!= '\n' && c
!= EOF
)
323 error_at_line (pos
, "junk on line after language tag [%s]", line
);
326 error_at_line (pos
, "missing close bracket for language tag [%s",
335 /* Leave space for a lang_bitmap. */
336 memset (here
, 0, sizeof (lang_bitmap
));
337 here
+= sizeof (lang_bitmap
);
344 while (c
!= EOF
&& c
!= '\n');
352 /* Read the list of input files from LIST and compute all of the
353 relevant tables. There is one file per line of the list. At
354 first, all the files on the list are language-generic, but
355 eventually a line will appear which is the name of a language
356 subdirectory in square brackets, like this: [cp]. All subsequent
357 files are specific to that language, until another language
358 subdirectory tag appears. Files can appear more than once, if
359 they apply to more than one language. */
361 read_input_list (const char *listname
)
363 FILE *list
= fopen (listname
, "r");
365 fatal ("cannot open %s: %s", listname
, xstrerror (errno
));
369 size_t bufsz
= measure_input_list (list
);
370 char *buf
= XNEWVEC (char, bufsz
);
372 char *committed
= buf
;
373 char *limit
= buf
+ bufsz
;
378 lang_bitmap curlangs
= (1 << num_lang_dirs
) - 1;
380 epos
.file
= input_file_by_name (listname
);
383 lang_dir_names
= XNEWVEC (const char *, num_lang_dirs
);
384 gt_files
= XNEWVEC (const input_file
*, num_gt_files
);
391 is_language
= read_input_line (list
, &here
, &line
, &epos
);
392 gcc_assert (here
<= limit
);
395 else if (is_language
)
398 gcc_assert (langno
<= num_lang_dirs
);
399 for (i
= 0; i
< langno
; i
++)
400 if (strcmp (lang_dir_names
[i
], line
) == 0)
402 error_at_line (&epos
, "duplicate language tag [%s]",
409 curlangs
= 1 << langno
;
410 lang_dir_names
[langno
++] = line
;
415 input_file
*inpf
= input_file_by_name (line
);
416 gcc_assert (nfiles
<= num_gt_files
);
417 for (i
= 0; i
< nfiles
; i
++)
418 /* Since the input_file-s are uniquely hash-consed, we
419 can just compare pointers! */
420 if (gt_files
[i
] == inpf
)
422 /* Throw away the string we just read, and add the
423 current language to the existing string's bitmap. */
424 lang_bitmap bmap
= get_lang_bitmap (inpf
);
426 error_at_line (&epos
,
427 "file %s specified more than once "
428 "for language %s", line
,
430 0 ? "(all)" : lang_dir_names
[langno
-
434 set_lang_bitmap (inpf
, bmap
);
439 set_lang_bitmap (inpf
, curlangs
);
440 gt_files
[nfiles
++] = inpf
;
443 /* Update the global counts now that we know accurately how many
444 things there are. (We do not bother resizing the arrays down.) */
445 num_lang_dirs
= langno
;
446 /* Add the plugin files if provided. */
450 for (i
= 0; i
< nb_plugin_files
; i
++)
451 gt_files
[nfiles
++] = plugin_files
[i
];
453 num_gt_files
= nfiles
;
456 /* Sanity check: any file that resides in a language subdirectory
457 (e.g. 'cp') ought to belong to the corresponding language.
458 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
459 (Can you even do that? Should you be allowed to?) */
462 for (f
= 0; f
< num_gt_files
; f
++)
464 lang_bitmap bitmap
= get_lang_bitmap (gt_files
[f
]);
465 const char *basename
= get_file_basename (gt_files
[f
]);
466 const char *slashpos
= strchr (basename
, '/');
467 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
468 const char *slashpos2
= strchr (basename
, '\\');
470 if (!slashpos
|| (slashpos2
&& slashpos2
< slashpos
))
471 slashpos
= slashpos2
;
477 for (l
= 0; l
< num_lang_dirs
; l
++)
478 if ((size_t) (slashpos
- basename
) == strlen (lang_dir_names
[l
])
479 && memcmp (basename
, lang_dir_names
[l
],
480 strlen (lang_dir_names
[l
])) == 0)
482 if (!(bitmap
& (1 << l
)))
483 error ("%s is in language directory '%s' but is not "
484 "tagged for that language",
485 basename
, lang_dir_names
[l
]);
493 fatal ("error reading %s: %s", listname
, xstrerror (errno
));
500 /* The one and only TYPE_STRING. */
502 struct type string_type
= {
503 TYPE_STRING
, 0, 0, 0, GC_USED
, {0}
506 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
507 set early in main. */
509 struct type scalar_nonchar
= {
510 TYPE_SCALAR
, 0, 0, 0, GC_USED
, {0}
513 struct type scalar_char
= {
514 TYPE_SCALAR
, 0, 0, 0, GC_USED
, {0}
517 /* Lists of various things. */
519 pair_p typedefs
= NULL
;
520 type_p structures
= NULL
;
521 type_p param_structs
= NULL
;
522 pair_p variables
= NULL
;
524 static type_p
find_param_structure (type_p t
, type_p param
[NUM_PARAM
]);
525 static type_p
adjust_field_tree_exp (type_p t
, options_p opt
);
526 static type_p
adjust_field_rtx_def (type_p t
, options_p opt
);
528 /* Define S as a typedef to T at POS. */
531 do_typedef (const char *s
, type_p t
, struct fileloc
*pos
)
535 /* temporary kludge - gengtype doesn't handle conditionals or
536 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
537 is coming from this file (main() sets them up with safe dummy
539 if (!strcmp (s
, "CUMULATIVE_ARGS") && pos
->file
!= this_file
)
542 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
543 if (strcmp (p
->name
, s
) == 0)
545 if (p
->type
!= t
&& strcmp (s
, "result_type") != 0)
547 error_at_line (pos
, "type `%s' previously defined", s
);
548 error_at_line (&p
->line
, "previously defined here");
553 p
= XNEW (struct pair
);
562 /* Define S as a typename of a scalar. Cannot be used to define
563 typedefs of 'char'. Note: is also used for pointer-to-function
564 typedefs (which are therefore not treated as pointers). */
567 do_scalar_typedef (const char *s
, struct fileloc
*pos
)
569 do_typedef (s
, &scalar_nonchar
, pos
);
572 /* Similar to strtok_r. */
575 strtoken (char *str
, const char *delim
, char **next
)
582 /* Skip the leading delimiters. */
583 str
+= strspn (str
, delim
);
585 /* This is an empty token. */
588 /* The current token. */
591 /* Find the next delimiter. */
592 str
+= strcspn (str
, delim
);
594 /* This is the last token. */
598 /* Terminate the current token. */
600 /* Advance to the next token. */
607 /* Define TYPE_NAME to be a user defined type at location POS. */
610 create_user_defined_type (const char *type_name
, struct fileloc
*pos
)
612 type_p ty
= find_structure (type_name
, TYPE_USER_STRUCT
);
614 /* We might have already seen an incomplete decl of the given type,
615 in which case we won't have yet seen a GTY((user)), and the type will
616 only have kind "TYPE_STRUCT". Mark it as a user struct. */
617 ty
->kind
= TYPE_USER_STRUCT
;
620 ty
->u
.s
.bitmap
= get_lang_bitmap (pos
->file
);
621 do_typedef (type_name
, ty
, pos
);
623 /* If TYPE_NAME specifies a template, create references to the types
624 in the template by pretending that each type is a field of TY.
625 This is needed to make sure that the types referenced by the
626 template are marked as used. */
627 char *str
= xstrdup (type_name
);
628 char *open_bracket
= strchr (str
, '<');
631 /* We only accept simple template declarations (see
632 require_template_declaration), so we only need to parse a
633 comma-separated list of strings, implicitly assumed to
634 be type names, potentially with "*" characters. */
635 char *arg
= open_bracket
+ 1;
637 char *type_id
= strtoken (arg
, ",>", &next
);
641 /* Create a new field for every type found inside the template
644 /* Support a single trailing "*" character. */
645 const char *star
= strchr (type_id
, '*');
646 int is_ptr
= (star
!= NULL
);
647 size_t offset_to_star
= star
- type_id
;
649 offset_to_star
= star
- type_id
;
651 if (strstr (type_id
, "char*"))
653 type_id
= strtoken (0, ",>", &next
);
657 char *field_name
= xstrdup (type_id
);
662 /* Strip off the first '*' character (and any subsequent text). */
663 *(field_name
+ offset_to_star
) = '\0';
665 arg_type
= find_structure (field_name
, TYPE_STRUCT
);
666 arg_type
= create_pointer (arg_type
);
669 arg_type
= resolve_typedef (field_name
, pos
);
671 fields
= create_field_at (fields
, arg_type
, field_name
, 0, pos
);
672 type_id
= strtoken (0, ",>", &next
);
675 /* Associate the field list to TY. */
676 ty
->u
.s
.fields
= fields
;
684 /* Given a typedef name S, return its associated type. Return NULL if
685 S is not a registered type name. */
688 type_for_name (const char *s
)
692 /* Special-case support for types within a "gcc::" namespace. Rather
693 than fully-supporting namespaces, simply strip off the "gcc::" prefix
694 where present. This allows us to have GTY roots of this form:
695 extern GTY(()) gcc::some_type *some_ptr;
696 where the autogenerated functions will refer to simply "some_type",
697 where they can be resolved into their namespace. */
698 if (0 == strncmp (s
, "gcc::", 5))
701 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
702 if (strcmp (p
->name
, s
) == 0)
708 /* Create an undefined type with name S and location POS. Return the
709 newly created type. */
712 create_undefined_type (const char *s
, struct fileloc
*pos
)
714 type_p ty
= find_structure (s
, TYPE_UNDEFINED
);
716 ty
->u
.s
.bitmap
= get_lang_bitmap (pos
->file
);
717 do_typedef (s
, ty
, pos
);
722 /* Return the type previously defined for S. Use POS to report errors. */
725 resolve_typedef (const char *s
, struct fileloc
*pos
)
727 bool is_template_instance
= (strchr (s
, '<') != NULL
);
728 type_p p
= type_for_name (s
);
730 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
731 type for regular type identifiers. If the type identifier S is a
732 template instantiation, however, we treat it as a user defined
735 FIXME, this is actually a limitation in gengtype. Supporting
736 template types and their instances would require keeping separate
737 track of the basic types definition and its instances. This
738 essentially forces all template classes in GC to be marked
741 p
= (is_template_instance
)
742 ? create_user_defined_type (s
, pos
)
743 : create_undefined_type (s
, pos
);
748 /* Add SUBCLASS to head of linked list of BASE's subclasses. */
750 void add_subclass (type_p base
, type_p subclass
)
752 gcc_assert (union_or_struct_p (base
));
753 gcc_assert (union_or_struct_p (subclass
));
755 subclass
->u
.s
.next_sibling_class
= base
->u
.s
.first_subclass
;
756 base
->u
.s
.first_subclass
= subclass
;
759 /* Create and return a new structure with tag NAME at POS with fields
760 FIELDS and options O. The KIND of structure must be one of
761 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
764 new_structure (const char *name
, enum typekind kind
, struct fileloc
*pos
,
765 pair_p fields
, options_p o
, type_p base_class
)
769 lang_bitmap bitmap
= get_lang_bitmap (pos
->file
);
770 bool isunion
= (kind
== TYPE_UNION
);
772 gcc_assert (union_or_struct_p (kind
));
774 for (si
= structures
; si
!= NULL
; si
= si
->next
)
775 if (strcmp (name
, si
->u
.s
.tag
) == 0 && UNION_P (si
) == isunion
)
778 if (si
->kind
== TYPE_LANG_STRUCT
)
782 for (si
= ls
->u
.s
.lang_struct
; si
!= NULL
; si
= si
->next
)
783 if (si
->u
.s
.bitmap
== bitmap
)
786 else if (si
->u
.s
.line
.file
!= NULL
&& si
->u
.s
.bitmap
!= bitmap
)
790 si
= XCNEW (struct type
);
791 memcpy (si
, ls
, sizeof (struct type
));
792 ls
->kind
= TYPE_LANG_STRUCT
;
793 ls
->u
.s
.lang_struct
= si
;
794 ls
->u
.s
.fields
= NULL
;
796 si
->state_number
= -type_count
;
797 si
->pointer_to
= NULL
;
798 si
->u
.s
.lang_struct
= ls
;
803 if (ls
!= NULL
&& s
== NULL
)
806 s
= XCNEW (struct type
);
807 s
->state_number
= -type_count
;
808 s
->next
= ls
->u
.s
.lang_struct
;
809 ls
->u
.s
.lang_struct
= s
;
810 s
->u
.s
.lang_struct
= ls
;
818 s
= XCNEW (struct type
);
819 s
->state_number
= -type_count
;
820 s
->next
= structures
;
824 if (s
->u
.s
.lang_struct
&& (s
->u
.s
.lang_struct
->u
.s
.bitmap
& bitmap
))
826 error_at_line (pos
, "duplicate definition of '%s %s'",
827 isunion
? "union" : "struct", s
->u
.s
.tag
);
828 error_at_line (&s
->u
.s
.line
, "previous definition here");
834 s
->u
.s
.fields
= fields
;
836 s
->u
.s
.bitmap
= bitmap
;
837 if (s
->u
.s
.lang_struct
)
838 s
->u
.s
.lang_struct
->u
.s
.bitmap
|= bitmap
;
839 s
->u
.s
.base_class
= base_class
;
841 add_subclass (base_class
, s
);
846 /* Return the previously-defined structure or union with tag NAME,
847 or a new empty structure or union if none was defined previously.
848 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
852 find_structure (const char *name
, enum typekind kind
)
855 bool isunion
= (kind
== TYPE_UNION
);
857 gcc_assert (kind
== TYPE_UNDEFINED
|| union_or_struct_p (kind
));
859 for (s
= structures
; s
!= NULL
; s
= s
->next
)
860 if (strcmp (name
, s
->u
.s
.tag
) == 0 && UNION_P (s
) == isunion
)
864 s
= XCNEW (struct type
);
865 s
->next
= structures
;
866 s
->state_number
= -type_count
;
874 /* Return the previously-defined parameterized structure for structure
875 T and parameters PARAM, or a new parameterized empty structure or
876 union if none was defined previously. */
879 find_param_structure (type_p t
, type_p param
[NUM_PARAM
])
883 for (res
= param_structs
; res
; res
= res
->next
)
884 if (res
->u
.param_struct
.stru
== t
885 && memcmp (res
->u
.param_struct
.param
, param
,
886 sizeof (type_p
) * NUM_PARAM
) == 0)
891 res
= XCNEW (struct type
);
892 res
->kind
= TYPE_PARAM_STRUCT
;
893 res
->next
= param_structs
;
894 res
->state_number
= -type_count
;
896 res
->u
.param_struct
.stru
= t
;
897 memcpy (res
->u
.param_struct
.param
, param
, sizeof (type_p
) * NUM_PARAM
);
902 /* Return a scalar type with name NAME. */
905 create_scalar_type (const char *name
)
907 if (!strcmp (name
, "char") || !strcmp (name
, "unsigned char"))
910 return &scalar_nonchar
;
914 /* Return a pointer to T. */
917 create_pointer (type_p t
)
921 type_p r
= XCNEW (struct type
);
923 r
->state_number
= -type_count
;
924 r
->kind
= TYPE_POINTER
;
928 return t
->pointer_to
;
931 /* Return an array of length LEN. */
934 create_array (type_p t
, const char *len
)
939 v
= XCNEW (struct type
);
940 v
->kind
= TYPE_ARRAY
;
941 v
->state_number
= -type_count
;
947 /* Return a string options structure with name NAME and info INFO.
948 NEXT is the next option in the chain. */
950 create_string_option (options_p next
, const char *name
, const char *info
)
952 options_p o
= XNEW (struct options
);
953 o
->kind
= OPTION_STRING
;
956 o
->info
.string
= info
;
960 /* Create a type options structure with name NAME and info INFO. NEXT
961 is the next option in the chain. */
963 create_type_option (options_p next
, const char* name
, type_p info
)
965 options_p o
= XNEW (struct options
);
968 o
->kind
= OPTION_TYPE
;
973 /* Create a nested pointer options structure with name NAME and info
974 INFO. NEXT is the next option in the chain. */
976 create_nested_option (options_p next
, const char* name
,
977 struct nested_ptr_data
* info
)
980 o
= XNEW (struct options
);
983 o
->kind
= OPTION_NESTED
;
984 o
->info
.nested
= info
;
988 /* Return an options structure for a "nested_ptr" option. */
990 create_nested_ptr_option (options_p next
, type_p t
,
991 const char *to
, const char *from
)
993 struct nested_ptr_data
*d
= XNEW (struct nested_ptr_data
);
995 d
->type
= adjust_field_type (t
, 0);
997 d
->convert_from
= from
;
998 return create_nested_option (next
, "nested_ptr", d
);
1001 /* Add a variable named S of type T with options O defined at POS,
1004 note_variable (const char *s
, type_p t
, options_p o
, struct fileloc
*pos
)
1007 n
= XNEW (struct pair
);
1012 n
->next
= variables
;
1016 /* Most-general structure field creator. */
1018 create_field_all (pair_p next
, type_p type
, const char *name
, options_p opt
,
1019 const input_file
*inpf
, int line
)
1023 field
= XNEW (struct pair
);
1028 field
->line
.file
= inpf
;
1029 field
->line
.line
= line
;
1033 /* Create a field that came from the source code we are scanning,
1034 i.e. we have a 'struct fileloc', and possibly options; also,
1035 adjust_field_type should be called. */
1037 create_field_at (pair_p next
, type_p type
, const char *name
, options_p opt
,
1038 struct fileloc
*pos
)
1040 return create_field_all (next
, adjust_field_type (type
, opt
),
1041 name
, opt
, pos
->file
, pos
->line
);
1044 /* Create a fake field with the given type and name. NEXT is the next
1045 field in the chain. */
1046 #define create_field(next,type,name) \
1047 create_field_all (next,type,name, 0, this_file, __LINE__)
1049 /* Like create_field, but the field is only valid when condition COND
1053 create_optional_field_ (pair_p next
, type_p type
, const char *name
,
1054 const char *cond
, int line
)
1057 pair_p union_fields
;
1060 /* Create a fake union type with a single nameless field of type TYPE.
1061 The field has a tag of "1". This allows us to make the presence
1062 of a field of type TYPE depend on some boolean "desc" being true. */
1063 union_fields
= create_field (NULL
, type
, "");
1065 create_string_option (union_fields
->opt
, "dot", "");
1067 create_string_option (union_fields
->opt
, "tag", "1");
1069 new_structure (xasprintf ("%s_%d", "fake_union", id
++), TYPE_UNION
,
1070 &lexer_line
, union_fields
, NULL
, NULL
);
1072 /* Create the field and give it the new fake union type. Add a "desc"
1073 tag that specifies the condition under which the field is valid. */
1074 return create_field_all (next
, union_type
, name
,
1075 create_string_option (0, "desc", cond
),
1079 #define create_optional_field(next,type,name,cond) \
1080 create_optional_field_(next,type,name,cond,__LINE__)
1082 /* Reverse a linked list of 'struct pair's in place. */
1084 nreverse_pairs (pair_p list
)
1086 pair_p prev
= 0, p
, next
;
1087 for (p
= list
; p
; p
= next
)
1097 /* We don't care how long a CONST_DOUBLE is. */
1098 #define CONST_DOUBLE_FORMAT "ww"
1099 /* We don't want to see codes that are only for generator files. */
1100 #undef GENERATOR_FILE
1104 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1110 static const char *const rtx_name
[NUM_RTX_CODE
] = {
1111 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1116 static const char *const rtx_format
[NUM_RTX_CODE
] = {
1117 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1122 static int rtx_next_new
[NUM_RTX_CODE
];
1124 /* We also need codes and names for insn notes (not register notes).
1125 Note that we do *not* bias the note values here. */
1128 #define DEF_INSN_NOTE(NAME) NAME,
1129 #include "insn-notes.def"
1130 #undef DEF_INSN_NOTE
1135 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1136 default field for line number notes. */
1137 static const char *const note_insn_name
[NOTE_INSN_MAX
+ 1] = {
1138 #define DEF_INSN_NOTE(NAME) #NAME,
1139 #include "insn-notes.def"
1140 #undef DEF_INSN_NOTE
1143 #undef CONST_DOUBLE_FORMAT
1144 #define GENERATOR_FILE
1146 /* Generate the contents of the rtx_next array. This really doesn't belong
1147 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1153 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1157 rtx_next_new
[i
] = -1;
1158 if (strncmp (rtx_format
[i
], "uu", 2) == 0)
1159 rtx_next_new
[i
] = 1;
1160 else if (i
== COND_EXEC
|| i
== SET
|| i
== EXPR_LIST
|| i
== INSN_LIST
)
1161 rtx_next_new
[i
] = 1;
1163 for (k
= strlen (rtx_format
[i
]) - 1; k
>= 0; k
--)
1164 if (rtx_format
[i
][k
] == 'e' || rtx_format
[i
][k
] == 'u')
1165 rtx_next_new
[i
] = k
;
1169 /* Write out the contents of the rtx_next array. */
1171 write_rtx_next (void)
1173 outf_p f
= get_output_file_with_visibility (NULL
);
1178 oprintf (f
, "\n/* Used to implement the RTX_NEXT macro. */\n");
1179 oprintf (f
, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1180 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1181 if (rtx_next_new
[i
] == -1)
1182 oprintf (f
, " 0,\n");
1185 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new
[i
]);
1186 oprintf (f
, "};\n");
1189 /* Handle `special("rtx_def")'. This is a special case for field
1190 `fld' of struct rtx_def, which is an array of unions whose values
1191 are based in a complex way on the type of RTL. */
1194 adjust_field_rtx_def (type_p t
, options_p
ARG_UNUSED (opt
))
1199 type_p rtx_tp
, rtvec_tp
, tree_tp
, mem_attrs_tp
, note_union_tp
, scalar_tp
;
1200 type_p basic_block_tp
, reg_attrs_tp
, constant_tp
, symbol_union_tp
;
1202 if (t
->kind
!= TYPE_UNION
)
1204 error_at_line (&lexer_line
,
1205 "special `rtx_def' must be applied to a union");
1206 return &string_type
;
1209 nodot
= create_string_option (NULL
, "dot", "");
1211 rtx_tp
= create_pointer (find_structure ("rtx_def", TYPE_STRUCT
));
1212 rtvec_tp
= create_pointer (find_structure ("rtvec_def", TYPE_STRUCT
));
1213 tree_tp
= create_pointer (find_structure ("tree_node", TYPE_UNION
));
1214 mem_attrs_tp
= create_pointer (find_structure ("mem_attrs", TYPE_STRUCT
));
1216 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT
));
1218 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT
));
1220 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT
));
1221 scalar_tp
= &scalar_nonchar
; /* rtunion int */
1224 pair_p note_flds
= NULL
;
1227 for (c
= 0; c
<= NOTE_INSN_MAX
; c
++)
1232 case NOTE_INSN_DELETED_LABEL
:
1233 case NOTE_INSN_DELETED_DEBUG_LABEL
:
1234 note_flds
= create_field (note_flds
, &string_type
, "rt_str");
1237 case NOTE_INSN_BLOCK_BEG
:
1238 case NOTE_INSN_BLOCK_END
:
1239 note_flds
= create_field (note_flds
, tree_tp
, "rt_tree");
1242 case NOTE_INSN_VAR_LOCATION
:
1243 case NOTE_INSN_CALL_ARG_LOCATION
:
1244 note_flds
= create_field (note_flds
, rtx_tp
, "rt_rtx");
1248 note_flds
= create_field (note_flds
, scalar_tp
, "rt_int");
1251 /* NOTE_INSN_MAX is used as the default field for line
1253 if (c
== NOTE_INSN_MAX
)
1255 create_string_option (nodot
, "default", "");
1258 create_string_option (nodot
, "tag", note_insn_name
[c
]);
1260 note_union_tp
= new_structure ("rtx_def_note_subunion", TYPE_UNION
,
1261 &lexer_line
, note_flds
, NULL
, NULL
);
1263 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1266 sym_flds
= create_field (NULL
, tree_tp
, "rt_tree");
1267 sym_flds
->opt
= create_string_option (nodot
, "default", "");
1268 sym_flds
= create_field (sym_flds
, constant_tp
, "rt_constant");
1269 sym_flds
->opt
= create_string_option (nodot
, "tag", "1");
1270 symbol_union_tp
= new_structure ("rtx_def_symbol_subunion", TYPE_UNION
,
1271 &lexer_line
, sym_flds
, NULL
, NULL
);
1273 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1275 pair_p subfields
= NULL
;
1276 size_t aindex
, nmindex
;
1281 for (aindex
= 0; aindex
< strlen (rtx_format
[i
]); aindex
++)
1284 const char *subname
;
1286 switch (rtx_format
[i
][aindex
])
1297 if (i
== MEM
&& aindex
== 1)
1298 t
= mem_attrs_tp
, subname
= "rt_mem";
1299 else if (i
== JUMP_INSN
&& aindex
== 7)
1300 t
= rtx_tp
, subname
= "rt_rtx";
1301 else if (i
== CODE_LABEL
&& aindex
== 4)
1302 t
= scalar_tp
, subname
= "rt_int";
1303 else if (i
== CODE_LABEL
&& aindex
== 3)
1304 t
= rtx_tp
, subname
= "rt_rtx";
1305 else if (i
== LABEL_REF
&& (aindex
== 1 || aindex
== 2))
1306 t
= rtx_tp
, subname
= "rt_rtx";
1307 else if (i
== NOTE
&& aindex
== 3)
1308 t
= note_union_tp
, subname
= "";
1309 else if (i
== NOTE
&& aindex
== 4)
1310 t
= scalar_tp
, subname
= "rt_int";
1311 else if (i
== NOTE
&& aindex
>= 6)
1312 t
= scalar_tp
, subname
= "rt_int";
1313 else if (i
== ADDR_DIFF_VEC
&& aindex
== 4)
1314 t
= scalar_tp
, subname
= "rt_int";
1315 else if (i
== VALUE
&& aindex
== 0)
1316 t
= scalar_tp
, subname
= "rt_int";
1317 else if (i
== DEBUG_EXPR
&& aindex
== 0)
1318 t
= tree_tp
, subname
= "rt_tree";
1319 else if (i
== REG
&& aindex
== 1)
1320 t
= reg_attrs_tp
, subname
= "rt_reg";
1321 else if (i
== SYMBOL_REF
&& aindex
== 1)
1322 t
= symbol_union_tp
, subname
= "";
1323 else if (i
== JUMP_TABLE_DATA
&& aindex
>= 4)
1324 t
= scalar_tp
, subname
= "rt_int";
1325 else if (i
== BARRIER
&& aindex
>= 2)
1326 t
= scalar_tp
, subname
= "rt_int";
1327 else if (i
== ENTRY_VALUE
&& aindex
== 0)
1328 t
= rtx_tp
, subname
= "rt_rtx";
1333 "rtx type `%s' has `0' in position %lu, can't handle",
1334 rtx_name
[i
], (unsigned long) aindex
);
1356 subname
= "rt_rtvec";
1361 subname
= "rt_tree";
1372 "rtx type `%s' has `%c' in position %lu, can't handle",
1373 rtx_name
[i
], rtx_format
[i
][aindex
],
1374 (unsigned long) aindex
);
1380 subfields
= create_field (subfields
, t
,
1381 xasprintf (".fld[%lu].%s",
1382 (unsigned long) aindex
,
1384 subfields
->opt
= nodot
;
1385 if (t
== note_union_tp
)
1387 create_string_option (subfields
->opt
, "desc",
1389 if (t
== symbol_union_tp
)
1391 create_string_option (subfields
->opt
, "desc",
1392 "CONSTANT_POOL_ADDRESS_P (&%0)");
1395 if (i
== SYMBOL_REF
)
1397 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1399 type_p field_tp
= find_structure ("block_symbol", TYPE_STRUCT
);
1401 = create_optional_field (subfields
, field_tp
, "block_sym",
1402 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1405 sname
= xasprintf ("rtx_def_%s", rtx_name
[i
]);
1406 substruct
= new_structure (sname
, TYPE_STRUCT
, &lexer_line
, subfields
,
1409 ftag
= xstrdup (rtx_name
[i
]);
1410 for (nmindex
= 0; nmindex
< strlen (ftag
); nmindex
++)
1411 ftag
[nmindex
] = TOUPPER (ftag
[nmindex
]);
1412 flds
= create_field (flds
, substruct
, "");
1413 flds
->opt
= create_string_option (nodot
, "tag", ftag
);
1415 return new_structure ("rtx_def_subunion", TYPE_UNION
, &lexer_line
, flds
,
1419 /* Handle `special("tree_exp")'. This is a special case for
1420 field `operands' of struct tree_exp, which although it claims to contain
1421 pointers to trees, actually sometimes contains pointers to RTL too.
1422 Passed T, the old type of the field, and OPT its options. Returns
1423 a new type for the field. */
1426 adjust_field_tree_exp (type_p t
, options_p opt ATTRIBUTE_UNUSED
)
1431 if (t
->kind
!= TYPE_ARRAY
)
1433 error_at_line (&lexer_line
,
1434 "special `tree_exp' must be applied to an array");
1435 return &string_type
;
1438 nodot
= create_string_option (NULL
, "dot", "");
1440 flds
= create_field (NULL
, t
, "");
1441 flds
->opt
= create_string_option (nodot
, "length",
1442 "TREE_OPERAND_LENGTH ((tree) &%0)");
1443 flds
->opt
= create_string_option (flds
->opt
, "default", "");
1445 return new_structure ("tree_exp_subunion", TYPE_UNION
, &lexer_line
, flds
,
1449 /* Perform any special processing on a type T, about to become the type
1450 of a field. Return the appropriate type for the field.
1452 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1453 - Similarly for arrays of pointer-to-char;
1454 - Converts structures for which a parameter is provided to
1456 - Handles "special" options.
1460 adjust_field_type (type_p t
, options_p opt
)
1463 const int pointer_p
= t
->kind
== TYPE_POINTER
;
1464 type_p params
[NUM_PARAM
];
1468 for (i
= 0; i
< NUM_PARAM
; i
++)
1471 for (; opt
; opt
= opt
->next
)
1472 if (strcmp (opt
->name
, "length") == 0)
1475 error_at_line (&lexer_line
, "duplicate `%s' option", opt
->name
);
1476 if (t
->u
.p
->kind
== TYPE_SCALAR
|| t
->u
.p
->kind
== TYPE_STRING
)
1478 error_at_line (&lexer_line
,
1479 "option `%s' may not be applied to "
1480 "arrays of atomic types", opt
->name
);
1484 else if ((strcmp (opt
->name
, "param_is") == 0
1485 || (strncmp (opt
->name
, "param", 5) == 0
1486 && ISDIGIT (opt
->name
[5])
1487 && strcmp (opt
->name
+ 6, "_is") == 0))
1488 && opt
->kind
== OPTION_TYPE
)
1490 int num
= ISDIGIT (opt
->name
[5]) ? opt
->name
[5] - '0' : 0;
1492 if (!union_or_struct_p (t
)
1493 && (t
->kind
!= TYPE_POINTER
|| !union_or_struct_p (t
->u
.p
)))
1495 error_at_line (&lexer_line
,
1496 "option `%s' may only be applied to structures or structure pointers",
1502 if (params
[num
] != NULL
)
1503 error_at_line (&lexer_line
, "duplicate `%s' option", opt
->name
);
1504 if (!ISDIGIT (opt
->name
[5]))
1505 params
[num
] = create_pointer (opt
->info
.type
);
1507 params
[num
] = opt
->info
.type
;
1509 else if (strcmp (opt
->name
, "special") == 0
1510 && opt
->kind
== OPTION_STRING
)
1512 const char *special_name
= opt
->info
.string
;
1513 if (strcmp (special_name
, "tree_exp") == 0)
1514 t
= adjust_field_tree_exp (t
, opt
);
1515 else if (strcmp (special_name
, "rtx_def") == 0)
1516 t
= adjust_field_rtx_def (t
, opt
);
1518 error_at_line (&lexer_line
, "unknown special `%s'", special_name
);
1527 realt
= find_param_structure (t
, params
);
1528 t
= pointer_p
? create_pointer (realt
) : realt
;
1532 && pointer_p
&& t
->u
.p
->kind
== TYPE_SCALAR
&& t
->u
.p
->u
.scalar_is_char
)
1533 return &string_type
;
1534 if (t
->kind
== TYPE_ARRAY
&& t
->u
.a
.p
->kind
== TYPE_POINTER
1535 && t
->u
.a
.p
->u
.p
->kind
== TYPE_SCALAR
1536 && t
->u
.a
.p
->u
.p
->u
.scalar_is_char
)
1537 return create_array (&string_type
, t
->u
.a
.len
);
1543 static void set_gc_used_type (type_p
, enum gc_used_enum
, type_p
*,
1545 static void set_gc_used (pair_p
);
1547 /* Handle OPT for set_gc_used_type. */
1550 process_gc_options (options_p opt
, enum gc_used_enum level
, int *maybe_undef
,
1551 int *pass_param
, int *length
, int *skip
,
1555 for (o
= opt
; o
; o
= o
->next
)
1556 if (strcmp (o
->name
, "ptr_alias") == 0 && level
== GC_POINTED_TO
1557 && o
->kind
== OPTION_TYPE
)
1558 set_gc_used_type (o
->info
.type
,
1559 GC_POINTED_TO
, NULL
);
1560 else if (strcmp (o
->name
, "maybe_undef") == 0)
1562 else if (strcmp (o
->name
, "use_params") == 0)
1564 else if (strcmp (o
->name
, "length") == 0)
1566 else if (strcmp (o
->name
, "skip") == 0)
1568 else if (strcmp (o
->name
, "nested_ptr") == 0
1569 && o
->kind
== OPTION_NESTED
)
1570 *nested_ptr
= ((const struct nested_ptr_data
*) o
->info
.nested
)->type
;
1574 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1576 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1577 are set to GC_UNUSED. Otherwise, an error is emitted for
1578 TYPE_UNDEFINED types. This is used to support user-defined
1579 template types with non-type arguments.
1581 For instance, when we parse a template type with enum arguments
1582 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1583 artificial fields for 'MyType', one for 'AnotherType', the other
1584 one for 'EnumValue'.
1586 At the time that we parse this type we don't know that 'EnumValue'
1587 is really an enum value, so the parser creates a TYPE_UNDEFINED
1588 type for it. Since 'EnumValue' is never resolved to a known
1589 structure, it will stay with TYPE_UNDEFINED.
1591 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1592 'EnumValue'. Generating marking code for it would cause
1593 compilation failures since the marking routines assumes that
1594 'EnumValue' is a type. */
1597 set_gc_used_type (type_p t
, enum gc_used_enum level
, type_p param
[NUM_PARAM
],
1598 bool allow_undefined_types
)
1600 if (t
->gc_used
>= level
)
1609 case TYPE_USER_STRUCT
:
1614 bool allow_undefined_field_types
= (t
->kind
== TYPE_USER_STRUCT
);
1616 process_gc_options (t
->u
.s
.opt
, level
, &dummy
, &dummy
, &dummy
, &dummy
,
1619 if (t
->u
.s
.base_class
)
1620 set_gc_used_type (t
->u
.s
.base_class
, level
, param
,
1621 allow_undefined_types
);
1622 /* Anything pointing to a base class might actually be pointing
1624 for (type_p subclass
= t
->u
.s
.first_subclass
; subclass
;
1625 subclass
= subclass
->u
.s
.next_sibling_class
)
1626 set_gc_used_type (subclass
, level
, param
,
1627 allow_undefined_types
);
1629 FOR_ALL_INHERITED_FIELDS(t
, f
)
1631 int maybe_undef
= 0;
1635 type_p nested_ptr
= NULL
;
1636 process_gc_options (f
->opt
, level
, &maybe_undef
, &pass_param
,
1637 &length
, &skip
, &nested_ptr
);
1639 if (nested_ptr
&& f
->type
->kind
== TYPE_POINTER
)
1640 set_gc_used_type (nested_ptr
, GC_POINTED_TO
,
1641 pass_param
? param
: NULL
);
1642 else if (length
&& f
->type
->kind
== TYPE_POINTER
)
1643 set_gc_used_type (f
->type
->u
.p
, GC_USED
, NULL
);
1644 else if (maybe_undef
&& f
->type
->kind
== TYPE_POINTER
)
1645 set_gc_used_type (f
->type
->u
.p
, GC_MAYBE_POINTED_TO
, NULL
);
1646 else if (pass_param
&& f
->type
->kind
== TYPE_POINTER
&& param
)
1647 set_gc_used_type (find_param_structure (f
->type
->u
.p
, param
),
1648 GC_POINTED_TO
, NULL
);
1650 ; /* target type is not used through this field */
1652 set_gc_used_type (f
->type
, GC_USED
, pass_param
? param
: NULL
,
1653 allow_undefined_field_types
);
1658 case TYPE_UNDEFINED
:
1659 if (level
> GC_UNUSED
)
1661 if (!allow_undefined_types
)
1662 error_at_line (&t
->u
.s
.line
, "undefined type `%s'", t
->u
.s
.tag
);
1663 t
->gc_used
= GC_UNUSED
;
1668 set_gc_used_type (t
->u
.p
, GC_POINTED_TO
, NULL
);
1672 set_gc_used_type (t
->u
.a
.p
, GC_USED
, param
);
1675 case TYPE_LANG_STRUCT
:
1676 for (t
= t
->u
.s
.lang_struct
; t
; t
= t
->next
)
1677 set_gc_used_type (t
, level
, param
);
1680 case TYPE_PARAM_STRUCT
:
1683 for (i
= 0; i
< NUM_PARAM
; i
++)
1684 if (t
->u
.param_struct
.param
[i
] != 0)
1685 set_gc_used_type (t
->u
.param_struct
.param
[i
], GC_USED
, NULL
);
1687 if (t
->u
.param_struct
.stru
->gc_used
== GC_POINTED_TO
)
1688 level
= GC_POINTED_TO
;
1691 t
->u
.param_struct
.stru
->gc_used
= GC_UNUSED
;
1692 set_gc_used_type (t
->u
.param_struct
.stru
, level
,
1693 t
->u
.param_struct
.param
);
1701 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1704 set_gc_used (pair_p variables
)
1708 for (p
= variables
; p
; p
= p
->next
)
1710 set_gc_used_type (p
->type
, GC_USED
, NULL
);
1713 if (verbosity_level
>= 2)
1714 printf ("%s used %d GTY-ed variables\n", progname
, nbvars
);
1717 /* File mapping routines. For each input file, there is one output .c file
1718 (but some output files have many input files), and there is one .h file
1719 for the whole build. */
1721 /* Output file handling. */
1723 /* Create and return an outf_p for a new file for NAME, to be called
1727 create_file (const char *name
, const char *oname
)
1729 static const char *const hdr
[] = {
1730 " Copyright (C) 2004-2014 Free Software Foundation, Inc.\n",
1732 "This file is part of GCC.\n",
1734 "GCC is free software; you can redistribute it and/or modify it under\n",
1735 "the terms of the GNU General Public License as published by the Free\n",
1736 "Software Foundation; either version 3, or (at your option) any later\n",
1739 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1740 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1741 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1742 "for more details.\n",
1744 "You should have received a copy of the GNU General Public License\n",
1745 "along with GCC; see the file COPYING3. If not see\n",
1746 "<http://www.gnu.org/licenses/>. */\n",
1748 "/* This file is machine generated. Do not edit. */\n"
1753 gcc_assert (name
!= NULL
);
1754 gcc_assert (oname
!= NULL
);
1755 f
= XCNEW (struct outf
);
1756 f
->next
= output_files
;
1760 oprintf (f
, "/* Type information for %s.\n", name
);
1761 for (i
= 0; i
< ARRAY_SIZE (hdr
); i
++)
1762 oprintf (f
, "%s", hdr
[i
]);
1766 /* Print, like fprintf, to O.
1767 N.B. You might think this could be implemented more efficiently
1768 with vsnprintf(). Unfortunately, there are C libraries that
1769 provide that function but without the C99 semantics for its return
1770 value, making it impossible to know how much space is required. */
1772 oprintf (outf_p o
, const char *format
, ...)
1778 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1783 va_start (ap
, format
);
1784 slength
= vasprintf (&s
, format
, ap
);
1785 if (s
== NULL
|| (int) slength
< 0)
1786 fatal ("out of memory");
1789 if (o
->bufused
+ slength
> o
->buflength
)
1791 size_t new_len
= o
->buflength
;
1798 while (o
->bufused
+ slength
>= new_len
);
1799 o
->buf
= XRESIZEVEC (char, o
->buf
, new_len
);
1800 o
->buflength
= new_len
;
1802 memcpy (o
->buf
+ o
->bufused
, s
, slength
);
1803 o
->bufused
+= slength
;
1807 /* Open the global header file and the language-specific header files. */
1810 open_base_files (void)
1814 if (nb_plugin_files
> 0 && plugin_files
)
1817 header_file
= create_file ("GCC", "gtype-desc.h");
1819 base_files
= XNEWVEC (outf_p
, num_lang_dirs
);
1821 for (i
= 0; i
< num_lang_dirs
; i
++)
1822 base_files
[i
] = create_file (lang_dir_names
[i
],
1823 xasprintf ("gtype-%s.h", lang_dir_names
[i
]));
1825 /* gtype-desc.c is a little special, so we create it here. */
1827 /* The order of files here matters very much. */
1828 static const char *const ifiles
[] = {
1829 "config.h", "system.h", "coretypes.h", "tm.h",
1830 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1831 "tree.h", "rtl.h", "wide-int.h", "hashtab.h", "hash-set.h", "vec.h",
1832 "machmode.h", "tm.h", "hard-reg-set.h", "input.h", "predict.h",
1833 "function.h", "insn-config.h", "expr.h", "alloc-pool.h",
1834 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1835 "optabs.h", "libfuncs.h", "debug.h", "ggc.h",
1836 "hash-table.h", "vec.h", "ggc.h", "dominance.h", "cfg.h", "basic-block.h",
1837 "tree-ssa-alias.h", "internal-fn.h", "gimple-fold.h", "tree-eh.h",
1838 "gimple-expr.h", "is-a.h",
1839 "gimple.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
1840 "tree-phinodes.h", "ssa-iterators.h", "stringpool.h", "tree-ssanames.h",
1841 "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h", "tree-ssa-loop-manip.h",
1842 "tree-ssa-loop-niter.h", "tree-into-ssa.h", "tree-dfa.h",
1843 "tree-ssa.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1844 "except.h", "output.h", "cfgloop.h", "target.h", "lto-streamer.h",
1845 "target-globals.h", "ipa-ref.h", "cgraph.h", "ipa-prop.h",
1846 "ipa-inline.h", "dwarf2out.h", NULL
1848 const char *const *ifp
;
1849 outf_p gtype_desc_c
;
1851 gtype_desc_c
= create_file ("GCC", "gtype-desc.c");
1852 for (ifp
= ifiles
; *ifp
; ifp
++)
1853 oprintf (gtype_desc_c
, "#include \"%s\"\n", *ifp
);
1855 /* Make sure we handle "cfun" specially. */
1856 oprintf (gtype_desc_c
, "\n/* See definition in function.h. */\n");
1857 oprintf (gtype_desc_c
, "#undef cfun\n");
1859 oprintf (gtype_desc_c
,
1861 "/* Types with a \"gcc::\" namespace have it stripped\n"
1862 " during gengtype parsing. Provide a \"using\" directive\n"
1863 " to ensure that the fully-qualified types are found. */\n"
1864 "using namespace gcc;\n");
1868 /* For INPF an input file, return the real basename of INPF, with all
1869 the directory components skipped. */
1872 get_file_realbasename (const input_file
*inpf
)
1874 return lbasename (get_input_file_name (inpf
));
1877 /* For INPF a filename, return the relative path to INPF from
1878 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1881 get_file_srcdir_relative_path (const input_file
*inpf
)
1883 const char *f
= get_input_file_name (inpf
);
1884 if (strlen (f
) > srcdir_len
1885 && IS_DIR_SEPARATOR (f
[srcdir_len
])
1886 && strncmp (f
, srcdir
, srcdir_len
) == 0)
1887 return f
+ srcdir_len
+ 1;
1892 /* For INPF an input_file, return the relative path to INPF from
1893 $(srcdir) if the latter is a prefix in INPF, or the real basename
1894 of INPF otherwise. */
1897 get_file_basename (const input_file
*inpf
)
1899 const char *srcdir_path
= get_file_srcdir_relative_path (inpf
);
1901 return (srcdir_path
!= NULL
) ? srcdir_path
: get_file_realbasename (inpf
);
1904 /* For F a filename, return the lang_dir_names relative index of the language
1905 directory that is a prefix in F, if any, -1 otherwise. */
1908 get_prefix_langdir_index (const char *f
)
1910 size_t f_len
= strlen (f
);
1913 for (lang_index
= 0; lang_index
< num_lang_dirs
; lang_index
++)
1915 const char *langdir
= lang_dir_names
[lang_index
];
1916 size_t langdir_len
= strlen (langdir
);
1918 if (f_len
> langdir_len
1919 && IS_DIR_SEPARATOR (f
[langdir_len
])
1920 && memcmp (f
, langdir
, langdir_len
) == 0)
1927 /* For INPF an input file, return the name of language directory where
1928 F is located, if any, NULL otherwise. */
1931 get_file_langdir (const input_file
*inpf
)
1933 /* Get the relative path to INPF from $(srcdir) and find the
1934 language by comparing the prefix with language directory names.
1935 If INPF is not even srcdir relative, no point in looking
1939 const char *srcdir_relative_path
= get_file_srcdir_relative_path (inpf
);
1942 if (!srcdir_relative_path
)
1945 lang_index
= get_prefix_langdir_index (srcdir_relative_path
);
1946 if (lang_index
< 0 && strncmp (srcdir_relative_path
, "c-family", 8) == 0)
1948 else if (lang_index
>= 0)
1949 r
= lang_dir_names
[lang_index
];
1956 /* The gt- output file name for INPF. */
1959 get_file_gtfilename (const input_file
*inpf
)
1961 /* Cook up an initial version of the gt- file name from the file real
1962 basename and the language name, if any. */
1964 const char *basename
= get_file_realbasename (inpf
);
1965 const char *langdir
= get_file_langdir (inpf
);
1968 (langdir
? xasprintf ("gt-%s-%s", langdir
, basename
)
1969 : xasprintf ("gt-%s", basename
));
1971 /* Then replace all non alphanumerics characters by '-' and change the
1972 extension to ".h". We expect the input filename extension was at least
1973 one character long. */
1977 for (; *s
!= '.'; s
++)
1978 if (!ISALNUM (*s
) && *s
!= '-')
1981 memcpy (s
, ".h", sizeof (".h"));
1986 /* Each input_file has its associated output file outf_p. The
1987 association is computed by the function
1988 get_output_file_with_visibility. The associated file is cached
1989 inside input_file in its inpoutf field, so is really computed only
1990 once. Associated output file paths (i.e. output_name-s) are
1991 computed by a rule based regexp machinery, using the files_rules
1992 array of struct file_rule_st. A for_name is also computed, giving
1993 the source file name for which the output_file is generated; it is
1994 often the last component of the input_file path. */
1998 Regexpr machinery to compute the output_name and for_name-s of each
1999 input_file. We have a sequence of file rules which gives the POSIX
2000 extended regular expression to match an input file path, and two
2001 transformed strings for the corresponding output_name and the
2002 corresponding for_name. The transformed string contain dollars: $0
2003 is replaced by the entire match, $1 is replaced by the substring
2004 matching the first parenthesis in the regexp, etc. And $$ is replaced
2005 by a single verbatim dollar. The rule order is important. The
2006 general case is last, and the particular cases should come before.
2007 An action routine can, when needed, update the out_name & for_name
2008 and/or return the appropriate output file. It is invoked only when a
2009 rule is triggered. When a rule is triggered, the output_name and
2010 for_name are computed using their transform string in while $$, $0,
2011 $1, ... are suitably replaced. If there is an action, it is called.
2012 In some few cases, the action can directly return the outf_p, but
2013 usually it just updates the output_name and for_name so should free
2014 them before replacing them. The get_output_file_with_visibility
2015 function creates an outf_p only once per each output_name, so it
2016 scans the output_files list for previously seen output file names.
2019 /* Signature of actions in file rules. */
2020 typedef outf_p (frul_actionrout_t
) (input_file
*, char**, char**);
2023 struct file_rule_st
{
2024 const char* frul_srcexpr
; /* Source string for regexp. */
2025 int frul_rflags
; /* Flags passed to regcomp, usually
2027 regex_t
* frul_re
; /* Compiled regular expression
2028 obtained by regcomp. */
2029 const char* frul_tr_out
; /* Transformation string for making
2030 * the output_name, with $1 ... $9 for
2031 * subpatterns and $0 for the whole
2032 * matched filename. */
2033 const char* frul_tr_for
; /* Tranformation string for making the
2035 frul_actionrout_t
* frul_action
; /* The action, if non null, is
2036 * called once the rule matches, on
2037 * the transformed out_name &
2038 * for_name. It could change them
2039 * and/or give the output file. */
2042 /* File rule action handling *.h files. */
2043 static outf_p
header_dot_h_frul (input_file
*, char**, char**);
2045 /* File rule action handling *.c files. */
2046 static outf_p
source_dot_c_frul (input_file
*, char**, char**);
2048 #define NULL_REGEX (regex_t*)0
2050 /* The prefix in our regexp-s matching the directory. */
2051 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
2053 #define NULL_FRULACT (frul_actionrout_t*)0
2055 /* The array of our rules governing file name generation. Rules order
2056 matters, so change with extreme care! */
2058 struct file_rule_st files_rules
[] = {
2059 /* The general rule assumes that files in subdirectories belong to a
2060 particular front-end, and files not in subdirectories are shared.
2061 The following rules deal with exceptions - files that are in
2062 subdirectories and yet are shared, and files that are top-level,
2063 but are not shared. */
2065 /* the c-family/ source directory is special. */
2066 { DIR_PREFIX_REGEX
"c-family/([[:alnum:]_-]*)\\.c$",
2067 REG_EXTENDED
, NULL_REGEX
,
2068 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT
},
2070 { DIR_PREFIX_REGEX
"c-family/([[:alnum:]_-]*)\\.h$",
2071 REG_EXTENDED
, NULL_REGEX
,
2072 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT
},
2074 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
2075 { DIR_PREFIX_REGEX
"c/c-lang\\.h$",
2076 REG_EXTENDED
, NULL_REGEX
, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT
},
2078 { DIR_PREFIX_REGEX
"c/c-tree\\.h$",
2079 REG_EXTENDED
, NULL_REGEX
, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT
},
2081 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
2082 { DIR_PREFIX_REGEX
"cp/cp-tree\\.h$",
2083 REG_EXTENDED
, NULL_REGEX
,
2084 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT
},
2086 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
2087 { DIR_PREFIX_REGEX
"cp/decl\\.[ch]$",
2088 REG_EXTENDED
, NULL_REGEX
,
2089 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT
},
2091 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
2092 { DIR_PREFIX_REGEX
"cp/name-lookup\\.h$",
2093 REG_EXTENDED
, NULL_REGEX
,
2094 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT
},
2096 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
2097 { DIR_PREFIX_REGEX
"cp/parser\\.h$",
2098 REG_EXTENDED
, NULL_REGEX
,
2099 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT
},
2101 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
2102 { DIR_PREFIX_REGEX
"objc/objc-act\\.h$",
2103 REG_EXTENDED
, NULL_REGEX
,
2104 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT
},
2106 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
2107 { DIR_PREFIX_REGEX
"objc/objc-map\\.h$",
2108 REG_EXTENDED
, NULL_REGEX
,
2109 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT
},
2111 /* General cases. For header *.h and source *.c or *.cc files, we
2112 * need special actions to handle the language. */
2114 /* Source *.c files are using get_file_gtfilename to compute their
2115 output_name and get_file_basename to compute their for_name
2116 through the source_dot_c_frul action. */
2117 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.c$",
2118 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.c", source_dot_c_frul
},
2120 /* Source *.cc files are using get_file_gtfilename to compute their
2121 output_name and get_file_basename to compute their for_name
2122 through the source_dot_c_frul action. */
2123 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.cc$",
2124 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.cc", source_dot_c_frul
},
2126 /* Common header files get "gtype-desc.c" as their output_name,
2127 * while language specific header files are handled specially. So
2128 * we need the header_dot_h_frul action. */
2129 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.h$",
2130 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.h", header_dot_h_frul
},
2132 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.in$",
2133 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.in", NULL_FRULACT
},
2135 /* Mandatory null last entry signaling end of rules. */
2136 {NULL
, 0, NULL_REGEX
, NULL
, NULL
, NULL_FRULACT
}
2139 /* Special file rules action for handling *.h header files. It gives
2140 "gtype-desc.c" for common headers and corresponding output
2141 files for language-specific header files. */
2143 header_dot_h_frul (input_file
* inpf
, char**poutname
,
2144 char**pforname ATTRIBUTE_UNUSED
)
2146 const char *basename
= 0;
2148 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2149 (void*) inpf
, get_input_file_name (inpf
),
2150 *poutname
, *pforname
);
2151 basename
= get_file_basename (inpf
);
2152 lang_index
= get_prefix_langdir_index (basename
);
2153 DBGPRINTF ("basename %s lang_index %d", basename
, lang_index
);
2155 if (lang_index
>= 0)
2157 /* The header is language specific. Given output_name &
2158 for_name remains unchanged. The base_files array gives the
2160 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2161 (void*) base_files
[lang_index
],
2162 (base_files
[lang_index
])->name
);
2163 return base_files
[lang_index
];
2167 /* The header is common to all front-end languages. So
2168 output_name is "gtype-desc.c" file. The calling function
2169 get_output_file_with_visibility will find its outf_p. */
2171 *poutname
= xstrdup ("gtype-desc.c");
2172 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2173 get_input_file_name (inpf
));
2179 /* Special file rules action for handling *.c source files using
2180 * get_file_gtfilename to compute their output_name and
2181 * get_file_basename to compute their for_name. The output_name is
2182 * gt-<LANG>-<BASE>.h for language specific source files, and
2183 * gt-<BASE>.h for common source files. */
2185 source_dot_c_frul (input_file
* inpf
, char**poutname
, char**pforname
)
2187 char *newbasename
= CONST_CAST (char*, get_file_basename (inpf
));
2188 char *newoutname
= CONST_CAST (char*, get_file_gtfilename (inpf
));
2189 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2190 (void*) inpf
, get_input_file_name (inpf
),
2191 *poutname
, *pforname
);
2192 DBGPRINTF ("newoutname %s", newoutname
);
2193 DBGPRINTF ("newbasename %s", newbasename
);
2196 *poutname
= newoutname
;
2197 *pforname
= newbasename
;
2201 /* Utility function for get_output_file_with_visibility which returns
2202 * a malloc-ed substituted string using TRS on matching of the FILNAM
2203 * file name, using the PMATCH array. */
2205 matching_file_name_substitute (const char *filnam
, regmatch_t pmatch
[10],
2208 struct obstack str_obstack
;
2210 char *rawstr
= NULL
;
2211 const char *pt
= NULL
;
2212 DBGPRINTF ("filnam %s", filnam
);
2213 obstack_init (&str_obstack
);
2214 for (pt
= trs
; *pt
; pt
++) {
2220 /* A double dollar $$ is substituted by a single verbatim
2221 dollar, but who really uses dollar signs in file
2223 obstack_1grow (&str_obstack
, '$');
2225 else if (ISDIGIT (pt
[1]))
2227 /* Handle $0 $1 ... $9 by appropriate substitution. */
2228 int dolnum
= pt
[1] - '0';
2229 int so
= pmatch
[dolnum
].rm_so
;
2230 int eo
= pmatch
[dolnum
].rm_eo
;
2231 DBGPRINTF ("so=%d eo=%d dolnum=%d", so
, eo
, dolnum
);
2232 if (so
>=0 && eo
>=so
)
2233 obstack_grow (&str_obstack
, filnam
+ so
, eo
- so
);
2237 /* This can happen only when files_rules is buggy! */
2240 /* Always skip the character after the dollar. */
2244 obstack_1grow (&str_obstack
, c
);
2246 obstack_1grow (&str_obstack
, '\0');
2247 rawstr
= XOBFINISH (&str_obstack
, char *);
2248 str
= xstrdup (rawstr
);
2249 obstack_free (&str_obstack
, NULL
);
2250 DBGPRINTF ("matched replacement %s", str
);
2256 /* An output file, suitable for definitions, that can see declarations
2257 made in INPF and is linked into every language that uses INPF.
2258 Since the result is cached inside INPF, that argument cannot be
2259 declared constant, but is "almost" constant. */
2262 get_output_file_with_visibility (input_file
*inpf
)
2265 char *for_name
= NULL
;
2266 char *output_name
= NULL
;
2267 const char* inpfname
;
2269 /* This can happen when we need a file with visibility on a
2270 structure that we've never seen. We have to just hope that it's
2271 globally visible. */
2273 inpf
= system_h_file
;
2275 /* The result is cached in INPF, so return it if already known. */
2277 return inpf
->inpoutf
;
2279 /* In plugin mode, return NULL unless the input_file is one of the
2284 for (i
= 0; i
< nb_plugin_files
; i
++)
2285 if (inpf
== plugin_files
[i
])
2287 inpf
->inpoutf
= plugin_output
;
2288 return plugin_output
;
2294 inpfname
= get_input_file_name (inpf
);
2296 /* Try each rule in sequence in files_rules until one is triggered. */
2299 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2300 (void*) inpf
, inpfname
);
2302 for (; files_rules
[rulix
].frul_srcexpr
!= NULL
; rulix
++)
2304 DBGPRINTF ("rulix#%d srcexpr %s",
2305 rulix
, files_rules
[rulix
].frul_srcexpr
);
2307 if (!files_rules
[rulix
].frul_re
)
2309 /* Compile the regexpr lazily. */
2311 files_rules
[rulix
].frul_re
= XCNEW (regex_t
);
2312 err
= regcomp (files_rules
[rulix
].frul_re
,
2313 files_rules
[rulix
].frul_srcexpr
,
2314 files_rules
[rulix
].frul_rflags
);
2317 /* The regular expression compilation fails only when
2318 file_rules is buggy. */
2326 /* Match the regexpr and trigger the rule if matched. */
2328 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2330 regmatch_t pmatch
[10];
2331 memset (pmatch
, 0, sizeof (pmatch
));
2332 if (!regexec (files_rules
[rulix
].frul_re
,
2333 inpfname
, 10, pmatch
, 0))
2335 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2336 (void*) inpf
, inpfname
, rulix
,
2337 files_rules
[rulix
].frul_srcexpr
);
2339 matching_file_name_substitute (inpfname
, pmatch
,
2340 files_rules
[rulix
].frul_tr_for
);
2341 DBGPRINTF ("for_name %s", for_name
);
2343 matching_file_name_substitute (inpfname
, pmatch
,
2344 files_rules
[rulix
].frul_tr_out
);
2345 DBGPRINTF ("output_name %s", output_name
);
2346 if (files_rules
[rulix
].frul_action
)
2348 /* Invoke our action routine. */
2350 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2351 rulix
, output_name
, for_name
);
2353 (files_rules
[rulix
].frul_action
) (inpf
,
2354 &output_name
, &for_name
);
2355 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2356 rulix
, (void*)of
, output_name
, for_name
);
2357 /* If the action routine returned something, give it back
2358 immediately and cache it in inpf. */
2365 /* The rule matched, and had no action, or that action did
2366 not return any output file but could have changed the
2367 output_name or for_name. We break out of the loop on the
2373 /* The regexpr did not match. */
2374 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2375 rulix
, inpfname
, files_rules
[rulix
].frul_srcexpr
);
2381 if (!output_name
|| !for_name
)
2383 /* This should not be possible, and could only happen if the
2384 files_rules is incomplete or buggy. */
2385 fatal ("failed to compute output name for %s", inpfname
);
2388 /* Look through to see if we've ever seen this output filename
2389 before. If found, cache the result in inpf. */
2390 for (r
= output_files
; r
; r
= r
->next
)
2391 if (filename_cmp (r
->name
, output_name
) == 0)
2394 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r
,
2395 output_name
, for_name
);
2399 /* If not found, create it, and cache it in inpf. */
2400 r
= create_file (for_name
, output_name
);
2402 gcc_assert (r
&& r
->name
);
2403 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r
,
2404 output_name
, for_name
);
2411 /* The name of an output file, suitable for definitions, that can see
2412 declarations made in INPF and is linked into every language that
2416 get_output_file_name (input_file
* inpf
)
2418 outf_p o
= get_output_file_with_visibility (inpf
);
2424 /* Check if existing file is equal to the in memory buffer. */
2427 is_file_equal (outf_p of
)
2429 FILE *newfile
= fopen (of
->name
, "r");
2432 if (newfile
== NULL
)
2436 for (i
= 0; i
< of
->bufused
; i
++)
2439 ch
= fgetc (newfile
);
2440 if (ch
== EOF
|| ch
!= (unsigned char) of
->buf
[i
])
2446 if (equal
&& EOF
!= fgetc (newfile
))
2452 /* Copy the output to its final destination,
2453 but don't unnecessarily change modification times. */
2456 close_output_files (void)
2458 int nbwrittenfiles
= 0;
2461 for (of
= output_files
; of
; of
= of
->next
)
2463 if (!is_file_equal (of
))
2465 FILE *newfile
= NULL
;
2466 char *backupname
= NULL
;
2467 /* Back up the old version of the output file gt-FOO.c as
2468 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2471 backupname
= concat (backup_dir
, "/",
2472 lbasename (of
->name
), "~", NULL
);
2473 if (!access (of
->name
, F_OK
) && rename (of
->name
, backupname
))
2474 fatal ("failed to back up %s as %s: %s",
2475 of
->name
, backupname
, xstrerror (errno
));
2478 newfile
= fopen (of
->name
, "w");
2479 if (newfile
== NULL
)
2480 fatal ("opening output file %s: %s", of
->name
, xstrerror (errno
));
2481 if (fwrite (of
->buf
, 1, of
->bufused
, newfile
) != of
->bufused
)
2482 fatal ("writing output file %s: %s", of
->name
, xstrerror (errno
));
2483 if (fclose (newfile
) != 0)
2484 fatal ("closing output file %s: %s", of
->name
, xstrerror (errno
));
2486 if (verbosity_level
>= 2 && backupname
)
2487 printf ("%s wrote #%-3d %s backed-up in %s\n",
2488 progname
, nbwrittenfiles
, of
->name
, backupname
);
2489 else if (verbosity_level
>= 1)
2490 printf ("%s write #%-3d %s\n", progname
, nbwrittenfiles
, of
->name
);
2495 /* output file remains unchanged. */
2496 if (verbosity_level
>= 2)
2497 printf ("%s keep %s\n", progname
, of
->name
);
2501 of
->bufused
= of
->buflength
= 0;
2503 if (verbosity_level
>= 1)
2504 printf ("%s wrote %d files.\n", progname
, nbwrittenfiles
);
2511 const input_file
* file
;
2515 struct walk_type_data
;
2517 /* For scalars and strings, given the item in 'val'.
2518 For structures, given a pointer to the item in 'val'.
2519 For misc. pointers, given the item in 'val'.
2521 typedef void (*process_field_fn
) (type_p f
, const struct walk_type_data
* p
);
2522 typedef void (*func_name_fn
) (type_p s
, const struct walk_type_data
* p
);
2524 /* Parameters for write_types. */
2526 struct write_types_data
2529 const char *param_prefix
;
2530 const char *subfield_marker_routine
;
2531 const char *marker_routine
;
2532 const char *reorder_note_routine
;
2533 const char *comment
;
2534 int skip_hooks
; /* skip hook generation if non zero */
2535 enum write_types_kinds kind
;
2538 static void output_escaped_param (struct walk_type_data
*d
,
2539 const char *, const char *);
2540 static void output_mangled_typename (outf_p
, const_type_p
);
2541 static void walk_type (type_p t
, struct walk_type_data
*d
);
2542 static void write_func_for_structure (type_p orig_s
, type_p s
, type_p
*param
,
2543 const struct write_types_data
*wtd
);
2544 static void write_types_process_field
2545 (type_p f
, const struct walk_type_data
*d
);
2546 static void write_types (outf_p output_header
,
2548 type_p param_structs
,
2549 const struct write_types_data
*wtd
);
2550 static void write_types_local_process_field
2551 (type_p f
, const struct walk_type_data
*d
);
2552 static void write_local_func_for_structure
2553 (const_type_p orig_s
, type_p s
, type_p
*param
);
2554 static void write_local (outf_p output_header
,
2555 type_p structures
, type_p param_structs
);
2556 static int contains_scalar_p (type_p t
);
2557 static void put_mangled_filename (outf_p
, const input_file
*);
2558 static void finish_root_table (struct flist
*flp
, const char *pfx
,
2559 const char *tname
, const char *lastname
,
2561 static void write_root (outf_p
, pair_p
, type_p
, const char *, int,
2562 struct fileloc
*, const char *, bool);
2563 static void write_array (outf_p f
, pair_p v
,
2564 const struct write_types_data
*wtd
);
2565 static void write_roots (pair_p
, bool);
2567 /* Parameters for walk_type. */
2569 struct walk_type_data
2571 process_field_fn process_field
;
2576 const char *prev_val
[4];
2579 const struct fileloc
*line
;
2584 const char *reorder_fn
;
2586 bool fn_wants_lvalue
;
2594 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2595 pre-processor identifier to use in a #define directive. This replaces
2596 special characters used in C++ identifiers like '>', '<' and ':' with
2599 If no C++ special characters are found in TYPE_NAME, return
2600 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2601 characters replaced with '_'. In this case, the caller is
2602 responsible for freeing the allocated string. */
2605 filter_type_name (const char *type_name
)
2607 if (strchr (type_name
, '<') || strchr (type_name
, ':'))
2610 char *s
= xstrdup (type_name
);
2611 for (i
= 0; i
< strlen (s
); i
++)
2612 if (s
[i
] == '<' || s
[i
] == '>' || s
[i
] == ':' || s
[i
] == ','
2622 /* Print a mangled name representing T to OF. */
2625 output_mangled_typename (outf_p of
, const_type_p t
)
2633 case TYPE_UNDEFINED
:
2638 output_mangled_typename (of
, t
->u
.p
);
2648 case TYPE_LANG_STRUCT
:
2649 case TYPE_USER_STRUCT
:
2651 /* For references to classes within an inheritance hierarchy,
2652 only ever reference the ultimate base class, since only
2653 it will have gt_ functions. */
2654 t
= get_ultimate_base_class (t
);
2655 const char *id_for_tag
= filter_type_name (t
->u
.s
.tag
);
2656 oprintf (of
, "%lu%s", (unsigned long) strlen (id_for_tag
),
2658 if (id_for_tag
!= t
->u
.s
.tag
)
2659 free (CONST_CAST (char *, id_for_tag
));
2662 case TYPE_PARAM_STRUCT
:
2665 for (i
= 0; i
< NUM_PARAM
; i
++)
2666 if (t
->u
.param_struct
.param
[i
] != NULL
)
2667 output_mangled_typename (of
, t
->u
.param_struct
.param
[i
]);
2668 output_mangled_typename (of
, t
->u
.param_struct
.stru
);
2676 /* Print PARAM to D->OF processing escapes. D->VAL references the
2677 current object, D->PREV_VAL the object containing the current
2678 object, ONAME is the name of the option and D->LINE is used to
2679 print error messages. */
2682 output_escaped_param (struct walk_type_data
*d
, const char *param
,
2687 for (p
= param
; *p
; p
++)
2689 oprintf (d
->of
, "%c", *p
);
2694 oprintf (d
->of
, "(%s)", d
->prev_val
[2]);
2697 oprintf (d
->of
, "(%s)", d
->prev_val
[0]);
2700 oprintf (d
->of
, "(%s)", d
->prev_val
[1]);
2704 const char *pp
= d
->val
+ strlen (d
->val
);
2705 while (pp
[-1] == ']')
2708 oprintf (d
->of
, "%s", pp
);
2712 error_at_line (d
->line
, "`%s' option contains bad escape %c%c",
2718 get_string_option (options_p opt
, const char *key
)
2720 for (; opt
; opt
= opt
->next
)
2721 if (strcmp (opt
->name
, key
) == 0)
2722 return opt
->info
.string
;
2726 /* Machinery for avoiding duplicate tags within switch statements. */
2730 struct seen_tag
*next
;
2734 already_seen_tag (struct seen_tag
*seen_tags
, const char *tag
)
2736 /* Linear search, so O(n^2), but n is currently small. */
2739 if (!strcmp (seen_tags
->tag
, tag
))
2741 seen_tags
= seen_tags
->next
;
2743 /* Not yet seen this tag. */
2748 mark_tag_as_seen (struct seen_tag
**seen_tags
, const char *tag
)
2750 /* Add to front of linked list. */
2751 struct seen_tag
*new_node
= XCNEW (struct seen_tag
);
2752 new_node
->tag
= tag
;
2753 new_node
->next
= *seen_tags
;
2754 *seen_tags
= new_node
;
2758 walk_subclasses (type_p base
, struct walk_type_data
*d
,
2759 struct seen_tag
**seen_tags
)
2761 for (type_p sub
= base
->u
.s
.first_subclass
; sub
!= NULL
;
2762 sub
= sub
->u
.s
.next_sibling_class
)
2764 const char *type_tag
= get_string_option (sub
->u
.s
.opt
, "tag");
2765 if (type_tag
&& !already_seen_tag (*seen_tags
, type_tag
))
2767 mark_tag_as_seen (seen_tags
, type_tag
);
2768 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", type_tag
);
2770 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2772 oprintf (d
->of
, "%*s%s *sub = static_cast <%s *> (x);\n",
2773 d
->indent
, "", sub
->u
.s
.tag
, sub
->u
.s
.tag
);
2774 const char *old_val
= d
->val
;
2779 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2780 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
2783 walk_subclasses (sub
, d
, seen_tags
);
2787 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2788 which is of type T. Write code to D->OF to constrain execution (at
2789 the point that D->PROCESS_FIELD is called) to the appropriate
2790 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2791 pointers to those objects. D->PREV_VAL lists the objects
2792 containing the current object, D->OPT is a list of options to
2793 apply, D->INDENT is the current indentation level, D->LINE is used
2794 to print error messages, D->BITMAP indicates which languages to
2795 print the structure for, and D->PARAM is the current parameter
2796 (from an enclosing param_is option). */
2799 walk_type (type_p t
, struct walk_type_data
*d
)
2801 const char *length
= NULL
;
2802 const char *desc
= NULL
;
2803 const char *type_tag
= NULL
;
2804 int maybe_undef_p
= 0;
2805 int use_param_num
= -1;
2806 int use_params_p
= 0;
2809 const struct nested_ptr_data
*nested_ptr_d
= NULL
;
2811 d
->needs_cast_p
= false;
2812 for (oo
= d
->opt
; oo
; oo
= oo
->next
)
2813 if (strcmp (oo
->name
, "length") == 0 && oo
->kind
== OPTION_STRING
)
2814 length
= oo
->info
.string
;
2815 else if (strcmp (oo
->name
, "maybe_undef") == 0)
2817 else if (strncmp (oo
->name
, "use_param", 9) == 0
2818 && (oo
->name
[9] == '\0' || ISDIGIT (oo
->name
[9])))
2819 use_param_num
= oo
->name
[9] == '\0' ? 0 : oo
->name
[9] - '0';
2820 else if (strcmp (oo
->name
, "use_params") == 0)
2822 else if (strcmp (oo
->name
, "desc") == 0 && oo
->kind
== OPTION_STRING
)
2823 desc
= oo
->info
.string
;
2824 else if (strcmp (oo
->name
, "mark_hook") == 0)
2826 else if (strcmp (oo
->name
, "nested_ptr") == 0
2827 && oo
->kind
== OPTION_NESTED
)
2828 nested_ptr_d
= (const struct nested_ptr_data
*) oo
->info
.nested
;
2829 else if (strcmp (oo
->name
, "dot") == 0)
2831 else if (strcmp (oo
->name
, "tag") == 0)
2832 type_tag
= oo
->info
.string
;
2833 else if (strcmp (oo
->name
, "special") == 0)
2835 else if (strcmp (oo
->name
, "skip") == 0)
2837 else if (strcmp (oo
->name
, "atomic") == 0)
2839 else if (strcmp (oo
->name
, "default") == 0)
2841 else if (strcmp (oo
->name
, "param_is") == 0)
2843 else if (strncmp (oo
->name
, "param", 5) == 0
2844 && ISDIGIT (oo
->name
[5]) && strcmp (oo
->name
+ 6, "_is") == 0)
2846 else if (strcmp (oo
->name
, "chain_next") == 0)
2848 else if (strcmp (oo
->name
, "chain_prev") == 0)
2850 else if (strcmp (oo
->name
, "chain_circular") == 0)
2852 else if (strcmp (oo
->name
, "reorder") == 0)
2854 else if (strcmp (oo
->name
, "variable_size") == 0)
2856 else if (strcmp (oo
->name
, "for_user") == 0)
2859 error_at_line (d
->line
, "unknown option `%s'\n", oo
->name
);
2866 int pointer_p
= t
->kind
== TYPE_POINTER
;
2870 if (!union_or_struct_p (t
))
2871 error_at_line (d
->line
, "`use_params' option on unimplemented type");
2873 t
= find_param_structure (t
, d
->param
);
2875 t
= create_pointer (t
);
2878 if (use_param_num
!= -1)
2880 if (d
->param
!= NULL
&& d
->param
[use_param_num
] != NULL
)
2882 type_p nt
= d
->param
[use_param_num
];
2884 if (t
->kind
== TYPE_ARRAY
)
2885 nt
= create_array (nt
, t
->u
.a
.len
);
2886 else if (length
!= NULL
&& t
->kind
== TYPE_POINTER
)
2887 nt
= create_pointer (nt
);
2888 d
->needs_cast_p
= (t
->kind
!= TYPE_POINTER
2889 && (nt
->kind
== TYPE_POINTER
2890 || nt
->kind
== TYPE_STRING
));
2894 error_at_line (d
->line
, "no parameter defined for `%s'", d
->val
);
2898 && (t
->kind
!= TYPE_POINTER
|| !union_or_struct_p (t
->u
.p
)))
2900 error_at_line (d
->line
,
2901 "field `%s' has invalid option `maybe_undef_p'\n",
2906 if (atomic_p
&& (t
->kind
!= TYPE_POINTER
) && (t
->kind
!= TYPE_STRING
))
2908 error_at_line (d
->line
, "field `%s' has invalid option `atomic'\n", d
->val
);
2916 d
->process_field (t
, d
);
2921 d
->in_ptr_field
= true;
2922 if (maybe_undef_p
&& t
->u
.p
->u
.s
.line
.file
== NULL
)
2924 oprintf (d
->of
, "%*sgcc_assert (!%s);\n", d
->indent
, "", d
->val
);
2928 /* If a pointer type is marked as "atomic", we process the
2929 field itself, but we don't walk the data that they point to.
2931 There are two main cases where we walk types: to mark
2932 pointers that are reachable, and to relocate pointers when
2933 writing a PCH file. In both cases, an atomic pointer is
2934 itself marked or relocated, but the memory that it points
2935 to is left untouched. In the case of PCH, that memory will
2936 be read/written unchanged to the PCH file. */
2939 oprintf (d
->of
, "%*sif (%s != NULL) {\n", d
->indent
, "", d
->val
);
2941 d
->process_field (t
, d
);
2943 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2949 if (!union_or_struct_p (t
->u
.p
)
2950 && t
->u
.p
->kind
!= TYPE_PARAM_STRUCT
)
2952 error_at_line (d
->line
,
2953 "field `%s' is pointer to unimplemented type",
2960 const char *oldprevval2
= d
->prev_val
[2];
2962 if (!union_or_struct_p (nested_ptr_d
->type
))
2964 error_at_line (d
->line
,
2965 "field `%s' has invalid "
2966 "option `nested_ptr'\n", d
->val
);
2970 d
->prev_val
[2] = d
->val
;
2971 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2973 d
->val
= xasprintf ("x%d", d
->counter
++);
2974 oprintf (d
->of
, "%*s%s %s * %s%s =\n", d
->indent
, "",
2975 (nested_ptr_d
->type
->kind
== TYPE_UNION
2976 ? "union" : "struct"),
2977 nested_ptr_d
->type
->u
.s
.tag
,
2978 d
->fn_wants_lvalue
? "" : "const ", d
->val
);
2979 oprintf (d
->of
, "%*s", d
->indent
+ 2, "");
2980 output_escaped_param (d
, nested_ptr_d
->convert_from
,
2982 oprintf (d
->of
, ";\n");
2984 d
->process_field (nested_ptr_d
->type
, d
);
2986 if (d
->fn_wants_lvalue
)
2988 oprintf (d
->of
, "%*s%s = ", d
->indent
, "",
2990 d
->prev_val
[2] = d
->val
;
2991 output_escaped_param (d
, nested_ptr_d
->convert_to
,
2993 oprintf (d
->of
, ";\n");
2997 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2998 d
->val
= d
->prev_val
[2];
2999 d
->prev_val
[2] = oldprevval2
;
3002 d
->process_field (t
->u
.p
, d
);
3006 int loopcounter
= d
->loopcounter
;
3007 const char *oldval
= d
->val
;
3008 const char *oldprevval3
= d
->prev_val
[3];
3011 oprintf (d
->of
, "%*sif (%s != NULL) {\n", d
->indent
, "", d
->val
);
3013 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
3014 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != (size_t)(", d
->indent
,
3015 "", loopcounter
, loopcounter
);
3016 if (!d
->in_record_p
)
3017 output_escaped_param (d
, length
, "length");
3019 oprintf (d
->of
, "l%d", loopcounter
);
3020 if (d
->have_this_obj
)
3021 /* Try to unswitch loops (see PR53880). */
3022 oprintf (d
->of
, ") && ((void *)%s == this_obj", oldval
);
3023 oprintf (d
->of
, "); i%d++) {\n", loopcounter
);
3025 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
3027 d
->prev_val
[3] = oldval
;
3028 walk_type (t
->u
.p
, d
);
3031 d
->prev_val
[3] = oldprevval3
;
3034 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3035 d
->process_field (t
, d
);
3037 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3039 d
->in_ptr_field
= false;
3046 const char *oldval
= d
->val
;
3049 /* If it's an array of scalars, we optimize by not generating
3051 if (t
->u
.a
.p
->kind
== TYPE_SCALAR
)
3055 loopcounter
= d
->loopcounter
;
3057 loopcounter
= d
->counter
++;
3059 /* When walking an array, compute the length and store it in a
3060 local variable before walking the array elements, instead of
3061 recomputing the length expression each time through the loop.
3062 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
3063 where the length is stored in the first array element,
3064 because otherwise that operand can get overwritten on the
3066 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
3068 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
3069 if (!d
->in_record_p
|| !length
)
3071 oprintf (d
->of
, "%*ssize_t l%d = (size_t)(",
3072 d
->indent
, "", loopcounter
);
3074 output_escaped_param (d
, length
, "length");
3076 oprintf (d
->of
, "%s", t
->u
.a
.len
);
3077 oprintf (d
->of
, ");\n");
3080 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
3082 loopcounter
, loopcounter
, loopcounter
, loopcounter
);
3084 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
3086 walk_type (t
->u
.a
.p
, d
);
3091 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3093 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3101 const char *oldval
= d
->val
;
3102 const char *oldprevval1
= d
->prev_val
[1];
3103 const char *oldprevval2
= d
->prev_val
[2];
3104 const char *struct_mark_hook
= NULL
;
3105 const int union_p
= t
->kind
== TYPE_UNION
;
3106 int seen_default_p
= 0;
3108 int lengths_seen
= 0;
3110 bool any_length_seen
= false;
3112 if (!t
->u
.s
.line
.file
)
3113 error_at_line (d
->line
, "incomplete structure `%s'", t
->u
.s
.tag
);
3115 if ((d
->bitmap
& t
->u
.s
.bitmap
) != d
->bitmap
)
3117 error_at_line (d
->line
,
3118 "structure `%s' defined for mismatching languages",
3120 error_at_line (&t
->u
.s
.line
, "one structure defined here");
3123 /* Some things may also be defined in the structure's options. */
3124 for (o
= t
->u
.s
.opt
; o
; o
= o
->next
)
3125 if (!desc
&& strcmp (o
->name
, "desc") == 0
3126 && o
->kind
== OPTION_STRING
)
3127 desc
= o
->info
.string
;
3128 else if (!struct_mark_hook
&& strcmp (o
->name
, "mark_hook") == 0
3129 && o
->kind
== OPTION_STRING
)
3130 struct_mark_hook
= o
->info
.string
;
3132 if (struct_mark_hook
)
3133 oprintf (d
->of
, "%*s%s (&%s);\n",
3134 d
->indent
, "", struct_mark_hook
, oldval
);
3136 d
->prev_val
[2] = oldval
;
3137 d
->prev_val
[1] = oldprevval2
;
3142 error_at_line (d
->line
,
3143 "missing `desc' option for union `%s'",
3147 oprintf (d
->of
, "%*sswitch ((int) (", d
->indent
, "");
3148 output_escaped_param (d
, desc
, "desc");
3149 oprintf (d
->of
, "))\n");
3151 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
3155 /* We have a "desc" option on a struct, signifying the
3156 base class within a GC-managed inheritance hierarchy.
3157 The current code specialcases the base class, then walks
3158 into subclasses, recursing into this routine to handle them.
3159 This organization requires the base class to have a case in
3160 the switch statement, and hence a tag value is mandatory
3161 for the base class. This restriction could be removed, but
3162 it would require some restructing of this code. */
3165 error_at_line (d
->line
,
3166 "missing `tag' option for type `%s'",
3169 oprintf (d
->of
, "%*sswitch ((int) (", d
->indent
, "");
3170 output_escaped_param (d
, desc
, "desc");
3171 oprintf (d
->of
, "))\n");
3173 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
3174 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", type_tag
);
3178 FOR_ALL_INHERITED_FIELDS (t
, f
)
3182 const char *fieldlength
= NULL
;
3184 d
->reorder_fn
= NULL
;
3185 for (oo
= f
->opt
; oo
; oo
= oo
->next
)
3186 if (strcmp (oo
->name
, "skip") == 0)
3188 else if (strcmp (oo
->name
, "length") == 0
3189 && oo
->kind
== OPTION_STRING
)
3190 fieldlength
= oo
->info
.string
;
3200 if (!any_length_seen
)
3202 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
3205 any_length_seen
= true;
3207 oprintf (d
->of
, "%*ssize_t l%d = (size_t)(",
3208 d
->indent
, "", d
->counter
- 1);
3209 output_escaped_param (d
, fieldlength
, "length");
3210 oprintf (d
->of
, ");\n");
3214 endcounter
= d
->counter
;
3216 FOR_ALL_INHERITED_FIELDS (t
, f
)
3219 const char *dot
= ".";
3220 const char *tagid
= NULL
;
3223 int use_param_p
= 0;
3224 const char *fieldlength
= NULL
;
3227 d
->reorder_fn
= NULL
;
3228 for (oo
= f
->opt
; oo
; oo
= oo
->next
)
3229 if (strcmp (oo
->name
, "dot") == 0
3230 && oo
->kind
== OPTION_STRING
)
3231 dot
= oo
->info
.string
;
3232 else if (strcmp (oo
->name
, "tag") == 0
3233 && oo
->kind
== OPTION_STRING
)
3234 tagid
= oo
->info
.string
;
3235 else if (strcmp (oo
->name
, "skip") == 0)
3237 else if (strcmp (oo
->name
, "default") == 0)
3239 else if (strcmp (oo
->name
, "reorder") == 0
3240 && oo
->kind
== OPTION_STRING
)
3241 d
->reorder_fn
= oo
->info
.string
;
3242 else if (strncmp (oo
->name
, "use_param", 9) == 0
3243 && (oo
->name
[9] == '\0' || ISDIGIT (oo
->name
[9])))
3245 else if (strcmp (oo
->name
, "length") == 0
3246 && oo
->kind
== OPTION_STRING
)
3247 fieldlength
= oo
->info
.string
;
3252 if (union_p
&& tagid
)
3254 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", tagid
);
3257 else if (union_p
&& default_p
)
3259 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
3263 else if (!union_p
&& (default_p
|| tagid
))
3264 error_at_line (d
->line
,
3265 "can't use `%s' outside a union on field `%s'",
3266 default_p
? "default" : "tag", f
->name
);
3267 else if (union_p
&& !(default_p
|| tagid
)
3268 && f
->type
->kind
== TYPE_SCALAR
)
3271 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3272 get_input_file_name (d
->line
->file
), d
->line
->line
,
3276 else if (union_p
&& !(default_p
|| tagid
))
3277 error_at_line (d
->line
,
3278 "field `%s' is missing `tag' or `default' option",
3283 d
->loopcounter
= endcounter
- lengths_seen
--;
3287 d
->val
= newval
= xasprintf ("%s%s%s", oldval
, dot
, f
->name
);
3289 d
->used_length
= false;
3290 d
->in_record_p
= !union_p
;
3292 if (union_p
&& use_param_p
&& d
->param
== NULL
)
3293 oprintf (d
->of
, "%*sgcc_unreachable ();\n", d
->indent
, "");
3295 walk_type (f
->type
, d
);
3297 d
->in_record_p
= false;
3303 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
3307 d
->reorder_fn
= NULL
;
3310 d
->prev_val
[1] = oldprevval1
;
3311 d
->prev_val
[2] = oldprevval2
;
3313 if (union_p
&& !seen_default_p
)
3315 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
3316 oprintf (d
->of
, "%*s break;\n", d
->indent
, "");
3319 if (desc
&& !union_p
)
3321 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
3326 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3331 /* Add cases to handle subclasses. */
3332 struct seen_tag
*tags
= NULL
;
3333 walk_subclasses (t
, d
, &tags
);
3335 /* Ensure that if someone forgets a "tag" option that we don't
3336 silent fail to traverse that subclass's fields. */
3337 if (!seen_default_p
)
3339 oprintf (d
->of
, "%*s/* Unrecognized tag value. */\n",
3341 oprintf (d
->of
, "%*sdefault: gcc_unreachable (); \n",
3345 /* End of the switch statement */
3346 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3349 if (any_length_seen
)
3352 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3357 case TYPE_LANG_STRUCT
:
3360 for (nt
= t
->u
.s
.lang_struct
; nt
; nt
= nt
->next
)
3361 if ((d
->bitmap
& nt
->u
.s
.bitmap
) == d
->bitmap
)
3364 error_at_line (d
->line
, "structure `%s' differs between languages",
3371 case TYPE_PARAM_STRUCT
:
3373 type_p
*oldparam
= d
->param
;
3375 d
->param
= t
->u
.param_struct
.param
;
3376 walk_type (t
->u
.param_struct
.stru
, d
);
3377 d
->param
= oldparam
;
3381 case TYPE_USER_STRUCT
:
3382 d
->process_field (t
, d
);
3386 case TYPE_UNDEFINED
:
3391 /* process_field routine for marking routines. */
3394 write_types_process_field (type_p f
, const struct walk_type_data
*d
)
3396 const struct write_types_data
*wtd
;
3397 const char *cast
= d
->needs_cast_p
? "(void *)" : "";
3398 wtd
= (const struct write_types_data
*) d
->cookie
;
3403 case TYPE_UNDEFINED
:
3406 oprintf (d
->of
, "%*s%s (%s%s", d
->indent
, "",
3407 wtd
->subfield_marker_routine
, cast
, d
->val
);
3408 if (wtd
->param_prefix
)
3410 if (f
->u
.p
->kind
== TYPE_SCALAR
)
3411 /* The current type is a pointer to a scalar (so not
3412 considered like a pointer to instances of user defined
3413 types) and we are seeing it; it means we must be even
3414 more careful about the second argument of the
3415 SUBFIELD_MARKER_ROUTINE call. That argument must
3416 always be the instance of the type for which
3417 write_func_for_structure was called - this really is
3418 what the function SUBFIELD_MARKER_ROUTINE expects.
3419 That is, it must be an instance of the ORIG_S type
3420 parameter of write_func_for_structure. The convention
3421 is that that argument must be "x" in that case (as set
3422 by write_func_for_structure). The problem is, we can't
3423 count on d->prev_val[3] to be always set to "x" in that
3424 case. Sometimes walk_type can set it to something else
3425 (to e.g cooperate with write_array when called from
3426 write_roots). So let's set it to "x" here then. */
3427 oprintf (d
->of
, ", x");
3429 oprintf (d
->of
, ", %s", d
->prev_val
[3]);
3432 oprintf (d
->of
, ", gt_%s_", wtd
->param_prefix
);
3433 output_mangled_typename (d
->of
, d
->orig_s
);
3436 oprintf (d
->of
, ", gt_%sa_%s", wtd
->param_prefix
, d
->prev_val
[0]);
3438 oprintf (d
->of
, ");\n");
3439 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
3440 oprintf (d
->of
, "%*s%s (%s%s, %s, %s);\n", d
->indent
, "",
3441 wtd
->reorder_note_routine
, cast
, d
->val
,
3442 d
->prev_val
[3], d
->reorder_fn
);
3448 case TYPE_LANG_STRUCT
:
3449 case TYPE_PARAM_STRUCT
:
3450 case TYPE_USER_STRUCT
:
3451 if (f
->kind
== TYPE_USER_STRUCT
&& !d
->in_ptr_field
)
3453 /* If F is a user-defined type and the field is not a
3454 pointer to the type, then we should not generate the
3455 standard pointer-marking code. All we need to do is call
3456 the user-provided marking function to process the fields
3458 oprintf (d
->of
, "%*sgt_%sx (&(%s));\n", d
->indent
, "", wtd
->prefix
,
3463 oprintf (d
->of
, "%*sgt_%s_", d
->indent
, "", wtd
->prefix
);
3464 output_mangled_typename (d
->of
, f
);
3465 oprintf (d
->of
, " (%s%s);\n", cast
, d
->val
);
3466 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
3467 oprintf (d
->of
, "%*s%s (%s%s, %s%s, %s);\n", d
->indent
, "",
3468 wtd
->reorder_note_routine
, cast
, d
->val
, cast
, d
->val
,
3481 /* Return an output file that is suitable for definitions which can
3482 reference struct S */
3485 get_output_file_for_structure (const_type_p s
, type_p
*param
)
3487 const input_file
*fn
;
3490 gcc_assert (union_or_struct_p (s
));
3491 fn
= s
->u
.s
.line
.file
;
3493 /* This is a hack, and not the good kind either. */
3494 for (i
= NUM_PARAM
- 1; i
>= 0; i
--)
3495 if (param
&& param
[i
] && param
[i
]->kind
== TYPE_POINTER
3496 && union_or_struct_p (param
[i
]->u
.p
))
3497 fn
= param
[i
]->u
.p
->u
.s
.line
.file
;
3499 /* The call to get_output_file_with_visibility may update fn by
3500 caching its result inside, so we need the CONST_CAST. */
3501 return get_output_file_with_visibility (CONST_CAST (input_file
*, fn
));
3505 /* Returns the specifier keyword for a string or union type S, empty string
3509 get_type_specifier (const type_p s
)
3511 if (s
->kind
== TYPE_STRUCT
)
3513 else if (s
->kind
== TYPE_LANG_STRUCT
)
3514 return get_type_specifier (s
->u
.s
.lang_struct
);
3515 else if (s
->kind
== TYPE_UNION
)
3521 /* Emits a declaration for type TY (assumed to be a union or a
3522 structure) on stream OUT. */
3525 write_type_decl (outf_p out
, type_p ty
)
3527 if (union_or_struct_p (ty
))
3528 oprintf (out
, "%s%s", get_type_specifier (ty
), ty
->u
.s
.tag
);
3529 else if (ty
->kind
== TYPE_SCALAR
)
3531 if (ty
->u
.scalar_is_char
)
3532 oprintf (out
, "const char");
3534 oprintf (out
, "void");
3536 else if (ty
->kind
== TYPE_POINTER
)
3538 write_type_decl (out
, ty
->u
.p
);
3539 oprintf (out
, " *");
3541 else if (ty
->kind
== TYPE_ARRAY
)
3543 write_type_decl (out
, ty
->u
.a
.p
);
3544 oprintf (out
, " *");
3546 else if (ty
->kind
== TYPE_STRING
)
3548 oprintf (out
, "const char *");
3555 /* Write on OF the name of the marker function for structure S. PREFIX
3556 is the prefix to use (to distinguish ggc from pch markers). */
3559 write_marker_function_name (outf_p of
, type_p s
, const char *prefix
)
3561 if (union_or_struct_p (s
))
3563 const char *id_for_tag
= filter_type_name (s
->u
.s
.tag
);
3564 oprintf (of
, "gt_%sx_%s", prefix
, id_for_tag
);
3565 if (id_for_tag
!= s
->u
.s
.tag
)
3566 free (CONST_CAST (char *, id_for_tag
));
3568 else if (s
->kind
== TYPE_PARAM_STRUCT
)
3570 oprintf (of
, "gt_%s_", prefix
);
3571 output_mangled_typename (of
, s
);
3577 /* Write on OF a user-callable routine to act as an entry point for
3578 the marking routine for S, generated by write_func_for_structure.
3579 WTD distinguishes between ggc and pch markers. */
3582 write_user_func_for_structure_ptr (outf_p of
, type_p s
, const write_types_data
*wtd
)
3584 /* Parameterized structures are not supported in user markers. There
3585 is no way for the marker function to know which specific type
3586 to use to generate the call to the void * entry point. For
3587 instance, a marker for struct htab may need to call different
3588 routines to mark the fields, depending on the paramN_is attributes.
3590 A user-defined marker that accepts 'struct htab' as its argument
3591 would not know which variant to call. Generating several entry
3592 points accepting 'struct htab' would cause multiply-defined
3593 errors during compilation. */
3594 gcc_assert (union_or_struct_p (s
));
3596 type_p alias_of
= NULL
;
3597 for (options_p opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3598 if (strcmp (opt
->name
, "ptr_alias") == 0)
3600 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3601 we do not generate marking code for ORIG_S here. Instead, a
3602 forwarder #define in gtype-desc.h will cause every call to its
3603 marker to call the target of this alias.
3605 However, we still want to create a user entry code for the
3606 aliased type. So, if ALIAS_OF is set, we only generate the
3607 user-callable marker function. */
3608 alias_of
= opt
->info
.type
;
3612 DBGPRINTF ("write_user_func_for_structure_ptr: %s %s", s
->u
.s
.tag
,
3615 /* Only write the function once. */
3616 if (s
->u
.s
.wrote_user_func_for_ptr
[wtd
->kind
])
3618 s
->u
.s
.wrote_user_func_for_ptr
[wtd
->kind
] = true;
3620 oprintf (of
, "\nvoid\n");
3621 oprintf (of
, "gt_%sx (", wtd
->prefix
);
3622 write_type_decl (of
, s
);
3623 oprintf (of
, " *& x)\n");
3624 oprintf (of
, "{\n");
3625 oprintf (of
, " if (x)\n ");
3626 write_marker_function_name (of
,
3627 alias_of
? alias_of
: get_ultimate_base_class (s
),
3629 oprintf (of
, " ((void *) x);\n");
3630 oprintf (of
, "}\n");
3634 /* Write a function to mark all the fields of type S on OF. PREFIX
3635 and D are as in write_user_marking_functions. */
3638 write_user_func_for_structure_body (type_p s
, const char *prefix
,
3639 struct walk_type_data
*d
)
3641 oprintf (d
->of
, "\nvoid\n");
3642 oprintf (d
->of
, "gt_%sx (", prefix
);
3643 write_type_decl (d
->of
, s
);
3644 oprintf (d
->of
, "& x_r ATTRIBUTE_UNUSED)\n");
3645 oprintf (d
->of
, "{\n");
3646 oprintf (d
->of
, " ");
3647 write_type_decl (d
->of
, s
);
3648 oprintf (d
->of
, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3652 oprintf (d
->of
, "}\n");
3655 /* Emit the user-callable functions needed to mark all the types used
3656 by the user structure S. PREFIX is the prefix to use to
3657 distinguish ggc and pch markers. D contains data needed to pass to
3658 walk_type when traversing the fields of a type.
3660 For every type T referenced by S, two routines are generated: one
3661 that takes 'T *', marks the pointer and calls the second routine,
3662 which just marks the fields of T. */
3665 write_user_marking_functions (type_p s
,
3666 const write_types_data
*w
,
3667 struct walk_type_data
*d
)
3669 gcc_assert (s
->kind
== TYPE_USER_STRUCT
);
3671 for (pair_p fld
= s
->u
.s
.fields
; fld
; fld
= fld
->next
)
3673 type_p fld_type
= fld
->type
;
3674 if (fld_type
->kind
== TYPE_POINTER
)
3676 type_p pointed_to_type
= fld_type
->u
.p
;
3677 if (union_or_struct_p (pointed_to_type
))
3678 write_user_func_for_structure_ptr (d
->of
, pointed_to_type
, w
);
3680 else if (union_or_struct_p (fld_type
))
3681 write_user_func_for_structure_body (fld_type
, w
->prefix
, d
);
3686 /* For S, a structure that's part of ORIG_S, and using parameters
3687 PARAM, write out a routine that:
3688 - Takes a parameter, a void * but actually of type *S
3689 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3690 field of S or its substructures and (in some cases) things
3691 that are pointed to by S. */
3694 write_func_for_structure (type_p orig_s
, type_p s
, type_p
*param
,
3695 const struct write_types_data
*wtd
)
3697 const char *chain_next
= NULL
;
3698 const char *chain_prev
= NULL
;
3699 const char *chain_circular
= NULL
;
3700 const char *mark_hook_name
= NULL
;
3702 struct walk_type_data d
;
3704 if (s
->u
.s
.base_class
)
3706 /* Verify that the base class has a "desc", since otherwise
3707 the traversal hooks there won't attempt to visit fields of
3708 subclasses such as this one. */
3709 const_type_p ubc
= get_ultimate_base_class (s
);
3710 if ((!opts_have (ubc
->u
.s
.opt
, "user")
3711 && !opts_have (ubc
->u
.s
.opt
, "desc")))
3712 error_at_line (&s
->u
.s
.line
,
3713 ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
3714 ", but '%s' lacks a discriminator 'desc' option"),
3715 s
->u
.s
.tag
, ubc
->u
.s
.tag
, ubc
->u
.s
.tag
);
3717 /* Don't write fns for subclasses, only for the ultimate base class
3718 within an inheritance hierarchy. */
3722 memset (&d
, 0, sizeof (d
));
3723 d
.of
= get_output_file_for_structure (s
, param
);
3725 bool for_user
= false;
3726 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3727 if (strcmp (opt
->name
, "chain_next") == 0
3728 && opt
->kind
== OPTION_STRING
)
3729 chain_next
= opt
->info
.string
;
3730 else if (strcmp (opt
->name
, "chain_prev") == 0
3731 && opt
->kind
== OPTION_STRING
)
3732 chain_prev
= opt
->info
.string
;
3733 else if (strcmp (opt
->name
, "chain_circular") == 0
3734 && opt
->kind
== OPTION_STRING
)
3735 chain_circular
= opt
->info
.string
;
3736 else if (strcmp (opt
->name
, "mark_hook") == 0
3737 && opt
->kind
== OPTION_STRING
)
3738 mark_hook_name
= opt
->info
.string
;
3739 else if (strcmp (opt
->name
, "for_user") == 0)
3741 if (chain_prev
!= NULL
&& chain_next
== NULL
)
3742 error_at_line (&s
->u
.s
.line
, "chain_prev without chain_next");
3743 if (chain_circular
!= NULL
&& chain_next
!= NULL
)
3744 error_at_line (&s
->u
.s
.line
, "chain_circular with chain_next");
3745 if (chain_circular
!= NULL
)
3746 chain_next
= chain_circular
;
3748 d
.process_field
= write_types_process_field
;
3752 d
.line
= &s
->u
.s
.line
;
3753 d
.bitmap
= s
->u
.s
.bitmap
;
3755 d
.prev_val
[0] = "*x";
3756 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
3757 d
.prev_val
[3] = "x";
3759 d
.have_this_obj
= false;
3761 oprintf (d
.of
, "\n");
3762 oprintf (d
.of
, "void\n");
3763 write_marker_function_name (d
.of
, orig_s
, wtd
->prefix
);
3764 oprintf (d
.of
, " (void *x_p)\n");
3765 oprintf (d
.of
, "{\n ");
3766 write_type_decl (d
.of
, s
);
3767 oprintf (d
.of
, " * %sx = (", chain_next
== NULL
? "const " : "");
3768 write_type_decl (d
.of
, s
);
3769 oprintf (d
.of
, " *)x_p;\n");
3770 if (chain_next
!= NULL
)
3772 /* TYPE_USER_STRUCTs should not occur here. These structures
3773 are completely handled by user code. */
3774 gcc_assert (orig_s
->kind
!= TYPE_USER_STRUCT
);
3776 oprintf (d
.of
, " ");
3777 write_type_decl (d
.of
, s
);
3778 oprintf (d
.of
, " * xlimit = x;\n");
3780 if (chain_next
== NULL
)
3782 oprintf (d
.of
, " if (%s (x", wtd
->marker_routine
);
3783 if (wtd
->param_prefix
)
3785 oprintf (d
.of
, ", x, gt_%s_", wtd
->param_prefix
);
3786 output_mangled_typename (d
.of
, orig_s
);
3788 oprintf (d
.of
, "))\n");
3792 if (chain_circular
!= NULL
)
3793 oprintf (d
.of
, " if (!%s (xlimit", wtd
->marker_routine
);
3795 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
3796 if (wtd
->param_prefix
)
3798 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
3799 output_mangled_typename (d
.of
, orig_s
);
3801 oprintf (d
.of
, "))\n");
3802 if (chain_circular
!= NULL
)
3803 oprintf (d
.of
, " return;\n do\n");
3804 if (mark_hook_name
&& !wtd
->skip_hooks
)
3806 oprintf (d
.of
, " {\n");
3807 oprintf (d
.of
, " %s (xlimit);\n ", mark_hook_name
);
3809 oprintf (d
.of
, " xlimit = (");
3810 d
.prev_val
[2] = "*xlimit";
3811 output_escaped_param (&d
, chain_next
, "chain_next");
3812 oprintf (d
.of
, ");\n");
3813 if (mark_hook_name
&& !wtd
->skip_hooks
)
3814 oprintf (d
.of
, " }\n");
3815 if (chain_prev
!= NULL
)
3817 oprintf (d
.of
, " if (x != xlimit)\n");
3818 oprintf (d
.of
, " for (;;)\n");
3819 oprintf (d
.of
, " {\n");
3820 oprintf (d
.of
, " %s %s * const xprev = (",
3821 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
3823 d
.prev_val
[2] = "*x";
3824 output_escaped_param (&d
, chain_prev
, "chain_prev");
3825 oprintf (d
.of
, ");\n");
3826 oprintf (d
.of
, " if (xprev == NULL) break;\n");
3827 oprintf (d
.of
, " x = xprev;\n");
3828 oprintf (d
.of
, " (void) %s (xprev", wtd
->marker_routine
);
3829 if (wtd
->param_prefix
)
3831 oprintf (d
.of
, ", xprev, gt_%s_", wtd
->param_prefix
);
3832 output_mangled_typename (d
.of
, orig_s
);
3834 oprintf (d
.of
, ");\n");
3835 oprintf (d
.of
, " }\n");
3837 if (chain_circular
!= NULL
)
3839 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
3840 if (wtd
->param_prefix
)
3842 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
3843 output_mangled_typename (d
.of
, orig_s
);
3845 oprintf (d
.of
, "));\n");
3846 if (mark_hook_name
&& !wtd
->skip_hooks
)
3847 oprintf (d
.of
, " %s (xlimit);\n", mark_hook_name
);
3848 oprintf (d
.of
, " do\n");
3851 oprintf (d
.of
, " while (x != xlimit)\n");
3853 oprintf (d
.of
, " {\n");
3854 if (mark_hook_name
&& chain_next
== NULL
&& !wtd
->skip_hooks
)
3856 oprintf (d
.of
, " %s (x);\n", mark_hook_name
);
3859 d
.prev_val
[2] = "*x";
3861 if (orig_s
->kind
!= TYPE_USER_STRUCT
)
3865 /* User structures have no fields to walk. Simply generate a call
3866 to the user-provided structure marker. */
3867 oprintf (d
.of
, "%*sgt_%sx (x);\n", d
.indent
, "", wtd
->prefix
);
3870 if (chain_next
!= NULL
)
3872 oprintf (d
.of
, " x = (");
3873 output_escaped_param (&d
, chain_next
, "chain_next");
3874 oprintf (d
.of
, ");\n");
3877 oprintf (d
.of
, " }\n");
3878 if (chain_circular
!= NULL
)
3879 oprintf (d
.of
, " while (x != xlimit);\n");
3880 oprintf (d
.of
, "}\n");
3882 if (orig_s
->kind
== TYPE_USER_STRUCT
)
3883 write_user_marking_functions (orig_s
, wtd
, &d
);
3887 write_user_func_for_structure_body (orig_s
, wtd
->prefix
, &d
);
3888 write_user_func_for_structure_ptr (d
.of
, orig_s
, wtd
);
3893 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3896 write_types (outf_p output_header
, type_p structures
, type_p param_structs
,
3897 const struct write_types_data
*wtd
)
3899 int nbfun
= 0; /* Count the emitted functions. */
3902 oprintf (output_header
, "\n/* %s*/\n", wtd
->comment
);
3904 /* We first emit the macros and the declarations. Functions' code is
3905 emitted afterwards. This is needed in plugin mode. */
3906 oprintf (output_header
, "/* Macros and declarations. */\n");
3907 for (s
= structures
; s
; s
= s
->next
)
3908 /* Do not emit handlers for derived classes; we only ever deal with
3909 the ultimate base class within an inheritance hierarchy. */
3910 if ((s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
3911 && !s
->u
.s
.base_class
)
3915 if (s
->gc_used
== GC_MAYBE_POINTED_TO
&& s
->u
.s
.line
.file
== NULL
)
3918 const char *s_id_for_tag
= filter_type_name (s
->u
.s
.tag
);
3920 oprintf (output_header
, "#define gt_%s_", wtd
->prefix
);
3921 output_mangled_typename (output_header
, s
);
3922 oprintf (output_header
, "(X) do { \\\n");
3923 oprintf (output_header
,
3924 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd
->prefix
,
3926 oprintf (output_header
, " } while (0)\n");
3928 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3929 if (strcmp (opt
->name
, "ptr_alias") == 0
3930 && opt
->kind
== OPTION_TYPE
)
3932 const_type_p
const t
= (const_type_p
) opt
->info
.type
;
3933 if (t
->kind
== TYPE_STRUCT
3934 || t
->kind
== TYPE_UNION
|| t
->kind
== TYPE_LANG_STRUCT
)
3936 const char *t_id_for_tag
= filter_type_name (t
->u
.s
.tag
);
3937 oprintf (output_header
,
3938 "#define gt_%sx_%s gt_%sx_%s\n",
3939 wtd
->prefix
, s
->u
.s
.tag
, wtd
->prefix
, t_id_for_tag
);
3940 if (t_id_for_tag
!= t
->u
.s
.tag
)
3941 free (CONST_CAST (char *, t_id_for_tag
));
3944 error_at_line (&s
->u
.s
.line
,
3945 "structure alias is not a structure");
3951 /* Declare the marker procedure only once. */
3952 oprintf (output_header
,
3953 "extern void gt_%sx_%s (void *);\n",
3954 wtd
->prefix
, s_id_for_tag
);
3956 if (s_id_for_tag
!= s
->u
.s
.tag
)
3957 free (CONST_CAST (char *, s_id_for_tag
));
3959 if (s
->u
.s
.line
.file
== NULL
)
3961 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
3967 for (s
= param_structs
; s
; s
= s
->next
)
3968 if (s
->gc_used
== GC_POINTED_TO
)
3970 type_p stru
= s
->u
.param_struct
.stru
;
3972 /* Declare the marker procedure. */
3973 oprintf (output_header
, "extern void gt_%s_", wtd
->prefix
);
3974 output_mangled_typename (output_header
, s
);
3975 oprintf (output_header
, " (void *);\n");
3977 if (stru
->u
.s
.line
.file
== NULL
)
3979 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
3985 /* At last we emit the functions code. */
3986 oprintf (output_header
, "\n/* functions code */\n");
3987 for (s
= structures
; s
; s
= s
->next
)
3988 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
3992 if (s
->gc_used
== GC_MAYBE_POINTED_TO
&& s
->u
.s
.line
.file
== NULL
)
3994 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3995 if (strcmp (opt
->name
, "ptr_alias") == 0)
4000 if (s
->kind
== TYPE_LANG_STRUCT
)
4003 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
4006 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
4007 nbfun
, (void*) ss
, ss
->u
.s
.tag
);
4008 write_func_for_structure (s
, ss
, NULL
, wtd
);
4014 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
4015 nbfun
, (void*) s
, s
->u
.s
.tag
);
4016 write_func_for_structure (s
, s
, NULL
, wtd
);
4021 /* Structure s is not possibly pointed to, so can be ignored. */
4022 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
4023 (void*)s
, s
->u
.s
.tag
,
4027 for (s
= param_structs
; s
; s
= s
->next
)
4028 if (s
->gc_used
== GC_POINTED_TO
)
4030 type_p
*param
= s
->u
.param_struct
.param
;
4031 type_p stru
= s
->u
.param_struct
.stru
;
4032 if (stru
->u
.s
.line
.file
== NULL
)
4034 if (stru
->kind
== TYPE_LANG_STRUCT
)
4037 for (ss
= stru
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
4040 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
4041 nbfun
, (void*) ss
, ss
->u
.s
.tag
);
4042 write_func_for_structure (s
, ss
, param
, wtd
);
4048 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
4050 (void*) stru
, stru
->u
.s
.tag
);
4051 write_func_for_structure (s
, stru
, param
, wtd
);
4056 /* Param structure s is not pointed to, so should be ignored. */
4057 DBGPRINTF ("ignored s @ %p", (void*)s
);
4059 if (verbosity_level
>= 2)
4060 printf ("%s emitted %d routines for %s\n",
4061 progname
, nbfun
, wtd
->comment
);
4064 static const struct write_types_data ggc_wtd
= {
4065 "ggc_m", NULL
, "ggc_mark", "ggc_test_and_set_mark", NULL
,
4066 "GC marker procedures. ",
4070 static const struct write_types_data pch_wtd
= {
4071 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
4072 "gt_pch_note_reorder",
4073 "PCH type-walking procedures. ",
4077 /* Write out the local pointer-walking routines. */
4079 /* process_field routine for local pointer-walking for user-callable
4080 routines. The difference between this and
4081 write_types_local_process_field is that, in this case, we do not
4082 need to check whether the given pointer matches the address of the
4083 parent structure. This check was already generated by the call
4084 to gt_pch_nx in the main gt_pch_p_*() function that is calling
4088 write_types_local_user_process_field (type_p f
, const struct walk_type_data
*d
)
4095 case TYPE_LANG_STRUCT
:
4096 case TYPE_PARAM_STRUCT
:
4098 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
4101 case TYPE_USER_STRUCT
:
4102 if (d
->in_ptr_field
)
4103 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
4105 oprintf (d
->of
, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4106 d
->indent
, "", d
->val
);
4114 case TYPE_UNDEFINED
:
4120 /* Write a function to PCH walk all the fields of type S on OF.
4121 D contains data needed by walk_type to recurse into the fields of S. */
4124 write_pch_user_walking_for_structure_body (type_p s
, struct walk_type_data
*d
)
4126 oprintf (d
->of
, "\nvoid\n");
4127 oprintf (d
->of
, "gt_pch_nx (");
4128 write_type_decl (d
->of
, s
);
4129 oprintf (d
->of
, "* x ATTRIBUTE_UNUSED,\n"
4130 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4131 "\tATTRIBUTE_UNUSED void *cookie)\n");
4132 oprintf (d
->of
, "{\n");
4135 d
->process_field
= write_types_local_user_process_field
;
4137 oprintf (d
->of
, "}\n");
4141 /* Emit the user-callable functions needed to mark all the types used
4142 by the user structure S. PREFIX is the prefix to use to
4143 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
4144 chain_next option defined. D contains data needed to pass to
4145 walk_type when traversing the fields of a type.
4147 For every type T referenced by S, two routines are generated: one
4148 that takes 'T *', marks the pointer and calls the second routine,
4149 which just marks the fields of T. */
4152 write_pch_user_walking_functions (type_p s
, struct walk_type_data
*d
)
4154 gcc_assert (s
->kind
== TYPE_USER_STRUCT
);
4156 for (pair_p fld
= s
->u
.s
.fields
; fld
; fld
= fld
->next
)
4158 type_p fld_type
= fld
->type
;
4159 if (union_or_struct_p (fld_type
))
4160 write_pch_user_walking_for_structure_body (fld_type
, d
);
4165 /* process_field routine for local pointer-walking. */
4168 write_types_local_process_field (type_p f
, const struct walk_type_data
*d
)
4170 gcc_assert (d
->have_this_obj
);
4176 case TYPE_LANG_STRUCT
:
4177 case TYPE_PARAM_STRUCT
:
4179 oprintf (d
->of
, "%*sif ((void *)(%s) == this_obj)\n", d
->indent
, "",
4181 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
4184 case TYPE_USER_STRUCT
:
4185 oprintf (d
->of
, "%*sif ((void *)(%s) == this_obj)\n", d
->indent
, "",
4187 if (d
->in_ptr_field
)
4188 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
4190 oprintf (d
->of
, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4191 d
->indent
, "", d
->val
);
4199 case TYPE_UNDEFINED
:
4205 /* For S, a structure that's part of ORIG_S, and using parameters
4206 PARAM, write out a routine that:
4207 - Is of type gt_note_pointers
4208 - Calls PROCESS_FIELD on each field of S or its substructures.
4212 write_local_func_for_structure (const_type_p orig_s
, type_p s
, type_p
*param
)
4214 struct walk_type_data d
;
4216 /* Don't write fns for subclasses, only for the ultimate base class
4217 within an inheritance hierarchy. */
4218 if (s
->u
.s
.base_class
)
4221 memset (&d
, 0, sizeof (d
));
4222 d
.of
= get_output_file_for_structure (s
, param
);
4223 d
.process_field
= write_types_local_process_field
;
4225 d
.line
= &s
->u
.s
.line
;
4226 d
.bitmap
= s
->u
.s
.bitmap
;
4228 d
.prev_val
[0] = d
.prev_val
[2] = "*x";
4229 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
4230 d
.prev_val
[3] = "x";
4232 d
.fn_wants_lvalue
= true;
4234 oprintf (d
.of
, "\n");
4235 oprintf (d
.of
, "void\n");
4236 oprintf (d
.of
, "gt_pch_p_");
4237 output_mangled_typename (d
.of
, orig_s
);
4238 oprintf (d
.of
, " (ATTRIBUTE_UNUSED void *this_obj,\n"
4240 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4241 "\tATTRIBUTE_UNUSED void *cookie)\n");
4242 oprintf (d
.of
, "{\n");
4243 oprintf (d
.of
, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
4244 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
,
4245 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
4247 d
.have_this_obj
= true;
4249 if (s
->kind
!= TYPE_USER_STRUCT
)
4253 /* User structures have no fields to walk. Simply generate a
4254 call to the user-provided PCH walker. */
4255 oprintf (d
.of
, "%*sif ((void *)(%s) == this_obj)\n", d
.indent
, "",
4257 oprintf (d
.of
, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4258 d
.indent
, "", d
.val
);
4261 oprintf (d
.of
, "}\n");
4263 /* Write user-callable entry points for the PCH walking routines. */
4264 if (orig_s
->kind
== TYPE_USER_STRUCT
)
4265 write_pch_user_walking_functions (s
, &d
);
4267 for (options_p o
= s
->u
.s
.opt
; o
; o
= o
->next
)
4268 if (strcmp (o
->name
, "for_user") == 0)
4270 write_pch_user_walking_for_structure_body (s
, &d
);
4275 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
4278 write_local (outf_p output_header
, type_p structures
, type_p param_structs
)
4285 oprintf (output_header
, "\n/* Local pointer-walking routines. */\n");
4286 for (s
= structures
; s
; s
= s
->next
)
4287 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
4291 if (s
->u
.s
.line
.file
== NULL
)
4293 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
4294 if (strcmp (opt
->name
, "ptr_alias") == 0
4295 && opt
->kind
== OPTION_TYPE
)
4297 const_type_p
const t
= (const_type_p
) opt
->info
.type
;
4298 if (t
->kind
== TYPE_STRUCT
4299 || t
->kind
== TYPE_UNION
|| t
->kind
== TYPE_LANG_STRUCT
)
4301 oprintf (output_header
, "#define gt_pch_p_");
4302 output_mangled_typename (output_header
, s
);
4303 oprintf (output_header
, " gt_pch_p_");
4304 output_mangled_typename (output_header
, t
);
4305 oprintf (output_header
, "\n");
4308 error_at_line (&s
->u
.s
.line
,
4309 "structure alias is not a structure");
4315 /* Declare the marker procedure only once. */
4316 oprintf (output_header
, "extern void gt_pch_p_");
4317 output_mangled_typename (output_header
, s
);
4318 oprintf (output_header
,
4319 "\n (void *, void *, gt_pointer_operator, void *);\n");
4321 if (s
->kind
== TYPE_LANG_STRUCT
)
4324 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
4325 write_local_func_for_structure (s
, ss
, NULL
);
4328 write_local_func_for_structure (s
, s
, NULL
);
4331 for (s
= param_structs
; s
; s
= s
->next
)
4332 if (s
->gc_used
== GC_POINTED_TO
)
4334 type_p
*param
= s
->u
.param_struct
.param
;
4335 type_p stru
= s
->u
.param_struct
.stru
;
4337 /* Declare the marker procedure. */
4338 oprintf (output_header
, "extern void gt_pch_p_");
4339 output_mangled_typename (output_header
, s
);
4340 oprintf (output_header
,
4341 "\n (void *, void *, gt_pointer_operator, void *);\n");
4343 if (stru
->u
.s
.line
.file
== NULL
)
4345 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
4350 if (stru
->kind
== TYPE_LANG_STRUCT
)
4353 for (ss
= stru
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
4354 write_local_func_for_structure (s
, ss
, param
);
4357 write_local_func_for_structure (s
, stru
, param
);
4361 /* Nonzero if S is a type for which typed GC allocators should be output. */
4363 #define USED_BY_TYPED_GC_P(s) \
4364 ((s->kind == TYPE_POINTER \
4365 && (s->u.p->gc_used == GC_POINTED_TO \
4366 || s->u.p->gc_used == GC_USED)) \
4367 || (union_or_struct_p (s) \
4368 && ((s)->gc_used == GC_POINTED_TO \
4369 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4370 && s->u.s.line.file != NULL) \
4371 || ((s)->gc_used == GC_USED \
4372 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))) \
4373 || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
4377 /* Might T contain any non-pointer elements? */
4380 contains_scalar_p (type_p t
)
4388 return contains_scalar_p (t
->u
.a
.p
);
4389 case TYPE_USER_STRUCT
:
4390 /* User-marked structures will typically contain pointers. */
4393 /* Could also check for structures that have no non-pointer
4394 fields, but there aren't enough of those to worry about. */
4399 /* Mangle INPF and print it to F. */
4402 put_mangled_filename (outf_p f
, const input_file
*inpf
)
4404 /* The call to get_output_file_name may indirectly update fn since
4405 get_output_file_with_visibility caches its result inside, so we
4406 need the CONST_CAST. */
4407 const char *name
= get_output_file_name (CONST_CAST (input_file
*, inpf
));
4410 for (; *name
!= 0; name
++)
4411 if (ISALNUM (*name
))
4412 oprintf (f
, "%c", *name
);
4414 oprintf (f
, "%c", '_');
4417 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4418 LASTNAME, and NAME are all strings to insert in various places in
4419 the resulting code. */
4422 finish_root_table (struct flist
*flp
, const char *pfx
, const char *lastname
,
4423 const char *tname
, const char *name
)
4427 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
4428 if (fli2
->started_p
)
4430 oprintf (fli2
->f
, " %s\n", lastname
);
4431 oprintf (fli2
->f
, "};\n\n");
4434 for (fli2
= flp
; fli2
&& base_files
; fli2
= fli2
->next
)
4435 if (fli2
->started_p
)
4437 lang_bitmap bitmap
= get_lang_bitmap (fli2
->file
);
4440 for (fnum
= 0; bitmap
!= 0; fnum
++, bitmap
>>= 1)
4443 oprintf (base_files
[fnum
],
4444 "extern const struct %s gt_%s_", tname
, pfx
);
4445 put_mangled_filename (base_files
[fnum
], fli2
->file
);
4446 oprintf (base_files
[fnum
], "[];\n");
4452 for (fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
4453 oprintf (base_files
[fnum
],
4454 "EXPORTED_CONST struct %s * const %s[] = {\n", tname
, name
);
4458 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
4459 if (fli2
->started_p
)
4461 lang_bitmap bitmap
= get_lang_bitmap (fli2
->file
);
4464 fli2
->started_p
= 0;
4466 for (fnum
= 0; base_files
&& bitmap
!= 0; fnum
++, bitmap
>>= 1)
4469 oprintf (base_files
[fnum
], " gt_%s_", pfx
);
4470 put_mangled_filename (base_files
[fnum
], fli2
->file
);
4471 oprintf (base_files
[fnum
], ",\n");
4477 for (fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
4479 oprintf (base_files
[fnum
], " NULL\n");
4480 oprintf (base_files
[fnum
], "};\n");
4485 /* Write the first three fields (pointer, count and stride) for
4486 root NAME to F. V and LINE are as for write_root.
4488 Return true if the entry could be written; return false on error. */
4491 start_root_entry (outf_p f
, pair_p v
, const char *name
, struct fileloc
*line
)
4497 error_at_line (line
, "`%s' is too complex to be a root", name
);
4501 oprintf (f
, " {\n");
4502 oprintf (f
, " &%s,\n", name
);
4505 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
4507 oprintf (f
, " * (%s)", ap
->u
.a
.len
);
4508 else if (ap
== v
->type
)
4509 oprintf (f
, " * ARRAY_SIZE (%s)", v
->name
);
4511 oprintf (f
, " sizeof (%s", v
->name
);
4512 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
4514 oprintf (f
, "),\n");
4518 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4519 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4523 write_field_root (outf_p f
, pair_p v
, type_p type
, const char *name
,
4524 int has_length
, struct fileloc
*line
, const char *if_marked
,
4525 bool emit_pch
, type_p field_type
, const char *field_name
)
4528 /* If the field reference is relative to V, rather than to some
4529 subcomponent of V, we can mark any subarrays with a single stride.
4530 We're effectively treating the field as a global variable in its
4532 if (v
&& type
== v
->type
)
4535 newv
.type
= field_type
;
4536 newv
.name
= ACONCAT ((v
->name
, ".", field_name
, NULL
));
4539 /* Otherwise, any arrays nested in the structure are too complex to
4541 else if (field_type
->kind
== TYPE_ARRAY
)
4543 write_root (f
, v
, field_type
, ACONCAT ((name
, ".", field_name
, NULL
)),
4544 has_length
, line
, if_marked
, emit_pch
);
4547 /* Write out to F the table entry and any marker routines needed to
4548 mark NAME as TYPE. V can be one of three values:
4550 - null, if NAME is too complex to represent using a single
4551 count and stride. In this case, it is an error for NAME to
4552 contain any gc-ed data.
4554 - the outermost array that contains NAME, if NAME is part of an array.
4556 - the C variable that contains NAME, if NAME is not part of an array.
4558 LINE is the line of the C source that declares the root variable.
4559 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
4560 is nonzero iff we are building the root table for hash table caches. */
4563 write_root (outf_p f
, pair_p v
, type_p type
, const char *name
, int has_length
,
4564 struct fileloc
*line
, const char *if_marked
, bool emit_pch
)
4571 for (fld
= type
->u
.s
.fields
; fld
; fld
= fld
->next
)
4574 const char *desc
= NULL
;
4577 for (o
= fld
->opt
; o
; o
= o
->next
)
4578 if (strcmp (o
->name
, "skip") == 0)
4580 else if (strcmp (o
->name
, "desc") == 0
4581 && o
->kind
== OPTION_STRING
)
4582 desc
= o
->info
.string
;
4583 else if (strcmp (o
->name
, "param_is") == 0)
4586 error_at_line (line
,
4587 "field `%s' of global `%s' has unknown option `%s'",
4588 fld
->name
, name
, o
->name
);
4592 else if (desc
&& fld
->type
->kind
== TYPE_UNION
)
4594 pair_p validf
= NULL
;
4597 for (ufld
= fld
->type
->u
.s
.fields
; ufld
; ufld
= ufld
->next
)
4599 const char *tag
= NULL
;
4601 for (oo
= ufld
->opt
; oo
; oo
= oo
->next
)
4602 if (strcmp (oo
->name
, "tag") == 0
4603 && oo
->kind
== OPTION_STRING
)
4604 tag
= oo
->info
.string
;
4605 if (tag
== NULL
|| strcmp (tag
, desc
) != 0)
4608 error_at_line (line
,
4609 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4610 name
, fld
->name
, validf
->name
,
4611 name
, fld
->name
, ufld
->name
, tag
);
4615 write_field_root (f
, v
, type
, name
, 0, line
, if_marked
,
4616 emit_pch
, validf
->type
,
4617 ACONCAT ((fld
->name
, ".",
4618 validf
->name
, NULL
)));
4621 error_at_line (line
,
4622 "global `%s.%s' has `desc' option but is not union",
4625 write_field_root (f
, v
, type
, name
, 0, line
, if_marked
,
4626 emit_pch
, fld
->type
, fld
->name
);
4634 newname
= xasprintf ("%s[0]", name
);
4635 write_root (f
, v
, type
->u
.a
.p
, newname
, has_length
, line
, if_marked
,
4641 case TYPE_USER_STRUCT
:
4642 error_at_line (line
, "`%s' must be a pointer type, because it is "
4643 "a GC root and its type is marked with GTY((user))",
4651 if (!start_root_entry (f
, v
, name
, line
))
4656 if (!has_length
&& union_or_struct_p (tp
))
4658 tp
= get_ultimate_base_class (tp
);
4659 const char *id_for_tag
= filter_type_name (tp
->u
.s
.tag
);
4660 oprintf (f
, " >_ggc_mx_%s,\n", id_for_tag
);
4662 oprintf (f
, " >_pch_nx_%s", id_for_tag
);
4664 oprintf (f
, " NULL");
4665 if (id_for_tag
!= tp
->u
.s
.tag
)
4666 free (CONST_CAST (char *, id_for_tag
));
4668 else if (!has_length
&& tp
->kind
== TYPE_PARAM_STRUCT
)
4670 oprintf (f
, " >_ggc_m_");
4671 output_mangled_typename (f
, tp
);
4674 oprintf (f
, ",\n >_pch_n_");
4675 output_mangled_typename (f
, tp
);
4678 oprintf (f
, ",\n NULL");
4681 && (tp
->kind
== TYPE_POINTER
|| union_or_struct_p (tp
)))
4683 oprintf (f
, " >_ggc_ma_%s,\n", name
);
4685 oprintf (f
, " >_pch_na_%s", name
);
4687 oprintf (f
, " NULL");
4691 error_at_line (line
,
4692 "global `%s' is pointer to unimplemented type",
4696 oprintf (f
, ",\n &%s", if_marked
);
4697 oprintf (f
, "\n },\n");
4703 if (!start_root_entry (f
, v
, name
, line
))
4706 oprintf (f
, " (gt_pointer_walker) >_ggc_m_S,\n");
4707 oprintf (f
, " (gt_pointer_walker) >_pch_n_S\n");
4708 oprintf (f
, " },\n");
4716 case TYPE_UNDEFINED
:
4718 case TYPE_LANG_STRUCT
:
4719 case TYPE_PARAM_STRUCT
:
4720 error_at_line (line
, "global `%s' is unimplemented type", name
);
4724 /* This generates a routine to walk an array. */
4727 write_array (outf_p f
, pair_p v
, const struct write_types_data
*wtd
)
4729 struct walk_type_data d
;
4732 memset (&d
, 0, sizeof (d
));
4738 d
.bitmap
= get_lang_bitmap (v
->line
.file
);
4741 d
.prev_val
[3] = prevval3
= xasprintf ("&%s", v
->name
);
4743 if (wtd
->param_prefix
)
4745 oprintf (f
, "static void gt_%sa_%s\n", wtd
->param_prefix
, v
->name
);
4746 oprintf (f
, " (void *, void *, gt_pointer_operator, void *);\n");
4747 oprintf (f
, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4748 wtd
->param_prefix
, v
->name
);
4750 " ATTRIBUTE_UNUSED void *x_p,\n"
4751 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4752 " ATTRIBUTE_UNUSED void * cookie)\n");
4753 oprintf (d
.of
, "{\n");
4754 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
4755 d
.process_field
= write_types_local_process_field
;
4756 d
.have_this_obj
= true;
4757 walk_type (v
->type
, &d
);
4758 oprintf (f
, "}\n\n");
4762 oprintf (f
, "static void gt_%sa_%s (void *);\n", wtd
->prefix
, v
->name
);
4763 oprintf (f
, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4764 wtd
->prefix
, v
->name
);
4766 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
4767 d
.process_field
= write_types_process_field
;
4768 d
.have_this_obj
= false;
4769 walk_type (v
->type
, &d
);
4771 oprintf (f
, "}\n\n");
4774 /* Output a table describing the locations and types of VARIABLES. */
4777 write_roots (pair_p variables
, bool emit_pch
)
4780 struct flist
*flp
= NULL
;
4782 for (v
= variables
; v
; v
= v
->next
)
4785 get_output_file_with_visibility (CONST_CAST (input_file
*,
4788 const char *length
= NULL
;
4789 int deletable_p
= 0;
4791 for (o
= v
->opt
; o
; o
= o
->next
)
4792 if (strcmp (o
->name
, "length") == 0
4793 && o
->kind
== OPTION_STRING
)
4794 length
= o
->info
.string
;
4795 else if (strcmp (o
->name
, "deletable") == 0)
4797 else if (strcmp (o
->name
, "param_is") == 0)
4799 else if (strncmp (o
->name
, "param", 5) == 0
4800 && ISDIGIT (o
->name
[5]) && strcmp (o
->name
+ 6, "_is") == 0)
4802 else if (strcmp (o
->name
, "if_marked") == 0)
4805 error_at_line (&v
->line
,
4806 "global `%s' has unknown option `%s'",
4809 for (fli
= flp
; fli
; fli
= fli
->next
)
4810 if (fli
->f
== f
&& f
)
4814 fli
= XNEW (struct flist
);
4818 fli
->file
= v
->line
.file
;
4819 gcc_assert (fli
->file
);
4822 oprintf (f
, "\n/* GC roots. */\n\n");
4827 && v
->type
->kind
== TYPE_POINTER
4828 && (v
->type
->u
.p
->kind
== TYPE_POINTER
4829 || v
->type
->u
.p
->kind
== TYPE_STRUCT
))
4831 write_array (f
, v
, &ggc_wtd
);
4832 write_array (f
, v
, &pch_wtd
);
4836 for (v
= variables
; v
; v
= v
->next
)
4838 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4845 for (o
= v
->opt
; o
; o
= o
->next
)
4846 if (strcmp (o
->name
, "length") == 0)
4848 else if (strcmp (o
->name
, "deletable") == 0
4849 || strcmp (o
->name
, "if_marked") == 0)
4855 for (fli
= flp
; fli
; fli
= fli
->next
)
4858 if (!fli
->started_p
)
4862 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4863 put_mangled_filename (f
, v
->line
.file
);
4864 oprintf (f
, "[] = {\n");
4867 write_root (f
, v
, v
->type
, v
->name
, length_p
, &v
->line
, NULL
, emit_pch
);
4870 finish_root_table (flp
, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4873 for (v
= variables
; v
; v
= v
->next
)
4875 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4881 for (o
= v
->opt
; o
; o
= o
->next
)
4882 if (strcmp (o
->name
, "deletable") == 0)
4884 else if (strcmp (o
->name
, "if_marked") == 0)
4890 for (fli
= flp
; fli
; fli
= fli
->next
)
4893 if (!fli
->started_p
)
4897 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4898 put_mangled_filename (f
, v
->line
.file
);
4899 oprintf (f
, "[] = {\n");
4902 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4906 finish_root_table (flp
, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4907 "gt_ggc_deletable_rtab");
4909 for (v
= variables
; v
; v
= v
->next
)
4911 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4914 const char *if_marked
= NULL
;
4918 for (o
= v
->opt
; o
; o
= o
->next
)
4919 if (strcmp (o
->name
, "length") == 0)
4921 else if (strcmp (o
->name
, "if_marked") == 0
4922 && o
->kind
== OPTION_STRING
)
4923 if_marked
= o
->info
.string
;
4924 if (if_marked
== NULL
)
4926 if (v
->type
->kind
!= TYPE_POINTER
4927 || v
->type
->u
.p
->kind
!= TYPE_PARAM_STRUCT
4928 || v
->type
->u
.p
->u
.param_struct
.stru
!= find_structure ("htab",
4931 error_at_line (&v
->line
,
4932 "if_marked option used but not hash table");
4936 for (fli
= flp
; fli
; fli
= fli
->next
)
4939 if (!fli
->started_p
)
4943 oprintf (f
, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4944 put_mangled_filename (f
, v
->line
.file
);
4945 oprintf (f
, "[] = {\n");
4948 write_root (f
, v
, v
->type
->u
.p
->u
.param_struct
.param
[0],
4949 v
->name
, length_p
, &v
->line
, if_marked
, emit_pch
);
4952 finish_root_table (flp
, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4953 "gt_ggc_cache_rtab");
4958 for (v
= variables
; v
; v
= v
->next
)
4960 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4964 int if_marked_p
= 0;
4967 for (o
= v
->opt
; o
; o
= o
->next
)
4968 if (strcmp (o
->name
, "length") == 0)
4970 else if (strcmp (o
->name
, "if_marked") == 0)
4976 for (fli
= flp
; fli
; fli
= fli
->next
)
4979 if (!fli
->started_p
)
4983 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4984 put_mangled_filename (f
, v
->line
.file
);
4985 oprintf (f
, "[] = {\n");
4988 write_root (f
, v
, v
->type
, v
->name
, length_p
, &v
->line
, NULL
, emit_pch
);
4991 finish_root_table (flp
, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4992 "gt_pch_cache_rtab");
4994 for (v
= variables
; v
; v
= v
->next
)
4996 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
5002 for (o
= v
->opt
; o
; o
= o
->next
)
5003 if (strcmp (o
->name
, "deletable") == 0
5004 || strcmp (o
->name
, "if_marked") == 0)
5013 if (!contains_scalar_p (v
->type
))
5016 for (fli
= flp
; fli
; fli
= fli
->next
)
5019 if (!fli
->started_p
)
5023 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
5024 put_mangled_filename (f
, v
->line
.file
);
5025 oprintf (f
, "[] = {\n");
5028 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
5032 finish_root_table (flp
, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
5033 "gt_pch_scalar_rtab");
5036 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
5037 guaranteee for somewhat increased readability. If name conflicts do happen,
5038 this funcion will have to be adjusted to be more like
5039 output_mangled_typename. */
5042 output_typename (outf_p of
, const_type_p t
)
5047 oprintf (of
, "str");
5050 oprintf (of
, "scalar");
5053 output_typename (of
, t
->u
.p
);
5056 case TYPE_USER_STRUCT
:
5058 case TYPE_LANG_STRUCT
:
5059 oprintf (of
, "%s", t
->u
.s
.tag
);
5061 case TYPE_PARAM_STRUCT
:
5064 for (i
= 0; i
< NUM_PARAM
; i
++)
5065 if (t
->u
.param_struct
.param
[i
] != NULL
)
5067 output_typename (of
, t
->u
.param_struct
.param
[i
]);
5070 output_typename (of
, t
->u
.param_struct
.stru
);
5074 case TYPE_UNDEFINED
:
5080 /* Writes a typed GC allocator for type S that is suitable as a callback for
5081 the splay tree implementation in libiberty. */
5084 write_splay_tree_allocator_def (const_type_p s
)
5086 outf_p of
= get_output_file_with_visibility (NULL
);
5087 oprintf (of
, "void * ggc_alloc_splay_tree_");
5088 output_typename (of
, s
);
5089 oprintf (of
, " (int sz, void * nl)\n");
5090 oprintf (of
, "{\n");
5091 oprintf (of
, " return ggc_splay_alloc (sz, nl);\n");
5092 oprintf (of
, "}\n\n");
5095 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
5096 for the splay tree implementation in libiberty. */
5099 write_splay_tree_allocators (const_type_p param_structs
)
5103 oprintf (header_file
, "\n/* Splay tree callback allocators. */\n");
5104 for (s
= param_structs
; s
; s
= s
->next
)
5105 if (s
->gc_used
== GC_POINTED_TO
)
5107 oprintf (header_file
, "extern void * ggc_alloc_splay_tree_");
5108 output_typename (header_file
, s
);
5109 oprintf (header_file
, " (int, void *);\n");
5110 write_splay_tree_allocator_def (s
);
5116 /* Dumps the value of typekind KIND. */
5119 dump_typekind (int indent
, enum typekind kind
)
5121 printf ("%*ckind = ", indent
, ' ');
5125 printf ("TYPE_SCALAR");
5128 printf ("TYPE_STRING");
5131 printf ("TYPE_STRUCT");
5133 case TYPE_UNDEFINED
:
5134 printf ("TYPE_UNDEFINED");
5136 case TYPE_USER_STRUCT
:
5137 printf ("TYPE_USER_STRUCT");
5140 printf ("TYPE_UNION");
5143 printf ("TYPE_POINTER");
5146 printf ("TYPE_ARRAY");
5148 case TYPE_LANG_STRUCT
:
5149 printf ("TYPE_LANG_STRUCT");
5151 case TYPE_PARAM_STRUCT
:
5152 printf ("TYPE_PARAM_STRUCT");
5160 /* Dumps the value of GC_USED flag. */
5163 dump_gc_used (int indent
, enum gc_used_enum gc_used
)
5165 printf ("%*cgc_used = ", indent
, ' ');
5169 printf ("GC_UNUSED");
5174 case GC_MAYBE_POINTED_TO
:
5175 printf ("GC_MAYBE_POINTED_TO");
5178 printf ("GC_POINTED_TO");
5186 /* Dumps the type options OPT. */
5189 dump_options (int indent
, options_p opt
)
5192 printf ("%*coptions = ", indent
, ' ');
5199 printf ("%s:string %s ", o
->name
, o
->info
.string
);
5202 printf ("%s:type ", o
->name
);
5203 dump_type (indent
+1, o
->info
.type
);
5206 printf ("%s:nested ", o
->name
);
5216 /* Dumps the source file location in LINE. */
5219 dump_fileloc (int indent
, struct fileloc line
)
5221 printf ("%*cfileloc: file = %s, line = %d\n", indent
, ' ',
5222 get_input_file_name (line
.file
),
5226 /* Recursively dumps the struct, union, or a language-specific
5230 dump_type_u_s (int indent
, type_p t
)
5234 gcc_assert (union_or_struct_p (t
));
5235 printf ("%*cu.s.tag = %s\n", indent
, ' ', t
->u
.s
.tag
);
5236 dump_fileloc (indent
, t
->u
.s
.line
);
5237 printf ("%*cu.s.fields =\n", indent
, ' ');
5238 fields
= t
->u
.s
.fields
;
5241 dump_pair (indent
+ INDENT
, fields
);
5242 fields
= fields
->next
;
5244 printf ("%*cend of fields of type %p\n", indent
, ' ', (void *) t
);
5245 dump_options (indent
, t
->u
.s
.opt
);
5246 printf ("%*cu.s.bitmap = %X\n", indent
, ' ', t
->u
.s
.bitmap
);
5247 if (t
->kind
== TYPE_LANG_STRUCT
)
5249 printf ("%*cu.s.lang_struct:\n", indent
, ' ');
5250 dump_type_list (indent
+ INDENT
, t
->u
.s
.lang_struct
);
5254 /* Recursively dumps the array T. */
5257 dump_type_u_a (int indent
, type_p t
)
5259 gcc_assert (t
->kind
== TYPE_ARRAY
);
5260 printf ("%*clen = %s, u.a.p:\n", indent
, ' ', t
->u
.a
.len
);
5261 dump_type_list (indent
+ INDENT
, t
->u
.a
.p
);
5264 /* Recursively dumps the parameterized struct T. */
5267 dump_type_u_param_struct (int indent
, type_p t
)
5270 gcc_assert (t
->kind
== TYPE_PARAM_STRUCT
);
5271 printf ("%*cu.param_struct.stru:\n", indent
, ' ');
5272 dump_type_list (indent
, t
->u
.param_struct
.stru
);
5273 dump_fileloc (indent
, t
->u
.param_struct
.line
);
5274 for (i
= 0; i
< NUM_PARAM
; i
++)
5276 if (t
->u
.param_struct
.param
[i
] == NULL
)
5278 printf ("%*cu.param_struct.param[%d]:\n", indent
, ' ', i
);
5279 dump_type (indent
+ INDENT
, t
->u
.param_struct
.param
[i
]);
5283 /* Recursively dumps the type list T. */
5286 dump_type_list (int indent
, type_p t
)
5291 dump_type (indent
, p
);
5296 static htab_t seen_types
;
5298 /* Recursively dumps the type T if it was not dumped previously. */
5301 dump_type (int indent
, type_p t
)
5305 if (seen_types
== NULL
)
5306 seen_types
= htab_create (100, htab_hash_pointer
, htab_eq_pointer
, NULL
);
5308 printf ("%*cType at %p: ", indent
, ' ', (void *) t
);
5309 slot
= htab_find_slot (seen_types
, t
, INSERT
);
5312 printf ("already seen.\n");
5318 dump_typekind (indent
, t
->kind
);
5319 printf ("%*cpointer_to = %p\n", indent
+ INDENT
, ' ',
5320 (void *) t
->pointer_to
);
5321 dump_gc_used (indent
+ INDENT
, t
->gc_used
);
5325 printf ("%*cscalar_is_char = %s\n", indent
+ INDENT
, ' ',
5326 t
->u
.scalar_is_char
? "true" : "false");
5332 case TYPE_LANG_STRUCT
:
5333 case TYPE_USER_STRUCT
:
5334 dump_type_u_s (indent
+ INDENT
, t
);
5337 printf ("%*cp:\n", indent
+ INDENT
, ' ');
5338 dump_type (indent
+ INDENT
, t
->u
.p
);
5341 dump_type_u_a (indent
+ INDENT
, t
);
5343 case TYPE_PARAM_STRUCT
:
5344 dump_type_u_param_struct (indent
+ INDENT
, t
);
5349 printf ("%*cEnd of type at %p\n", indent
, ' ', (void *) t
);
5352 /* Dumps the pair P. */
5355 dump_pair (int indent
, pair_p p
)
5357 printf ("%*cpair: name = %s\n", indent
, ' ', p
->name
);
5358 dump_type (indent
, p
->type
);
5359 dump_fileloc (indent
, p
->line
);
5360 dump_options (indent
, p
->opt
);
5361 printf ("%*cEnd of pair %s\n", indent
, ' ', p
->name
);
5364 /* Dumps the list of pairs PP. */
5367 dump_pair_list (const char *name
, pair_p pp
)
5370 printf ("%s:\n", name
);
5371 for (p
= pp
; p
!= NULL
; p
= p
->next
)
5373 printf ("End of %s\n\n", name
);
5376 /* Dumps the STRUCTURES. */
5379 dump_structures (const char *name
, type_p structures
)
5381 printf ("%s:\n", name
);
5382 dump_type_list (0, structures
);
5383 printf ("End of %s\n\n", name
);
5386 /* Dumps the internal structures of gengtype. This is useful to debug
5387 gengtype itself, or to understand what it does, e.g. for plugin
5391 dump_everything (void)
5393 dump_pair_list ("typedefs", typedefs
);
5394 dump_structures ("structures", structures
);
5395 dump_structures ("param_structs", param_structs
);
5396 dump_pair_list ("variables", variables
);
5398 /* Allocated with the first call to dump_type. */
5399 htab_delete (seen_types
);
5404 /* Option specification for getopt_long. */
5405 static const struct option gengtype_long_options
[] = {
5406 {"help", no_argument
, NULL
, 'h'},
5407 {"version", no_argument
, NULL
, 'V'},
5408 {"verbose", no_argument
, NULL
, 'v'},
5409 {"dump", no_argument
, NULL
, 'd'},
5410 {"debug", no_argument
, NULL
, 'D'},
5411 {"plugin", required_argument
, NULL
, 'P'},
5412 {"srcdir", required_argument
, NULL
, 'S'},
5413 {"backupdir", required_argument
, NULL
, 'B'},
5414 {"inputs", required_argument
, NULL
, 'I'},
5415 {"read-state", required_argument
, NULL
, 'r'},
5416 {"write-state", required_argument
, NULL
, 'w'},
5417 /* Terminating NULL placeholder. */
5418 {NULL
, no_argument
, NULL
, 0},
5425 printf ("Usage: %s\n", progname
);
5426 printf ("\t -h | --help " " \t# Give this help.\n");
5427 printf ("\t -D | --debug "
5428 " \t# Give debug output to debug %s itself.\n", progname
);
5429 printf ("\t -V | --version " " \t# Give version information.\n");
5430 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5431 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5432 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5433 " \t# Generate for plugin.\n");
5434 printf ("\t -S | --srcdir <GCC-directory> "
5435 " \t# Specify the GCC source directory.\n");
5436 printf ("\t -B | --backupdir <directory> "
5437 " \t# Specify the backup directory for updated files.\n");
5438 printf ("\t -I | --inputs <input-list> "
5439 " \t# Specify the file with source files list.\n");
5440 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5441 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5445 print_version (void)
5447 printf ("%s %s%s\n", progname
, pkgversion_string
, version_string
);
5448 printf ("Report bugs: %s\n", bug_report_url
);
5451 /* Parse the program options using getopt_long... */
5453 parse_program_options (int argc
, char **argv
)
5456 while ((opt
= getopt_long (argc
, argv
, "hVvdP:S:B:I:w:r:D",
5457 gengtype_long_options
, NULL
)) >= 0)
5461 case 'h': /* --help */
5464 case 'V': /* --version */
5467 case 'd': /* --dump */
5470 case 'D': /* --debug */
5473 case 'v': /* --verbose */
5476 case 'P': /* --plugin */
5478 plugin_output_filename
= optarg
;
5480 fatal ("missing plugin output file name");
5482 case 'S': /* --srcdir */
5486 fatal ("missing source directory");
5487 srcdir_len
= strlen (srcdir
);
5489 case 'B': /* --backupdir */
5491 backup_dir
= optarg
;
5493 fatal ("missing backup directory");
5495 case 'I': /* --inputs */
5499 fatal ("missing input list");
5501 case 'r': /* --read-state */
5503 read_state_filename
= optarg
;
5505 fatal ("missing read state file");
5506 DBGPRINTF ("read state %s\n", optarg
);
5508 case 'w': /* --write-state */
5509 DBGPRINTF ("write state %s\n", optarg
);
5511 write_state_filename
= optarg
;
5513 fatal ("missing write state file");
5516 fprintf (stderr
, "%s: unknown flag '%c'\n", progname
, opt
);
5518 fatal ("unexpected flag");
5521 if (plugin_output_filename
)
5523 /* In plugin mode we require some input files. */
5526 fatal ("no source files given in plugin mode");
5527 nb_plugin_files
= argc
- optind
;
5528 plugin_files
= XNEWVEC (input_file
*, nb_plugin_files
);
5529 for (i
= 0; i
< (int) nb_plugin_files
; i
++)
5531 char *name
= argv
[i
+ optind
];
5532 plugin_files
[i
] = input_file_by_name (name
);
5539 /******* Manage input files. ******/
5541 /* Hash table of unique input file names. */
5542 static htab_t input_file_htab
;
5544 /* Find or allocate a new input_file by hash-consing it. */
5546 input_file_by_name (const char* name
)
5549 input_file
* f
= NULL
;
5553 namlen
= strlen (name
);
5554 f
= XCNEWVAR (input_file
, sizeof (input_file
)+namlen
+2);
5557 f
->inpisplugin
= false;
5558 strcpy (f
->inpname
, name
);
5559 slot
= htab_find_slot (input_file_htab
, f
, INSERT
);
5560 gcc_assert (slot
!= NULL
);
5563 /* Already known input file. */
5565 return (input_file
*)(*slot
);
5567 /* New input file. */
5572 /* Hash table support routines for input_file-s. */
5574 htab_hash_inputfile (const void *p
)
5576 const input_file
*inpf
= (const input_file
*) p
;
5578 return htab_hash_string (get_input_file_name (inpf
));
5582 htab_eq_inputfile (const void *x
, const void *y
)
5584 const input_file
*inpfx
= (const input_file
*) x
;
5585 const input_file
*inpfy
= (const input_file
*) y
;
5586 gcc_assert (inpfx
!= NULL
&& inpfy
!= NULL
);
5587 return !filename_cmp (get_input_file_name (inpfx
), get_input_file_name (inpfy
));
5592 main (int argc
, char **argv
)
5595 static struct fileloc pos
= { NULL
, 0 };
5596 outf_p output_header
;
5598 /* Mandatory common initializations. */
5599 progname
= "gengtype"; /* For fatal and messages. */
5600 /* Create the hash-table used to hash-cons input files. */
5602 htab_create (800, htab_hash_inputfile
, htab_eq_inputfile
, NULL
);
5603 /* Initialize our special input files. */
5604 this_file
= input_file_by_name (__FILE__
);
5605 system_h_file
= input_file_by_name ("system.h");
5606 /* Set the scalar_is_char union number for predefined scalar types. */
5607 scalar_nonchar
.u
.scalar_is_char
= FALSE
;
5608 scalar_char
.u
.scalar_is_char
= TRUE
;
5610 parse_program_options (argc
, argv
);
5615 time_t now
= (time_t) 0;
5617 DBGPRINTF ("gengtype started pid %d at %s",
5618 (int) getpid (), ctime (&now
));
5620 #endif /* ENABLE_CHECKING */
5622 /* Parse the input list and the input files. */
5623 DBGPRINTF ("inputlist %s", inputlist
);
5624 if (read_state_filename
)
5627 fatal ("input list %s cannot be given with a read state file %s",
5628 inputlist
, read_state_filename
);
5629 read_state (read_state_filename
);
5630 DBGPRINT_COUNT_TYPE ("structures after read_state", structures
);
5631 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs
);
5635 /* These types are set up with #define or else outside of where
5636 we can see them. We should initialize them before calling
5638 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5640 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos
));
5641 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos
));
5642 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos
));
5643 POS_HERE (do_scalar_typedef ("double_int", &pos
));
5644 POS_HERE (do_scalar_typedef ("offset_int", &pos
));
5645 POS_HERE (do_scalar_typedef ("widest_int", &pos
));
5646 POS_HERE (do_scalar_typedef ("int64_t", &pos
));
5647 POS_HERE (do_scalar_typedef ("uint64_t", &pos
));
5648 POS_HERE (do_scalar_typedef ("uint8", &pos
));
5649 POS_HERE (do_scalar_typedef ("uintptr_t", &pos
));
5650 POS_HERE (do_scalar_typedef ("jword", &pos
));
5651 POS_HERE (do_scalar_typedef ("JCF_u2", &pos
));
5652 POS_HERE (do_scalar_typedef ("void", &pos
));
5653 POS_HERE (do_scalar_typedef ("machine_mode", &pos
));
5654 POS_HERE (do_typedef ("PTR",
5655 create_pointer (resolve_typedef ("void", &pos
)),
5658 read_input_list (inputlist
);
5659 for (i
= 0; i
< num_gt_files
; i
++)
5661 parse_file (get_input_file_name (gt_files
[i
]));
5662 DBGPRINTF ("parsed file #%d %s",
5663 (int) i
, get_input_file_name (gt_files
[i
]));
5665 if (verbosity_level
>= 1)
5666 printf ("%s parsed %d files with %d GTY types\n",
5667 progname
, (int) num_gt_files
, type_count
);
5669 DBGPRINT_COUNT_TYPE ("structures after parsing", structures
);
5670 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs
);
5674 fatal ("either an input list or a read state file should be given");
5679 if (plugin_output_filename
)
5682 /* In plugin mode, we should have read a state file, and have
5683 given at least one plugin file. */
5684 if (!read_state_filename
)
5685 fatal ("No read state given in plugin mode for %s",
5686 plugin_output_filename
);
5688 if (nb_plugin_files
== 0 || !plugin_files
)
5689 fatal ("No plugin files given in plugin mode for %s",
5690 plugin_output_filename
);
5692 /* Parse our plugin files and augment the state. */
5693 for (ix
= 0; ix
< nb_plugin_files
; ix
++)
5695 input_file
* pluginput
= plugin_files
[ix
];
5696 pluginput
->inpisplugin
= true;
5697 parse_file (get_input_file_name (pluginput
));
5702 plugin_output
= create_file ("GCC", plugin_output_filename
);
5703 DBGPRINTF ("created plugin_output %p named %s",
5704 (void *) plugin_output
, plugin_output
->name
);
5707 { /* No plugin files, we are in normal mode. */
5709 fatal ("gengtype needs a source directory in normal mode");
5716 /* The call to set_gc_used may indirectly call find_param_structure
5717 hence enlarge the param_structs list of types. */
5718 set_gc_used (variables
);
5720 for (type_p t
= structures
; t
; t
= t
->next
)
5722 bool for_user
= false;
5723 for (options_p o
= t
->u
.s
.opt
; o
; o
= o
->next
)
5724 if (strcmp (o
->name
, "for_user") == 0)
5731 set_gc_used_type (t
, GC_POINTED_TO
, NULL
);
5733 /* The state at this point is read from the state input file or by
5734 parsing source files and optionally augmented by parsing plugin
5735 source files. Write it now. */
5736 if (write_state_filename
)
5738 DBGPRINT_COUNT_TYPE ("structures before write_state", structures
);
5739 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs
);
5742 fatal ("didn't write state file %s after errors",
5743 write_state_filename
);
5745 DBGPRINTF ("before write_state %s", write_state_filename
);
5746 write_state (write_state_filename
);
5751 /* After having written the state file we return immediately to
5752 avoid generating any output file. */
5762 output_header
= plugin_output
? plugin_output
: header_file
;
5763 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5765 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5768 write_types (output_header
, structures
, param_structs
, &ggc_wtd
);
5769 if (plugin_files
== NULL
)
5771 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5773 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5775 write_types (header_file
, structures
, param_structs
, &pch_wtd
);
5776 write_local (header_file
, structures
, param_structs
);
5778 write_splay_tree_allocators (param_structs
);
5779 write_roots (variables
, plugin_files
== NULL
);
5781 close_output_files ();
5786 /* Don't bother about free-ing any input or plugin file, etc. */