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
;
149 /* Input file handling. */
151 /* Table of all input files. */
152 const input_file
**gt_files
;
155 /* A number of places use the name of this "gengtype.c" file for a
156 location for things that we can't rely on the source to define.
157 Make sure we can still use pointer comparison on filenames. */
158 input_file
* this_file
;
159 /* The "system.h" file is likewise specially useful. */
160 input_file
* system_h_file
;
162 /* Vector of per-language directories. */
163 const char **lang_dir_names
;
164 size_t num_lang_dirs
;
166 /* An array of output files suitable for definitions. There is one
167 BASE_FILES entry for each language. */
168 static outf_p
*base_files
;
173 /* Utility debugging function, printing the various type counts within
174 a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
176 dbgprint_count_type_at (const char *fil
, int lin
, const char *msg
, type_p t
)
178 int nb_types
= 0, nb_scalar
= 0, nb_string
= 0;
179 int nb_struct
= 0, nb_union
= 0, nb_array
= 0, nb_pointer
= 0;
180 int nb_lang_struct
= 0, nb_param_struct
= 0;
181 int nb_user_struct
= 0, nb_undefined
= 0;
183 for (p
= t
; p
; p
= p
->next
)
199 case TYPE_USER_STRUCT
:
211 case TYPE_LANG_STRUCT
:
214 case TYPE_PARAM_STRUCT
:
221 fprintf (stderr
, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
222 lbasename (fil
), lin
, msg
, nb_types
);
223 if (nb_scalar
> 0 || nb_string
> 0)
224 fprintf (stderr
, "@@%%@@ %d scalars, %d strings\n", nb_scalar
, nb_string
);
225 if (nb_struct
> 0 || nb_union
> 0)
226 fprintf (stderr
, "@@%%@@ %d structs, %d unions\n", nb_struct
, nb_union
);
227 if (nb_pointer
> 0 || nb_array
> 0)
228 fprintf (stderr
, "@@%%@@ %d pointers, %d arrays\n", nb_pointer
, nb_array
);
229 if (nb_lang_struct
> 0 || nb_param_struct
> 0)
230 fprintf (stderr
, "@@%%@@ %d lang_structs, %d param_structs\n",
231 nb_lang_struct
, nb_param_struct
);
232 if (nb_user_struct
> 0)
233 fprintf (stderr
, "@@%%@@ %d user_structs\n", nb_user_struct
);
234 if (nb_undefined
> 0)
235 fprintf (stderr
, "@@%%@@ %d undefined types\n", nb_undefined
);
236 fprintf (stderr
, "\n");
238 #endif /* ENABLE_CHECKING */
240 /* Scan the input file, LIST, and determine how much space we need to
241 store strings in. Also, count the number of language directories
242 and files. The numbers returned are overestimates as they does not
243 consider repeated files. */
245 measure_input_list (FILE *list
)
251 num_gt_files
= plugin_files
? nb_plugin_files
: 0;
252 while ((c
= getc (list
)) != EOF
)
261 /* Add space for a lang_bitmap before the input file name. */
262 n
+= sizeof (lang_bitmap
);
276 /* Read one input line from LIST to HEREP (which is updated). A
277 pointer to the string is returned via LINEP. If it was a language
278 subdirectory in square brackets, strip off the square brackets and
279 return true. Otherwise, leave space before the string for a
280 lang_bitmap, and return false. At EOF, returns false, does not
281 touch *HEREP, and sets *LINEP to NULL. POS is used for
284 read_input_line (FILE *list
, char **herep
, char **linep
, struct fileloc
*pos
)
290 /* Read over whitespace. */
291 while (c
== '\n' || c
== ' ')
301 /* No space for a lang_bitmap is necessary. Discard the '['. */
304 while (c
!= ']' && c
!= '\n' && c
!= EOF
)
313 c
= getc (list
); /* eat what should be a newline */
314 if (c
!= '\n' && c
!= EOF
)
315 error_at_line (pos
, "junk on line after language tag [%s]", line
);
318 error_at_line (pos
, "missing close bracket for language tag [%s",
327 /* Leave space for a lang_bitmap. */
328 memset (here
, 0, sizeof (lang_bitmap
));
329 here
+= sizeof (lang_bitmap
);
336 while (c
!= EOF
&& c
!= '\n');
344 /* Read the list of input files from LIST and compute all of the
345 relevant tables. There is one file per line of the list. At
346 first, all the files on the list are language-generic, but
347 eventually a line will appear which is the name of a language
348 subdirectory in square brackets, like this: [cp]. All subsequent
349 files are specific to that language, until another language
350 subdirectory tag appears. Files can appear more than once, if
351 they apply to more than one language. */
353 read_input_list (const char *listname
)
355 FILE *list
= fopen (listname
, "r");
357 fatal ("cannot open %s: %s", listname
, xstrerror (errno
));
361 size_t bufsz
= measure_input_list (list
);
362 char *buf
= XNEWVEC (char, bufsz
);
364 char *committed
= buf
;
365 char *limit
= buf
+ bufsz
;
370 lang_bitmap curlangs
= (1 << num_lang_dirs
) - 1;
372 epos
.file
= input_file_by_name (listname
);
375 lang_dir_names
= XNEWVEC (const char *, num_lang_dirs
);
376 gt_files
= XNEWVEC (const input_file
*, num_gt_files
);
383 is_language
= read_input_line (list
, &here
, &line
, &epos
);
384 gcc_assert (here
<= limit
);
387 else if (is_language
)
390 gcc_assert (langno
<= num_lang_dirs
);
391 for (i
= 0; i
< langno
; i
++)
392 if (strcmp (lang_dir_names
[i
], line
) == 0)
394 error_at_line (&epos
, "duplicate language tag [%s]",
401 curlangs
= 1 << langno
;
402 lang_dir_names
[langno
++] = line
;
407 input_file
*inpf
= input_file_by_name (line
);
408 gcc_assert (nfiles
<= num_gt_files
);
409 for (i
= 0; i
< nfiles
; i
++)
410 /* Since the input_file-s are uniquely hash-consed, we
411 can just compare pointers! */
412 if (gt_files
[i
] == inpf
)
414 /* Throw away the string we just read, and add the
415 current language to the existing string's bitmap. */
416 lang_bitmap bmap
= get_lang_bitmap (inpf
);
418 error_at_line (&epos
,
419 "file %s specified more than once "
420 "for language %s", line
,
422 0 ? "(all)" : lang_dir_names
[langno
-
426 set_lang_bitmap (inpf
, bmap
);
431 set_lang_bitmap (inpf
, curlangs
);
432 gt_files
[nfiles
++] = inpf
;
435 /* Update the global counts now that we know accurately how many
436 things there are. (We do not bother resizing the arrays down.) */
437 num_lang_dirs
= langno
;
438 /* Add the plugin files if provided. */
442 for (i
= 0; i
< nb_plugin_files
; i
++)
443 gt_files
[nfiles
++] = plugin_files
[i
];
445 num_gt_files
= nfiles
;
448 /* Sanity check: any file that resides in a language subdirectory
449 (e.g. 'cp') ought to belong to the corresponding language.
450 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
451 (Can you even do that? Should you be allowed to?) */
454 for (f
= 0; f
< num_gt_files
; f
++)
456 lang_bitmap bitmap
= get_lang_bitmap (gt_files
[f
]);
457 const char *basename
= get_file_basename (gt_files
[f
]);
458 const char *slashpos
= strchr (basename
, '/');
459 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
460 const char *slashpos2
= strchr (basename
, '\\');
462 if (!slashpos
|| (slashpos2
&& slashpos2
< slashpos
))
463 slashpos
= slashpos2
;
469 for (l
= 0; l
< num_lang_dirs
; l
++)
470 if ((size_t) (slashpos
- basename
) == strlen (lang_dir_names
[l
])
471 && memcmp (basename
, lang_dir_names
[l
],
472 strlen (lang_dir_names
[l
])) == 0)
474 if (!(bitmap
& (1 << l
)))
475 error ("%s is in language directory '%s' but is not "
476 "tagged for that language",
477 basename
, lang_dir_names
[l
]);
485 fatal ("error reading %s: %s", listname
, xstrerror (errno
));
492 /* The one and only TYPE_STRING. */
494 struct type string_type
= {
495 TYPE_STRING
, 0, 0, 0, GC_USED
, {0}
498 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
499 set early in main. */
501 struct type scalar_nonchar
= {
502 TYPE_SCALAR
, 0, 0, 0, GC_USED
, {0}
505 struct type scalar_char
= {
506 TYPE_SCALAR
, 0, 0, 0, GC_USED
, {0}
509 /* Lists of various things. */
511 pair_p typedefs
= NULL
;
512 type_p structures
= NULL
;
513 type_p param_structs
= NULL
;
514 pair_p variables
= NULL
;
516 static type_p
find_param_structure (type_p t
, type_p param
[NUM_PARAM
]);
517 static type_p
adjust_field_tree_exp (type_p t
, options_p opt
);
518 static type_p
adjust_field_rtx_def (type_p t
, options_p opt
);
520 /* Define S as a typedef to T at POS. */
523 do_typedef (const char *s
, type_p t
, struct fileloc
*pos
)
527 /* temporary kludge - gengtype doesn't handle conditionals or
528 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
529 is coming from this file (main() sets them up with safe dummy
531 if (!strcmp (s
, "CUMULATIVE_ARGS") && pos
->file
!= this_file
)
534 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
535 if (strcmp (p
->name
, s
) == 0)
537 if (p
->type
!= t
&& strcmp (s
, "result_type") != 0)
539 error_at_line (pos
, "type `%s' previously defined", s
);
540 error_at_line (&p
->line
, "previously defined here");
545 p
= XNEW (struct pair
);
554 /* Define S as a typename of a scalar. Cannot be used to define
555 typedefs of 'char'. Note: is also used for pointer-to-function
556 typedefs (which are therefore not treated as pointers). */
559 do_scalar_typedef (const char *s
, struct fileloc
*pos
)
561 do_typedef (s
, &scalar_nonchar
, pos
);
565 /* Define TYPE_NAME to be a user defined type at location POS. */
568 create_user_defined_type (const char *type_name
, struct fileloc
*pos
)
570 type_p ty
= find_structure (type_name
, TYPE_USER_STRUCT
);
572 /* We might have already seen an incomplete decl of the given type,
573 in which case we won't have yet seen a GTY((user)), and the type will
574 only have kind "TYPE_STRUCT". Mark it as a user struct. */
575 ty
->kind
= TYPE_USER_STRUCT
;
578 ty
->u
.s
.bitmap
= get_lang_bitmap (pos
->file
);
579 do_typedef (type_name
, ty
, pos
);
581 /* If TYPE_NAME specifies a template, create references to the types
582 in the template by pretending that each type is a field of TY.
583 This is needed to make sure that the types referenced by the
584 template are marked as used. */
585 char *str
= xstrdup (type_name
);
586 char *open_bracket
= strchr (str
, '<');
589 /* We only accept simple template declarations (see
590 require_template_declaration), so we only need to parse a
591 comma-separated list of strings, implicitly assumed to
593 char *arg
= open_bracket
+ 1;
594 char *type_id
= strtok (arg
, ",>");
598 /* Create a new field for every type found inside the template
600 const char *field_name
= xstrdup (type_id
);
601 type_p arg_type
= resolve_typedef (field_name
, pos
);
602 fields
= create_field_at (fields
, arg_type
, field_name
, 0, pos
);
603 type_id
= strtok (0, ",>");
606 /* Associate the field list to TY. */
607 ty
->u
.s
.fields
= fields
;
615 /* Given a typedef name S, return its associated type. Return NULL if
616 S is not a registered type name. */
619 type_for_name (const char *s
)
623 /* Special-case support for types within a "gcc::" namespace. Rather
624 than fully-supporting namespaces, simply strip off the "gcc::" prefix
625 where present. This allows us to have GTY roots of this form:
626 extern GTY(()) gcc::some_type *some_ptr;
627 where the autogenerated functions will refer to simply "some_type",
628 where they can be resolved into their namespace. */
629 if (0 == strncmp (s
, "gcc::", 5))
632 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
633 if (strcmp (p
->name
, s
) == 0)
639 /* Create an undefined type with name S and location POS. Return the
640 newly created type. */
643 create_undefined_type (const char *s
, struct fileloc
*pos
)
645 type_p ty
= find_structure (s
, TYPE_UNDEFINED
);
647 ty
->u
.s
.bitmap
= get_lang_bitmap (pos
->file
);
648 do_typedef (s
, ty
, pos
);
653 /* Return the type previously defined for S. Use POS to report errors. */
656 resolve_typedef (const char *s
, struct fileloc
*pos
)
658 bool is_template_instance
= (strchr (s
, '<') != NULL
);
659 type_p p
= type_for_name (s
);
661 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
662 type for regular type identifiers. If the type identifier S is a
663 template instantiation, however, we treat it as a user defined
666 FIXME, this is actually a limitation in gengtype. Supporting
667 template types and their instances would require keeping separate
668 track of the basic types definition and its instances. This
669 essentially forces all template classes in GC to be marked
672 p
= (is_template_instance
)
673 ? create_user_defined_type (s
, pos
)
674 : create_undefined_type (s
, pos
);
679 /* Add SUBCLASS to head of linked list of BASE's subclasses. */
681 void add_subclass (type_p base
, type_p subclass
)
683 gcc_assert (union_or_struct_p (base
));
684 gcc_assert (union_or_struct_p (subclass
));
686 subclass
->u
.s
.next_sibling_class
= base
->u
.s
.first_subclass
;
687 base
->u
.s
.first_subclass
= subclass
;
690 /* Create and return a new structure with tag NAME at POS with fields
691 FIELDS and options O. The KIND of structure must be one of
692 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
695 new_structure (const char *name
, enum typekind kind
, struct fileloc
*pos
,
696 pair_p fields
, options_p o
, type_p base_class
)
700 lang_bitmap bitmap
= get_lang_bitmap (pos
->file
);
701 bool isunion
= (kind
== TYPE_UNION
);
703 gcc_assert (union_or_struct_p (kind
));
705 for (si
= structures
; si
!= NULL
; si
= si
->next
)
706 if (strcmp (name
, si
->u
.s
.tag
) == 0 && UNION_P (si
) == isunion
)
709 if (si
->kind
== TYPE_LANG_STRUCT
)
713 for (si
= ls
->u
.s
.lang_struct
; si
!= NULL
; si
= si
->next
)
714 if (si
->u
.s
.bitmap
== bitmap
)
717 else if (si
->u
.s
.line
.file
!= NULL
&& si
->u
.s
.bitmap
!= bitmap
)
721 si
= XCNEW (struct type
);
722 memcpy (si
, ls
, sizeof (struct type
));
723 ls
->kind
= TYPE_LANG_STRUCT
;
724 ls
->u
.s
.lang_struct
= si
;
725 ls
->u
.s
.fields
= NULL
;
727 si
->state_number
= -type_count
;
728 si
->pointer_to
= NULL
;
729 si
->u
.s
.lang_struct
= ls
;
734 if (ls
!= NULL
&& s
== NULL
)
737 s
= XCNEW (struct type
);
738 s
->state_number
= -type_count
;
739 s
->next
= ls
->u
.s
.lang_struct
;
740 ls
->u
.s
.lang_struct
= s
;
741 s
->u
.s
.lang_struct
= ls
;
749 s
= XCNEW (struct type
);
750 s
->state_number
= -type_count
;
751 s
->next
= structures
;
755 if (s
->u
.s
.lang_struct
&& (s
->u
.s
.lang_struct
->u
.s
.bitmap
& bitmap
))
757 error_at_line (pos
, "duplicate definition of '%s %s'",
758 isunion
? "union" : "struct", s
->u
.s
.tag
);
759 error_at_line (&s
->u
.s
.line
, "previous definition here");
765 s
->u
.s
.fields
= fields
;
767 s
->u
.s
.bitmap
= bitmap
;
768 if (s
->u
.s
.lang_struct
)
769 s
->u
.s
.lang_struct
->u
.s
.bitmap
|= bitmap
;
770 s
->u
.s
.base_class
= base_class
;
772 add_subclass (base_class
, s
);
777 /* Return the previously-defined structure or union with tag NAME,
778 or a new empty structure or union if none was defined previously.
779 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
783 find_structure (const char *name
, enum typekind kind
)
786 bool isunion
= (kind
== TYPE_UNION
);
788 gcc_assert (kind
== TYPE_UNDEFINED
|| union_or_struct_p (kind
));
790 for (s
= structures
; s
!= NULL
; s
= s
->next
)
791 if (strcmp (name
, s
->u
.s
.tag
) == 0 && UNION_P (s
) == isunion
)
795 s
= XCNEW (struct type
);
796 s
->next
= structures
;
797 s
->state_number
= -type_count
;
805 /* Return the previously-defined parameterized structure for structure
806 T and parameters PARAM, or a new parameterized empty structure or
807 union if none was defined previously. */
810 find_param_structure (type_p t
, type_p param
[NUM_PARAM
])
814 for (res
= param_structs
; res
; res
= res
->next
)
815 if (res
->u
.param_struct
.stru
== t
816 && memcmp (res
->u
.param_struct
.param
, param
,
817 sizeof (type_p
) * NUM_PARAM
) == 0)
822 res
= XCNEW (struct type
);
823 res
->kind
= TYPE_PARAM_STRUCT
;
824 res
->next
= param_structs
;
825 res
->state_number
= -type_count
;
827 res
->u
.param_struct
.stru
= t
;
828 memcpy (res
->u
.param_struct
.param
, param
, sizeof (type_p
) * NUM_PARAM
);
833 /* Return a scalar type with name NAME. */
836 create_scalar_type (const char *name
)
838 if (!strcmp (name
, "char") || !strcmp (name
, "unsigned char"))
841 return &scalar_nonchar
;
845 /* Return a pointer to T. */
848 create_pointer (type_p t
)
852 type_p r
= XCNEW (struct type
);
854 r
->state_number
= -type_count
;
855 r
->kind
= TYPE_POINTER
;
859 return t
->pointer_to
;
862 /* Return an array of length LEN. */
865 create_array (type_p t
, const char *len
)
870 v
= XCNEW (struct type
);
871 v
->kind
= TYPE_ARRAY
;
872 v
->state_number
= -type_count
;
878 /* Return a string options structure with name NAME and info INFO.
879 NEXT is the next option in the chain. */
881 create_string_option (options_p next
, const char *name
, const char *info
)
883 options_p o
= XNEW (struct options
);
884 o
->kind
= OPTION_STRING
;
887 o
->info
.string
= info
;
891 /* Create a type options structure with name NAME and info INFO. NEXT
892 is the next option in the chain. */
894 create_type_option (options_p next
, const char* name
, type_p info
)
896 options_p o
= XNEW (struct options
);
899 o
->kind
= OPTION_TYPE
;
904 /* Create a nested pointer options structure with name NAME and info
905 INFO. NEXT is the next option in the chain. */
907 create_nested_option (options_p next
, const char* name
,
908 struct nested_ptr_data
* info
)
911 o
= XNEW (struct options
);
914 o
->kind
= OPTION_NESTED
;
915 o
->info
.nested
= info
;
919 /* Return an options structure for a "nested_ptr" option. */
921 create_nested_ptr_option (options_p next
, type_p t
,
922 const char *to
, const char *from
)
924 struct nested_ptr_data
*d
= XNEW (struct nested_ptr_data
);
926 d
->type
= adjust_field_type (t
, 0);
928 d
->convert_from
= from
;
929 return create_nested_option (next
, "nested_ptr", d
);
932 /* Add a variable named S of type T with options O defined at POS,
935 note_variable (const char *s
, type_p t
, options_p o
, struct fileloc
*pos
)
938 n
= XNEW (struct pair
);
947 /* Most-general structure field creator. */
949 create_field_all (pair_p next
, type_p type
, const char *name
, options_p opt
,
950 const input_file
*inpf
, int line
)
954 field
= XNEW (struct pair
);
959 field
->line
.file
= inpf
;
960 field
->line
.line
= line
;
964 /* Create a field that came from the source code we are scanning,
965 i.e. we have a 'struct fileloc', and possibly options; also,
966 adjust_field_type should be called. */
968 create_field_at (pair_p next
, type_p type
, const char *name
, options_p opt
,
971 return create_field_all (next
, adjust_field_type (type
, opt
),
972 name
, opt
, pos
->file
, pos
->line
);
975 /* Create a fake field with the given type and name. NEXT is the next
976 field in the chain. */
977 #define create_field(next,type,name) \
978 create_field_all (next,type,name, 0, this_file, __LINE__)
980 /* Like create_field, but the field is only valid when condition COND
984 create_optional_field_ (pair_p next
, type_p type
, const char *name
,
985 const char *cond
, int line
)
991 /* Create a fake union type with a single nameless field of type TYPE.
992 The field has a tag of "1". This allows us to make the presence
993 of a field of type TYPE depend on some boolean "desc" being true. */
994 union_fields
= create_field (NULL
, type
, "");
996 create_string_option (union_fields
->opt
, "dot", "");
998 create_string_option (union_fields
->opt
, "tag", "1");
1000 new_structure (xasprintf ("%s_%d", "fake_union", id
++), TYPE_UNION
,
1001 &lexer_line
, union_fields
, NULL
, NULL
);
1003 /* Create the field and give it the new fake union type. Add a "desc"
1004 tag that specifies the condition under which the field is valid. */
1005 return create_field_all (next
, union_type
, name
,
1006 create_string_option (0, "desc", cond
),
1010 #define create_optional_field(next,type,name,cond) \
1011 create_optional_field_(next,type,name,cond,__LINE__)
1013 /* Reverse a linked list of 'struct pair's in place. */
1015 nreverse_pairs (pair_p list
)
1017 pair_p prev
= 0, p
, next
;
1018 for (p
= list
; p
; p
= next
)
1028 /* We don't care how long a CONST_DOUBLE is. */
1029 #define CONST_DOUBLE_FORMAT "ww"
1030 /* We don't want to see codes that are only for generator files. */
1031 #undef GENERATOR_FILE
1035 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1041 static const char *const rtx_name
[NUM_RTX_CODE
] = {
1042 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1047 static const char *const rtx_format
[NUM_RTX_CODE
] = {
1048 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1053 static int rtx_next_new
[NUM_RTX_CODE
];
1055 /* We also need codes and names for insn notes (not register notes).
1056 Note that we do *not* bias the note values here. */
1059 #define DEF_INSN_NOTE(NAME) NAME,
1060 #include "insn-notes.def"
1061 #undef DEF_INSN_NOTE
1066 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1067 default field for line number notes. */
1068 static const char *const note_insn_name
[NOTE_INSN_MAX
+ 1] = {
1069 #define DEF_INSN_NOTE(NAME) #NAME,
1070 #include "insn-notes.def"
1071 #undef DEF_INSN_NOTE
1074 #undef CONST_DOUBLE_FORMAT
1075 #define GENERATOR_FILE
1077 /* Generate the contents of the rtx_next array. This really doesn't belong
1078 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1084 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1088 rtx_next_new
[i
] = -1;
1089 if (strncmp (rtx_format
[i
], "iuu", 3) == 0)
1090 rtx_next_new
[i
] = 2;
1091 else if (i
== COND_EXEC
|| i
== SET
|| i
== EXPR_LIST
|| i
== INSN_LIST
)
1092 rtx_next_new
[i
] = 1;
1094 for (k
= strlen (rtx_format
[i
]) - 1; k
>= 0; k
--)
1095 if (rtx_format
[i
][k
] == 'e' || rtx_format
[i
][k
] == 'u')
1096 rtx_next_new
[i
] = k
;
1100 /* Write out the contents of the rtx_next array. */
1102 write_rtx_next (void)
1104 outf_p f
= get_output_file_with_visibility (NULL
);
1109 oprintf (f
, "\n/* Used to implement the RTX_NEXT macro. */\n");
1110 oprintf (f
, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1111 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1112 if (rtx_next_new
[i
] == -1)
1113 oprintf (f
, " 0,\n");
1116 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new
[i
]);
1117 oprintf (f
, "};\n");
1120 /* Handle `special("rtx_def")'. This is a special case for field
1121 `fld' of struct rtx_def, which is an array of unions whose values
1122 are based in a complex way on the type of RTL. */
1125 adjust_field_rtx_def (type_p t
, options_p
ARG_UNUSED (opt
))
1130 type_p rtx_tp
, rtvec_tp
, tree_tp
, mem_attrs_tp
, note_union_tp
, scalar_tp
;
1131 type_p basic_block_tp
, reg_attrs_tp
, constant_tp
, symbol_union_tp
;
1133 if (t
->kind
!= TYPE_UNION
)
1135 error_at_line (&lexer_line
,
1136 "special `rtx_def' must be applied to a union");
1137 return &string_type
;
1140 nodot
= create_string_option (NULL
, "dot", "");
1142 rtx_tp
= create_pointer (find_structure ("rtx_def", TYPE_STRUCT
));
1143 rtvec_tp
= create_pointer (find_structure ("rtvec_def", TYPE_STRUCT
));
1144 tree_tp
= create_pointer (find_structure ("tree_node", TYPE_UNION
));
1145 mem_attrs_tp
= create_pointer (find_structure ("mem_attrs", TYPE_STRUCT
));
1147 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT
));
1149 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT
));
1151 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT
));
1152 scalar_tp
= &scalar_nonchar
; /* rtunion int */
1155 pair_p note_flds
= NULL
;
1158 for (c
= 0; c
<= NOTE_INSN_MAX
; c
++)
1163 case NOTE_INSN_DELETED_LABEL
:
1164 case NOTE_INSN_DELETED_DEBUG_LABEL
:
1165 note_flds
= create_field (note_flds
, &string_type
, "rt_str");
1168 case NOTE_INSN_BLOCK_BEG
:
1169 case NOTE_INSN_BLOCK_END
:
1170 note_flds
= create_field (note_flds
, tree_tp
, "rt_tree");
1173 case NOTE_INSN_VAR_LOCATION
:
1174 case NOTE_INSN_CALL_ARG_LOCATION
:
1175 note_flds
= create_field (note_flds
, rtx_tp
, "rt_rtx");
1179 note_flds
= create_field (note_flds
, scalar_tp
, "rt_int");
1182 /* NOTE_INSN_MAX is used as the default field for line
1184 if (c
== NOTE_INSN_MAX
)
1186 create_string_option (nodot
, "default", "");
1189 create_string_option (nodot
, "tag", note_insn_name
[c
]);
1191 note_union_tp
= new_structure ("rtx_def_note_subunion", TYPE_UNION
,
1192 &lexer_line
, note_flds
, NULL
, NULL
);
1194 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1197 sym_flds
= create_field (NULL
, tree_tp
, "rt_tree");
1198 sym_flds
->opt
= create_string_option (nodot
, "default", "");
1199 sym_flds
= create_field (sym_flds
, constant_tp
, "rt_constant");
1200 sym_flds
->opt
= create_string_option (nodot
, "tag", "1");
1201 symbol_union_tp
= new_structure ("rtx_def_symbol_subunion", TYPE_UNION
,
1202 &lexer_line
, sym_flds
, NULL
, NULL
);
1204 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1206 pair_p subfields
= NULL
;
1207 size_t aindex
, nmindex
;
1212 for (aindex
= 0; aindex
< strlen (rtx_format
[i
]); aindex
++)
1215 const char *subname
;
1217 switch (rtx_format
[i
][aindex
])
1228 if (i
== MEM
&& aindex
== 1)
1229 t
= mem_attrs_tp
, subname
= "rt_mem";
1230 else if (i
== JUMP_INSN
&& aindex
== 8)
1231 t
= rtx_tp
, subname
= "rt_rtx";
1232 else if (i
== CODE_LABEL
&& aindex
== 5)
1233 t
= scalar_tp
, subname
= "rt_int";
1234 else if (i
== CODE_LABEL
&& aindex
== 4)
1235 t
= rtx_tp
, subname
= "rt_rtx";
1236 else if (i
== LABEL_REF
&& (aindex
== 1 || aindex
== 2))
1237 t
= rtx_tp
, subname
= "rt_rtx";
1238 else if (i
== NOTE
&& aindex
== 4)
1239 t
= note_union_tp
, subname
= "";
1240 else if (i
== NOTE
&& aindex
== 5)
1241 t
= scalar_tp
, subname
= "rt_int";
1242 else if (i
== NOTE
&& aindex
>= 7)
1243 t
= scalar_tp
, subname
= "rt_int";
1244 else if (i
== ADDR_DIFF_VEC
&& aindex
== 4)
1245 t
= scalar_tp
, subname
= "rt_int";
1246 else if (i
== VALUE
&& aindex
== 0)
1247 t
= scalar_tp
, subname
= "rt_int";
1248 else if (i
== DEBUG_EXPR
&& aindex
== 0)
1249 t
= tree_tp
, subname
= "rt_tree";
1250 else if (i
== REG
&& aindex
== 1)
1251 t
= scalar_tp
, subname
= "rt_int";
1252 else if (i
== REG
&& aindex
== 2)
1253 t
= reg_attrs_tp
, subname
= "rt_reg";
1254 else if (i
== SCRATCH
&& aindex
== 0)
1255 t
= scalar_tp
, subname
= "rt_int";
1256 else if (i
== SYMBOL_REF
&& aindex
== 1)
1257 t
= scalar_tp
, subname
= "rt_int";
1258 else if (i
== SYMBOL_REF
&& aindex
== 2)
1259 t
= symbol_union_tp
, subname
= "";
1260 else if (i
== JUMP_TABLE_DATA
&& aindex
>= 5)
1261 t
= scalar_tp
, subname
= "rt_int";
1262 else if (i
== BARRIER
&& aindex
>= 3)
1263 t
= scalar_tp
, subname
= "rt_int";
1264 else if (i
== ENTRY_VALUE
&& aindex
== 0)
1265 t
= rtx_tp
, subname
= "rt_rtx";
1270 "rtx type `%s' has `0' in position %lu, can't handle",
1271 rtx_name
[i
], (unsigned long) aindex
);
1293 subname
= "rt_rtvec";
1298 subname
= "rt_tree";
1309 "rtx type `%s' has `%c' in position %lu, can't handle",
1310 rtx_name
[i
], rtx_format
[i
][aindex
],
1311 (unsigned long) aindex
);
1317 subfields
= create_field (subfields
, t
,
1318 xasprintf (".fld[%lu].%s",
1319 (unsigned long) aindex
,
1321 subfields
->opt
= nodot
;
1322 if (t
== note_union_tp
)
1324 create_string_option (subfields
->opt
, "desc",
1326 if (t
== symbol_union_tp
)
1328 create_string_option (subfields
->opt
, "desc",
1329 "CONSTANT_POOL_ADDRESS_P (&%0)");
1332 if (i
== SYMBOL_REF
)
1334 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1336 type_p field_tp
= find_structure ("block_symbol", TYPE_STRUCT
);
1338 = create_optional_field (subfields
, field_tp
, "block_sym",
1339 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1342 sname
= xasprintf ("rtx_def_%s", rtx_name
[i
]);
1343 substruct
= new_structure (sname
, TYPE_STRUCT
, &lexer_line
, subfields
,
1346 ftag
= xstrdup (rtx_name
[i
]);
1347 for (nmindex
= 0; nmindex
< strlen (ftag
); nmindex
++)
1348 ftag
[nmindex
] = TOUPPER (ftag
[nmindex
]);
1349 flds
= create_field (flds
, substruct
, "");
1350 flds
->opt
= create_string_option (nodot
, "tag", ftag
);
1352 return new_structure ("rtx_def_subunion", TYPE_UNION
, &lexer_line
, flds
,
1356 /* Handle `special("tree_exp")'. This is a special case for
1357 field `operands' of struct tree_exp, which although it claims to contain
1358 pointers to trees, actually sometimes contains pointers to RTL too.
1359 Passed T, the old type of the field, and OPT its options. Returns
1360 a new type for the field. */
1363 adjust_field_tree_exp (type_p t
, options_p opt ATTRIBUTE_UNUSED
)
1368 if (t
->kind
!= TYPE_ARRAY
)
1370 error_at_line (&lexer_line
,
1371 "special `tree_exp' must be applied to an array");
1372 return &string_type
;
1375 nodot
= create_string_option (NULL
, "dot", "");
1377 flds
= create_field (NULL
, t
, "");
1378 flds
->opt
= create_string_option (nodot
, "length",
1379 "TREE_OPERAND_LENGTH ((tree) &%0)");
1380 flds
->opt
= create_string_option (flds
->opt
, "default", "");
1382 return new_structure ("tree_exp_subunion", TYPE_UNION
, &lexer_line
, flds
,
1386 /* Perform any special processing on a type T, about to become the type
1387 of a field. Return the appropriate type for the field.
1389 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1390 - Similarly for arrays of pointer-to-char;
1391 - Converts structures for which a parameter is provided to
1393 - Handles "special" options.
1397 adjust_field_type (type_p t
, options_p opt
)
1400 const int pointer_p
= t
->kind
== TYPE_POINTER
;
1401 type_p params
[NUM_PARAM
];
1405 for (i
= 0; i
< NUM_PARAM
; i
++)
1408 for (; opt
; opt
= opt
->next
)
1409 if (strcmp (opt
->name
, "length") == 0)
1412 error_at_line (&lexer_line
, "duplicate `%s' option", opt
->name
);
1413 if (t
->u
.p
->kind
== TYPE_SCALAR
|| t
->u
.p
->kind
== TYPE_STRING
)
1415 error_at_line (&lexer_line
,
1416 "option `%s' may not be applied to "
1417 "arrays of atomic types", opt
->name
);
1421 else if ((strcmp (opt
->name
, "param_is") == 0
1422 || (strncmp (opt
->name
, "param", 5) == 0
1423 && ISDIGIT (opt
->name
[5])
1424 && strcmp (opt
->name
+ 6, "_is") == 0))
1425 && opt
->kind
== OPTION_TYPE
)
1427 int num
= ISDIGIT (opt
->name
[5]) ? opt
->name
[5] - '0' : 0;
1429 if (!union_or_struct_p (t
)
1430 && (t
->kind
!= TYPE_POINTER
|| !union_or_struct_p (t
->u
.p
)))
1432 error_at_line (&lexer_line
,
1433 "option `%s' may only be applied to structures or structure pointers",
1439 if (params
[num
] != NULL
)
1440 error_at_line (&lexer_line
, "duplicate `%s' option", opt
->name
);
1441 if (!ISDIGIT (opt
->name
[5]))
1442 params
[num
] = create_pointer (opt
->info
.type
);
1444 params
[num
] = opt
->info
.type
;
1446 else if (strcmp (opt
->name
, "special") == 0
1447 && opt
->kind
== OPTION_STRING
)
1449 const char *special_name
= opt
->info
.string
;
1450 if (strcmp (special_name
, "tree_exp") == 0)
1451 t
= adjust_field_tree_exp (t
, opt
);
1452 else if (strcmp (special_name
, "rtx_def") == 0)
1453 t
= adjust_field_rtx_def (t
, opt
);
1455 error_at_line (&lexer_line
, "unknown special `%s'", special_name
);
1464 realt
= find_param_structure (t
, params
);
1465 t
= pointer_p
? create_pointer (realt
) : realt
;
1469 && pointer_p
&& t
->u
.p
->kind
== TYPE_SCALAR
&& t
->u
.p
->u
.scalar_is_char
)
1470 return &string_type
;
1471 if (t
->kind
== TYPE_ARRAY
&& t
->u
.a
.p
->kind
== TYPE_POINTER
1472 && t
->u
.a
.p
->u
.p
->kind
== TYPE_SCALAR
1473 && t
->u
.a
.p
->u
.p
->u
.scalar_is_char
)
1474 return create_array (&string_type
, t
->u
.a
.len
);
1480 static void set_gc_used_type (type_p
, enum gc_used_enum
, type_p
*,
1482 static void set_gc_used (pair_p
);
1484 /* Handle OPT for set_gc_used_type. */
1487 process_gc_options (options_p opt
, enum gc_used_enum level
, int *maybe_undef
,
1488 int *pass_param
, int *length
, int *skip
,
1492 for (o
= opt
; o
; o
= o
->next
)
1493 if (strcmp (o
->name
, "ptr_alias") == 0 && level
== GC_POINTED_TO
1494 && o
->kind
== OPTION_TYPE
)
1495 set_gc_used_type (o
->info
.type
,
1496 GC_POINTED_TO
, NULL
);
1497 else if (strcmp (o
->name
, "maybe_undef") == 0)
1499 else if (strcmp (o
->name
, "use_params") == 0)
1501 else if (strcmp (o
->name
, "length") == 0)
1503 else if (strcmp (o
->name
, "skip") == 0)
1505 else if (strcmp (o
->name
, "nested_ptr") == 0
1506 && o
->kind
== OPTION_NESTED
)
1507 *nested_ptr
= ((const struct nested_ptr_data
*) o
->info
.nested
)->type
;
1511 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1513 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1514 are set to GC_UNUSED. Otherwise, an error is emitted for
1515 TYPE_UNDEFINED types. This is used to support user-defined
1516 template types with non-type arguments.
1518 For instance, when we parse a template type with enum arguments
1519 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1520 artificial fields for 'MyType', one for 'AnotherType', the other
1521 one for 'EnumValue'.
1523 At the time that we parse this type we don't know that 'EnumValue'
1524 is really an enum value, so the parser creates a TYPE_UNDEFINED
1525 type for it. Since 'EnumValue' is never resolved to a known
1526 structure, it will stay with TYPE_UNDEFINED.
1528 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1529 'EnumValue'. Generating marking code for it would cause
1530 compilation failures since the marking routines assumes that
1531 'EnumValue' is a type. */
1534 set_gc_used_type (type_p t
, enum gc_used_enum level
, type_p param
[NUM_PARAM
],
1535 bool allow_undefined_types
)
1537 if (t
->gc_used
>= level
)
1546 case TYPE_USER_STRUCT
:
1551 bool allow_undefined_field_types
= (t
->kind
== TYPE_USER_STRUCT
);
1553 process_gc_options (t
->u
.s
.opt
, level
, &dummy
, &dummy
, &dummy
, &dummy
,
1556 if (t
->u
.s
.base_class
)
1557 set_gc_used_type (t
->u
.s
.base_class
, level
, param
,
1558 allow_undefined_types
);
1559 /* Anything pointing to a base class might actually be pointing
1561 for (type_p subclass
= t
->u
.s
.first_subclass
; subclass
;
1562 subclass
= subclass
->u
.s
.next_sibling_class
)
1563 set_gc_used_type (subclass
, level
, param
,
1564 allow_undefined_types
);
1566 FOR_ALL_INHERITED_FIELDS(t
, f
)
1568 int maybe_undef
= 0;
1572 type_p nested_ptr
= NULL
;
1573 process_gc_options (f
->opt
, level
, &maybe_undef
, &pass_param
,
1574 &length
, &skip
, &nested_ptr
);
1576 if (nested_ptr
&& f
->type
->kind
== TYPE_POINTER
)
1577 set_gc_used_type (nested_ptr
, GC_POINTED_TO
,
1578 pass_param
? param
: NULL
);
1579 else if (length
&& f
->type
->kind
== TYPE_POINTER
)
1580 set_gc_used_type (f
->type
->u
.p
, GC_USED
, NULL
);
1581 else if (maybe_undef
&& f
->type
->kind
== TYPE_POINTER
)
1582 set_gc_used_type (f
->type
->u
.p
, GC_MAYBE_POINTED_TO
, NULL
);
1583 else if (pass_param
&& f
->type
->kind
== TYPE_POINTER
&& param
)
1584 set_gc_used_type (find_param_structure (f
->type
->u
.p
, param
),
1585 GC_POINTED_TO
, NULL
);
1587 ; /* target type is not used through this field */
1589 set_gc_used_type (f
->type
, GC_USED
, pass_param
? param
: NULL
,
1590 allow_undefined_field_types
);
1595 case TYPE_UNDEFINED
:
1596 if (level
> GC_UNUSED
)
1598 if (!allow_undefined_types
)
1599 error_at_line (&t
->u
.s
.line
, "undefined type `%s'", t
->u
.s
.tag
);
1600 t
->gc_used
= GC_UNUSED
;
1605 set_gc_used_type (t
->u
.p
, GC_POINTED_TO
, NULL
);
1609 set_gc_used_type (t
->u
.a
.p
, GC_USED
, param
);
1612 case TYPE_LANG_STRUCT
:
1613 for (t
= t
->u
.s
.lang_struct
; t
; t
= t
->next
)
1614 set_gc_used_type (t
, level
, param
);
1617 case TYPE_PARAM_STRUCT
:
1620 for (i
= 0; i
< NUM_PARAM
; i
++)
1621 if (t
->u
.param_struct
.param
[i
] != 0)
1622 set_gc_used_type (t
->u
.param_struct
.param
[i
], GC_USED
, NULL
);
1624 if (t
->u
.param_struct
.stru
->gc_used
== GC_POINTED_TO
)
1625 level
= GC_POINTED_TO
;
1628 t
->u
.param_struct
.stru
->gc_used
= GC_UNUSED
;
1629 set_gc_used_type (t
->u
.param_struct
.stru
, level
,
1630 t
->u
.param_struct
.param
);
1638 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1641 set_gc_used (pair_p variables
)
1645 for (p
= variables
; p
; p
= p
->next
)
1647 set_gc_used_type (p
->type
, GC_USED
, NULL
);
1650 if (verbosity_level
>= 2)
1651 printf ("%s used %d GTY-ed variables\n", progname
, nbvars
);
1654 /* File mapping routines. For each input file, there is one output .c file
1655 (but some output files have many input files), and there is one .h file
1656 for the whole build. */
1658 /* Output file handling. */
1660 /* Create and return an outf_p for a new file for NAME, to be called
1664 create_file (const char *name
, const char *oname
)
1666 static const char *const hdr
[] = {
1667 " Copyright (C) 2004-2014 Free Software Foundation, Inc.\n",
1669 "This file is part of GCC.\n",
1671 "GCC is free software; you can redistribute it and/or modify it under\n",
1672 "the terms of the GNU General Public License as published by the Free\n",
1673 "Software Foundation; either version 3, or (at your option) any later\n",
1676 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1677 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1678 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1679 "for more details.\n",
1681 "You should have received a copy of the GNU General Public License\n",
1682 "along with GCC; see the file COPYING3. If not see\n",
1683 "<http://www.gnu.org/licenses/>. */\n",
1685 "/* This file is machine generated. Do not edit. */\n"
1690 gcc_assert (name
!= NULL
);
1691 gcc_assert (oname
!= NULL
);
1692 f
= XCNEW (struct outf
);
1693 f
->next
= output_files
;
1697 oprintf (f
, "/* Type information for %s.\n", name
);
1698 for (i
= 0; i
< ARRAY_SIZE (hdr
); i
++)
1699 oprintf (f
, "%s", hdr
[i
]);
1703 /* Print, like fprintf, to O.
1704 N.B. You might think this could be implemented more efficiently
1705 with vsnprintf(). Unfortunately, there are C libraries that
1706 provide that function but without the C99 semantics for its return
1707 value, making it impossible to know how much space is required. */
1709 oprintf (outf_p o
, const char *format
, ...)
1715 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1720 va_start (ap
, format
);
1721 slength
= vasprintf (&s
, format
, ap
);
1722 if (s
== NULL
|| (int) slength
< 0)
1723 fatal ("out of memory");
1726 if (o
->bufused
+ slength
> o
->buflength
)
1728 size_t new_len
= o
->buflength
;
1735 while (o
->bufused
+ slength
>= new_len
);
1736 o
->buf
= XRESIZEVEC (char, o
->buf
, new_len
);
1737 o
->buflength
= new_len
;
1739 memcpy (o
->buf
+ o
->bufused
, s
, slength
);
1740 o
->bufused
+= slength
;
1744 /* Open the global header file and the language-specific header files. */
1747 open_base_files (void)
1751 if (nb_plugin_files
> 0 && plugin_files
)
1754 header_file
= create_file ("GCC", "gtype-desc.h");
1756 base_files
= XNEWVEC (outf_p
, num_lang_dirs
);
1758 for (i
= 0; i
< num_lang_dirs
; i
++)
1759 base_files
[i
] = create_file (lang_dir_names
[i
],
1760 xasprintf ("gtype-%s.h", lang_dir_names
[i
]));
1762 /* gtype-desc.c is a little special, so we create it here. */
1764 /* The order of files here matters very much. */
1765 static const char *const ifiles
[] = {
1766 "config.h", "system.h", "coretypes.h", "tm.h",
1767 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1768 "tree.h", "rtl.h", "wide-int.h", "function.h", "insn-config.h", "expr.h",
1769 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1770 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1771 "pointer-set.h", "hash-table.h", "vec.h", "ggc.h", "basic-block.h",
1772 "tree-ssa-alias.h", "internal-fn.h", "gimple-fold.h", "tree-eh.h",
1773 "gimple-expr.h", "is-a.h",
1774 "gimple.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
1775 "tree-phinodes.h", "ssa-iterators.h", "stringpool.h", "tree-ssanames.h",
1776 "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h", "tree-ssa-loop-manip.h",
1777 "tree-ssa-loop-niter.h", "tree-into-ssa.h", "tree-dfa.h",
1778 "tree-ssa.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1779 "except.h", "output.h", "cfgloop.h",
1780 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h",
1781 "ipa-inline.h", "dwarf2out.h", NULL
1783 const char *const *ifp
;
1784 outf_p gtype_desc_c
;
1786 gtype_desc_c
= create_file ("GCC", "gtype-desc.c");
1787 for (ifp
= ifiles
; *ifp
; ifp
++)
1788 oprintf (gtype_desc_c
, "#include \"%s\"\n", *ifp
);
1790 /* Make sure we handle "cfun" specially. */
1791 oprintf (gtype_desc_c
, "\n/* See definition in function.h. */\n");
1792 oprintf (gtype_desc_c
, "#undef cfun\n");
1794 oprintf (gtype_desc_c
,
1796 "/* Types with a \"gcc::\" namespace have it stripped\n"
1797 " during gengtype parsing. Provide a \"using\" directive\n"
1798 " to ensure that the fully-qualified types are found. */\n"
1799 "using namespace gcc;\n");
1803 /* For INPF an input file, return the real basename of INPF, with all
1804 the directory components skipped. */
1807 get_file_realbasename (const input_file
*inpf
)
1809 return lbasename (get_input_file_name (inpf
));
1812 /* For INPF a filename, return the relative path to INPF from
1813 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1816 get_file_srcdir_relative_path (const input_file
*inpf
)
1818 const char *f
= get_input_file_name (inpf
);
1819 if (strlen (f
) > srcdir_len
1820 && IS_DIR_SEPARATOR (f
[srcdir_len
])
1821 && strncmp (f
, srcdir
, srcdir_len
) == 0)
1822 return f
+ srcdir_len
+ 1;
1827 /* For INPF an input_file, return the relative path to INPF from
1828 $(srcdir) if the latter is a prefix in INPF, or the real basename
1829 of INPF otherwise. */
1832 get_file_basename (const input_file
*inpf
)
1834 const char *srcdir_path
= get_file_srcdir_relative_path (inpf
);
1836 return (srcdir_path
!= NULL
) ? srcdir_path
: get_file_realbasename (inpf
);
1839 /* For F a filename, return the lang_dir_names relative index of the language
1840 directory that is a prefix in F, if any, -1 otherwise. */
1843 get_prefix_langdir_index (const char *f
)
1845 size_t f_len
= strlen (f
);
1848 for (lang_index
= 0; lang_index
< num_lang_dirs
; lang_index
++)
1850 const char *langdir
= lang_dir_names
[lang_index
];
1851 size_t langdir_len
= strlen (langdir
);
1853 if (f_len
> langdir_len
1854 && IS_DIR_SEPARATOR (f
[langdir_len
])
1855 && memcmp (f
, langdir
, langdir_len
) == 0)
1862 /* For INPF an input file, return the name of language directory where
1863 F is located, if any, NULL otherwise. */
1866 get_file_langdir (const input_file
*inpf
)
1868 /* Get the relative path to INPF from $(srcdir) and find the
1869 language by comparing the prefix with language directory names.
1870 If INPF is not even srcdir relative, no point in looking
1874 const char *srcdir_relative_path
= get_file_srcdir_relative_path (inpf
);
1877 if (!srcdir_relative_path
)
1880 lang_index
= get_prefix_langdir_index (srcdir_relative_path
);
1881 if (lang_index
< 0 && strncmp (srcdir_relative_path
, "c-family", 8) == 0)
1883 else if (lang_index
>= 0)
1884 r
= lang_dir_names
[lang_index
];
1891 /* The gt- output file name for INPF. */
1894 get_file_gtfilename (const input_file
*inpf
)
1896 /* Cook up an initial version of the gt- file name from the file real
1897 basename and the language name, if any. */
1899 const char *basename
= get_file_realbasename (inpf
);
1900 const char *langdir
= get_file_langdir (inpf
);
1903 (langdir
? xasprintf ("gt-%s-%s", langdir
, basename
)
1904 : xasprintf ("gt-%s", basename
));
1906 /* Then replace all non alphanumerics characters by '-' and change the
1907 extension to ".h". We expect the input filename extension was at least
1908 one character long. */
1912 for (; *s
!= '.'; s
++)
1913 if (!ISALNUM (*s
) && *s
!= '-')
1916 memcpy (s
, ".h", sizeof (".h"));
1921 /* Each input_file has its associated output file outf_p. The
1922 association is computed by the function
1923 get_output_file_with_visibility. The associated file is cached
1924 inside input_file in its inpoutf field, so is really computed only
1925 once. Associated output file paths (i.e. output_name-s) are
1926 computed by a rule based regexp machinery, using the files_rules
1927 array of struct file_rule_st. A for_name is also computed, giving
1928 the source file name for which the output_file is generated; it is
1929 often the last component of the input_file path. */
1933 Regexpr machinery to compute the output_name and for_name-s of each
1934 input_file. We have a sequence of file rules which gives the POSIX
1935 extended regular expression to match an input file path, and two
1936 transformed strings for the corresponding output_name and the
1937 corresponding for_name. The transformed string contain dollars: $0
1938 is replaced by the entire match, $1 is replaced by the substring
1939 matching the first parenthesis in the regexp, etc. And $$ is replaced
1940 by a single verbatim dollar. The rule order is important. The
1941 general case is last, and the particular cases should come before.
1942 An action routine can, when needed, update the out_name & for_name
1943 and/or return the appropriate output file. It is invoked only when a
1944 rule is triggered. When a rule is triggered, the output_name and
1945 for_name are computed using their transform string in while $$, $0,
1946 $1, ... are suitably replaced. If there is an action, it is called.
1947 In some few cases, the action can directly return the outf_p, but
1948 usually it just updates the output_name and for_name so should free
1949 them before replacing them. The get_output_file_with_visibility
1950 function creates an outf_p only once per each output_name, so it
1951 scans the output_files list for previously seen output file names.
1954 /* Signature of actions in file rules. */
1955 typedef outf_p (frul_actionrout_t
) (input_file
*, char**, char**);
1958 struct file_rule_st
{
1959 const char* frul_srcexpr
; /* Source string for regexp. */
1960 int frul_rflags
; /* Flags passed to regcomp, usually
1962 regex_t
* frul_re
; /* Compiled regular expression
1963 obtained by regcomp. */
1964 const char* frul_tr_out
; /* Transformation string for making
1965 * the output_name, with $1 ... $9 for
1966 * subpatterns and $0 for the whole
1967 * matched filename. */
1968 const char* frul_tr_for
; /* Tranformation string for making the
1970 frul_actionrout_t
* frul_action
; /* The action, if non null, is
1971 * called once the rule matches, on
1972 * the transformed out_name &
1973 * for_name. It could change them
1974 * and/or give the output file. */
1977 /* File rule action handling *.h files. */
1978 static outf_p
header_dot_h_frul (input_file
*, char**, char**);
1980 /* File rule action handling *.c files. */
1981 static outf_p
source_dot_c_frul (input_file
*, char**, char**);
1983 #define NULL_REGEX (regex_t*)0
1985 /* The prefix in our regexp-s matching the directory. */
1986 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1988 #define NULL_FRULACT (frul_actionrout_t*)0
1990 /* The array of our rules governing file name generation. Rules order
1991 matters, so change with extreme care! */
1993 struct file_rule_st files_rules
[] = {
1994 /* The general rule assumes that files in subdirectories belong to a
1995 particular front-end, and files not in subdirectories are shared.
1996 The following rules deal with exceptions - files that are in
1997 subdirectories and yet are shared, and files that are top-level,
1998 but are not shared. */
2000 /* the c-family/ source directory is special. */
2001 { DIR_PREFIX_REGEX
"c-family/([[:alnum:]_-]*)\\.c$",
2002 REG_EXTENDED
, NULL_REGEX
,
2003 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT
},
2005 { DIR_PREFIX_REGEX
"c-family/([[:alnum:]_-]*)\\.h$",
2006 REG_EXTENDED
, NULL_REGEX
,
2007 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT
},
2009 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
2010 { DIR_PREFIX_REGEX
"c/c-lang\\.h$",
2011 REG_EXTENDED
, NULL_REGEX
, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT
},
2013 { DIR_PREFIX_REGEX
"c/c-tree\\.h$",
2014 REG_EXTENDED
, NULL_REGEX
, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT
},
2016 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
2017 { DIR_PREFIX_REGEX
"cp/cp-tree\\.h$",
2018 REG_EXTENDED
, NULL_REGEX
,
2019 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT
},
2021 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
2022 { DIR_PREFIX_REGEX
"cp/decl\\.[ch]$",
2023 REG_EXTENDED
, NULL_REGEX
,
2024 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT
},
2026 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
2027 { DIR_PREFIX_REGEX
"cp/name-lookup\\.h$",
2028 REG_EXTENDED
, NULL_REGEX
,
2029 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT
},
2031 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
2032 { DIR_PREFIX_REGEX
"cp/parser\\.h$",
2033 REG_EXTENDED
, NULL_REGEX
,
2034 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT
},
2036 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
2037 { DIR_PREFIX_REGEX
"objc/objc-act\\.h$",
2038 REG_EXTENDED
, NULL_REGEX
,
2039 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT
},
2041 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
2042 { DIR_PREFIX_REGEX
"objc/objc-map\\.h$",
2043 REG_EXTENDED
, NULL_REGEX
,
2044 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT
},
2046 /* General cases. For header *.h and source *.c or *.cc files, we
2047 * need special actions to handle the language. */
2049 /* Source *.c files are using get_file_gtfilename to compute their
2050 output_name and get_file_basename to compute their for_name
2051 through the source_dot_c_frul action. */
2052 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.c$",
2053 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.c", source_dot_c_frul
},
2055 /* Source *.cc files are using get_file_gtfilename to compute their
2056 output_name and get_file_basename to compute their for_name
2057 through the source_dot_c_frul action. */
2058 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.cc$",
2059 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.cc", source_dot_c_frul
},
2061 /* Common header files get "gtype-desc.c" as their output_name,
2062 * while language specific header files are handled specially. So
2063 * we need the header_dot_h_frul action. */
2064 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.h$",
2065 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.h", header_dot_h_frul
},
2067 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.in$",
2068 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.in", NULL_FRULACT
},
2070 /* Mandatory null last entry signaling end of rules. */
2071 {NULL
, 0, NULL_REGEX
, NULL
, NULL
, NULL_FRULACT
}
2074 /* Special file rules action for handling *.h header files. It gives
2075 "gtype-desc.c" for common headers and corresponding output
2076 files for language-specific header files. */
2078 header_dot_h_frul (input_file
* inpf
, char**poutname
,
2079 char**pforname ATTRIBUTE_UNUSED
)
2081 const char *basename
= 0;
2083 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2084 (void*) inpf
, get_input_file_name (inpf
),
2085 *poutname
, *pforname
);
2086 basename
= get_file_basename (inpf
);
2087 lang_index
= get_prefix_langdir_index (basename
);
2088 DBGPRINTF ("basename %s lang_index %d", basename
, lang_index
);
2090 if (lang_index
>= 0)
2092 /* The header is language specific. Given output_name &
2093 for_name remains unchanged. The base_files array gives the
2095 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2096 (void*) base_files
[lang_index
],
2097 (base_files
[lang_index
])->name
);
2098 return base_files
[lang_index
];
2102 /* The header is common to all front-end languages. So
2103 output_name is "gtype-desc.c" file. The calling function
2104 get_output_file_with_visibility will find its outf_p. */
2106 *poutname
= xstrdup ("gtype-desc.c");
2107 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2108 get_input_file_name (inpf
));
2114 /* Special file rules action for handling *.c source files using
2115 * get_file_gtfilename to compute their output_name and
2116 * get_file_basename to compute their for_name. The output_name is
2117 * gt-<LANG>-<BASE>.h for language specific source files, and
2118 * gt-<BASE>.h for common source files. */
2120 source_dot_c_frul (input_file
* inpf
, char**poutname
, char**pforname
)
2122 char *newbasename
= CONST_CAST (char*, get_file_basename (inpf
));
2123 char *newoutname
= CONST_CAST (char*, get_file_gtfilename (inpf
));
2124 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2125 (void*) inpf
, get_input_file_name (inpf
),
2126 *poutname
, *pforname
);
2127 DBGPRINTF ("newoutname %s", newoutname
);
2128 DBGPRINTF ("newbasename %s", newbasename
);
2131 *poutname
= newoutname
;
2132 *pforname
= newbasename
;
2136 /* Utility function for get_output_file_with_visibility which returns
2137 * a malloc-ed substituted string using TRS on matching of the FILNAM
2138 * file name, using the PMATCH array. */
2140 matching_file_name_substitute (const char *filnam
, regmatch_t pmatch
[10],
2143 struct obstack str_obstack
;
2145 char *rawstr
= NULL
;
2146 const char *pt
= NULL
;
2147 DBGPRINTF ("filnam %s", filnam
);
2148 obstack_init (&str_obstack
);
2149 for (pt
= trs
; *pt
; pt
++) {
2155 /* A double dollar $$ is substituted by a single verbatim
2156 dollar, but who really uses dollar signs in file
2158 obstack_1grow (&str_obstack
, '$');
2160 else if (ISDIGIT (pt
[1]))
2162 /* Handle $0 $1 ... $9 by appropriate substitution. */
2163 int dolnum
= pt
[1] - '0';
2164 int so
= pmatch
[dolnum
].rm_so
;
2165 int eo
= pmatch
[dolnum
].rm_eo
;
2166 DBGPRINTF ("so=%d eo=%d dolnum=%d", so
, eo
, dolnum
);
2167 if (so
>=0 && eo
>=so
)
2168 obstack_grow (&str_obstack
, filnam
+ so
, eo
- so
);
2172 /* This can happen only when files_rules is buggy! */
2175 /* Always skip the character after the dollar. */
2179 obstack_1grow (&str_obstack
, c
);
2181 obstack_1grow (&str_obstack
, '\0');
2182 rawstr
= XOBFINISH (&str_obstack
, char *);
2183 str
= xstrdup (rawstr
);
2184 obstack_free (&str_obstack
, NULL
);
2185 DBGPRINTF ("matched replacement %s", str
);
2191 /* An output file, suitable for definitions, that can see declarations
2192 made in INPF and is linked into every language that uses INPF.
2193 Since the result is cached inside INPF, that argument cannot be
2194 declared constant, but is "almost" constant. */
2197 get_output_file_with_visibility (input_file
*inpf
)
2200 char *for_name
= NULL
;
2201 char *output_name
= NULL
;
2202 const char* inpfname
;
2204 /* This can happen when we need a file with visibility on a
2205 structure that we've never seen. We have to just hope that it's
2206 globally visible. */
2208 inpf
= system_h_file
;
2210 /* The result is cached in INPF, so return it if already known. */
2212 return inpf
->inpoutf
;
2214 /* In plugin mode, return NULL unless the input_file is one of the
2219 for (i
= 0; i
< nb_plugin_files
; i
++)
2220 if (inpf
== plugin_files
[i
])
2222 inpf
->inpoutf
= plugin_output
;
2223 return plugin_output
;
2229 inpfname
= get_input_file_name (inpf
);
2231 /* Try each rule in sequence in files_rules until one is triggered. */
2234 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2235 (void*) inpf
, inpfname
);
2237 for (; files_rules
[rulix
].frul_srcexpr
!= NULL
; rulix
++)
2239 DBGPRINTF ("rulix#%d srcexpr %s",
2240 rulix
, files_rules
[rulix
].frul_srcexpr
);
2242 if (!files_rules
[rulix
].frul_re
)
2244 /* Compile the regexpr lazily. */
2246 files_rules
[rulix
].frul_re
= XCNEW (regex_t
);
2247 err
= regcomp (files_rules
[rulix
].frul_re
,
2248 files_rules
[rulix
].frul_srcexpr
,
2249 files_rules
[rulix
].frul_rflags
);
2252 /* The regular expression compilation fails only when
2253 file_rules is buggy. */
2261 /* Match the regexpr and trigger the rule if matched. */
2263 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2265 regmatch_t pmatch
[10];
2266 memset (pmatch
, 0, sizeof (pmatch
));
2267 if (!regexec (files_rules
[rulix
].frul_re
,
2268 inpfname
, 10, pmatch
, 0))
2270 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2271 (void*) inpf
, inpfname
, rulix
,
2272 files_rules
[rulix
].frul_srcexpr
);
2274 matching_file_name_substitute (inpfname
, pmatch
,
2275 files_rules
[rulix
].frul_tr_for
);
2276 DBGPRINTF ("for_name %s", for_name
);
2278 matching_file_name_substitute (inpfname
, pmatch
,
2279 files_rules
[rulix
].frul_tr_out
);
2280 DBGPRINTF ("output_name %s", output_name
);
2281 if (files_rules
[rulix
].frul_action
)
2283 /* Invoke our action routine. */
2285 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2286 rulix
, output_name
, for_name
);
2288 (files_rules
[rulix
].frul_action
) (inpf
,
2289 &output_name
, &for_name
);
2290 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2291 rulix
, (void*)of
, output_name
, for_name
);
2292 /* If the action routine returned something, give it back
2293 immediately and cache it in inpf. */
2300 /* The rule matched, and had no action, or that action did
2301 not return any output file but could have changed the
2302 output_name or for_name. We break out of the loop on the
2308 /* The regexpr did not match. */
2309 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2310 rulix
, inpfname
, files_rules
[rulix
].frul_srcexpr
);
2316 if (!output_name
|| !for_name
)
2318 /* This should not be possible, and could only happen if the
2319 files_rules is incomplete or buggy. */
2320 fatal ("failed to compute output name for %s", inpfname
);
2323 /* Look through to see if we've ever seen this output filename
2324 before. If found, cache the result in inpf. */
2325 for (r
= output_files
; r
; r
= r
->next
)
2326 if (filename_cmp (r
->name
, output_name
) == 0)
2329 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r
,
2330 output_name
, for_name
);
2334 /* If not found, create it, and cache it in inpf. */
2335 r
= create_file (for_name
, output_name
);
2337 gcc_assert (r
&& r
->name
);
2338 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r
,
2339 output_name
, for_name
);
2346 /* The name of an output file, suitable for definitions, that can see
2347 declarations made in INPF and is linked into every language that
2351 get_output_file_name (input_file
* inpf
)
2353 outf_p o
= get_output_file_with_visibility (inpf
);
2359 /* Check if existing file is equal to the in memory buffer. */
2362 is_file_equal (outf_p of
)
2364 FILE *newfile
= fopen (of
->name
, "r");
2367 if (newfile
== NULL
)
2371 for (i
= 0; i
< of
->bufused
; i
++)
2374 ch
= fgetc (newfile
);
2375 if (ch
== EOF
|| ch
!= (unsigned char) of
->buf
[i
])
2381 if (equal
&& EOF
!= fgetc (newfile
))
2387 /* Copy the output to its final destination,
2388 but don't unnecessarily change modification times. */
2391 close_output_files (void)
2393 int nbwrittenfiles
= 0;
2396 for (of
= output_files
; of
; of
= of
->next
)
2398 if (!is_file_equal (of
))
2400 FILE *newfile
= NULL
;
2401 char *backupname
= NULL
;
2402 /* Back up the old version of the output file gt-FOO.c as
2403 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2406 backupname
= concat (backup_dir
, "/",
2407 lbasename (of
->name
), "~", NULL
);
2408 if (!access (of
->name
, F_OK
) && rename (of
->name
, backupname
))
2409 fatal ("failed to back up %s as %s: %s",
2410 of
->name
, backupname
, xstrerror (errno
));
2413 newfile
= fopen (of
->name
, "w");
2414 if (newfile
== NULL
)
2415 fatal ("opening output file %s: %s", of
->name
, xstrerror (errno
));
2416 if (fwrite (of
->buf
, 1, of
->bufused
, newfile
) != of
->bufused
)
2417 fatal ("writing output file %s: %s", of
->name
, xstrerror (errno
));
2418 if (fclose (newfile
) != 0)
2419 fatal ("closing output file %s: %s", of
->name
, xstrerror (errno
));
2421 if (verbosity_level
>= 2 && backupname
)
2422 printf ("%s wrote #%-3d %s backed-up in %s\n",
2423 progname
, nbwrittenfiles
, of
->name
, backupname
);
2424 else if (verbosity_level
>= 1)
2425 printf ("%s write #%-3d %s\n", progname
, nbwrittenfiles
, of
->name
);
2430 /* output file remains unchanged. */
2431 if (verbosity_level
>= 2)
2432 printf ("%s keep %s\n", progname
, of
->name
);
2436 of
->bufused
= of
->buflength
= 0;
2438 if (verbosity_level
>= 1)
2439 printf ("%s wrote %d files.\n", progname
, nbwrittenfiles
);
2446 const input_file
* file
;
2450 struct walk_type_data
;
2452 /* For scalars and strings, given the item in 'val'.
2453 For structures, given a pointer to the item in 'val'.
2454 For misc. pointers, given the item in 'val'.
2456 typedef void (*process_field_fn
) (type_p f
, const struct walk_type_data
* p
);
2457 typedef void (*func_name_fn
) (type_p s
, const struct walk_type_data
* p
);
2459 /* Parameters for write_types. */
2461 struct write_types_data
2464 const char *param_prefix
;
2465 const char *subfield_marker_routine
;
2466 const char *marker_routine
;
2467 const char *reorder_note_routine
;
2468 const char *comment
;
2469 int skip_hooks
; /* skip hook generation if non zero */
2472 static void output_escaped_param (struct walk_type_data
*d
,
2473 const char *, const char *);
2474 static void output_mangled_typename (outf_p
, const_type_p
);
2475 static void walk_type (type_p t
, struct walk_type_data
*d
);
2476 static void write_func_for_structure (type_p orig_s
, type_p s
, type_p
*param
,
2477 const struct write_types_data
*wtd
);
2478 static void write_types_process_field
2479 (type_p f
, const struct walk_type_data
*d
);
2480 static void write_types (outf_p output_header
,
2482 type_p param_structs
,
2483 const struct write_types_data
*wtd
);
2484 static void write_types_local_process_field
2485 (type_p f
, const struct walk_type_data
*d
);
2486 static void write_local_func_for_structure
2487 (const_type_p orig_s
, type_p s
, type_p
*param
);
2488 static void write_local (outf_p output_header
,
2489 type_p structures
, type_p param_structs
);
2490 static int contains_scalar_p (type_p t
);
2491 static void put_mangled_filename (outf_p
, const input_file
*);
2492 static void finish_root_table (struct flist
*flp
, const char *pfx
,
2493 const char *tname
, const char *lastname
,
2495 static void write_root (outf_p
, pair_p
, type_p
, const char *, int,
2496 struct fileloc
*, const char *, bool);
2497 static void write_array (outf_p f
, pair_p v
,
2498 const struct write_types_data
*wtd
);
2499 static void write_roots (pair_p
, bool);
2501 /* Parameters for walk_type. */
2503 struct walk_type_data
2505 process_field_fn process_field
;
2510 const char *prev_val
[4];
2513 const struct fileloc
*line
;
2518 const char *reorder_fn
;
2520 bool fn_wants_lvalue
;
2528 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2529 pre-processor identifier to use in a #define directive. This replaces
2530 special characters used in C++ identifiers like '>', '<' and ':' with
2533 If no C++ special characters are found in TYPE_NAME, return
2534 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2535 characters replaced with '_'. In this case, the caller is
2536 responsible for freeing the allocated string. */
2539 filter_type_name (const char *type_name
)
2541 if (strchr (type_name
, '<') || strchr (type_name
, ':'))
2544 char *s
= xstrdup (type_name
);
2545 for (i
= 0; i
< strlen (s
); i
++)
2546 if (s
[i
] == '<' || s
[i
] == '>' || s
[i
] == ':' || s
[i
] == ',')
2555 /* Print a mangled name representing T to OF. */
2558 output_mangled_typename (outf_p of
, const_type_p t
)
2566 case TYPE_UNDEFINED
:
2571 output_mangled_typename (of
, t
->u
.p
);
2581 case TYPE_LANG_STRUCT
:
2582 case TYPE_USER_STRUCT
:
2584 /* For references to classes within an inheritance hierarchy,
2585 only ever reference the ultimate base class, since only
2586 it will have gt_ functions. */
2587 t
= get_ultimate_base_class (t
);
2588 const char *id_for_tag
= filter_type_name (t
->u
.s
.tag
);
2589 oprintf (of
, "%lu%s", (unsigned long) strlen (id_for_tag
),
2591 if (id_for_tag
!= t
->u
.s
.tag
)
2592 free (CONST_CAST (char *, id_for_tag
));
2595 case TYPE_PARAM_STRUCT
:
2598 for (i
= 0; i
< NUM_PARAM
; i
++)
2599 if (t
->u
.param_struct
.param
[i
] != NULL
)
2600 output_mangled_typename (of
, t
->u
.param_struct
.param
[i
]);
2601 output_mangled_typename (of
, t
->u
.param_struct
.stru
);
2609 /* Print PARAM to D->OF processing escapes. D->VAL references the
2610 current object, D->PREV_VAL the object containing the current
2611 object, ONAME is the name of the option and D->LINE is used to
2612 print error messages. */
2615 output_escaped_param (struct walk_type_data
*d
, const char *param
,
2620 for (p
= param
; *p
; p
++)
2622 oprintf (d
->of
, "%c", *p
);
2627 oprintf (d
->of
, "(%s)", d
->prev_val
[2]);
2630 oprintf (d
->of
, "(%s)", d
->prev_val
[0]);
2633 oprintf (d
->of
, "(%s)", d
->prev_val
[1]);
2637 const char *pp
= d
->val
+ strlen (d
->val
);
2638 while (pp
[-1] == ']')
2641 oprintf (d
->of
, "%s", pp
);
2645 error_at_line (d
->line
, "`%s' option contains bad escape %c%c",
2651 get_string_option (options_p opt
, const char *key
)
2653 for (; opt
; opt
= opt
->next
)
2654 if (strcmp (opt
->name
, key
) == 0)
2655 return opt
->info
.string
;
2659 /* Machinery for avoiding duplicate tags within switch statements. */
2663 struct seen_tag
*next
;
2667 already_seen_tag (struct seen_tag
*seen_tags
, const char *tag
)
2669 /* Linear search, so O(n^2), but n is currently small. */
2672 if (!strcmp (seen_tags
->tag
, tag
))
2674 seen_tags
= seen_tags
->next
;
2676 /* Not yet seen this tag. */
2681 mark_tag_as_seen (struct seen_tag
**seen_tags
, const char *tag
)
2683 /* Add to front of linked list. */
2684 struct seen_tag
*new_node
= XCNEW (struct seen_tag
);
2685 new_node
->tag
= tag
;
2686 new_node
->next
= *seen_tags
;
2687 *seen_tags
= new_node
;
2691 walk_subclasses (type_p base
, struct walk_type_data
*d
,
2692 struct seen_tag
**seen_tags
)
2694 for (type_p sub
= base
->u
.s
.first_subclass
; sub
!= NULL
;
2695 sub
= sub
->u
.s
.next_sibling_class
)
2697 const char *type_tag
= get_string_option (sub
->u
.s
.opt
, "tag");
2698 if (type_tag
&& !already_seen_tag (*seen_tags
, type_tag
))
2700 mark_tag_as_seen (seen_tags
, type_tag
);
2701 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", type_tag
);
2703 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2705 oprintf (d
->of
, "%*s%s *sub = static_cast <%s *> (x);\n",
2706 d
->indent
, "", sub
->u
.s
.tag
, sub
->u
.s
.tag
);
2707 const char *old_val
= d
->val
;
2712 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2713 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
2716 walk_subclasses (sub
, d
, seen_tags
);
2720 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2721 which is of type T. Write code to D->OF to constrain execution (at
2722 the point that D->PROCESS_FIELD is called) to the appropriate
2723 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2724 pointers to those objects. D->PREV_VAL lists the objects
2725 containing the current object, D->OPT is a list of options to
2726 apply, D->INDENT is the current indentation level, D->LINE is used
2727 to print error messages, D->BITMAP indicates which languages to
2728 print the structure for, and D->PARAM is the current parameter
2729 (from an enclosing param_is option). */
2732 walk_type (type_p t
, struct walk_type_data
*d
)
2734 const char *length
= NULL
;
2735 const char *desc
= NULL
;
2736 const char *type_tag
= NULL
;
2737 int maybe_undef_p
= 0;
2738 int use_param_num
= -1;
2739 int use_params_p
= 0;
2742 const struct nested_ptr_data
*nested_ptr_d
= NULL
;
2744 d
->needs_cast_p
= false;
2745 for (oo
= d
->opt
; oo
; oo
= oo
->next
)
2746 if (strcmp (oo
->name
, "length") == 0 && oo
->kind
== OPTION_STRING
)
2747 length
= oo
->info
.string
;
2748 else if (strcmp (oo
->name
, "maybe_undef") == 0)
2750 else if (strncmp (oo
->name
, "use_param", 9) == 0
2751 && (oo
->name
[9] == '\0' || ISDIGIT (oo
->name
[9])))
2752 use_param_num
= oo
->name
[9] == '\0' ? 0 : oo
->name
[9] - '0';
2753 else if (strcmp (oo
->name
, "use_params") == 0)
2755 else if (strcmp (oo
->name
, "desc") == 0 && oo
->kind
== OPTION_STRING
)
2756 desc
= oo
->info
.string
;
2757 else if (strcmp (oo
->name
, "mark_hook") == 0)
2759 else if (strcmp (oo
->name
, "nested_ptr") == 0
2760 && oo
->kind
== OPTION_NESTED
)
2761 nested_ptr_d
= (const struct nested_ptr_data
*) oo
->info
.nested
;
2762 else if (strcmp (oo
->name
, "dot") == 0)
2764 else if (strcmp (oo
->name
, "tag") == 0)
2765 type_tag
= oo
->info
.string
;
2766 else if (strcmp (oo
->name
, "special") == 0)
2768 else if (strcmp (oo
->name
, "skip") == 0)
2770 else if (strcmp (oo
->name
, "atomic") == 0)
2772 else if (strcmp (oo
->name
, "default") == 0)
2774 else if (strcmp (oo
->name
, "param_is") == 0)
2776 else if (strncmp (oo
->name
, "param", 5) == 0
2777 && ISDIGIT (oo
->name
[5]) && strcmp (oo
->name
+ 6, "_is") == 0)
2779 else if (strcmp (oo
->name
, "chain_next") == 0)
2781 else if (strcmp (oo
->name
, "chain_prev") == 0)
2783 else if (strcmp (oo
->name
, "chain_circular") == 0)
2785 else if (strcmp (oo
->name
, "reorder") == 0)
2787 else if (strcmp (oo
->name
, "variable_size") == 0)
2790 error_at_line (d
->line
, "unknown option `%s'\n", oo
->name
);
2797 int pointer_p
= t
->kind
== TYPE_POINTER
;
2801 if (!union_or_struct_p (t
))
2802 error_at_line (d
->line
, "`use_params' option on unimplemented type");
2804 t
= find_param_structure (t
, d
->param
);
2806 t
= create_pointer (t
);
2809 if (use_param_num
!= -1)
2811 if (d
->param
!= NULL
&& d
->param
[use_param_num
] != NULL
)
2813 type_p nt
= d
->param
[use_param_num
];
2815 if (t
->kind
== TYPE_ARRAY
)
2816 nt
= create_array (nt
, t
->u
.a
.len
);
2817 else if (length
!= NULL
&& t
->kind
== TYPE_POINTER
)
2818 nt
= create_pointer (nt
);
2819 d
->needs_cast_p
= (t
->kind
!= TYPE_POINTER
2820 && (nt
->kind
== TYPE_POINTER
2821 || nt
->kind
== TYPE_STRING
));
2825 error_at_line (d
->line
, "no parameter defined for `%s'", d
->val
);
2829 && (t
->kind
!= TYPE_POINTER
|| !union_or_struct_p (t
->u
.p
)))
2831 error_at_line (d
->line
,
2832 "field `%s' has invalid option `maybe_undef_p'\n",
2837 if (atomic_p
&& (t
->kind
!= TYPE_POINTER
) && (t
->kind
!= TYPE_STRING
))
2839 error_at_line (d
->line
, "field `%s' has invalid option `atomic'\n", d
->val
);
2847 d
->process_field (t
, d
);
2852 d
->in_ptr_field
= true;
2853 if (maybe_undef_p
&& t
->u
.p
->u
.s
.line
.file
== NULL
)
2855 oprintf (d
->of
, "%*sgcc_assert (!%s);\n", d
->indent
, "", d
->val
);
2859 /* If a pointer type is marked as "atomic", we process the
2860 field itself, but we don't walk the data that they point to.
2862 There are two main cases where we walk types: to mark
2863 pointers that are reachable, and to relocate pointers when
2864 writing a PCH file. In both cases, an atomic pointer is
2865 itself marked or relocated, but the memory that it points
2866 to is left untouched. In the case of PCH, that memory will
2867 be read/written unchanged to the PCH file. */
2870 oprintf (d
->of
, "%*sif (%s != NULL) {\n", d
->indent
, "", d
->val
);
2872 d
->process_field (t
, d
);
2874 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2880 if (!union_or_struct_p (t
->u
.p
)
2881 && t
->u
.p
->kind
!= TYPE_PARAM_STRUCT
)
2883 error_at_line (d
->line
,
2884 "field `%s' is pointer to unimplemented type",
2891 const char *oldprevval2
= d
->prev_val
[2];
2893 if (!union_or_struct_p (nested_ptr_d
->type
))
2895 error_at_line (d
->line
,
2896 "field `%s' has invalid "
2897 "option `nested_ptr'\n", d
->val
);
2901 d
->prev_val
[2] = d
->val
;
2902 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2904 d
->val
= xasprintf ("x%d", d
->counter
++);
2905 oprintf (d
->of
, "%*s%s %s * %s%s =\n", d
->indent
, "",
2906 (nested_ptr_d
->type
->kind
== TYPE_UNION
2907 ? "union" : "struct"),
2908 nested_ptr_d
->type
->u
.s
.tag
,
2909 d
->fn_wants_lvalue
? "" : "const ", d
->val
);
2910 oprintf (d
->of
, "%*s", d
->indent
+ 2, "");
2911 output_escaped_param (d
, nested_ptr_d
->convert_from
,
2913 oprintf (d
->of
, ";\n");
2915 d
->process_field (nested_ptr_d
->type
, d
);
2917 if (d
->fn_wants_lvalue
)
2919 oprintf (d
->of
, "%*s%s = ", d
->indent
, "",
2921 d
->prev_val
[2] = d
->val
;
2922 output_escaped_param (d
, nested_ptr_d
->convert_to
,
2924 oprintf (d
->of
, ";\n");
2928 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2929 d
->val
= d
->prev_val
[2];
2930 d
->prev_val
[2] = oldprevval2
;
2933 d
->process_field (t
->u
.p
, d
);
2937 int loopcounter
= d
->loopcounter
;
2938 const char *oldval
= d
->val
;
2939 const char *oldprevval3
= d
->prev_val
[3];
2942 oprintf (d
->of
, "%*sif (%s != NULL) {\n", d
->indent
, "", d
->val
);
2944 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
2945 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != (size_t)(", d
->indent
,
2946 "", loopcounter
, loopcounter
);
2947 if (!d
->in_record_p
)
2948 output_escaped_param (d
, length
, "length");
2950 oprintf (d
->of
, "l%d", loopcounter
);
2951 if (d
->have_this_obj
)
2952 /* Try to unswitch loops (see PR53880). */
2953 oprintf (d
->of
, ") && ((void *)%s == this_obj", oldval
);
2954 oprintf (d
->of
, "); i%d++) {\n", loopcounter
);
2956 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
2958 d
->prev_val
[3] = oldval
;
2959 walk_type (t
->u
.p
, d
);
2962 d
->prev_val
[3] = oldprevval3
;
2965 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2966 d
->process_field (t
, d
);
2968 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2970 d
->in_ptr_field
= false;
2977 const char *oldval
= d
->val
;
2980 /* If it's an array of scalars, we optimize by not generating
2982 if (t
->u
.a
.p
->kind
== TYPE_SCALAR
)
2986 loopcounter
= d
->loopcounter
;
2988 loopcounter
= d
->counter
++;
2990 /* When walking an array, compute the length and store it in a
2991 local variable before walking the array elements, instead of
2992 recomputing the length expression each time through the loop.
2993 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2994 where the length is stored in the first array element,
2995 because otherwise that operand can get overwritten on the
2997 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2999 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
3000 if (!d
->in_record_p
|| !length
)
3002 oprintf (d
->of
, "%*ssize_t l%d = (size_t)(",
3003 d
->indent
, "", loopcounter
);
3005 output_escaped_param (d
, length
, "length");
3007 oprintf (d
->of
, "%s", t
->u
.a
.len
);
3008 oprintf (d
->of
, ");\n");
3011 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
3013 loopcounter
, loopcounter
, loopcounter
, loopcounter
);
3015 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
3017 walk_type (t
->u
.a
.p
, d
);
3022 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3024 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3032 const char *oldval
= d
->val
;
3033 const char *oldprevval1
= d
->prev_val
[1];
3034 const char *oldprevval2
= d
->prev_val
[2];
3035 const char *struct_mark_hook
= NULL
;
3036 const int union_p
= t
->kind
== TYPE_UNION
;
3037 int seen_default_p
= 0;
3039 int lengths_seen
= 0;
3041 bool any_length_seen
= false;
3043 if (!t
->u
.s
.line
.file
)
3044 error_at_line (d
->line
, "incomplete structure `%s'", t
->u
.s
.tag
);
3046 if ((d
->bitmap
& t
->u
.s
.bitmap
) != d
->bitmap
)
3048 error_at_line (d
->line
,
3049 "structure `%s' defined for mismatching languages",
3051 error_at_line (&t
->u
.s
.line
, "one structure defined here");
3054 /* Some things may also be defined in the structure's options. */
3055 for (o
= t
->u
.s
.opt
; o
; o
= o
->next
)
3056 if (!desc
&& strcmp (o
->name
, "desc") == 0
3057 && o
->kind
== OPTION_STRING
)
3058 desc
= o
->info
.string
;
3059 else if (!struct_mark_hook
&& strcmp (o
->name
, "mark_hook") == 0
3060 && o
->kind
== OPTION_STRING
)
3061 struct_mark_hook
= o
->info
.string
;
3063 if (struct_mark_hook
)
3064 oprintf (d
->of
, "%*s%s (&%s);\n",
3065 d
->indent
, "", struct_mark_hook
, oldval
);
3067 d
->prev_val
[2] = oldval
;
3068 d
->prev_val
[1] = oldprevval2
;
3073 error_at_line (d
->line
,
3074 "missing `desc' option for union `%s'",
3078 oprintf (d
->of
, "%*sswitch (", d
->indent
, "");
3079 output_escaped_param (d
, desc
, "desc");
3080 oprintf (d
->of
, ")\n");
3082 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
3086 /* We have a "desc" option on a struct, signifying the
3087 base class within a GC-managed inheritance hierarchy.
3088 The current code specialcases the base class, then walks
3089 into subclasses, recursing into this routine to handle them.
3090 This organization requires the base class to have a case in
3091 the switch statement, and hence a tag value is mandatory
3092 for the base class. This restriction could be removed, but
3093 it would require some restructing of this code. */
3096 error_at_line (d
->line
,
3097 "missing `tag' option for type `%s'",
3100 oprintf (d
->of
, "%*sswitch (", d
->indent
, "");
3101 output_escaped_param (d
, desc
, "desc");
3102 oprintf (d
->of
, ")\n");
3104 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
3105 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", type_tag
);
3109 FOR_ALL_INHERITED_FIELDS (t
, f
)
3113 const char *fieldlength
= NULL
;
3115 d
->reorder_fn
= NULL
;
3116 for (oo
= f
->opt
; oo
; oo
= oo
->next
)
3117 if (strcmp (oo
->name
, "skip") == 0)
3119 else if (strcmp (oo
->name
, "length") == 0
3120 && oo
->kind
== OPTION_STRING
)
3121 fieldlength
= oo
->info
.string
;
3131 if (!any_length_seen
)
3133 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
3136 any_length_seen
= true;
3138 oprintf (d
->of
, "%*ssize_t l%d = (size_t)(",
3139 d
->indent
, "", d
->counter
- 1);
3140 output_escaped_param (d
, fieldlength
, "length");
3141 oprintf (d
->of
, ");\n");
3145 endcounter
= d
->counter
;
3147 FOR_ALL_INHERITED_FIELDS (t
, f
)
3150 const char *dot
= ".";
3151 const char *tagid
= NULL
;
3154 int use_param_p
= 0;
3155 const char *fieldlength
= NULL
;
3158 d
->reorder_fn
= NULL
;
3159 for (oo
= f
->opt
; oo
; oo
= oo
->next
)
3160 if (strcmp (oo
->name
, "dot") == 0
3161 && oo
->kind
== OPTION_STRING
)
3162 dot
= oo
->info
.string
;
3163 else if (strcmp (oo
->name
, "tag") == 0
3164 && oo
->kind
== OPTION_STRING
)
3165 tagid
= oo
->info
.string
;
3166 else if (strcmp (oo
->name
, "skip") == 0)
3168 else if (strcmp (oo
->name
, "default") == 0)
3170 else if (strcmp (oo
->name
, "reorder") == 0
3171 && oo
->kind
== OPTION_STRING
)
3172 d
->reorder_fn
= oo
->info
.string
;
3173 else if (strncmp (oo
->name
, "use_param", 9) == 0
3174 && (oo
->name
[9] == '\0' || ISDIGIT (oo
->name
[9])))
3176 else if (strcmp (oo
->name
, "length") == 0
3177 && oo
->kind
== OPTION_STRING
)
3178 fieldlength
= oo
->info
.string
;
3183 if (union_p
&& tagid
)
3185 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", tagid
);
3188 else if (union_p
&& default_p
)
3190 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
3194 else if (!union_p
&& (default_p
|| tagid
))
3195 error_at_line (d
->line
,
3196 "can't use `%s' outside a union on field `%s'",
3197 default_p
? "default" : "tag", f
->name
);
3198 else if (union_p
&& !(default_p
|| tagid
)
3199 && f
->type
->kind
== TYPE_SCALAR
)
3202 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3203 get_input_file_name (d
->line
->file
), d
->line
->line
,
3207 else if (union_p
&& !(default_p
|| tagid
))
3208 error_at_line (d
->line
,
3209 "field `%s' is missing `tag' or `default' option",
3214 d
->loopcounter
= endcounter
- lengths_seen
--;
3218 d
->val
= newval
= xasprintf ("%s%s%s", oldval
, dot
, f
->name
);
3220 d
->used_length
= false;
3221 d
->in_record_p
= !union_p
;
3223 if (union_p
&& use_param_p
&& d
->param
== NULL
)
3224 oprintf (d
->of
, "%*sgcc_unreachable ();\n", d
->indent
, "");
3226 walk_type (f
->type
, d
);
3228 d
->in_record_p
= false;
3234 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
3238 d
->reorder_fn
= NULL
;
3241 d
->prev_val
[1] = oldprevval1
;
3242 d
->prev_val
[2] = oldprevval2
;
3244 if (union_p
&& !seen_default_p
)
3246 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
3247 oprintf (d
->of
, "%*s break;\n", d
->indent
, "");
3250 if (desc
&& !union_p
)
3252 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
3257 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3262 /* Add cases to handle subclasses. */
3263 struct seen_tag
*tags
= NULL
;
3264 walk_subclasses (t
, d
, &tags
);
3266 /* Ensure that if someone forgets a "tag" option that we don't
3267 silent fail to traverse that subclass's fields. */
3268 if (!seen_default_p
)
3270 oprintf (d
->of
, "%*s/* Unrecognized tag value. */\n",
3272 oprintf (d
->of
, "%*sdefault: gcc_unreachable (); \n",
3276 /* End of the switch statement */
3277 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3280 if (any_length_seen
)
3283 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3288 case TYPE_LANG_STRUCT
:
3291 for (nt
= t
->u
.s
.lang_struct
; nt
; nt
= nt
->next
)
3292 if ((d
->bitmap
& nt
->u
.s
.bitmap
) == d
->bitmap
)
3295 error_at_line (d
->line
, "structure `%s' differs between languages",
3302 case TYPE_PARAM_STRUCT
:
3304 type_p
*oldparam
= d
->param
;
3306 d
->param
= t
->u
.param_struct
.param
;
3307 walk_type (t
->u
.param_struct
.stru
, d
);
3308 d
->param
= oldparam
;
3312 case TYPE_USER_STRUCT
:
3313 d
->process_field (t
, d
);
3317 case TYPE_UNDEFINED
:
3322 /* process_field routine for marking routines. */
3325 write_types_process_field (type_p f
, const struct walk_type_data
*d
)
3327 const struct write_types_data
*wtd
;
3328 const char *cast
= d
->needs_cast_p
? "(void *)" : "";
3329 wtd
= (const struct write_types_data
*) d
->cookie
;
3334 case TYPE_UNDEFINED
:
3337 oprintf (d
->of
, "%*s%s (%s%s", d
->indent
, "",
3338 wtd
->subfield_marker_routine
, cast
, d
->val
);
3339 if (wtd
->param_prefix
)
3341 if (f
->u
.p
->kind
== TYPE_SCALAR
)
3342 /* The current type is a pointer to a scalar (so not
3343 considered like a pointer to instances of user defined
3344 types) and we are seeing it; it means we must be even
3345 more careful about the second argument of the
3346 SUBFIELD_MARKER_ROUTINE call. That argument must
3347 always be the instance of the type for which
3348 write_func_for_structure was called - this really is
3349 what the function SUBFIELD_MARKER_ROUTINE expects.
3350 That is, it must be an instance of the ORIG_S type
3351 parameter of write_func_for_structure. The convention
3352 is that that argument must be "x" in that case (as set
3353 by write_func_for_structure). The problem is, we can't
3354 count on d->prev_val[3] to be always set to "x" in that
3355 case. Sometimes walk_type can set it to something else
3356 (to e.g cooperate with write_array when called from
3357 write_roots). So let's set it to "x" here then. */
3358 oprintf (d
->of
, ", x");
3360 oprintf (d
->of
, ", %s", d
->prev_val
[3]);
3363 oprintf (d
->of
, ", gt_%s_", wtd
->param_prefix
);
3364 output_mangled_typename (d
->of
, d
->orig_s
);
3367 oprintf (d
->of
, ", gt_%sa_%s", wtd
->param_prefix
, d
->prev_val
[0]);
3369 oprintf (d
->of
, ");\n");
3370 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
3371 oprintf (d
->of
, "%*s%s (%s%s, %s, %s);\n", d
->indent
, "",
3372 wtd
->reorder_note_routine
, cast
, d
->val
,
3373 d
->prev_val
[3], d
->reorder_fn
);
3379 case TYPE_LANG_STRUCT
:
3380 case TYPE_PARAM_STRUCT
:
3381 case TYPE_USER_STRUCT
:
3382 if (f
->kind
== TYPE_USER_STRUCT
&& !d
->in_ptr_field
)
3384 /* If F is a user-defined type and the field is not a
3385 pointer to the type, then we should not generate the
3386 standard pointer-marking code. All we need to do is call
3387 the user-provided marking function to process the fields
3389 oprintf (d
->of
, "%*sgt_%sx (&(%s));\n", d
->indent
, "", wtd
->prefix
,
3394 oprintf (d
->of
, "%*sgt_%s_", d
->indent
, "", wtd
->prefix
);
3395 output_mangled_typename (d
->of
, f
);
3396 oprintf (d
->of
, " (%s%s);\n", cast
, d
->val
);
3397 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
3398 oprintf (d
->of
, "%*s%s (%s%s, %s%s, %s);\n", d
->indent
, "",
3399 wtd
->reorder_note_routine
, cast
, d
->val
, cast
, d
->val
,
3412 /* Return an output file that is suitable for definitions which can
3413 reference struct S */
3416 get_output_file_for_structure (const_type_p s
, type_p
*param
)
3418 const input_file
*fn
;
3421 gcc_assert (union_or_struct_p (s
));
3422 fn
= s
->u
.s
.line
.file
;
3424 /* This is a hack, and not the good kind either. */
3425 for (i
= NUM_PARAM
- 1; i
>= 0; i
--)
3426 if (param
&& param
[i
] && param
[i
]->kind
== TYPE_POINTER
3427 && union_or_struct_p (param
[i
]->u
.p
))
3428 fn
= param
[i
]->u
.p
->u
.s
.line
.file
;
3430 /* The call to get_output_file_with_visibility may update fn by
3431 caching its result inside, so we need the CONST_CAST. */
3432 return get_output_file_with_visibility (CONST_CAST (input_file
*, fn
));
3436 /* Returns the specifier keyword for a string or union type S, empty string
3440 get_type_specifier (const type_p s
)
3442 if (s
->kind
== TYPE_STRUCT
)
3444 else if (s
->kind
== TYPE_LANG_STRUCT
)
3445 return get_type_specifier (s
->u
.s
.lang_struct
);
3446 else if (s
->kind
== TYPE_UNION
)
3452 /* Emits a declaration for type TY (assumed to be a union or a
3453 structure) on stream OUT. */
3456 write_type_decl (outf_p out
, type_p ty
)
3458 if (union_or_struct_p (ty
))
3459 oprintf (out
, "%s%s", get_type_specifier (ty
), ty
->u
.s
.tag
);
3460 else if (ty
->kind
== TYPE_SCALAR
)
3462 if (ty
->u
.scalar_is_char
)
3463 oprintf (out
, "const char");
3465 oprintf (out
, "void");
3467 else if (ty
->kind
== TYPE_POINTER
)
3469 write_type_decl (out
, ty
->u
.p
);
3470 oprintf (out
, " *");
3472 else if (ty
->kind
== TYPE_ARRAY
)
3474 write_type_decl (out
, ty
->u
.a
.p
);
3475 oprintf (out
, " *");
3477 else if (ty
->kind
== TYPE_STRING
)
3479 oprintf (out
, "const char *");
3486 /* Write on OF the name of the marker function for structure S. PREFIX
3487 is the prefix to use (to distinguish ggc from pch markers). */
3490 write_marker_function_name (outf_p of
, type_p s
, const char *prefix
)
3492 if (union_or_struct_p (s
))
3494 const char *id_for_tag
= filter_type_name (s
->u
.s
.tag
);
3495 oprintf (of
, "gt_%sx_%s", prefix
, id_for_tag
);
3496 if (id_for_tag
!= s
->u
.s
.tag
)
3497 free (CONST_CAST (char *, id_for_tag
));
3499 else if (s
->kind
== TYPE_PARAM_STRUCT
)
3501 oprintf (of
, "gt_%s_", prefix
);
3502 output_mangled_typename (of
, s
);
3508 /* Write on OF a user-callable routine to act as an entry point for
3509 the marking routine for S, generated by write_func_for_structure.
3510 PREFIX is the prefix to use to distinguish ggc and pch markers. */
3513 write_user_func_for_structure_ptr (outf_p of
, type_p s
, const char *prefix
)
3515 /* Parameterized structures are not supported in user markers. There
3516 is no way for the marker function to know which specific type
3517 to use to generate the call to the void * entry point. For
3518 instance, a marker for struct htab may need to call different
3519 routines to mark the fields, depending on the paramN_is attributes.
3521 A user-defined marker that accepts 'struct htab' as its argument
3522 would not know which variant to call. Generating several entry
3523 points accepting 'struct htab' would cause multiply-defined
3524 errors during compilation. */
3525 gcc_assert (union_or_struct_p (s
));
3527 type_p alias_of
= NULL
;
3528 for (options_p opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3529 if (strcmp (opt
->name
, "ptr_alias") == 0)
3531 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3532 we do not generate marking code for ORIG_S here. Instead, a
3533 forwarder #define in gtype-desc.h will cause every call to its
3534 marker to call the target of this alias.
3536 However, we still want to create a user entry code for the
3537 aliased type. So, if ALIAS_OF is set, we only generate the
3538 user-callable marker function. */
3539 alias_of
= opt
->info
.type
;
3543 oprintf (of
, "\nvoid\n");
3544 oprintf (of
, "gt_%sx (", prefix
);
3545 write_type_decl (of
, s
);
3546 oprintf (of
, " *& x)\n");
3547 oprintf (of
, "{\n");
3548 oprintf (of
, " if (x)\n ");
3549 write_marker_function_name (of
, alias_of
? alias_of
: s
, prefix
);
3550 oprintf (of
, " ((void *) x);\n");
3551 oprintf (of
, "}\n");
3555 /* Write a function to mark all the fields of type S on OF. PREFIX
3556 and D are as in write_user_marking_functions. */
3559 write_user_func_for_structure_body (type_p s
, const char *prefix
,
3560 struct walk_type_data
*d
)
3562 oprintf (d
->of
, "\nvoid\n");
3563 oprintf (d
->of
, "gt_%sx (", prefix
);
3564 write_type_decl (d
->of
, s
);
3565 oprintf (d
->of
, "& x_r ATTRIBUTE_UNUSED)\n");
3566 oprintf (d
->of
, "{\n");
3567 oprintf (d
->of
, " ");
3568 write_type_decl (d
->of
, s
);
3569 oprintf (d
->of
, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3573 oprintf (d
->of
, "}\n");
3577 /* Emit the user-callable functions needed to mark all the types used
3578 by the user structure S. PREFIX is the prefix to use to
3579 distinguish ggc and pch markers. D contains data needed to pass to
3580 walk_type when traversing the fields of a type.
3582 For every type T referenced by S, two routines are generated: one
3583 that takes 'T *', marks the pointer and calls the second routine,
3584 which just marks the fields of T. */
3587 write_user_marking_functions (type_p s
, const char *prefix
,
3588 struct walk_type_data
*d
)
3590 gcc_assert (s
->kind
== TYPE_USER_STRUCT
);
3592 for (pair_p fld
= s
->u
.s
.fields
; fld
; fld
= fld
->next
)
3594 type_p fld_type
= fld
->type
;
3595 if (fld_type
->kind
== TYPE_POINTER
)
3597 type_p pointed_to_type
= fld_type
->u
.p
;
3598 if (union_or_struct_p (pointed_to_type
))
3599 write_user_func_for_structure_ptr (d
->of
, pointed_to_type
, prefix
);
3601 else if (union_or_struct_p (fld_type
))
3602 write_user_func_for_structure_body (fld_type
, prefix
, d
);
3607 /* For S, a structure that's part of ORIG_S, and using parameters
3608 PARAM, write out a routine that:
3609 - Takes a parameter, a void * but actually of type *S
3610 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3611 field of S or its substructures and (in some cases) things
3612 that are pointed to by S. */
3615 write_func_for_structure (type_p orig_s
, type_p s
, type_p
*param
,
3616 const struct write_types_data
*wtd
)
3618 const char *chain_next
= NULL
;
3619 const char *chain_prev
= NULL
;
3620 const char *chain_circular
= NULL
;
3621 const char *mark_hook_name
= NULL
;
3623 struct walk_type_data d
;
3625 if (s
->u
.s
.base_class
)
3627 /* Verify that the base class has a "desc", since otherwise
3628 the traversal hooks there won't attempt to visit fields of
3629 subclasses such as this one. */
3630 const_type_p ubc
= get_ultimate_base_class (s
);
3631 if ((!opts_have (ubc
->u
.s
.opt
, "user")
3632 && !opts_have (ubc
->u
.s
.opt
, "desc")))
3633 error_at_line (&s
->u
.s
.line
,
3634 ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
3635 ", but '%s' lacks a discriminator 'desc' option"),
3636 s
->u
.s
.tag
, ubc
->u
.s
.tag
, ubc
->u
.s
.tag
);
3638 /* Don't write fns for subclasses, only for the ultimate base class
3639 within an inheritance hierarchy. */
3643 memset (&d
, 0, sizeof (d
));
3644 d
.of
= get_output_file_for_structure (s
, param
);
3645 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3646 if (strcmp (opt
->name
, "chain_next") == 0
3647 && opt
->kind
== OPTION_STRING
)
3648 chain_next
= opt
->info
.string
;
3649 else if (strcmp (opt
->name
, "chain_prev") == 0
3650 && opt
->kind
== OPTION_STRING
)
3651 chain_prev
= opt
->info
.string
;
3652 else if (strcmp (opt
->name
, "chain_circular") == 0
3653 && opt
->kind
== OPTION_STRING
)
3654 chain_circular
= opt
->info
.string
;
3655 else if (strcmp (opt
->name
, "mark_hook") == 0
3656 && opt
->kind
== OPTION_STRING
)
3657 mark_hook_name
= opt
->info
.string
;
3658 if (chain_prev
!= NULL
&& chain_next
== NULL
)
3659 error_at_line (&s
->u
.s
.line
, "chain_prev without chain_next");
3660 if (chain_circular
!= NULL
&& chain_next
!= NULL
)
3661 error_at_line (&s
->u
.s
.line
, "chain_circular with chain_next");
3662 if (chain_circular
!= NULL
)
3663 chain_next
= chain_circular
;
3665 d
.process_field
= write_types_process_field
;
3669 d
.line
= &s
->u
.s
.line
;
3670 d
.bitmap
= s
->u
.s
.bitmap
;
3672 d
.prev_val
[0] = "*x";
3673 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
3674 d
.prev_val
[3] = "x";
3676 d
.have_this_obj
= false;
3678 oprintf (d
.of
, "\n");
3679 oprintf (d
.of
, "void\n");
3680 write_marker_function_name (d
.of
, orig_s
, wtd
->prefix
);
3681 oprintf (d
.of
, " (void *x_p)\n");
3682 oprintf (d
.of
, "{\n ");
3683 write_type_decl (d
.of
, s
);
3684 oprintf (d
.of
, " * %sx = (", chain_next
== NULL
? "const " : "");
3685 write_type_decl (d
.of
, s
);
3686 oprintf (d
.of
, " *)x_p;\n");
3687 if (chain_next
!= NULL
)
3689 /* TYPE_USER_STRUCTs should not occur here. These structures
3690 are completely handled by user code. */
3691 gcc_assert (orig_s
->kind
!= TYPE_USER_STRUCT
);
3693 oprintf (d
.of
, " ");
3694 write_type_decl (d
.of
, s
);
3695 oprintf (d
.of
, " * xlimit = x;\n");
3697 if (chain_next
== NULL
)
3699 oprintf (d
.of
, " if (%s (x", wtd
->marker_routine
);
3700 if (wtd
->param_prefix
)
3702 oprintf (d
.of
, ", x, gt_%s_", wtd
->param_prefix
);
3703 output_mangled_typename (d
.of
, orig_s
);
3705 oprintf (d
.of
, "))\n");
3709 if (chain_circular
!= NULL
)
3710 oprintf (d
.of
, " if (!%s (xlimit", wtd
->marker_routine
);
3712 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
3713 if (wtd
->param_prefix
)
3715 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
3716 output_mangled_typename (d
.of
, orig_s
);
3718 oprintf (d
.of
, "))\n");
3719 if (chain_circular
!= NULL
)
3720 oprintf (d
.of
, " return;\n do\n");
3721 if (mark_hook_name
&& !wtd
->skip_hooks
)
3723 oprintf (d
.of
, " {\n");
3724 oprintf (d
.of
, " %s (xlimit);\n ", mark_hook_name
);
3726 oprintf (d
.of
, " xlimit = (");
3727 d
.prev_val
[2] = "*xlimit";
3728 output_escaped_param (&d
, chain_next
, "chain_next");
3729 oprintf (d
.of
, ");\n");
3730 if (mark_hook_name
&& !wtd
->skip_hooks
)
3731 oprintf (d
.of
, " }\n");
3732 if (chain_prev
!= NULL
)
3734 oprintf (d
.of
, " if (x != xlimit)\n");
3735 oprintf (d
.of
, " for (;;)\n");
3736 oprintf (d
.of
, " {\n");
3737 oprintf (d
.of
, " %s %s * const xprev = (",
3738 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
3740 d
.prev_val
[2] = "*x";
3741 output_escaped_param (&d
, chain_prev
, "chain_prev");
3742 oprintf (d
.of
, ");\n");
3743 oprintf (d
.of
, " if (xprev == NULL) break;\n");
3744 oprintf (d
.of
, " x = xprev;\n");
3745 oprintf (d
.of
, " (void) %s (xprev", wtd
->marker_routine
);
3746 if (wtd
->param_prefix
)
3748 oprintf (d
.of
, ", xprev, gt_%s_", wtd
->param_prefix
);
3749 output_mangled_typename (d
.of
, orig_s
);
3751 oprintf (d
.of
, ");\n");
3752 oprintf (d
.of
, " }\n");
3754 if (chain_circular
!= NULL
)
3756 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
3757 if (wtd
->param_prefix
)
3759 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
3760 output_mangled_typename (d
.of
, orig_s
);
3762 oprintf (d
.of
, "));\n");
3763 if (mark_hook_name
&& !wtd
->skip_hooks
)
3764 oprintf (d
.of
, " %s (xlimit);\n", mark_hook_name
);
3765 oprintf (d
.of
, " do\n");
3768 oprintf (d
.of
, " while (x != xlimit)\n");
3770 oprintf (d
.of
, " {\n");
3771 if (mark_hook_name
&& chain_next
== NULL
&& !wtd
->skip_hooks
)
3773 oprintf (d
.of
, " %s (x);\n", mark_hook_name
);
3776 d
.prev_val
[2] = "*x";
3778 if (orig_s
->kind
!= TYPE_USER_STRUCT
)
3782 /* User structures have no fields to walk. Simply generate a call
3783 to the user-provided structure marker. */
3784 oprintf (d
.of
, "%*sgt_%sx (x);\n", d
.indent
, "", wtd
->prefix
);
3787 if (chain_next
!= NULL
)
3789 oprintf (d
.of
, " x = (");
3790 output_escaped_param (&d
, chain_next
, "chain_next");
3791 oprintf (d
.of
, ");\n");
3794 oprintf (d
.of
, " }\n");
3795 if (chain_circular
!= NULL
)
3796 oprintf (d
.of
, " while (x != xlimit);\n");
3797 oprintf (d
.of
, "}\n");
3799 if (orig_s
->kind
== TYPE_USER_STRUCT
)
3800 write_user_marking_functions (orig_s
, wtd
->prefix
, &d
);
3804 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3807 write_types (outf_p output_header
, type_p structures
, type_p param_structs
,
3808 const struct write_types_data
*wtd
)
3810 int nbfun
= 0; /* Count the emitted functions. */
3813 oprintf (output_header
, "\n/* %s*/\n", wtd
->comment
);
3815 /* We first emit the macros and the declarations. Functions' code is
3816 emitted afterwards. This is needed in plugin mode. */
3817 oprintf (output_header
, "/* Macros and declarations. */\n");
3818 for (s
= structures
; s
; s
= s
->next
)
3819 /* Do not emit handlers for derived classes; we only ever deal with
3820 the ultimate base class within an inheritance hierarchy. */
3821 if ((s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
3822 && !s
->u
.s
.base_class
)
3826 if (s
->gc_used
== GC_MAYBE_POINTED_TO
&& s
->u
.s
.line
.file
== NULL
)
3829 const char *s_id_for_tag
= filter_type_name (s
->u
.s
.tag
);
3831 oprintf (output_header
, "#define gt_%s_", wtd
->prefix
);
3832 output_mangled_typename (output_header
, s
);
3833 oprintf (output_header
, "(X) do { \\\n");
3834 oprintf (output_header
,
3835 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd
->prefix
,
3837 oprintf (output_header
, " } while (0)\n");
3839 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3840 if (strcmp (opt
->name
, "ptr_alias") == 0
3841 && opt
->kind
== OPTION_TYPE
)
3843 const_type_p
const t
= (const_type_p
) opt
->info
.type
;
3844 if (t
->kind
== TYPE_STRUCT
3845 || t
->kind
== TYPE_UNION
|| t
->kind
== TYPE_LANG_STRUCT
)
3847 const char *t_id_for_tag
= filter_type_name (t
->u
.s
.tag
);
3848 oprintf (output_header
,
3849 "#define gt_%sx_%s gt_%sx_%s\n",
3850 wtd
->prefix
, s
->u
.s
.tag
, wtd
->prefix
, t_id_for_tag
);
3851 if (t_id_for_tag
!= t
->u
.s
.tag
)
3852 free (CONST_CAST (char *, t_id_for_tag
));
3855 error_at_line (&s
->u
.s
.line
,
3856 "structure alias is not a structure");
3862 /* Declare the marker procedure only once. */
3863 oprintf (output_header
,
3864 "extern void gt_%sx_%s (void *);\n",
3865 wtd
->prefix
, s_id_for_tag
);
3867 if (s_id_for_tag
!= s
->u
.s
.tag
)
3868 free (CONST_CAST (char *, s_id_for_tag
));
3870 if (s
->u
.s
.line
.file
== NULL
)
3872 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
3878 for (s
= param_structs
; s
; s
= s
->next
)
3879 if (s
->gc_used
== GC_POINTED_TO
)
3881 type_p stru
= s
->u
.param_struct
.stru
;
3883 /* Declare the marker procedure. */
3884 oprintf (output_header
, "extern void gt_%s_", wtd
->prefix
);
3885 output_mangled_typename (output_header
, s
);
3886 oprintf (output_header
, " (void *);\n");
3888 if (stru
->u
.s
.line
.file
== NULL
)
3890 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
3896 /* At last we emit the functions code. */
3897 oprintf (output_header
, "\n/* functions code */\n");
3898 for (s
= structures
; s
; s
= s
->next
)
3899 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
3903 if (s
->gc_used
== GC_MAYBE_POINTED_TO
&& s
->u
.s
.line
.file
== NULL
)
3905 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3906 if (strcmp (opt
->name
, "ptr_alias") == 0)
3911 if (s
->kind
== TYPE_LANG_STRUCT
)
3914 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
3917 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3918 nbfun
, (void*) ss
, ss
->u
.s
.tag
);
3919 write_func_for_structure (s
, ss
, NULL
, wtd
);
3925 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3926 nbfun
, (void*) s
, s
->u
.s
.tag
);
3927 write_func_for_structure (s
, s
, NULL
, wtd
);
3932 /* Structure s is not possibly pointed to, so can be ignored. */
3933 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3934 (void*)s
, s
->u
.s
.tag
,
3938 for (s
= param_structs
; s
; s
= s
->next
)
3939 if (s
->gc_used
== GC_POINTED_TO
)
3941 type_p
*param
= s
->u
.param_struct
.param
;
3942 type_p stru
= s
->u
.param_struct
.stru
;
3943 if (stru
->u
.s
.line
.file
== NULL
)
3945 if (stru
->kind
== TYPE_LANG_STRUCT
)
3948 for (ss
= stru
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
3951 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
3952 nbfun
, (void*) ss
, ss
->u
.s
.tag
);
3953 write_func_for_structure (s
, ss
, param
, wtd
);
3959 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
3961 (void*) stru
, stru
->u
.s
.tag
);
3962 write_func_for_structure (s
, stru
, param
, wtd
);
3967 /* Param structure s is not pointed to, so should be ignored. */
3968 DBGPRINTF ("ignored s @ %p", (void*)s
);
3970 if (verbosity_level
>= 2)
3971 printf ("%s emitted %d routines for %s\n",
3972 progname
, nbfun
, wtd
->comment
);
3975 static const struct write_types_data ggc_wtd
= {
3976 "ggc_m", NULL
, "ggc_mark", "ggc_test_and_set_mark", NULL
,
3977 "GC marker procedures. ",
3981 static const struct write_types_data pch_wtd
= {
3982 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3983 "gt_pch_note_reorder",
3984 "PCH type-walking procedures. ",
3988 /* Write out the local pointer-walking routines. */
3990 /* process_field routine for local pointer-walking for user-callable
3991 routines. The difference between this and
3992 write_types_local_process_field is that, in this case, we do not
3993 need to check whether the given pointer matches the address of the
3994 parent structure. This check was already generated by the call
3995 to gt_pch_nx in the main gt_pch_p_*() function that is calling
3999 write_types_local_user_process_field (type_p f
, const struct walk_type_data
*d
)
4006 case TYPE_LANG_STRUCT
:
4007 case TYPE_PARAM_STRUCT
:
4009 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
4012 case TYPE_USER_STRUCT
:
4013 if (d
->in_ptr_field
)
4014 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
4016 oprintf (d
->of
, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4017 d
->indent
, "", d
->val
);
4025 case TYPE_UNDEFINED
:
4031 /* Write a function to PCH walk all the fields of type S on OF.
4032 D contains data needed by walk_type to recurse into the fields of S. */
4035 write_pch_user_walking_for_structure_body (type_p s
, struct walk_type_data
*d
)
4037 oprintf (d
->of
, "\nvoid\n");
4038 oprintf (d
->of
, "gt_pch_nx (");
4039 write_type_decl (d
->of
, s
);
4040 oprintf (d
->of
, "* x ATTRIBUTE_UNUSED,\n"
4041 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4042 "\tATTRIBUTE_UNUSED void *cookie)\n");
4043 oprintf (d
->of
, "{\n");
4046 d
->process_field
= write_types_local_user_process_field
;
4048 oprintf (d
->of
, "}\n");
4052 /* Emit the user-callable functions needed to mark all the types used
4053 by the user structure S. PREFIX is the prefix to use to
4054 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
4055 chain_next option defined. D contains data needed to pass to
4056 walk_type when traversing the fields of a type.
4058 For every type T referenced by S, two routines are generated: one
4059 that takes 'T *', marks the pointer and calls the second routine,
4060 which just marks the fields of T. */
4063 write_pch_user_walking_functions (type_p s
, struct walk_type_data
*d
)
4065 gcc_assert (s
->kind
== TYPE_USER_STRUCT
);
4067 for (pair_p fld
= s
->u
.s
.fields
; fld
; fld
= fld
->next
)
4069 type_p fld_type
= fld
->type
;
4070 if (union_or_struct_p (fld_type
))
4071 write_pch_user_walking_for_structure_body (fld_type
, d
);
4076 /* process_field routine for local pointer-walking. */
4079 write_types_local_process_field (type_p f
, const struct walk_type_data
*d
)
4081 gcc_assert (d
->have_this_obj
);
4087 case TYPE_LANG_STRUCT
:
4088 case TYPE_PARAM_STRUCT
:
4090 oprintf (d
->of
, "%*sif ((void *)(%s) == this_obj)\n", d
->indent
, "",
4092 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
4095 case TYPE_USER_STRUCT
:
4096 oprintf (d
->of
, "%*sif ((void *)(%s) == this_obj)\n", d
->indent
, "",
4098 if (d
->in_ptr_field
)
4099 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
4101 oprintf (d
->of
, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4102 d
->indent
, "", d
->val
);
4110 case TYPE_UNDEFINED
:
4116 /* For S, a structure that's part of ORIG_S, and using parameters
4117 PARAM, write out a routine that:
4118 - Is of type gt_note_pointers
4119 - Calls PROCESS_FIELD on each field of S or its substructures.
4123 write_local_func_for_structure (const_type_p orig_s
, type_p s
, type_p
*param
)
4125 struct walk_type_data d
;
4127 /* Don't write fns for subclasses, only for the ultimate base class
4128 within an inheritance hierarchy. */
4129 if (s
->u
.s
.base_class
)
4132 memset (&d
, 0, sizeof (d
));
4133 d
.of
= get_output_file_for_structure (s
, param
);
4134 d
.process_field
= write_types_local_process_field
;
4136 d
.line
= &s
->u
.s
.line
;
4137 d
.bitmap
= s
->u
.s
.bitmap
;
4139 d
.prev_val
[0] = d
.prev_val
[2] = "*x";
4140 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
4141 d
.prev_val
[3] = "x";
4143 d
.fn_wants_lvalue
= true;
4145 oprintf (d
.of
, "\n");
4146 oprintf (d
.of
, "void\n");
4147 oprintf (d
.of
, "gt_pch_p_");
4148 output_mangled_typename (d
.of
, orig_s
);
4149 oprintf (d
.of
, " (ATTRIBUTE_UNUSED void *this_obj,\n"
4151 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4152 "\tATTRIBUTE_UNUSED void *cookie)\n");
4153 oprintf (d
.of
, "{\n");
4154 oprintf (d
.of
, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
4155 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
,
4156 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
4158 d
.have_this_obj
= true;
4160 if (s
->kind
!= TYPE_USER_STRUCT
)
4164 /* User structures have no fields to walk. Simply generate a
4165 call to the user-provided PCH walker. */
4166 oprintf (d
.of
, "%*sif ((void *)(%s) == this_obj)\n", d
.indent
, "",
4168 oprintf (d
.of
, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4169 d
.indent
, "", d
.val
);
4172 oprintf (d
.of
, "}\n");
4174 /* Write user-callable entry points for the PCH walking routines. */
4175 if (orig_s
->kind
== TYPE_USER_STRUCT
)
4176 write_pch_user_walking_functions (s
, &d
);
4179 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
4182 write_local (outf_p output_header
, type_p structures
, type_p param_structs
)
4189 oprintf (output_header
, "\n/* Local pointer-walking routines. */\n");
4190 for (s
= structures
; s
; s
= s
->next
)
4191 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
4195 if (s
->u
.s
.line
.file
== NULL
)
4197 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
4198 if (strcmp (opt
->name
, "ptr_alias") == 0
4199 && opt
->kind
== OPTION_TYPE
)
4201 const_type_p
const t
= (const_type_p
) opt
->info
.type
;
4202 if (t
->kind
== TYPE_STRUCT
4203 || t
->kind
== TYPE_UNION
|| t
->kind
== TYPE_LANG_STRUCT
)
4205 oprintf (output_header
, "#define gt_pch_p_");
4206 output_mangled_typename (output_header
, s
);
4207 oprintf (output_header
, " gt_pch_p_");
4208 output_mangled_typename (output_header
, t
);
4209 oprintf (output_header
, "\n");
4212 error_at_line (&s
->u
.s
.line
,
4213 "structure alias is not a structure");
4219 /* Declare the marker procedure only once. */
4220 oprintf (output_header
, "extern void gt_pch_p_");
4221 output_mangled_typename (output_header
, s
);
4222 oprintf (output_header
,
4223 "\n (void *, void *, gt_pointer_operator, void *);\n");
4225 if (s
->kind
== TYPE_LANG_STRUCT
)
4228 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
4229 write_local_func_for_structure (s
, ss
, NULL
);
4232 write_local_func_for_structure (s
, s
, NULL
);
4235 for (s
= param_structs
; s
; s
= s
->next
)
4236 if (s
->gc_used
== GC_POINTED_TO
)
4238 type_p
*param
= s
->u
.param_struct
.param
;
4239 type_p stru
= s
->u
.param_struct
.stru
;
4241 /* Declare the marker procedure. */
4242 oprintf (output_header
, "extern void gt_pch_p_");
4243 output_mangled_typename (output_header
, s
);
4244 oprintf (output_header
,
4245 "\n (void *, void *, gt_pointer_operator, void *);\n");
4247 if (stru
->u
.s
.line
.file
== NULL
)
4249 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
4254 if (stru
->kind
== TYPE_LANG_STRUCT
)
4257 for (ss
= stru
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
4258 write_local_func_for_structure (s
, ss
, param
);
4261 write_local_func_for_structure (s
, stru
, param
);
4265 /* Nonzero if S is a type for which typed GC allocators should be output. */
4267 #define USED_BY_TYPED_GC_P(s) \
4268 ((s->kind == TYPE_POINTER \
4269 && (s->u.p->gc_used == GC_POINTED_TO \
4270 || s->u.p->gc_used == GC_USED)) \
4271 || (union_or_struct_p (s) \
4272 && ((s)->gc_used == GC_POINTED_TO \
4273 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4274 && s->u.s.line.file != NULL) \
4275 || ((s)->gc_used == GC_USED \
4276 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))) \
4277 || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
4281 /* Might T contain any non-pointer elements? */
4284 contains_scalar_p (type_p t
)
4292 return contains_scalar_p (t
->u
.a
.p
);
4293 case TYPE_USER_STRUCT
:
4294 /* User-marked structures will typically contain pointers. */
4297 /* Could also check for structures that have no non-pointer
4298 fields, but there aren't enough of those to worry about. */
4303 /* Mangle INPF and print it to F. */
4306 put_mangled_filename (outf_p f
, const input_file
*inpf
)
4308 /* The call to get_output_file_name may indirectly update fn since
4309 get_output_file_with_visibility caches its result inside, so we
4310 need the CONST_CAST. */
4311 const char *name
= get_output_file_name (CONST_CAST (input_file
*, inpf
));
4314 for (; *name
!= 0; name
++)
4315 if (ISALNUM (*name
))
4316 oprintf (f
, "%c", *name
);
4318 oprintf (f
, "%c", '_');
4321 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4322 LASTNAME, and NAME are all strings to insert in various places in
4323 the resulting code. */
4326 finish_root_table (struct flist
*flp
, const char *pfx
, const char *lastname
,
4327 const char *tname
, const char *name
)
4331 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
4332 if (fli2
->started_p
)
4334 oprintf (fli2
->f
, " %s\n", lastname
);
4335 oprintf (fli2
->f
, "};\n\n");
4338 for (fli2
= flp
; fli2
&& base_files
; fli2
= fli2
->next
)
4339 if (fli2
->started_p
)
4341 lang_bitmap bitmap
= get_lang_bitmap (fli2
->file
);
4344 for (fnum
= 0; bitmap
!= 0; fnum
++, bitmap
>>= 1)
4347 oprintf (base_files
[fnum
],
4348 "extern const struct %s gt_%s_", tname
, pfx
);
4349 put_mangled_filename (base_files
[fnum
], fli2
->file
);
4350 oprintf (base_files
[fnum
], "[];\n");
4356 for (fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
4357 oprintf (base_files
[fnum
],
4358 "EXPORTED_CONST struct %s * const %s[] = {\n", tname
, name
);
4362 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
4363 if (fli2
->started_p
)
4365 lang_bitmap bitmap
= get_lang_bitmap (fli2
->file
);
4368 fli2
->started_p
= 0;
4370 for (fnum
= 0; base_files
&& bitmap
!= 0; fnum
++, bitmap
>>= 1)
4373 oprintf (base_files
[fnum
], " gt_%s_", pfx
);
4374 put_mangled_filename (base_files
[fnum
], fli2
->file
);
4375 oprintf (base_files
[fnum
], ",\n");
4381 for (fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
4383 oprintf (base_files
[fnum
], " NULL\n");
4384 oprintf (base_files
[fnum
], "};\n");
4389 /* Write the first three fields (pointer, count and stride) for
4390 root NAME to F. V and LINE are as for write_root.
4392 Return true if the entry could be written; return false on error. */
4395 start_root_entry (outf_p f
, pair_p v
, const char *name
, struct fileloc
*line
)
4401 error_at_line (line
, "`%s' is too complex to be a root", name
);
4405 oprintf (f
, " {\n");
4406 oprintf (f
, " &%s,\n", name
);
4409 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
4411 oprintf (f
, " * (%s)", ap
->u
.a
.len
);
4412 else if (ap
== v
->type
)
4413 oprintf (f
, " * ARRAY_SIZE (%s)", v
->name
);
4415 oprintf (f
, " sizeof (%s", v
->name
);
4416 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
4418 oprintf (f
, "),\n");
4422 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4423 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4427 write_field_root (outf_p f
, pair_p v
, type_p type
, const char *name
,
4428 int has_length
, struct fileloc
*line
, const char *if_marked
,
4429 bool emit_pch
, type_p field_type
, const char *field_name
)
4432 /* If the field reference is relative to V, rather than to some
4433 subcomponent of V, we can mark any subarrays with a single stride.
4434 We're effectively treating the field as a global variable in its
4436 if (v
&& type
== v
->type
)
4439 newv
.type
= field_type
;
4440 newv
.name
= ACONCAT ((v
->name
, ".", field_name
, NULL
));
4443 /* Otherwise, any arrays nested in the structure are too complex to
4445 else if (field_type
->kind
== TYPE_ARRAY
)
4447 write_root (f
, v
, field_type
, ACONCAT ((name
, ".", field_name
, NULL
)),
4448 has_length
, line
, if_marked
, emit_pch
);
4451 /* Write out to F the table entry and any marker routines needed to
4452 mark NAME as TYPE. V can be one of three values:
4454 - null, if NAME is too complex to represent using a single
4455 count and stride. In this case, it is an error for NAME to
4456 contain any gc-ed data.
4458 - the outermost array that contains NAME, if NAME is part of an array.
4460 - the C variable that contains NAME, if NAME is not part of an array.
4462 LINE is the line of the C source that declares the root variable.
4463 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
4464 is nonzero iff we are building the root table for hash table caches. */
4467 write_root (outf_p f
, pair_p v
, type_p type
, const char *name
, int has_length
,
4468 struct fileloc
*line
, const char *if_marked
, bool emit_pch
)
4475 for (fld
= type
->u
.s
.fields
; fld
; fld
= fld
->next
)
4478 const char *desc
= NULL
;
4481 for (o
= fld
->opt
; o
; o
= o
->next
)
4482 if (strcmp (o
->name
, "skip") == 0)
4484 else if (strcmp (o
->name
, "desc") == 0
4485 && o
->kind
== OPTION_STRING
)
4486 desc
= o
->info
.string
;
4487 else if (strcmp (o
->name
, "param_is") == 0)
4490 error_at_line (line
,
4491 "field `%s' of global `%s' has unknown option `%s'",
4492 fld
->name
, name
, o
->name
);
4496 else if (desc
&& fld
->type
->kind
== TYPE_UNION
)
4498 pair_p validf
= NULL
;
4501 for (ufld
= fld
->type
->u
.s
.fields
; ufld
; ufld
= ufld
->next
)
4503 const char *tag
= NULL
;
4505 for (oo
= ufld
->opt
; oo
; oo
= oo
->next
)
4506 if (strcmp (oo
->name
, "tag") == 0
4507 && oo
->kind
== OPTION_STRING
)
4508 tag
= oo
->info
.string
;
4509 if (tag
== NULL
|| strcmp (tag
, desc
) != 0)
4512 error_at_line (line
,
4513 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4514 name
, fld
->name
, validf
->name
,
4515 name
, fld
->name
, ufld
->name
, tag
);
4519 write_field_root (f
, v
, type
, name
, 0, line
, if_marked
,
4520 emit_pch
, validf
->type
,
4521 ACONCAT ((fld
->name
, ".",
4522 validf
->name
, NULL
)));
4525 error_at_line (line
,
4526 "global `%s.%s' has `desc' option but is not union",
4529 write_field_root (f
, v
, type
, name
, 0, line
, if_marked
,
4530 emit_pch
, fld
->type
, fld
->name
);
4538 newname
= xasprintf ("%s[0]", name
);
4539 write_root (f
, v
, type
->u
.a
.p
, newname
, has_length
, line
, if_marked
,
4545 case TYPE_USER_STRUCT
:
4546 error_at_line (line
, "`%s' must be a pointer type, because it is "
4547 "a GC root and its type is marked with GTY((user))",
4555 if (!start_root_entry (f
, v
, name
, line
))
4560 if (!has_length
&& union_or_struct_p (tp
))
4562 tp
= get_ultimate_base_class (tp
);
4563 const char *id_for_tag
= filter_type_name (tp
->u
.s
.tag
);
4564 oprintf (f
, " >_ggc_mx_%s,\n", id_for_tag
);
4566 oprintf (f
, " >_pch_nx_%s", id_for_tag
);
4568 oprintf (f
, " NULL");
4569 if (id_for_tag
!= tp
->u
.s
.tag
)
4570 free (CONST_CAST (char *, id_for_tag
));
4572 else if (!has_length
&& tp
->kind
== TYPE_PARAM_STRUCT
)
4574 oprintf (f
, " >_ggc_m_");
4575 output_mangled_typename (f
, tp
);
4578 oprintf (f
, ",\n >_pch_n_");
4579 output_mangled_typename (f
, tp
);
4582 oprintf (f
, ",\n NULL");
4585 && (tp
->kind
== TYPE_POINTER
|| union_or_struct_p (tp
)))
4587 oprintf (f
, " >_ggc_ma_%s,\n", name
);
4589 oprintf (f
, " >_pch_na_%s", name
);
4591 oprintf (f
, " NULL");
4595 error_at_line (line
,
4596 "global `%s' is pointer to unimplemented type",
4600 oprintf (f
, ",\n &%s", if_marked
);
4601 oprintf (f
, "\n },\n");
4607 if (!start_root_entry (f
, v
, name
, line
))
4610 oprintf (f
, " (gt_pointer_walker) >_ggc_m_S,\n");
4611 oprintf (f
, " (gt_pointer_walker) >_pch_n_S\n");
4612 oprintf (f
, " },\n");
4620 case TYPE_UNDEFINED
:
4622 case TYPE_LANG_STRUCT
:
4623 case TYPE_PARAM_STRUCT
:
4624 error_at_line (line
, "global `%s' is unimplemented type", name
);
4628 /* This generates a routine to walk an array. */
4631 write_array (outf_p f
, pair_p v
, const struct write_types_data
*wtd
)
4633 struct walk_type_data d
;
4636 memset (&d
, 0, sizeof (d
));
4642 d
.bitmap
= get_lang_bitmap (v
->line
.file
);
4645 d
.prev_val
[3] = prevval3
= xasprintf ("&%s", v
->name
);
4647 if (wtd
->param_prefix
)
4649 oprintf (f
, "static void gt_%sa_%s\n", wtd
->param_prefix
, v
->name
);
4650 oprintf (f
, " (void *, void *, gt_pointer_operator, void *);\n");
4651 oprintf (f
, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4652 wtd
->param_prefix
, v
->name
);
4654 " ATTRIBUTE_UNUSED void *x_p,\n"
4655 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4656 " ATTRIBUTE_UNUSED void * cookie)\n");
4657 oprintf (d
.of
, "{\n");
4658 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
4659 d
.process_field
= write_types_local_process_field
;
4660 d
.have_this_obj
= true;
4661 walk_type (v
->type
, &d
);
4662 oprintf (f
, "}\n\n");
4666 oprintf (f
, "static void gt_%sa_%s (void *);\n", wtd
->prefix
, v
->name
);
4667 oprintf (f
, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4668 wtd
->prefix
, v
->name
);
4670 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
4671 d
.process_field
= write_types_process_field
;
4672 d
.have_this_obj
= false;
4673 walk_type (v
->type
, &d
);
4675 oprintf (f
, "}\n\n");
4678 /* Output a table describing the locations and types of VARIABLES. */
4681 write_roots (pair_p variables
, bool emit_pch
)
4684 struct flist
*flp
= NULL
;
4686 for (v
= variables
; v
; v
= v
->next
)
4689 get_output_file_with_visibility (CONST_CAST (input_file
*,
4692 const char *length
= NULL
;
4693 int deletable_p
= 0;
4695 for (o
= v
->opt
; o
; o
= o
->next
)
4696 if (strcmp (o
->name
, "length") == 0
4697 && o
->kind
== OPTION_STRING
)
4698 length
= o
->info
.string
;
4699 else if (strcmp (o
->name
, "deletable") == 0)
4701 else if (strcmp (o
->name
, "param_is") == 0)
4703 else if (strncmp (o
->name
, "param", 5) == 0
4704 && ISDIGIT (o
->name
[5]) && strcmp (o
->name
+ 6, "_is") == 0)
4706 else if (strcmp (o
->name
, "if_marked") == 0)
4709 error_at_line (&v
->line
,
4710 "global `%s' has unknown option `%s'",
4713 for (fli
= flp
; fli
; fli
= fli
->next
)
4714 if (fli
->f
== f
&& f
)
4718 fli
= XNEW (struct flist
);
4722 fli
->file
= v
->line
.file
;
4723 gcc_assert (fli
->file
);
4726 oprintf (f
, "\n/* GC roots. */\n\n");
4731 && v
->type
->kind
== TYPE_POINTER
4732 && (v
->type
->u
.p
->kind
== TYPE_POINTER
4733 || v
->type
->u
.p
->kind
== TYPE_STRUCT
))
4735 write_array (f
, v
, &ggc_wtd
);
4736 write_array (f
, v
, &pch_wtd
);
4740 for (v
= variables
; v
; v
= v
->next
)
4742 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4749 for (o
= v
->opt
; o
; o
= o
->next
)
4750 if (strcmp (o
->name
, "length") == 0)
4752 else if (strcmp (o
->name
, "deletable") == 0
4753 || strcmp (o
->name
, "if_marked") == 0)
4759 for (fli
= flp
; fli
; fli
= fli
->next
)
4762 if (!fli
->started_p
)
4766 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4767 put_mangled_filename (f
, v
->line
.file
);
4768 oprintf (f
, "[] = {\n");
4771 write_root (f
, v
, v
->type
, v
->name
, length_p
, &v
->line
, NULL
, emit_pch
);
4774 finish_root_table (flp
, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4777 for (v
= variables
; v
; v
= v
->next
)
4779 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4785 for (o
= v
->opt
; o
; o
= o
->next
)
4786 if (strcmp (o
->name
, "deletable") == 0)
4788 else if (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_rd_");
4802 put_mangled_filename (f
, v
->line
.file
);
4803 oprintf (f
, "[] = {\n");
4806 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4810 finish_root_table (flp
, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4811 "gt_ggc_deletable_rtab");
4813 for (v
= variables
; v
; v
= v
->next
)
4815 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4818 const char *if_marked
= NULL
;
4822 for (o
= v
->opt
; o
; o
= o
->next
)
4823 if (strcmp (o
->name
, "length") == 0)
4825 else if (strcmp (o
->name
, "if_marked") == 0
4826 && o
->kind
== OPTION_STRING
)
4827 if_marked
= o
->info
.string
;
4828 if (if_marked
== NULL
)
4830 if (v
->type
->kind
!= TYPE_POINTER
4831 || v
->type
->u
.p
->kind
!= TYPE_PARAM_STRUCT
4832 || v
->type
->u
.p
->u
.param_struct
.stru
!= find_structure ("htab",
4835 error_at_line (&v
->line
,
4836 "if_marked option used but not hash table");
4840 for (fli
= flp
; fli
; fli
= fli
->next
)
4843 if (!fli
->started_p
)
4847 oprintf (f
, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4848 put_mangled_filename (f
, v
->line
.file
);
4849 oprintf (f
, "[] = {\n");
4852 write_root (f
, v
, v
->type
->u
.p
->u
.param_struct
.param
[0],
4853 v
->name
, length_p
, &v
->line
, if_marked
, emit_pch
);
4856 finish_root_table (flp
, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4857 "gt_ggc_cache_rtab");
4862 for (v
= variables
; v
; v
= v
->next
)
4864 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4868 int if_marked_p
= 0;
4871 for (o
= v
->opt
; o
; o
= o
->next
)
4872 if (strcmp (o
->name
, "length") == 0)
4874 else if (strcmp (o
->name
, "if_marked") == 0)
4880 for (fli
= flp
; fli
; fli
= fli
->next
)
4883 if (!fli
->started_p
)
4887 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4888 put_mangled_filename (f
, v
->line
.file
);
4889 oprintf (f
, "[] = {\n");
4892 write_root (f
, v
, v
->type
, v
->name
, length_p
, &v
->line
, NULL
, emit_pch
);
4895 finish_root_table (flp
, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4896 "gt_pch_cache_rtab");
4898 for (v
= variables
; v
; v
= v
->next
)
4900 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4906 for (o
= v
->opt
; o
; o
= o
->next
)
4907 if (strcmp (o
->name
, "deletable") == 0
4908 || strcmp (o
->name
, "if_marked") == 0)
4917 if (!contains_scalar_p (v
->type
))
4920 for (fli
= flp
; fli
; fli
= fli
->next
)
4923 if (!fli
->started_p
)
4927 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4928 put_mangled_filename (f
, v
->line
.file
);
4929 oprintf (f
, "[] = {\n");
4932 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4936 finish_root_table (flp
, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4937 "gt_pch_scalar_rtab");
4940 /* TRUE if type S has the GTY variable_size annotation. */
4943 variable_size_p (const type_p s
)
4946 for (o
= s
->u
.s
.opt
; o
; o
= o
->next
)
4947 if (strcmp (o
->name
, "variable_size") == 0)
4955 /* Writes one typed allocator definition into output F for type
4956 identifier TYPE_NAME with optional type specifier TYPE_SPECIFIER.
4957 The allocator name will contain ALLOCATOR_TYPE. If VARIABLE_SIZE
4958 is true, the allocator will have an extra parameter specifying
4959 number of bytes to allocate. If QUANTITY is set to VECTOR, a
4960 vector allocator will be output. */
4963 write_typed_alloc_def (outf_p f
,
4964 bool variable_size
, const char *type_specifier
,
4965 const char *type_name
, const char *allocator_type
,
4966 enum alloc_quantity quantity
)
4968 bool two_args
= variable_size
&& (quantity
== vector
);
4969 gcc_assert (f
!= NULL
);
4970 const char *type_name_as_id
= filter_type_name (type_name
);
4971 oprintf (f
, "#define ggc_alloc_%s%s", allocator_type
, type_name_as_id
);
4972 oprintf (f
, "(%s%s%s) ",
4973 (variable_size
? "SIZE" : ""),
4974 (two_args
? ", " : ""),
4975 (quantity
== vector
) ? "n" : "");
4976 oprintf (f
, "((%s%s *)", type_specifier
, type_name
);
4977 oprintf (f
, "(ggc_internal_%salloc_stat (", allocator_type
);
4979 oprintf (f
, "SIZE");
4981 oprintf (f
, "sizeof (%s%s)", type_specifier
, type_name
);
4982 if (quantity
== vector
)
4984 oprintf (f
, " MEM_STAT_INFO)))\n");
4985 if (type_name_as_id
!= type_name
)
4986 free (CONST_CAST (char *, type_name_as_id
));
4989 /* Writes a typed allocator definition into output F for a struct or
4990 union S, with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4993 write_typed_struct_alloc_def (outf_p f
,
4994 const type_p s
, const char *allocator_type
,
4995 enum alloc_quantity quantity
)
4997 gcc_assert (union_or_struct_p (s
));
4998 write_typed_alloc_def (f
, variable_size_p (s
), get_type_specifier (s
),
4999 s
->u
.s
.tag
, allocator_type
, quantity
);
5002 /* Writes a typed allocator definition into output F for a typedef P,
5003 with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
5006 write_typed_typedef_alloc_def (outf_p f
,
5007 const pair_p p
, const char *allocator_type
,
5008 enum alloc_quantity quantity
)
5010 write_typed_alloc_def (f
, variable_size_p (p
->type
), "", p
->name
,
5011 allocator_type
, quantity
);
5014 /* Writes typed allocator definitions into output F for the types in
5015 STRUCTURES and TYPEDEFS that are used by GC. */
5018 write_typed_alloc_defns (outf_p f
,
5019 const type_p structures
, const pair_p typedefs
)
5024 gcc_assert (f
!= NULL
);
5026 "\n/* Allocators for known structs and unions. */\n\n");
5027 for (s
= structures
; s
; s
= s
->next
)
5029 if (!USED_BY_TYPED_GC_P (s
))
5031 gcc_assert (union_or_struct_p (s
));
5032 /* In plugin mode onput output ggc_alloc macro definitions
5033 relevant to plugin input files. */
5034 if (nb_plugin_files
> 0
5035 && ((s
->u
.s
.line
.file
== NULL
) || !s
->u
.s
.line
.file
->inpisplugin
))
5037 write_typed_struct_alloc_def (f
, s
, "", single
);
5038 write_typed_struct_alloc_def (f
, s
, "cleared_", single
);
5039 write_typed_struct_alloc_def (f
, s
, "vec_", vector
);
5040 write_typed_struct_alloc_def (f
, s
, "cleared_vec_", vector
);
5043 oprintf (f
, "\n/* Allocators for known typedefs. */\n");
5044 for (p
= typedefs
; p
; p
= p
->next
)
5047 if (!USED_BY_TYPED_GC_P (s
) || (strcmp (p
->name
, s
->u
.s
.tag
) == 0))
5049 /* In plugin mode onput output ggc_alloc macro definitions
5050 relevant to plugin input files. */
5051 if (nb_plugin_files
> 0)
5053 struct fileloc
* filoc
= type_fileloc (s
);
5054 if (!filoc
|| !filoc
->file
->inpisplugin
)
5057 write_typed_typedef_alloc_def (f
, p
, "", single
);
5058 write_typed_typedef_alloc_def (f
, p
, "cleared_", single
);
5059 write_typed_typedef_alloc_def (f
, p
, "vec_", vector
);
5060 write_typed_typedef_alloc_def (f
, p
, "cleared_vec_", vector
);
5064 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
5065 guaranteee for somewhat increased readability. If name conflicts do happen,
5066 this funcion will have to be adjusted to be more like
5067 output_mangled_typename. */
5070 output_typename (outf_p of
, const_type_p t
)
5075 oprintf (of
, "str");
5078 oprintf (of
, "scalar");
5081 output_typename (of
, t
->u
.p
);
5084 case TYPE_USER_STRUCT
:
5086 case TYPE_LANG_STRUCT
:
5087 oprintf (of
, "%s", t
->u
.s
.tag
);
5089 case TYPE_PARAM_STRUCT
:
5092 for (i
= 0; i
< NUM_PARAM
; i
++)
5093 if (t
->u
.param_struct
.param
[i
] != NULL
)
5095 output_typename (of
, t
->u
.param_struct
.param
[i
]);
5098 output_typename (of
, t
->u
.param_struct
.stru
);
5102 case TYPE_UNDEFINED
:
5108 /* Writes a typed GC allocator for type S that is suitable as a callback for
5109 the splay tree implementation in libiberty. */
5112 write_splay_tree_allocator_def (const_type_p s
)
5114 outf_p of
= get_output_file_with_visibility (NULL
);
5115 oprintf (of
, "void * ggc_alloc_splay_tree_");
5116 output_typename (of
, s
);
5117 oprintf (of
, " (int sz, void * nl)\n");
5118 oprintf (of
, "{\n");
5119 oprintf (of
, " return ggc_splay_alloc (sz, nl);\n");
5120 oprintf (of
, "}\n\n");
5123 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
5124 for the splay tree implementation in libiberty. */
5127 write_splay_tree_allocators (const_type_p param_structs
)
5131 oprintf (header_file
, "\n/* Splay tree callback allocators. */\n");
5132 for (s
= param_structs
; s
; s
= s
->next
)
5133 if (s
->gc_used
== GC_POINTED_TO
)
5135 oprintf (header_file
, "extern void * ggc_alloc_splay_tree_");
5136 output_typename (header_file
, s
);
5137 oprintf (header_file
, " (int, void *);\n");
5138 write_splay_tree_allocator_def (s
);
5144 /* Dumps the value of typekind KIND. */
5147 dump_typekind (int indent
, enum typekind kind
)
5149 printf ("%*ckind = ", indent
, ' ');
5153 printf ("TYPE_SCALAR");
5156 printf ("TYPE_STRING");
5159 printf ("TYPE_STRUCT");
5161 case TYPE_UNDEFINED
:
5162 printf ("TYPE_UNDEFINED");
5164 case TYPE_USER_STRUCT
:
5165 printf ("TYPE_USER_STRUCT");
5168 printf ("TYPE_UNION");
5171 printf ("TYPE_POINTER");
5174 printf ("TYPE_ARRAY");
5176 case TYPE_LANG_STRUCT
:
5177 printf ("TYPE_LANG_STRUCT");
5179 case TYPE_PARAM_STRUCT
:
5180 printf ("TYPE_PARAM_STRUCT");
5188 /* Dumps the value of GC_USED flag. */
5191 dump_gc_used (int indent
, enum gc_used_enum gc_used
)
5193 printf ("%*cgc_used = ", indent
, ' ');
5197 printf ("GC_UNUSED");
5202 case GC_MAYBE_POINTED_TO
:
5203 printf ("GC_MAYBE_POINTED_TO");
5206 printf ("GC_POINTED_TO");
5214 /* Dumps the type options OPT. */
5217 dump_options (int indent
, options_p opt
)
5220 printf ("%*coptions = ", indent
, ' ');
5227 printf ("%s:string %s ", o
->name
, o
->info
.string
);
5230 printf ("%s:type ", o
->name
);
5231 dump_type (indent
+1, o
->info
.type
);
5234 printf ("%s:nested ", o
->name
);
5244 /* Dumps the source file location in LINE. */
5247 dump_fileloc (int indent
, struct fileloc line
)
5249 printf ("%*cfileloc: file = %s, line = %d\n", indent
, ' ',
5250 get_input_file_name (line
.file
),
5254 /* Recursively dumps the struct, union, or a language-specific
5258 dump_type_u_s (int indent
, type_p t
)
5262 gcc_assert (union_or_struct_p (t
));
5263 printf ("%*cu.s.tag = %s\n", indent
, ' ', t
->u
.s
.tag
);
5264 dump_fileloc (indent
, t
->u
.s
.line
);
5265 printf ("%*cu.s.fields =\n", indent
, ' ');
5266 fields
= t
->u
.s
.fields
;
5269 dump_pair (indent
+ INDENT
, fields
);
5270 fields
= fields
->next
;
5272 printf ("%*cend of fields of type %p\n", indent
, ' ', (void *) t
);
5273 dump_options (indent
, t
->u
.s
.opt
);
5274 printf ("%*cu.s.bitmap = %X\n", indent
, ' ', t
->u
.s
.bitmap
);
5275 if (t
->kind
== TYPE_LANG_STRUCT
)
5277 printf ("%*cu.s.lang_struct:\n", indent
, ' ');
5278 dump_type_list (indent
+ INDENT
, t
->u
.s
.lang_struct
);
5282 /* Recursively dumps the array T. */
5285 dump_type_u_a (int indent
, type_p t
)
5287 gcc_assert (t
->kind
== TYPE_ARRAY
);
5288 printf ("%*clen = %s, u.a.p:\n", indent
, ' ', t
->u
.a
.len
);
5289 dump_type_list (indent
+ INDENT
, t
->u
.a
.p
);
5292 /* Recursively dumps the parameterized struct T. */
5295 dump_type_u_param_struct (int indent
, type_p t
)
5298 gcc_assert (t
->kind
== TYPE_PARAM_STRUCT
);
5299 printf ("%*cu.param_struct.stru:\n", indent
, ' ');
5300 dump_type_list (indent
, t
->u
.param_struct
.stru
);
5301 dump_fileloc (indent
, t
->u
.param_struct
.line
);
5302 for (i
= 0; i
< NUM_PARAM
; i
++)
5304 if (t
->u
.param_struct
.param
[i
] == NULL
)
5306 printf ("%*cu.param_struct.param[%d]:\n", indent
, ' ', i
);
5307 dump_type (indent
+ INDENT
, t
->u
.param_struct
.param
[i
]);
5311 /* Recursively dumps the type list T. */
5314 dump_type_list (int indent
, type_p t
)
5319 dump_type (indent
, p
);
5324 static htab_t seen_types
;
5326 /* Recursively dumps the type T if it was not dumped previously. */
5329 dump_type (int indent
, type_p t
)
5333 if (seen_types
== NULL
)
5334 seen_types
= htab_create (100, htab_hash_pointer
, htab_eq_pointer
, NULL
);
5336 printf ("%*cType at %p: ", indent
, ' ', (void *) t
);
5337 slot
= htab_find_slot (seen_types
, t
, INSERT
);
5340 printf ("already seen.\n");
5346 dump_typekind (indent
, t
->kind
);
5347 printf ("%*cpointer_to = %p\n", indent
+ INDENT
, ' ',
5348 (void *) t
->pointer_to
);
5349 dump_gc_used (indent
+ INDENT
, t
->gc_used
);
5353 printf ("%*cscalar_is_char = %s\n", indent
+ INDENT
, ' ',
5354 t
->u
.scalar_is_char
? "true" : "false");
5360 case TYPE_LANG_STRUCT
:
5361 case TYPE_USER_STRUCT
:
5362 dump_type_u_s (indent
+ INDENT
, t
);
5365 printf ("%*cp:\n", indent
+ INDENT
, ' ');
5366 dump_type (indent
+ INDENT
, t
->u
.p
);
5369 dump_type_u_a (indent
+ INDENT
, t
);
5371 case TYPE_PARAM_STRUCT
:
5372 dump_type_u_param_struct (indent
+ INDENT
, t
);
5377 printf ("%*cEnd of type at %p\n", indent
, ' ', (void *) t
);
5380 /* Dumps the pair P. */
5383 dump_pair (int indent
, pair_p p
)
5385 printf ("%*cpair: name = %s\n", indent
, ' ', p
->name
);
5386 dump_type (indent
, p
->type
);
5387 dump_fileloc (indent
, p
->line
);
5388 dump_options (indent
, p
->opt
);
5389 printf ("%*cEnd of pair %s\n", indent
, ' ', p
->name
);
5392 /* Dumps the list of pairs PP. */
5395 dump_pair_list (const char *name
, pair_p pp
)
5398 printf ("%s:\n", name
);
5399 for (p
= pp
; p
!= NULL
; p
= p
->next
)
5401 printf ("End of %s\n\n", name
);
5404 /* Dumps the STRUCTURES. */
5407 dump_structures (const char *name
, type_p structures
)
5409 printf ("%s:\n", name
);
5410 dump_type_list (0, structures
);
5411 printf ("End of %s\n\n", name
);
5414 /* Dumps the internal structures of gengtype. This is useful to debug
5415 gengtype itself, or to understand what it does, e.g. for plugin
5419 dump_everything (void)
5421 dump_pair_list ("typedefs", typedefs
);
5422 dump_structures ("structures", structures
);
5423 dump_structures ("param_structs", param_structs
);
5424 dump_pair_list ("variables", variables
);
5426 /* Allocated with the first call to dump_type. */
5427 htab_delete (seen_types
);
5432 /* Option specification for getopt_long. */
5433 static const struct option gengtype_long_options
[] = {
5434 {"help", no_argument
, NULL
, 'h'},
5435 {"version", no_argument
, NULL
, 'V'},
5436 {"verbose", no_argument
, NULL
, 'v'},
5437 {"dump", no_argument
, NULL
, 'd'},
5438 {"debug", no_argument
, NULL
, 'D'},
5439 {"plugin", required_argument
, NULL
, 'P'},
5440 {"srcdir", required_argument
, NULL
, 'S'},
5441 {"backupdir", required_argument
, NULL
, 'B'},
5442 {"inputs", required_argument
, NULL
, 'I'},
5443 {"read-state", required_argument
, NULL
, 'r'},
5444 {"write-state", required_argument
, NULL
, 'w'},
5445 /* Terminating NULL placeholder. */
5446 {NULL
, no_argument
, NULL
, 0},
5453 printf ("Usage: %s\n", progname
);
5454 printf ("\t -h | --help " " \t# Give this help.\n");
5455 printf ("\t -D | --debug "
5456 " \t# Give debug output to debug %s itself.\n", progname
);
5457 printf ("\t -V | --version " " \t# Give version information.\n");
5458 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5459 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5460 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5461 " \t# Generate for plugin.\n");
5462 printf ("\t -S | --srcdir <GCC-directory> "
5463 " \t# Specify the GCC source directory.\n");
5464 printf ("\t -B | --backupdir <directory> "
5465 " \t# Specify the backup directory for updated files.\n");
5466 printf ("\t -I | --inputs <input-list> "
5467 " \t# Specify the file with source files list.\n");
5468 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5469 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5473 print_version (void)
5475 printf ("%s %s%s\n", progname
, pkgversion_string
, version_string
);
5476 printf ("Report bugs: %s\n", bug_report_url
);
5479 /* Parse the program options using getopt_long... */
5481 parse_program_options (int argc
, char **argv
)
5484 while ((opt
= getopt_long (argc
, argv
, "hVvdP:S:B:I:w:r:D",
5485 gengtype_long_options
, NULL
)) >= 0)
5489 case 'h': /* --help */
5492 case 'V': /* --version */
5495 case 'd': /* --dump */
5498 case 'D': /* --debug */
5501 case 'v': /* --verbose */
5504 case 'P': /* --plugin */
5506 plugin_output_filename
= optarg
;
5508 fatal ("missing plugin output file name");
5510 case 'S': /* --srcdir */
5514 fatal ("missing source directory");
5515 srcdir_len
= strlen (srcdir
);
5517 case 'B': /* --backupdir */
5519 backup_dir
= optarg
;
5521 fatal ("missing backup directory");
5523 case 'I': /* --inputs */
5527 fatal ("missing input list");
5529 case 'r': /* --read-state */
5531 read_state_filename
= optarg
;
5533 fatal ("missing read state file");
5534 DBGPRINTF ("read state %s\n", optarg
);
5536 case 'w': /* --write-state */
5537 DBGPRINTF ("write state %s\n", optarg
);
5539 write_state_filename
= optarg
;
5541 fatal ("missing write state file");
5544 fprintf (stderr
, "%s: unknown flag '%c'\n", progname
, opt
);
5546 fatal ("unexpected flag");
5549 if (plugin_output_filename
)
5551 /* In plugin mode we require some input files. */
5554 fatal ("no source files given in plugin mode");
5555 nb_plugin_files
= argc
- optind
;
5556 plugin_files
= XNEWVEC (input_file
*, nb_plugin_files
);
5557 for (i
= 0; i
< (int) nb_plugin_files
; i
++)
5559 char *name
= argv
[i
+ optind
];
5560 plugin_files
[i
] = input_file_by_name (name
);
5567 /******* Manage input files. ******/
5569 /* Hash table of unique input file names. */
5570 static htab_t input_file_htab
;
5572 /* Find or allocate a new input_file by hash-consing it. */
5574 input_file_by_name (const char* name
)
5577 input_file
* f
= NULL
;
5581 namlen
= strlen (name
);
5582 f
= XCNEWVAR (input_file
, sizeof (input_file
)+namlen
+2);
5585 f
->inpisplugin
= false;
5586 strcpy (f
->inpname
, name
);
5587 slot
= htab_find_slot (input_file_htab
, f
, INSERT
);
5588 gcc_assert (slot
!= NULL
);
5591 /* Already known input file. */
5593 return (input_file
*)(*slot
);
5595 /* New input file. */
5600 /* Hash table support routines for input_file-s. */
5602 htab_hash_inputfile (const void *p
)
5604 const input_file
*inpf
= (const input_file
*) p
;
5606 return htab_hash_string (get_input_file_name (inpf
));
5610 htab_eq_inputfile (const void *x
, const void *y
)
5612 const input_file
*inpfx
= (const input_file
*) x
;
5613 const input_file
*inpfy
= (const input_file
*) y
;
5614 gcc_assert (inpfx
!= NULL
&& inpfy
!= NULL
);
5615 return !filename_cmp (get_input_file_name (inpfx
), get_input_file_name (inpfy
));
5620 main (int argc
, char **argv
)
5623 static struct fileloc pos
= { NULL
, 0 };
5624 outf_p output_header
;
5626 /* Mandatory common initializations. */
5627 progname
= "gengtype"; /* For fatal and messages. */
5628 /* Create the hash-table used to hash-cons input files. */
5630 htab_create (800, htab_hash_inputfile
, htab_eq_inputfile
, NULL
);
5631 /* Initialize our special input files. */
5632 this_file
= input_file_by_name (__FILE__
);
5633 system_h_file
= input_file_by_name ("system.h");
5634 /* Set the scalar_is_char union number for predefined scalar types. */
5635 scalar_nonchar
.u
.scalar_is_char
= FALSE
;
5636 scalar_char
.u
.scalar_is_char
= TRUE
;
5638 parse_program_options (argc
, argv
);
5643 time_t now
= (time_t) 0;
5645 DBGPRINTF ("gengtype started pid %d at %s",
5646 (int) getpid (), ctime (&now
));
5648 #endif /* ENABLE_CHECKING */
5650 /* Parse the input list and the input files. */
5651 DBGPRINTF ("inputlist %s", inputlist
);
5652 if (read_state_filename
)
5655 fatal ("input list %s cannot be given with a read state file %s",
5656 inputlist
, read_state_filename
);
5657 read_state (read_state_filename
);
5658 DBGPRINT_COUNT_TYPE ("structures after read_state", structures
);
5659 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs
);
5663 /* These types are set up with #define or else outside of where
5664 we can see them. We should initialize them before calling
5666 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5668 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos
));
5669 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos
));
5670 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos
));
5671 POS_HERE (do_scalar_typedef ("double_int", &pos
));
5672 POS_HERE (do_scalar_typedef ("offset_int", &pos
));
5673 POS_HERE (do_scalar_typedef ("widest_int", &pos
));
5674 POS_HERE (do_scalar_typedef ("uint64_t", &pos
));
5675 POS_HERE (do_scalar_typedef ("uint8", &pos
));
5676 POS_HERE (do_scalar_typedef ("uintptr_t", &pos
));
5677 POS_HERE (do_scalar_typedef ("jword", &pos
));
5678 POS_HERE (do_scalar_typedef ("JCF_u2", &pos
));
5679 POS_HERE (do_scalar_typedef ("void", &pos
));
5680 POS_HERE (do_typedef ("PTR",
5681 create_pointer (resolve_typedef ("void", &pos
)),
5684 read_input_list (inputlist
);
5685 for (i
= 0; i
< num_gt_files
; i
++)
5687 parse_file (get_input_file_name (gt_files
[i
]));
5688 DBGPRINTF ("parsed file #%d %s",
5689 (int) i
, get_input_file_name (gt_files
[i
]));
5691 if (verbosity_level
>= 1)
5692 printf ("%s parsed %d files with %d GTY types\n",
5693 progname
, (int) num_gt_files
, type_count
);
5695 DBGPRINT_COUNT_TYPE ("structures after parsing", structures
);
5696 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs
);
5700 fatal ("either an input list or a read state file should be given");
5705 if (plugin_output_filename
)
5708 /* In plugin mode, we should have read a state file, and have
5709 given at least one plugin file. */
5710 if (!read_state_filename
)
5711 fatal ("No read state given in plugin mode for %s",
5712 plugin_output_filename
);
5714 if (nb_plugin_files
== 0 || !plugin_files
)
5715 fatal ("No plugin files given in plugin mode for %s",
5716 plugin_output_filename
);
5718 /* Parse our plugin files and augment the state. */
5719 for (ix
= 0; ix
< nb_plugin_files
; ix
++)
5721 input_file
* pluginput
= plugin_files
[ix
];
5722 pluginput
->inpisplugin
= true;
5723 parse_file (get_input_file_name (pluginput
));
5728 plugin_output
= create_file ("GCC", plugin_output_filename
);
5729 DBGPRINTF ("created plugin_output %p named %s",
5730 (void *) plugin_output
, plugin_output
->name
);
5733 { /* No plugin files, we are in normal mode. */
5735 fatal ("gengtype needs a source directory in normal mode");
5742 /* The call to set_gc_used may indirectly call find_param_structure
5743 hence enlarge the param_structs list of types. */
5744 set_gc_used (variables
);
5746 /* The state at this point is read from the state input file or by
5747 parsing source files and optionally augmented by parsing plugin
5748 source files. Write it now. */
5749 if (write_state_filename
)
5751 DBGPRINT_COUNT_TYPE ("structures before write_state", structures
);
5752 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs
);
5755 fatal ("didn't write state file %s after errors",
5756 write_state_filename
);
5758 DBGPRINTF ("before write_state %s", write_state_filename
);
5759 write_state (write_state_filename
);
5764 /* After having written the state file we return immediately to
5765 avoid generating any output file. */
5775 output_header
= plugin_output
? plugin_output
: header_file
;
5776 write_typed_alloc_defns (output_header
, structures
, typedefs
);
5777 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5779 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5782 write_types (output_header
, structures
, param_structs
, &ggc_wtd
);
5783 if (plugin_files
== NULL
)
5785 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5787 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5789 write_types (header_file
, structures
, param_structs
, &pch_wtd
);
5790 write_local (header_file
, structures
, param_structs
);
5792 write_splay_tree_allocators (param_structs
);
5793 write_roots (variables
, plugin_files
== NULL
);
5795 close_output_files ();
5800 /* Don't bother about free-ing any input or plugin file, etc. */