1 /* Process source files and output type information.
2 Copyright (C) 2002-2013 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 "double-int.h"
29 #include "version.h" /* for version_string & pkgversion_string. */
34 #include "filenames.h"
36 /* Data types, macros, etc. used only in this file. */
39 /* The list of output files. */
42 /* The output header file that is included into pretty much every
47 /* The name of the file containing the list of input files. */
48 static char *inputlist
;
50 /* The plugin input files and their number; in that case only
51 a single file is produced. */
52 static input_file
**plugin_files
;
53 static size_t nb_plugin_files
;
55 /* The generated plugin output file and name. */
56 static outf_p plugin_output
;
57 static char *plugin_output_filename
;
59 /* Our source directory and its length. */
63 /* Variables used for reading and writing the state. */
64 const char *read_state_filename
;
65 const char *write_state_filename
;
67 /* Variables to help debugging. */
71 /* Level for verbose messages. */
74 /* We have a type count and use it to set the state_number of newly
75 allocated types to some unique negative number. */
76 static int type_count
;
78 /* The backup directory should be in the same file system as the
79 generated files, otherwise the rename(2) system call would fail.
80 If NULL, no backup is made when overwriting a generated file. */
81 static const char* backup_dir
; /* (-B) program option. */
84 static outf_p
create_file (const char *, const char *);
86 static const char *get_file_basename (const input_file
*);
87 static const char *get_file_realbasename (const input_file
*);
89 static int get_prefix_langdir_index (const char *);
90 static const char *get_file_langdir (const input_file
*);
92 static void dump_pair (int indent
, pair_p p
);
93 static void dump_type (int indent
, type_p p
);
94 static void dump_type_list (int indent
, type_p p
);
97 /* Nonzero iff an error has occurred. */
98 bool hit_error
= false;
100 static void gen_rtx_next (void);
101 static void write_rtx_next (void);
102 static void open_base_files (void);
103 static void close_output_files (void);
105 /* Report an error at POS, printing MSG. */
108 error_at_line (const struct fileloc
*pos
, const char *msg
, ...)
112 gcc_assert (pos
!= NULL
&& pos
->file
!= NULL
);
115 fprintf (stderr
, "%s:%d: ", get_input_file_name (pos
->file
), pos
->line
);
116 vfprintf (stderr
, msg
, ap
);
117 fputc ('\n', stderr
);
123 /* asprintf, but produces fatal message on out-of-memory. */
125 xasprintf (const char *format
, ...)
131 va_start (ap
, format
);
132 n
= vasprintf (&result
, format
, ap
);
133 if (result
== NULL
|| n
< 0)
134 fatal ("out of memory");
140 /* Locate the ultimate base class of struct S. */
143 get_ultimate_base_class (const_type_p s
)
145 while (s
->u
.s
.base_class
)
146 s
= s
->u
.s
.base_class
;
150 /* Input file handling. */
152 /* Table of all input files. */
153 const input_file
**gt_files
;
156 /* A number of places use the name of this "gengtype.c" file for a
157 location for things that we can't rely on the source to define.
158 Make sure we can still use pointer comparison on filenames. */
159 input_file
* this_file
;
160 /* The "system.h" file is likewise specially useful. */
161 input_file
* system_h_file
;
163 /* Vector of per-language directories. */
164 const char **lang_dir_names
;
165 size_t num_lang_dirs
;
167 /* An array of output files suitable for definitions. There is one
168 BASE_FILES entry for each language. */
169 static outf_p
*base_files
;
174 /* Utility debugging function, printing the various type counts within
175 a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
177 dbgprint_count_type_at (const char *fil
, int lin
, const char *msg
, type_p t
)
179 int nb_types
= 0, nb_scalar
= 0, nb_string
= 0;
180 int nb_struct
= 0, nb_union
= 0, nb_array
= 0, nb_pointer
= 0;
181 int nb_lang_struct
= 0, nb_param_struct
= 0;
182 int nb_user_struct
= 0, nb_undefined
= 0;
184 for (p
= t
; p
; p
= p
->next
)
200 case TYPE_USER_STRUCT
:
212 case TYPE_LANG_STRUCT
:
215 case TYPE_PARAM_STRUCT
:
222 fprintf (stderr
, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
223 lbasename (fil
), lin
, msg
, nb_types
);
224 if (nb_scalar
> 0 || nb_string
> 0)
225 fprintf (stderr
, "@@%%@@ %d scalars, %d strings\n", nb_scalar
, nb_string
);
226 if (nb_struct
> 0 || nb_union
> 0)
227 fprintf (stderr
, "@@%%@@ %d structs, %d unions\n", nb_struct
, nb_union
);
228 if (nb_pointer
> 0 || nb_array
> 0)
229 fprintf (stderr
, "@@%%@@ %d pointers, %d arrays\n", nb_pointer
, nb_array
);
230 if (nb_lang_struct
> 0 || nb_param_struct
> 0)
231 fprintf (stderr
, "@@%%@@ %d lang_structs, %d param_structs\n",
232 nb_lang_struct
, nb_param_struct
);
233 if (nb_user_struct
> 0)
234 fprintf (stderr
, "@@%%@@ %d user_structs\n", nb_user_struct
);
235 if (nb_undefined
> 0)
236 fprintf (stderr
, "@@%%@@ %d undefined types\n", nb_undefined
);
237 fprintf (stderr
, "\n");
239 #endif /* ENABLE_CHECKING */
241 /* Scan the input file, LIST, and determine how much space we need to
242 store strings in. Also, count the number of language directories
243 and files. The numbers returned are overestimates as they does not
244 consider repeated files. */
246 measure_input_list (FILE *list
)
252 num_gt_files
= plugin_files
? nb_plugin_files
: 0;
253 while ((c
= getc (list
)) != EOF
)
262 /* Add space for a lang_bitmap before the input file name. */
263 n
+= sizeof (lang_bitmap
);
277 /* Read one input line from LIST to HEREP (which is updated). A
278 pointer to the string is returned via LINEP. If it was a language
279 subdirectory in square brackets, strip off the square brackets and
280 return true. Otherwise, leave space before the string for a
281 lang_bitmap, and return false. At EOF, returns false, does not
282 touch *HEREP, and sets *LINEP to NULL. POS is used for
285 read_input_line (FILE *list
, char **herep
, char **linep
, struct fileloc
*pos
)
291 /* Read over whitespace. */
292 while (c
== '\n' || c
== ' ')
302 /* No space for a lang_bitmap is necessary. Discard the '['. */
305 while (c
!= ']' && c
!= '\n' && c
!= EOF
)
314 c
= getc (list
); /* eat what should be a newline */
315 if (c
!= '\n' && c
!= EOF
)
316 error_at_line (pos
, "junk on line after language tag [%s]", line
);
319 error_at_line (pos
, "missing close bracket for language tag [%s",
328 /* Leave space for a lang_bitmap. */
329 memset (here
, 0, sizeof (lang_bitmap
));
330 here
+= sizeof (lang_bitmap
);
337 while (c
!= EOF
&& c
!= '\n');
345 /* Read the list of input files from LIST and compute all of the
346 relevant tables. There is one file per line of the list. At
347 first, all the files on the list are language-generic, but
348 eventually a line will appear which is the name of a language
349 subdirectory in square brackets, like this: [cp]. All subsequent
350 files are specific to that language, until another language
351 subdirectory tag appears. Files can appear more than once, if
352 they apply to more than one language. */
354 read_input_list (const char *listname
)
356 FILE *list
= fopen (listname
, "r");
358 fatal ("cannot open %s: %s", listname
, xstrerror (errno
));
362 size_t bufsz
= measure_input_list (list
);
363 char *buf
= XNEWVEC (char, bufsz
);
365 char *committed
= buf
;
366 char *limit
= buf
+ bufsz
;
371 lang_bitmap curlangs
= (1 << num_lang_dirs
) - 1;
373 epos
.file
= input_file_by_name (listname
);
376 lang_dir_names
= XNEWVEC (const char *, num_lang_dirs
);
377 gt_files
= XNEWVEC (const input_file
*, num_gt_files
);
384 is_language
= read_input_line (list
, &here
, &line
, &epos
);
385 gcc_assert (here
<= limit
);
388 else if (is_language
)
391 gcc_assert (langno
<= num_lang_dirs
);
392 for (i
= 0; i
< langno
; i
++)
393 if (strcmp (lang_dir_names
[i
], line
) == 0)
395 error_at_line (&epos
, "duplicate language tag [%s]",
402 curlangs
= 1 << langno
;
403 lang_dir_names
[langno
++] = line
;
408 input_file
*inpf
= input_file_by_name (line
);
409 gcc_assert (nfiles
<= num_gt_files
);
410 for (i
= 0; i
< nfiles
; i
++)
411 /* Since the input_file-s are uniquely hash-consed, we
412 can just compare pointers! */
413 if (gt_files
[i
] == inpf
)
415 /* Throw away the string we just read, and add the
416 current language to the existing string's bitmap. */
417 lang_bitmap bmap
= get_lang_bitmap (inpf
);
419 error_at_line (&epos
,
420 "file %s specified more than once "
421 "for language %s", line
,
423 0 ? "(all)" : lang_dir_names
[langno
-
427 set_lang_bitmap (inpf
, bmap
);
432 set_lang_bitmap (inpf
, curlangs
);
433 gt_files
[nfiles
++] = inpf
;
436 /* Update the global counts now that we know accurately how many
437 things there are. (We do not bother resizing the arrays down.) */
438 num_lang_dirs
= langno
;
439 /* Add the plugin files if provided. */
443 for (i
= 0; i
< nb_plugin_files
; i
++)
444 gt_files
[nfiles
++] = plugin_files
[i
];
446 num_gt_files
= nfiles
;
449 /* Sanity check: any file that resides in a language subdirectory
450 (e.g. 'cp') ought to belong to the corresponding language.
451 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
452 (Can you even do that? Should you be allowed to?) */
455 for (f
= 0; f
< num_gt_files
; f
++)
457 lang_bitmap bitmap
= get_lang_bitmap (gt_files
[f
]);
458 const char *basename
= get_file_basename (gt_files
[f
]);
459 const char *slashpos
= strchr (basename
, '/');
460 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
461 const char *slashpos2
= strchr (basename
, '\\');
463 if (!slashpos
|| (slashpos2
&& slashpos2
< slashpos
))
464 slashpos
= slashpos2
;
470 for (l
= 0; l
< num_lang_dirs
; l
++)
471 if ((size_t) (slashpos
- basename
) == strlen (lang_dir_names
[l
])
472 && memcmp (basename
, lang_dir_names
[l
],
473 strlen (lang_dir_names
[l
])) == 0)
475 if (!(bitmap
& (1 << l
)))
476 error ("%s is in language directory '%s' but is not "
477 "tagged for that language",
478 basename
, lang_dir_names
[l
]);
486 fatal ("error reading %s: %s", listname
, xstrerror (errno
));
493 /* The one and only TYPE_STRING. */
495 struct type string_type
= {
496 TYPE_STRING
, 0, 0, 0, GC_USED
, {0}
499 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
500 set early in main. */
502 struct type scalar_nonchar
= {
503 TYPE_SCALAR
, 0, 0, 0, GC_USED
, {0}
506 struct type scalar_char
= {
507 TYPE_SCALAR
, 0, 0, 0, GC_USED
, {0}
510 /* Lists of various things. */
512 pair_p typedefs
= NULL
;
513 type_p structures
= NULL
;
514 type_p param_structs
= NULL
;
515 pair_p variables
= NULL
;
517 static type_p
find_param_structure (type_p t
, type_p param
[NUM_PARAM
]);
518 static type_p
adjust_field_tree_exp (type_p t
, options_p opt
);
519 static type_p
adjust_field_rtx_def (type_p t
, options_p opt
);
521 /* Define S as a typedef to T at POS. */
524 do_typedef (const char *s
, type_p t
, struct fileloc
*pos
)
528 /* temporary kludge - gengtype doesn't handle conditionals or
529 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
530 is coming from this file (main() sets them up with safe dummy
532 if (!strcmp (s
, "CUMULATIVE_ARGS") && pos
->file
!= this_file
)
535 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
536 if (strcmp (p
->name
, s
) == 0)
540 error_at_line (pos
, "type `%s' previously defined", s
);
541 error_at_line (&p
->line
, "previously defined here");
546 p
= XNEW (struct pair
);
555 /* Define S as a typename of a scalar. Cannot be used to define
556 typedefs of 'char'. Note: is also used for pointer-to-function
557 typedefs (which are therefore not treated as pointers). */
560 do_scalar_typedef (const char *s
, struct fileloc
*pos
)
562 do_typedef (s
, &scalar_nonchar
, pos
);
566 /* Define TYPE_NAME to be a user defined type at location POS. */
569 create_user_defined_type (const char *type_name
, struct fileloc
*pos
)
571 type_p ty
= find_structure (type_name
, TYPE_USER_STRUCT
);
573 /* We might have already seen an incomplete decl of the given type,
574 in which case we won't have yet seen a GTY((user)), and the type will
575 only have kind "TYPE_STRUCT". Mark it as a user struct. */
576 ty
->kind
= TYPE_USER_STRUCT
;
579 ty
->u
.s
.bitmap
= get_lang_bitmap (pos
->file
);
580 do_typedef (type_name
, ty
, pos
);
582 /* If TYPE_NAME specifies a template, create references to the types
583 in the template by pretending that each type is a field of TY.
584 This is needed to make sure that the types referenced by the
585 template are marked as used. */
586 char *str
= xstrdup (type_name
);
587 char *open_bracket
= strchr (str
, '<');
590 /* We only accept simple template declarations (see
591 require_template_declaration), so we only need to parse a
592 comma-separated list of strings, implicitly assumed to
594 char *arg
= open_bracket
+ 1;
595 char *type_id
= strtok (arg
, ",>");
599 /* Create a new field for every type found inside the template
601 const char *field_name
= xstrdup (type_id
);
602 type_p arg_type
= resolve_typedef (field_name
, pos
);
603 fields
= create_field_at (fields
, arg_type
, field_name
, 0, pos
);
604 type_id
= strtok (0, ",>");
607 /* Associate the field list to TY. */
608 ty
->u
.s
.fields
= fields
;
616 /* Given a typedef name S, return its associated type. Return NULL if
617 S is not a registered type name. */
620 type_for_name (const char *s
)
624 /* Special-case support for types within a "gcc::" namespace. Rather
625 than fully-supporting namespaces, simply strip off the "gcc::" prefix
626 where present. This allows us to have GTY roots of this form:
627 extern GTY(()) gcc::some_type *some_ptr;
628 where the autogenerated functions will refer to simply "some_type",
629 where they can be resolved into their namespace. */
630 if (0 == strncmp (s
, "gcc::", 5))
633 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
634 if (strcmp (p
->name
, s
) == 0)
640 /* Create an undefined type with name S and location POS. Return the
641 newly created type. */
644 create_undefined_type (const char *s
, struct fileloc
*pos
)
646 type_p ty
= find_structure (s
, TYPE_UNDEFINED
);
648 ty
->u
.s
.bitmap
= get_lang_bitmap (pos
->file
);
649 do_typedef (s
, ty
, pos
);
654 /* Return the type previously defined for S. Use POS to report errors. */
657 resolve_typedef (const char *s
, struct fileloc
*pos
)
659 bool is_template_instance
= (strchr (s
, '<') != NULL
);
660 type_p p
= type_for_name (s
);
662 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
663 type for regular type identifiers. If the type identifier S is a
664 template instantiation, however, we treat it as a user defined
667 FIXME, this is actually a limitation in gengtype. Supporting
668 template types and their instances would require keeping separate
669 track of the basic types definition and its instances. This
670 essentially forces all template classes in GC to be marked
673 p
= (is_template_instance
)
674 ? create_user_defined_type (s
, pos
)
675 : create_undefined_type (s
, pos
);
680 /* Add SUBCLASS to head of linked list of BASE's subclasses. */
682 void add_subclass (type_p base
, type_p subclass
)
684 gcc_assert (union_or_struct_p (base
));
685 gcc_assert (union_or_struct_p (subclass
));
687 subclass
->u
.s
.next_sibling_class
= base
->u
.s
.first_subclass
;
688 base
->u
.s
.first_subclass
= subclass
;
691 /* Create and return a new structure with tag NAME at POS with fields
692 FIELDS and options O. The KIND of structure must be one of
693 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
696 new_structure (const char *name
, enum typekind kind
, struct fileloc
*pos
,
697 pair_p fields
, options_p o
, type_p base_class
)
701 lang_bitmap bitmap
= get_lang_bitmap (pos
->file
);
702 bool isunion
= (kind
== TYPE_UNION
);
704 gcc_assert (union_or_struct_p (kind
));
706 for (si
= structures
; si
!= NULL
; si
= si
->next
)
707 if (strcmp (name
, si
->u
.s
.tag
) == 0 && UNION_P (si
) == isunion
)
710 if (si
->kind
== TYPE_LANG_STRUCT
)
714 for (si
= ls
->u
.s
.lang_struct
; si
!= NULL
; si
= si
->next
)
715 if (si
->u
.s
.bitmap
== bitmap
)
718 else if (si
->u
.s
.line
.file
!= NULL
&& si
->u
.s
.bitmap
!= bitmap
)
722 si
= XCNEW (struct type
);
723 memcpy (si
, ls
, sizeof (struct type
));
724 ls
->kind
= TYPE_LANG_STRUCT
;
725 ls
->u
.s
.lang_struct
= si
;
726 ls
->u
.s
.fields
= NULL
;
728 si
->state_number
= -type_count
;
729 si
->pointer_to
= NULL
;
730 si
->u
.s
.lang_struct
= ls
;
735 if (ls
!= NULL
&& s
== NULL
)
738 s
= XCNEW (struct type
);
739 s
->state_number
= -type_count
;
740 s
->next
= ls
->u
.s
.lang_struct
;
741 ls
->u
.s
.lang_struct
= s
;
742 s
->u
.s
.lang_struct
= ls
;
750 s
= XCNEW (struct type
);
751 s
->state_number
= -type_count
;
752 s
->next
= structures
;
756 if (s
->u
.s
.lang_struct
&& (s
->u
.s
.lang_struct
->u
.s
.bitmap
& bitmap
))
758 error_at_line (pos
, "duplicate definition of '%s %s'",
759 isunion
? "union" : "struct", s
->u
.s
.tag
);
760 error_at_line (&s
->u
.s
.line
, "previous definition here");
766 s
->u
.s
.fields
= fields
;
768 s
->u
.s
.bitmap
= bitmap
;
769 if (s
->u
.s
.lang_struct
)
770 s
->u
.s
.lang_struct
->u
.s
.bitmap
|= bitmap
;
771 s
->u
.s
.base_class
= base_class
;
773 add_subclass (base_class
, s
);
778 /* Return the previously-defined structure or union with tag NAME,
779 or a new empty structure or union if none was defined previously.
780 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
784 find_structure (const char *name
, enum typekind kind
)
787 bool isunion
= (kind
== TYPE_UNION
);
789 gcc_assert (kind
== TYPE_UNDEFINED
|| union_or_struct_p (kind
));
791 for (s
= structures
; s
!= NULL
; s
= s
->next
)
792 if (strcmp (name
, s
->u
.s
.tag
) == 0 && UNION_P (s
) == isunion
)
796 s
= XCNEW (struct type
);
797 s
->next
= structures
;
798 s
->state_number
= -type_count
;
806 /* Return the previously-defined parameterized structure for structure
807 T and parameters PARAM, or a new parameterized empty structure or
808 union if none was defined previously. */
811 find_param_structure (type_p t
, type_p param
[NUM_PARAM
])
815 for (res
= param_structs
; res
; res
= res
->next
)
816 if (res
->u
.param_struct
.stru
== t
817 && memcmp (res
->u
.param_struct
.param
, param
,
818 sizeof (type_p
) * NUM_PARAM
) == 0)
823 res
= XCNEW (struct type
);
824 res
->kind
= TYPE_PARAM_STRUCT
;
825 res
->next
= param_structs
;
826 res
->state_number
= -type_count
;
828 res
->u
.param_struct
.stru
= t
;
829 memcpy (res
->u
.param_struct
.param
, param
, sizeof (type_p
) * NUM_PARAM
);
834 /* Return a scalar type with name NAME. */
837 create_scalar_type (const char *name
)
839 if (!strcmp (name
, "char") || !strcmp (name
, "unsigned char"))
842 return &scalar_nonchar
;
846 /* Return a pointer to T. */
849 create_pointer (type_p t
)
853 type_p r
= XCNEW (struct type
);
855 r
->state_number
= -type_count
;
856 r
->kind
= TYPE_POINTER
;
860 return t
->pointer_to
;
863 /* Return an array of length LEN. */
866 create_array (type_p t
, const char *len
)
871 v
= XCNEW (struct type
);
872 v
->kind
= TYPE_ARRAY
;
873 v
->state_number
= -type_count
;
879 /* Return a string options structure with name NAME and info INFO.
880 NEXT is the next option in the chain. */
882 create_string_option (options_p next
, const char *name
, const char *info
)
884 options_p o
= XNEW (struct options
);
885 o
->kind
= OPTION_STRING
;
888 o
->info
.string
= info
;
892 /* Create a type options structure with name NAME and info INFO. NEXT
893 is the next option in the chain. */
895 create_type_option (options_p next
, const char* name
, type_p info
)
897 options_p o
= XNEW (struct options
);
900 o
->kind
= OPTION_TYPE
;
905 /* Create a nested pointer options structure with name NAME and info
906 INFO. NEXT is the next option in the chain. */
908 create_nested_option (options_p next
, const char* name
,
909 struct nested_ptr_data
* info
)
912 o
= XNEW (struct options
);
915 o
->kind
= OPTION_NESTED
;
916 o
->info
.nested
= info
;
920 /* Return an options structure for a "nested_ptr" option. */
922 create_nested_ptr_option (options_p next
, type_p t
,
923 const char *to
, const char *from
)
925 struct nested_ptr_data
*d
= XNEW (struct nested_ptr_data
);
927 d
->type
= adjust_field_type (t
, 0);
929 d
->convert_from
= from
;
930 return create_nested_option (next
, "nested_ptr", d
);
933 /* Add a variable named S of type T with options O defined at POS,
936 note_variable (const char *s
, type_p t
, options_p o
, struct fileloc
*pos
)
939 n
= XNEW (struct pair
);
948 /* Most-general structure field creator. */
950 create_field_all (pair_p next
, type_p type
, const char *name
, options_p opt
,
951 const input_file
*inpf
, int line
)
955 field
= XNEW (struct pair
);
960 field
->line
.file
= inpf
;
961 field
->line
.line
= line
;
965 /* Create a field that came from the source code we are scanning,
966 i.e. we have a 'struct fileloc', and possibly options; also,
967 adjust_field_type should be called. */
969 create_field_at (pair_p next
, type_p type
, const char *name
, options_p opt
,
972 return create_field_all (next
, adjust_field_type (type
, opt
),
973 name
, opt
, pos
->file
, pos
->line
);
976 /* Create a fake field with the given type and name. NEXT is the next
977 field in the chain. */
978 #define create_field(next,type,name) \
979 create_field_all (next,type,name, 0, this_file, __LINE__)
981 /* Like create_field, but the field is only valid when condition COND
985 create_optional_field_ (pair_p next
, type_p type
, const char *name
,
986 const char *cond
, int line
)
992 /* Create a fake union type with a single nameless field of type TYPE.
993 The field has a tag of "1". This allows us to make the presence
994 of a field of type TYPE depend on some boolean "desc" being true. */
995 union_fields
= create_field (NULL
, type
, "");
997 create_string_option (union_fields
->opt
, "dot", "");
999 create_string_option (union_fields
->opt
, "tag", "1");
1001 new_structure (xasprintf ("%s_%d", "fake_union", id
++), TYPE_UNION
,
1002 &lexer_line
, union_fields
, NULL
, NULL
);
1004 /* Create the field and give it the new fake union type. Add a "desc"
1005 tag that specifies the condition under which the field is valid. */
1006 return create_field_all (next
, union_type
, name
,
1007 create_string_option (0, "desc", cond
),
1011 #define create_optional_field(next,type,name,cond) \
1012 create_optional_field_(next,type,name,cond,__LINE__)
1014 /* Reverse a linked list of 'struct pair's in place. */
1016 nreverse_pairs (pair_p list
)
1018 pair_p prev
= 0, p
, next
;
1019 for (p
= list
; p
; p
= next
)
1029 /* We don't care how long a CONST_DOUBLE is. */
1030 #define CONST_DOUBLE_FORMAT "ww"
1031 /* We don't want to see codes that are only for generator files. */
1032 #undef GENERATOR_FILE
1036 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1042 static const char *const rtx_name
[NUM_RTX_CODE
] = {
1043 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1048 static const char *const rtx_format
[NUM_RTX_CODE
] = {
1049 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1054 static int rtx_next_new
[NUM_RTX_CODE
];
1056 /* We also need codes and names for insn notes (not register notes).
1057 Note that we do *not* bias the note values here. */
1060 #define DEF_INSN_NOTE(NAME) NAME,
1061 #include "insn-notes.def"
1062 #undef DEF_INSN_NOTE
1067 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1068 default field for line number notes. */
1069 static const char *const note_insn_name
[NOTE_INSN_MAX
+ 1] = {
1070 #define DEF_INSN_NOTE(NAME) #NAME,
1071 #include "insn-notes.def"
1072 #undef DEF_INSN_NOTE
1075 #undef CONST_DOUBLE_FORMAT
1076 #define GENERATOR_FILE
1078 /* Generate the contents of the rtx_next array. This really doesn't belong
1079 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1085 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1089 rtx_next_new
[i
] = -1;
1090 if (strncmp (rtx_format
[i
], "iuu", 3) == 0)
1091 rtx_next_new
[i
] = 2;
1092 else if (i
== COND_EXEC
|| i
== SET
|| i
== EXPR_LIST
|| i
== INSN_LIST
)
1093 rtx_next_new
[i
] = 1;
1095 for (k
= strlen (rtx_format
[i
]) - 1; k
>= 0; k
--)
1096 if (rtx_format
[i
][k
] == 'e' || rtx_format
[i
][k
] == 'u')
1097 rtx_next_new
[i
] = k
;
1101 /* Write out the contents of the rtx_next array. */
1103 write_rtx_next (void)
1105 outf_p f
= get_output_file_with_visibility (NULL
);
1110 oprintf (f
, "\n/* Used to implement the RTX_NEXT macro. */\n");
1111 oprintf (f
, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1112 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1113 if (rtx_next_new
[i
] == -1)
1114 oprintf (f
, " 0,\n");
1117 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new
[i
]);
1118 oprintf (f
, "};\n");
1121 /* Handle `special("rtx_def")'. This is a special case for field
1122 `fld' of struct rtx_def, which is an array of unions whose values
1123 are based in a complex way on the type of RTL. */
1126 adjust_field_rtx_def (type_p t
, options_p
ARG_UNUSED (opt
))
1131 type_p rtx_tp
, rtvec_tp
, tree_tp
, mem_attrs_tp
, note_union_tp
, scalar_tp
;
1132 type_p basic_block_tp
, reg_attrs_tp
, constant_tp
, symbol_union_tp
;
1134 if (t
->kind
!= TYPE_UNION
)
1136 error_at_line (&lexer_line
,
1137 "special `rtx_def' must be applied to a union");
1138 return &string_type
;
1141 nodot
= create_string_option (NULL
, "dot", "");
1143 rtx_tp
= create_pointer (find_structure ("rtx_def", TYPE_STRUCT
));
1144 rtvec_tp
= create_pointer (find_structure ("rtvec_def", TYPE_STRUCT
));
1145 tree_tp
= create_pointer (find_structure ("tree_node", TYPE_UNION
));
1146 mem_attrs_tp
= create_pointer (find_structure ("mem_attrs", TYPE_STRUCT
));
1148 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT
));
1150 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT
));
1152 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT
));
1153 scalar_tp
= &scalar_nonchar
; /* rtunion int */
1156 pair_p note_flds
= NULL
;
1159 for (c
= 0; c
<= NOTE_INSN_MAX
; c
++)
1164 case NOTE_INSN_DELETED_LABEL
:
1165 case NOTE_INSN_DELETED_DEBUG_LABEL
:
1166 note_flds
= create_field (note_flds
, &string_type
, "rt_str");
1169 case NOTE_INSN_BLOCK_BEG
:
1170 case NOTE_INSN_BLOCK_END
:
1171 note_flds
= create_field (note_flds
, tree_tp
, "rt_tree");
1174 case NOTE_INSN_VAR_LOCATION
:
1175 case NOTE_INSN_CALL_ARG_LOCATION
:
1176 note_flds
= create_field (note_flds
, rtx_tp
, "rt_rtx");
1180 note_flds
= create_field (note_flds
, scalar_tp
, "rt_int");
1183 /* NOTE_INSN_MAX is used as the default field for line
1185 if (c
== NOTE_INSN_MAX
)
1187 create_string_option (nodot
, "default", "");
1190 create_string_option (nodot
, "tag", note_insn_name
[c
]);
1192 note_union_tp
= new_structure ("rtx_def_note_subunion", TYPE_UNION
,
1193 &lexer_line
, note_flds
, NULL
, NULL
);
1195 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1198 sym_flds
= create_field (NULL
, tree_tp
, "rt_tree");
1199 sym_flds
->opt
= create_string_option (nodot
, "default", "");
1200 sym_flds
= create_field (sym_flds
, constant_tp
, "rt_constant");
1201 sym_flds
->opt
= create_string_option (nodot
, "tag", "1");
1202 symbol_union_tp
= new_structure ("rtx_def_symbol_subunion", TYPE_UNION
,
1203 &lexer_line
, sym_flds
, NULL
, NULL
);
1205 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1207 pair_p subfields
= NULL
;
1208 size_t aindex
, nmindex
;
1213 for (aindex
= 0; aindex
< strlen (rtx_format
[i
]); aindex
++)
1216 const char *subname
;
1218 switch (rtx_format
[i
][aindex
])
1229 if (i
== MEM
&& aindex
== 1)
1230 t
= mem_attrs_tp
, subname
= "rt_mem";
1231 else if (i
== JUMP_INSN
&& aindex
== 8)
1232 t
= rtx_tp
, subname
= "rt_rtx";
1233 else if (i
== CODE_LABEL
&& aindex
== 5)
1234 t
= scalar_tp
, subname
= "rt_int";
1235 else if (i
== CODE_LABEL
&& aindex
== 4)
1236 t
= rtx_tp
, subname
= "rt_rtx";
1237 else if (i
== LABEL_REF
&& (aindex
== 1 || aindex
== 2))
1238 t
= rtx_tp
, subname
= "rt_rtx";
1239 else if (i
== NOTE
&& aindex
== 4)
1240 t
= note_union_tp
, subname
= "";
1241 else if (i
== NOTE
&& aindex
== 5)
1242 t
= scalar_tp
, subname
= "rt_int";
1243 else if (i
== NOTE
&& aindex
>= 7)
1244 t
= scalar_tp
, subname
= "rt_int";
1245 else if (i
== ADDR_DIFF_VEC
&& aindex
== 4)
1246 t
= scalar_tp
, subname
= "rt_int";
1247 else if (i
== VALUE
&& aindex
== 0)
1248 t
= scalar_tp
, subname
= "rt_int";
1249 else if (i
== DEBUG_EXPR
&& aindex
== 0)
1250 t
= tree_tp
, subname
= "rt_tree";
1251 else if (i
== REG
&& aindex
== 1)
1252 t
= scalar_tp
, subname
= "rt_int";
1253 else if (i
== REG
&& aindex
== 2)
1254 t
= reg_attrs_tp
, subname
= "rt_reg";
1255 else if (i
== SCRATCH
&& aindex
== 0)
1256 t
= scalar_tp
, subname
= "rt_int";
1257 else if (i
== SYMBOL_REF
&& aindex
== 1)
1258 t
= scalar_tp
, subname
= "rt_int";
1259 else if (i
== SYMBOL_REF
&& aindex
== 2)
1260 t
= symbol_union_tp
, subname
= "";
1261 else if (i
== JUMP_TABLE_DATA
&& aindex
>= 5)
1262 t
= scalar_tp
, subname
= "rt_int";
1263 else if (i
== BARRIER
&& aindex
>= 3)
1264 t
= scalar_tp
, subname
= "rt_int";
1265 else if (i
== ENTRY_VALUE
&& aindex
== 0)
1266 t
= rtx_tp
, subname
= "rt_rtx";
1271 "rtx type `%s' has `0' in position %lu, can't handle",
1272 rtx_name
[i
], (unsigned long) aindex
);
1294 subname
= "rt_rtvec";
1299 subname
= "rt_tree";
1310 "rtx type `%s' has `%c' in position %lu, can't handle",
1311 rtx_name
[i
], rtx_format
[i
][aindex
],
1312 (unsigned long) aindex
);
1318 subfields
= create_field (subfields
, t
,
1319 xasprintf (".fld[%lu].%s",
1320 (unsigned long) aindex
,
1322 subfields
->opt
= nodot
;
1323 if (t
== note_union_tp
)
1325 create_string_option (subfields
->opt
, "desc",
1327 if (t
== symbol_union_tp
)
1329 create_string_option (subfields
->opt
, "desc",
1330 "CONSTANT_POOL_ADDRESS_P (&%0)");
1333 if (i
== SYMBOL_REF
)
1335 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1337 type_p field_tp
= find_structure ("block_symbol", TYPE_STRUCT
);
1339 = create_optional_field (subfields
, field_tp
, "block_sym",
1340 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1343 sname
= xasprintf ("rtx_def_%s", rtx_name
[i
]);
1344 substruct
= new_structure (sname
, TYPE_STRUCT
, &lexer_line
, subfields
,
1347 ftag
= xstrdup (rtx_name
[i
]);
1348 for (nmindex
= 0; nmindex
< strlen (ftag
); nmindex
++)
1349 ftag
[nmindex
] = TOUPPER (ftag
[nmindex
]);
1350 flds
= create_field (flds
, substruct
, "");
1351 flds
->opt
= create_string_option (nodot
, "tag", ftag
);
1353 return new_structure ("rtx_def_subunion", TYPE_UNION
, &lexer_line
, flds
,
1357 /* Handle `special("tree_exp")'. This is a special case for
1358 field `operands' of struct tree_exp, which although it claims to contain
1359 pointers to trees, actually sometimes contains pointers to RTL too.
1360 Passed T, the old type of the field, and OPT its options. Returns
1361 a new type for the field. */
1364 adjust_field_tree_exp (type_p t
, options_p opt ATTRIBUTE_UNUSED
)
1369 if (t
->kind
!= TYPE_ARRAY
)
1371 error_at_line (&lexer_line
,
1372 "special `tree_exp' must be applied to an array");
1373 return &string_type
;
1376 nodot
= create_string_option (NULL
, "dot", "");
1378 flds
= create_field (NULL
, t
, "");
1379 flds
->opt
= create_string_option (nodot
, "length",
1380 "TREE_OPERAND_LENGTH ((tree) &%0)");
1381 flds
->opt
= create_string_option (flds
->opt
, "default", "");
1383 return new_structure ("tree_exp_subunion", TYPE_UNION
, &lexer_line
, flds
,
1387 /* Perform any special processing on a type T, about to become the type
1388 of a field. Return the appropriate type for the field.
1390 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1391 - Similarly for arrays of pointer-to-char;
1392 - Converts structures for which a parameter is provided to
1394 - Handles "special" options.
1398 adjust_field_type (type_p t
, options_p opt
)
1401 const int pointer_p
= t
->kind
== TYPE_POINTER
;
1402 type_p params
[NUM_PARAM
];
1406 for (i
= 0; i
< NUM_PARAM
; i
++)
1409 for (; opt
; opt
= opt
->next
)
1410 if (strcmp (opt
->name
, "length") == 0)
1413 error_at_line (&lexer_line
, "duplicate `%s' option", opt
->name
);
1414 if (t
->u
.p
->kind
== TYPE_SCALAR
|| t
->u
.p
->kind
== TYPE_STRING
)
1416 error_at_line (&lexer_line
,
1417 "option `%s' may not be applied to "
1418 "arrays of atomic types", opt
->name
);
1422 else if ((strcmp (opt
->name
, "param_is") == 0
1423 || (strncmp (opt
->name
, "param", 5) == 0
1424 && ISDIGIT (opt
->name
[5])
1425 && strcmp (opt
->name
+ 6, "_is") == 0))
1426 && opt
->kind
== OPTION_TYPE
)
1428 int num
= ISDIGIT (opt
->name
[5]) ? opt
->name
[5] - '0' : 0;
1430 if (!union_or_struct_p (t
)
1431 && (t
->kind
!= TYPE_POINTER
|| !union_or_struct_p (t
->u
.p
)))
1433 error_at_line (&lexer_line
,
1434 "option `%s' may only be applied to structures or structure pointers",
1440 if (params
[num
] != NULL
)
1441 error_at_line (&lexer_line
, "duplicate `%s' option", opt
->name
);
1442 if (!ISDIGIT (opt
->name
[5]))
1443 params
[num
] = create_pointer (opt
->info
.type
);
1445 params
[num
] = opt
->info
.type
;
1447 else if (strcmp (opt
->name
, "special") == 0
1448 && opt
->kind
== OPTION_STRING
)
1450 const char *special_name
= opt
->info
.string
;
1451 if (strcmp (special_name
, "tree_exp") == 0)
1452 t
= adjust_field_tree_exp (t
, opt
);
1453 else if (strcmp (special_name
, "rtx_def") == 0)
1454 t
= adjust_field_rtx_def (t
, opt
);
1456 error_at_line (&lexer_line
, "unknown special `%s'", special_name
);
1465 realt
= find_param_structure (t
, params
);
1466 t
= pointer_p
? create_pointer (realt
) : realt
;
1470 && pointer_p
&& t
->u
.p
->kind
== TYPE_SCALAR
&& t
->u
.p
->u
.scalar_is_char
)
1471 return &string_type
;
1472 if (t
->kind
== TYPE_ARRAY
&& t
->u
.a
.p
->kind
== TYPE_POINTER
1473 && t
->u
.a
.p
->u
.p
->kind
== TYPE_SCALAR
1474 && t
->u
.a
.p
->u
.p
->u
.scalar_is_char
)
1475 return create_array (&string_type
, t
->u
.a
.len
);
1481 static void set_gc_used_type (type_p
, enum gc_used_enum
, type_p
*,
1483 static void set_gc_used (pair_p
);
1485 /* Handle OPT for set_gc_used_type. */
1488 process_gc_options (options_p opt
, enum gc_used_enum level
, int *maybe_undef
,
1489 int *pass_param
, int *length
, int *skip
,
1493 for (o
= opt
; o
; o
= o
->next
)
1494 if (strcmp (o
->name
, "ptr_alias") == 0 && level
== GC_POINTED_TO
1495 && o
->kind
== OPTION_TYPE
)
1496 set_gc_used_type (o
->info
.type
,
1497 GC_POINTED_TO
, NULL
);
1498 else if (strcmp (o
->name
, "maybe_undef") == 0)
1500 else if (strcmp (o
->name
, "use_params") == 0)
1502 else if (strcmp (o
->name
, "length") == 0)
1504 else if (strcmp (o
->name
, "skip") == 0)
1506 else if (strcmp (o
->name
, "nested_ptr") == 0
1507 && o
->kind
== OPTION_NESTED
)
1508 *nested_ptr
= ((const struct nested_ptr_data
*) o
->info
.nested
)->type
;
1512 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1514 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1515 are set to GC_UNUSED. Otherwise, an error is emitted for
1516 TYPE_UNDEFINED types. This is used to support user-defined
1517 template types with non-type arguments.
1519 For instance, when we parse a template type with enum arguments
1520 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1521 artificial fields for 'MyType', one for 'AnotherType', the other
1522 one for 'EnumValue'.
1524 At the time that we parse this type we don't know that 'EnumValue'
1525 is really an enum value, so the parser creates a TYPE_UNDEFINED
1526 type for it. Since 'EnumValue' is never resolved to a known
1527 structure, it will stay with TYPE_UNDEFINED.
1529 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1530 'EnumValue'. Generating marking code for it would cause
1531 compilation failures since the marking routines assumes that
1532 'EnumValue' is a type. */
1535 set_gc_used_type (type_p t
, enum gc_used_enum level
, type_p param
[NUM_PARAM
],
1536 bool allow_undefined_types
)
1538 if (t
->gc_used
>= level
)
1547 case TYPE_USER_STRUCT
:
1552 bool allow_undefined_field_types
= (t
->kind
== TYPE_USER_STRUCT
);
1554 process_gc_options (t
->u
.s
.opt
, level
, &dummy
, &dummy
, &dummy
, &dummy
,
1557 if (t
->u
.s
.base_class
)
1558 set_gc_used_type (t
->u
.s
.base_class
, level
, param
,
1559 allow_undefined_types
);
1560 /* Anything pointing to a base class might actually be pointing
1562 for (type_p subclass
= t
->u
.s
.first_subclass
; subclass
;
1563 subclass
= subclass
->u
.s
.next_sibling_class
)
1564 set_gc_used_type (subclass
, level
, param
,
1565 allow_undefined_types
);
1567 FOR_ALL_INHERITED_FIELDS(t
, f
)
1569 int maybe_undef
= 0;
1573 type_p nested_ptr
= NULL
;
1574 process_gc_options (f
->opt
, level
, &maybe_undef
, &pass_param
,
1575 &length
, &skip
, &nested_ptr
);
1577 if (nested_ptr
&& f
->type
->kind
== TYPE_POINTER
)
1578 set_gc_used_type (nested_ptr
, GC_POINTED_TO
,
1579 pass_param
? param
: NULL
);
1580 else if (length
&& f
->type
->kind
== TYPE_POINTER
)
1581 set_gc_used_type (f
->type
->u
.p
, GC_USED
, NULL
);
1582 else if (maybe_undef
&& f
->type
->kind
== TYPE_POINTER
)
1583 set_gc_used_type (f
->type
->u
.p
, GC_MAYBE_POINTED_TO
, NULL
);
1584 else if (pass_param
&& f
->type
->kind
== TYPE_POINTER
&& param
)
1585 set_gc_used_type (find_param_structure (f
->type
->u
.p
, param
),
1586 GC_POINTED_TO
, NULL
);
1588 ; /* target type is not used through this field */
1590 set_gc_used_type (f
->type
, GC_USED
, pass_param
? param
: NULL
,
1591 allow_undefined_field_types
);
1596 case TYPE_UNDEFINED
:
1597 if (level
> GC_UNUSED
)
1599 if (!allow_undefined_types
)
1600 error_at_line (&t
->u
.s
.line
, "undefined type `%s'", t
->u
.s
.tag
);
1601 t
->gc_used
= GC_UNUSED
;
1606 set_gc_used_type (t
->u
.p
, GC_POINTED_TO
, NULL
);
1610 set_gc_used_type (t
->u
.a
.p
, GC_USED
, param
);
1613 case TYPE_LANG_STRUCT
:
1614 for (t
= t
->u
.s
.lang_struct
; t
; t
= t
->next
)
1615 set_gc_used_type (t
, level
, param
);
1618 case TYPE_PARAM_STRUCT
:
1621 for (i
= 0; i
< NUM_PARAM
; i
++)
1622 if (t
->u
.param_struct
.param
[i
] != 0)
1623 set_gc_used_type (t
->u
.param_struct
.param
[i
], GC_USED
, NULL
);
1625 if (t
->u
.param_struct
.stru
->gc_used
== GC_POINTED_TO
)
1626 level
= GC_POINTED_TO
;
1629 t
->u
.param_struct
.stru
->gc_used
= GC_UNUSED
;
1630 set_gc_used_type (t
->u
.param_struct
.stru
, level
,
1631 t
->u
.param_struct
.param
);
1639 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1642 set_gc_used (pair_p variables
)
1646 for (p
= variables
; p
; p
= p
->next
)
1648 set_gc_used_type (p
->type
, GC_USED
, NULL
);
1651 if (verbosity_level
>= 2)
1652 printf ("%s used %d GTY-ed variables\n", progname
, nbvars
);
1655 /* File mapping routines. For each input file, there is one output .c file
1656 (but some output files have many input files), and there is one .h file
1657 for the whole build. */
1659 /* Output file handling. */
1661 /* Create and return an outf_p for a new file for NAME, to be called
1665 create_file (const char *name
, const char *oname
)
1667 static const char *const hdr
[] = {
1668 " Copyright (C) 2004-2013 Free Software Foundation, Inc.\n",
1670 "This file is part of GCC.\n",
1672 "GCC is free software; you can redistribute it and/or modify it under\n",
1673 "the terms of the GNU General Public License as published by the Free\n",
1674 "Software Foundation; either version 3, or (at your option) any later\n",
1677 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1678 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1679 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1680 "for more details.\n",
1682 "You should have received a copy of the GNU General Public License\n",
1683 "along with GCC; see the file COPYING3. If not see\n",
1684 "<http://www.gnu.org/licenses/>. */\n",
1686 "/* This file is machine generated. Do not edit. */\n"
1691 gcc_assert (name
!= NULL
);
1692 gcc_assert (oname
!= NULL
);
1693 f
= XCNEW (struct outf
);
1694 f
->next
= output_files
;
1698 oprintf (f
, "/* Type information for %s.\n", name
);
1699 for (i
= 0; i
< ARRAY_SIZE (hdr
); i
++)
1700 oprintf (f
, "%s", hdr
[i
]);
1704 /* Print, like fprintf, to O.
1705 N.B. You might think this could be implemented more efficiently
1706 with vsnprintf(). Unfortunately, there are C libraries that
1707 provide that function but without the C99 semantics for its return
1708 value, making it impossible to know how much space is required. */
1710 oprintf (outf_p o
, const char *format
, ...)
1716 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1721 va_start (ap
, format
);
1722 slength
= vasprintf (&s
, format
, ap
);
1723 if (s
== NULL
|| (int) slength
< 0)
1724 fatal ("out of memory");
1727 if (o
->bufused
+ slength
> o
->buflength
)
1729 size_t new_len
= o
->buflength
;
1736 while (o
->bufused
+ slength
>= new_len
);
1737 o
->buf
= XRESIZEVEC (char, o
->buf
, new_len
);
1738 o
->buflength
= new_len
;
1740 memcpy (o
->buf
+ o
->bufused
, s
, slength
);
1741 o
->bufused
+= slength
;
1745 /* Open the global header file and the language-specific header files. */
1748 open_base_files (void)
1752 if (nb_plugin_files
> 0 && plugin_files
)
1755 header_file
= create_file ("GCC", "gtype-desc.h");
1757 base_files
= XNEWVEC (outf_p
, num_lang_dirs
);
1759 for (i
= 0; i
< num_lang_dirs
; i
++)
1760 base_files
[i
] = create_file (lang_dir_names
[i
],
1761 xasprintf ("gtype-%s.h", lang_dir_names
[i
]));
1763 /* gtype-desc.c is a little special, so we create it here. */
1765 /* The order of files here matters very much. */
1766 static const char *const ifiles
[] = {
1767 "config.h", "system.h", "coretypes.h", "tm.h",
1768 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1769 "tree.h", "rtl.h", "function.h", "insn-config.h", "expr.h",
1770 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1771 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1772 "pointer-set.h", "hash-table.h", "vec.h", "ggc.h", "basic-block.h",
1773 "tree-ssa-alias.h", "internal-fn.h", "gimple-fold.h", "tree-eh.h",
1774 "gimple-expr.h", "is-a.h",
1775 "gimple.h", "gimple-iterator.h", "gimple-ssa.h", "tree-cfg.h",
1776 "tree-phinodes.h", "ssa-iterators.h", "stringpool.h", "tree-ssanames.h",
1777 "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h", "tree-ssa-loop-manip.h",
1778 "tree-ssa-loop-niter.h", "tree-into-ssa.h", "tree-dfa.h",
1779 "tree-ssa.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1780 "except.h", "output.h", "cfgloop.h",
1781 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h",
1782 "ipa-inline.h", "dwarf2out.h", NULL
1784 const char *const *ifp
;
1785 outf_p gtype_desc_c
;
1787 gtype_desc_c
= create_file ("GCC", "gtype-desc.c");
1788 for (ifp
= ifiles
; *ifp
; ifp
++)
1789 oprintf (gtype_desc_c
, "#include \"%s\"\n", *ifp
);
1791 /* Make sure we handle "cfun" specially. */
1792 oprintf (gtype_desc_c
, "\n/* See definition in function.h. */\n");
1793 oprintf (gtype_desc_c
, "#undef cfun\n");
1795 oprintf (gtype_desc_c
,
1797 "/* Types with a \"gcc::\" namespace have it stripped\n"
1798 " during gengtype parsing. Provide a \"using\" directive\n"
1799 " to ensure that the fully-qualified types are found. */\n"
1800 "using namespace gcc;\n");
1804 /* For INPF an input file, return the real basename of INPF, with all
1805 the directory components skipped. */
1808 get_file_realbasename (const input_file
*inpf
)
1810 return lbasename (get_input_file_name (inpf
));
1813 /* For INPF a filename, return the relative path to INPF from
1814 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1817 get_file_srcdir_relative_path (const input_file
*inpf
)
1819 const char *f
= get_input_file_name (inpf
);
1820 if (strlen (f
) > srcdir_len
1821 && IS_DIR_SEPARATOR (f
[srcdir_len
])
1822 && strncmp (f
, srcdir
, srcdir_len
) == 0)
1823 return f
+ srcdir_len
+ 1;
1828 /* For INPF an input_file, return the relative path to INPF from
1829 $(srcdir) if the latter is a prefix in INPF, or the real basename
1830 of INPF otherwise. */
1833 get_file_basename (const input_file
*inpf
)
1835 const char *srcdir_path
= get_file_srcdir_relative_path (inpf
);
1837 return (srcdir_path
!= NULL
) ? srcdir_path
: get_file_realbasename (inpf
);
1840 /* For F a filename, return the lang_dir_names relative index of the language
1841 directory that is a prefix in F, if any, -1 otherwise. */
1844 get_prefix_langdir_index (const char *f
)
1846 size_t f_len
= strlen (f
);
1849 for (lang_index
= 0; lang_index
< num_lang_dirs
; lang_index
++)
1851 const char *langdir
= lang_dir_names
[lang_index
];
1852 size_t langdir_len
= strlen (langdir
);
1854 if (f_len
> langdir_len
1855 && IS_DIR_SEPARATOR (f
[langdir_len
])
1856 && memcmp (f
, langdir
, langdir_len
) == 0)
1863 /* For INPF an input file, return the name of language directory where
1864 F is located, if any, NULL otherwise. */
1867 get_file_langdir (const input_file
*inpf
)
1869 /* Get the relative path to INPF from $(srcdir) and find the
1870 language by comparing the prefix with language directory names.
1871 If INPF is not even srcdir relative, no point in looking
1875 const char *srcdir_relative_path
= get_file_srcdir_relative_path (inpf
);
1878 if (!srcdir_relative_path
)
1881 lang_index
= get_prefix_langdir_index (srcdir_relative_path
);
1882 if (lang_index
< 0 && strncmp (srcdir_relative_path
, "c-family", 8) == 0)
1884 else if (lang_index
>= 0)
1885 r
= lang_dir_names
[lang_index
];
1892 /* The gt- output file name for INPF. */
1895 get_file_gtfilename (const input_file
*inpf
)
1897 /* Cook up an initial version of the gt- file name from the file real
1898 basename and the language name, if any. */
1900 const char *basename
= get_file_realbasename (inpf
);
1901 const char *langdir
= get_file_langdir (inpf
);
1904 (langdir
? xasprintf ("gt-%s-%s", langdir
, basename
)
1905 : xasprintf ("gt-%s", basename
));
1907 /* Then replace all non alphanumerics characters by '-' and change the
1908 extension to ".h". We expect the input filename extension was at least
1909 one character long. */
1913 for (; *s
!= '.'; s
++)
1914 if (!ISALNUM (*s
) && *s
!= '-')
1917 memcpy (s
, ".h", sizeof (".h"));
1922 /* Each input_file has its associated output file outf_p. The
1923 association is computed by the function
1924 get_output_file_with_visibility. The associated file is cached
1925 inside input_file in its inpoutf field, so is really computed only
1926 once. Associated output file paths (i.e. output_name-s) are
1927 computed by a rule based regexp machinery, using the files_rules
1928 array of struct file_rule_st. A for_name is also computed, giving
1929 the source file name for which the output_file is generated; it is
1930 often the last component of the input_file path. */
1934 Regexpr machinery to compute the output_name and for_name-s of each
1935 input_file. We have a sequence of file rules which gives the POSIX
1936 extended regular expression to match an input file path, and two
1937 transformed strings for the corresponding output_name and the
1938 corresponding for_name. The transformed string contain dollars: $0
1939 is replaced by the entire match, $1 is replaced by the substring
1940 matching the first parenthesis in the regexp, etc. And $$ is replaced
1941 by a single verbatim dollar. The rule order is important. The
1942 general case is last, and the particular cases should come before.
1943 An action routine can, when needed, update the out_name & for_name
1944 and/or return the appropriate output file. It is invoked only when a
1945 rule is triggered. When a rule is triggered, the output_name and
1946 for_name are computed using their transform string in while $$, $0,
1947 $1, ... are suitably replaced. If there is an action, it is called.
1948 In some few cases, the action can directly return the outf_p, but
1949 usually it just updates the output_name and for_name so should free
1950 them before replacing them. The get_output_file_with_visibility
1951 function creates an outf_p only once per each output_name, so it
1952 scans the output_files list for previously seen output file names.
1955 /* Signature of actions in file rules. */
1956 typedef outf_p (frul_actionrout_t
) (input_file
*, char**, char**);
1959 struct file_rule_st
{
1960 const char* frul_srcexpr
; /* Source string for regexp. */
1961 int frul_rflags
; /* Flags passed to regcomp, usually
1963 regex_t
* frul_re
; /* Compiled regular expression
1964 obtained by regcomp. */
1965 const char* frul_tr_out
; /* Transformation string for making
1966 * the output_name, with $1 ... $9 for
1967 * subpatterns and $0 for the whole
1968 * matched filename. */
1969 const char* frul_tr_for
; /* Tranformation string for making the
1971 frul_actionrout_t
* frul_action
; /* The action, if non null, is
1972 * called once the rule matches, on
1973 * the transformed out_name &
1974 * for_name. It could change them
1975 * and/or give the output file. */
1978 /* File rule action handling *.h files. */
1979 static outf_p
header_dot_h_frul (input_file
*, char**, char**);
1981 /* File rule action handling *.c files. */
1982 static outf_p
source_dot_c_frul (input_file
*, char**, char**);
1984 #define NULL_REGEX (regex_t*)0
1986 /* The prefix in our regexp-s matching the directory. */
1987 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1989 #define NULL_FRULACT (frul_actionrout_t*)0
1991 /* The array of our rules governing file name generation. Rules order
1992 matters, so change with extreme care! */
1994 struct file_rule_st files_rules
[] = {
1995 /* The general rule assumes that files in subdirectories belong to a
1996 particular front-end, and files not in subdirectories are shared.
1997 The following rules deal with exceptions - files that are in
1998 subdirectories and yet are shared, and files that are top-level,
1999 but are not shared. */
2001 /* the c-family/ source directory is special. */
2002 { DIR_PREFIX_REGEX
"c-family/([[:alnum:]_-]*)\\.c$",
2003 REG_EXTENDED
, NULL_REGEX
,
2004 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT
},
2006 { DIR_PREFIX_REGEX
"c-family/([[:alnum:]_-]*)\\.h$",
2007 REG_EXTENDED
, NULL_REGEX
,
2008 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT
},
2010 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
2011 { DIR_PREFIX_REGEX
"c/c-lang\\.h$",
2012 REG_EXTENDED
, NULL_REGEX
, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT
},
2014 { DIR_PREFIX_REGEX
"c/c-tree\\.h$",
2015 REG_EXTENDED
, NULL_REGEX
, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT
},
2017 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
2018 { DIR_PREFIX_REGEX
"cp/cp-tree\\.h$",
2019 REG_EXTENDED
, NULL_REGEX
,
2020 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT
},
2022 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
2023 { DIR_PREFIX_REGEX
"cp/decl\\.[ch]$",
2024 REG_EXTENDED
, NULL_REGEX
,
2025 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT
},
2027 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
2028 { DIR_PREFIX_REGEX
"cp/name-lookup\\.h$",
2029 REG_EXTENDED
, NULL_REGEX
,
2030 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT
},
2032 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
2033 { DIR_PREFIX_REGEX
"cp/parser\\.h$",
2034 REG_EXTENDED
, NULL_REGEX
,
2035 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT
},
2037 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
2038 { DIR_PREFIX_REGEX
"objc/objc-act\\.h$",
2039 REG_EXTENDED
, NULL_REGEX
,
2040 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT
},
2042 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
2043 { DIR_PREFIX_REGEX
"objc/objc-map\\.h$",
2044 REG_EXTENDED
, NULL_REGEX
,
2045 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT
},
2047 /* General cases. For header *.h and source *.c or *.cc files, we
2048 * need special actions to handle the language. */
2050 /* Source *.c files are using get_file_gtfilename to compute their
2051 output_name and get_file_basename to compute their for_name
2052 through the source_dot_c_frul action. */
2053 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.c$",
2054 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.c", source_dot_c_frul
},
2056 /* Source *.cc files are using get_file_gtfilename to compute their
2057 output_name and get_file_basename to compute their for_name
2058 through the source_dot_c_frul action. */
2059 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.cc$",
2060 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.cc", source_dot_c_frul
},
2062 /* Common header files get "gtype-desc.c" as their output_name,
2063 * while language specific header files are handled specially. So
2064 * we need the header_dot_h_frul action. */
2065 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.h$",
2066 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.h", header_dot_h_frul
},
2068 { DIR_PREFIX_REGEX
"([[:alnum:]_-]*)\\.in$",
2069 REG_EXTENDED
, NULL_REGEX
, "gt-$3.h", "$3.in", NULL_FRULACT
},
2071 /* Mandatory null last entry signaling end of rules. */
2072 {NULL
, 0, NULL_REGEX
, NULL
, NULL
, NULL_FRULACT
}
2075 /* Special file rules action for handling *.h header files. It gives
2076 "gtype-desc.c" for common headers and corresponding output
2077 files for language-specific header files. */
2079 header_dot_h_frul (input_file
* inpf
, char**poutname
,
2080 char**pforname ATTRIBUTE_UNUSED
)
2082 const char *basename
= 0;
2084 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2085 (void*) inpf
, get_input_file_name (inpf
),
2086 *poutname
, *pforname
);
2087 basename
= get_file_basename (inpf
);
2088 lang_index
= get_prefix_langdir_index (basename
);
2089 DBGPRINTF ("basename %s lang_index %d", basename
, lang_index
);
2091 if (lang_index
>= 0)
2093 /* The header is language specific. Given output_name &
2094 for_name remains unchanged. The base_files array gives the
2096 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2097 (void*) base_files
[lang_index
],
2098 (base_files
[lang_index
])->name
);
2099 return base_files
[lang_index
];
2103 /* The header is common to all front-end languages. So
2104 output_name is "gtype-desc.c" file. The calling function
2105 get_output_file_with_visibility will find its outf_p. */
2107 *poutname
= xstrdup ("gtype-desc.c");
2108 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2109 get_input_file_name (inpf
));
2115 /* Special file rules action for handling *.c source files using
2116 * get_file_gtfilename to compute their output_name and
2117 * get_file_basename to compute their for_name. The output_name is
2118 * gt-<LANG>-<BASE>.h for language specific source files, and
2119 * gt-<BASE>.h for common source files. */
2121 source_dot_c_frul (input_file
* inpf
, char**poutname
, char**pforname
)
2123 char *newbasename
= CONST_CAST (char*, get_file_basename (inpf
));
2124 char *newoutname
= CONST_CAST (char*, get_file_gtfilename (inpf
));
2125 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2126 (void*) inpf
, get_input_file_name (inpf
),
2127 *poutname
, *pforname
);
2128 DBGPRINTF ("newoutname %s", newoutname
);
2129 DBGPRINTF ("newbasename %s", newbasename
);
2132 *poutname
= newoutname
;
2133 *pforname
= newbasename
;
2137 /* Utility function for get_output_file_with_visibility which returns
2138 * a malloc-ed substituted string using TRS on matching of the FILNAM
2139 * file name, using the PMATCH array. */
2141 matching_file_name_substitute (const char *filnam
, regmatch_t pmatch
[10],
2144 struct obstack str_obstack
;
2146 char *rawstr
= NULL
;
2147 const char *pt
= NULL
;
2148 DBGPRINTF ("filnam %s", filnam
);
2149 obstack_init (&str_obstack
);
2150 for (pt
= trs
; *pt
; pt
++) {
2156 /* A double dollar $$ is substituted by a single verbatim
2157 dollar, but who really uses dollar signs in file
2159 obstack_1grow (&str_obstack
, '$');
2161 else if (ISDIGIT (pt
[1]))
2163 /* Handle $0 $1 ... $9 by appropriate substitution. */
2164 int dolnum
= pt
[1] - '0';
2165 int so
= pmatch
[dolnum
].rm_so
;
2166 int eo
= pmatch
[dolnum
].rm_eo
;
2167 DBGPRINTF ("so=%d eo=%d dolnum=%d", so
, eo
, dolnum
);
2168 if (so
>=0 && eo
>=so
)
2169 obstack_grow (&str_obstack
, filnam
+ so
, eo
- so
);
2173 /* This can happen only when files_rules is buggy! */
2176 /* Always skip the character after the dollar. */
2180 obstack_1grow (&str_obstack
, c
);
2182 obstack_1grow (&str_obstack
, '\0');
2183 rawstr
= XOBFINISH (&str_obstack
, char *);
2184 str
= xstrdup (rawstr
);
2185 obstack_free (&str_obstack
, NULL
);
2186 DBGPRINTF ("matched replacement %s", str
);
2192 /* An output file, suitable for definitions, that can see declarations
2193 made in INPF and is linked into every language that uses INPF.
2194 Since the result is cached inside INPF, that argument cannot be
2195 declared constant, but is "almost" constant. */
2198 get_output_file_with_visibility (input_file
*inpf
)
2201 char *for_name
= NULL
;
2202 char *output_name
= NULL
;
2203 const char* inpfname
;
2205 /* This can happen when we need a file with visibility on a
2206 structure that we've never seen. We have to just hope that it's
2207 globally visible. */
2209 inpf
= system_h_file
;
2211 /* The result is cached in INPF, so return it if already known. */
2213 return inpf
->inpoutf
;
2215 /* In plugin mode, return NULL unless the input_file is one of the
2220 for (i
= 0; i
< nb_plugin_files
; i
++)
2221 if (inpf
== plugin_files
[i
])
2223 inpf
->inpoutf
= plugin_output
;
2224 return plugin_output
;
2230 inpfname
= get_input_file_name (inpf
);
2232 /* Try each rule in sequence in files_rules until one is triggered. */
2235 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2236 (void*) inpf
, inpfname
);
2238 for (; files_rules
[rulix
].frul_srcexpr
!= NULL
; rulix
++)
2240 DBGPRINTF ("rulix#%d srcexpr %s",
2241 rulix
, files_rules
[rulix
].frul_srcexpr
);
2243 if (!files_rules
[rulix
].frul_re
)
2245 /* Compile the regexpr lazily. */
2247 files_rules
[rulix
].frul_re
= XCNEW (regex_t
);
2248 err
= regcomp (files_rules
[rulix
].frul_re
,
2249 files_rules
[rulix
].frul_srcexpr
,
2250 files_rules
[rulix
].frul_rflags
);
2253 /* The regular expression compilation fails only when
2254 file_rules is buggy. */
2262 /* Match the regexpr and trigger the rule if matched. */
2264 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2266 regmatch_t pmatch
[10];
2267 memset (pmatch
, 0, sizeof (pmatch
));
2268 if (!regexec (files_rules
[rulix
].frul_re
,
2269 inpfname
, 10, pmatch
, 0))
2271 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2272 (void*) inpf
, inpfname
, rulix
,
2273 files_rules
[rulix
].frul_srcexpr
);
2275 matching_file_name_substitute (inpfname
, pmatch
,
2276 files_rules
[rulix
].frul_tr_for
);
2277 DBGPRINTF ("for_name %s", for_name
);
2279 matching_file_name_substitute (inpfname
, pmatch
,
2280 files_rules
[rulix
].frul_tr_out
);
2281 DBGPRINTF ("output_name %s", output_name
);
2282 if (files_rules
[rulix
].frul_action
)
2284 /* Invoke our action routine. */
2286 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2287 rulix
, output_name
, for_name
);
2289 (files_rules
[rulix
].frul_action
) (inpf
,
2290 &output_name
, &for_name
);
2291 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2292 rulix
, (void*)of
, output_name
, for_name
);
2293 /* If the action routine returned something, give it back
2294 immediately and cache it in inpf. */
2301 /* The rule matched, and had no action, or that action did
2302 not return any output file but could have changed the
2303 output_name or for_name. We break out of the loop on the
2309 /* The regexpr did not match. */
2310 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2311 rulix
, inpfname
, files_rules
[rulix
].frul_srcexpr
);
2317 if (!output_name
|| !for_name
)
2319 /* This should not be possible, and could only happen if the
2320 files_rules is incomplete or buggy. */
2321 fatal ("failed to compute output name for %s", inpfname
);
2324 /* Look through to see if we've ever seen this output filename
2325 before. If found, cache the result in inpf. */
2326 for (r
= output_files
; r
; r
= r
->next
)
2327 if (filename_cmp (r
->name
, output_name
) == 0)
2330 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r
,
2331 output_name
, for_name
);
2335 /* If not found, create it, and cache it in inpf. */
2336 r
= create_file (for_name
, output_name
);
2338 gcc_assert (r
&& r
->name
);
2339 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r
,
2340 output_name
, for_name
);
2347 /* The name of an output file, suitable for definitions, that can see
2348 declarations made in INPF and is linked into every language that
2352 get_output_file_name (input_file
* inpf
)
2354 outf_p o
= get_output_file_with_visibility (inpf
);
2360 /* Check if existing file is equal to the in memory buffer. */
2363 is_file_equal (outf_p of
)
2365 FILE *newfile
= fopen (of
->name
, "r");
2368 if (newfile
== NULL
)
2372 for (i
= 0; i
< of
->bufused
; i
++)
2375 ch
= fgetc (newfile
);
2376 if (ch
== EOF
|| ch
!= (unsigned char) of
->buf
[i
])
2382 if (equal
&& EOF
!= fgetc (newfile
))
2388 /* Copy the output to its final destination,
2389 but don't unnecessarily change modification times. */
2392 close_output_files (void)
2394 int nbwrittenfiles
= 0;
2397 for (of
= output_files
; of
; of
= of
->next
)
2399 if (!is_file_equal (of
))
2401 FILE *newfile
= NULL
;
2402 char *backupname
= NULL
;
2403 /* Back up the old version of the output file gt-FOO.c as
2404 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2407 backupname
= concat (backup_dir
, "/",
2408 lbasename (of
->name
), "~", NULL
);
2409 if (!access (of
->name
, F_OK
) && rename (of
->name
, backupname
))
2410 fatal ("failed to back up %s as %s: %s",
2411 of
->name
, backupname
, xstrerror (errno
));
2414 newfile
= fopen (of
->name
, "w");
2415 if (newfile
== NULL
)
2416 fatal ("opening output file %s: %s", of
->name
, xstrerror (errno
));
2417 if (fwrite (of
->buf
, 1, of
->bufused
, newfile
) != of
->bufused
)
2418 fatal ("writing output file %s: %s", of
->name
, xstrerror (errno
));
2419 if (fclose (newfile
) != 0)
2420 fatal ("closing output file %s: %s", of
->name
, xstrerror (errno
));
2422 if (verbosity_level
>= 2 && backupname
)
2423 printf ("%s wrote #%-3d %s backed-up in %s\n",
2424 progname
, nbwrittenfiles
, of
->name
, backupname
);
2425 else if (verbosity_level
>= 1)
2426 printf ("%s write #%-3d %s\n", progname
, nbwrittenfiles
, of
->name
);
2431 /* output file remains unchanged. */
2432 if (verbosity_level
>= 2)
2433 printf ("%s keep %s\n", progname
, of
->name
);
2437 of
->bufused
= of
->buflength
= 0;
2439 if (verbosity_level
>= 1)
2440 printf ("%s wrote %d files.\n", progname
, nbwrittenfiles
);
2447 const input_file
* file
;
2451 struct walk_type_data
;
2453 /* For scalars and strings, given the item in 'val'.
2454 For structures, given a pointer to the item in 'val'.
2455 For misc. pointers, given the item in 'val'.
2457 typedef void (*process_field_fn
) (type_p f
, const struct walk_type_data
* p
);
2458 typedef void (*func_name_fn
) (type_p s
, const struct walk_type_data
* p
);
2460 /* Parameters for write_types. */
2462 struct write_types_data
2465 const char *param_prefix
;
2466 const char *subfield_marker_routine
;
2467 const char *marker_routine
;
2468 const char *reorder_note_routine
;
2469 const char *comment
;
2470 int skip_hooks
; /* skip hook generation if non zero */
2473 static void output_escaped_param (struct walk_type_data
*d
,
2474 const char *, const char *);
2475 static void output_mangled_typename (outf_p
, const_type_p
);
2476 static void walk_type (type_p t
, struct walk_type_data
*d
);
2477 static void write_func_for_structure (type_p orig_s
, type_p s
, type_p
*param
,
2478 const struct write_types_data
*wtd
);
2479 static void write_types_process_field
2480 (type_p f
, const struct walk_type_data
*d
);
2481 static void write_types (outf_p output_header
,
2483 type_p param_structs
,
2484 const struct write_types_data
*wtd
);
2485 static void write_types_local_process_field
2486 (type_p f
, const struct walk_type_data
*d
);
2487 static void write_local_func_for_structure
2488 (const_type_p orig_s
, type_p s
, type_p
*param
);
2489 static void write_local (outf_p output_header
,
2490 type_p structures
, type_p param_structs
);
2491 static int contains_scalar_p (type_p t
);
2492 static void put_mangled_filename (outf_p
, const input_file
*);
2493 static void finish_root_table (struct flist
*flp
, const char *pfx
,
2494 const char *tname
, const char *lastname
,
2496 static void write_root (outf_p
, pair_p
, type_p
, const char *, int,
2497 struct fileloc
*, const char *, bool);
2498 static void write_array (outf_p f
, pair_p v
,
2499 const struct write_types_data
*wtd
);
2500 static void write_roots (pair_p
, bool);
2502 /* Parameters for walk_type. */
2504 struct walk_type_data
2506 process_field_fn process_field
;
2511 const char *prev_val
[4];
2514 const struct fileloc
*line
;
2519 const char *reorder_fn
;
2521 bool fn_wants_lvalue
;
2529 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2530 pre-processor identifier to use in a #define directive. This replaces
2531 special characters used in C++ identifiers like '>', '<' and ':' with
2534 If no C++ special characters are found in TYPE_NAME, return
2535 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2536 characters replaced with '_'. In this case, the caller is
2537 responsible for freeing the allocated string. */
2540 filter_type_name (const char *type_name
)
2542 if (strchr (type_name
, '<') || strchr (type_name
, ':'))
2545 char *s
= xstrdup (type_name
);
2546 for (i
= 0; i
< strlen (s
); i
++)
2547 if (s
[i
] == '<' || s
[i
] == '>' || s
[i
] == ':' || s
[i
] == ',')
2556 /* Print a mangled name representing T to OF. */
2559 output_mangled_typename (outf_p of
, const_type_p t
)
2567 case TYPE_UNDEFINED
:
2572 output_mangled_typename (of
, t
->u
.p
);
2582 case TYPE_LANG_STRUCT
:
2583 case TYPE_USER_STRUCT
:
2585 /* For references to classes within an inheritance hierarchy,
2586 only ever reference the ultimate base class, since only
2587 it will have gt_ functions. */
2588 t
= get_ultimate_base_class (t
);
2589 const char *id_for_tag
= filter_type_name (t
->u
.s
.tag
);
2590 oprintf (of
, "%lu%s", (unsigned long) strlen (id_for_tag
),
2592 if (id_for_tag
!= t
->u
.s
.tag
)
2593 free (CONST_CAST (char *, id_for_tag
));
2596 case TYPE_PARAM_STRUCT
:
2599 for (i
= 0; i
< NUM_PARAM
; i
++)
2600 if (t
->u
.param_struct
.param
[i
] != NULL
)
2601 output_mangled_typename (of
, t
->u
.param_struct
.param
[i
]);
2602 output_mangled_typename (of
, t
->u
.param_struct
.stru
);
2610 /* Print PARAM to D->OF processing escapes. D->VAL references the
2611 current object, D->PREV_VAL the object containing the current
2612 object, ONAME is the name of the option and D->LINE is used to
2613 print error messages. */
2616 output_escaped_param (struct walk_type_data
*d
, const char *param
,
2621 for (p
= param
; *p
; p
++)
2623 oprintf (d
->of
, "%c", *p
);
2628 oprintf (d
->of
, "(%s)", d
->prev_val
[2]);
2631 oprintf (d
->of
, "(%s)", d
->prev_val
[0]);
2634 oprintf (d
->of
, "(%s)", d
->prev_val
[1]);
2638 const char *pp
= d
->val
+ strlen (d
->val
);
2639 while (pp
[-1] == ']')
2642 oprintf (d
->of
, "%s", pp
);
2646 error_at_line (d
->line
, "`%s' option contains bad escape %c%c",
2652 get_string_option (options_p opt
, const char *key
)
2654 for (; opt
; opt
= opt
->next
)
2655 if (strcmp (opt
->name
, key
) == 0)
2656 return opt
->info
.string
;
2661 walk_subclasses (type_p base
, struct walk_type_data
*d
)
2663 for (type_p sub
= base
->u
.s
.first_subclass
; sub
!= NULL
;
2664 sub
= sub
->u
.s
.next_sibling_class
)
2666 const char *type_tag
= get_string_option (sub
->u
.s
.opt
, "tag");
2669 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", type_tag
);
2671 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2673 oprintf (d
->of
, "%*s%s *sub = static_cast <%s *> (x);\n",
2674 d
->indent
, "", sub
->u
.s
.tag
, sub
->u
.s
.tag
);
2675 const char *old_val
= d
->val
;
2680 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2681 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
2684 walk_subclasses (sub
, d
);
2688 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2689 which is of type T. Write code to D->OF to constrain execution (at
2690 the point that D->PROCESS_FIELD is called) to the appropriate
2691 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2692 pointers to those objects. D->PREV_VAL lists the objects
2693 containing the current object, D->OPT is a list of options to
2694 apply, D->INDENT is the current indentation level, D->LINE is used
2695 to print error messages, D->BITMAP indicates which languages to
2696 print the structure for, and D->PARAM is the current parameter
2697 (from an enclosing param_is option). */
2700 walk_type (type_p t
, struct walk_type_data
*d
)
2702 const char *length
= NULL
;
2703 const char *desc
= NULL
;
2704 const char *type_tag
= NULL
;
2705 int maybe_undef_p
= 0;
2706 int use_param_num
= -1;
2707 int use_params_p
= 0;
2710 const struct nested_ptr_data
*nested_ptr_d
= NULL
;
2712 d
->needs_cast_p
= false;
2713 for (oo
= d
->opt
; oo
; oo
= oo
->next
)
2714 if (strcmp (oo
->name
, "length") == 0 && oo
->kind
== OPTION_STRING
)
2715 length
= oo
->info
.string
;
2716 else if (strcmp (oo
->name
, "maybe_undef") == 0)
2718 else if (strncmp (oo
->name
, "use_param", 9) == 0
2719 && (oo
->name
[9] == '\0' || ISDIGIT (oo
->name
[9])))
2720 use_param_num
= oo
->name
[9] == '\0' ? 0 : oo
->name
[9] - '0';
2721 else if (strcmp (oo
->name
, "use_params") == 0)
2723 else if (strcmp (oo
->name
, "desc") == 0 && oo
->kind
== OPTION_STRING
)
2724 desc
= oo
->info
.string
;
2725 else if (strcmp (oo
->name
, "mark_hook") == 0)
2727 else if (strcmp (oo
->name
, "nested_ptr") == 0
2728 && oo
->kind
== OPTION_NESTED
)
2729 nested_ptr_d
= (const struct nested_ptr_data
*) oo
->info
.nested
;
2730 else if (strcmp (oo
->name
, "dot") == 0)
2732 else if (strcmp (oo
->name
, "tag") == 0)
2733 type_tag
= oo
->info
.string
;
2734 else if (strcmp (oo
->name
, "special") == 0)
2736 else if (strcmp (oo
->name
, "skip") == 0)
2738 else if (strcmp (oo
->name
, "atomic") == 0)
2740 else if (strcmp (oo
->name
, "default") == 0)
2742 else if (strcmp (oo
->name
, "param_is") == 0)
2744 else if (strncmp (oo
->name
, "param", 5) == 0
2745 && ISDIGIT (oo
->name
[5]) && strcmp (oo
->name
+ 6, "_is") == 0)
2747 else if (strcmp (oo
->name
, "chain_next") == 0)
2749 else if (strcmp (oo
->name
, "chain_prev") == 0)
2751 else if (strcmp (oo
->name
, "chain_circular") == 0)
2753 else if (strcmp (oo
->name
, "reorder") == 0)
2755 else if (strcmp (oo
->name
, "variable_size") == 0)
2758 error_at_line (d
->line
, "unknown option `%s'\n", oo
->name
);
2765 int pointer_p
= t
->kind
== TYPE_POINTER
;
2769 if (!union_or_struct_p (t
))
2770 error_at_line (d
->line
, "`use_params' option on unimplemented type");
2772 t
= find_param_structure (t
, d
->param
);
2774 t
= create_pointer (t
);
2777 if (use_param_num
!= -1)
2779 if (d
->param
!= NULL
&& d
->param
[use_param_num
] != NULL
)
2781 type_p nt
= d
->param
[use_param_num
];
2783 if (t
->kind
== TYPE_ARRAY
)
2784 nt
= create_array (nt
, t
->u
.a
.len
);
2785 else if (length
!= NULL
&& t
->kind
== TYPE_POINTER
)
2786 nt
= create_pointer (nt
);
2787 d
->needs_cast_p
= (t
->kind
!= TYPE_POINTER
2788 && (nt
->kind
== TYPE_POINTER
2789 || nt
->kind
== TYPE_STRING
));
2793 error_at_line (d
->line
, "no parameter defined for `%s'", d
->val
);
2797 && (t
->kind
!= TYPE_POINTER
|| !union_or_struct_p (t
->u
.p
)))
2799 error_at_line (d
->line
,
2800 "field `%s' has invalid option `maybe_undef_p'\n",
2805 if (atomic_p
&& (t
->kind
!= TYPE_POINTER
) && (t
->kind
!= TYPE_STRING
))
2807 error_at_line (d
->line
, "field `%s' has invalid option `atomic'\n", d
->val
);
2815 d
->process_field (t
, d
);
2820 d
->in_ptr_field
= true;
2821 if (maybe_undef_p
&& t
->u
.p
->u
.s
.line
.file
== NULL
)
2823 oprintf (d
->of
, "%*sgcc_assert (!%s);\n", d
->indent
, "", d
->val
);
2827 /* If a pointer type is marked as "atomic", we process the
2828 field itself, but we don't walk the data that they point to.
2830 There are two main cases where we walk types: to mark
2831 pointers that are reachable, and to relocate pointers when
2832 writing a PCH file. In both cases, an atomic pointer is
2833 itself marked or relocated, but the memory that it points
2834 to is left untouched. In the case of PCH, that memory will
2835 be read/written unchanged to the PCH file. */
2838 oprintf (d
->of
, "%*sif (%s != NULL) {\n", d
->indent
, "", d
->val
);
2840 d
->process_field (t
, d
);
2842 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2848 if (!union_or_struct_p (t
->u
.p
)
2849 && t
->u
.p
->kind
!= TYPE_PARAM_STRUCT
)
2851 error_at_line (d
->line
,
2852 "field `%s' is pointer to unimplemented type",
2859 const char *oldprevval2
= d
->prev_val
[2];
2861 if (!union_or_struct_p (nested_ptr_d
->type
))
2863 error_at_line (d
->line
,
2864 "field `%s' has invalid "
2865 "option `nested_ptr'\n", d
->val
);
2869 d
->prev_val
[2] = d
->val
;
2870 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2872 d
->val
= xasprintf ("x%d", d
->counter
++);
2873 oprintf (d
->of
, "%*s%s %s * %s%s =\n", d
->indent
, "",
2874 (nested_ptr_d
->type
->kind
== TYPE_UNION
2875 ? "union" : "struct"),
2876 nested_ptr_d
->type
->u
.s
.tag
,
2877 d
->fn_wants_lvalue
? "" : "const ", d
->val
);
2878 oprintf (d
->of
, "%*s", d
->indent
+ 2, "");
2879 output_escaped_param (d
, nested_ptr_d
->convert_from
,
2881 oprintf (d
->of
, ";\n");
2883 d
->process_field (nested_ptr_d
->type
, d
);
2885 if (d
->fn_wants_lvalue
)
2887 oprintf (d
->of
, "%*s%s = ", d
->indent
, "",
2889 d
->prev_val
[2] = d
->val
;
2890 output_escaped_param (d
, nested_ptr_d
->convert_to
,
2892 oprintf (d
->of
, ";\n");
2896 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2897 d
->val
= d
->prev_val
[2];
2898 d
->prev_val
[2] = oldprevval2
;
2901 d
->process_field (t
->u
.p
, d
);
2905 int loopcounter
= d
->loopcounter
;
2906 const char *oldval
= d
->val
;
2907 const char *oldprevval3
= d
->prev_val
[3];
2910 oprintf (d
->of
, "%*sif (%s != NULL) {\n", d
->indent
, "", d
->val
);
2912 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
2913 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != (size_t)(", d
->indent
,
2914 "", loopcounter
, loopcounter
);
2915 if (!d
->in_record_p
)
2916 output_escaped_param (d
, length
, "length");
2918 oprintf (d
->of
, "l%d", loopcounter
);
2919 if (d
->have_this_obj
)
2920 /* Try to unswitch loops (see PR53880). */
2921 oprintf (d
->of
, ") && ((void *)%s == this_obj", oldval
);
2922 oprintf (d
->of
, "); i%d++) {\n", loopcounter
);
2924 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
2926 d
->prev_val
[3] = oldval
;
2927 walk_type (t
->u
.p
, d
);
2930 d
->prev_val
[3] = oldprevval3
;
2933 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2934 d
->process_field (t
, d
);
2936 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2938 d
->in_ptr_field
= false;
2945 const char *oldval
= d
->val
;
2948 /* If it's an array of scalars, we optimize by not generating
2950 if (t
->u
.a
.p
->kind
== TYPE_SCALAR
)
2954 loopcounter
= d
->loopcounter
;
2956 loopcounter
= d
->counter
++;
2958 /* When walking an array, compute the length and store it in a
2959 local variable before walking the array elements, instead of
2960 recomputing the length expression each time through the loop.
2961 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2962 where the length is stored in the first array element,
2963 because otherwise that operand can get overwritten on the
2965 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2967 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
2968 if (!d
->in_record_p
|| !length
)
2970 oprintf (d
->of
, "%*ssize_t l%d = (size_t)(",
2971 d
->indent
, "", loopcounter
);
2973 output_escaped_param (d
, length
, "length");
2975 oprintf (d
->of
, "%s", t
->u
.a
.len
);
2976 oprintf (d
->of
, ");\n");
2979 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2981 loopcounter
, loopcounter
, loopcounter
, loopcounter
);
2983 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
2985 walk_type (t
->u
.a
.p
, d
);
2990 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2992 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3000 const char *oldval
= d
->val
;
3001 const char *oldprevval1
= d
->prev_val
[1];
3002 const char *oldprevval2
= d
->prev_val
[2];
3003 const char *struct_mark_hook
= NULL
;
3004 const int union_p
= t
->kind
== TYPE_UNION
;
3005 int seen_default_p
= 0;
3007 int lengths_seen
= 0;
3009 bool any_length_seen
= false;
3011 if (!t
->u
.s
.line
.file
)
3012 error_at_line (d
->line
, "incomplete structure `%s'", t
->u
.s
.tag
);
3014 if ((d
->bitmap
& t
->u
.s
.bitmap
) != d
->bitmap
)
3016 error_at_line (d
->line
,
3017 "structure `%s' defined for mismatching languages",
3019 error_at_line (&t
->u
.s
.line
, "one structure defined here");
3022 /* Some things may also be defined in the structure's options. */
3023 for (o
= t
->u
.s
.opt
; o
; o
= o
->next
)
3024 if (!desc
&& strcmp (o
->name
, "desc") == 0
3025 && o
->kind
== OPTION_STRING
)
3026 desc
= o
->info
.string
;
3027 else if (!struct_mark_hook
&& strcmp (o
->name
, "mark_hook") == 0
3028 && o
->kind
== OPTION_STRING
)
3029 struct_mark_hook
= o
->info
.string
;
3031 if (struct_mark_hook
)
3032 oprintf (d
->of
, "%*s%s (&%s);\n",
3033 d
->indent
, "", struct_mark_hook
, oldval
);
3035 d
->prev_val
[2] = oldval
;
3036 d
->prev_val
[1] = oldprevval2
;
3041 error_at_line (d
->line
,
3042 "missing `desc' option for union `%s'",
3046 oprintf (d
->of
, "%*sswitch (", d
->indent
, "");
3047 output_escaped_param (d
, desc
, "desc");
3048 oprintf (d
->of
, ")\n");
3050 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
3054 /* We have a "desc" option on a struct, signifying the
3055 base class within a GC-managed inheritance hierarchy.
3056 The current code specialcases the base class, then walks
3057 into subclasses, recursing into this routine to handle them.
3058 This organization requires the base class to have a case in
3059 the switch statement, and hence a tag value is mandatory
3060 for the base class. This restriction could be removed, but
3061 it would require some restructing of this code. */
3064 error_at_line (d
->line
,
3065 "missing `tag' option for type `%s'",
3068 oprintf (d
->of
, "%*sswitch (", d
->indent
, "");
3069 output_escaped_param (d
, desc
, "desc");
3070 oprintf (d
->of
, ")\n");
3072 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
3073 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", type_tag
);
3077 FOR_ALL_INHERITED_FIELDS (t
, f
)
3081 const char *fieldlength
= NULL
;
3083 d
->reorder_fn
= NULL
;
3084 for (oo
= f
->opt
; oo
; oo
= oo
->next
)
3085 if (strcmp (oo
->name
, "skip") == 0)
3087 else if (strcmp (oo
->name
, "length") == 0
3088 && oo
->kind
== OPTION_STRING
)
3089 fieldlength
= oo
->info
.string
;
3099 if (!any_length_seen
)
3101 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
3104 any_length_seen
= true;
3106 oprintf (d
->of
, "%*ssize_t l%d = (size_t)(",
3107 d
->indent
, "", d
->counter
- 1);
3108 output_escaped_param (d
, fieldlength
, "length");
3109 oprintf (d
->of
, ");\n");
3113 endcounter
= d
->counter
;
3115 FOR_ALL_INHERITED_FIELDS (t
, f
)
3118 const char *dot
= ".";
3119 const char *tagid
= NULL
;
3122 int use_param_p
= 0;
3123 const char *fieldlength
= NULL
;
3126 d
->reorder_fn
= NULL
;
3127 for (oo
= f
->opt
; oo
; oo
= oo
->next
)
3128 if (strcmp (oo
->name
, "dot") == 0
3129 && oo
->kind
== OPTION_STRING
)
3130 dot
= oo
->info
.string
;
3131 else if (strcmp (oo
->name
, "tag") == 0
3132 && oo
->kind
== OPTION_STRING
)
3133 tagid
= oo
->info
.string
;
3134 else if (strcmp (oo
->name
, "skip") == 0)
3136 else if (strcmp (oo
->name
, "default") == 0)
3138 else if (strcmp (oo
->name
, "reorder") == 0
3139 && oo
->kind
== OPTION_STRING
)
3140 d
->reorder_fn
= oo
->info
.string
;
3141 else if (strncmp (oo
->name
, "use_param", 9) == 0
3142 && (oo
->name
[9] == '\0' || ISDIGIT (oo
->name
[9])))
3144 else if (strcmp (oo
->name
, "length") == 0
3145 && oo
->kind
== OPTION_STRING
)
3146 fieldlength
= oo
->info
.string
;
3151 if (union_p
&& tagid
)
3153 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", tagid
);
3156 else if (union_p
&& default_p
)
3158 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
3162 else if (!union_p
&& (default_p
|| tagid
))
3163 error_at_line (d
->line
,
3164 "can't use `%s' outside a union on field `%s'",
3165 default_p
? "default" : "tag", f
->name
);
3166 else if (union_p
&& !(default_p
|| tagid
)
3167 && f
->type
->kind
== TYPE_SCALAR
)
3170 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3171 get_input_file_name (d
->line
->file
), d
->line
->line
,
3175 else if (union_p
&& !(default_p
|| tagid
))
3176 error_at_line (d
->line
,
3177 "field `%s' is missing `tag' or `default' option",
3182 d
->loopcounter
= endcounter
- lengths_seen
--;
3186 d
->val
= newval
= xasprintf ("%s%s%s", oldval
, dot
, f
->name
);
3188 d
->used_length
= false;
3189 d
->in_record_p
= !union_p
;
3191 if (union_p
&& use_param_p
&& d
->param
== NULL
)
3192 oprintf (d
->of
, "%*sgcc_unreachable ();\n", d
->indent
, "");
3194 walk_type (f
->type
, d
);
3196 d
->in_record_p
= false;
3202 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
3206 d
->reorder_fn
= NULL
;
3209 d
->prev_val
[1] = oldprevval1
;
3210 d
->prev_val
[2] = oldprevval2
;
3212 if (union_p
&& !seen_default_p
)
3214 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
3215 oprintf (d
->of
, "%*s break;\n", d
->indent
, "");
3218 if (desc
&& !union_p
)
3220 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
3225 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3230 /* Add cases to handle subclasses. */
3231 walk_subclasses (t
, d
);
3233 /* Ensure that if someone forgets a "tag" option that we don't
3234 silent fail to traverse that subclass's fields. */
3235 if (!seen_default_p
)
3237 oprintf (d
->of
, "%*s/* Unrecognized tag value. */\n",
3239 oprintf (d
->of
, "%*sdefault: gcc_unreachable (); \n",
3243 /* End of the switch statement */
3244 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3247 if (any_length_seen
)
3250 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
3255 case TYPE_LANG_STRUCT
:
3258 for (nt
= t
->u
.s
.lang_struct
; nt
; nt
= nt
->next
)
3259 if ((d
->bitmap
& nt
->u
.s
.bitmap
) == d
->bitmap
)
3262 error_at_line (d
->line
, "structure `%s' differs between languages",
3269 case TYPE_PARAM_STRUCT
:
3271 type_p
*oldparam
= d
->param
;
3273 d
->param
= t
->u
.param_struct
.param
;
3274 walk_type (t
->u
.param_struct
.stru
, d
);
3275 d
->param
= oldparam
;
3279 case TYPE_USER_STRUCT
:
3280 d
->process_field (t
, d
);
3284 case TYPE_UNDEFINED
:
3289 /* process_field routine for marking routines. */
3292 write_types_process_field (type_p f
, const struct walk_type_data
*d
)
3294 const struct write_types_data
*wtd
;
3295 const char *cast
= d
->needs_cast_p
? "(void *)" : "";
3296 wtd
= (const struct write_types_data
*) d
->cookie
;
3301 case TYPE_UNDEFINED
:
3304 oprintf (d
->of
, "%*s%s (%s%s", d
->indent
, "",
3305 wtd
->subfield_marker_routine
, cast
, d
->val
);
3306 if (wtd
->param_prefix
)
3308 if (f
->u
.p
->kind
== TYPE_SCALAR
)
3309 /* The current type is a pointer to a scalar (so not
3310 considered like a pointer to instances of user defined
3311 types) and we are seeing it; it means we must be even
3312 more careful about the second argument of the
3313 SUBFIELD_MARKER_ROUTINE call. That argument must
3314 always be the instance of the type for which
3315 write_func_for_structure was called - this really is
3316 what the function SUBFIELD_MARKER_ROUTINE expects.
3317 That is, it must be an instance of the ORIG_S type
3318 parameter of write_func_for_structure. The convention
3319 is that that argument must be "x" in that case (as set
3320 by write_func_for_structure). The problem is, we can't
3321 count on d->prev_val[3] to be always set to "x" in that
3322 case. Sometimes walk_type can set it to something else
3323 (to e.g cooperate with write_array when called from
3324 write_roots). So let's set it to "x" here then. */
3325 oprintf (d
->of
, ", x");
3327 oprintf (d
->of
, ", %s", d
->prev_val
[3]);
3330 oprintf (d
->of
, ", gt_%s_", wtd
->param_prefix
);
3331 output_mangled_typename (d
->of
, d
->orig_s
);
3334 oprintf (d
->of
, ", gt_%sa_%s", wtd
->param_prefix
, d
->prev_val
[0]);
3336 oprintf (d
->of
, ");\n");
3337 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
3338 oprintf (d
->of
, "%*s%s (%s%s, %s, %s);\n", d
->indent
, "",
3339 wtd
->reorder_note_routine
, cast
, d
->val
,
3340 d
->prev_val
[3], d
->reorder_fn
);
3346 case TYPE_LANG_STRUCT
:
3347 case TYPE_PARAM_STRUCT
:
3348 case TYPE_USER_STRUCT
:
3349 if (f
->kind
== TYPE_USER_STRUCT
&& !d
->in_ptr_field
)
3351 /* If F is a user-defined type and the field is not a
3352 pointer to the type, then we should not generate the
3353 standard pointer-marking code. All we need to do is call
3354 the user-provided marking function to process the fields
3356 oprintf (d
->of
, "%*sgt_%sx (&(%s));\n", d
->indent
, "", wtd
->prefix
,
3361 oprintf (d
->of
, "%*sgt_%s_", d
->indent
, "", wtd
->prefix
);
3362 output_mangled_typename (d
->of
, f
);
3363 oprintf (d
->of
, " (%s%s);\n", cast
, d
->val
);
3364 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
3365 oprintf (d
->of
, "%*s%s (%s%s, %s%s, %s);\n", d
->indent
, "",
3366 wtd
->reorder_note_routine
, cast
, d
->val
, cast
, d
->val
,
3379 /* Return an output file that is suitable for definitions which can
3380 reference struct S */
3383 get_output_file_for_structure (const_type_p s
, type_p
*param
)
3385 const input_file
*fn
;
3388 gcc_assert (union_or_struct_p (s
));
3389 fn
= s
->u
.s
.line
.file
;
3391 /* This is a hack, and not the good kind either. */
3392 for (i
= NUM_PARAM
- 1; i
>= 0; i
--)
3393 if (param
&& param
[i
] && param
[i
]->kind
== TYPE_POINTER
3394 && union_or_struct_p (param
[i
]->u
.p
))
3395 fn
= param
[i
]->u
.p
->u
.s
.line
.file
;
3397 /* The call to get_output_file_with_visibility may update fn by
3398 caching its result inside, so we need the CONST_CAST. */
3399 return get_output_file_with_visibility (CONST_CAST (input_file
*, fn
));
3403 /* Returns the specifier keyword for a string or union type S, empty string
3407 get_type_specifier (const type_p s
)
3409 if (s
->kind
== TYPE_STRUCT
)
3411 else if (s
->kind
== TYPE_LANG_STRUCT
)
3412 return get_type_specifier (s
->u
.s
.lang_struct
);
3413 else if (s
->kind
== TYPE_UNION
)
3419 /* Emits a declaration for type TY (assumed to be a union or a
3420 structure) on stream OUT. */
3423 write_type_decl (outf_p out
, type_p ty
)
3425 if (union_or_struct_p (ty
))
3426 oprintf (out
, "%s%s", get_type_specifier (ty
), ty
->u
.s
.tag
);
3427 else if (ty
->kind
== TYPE_SCALAR
)
3429 if (ty
->u
.scalar_is_char
)
3430 oprintf (out
, "const char");
3432 oprintf (out
, "void");
3434 else if (ty
->kind
== TYPE_POINTER
)
3436 write_type_decl (out
, ty
->u
.p
);
3437 oprintf (out
, " *");
3439 else if (ty
->kind
== TYPE_ARRAY
)
3441 write_type_decl (out
, ty
->u
.a
.p
);
3442 oprintf (out
, " *");
3444 else if (ty
->kind
== TYPE_STRING
)
3446 oprintf (out
, "const char *");
3453 /* Write on OF the name of the marker function for structure S. PREFIX
3454 is the prefix to use (to distinguish ggc from pch markers). */
3457 write_marker_function_name (outf_p of
, type_p s
, const char *prefix
)
3459 if (union_or_struct_p (s
))
3461 const char *id_for_tag
= filter_type_name (s
->u
.s
.tag
);
3462 oprintf (of
, "gt_%sx_%s", prefix
, id_for_tag
);
3463 if (id_for_tag
!= s
->u
.s
.tag
)
3464 free (CONST_CAST (char *, id_for_tag
));
3466 else if (s
->kind
== TYPE_PARAM_STRUCT
)
3468 oprintf (of
, "gt_%s_", prefix
);
3469 output_mangled_typename (of
, s
);
3475 /* Write on OF a user-callable routine to act as an entry point for
3476 the marking routine for S, generated by write_func_for_structure.
3477 PREFIX is the prefix to use to distinguish ggc and pch markers. */
3480 write_user_func_for_structure_ptr (outf_p of
, type_p s
, const char *prefix
)
3482 /* Parameterized structures are not supported in user markers. There
3483 is no way for the marker function to know which specific type
3484 to use to generate the call to the void * entry point. For
3485 instance, a marker for struct htab may need to call different
3486 routines to mark the fields, depending on the paramN_is attributes.
3488 A user-defined marker that accepts 'struct htab' as its argument
3489 would not know which variant to call. Generating several entry
3490 points accepting 'struct htab' would cause multiply-defined
3491 errors during compilation. */
3492 gcc_assert (union_or_struct_p (s
));
3494 type_p alias_of
= NULL
;
3495 for (options_p opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3496 if (strcmp (opt
->name
, "ptr_alias") == 0)
3498 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3499 we do not generate marking code for ORIG_S here. Instead, a
3500 forwarder #define in gtype-desc.h will cause every call to its
3501 marker to call the target of this alias.
3503 However, we still want to create a user entry code for the
3504 aliased type. So, if ALIAS_OF is set, we only generate the
3505 user-callable marker function. */
3506 alias_of
= opt
->info
.type
;
3510 oprintf (of
, "\nvoid\n");
3511 oprintf (of
, "gt_%sx (", prefix
);
3512 write_type_decl (of
, s
);
3513 oprintf (of
, " *& x)\n");
3514 oprintf (of
, "{\n");
3515 oprintf (of
, " if (x)\n ");
3516 write_marker_function_name (of
, alias_of
? alias_of
: s
, prefix
);
3517 oprintf (of
, " ((void *) x);\n");
3518 oprintf (of
, "}\n");
3522 /* Write a function to mark all the fields of type S on OF. PREFIX
3523 and D are as in write_user_marking_functions. */
3526 write_user_func_for_structure_body (type_p s
, const char *prefix
,
3527 struct walk_type_data
*d
)
3529 oprintf (d
->of
, "\nvoid\n");
3530 oprintf (d
->of
, "gt_%sx (", prefix
);
3531 write_type_decl (d
->of
, s
);
3532 oprintf (d
->of
, "& x_r ATTRIBUTE_UNUSED)\n");
3533 oprintf (d
->of
, "{\n");
3534 oprintf (d
->of
, " ");
3535 write_type_decl (d
->of
, s
);
3536 oprintf (d
->of
, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3540 oprintf (d
->of
, "}\n");
3544 /* Emit the user-callable functions needed to mark all the types used
3545 by the user structure S. PREFIX is the prefix to use to
3546 distinguish ggc and pch markers. D contains data needed to pass to
3547 walk_type when traversing the fields of a type.
3549 For every type T referenced by S, two routines are generated: one
3550 that takes 'T *', marks the pointer and calls the second routine,
3551 which just marks the fields of T. */
3554 write_user_marking_functions (type_p s
, const char *prefix
,
3555 struct walk_type_data
*d
)
3557 gcc_assert (s
->kind
== TYPE_USER_STRUCT
);
3559 for (pair_p fld
= s
->u
.s
.fields
; fld
; fld
= fld
->next
)
3561 type_p fld_type
= fld
->type
;
3562 if (fld_type
->kind
== TYPE_POINTER
)
3564 type_p pointed_to_type
= fld_type
->u
.p
;
3565 if (union_or_struct_p (pointed_to_type
))
3566 write_user_func_for_structure_ptr (d
->of
, pointed_to_type
, prefix
);
3568 else if (union_or_struct_p (fld_type
))
3569 write_user_func_for_structure_body (fld_type
, prefix
, d
);
3574 /* For S, a structure that's part of ORIG_S, and using parameters
3575 PARAM, write out a routine that:
3576 - Takes a parameter, a void * but actually of type *S
3577 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3578 field of S or its substructures and (in some cases) things
3579 that are pointed to by S. */
3582 write_func_for_structure (type_p orig_s
, type_p s
, type_p
*param
,
3583 const struct write_types_data
*wtd
)
3585 const char *chain_next
= NULL
;
3586 const char *chain_prev
= NULL
;
3587 const char *chain_circular
= NULL
;
3588 const char *mark_hook_name
= NULL
;
3590 struct walk_type_data d
;
3592 if (s
->u
.s
.base_class
)
3594 /* Verify that the base class has a "desc", since otherwise
3595 the traversal hooks there won't attempt to visit fields of
3596 subclasses such as this one. */
3597 const_type_p ubc
= get_ultimate_base_class (s
);
3598 if ((!opts_have (ubc
->u
.s
.opt
, "user")
3599 && !opts_have (ubc
->u
.s
.opt
, "desc")))
3600 error_at_line (&s
->u
.s
.line
,
3601 ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
3602 ", but '%s' lacks a discriminator 'desc' option"),
3603 s
->u
.s
.tag
, ubc
->u
.s
.tag
, ubc
->u
.s
.tag
);
3605 /* Don't write fns for subclasses, only for the ultimate base class
3606 within an inheritance hierarchy. */
3610 memset (&d
, 0, sizeof (d
));
3611 d
.of
= get_output_file_for_structure (s
, param
);
3612 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3613 if (strcmp (opt
->name
, "chain_next") == 0
3614 && opt
->kind
== OPTION_STRING
)
3615 chain_next
= opt
->info
.string
;
3616 else if (strcmp (opt
->name
, "chain_prev") == 0
3617 && opt
->kind
== OPTION_STRING
)
3618 chain_prev
= opt
->info
.string
;
3619 else if (strcmp (opt
->name
, "chain_circular") == 0
3620 && opt
->kind
== OPTION_STRING
)
3621 chain_circular
= opt
->info
.string
;
3622 else if (strcmp (opt
->name
, "mark_hook") == 0
3623 && opt
->kind
== OPTION_STRING
)
3624 mark_hook_name
= opt
->info
.string
;
3625 if (chain_prev
!= NULL
&& chain_next
== NULL
)
3626 error_at_line (&s
->u
.s
.line
, "chain_prev without chain_next");
3627 if (chain_circular
!= NULL
&& chain_next
!= NULL
)
3628 error_at_line (&s
->u
.s
.line
, "chain_circular with chain_next");
3629 if (chain_circular
!= NULL
)
3630 chain_next
= chain_circular
;
3632 d
.process_field
= write_types_process_field
;
3636 d
.line
= &s
->u
.s
.line
;
3637 d
.bitmap
= s
->u
.s
.bitmap
;
3639 d
.prev_val
[0] = "*x";
3640 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
3641 d
.prev_val
[3] = "x";
3643 d
.have_this_obj
= false;
3645 oprintf (d
.of
, "\n");
3646 oprintf (d
.of
, "void\n");
3647 write_marker_function_name (d
.of
, orig_s
, wtd
->prefix
);
3648 oprintf (d
.of
, " (void *x_p)\n");
3649 oprintf (d
.of
, "{\n ");
3650 write_type_decl (d
.of
, s
);
3651 oprintf (d
.of
, " * %sx = (", chain_next
== NULL
? "const " : "");
3652 write_type_decl (d
.of
, s
);
3653 oprintf (d
.of
, " *)x_p;\n");
3654 if (chain_next
!= NULL
)
3656 /* TYPE_USER_STRUCTs should not occur here. These structures
3657 are completely handled by user code. */
3658 gcc_assert (orig_s
->kind
!= TYPE_USER_STRUCT
);
3660 oprintf (d
.of
, " ");
3661 write_type_decl (d
.of
, s
);
3662 oprintf (d
.of
, " * xlimit = x;\n");
3664 if (chain_next
== NULL
)
3666 oprintf (d
.of
, " if (%s (x", wtd
->marker_routine
);
3667 if (wtd
->param_prefix
)
3669 oprintf (d
.of
, ", x, gt_%s_", wtd
->param_prefix
);
3670 output_mangled_typename (d
.of
, orig_s
);
3672 oprintf (d
.of
, "))\n");
3676 if (chain_circular
!= NULL
)
3677 oprintf (d
.of
, " if (!%s (xlimit", wtd
->marker_routine
);
3679 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
3680 if (wtd
->param_prefix
)
3682 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
3683 output_mangled_typename (d
.of
, orig_s
);
3685 oprintf (d
.of
, "))\n");
3686 if (chain_circular
!= NULL
)
3687 oprintf (d
.of
, " return;\n do\n");
3688 if (mark_hook_name
&& !wtd
->skip_hooks
)
3690 oprintf (d
.of
, " {\n");
3691 oprintf (d
.of
, " %s (xlimit);\n ", mark_hook_name
);
3693 oprintf (d
.of
, " xlimit = (");
3694 d
.prev_val
[2] = "*xlimit";
3695 output_escaped_param (&d
, chain_next
, "chain_next");
3696 oprintf (d
.of
, ");\n");
3697 if (mark_hook_name
&& !wtd
->skip_hooks
)
3698 oprintf (d
.of
, " }\n");
3699 if (chain_prev
!= NULL
)
3701 oprintf (d
.of
, " if (x != xlimit)\n");
3702 oprintf (d
.of
, " for (;;)\n");
3703 oprintf (d
.of
, " {\n");
3704 oprintf (d
.of
, " %s %s * const xprev = (",
3705 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
3707 d
.prev_val
[2] = "*x";
3708 output_escaped_param (&d
, chain_prev
, "chain_prev");
3709 oprintf (d
.of
, ");\n");
3710 oprintf (d
.of
, " if (xprev == NULL) break;\n");
3711 oprintf (d
.of
, " x = xprev;\n");
3712 oprintf (d
.of
, " (void) %s (xprev", wtd
->marker_routine
);
3713 if (wtd
->param_prefix
)
3715 oprintf (d
.of
, ", xprev, gt_%s_", wtd
->param_prefix
);
3716 output_mangled_typename (d
.of
, orig_s
);
3718 oprintf (d
.of
, ");\n");
3719 oprintf (d
.of
, " }\n");
3721 if (chain_circular
!= NULL
)
3723 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
3724 if (wtd
->param_prefix
)
3726 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
3727 output_mangled_typename (d
.of
, orig_s
);
3729 oprintf (d
.of
, "));\n");
3730 if (mark_hook_name
&& !wtd
->skip_hooks
)
3731 oprintf (d
.of
, " %s (xlimit);\n", mark_hook_name
);
3732 oprintf (d
.of
, " do\n");
3735 oprintf (d
.of
, " while (x != xlimit)\n");
3737 oprintf (d
.of
, " {\n");
3738 if (mark_hook_name
&& chain_next
== NULL
&& !wtd
->skip_hooks
)
3740 oprintf (d
.of
, " %s (x);\n", mark_hook_name
);
3743 d
.prev_val
[2] = "*x";
3745 if (orig_s
->kind
!= TYPE_USER_STRUCT
)
3749 /* User structures have no fields to walk. Simply generate a call
3750 to the user-provided structure marker. */
3751 oprintf (d
.of
, "%*sgt_%sx (x);\n", d
.indent
, "", wtd
->prefix
);
3754 if (chain_next
!= NULL
)
3756 oprintf (d
.of
, " x = (");
3757 output_escaped_param (&d
, chain_next
, "chain_next");
3758 oprintf (d
.of
, ");\n");
3761 oprintf (d
.of
, " }\n");
3762 if (chain_circular
!= NULL
)
3763 oprintf (d
.of
, " while (x != xlimit);\n");
3764 oprintf (d
.of
, "}\n");
3766 if (orig_s
->kind
== TYPE_USER_STRUCT
)
3767 write_user_marking_functions (orig_s
, wtd
->prefix
, &d
);
3771 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3774 write_types (outf_p output_header
, type_p structures
, type_p param_structs
,
3775 const struct write_types_data
*wtd
)
3777 int nbfun
= 0; /* Count the emitted functions. */
3780 oprintf (output_header
, "\n/* %s*/\n", wtd
->comment
);
3782 /* We first emit the macros and the declarations. Functions' code is
3783 emitted afterwards. This is needed in plugin mode. */
3784 oprintf (output_header
, "/* Macros and declarations. */\n");
3785 for (s
= structures
; s
; s
= s
->next
)
3786 /* Do not emit handlers for derived classes; we only ever deal with
3787 the ultimate base class within an inheritance hierarchy. */
3788 if ((s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
3789 && !s
->u
.s
.base_class
)
3793 if (s
->gc_used
== GC_MAYBE_POINTED_TO
&& s
->u
.s
.line
.file
== NULL
)
3796 const char *s_id_for_tag
= filter_type_name (s
->u
.s
.tag
);
3798 oprintf (output_header
, "#define gt_%s_", wtd
->prefix
);
3799 output_mangled_typename (output_header
, s
);
3800 oprintf (output_header
, "(X) do { \\\n");
3801 oprintf (output_header
,
3802 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd
->prefix
,
3804 oprintf (output_header
, " } while (0)\n");
3806 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3807 if (strcmp (opt
->name
, "ptr_alias") == 0
3808 && opt
->kind
== OPTION_TYPE
)
3810 const_type_p
const t
= (const_type_p
) opt
->info
.type
;
3811 if (t
->kind
== TYPE_STRUCT
3812 || t
->kind
== TYPE_UNION
|| t
->kind
== TYPE_LANG_STRUCT
)
3814 const char *t_id_for_tag
= filter_type_name (t
->u
.s
.tag
);
3815 oprintf (output_header
,
3816 "#define gt_%sx_%s gt_%sx_%s\n",
3817 wtd
->prefix
, s
->u
.s
.tag
, wtd
->prefix
, t_id_for_tag
);
3818 if (t_id_for_tag
!= t
->u
.s
.tag
)
3819 free (CONST_CAST (char *, t_id_for_tag
));
3822 error_at_line (&s
->u
.s
.line
,
3823 "structure alias is not a structure");
3829 /* Declare the marker procedure only once. */
3830 oprintf (output_header
,
3831 "extern void gt_%sx_%s (void *);\n",
3832 wtd
->prefix
, s_id_for_tag
);
3834 if (s_id_for_tag
!= s
->u
.s
.tag
)
3835 free (CONST_CAST (char *, s_id_for_tag
));
3837 if (s
->u
.s
.line
.file
== NULL
)
3839 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
3845 for (s
= param_structs
; s
; s
= s
->next
)
3846 if (s
->gc_used
== GC_POINTED_TO
)
3848 type_p stru
= s
->u
.param_struct
.stru
;
3850 /* Declare the marker procedure. */
3851 oprintf (output_header
, "extern void gt_%s_", wtd
->prefix
);
3852 output_mangled_typename (output_header
, s
);
3853 oprintf (output_header
, " (void *);\n");
3855 if (stru
->u
.s
.line
.file
== NULL
)
3857 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
3863 /* At last we emit the functions code. */
3864 oprintf (output_header
, "\n/* functions code */\n");
3865 for (s
= structures
; s
; s
= s
->next
)
3866 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
3870 if (s
->gc_used
== GC_MAYBE_POINTED_TO
&& s
->u
.s
.line
.file
== NULL
)
3872 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3873 if (strcmp (opt
->name
, "ptr_alias") == 0)
3878 if (s
->kind
== TYPE_LANG_STRUCT
)
3881 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
3884 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3885 nbfun
, (void*) ss
, ss
->u
.s
.tag
);
3886 write_func_for_structure (s
, ss
, NULL
, wtd
);
3892 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3893 nbfun
, (void*) s
, s
->u
.s
.tag
);
3894 write_func_for_structure (s
, s
, NULL
, wtd
);
3899 /* Structure s is not possibly pointed to, so can be ignored. */
3900 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3901 (void*)s
, s
->u
.s
.tag
,
3905 for (s
= param_structs
; s
; s
= s
->next
)
3906 if (s
->gc_used
== GC_POINTED_TO
)
3908 type_p
*param
= s
->u
.param_struct
.param
;
3909 type_p stru
= s
->u
.param_struct
.stru
;
3910 if (stru
->u
.s
.line
.file
== NULL
)
3912 if (stru
->kind
== TYPE_LANG_STRUCT
)
3915 for (ss
= stru
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
3918 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
3919 nbfun
, (void*) ss
, ss
->u
.s
.tag
);
3920 write_func_for_structure (s
, ss
, param
, wtd
);
3926 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
3928 (void*) stru
, stru
->u
.s
.tag
);
3929 write_func_for_structure (s
, stru
, param
, wtd
);
3934 /* Param structure s is not pointed to, so should be ignored. */
3935 DBGPRINTF ("ignored s @ %p", (void*)s
);
3937 if (verbosity_level
>= 2)
3938 printf ("%s emitted %d routines for %s\n",
3939 progname
, nbfun
, wtd
->comment
);
3942 static const struct write_types_data ggc_wtd
= {
3943 "ggc_m", NULL
, "ggc_mark", "ggc_test_and_set_mark", NULL
,
3944 "GC marker procedures. ",
3948 static const struct write_types_data pch_wtd
= {
3949 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3950 "gt_pch_note_reorder",
3951 "PCH type-walking procedures. ",
3955 /* Write out the local pointer-walking routines. */
3957 /* process_field routine for local pointer-walking for user-callable
3958 routines. The difference between this and
3959 write_types_local_process_field is that, in this case, we do not
3960 need to check whether the given pointer matches the address of the
3961 parent structure. This check was already generated by the call
3962 to gt_pch_nx in the main gt_pch_p_*() function that is calling
3966 write_types_local_user_process_field (type_p f
, const struct walk_type_data
*d
)
3973 case TYPE_LANG_STRUCT
:
3974 case TYPE_PARAM_STRUCT
:
3976 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
3979 case TYPE_USER_STRUCT
:
3980 if (d
->in_ptr_field
)
3981 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
3983 oprintf (d
->of
, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3984 d
->indent
, "", d
->val
);
3992 case TYPE_UNDEFINED
:
3998 /* Write a function to PCH walk all the fields of type S on OF.
3999 D contains data needed by walk_type to recurse into the fields of S. */
4002 write_pch_user_walking_for_structure_body (type_p s
, struct walk_type_data
*d
)
4004 oprintf (d
->of
, "\nvoid\n");
4005 oprintf (d
->of
, "gt_pch_nx (");
4006 write_type_decl (d
->of
, s
);
4007 oprintf (d
->of
, "* x ATTRIBUTE_UNUSED,\n"
4008 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4009 "\tATTRIBUTE_UNUSED void *cookie)\n");
4010 oprintf (d
->of
, "{\n");
4013 d
->process_field
= write_types_local_user_process_field
;
4015 oprintf (d
->of
, "}\n");
4019 /* Emit the user-callable functions needed to mark all the types used
4020 by the user structure S. PREFIX is the prefix to use to
4021 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
4022 chain_next option defined. D contains data needed to pass to
4023 walk_type when traversing the fields of a type.
4025 For every type T referenced by S, two routines are generated: one
4026 that takes 'T *', marks the pointer and calls the second routine,
4027 which just marks the fields of T. */
4030 write_pch_user_walking_functions (type_p s
, struct walk_type_data
*d
)
4032 gcc_assert (s
->kind
== TYPE_USER_STRUCT
);
4034 for (pair_p fld
= s
->u
.s
.fields
; fld
; fld
= fld
->next
)
4036 type_p fld_type
= fld
->type
;
4037 if (union_or_struct_p (fld_type
))
4038 write_pch_user_walking_for_structure_body (fld_type
, d
);
4043 /* process_field routine for local pointer-walking. */
4046 write_types_local_process_field (type_p f
, const struct walk_type_data
*d
)
4048 gcc_assert (d
->have_this_obj
);
4054 case TYPE_LANG_STRUCT
:
4055 case TYPE_PARAM_STRUCT
:
4057 oprintf (d
->of
, "%*sif ((void *)(%s) == this_obj)\n", d
->indent
, "",
4059 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
4062 case TYPE_USER_STRUCT
:
4063 oprintf (d
->of
, "%*sif ((void *)(%s) == this_obj)\n", d
->indent
, "",
4065 if (d
->in_ptr_field
)
4066 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
4068 oprintf (d
->of
, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4069 d
->indent
, "", d
->val
);
4077 case TYPE_UNDEFINED
:
4083 /* For S, a structure that's part of ORIG_S, and using parameters
4084 PARAM, write out a routine that:
4085 - Is of type gt_note_pointers
4086 - Calls PROCESS_FIELD on each field of S or its substructures.
4090 write_local_func_for_structure (const_type_p orig_s
, type_p s
, type_p
*param
)
4092 struct walk_type_data d
;
4094 /* Don't write fns for subclasses, only for the ultimate base class
4095 within an inheritance hierarchy. */
4096 if (s
->u
.s
.base_class
)
4099 memset (&d
, 0, sizeof (d
));
4100 d
.of
= get_output_file_for_structure (s
, param
);
4101 d
.process_field
= write_types_local_process_field
;
4103 d
.line
= &s
->u
.s
.line
;
4104 d
.bitmap
= s
->u
.s
.bitmap
;
4106 d
.prev_val
[0] = d
.prev_val
[2] = "*x";
4107 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
4108 d
.prev_val
[3] = "x";
4110 d
.fn_wants_lvalue
= true;
4112 oprintf (d
.of
, "\n");
4113 oprintf (d
.of
, "void\n");
4114 oprintf (d
.of
, "gt_pch_p_");
4115 output_mangled_typename (d
.of
, orig_s
);
4116 oprintf (d
.of
, " (ATTRIBUTE_UNUSED void *this_obj,\n"
4118 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4119 "\tATTRIBUTE_UNUSED void *cookie)\n");
4120 oprintf (d
.of
, "{\n");
4121 oprintf (d
.of
, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
4122 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
,
4123 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
4125 d
.have_this_obj
= true;
4127 if (s
->kind
!= TYPE_USER_STRUCT
)
4131 /* User structures have no fields to walk. Simply generate a
4132 call to the user-provided PCH walker. */
4133 oprintf (d
.of
, "%*sif ((void *)(%s) == this_obj)\n", d
.indent
, "",
4135 oprintf (d
.of
, "%*s gt_pch_nx (&(%s), op, cookie);\n",
4136 d
.indent
, "", d
.val
);
4139 oprintf (d
.of
, "}\n");
4141 /* Write user-callable entry points for the PCH walking routines. */
4142 if (orig_s
->kind
== TYPE_USER_STRUCT
)
4143 write_pch_user_walking_functions (s
, &d
);
4146 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
4149 write_local (outf_p output_header
, type_p structures
, type_p param_structs
)
4156 oprintf (output_header
, "\n/* Local pointer-walking routines. */\n");
4157 for (s
= structures
; s
; s
= s
->next
)
4158 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
4162 if (s
->u
.s
.line
.file
== NULL
)
4164 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
4165 if (strcmp (opt
->name
, "ptr_alias") == 0
4166 && opt
->kind
== OPTION_TYPE
)
4168 const_type_p
const t
= (const_type_p
) opt
->info
.type
;
4169 if (t
->kind
== TYPE_STRUCT
4170 || t
->kind
== TYPE_UNION
|| t
->kind
== TYPE_LANG_STRUCT
)
4172 oprintf (output_header
, "#define gt_pch_p_");
4173 output_mangled_typename (output_header
, s
);
4174 oprintf (output_header
, " gt_pch_p_");
4175 output_mangled_typename (output_header
, t
);
4176 oprintf (output_header
, "\n");
4179 error_at_line (&s
->u
.s
.line
,
4180 "structure alias is not a structure");
4186 /* Declare the marker procedure only once. */
4187 oprintf (output_header
, "extern void gt_pch_p_");
4188 output_mangled_typename (output_header
, s
);
4189 oprintf (output_header
,
4190 "\n (void *, void *, gt_pointer_operator, void *);\n");
4192 if (s
->kind
== TYPE_LANG_STRUCT
)
4195 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
4196 write_local_func_for_structure (s
, ss
, NULL
);
4199 write_local_func_for_structure (s
, s
, NULL
);
4202 for (s
= param_structs
; s
; s
= s
->next
)
4203 if (s
->gc_used
== GC_POINTED_TO
)
4205 type_p
*param
= s
->u
.param_struct
.param
;
4206 type_p stru
= s
->u
.param_struct
.stru
;
4208 /* Declare the marker procedure. */
4209 oprintf (output_header
, "extern void gt_pch_p_");
4210 output_mangled_typename (output_header
, s
);
4211 oprintf (output_header
,
4212 "\n (void *, void *, gt_pointer_operator, void *);\n");
4214 if (stru
->u
.s
.line
.file
== NULL
)
4216 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
4221 if (stru
->kind
== TYPE_LANG_STRUCT
)
4224 for (ss
= stru
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
4225 write_local_func_for_structure (s
, ss
, param
);
4228 write_local_func_for_structure (s
, stru
, param
);
4232 /* Nonzero if S is a type for which typed GC allocators should be output. */
4234 #define USED_BY_TYPED_GC_P(s) \
4235 ((s->kind == TYPE_POINTER \
4236 && (s->u.p->gc_used == GC_POINTED_TO \
4237 || s->u.p->gc_used == GC_USED)) \
4238 || (union_or_struct_p (s) \
4239 && ((s)->gc_used == GC_POINTED_TO \
4240 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4241 && s->u.s.line.file != NULL) \
4242 || ((s)->gc_used == GC_USED \
4243 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))) \
4244 || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
4248 /* Might T contain any non-pointer elements? */
4251 contains_scalar_p (type_p t
)
4259 return contains_scalar_p (t
->u
.a
.p
);
4260 case TYPE_USER_STRUCT
:
4261 /* User-marked structures will typically contain pointers. */
4264 /* Could also check for structures that have no non-pointer
4265 fields, but there aren't enough of those to worry about. */
4270 /* Mangle INPF and print it to F. */
4273 put_mangled_filename (outf_p f
, const input_file
*inpf
)
4275 /* The call to get_output_file_name may indirectly update fn since
4276 get_output_file_with_visibility caches its result inside, so we
4277 need the CONST_CAST. */
4278 const char *name
= get_output_file_name (CONST_CAST (input_file
*, inpf
));
4281 for (; *name
!= 0; name
++)
4282 if (ISALNUM (*name
))
4283 oprintf (f
, "%c", *name
);
4285 oprintf (f
, "%c", '_');
4288 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4289 LASTNAME, and NAME are all strings to insert in various places in
4290 the resulting code. */
4293 finish_root_table (struct flist
*flp
, const char *pfx
, const char *lastname
,
4294 const char *tname
, const char *name
)
4298 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
4299 if (fli2
->started_p
)
4301 oprintf (fli2
->f
, " %s\n", lastname
);
4302 oprintf (fli2
->f
, "};\n\n");
4305 for (fli2
= flp
; fli2
&& base_files
; fli2
= fli2
->next
)
4306 if (fli2
->started_p
)
4308 lang_bitmap bitmap
= get_lang_bitmap (fli2
->file
);
4311 for (fnum
= 0; bitmap
!= 0; fnum
++, bitmap
>>= 1)
4314 oprintf (base_files
[fnum
],
4315 "extern const struct %s gt_%s_", tname
, pfx
);
4316 put_mangled_filename (base_files
[fnum
], fli2
->file
);
4317 oprintf (base_files
[fnum
], "[];\n");
4323 for (fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
4324 oprintf (base_files
[fnum
],
4325 "EXPORTED_CONST struct %s * const %s[] = {\n", tname
, name
);
4329 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
4330 if (fli2
->started_p
)
4332 lang_bitmap bitmap
= get_lang_bitmap (fli2
->file
);
4335 fli2
->started_p
= 0;
4337 for (fnum
= 0; base_files
&& bitmap
!= 0; fnum
++, bitmap
>>= 1)
4340 oprintf (base_files
[fnum
], " gt_%s_", pfx
);
4341 put_mangled_filename (base_files
[fnum
], fli2
->file
);
4342 oprintf (base_files
[fnum
], ",\n");
4348 for (fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
4350 oprintf (base_files
[fnum
], " NULL\n");
4351 oprintf (base_files
[fnum
], "};\n");
4356 /* Write the first three fields (pointer, count and stride) for
4357 root NAME to F. V and LINE are as for write_root.
4359 Return true if the entry could be written; return false on error. */
4362 start_root_entry (outf_p f
, pair_p v
, const char *name
, struct fileloc
*line
)
4368 error_at_line (line
, "`%s' is too complex to be a root", name
);
4372 oprintf (f
, " {\n");
4373 oprintf (f
, " &%s,\n", name
);
4376 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
4378 oprintf (f
, " * (%s)", ap
->u
.a
.len
);
4379 else if (ap
== v
->type
)
4380 oprintf (f
, " * ARRAY_SIZE (%s)", v
->name
);
4382 oprintf (f
, " sizeof (%s", v
->name
);
4383 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
4385 oprintf (f
, "),\n");
4389 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4390 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4394 write_field_root (outf_p f
, pair_p v
, type_p type
, const char *name
,
4395 int has_length
, struct fileloc
*line
, const char *if_marked
,
4396 bool emit_pch
, type_p field_type
, const char *field_name
)
4399 /* If the field reference is relative to V, rather than to some
4400 subcomponent of V, we can mark any subarrays with a single stride.
4401 We're effectively treating the field as a global variable in its
4403 if (v
&& type
== v
->type
)
4406 newv
.type
= field_type
;
4407 newv
.name
= ACONCAT ((v
->name
, ".", field_name
, NULL
));
4410 /* Otherwise, any arrays nested in the structure are too complex to
4412 else if (field_type
->kind
== TYPE_ARRAY
)
4414 write_root (f
, v
, field_type
, ACONCAT ((name
, ".", field_name
, NULL
)),
4415 has_length
, line
, if_marked
, emit_pch
);
4418 /* Write out to F the table entry and any marker routines needed to
4419 mark NAME as TYPE. V can be one of three values:
4421 - null, if NAME is too complex to represent using a single
4422 count and stride. In this case, it is an error for NAME to
4423 contain any gc-ed data.
4425 - the outermost array that contains NAME, if NAME is part of an array.
4427 - the C variable that contains NAME, if NAME is not part of an array.
4429 LINE is the line of the C source that declares the root variable.
4430 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
4431 is nonzero iff we are building the root table for hash table caches. */
4434 write_root (outf_p f
, pair_p v
, type_p type
, const char *name
, int has_length
,
4435 struct fileloc
*line
, const char *if_marked
, bool emit_pch
)
4442 for (fld
= type
->u
.s
.fields
; fld
; fld
= fld
->next
)
4445 const char *desc
= NULL
;
4448 for (o
= fld
->opt
; o
; o
= o
->next
)
4449 if (strcmp (o
->name
, "skip") == 0)
4451 else if (strcmp (o
->name
, "desc") == 0
4452 && o
->kind
== OPTION_STRING
)
4453 desc
= o
->info
.string
;
4454 else if (strcmp (o
->name
, "param_is") == 0)
4457 error_at_line (line
,
4458 "field `%s' of global `%s' has unknown option `%s'",
4459 fld
->name
, name
, o
->name
);
4463 else if (desc
&& fld
->type
->kind
== TYPE_UNION
)
4465 pair_p validf
= NULL
;
4468 for (ufld
= fld
->type
->u
.s
.fields
; ufld
; ufld
= ufld
->next
)
4470 const char *tag
= NULL
;
4472 for (oo
= ufld
->opt
; oo
; oo
= oo
->next
)
4473 if (strcmp (oo
->name
, "tag") == 0
4474 && oo
->kind
== OPTION_STRING
)
4475 tag
= oo
->info
.string
;
4476 if (tag
== NULL
|| strcmp (tag
, desc
) != 0)
4479 error_at_line (line
,
4480 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4481 name
, fld
->name
, validf
->name
,
4482 name
, fld
->name
, ufld
->name
, tag
);
4486 write_field_root (f
, v
, type
, name
, 0, line
, if_marked
,
4487 emit_pch
, validf
->type
,
4488 ACONCAT ((fld
->name
, ".",
4489 validf
->name
, NULL
)));
4492 error_at_line (line
,
4493 "global `%s.%s' has `desc' option but is not union",
4496 write_field_root (f
, v
, type
, name
, 0, line
, if_marked
,
4497 emit_pch
, fld
->type
, fld
->name
);
4505 newname
= xasprintf ("%s[0]", name
);
4506 write_root (f
, v
, type
->u
.a
.p
, newname
, has_length
, line
, if_marked
,
4512 case TYPE_USER_STRUCT
:
4513 error_at_line (line
, "`%s' must be a pointer type, because it is "
4514 "a GC root and its type is marked with GTY((user))",
4522 if (!start_root_entry (f
, v
, name
, line
))
4527 if (!has_length
&& union_or_struct_p (tp
))
4529 tp
= get_ultimate_base_class (tp
);
4530 const char *id_for_tag
= filter_type_name (tp
->u
.s
.tag
);
4531 oprintf (f
, " >_ggc_mx_%s,\n", id_for_tag
);
4533 oprintf (f
, " >_pch_nx_%s", id_for_tag
);
4535 oprintf (f
, " NULL");
4536 if (id_for_tag
!= tp
->u
.s
.tag
)
4537 free (CONST_CAST (char *, id_for_tag
));
4539 else if (!has_length
&& tp
->kind
== TYPE_PARAM_STRUCT
)
4541 oprintf (f
, " >_ggc_m_");
4542 output_mangled_typename (f
, tp
);
4545 oprintf (f
, ",\n >_pch_n_");
4546 output_mangled_typename (f
, tp
);
4549 oprintf (f
, ",\n NULL");
4552 && (tp
->kind
== TYPE_POINTER
|| union_or_struct_p (tp
)))
4554 oprintf (f
, " >_ggc_ma_%s,\n", name
);
4556 oprintf (f
, " >_pch_na_%s", name
);
4558 oprintf (f
, " NULL");
4562 error_at_line (line
,
4563 "global `%s' is pointer to unimplemented type",
4567 oprintf (f
, ",\n &%s", if_marked
);
4568 oprintf (f
, "\n },\n");
4574 if (!start_root_entry (f
, v
, name
, line
))
4577 oprintf (f
, " (gt_pointer_walker) >_ggc_m_S,\n");
4578 oprintf (f
, " (gt_pointer_walker) >_pch_n_S\n");
4579 oprintf (f
, " },\n");
4587 case TYPE_UNDEFINED
:
4589 case TYPE_LANG_STRUCT
:
4590 case TYPE_PARAM_STRUCT
:
4591 error_at_line (line
, "global `%s' is unimplemented type", name
);
4595 /* This generates a routine to walk an array. */
4598 write_array (outf_p f
, pair_p v
, const struct write_types_data
*wtd
)
4600 struct walk_type_data d
;
4603 memset (&d
, 0, sizeof (d
));
4609 d
.bitmap
= get_lang_bitmap (v
->line
.file
);
4612 d
.prev_val
[3] = prevval3
= xasprintf ("&%s", v
->name
);
4614 if (wtd
->param_prefix
)
4616 oprintf (f
, "static void gt_%sa_%s\n", wtd
->param_prefix
, v
->name
);
4617 oprintf (f
, " (void *, void *, gt_pointer_operator, void *);\n");
4618 oprintf (f
, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4619 wtd
->param_prefix
, v
->name
);
4621 " ATTRIBUTE_UNUSED void *x_p,\n"
4622 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4623 " ATTRIBUTE_UNUSED void * cookie)\n");
4624 oprintf (d
.of
, "{\n");
4625 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
4626 d
.process_field
= write_types_local_process_field
;
4627 d
.have_this_obj
= true;
4628 walk_type (v
->type
, &d
);
4629 oprintf (f
, "}\n\n");
4633 oprintf (f
, "static void gt_%sa_%s (void *);\n", wtd
->prefix
, v
->name
);
4634 oprintf (f
, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4635 wtd
->prefix
, v
->name
);
4637 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
4638 d
.process_field
= write_types_process_field
;
4639 d
.have_this_obj
= false;
4640 walk_type (v
->type
, &d
);
4642 oprintf (f
, "}\n\n");
4645 /* Output a table describing the locations and types of VARIABLES. */
4648 write_roots (pair_p variables
, bool emit_pch
)
4651 struct flist
*flp
= NULL
;
4653 for (v
= variables
; v
; v
= v
->next
)
4656 get_output_file_with_visibility (CONST_CAST (input_file
*,
4659 const char *length
= NULL
;
4660 int deletable_p
= 0;
4662 for (o
= v
->opt
; o
; o
= o
->next
)
4663 if (strcmp (o
->name
, "length") == 0
4664 && o
->kind
== OPTION_STRING
)
4665 length
= o
->info
.string
;
4666 else if (strcmp (o
->name
, "deletable") == 0)
4668 else if (strcmp (o
->name
, "param_is") == 0)
4670 else if (strncmp (o
->name
, "param", 5) == 0
4671 && ISDIGIT (o
->name
[5]) && strcmp (o
->name
+ 6, "_is") == 0)
4673 else if (strcmp (o
->name
, "if_marked") == 0)
4676 error_at_line (&v
->line
,
4677 "global `%s' has unknown option `%s'",
4680 for (fli
= flp
; fli
; fli
= fli
->next
)
4681 if (fli
->f
== f
&& f
)
4685 fli
= XNEW (struct flist
);
4689 fli
->file
= v
->line
.file
;
4690 gcc_assert (fli
->file
);
4693 oprintf (f
, "\n/* GC roots. */\n\n");
4698 && v
->type
->kind
== TYPE_POINTER
4699 && (v
->type
->u
.p
->kind
== TYPE_POINTER
4700 || v
->type
->u
.p
->kind
== TYPE_STRUCT
))
4702 write_array (f
, v
, &ggc_wtd
);
4703 write_array (f
, v
, &pch_wtd
);
4707 for (v
= variables
; v
; v
= v
->next
)
4709 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4716 for (o
= v
->opt
; o
; o
= o
->next
)
4717 if (strcmp (o
->name
, "length") == 0)
4719 else if (strcmp (o
->name
, "deletable") == 0
4720 || strcmp (o
->name
, "if_marked") == 0)
4726 for (fli
= flp
; fli
; fli
= fli
->next
)
4729 if (!fli
->started_p
)
4733 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4734 put_mangled_filename (f
, v
->line
.file
);
4735 oprintf (f
, "[] = {\n");
4738 write_root (f
, v
, v
->type
, v
->name
, length_p
, &v
->line
, NULL
, emit_pch
);
4741 finish_root_table (flp
, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4744 for (v
= variables
; v
; v
= v
->next
)
4746 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4752 for (o
= v
->opt
; o
; o
= o
->next
)
4753 if (strcmp (o
->name
, "deletable") == 0)
4755 else if (strcmp (o
->name
, "if_marked") == 0)
4761 for (fli
= flp
; fli
; fli
= fli
->next
)
4764 if (!fli
->started_p
)
4768 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4769 put_mangled_filename (f
, v
->line
.file
);
4770 oprintf (f
, "[] = {\n");
4773 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4777 finish_root_table (flp
, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4778 "gt_ggc_deletable_rtab");
4780 for (v
= variables
; v
; v
= v
->next
)
4782 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4785 const char *if_marked
= NULL
;
4789 for (o
= v
->opt
; o
; o
= o
->next
)
4790 if (strcmp (o
->name
, "length") == 0)
4792 else if (strcmp (o
->name
, "if_marked") == 0
4793 && o
->kind
== OPTION_STRING
)
4794 if_marked
= o
->info
.string
;
4795 if (if_marked
== NULL
)
4797 if (v
->type
->kind
!= TYPE_POINTER
4798 || v
->type
->u
.p
->kind
!= TYPE_PARAM_STRUCT
4799 || v
->type
->u
.p
->u
.param_struct
.stru
!= find_structure ("htab",
4802 error_at_line (&v
->line
,
4803 "if_marked option used but not hash table");
4807 for (fli
= flp
; fli
; fli
= fli
->next
)
4810 if (!fli
->started_p
)
4814 oprintf (f
, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4815 put_mangled_filename (f
, v
->line
.file
);
4816 oprintf (f
, "[] = {\n");
4819 write_root (f
, v
, v
->type
->u
.p
->u
.param_struct
.param
[0],
4820 v
->name
, length_p
, &v
->line
, if_marked
, emit_pch
);
4823 finish_root_table (flp
, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4824 "gt_ggc_cache_rtab");
4829 for (v
= variables
; v
; v
= v
->next
)
4831 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4835 int if_marked_p
= 0;
4838 for (o
= v
->opt
; o
; o
= o
->next
)
4839 if (strcmp (o
->name
, "length") == 0)
4841 else if (strcmp (o
->name
, "if_marked") == 0)
4847 for (fli
= flp
; fli
; fli
= fli
->next
)
4850 if (!fli
->started_p
)
4854 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4855 put_mangled_filename (f
, v
->line
.file
);
4856 oprintf (f
, "[] = {\n");
4859 write_root (f
, v
, v
->type
, v
->name
, length_p
, &v
->line
, NULL
, emit_pch
);
4862 finish_root_table (flp
, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4863 "gt_pch_cache_rtab");
4865 for (v
= variables
; v
; v
= v
->next
)
4867 outf_p f
= get_output_file_with_visibility (CONST_CAST (input_file
*,
4873 for (o
= v
->opt
; o
; o
= o
->next
)
4874 if (strcmp (o
->name
, "deletable") == 0
4875 || strcmp (o
->name
, "if_marked") == 0)
4884 if (!contains_scalar_p (v
->type
))
4887 for (fli
= flp
; fli
; fli
= fli
->next
)
4890 if (!fli
->started_p
)
4894 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4895 put_mangled_filename (f
, v
->line
.file
);
4896 oprintf (f
, "[] = {\n");
4899 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4903 finish_root_table (flp
, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4904 "gt_pch_scalar_rtab");
4907 /* TRUE if type S has the GTY variable_size annotation. */
4910 variable_size_p (const type_p s
)
4913 for (o
= s
->u
.s
.opt
; o
; o
= o
->next
)
4914 if (strcmp (o
->name
, "variable_size") == 0)
4922 /* Writes one typed allocator definition into output F for type
4923 identifier TYPE_NAME with optional type specifier TYPE_SPECIFIER.
4924 The allocator name will contain ALLOCATOR_TYPE. If VARIABLE_SIZE
4925 is true, the allocator will have an extra parameter specifying
4926 number of bytes to allocate. If QUANTITY is set to VECTOR, a
4927 vector allocator will be output. */
4930 write_typed_alloc_def (outf_p f
,
4931 bool variable_size
, const char *type_specifier
,
4932 const char *type_name
, const char *allocator_type
,
4933 enum alloc_quantity quantity
)
4935 bool two_args
= variable_size
&& (quantity
== vector
);
4936 gcc_assert (f
!= NULL
);
4937 const char *type_name_as_id
= filter_type_name (type_name
);
4938 oprintf (f
, "#define ggc_alloc_%s%s", allocator_type
, type_name_as_id
);
4939 oprintf (f
, "(%s%s%s) ",
4940 (variable_size
? "SIZE" : ""),
4941 (two_args
? ", " : ""),
4942 (quantity
== vector
) ? "n" : "");
4943 oprintf (f
, "((%s%s *)", type_specifier
, type_name
);
4944 oprintf (f
, "(ggc_internal_%salloc_stat (", allocator_type
);
4946 oprintf (f
, "SIZE");
4948 oprintf (f
, "sizeof (%s%s)", type_specifier
, type_name
);
4949 if (quantity
== vector
)
4951 oprintf (f
, " MEM_STAT_INFO)))\n");
4952 if (type_name_as_id
!= type_name
)
4953 free (CONST_CAST (char *, type_name_as_id
));
4956 /* Writes a typed allocator definition into output F for a struct or
4957 union S, with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4960 write_typed_struct_alloc_def (outf_p f
,
4961 const type_p s
, const char *allocator_type
,
4962 enum alloc_quantity quantity
)
4964 gcc_assert (union_or_struct_p (s
));
4965 write_typed_alloc_def (f
, variable_size_p (s
), get_type_specifier (s
),
4966 s
->u
.s
.tag
, allocator_type
, quantity
);
4969 /* Writes a typed allocator definition into output F for a typedef P,
4970 with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4973 write_typed_typedef_alloc_def (outf_p f
,
4974 const pair_p p
, const char *allocator_type
,
4975 enum alloc_quantity quantity
)
4977 write_typed_alloc_def (f
, variable_size_p (p
->type
), "", p
->name
,
4978 allocator_type
, quantity
);
4981 /* Writes typed allocator definitions into output F for the types in
4982 STRUCTURES and TYPEDEFS that are used by GC. */
4985 write_typed_alloc_defns (outf_p f
,
4986 const type_p structures
, const pair_p typedefs
)
4991 gcc_assert (f
!= NULL
);
4993 "\n/* Allocators for known structs and unions. */\n\n");
4994 for (s
= structures
; s
; s
= s
->next
)
4996 if (!USED_BY_TYPED_GC_P (s
))
4998 gcc_assert (union_or_struct_p (s
));
4999 /* In plugin mode onput output ggc_alloc macro definitions
5000 relevant to plugin input files. */
5001 if (nb_plugin_files
> 0
5002 && ((s
->u
.s
.line
.file
== NULL
) || !s
->u
.s
.line
.file
->inpisplugin
))
5004 write_typed_struct_alloc_def (f
, s
, "", single
);
5005 write_typed_struct_alloc_def (f
, s
, "cleared_", single
);
5006 write_typed_struct_alloc_def (f
, s
, "vec_", vector
);
5007 write_typed_struct_alloc_def (f
, s
, "cleared_vec_", vector
);
5010 oprintf (f
, "\n/* Allocators for known typedefs. */\n");
5011 for (p
= typedefs
; p
; p
= p
->next
)
5014 if (!USED_BY_TYPED_GC_P (s
) || (strcmp (p
->name
, s
->u
.s
.tag
) == 0))
5016 /* In plugin mode onput output ggc_alloc macro definitions
5017 relevant to plugin input files. */
5018 if (nb_plugin_files
> 0)
5020 struct fileloc
* filoc
= type_fileloc (s
);
5021 if (!filoc
|| !filoc
->file
->inpisplugin
)
5024 write_typed_typedef_alloc_def (f
, p
, "", single
);
5025 write_typed_typedef_alloc_def (f
, p
, "cleared_", single
);
5026 write_typed_typedef_alloc_def (f
, p
, "vec_", vector
);
5027 write_typed_typedef_alloc_def (f
, p
, "cleared_vec_", vector
);
5031 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
5032 guaranteee for somewhat increased readability. If name conflicts do happen,
5033 this funcion will have to be adjusted to be more like
5034 output_mangled_typename. */
5037 output_typename (outf_p of
, const_type_p t
)
5042 oprintf (of
, "str");
5045 oprintf (of
, "scalar");
5048 output_typename (of
, t
->u
.p
);
5051 case TYPE_USER_STRUCT
:
5053 case TYPE_LANG_STRUCT
:
5054 oprintf (of
, "%s", t
->u
.s
.tag
);
5056 case TYPE_PARAM_STRUCT
:
5059 for (i
= 0; i
< NUM_PARAM
; i
++)
5060 if (t
->u
.param_struct
.param
[i
] != NULL
)
5062 output_typename (of
, t
->u
.param_struct
.param
[i
]);
5065 output_typename (of
, t
->u
.param_struct
.stru
);
5069 case TYPE_UNDEFINED
:
5075 /* Writes a typed GC allocator for type S that is suitable as a callback for
5076 the splay tree implementation in libiberty. */
5079 write_splay_tree_allocator_def (const_type_p s
)
5081 outf_p of
= get_output_file_with_visibility (NULL
);
5082 oprintf (of
, "void * ggc_alloc_splay_tree_");
5083 output_typename (of
, s
);
5084 oprintf (of
, " (int sz, void * nl)\n");
5085 oprintf (of
, "{\n");
5086 oprintf (of
, " return ggc_splay_alloc (sz, nl);\n");
5087 oprintf (of
, "}\n\n");
5090 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
5091 for the splay tree implementation in libiberty. */
5094 write_splay_tree_allocators (const_type_p param_structs
)
5098 oprintf (header_file
, "\n/* Splay tree callback allocators. */\n");
5099 for (s
= param_structs
; s
; s
= s
->next
)
5100 if (s
->gc_used
== GC_POINTED_TO
)
5102 oprintf (header_file
, "extern void * ggc_alloc_splay_tree_");
5103 output_typename (header_file
, s
);
5104 oprintf (header_file
, " (int, void *);\n");
5105 write_splay_tree_allocator_def (s
);
5111 /* Dumps the value of typekind KIND. */
5114 dump_typekind (int indent
, enum typekind kind
)
5116 printf ("%*ckind = ", indent
, ' ');
5120 printf ("TYPE_SCALAR");
5123 printf ("TYPE_STRING");
5126 printf ("TYPE_STRUCT");
5128 case TYPE_UNDEFINED
:
5129 printf ("TYPE_UNDEFINED");
5131 case TYPE_USER_STRUCT
:
5132 printf ("TYPE_USER_STRUCT");
5135 printf ("TYPE_UNION");
5138 printf ("TYPE_POINTER");
5141 printf ("TYPE_ARRAY");
5143 case TYPE_LANG_STRUCT
:
5144 printf ("TYPE_LANG_STRUCT");
5146 case TYPE_PARAM_STRUCT
:
5147 printf ("TYPE_PARAM_STRUCT");
5155 /* Dumps the value of GC_USED flag. */
5158 dump_gc_used (int indent
, enum gc_used_enum gc_used
)
5160 printf ("%*cgc_used = ", indent
, ' ');
5164 printf ("GC_UNUSED");
5169 case GC_MAYBE_POINTED_TO
:
5170 printf ("GC_MAYBE_POINTED_TO");
5173 printf ("GC_POINTED_TO");
5181 /* Dumps the type options OPT. */
5184 dump_options (int indent
, options_p opt
)
5187 printf ("%*coptions = ", indent
, ' ');
5194 printf ("%s:string %s ", o
->name
, o
->info
.string
);
5197 printf ("%s:type ", o
->name
);
5198 dump_type (indent
+1, o
->info
.type
);
5201 printf ("%s:nested ", o
->name
);
5211 /* Dumps the source file location in LINE. */
5214 dump_fileloc (int indent
, struct fileloc line
)
5216 printf ("%*cfileloc: file = %s, line = %d\n", indent
, ' ',
5217 get_input_file_name (line
.file
),
5221 /* Recursively dumps the struct, union, or a language-specific
5225 dump_type_u_s (int indent
, type_p t
)
5229 gcc_assert (union_or_struct_p (t
));
5230 printf ("%*cu.s.tag = %s\n", indent
, ' ', t
->u
.s
.tag
);
5231 dump_fileloc (indent
, t
->u
.s
.line
);
5232 printf ("%*cu.s.fields =\n", indent
, ' ');
5233 fields
= t
->u
.s
.fields
;
5236 dump_pair (indent
+ INDENT
, fields
);
5237 fields
= fields
->next
;
5239 printf ("%*cend of fields of type %p\n", indent
, ' ', (void *) t
);
5240 dump_options (indent
, t
->u
.s
.opt
);
5241 printf ("%*cu.s.bitmap = %X\n", indent
, ' ', t
->u
.s
.bitmap
);
5242 if (t
->kind
== TYPE_LANG_STRUCT
)
5244 printf ("%*cu.s.lang_struct:\n", indent
, ' ');
5245 dump_type_list (indent
+ INDENT
, t
->u
.s
.lang_struct
);
5249 /* Recursively dumps the array T. */
5252 dump_type_u_a (int indent
, type_p t
)
5254 gcc_assert (t
->kind
== TYPE_ARRAY
);
5255 printf ("%*clen = %s, u.a.p:\n", indent
, ' ', t
->u
.a
.len
);
5256 dump_type_list (indent
+ INDENT
, t
->u
.a
.p
);
5259 /* Recursively dumps the parameterized struct T. */
5262 dump_type_u_param_struct (int indent
, type_p t
)
5265 gcc_assert (t
->kind
== TYPE_PARAM_STRUCT
);
5266 printf ("%*cu.param_struct.stru:\n", indent
, ' ');
5267 dump_type_list (indent
, t
->u
.param_struct
.stru
);
5268 dump_fileloc (indent
, t
->u
.param_struct
.line
);
5269 for (i
= 0; i
< NUM_PARAM
; i
++)
5271 if (t
->u
.param_struct
.param
[i
] == NULL
)
5273 printf ("%*cu.param_struct.param[%d]:\n", indent
, ' ', i
);
5274 dump_type (indent
+ INDENT
, t
->u
.param_struct
.param
[i
]);
5278 /* Recursively dumps the type list T. */
5281 dump_type_list (int indent
, type_p t
)
5286 dump_type (indent
, p
);
5291 static htab_t seen_types
;
5293 /* Recursively dumps the type T if it was not dumped previously. */
5296 dump_type (int indent
, type_p t
)
5300 if (seen_types
== NULL
)
5301 seen_types
= htab_create (100, htab_hash_pointer
, htab_eq_pointer
, NULL
);
5303 printf ("%*cType at %p: ", indent
, ' ', (void *) t
);
5304 slot
= htab_find_slot (seen_types
, t
, INSERT
);
5307 printf ("already seen.\n");
5313 dump_typekind (indent
, t
->kind
);
5314 printf ("%*cpointer_to = %p\n", indent
+ INDENT
, ' ',
5315 (void *) t
->pointer_to
);
5316 dump_gc_used (indent
+ INDENT
, t
->gc_used
);
5320 printf ("%*cscalar_is_char = %s\n", indent
+ INDENT
, ' ',
5321 t
->u
.scalar_is_char
? "true" : "false");
5327 case TYPE_LANG_STRUCT
:
5328 case TYPE_USER_STRUCT
:
5329 dump_type_u_s (indent
+ INDENT
, t
);
5332 printf ("%*cp:\n", indent
+ INDENT
, ' ');
5333 dump_type (indent
+ INDENT
, t
->u
.p
);
5336 dump_type_u_a (indent
+ INDENT
, t
);
5338 case TYPE_PARAM_STRUCT
:
5339 dump_type_u_param_struct (indent
+ INDENT
, t
);
5344 printf ("%*cEnd of type at %p\n", indent
, ' ', (void *) t
);
5347 /* Dumps the pair P. */
5350 dump_pair (int indent
, pair_p p
)
5352 printf ("%*cpair: name = %s\n", indent
, ' ', p
->name
);
5353 dump_type (indent
, p
->type
);
5354 dump_fileloc (indent
, p
->line
);
5355 dump_options (indent
, p
->opt
);
5356 printf ("%*cEnd of pair %s\n", indent
, ' ', p
->name
);
5359 /* Dumps the list of pairs PP. */
5362 dump_pair_list (const char *name
, pair_p pp
)
5365 printf ("%s:\n", name
);
5366 for (p
= pp
; p
!= NULL
; p
= p
->next
)
5368 printf ("End of %s\n\n", name
);
5371 /* Dumps the STRUCTURES. */
5374 dump_structures (const char *name
, type_p structures
)
5376 printf ("%s:\n", name
);
5377 dump_type_list (0, structures
);
5378 printf ("End of %s\n\n", name
);
5381 /* Dumps the internal structures of gengtype. This is useful to debug
5382 gengtype itself, or to understand what it does, e.g. for plugin
5386 dump_everything (void)
5388 dump_pair_list ("typedefs", typedefs
);
5389 dump_structures ("structures", structures
);
5390 dump_structures ("param_structs", param_structs
);
5391 dump_pair_list ("variables", variables
);
5393 /* Allocated with the first call to dump_type. */
5394 htab_delete (seen_types
);
5399 /* Option specification for getopt_long. */
5400 static const struct option gengtype_long_options
[] = {
5401 {"help", no_argument
, NULL
, 'h'},
5402 {"version", no_argument
, NULL
, 'V'},
5403 {"verbose", no_argument
, NULL
, 'v'},
5404 {"dump", no_argument
, NULL
, 'd'},
5405 {"debug", no_argument
, NULL
, 'D'},
5406 {"plugin", required_argument
, NULL
, 'P'},
5407 {"srcdir", required_argument
, NULL
, 'S'},
5408 {"backupdir", required_argument
, NULL
, 'B'},
5409 {"inputs", required_argument
, NULL
, 'I'},
5410 {"read-state", required_argument
, NULL
, 'r'},
5411 {"write-state", required_argument
, NULL
, 'w'},
5412 /* Terminating NULL placeholder. */
5413 {NULL
, no_argument
, NULL
, 0},
5420 printf ("Usage: %s\n", progname
);
5421 printf ("\t -h | --help " " \t# Give this help.\n");
5422 printf ("\t -D | --debug "
5423 " \t# Give debug output to debug %s itself.\n", progname
);
5424 printf ("\t -V | --version " " \t# Give version information.\n");
5425 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5426 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5427 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5428 " \t# Generate for plugin.\n");
5429 printf ("\t -S | --srcdir <GCC-directory> "
5430 " \t# Specify the GCC source directory.\n");
5431 printf ("\t -B | --backupdir <directory> "
5432 " \t# Specify the backup directory for updated files.\n");
5433 printf ("\t -I | --inputs <input-list> "
5434 " \t# Specify the file with source files list.\n");
5435 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5436 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5440 print_version (void)
5442 printf ("%s %s%s\n", progname
, pkgversion_string
, version_string
);
5443 printf ("Report bugs: %s\n", bug_report_url
);
5446 /* Parse the program options using getopt_long... */
5448 parse_program_options (int argc
, char **argv
)
5451 while ((opt
= getopt_long (argc
, argv
, "hVvdP:S:B:I:w:r:D",
5452 gengtype_long_options
, NULL
)) >= 0)
5456 case 'h': /* --help */
5459 case 'V': /* --version */
5462 case 'd': /* --dump */
5465 case 'D': /* --debug */
5468 case 'v': /* --verbose */
5471 case 'P': /* --plugin */
5473 plugin_output_filename
= optarg
;
5475 fatal ("missing plugin output file name");
5477 case 'S': /* --srcdir */
5481 fatal ("missing source directory");
5482 srcdir_len
= strlen (srcdir
);
5484 case 'B': /* --backupdir */
5486 backup_dir
= optarg
;
5488 fatal ("missing backup directory");
5490 case 'I': /* --inputs */
5494 fatal ("missing input list");
5496 case 'r': /* --read-state */
5498 read_state_filename
= optarg
;
5500 fatal ("missing read state file");
5501 DBGPRINTF ("read state %s\n", optarg
);
5503 case 'w': /* --write-state */
5504 DBGPRINTF ("write state %s\n", optarg
);
5506 write_state_filename
= optarg
;
5508 fatal ("missing write state file");
5511 fprintf (stderr
, "%s: unknown flag '%c'\n", progname
, opt
);
5513 fatal ("unexpected flag");
5516 if (plugin_output_filename
)
5518 /* In plugin mode we require some input files. */
5521 fatal ("no source files given in plugin mode");
5522 nb_plugin_files
= argc
- optind
;
5523 plugin_files
= XNEWVEC (input_file
*, nb_plugin_files
);
5524 for (i
= 0; i
< (int) nb_plugin_files
; i
++)
5526 char *name
= argv
[i
+ optind
];
5527 plugin_files
[i
] = input_file_by_name (name
);
5534 /******* Manage input files. ******/
5536 /* Hash table of unique input file names. */
5537 static htab_t input_file_htab
;
5539 /* Find or allocate a new input_file by hash-consing it. */
5541 input_file_by_name (const char* name
)
5544 input_file
* f
= NULL
;
5548 namlen
= strlen (name
);
5549 f
= XCNEWVAR (input_file
, sizeof (input_file
)+namlen
+2);
5552 f
->inpisplugin
= false;
5553 strcpy (f
->inpname
, name
);
5554 slot
= htab_find_slot (input_file_htab
, f
, INSERT
);
5555 gcc_assert (slot
!= NULL
);
5558 /* Already known input file. */
5560 return (input_file
*)(*slot
);
5562 /* New input file. */
5567 /* Hash table support routines for input_file-s. */
5569 htab_hash_inputfile (const void *p
)
5571 const input_file
*inpf
= (const input_file
*) p
;
5573 return htab_hash_string (get_input_file_name (inpf
));
5577 htab_eq_inputfile (const void *x
, const void *y
)
5579 const input_file
*inpfx
= (const input_file
*) x
;
5580 const input_file
*inpfy
= (const input_file
*) y
;
5581 gcc_assert (inpfx
!= NULL
&& inpfy
!= NULL
);
5582 return !filename_cmp (get_input_file_name (inpfx
), get_input_file_name (inpfy
));
5587 main (int argc
, char **argv
)
5590 static struct fileloc pos
= { NULL
, 0 };
5591 outf_p output_header
;
5593 /* Mandatory common initializations. */
5594 progname
= "gengtype"; /* For fatal and messages. */
5595 /* Create the hash-table used to hash-cons input files. */
5597 htab_create (800, htab_hash_inputfile
, htab_eq_inputfile
, NULL
);
5598 /* Initialize our special input files. */
5599 this_file
= input_file_by_name (__FILE__
);
5600 system_h_file
= input_file_by_name ("system.h");
5601 /* Set the scalar_is_char union number for predefined scalar types. */
5602 scalar_nonchar
.u
.scalar_is_char
= FALSE
;
5603 scalar_char
.u
.scalar_is_char
= TRUE
;
5605 parse_program_options (argc
, argv
);
5610 time_t now
= (time_t) 0;
5612 DBGPRINTF ("gengtype started pid %d at %s",
5613 (int) getpid (), ctime (&now
));
5615 #endif /* ENABLE_CHECKING */
5617 /* Parse the input list and the input files. */
5618 DBGPRINTF ("inputlist %s", inputlist
);
5619 if (read_state_filename
)
5622 fatal ("input list %s cannot be given with a read state file %s",
5623 inputlist
, read_state_filename
);
5624 read_state (read_state_filename
);
5625 DBGPRINT_COUNT_TYPE ("structures after read_state", structures
);
5626 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs
);
5630 /* These types are set up with #define or else outside of where
5631 we can see them. We should initialize them before calling
5633 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5635 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos
));
5636 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos
));
5637 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos
));
5638 POS_HERE (do_scalar_typedef ("double_int", &pos
));
5639 POS_HERE (do_scalar_typedef ("uint64_t", &pos
));
5640 POS_HERE (do_scalar_typedef ("uint8", &pos
));
5641 POS_HERE (do_scalar_typedef ("uintptr_t", &pos
));
5642 POS_HERE (do_scalar_typedef ("jword", &pos
));
5643 POS_HERE (do_scalar_typedef ("JCF_u2", &pos
));
5644 POS_HERE (do_scalar_typedef ("void", &pos
));
5645 POS_HERE (do_typedef ("PTR",
5646 create_pointer (resolve_typedef ("void", &pos
)),
5649 read_input_list (inputlist
);
5650 for (i
= 0; i
< num_gt_files
; i
++)
5652 parse_file (get_input_file_name (gt_files
[i
]));
5653 DBGPRINTF ("parsed file #%d %s",
5654 (int) i
, get_input_file_name (gt_files
[i
]));
5656 if (verbosity_level
>= 1)
5657 printf ("%s parsed %d files with %d GTY types\n",
5658 progname
, (int) num_gt_files
, type_count
);
5660 DBGPRINT_COUNT_TYPE ("structures after parsing", structures
);
5661 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs
);
5665 fatal ("either an input list or a read state file should be given");
5670 if (plugin_output_filename
)
5673 /* In plugin mode, we should have read a state file, and have
5674 given at least one plugin file. */
5675 if (!read_state_filename
)
5676 fatal ("No read state given in plugin mode for %s",
5677 plugin_output_filename
);
5679 if (nb_plugin_files
== 0 || !plugin_files
)
5680 fatal ("No plugin files given in plugin mode for %s",
5681 plugin_output_filename
);
5683 /* Parse our plugin files and augment the state. */
5684 for (ix
= 0; ix
< nb_plugin_files
; ix
++)
5686 input_file
* pluginput
= plugin_files
[ix
];
5687 pluginput
->inpisplugin
= true;
5688 parse_file (get_input_file_name (pluginput
));
5693 plugin_output
= create_file ("GCC", plugin_output_filename
);
5694 DBGPRINTF ("created plugin_output %p named %s",
5695 (void *) plugin_output
, plugin_output
->name
);
5698 { /* No plugin files, we are in normal mode. */
5700 fatal ("gengtype needs a source directory in normal mode");
5707 /* The call to set_gc_used may indirectly call find_param_structure
5708 hence enlarge the param_structs list of types. */
5709 set_gc_used (variables
);
5711 /* The state at this point is read from the state input file or by
5712 parsing source files and optionally augmented by parsing plugin
5713 source files. Write it now. */
5714 if (write_state_filename
)
5716 DBGPRINT_COUNT_TYPE ("structures before write_state", structures
);
5717 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs
);
5720 fatal ("didn't write state file %s after errors",
5721 write_state_filename
);
5723 DBGPRINTF ("before write_state %s", write_state_filename
);
5724 write_state (write_state_filename
);
5729 /* After having written the state file we return immediately to
5730 avoid generating any output file. */
5740 output_header
= plugin_output
? plugin_output
: header_file
;
5741 write_typed_alloc_defns (output_header
, structures
, typedefs
);
5742 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5744 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5747 write_types (output_header
, structures
, param_structs
, &ggc_wtd
);
5748 if (plugin_files
== NULL
)
5750 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5752 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5754 write_types (header_file
, structures
, param_structs
, &pch_wtd
);
5755 write_local (header_file
, structures
, param_structs
);
5757 write_splay_tree_allocators (param_structs
);
5758 write_roots (variables
, plugin_files
== NULL
);
5760 close_output_files ();
5765 /* Don't bother about free-ing any input or plugin file, etc. */