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
);
573 /* Define TYPE_NAME to be a user defined type at location POS. */
576 create_user_defined_type (const char *type_name
, struct fileloc
*pos
)
578 type_p ty
= find_structure (type_name
, TYPE_USER_STRUCT
);
580 /* We might have already seen an incomplete decl of the given type,
581 in which case we won't have yet seen a GTY((user)), and the type will
582 only have kind "TYPE_STRUCT". Mark it as a user struct. */
583 ty
->kind
= TYPE_USER_STRUCT
;
586 ty
->u
.s
.bitmap
= get_lang_bitmap (pos
->file
);
587 do_typedef (type_name
, ty
, pos
);
589 /* If TYPE_NAME specifies a template, create references to the types
590 in the template by pretending that each type is a field of TY.
591 This is needed to make sure that the types referenced by the
592 template are marked as used. */
593 char *str
= xstrdup (type_name
);
594 char *open_bracket
= strchr (str
, '<');
597 /* We only accept simple template declarations (see
598 require_template_declaration), so we only need to parse a
599 comma-separated list of strings, implicitly assumed to
600 be type names, potentially with "*" characters. */
601 char *arg
= open_bracket
+ 1;
602 char *type_id
= strtok (arg
, ",>");
606 /* Create a new field for every type found inside the template
609 /* Support a single trailing "*" character. */
610 const char *star
= strchr (type_id
, '*');
611 int is_ptr
= (star
!= NULL
);
612 size_t offset_to_star
= star
- type_id
;
614 offset_to_star
= star
- type_id
;
616 char *field_name
= xstrdup (type_id
);
621 /* Strip off the first '*' character (and any subsequent text). */
622 *(field_name
+ offset_to_star
) = '\0';
624 arg_type
= find_structure (field_name
, TYPE_STRUCT
);
625 arg_type
= create_pointer (arg_type
);
628 arg_type
= resolve_typedef (field_name
, pos
);
630 fields
= create_field_at (fields
, arg_type
, field_name
, 0, pos
);
631 type_id
= strtok (0, ",>");
634 /* Associate the field list to TY. */
635 ty
->u
.s
.fields
= fields
;
643 /* Given a typedef name S, return its associated type. Return NULL if
644 S is not a registered type name. */
647 type_for_name (const char *s
)
651 /* Special-case support for types within a "gcc::" namespace. Rather
652 than fully-supporting namespaces, simply strip off the "gcc::" prefix
653 where present. This allows us to have GTY roots of this form:
654 extern GTY(()) gcc::some_type *some_ptr;
655 where the autogenerated functions will refer to simply "some_type",
656 where they can be resolved into their namespace. */
657 if (0 == strncmp (s
, "gcc::", 5))
660 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
661 if (strcmp (p
->name
, s
) == 0)
667 /* Create an undefined type with name S and location POS. Return the
668 newly created type. */
671 create_undefined_type (const char *s
, struct fileloc
*pos
)
673 type_p ty
= find_structure (s
, TYPE_UNDEFINED
);
675 ty
->u
.s
.bitmap
= get_lang_bitmap (pos
->file
);
676 do_typedef (s
, ty
, pos
);
681 /* Return the type previously defined for S. Use POS to report errors. */
684 resolve_typedef (const char *s
, struct fileloc
*pos
)
686 bool is_template_instance
= (strchr (s
, '<') != NULL
);
687 type_p p
= type_for_name (s
);
689 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
690 type for regular type identifiers. If the type identifier S is a
691 template instantiation, however, we treat it as a user defined
694 FIXME, this is actually a limitation in gengtype. Supporting
695 template types and their instances would require keeping separate
696 track of the basic types definition and its instances. This
697 essentially forces all template classes in GC to be marked
700 p
= (is_template_instance
)
701 ? create_user_defined_type (s
, pos
)
702 : create_undefined_type (s
, pos
);
707 /* Add SUBCLASS to head of linked list of BASE's subclasses. */
709 void add_subclass (type_p base
, type_p subclass
)
711 gcc_assert (union_or_struct_p (base
));
712 gcc_assert (union_or_struct_p (subclass
));
714 subclass
->u
.s
.next_sibling_class
= base
->u
.s
.first_subclass
;
715 base
->u
.s
.first_subclass
= subclass
;
718 /* Create and return a new structure with tag NAME at POS with fields
719 FIELDS and options O. The KIND of structure must be one of
720 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
723 new_structure (const char *name
, enum typekind kind
, struct fileloc
*pos
,
724 pair_p fields
, options_p o
, type_p base_class
)
728 lang_bitmap bitmap
= get_lang_bitmap (pos
->file
);
729 bool isunion
= (kind
== TYPE_UNION
);
731 gcc_assert (union_or_struct_p (kind
));
733 for (si
= structures
; si
!= NULL
; si
= si
->next
)
734 if (strcmp (name
, si
->u
.s
.tag
) == 0 && UNION_P (si
) == isunion
)
737 if (si
->kind
== TYPE_LANG_STRUCT
)
741 for (si
= ls
->u
.s
.lang_struct
; si
!= NULL
; si
= si
->next
)
742 if (si
->u
.s
.bitmap
== bitmap
)
745 else if (si
->u
.s
.line
.file
!= NULL
&& si
->u
.s
.bitmap
!= bitmap
)
749 si
= XCNEW (struct type
);
750 memcpy (si
, ls
, sizeof (struct type
));
751 ls
->kind
= TYPE_LANG_STRUCT
;
752 ls
->u
.s
.lang_struct
= si
;
753 ls
->u
.s
.fields
= NULL
;
755 si
->state_number
= -type_count
;
756 si
->pointer_to
= NULL
;
757 si
->u
.s
.lang_struct
= ls
;
762 if (ls
!= NULL
&& s
== NULL
)
765 s
= XCNEW (struct type
);
766 s
->state_number
= -type_count
;
767 s
->next
= ls
->u
.s
.lang_struct
;
768 ls
->u
.s
.lang_struct
= s
;
769 s
->u
.s
.lang_struct
= ls
;
777 s
= XCNEW (struct type
);
778 s
->state_number
= -type_count
;
779 s
->next
= structures
;
783 if (s
->u
.s
.lang_struct
&& (s
->u
.s
.lang_struct
->u
.s
.bitmap
& bitmap
))
785 error_at_line (pos
, "duplicate definition of '%s %s'",
786 isunion
? "union" : "struct", s
->u
.s
.tag
);
787 error_at_line (&s
->u
.s
.line
, "previous definition here");
793 s
->u
.s
.fields
= fields
;
795 s
->u
.s
.bitmap
= bitmap
;
796 if (s
->u
.s
.lang_struct
)
797 s
->u
.s
.lang_struct
->u
.s
.bitmap
|= bitmap
;
798 s
->u
.s
.base_class
= base_class
;
800 add_subclass (base_class
, s
);
805 /* Return the previously-defined structure or union with tag NAME,
806 or a new empty structure or union if none was defined previously.
807 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
811 find_structure (const char *name
, enum typekind kind
)
814 bool isunion
= (kind
== TYPE_UNION
);
816 gcc_assert (kind
== TYPE_UNDEFINED
|| union_or_struct_p (kind
));
818 for (s
= structures
; s
!= NULL
; s
= s
->next
)
819 if (strcmp (name
, s
->u
.s
.tag
) == 0 && UNION_P (s
) == isunion
)
823 s
= XCNEW (struct type
);
824 s
->next
= structures
;
825 s
->state_number
= -type_count
;
833 /* Return the previously-defined parameterized structure for structure
834 T and parameters PARAM, or a new parameterized empty structure or
835 union if none was defined previously. */
838 find_param_structure (type_p t
, type_p param
[NUM_PARAM
])
842 for (res
= param_structs
; res
; res
= res
->next
)
843 if (res
->u
.param_struct
.stru
== t
844 && memcmp (res
->u
.param_struct
.param
, param
,
845 sizeof (type_p
) * NUM_PARAM
) == 0)
850 res
= XCNEW (struct type
);
851 res
->kind
= TYPE_PARAM_STRUCT
;
852 res
->next
= param_structs
;
853 res
->state_number
= -type_count
;
855 res
->u
.param_struct
.stru
= t
;
856 memcpy (res
->u
.param_struct
.param
, param
, sizeof (type_p
) * NUM_PARAM
);
861 /* Return a scalar type with name NAME. */
864 create_scalar_type (const char *name
)
866 if (!strcmp (name
, "char") || !strcmp (name
, "unsigned char"))
869 return &scalar_nonchar
;
873 /* Return a pointer to T. */
876 create_pointer (type_p t
)
880 type_p r
= XCNEW (struct type
);
882 r
->state_number
= -type_count
;
883 r
->kind
= TYPE_POINTER
;
887 return t
->pointer_to
;
890 /* Return an array of length LEN. */
893 create_array (type_p t
, const char *len
)
898 v
= XCNEW (struct type
);
899 v
->kind
= TYPE_ARRAY
;
900 v
->state_number
= -type_count
;
906 /* Return a string options structure with name NAME and info INFO.
907 NEXT is the next option in the chain. */
909 create_string_option (options_p next
, const char *name
, const char *info
)
911 options_p o
= XNEW (struct options
);
912 o
->kind
= OPTION_STRING
;
915 o
->info
.string
= info
;
919 /* Create a type options structure with name NAME and info INFO. NEXT
920 is the next option in the chain. */
922 create_type_option (options_p next
, const char* name
, type_p info
)
924 options_p o
= XNEW (struct options
);
927 o
->kind
= OPTION_TYPE
;
932 /* Create a nested pointer options structure with name NAME and info
933 INFO. NEXT is the next option in the chain. */
935 create_nested_option (options_p next
, const char* name
,
936 struct nested_ptr_data
* info
)
939 o
= XNEW (struct options
);
942 o
->kind
= OPTION_NESTED
;
943 o
->info
.nested
= info
;
947 /* Return an options structure for a "nested_ptr" option. */
949 create_nested_ptr_option (options_p next
, type_p t
,
950 const char *to
, const char *from
)
952 struct nested_ptr_data
*d
= XNEW (struct nested_ptr_data
);
954 d
->type
= adjust_field_type (t
, 0);
956 d
->convert_from
= from
;
957 return create_nested_option (next
, "nested_ptr", d
);
960 /* Add a variable named S of type T with options O defined at POS,
963 note_variable (const char *s
, type_p t
, options_p o
, struct fileloc
*pos
)
966 n
= XNEW (struct pair
);
975 /* Most-general structure field creator. */
977 create_field_all (pair_p next
, type_p type
, const char *name
, options_p opt
,
978 const input_file
*inpf
, int line
)
982 field
= XNEW (struct pair
);
987 field
->line
.file
= inpf
;
988 field
->line
.line
= line
;
992 /* Create a field that came from the source code we are scanning,
993 i.e. we have a 'struct fileloc', and possibly options; also,
994 adjust_field_type should be called. */
996 create_field_at (pair_p next
, type_p type
, const char *name
, options_p opt
,
999 return create_field_all (next
, adjust_field_type (type
, opt
),
1000 name
, opt
, pos
->file
, pos
->line
);
1003 /* Create a fake field with the given type and name. NEXT is the next
1004 field in the chain. */
1005 #define create_field(next,type,name) \
1006 create_field_all (next,type,name, 0, this_file, __LINE__)
1008 /* Like create_field, but the field is only valid when condition COND
1012 create_optional_field_ (pair_p next
, type_p type
, const char *name
,
1013 const char *cond
, int line
)
1016 pair_p union_fields
;
1019 /* Create a fake union type with a single nameless field of type TYPE.
1020 The field has a tag of "1". This allows us to make the presence
1021 of a field of type TYPE depend on some boolean "desc" being true. */
1022 union_fields
= create_field (NULL
, type
, "");
1024 create_string_option (union_fields
->opt
, "dot", "");
1026 create_string_option (union_fields
->opt
, "tag", "1");
1028 new_structure (xasprintf ("%s_%d", "fake_union", id
++), TYPE_UNION
,
1029 &lexer_line
, union_fields
, NULL
, NULL
);
1031 /* Create the field and give it the new fake union type. Add a "desc"
1032 tag that specifies the condition under which the field is valid. */
1033 return create_field_all (next
, union_type
, name
,
1034 create_string_option (0, "desc", cond
),
1038 #define create_optional_field(next,type,name,cond) \
1039 create_optional_field_(next,type,name,cond,__LINE__)
1041 /* Reverse a linked list of 'struct pair's in place. */
1043 nreverse_pairs (pair_p list
)
1045 pair_p prev
= 0, p
, next
;
1046 for (p
= list
; p
; p
= next
)
1056 /* We don't care how long a CONST_DOUBLE is. */
1057 #define CONST_DOUBLE_FORMAT "ww"
1058 /* We don't want to see codes that are only for generator files. */
1059 #undef GENERATOR_FILE
1063 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1069 static const char *const rtx_name
[NUM_RTX_CODE
] = {
1070 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1075 static const char *const rtx_format
[NUM_RTX_CODE
] = {
1076 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1081 static int rtx_next_new
[NUM_RTX_CODE
];
1083 /* We also need codes and names for insn notes (not register notes).
1084 Note that we do *not* bias the note values here. */
1087 #define DEF_INSN_NOTE(NAME) NAME,
1088 #include "insn-notes.def"
1089 #undef DEF_INSN_NOTE
1094 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1095 default field for line number notes. */
1096 static const char *const note_insn_name
[NOTE_INSN_MAX
+ 1] = {
1097 #define DEF_INSN_NOTE(NAME) #NAME,
1098 #include "insn-notes.def"
1099 #undef DEF_INSN_NOTE
1102 #undef CONST_DOUBLE_FORMAT
1103 #define GENERATOR_FILE
1105 /* Generate the contents of the rtx_next array. This really doesn't belong
1106 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1112 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1116 rtx_next_new
[i
] = -1;
1117 if (strncmp (rtx_format
[i
], "uu", 2) == 0)
1118 rtx_next_new
[i
] = 1;
1119 else if (i
== COND_EXEC
|| i
== SET
|| i
== EXPR_LIST
|| i
== INSN_LIST
)
1120 rtx_next_new
[i
] = 1;
1122 for (k
= strlen (rtx_format
[i
]) - 1; k
>= 0; k
--)
1123 if (rtx_format
[i
][k
] == 'e' || rtx_format
[i
][k
] == 'u')
1124 rtx_next_new
[i
] = k
;
1128 /* Write out the contents of the rtx_next array. */
1130 write_rtx_next (void)
1132 outf_p f
= get_output_file_with_visibility (NULL
);
1137 oprintf (f
, "\n/* Used to implement the RTX_NEXT macro. */\n");
1138 oprintf (f
, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1139 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1140 if (rtx_next_new
[i
] == -1)
1141 oprintf (f
, " 0,\n");
1144 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new
[i
]);
1145 oprintf (f
, "};\n");
1148 /* Handle `special("rtx_def")'. This is a special case for field
1149 `fld' of struct rtx_def, which is an array of unions whose values
1150 are based in a complex way on the type of RTL. */
1153 adjust_field_rtx_def (type_p t
, options_p
ARG_UNUSED (opt
))
1158 type_p rtx_tp
, rtvec_tp
, tree_tp
, mem_attrs_tp
, note_union_tp
, scalar_tp
;
1159 type_p basic_block_tp
, reg_attrs_tp
, constant_tp
, symbol_union_tp
;
1161 if (t
->kind
!= TYPE_UNION
)
1163 error_at_line (&lexer_line
,
1164 "special `rtx_def' must be applied to a union");
1165 return &string_type
;
1168 nodot
= create_string_option (NULL
, "dot", "");
1170 rtx_tp
= create_pointer (find_structure ("rtx_def", TYPE_STRUCT
));
1171 rtvec_tp
= create_pointer (find_structure ("rtvec_def", TYPE_STRUCT
));
1172 tree_tp
= create_pointer (find_structure ("tree_node", TYPE_UNION
));
1173 mem_attrs_tp
= create_pointer (find_structure ("mem_attrs", TYPE_STRUCT
));
1175 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT
));
1177 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT
));
1179 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT
));
1180 scalar_tp
= &scalar_nonchar
; /* rtunion int */
1183 pair_p note_flds
= NULL
;
1186 for (c
= 0; c
<= NOTE_INSN_MAX
; c
++)
1191 case NOTE_INSN_DELETED_LABEL
:
1192 case NOTE_INSN_DELETED_DEBUG_LABEL
:
1193 note_flds
= create_field (note_flds
, &string_type
, "rt_str");
1196 case NOTE_INSN_BLOCK_BEG
:
1197 case NOTE_INSN_BLOCK_END
:
1198 note_flds
= create_field (note_flds
, tree_tp
, "rt_tree");
1201 case NOTE_INSN_VAR_LOCATION
:
1202 case NOTE_INSN_CALL_ARG_LOCATION
:
1203 note_flds
= create_field (note_flds
, rtx_tp
, "rt_rtx");
1207 note_flds
= create_field (note_flds
, scalar_tp
, "rt_int");
1210 /* NOTE_INSN_MAX is used as the default field for line
1212 if (c
== NOTE_INSN_MAX
)
1214 create_string_option (nodot
, "default", "");
1217 create_string_option (nodot
, "tag", note_insn_name
[c
]);
1219 note_union_tp
= new_structure ("rtx_def_note_subunion", TYPE_UNION
,
1220 &lexer_line
, note_flds
, NULL
, NULL
);
1222 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1225 sym_flds
= create_field (NULL
, tree_tp
, "rt_tree");
1226 sym_flds
->opt
= create_string_option (nodot
, "default", "");
1227 sym_flds
= create_field (sym_flds
, constant_tp
, "rt_constant");
1228 sym_flds
->opt
= create_string_option (nodot
, "tag", "1");
1229 symbol_union_tp
= new_structure ("rtx_def_symbol_subunion", TYPE_UNION
,
1230 &lexer_line
, sym_flds
, NULL
, NULL
);
1232 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1234 pair_p subfields
= NULL
;
1235 size_t aindex
, nmindex
;
1240 for (aindex
= 0; aindex
< strlen (rtx_format
[i
]); aindex
++)
1243 const char *subname
;
1245 switch (rtx_format
[i
][aindex
])
1256 if (i
== MEM
&& aindex
== 1)
1257 t
= mem_attrs_tp
, subname
= "rt_mem";
1258 else if (i
== JUMP_INSN
&& aindex
== 7)
1259 t
= rtx_tp
, subname
= "rt_rtx";
1260 else if (i
== CODE_LABEL
&& aindex
== 4)
1261 t
= scalar_tp
, subname
= "rt_int";
1262 else if (i
== CODE_LABEL
&& aindex
== 3)
1263 t
= rtx_tp
, subname
= "rt_rtx";
1264 else if (i
== LABEL_REF
&& (aindex
== 1 || aindex
== 2))
1265 t
= rtx_tp
, subname
= "rt_rtx";
1266 else if (i
== NOTE
&& aindex
== 3)
1267 t
= note_union_tp
, subname
= "";
1268 else if (i
== NOTE
&& aindex
== 4)
1269 t
= scalar_tp
, subname
= "rt_int";
1270 else if (i
== NOTE
&& aindex
>= 6)
1271 t
= scalar_tp
, subname
= "rt_int";
1272 else if (i
== ADDR_DIFF_VEC
&& aindex
== 4)
1273 t
= scalar_tp
, subname
= "rt_int";
1274 else if (i
== VALUE
&& aindex
== 0)
1275 t
= scalar_tp
, subname
= "rt_int";
1276 else if (i
== DEBUG_EXPR
&& aindex
== 0)
1277 t
= tree_tp
, subname
= "rt_tree";
1278 else if (i
== REG
&& aindex
== 1)
1279 t
= reg_attrs_tp
, subname
= "rt_reg";
1280 else if (i
== SYMBOL_REF
&& aindex
== 1)
1281 t
= symbol_union_tp
, subname
= "";
1282 else if (i
== JUMP_TABLE_DATA
&& aindex
>= 4)
1283 t
= scalar_tp
, subname
= "rt_int";
1284 else if (i
== BARRIER
&& aindex
>= 2)
1285 t
= scalar_tp
, subname
= "rt_int";
1286 else if (i
== ENTRY_VALUE
&& aindex
== 0)
1287 t
= rtx_tp
, subname
= "rt_rtx";
1292 "rtx type `%s' has `0' in position %lu, can't handle",
1293 rtx_name
[i
], (unsigned long) aindex
);
1315 subname
= "rt_rtvec";
1320 subname
= "rt_tree";
1331 "rtx type `%s' has `%c' in position %lu, can't handle",
1332 rtx_name
[i
], rtx_format
[i
][aindex
],
1333 (unsigned long) aindex
);
1339 subfields
= create_field (subfields
, t
,
1340 xasprintf (".fld[%lu].%s",
1341 (unsigned long) aindex
,
1343 subfields
->opt
= nodot
;
1344 if (t
== note_union_tp
)
1346 create_string_option (subfields
->opt
, "desc",
1348 if (t
== symbol_union_tp
)
1350 create_string_option (subfields
->opt
, "desc",
1351 "CONSTANT_POOL_ADDRESS_P (&%0)");
1354 if (i
== SYMBOL_REF
)
1356 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1358 type_p field_tp
= find_structure ("block_symbol", TYPE_STRUCT
);
1360 = create_optional_field (subfields
, field_tp
, "block_sym",
1361 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1364 sname
= xasprintf ("rtx_def_%s", rtx_name
[i
]);
1365 substruct
= new_structure (sname
, TYPE_STRUCT
, &lexer_line
, subfields
,
1368 ftag
= xstrdup (rtx_name
[i
]);
1369 for (nmindex
= 0; nmindex
< strlen (ftag
); nmindex
++)
1370 ftag
[nmindex
] = TOUPPER (ftag
[nmindex
]);
1371 flds
= create_field (flds
, substruct
, "");
1372 flds
->opt
= create_string_option (nodot
, "tag", ftag
);
1374 return new_structure ("rtx_def_subunion", TYPE_UNION
, &lexer_line
, flds
,
1378 /* Handle `special("tree_exp")'. This is a special case for
1379 field `operands' of struct tree_exp, which although it claims to contain
1380 pointers to trees, actually sometimes contains pointers to RTL too.
1381 Passed T, the old type of the field, and OPT its options. Returns
1382 a new type for the field. */
1385 adjust_field_tree_exp (type_p t
, options_p opt ATTRIBUTE_UNUSED
)
1390 if (t
->kind
!= TYPE_ARRAY
)
1392 error_at_line (&lexer_line
,
1393 "special `tree_exp' must be applied to an array");
1394 return &string_type
;
1397 nodot
= create_string_option (NULL
, "dot", "");
1399 flds
= create_field (NULL
, t
, "");
1400 flds
->opt
= create_string_option (nodot
, "length",
1401 "TREE_OPERAND_LENGTH ((tree) &%0)");
1402 flds
->opt
= create_string_option (flds
->opt
, "default", "");
1404 return new_structure ("tree_exp_subunion", TYPE_UNION
, &lexer_line
, flds
,
1408 /* Perform any special processing on a type T, about to become the type
1409 of a field. Return the appropriate type for the field.
1411 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1412 - Similarly for arrays of pointer-to-char;
1413 - Converts structures for which a parameter is provided to
1415 - Handles "special" options.
1419 adjust_field_type (type_p t
, options_p opt
)
1422 const int pointer_p
= t
->kind
== TYPE_POINTER
;
1423 type_p params
[NUM_PARAM
];
1427 for (i
= 0; i
< NUM_PARAM
; i
++)
1430 for (; opt
; opt
= opt
->next
)
1431 if (strcmp (opt
->name
, "length") == 0)
1434 error_at_line (&lexer_line
, "duplicate `%s' option", opt
->name
);
1435 if (t
->u
.p
->kind
== TYPE_SCALAR
|| t
->u
.p
->kind
== TYPE_STRING
)
1437 error_at_line (&lexer_line
,
1438 "option `%s' may not be applied to "
1439 "arrays of atomic types", opt
->name
);
1443 else if ((strcmp (opt
->name
, "param_is") == 0
1444 || (strncmp (opt
->name
, "param", 5) == 0
1445 && ISDIGIT (opt
->name
[5])
1446 && strcmp (opt
->name
+ 6, "_is") == 0))
1447 && opt
->kind
== OPTION_TYPE
)
1449 int num
= ISDIGIT (opt
->name
[5]) ? opt
->name
[5] - '0' : 0;
1451 if (!union_or_struct_p (t
)
1452 && (t
->kind
!= TYPE_POINTER
|| !union_or_struct_p (t
->u
.p
)))
1454 error_at_line (&lexer_line
,
1455 "option `%s' may only be applied to structures or structure pointers",
1461 if (params
[num
] != NULL
)
1462 error_at_line (&lexer_line
, "duplicate `%s' option", opt
->name
);
1463 if (!ISDIGIT (opt
->name
[5]))
1464 params
[num
] = create_pointer (opt
->info
.type
);
1466 params
[num
] = opt
->info
.type
;
1468 else if (strcmp (opt
->name
, "special") == 0
1469 && opt
->kind
== OPTION_STRING
)
1471 const char *special_name
= opt
->info
.string
;
1472 if (strcmp (special_name
, "tree_exp") == 0)
1473 t
= adjust_field_tree_exp (t
, opt
);
1474 else if (strcmp (special_name
, "rtx_def") == 0)
1475 t
= adjust_field_rtx_def (t
, opt
);
1477 error_at_line (&lexer_line
, "unknown special `%s'", special_name
);
1486 realt
= find_param_structure (t
, params
);
1487 t
= pointer_p
? create_pointer (realt
) : realt
;
1491 && pointer_p
&& t
->u
.p
->kind
== TYPE_SCALAR
&& t
->u
.p
->u
.scalar_is_char
)
1492 return &string_type
;
1493 if (t
->kind
== TYPE_ARRAY
&& t
->u
.a
.p
->kind
== TYPE_POINTER
1494 && t
->u
.a
.p
->u
.p
->kind
== TYPE_SCALAR
1495 && t
->u
.a
.p
->u
.p
->u
.scalar_is_char
)
1496 return create_array (&string_type
, t
->u
.a
.len
);
1502 static void set_gc_used_type (type_p
, enum gc_used_enum
, type_p
*,
1504 static void set_gc_used (pair_p
);
1506 /* Handle OPT for set_gc_used_type. */
1509 process_gc_options (options_p opt
, enum gc_used_enum level
, int *maybe_undef
,
1510 int *pass_param
, int *length
, int *skip
,
1514 for (o
= opt
; o
; o
= o
->next
)
1515 if (strcmp (o
->name
, "ptr_alias") == 0 && level
== GC_POINTED_TO
1516 && o
->kind
== OPTION_TYPE
)
1517 set_gc_used_type (o
->info
.type
,
1518 GC_POINTED_TO
, NULL
);
1519 else if (strcmp (o
->name
, "maybe_undef") == 0)
1521 else if (strcmp (o
->name
, "use_params") == 0)
1523 else if (strcmp (o
->name
, "length") == 0)
1525 else if (strcmp (o
->name
, "skip") == 0)
1527 else if (strcmp (o
->name
, "nested_ptr") == 0
1528 && o
->kind
== OPTION_NESTED
)
1529 *nested_ptr
= ((const struct nested_ptr_data
*) o
->info
.nested
)->type
;
1533 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1535 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1536 are set to GC_UNUSED. Otherwise, an error is emitted for
1537 TYPE_UNDEFINED types. This is used to support user-defined
1538 template types with non-type arguments.
1540 For instance, when we parse a template type with enum arguments
1541 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1542 artificial fields for 'MyType', one for 'AnotherType', the other
1543 one for 'EnumValue'.
1545 At the time that we parse this type we don't know that 'EnumValue'
1546 is really an enum value, so the parser creates a TYPE_UNDEFINED
1547 type for it. Since 'EnumValue' is never resolved to a known
1548 structure, it will stay with TYPE_UNDEFINED.
1550 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1551 'EnumValue'. Generating marking code for it would cause
1552 compilation failures since the marking routines assumes that
1553 'EnumValue' is a type. */
1556 set_gc_used_type (type_p t
, enum gc_used_enum level
, type_p param
[NUM_PARAM
],
1557 bool allow_undefined_types
)
1559 if (t
->gc_used
>= level
)
1568 case TYPE_USER_STRUCT
:
1573 bool allow_undefined_field_types
= (t
->kind
== TYPE_USER_STRUCT
);
1575 process_gc_options (t
->u
.s
.opt
, level
, &dummy
, &dummy
, &dummy
, &dummy
,
1578 if (t
->u
.s
.base_class
)
1579 set_gc_used_type (t
->u
.s
.base_class
, level
, param
,
1580 allow_undefined_types
);
1581 /* Anything pointing to a base class might actually be pointing
1583 for (type_p subclass
= t
->u
.s
.first_subclass
; subclass
;
1584 subclass
= subclass
->u
.s
.next_sibling_class
)
1585 set_gc_used_type (subclass
, level
, param
,
1586 allow_undefined_types
);
1588 FOR_ALL_INHERITED_FIELDS(t
, f
)
1590 int maybe_undef
= 0;
1594 type_p nested_ptr
= NULL
;
1595 process_gc_options (f
->opt
, level
, &maybe_undef
, &pass_param
,
1596 &length
, &skip
, &nested_ptr
);
1598 if (nested_ptr
&& f
->type
->kind
== TYPE_POINTER
)
1599 set_gc_used_type (nested_ptr
, GC_POINTED_TO
,
1600 pass_param
? param
: NULL
);
1601 else if (length
&& f
->type
->kind
== TYPE_POINTER
)
1602 set_gc_used_type (f
->type
->u
.p
, GC_USED
, NULL
);
1603 else if (maybe_undef
&& f
->type
->kind
== TYPE_POINTER
)
1604 set_gc_used_type (f
->type
->u
.p
, GC_MAYBE_POINTED_TO
, NULL
);
1605 else if (pass_param
&& f
->type
->kind
== TYPE_POINTER
&& param
)
1606 set_gc_used_type (find_param_structure (f
->type
->u
.p
, param
),
1607 GC_POINTED_TO
, NULL
);
1609 ; /* target type is not used through this field */
1611 set_gc_used_type (f
->type
, GC_USED
, pass_param
? param
: NULL
,
1612 allow_undefined_field_types
);
1617 case TYPE_UNDEFINED
:
1618 if (level
> GC_UNUSED
)
1620 if (!allow_undefined_types
)
1621 error_at_line (&t
->u
.s
.line
, "undefined type `%s'", t
->u
.s
.tag
);
1622 t
->gc_used
= GC_UNUSED
;
1627 set_gc_used_type (t
->u
.p
, GC_POINTED_TO
, NULL
);
1631 set_gc_used_type (t
->u
.a
.p
, GC_USED
, param
);
1634 case TYPE_LANG_STRUCT
:
1635 for (t
= t
->u
.s
.lang_struct
; t
; t
= t
->next
)
1636 set_gc_used_type (t
, level
, param
);
1639 case TYPE_PARAM_STRUCT
:
1642 for (i
= 0; i
< NUM_PARAM
; i
++)
1643 if (t
->u
.param_struct
.param
[i
] != 0)
1644 set_gc_used_type (t
->u
.param_struct
.param
[i
], GC_USED
, NULL
);
1646 if (t
->u
.param_struct
.stru
->gc_used
== GC_POINTED_TO
)
1647 level
= GC_POINTED_TO
;
1650 t
->u
.param_struct
.stru
->gc_used
= GC_UNUSED
;
1651 set_gc_used_type (t
->u
.param_struct
.stru
, level
,
1652 t
->u
.param_struct
.param
);
1660 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1663 set_gc_used (pair_p variables
)
1667 for (p
= variables
; p
; p
= p
->next
)
1669 set_gc_used_type (p
->type
, GC_USED
, NULL
);
1672 if (verbosity_level
>= 2)
1673 printf ("%s used %d GTY-ed variables\n", progname
, nbvars
);
1676 /* File mapping routines. For each input file, there is one output .c file
1677 (but some output files have many input files), and there is one .h file
1678 for the whole build. */
1680 /* Output file handling. */
1682 /* Create and return an outf_p for a new file for NAME, to be called
1686 create_file (const char *name
, const char *oname
)
1688 static const char *const hdr
[] = {
1689 " Copyright (C) 2004-2014 Free Software Foundation, Inc.\n",
1691 "This file is part of GCC.\n",
1693 "GCC is free software; you can redistribute it and/or modify it under\n",
1694 "the terms of the GNU General Public License as published by the Free\n",
1695 "Software Foundation; either version 3, or (at your option) any later\n",
1698 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1699 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1700 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1701 "for more details.\n",
1703 "You should have received a copy of the GNU General Public License\n",
1704 "along with GCC; see the file COPYING3. If not see\n",
1705 "<http://www.gnu.org/licenses/>. */\n",
1707 "/* This file is machine generated. Do not edit. */\n"
1712 gcc_assert (name
!= NULL
);
1713 gcc_assert (oname
!= NULL
);
1714 f
= XCNEW (struct outf
);
1715 f
->next
= output_files
;
1719 oprintf (f
, "/* Type information for %s.\n", name
);
1720 for (i
= 0; i
< ARRAY_SIZE (hdr
); i
++)
1721 oprintf (f
, "%s", hdr
[i
]);
1725 /* Print, like fprintf, to O.
1726 N.B. You might think this could be implemented more efficiently
1727 with vsnprintf(). Unfortunately, there are C libraries that
1728 provide that function but without the C99 semantics for its return
1729 value, making it impossible to know how much space is required. */
1731 oprintf (outf_p o
, const char *format
, ...)
1737 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1742 va_start (ap
, format
);
1743 slength
= vasprintf (&s
, format
, ap
);
1744 if (s
== NULL
|| (int) slength
< 0)
1745 fatal ("out of memory");
1748 if (o
->bufused
+ slength
> o
->buflength
)
1750 size_t new_len
= o
->buflength
;
1757 while (o
->bufused
+ slength
>= new_len
);
1758 o
->buf
= XRESIZEVEC (char, o
->buf
, new_len
);
1759 o
->buflength
= new_len
;
1761 memcpy (o
->buf
+ o
->bufused
, s
, slength
);
1762 o
->bufused
+= slength
;
1766 /* Open the global header file and the language-specific header files. */
1769 open_base_files (void)
1773 if (nb_plugin_files
> 0 && plugin_files
)
1776 header_file
= create_file ("GCC", "gtype-desc.h");
1778 base_files
= XNEWVEC (outf_p
, num_lang_dirs
);
1780 for (i
= 0; i
< num_lang_dirs
; i
++)
1781 base_files
[i
] = create_file (lang_dir_names
[i
],
1782 xasprintf ("gtype-%s.h", lang_dir_names
[i
]));
1784 /* gtype-desc.c is a little special, so we create it here. */
1786 /* The order of files here matters very much. */
1787 static const char *const ifiles
[] = {
1788 "config.h", "system.h", "coretypes.h", "tm.h",
1789 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1790 "tree.h", "rtl.h", "wide-int.h", "function.h", "insn-config.h", "expr.h",
1791 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1792 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1793 "pointer-set.h", "hash-table.h", "vec.h", "ggc.h", "basic-block.h",
1794 "tree-ssa-alias.h", "internal-fn.h", "gimple-fold.h", "tree-eh.h",
1795 "gimple-expr.h", "is-a.h",
1796 "gimple.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
1797 "tree-phinodes.h", "ssa-iterators.h", "stringpool.h", "tree-ssanames.h",
1798 "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h", "tree-ssa-loop-manip.h",
1799 "tree-ssa-loop-niter.h", "tree-into-ssa.h", "tree-dfa.h",
1800 "tree-ssa.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1801 "except.h", "output.h", "cfgloop.h",
1802 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h",
1803 "ipa-inline.h", "dwarf2out.h", NULL
1805 const char *const *ifp
;
1806 outf_p gtype_desc_c
;
1808 gtype_desc_c
= create_file ("GCC", "gtype-desc.c");
1809 for (ifp
= ifiles
; *ifp
; ifp
++)
1810 oprintf (gtype_desc_c
, "#include \"%s\"\n", *ifp
);
1812 /* Make sure we handle "cfun" specially. */
1813 oprintf (gtype_desc_c
, "\n/* See definition in function.h. */\n");
1814 oprintf (gtype_desc_c
, "#undef cfun\n");
1816 oprintf (gtype_desc_c
,
1818 "/* Types with a \"gcc::\" namespace have it stripped\n"
1819 " during gengtype parsing. Provide a \"using\" directive\n"
1820 " to ensure that the fully-qualified types are found. */\n"
1821 "using namespace gcc;\n");
1825 /* For INPF an input file, return the real basename of INPF, with all
1826 the directory components skipped. */
1829 get_file_realbasename (const input_file
*inpf
)
1831 return lbasename (get_input_file_name (inpf
));
1834 /* For INPF a filename, return the relative path to INPF from
1835 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1838 get_file_srcdir_relative_path (const input_file
*inpf
)
1840 const char *f
= get_input_file_name (inpf
);
1841 if (strlen (f
) > srcdir_len
1842 && IS_DIR_SEPARATOR (f
[srcdir_len
])
1843 && strncmp (f
, srcdir
, srcdir_len
) == 0)
1844 return f
+ srcdir_len
+ 1;
1849 /* For INPF an input_file, return the relative path to INPF from
1850 $(srcdir) if the latter is a prefix in INPF, or the real basename
1851 of INPF otherwise. */
1854 get_file_basename (const input_file
*inpf
)
1856 const char *srcdir_path
= get_file_srcdir_relative_path (inpf
);
1858 return (srcdir_path
!= NULL
) ? srcdir_path
: get_file_realbasename (inpf
);
1861 /* For F a filename, return the lang_dir_names relative index of the language
1862 directory that is a prefix in F, if any, -1 otherwise. */
1865 get_prefix_langdir_index (const char *f
)
1867 size_t f_len
= strlen (f
);
1870 for (lang_index
= 0; lang_index
< num_lang_dirs
; lang_index
++)
1872 const char *langdir
= lang_dir_names
[lang_index
];
1873 size_t langdir_len
= strlen (langdir
);
1875 if (f_len
> langdir_len
1876 && IS_DIR_SEPARATOR (f
[langdir_len
])
1877 && memcmp (f
, langdir
, langdir_len
) == 0)
1884 /* For INPF an input file, return the name of language directory where
1885 F is located, if any, NULL otherwise. */
1888 get_file_langdir (const input_file
*inpf
)
1890 /* Get the relative path to INPF from $(srcdir) and find the
1891 language by comparing the prefix with language directory names.
1892 If INPF is not even srcdir relative, no point in looking
1896 const char *srcdir_relative_path
= get_file_srcdir_relative_path (inpf
);
1899 if (!srcdir_relative_path
)
1902 lang_index
= get_prefix_langdir_index (srcdir_relative_path
);
1903 if (lang_index
< 0 && strncmp (srcdir_relative_path
, "c-family", 8) == 0)
1905 else if (lang_index
>= 0)
1906 r
= lang_dir_names
[lang_index
];
1913 /* The gt- output file name for INPF. */
1916 get_file_gtfilename (const input_file
*inpf
)
1918 /* Cook up an initial version of the gt- file name from the file real
1919 basename and the language name, if any. */
1921 const char *basename
= get_file_realbasename (inpf
);
1922 const char *langdir
= get_file_langdir (inpf
);
1925 (langdir
? xasprintf ("gt-%s-%s", langdir
, basename
)
1926 : xasprintf ("gt-%s", basename
));
1928 /* Then replace all non alphanumerics characters by '-' and change the
1929 extension to ".h". We expect the input filename extension was at least
1930 one character long. */
1934 for (; *s
!= '.'; s
++)
1935 if (!ISALNUM (*s
) && *s
!= '-')
1938 memcpy (s
, ".h", sizeof (".h"));
1943 /* Each input_file has its associated output file outf_p. The
1944 association is computed by the function
1945 get_output_file_with_visibility. The associated file is cached
1946 inside input_file in its inpoutf field, so is really computed only
1947 once. Associated output file paths (i.e. output_name-s) are
1948 computed by a rule based regexp machinery, using the files_rules
1949 array of struct file_rule_st. A for_name is also computed, giving
1950 the source file name for which the output_file is generated; it is
1951 often the last component of the input_file path. */
1955 Regexpr machinery to compute the output_name and for_name-s of each
1956 input_file. We have a sequence of file rules which gives the POSIX
1957 extended regular expression to match an input file path, and two
1958 transformed strings for the corresponding output_name and the
1959 corresponding for_name. The transformed string contain dollars: $0
1960 is replaced by the entire match, $1 is replaced by the substring
1961 matching the first parenthesis in the regexp, etc. And $$ is replaced
1962 by a single verbatim dollar. The rule order is important. The
1963 general case is last, and the particular cases should come before.
1964 An action routine can, when needed, update the out_name & for_name
1965 and/or return the appropriate output file. It is invoked only when a
1966 rule is triggered. When a rule is triggered, the output_name and
1967 for_name are computed using their transform string in while $$, $0,
1968 $1, ... are suitably replaced. If there is an action, it is called.
1969 In some few cases, the action can directly return the outf_p, but
1970 usually it just updates the output_name and for_name so should free
1971 them before replacing them. The get_output_file_with_visibility
1972 function creates an outf_p only once per each output_name, so it
1973 scans the output_files list for previously seen output file names.
1976 /* Signature of actions in file rules. */
1977 typedef outf_p (frul_actionrout_t
) (input_file
*, char**, char**);
1980 struct file_rule_st
{
1981 const char* frul_srcexpr
; /* Source string for regexp. */
1982 int frul_rflags
; /* Flags passed to regcomp, usually
1984 regex_t
* frul_re
; /* Compiled regular expression
1985 obtained by regcomp. */
1986 const char* frul_tr_out
; /* Transformation string for making
1987 * the output_name, with $1 ... $9 for
1988 * subpatterns and $0 for the whole
1989 * matched filename. */
1990 const char* frul_tr_for
; /* Tranformation string for making the
1992 frul_actionrout_t
* frul_action
; /* The action, if non null, is
1993 * called once the rule matches, on
1994 * the transformed out_name &
1995 * for_name. It could change them
1996 * and/or give the output file. */
1999 /* File rule action handling *.h files. */
2000 static outf_p
header_dot_h_frul (input_file
*, char**, char**);
2002 /* File rule action handling *.c files. */
2003 static outf_p
source_dot_c_frul (input_file
*, char**, char**);
2005 #define NULL_REGEX (regex_t*)0
2007 /* The prefix in our regexp-s matching the directory. */
2008 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
2010 #define NULL_FRULACT (frul_actionrout_t*)0
2012 /* The array of our rules governing file name generation. Rules order
2013 matters, so change with extreme care! */
2015 struct file_rule_st files_rules
[] = {
2016 /* The general rule assumes that files in subdirectories belong to a
2017 particular front-end, and files not in subdirectories are shared.
2018 The following rules deal with exceptions - files that are in
2019 subdirectories and yet are shared, and files that are top-level,
2020 but are not shared. */
2022 /* the c-family/ source directory is special. */
2023 { DIR_PREFIX_REGEX
"c-family/([[:alnum:]_-]*)\\.c$",
2024 REG_EXTENDED
, NULL_REGEX
,
2025 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT
},
2027 { DIR_PREFIX_REGEX
"c-family/([[:alnum:]_-]*)\\.h$",
2028 REG_EXTENDED
, NULL_REGEX
,
2029 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT
},
2031 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
2032 { DIR_PREFIX_REGEX
"c/c-lang\\.h$",
2033 REG_EXTENDED
, NULL_REGEX
, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT
},
2035 { DIR_PREFIX_REGEX
"c/c-tree\\.h$",
2036 REG_EXTENDED
, NULL_REGEX
, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT
},
2038 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
2039 { DIR_PREFIX_REGEX
"cp/cp-tree\\.h$",
2040 REG_EXTENDED
, NULL_REGEX
,
2041 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT
},
2043 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
2044 { DIR_PREFIX_REGEX
"cp/decl\\.[ch]$",
2045 REG_EXTENDED
, NULL_REGEX
,
2046 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT
},
2048 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
2049 { DIR_PREFIX_REGEX
"cp/name-lookup\\.h$",
2050 REG_EXTENDED
, NULL_REGEX
,
2051 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT
},
2053 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
2054 { DIR_PREFIX_REGEX
"cp/parser\\.h$",
2055 REG_EXTENDED
, NULL_REGEX
,
2056 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT
},
2058 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
2059 { DIR_PREFIX_REGEX
"objc/objc-act\\.h$",
2060 REG_EXTENDED
, NULL_REGEX
,
2061 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT
},
2063 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
2064 { DIR_PREFIX_REGEX
"objc/objc-map\\.h$",
2065 REG_EXTENDED
, NULL_REGEX
,
2066 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT
},
2068 /* General cases. For header *.h and source *.c or *.cc files, we
2069 * need special actions to handle the language. */
2071 /* Source *.c files are using get_file_gtfilename to compute their
2072 output_name and get_file_basename to compute their for_name
2073 through the source_dot_c_frul action. */
2074 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.c$",
2075 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.c", source_dot_c_frul
},
2077 /* Source *.cc files are using get_file_gtfilename to compute their
2078 output_name and get_file_basename to compute their for_name
2079 through the source_dot_c_frul action. */
2080 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.cc$",
2081 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.cc", source_dot_c_frul
},
2083 /* Common header files get "gtype-desc.c" as their output_name,
2084 * while language specific header files are handled specially. So
2085 * we need the header_dot_h_frul action. */
2086 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.h$",
2087 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.h", header_dot_h_frul
},
2089 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.in$",
2090 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.in", NULL_FRULACT
},
2092 /* Mandatory null last entry signaling end of rules. */
2093 {NULL
, 0, NULL_REGEX
, NULL
, NULL
, NULL_FRULACT
}
2096 /* Special file rules action for handling *.h header files. It gives
2097 "gtype-desc.c" for common headers and corresponding output
2098 files for language-specific header files. */
2100 header_dot_h_frul (input_file
* inpf
, char**poutname
,
2101 char**pforname ATTRIBUTE_UNUSED
)
2103 const char *basename
= 0;
2105 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2106 (void*) inpf
, get_input_file_name (inpf
),
2107 *poutname
, *pforname
);
2108 basename
= get_file_basename (inpf
);
2109 lang_index
= get_prefix_langdir_index (basename
);
2110 DBGPRINTF ("basename %s lang_index %d", basename
, lang_index
);
2112 if (lang_index
>= 0)
2114 /* The header is language specific. Given output_name &
2115 for_name remains unchanged. The base_files array gives the
2117 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2118 (void*) base_files
[lang_index
],
2119 (base_files
[lang_index
])->name
);
2120 return base_files
[lang_index
];
2124 /* The header is common to all front-end languages. So
2125 output_name is "gtype-desc.c" file. The calling function
2126 get_output_file_with_visibility will find its outf_p. */
2128 *poutname
= xstrdup ("gtype-desc.c");
2129 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2130 get_input_file_name (inpf
));
2136 /* Special file rules action for handling *.c source files using
2137 * get_file_gtfilename to compute their output_name and
2138 * get_file_basename to compute their for_name. The output_name is
2139 * gt-<LANG>-<BASE>.h for language specific source files, and
2140 * gt-<BASE>.h for common source files. */
2142 source_dot_c_frul (input_file
* inpf
, char**poutname
, char**pforname
)
2144 char *newbasename
= CONST_CAST (char*, get_file_basename (inpf
));
2145 char *newoutname
= CONST_CAST (char*, get_file_gtfilename (inpf
));
2146 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2147 (void*) inpf
, get_input_file_name (inpf
),
2148 *poutname
, *pforname
);
2149 DBGPRINTF ("newoutname %s", newoutname
);
2150 DBGPRINTF ("newbasename %s", newbasename
);
2153 *poutname
= newoutname
;
2154 *pforname
= newbasename
;
2158 /* Utility function for get_output_file_with_visibility which returns
2159 * a malloc-ed substituted string using TRS on matching of the FILNAM
2160 * file name, using the PMATCH array. */
2162 matching_file_name_substitute (const char *filnam
, regmatch_t pmatch
[10],
2165 struct obstack str_obstack
;
2167 char *rawstr
= NULL
;
2168 const char *pt
= NULL
;
2169 DBGPRINTF ("filnam %s", filnam
);
2170 obstack_init (&str_obstack
);
2171 for (pt
= trs
; *pt
; pt
++) {
2177 /* A double dollar $$ is substituted by a single verbatim
2178 dollar, but who really uses dollar signs in file
2180 obstack_1grow (&str_obstack
, '$');
2182 else if (ISDIGIT (pt
[1]))
2184 /* Handle $0 $1 ... $9 by appropriate substitution. */
2185 int dolnum
= pt
[1] - '0';
2186 int so
= pmatch
[dolnum
].rm_so
;
2187 int eo
= pmatch
[dolnum
].rm_eo
;
2188 DBGPRINTF ("so=%d eo=%d dolnum=%d", so
, eo
, dolnum
);
2189 if (so
>=0 && eo
>=so
)
2190 obstack_grow (&str_obstack
, filnam
+ so
, eo
- so
);
2194 /* This can happen only when files_rules is buggy! */
2197 /* Always skip the character after the dollar. */
2201 obstack_1grow (&str_obstack
, c
);
2203 obstack_1grow (&str_obstack
, '\0');
2204 rawstr
= XOBFINISH (&str_obstack
, char *);
2205 str
= xstrdup (rawstr
);
2206 obstack_free (&str_obstack
, NULL
);
2207 DBGPRINTF ("matched replacement %s", str
);
2213 /* An output file, suitable for definitions, that can see declarations
2214 made in INPF and is linked into every language that uses INPF.
2215 Since the result is cached inside INPF, that argument cannot be
2216 declared constant, but is "almost" constant. */
2219 get_output_file_with_visibility (input_file
*inpf
)
2222 char *for_name
= NULL
;
2223 char *output_name
= NULL
;
2224 const char* inpfname
;
2226 /* This can happen when we need a file with visibility on a
2227 structure that we've never seen. We have to just hope that it's
2228 globally visible. */
2230 inpf
= system_h_file
;
2232 /* The result is cached in INPF, so return it if already known. */
2234 return inpf
->inpoutf
;
2236 /* In plugin mode, return NULL unless the input_file is one of the
2241 for (i
= 0; i
< nb_plugin_files
; i
++)
2242 if (inpf
== plugin_files
[i
])
2244 inpf
->inpoutf
= plugin_output
;
2245 return plugin_output
;
2251 inpfname
= get_input_file_name (inpf
);
2253 /* Try each rule in sequence in files_rules until one is triggered. */
2256 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2257 (void*) inpf
, inpfname
);
2259 for (; files_rules
[rulix
].frul_srcexpr
!= NULL
; rulix
++)
2261 DBGPRINTF ("rulix#%d srcexpr %s",
2262 rulix
, files_rules
[rulix
].frul_srcexpr
);
2264 if (!files_rules
[rulix
].frul_re
)
2266 /* Compile the regexpr lazily. */
2268 files_rules
[rulix
].frul_re
= XCNEW (regex_t
);
2269 err
= regcomp (files_rules
[rulix
].frul_re
,
2270 files_rules
[rulix
].frul_srcexpr
,
2271 files_rules
[rulix
].frul_rflags
);
2274 /* The regular expression compilation fails only when
2275 file_rules is buggy. */
2283 /* Match the regexpr and trigger the rule if matched. */
2285 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2287 regmatch_t pmatch
[10];
2288 memset (pmatch
, 0, sizeof (pmatch
));
2289 if (!regexec (files_rules
[rulix
].frul_re
,
2290 inpfname
, 10, pmatch
, 0))
2292 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2293 (void*) inpf
, inpfname
, rulix
,
2294 files_rules
[rulix
].frul_srcexpr
);
2296 matching_file_name_substitute (inpfname
, pmatch
,
2297 files_rules
[rulix
].frul_tr_for
);
2298 DBGPRINTF ("for_name %s", for_name
);
2300 matching_file_name_substitute (inpfname
, pmatch
,
2301 files_rules
[rulix
].frul_tr_out
);
2302 DBGPRINTF ("output_name %s", output_name
);
2303 if (files_rules
[rulix
].frul_action
)
2305 /* Invoke our action routine. */
2307 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2308 rulix
, output_name
, for_name
);
2310 (files_rules
[rulix
].frul_action
) (inpf
,
2311 &output_name
, &for_name
);
2312 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2313 rulix
, (void*)of
, output_name
, for_name
);
2314 /* If the action routine returned something, give it back
2315 immediately and cache it in inpf. */
2322 /* The rule matched, and had no action, or that action did
2323 not return any output file but could have changed the
2324 output_name or for_name. We break out of the loop on the
2330 /* The regexpr did not match. */
2331 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2332 rulix
, inpfname
, files_rules
[rulix
].frul_srcexpr
);
2338 if (!output_name
|| !for_name
)
2340 /* This should not be possible, and could only happen if the
2341 files_rules is incomplete or buggy. */
2342 fatal ("failed to compute output name for %s", inpfname
);
2345 /* Look through to see if we've ever seen this output filename
2346 before. If found, cache the result in inpf. */
2347 for (r
= output_files
; r
; r
= r
->next
)
2348 if (filename_cmp (r
->name
, output_name
) == 0)
2351 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r
,
2352 output_name
, for_name
);
2356 /* If not found, create it, and cache it in inpf. */
2357 r
= create_file (for_name
, output_name
);
2359 gcc_assert (r
&& r
->name
);
2360 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r
,
2361 output_name
, for_name
);
2368 /* The name of an output file, suitable for definitions, that can see
2369 declarations made in INPF and is linked into every language that
2373 get_output_file_name (input_file
* inpf
)
2375 outf_p o
= get_output_file_with_visibility (inpf
);
2381 /* Check if existing file is equal to the in memory buffer. */
2384 is_file_equal (outf_p of
)
2386 FILE *newfile
= fopen (of
->name
, "r");
2389 if (newfile
== NULL
)
2393 for (i
= 0; i
< of
->bufused
; i
++)
2396 ch
= fgetc (newfile
);
2397 if (ch
== EOF
|| ch
!= (unsigned char) of
->buf
[i
])
2403 if (equal
&& EOF
!= fgetc (newfile
))
2409 /* Copy the output to its final destination,
2410 but don't unnecessarily change modification times. */
2413 close_output_files (void)
2415 int nbwrittenfiles
= 0;
2418 for (of
= output_files
; of
; of
= of
->next
)
2420 if (!is_file_equal (of
))
2422 FILE *newfile
= NULL
;
2423 char *backupname
= NULL
;
2424 /* Back up the old version of the output file gt-FOO.c as
2425 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2428 backupname
= concat (backup_dir
, "/",
2429 lbasename (of
->name
), "~", NULL
);
2430 if (!access (of
->name
, F_OK
) && rename (of
->name
, backupname
))
2431 fatal ("failed to back up %s as %s: %s",
2432 of
->name
, backupname
, xstrerror (errno
));
2435 newfile
= fopen (of
->name
, "w");
2436 if (newfile
== NULL
)
2437 fatal ("opening output file %s: %s", of
->name
, xstrerror (errno
));
2438 if (fwrite (of
->buf
, 1, of
->bufused
, newfile
) != of
->bufused
)
2439 fatal ("writing output file %s: %s", of
->name
, xstrerror (errno
));
2440 if (fclose (newfile
) != 0)
2441 fatal ("closing output file %s: %s", of
->name
, xstrerror (errno
));
2443 if (verbosity_level
>= 2 && backupname
)
2444 printf ("%s wrote #%-3d %s backed-up in %s\n",
2445 progname
, nbwrittenfiles
, of
->name
, backupname
);
2446 else if (verbosity_level
>= 1)
2447 printf ("%s write #%-3d %s\n", progname
, nbwrittenfiles
, of
->name
);
2452 /* output file remains unchanged. */
2453 if (verbosity_level
>= 2)
2454 printf ("%s keep %s\n", progname
, of
->name
);
2458 of
->bufused
= of
->buflength
= 0;
2460 if (verbosity_level
>= 1)
2461 printf ("%s wrote %d files.\n", progname
, nbwrittenfiles
);
2468 const input_file
* file
;
2472 struct walk_type_data
;
2474 /* For scalars and strings, given the item in 'val'.
2475 For structures, given a pointer to the item in 'val'.
2476 For misc. pointers, given the item in 'val'.
2478 typedef void (*process_field_fn
) (type_p f
, const struct walk_type_data
* p
);
2479 typedef void (*func_name_fn
) (type_p s
, const struct walk_type_data
* p
);
2481 /* Parameters for write_types. */
2483 struct write_types_data
2486 const char *param_prefix
;
2487 const char *subfield_marker_routine
;
2488 const char *marker_routine
;
2489 const char *reorder_note_routine
;
2490 const char *comment
;
2491 int skip_hooks
; /* skip hook generation if non zero */
2492 enum write_types_kinds kind
;
2495 static void output_escaped_param (struct walk_type_data
*d
,
2496 const char *, const char *);
2497 static void output_mangled_typename (outf_p
, const_type_p
);
2498 static void walk_type (type_p t
, struct walk_type_data
*d
);
2499 static void write_func_for_structure (type_p orig_s
, type_p s
, type_p
*param
,
2500 const struct write_types_data
*wtd
);
2501 static void write_types_process_field
2502 (type_p f
, const struct walk_type_data
*d
);
2503 static void write_types (outf_p output_header
,
2505 type_p param_structs
,
2506 const struct write_types_data
*wtd
);
2507 static void write_types_local_process_field
2508 (type_p f
, const struct walk_type_data
*d
);
2509 static void write_local_func_for_structure
2510 (const_type_p orig_s
, type_p s
, type_p
*param
);
2511 static void write_local (outf_p output_header
,
2512 type_p structures
, type_p param_structs
);
2513 static int contains_scalar_p (type_p t
);
2514 static void put_mangled_filename (outf_p
, const input_file
*);
2515 static void finish_root_table (struct flist
*flp
, const char *pfx
,
2516 const char *tname
, const char *lastname
,
2518 static void write_root (outf_p
, pair_p
, type_p
, const char *, int,
2519 struct fileloc
*, const char *, bool);
2520 static void write_array (outf_p f
, pair_p v
,
2521 const struct write_types_data
*wtd
);
2522 static void write_roots (pair_p
, bool);
2524 /* Parameters for walk_type. */
2526 struct walk_type_data
2528 process_field_fn process_field
;
2533 const char *prev_val
[4];
2536 const struct fileloc
*line
;
2541 const char *reorder_fn
;
2543 bool fn_wants_lvalue
;
2551 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2552 pre-processor identifier to use in a #define directive. This replaces
2553 special characters used in C++ identifiers like '>', '<' and ':' with
2556 If no C++ special characters are found in TYPE_NAME, return
2557 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2558 characters replaced with '_'. In this case, the caller is
2559 responsible for freeing the allocated string. */
2562 filter_type_name (const char *type_name
)
2564 if (strchr (type_name
, '<') || strchr (type_name
, ':'))
2567 char *s
= xstrdup (type_name
);
2568 for (i
= 0; i
< strlen (s
); i
++)
2569 if (s
[i
] == '<' || s
[i
] == '>' || s
[i
] == ':' || s
[i
] == ','
2579 /* Print a mangled name representing T to OF. */
2582 output_mangled_typename (outf_p of
, const_type_p t
)
2590 case TYPE_UNDEFINED
:
2595 output_mangled_typename (of
, t
->u
.p
);
2605 case TYPE_LANG_STRUCT
:
2606 case TYPE_USER_STRUCT
:
2608 /* For references to classes within an inheritance hierarchy,
2609 only ever reference the ultimate base class, since only
2610 it will have gt_ functions. */
2611 t
= get_ultimate_base_class (t
);
2612 const char *id_for_tag
= filter_type_name (t
->u
.s
.tag
);
2613 oprintf (of
, "%lu%s", (unsigned long) strlen (id_for_tag
),
2615 if (id_for_tag
!= t
->u
.s
.tag
)
2616 free (CONST_CAST (char *, id_for_tag
));
2619 case TYPE_PARAM_STRUCT
:
2622 for (i
= 0; i
< NUM_PARAM
; i
++)
2623 if (t
->u
.param_struct
.param
[i
] != NULL
)
2624 output_mangled_typename (of
, t
->u
.param_struct
.param
[i
]);
2625 output_mangled_typename (of
, t
->u
.param_struct
.stru
);
2633 /* Print PARAM to D->OF processing escapes. D->VAL references the
2634 current object, D->PREV_VAL the object containing the current
2635 object, ONAME is the name of the option and D->LINE is used to
2636 print error messages. */
2639 output_escaped_param (struct walk_type_data
*d
, const char *param
,
2644 for (p
= param
; *p
; p
++)
2646 oprintf (d
->of
, "%c", *p
);
2651 oprintf (d
->of
, "(%s)", d
->prev_val
[2]);
2654 oprintf (d
->of
, "(%s)", d
->prev_val
[0]);
2657 oprintf (d
->of
, "(%s)", d
->prev_val
[1]);
2661 const char *pp
= d
->val
+ strlen (d
->val
);
2662 while (pp
[-1] == ']')
2665 oprintf (d
->of
, "%s", pp
);
2669 error_at_line (d
->line
, "`%s' option contains bad escape %c%c",
2675 get_string_option (options_p opt
, const char *key
)
2677 for (; opt
; opt
= opt
->next
)
2678 if (strcmp (opt
->name
, key
) == 0)
2679 return opt
->info
.string
;
2683 /* Machinery for avoiding duplicate tags within switch statements. */
2687 struct seen_tag
*next
;
2691 already_seen_tag (struct seen_tag
*seen_tags
, const char *tag
)
2693 /* Linear search, so O(n^2), but n is currently small. */
2696 if (!strcmp (seen_tags
->tag
, tag
))
2698 seen_tags
= seen_tags
->next
;
2700 /* Not yet seen this tag. */
2705 mark_tag_as_seen (struct seen_tag
**seen_tags
, const char *tag
)
2707 /* Add to front of linked list. */
2708 struct seen_tag
*new_node
= XCNEW (struct seen_tag
);
2709 new_node
->tag
= tag
;
2710 new_node
->next
= *seen_tags
;
2711 *seen_tags
= new_node
;
2715 walk_subclasses (type_p base
, struct walk_type_data
*d
,
2716 struct seen_tag
**seen_tags
)
2718 for (type_p sub
= base
->u
.s
.first_subclass
; sub
!= NULL
;
2719 sub
= sub
->u
.s
.next_sibling_class
)
2721 const char *type_tag
= get_string_option (sub
->u
.s
.opt
, "tag");
2722 if (type_tag
&& !already_seen_tag (*seen_tags
, type_tag
))
2724 mark_tag_as_seen (seen_tags
, type_tag
);
2725 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", type_tag
);
2727 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2729 oprintf (d
->of
, "%*s%s *sub = static_cast <%s *> (x);\n",
2730 d
->indent
, "", sub
->u
.s
.tag
, sub
->u
.s
.tag
);
2731 const char *old_val
= d
->val
;
2736 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2737 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
2740 walk_subclasses (sub
, d
, seen_tags
);
2744 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2745 which is of type T. Write code to D->OF to constrain execution (at
2746 the point that D->PROCESS_FIELD is called) to the appropriate
2747 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2748 pointers to those objects. D->PREV_VAL lists the objects
2749 containing the current object, D->OPT is a list of options to
2750 apply, D->INDENT is the current indentation level, D->LINE is used
2751 to print error messages, D->BITMAP indicates which languages to
2752 print the structure for, and D->PARAM is the current parameter
2753 (from an enclosing param_is option). */
2756 walk_type (type_p t
, struct walk_type_data
*d
)
2758 const char *length
= NULL
;
2759 const char *desc
= NULL
;
2760 const char *type_tag
= NULL
;
2761 int maybe_undef_p
= 0;
2762 int use_param_num
= -1;
2763 int use_params_p
= 0;
2766 const struct nested_ptr_data
*nested_ptr_d
= NULL
;
2768 d
->needs_cast_p
= false;
2769 for (oo
= d
->opt
; oo
; oo
= oo
->next
)
2770 if (strcmp (oo
->name
, "length") == 0 && oo
->kind
== OPTION_STRING
)
2771 length
= oo
->info
.string
;
2772 else if (strcmp (oo
->name
, "maybe_undef") == 0)
2774 else if (strncmp (oo
->name
, "use_param", 9) == 0
2775 && (oo
->name
[9] == '\0' || ISDIGIT (oo
->name
[9])))
2776 use_param_num
= oo
->name
[9] == '\0' ? 0 : oo
->name
[9] - '0';
2777 else if (strcmp (oo
->name
, "use_params") == 0)
2779 else if (strcmp (oo
->name
, "desc") == 0 && oo
->kind
== OPTION_STRING
)
2780 desc
= oo
->info
.string
;
2781 else if (strcmp (oo
->name
, "mark_hook") == 0)
2783 else if (strcmp (oo
->name
, "nested_ptr") == 0
2784 && oo
->kind
== OPTION_NESTED
)
2785 nested_ptr_d
= (const struct nested_ptr_data
*) oo
->info
.nested
;
2786 else if (strcmp (oo
->name
, "dot") == 0)
2788 else if (strcmp (oo
->name
, "tag") == 0)
2789 type_tag
= oo
->info
.string
;
2790 else if (strcmp (oo
->name
, "special") == 0)
2792 else if (strcmp (oo
->name
, "skip") == 0)
2794 else if (strcmp (oo
->name
, "atomic") == 0)
2796 else if (strcmp (oo
->name
, "default") == 0)
2798 else if (strcmp (oo
->name
, "param_is") == 0)
2800 else if (strncmp (oo
->name
, "param", 5) == 0
2801 && ISDIGIT (oo
->name
[5]) && strcmp (oo
->name
+ 6, "_is") == 0)
2803 else if (strcmp (oo
->name
, "chain_next") == 0)
2805 else if (strcmp (oo
->name
, "chain_prev") == 0)
2807 else if (strcmp (oo
->name
, "chain_circular") == 0)
2809 else if (strcmp (oo
->name
, "reorder") == 0)
2811 else if (strcmp (oo
->name
, "variable_size") == 0)
2814 error_at_line (d
->line
, "unknown option `%s'\n", oo
->name
);
2821 int pointer_p
= t
->kind
== TYPE_POINTER
;
2825 if (!union_or_struct_p (t
))
2826 error_at_line (d
->line
, "`use_params' option on unimplemented type");
2828 t
= find_param_structure (t
, d
->param
);
2830 t
= create_pointer (t
);
2833 if (use_param_num
!= -1)
2835 if (d
->param
!= NULL
&& d
->param
[use_param_num
] != NULL
)
2837 type_p nt
= d
->param
[use_param_num
];
2839 if (t
->kind
== TYPE_ARRAY
)
2840 nt
= create_array (nt
, t
->u
.a
.len
);
2841 else if (length
!= NULL
&& t
->kind
== TYPE_POINTER
)
2842 nt
= create_pointer (nt
);
2843 d
->needs_cast_p
= (t
->kind
!= TYPE_POINTER
2844 && (nt
->kind
== TYPE_POINTER
2845 || nt
->kind
== TYPE_STRING
));
2849 error_at_line (d
->line
, "no parameter defined for `%s'", d
->val
);
2853 && (t
->kind
!= TYPE_POINTER
|| !union_or_struct_p (t
->u
.p
)))
2855 error_at_line (d
->line
,
2856 "field `%s' has invalid option `maybe_undef_p'\n",
2861 if (atomic_p
&& (t
->kind
!= TYPE_POINTER
) && (t
->kind
!= TYPE_STRING
))
2863 error_at_line (d
->line
, "field `%s' has invalid option `atomic'\n", d
->val
);
2871 d
->process_field (t
, d
);
2876 d
->in_ptr_field
= true;
2877 if (maybe_undef_p
&& t
->u
.p
->u
.s
.line
.file
== NULL
)
2879 oprintf (d
->of
, "%*sgcc_assert (!%s);\n", d
->indent
, "", d
->val
);
2883 /* If a pointer type is marked as "atomic", we process the
2884 field itself, but we don't walk the data that they point to.
2886 There are two main cases where we walk types: to mark
2887 pointers that are reachable, and to relocate pointers when
2888 writing a PCH file. In both cases, an atomic pointer is
2889 itself marked or relocated, but the memory that it points
2890 to is left untouched. In the case of PCH, that memory will
2891 be read/written unchanged to the PCH file. */
2894 oprintf (d
->of
, "%*sif (%s != NULL) {\n", d
->indent
, "", d
->val
);
2896 d
->process_field (t
, d
);
2898 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2904 if (!union_or_struct_p (t
->u
.p
)
2905 && t
->u
.p
->kind
!= TYPE_PARAM_STRUCT
)
2907 error_at_line (d
->line
,
2908 "field `%s' is pointer to unimplemented type",
2915 const char *oldprevval2
= d
->prev_val
[2];
2917 if (!union_or_struct_p (nested_ptr_d
->type
))
2919 error_at_line (d
->line
,
2920 "field `%s' has invalid "
2921 "option `nested_ptr'\n", d
->val
);
2925 d
->prev_val
[2] = d
->val
;
2926 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2928 d
->val
= xasprintf ("x%d", d
->counter
++);
2929 oprintf (d
->of
, "%*s%s %s * %s%s =\n", d
->indent
, "",
2930 (nested_ptr_d
->type
->kind
== TYPE_UNION
2931 ? "union" : "struct"),
2932 nested_ptr_d
->type
->u
.s
.tag
,
2933 d
->fn_wants_lvalue
? "" : "const ", d
->val
);
2934 oprintf (d
->of
, "%*s", d
->indent
+ 2, "");
2935 output_escaped_param (d
, nested_ptr_d
->convert_from
,
2937 oprintf (d
->of
, ";\n");
2939 d
->process_field (nested_ptr_d
->type
, d
);
2941 if (d
->fn_wants_lvalue
)
2943 oprintf (d
->of
, "%*s%s = ", d
->indent
, "",
2945 d
->prev_val
[2] = d
->val
;
2946 output_escaped_param (d
, nested_ptr_d
->convert_to
,
2948 oprintf (d
->of
, ";\n");
2952 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2953 d
->val
= d
->prev_val
[2];
2954 d
->prev_val
[2] = oldprevval2
;
2957 d
->process_field (t
->u
.p
, d
);
2961 int loopcounter
= d
->loopcounter
;
2962 const char *oldval
= d
->val
;
2963 const char *oldprevval3
= d
->prev_val
[3];
2966 oprintf (d
->of
, "%*sif (%s != NULL) {\n", d
->indent
, "", d
->val
);
2968 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
2969 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != (size_t)(", d
->indent
,
2970 "", loopcounter
, loopcounter
);
2971 if (!d
->in_record_p
)
2972 output_escaped_param (d
, length
, "length");
2974 oprintf (d
->of
, "l%d", loopcounter
);
2975 if (d
->have_this_obj
)
2976 /* Try to unswitch loops (see PR53880). */
2977 oprintf (d
->of
, ") && ((void *)%s == this_obj", oldval
);
2978 oprintf (d
->of
, "); i%d++) {\n", loopcounter
);
2980 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
2982 d
->prev_val
[3] = oldval
;
2983 walk_type (t
->u
.p
, d
);
2986 d
->prev_val
[3] = oldprevval3
;
2989 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2990 d
->process_field (t
, d
);
2992 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2994 d
->in_ptr_field
= false;
3001 const char *oldval
= d
->val
;
3004 /* If it's an array of scalars, we optimize by not generating
3006 if (t
->u
.a
.p
->kind
== TYPE_SCALAR
)
3010 loopcounter
= d
->loopcounter
;
3012 loopcounter
= d
->counter
++;
3014 /* When walking an array, compute the length and store it in a
3015 local variable before walking the array elements, instead of
3016 recomputing the length expression each time through the loop.
3017 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
3018 where the length is stored in the first array element,
3019 because otherwise that operand can get overwritten on the
3021 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
3023 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
3024 if (!d
->in_record_p
|| !length
)
3026 oprintf (d
->of
, "%*ssize_t l%d = (size_t)(",
3027 d
->indent
, "", loopcounter
);
3029 output_escaped_param (d
, length
, "length");
3031 oprintf (d
->of
, "%s", t
->u
.a
.len
);
3032 oprintf (d
->of
, ");\n");
3035 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
3037 loopcounter
, loopcounter
, loopcounter
, loopcounter
);
3039 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
3041 walk_type (t
->u
.a
.p
, d
);
3046 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3048 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3056 const char *oldval
= d
->val
;
3057 const char *oldprevval1
= d
->prev_val
[1];
3058 const char *oldprevval2
= d
->prev_val
[2];
3059 const char *struct_mark_hook
= NULL
;
3060 const int union_p
= t
->kind
== TYPE_UNION
;
3061 int seen_default_p
= 0;
3063 int lengths_seen
= 0;
3065 bool any_length_seen
= false;
3067 if (!t
->u
.s
.line
.file
)
3068 error_at_line (d
->line
, "incomplete structure `%s'", t
->u
.s
.tag
);
3070 if ((d
->bitmap
& t
->u
.s
.bitmap
) != d
->bitmap
)
3072 error_at_line (d
->line
,
3073 "structure `%s' defined for mismatching languages",
3075 error_at_line (&t
->u
.s
.line
, "one structure defined here");
3078 /* Some things may also be defined in the structure's options. */
3079 for (o
= t
->u
.s
.opt
; o
; o
= o
->next
)
3080 if (!desc
&& strcmp (o
->name
, "desc") == 0
3081 && o
->kind
== OPTION_STRING
)
3082 desc
= o
->info
.string
;
3083 else if (!struct_mark_hook
&& strcmp (o
->name
, "mark_hook") == 0
3084 && o
->kind
== OPTION_STRING
)
3085 struct_mark_hook
= o
->info
.string
;
3087 if (struct_mark_hook
)
3088 oprintf (d
->of
, "%*s%s (&%s);\n",
3089 d
->indent
, "", struct_mark_hook
, oldval
);
3091 d
->prev_val
[2] = oldval
;
3092 d
->prev_val
[1] = oldprevval2
;
3097 error_at_line (d
->line
,
3098 "missing `desc' option for union `%s'",
3102 oprintf (d
->of
, "%*sswitch ((int) (", d
->indent
, "");
3103 output_escaped_param (d
, desc
, "desc");
3104 oprintf (d
->of
, "))\n");
3106 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
3110 /* We have a "desc" option on a struct, signifying the
3111 base class within a GC-managed inheritance hierarchy.
3112 The current code specialcases the base class, then walks
3113 into subclasses, recursing into this routine to handle them.
3114 This organization requires the base class to have a case in
3115 the switch statement, and hence a tag value is mandatory
3116 for the base class. This restriction could be removed, but
3117 it would require some restructing of this code. */
3120 error_at_line (d
->line
,
3121 "missing `tag' option for type `%s'",
3124 oprintf (d
->of
, "%*sswitch ((int) (", d
->indent
, "");
3125 output_escaped_param (d
, desc
, "desc");
3126 oprintf (d
->of
, "))\n");
3128 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
3129 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", type_tag
);
3133 FOR_ALL_INHERITED_FIELDS (t
, f
)
3137 const char *fieldlength
= NULL
;
3139 d
->reorder_fn
= NULL
;
3140 for (oo
= f
->opt
; oo
; oo
= oo
->next
)
3141 if (strcmp (oo
->name
, "skip") == 0)
3143 else if (strcmp (oo
->name
, "length") == 0
3144 && oo
->kind
== OPTION_STRING
)
3145 fieldlength
= oo
->info
.string
;
3155 if (!any_length_seen
)
3157 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
3160 any_length_seen
= true;
3162 oprintf (d
->of
, "%*ssize_t l%d = (size_t)(",
3163 d
->indent
, "", d
->counter
- 1);
3164 output_escaped_param (d
, fieldlength
, "length");
3165 oprintf (d
->of
, ");\n");
3169 endcounter
= d
->counter
;
3171 FOR_ALL_INHERITED_FIELDS (t
, f
)
3174 const char *dot
= ".";
3175 const char *tagid
= NULL
;
3178 int use_param_p
= 0;
3179 const char *fieldlength
= NULL
;
3182 d
->reorder_fn
= NULL
;
3183 for (oo
= f
->opt
; oo
; oo
= oo
->next
)
3184 if (strcmp (oo
->name
, "dot") == 0
3185 && oo
->kind
== OPTION_STRING
)
3186 dot
= oo
->info
.string
;
3187 else if (strcmp (oo
->name
, "tag") == 0
3188 && oo
->kind
== OPTION_STRING
)
3189 tagid
= oo
->info
.string
;
3190 else if (strcmp (oo
->name
, "skip") == 0)
3192 else if (strcmp (oo
->name
, "default") == 0)
3194 else if (strcmp (oo
->name
, "reorder") == 0
3195 && oo
->kind
== OPTION_STRING
)
3196 d
->reorder_fn
= oo
->info
.string
;
3197 else if (strncmp (oo
->name
, "use_param", 9) == 0
3198 && (oo
->name
[9] == '\0' || ISDIGIT (oo
->name
[9])))
3200 else if (strcmp (oo
->name
, "length") == 0
3201 && oo
->kind
== OPTION_STRING
)
3202 fieldlength
= oo
->info
.string
;
3207 if (union_p
&& tagid
)
3209 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", tagid
);
3212 else if (union_p
&& default_p
)
3214 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
3218 else if (!union_p
&& (default_p
|| tagid
))
3219 error_at_line (d
->line
,
3220 "can't use `%s' outside a union on field `%s'",
3221 default_p
? "default" : "tag", f
->name
);
3222 else if (union_p
&& !(default_p
|| tagid
)
3223 && f
->type
->kind
== TYPE_SCALAR
)
3226 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3227 get_input_file_name (d
->line
->file
), d
->line
->line
,
3231 else if (union_p
&& !(default_p
|| tagid
))
3232 error_at_line (d
->line
,
3233 "field `%s' is missing `tag' or `default' option",
3238 d
->loopcounter
= endcounter
- lengths_seen
--;
3242 d
->val
= newval
= xasprintf ("%s%s%s", oldval
, dot
, f
->name
);
3244 d
->used_length
= false;
3245 d
->in_record_p
= !union_p
;
3247 if (union_p
&& use_param_p
&& d
->param
== NULL
)
3248 oprintf (d
->of
, "%*sgcc_unreachable ();\n", d
->indent
, "");
3250 walk_type (f
->type
, d
);
3252 d
->in_record_p
= false;
3258 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
3262 d
->reorder_fn
= NULL
;
3265 d
->prev_val
[1] = oldprevval1
;
3266 d
->prev_val
[2] = oldprevval2
;
3268 if (union_p
&& !seen_default_p
)
3270 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
3271 oprintf (d
->of
, "%*s break;\n", d
->indent
, "");
3274 if (desc
&& !union_p
)
3276 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
3281 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3286 /* Add cases to handle subclasses. */
3287 struct seen_tag
*tags
= NULL
;
3288 walk_subclasses (t
, d
, &tags
);
3290 /* Ensure that if someone forgets a "tag" option that we don't
3291 silent fail to traverse that subclass's fields. */
3292 if (!seen_default_p
)
3294 oprintf (d
->of
, "%*s/* Unrecognized tag value. */\n",
3296 oprintf (d
->of
, "%*sdefault: gcc_unreachable (); \n",
3300 /* End of the switch statement */
3301 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3304 if (any_length_seen
)
3307 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3312 case TYPE_LANG_STRUCT
:
3315 for (nt
= t
->u
.s
.lang_struct
; nt
; nt
= nt
->next
)
3316 if ((d
->bitmap
& nt
->u
.s
.bitmap
) == d
->bitmap
)
3319 error_at_line (d
->line
, "structure `%s' differs between languages",
3326 case TYPE_PARAM_STRUCT
:
3328 type_p
*oldparam
= d
->param
;
3330 d
->param
= t
->u
.param_struct
.param
;
3331 walk_type (t
->u
.param_struct
.stru
, d
);
3332 d
->param
= oldparam
;
3336 case TYPE_USER_STRUCT
:
3337 d
->process_field (t
, d
);
3341 case TYPE_UNDEFINED
:
3346 /* process_field routine for marking routines. */
3349 write_types_process_field (type_p f
, const struct walk_type_data
*d
)
3351 const struct write_types_data
*wtd
;
3352 const char *cast
= d
->needs_cast_p
? "(void *)" : "";
3353 wtd
= (const struct write_types_data
*) d
->cookie
;
3358 case TYPE_UNDEFINED
:
3361 oprintf (d
->of
, "%*s%s (%s%s", d
->indent
, "",
3362 wtd
->subfield_marker_routine
, cast
, d
->val
);
3363 if (wtd
->param_prefix
)
3365 if (f
->u
.p
->kind
== TYPE_SCALAR
)
3366 /* The current type is a pointer to a scalar (so not
3367 considered like a pointer to instances of user defined
3368 types) and we are seeing it; it means we must be even
3369 more careful about the second argument of the
3370 SUBFIELD_MARKER_ROUTINE call. That argument must
3371 always be the instance of the type for which
3372 write_func_for_structure was called - this really is
3373 what the function SUBFIELD_MARKER_ROUTINE expects.
3374 That is, it must be an instance of the ORIG_S type
3375 parameter of write_func_for_structure. The convention
3376 is that that argument must be "x" in that case (as set
3377 by write_func_for_structure). The problem is, we can't
3378 count on d->prev_val[3] to be always set to "x" in that
3379 case. Sometimes walk_type can set it to something else
3380 (to e.g cooperate with write_array when called from
3381 write_roots). So let's set it to "x" here then. */
3382 oprintf (d
->of
, ", x");
3384 oprintf (d
->of
, ", %s", d
->prev_val
[3]);
3387 oprintf (d
->of
, ", gt_%s_", wtd
->param_prefix
);
3388 output_mangled_typename (d
->of
, d
->orig_s
);
3391 oprintf (d
->of
, ", gt_%sa_%s", wtd
->param_prefix
, d
->prev_val
[0]);
3393 oprintf (d
->of
, ");\n");
3394 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
3395 oprintf (d
->of
, "%*s%s (%s%s, %s, %s);\n", d
->indent
, "",
3396 wtd
->reorder_note_routine
, cast
, d
->val
,
3397 d
->prev_val
[3], d
->reorder_fn
);
3403 case TYPE_LANG_STRUCT
:
3404 case TYPE_PARAM_STRUCT
:
3405 case TYPE_USER_STRUCT
:
3406 if (f
->kind
== TYPE_USER_STRUCT
&& !d
->in_ptr_field
)
3408 /* If F is a user-defined type and the field is not a
3409 pointer to the type, then we should not generate the
3410 standard pointer-marking code. All we need to do is call
3411 the user-provided marking function to process the fields
3413 oprintf (d
->of
, "%*sgt_%sx (&(%s));\n", d
->indent
, "", wtd
->prefix
,
3418 oprintf (d
->of
, "%*sgt_%s_", d
->indent
, "", wtd
->prefix
);
3419 output_mangled_typename (d
->of
, f
);
3420 oprintf (d
->of
, " (%s%s);\n", cast
, d
->val
);
3421 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
3422 oprintf (d
->of
, "%*s%s (%s%s, %s%s, %s);\n", d
->indent
, "",
3423 wtd
->reorder_note_routine
, cast
, d
->val
, cast
, d
->val
,
3436 /* Return an output file that is suitable for definitions which can
3437 reference struct S */
3440 get_output_file_for_structure (const_type_p s
, type_p
*param
)
3442 const input_file
*fn
;
3445 gcc_assert (union_or_struct_p (s
));
3446 fn
= s
->u
.s
.line
.file
;
3448 /* This is a hack, and not the good kind either. */
3449 for (i
= NUM_PARAM
- 1; i
>= 0; i
--)
3450 if (param
&& param
[i
] && param
[i
]->kind
== TYPE_POINTER
3451 && union_or_struct_p (param
[i
]->u
.p
))
3452 fn
= param
[i
]->u
.p
->u
.s
.line
.file
;
3454 /* The call to get_output_file_with_visibility may update fn by
3455 caching its result inside, so we need the CONST_CAST. */
3456 return get_output_file_with_visibility (CONST_CAST (input_file
*, fn
));
3460 /* Returns the specifier keyword for a string or union type S, empty string
3464 get_type_specifier (const type_p s
)
3466 if (s
->kind
== TYPE_STRUCT
)
3468 else if (s
->kind
== TYPE_LANG_STRUCT
)
3469 return get_type_specifier (s
->u
.s
.lang_struct
);
3470 else if (s
->kind
== TYPE_UNION
)
3476 /* Emits a declaration for type TY (assumed to be a union or a
3477 structure) on stream OUT. */
3480 write_type_decl (outf_p out
, type_p ty
)
3482 if (union_or_struct_p (ty
))
3483 oprintf (out
, "%s%s", get_type_specifier (ty
), ty
->u
.s
.tag
);
3484 else if (ty
->kind
== TYPE_SCALAR
)
3486 if (ty
->u
.scalar_is_char
)
3487 oprintf (out
, "const char");
3489 oprintf (out
, "void");
3491 else if (ty
->kind
== TYPE_POINTER
)
3493 write_type_decl (out
, ty
->u
.p
);
3494 oprintf (out
, " *");
3496 else if (ty
->kind
== TYPE_ARRAY
)
3498 write_type_decl (out
, ty
->u
.a
.p
);
3499 oprintf (out
, " *");
3501 else if (ty
->kind
== TYPE_STRING
)
3503 oprintf (out
, "const char *");
3510 /* Write on OF the name of the marker function for structure S. PREFIX
3511 is the prefix to use (to distinguish ggc from pch markers). */
3514 write_marker_function_name (outf_p of
, type_p s
, const char *prefix
)
3516 if (union_or_struct_p (s
))
3518 const char *id_for_tag
= filter_type_name (s
->u
.s
.tag
);
3519 oprintf (of
, "gt_%sx_%s", prefix
, id_for_tag
);
3520 if (id_for_tag
!= s
->u
.s
.tag
)
3521 free (CONST_CAST (char *, id_for_tag
));
3523 else if (s
->kind
== TYPE_PARAM_STRUCT
)
3525 oprintf (of
, "gt_%s_", prefix
);
3526 output_mangled_typename (of
, s
);
3532 /* Write on OF a user-callable routine to act as an entry point for
3533 the marking routine for S, generated by write_func_for_structure.
3534 WTD distinguishes between ggc and pch markers. */
3537 write_user_func_for_structure_ptr (outf_p of
, type_p s
, const write_types_data
*wtd
)
3539 /* Parameterized structures are not supported in user markers. There
3540 is no way for the marker function to know which specific type
3541 to use to generate the call to the void * entry point. For
3542 instance, a marker for struct htab may need to call different
3543 routines to mark the fields, depending on the paramN_is attributes.
3545 A user-defined marker that accepts 'struct htab' as its argument
3546 would not know which variant to call. Generating several entry
3547 points accepting 'struct htab' would cause multiply-defined
3548 errors during compilation. */
3549 gcc_assert (union_or_struct_p (s
));
3551 type_p alias_of
= NULL
;
3552 for (options_p opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3553 if (strcmp (opt
->name
, "ptr_alias") == 0)
3555 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3556 we do not generate marking code for ORIG_S here. Instead, a
3557 forwarder #define in gtype-desc.h will cause every call to its
3558 marker to call the target of this alias.
3560 However, we still want to create a user entry code for the
3561 aliased type. So, if ALIAS_OF is set, we only generate the
3562 user-callable marker function. */
3563 alias_of
= opt
->info
.type
;
3567 DBGPRINTF ("write_user_func_for_structure_ptr: %s %s", s
->u
.s
.tag
,
3570 /* Only write the function once. */
3571 if (s
->u
.s
.wrote_user_func_for_ptr
[wtd
->kind
])
3573 s
->u
.s
.wrote_user_func_for_ptr
[wtd
->kind
] = true;
3575 oprintf (of
, "\nvoid\n");
3576 oprintf (of
, "gt_%sx (", wtd
->prefix
);
3577 write_type_decl (of
, s
);
3578 oprintf (of
, " *& x)\n");
3579 oprintf (of
, "{\n");
3580 oprintf (of
, " if (x)\n ");
3581 write_marker_function_name (of
,
3582 alias_of
? alias_of
: get_ultimate_base_class (s
),
3584 oprintf (of
, " ((void *) x);\n");
3585 oprintf (of
, "}\n");
3589 /* Write a function to mark all the fields of type S on OF. PREFIX
3590 and D are as in write_user_marking_functions. */
3593 write_user_func_for_structure_body (type_p s
, const char *prefix
,
3594 struct walk_type_data
*d
)
3596 oprintf (d
->of
, "\nvoid\n");
3597 oprintf (d
->of
, "gt_%sx (", prefix
);
3598 write_type_decl (d
->of
, s
);
3599 oprintf (d
->of
, "& x_r ATTRIBUTE_UNUSED)\n");
3600 oprintf (d
->of
, "{\n");
3601 oprintf (d
->of
, " ");
3602 write_type_decl (d
->of
, s
);
3603 oprintf (d
->of
, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3607 oprintf (d
->of
, "}\n");
3611 /* Emit the user-callable functions needed to mark all the types used
3612 by the user structure S. PREFIX is the prefix to use to
3613 distinguish ggc and pch markers. D contains data needed to pass to
3614 walk_type when traversing the fields of a type.
3616 For every type T referenced by S, two routines are generated: one
3617 that takes 'T *', marks the pointer and calls the second routine,
3618 which just marks the fields of T. */
3621 write_user_marking_functions (type_p s
,
3622 const write_types_data
*w
,
3623 struct walk_type_data
*d
)
3625 gcc_assert (s
->kind
== TYPE_USER_STRUCT
);
3627 for (pair_p fld
= s
->u
.s
.fields
; fld
; fld
= fld
->next
)
3629 type_p fld_type
= fld
->type
;
3630 if (fld_type
->kind
== TYPE_POINTER
)
3632 type_p pointed_to_type
= fld_type
->u
.p
;
3633 if (union_or_struct_p (pointed_to_type
))
3634 write_user_func_for_structure_ptr (d
->of
, pointed_to_type
, w
);
3636 else if (union_or_struct_p (fld_type
))
3637 write_user_func_for_structure_body (fld_type
, w
->prefix
, d
);
3642 /* For S, a structure that's part of ORIG_S, and using parameters
3643 PARAM, write out a routine that:
3644 - Takes a parameter, a void * but actually of type *S
3645 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3646 field of S or its substructures and (in some cases) things
3647 that are pointed to by S. */
3650 write_func_for_structure (type_p orig_s
, type_p s
, type_p
*param
,
3651 const struct write_types_data
*wtd
)
3653 const char *chain_next
= NULL
;
3654 const char *chain_prev
= NULL
;
3655 const char *chain_circular
= NULL
;
3656 const char *mark_hook_name
= NULL
;
3658 struct walk_type_data d
;
3660 if (s
->u
.s
.base_class
)
3662 /* Verify that the base class has a "desc", since otherwise
3663 the traversal hooks there won't attempt to visit fields of
3664 subclasses such as this one. */
3665 const_type_p ubc
= get_ultimate_base_class (s
);
3666 if ((!opts_have (ubc
->u
.s
.opt
, "user")
3667 && !opts_have (ubc
->u
.s
.opt
, "desc")))
3668 error_at_line (&s
->u
.s
.line
,
3669 ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
3670 ", but '%s' lacks a discriminator 'desc' option"),
3671 s
->u
.s
.tag
, ubc
->u
.s
.tag
, ubc
->u
.s
.tag
);
3673 /* Don't write fns for subclasses, only for the ultimate base class
3674 within an inheritance hierarchy. */
3678 memset (&d
, 0, sizeof (d
));
3679 d
.of
= get_output_file_for_structure (s
, param
);
3680 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3681 if (strcmp (opt
->name
, "chain_next") == 0
3682 && opt
->kind
== OPTION_STRING
)
3683 chain_next
= opt
->info
.string
;
3684 else if (strcmp (opt
->name
, "chain_prev") == 0
3685 && opt
->kind
== OPTION_STRING
)
3686 chain_prev
= opt
->info
.string
;
3687 else if (strcmp (opt
->name
, "chain_circular") == 0
3688 && opt
->kind
== OPTION_STRING
)
3689 chain_circular
= opt
->info
.string
;
3690 else if (strcmp (opt
->name
, "mark_hook") == 0
3691 && opt
->kind
== OPTION_STRING
)
3692 mark_hook_name
= opt
->info
.string
;
3693 if (chain_prev
!= NULL
&& chain_next
== NULL
)
3694 error_at_line (&s
->u
.s
.line
, "chain_prev without chain_next");
3695 if (chain_circular
!= NULL
&& chain_next
!= NULL
)
3696 error_at_line (&s
->u
.s
.line
, "chain_circular with chain_next");
3697 if (chain_circular
!= NULL
)
3698 chain_next
= chain_circular
;
3700 d
.process_field
= write_types_process_field
;
3704 d
.line
= &s
->u
.s
.line
;
3705 d
.bitmap
= s
->u
.s
.bitmap
;
3707 d
.prev_val
[0] = "*x";
3708 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
3709 d
.prev_val
[3] = "x";
3711 d
.have_this_obj
= false;
3713 oprintf (d
.of
, "\n");
3714 oprintf (d
.of
, "void\n");
3715 write_marker_function_name (d
.of
, orig_s
, wtd
->prefix
);
3716 oprintf (d
.of
, " (void *x_p)\n");
3717 oprintf (d
.of
, "{\n ");
3718 write_type_decl (d
.of
, s
);
3719 oprintf (d
.of
, " * %sx = (", chain_next
== NULL
? "const " : "");
3720 write_type_decl (d
.of
, s
);
3721 oprintf (d
.of
, " *)x_p;\n");
3722 if (chain_next
!= NULL
)
3724 /* TYPE_USER_STRUCTs should not occur here. These structures
3725 are completely handled by user code. */
3726 gcc_assert (orig_s
->kind
!= TYPE_USER_STRUCT
);
3728 oprintf (d
.of
, " ");
3729 write_type_decl (d
.of
, s
);
3730 oprintf (d
.of
, " * xlimit = x;\n");
3732 if (chain_next
== NULL
)
3734 oprintf (d
.of
, " if (%s (x", wtd
->marker_routine
);
3735 if (wtd
->param_prefix
)
3737 oprintf (d
.of
, ", x, gt_%s_", wtd
->param_prefix
);
3738 output_mangled_typename (d
.of
, orig_s
);
3740 oprintf (d
.of
, "))\n");
3744 if (chain_circular
!= NULL
)
3745 oprintf (d
.of
, " if (!%s (xlimit", wtd
->marker_routine
);
3747 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
3748 if (wtd
->param_prefix
)
3750 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
3751 output_mangled_typename (d
.of
, orig_s
);
3753 oprintf (d
.of
, "))\n");
3754 if (chain_circular
!= NULL
)
3755 oprintf (d
.of
, " return;\n do\n");
3756 if (mark_hook_name
&& !wtd
->skip_hooks
)
3758 oprintf (d
.of
, " {\n");
3759 oprintf (d
.of
, " %s (xlimit);\n ", mark_hook_name
);
3761 oprintf (d
.of
, " xlimit = (");
3762 d
.prev_val
[2] = "*xlimit";
3763 output_escaped_param (&d
, chain_next
, "chain_next");
3764 oprintf (d
.of
, ");\n");
3765 if (mark_hook_name
&& !wtd
->skip_hooks
)
3766 oprintf (d
.of
, " }\n");
3767 if (chain_prev
!= NULL
)
3769 oprintf (d
.of
, " if (x != xlimit)\n");
3770 oprintf (d
.of
, " for (;;)\n");
3771 oprintf (d
.of
, " {\n");
3772 oprintf (d
.of
, " %s %s * const xprev = (",
3773 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
3775 d
.prev_val
[2] = "*x";
3776 output_escaped_param (&d
, chain_prev
, "chain_prev");
3777 oprintf (d
.of
, ");\n");
3778 oprintf (d
.of
, " if (xprev == NULL) break;\n");
3779 oprintf (d
.of
, " x = xprev;\n");
3780 oprintf (d
.of
, " (void) %s (xprev", wtd
->marker_routine
);
3781 if (wtd
->param_prefix
)
3783 oprintf (d
.of
, ", xprev, gt_%s_", wtd
->param_prefix
);
3784 output_mangled_typename (d
.of
, orig_s
);
3786 oprintf (d
.of
, ");\n");
3787 oprintf (d
.of
, " }\n");
3789 if (chain_circular
!= NULL
)
3791 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
3792 if (wtd
->param_prefix
)
3794 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
3795 output_mangled_typename (d
.of
, orig_s
);
3797 oprintf (d
.of
, "));\n");
3798 if (mark_hook_name
&& !wtd
->skip_hooks
)
3799 oprintf (d
.of
, " %s (xlimit);\n", mark_hook_name
);
3800 oprintf (d
.of
, " do\n");
3803 oprintf (d
.of
, " while (x != xlimit)\n");
3805 oprintf (d
.of
, " {\n");
3806 if (mark_hook_name
&& chain_next
== NULL
&& !wtd
->skip_hooks
)
3808 oprintf (d
.of
, " %s (x);\n", mark_hook_name
);
3811 d
.prev_val
[2] = "*x";
3813 if (orig_s
->kind
!= TYPE_USER_STRUCT
)
3817 /* User structures have no fields to walk. Simply generate a call
3818 to the user-provided structure marker. */
3819 oprintf (d
.of
, "%*sgt_%sx (x);\n", d
.indent
, "", wtd
->prefix
);
3822 if (chain_next
!= NULL
)
3824 oprintf (d
.of
, " x = (");
3825 output_escaped_param (&d
, chain_next
, "chain_next");
3826 oprintf (d
.of
, ");\n");
3829 oprintf (d
.of
, " }\n");
3830 if (chain_circular
!= NULL
)
3831 oprintf (d
.of
, " while (x != xlimit);\n");
3832 oprintf (d
.of
, "}\n");
3834 if (orig_s
->kind
== TYPE_USER_STRUCT
)
3835 write_user_marking_functions (orig_s
, wtd
, &d
);
3839 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3842 write_types (outf_p output_header
, type_p structures
, type_p param_structs
,
3843 const struct write_types_data
*wtd
)
3845 int nbfun
= 0; /* Count the emitted functions. */
3848 oprintf (output_header
, "\n/* %s*/\n", wtd
->comment
);
3850 /* We first emit the macros and the declarations. Functions' code is
3851 emitted afterwards. This is needed in plugin mode. */
3852 oprintf (output_header
, "/* Macros and declarations. */\n");
3853 for (s
= structures
; s
; s
= s
->next
)
3854 /* Do not emit handlers for derived classes; we only ever deal with
3855 the ultimate base class within an inheritance hierarchy. */
3856 if ((s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
3857 && !s
->u
.s
.base_class
)
3861 if (s
->gc_used
== GC_MAYBE_POINTED_TO
&& s
->u
.s
.line
.file
== NULL
)
3864 const char *s_id_for_tag
= filter_type_name (s
->u
.s
.tag
);
3866 oprintf (output_header
, "#define gt_%s_", wtd
->prefix
);
3867 output_mangled_typename (output_header
, s
);
3868 oprintf (output_header
, "(X) do { \\\n");
3869 oprintf (output_header
,
3870 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd
->prefix
,
3872 oprintf (output_header
, " } while (0)\n");
3874 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3875 if (strcmp (opt
->name
, "ptr_alias") == 0
3876 && opt
->kind
== OPTION_TYPE
)
3878 const_type_p
const t
= (const_type_p
) opt
->info
.type
;
3879 if (t
->kind
== TYPE_STRUCT
3880 || t
->kind
== TYPE_UNION
|| t
->kind
== TYPE_LANG_STRUCT
)
3882 const char *t_id_for_tag
= filter_type_name (t
->u
.s
.tag
);
3883 oprintf (output_header
,
3884 "#define gt_%sx_%s gt_%sx_%s\n",
3885 wtd
->prefix
, s
->u
.s
.tag
, wtd
->prefix
, t_id_for_tag
);
3886 if (t_id_for_tag
!= t
->u
.s
.tag
)
3887 free (CONST_CAST (char *, t_id_for_tag
));
3890 error_at_line (&s
->u
.s
.line
,
3891 "structure alias is not a structure");
3897 /* Declare the marker procedure only once. */
3898 oprintf (output_header
,
3899 "extern void gt_%sx_%s (void *);\n",
3900 wtd
->prefix
, s_id_for_tag
);
3902 if (s_id_for_tag
!= s
->u
.s
.tag
)
3903 free (CONST_CAST (char *, s_id_for_tag
));
3905 if (s
->u
.s
.line
.file
== NULL
)
3907 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
3913 for (s
= param_structs
; s
; s
= s
->next
)
3914 if (s
->gc_used
== GC_POINTED_TO
)
3916 type_p stru
= s
->u
.param_struct
.stru
;
3918 /* Declare the marker procedure. */
3919 oprintf (output_header
, "extern void gt_%s_", wtd
->prefix
);
3920 output_mangled_typename (output_header
, s
);
3921 oprintf (output_header
, " (void *);\n");
3923 if (stru
->u
.s
.line
.file
== NULL
)
3925 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
3931 /* At last we emit the functions code. */
3932 oprintf (output_header
, "\n/* functions code */\n");
3933 for (s
= structures
; s
; s
= s
->next
)
3934 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
3938 if (s
->gc_used
== GC_MAYBE_POINTED_TO
&& s
->u
.s
.line
.file
== NULL
)
3940 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3941 if (strcmp (opt
->name
, "ptr_alias") == 0)
3946 if (s
->kind
== TYPE_LANG_STRUCT
)
3949 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
3952 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3953 nbfun
, (void*) ss
, ss
->u
.s
.tag
);
3954 write_func_for_structure (s
, ss
, NULL
, wtd
);
3960 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3961 nbfun
, (void*) s
, s
->u
.s
.tag
);
3962 write_func_for_structure (s
, s
, NULL
, wtd
);
3967 /* Structure s is not possibly pointed to, so can be ignored. */
3968 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3969 (void*)s
, s
->u
.s
.tag
,
3973 for (s
= param_structs
; s
; s
= s
->next
)
3974 if (s
->gc_used
== GC_POINTED_TO
)
3976 type_p
*param
= s
->u
.param_struct
.param
;
3977 type_p stru
= s
->u
.param_struct
.stru
;
3978 if (stru
->u
.s
.line
.file
== NULL
)
3980 if (stru
->kind
== TYPE_LANG_STRUCT
)
3983 for (ss
= stru
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
3986 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
3987 nbfun
, (void*) ss
, ss
->u
.s
.tag
);
3988 write_func_for_structure (s
, ss
, param
, wtd
);
3994 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
3996 (void*) stru
, stru
->u
.s
.tag
);
3997 write_func_for_structure (s
, stru
, param
, wtd
);
4002 /* Param structure s is not pointed to, so should be ignored. */
4003 DBGPRINTF ("ignored s @ %p", (void*)s
);
4005 if (verbosity_level
>= 2)
4006 printf ("%s emitted %d routines for %s\n",
4007 progname
, nbfun
, wtd
->comment
);
4010 static const struct write_types_data ggc_wtd
= {
4011 "ggc_m", NULL
, "ggc_mark", "ggc_test_and_set_mark", NULL
,
4012 "GC marker procedures. ",
4016 static const struct write_types_data pch_wtd
= {
4017 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
4018 "gt_pch_note_reorder",
4019 "PCH type-walking procedures. ",
4023 /* Write out the local pointer-walking routines. */
4025 /* process_field routine for local pointer-walking for user-callable
4026 routines. The difference between this and
4027 write_types_local_process_field is that, in this case, we do not
4028 need to check whether the given pointer matches the address of the
4029 parent structure. This check was already generated by the call
4030 to gt_pch_nx in the main gt_pch_p_*() function that is calling
4034 write_types_local_user_process_field (type_p f
, const struct walk_type_data
*d
)
4041 case TYPE_LANG_STRUCT
:
4042 case TYPE_PARAM_STRUCT
:
4044 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
4047 case TYPE_USER_STRUCT
:
4048 if (d
->in_ptr_field
)
4049 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
4051 oprintf (d
->of
, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4052 d
->indent
, "", d
->val
);
4060 case TYPE_UNDEFINED
:
4066 /* Write a function to PCH walk all the fields of type S on OF.
4067 D contains data needed by walk_type to recurse into the fields of S. */
4070 write_pch_user_walking_for_structure_body (type_p s
, struct walk_type_data
*d
)
4072 oprintf (d
->of
, "\nvoid\n");
4073 oprintf (d
->of
, "gt_pch_nx (");
4074 write_type_decl (d
->of
, s
);
4075 oprintf (d
->of
, "* x ATTRIBUTE_UNUSED,\n"
4076 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4077 "\tATTRIBUTE_UNUSED void *cookie)\n");
4078 oprintf (d
->of
, "{\n");
4081 d
->process_field
= write_types_local_user_process_field
;
4083 oprintf (d
->of
, "}\n");
4087 /* Emit the user-callable functions needed to mark all the types used
4088 by the user structure S. PREFIX is the prefix to use to
4089 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
4090 chain_next option defined. D contains data needed to pass to
4091 walk_type when traversing the fields of a type.
4093 For every type T referenced by S, two routines are generated: one
4094 that takes 'T *', marks the pointer and calls the second routine,
4095 which just marks the fields of T. */
4098 write_pch_user_walking_functions (type_p s
, struct walk_type_data
*d
)
4100 gcc_assert (s
->kind
== TYPE_USER_STRUCT
);
4102 for (pair_p fld
= s
->u
.s
.fields
; fld
; fld
= fld
->next
)
4104 type_p fld_type
= fld
->type
;
4105 if (union_or_struct_p (fld_type
))
4106 write_pch_user_walking_for_structure_body (fld_type
, d
);
4111 /* process_field routine for local pointer-walking. */
4114 write_types_local_process_field (type_p f
, const struct walk_type_data
*d
)
4116 gcc_assert (d
->have_this_obj
);
4122 case TYPE_LANG_STRUCT
:
4123 case TYPE_PARAM_STRUCT
:
4125 oprintf (d
->of
, "%*sif ((void *)(%s) == this_obj)\n", d
->indent
, "",
4127 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
4130 case TYPE_USER_STRUCT
:
4131 oprintf (d
->of
, "%*sif ((void *)(%s) == this_obj)\n", d
->indent
, "",
4133 if (d
->in_ptr_field
)
4134 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
4136 oprintf (d
->of
, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4137 d
->indent
, "", d
->val
);
4145 case TYPE_UNDEFINED
:
4151 /* For S, a structure that's part of ORIG_S, and using parameters
4152 PARAM, write out a routine that:
4153 - Is of type gt_note_pointers
4154 - Calls PROCESS_FIELD on each field of S or its substructures.
4158 write_local_func_for_structure (const_type_p orig_s
, type_p s
, type_p
*param
)
4160 struct walk_type_data d
;
4162 /* Don't write fns for subclasses, only for the ultimate base class
4163 within an inheritance hierarchy. */
4164 if (s
->u
.s
.base_class
)
4167 memset (&d
, 0, sizeof (d
));
4168 d
.of
= get_output_file_for_structure (s
, param
);
4169 d
.process_field
= write_types_local_process_field
;
4171 d
.line
= &s
->u
.s
.line
;
4172 d
.bitmap
= s
->u
.s
.bitmap
;
4174 d
.prev_val
[0] = d
.prev_val
[2] = "*x";
4175 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
4176 d
.prev_val
[3] = "x";
4178 d
.fn_wants_lvalue
= true;
4180 oprintf (d
.of
, "\n");
4181 oprintf (d
.of
, "void\n");
4182 oprintf (d
.of
, "gt_pch_p_");
4183 output_mangled_typename (d
.of
, orig_s
);
4184 oprintf (d
.of
, " (ATTRIBUTE_UNUSED void *this_obj,\n"
4186 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4187 "\tATTRIBUTE_UNUSED void *cookie)\n");
4188 oprintf (d
.of
, "{\n");
4189 oprintf (d
.of
, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
4190 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
,
4191 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
4193 d
.have_this_obj
= true;
4195 if (s
->kind
!= TYPE_USER_STRUCT
)
4199 /* User structures have no fields to walk. Simply generate a
4200 call to the user-provided PCH walker. */
4201 oprintf (d
.of
, "%*sif ((void *)(%s) == this_obj)\n", d
.indent
, "",
4203 oprintf (d
.of
, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4204 d
.indent
, "", d
.val
);
4207 oprintf (d
.of
, "}\n");
4209 /* Write user-callable entry points for the PCH walking routines. */
4210 if (orig_s
->kind
== TYPE_USER_STRUCT
)
4211 write_pch_user_walking_functions (s
, &d
);
4214 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
4217 write_local (outf_p output_header
, type_p structures
, type_p param_structs
)
4224 oprintf (output_header
, "\n/* Local pointer-walking routines. */\n");
4225 for (s
= structures
; s
; s
= s
->next
)
4226 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
4230 if (s
->u
.s
.line
.file
== NULL
)
4232 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
4233 if (strcmp (opt
->name
, "ptr_alias") == 0
4234 && opt
->kind
== OPTION_TYPE
)
4236 const_type_p
const t
= (const_type_p
) opt
->info
.type
;
4237 if (t
->kind
== TYPE_STRUCT
4238 || t
->kind
== TYPE_UNION
|| t
->kind
== TYPE_LANG_STRUCT
)
4240 oprintf (output_header
, "#define gt_pch_p_");
4241 output_mangled_typename (output_header
, s
);
4242 oprintf (output_header
, " gt_pch_p_");
4243 output_mangled_typename (output_header
, t
);
4244 oprintf (output_header
, "\n");
4247 error_at_line (&s
->u
.s
.line
,
4248 "structure alias is not a structure");
4254 /* Declare the marker procedure only once. */
4255 oprintf (output_header
, "extern void gt_pch_p_");
4256 output_mangled_typename (output_header
, s
);
4257 oprintf (output_header
,
4258 "\n (void *, void *, gt_pointer_operator, void *);\n");
4260 if (s
->kind
== TYPE_LANG_STRUCT
)
4263 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
4264 write_local_func_for_structure (s
, ss
, NULL
);
4267 write_local_func_for_structure (s
, s
, NULL
);
4270 for (s
= param_structs
; s
; s
= s
->next
)
4271 if (s
->gc_used
== GC_POINTED_TO
)
4273 type_p
*param
= s
->u
.param_struct
.param
;
4274 type_p stru
= s
->u
.param_struct
.stru
;
4276 /* Declare the marker procedure. */
4277 oprintf (output_header
, "extern void gt_pch_p_");
4278 output_mangled_typename (output_header
, s
);
4279 oprintf (output_header
,
4280 "\n (void *, void *, gt_pointer_operator, void *);\n");
4282 if (stru
->u
.s
.line
.file
== NULL
)
4284 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
4289 if (stru
->kind
== TYPE_LANG_STRUCT
)
4292 for (ss
= stru
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
4293 write_local_func_for_structure (s
, ss
, param
);
4296 write_local_func_for_structure (s
, stru
, param
);
4300 /* Nonzero if S is a type for which typed GC allocators should be output. */
4302 #define USED_BY_TYPED_GC_P(s) \
4303 ((s->kind == TYPE_POINTER \
4304 && (s->u.p->gc_used == GC_POINTED_TO \
4305 || s->u.p->gc_used == GC_USED)) \
4306 || (union_or_struct_p (s) \
4307 && ((s)->gc_used == GC_POINTED_TO \
4308 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4309 && s->u.s.line.file != NULL) \
4310 || ((s)->gc_used == GC_USED \
4311 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))) \
4312 || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
4316 /* Might T contain any non-pointer elements? */
4319 contains_scalar_p (type_p t
)
4327 return contains_scalar_p (t
->u
.a
.p
);
4328 case TYPE_USER_STRUCT
:
4329 /* User-marked structures will typically contain pointers. */
4332 /* Could also check for structures that have no non-pointer
4333 fields, but there aren't enough of those to worry about. */
4338 /* Mangle INPF and print it to F. */
4341 put_mangled_filename (outf_p f
, const input_file
*inpf
)
4343 /* The call to get_output_file_name may indirectly update fn since
4344 get_output_file_with_visibility caches its result inside, so we
4345 need the CONST_CAST. */
4346 const char *name
= get_output_file_name (CONST_CAST (input_file
*, inpf
));
4349 for (; *name
!= 0; name
++)
4350 if (ISALNUM (*name
))
4351 oprintf (f
, "%c", *name
);
4353 oprintf (f
, "%c", '_');
4356 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4357 LASTNAME, and NAME are all strings to insert in various places in
4358 the resulting code. */
4361 finish_root_table (struct flist
*flp
, const char *pfx
, const char *lastname
,
4362 const char *tname
, const char *name
)
4366 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
4367 if (fli2
->started_p
)
4369 oprintf (fli2
->f
, " %s\n", lastname
);
4370 oprintf (fli2
->f
, "};\n\n");
4373 for (fli2
= flp
; fli2
&& base_files
; fli2
= fli2
->next
)
4374 if (fli2
->started_p
)
4376 lang_bitmap bitmap
= get_lang_bitmap (fli2
->file
);
4379 for (fnum
= 0; bitmap
!= 0; fnum
++, bitmap
>>= 1)
4382 oprintf (base_files
[fnum
],
4383 "extern const struct %s gt_%s_", tname
, pfx
);
4384 put_mangled_filename (base_files
[fnum
], fli2
->file
);
4385 oprintf (base_files
[fnum
], "[];\n");
4391 for (fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
4392 oprintf (base_files
[fnum
],
4393 "EXPORTED_CONST struct %s * const %s[] = {\n", tname
, name
);
4397 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
4398 if (fli2
->started_p
)
4400 lang_bitmap bitmap
= get_lang_bitmap (fli2
->file
);
4403 fli2
->started_p
= 0;
4405 for (fnum
= 0; base_files
&& bitmap
!= 0; fnum
++, bitmap
>>= 1)
4408 oprintf (base_files
[fnum
], " gt_%s_", pfx
);
4409 put_mangled_filename (base_files
[fnum
], fli2
->file
);
4410 oprintf (base_files
[fnum
], ",\n");
4416 for (fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
4418 oprintf (base_files
[fnum
], " NULL\n");
4419 oprintf (base_files
[fnum
], "};\n");
4424 /* Write the first three fields (pointer, count and stride) for
4425 root NAME to F. V and LINE are as for write_root.
4427 Return true if the entry could be written; return false on error. */
4430 start_root_entry (outf_p f
, pair_p v
, const char *name
, struct fileloc
*line
)
4436 error_at_line (line
, "`%s' is too complex to be a root", name
);
4440 oprintf (f
, " {\n");
4441 oprintf (f
, " &%s,\n", name
);
4444 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
4446 oprintf (f
, " * (%s)", ap
->u
.a
.len
);
4447 else if (ap
== v
->type
)
4448 oprintf (f
, " * ARRAY_SIZE (%s)", v
->name
);
4450 oprintf (f
, " sizeof (%s", v
->name
);
4451 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
4453 oprintf (f
, "),\n");
4457 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4458 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4462 write_field_root (outf_p f
, pair_p v
, type_p type
, const char *name
,
4463 int has_length
, struct fileloc
*line
, const char *if_marked
,
4464 bool emit_pch
, type_p field_type
, const char *field_name
)
4467 /* If the field reference is relative to V, rather than to some
4468 subcomponent of V, we can mark any subarrays with a single stride.
4469 We're effectively treating the field as a global variable in its
4471 if (v
&& type
== v
->type
)
4474 newv
.type
= field_type
;
4475 newv
.name
= ACONCAT ((v
->name
, ".", field_name
, NULL
));
4478 /* Otherwise, any arrays nested in the structure are too complex to
4480 else if (field_type
->kind
== TYPE_ARRAY
)
4482 write_root (f
, v
, field_type
, ACONCAT ((name
, ".", field_name
, NULL
)),
4483 has_length
, line
, if_marked
, emit_pch
);
4486 /* Write out to F the table entry and any marker routines needed to
4487 mark NAME as TYPE. V can be one of three values:
4489 - null, if NAME is too complex to represent using a single
4490 count and stride. In this case, it is an error for NAME to
4491 contain any gc-ed data.
4493 - the outermost array that contains NAME, if NAME is part of an array.
4495 - the C variable that contains NAME, if NAME is not part of an array.
4497 LINE is the line of the C source that declares the root variable.
4498 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
4499 is nonzero iff we are building the root table for hash table caches. */
4502 write_root (outf_p f
, pair_p v
, type_p type
, const char *name
, int has_length
,
4503 struct fileloc
*line
, const char *if_marked
, bool emit_pch
)
4510 for (fld
= type
->u
.s
.fields
; fld
; fld
= fld
->next
)
4513 const char *desc
= NULL
;
4516 for (o
= fld
->opt
; o
; o
= o
->next
)
4517 if (strcmp (o
->name
, "skip") == 0)
4519 else if (strcmp (o
->name
, "desc") == 0
4520 && o
->kind
== OPTION_STRING
)
4521 desc
= o
->info
.string
;
4522 else if (strcmp (o
->name
, "param_is") == 0)
4525 error_at_line (line
,
4526 "field `%s' of global `%s' has unknown option `%s'",
4527 fld
->name
, name
, o
->name
);
4531 else if (desc
&& fld
->type
->kind
== TYPE_UNION
)
4533 pair_p validf
= NULL
;
4536 for (ufld
= fld
->type
->u
.s
.fields
; ufld
; ufld
= ufld
->next
)
4538 const char *tag
= NULL
;
4540 for (oo
= ufld
->opt
; oo
; oo
= oo
->next
)
4541 if (strcmp (oo
->name
, "tag") == 0
4542 && oo
->kind
== OPTION_STRING
)
4543 tag
= oo
->info
.string
;
4544 if (tag
== NULL
|| strcmp (tag
, desc
) != 0)
4547 error_at_line (line
,
4548 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4549 name
, fld
->name
, validf
->name
,
4550 name
, fld
->name
, ufld
->name
, tag
);
4554 write_field_root (f
, v
, type
, name
, 0, line
, if_marked
,
4555 emit_pch
, validf
->type
,
4556 ACONCAT ((fld
->name
, ".",
4557 validf
->name
, NULL
)));
4560 error_at_line (line
,
4561 "global `%s.%s' has `desc' option but is not union",
4564 write_field_root (f
, v
, type
, name
, 0, line
, if_marked
,
4565 emit_pch
, fld
->type
, fld
->name
);
4573 newname
= xasprintf ("%s[0]", name
);
4574 write_root (f
, v
, type
->u
.a
.p
, newname
, has_length
, line
, if_marked
,
4580 case TYPE_USER_STRUCT
:
4581 error_at_line (line
, "`%s' must be a pointer type, because it is "
4582 "a GC root and its type is marked with GTY((user))",
4590 if (!start_root_entry (f
, v
, name
, line
))
4595 if (!has_length
&& union_or_struct_p (tp
))
4597 tp
= get_ultimate_base_class (tp
);
4598 const char *id_for_tag
= filter_type_name (tp
->u
.s
.tag
);
4599 oprintf (f
, " >_ggc_mx_%s,\n", id_for_tag
);
4601 oprintf (f
, " >_pch_nx_%s", id_for_tag
);
4603 oprintf (f
, " NULL");
4604 if (id_for_tag
!= tp
->u
.s
.tag
)
4605 free (CONST_CAST (char *, id_for_tag
));
4607 else if (!has_length
&& tp
->kind
== TYPE_PARAM_STRUCT
)
4609 oprintf (f
, " >_ggc_m_");
4610 output_mangled_typename (f
, tp
);
4613 oprintf (f
, ",\n >_pch_n_");
4614 output_mangled_typename (f
, tp
);
4617 oprintf (f
, ",\n NULL");
4620 && (tp
->kind
== TYPE_POINTER
|| union_or_struct_p (tp
)))
4622 oprintf (f
, " >_ggc_ma_%s,\n", name
);
4624 oprintf (f
, " >_pch_na_%s", name
);
4626 oprintf (f
, " NULL");
4630 error_at_line (line
,
4631 "global `%s' is pointer to unimplemented type",
4635 oprintf (f
, ",\n &%s", if_marked
);
4636 oprintf (f
, "\n },\n");
4642 if (!start_root_entry (f
, v
, name
, line
))
4645 oprintf (f
, " (gt_pointer_walker) >_ggc_m_S,\n");
4646 oprintf (f
, " (gt_pointer_walker) >_pch_n_S\n");
4647 oprintf (f
, " },\n");
4655 case TYPE_UNDEFINED
:
4657 case TYPE_LANG_STRUCT
:
4658 case TYPE_PARAM_STRUCT
:
4659 error_at_line (line
, "global `%s' is unimplemented type", name
);
4663 /* This generates a routine to walk an array. */
4666 write_array (outf_p f
, pair_p v
, const struct write_types_data
*wtd
)
4668 struct walk_type_data d
;
4671 memset (&d
, 0, sizeof (d
));
4677 d
.bitmap
= get_lang_bitmap (v
->line
.file
);
4680 d
.prev_val
[3] = prevval3
= xasprintf ("&%s", v
->name
);
4682 if (wtd
->param_prefix
)
4684 oprintf (f
, "static void gt_%sa_%s\n", wtd
->param_prefix
, v
->name
);
4685 oprintf (f
, " (void *, void *, gt_pointer_operator, void *);\n");
4686 oprintf (f
, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4687 wtd
->param_prefix
, v
->name
);
4689 " ATTRIBUTE_UNUSED void *x_p,\n"
4690 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4691 " ATTRIBUTE_UNUSED void * cookie)\n");
4692 oprintf (d
.of
, "{\n");
4693 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
4694 d
.process_field
= write_types_local_process_field
;
4695 d
.have_this_obj
= true;
4696 walk_type (v
->type
, &d
);
4697 oprintf (f
, "}\n\n");
4701 oprintf (f
, "static void gt_%sa_%s (void *);\n", wtd
->prefix
, v
->name
);
4702 oprintf (f
, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4703 wtd
->prefix
, v
->name
);
4705 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
4706 d
.process_field
= write_types_process_field
;
4707 d
.have_this_obj
= false;
4708 walk_type (v
->type
, &d
);
4710 oprintf (f
, "}\n\n");
4713 /* Output a table describing the locations and types of VARIABLES. */
4716 write_roots (pair_p variables
, bool emit_pch
)
4719 struct flist
*flp
= NULL
;
4721 for (v
= variables
; v
; v
= v
->next
)
4724 get_output_file_with_visibility (CONST_CAST (input_file
*,
4727 const char *length
= NULL
;
4728 int deletable_p
= 0;
4730 for (o
= v
->opt
; o
; o
= o
->next
)
4731 if (strcmp (o
->name
, "length") == 0
4732 && o
->kind
== OPTION_STRING
)
4733 length
= o
->info
.string
;
4734 else if (strcmp (o
->name
, "deletable") == 0)
4736 else if (strcmp (o
->name
, "param_is") == 0)
4738 else if (strncmp (o
->name
, "param", 5) == 0
4739 && ISDIGIT (o
->name
[5]) && strcmp (o
->name
+ 6, "_is") == 0)
4741 else if (strcmp (o
->name
, "if_marked") == 0)
4744 error_at_line (&v
->line
,
4745 "global `%s' has unknown option `%s'",
4748 for (fli
= flp
; fli
; fli
= fli
->next
)
4749 if (fli
->f
== f
&& f
)
4753 fli
= XNEW (struct flist
);
4757 fli
->file
= v
->line
.file
;
4758 gcc_assert (fli
->file
);
4761 oprintf (f
, "\n/* GC roots. */\n\n");
4766 && v
->type
->kind
== TYPE_POINTER
4767 && (v
->type
->u
.p
->kind
== TYPE_POINTER
4768 || v
->type
->u
.p
->kind
== TYPE_STRUCT
))
4770 write_array (f
, v
, &ggc_wtd
);
4771 write_array (f
, v
, &pch_wtd
);
4775 for (v
= variables
; v
; v
= v
->next
)
4777 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4784 for (o
= v
->opt
; o
; o
= o
->next
)
4785 if (strcmp (o
->name
, "length") == 0)
4787 else if (strcmp (o
->name
, "deletable") == 0
4788 || strcmp (o
->name
, "if_marked") == 0)
4794 for (fli
= flp
; fli
; fli
= fli
->next
)
4797 if (!fli
->started_p
)
4801 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4802 put_mangled_filename (f
, v
->line
.file
);
4803 oprintf (f
, "[] = {\n");
4806 write_root (f
, v
, v
->type
, v
->name
, length_p
, &v
->line
, NULL
, emit_pch
);
4809 finish_root_table (flp
, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4812 for (v
= variables
; v
; v
= v
->next
)
4814 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4820 for (o
= v
->opt
; o
; o
= o
->next
)
4821 if (strcmp (o
->name
, "deletable") == 0)
4823 else if (strcmp (o
->name
, "if_marked") == 0)
4829 for (fli
= flp
; fli
; fli
= fli
->next
)
4832 if (!fli
->started_p
)
4836 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4837 put_mangled_filename (f
, v
->line
.file
);
4838 oprintf (f
, "[] = {\n");
4841 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4845 finish_root_table (flp
, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4846 "gt_ggc_deletable_rtab");
4848 for (v
= variables
; v
; v
= v
->next
)
4850 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4853 const char *if_marked
= NULL
;
4857 for (o
= v
->opt
; o
; o
= o
->next
)
4858 if (strcmp (o
->name
, "length") == 0)
4860 else if (strcmp (o
->name
, "if_marked") == 0
4861 && o
->kind
== OPTION_STRING
)
4862 if_marked
= o
->info
.string
;
4863 if (if_marked
== NULL
)
4865 if (v
->type
->kind
!= TYPE_POINTER
4866 || v
->type
->u
.p
->kind
!= TYPE_PARAM_STRUCT
4867 || v
->type
->u
.p
->u
.param_struct
.stru
!= find_structure ("htab",
4870 error_at_line (&v
->line
,
4871 "if_marked option used but not hash table");
4875 for (fli
= flp
; fli
; fli
= fli
->next
)
4878 if (!fli
->started_p
)
4882 oprintf (f
, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4883 put_mangled_filename (f
, v
->line
.file
);
4884 oprintf (f
, "[] = {\n");
4887 write_root (f
, v
, v
->type
->u
.p
->u
.param_struct
.param
[0],
4888 v
->name
, length_p
, &v
->line
, if_marked
, emit_pch
);
4891 finish_root_table (flp
, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4892 "gt_ggc_cache_rtab");
4897 for (v
= variables
; v
; v
= v
->next
)
4899 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4903 int if_marked_p
= 0;
4906 for (o
= v
->opt
; o
; o
= o
->next
)
4907 if (strcmp (o
->name
, "length") == 0)
4909 else if (strcmp (o
->name
, "if_marked") == 0)
4915 for (fli
= flp
; fli
; fli
= fli
->next
)
4918 if (!fli
->started_p
)
4922 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4923 put_mangled_filename (f
, v
->line
.file
);
4924 oprintf (f
, "[] = {\n");
4927 write_root (f
, v
, v
->type
, v
->name
, length_p
, &v
->line
, NULL
, emit_pch
);
4930 finish_root_table (flp
, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4931 "gt_pch_cache_rtab");
4933 for (v
= variables
; v
; v
= v
->next
)
4935 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4941 for (o
= v
->opt
; o
; o
= o
->next
)
4942 if (strcmp (o
->name
, "deletable") == 0
4943 || strcmp (o
->name
, "if_marked") == 0)
4952 if (!contains_scalar_p (v
->type
))
4955 for (fli
= flp
; fli
; fli
= fli
->next
)
4958 if (!fli
->started_p
)
4962 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4963 put_mangled_filename (f
, v
->line
.file
);
4964 oprintf (f
, "[] = {\n");
4967 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4971 finish_root_table (flp
, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4972 "gt_pch_scalar_rtab");
4975 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4976 guaranteee for somewhat increased readability. If name conflicts do happen,
4977 this funcion will have to be adjusted to be more like
4978 output_mangled_typename. */
4981 output_typename (outf_p of
, const_type_p t
)
4986 oprintf (of
, "str");
4989 oprintf (of
, "scalar");
4992 output_typename (of
, t
->u
.p
);
4995 case TYPE_USER_STRUCT
:
4997 case TYPE_LANG_STRUCT
:
4998 oprintf (of
, "%s", t
->u
.s
.tag
);
5000 case TYPE_PARAM_STRUCT
:
5003 for (i
= 0; i
< NUM_PARAM
; i
++)
5004 if (t
->u
.param_struct
.param
[i
] != NULL
)
5006 output_typename (of
, t
->u
.param_struct
.param
[i
]);
5009 output_typename (of
, t
->u
.param_struct
.stru
);
5013 case TYPE_UNDEFINED
:
5019 /* Writes a typed GC allocator for type S that is suitable as a callback for
5020 the splay tree implementation in libiberty. */
5023 write_splay_tree_allocator_def (const_type_p s
)
5025 outf_p of
= get_output_file_with_visibility (NULL
);
5026 oprintf (of
, "void * ggc_alloc_splay_tree_");
5027 output_typename (of
, s
);
5028 oprintf (of
, " (int sz, void * nl)\n");
5029 oprintf (of
, "{\n");
5030 oprintf (of
, " return ggc_splay_alloc (sz, nl);\n");
5031 oprintf (of
, "}\n\n");
5034 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
5035 for the splay tree implementation in libiberty. */
5038 write_splay_tree_allocators (const_type_p param_structs
)
5042 oprintf (header_file
, "\n/* Splay tree callback allocators. */\n");
5043 for (s
= param_structs
; s
; s
= s
->next
)
5044 if (s
->gc_used
== GC_POINTED_TO
)
5046 oprintf (header_file
, "extern void * ggc_alloc_splay_tree_");
5047 output_typename (header_file
, s
);
5048 oprintf (header_file
, " (int, void *);\n");
5049 write_splay_tree_allocator_def (s
);
5055 /* Dumps the value of typekind KIND. */
5058 dump_typekind (int indent
, enum typekind kind
)
5060 printf ("%*ckind = ", indent
, ' ');
5064 printf ("TYPE_SCALAR");
5067 printf ("TYPE_STRING");
5070 printf ("TYPE_STRUCT");
5072 case TYPE_UNDEFINED
:
5073 printf ("TYPE_UNDEFINED");
5075 case TYPE_USER_STRUCT
:
5076 printf ("TYPE_USER_STRUCT");
5079 printf ("TYPE_UNION");
5082 printf ("TYPE_POINTER");
5085 printf ("TYPE_ARRAY");
5087 case TYPE_LANG_STRUCT
:
5088 printf ("TYPE_LANG_STRUCT");
5090 case TYPE_PARAM_STRUCT
:
5091 printf ("TYPE_PARAM_STRUCT");
5099 /* Dumps the value of GC_USED flag. */
5102 dump_gc_used (int indent
, enum gc_used_enum gc_used
)
5104 printf ("%*cgc_used = ", indent
, ' ');
5108 printf ("GC_UNUSED");
5113 case GC_MAYBE_POINTED_TO
:
5114 printf ("GC_MAYBE_POINTED_TO");
5117 printf ("GC_POINTED_TO");
5125 /* Dumps the type options OPT. */
5128 dump_options (int indent
, options_p opt
)
5131 printf ("%*coptions = ", indent
, ' ');
5138 printf ("%s:string %s ", o
->name
, o
->info
.string
);
5141 printf ("%s:type ", o
->name
);
5142 dump_type (indent
+1, o
->info
.type
);
5145 printf ("%s:nested ", o
->name
);
5155 /* Dumps the source file location in LINE. */
5158 dump_fileloc (int indent
, struct fileloc line
)
5160 printf ("%*cfileloc: file = %s, line = %d\n", indent
, ' ',
5161 get_input_file_name (line
.file
),
5165 /* Recursively dumps the struct, union, or a language-specific
5169 dump_type_u_s (int indent
, type_p t
)
5173 gcc_assert (union_or_struct_p (t
));
5174 printf ("%*cu.s.tag = %s\n", indent
, ' ', t
->u
.s
.tag
);
5175 dump_fileloc (indent
, t
->u
.s
.line
);
5176 printf ("%*cu.s.fields =\n", indent
, ' ');
5177 fields
= t
->u
.s
.fields
;
5180 dump_pair (indent
+ INDENT
, fields
);
5181 fields
= fields
->next
;
5183 printf ("%*cend of fields of type %p\n", indent
, ' ', (void *) t
);
5184 dump_options (indent
, t
->u
.s
.opt
);
5185 printf ("%*cu.s.bitmap = %X\n", indent
, ' ', t
->u
.s
.bitmap
);
5186 if (t
->kind
== TYPE_LANG_STRUCT
)
5188 printf ("%*cu.s.lang_struct:\n", indent
, ' ');
5189 dump_type_list (indent
+ INDENT
, t
->u
.s
.lang_struct
);
5193 /* Recursively dumps the array T. */
5196 dump_type_u_a (int indent
, type_p t
)
5198 gcc_assert (t
->kind
== TYPE_ARRAY
);
5199 printf ("%*clen = %s, u.a.p:\n", indent
, ' ', t
->u
.a
.len
);
5200 dump_type_list (indent
+ INDENT
, t
->u
.a
.p
);
5203 /* Recursively dumps the parameterized struct T. */
5206 dump_type_u_param_struct (int indent
, type_p t
)
5209 gcc_assert (t
->kind
== TYPE_PARAM_STRUCT
);
5210 printf ("%*cu.param_struct.stru:\n", indent
, ' ');
5211 dump_type_list (indent
, t
->u
.param_struct
.stru
);
5212 dump_fileloc (indent
, t
->u
.param_struct
.line
);
5213 for (i
= 0; i
< NUM_PARAM
; i
++)
5215 if (t
->u
.param_struct
.param
[i
] == NULL
)
5217 printf ("%*cu.param_struct.param[%d]:\n", indent
, ' ', i
);
5218 dump_type (indent
+ INDENT
, t
->u
.param_struct
.param
[i
]);
5222 /* Recursively dumps the type list T. */
5225 dump_type_list (int indent
, type_p t
)
5230 dump_type (indent
, p
);
5235 static htab_t seen_types
;
5237 /* Recursively dumps the type T if it was not dumped previously. */
5240 dump_type (int indent
, type_p t
)
5244 if (seen_types
== NULL
)
5245 seen_types
= htab_create (100, htab_hash_pointer
, htab_eq_pointer
, NULL
);
5247 printf ("%*cType at %p: ", indent
, ' ', (void *) t
);
5248 slot
= htab_find_slot (seen_types
, t
, INSERT
);
5251 printf ("already seen.\n");
5257 dump_typekind (indent
, t
->kind
);
5258 printf ("%*cpointer_to = %p\n", indent
+ INDENT
, ' ',
5259 (void *) t
->pointer_to
);
5260 dump_gc_used (indent
+ INDENT
, t
->gc_used
);
5264 printf ("%*cscalar_is_char = %s\n", indent
+ INDENT
, ' ',
5265 t
->u
.scalar_is_char
? "true" : "false");
5271 case TYPE_LANG_STRUCT
:
5272 case TYPE_USER_STRUCT
:
5273 dump_type_u_s (indent
+ INDENT
, t
);
5276 printf ("%*cp:\n", indent
+ INDENT
, ' ');
5277 dump_type (indent
+ INDENT
, t
->u
.p
);
5280 dump_type_u_a (indent
+ INDENT
, t
);
5282 case TYPE_PARAM_STRUCT
:
5283 dump_type_u_param_struct (indent
+ INDENT
, t
);
5288 printf ("%*cEnd of type at %p\n", indent
, ' ', (void *) t
);
5291 /* Dumps the pair P. */
5294 dump_pair (int indent
, pair_p p
)
5296 printf ("%*cpair: name = %s\n", indent
, ' ', p
->name
);
5297 dump_type (indent
, p
->type
);
5298 dump_fileloc (indent
, p
->line
);
5299 dump_options (indent
, p
->opt
);
5300 printf ("%*cEnd of pair %s\n", indent
, ' ', p
->name
);
5303 /* Dumps the list of pairs PP. */
5306 dump_pair_list (const char *name
, pair_p pp
)
5309 printf ("%s:\n", name
);
5310 for (p
= pp
; p
!= NULL
; p
= p
->next
)
5312 printf ("End of %s\n\n", name
);
5315 /* Dumps the STRUCTURES. */
5318 dump_structures (const char *name
, type_p structures
)
5320 printf ("%s:\n", name
);
5321 dump_type_list (0, structures
);
5322 printf ("End of %s\n\n", name
);
5325 /* Dumps the internal structures of gengtype. This is useful to debug
5326 gengtype itself, or to understand what it does, e.g. for plugin
5330 dump_everything (void)
5332 dump_pair_list ("typedefs", typedefs
);
5333 dump_structures ("structures", structures
);
5334 dump_structures ("param_structs", param_structs
);
5335 dump_pair_list ("variables", variables
);
5337 /* Allocated with the first call to dump_type. */
5338 htab_delete (seen_types
);
5343 /* Option specification for getopt_long. */
5344 static const struct option gengtype_long_options
[] = {
5345 {"help", no_argument
, NULL
, 'h'},
5346 {"version", no_argument
, NULL
, 'V'},
5347 {"verbose", no_argument
, NULL
, 'v'},
5348 {"dump", no_argument
, NULL
, 'd'},
5349 {"debug", no_argument
, NULL
, 'D'},
5350 {"plugin", required_argument
, NULL
, 'P'},
5351 {"srcdir", required_argument
, NULL
, 'S'},
5352 {"backupdir", required_argument
, NULL
, 'B'},
5353 {"inputs", required_argument
, NULL
, 'I'},
5354 {"read-state", required_argument
, NULL
, 'r'},
5355 {"write-state", required_argument
, NULL
, 'w'},
5356 /* Terminating NULL placeholder. */
5357 {NULL
, no_argument
, NULL
, 0},
5364 printf ("Usage: %s\n", progname
);
5365 printf ("\t -h | --help " " \t# Give this help.\n");
5366 printf ("\t -D | --debug "
5367 " \t# Give debug output to debug %s itself.\n", progname
);
5368 printf ("\t -V | --version " " \t# Give version information.\n");
5369 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5370 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5371 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5372 " \t# Generate for plugin.\n");
5373 printf ("\t -S | --srcdir <GCC-directory> "
5374 " \t# Specify the GCC source directory.\n");
5375 printf ("\t -B | --backupdir <directory> "
5376 " \t# Specify the backup directory for updated files.\n");
5377 printf ("\t -I | --inputs <input-list> "
5378 " \t# Specify the file with source files list.\n");
5379 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5380 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5384 print_version (void)
5386 printf ("%s %s%s\n", progname
, pkgversion_string
, version_string
);
5387 printf ("Report bugs: %s\n", bug_report_url
);
5390 /* Parse the program options using getopt_long... */
5392 parse_program_options (int argc
, char **argv
)
5395 while ((opt
= getopt_long (argc
, argv
, "hVvdP:S:B:I:w:r:D",
5396 gengtype_long_options
, NULL
)) >= 0)
5400 case 'h': /* --help */
5403 case 'V': /* --version */
5406 case 'd': /* --dump */
5409 case 'D': /* --debug */
5412 case 'v': /* --verbose */
5415 case 'P': /* --plugin */
5417 plugin_output_filename
= optarg
;
5419 fatal ("missing plugin output file name");
5421 case 'S': /* --srcdir */
5425 fatal ("missing source directory");
5426 srcdir_len
= strlen (srcdir
);
5428 case 'B': /* --backupdir */
5430 backup_dir
= optarg
;
5432 fatal ("missing backup directory");
5434 case 'I': /* --inputs */
5438 fatal ("missing input list");
5440 case 'r': /* --read-state */
5442 read_state_filename
= optarg
;
5444 fatal ("missing read state file");
5445 DBGPRINTF ("read state %s\n", optarg
);
5447 case 'w': /* --write-state */
5448 DBGPRINTF ("write state %s\n", optarg
);
5450 write_state_filename
= optarg
;
5452 fatal ("missing write state file");
5455 fprintf (stderr
, "%s: unknown flag '%c'\n", progname
, opt
);
5457 fatal ("unexpected flag");
5460 if (plugin_output_filename
)
5462 /* In plugin mode we require some input files. */
5465 fatal ("no source files given in plugin mode");
5466 nb_plugin_files
= argc
- optind
;
5467 plugin_files
= XNEWVEC (input_file
*, nb_plugin_files
);
5468 for (i
= 0; i
< (int) nb_plugin_files
; i
++)
5470 char *name
= argv
[i
+ optind
];
5471 plugin_files
[i
] = input_file_by_name (name
);
5478 /******* Manage input files. ******/
5480 /* Hash table of unique input file names. */
5481 static htab_t input_file_htab
;
5483 /* Find or allocate a new input_file by hash-consing it. */
5485 input_file_by_name (const char* name
)
5488 input_file
* f
= NULL
;
5492 namlen
= strlen (name
);
5493 f
= XCNEWVAR (input_file
, sizeof (input_file
)+namlen
+2);
5496 f
->inpisplugin
= false;
5497 strcpy (f
->inpname
, name
);
5498 slot
= htab_find_slot (input_file_htab
, f
, INSERT
);
5499 gcc_assert (slot
!= NULL
);
5502 /* Already known input file. */
5504 return (input_file
*)(*slot
);
5506 /* New input file. */
5511 /* Hash table support routines for input_file-s. */
5513 htab_hash_inputfile (const void *p
)
5515 const input_file
*inpf
= (const input_file
*) p
;
5517 return htab_hash_string (get_input_file_name (inpf
));
5521 htab_eq_inputfile (const void *x
, const void *y
)
5523 const input_file
*inpfx
= (const input_file
*) x
;
5524 const input_file
*inpfy
= (const input_file
*) y
;
5525 gcc_assert (inpfx
!= NULL
&& inpfy
!= NULL
);
5526 return !filename_cmp (get_input_file_name (inpfx
), get_input_file_name (inpfy
));
5531 main (int argc
, char **argv
)
5534 static struct fileloc pos
= { NULL
, 0 };
5535 outf_p output_header
;
5537 /* Mandatory common initializations. */
5538 progname
= "gengtype"; /* For fatal and messages. */
5539 /* Create the hash-table used to hash-cons input files. */
5541 htab_create (800, htab_hash_inputfile
, htab_eq_inputfile
, NULL
);
5542 /* Initialize our special input files. */
5543 this_file
= input_file_by_name (__FILE__
);
5544 system_h_file
= input_file_by_name ("system.h");
5545 /* Set the scalar_is_char union number for predefined scalar types. */
5546 scalar_nonchar
.u
.scalar_is_char
= FALSE
;
5547 scalar_char
.u
.scalar_is_char
= TRUE
;
5549 parse_program_options (argc
, argv
);
5554 time_t now
= (time_t) 0;
5556 DBGPRINTF ("gengtype started pid %d at %s",
5557 (int) getpid (), ctime (&now
));
5559 #endif /* ENABLE_CHECKING */
5561 /* Parse the input list and the input files. */
5562 DBGPRINTF ("inputlist %s", inputlist
);
5563 if (read_state_filename
)
5566 fatal ("input list %s cannot be given with a read state file %s",
5567 inputlist
, read_state_filename
);
5568 read_state (read_state_filename
);
5569 DBGPRINT_COUNT_TYPE ("structures after read_state", structures
);
5570 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs
);
5574 /* These types are set up with #define or else outside of where
5575 we can see them. We should initialize them before calling
5577 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5579 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos
));
5580 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos
));
5581 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos
));
5582 POS_HERE (do_scalar_typedef ("double_int", &pos
));
5583 POS_HERE (do_scalar_typedef ("offset_int", &pos
));
5584 POS_HERE (do_scalar_typedef ("widest_int", &pos
));
5585 POS_HERE (do_scalar_typedef ("int64_t", &pos
));
5586 POS_HERE (do_scalar_typedef ("uint64_t", &pos
));
5587 POS_HERE (do_scalar_typedef ("uint8", &pos
));
5588 POS_HERE (do_scalar_typedef ("uintptr_t", &pos
));
5589 POS_HERE (do_scalar_typedef ("jword", &pos
));
5590 POS_HERE (do_scalar_typedef ("JCF_u2", &pos
));
5591 POS_HERE (do_scalar_typedef ("void", &pos
));
5592 POS_HERE (do_typedef ("PTR",
5593 create_pointer (resolve_typedef ("void", &pos
)),
5596 read_input_list (inputlist
);
5597 for (i
= 0; i
< num_gt_files
; i
++)
5599 parse_file (get_input_file_name (gt_files
[i
]));
5600 DBGPRINTF ("parsed file #%d %s",
5601 (int) i
, get_input_file_name (gt_files
[i
]));
5603 if (verbosity_level
>= 1)
5604 printf ("%s parsed %d files with %d GTY types\n",
5605 progname
, (int) num_gt_files
, type_count
);
5607 DBGPRINT_COUNT_TYPE ("structures after parsing", structures
);
5608 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs
);
5612 fatal ("either an input list or a read state file should be given");
5617 if (plugin_output_filename
)
5620 /* In plugin mode, we should have read a state file, and have
5621 given at least one plugin file. */
5622 if (!read_state_filename
)
5623 fatal ("No read state given in plugin mode for %s",
5624 plugin_output_filename
);
5626 if (nb_plugin_files
== 0 || !plugin_files
)
5627 fatal ("No plugin files given in plugin mode for %s",
5628 plugin_output_filename
);
5630 /* Parse our plugin files and augment the state. */
5631 for (ix
= 0; ix
< nb_plugin_files
; ix
++)
5633 input_file
* pluginput
= plugin_files
[ix
];
5634 pluginput
->inpisplugin
= true;
5635 parse_file (get_input_file_name (pluginput
));
5640 plugin_output
= create_file ("GCC", plugin_output_filename
);
5641 DBGPRINTF ("created plugin_output %p named %s",
5642 (void *) plugin_output
, plugin_output
->name
);
5645 { /* No plugin files, we are in normal mode. */
5647 fatal ("gengtype needs a source directory in normal mode");
5654 /* The call to set_gc_used may indirectly call find_param_structure
5655 hence enlarge the param_structs list of types. */
5656 set_gc_used (variables
);
5658 /* The state at this point is read from the state input file or by
5659 parsing source files and optionally augmented by parsing plugin
5660 source files. Write it now. */
5661 if (write_state_filename
)
5663 DBGPRINT_COUNT_TYPE ("structures before write_state", structures
);
5664 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs
);
5667 fatal ("didn't write state file %s after errors",
5668 write_state_filename
);
5670 DBGPRINTF ("before write_state %s", write_state_filename
);
5671 write_state (write_state_filename
);
5676 /* After having written the state file we return immediately to
5677 avoid generating any output file. */
5687 output_header
= plugin_output
? plugin_output
: header_file
;
5688 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5690 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5693 write_types (output_header
, structures
, param_structs
, &ggc_wtd
);
5694 if (plugin_files
== NULL
)
5696 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5698 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5700 write_types (header_file
, structures
, param_structs
, &pch_wtd
);
5701 write_local (header_file
, structures
, param_structs
);
5703 write_splay_tree_allocators (param_structs
);
5704 write_roots (variables
, plugin_files
== NULL
);
5706 close_output_files ();
5711 /* Don't bother about free-ing any input or plugin file, etc. */