1/* Process source files and output type information.
2 Copyright (C) 2002-2023 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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
9 version.
10
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
14 for more details.
15
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/>. */
19
20#ifdef HOST_GENERATOR_FILE
21#include "config.h"
22#define GENERATOR_FILE 1
23#else
24#include "bconfig.h"
25#endif
26#include "system.h"
27#include "errors.h" /* for fatal */
28#include "getopt.h"
29#include "version.h" /* for version_string & pkgversion_string. */
30#include "xregex.h"
31#include "obstack.h"
32#include "gengtype.h"
33#include "filenames.h"
34
35/* Data types, macros, etc. used only in this file. */
36
37
38/* The list of output files. */
39outf_p output_files;
40
41/* The output header file that is included into pretty much every
42 source file. */
43outf_p header_file;
44
45
46/* The name of the file containing the list of input files. */
47static char *inputlist;
48
49/* The plugin input files and their number; in that case only
50 a single file is produced. */
51static input_file **plugin_files;
52static size_t nb_plugin_files;
53
54/* The generated plugin output file and name. */
55static outf_p plugin_output;
56static char *plugin_output_filename;
57
58/* Our source directory and its length. */
59const char *srcdir;
60size_t srcdir_len;
61
62/* Variables used for reading and writing the state. */
63const char *read_state_filename;
64const char *write_state_filename;
65
66/* Variables to help debugging. */
67int do_dump;
68int do_debug;
69
70/* Level for verbose messages. */
71int verbosity_level;
72
73/* We have a type count and use it to set the state_number of newly
74 allocated types to some unique negative number. */
75static int type_count;
76
77/* The backup directory should be in the same file system as the
78 generated files, otherwise the rename(2) system call would fail.
79 If NULL, no backup is made when overwriting a generated file. */
80static const char* backup_dir; /* (-B) program option. */
81
82
83static outf_p create_file (const char *, const char *);
84
85static const char *get_file_basename (const input_file *);
86static const char *get_file_realbasename (const input_file *);
87
88static int get_prefix_langdir_index (const char *);
89static const char *get_file_langdir (const input_file *);
90
91static void dump_pair (int indent, pair_p p);
92static void dump_type (int indent, type_p p);
93static void dump_type_list (int indent, type_p p);
94
95
96/* Nonzero iff an error has occurred. */
97bool hit_error = false;
98
99static void gen_rtx_next (void);
100static void write_rtx_next (void);
101static void open_base_files (void);
102static void close_output_files (void);
103
104/* Report an error at POS, printing MSG. */
105
106void
107error_at_line (const struct fileloc *pos, const char *msg, ...)
108{
109 va_list ap;
110
111 gcc_assert (pos != NULL && pos->file != NULL);
112 va_start (ap, msg);
113
114 fprintf (stderr, format: "%s:%d: ", get_input_file_name (inpf: pos->file), pos->line);
115 vfprintf (stderr, format: msg, arg: ap);
116 fputc ('\n', stderr);
117 hit_error = true;
118
119 va_end (ap);
120}
121
122/* Locate the ultimate base class of struct S. */
123
124static const_type_p
125get_ultimate_base_class (const_type_p s)
126{
127 while (s->u.s.base_class)
128 s = s->u.s.base_class;
129 return s;
130}
131
132static type_p
133get_ultimate_base_class (type_p s)
134{
135 while (s->u.s.base_class)
136 s = s->u.s.base_class;
137 return s;
138}
139
140/* Input file handling. */
141
142/* Table of all input files. */
143const input_file **gt_files;
144size_t num_gt_files;
145
146/* Table of headers to be included in gtype-desc.cc that are generated
147 during the build. These are identified as "./<filename>.h". */
148const char **build_headers;
149size_t num_build_headers;
150
151/* A number of places use the name of this "gengtype.cc" file for a
152 location for things that we can't rely on the source to define.
153 Make sure we can still use pointer comparison on filenames. */
154input_file* this_file;
155/* The "system.h" file is likewise specially useful. */
156input_file* system_h_file;
157
158/* Vector of per-language directories. */
159const char **lang_dir_names;
160size_t num_lang_dirs;
161
162/* An array of output files suitable for definitions. There is one
163 BASE_FILES entry for each language. */
164static outf_p *base_files;
165
166/* Utility debugging function, printing the various type counts within
167 a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
168void
169dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
170{
171 int nb_types = 0, nb_scalar = 0, nb_string = 0;
172 int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
173 int nb_lang_struct = 0;
174 int nb_user_struct = 0, nb_undefined = 0;
175 int nb_callback = 0;
176 type_p p = NULL;
177 for (p = t; p; p = p->next)
178 {
179 nb_types++;
180 switch (p->kind)
181 {
182 case TYPE_UNDEFINED:
183 nb_undefined++;
184 break;
185 case TYPE_SCALAR:
186 nb_scalar++;
187 break;
188 case TYPE_STRING:
189 nb_string++;
190 break;
191 case TYPE_STRUCT:
192 nb_struct++;
193 break;
194 case TYPE_USER_STRUCT:
195 nb_user_struct++;
196 break;
197 case TYPE_UNION:
198 nb_union++;
199 break;
200 case TYPE_POINTER:
201 nb_pointer++;
202 break;
203 case TYPE_ARRAY:
204 nb_array++;
205 break;
206 case TYPE_CALLBACK:
207 nb_callback++;
208 break;
209 case TYPE_LANG_STRUCT:
210 nb_lang_struct++;
211 break;
212 case TYPE_NONE:
213 gcc_unreachable ();
214 }
215 }
216 fprintf (stderr, format: "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
217 lbasename (fil), lin, msg, nb_types);
218 if (nb_scalar > 0 || nb_string > 0)
219 fprintf (stderr, format: "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
220 if (nb_struct > 0 || nb_union > 0)
221 fprintf (stderr, format: "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
222 if (nb_pointer > 0 || nb_array > 0)
223 fprintf (stderr, format: "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
224 if (nb_callback > 0)
225 fprintf (stderr, format: "@@%%@@ %d callbacks\n", nb_callback);
226 if (nb_lang_struct > 0)
227 fprintf (stderr, format: "@@%%@@ %d lang_structs\n", nb_lang_struct);
228 if (nb_user_struct > 0)
229 fprintf (stderr, format: "@@%%@@ %d user_structs\n", nb_user_struct);
230 if (nb_undefined > 0)
231 fprintf (stderr, format: "@@%%@@ %d undefined types\n", nb_undefined);
232 fprintf (stderr, format: "\n");
233}
234
235/* Scan the input file, LIST, and determine how much space we need to
236 store strings in. Also, count the number of language directories
237 and files. The numbers returned are overestimates as they does not
238 consider repeated files. */
239static size_t
240measure_input_list (FILE *list)
241{
242 size_t n = 0;
243 int c;
244 bool atbol = true;
245 num_lang_dirs = 0;
246 num_gt_files = plugin_files ? nb_plugin_files : 0;
247 while ((c = getc (list)) != EOF)
248 {
249 n++;
250 if (atbol)
251 {
252 if (c == '[')
253 num_lang_dirs++;
254 else
255 {
256 /* Add space for a lang_bitmap before the input file name. */
257 n += sizeof (lang_bitmap);
258 num_gt_files++;
259 }
260 atbol = false;
261 }
262
263 if (c == '\n')
264 atbol = true;
265 }
266
267 rewind (stream: list);
268 return n;
269}
270
271/* Read one input line from LIST to HEREP (which is updated). A
272 pointer to the string is returned via LINEP. If it was a language
273 subdirectory in square brackets, strip off the square brackets and
274 return true. Otherwise, leave space before the string for a
275 lang_bitmap, and return false. At EOF, returns false, does not
276 touch *HEREP, and sets *LINEP to NULL. POS is used for
277 diagnostics. */
278static bool
279read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
280{
281 char *here = *herep;
282 char *line;
283 int c = getc (list);
284
285 /* Read over whitespace. */
286 while (c == '\n' || c == ' ')
287 c = getc (list);
288
289 if (c == EOF)
290 {
291 *linep = 0;
292 return false;
293 }
294 else if (c == '[')
295 {
296 /* No space for a lang_bitmap is necessary. Discard the '['. */
297 c = getc (list);
298 line = here;
299 while (c != ']' && c != '\n' && c != EOF)
300 {
301 *here++ = c;
302 c = getc (list);
303 }
304 *here++ = '\0';
305
306 if (c == ']')
307 {
308 c = getc (list); /* eat what should be a newline */
309 if (c != '\n' && c != EOF)
310 error_at_line (pos, msg: "junk on line after language tag [%s]", line);
311 }
312 else
313 error_at_line (pos, msg: "missing close bracket for language tag [%s",
314 line);
315
316 *herep = here;
317 *linep = line;
318 return true;
319 }
320 else
321 {
322 /* Leave space for a lang_bitmap. */
323 memset (s: here, c: 0, n: sizeof (lang_bitmap));
324 here += sizeof (lang_bitmap);
325 line = here;
326 do
327 {
328 *here++ = c;
329 c = getc (list);
330 }
331 while (c != EOF && c != '\n');
332 *here++ = '\0';
333 *herep = here;
334 *linep = line;
335 return false;
336 }
337}
338
339/* Read the list of input files from LIST and compute all of the
340 relevant tables. There is one file per line of the list. At
341 first, all the files on the list are language-generic, but
342 eventually a line will appear which is the name of a language
343 subdirectory in square brackets, like this: [cp]. All subsequent
344 files are specific to that language, until another language
345 subdirectory tag appears. Files can appear more than once, if
346 they apply to more than one language. */
347static void
348read_input_list (const char *listname)
349{
350 FILE *list = fopen (listname, "r");
351 if (!list)
352 fatal ("cannot open %s: %s", listname, xstrerror (errno));
353 else
354 {
355 struct fileloc epos;
356 size_t bufsz = measure_input_list (list);
357 char *buf = XNEWVEC (char, bufsz);
358 char *here = buf;
359 char *committed = buf;
360 char *limit = buf + bufsz;
361 char *line;
362 bool is_language;
363 size_t langno = 0;
364 size_t nfiles = 0;
365 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
366
367 epos.file = input_file_by_name (name: listname);
368 epos.line = 0;
369
370 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
371 gt_files = XNEWVEC (const input_file *, num_gt_files);
372
373 for (;;)
374 {
375 next_line:
376 epos.line++;
377 committed = here;
378 is_language = read_input_line (list, herep: &here, linep: &line, pos: &epos);
379 gcc_assert (here <= limit);
380 if (line == 0)
381 break;
382 else if (is_language)
383 {
384 size_t i;
385 gcc_assert (langno <= num_lang_dirs);
386 for (i = 0; i < langno; i++)
387 if (strcmp (s1: lang_dir_names[i], s2: line) == 0)
388 {
389 error_at_line (pos: &epos, msg: "duplicate language tag [%s]",
390 line);
391 curlangs = 1 << i;
392 here = committed;
393 goto next_line;
394 }
395
396 curlangs = 1 << langno;
397 lang_dir_names[langno++] = line;
398 }
399 else
400 {
401 size_t i;
402 input_file *inpf = input_file_by_name (name: line);
403 gcc_assert (nfiles <= num_gt_files);
404 for (i = 0; i < nfiles; i++)
405 /* Since the input_file-s are uniquely hash-consed, we
406 can just compare pointers! */
407 if (gt_files[i] == inpf)
408 {
409 /* Throw away the string we just read, and add the
410 current language to the existing string's bitmap. */
411 lang_bitmap bmap = get_lang_bitmap (inpf);
412 if (bmap & curlangs)
413 error_at_line (pos: &epos,
414 msg: "file %s specified more than once "
415 "for language %s", line,
416 langno ==
417 0 ? "(all)" : lang_dir_names[langno -
418 1]);
419
420 bmap |= curlangs;
421 set_lang_bitmap (inpf, n: bmap);
422 here = committed;
423 goto next_line;
424 }
425
426 set_lang_bitmap (inpf, n: curlangs);
427 gt_files[nfiles++] = inpf;
428 }
429 }
430 /* Update the global counts now that we know accurately how many
431 things there are. (We do not bother resizing the arrays down.) */
432 num_lang_dirs = langno;
433 /* Add the plugin files if provided. */
434 if (plugin_files)
435 {
436 size_t i;
437 for (i = 0; i < nb_plugin_files; i++)
438 gt_files[nfiles++] = plugin_files[i];
439 }
440 num_gt_files = nfiles;
441 }
442
443 /* Sanity check: any file that resides in a language subdirectory
444 (e.g. 'cp') ought to belong to the corresponding language.
445 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
446 (Can you even do that? Should you be allowed to?) */
447 {
448 size_t f;
449 for (f = 0; f < num_gt_files; f++)
450 {
451 lang_bitmap bitmap = get_lang_bitmap (inpf: gt_files[f]);
452 const char *basename = get_file_basename (gt_files[f]);
453 const char *slashpos = strchr (s: basename, c: '/');
454#ifdef HAVE_DOS_BASED_FILE_SYSTEM
455 const char *slashpos2 = strchr (basename, '\\');
456
457 if (!slashpos || (slashpos2 && slashpos2 < slashpos))
458 slashpos = slashpos2;
459#endif
460
461 if (slashpos)
462 {
463 size_t l;
464 for (l = 0; l < num_lang_dirs; l++)
465 if ((size_t) (slashpos - basename) == strlen (s: lang_dir_names[l])
466 && memcmp (s1: basename, s2: lang_dir_names[l],
467 n: strlen (s: lang_dir_names[l])) == 0)
468 {
469 if (!(bitmap & (1 << l)))
470 error ("%s is in language directory '%s' but is not "
471 "tagged for that language",
472 basename, lang_dir_names[l]);
473 break;
474 }
475 }
476 }
477 }
478
479 if (ferror (list))
480 fatal ("error reading %s: %s", listname, xstrerror (errno));
481
482 fclose (stream: list);
483}
484
485
486
487/* The one and only TYPE_STRING. */
488
489struct type string_type = {
490 .kind: TYPE_STRING, .next: 0, .state_number: 0, .pointer_to: 0, .gc_used: GC_USED, .u: {.p: 0}
491};
492
493/* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
494 set early in main. */
495
496struct type scalar_nonchar = {
497 .kind: TYPE_SCALAR, .next: 0, .state_number: 0, .pointer_to: 0, .gc_used: GC_USED, .u: {.p: 0}
498};
499
500struct type scalar_char = {
501 .kind: TYPE_SCALAR, .next: 0, .state_number: 0, .pointer_to: 0, .gc_used: GC_USED, .u: {.p: 0}
502};
503
504struct type callback_type = {
505 .kind: TYPE_CALLBACK, .next: 0, .state_number: 0, .pointer_to: 0, .gc_used: GC_USED, .u: {.p: 0}
506};
507
508/* Lists of various things. */
509
510pair_p typedefs = NULL;
511type_p structures = NULL;
512pair_p variables = NULL;
513
514static type_p adjust_field_rtx_def (type_p t, options_p opt);
515
516/* Define S as a typedef to T at POS. */
517
518void
519do_typedef (const char *s, type_p t, struct fileloc *pos)
520{
521 pair_p p;
522
523 /* temporary kludge - gengtype doesn't handle conditionals or
524 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
525 is coming from this file (main() sets them up with safe dummy
526 definitions). */
527 if (!strcmp (s1: s, s2: "CUMULATIVE_ARGS") && pos->file != this_file)
528 return;
529
530 for (p = typedefs; p != NULL; p = p->next)
531 if (strcmp (s1: p->name, s2: s) == 0)
532 {
533 if (p->type != t && strcmp (s1: s, s2: "result_type") != 0)
534 {
535 error_at_line (pos, msg: "type `%s' previously defined", s);
536 error_at_line (pos: &p->line, msg: "previously defined here");
537 }
538 return;
539 }
540
541 p = XNEW (struct pair);
542 p->next = typedefs;
543 p->name = s;
544 p->type = t;
545 p->line = *pos;
546 p->opt = NULL;
547 typedefs = p;
548}
549
550/* Define S as a typename of a scalar. Cannot be used to define
551 typedefs of 'char'. Note: is also used for pointer-to-function
552 typedefs (which are therefore not treated as pointers). */
553
554void
555do_scalar_typedef (const char *s, struct fileloc *pos)
556{
557 do_typedef (s, t: &scalar_nonchar, pos);
558}
559
560/* Similar to strtok_r. */
561
562static char *
563strtoken (char *str, const char *delim, char **next)
564{
565 char *p;
566
567 if (str == NULL)
568 str = *next;
569
570 /* Skip the leading delimiters. */
571 str += strspn (s: str, accept: delim);
572 if (*str == '\0')
573 /* This is an empty token. */
574 return NULL;
575
576 /* The current token. */
577 p = str;
578
579 /* Find the next delimiter. */
580 str += strcspn (s: str, reject: delim);
581 if (*str == '\0')
582 /* This is the last token. */
583 *next = str;
584 else
585 {
586 /* Terminate the current token. */
587 *str = '\0';
588 /* Advance to the next token. */
589 *next = str + 1;
590 }
591
592 return p;
593}
594
595/* Define TYPE_NAME to be a user defined type at location POS. */
596
597type_p
598create_user_defined_type (const char *type_name, struct fileloc *pos)
599{
600 type_p ty = find_structure (s: type_name, kind: TYPE_USER_STRUCT);
601
602 /* We might have already seen an incomplete decl of the given type,
603 in which case we won't have yet seen a GTY((user)), and the type will
604 only have kind "TYPE_STRUCT". Mark it as a user struct. */
605 ty->kind = TYPE_USER_STRUCT;
606
607 ty->u.s.line = *pos;
608 ty->u.s.bitmap = get_lang_bitmap (inpf: pos->file);
609 do_typedef (s: type_name, t: ty, pos);
610
611 /* If TYPE_NAME specifies a template, create references to the types
612 in the template by pretending that each type is a field of TY.
613 This is needed to make sure that the types referenced by the
614 template are marked as used. */
615 char *str = xstrdup (type_name);
616 char *open_bracket = strchr (s: str, c: '<');
617 if (open_bracket)
618 {
619 /* We only accept simple template declarations (see
620 require_template_declaration), so we only need to parse a
621 comma-separated list of strings, implicitly assumed to
622 be type names, potentially with "*" characters. */
623 char *arg = open_bracket + 1;
624 /* Workaround -Wmaybe-uninitialized false positive during
625 profiledbootstrap by initializing it. */
626 char *next = NULL;
627 char *type_id = strtoken (str: arg, delim: ",>", next: &next);
628 pair_p fields = 0;
629 while (type_id)
630 {
631 /* Create a new field for every type found inside the template
632 parameter list. */
633
634 /* Support a single trailing "*" character. */
635 const char *star = strchr (s: type_id, c: '*');
636 int is_ptr = (star != NULL);
637 size_t offset_to_star = star - type_id;
638 if (is_ptr)
639 offset_to_star = star - type_id;
640
641 if (strstr (haystack: type_id, needle: "char*"))
642 {
643 type_id = strtoken (str: 0, delim: ",>", next: &next);
644 continue;
645 }
646
647 char *field_name = xstrdup (type_id);
648
649 type_p arg_type;
650 if (is_ptr)
651 {
652 /* Strip off the first '*' character (and any subsequent text). */
653 *(field_name + offset_to_star) = '\0';
654
655 arg_type = find_structure (s: field_name, kind: TYPE_STRUCT);
656 arg_type = create_pointer (t: arg_type);
657 }
658 else
659 arg_type = resolve_typedef (s: field_name, pos);
660
661 fields = create_field_at (next: fields, type: arg_type, name: field_name, opt: 0, pos);
662 type_id = strtoken (str: 0, delim: ",>", next: &next);
663 }
664
665 /* Associate the field list to TY. */
666 ty->u.s.fields = fields;
667 }
668 free (ptr: str);
669
670 return ty;
671}
672
673
674/* Given a typedef name S, return its associated type. Return NULL if
675 S is not a registered type name. */
676
677static type_p
678type_for_name (const char *s)
679{
680 pair_p p;
681
682 /* Special-case support for types within a "gcc::" namespace. Rather
683 than fully-supporting namespaces, simply strip off the "gcc::" prefix
684 where present. This allows us to have GTY roots of this form:
685 extern GTY(()) gcc::some_type *some_ptr;
686 where the autogenerated functions will refer to simply "some_type",
687 where they can be resolved into their namespace. */
688 if (startswith (str: s, prefix: "gcc::"))
689 s += 5;
690
691 for (p = typedefs; p != NULL; p = p->next)
692 if (strcmp (s1: p->name, s2: s) == 0)
693 return p->type;
694 return NULL;
695}
696
697
698/* Create an undefined type with name S and location POS. Return the
699 newly created type. */
700
701static type_p
702create_undefined_type (const char *s, struct fileloc *pos)
703{
704 type_p ty = find_structure (s, kind: TYPE_UNDEFINED);
705 ty->u.s.line = *pos;
706 ty->u.s.bitmap = get_lang_bitmap (inpf: pos->file);
707 do_typedef (s, t: ty, pos);
708 return ty;
709}
710
711
712/* Return the type previously defined for S. Use POS to report errors. */
713
714type_p
715resolve_typedef (const char *s, struct fileloc *pos)
716{
717 bool is_template_instance = (strchr (s: s, c: '<') != NULL);
718 type_p p = type_for_name (s);
719
720 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
721 type for regular type identifiers. If the type identifier S is a
722 template instantiation, however, we treat it as a user defined
723 type.
724
725 FIXME, this is actually a limitation in gengtype. Supporting
726 template types and their instances would require keeping separate
727 track of the basic types definition and its instances. This
728 essentially forces all template classes in GC to be marked
729 GTY((user)). */
730 if (!p)
731 p = (is_template_instance)
732 ? create_user_defined_type (type_name: s, pos)
733 : create_undefined_type (s, pos);
734
735 return p;
736}
737
738/* Add SUBCLASS to head of linked list of BASE's subclasses. */
739
740void add_subclass (type_p base, type_p subclass)
741{
742 gcc_assert (union_or_struct_p (base));
743 gcc_assert (union_or_struct_p (subclass));
744
745 subclass->u.s.next_sibling_class = base->u.s.first_subclass;
746 base->u.s.first_subclass = subclass;
747}
748
749/* Create and return a new structure with tag NAME at POS with fields
750 FIELDS and options O. The KIND of structure must be one of
751 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
752
753type_p
754new_structure (const char *name, enum typekind kind, struct fileloc *pos,
755 pair_p fields, options_p o, type_p base_class)
756{
757 type_p si;
758 type_p s = NULL;
759 lang_bitmap bitmap = get_lang_bitmap (inpf: pos->file);
760 bool isunion = (kind == TYPE_UNION);
761 type_p *p = &structures;
762
763 gcc_assert (union_or_struct_p (kind));
764
765 for (si = structures; si != NULL; p = &si->next, si = *p)
766 if (strcmp (s1: name, s2: si->u.s.tag) == 0 && UNION_P (si) == isunion)
767 {
768 type_p ls = NULL;
769 if (si->kind == TYPE_LANG_STRUCT)
770 {
771 ls = si;
772
773 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
774 if (si->u.s.bitmap == bitmap)
775 s = si;
776 }
777 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
778 {
779 ls = si;
780 type_count++;
781 si = XCNEW (struct type);
782 memcpy (dest: si, src: ls, n: sizeof (struct type));
783 ls->kind = TYPE_LANG_STRUCT;
784 ls->u.s.lang_struct = si;
785 ls->u.s.fields = NULL;
786 si->next = NULL;
787 si->state_number = -type_count;
788 si->pointer_to = NULL;
789 si->u.s.lang_struct = ls;
790 }
791 else
792 s = si;
793
794 if (ls != NULL && s == NULL)
795 {
796 type_count++;
797 s = XCNEW (struct type);
798 s->state_number = -type_count;
799 s->next = ls->u.s.lang_struct;
800 ls->u.s.lang_struct = s;
801 s->u.s.lang_struct = ls;
802 }
803 break;
804 }
805
806 if (s == NULL)
807 {
808 type_count++;
809 s = XCNEW (struct type);
810 s->state_number = -type_count;
811 *p = s;
812 }
813
814 if (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap))
815 {
816 error_at_line (pos, msg: "duplicate definition of '%s %s'",
817 isunion ? "union" : "struct", s->u.s.tag);
818 error_at_line (pos: &s->u.s.line, msg: "previous definition here");
819 }
820
821 s->kind = kind;
822 s->u.s.tag = name;
823 s->u.s.line = *pos;
824 s->u.s.fields = fields;
825 s->u.s.opt = o;
826 s->u.s.bitmap = bitmap;
827 if (s->u.s.lang_struct)
828 s->u.s.lang_struct->u.s.bitmap |= bitmap;
829 s->u.s.base_class = base_class;
830 if (base_class)
831 add_subclass (base: base_class, subclass: s);
832
833 return s;
834}
835
836/* Return the previously-defined structure or union with tag NAME,
837 or a new empty structure or union if none was defined previously.
838 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
839 TYPE_USER_STRUCT. */
840
841type_p
842find_structure (const char *name, enum typekind kind)
843{
844 type_p s;
845 bool isunion = (kind == TYPE_UNION);
846 type_p *p = &structures;
847
848 gcc_assert (kind == TYPE_UNDEFINED || union_or_struct_p (kind));
849
850 for (s = structures; s != NULL; p = &s->next, s = *p)
851 if (strcmp (s1: name, s2: s->u.s.tag) == 0 && UNION_P (s) == isunion)
852 return s;
853
854 type_count++;
855 s = XCNEW (struct type);
856 s->state_number = -type_count;
857 s->kind = kind;
858 s->u.s.tag = name;
859 *p = s;
860 return s;
861}
862
863/* Return a scalar type with name NAME. */
864
865type_p
866create_scalar_type (const char *name)
867{
868 if (!strcmp (s1: name, s2: "char") || !strcmp (s1: name, s2: "unsigned char"))
869 return &scalar_char;
870 else
871 return &scalar_nonchar;
872}
873
874
875/* Return a pointer to T. */
876
877type_p
878create_pointer (type_p t)
879{
880 if (!t->pointer_to)
881 {
882 type_p r = XCNEW (struct type);
883 type_count++;
884 r->state_number = -type_count;
885 r->kind = TYPE_POINTER;
886 r->u.p = t;
887 t->pointer_to = r;
888 }
889 return t->pointer_to;
890}
891
892/* Return an array of length LEN. */
893
894type_p
895create_array (type_p t, const char *len)
896{
897 type_p v;
898
899 type_count++;
900 v = XCNEW (struct type);
901 v->kind = TYPE_ARRAY;
902 v->state_number = -type_count;
903 v->u.a.p = t;
904 v->u.a.len = len;
905 return v;
906}
907
908/* Return a string options structure with name NAME and info INFO.
909 NEXT is the next option in the chain. */
910options_p
911create_string_option (options_p next, const char *name, const char *info)
912{
913 options_p o = XNEW (struct options);
914 o->kind = OPTION_STRING;
915 o->next = next;
916 o->name = name;
917 o->info.string = info;
918 return o;
919}
920
921/* Create a type options structure with name NAME and info INFO. NEXT
922 is the next option in the chain. */
923options_p
924create_type_option (options_p next, const char* name, type_p info)
925{
926 options_p o = XNEW (struct options);
927 o->next = next;
928 o->name = name;
929 o->kind = OPTION_TYPE;
930 o->info.type = info;
931 return o;
932}
933
934/* Create a nested pointer options structure with name NAME and info
935 INFO. NEXT is the next option in the chain. */
936options_p
937create_nested_option (options_p next, const char* name,
938 struct nested_ptr_data* info)
939{
940 options_p o;
941 o = XNEW (struct options);
942 o->next = next;
943 o->name = name;
944 o->kind = OPTION_NESTED;
945 o->info.nested = info;
946 return o;
947}
948
949/* Return an options structure for a "nested_ptr" option. */
950options_p
951create_nested_ptr_option (options_p next, type_p t,
952 const char *to, const char *from)
953{
954 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
955
956 d->type = adjust_field_type (t, 0);
957 d->convert_to = to;
958 d->convert_from = from;
959 return create_nested_option (next, name: "nested_ptr", info: d);
960}
961
962/* Add a variable named S of type T with options O defined at POS,
963 to `variables'. */
964void
965note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
966{
967 pair_p n;
968 n = XNEW (struct pair);
969 n->name = s;
970 n->type = t;
971 n->line = *pos;
972 n->opt = o;
973 n->next = variables;
974 variables = n;
975}
976
977/* Most-general structure field creator. */
978static pair_p
979create_field_all (pair_p next, type_p type, const char *name, options_p opt,
980 const input_file *inpf, int line)
981{
982 pair_p field;
983
984 field = XNEW (struct pair);
985 field->next = next;
986 field->type = type;
987 field->name = name;
988 field->opt = opt;
989 field->line.file = inpf;
990 field->line.line = line;
991 return field;
992}
993
994/* Create a field that came from the source code we are scanning,
995 i.e. we have a 'struct fileloc', and possibly options; also,
996 adjust_field_type should be called. */
997pair_p
998create_field_at (pair_p next, type_p type, const char *name, options_p opt,
999 struct fileloc *pos)
1000{
1001 return create_field_all (next, type: adjust_field_type (type, opt),
1002 name, opt, inpf: pos->file, line: pos->line);
1003}
1004
1005/* Create a fake field with the given type and name. NEXT is the next
1006 field in the chain. */
1007#define create_field(next,type,name) \
1008 create_field_all (next,type,name, 0, this_file, __LINE__)
1009
1010/* Like create_field, but the field is only valid when condition COND
1011 is true. */
1012
1013static pair_p
1014create_optional_field_ (pair_p next, type_p type, const char *name,
1015 const char *cond, int line)
1016{
1017 static int id = 1;
1018 pair_p union_fields;
1019 type_p union_type;
1020
1021 /* Create a fake union type with a single nameless field of type TYPE.
1022 The field has a tag of "1". This allows us to make the presence
1023 of a field of type TYPE depend on some boolean "desc" being true. */
1024 union_fields = create_field (NULL, type, "");
1025 union_fields->opt =
1026 create_string_option (next: union_fields->opt, name: "dot", info: "");
1027 union_fields->opt =
1028 create_string_option (next: union_fields->opt, name: "tag", info: "1");
1029 union_type =
1030 new_structure (name: xasprintf ("%s_%d", "fake_union", id++), kind: TYPE_UNION,
1031 pos: &lexer_line, fields: union_fields, NULL, NULL);
1032
1033 /* Create the field and give it the new fake union type. Add a "desc"
1034 tag that specifies the condition under which the field is valid. */
1035 return create_field_all (next, type: union_type, name,
1036 opt: create_string_option (next: 0, name: "desc", info: cond),
1037 inpf: this_file, line);
1038}
1039
1040#define create_optional_field(next,type,name,cond) \
1041 create_optional_field_(next,type,name,cond,__LINE__)
1042
1043/* Reverse a linked list of 'struct pair's in place. */
1044pair_p
1045nreverse_pairs (pair_p list)
1046{
1047 pair_p prev = 0, p, next;
1048 for (p = list; p; p = next)
1049 {
1050 next = p->next;
1051 p->next = prev;
1052 prev = p;
1053 }
1054 return prev;
1055}
1056
1057
1058/* We don't care how long a CONST_DOUBLE is. */
1059#define CONST_DOUBLE_FORMAT "ww"
1060/* We don't want to see codes that are only for generator files. */
1061#undef GENERATOR_FILE
1062
1063enum rtx_code
1064{
1065#define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
1066#include "rtl.def"
1067#undef DEF_RTL_EXPR
1068 NUM_RTX_CODE
1069};
1070
1071static const char *const rtx_name[NUM_RTX_CODE] = {
1072#define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1073#include "rtl.def"
1074#undef DEF_RTL_EXPR
1075};
1076
1077static const char *const rtx_format[NUM_RTX_CODE] = {
1078#define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1079#include "rtl.def"
1080#undef DEF_RTL_EXPR
1081};
1082
1083static int rtx_next_new[NUM_RTX_CODE];
1084
1085/* We also need codes and names for insn notes (not register notes).
1086 Note that we do *not* bias the note values here. */
1087enum insn_note
1088{
1089#define DEF_INSN_NOTE(NAME) NAME,
1090#include "insn-notes.def"
1091#undef DEF_INSN_NOTE
1092
1093 NOTE_INSN_MAX
1094};
1095
1096/* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1097 default field for line number notes. */
1098static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
1099#define DEF_INSN_NOTE(NAME) #NAME,
1100#include "insn-notes.def"
1101#undef DEF_INSN_NOTE
1102};
1103
1104#undef CONST_DOUBLE_FORMAT
1105#define GENERATOR_FILE
1106
1107/* Generate the contents of the rtx_next array. This really doesn't belong
1108 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1109
1110static void
1111gen_rtx_next (void)
1112{
1113 int i;
1114 for (i = 0; i < NUM_RTX_CODE; i++)
1115 {
1116 int k;
1117
1118 rtx_next_new[i] = -1;
1119 if (startswith (str: rtx_format[i], prefix: "uu"))
1120 rtx_next_new[i] = 1;
1121 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1122 rtx_next_new[i] = 1;
1123 else
1124 for (k = strlen (s: rtx_format[i]) - 1; k >= 0; k--)
1125 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1126 rtx_next_new[i] = k;
1127 }
1128}
1129
1130/* Write out the contents of the rtx_next array. */
1131static void
1132write_rtx_next (void)
1133{
1134 outf_p f = get_output_file_with_visibility (NULL);
1135 int i;
1136 if (!f)
1137 return;
1138
1139 oprintf (o: f, S: "\n/* Used to implement the RTX_NEXT macro. */\n");
1140 oprintf (o: f, S: "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1141 for (i = 0; i < NUM_RTX_CODE; i++)
1142 if (rtx_next_new[i] == -1)
1143 oprintf (o: f, S: " 0,\n");
1144 else
1145 oprintf (o: f,
1146 S: " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1147 oprintf (o: f, S: "};\n");
1148}
1149
1150/* Handle `special("rtx_def")'. This is a special case for field
1151 `fld' of struct rtx_def, which is an array of unions whose values
1152 are based in a complex way on the type of RTL. */
1153
1154static type_p
1155adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1156{
1157 pair_p flds = NULL;
1158 options_p nodot;
1159 int i;
1160 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1161 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1162
1163 if (t->kind != TYPE_UNION)
1164 {
1165 error_at_line (pos: &lexer_line,
1166 msg: "special `rtx_def' must be applied to a union");
1167 return &string_type;
1168 }
1169
1170 nodot = create_string_option (NULL, name: "dot", info: "");
1171
1172 rtx_tp = create_pointer (t: find_structure (name: "rtx_def", kind: TYPE_STRUCT));
1173 rtvec_tp = create_pointer (t: find_structure (name: "rtvec_def", kind: TYPE_STRUCT));
1174 tree_tp = create_pointer (t: find_structure (name: "tree_node", kind: TYPE_UNION));
1175 mem_attrs_tp = create_pointer (t: find_structure (name: "mem_attrs", kind: TYPE_STRUCT));
1176 reg_attrs_tp =
1177 create_pointer (t: find_structure (name: "reg_attrs", kind: TYPE_STRUCT));
1178 basic_block_tp =
1179 create_pointer (t: find_structure (name: "basic_block_def", kind: TYPE_STRUCT));
1180 constant_tp =
1181 create_pointer (t: find_structure (name: "constant_descriptor_rtx", kind: TYPE_STRUCT));
1182 scalar_tp = &scalar_nonchar; /* rtunion int */
1183
1184 {
1185 pair_p note_flds = NULL;
1186 int c;
1187
1188 for (c = 0; c <= NOTE_INSN_MAX; c++)
1189 {
1190 switch (c)
1191 {
1192 case NOTE_INSN_MAX:
1193 case NOTE_INSN_DELETED_LABEL:
1194 case NOTE_INSN_DELETED_DEBUG_LABEL:
1195 note_flds = create_field (note_flds, &string_type, "rt_str");
1196 break;
1197
1198 case NOTE_INSN_BLOCK_BEG:
1199 case NOTE_INSN_BLOCK_END:
1200 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1201 break;
1202
1203 case NOTE_INSN_VAR_LOCATION:
1204 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1205 break;
1206
1207 default:
1208 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1209 break;
1210 }
1211 /* NOTE_INSN_MAX is used as the default field for line
1212 number notes. */
1213 if (c == NOTE_INSN_MAX)
1214 note_flds->opt =
1215 create_string_option (next: nodot, name: "default", info: "");
1216 else
1217 note_flds->opt =
1218 create_string_option (next: nodot, name: "tag", info: note_insn_name[c]);
1219 }
1220 note_union_tp = new_structure (name: "rtx_def_note_subunion", kind: TYPE_UNION,
1221 pos: &lexer_line, fields: note_flds, NULL, NULL);
1222 }
1223 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1224 {
1225 pair_p sym_flds;
1226 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1227 sym_flds->opt = create_string_option (next: nodot, name: "default", info: "");
1228 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1229 sym_flds->opt = create_string_option (next: nodot, name: "tag", info: "1");
1230 symbol_union_tp = new_structure (name: "rtx_def_symbol_subunion", kind: TYPE_UNION,
1231 pos: &lexer_line, fields: sym_flds, NULL, NULL);
1232 }
1233 for (i = 0; i < NUM_RTX_CODE; i++)
1234 {
1235 pair_p subfields = NULL;
1236 size_t aindex, nmindex;
1237 const char *sname;
1238 type_p substruct;
1239 char *ftag;
1240
1241 for (aindex = 0; aindex < strlen (s: rtx_format[i]); aindex++)
1242 {
1243 type_p t;
1244 const char *subname;
1245
1246 switch (rtx_format[i][aindex])
1247 {
1248 case '*':
1249 case 'i':
1250 case 'n':
1251 case 'w':
1252 case 'r':
1253 t = scalar_tp;
1254 subname = "rt_int";
1255 break;
1256
1257 case 'p':
1258 t = scalar_tp;
1259 subname = "rt_subreg";
1260 break;
1261
1262 case '0':
1263 if (i == MEM && aindex == 1)
1264 t = mem_attrs_tp, subname = "rt_mem";
1265 else if (i == JUMP_INSN && aindex == 7)
1266 t = rtx_tp, subname = "rt_rtx";
1267 else if (i == CODE_LABEL && aindex == 4)
1268 t = scalar_tp, subname = "rt_int";
1269 else if (i == CODE_LABEL && aindex == 3)
1270 t = rtx_tp, subname = "rt_rtx";
1271 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1272 t = rtx_tp, subname = "rt_rtx";
1273 else if (i == NOTE && aindex == 3)
1274 t = note_union_tp, subname = "";
1275 else if (i == NOTE && aindex == 4)
1276 t = scalar_tp, subname = "rt_int";
1277 else if (i == NOTE && aindex >= 6)
1278 t = scalar_tp, subname = "rt_int";
1279 else if (i == ADDR_DIFF_VEC && aindex == 4)
1280 t = scalar_tp, subname = "rt_int";
1281 else if (i == VALUE && aindex == 0)
1282 t = scalar_tp, subname = "rt_int";
1283 else if (i == DEBUG_EXPR && aindex == 0)
1284 t = tree_tp, subname = "rt_tree";
1285 else if (i == SYMBOL_REF && aindex == 1)
1286 t = symbol_union_tp, subname = "";
1287 else if (i == JUMP_TABLE_DATA && aindex >= 4)
1288 t = scalar_tp, subname = "rt_int";
1289 else if (i == BARRIER && aindex >= 2)
1290 t = scalar_tp, subname = "rt_int";
1291 else if (i == ENTRY_VALUE && aindex == 0)
1292 t = rtx_tp, subname = "rt_rtx";
1293 else
1294 {
1295 error_at_line
1296 (pos: &lexer_line,
1297 msg: "rtx type `%s' has `0' in position %lu, can't handle",
1298 rtx_name[i], (unsigned long) aindex);
1299 t = &string_type;
1300 subname = "rt_int";
1301 }
1302 break;
1303
1304 case 's':
1305 case 'S':
1306 case 'T':
1307 t = &string_type;
1308 subname = "rt_str";
1309 break;
1310
1311 case 'e':
1312 case 'u':
1313 t = rtx_tp;
1314 subname = "rt_rtx";
1315 break;
1316
1317 case 'E':
1318 case 'V':
1319 t = rtvec_tp;
1320 subname = "rt_rtvec";
1321 break;
1322
1323 case 't':
1324 t = tree_tp;
1325 subname = "rt_tree";
1326 break;
1327
1328 case 'B':
1329 t = basic_block_tp;
1330 subname = "rt_bb";
1331 break;
1332
1333 default:
1334 error_at_line
1335 (pos: &lexer_line,
1336 msg: "rtx type `%s' has `%c' in position %lu, can't handle",
1337 rtx_name[i], rtx_format[i][aindex],
1338 (unsigned long) aindex);
1339 t = &string_type;
1340 subname = "rt_int";
1341 break;
1342 }
1343
1344 subfields = create_field (subfields, t,
1345 xasprintf (".fld[%lu].%s",
1346 (unsigned long) aindex,
1347 subname));
1348 subfields->opt = nodot;
1349 if (t == note_union_tp)
1350 subfields->opt =
1351 create_string_option (next: subfields->opt, name: "desc",
1352 info: "NOTE_KIND (&%0)");
1353 if (t == symbol_union_tp)
1354 subfields->opt =
1355 create_string_option (next: subfields->opt, name: "desc",
1356 info: "CONSTANT_POOL_ADDRESS_P (&%0)");
1357 }
1358
1359 if (i == REG)
1360 subfields = create_field (subfields, reg_attrs_tp, "reg.attrs");
1361
1362 if (i == SYMBOL_REF)
1363 {
1364 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1365 holds. */
1366 type_p field_tp = find_structure (name: "block_symbol", kind: TYPE_STRUCT);
1367 subfields
1368 = create_optional_field (subfields, field_tp, "block_sym",
1369 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1370 }
1371
1372 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1373 substruct = new_structure (name: sname, kind: TYPE_STRUCT, pos: &lexer_line, fields: subfields,
1374 NULL, NULL);
1375
1376 ftag = xstrdup (rtx_name[i]);
1377 for (nmindex = 0; nmindex < strlen (s: ftag); nmindex++)
1378 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1379 flds = create_field (flds, substruct, "");
1380 flds->opt = create_string_option (next: nodot, name: "tag", info: ftag);
1381 }
1382 return new_structure (name: "rtx_def_subunion", kind: TYPE_UNION, pos: &lexer_line, fields: flds,
1383 o: nodot, NULL);
1384}
1385
1386/* Perform any special processing on a type T, about to become the type
1387 of a field. Return the appropriate type for the field.
1388 At present:
1389 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1390 - Similarly for arrays of pointer-to-char;
1391 - Handles "special" options.
1392*/
1393
1394type_p
1395adjust_field_type (type_p t, options_p opt)
1396{
1397 int length_p = 0;
1398 const int pointer_p = t->kind == TYPE_POINTER;
1399
1400 for (; opt; opt = opt->next)
1401 if (strcmp (s1: opt->name, s2: "length") == 0)
1402 {
1403 if (length_p)
1404 error_at_line (pos: &lexer_line, msg: "duplicate `%s' option", opt->name);
1405 if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
1406 {
1407 error_at_line (pos: &lexer_line,
1408 msg: "option `%s' may not be applied to "
1409 "arrays of atomic types", opt->name);
1410 }
1411 length_p = 1;
1412 }
1413 else if (strcmp (s1: opt->name, s2: "special") == 0
1414 && opt->kind == OPTION_STRING)
1415 {
1416 const char *special_name = opt->info.string;
1417 if (strcmp (s1: special_name, s2: "rtx_def") == 0)
1418 t = adjust_field_rtx_def (t, opt);
1419 else
1420 error_at_line (pos: &lexer_line, msg: "unknown special `%s'", special_name);
1421 }
1422
1423 if (!length_p
1424 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1425 return &string_type;
1426 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1427 && t->u.a.p->u.p->kind == TYPE_SCALAR
1428 && t->u.a.p->u.p->u.scalar_is_char)
1429 return create_array (t: &string_type, len: t->u.a.len);
1430
1431 return t;
1432}
1433
1434
1435static void set_gc_used_type (type_p, enum gc_used_enum, bool = false);
1436static void set_gc_used (pair_p);
1437
1438/* Handle OPT for set_gc_used_type. */
1439
1440static void
1441process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1442 int *length, int *skip, int *callback, type_p *nested_ptr)
1443{
1444 options_p o;
1445 for (o = opt; o; o = o->next)
1446 if (strcmp (s1: o->name, s2: "ptr_alias") == 0 && level == GC_POINTED_TO
1447 && o->kind == OPTION_TYPE)
1448 set_gc_used_type (o->info.type,
1449 GC_POINTED_TO);
1450 else if (strcmp (s1: o->name, s2: "maybe_undef") == 0)
1451 *maybe_undef = 1;
1452 else if (strcmp (s1: o->name, s2: "length") == 0)
1453 *length = 1;
1454 else if (strcmp (s1: o->name, s2: "skip") == 0)
1455 *skip = 1;
1456 else if (strcmp (s1: o->name, s2: "callback") == 0)
1457 *callback = 1;
1458 else if (strcmp (s1: o->name, s2: "nested_ptr") == 0
1459 && o->kind == OPTION_NESTED)
1460 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1461}
1462
1463
1464/* Set the gc_used field of T to LEVEL, and handle the types it references.
1465
1466 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1467 are set to GC_UNUSED. Otherwise, an error is emitted for
1468 TYPE_UNDEFINED types. This is used to support user-defined
1469 template types with non-type arguments.
1470
1471 For instance, when we parse a template type with enum arguments
1472 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1473 artificial fields for 'MyType', one for 'AnotherType', the other
1474 one for 'EnumValue'.
1475
1476 At the time that we parse this type we don't know that 'EnumValue'
1477 is really an enum value, so the parser creates a TYPE_UNDEFINED
1478 type for it. Since 'EnumValue' is never resolved to a known
1479 structure, it will stay with TYPE_UNDEFINED.
1480
1481 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1482 'EnumValue'. Generating marking code for it would cause
1483 compilation failures since the marking routines assumes that
1484 'EnumValue' is a type. */
1485
1486static void
1487set_gc_used_type (type_p t, enum gc_used_enum level,
1488 bool allow_undefined_types)
1489{
1490 if (t->gc_used >= level)
1491 return;
1492
1493 t->gc_used = level;
1494
1495 switch (t->kind)
1496 {
1497 case TYPE_STRUCT:
1498 case TYPE_UNION:
1499 case TYPE_USER_STRUCT:
1500 {
1501 pair_p f;
1502 int dummy;
1503 type_p dummy2;
1504 bool allow_undefined_field_types = (t->kind == TYPE_USER_STRUCT);
1505
1506 process_gc_options (opt: t->u.s.opt, level, maybe_undef: &dummy, length: &dummy, skip: &dummy, callback: &dummy,
1507 nested_ptr: &dummy2);
1508
1509 if (t->u.s.base_class)
1510 set_gc_used_type (t: t->u.s.base_class, level, allow_undefined_types);
1511 /* Anything pointing to a base class might actually be pointing
1512 to a subclass. */
1513 for (type_p subclass = t->u.s.first_subclass; subclass;
1514 subclass = subclass->u.s.next_sibling_class)
1515 set_gc_used_type (t: subclass, level, allow_undefined_types);
1516
1517 FOR_ALL_INHERITED_FIELDS(t, f)
1518 {
1519 int maybe_undef = 0;
1520 int length = 0;
1521 int skip = 0;
1522 int callback = 0;
1523 type_p nested_ptr = NULL;
1524 process_gc_options (opt: f->opt, level, maybe_undef: &maybe_undef, length: &length, skip: &skip,
1525 callback: &callback, nested_ptr: &nested_ptr);
1526
1527 if (nested_ptr && f->type->kind == TYPE_POINTER)
1528 set_gc_used_type (t: nested_ptr, level: GC_POINTED_TO);
1529 else if (length && f->type->kind == TYPE_POINTER)
1530 set_gc_used_type (t: f->type->u.p, level: GC_USED);
1531 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1532 set_gc_used_type (t: f->type->u.p, level: GC_MAYBE_POINTED_TO);
1533 else if (skip)
1534 ; /* target type is not used through this field */
1535 else if (callback)
1536 f->type = &callback_type;
1537 else
1538 set_gc_used_type (t: f->type, level: GC_USED, allow_undefined_types: allow_undefined_field_types);
1539 }
1540 break;
1541 }
1542
1543 case TYPE_UNDEFINED:
1544 if (level > GC_UNUSED)
1545 {
1546 if (!allow_undefined_types)
1547 error_at_line (pos: &t->u.s.line, msg: "undefined type `%s'", t->u.s.tag);
1548 t->gc_used = GC_UNUSED;
1549 }
1550 break;
1551
1552 case TYPE_POINTER:
1553 set_gc_used_type (t: t->u.p, level: GC_POINTED_TO);
1554 break;
1555
1556 case TYPE_ARRAY:
1557 set_gc_used_type (t: t->u.a.p, level: GC_USED);
1558 break;
1559
1560 case TYPE_LANG_STRUCT:
1561 for (t = t->u.s.lang_struct; t; t = t->next)
1562 set_gc_used_type (t, level);
1563 break;
1564
1565 default:
1566 break;
1567 }
1568}
1569
1570/* Set the gc_used fields of all the types pointed to by VARIABLES. */
1571
1572static void
1573set_gc_used (pair_p variables)
1574{
1575 int nbvars = 0;
1576 pair_p p;
1577 for (p = variables; p; p = p->next)
1578 {
1579 set_gc_used_type (t: p->type, level: GC_USED);
1580 nbvars++;
1581 };
1582 if (verbosity_level >= 2)
1583 printf (format: "%s used %d GTY-ed variables\n", progname, nbvars);
1584}
1585
1586/* File mapping routines. For each input file, there is one output .cc file
1587 (but some output files have many input files), and there is one .h file
1588 for the whole build. */
1589
1590/* Output file handling. */
1591
1592/* Create and return an outf_p for a new file for NAME, to be called
1593 ONAME. */
1594
1595static outf_p
1596create_file (const char *name, const char *oname)
1597{
1598 static const char *const hdr[] = {
1599 " Copyright (C) 2004-2023 Free Software Foundation, Inc.\n",
1600 "\n",
1601 "This file is part of GCC.\n",
1602 "\n",
1603 "GCC is free software; you can redistribute it and/or modify it under\n",
1604 "the terms of the GNU General Public License as published by the Free\n",
1605 "Software Foundation; either version 3, or (at your option) any later\n",
1606 "version.\n",
1607 "\n",
1608 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1609 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1610 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1611 "for more details.\n",
1612 "\n",
1613 "You should have received a copy of the GNU General Public License\n",
1614 "along with GCC; see the file COPYING3. If not see\n",
1615 "<http://www.gnu.org/licenses/>. */\n",
1616 "\n",
1617 "/* This file is machine generated. Do not edit. */\n"
1618 };
1619 outf_p f;
1620 size_t i;
1621
1622 gcc_assert (name != NULL);
1623 gcc_assert (oname != NULL);
1624 f = XCNEW (struct outf);
1625 f->next = output_files;
1626 f->name = oname;
1627 output_files = f;
1628
1629 oprintf (o: f, S: "/* Type information for %s.\n", name);
1630 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1631 oprintf (o: f, S: "%s", hdr[i]);
1632 return f;
1633}
1634
1635/* Print, like fprintf, to O.
1636 N.B. You might think this could be implemented more efficiently
1637 with vsnprintf(). Unfortunately, there are C libraries that
1638 provide that function but without the C99 semantics for its return
1639 value, making it impossible to know how much space is required. */
1640void
1641oprintf (outf_p o, const char *format, ...)
1642{
1643 char *s;
1644 size_t slength;
1645 va_list ap;
1646
1647 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1648 in that case. */
1649 if (!o)
1650 return;
1651
1652 va_start (ap, format);
1653 slength = vasprintf (ptr: &s, f: format, arg: ap);
1654 if (s == NULL || (int) slength < 0)
1655 fatal ("out of memory");
1656 va_end (ap);
1657
1658 if (o->bufused + slength > o->buflength)
1659 {
1660 size_t new_len = o->buflength;
1661 if (new_len == 0)
1662 new_len = 1024;
1663 do
1664 {
1665 new_len *= 2;
1666 }
1667 while (o->bufused + slength >= new_len);
1668 o->buf = XRESIZEVEC (char, o->buf, new_len);
1669 o->buflength = new_len;
1670 }
1671 memcpy (dest: o->buf + o->bufused, src: s, n: slength);
1672 o->bufused += slength;
1673 free (ptr: s);
1674}
1675
1676/* Open the global header file and the language-specific header files. */
1677
1678static void
1679open_base_files (void)
1680{
1681 size_t i;
1682
1683 if (nb_plugin_files > 0 && plugin_files)
1684 return;
1685
1686 header_file = create_file (name: "GCC", oname: "gtype-desc.h");
1687
1688 base_files = XNEWVEC (outf_p, num_lang_dirs);
1689
1690 for (i = 0; i < num_lang_dirs; i++)
1691 base_files[i] = create_file (name: lang_dir_names[i],
1692 oname: xasprintf ("gtype-%s.h", lang_dir_names[i]));
1693
1694 /* gtype-desc.cc is a little special, so we create it here. */
1695 {
1696 /* The order of files here matters very much. */
1697 static const char *const ifiles[] = {
1698 "config.h", "system.h", "coretypes.h",
1699 "backend.h", "predict.h", "tree.h",
1700 "rtl.h", "gimple.h", "fold-const.h", "insn-codes.h", "splay-tree.h",
1701 "alias.h", "insn-config.h", "flags.h", "expmed.h", "dojump.h",
1702 "explow.h", "calls.h", "memmodel.h", "emit-rtl.h", "varasm.h",
1703 "stmt.h", "expr.h", "alloc-pool.h", "cselib.h", "insn-addr.h",
1704 "optabs.h", "libfuncs.h", "debug.h", "internal-fn.h",
1705 "gimple-iterator.h", "gimple-fold.h", "value-range.h",
1706 "value-range-storage.h",
1707 "tree-eh.h", "gimple-ssa.h", "tree-cfg.h",
1708 "tree-vrp.h", "tree-phinodes.h", "ssa-iterators.h", "stringpool.h",
1709 "tree-ssanames.h", "tree-ssa-loop.h", "tree-ssa-loop-ivopts.h",
1710 "tree-ssa-loop-manip.h", "tree-ssa-loop-niter.h", "tree-into-ssa.h",
1711 "tree-dfa.h", "tree-ssa.h", "reload.h", "cpplib.h", "tree-chrec.h",
1712 "except.h", "output.h", "cfgloop.h", "target.h", "lto-streamer.h",
1713 "target-globals.h", "ipa-ref.h", "cgraph.h", "symbol-summary.h",
1714 "ipa-prop.h", "ipa-fnsummary.h", "dwarf2out.h", "omp-general.h",
1715 "omp-offload.h", "ipa-modref-tree.h", "ipa-modref.h", "symtab-thunks.h",
1716 "symtab-clones.h", "diagnostic-spec.h", "ctfc.h",
1717 NULL
1718 };
1719 const char *const *ifp;
1720 outf_p gtype_desc_c;
1721
1722 gtype_desc_c = create_file (name: "GCC", oname: "gtype-desc.cc");
1723 for (ifp = ifiles; *ifp; ifp++)
1724 oprintf (o: gtype_desc_c, format: "#include \"%s\"\n", *ifp);
1725 for (int j = 0; j < (int) num_build_headers; j++)
1726 oprintf (o: gtype_desc_c, format: "#include \"%s\"\n", build_headers[j]);
1727
1728 /* Make sure we handle "cfun" specially. */
1729 oprintf (o: gtype_desc_c, format: "\n/* See definition in function.h. */\n");
1730 oprintf (o: gtype_desc_c, format: "#undef cfun\n");
1731
1732 oprintf (o: gtype_desc_c,
1733 format: "\n"
1734 "/* Types with a \"gcc::\" namespace have it stripped\n"
1735 " during gengtype parsing. Provide a \"using\" directive\n"
1736 " to ensure that the fully-qualified types are found. */\n"
1737 "using namespace gcc;\n");
1738 }
1739}
1740
1741/* For INPF an input file, return the real basename of INPF, with all
1742 the directory components skipped. */
1743
1744static const char *
1745get_file_realbasename (const input_file *inpf)
1746{
1747 return lbasename (get_input_file_name (inpf));
1748}
1749
1750/* For INPF a filename, return the relative path to INPF from
1751 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1752
1753const char *
1754get_file_srcdir_relative_path (const input_file *inpf)
1755{
1756 const char *f = get_input_file_name (inpf);
1757 if (strlen (s: f) > srcdir_len
1758 && IS_DIR_SEPARATOR (f[srcdir_len])
1759 && strncmp (s1: f, s2: srcdir, n: srcdir_len) == 0)
1760 return f + srcdir_len + 1;
1761 else
1762 return NULL;
1763}
1764
1765/* For INPF an input_file, return the relative path to INPF from
1766 $(srcdir) if the latter is a prefix in INPF, or the real basename
1767 of INPF otherwise. */
1768
1769static const char *
1770get_file_basename (const input_file *inpf)
1771{
1772 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1773
1774 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1775}
1776
1777/* For F a filename, return the lang_dir_names relative index of the language
1778 directory that is a prefix in F, if any, -1 otherwise. */
1779
1780static int
1781get_prefix_langdir_index (const char *f)
1782{
1783 size_t f_len = strlen (s: f);
1784 size_t lang_index;
1785
1786 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1787 {
1788 const char *langdir = lang_dir_names[lang_index];
1789 size_t langdir_len = strlen (s: langdir);
1790
1791 if (f_len > langdir_len
1792 && IS_DIR_SEPARATOR (f[langdir_len])
1793 && memcmp (s1: f, s2: langdir, n: langdir_len) == 0)
1794 return lang_index;
1795 }
1796
1797 return -1;
1798}
1799
1800/* For INPF an input file, return the name of language directory where
1801 F is located, if any, NULL otherwise. */
1802
1803static const char *
1804get_file_langdir (const input_file *inpf)
1805{
1806 /* Get the relative path to INPF from $(srcdir) and find the
1807 language by comparing the prefix with language directory names.
1808 If INPF is not even srcdir relative, no point in looking
1809 further. */
1810
1811 int lang_index;
1812 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1813 const char *r;
1814
1815 if (!srcdir_relative_path)
1816 return NULL;
1817
1818 lang_index = get_prefix_langdir_index (f: srcdir_relative_path);
1819 if (lang_index < 0 && startswith (str: srcdir_relative_path, prefix: "c-family"))
1820 r = "c-family";
1821 else if (lang_index >= 0)
1822 r = lang_dir_names[lang_index];
1823 else
1824 r = NULL;
1825
1826 return r;
1827}
1828
1829/* The gt- output file name for INPF. */
1830
1831static const char *
1832get_file_gtfilename (const input_file *inpf)
1833{
1834 /* Cook up an initial version of the gt- file name from the file real
1835 basename and the language name, if any. */
1836
1837 const char *basename = get_file_realbasename (inpf);
1838 const char *langdir = get_file_langdir (inpf);
1839
1840 char *result =
1841 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1842 : xasprintf ("gt-%s", basename));
1843
1844 /* Then replace all non alphanumerics characters by '-' and change the
1845 extension to ".h". We expect the input filename extension was at least
1846 one character long. */
1847
1848 char *s = result;
1849
1850 for (; *s != '.'; s++)
1851 if (!ISALNUM (*s) && *s != '-')
1852 *s = '-';
1853
1854 memcpy (dest: s, src: ".h", n: sizeof (".h"));
1855
1856 return result;
1857}
1858
1859/* Each input_file has its associated output file outf_p. The
1860 association is computed by the function
1861 get_output_file_with_visibility. The associated file is cached
1862 inside input_file in its inpoutf field, so is really computed only
1863 once. Associated output file paths (i.e. output_name-s) are
1864 computed by a rule based regexp machinery, using the files_rules
1865 array of struct file_rule_st. A for_name is also computed, giving
1866 the source file name for which the output_file is generated; it is
1867 often the last component of the input_file path. */
1868
1869
1870/*
1871 Regexpr machinery to compute the output_name and for_name-s of each
1872 input_file. We have a sequence of file rules which gives the POSIX
1873 extended regular expression to match an input file path, and two
1874 transformed strings for the corresponding output_name and the
1875 corresponding for_name. The transformed string contain dollars: $0
1876 is replaced by the entire match, $1 is replaced by the substring
1877 matching the first parenthesis in the regexp, etc. And $$ is replaced
1878 by a single verbatim dollar. The rule order is important. The
1879 general case is last, and the particular cases should come before.
1880 An action routine can, when needed, update the out_name & for_name
1881 and/or return the appropriate output file. It is invoked only when a
1882 rule is triggered. When a rule is triggered, the output_name and
1883 for_name are computed using their transform string in while $$, $0,
1884 $1, ... are suitably replaced. If there is an action, it is called.
1885 In some few cases, the action can directly return the outf_p, but
1886 usually it just updates the output_name and for_name so should free
1887 them before replacing them. The get_output_file_with_visibility
1888 function creates an outf_p only once per each output_name, so it
1889 scans the output_files list for previously seen output file names.
1890 */
1891
1892/* Signature of actions in file rules. */
1893typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
1894
1895
1896struct file_rule_st {
1897 const char* frul_srcexpr; /* Source string for regexp. */
1898 int frul_rflags; /* Flags passed to regcomp, usually
1899 * REG_EXTENDED. */
1900 regex_t* frul_re; /* Compiled regular expression
1901 obtained by regcomp. */
1902 const char* frul_tr_out; /* Transformation string for making
1903 * the output_name, with $1 ... $9 for
1904 * subpatterns and $0 for the whole
1905 * matched filename. */
1906 const char* frul_tr_for; /* Tranformation string for making the
1907 for_name. */
1908 frul_actionrout_t* frul_action; /* The action, if non null, is
1909 * called once the rule matches, on
1910 * the transformed out_name &
1911 * for_name. It could change them
1912 * and/or give the output file. */
1913};
1914
1915/* File rule action handling *.h files. */
1916static outf_p header_dot_h_frul (input_file*, char**, char**);
1917
1918/* File rule action handling *.cc files. */
1919static outf_p source_dot_cc_frul (input_file*, char**, char**);
1920
1921#define NULL_REGEX (regex_t*)0
1922
1923/* The prefix in our regexp-s matching the directory. */
1924#define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1925
1926#define NULL_FRULACT (frul_actionrout_t*)0
1927
1928/* The array of our rules governing file name generation. Rules order
1929 matters, so change with extreme care! */
1930
1931struct file_rule_st files_rules[] = {
1932 /* The general rule assumes that files in subdirectories belong to a
1933 particular front-end, and files not in subdirectories are shared.
1934 The following rules deal with exceptions - files that are in
1935 subdirectories and yet are shared, and files that are top-level,
1936 but are not shared. */
1937
1938 /* the c-family/ source directory is special. */
1939 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.cc$",
1940 REG_EXTENDED, NULL_REGEX,
1941 .frul_tr_out: "gt-c-family-$3.h", .frul_tr_for: "c-family/$3.cc", NULL_FRULACT},
1942
1943 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
1944 REG_EXTENDED, NULL_REGEX,
1945 .frul_tr_out: "gt-c-family-$3.h", .frul_tr_for: "c-family/$3.h", NULL_FRULACT},
1946
1947 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.cc ! */
1948 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
1949 REG_EXTENDED, NULL_REGEX, .frul_tr_out: "gt-c-c-decl.h", .frul_tr_for: "c/c-decl.cc", NULL_FRULACT},
1950
1951 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
1952 REG_EXTENDED, NULL_REGEX, .frul_tr_out: "gt-c-c-decl.h", .frul_tr_for: "c/c-decl.cc", NULL_FRULACT},
1953
1954 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.cc ! */
1955 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
1956 REG_EXTENDED, NULL_REGEX,
1957 .frul_tr_out: "gt-cp-tree.h", .frul_tr_for: "cp/tree.cc", NULL_FRULACT },
1958
1959 /* cp/decl.h & cp/decl.cc gives gt-cp-decl.h for cp/decl.cc ! */
1960 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
1961 REG_EXTENDED, NULL_REGEX,
1962 .frul_tr_out: "gt-cp-decl.h", .frul_tr_for: "cp/decl.cc", NULL_FRULACT },
1963
1964 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.cc ! */
1965 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
1966 REG_EXTENDED, NULL_REGEX,
1967 .frul_tr_out: "gt-cp-name-lookup.h", .frul_tr_for: "cp/name-lookup.cc", NULL_FRULACT },
1968
1969 /* cp/parser.h gives gt-cp-parser.h for cp/parser.cc ! */
1970 { DIR_PREFIX_REGEX "cp/parser\\.h$",
1971 REG_EXTENDED, NULL_REGEX,
1972 .frul_tr_out: "gt-cp-parser.h", .frul_tr_for: "cp/parser.cc", NULL_FRULACT },
1973
1974 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.cc ! */
1975 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
1976 REG_EXTENDED, NULL_REGEX,
1977 .frul_tr_out: "gt-objc-objc-act.h", .frul_tr_for: "objc/objc-act.cc", NULL_FRULACT },
1978
1979 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.cc ! */
1980 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
1981 REG_EXTENDED, NULL_REGEX,
1982 .frul_tr_out: "gt-objc-objc-map.h", .frul_tr_for: "objc/objc-map.cc", NULL_FRULACT },
1983
1984 /* General cases. For header *.h and *.cc files, we
1985 * need special actions to handle the language. */
1986
1987 /* Source *.cc files are using get_file_gtfilename to compute their
1988 output_name and get_file_basename to compute their for_name
1989 through the source_dot_cc_frul action. */
1990 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.cc$",
1991 REG_EXTENDED, NULL_REGEX, .frul_tr_out: "gt-$3.h", .frul_tr_for: "$3.cc", .frul_action: source_dot_cc_frul},
1992
1993 /* Common header files get "gtype-desc.cc" as their output_name,
1994 * while language specific header files are handled specially. So
1995 * we need the header_dot_h_frul action. */
1996 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
1997 REG_EXTENDED, NULL_REGEX, .frul_tr_out: "gt-$3.h", .frul_tr_for: "$3.h", .frul_action: header_dot_h_frul},
1998
1999 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
2000 REG_EXTENDED, NULL_REGEX, .frul_tr_out: "gt-$3.h", .frul_tr_for: "$3.in", NULL_FRULACT},
2001
2002 /* Mandatory null last entry signaling end of rules. */
2003 {NULL, .frul_rflags: 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
2004};
2005
2006/* Special file rules action for handling *.h header files. It gives
2007 "gtype-desc.cc" for common headers and corresponding output
2008 files for language-specific header files. */
2009static outf_p
2010header_dot_h_frul (input_file* inpf, char**poutname,
2011 char**pforname ATTRIBUTE_UNUSED)
2012{
2013 const char *basename = 0;
2014 int lang_index = 0;
2015 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2016 (void*) inpf, get_input_file_name (inpf),
2017 *poutname, *pforname);
2018 basename = get_file_basename (inpf);
2019 lang_index = get_prefix_langdir_index (f: basename);
2020 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
2021
2022 if (lang_index >= 0)
2023 {
2024 /* The header is language specific. Given output_name &
2025 for_name remains unchanged. The base_files array gives the
2026 outf_p. */
2027 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2028 (void*) base_files[lang_index],
2029 (base_files[lang_index])->name);
2030 return base_files[lang_index];
2031 }
2032 else
2033 {
2034 /* The header is common to all front-end languages. So
2035 output_name is "gtype-desc.cc" file. The calling function
2036 get_output_file_with_visibility will find its outf_p. */
2037 free (ptr: *poutname);
2038 *poutname = xstrdup ("gtype-desc.cc");
2039 DBGPRINTF ("special 'gtype-desc.cc' for inpname %s",
2040 get_input_file_name (inpf));
2041 return NULL;
2042 }
2043}
2044
2045
2046/* Special file rules action for handling *.cc source files using
2047 * get_file_gtfilename to compute their output_name and
2048 * get_file_basename to compute their for_name. The output_name is
2049 * gt-<LANG>-<BASE>.h for language specific source files, and
2050 * gt-<BASE>.h for common source files. */
2051static outf_p
2052source_dot_cc_frul (input_file* inpf, char**poutname, char**pforname)
2053{
2054 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
2055 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
2056 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2057 (void*) inpf, get_input_file_name (inpf),
2058 *poutname, *pforname);
2059 DBGPRINTF ("newoutname %s", newoutname);
2060 DBGPRINTF ("newbasename %s", newbasename);
2061 free (ptr: *poutname);
2062 free (ptr: *pforname);
2063 *poutname = newoutname;
2064 *pforname = newbasename;
2065 return NULL;
2066}
2067
2068/* Utility function for get_output_file_with_visibility which returns
2069 * a malloc-ed substituted string using TRS on matching of the FILNAM
2070 * file name, using the PMATCH array. */
2071static char*
2072matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
2073 const char *trs)
2074{
2075 struct obstack str_obstack;
2076 char *str = NULL;
2077 char *rawstr = NULL;
2078 const char *pt = NULL;
2079 DBGPRINTF ("filnam %s", filnam);
2080 obstack_init (&str_obstack);
2081 for (pt = trs; *pt; pt++) {
2082 char c = *pt;
2083 if (c == '$')
2084 {
2085 if (pt[1] == '$')
2086 {
2087 /* A double dollar $$ is substituted by a single verbatim
2088 dollar, but who really uses dollar signs in file
2089 paths? */
2090 obstack_1grow (&str_obstack, '$');
2091 }
2092 else if (ISDIGIT (pt[1]))
2093 {
2094 /* Handle $0 $1 ... $9 by appropriate substitution. */
2095 int dolnum = pt[1] - '0';
2096 int so = pmatch[dolnum].rm_so;
2097 int eo = pmatch[dolnum].rm_eo;
2098 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2099 if (so>=0 && eo>=so)
2100 obstack_grow (&str_obstack, filnam + so, eo - so);
2101 }
2102 else
2103 {
2104 /* This can happen only when files_rules is buggy! */
2105 gcc_unreachable ();
2106 }
2107 /* Always skip the character after the dollar. */
2108 pt++;
2109 }
2110 else
2111 obstack_1grow (&str_obstack, c);
2112 }
2113 obstack_1grow (&str_obstack, '\0');
2114 rawstr = XOBFINISH (&str_obstack, char *);
2115 str = xstrdup (rawstr);
2116 obstack_free (&str_obstack, NULL);
2117 DBGPRINTF ("matched replacement %s", str);
2118 rawstr = NULL;
2119 return str;
2120}
2121
2122
2123/* An output file, suitable for definitions, that can see declarations
2124 made in INPF and is linked into every language that uses INPF.
2125 Since the result is cached inside INPF, that argument cannot be
2126 declared constant, but is "almost" constant. */
2127
2128outf_p
2129get_output_file_with_visibility (input_file *inpf)
2130{
2131 outf_p r;
2132 char *for_name = NULL;
2133 char *output_name = NULL;
2134 const char* inpfname;
2135
2136 /* This can happen when we need a file with visibility on a
2137 structure that we've never seen. We have to just hope that it's
2138 globally visible. */
2139 if (inpf == NULL)
2140 inpf = system_h_file;
2141
2142 /* The result is cached in INPF, so return it if already known. */
2143 if (inpf->inpoutf)
2144 return inpf->inpoutf;
2145
2146 /* In plugin mode, return NULL unless the input_file is one of the
2147 plugin_files. */
2148 if (plugin_files)
2149 {
2150 size_t i;
2151 for (i = 0; i < nb_plugin_files; i++)
2152 if (inpf == plugin_files[i])
2153 {
2154 inpf->inpoutf = plugin_output;
2155 return plugin_output;
2156 }
2157
2158 return NULL;
2159 }
2160
2161 inpfname = get_input_file_name (inpf);
2162
2163 /* Try each rule in sequence in files_rules until one is triggered. */
2164 {
2165 int rulix = 0;
2166 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2167 (void*) inpf, inpfname);
2168
2169 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2170 {
2171 DBGPRINTF ("rulix#%d srcexpr %s",
2172 rulix, files_rules[rulix].frul_srcexpr);
2173
2174 if (!files_rules[rulix].frul_re)
2175 {
2176 /* Compile the regexpr lazily. */
2177 int err = 0;
2178 files_rules[rulix].frul_re = XCNEW (regex_t);
2179 err = regcomp (preg: files_rules[rulix].frul_re,
2180 pattern: files_rules[rulix].frul_srcexpr,
2181 cflags: files_rules[rulix].frul_rflags);
2182 if (err)
2183 {
2184 /* The regular expression compilation fails only when
2185 file_rules is buggy. */
2186 gcc_unreachable ();
2187 }
2188 }
2189
2190 output_name = NULL;
2191 for_name = NULL;
2192
2193 /* Match the regexpr and trigger the rule if matched. */
2194 {
2195 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2196 $3, ... $9. */
2197 regmatch_t pmatch[10];
2198 memset (s: pmatch, c: 0, n: sizeof (pmatch));
2199 if (!regexec (preg: files_rules[rulix].frul_re,
2200 string: inpfname, nmatch: 10, pmatch: pmatch, eflags: 0))
2201 {
2202 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2203 (void*) inpf, inpfname, rulix,
2204 files_rules[rulix].frul_srcexpr);
2205 for_name =
2206 matching_file_name_substitute (filnam: inpfname, pmatch,
2207 trs: files_rules[rulix].frul_tr_for);
2208 DBGPRINTF ("for_name %s", for_name);
2209 output_name =
2210 matching_file_name_substitute (filnam: inpfname, pmatch,
2211 trs: files_rules[rulix].frul_tr_out);
2212 DBGPRINTF ("output_name %s", output_name);
2213 if (files_rules[rulix].frul_action)
2214 {
2215 /* Invoke our action routine. */
2216 outf_p of = NULL;
2217 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2218 rulix, output_name, for_name);
2219 of =
2220 (files_rules[rulix].frul_action) (inpf,
2221 &output_name, &for_name);
2222 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2223 rulix, (void*)of, output_name, for_name);
2224 /* If the action routine returned something, give it back
2225 immediately and cache it in inpf. */
2226 if (of)
2227 {
2228 inpf->inpoutf = of;
2229 return of;
2230 }
2231 }
2232 /* The rule matched, and had no action, or that action did
2233 not return any output file but could have changed the
2234 output_name or for_name. We break out of the loop on the
2235 files_rules. */
2236 break;
2237 }
2238 else
2239 {
2240 /* The regexpr did not match. */
2241 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2242 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2243 continue;
2244 }
2245 }
2246 }
2247 }
2248 if (!output_name || !for_name)
2249 {
2250 /* This should not be possible, and could only happen if the
2251 files_rules is incomplete or buggy. */
2252 fatal ("failed to compute output name for %s", inpfname);
2253 }
2254
2255 /* Look through to see if we've ever seen this output filename
2256 before. If found, cache the result in inpf. */
2257 for (r = output_files; r; r = r->next)
2258 if (filename_cmp (s1: r->name, s2: output_name) == 0)
2259 {
2260 inpf->inpoutf = r;
2261 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2262 output_name, for_name);
2263 return r;
2264 }
2265
2266 /* If not found, create it, and cache it in inpf. */
2267 r = create_file (name: for_name, oname: output_name);
2268
2269 gcc_assert (r && r->name);
2270 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2271 output_name, for_name);
2272 inpf->inpoutf = r;
2273 return r;
2274
2275
2276}
2277
2278/* The name of an output file, suitable for definitions, that can see
2279 declarations made in INPF and is linked into every language that
2280 uses INPF. */
2281
2282const char *
2283get_output_file_name (input_file* inpf)
2284{
2285 outf_p o = get_output_file_with_visibility (inpf);
2286 if (o)
2287 return o->name;
2288 return NULL;
2289}
2290
2291/* Check if existing file is equal to the in memory buffer. */
2292
2293static bool
2294is_file_equal (outf_p of)
2295{
2296 FILE *newfile = fopen (of->name, "r");
2297 size_t i;
2298 bool equal;
2299 if (newfile == NULL)
2300 return false;
2301
2302 equal = true;
2303 for (i = 0; i < of->bufused; i++)
2304 {
2305 int ch;
2306 ch = fgetc (newfile);
2307 if (ch == EOF || ch != (unsigned char) of->buf[i])
2308 {
2309 equal = false;
2310 break;
2311 }
2312 }
2313 if (equal && EOF != fgetc (newfile))
2314 equal = false;
2315 fclose (stream: newfile);
2316 return equal;
2317}
2318
2319/* Copy the output to its final destination,
2320 but don't unnecessarily change modification times. */
2321
2322static void
2323close_output_files (void)
2324{
2325 int nbwrittenfiles = 0;
2326 outf_p of;
2327
2328 for (of = output_files; of; of = of->next)
2329 {
2330 if (!is_file_equal (of))
2331 {
2332 FILE *newfile = NULL;
2333 char *backupname = NULL;
2334 /* Back up the old version of the output file gt-FOO.cc as
2335 BACKUPDIR/gt-FOO.cc~ if we have a backup directory. */
2336 if (backup_dir)
2337 {
2338 backupname = concat (backup_dir, "/",
2339 lbasename (of->name), "~", NULL);
2340 if (!access (name: of->name, F_OK) && rename (old: of->name, new: backupname))
2341 fatal ("failed to back up %s as %s: %s",
2342 of->name, backupname, xstrerror (errno));
2343 }
2344
2345 newfile = fopen (of->name, "w");
2346 if (newfile == NULL)
2347 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2348 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2349 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2350 if (fclose (stream: newfile) != 0)
2351 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2352 nbwrittenfiles++;
2353 if (verbosity_level >= 2 && backupname)
2354 printf (format: "%s wrote #%-3d %s backed-up in %s\n",
2355 progname, nbwrittenfiles, of->name, backupname);
2356 else if (verbosity_level >= 1)
2357 printf (format: "%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2358 free (ptr: backupname);
2359 }
2360 else
2361 {
2362 /* output file remains unchanged. */
2363 if (verbosity_level >= 2)
2364 printf (format: "%s keep %s\n", progname, of->name);
2365 }
2366 free (ptr: of->buf);
2367 of->buf = NULL;
2368 of->bufused = of->buflength = 0;
2369 }
2370 if (verbosity_level >= 1)
2371 printf (format: "%s wrote %d files.\n", progname, nbwrittenfiles);
2372}
2373
2374struct flist
2375{
2376 struct flist *next;
2377 int started_p;
2378 const input_file* file;
2379 outf_p f;
2380};
2381
2382struct walk_type_data;
2383
2384/* For scalars and strings, given the item in 'val'.
2385 For structures, given a pointer to the item in 'val'.
2386 For misc. pointers, given the item in 'val'.
2387*/
2388typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2389typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2390
2391/* Parameters for write_types. */
2392
2393struct write_types_data
2394{
2395 const char *prefix;
2396 const char *param_prefix;
2397 const char *subfield_marker_routine;
2398 const char *marker_routine;
2399 const char *reorder_note_routine;
2400 const char *comment;
2401 enum write_types_kinds kind;
2402};
2403
2404static void output_escaped_param (const struct walk_type_data *d,
2405 const char *, const char *);
2406static void output_mangled_typename (outf_p, const_type_p);
2407static void walk_type (type_p t, struct walk_type_data *d);
2408static void write_func_for_structure (type_p orig_s, type_p s,
2409 const struct write_types_data *wtd);
2410static void write_types_process_field
2411 (type_p f, const struct walk_type_data *d);
2412static void write_types (outf_p output_header,
2413 type_p structures,
2414 const struct write_types_data *wtd);
2415static void write_types_local_process_field
2416 (type_p f, const struct walk_type_data *d);
2417static void write_local_func_for_structure (const_type_p orig_s, type_p s);
2418static void write_local (outf_p output_header,
2419 type_p structures);
2420static int contains_scalar_p (type_p t);
2421static void put_mangled_filename (outf_p, const input_file *);
2422static void finish_root_table (struct flist *flp, const char *pfx,
2423 const char *lastname,
2424 const char *tname, const char *name);
2425static void write_root (outf_p, pair_p, type_p, const char *, int,
2426 struct fileloc *, bool);
2427static void write_array (outf_p f, pair_p v,
2428 const struct write_types_data *wtd);
2429static void write_roots (pair_p, bool);
2430
2431/* Parameters for walk_type. */
2432
2433struct walk_type_data
2434{
2435 process_field_fn process_field;
2436 const void *cookie;
2437 outf_p of;
2438 options_p opt;
2439 const char *val;
2440 const char *prev_val[4];
2441 int indent;
2442 int counter;
2443 const struct fileloc *line;
2444 lang_bitmap bitmap;
2445 int used_length;
2446 type_p orig_s;
2447 const char *reorder_fn;
2448 bool fn_wants_lvalue;
2449 bool in_record_p;
2450 int loopcounter;
2451 bool in_ptr_field;
2452 bool have_this_obj;
2453 bool in_nested_ptr;
2454};
2455
2456
2457/* Given a string TYPE_NAME, representing a C++ typename, return a valid
2458 pre-processor identifier to use in a #define directive. This replaces
2459 special characters used in C++ identifiers like '>', '<' and ':' with
2460 '_'.
2461
2462 If no C++ special characters are found in TYPE_NAME, return
2463 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2464 characters replaced with '_'. In this case, the caller is
2465 responsible for freeing the allocated string. */
2466
2467static const char *
2468filter_type_name (const char *type_name)
2469{
2470 if (strchr (s: type_name, c: '<') || strchr (s: type_name, c: ':'))
2471 {
2472 size_t i;
2473 char *s = xstrdup (type_name);
2474 for (i = 0; i < strlen (s: s); i++)
2475 if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ','
2476 || s[i] == '*')
2477 s[i] = '_';
2478 return s;
2479 }
2480 else
2481 return type_name;
2482}
2483
2484
2485/* Print a mangled name representing T to OF. */
2486
2487static void
2488output_mangled_typename (outf_p of, const_type_p t)
2489{
2490 if (t == NULL)
2491 oprintf (o: of, format: "Z");
2492 else
2493 switch (t->kind)
2494 {
2495 case TYPE_NONE:
2496 case TYPE_UNDEFINED:
2497 case TYPE_CALLBACK:
2498 gcc_unreachable ();
2499 break;
2500 case TYPE_POINTER:
2501 oprintf (o: of, format: "P");
2502 output_mangled_typename (of, t: t->u.p);
2503 break;
2504 case TYPE_SCALAR:
2505 oprintf (o: of, format: "I");
2506 break;
2507 case TYPE_STRING:
2508 oprintf (o: of, format: "S");
2509 break;
2510 case TYPE_STRUCT:
2511 case TYPE_UNION:
2512 case TYPE_LANG_STRUCT:
2513 case TYPE_USER_STRUCT:
2514 {
2515 /* For references to classes within an inheritance hierarchy,
2516 only ever reference the ultimate base class, since only
2517 it will have gt_ functions. */
2518 t = get_ultimate_base_class (s: t);
2519 const char *id_for_tag = filter_type_name (type_name: t->u.s.tag);
2520 oprintf (o: of, format: "%lu%s", (unsigned long) strlen (s: id_for_tag),
2521 id_for_tag);
2522 if (id_for_tag != t->u.s.tag)
2523 free (CONST_CAST (char *, id_for_tag));
2524 }
2525 break;
2526 case TYPE_ARRAY:
2527 gcc_unreachable ();
2528 }
2529}
2530
2531/* Print PARAM to D->OF processing escapes. D->VAL references the
2532 current object, D->PREV_VAL the object containing the current
2533 object, ONAME is the name of the option and D->LINE is used to
2534 print error messages. */
2535
2536static void
2537output_escaped_param (const struct walk_type_data *d, const char *param,
2538 const char *oname)
2539{
2540 const char *p;
2541
2542 for (p = param; *p; p++)
2543 if (*p != '%')
2544 oprintf (o: d->of, format: "%c", *p);
2545 else
2546 switch (*++p)
2547 {
2548 case 'h':
2549 oprintf (o: d->of, format: "(%s)", d->prev_val[2]);
2550 break;
2551 case '0':
2552 oprintf (o: d->of, format: "(%s)", d->prev_val[0]);
2553 break;
2554 case '1':
2555 oprintf (o: d->of, format: "(%s)", d->prev_val[1]);
2556 break;
2557 case 'a':
2558 {
2559 const char *pp = d->val + strlen (s: d->val);
2560 while (pp[-1] == ']')
2561 while (*pp != '[')
2562 pp--;
2563 oprintf (o: d->of, format: "%s", pp);
2564 }
2565 break;
2566 default:
2567 error_at_line (pos: d->line, msg: "`%s' option contains bad escape %c%c",
2568 oname, '%', *p);
2569 }
2570}
2571
2572const char *
2573get_string_option (options_p opt, const char *key)
2574{
2575 for (; opt; opt = opt->next)
2576 if (opt->kind == OPTION_STRING && strcmp (s1: opt->name, s2: key) == 0)
2577 return opt->info.string;
2578 return NULL;
2579}
2580
2581/* Machinery for avoiding duplicate tags within switch statements. */
2582struct seen_tag
2583{
2584 const char *tag;
2585 struct seen_tag *next;
2586};
2587
2588int
2589already_seen_tag (struct seen_tag *seen_tags, const char *tag)
2590{
2591 /* Linear search, so O(n^2), but n is currently small. */
2592 while (seen_tags)
2593 {
2594 if (!strcmp (s1: seen_tags->tag, s2: tag))
2595 return 1;
2596 seen_tags = seen_tags->next;
2597 }
2598 /* Not yet seen this tag. */
2599 return 0;
2600}
2601
2602void
2603mark_tag_as_seen (struct seen_tag **seen_tags, const char *tag)
2604{
2605 /* Add to front of linked list. */
2606 struct seen_tag *new_node = XCNEW (struct seen_tag);
2607 new_node->tag = tag;
2608 new_node->next = *seen_tags;
2609 *seen_tags = new_node;
2610}
2611
2612static void
2613walk_subclasses (type_p base, struct walk_type_data *d,
2614 struct seen_tag **seen_tags)
2615{
2616 for (type_p sub = base->u.s.first_subclass; sub != NULL;
2617 sub = sub->u.s.next_sibling_class)
2618 {
2619 const char *type_tag = get_string_option (opt: sub->u.s.opt, key: "tag");
2620 if (type_tag && !already_seen_tag (seen_tags: *seen_tags, tag: type_tag))
2621 {
2622 mark_tag_as_seen (seen_tags, tag: type_tag);
2623 oprintf (o: d->of, format: "%*scase %s:\n", d->indent, "", type_tag);
2624 d->indent += 2;
2625 oprintf (o: d->of, format: "%*s{\n", d->indent, "");
2626 d->indent += 2;
2627 oprintf (o: d->of, format: "%*s%s *sub = static_cast <%s *> (x);\n",
2628 d->indent, "", sub->u.s.tag, sub->u.s.tag);
2629 const char *old_val = d->val;
2630 d->val = "(*sub)";
2631 walk_type (t: sub, d);
2632 d->val = old_val;
2633 d->indent -= 2;
2634 oprintf (o: d->of, format: "%*s}\n", d->indent, "");
2635 oprintf (o: d->of, format: "%*sbreak;\n", d->indent, "");
2636 d->indent -= 2;
2637 }
2638 walk_subclasses (base: sub, d, seen_tags);
2639 }
2640}
2641
2642/* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2643 which is of type T. Write code to D->OF to constrain execution (at
2644 the point that D->PROCESS_FIELD is called) to the appropriate
2645 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2646 pointers to those objects. D->PREV_VAL lists the objects
2647 containing the current object, D->OPT is a list of options to
2648 apply, D->INDENT is the current indentation level, D->LINE is used
2649 to print error messages, D->BITMAP indicates which languages to
2650 print the structure for. */
2651
2652static void
2653walk_type (type_p t, struct walk_type_data *d)
2654{
2655 const char *length = NULL;
2656 const char *desc = NULL;
2657 const char *type_tag = NULL;
2658 int maybe_undef_p = 0;
2659 int atomic_p = 0;
2660 options_p oo;
2661 const struct nested_ptr_data *nested_ptr_d = NULL;
2662
2663 for (oo = d->opt; oo; oo = oo->next)
2664 if (strcmp (s1: oo->name, s2: "length") == 0 && oo->kind == OPTION_STRING)
2665 length = oo->info.string;
2666 else if (strcmp (s1: oo->name, s2: "maybe_undef") == 0)
2667 maybe_undef_p = 1;
2668 else if (strcmp (s1: oo->name, s2: "desc") == 0 && oo->kind == OPTION_STRING)
2669 desc = oo->info.string;
2670 else if (strcmp (s1: oo->name, s2: "nested_ptr") == 0
2671 && oo->kind == OPTION_NESTED)
2672 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2673 else if (strcmp (s1: oo->name, s2: "dot") == 0)
2674 ;
2675 else if (strcmp (s1: oo->name, s2: "tag") == 0)
2676 type_tag = oo->info.string;
2677 else if (strcmp (s1: oo->name, s2: "special") == 0)
2678 ;
2679 else if (strcmp (s1: oo->name, s2: "skip") == 0)
2680 ;
2681 else if (strcmp (s1: oo->name, s2: "atomic") == 0)
2682 atomic_p = 1;
2683 else if (strcmp (s1: oo->name, s2: "default") == 0)
2684 ;
2685 else if (strcmp (s1: oo->name, s2: "chain_next") == 0)
2686 ;
2687 else if (strcmp (s1: oo->name, s2: "chain_prev") == 0)
2688 ;
2689 else if (strcmp (s1: oo->name, s2: "chain_circular") == 0)
2690 ;
2691 else if (strcmp (s1: oo->name, s2: "reorder") == 0)
2692 ;
2693 else if (strcmp (s1: oo->name, s2: "variable_size") == 0)
2694 ;
2695 else if (strcmp (s1: oo->name, s2: "for_user") == 0)
2696 ;
2697 else if (strcmp (s1: oo->name, s2: "callback") == 0)
2698 ;
2699 else if (strcmp (s1: oo->name, s2: "string_length") == 0)
2700 ;
2701 else
2702 error_at_line (pos: d->line, msg: "unknown option `%s'\n", oo->name);
2703
2704 if (d->used_length)
2705 length = NULL;
2706
2707 if (maybe_undef_p
2708 && (t->kind != TYPE_POINTER || !union_or_struct_p (x: t->u.p)))
2709 {
2710 error_at_line (pos: d->line,
2711 msg: "field `%s' has invalid option `maybe_undef_p'\n",
2712 d->val);
2713 return;
2714 }
2715
2716 if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2717 {
2718 error_at_line (pos: d->line, msg: "field `%s' has invalid option `atomic'\n", d->val);
2719 return;
2720 }
2721
2722 switch (t->kind)
2723 {
2724 case TYPE_SCALAR:
2725 case TYPE_STRING:
2726 case TYPE_CALLBACK:
2727 d->process_field (t, d);
2728 break;
2729
2730 case TYPE_POINTER:
2731 {
2732 d->in_ptr_field = true;
2733 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2734 {
2735 oprintf (o: d->of, format: "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2736 break;
2737 }
2738
2739 /* If a pointer type is marked as "atomic", we process the
2740 field itself, but we don't walk the data that they point to.
2741
2742 There are two main cases where we walk types: to mark
2743 pointers that are reachable, and to relocate pointers when
2744 writing a PCH file. In both cases, an atomic pointer is
2745 itself marked or relocated, but the memory that it points
2746 to is left untouched. In the case of PCH, that memory will
2747 be read/written unchanged to the PCH file. */
2748 if (atomic_p)
2749 {
2750 oprintf (o: d->of, format: "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2751 d->indent += 2;
2752 d->process_field (t, d);
2753 d->indent -= 2;
2754 oprintf (o: d->of, format: "%*s}\n", d->indent, "");
2755 break;
2756 }
2757
2758 if (!length)
2759 {
2760 if (!union_or_struct_p (x: t->u.p))
2761 {
2762 error_at_line (pos: d->line,
2763 msg: "field `%s' is pointer to unimplemented type",
2764 d->val);
2765 break;
2766 }
2767
2768 if (nested_ptr_d)
2769 {
2770 const char *oldprevval2 = d->prev_val[2];
2771 bool old_in_nested_ptr = d->in_nested_ptr;
2772
2773 if (!union_or_struct_p (x: nested_ptr_d->type))
2774 {
2775 error_at_line (pos: d->line,
2776 msg: "field `%s' has invalid "
2777 "option `nested_ptr'\n", d->val);
2778 return;
2779 }
2780
2781 d->prev_val[2] = d->val;
2782 d->in_nested_ptr = true;
2783 oprintf (o: d->of, format: "%*s{\n", d->indent, "");
2784 d->indent += 2;
2785 d->val = xasprintf ("x%d", d->counter++);
2786 oprintf (o: d->of, format: "%*s%s %s * %s%s =\n", d->indent, "",
2787 (nested_ptr_d->type->kind == TYPE_UNION
2788 ? "union" : "struct"),
2789 nested_ptr_d->type->u.s.tag,
2790 d->fn_wants_lvalue ? "" : "const ", d->val);
2791 oprintf (o: d->of, format: "%*s", d->indent + 2, "");
2792 output_escaped_param (d, param: nested_ptr_d->convert_from,
2793 oname: "nested_ptr");
2794 oprintf (o: d->of, format: ";\n");
2795
2796 d->process_field (nested_ptr_d->type, d);
2797
2798 if (d->fn_wants_lvalue)
2799 {
2800 oprintf (o: d->of, format: "%*s%s = ", d->indent, "",
2801 d->prev_val[2]);
2802 d->prev_val[2] = d->val;
2803 output_escaped_param (d, param: nested_ptr_d->convert_to,
2804 oname: "nested_ptr");
2805 oprintf (o: d->of, format: ";\n");
2806 }
2807
2808 d->indent -= 2;
2809 oprintf (o: d->of, format: "%*s}\n", d->indent, "");
2810 d->val = d->prev_val[2];
2811 d->prev_val[2] = oldprevval2;
2812 d->in_nested_ptr = old_in_nested_ptr;
2813 }
2814 else
2815 d->process_field (t->u.p, d);
2816 }
2817 else
2818 {
2819 int loopcounter = d->loopcounter;
2820 const char *oldval = d->val;
2821 const char *oldprevval3 = d->prev_val[3];
2822 char *newval;
2823
2824 oprintf (o: d->of, format: "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2825 d->indent += 2;
2826 oprintf (o: d->of, format: "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2827 oprintf (o: d->of, format: "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2828 "", loopcounter, loopcounter);
2829 if (!d->in_record_p)
2830 output_escaped_param (d, param: length, oname: "length");
2831 else
2832 oprintf (o: d->of, format: "l%d", loopcounter);
2833 if (d->have_this_obj)
2834 /* Try to unswitch loops (see PR53880). */
2835 oprintf (o: d->of, format: ") && ((void *)%s == this_obj", oldval);
2836 oprintf (o: d->of, format: "); i%d++) {\n", loopcounter);
2837 d->indent += 2;
2838 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2839 d->used_length = 1;
2840 d->prev_val[3] = oldval;
2841 walk_type (t: t->u.p, d);
2842 free (ptr: newval);
2843 d->val = oldval;
2844 d->prev_val[3] = oldprevval3;
2845 d->used_length = 0;
2846 d->indent -= 2;
2847 oprintf (o: d->of, format: "%*s}\n", d->indent, "");
2848 d->process_field (t, d);
2849 d->indent -= 2;
2850 oprintf (o: d->of, format: "%*s}\n", d->indent, "");
2851 }
2852 d->in_ptr_field = false;
2853 }
2854 break;
2855
2856 case TYPE_ARRAY:
2857 {
2858 int loopcounter;
2859 const char *oldval = d->val;
2860 char *newval;
2861
2862 /* If it's an array of scalars, we optimize by not generating
2863 any code. */
2864 if (t->u.a.p->kind == TYPE_SCALAR)
2865 break;
2866
2867 if (length)
2868 loopcounter = d->loopcounter;
2869 else
2870 loopcounter = d->counter++;
2871
2872 /* When walking an array, compute the length and store it in a
2873 local variable before walking the array elements, instead of
2874 recomputing the length expression each time through the loop.
2875 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2876 where the length is stored in the first array element,
2877 because otherwise that operand can get overwritten on the
2878 first iteration. */
2879 oprintf (o: d->of, format: "%*s{\n", d->indent, "");
2880 d->indent += 2;
2881 oprintf (o: d->of, format: "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2882 if (!d->in_record_p || !length)
2883 {
2884 oprintf (o: d->of, format: "%*ssize_t l%d = (size_t)(",
2885 d->indent, "", loopcounter);
2886 if (length)
2887 output_escaped_param (d, param: length, oname: "length");
2888 else
2889 oprintf (o: d->of, format: "%s", t->u.a.len);
2890 oprintf (o: d->of, format: ");\n");
2891 }
2892
2893 oprintf (o: d->of, format: "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2894 d->indent, "",
2895 loopcounter, loopcounter, loopcounter, loopcounter);
2896 d->indent += 2;
2897 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2898 d->used_length = 1;
2899 walk_type (t: t->u.a.p, d);
2900 free (ptr: newval);
2901 d->used_length = 0;
2902 d->val = oldval;
2903 d->indent -= 2;
2904 oprintf (o: d->of, format: "%*s}\n", d->indent, "");
2905 d->indent -= 2;
2906 oprintf (o: d->of, format: "%*s}\n", d->indent, "");
2907 }
2908 break;
2909
2910 case TYPE_STRUCT:
2911 case TYPE_UNION:
2912 {
2913 pair_p f;
2914 const char *oldval = d->val;
2915 const char *oldprevval1 = d->prev_val[1];
2916 const char *oldprevval2 = d->prev_val[2];
2917 const int union_p = t->kind == TYPE_UNION;
2918 int seen_default_p = 0;
2919 options_p o;
2920 int lengths_seen = 0;
2921 int endcounter;
2922 bool any_length_seen = false;
2923
2924 if (!t->u.s.line.file)
2925 error_at_line (pos: d->line, msg: "incomplete structure `%s'", t->u.s.tag);
2926
2927 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2928 {
2929 error_at_line (pos: d->line,
2930 msg: "structure `%s' defined for mismatching languages",
2931 t->u.s.tag);
2932 error_at_line (pos: &t->u.s.line, msg: "one structure defined here");
2933 }
2934
2935 /* Some things may also be defined in the structure's options. */
2936 for (o = t->u.s.opt; o; o = o->next)
2937 if (!desc && strcmp (s1: o->name, s2: "desc") == 0
2938 && o->kind == OPTION_STRING)
2939 desc = o->info.string;
2940
2941 d->prev_val[2] = oldval;
2942 d->prev_val[1] = oldprevval2;
2943 if (union_p)
2944 {
2945 if (desc == NULL)
2946 {
2947 error_at_line (pos: d->line,
2948 msg: "missing `desc' option for union `%s'",
2949 t->u.s.tag);
2950 desc = "1";
2951 }
2952 oprintf (o: d->of, format: "%*sswitch ((int) (", d->indent, "");
2953 output_escaped_param (d, param: desc, oname: "desc");
2954 oprintf (o: d->of, format: "))\n");
2955 d->indent += 2;
2956 oprintf (o: d->of, format: "%*s{\n", d->indent, "");
2957 }
2958 else if (desc)
2959 {
2960 /* We have a "desc" option on a struct, signifying the
2961 base class within a GC-managed inheritance hierarchy.
2962 The current code specialcases the base class, then walks
2963 into subclasses, recursing into this routine to handle them.
2964 This organization requires the base class to have a case in
2965 the switch statement, and hence a tag value is mandatory
2966 for the base class. This restriction could be removed, but
2967 it would require some restructing of this code. */
2968 if (!type_tag)
2969 {
2970 error_at_line (pos: d->line,
2971 msg: "missing `tag' option for type `%s'",
2972 t->u.s.tag);
2973 }
2974 oprintf (o: d->of, format: "%*sswitch ((int) (", d->indent, "");
2975 output_escaped_param (d, param: desc, oname: "desc");
2976 oprintf (o: d->of, format: "))\n");
2977 d->indent += 2;
2978 oprintf (o: d->of, format: "%*s{\n", d->indent, "");
2979 oprintf (o: d->of, format: "%*scase %s:\n", d->indent, "", type_tag);
2980 d->indent += 2;
2981 }
2982
2983 FOR_ALL_INHERITED_FIELDS (t, f)
2984 {
2985 options_p oo;
2986 int skip_p = 0;
2987 const char *fieldlength = NULL;
2988
2989 d->reorder_fn = NULL;
2990 for (oo = f->opt; oo; oo = oo->next)
2991 if (strcmp (s1: oo->name, s2: "skip") == 0)
2992 skip_p = 1;
2993 else if (strcmp (s1: oo->name, s2: "length") == 0
2994 && oo->kind == OPTION_STRING)
2995 fieldlength = oo->info.string;
2996
2997 if (skip_p)
2998 continue;
2999 if (fieldlength)
3000 {
3001 lengths_seen++;
3002 d->counter++;
3003 if (!union_p)
3004 {
3005 if (!any_length_seen)
3006 {
3007 oprintf (o: d->of, format: "%*s{\n", d->indent, "");
3008 d->indent += 2;
3009 }
3010 any_length_seen = true;
3011
3012 oprintf (o: d->of, format: "%*ssize_t l%d = (size_t)(",
3013 d->indent, "", d->counter - 1);
3014 output_escaped_param (d, param: fieldlength, oname: "length");
3015 oprintf (o: d->of, format: ");\n");
3016 }
3017 }
3018 }
3019 endcounter = d->counter;
3020
3021 FOR_ALL_INHERITED_FIELDS (t, f)
3022 {
3023 options_p oo;
3024 const char *dot = ".";
3025 const char *tagid = NULL;
3026 int skip_p = 0;
3027 int default_p = 0;
3028 const char *fieldlength = NULL;
3029 char *newval;
3030
3031 d->reorder_fn = NULL;
3032 for (oo = f->opt; oo; oo = oo->next)
3033 if (strcmp (s1: oo->name, s2: "dot") == 0
3034 && oo->kind == OPTION_STRING)
3035 dot = oo->info.string;
3036 else if (strcmp (s1: oo->name, s2: "tag") == 0
3037 && oo->kind == OPTION_STRING)
3038 tagid = oo->info.string;
3039 else if (strcmp (s1: oo->name, s2: "skip") == 0)
3040 skip_p = 1;
3041 else if (strcmp (s1: oo->name, s2: "default") == 0)
3042 default_p = 1;
3043 else if (strcmp (s1: oo->name, s2: "reorder") == 0
3044 && oo->kind == OPTION_STRING)
3045 d->reorder_fn = oo->info.string;
3046 else if (strcmp (s1: oo->name, s2: "length") == 0
3047 && oo->kind == OPTION_STRING)
3048 fieldlength = oo->info.string;
3049
3050 if (skip_p)
3051 continue;
3052
3053 if (union_p && tagid)
3054 {
3055 oprintf (o: d->of, format: "%*scase %s:\n", d->indent, "", tagid);
3056 d->indent += 2;
3057 }
3058 else if (union_p && default_p)
3059 {
3060 oprintf (o: d->of, format: "%*sdefault:\n", d->indent, "");
3061 d->indent += 2;
3062 seen_default_p = 1;
3063 }
3064 else if (!union_p && (default_p || tagid))
3065 error_at_line (pos: d->line,
3066 msg: "can't use `%s' outside a union on field `%s'",
3067 default_p ? "default" : "tag", f->name);
3068 else if (union_p && !(default_p || tagid)
3069 && f->type->kind == TYPE_SCALAR)
3070 {
3071 fprintf (stderr,
3072 format: "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3073 get_input_file_name (inpf: d->line->file), d->line->line,
3074 f->name);
3075 continue;
3076 }
3077 else if (union_p && !(default_p || tagid))
3078 error_at_line (pos: d->line,
3079 msg: "field `%s' is missing `tag' or `default' option",
3080 f->name);
3081
3082 if (fieldlength)
3083 {
3084 d->loopcounter = endcounter - lengths_seen--;
3085 }
3086
3087 d->line = &f->line;
3088 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
3089 d->opt = f->opt;
3090 d->used_length = false;
3091 d->in_record_p = !union_p;
3092
3093 walk_type (t: f->type, d);
3094
3095 d->in_record_p = false;
3096
3097 free (ptr: newval);
3098
3099 if (union_p)
3100 {
3101 oprintf (o: d->of, format: "%*sbreak;\n", d->indent, "");
3102 d->indent -= 2;
3103 }
3104 }
3105 d->reorder_fn = NULL;
3106
3107 d->val = oldval;
3108 d->prev_val[1] = oldprevval1;
3109 d->prev_val[2] = oldprevval2;
3110
3111 if (union_p && !seen_default_p)
3112 {
3113 oprintf (o: d->of, format: "%*sdefault:\n", d->indent, "");
3114 oprintf (o: d->of, format: "%*s break;\n", d->indent, "");
3115 }
3116
3117 if (desc && !union_p)
3118 {
3119 oprintf (o: d->of, format: "%*sbreak;\n", d->indent, "");
3120 d->indent -= 2;
3121 }
3122 if (union_p)
3123 {
3124 oprintf (o: d->of, format: "%*s}\n", d->indent, "");
3125 d->indent -= 2;
3126 }
3127 else if (desc)
3128 {
3129 /* Add cases to handle subclasses. */
3130 struct seen_tag *tags = NULL;
3131 walk_subclasses (base: t, d, seen_tags: &tags);
3132
3133 /* Ensure that if someone forgets a "tag" option that we don't
3134 silent fail to traverse that subclass's fields. */
3135 if (!seen_default_p)
3136 {
3137 oprintf (o: d->of, format: "%*s/* Unrecognized tag value. */\n",
3138 d->indent, "");
3139 oprintf (o: d->of, format: "%*sdefault: gcc_unreachable (); \n",
3140 d->indent, "");
3141 }
3142
3143 /* End of the switch statement */
3144 oprintf (o: d->of, format: "%*s}\n", d->indent, "");
3145 d->indent -= 2;
3146 }
3147 if (any_length_seen)
3148 {
3149 d->indent -= 2;
3150 oprintf (o: d->of, format: "%*s}\n", d->indent, "");
3151 }
3152 }
3153 break;
3154
3155 case TYPE_LANG_STRUCT:
3156 {
3157 type_p nt;
3158 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3159 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3160 break;
3161 if (nt == NULL)
3162 error_at_line (pos: d->line, msg: "structure `%s' differs between languages",
3163 t->u.s.tag);
3164 else
3165 walk_type (t: nt, d);
3166 }
3167 break;
3168
3169 case TYPE_USER_STRUCT:
3170 d->process_field (t, d);
3171 break;
3172
3173 case TYPE_NONE:
3174 case TYPE_UNDEFINED:
3175 gcc_unreachable ();
3176 }
3177}
3178
3179/* process_field routine for marking routines. */
3180
3181static void
3182write_types_process_field (type_p f, const struct walk_type_data *d)
3183{
3184 const struct write_types_data *wtd;
3185 wtd = (const struct write_types_data *) d->cookie;
3186
3187 switch (f->kind)
3188 {
3189 case TYPE_NONE:
3190 case TYPE_UNDEFINED:
3191 gcc_unreachable ();
3192 case TYPE_POINTER:
3193 oprintf (o: d->of, format: "%*s%s (%s", d->indent, "",
3194 wtd->subfield_marker_routine, d->val);
3195 if (wtd->param_prefix)
3196 {
3197 if (f->u.p->kind == TYPE_SCALAR)
3198 /* The current type is a pointer to a scalar (so not
3199 considered like a pointer to instances of user defined
3200 types) and we are seeing it; it means we must be even
3201 more careful about the second argument of the
3202 SUBFIELD_MARKER_ROUTINE call. That argument must
3203 always be the instance of the type for which
3204 write_func_for_structure was called - this really is
3205 what the function SUBFIELD_MARKER_ROUTINE expects.
3206 That is, it must be an instance of the ORIG_S type
3207 parameter of write_func_for_structure. The convention
3208 is that that argument must be "x" in that case (as set
3209 by write_func_for_structure). The problem is, we can't
3210 count on d->prev_val[3] to be always set to "x" in that
3211 case. Sometimes walk_type can set it to something else
3212 (to e.g cooperate with write_array when called from
3213 write_roots). So let's set it to "x" here then. */
3214 oprintf (o: d->of, format: ", x");
3215 else
3216 oprintf (o: d->of, format: ", %s", d->prev_val[3]);
3217 if (d->orig_s)
3218 {
3219 oprintf (o: d->of, format: ", gt_%s_", wtd->param_prefix);
3220 output_mangled_typename (of: d->of, t: d->orig_s);
3221 }
3222 else
3223 oprintf (o: d->of, format: ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3224 }
3225 oprintf (o: d->of, format: ");\n");
3226 if (d->reorder_fn && wtd->reorder_note_routine)
3227 oprintf (o: d->of, format: "%*s%s (%s, %s, %s);\n", d->indent, "",
3228 wtd->reorder_note_routine, d->val,
3229 d->prev_val[3], d->reorder_fn);
3230 break;
3231
3232 case TYPE_STRING:
3233 case TYPE_STRUCT:
3234 case TYPE_UNION:
3235 case TYPE_LANG_STRUCT:
3236 case TYPE_USER_STRUCT:
3237 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3238 {
3239 /* If F is a user-defined type and the field is not a
3240 pointer to the type, then we should not generate the
3241 standard pointer-marking code. All we need to do is call
3242 the user-provided marking function to process the fields
3243 of F. */
3244 oprintf (o: d->of, format: "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3245 d->val);
3246 }
3247 else
3248 {
3249 oprintf (o: d->of, format: "%*sgt_%s_", d->indent, "", wtd->prefix);
3250 output_mangled_typename (of: d->of, t: f);
3251
3252 /* Check if we need to call the special pch note version
3253 for strings that takes an explicit length. */
3254 const auto length_override
3255 = (f->kind == TYPE_STRING && !strcmp (s1: wtd->prefix, s2: "pch_n")
3256 ? get_string_option (opt: d->opt, key: "string_length")
3257 : nullptr);
3258 if (length_override)
3259 {
3260 oprintf (o: d->of, format: "2 (%s, ", d->val);
3261 output_escaped_param (d, param: length_override, oname: "string_length");
3262 }
3263 else
3264 oprintf (o: d->of, format: " (%s", d->val);
3265
3266 oprintf (o: d->of, format: ");\n");
3267 if (d->reorder_fn && wtd->reorder_note_routine)
3268 oprintf (o: d->of, format: "%*s%s (%s, %s, %s);\n", d->indent, "",
3269 wtd->reorder_note_routine, d->val, d->val,
3270 d->reorder_fn);
3271 }
3272 break;
3273
3274 case TYPE_SCALAR:
3275 case TYPE_CALLBACK:
3276 break;
3277
3278 case TYPE_ARRAY:
3279 gcc_unreachable ();
3280 }
3281}
3282
3283/* Return an output file that is suitable for definitions which can
3284 reference struct S */
3285
3286static outf_p
3287get_output_file_for_structure (const_type_p s)
3288{
3289 const input_file *fn;
3290
3291 gcc_assert (union_or_struct_p (s));
3292 fn = s->u.s.line.file;
3293
3294 /* The call to get_output_file_with_visibility may update fn by
3295 caching its result inside, so we need the CONST_CAST. */
3296 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3297}
3298
3299
3300/* Returns the specifier keyword for a string or union type S, empty string
3301 otherwise. */
3302
3303static const char *
3304get_type_specifier (const type_p s)
3305{
3306 if (s->kind == TYPE_STRUCT)
3307 return "struct ";
3308 else if (s->kind == TYPE_LANG_STRUCT)
3309 return get_type_specifier (s: s->u.s.lang_struct);
3310 else if (s->kind == TYPE_UNION)
3311 return "union ";
3312 return "";
3313}
3314
3315
3316/* Emits a declaration for type TY (assumed to be a union or a
3317 structure) on stream OUT. */
3318
3319static void
3320write_type_decl (outf_p out, type_p ty)
3321{
3322 if (union_or_struct_p (x: ty))
3323 oprintf (o: out, format: "%s%s", get_type_specifier (s: ty), ty->u.s.tag);
3324 else if (ty->kind == TYPE_SCALAR)
3325 {
3326 if (ty->u.scalar_is_char)
3327 oprintf (o: out, format: "const char");
3328 else
3329 oprintf (o: out, format: "void");
3330 }
3331 else if (ty->kind == TYPE_POINTER)
3332 {
3333 write_type_decl (out, ty: ty->u.p);
3334 oprintf (o: out, format: " *");
3335 }
3336 else if (ty->kind == TYPE_ARRAY)
3337 {
3338 write_type_decl (out, ty: ty->u.a.p);
3339 oprintf (o: out, format: " *");
3340 }
3341 else if (ty->kind == TYPE_STRING)
3342 {
3343 oprintf (o: out, format: "const char *");
3344 }
3345 else
3346 gcc_unreachable ();
3347}
3348
3349
3350/* Write on OF the name of the marker function for structure S. PREFIX
3351 is the prefix to use (to distinguish ggc from pch markers). */
3352
3353static void
3354write_marker_function_name (outf_p of, type_p s, const char *prefix)
3355{
3356 if (union_or_struct_p (x: s))
3357 {
3358 const char *id_for_tag = filter_type_name (type_name: s->u.s.tag);
3359 oprintf (o: of, format: "gt_%sx_%s", prefix, id_for_tag);
3360 if (id_for_tag != s->u.s.tag)
3361 free (CONST_CAST (char *, id_for_tag));
3362 }
3363 else
3364 gcc_unreachable ();
3365}
3366
3367/* Write on OF a user-callable routine to act as an entry point for
3368 the marking routine for S, generated by write_func_for_structure.
3369 WTD distinguishes between ggc and pch markers. */
3370
3371static void
3372write_user_func_for_structure_ptr (outf_p of, type_p s, const write_types_data *wtd)
3373{
3374 gcc_assert (union_or_struct_p (s));
3375
3376 type_p alias_of = NULL;
3377 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3378 if (strcmp (s1: opt->name, s2: "ptr_alias") == 0)
3379 {
3380 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3381 we do not generate marking code for ORIG_S here. Instead, a
3382 forwarder #define in gtype-desc.h will cause every call to its
3383 marker to call the target of this alias.
3384
3385 However, we still want to create a user entry code for the
3386 aliased type. So, if ALIAS_OF is set, we only generate the
3387 user-callable marker function. */
3388 alias_of = opt->info.type;
3389 break;
3390 }
3391
3392 DBGPRINTF ("write_user_func_for_structure_ptr: %s %s", s->u.s.tag,
3393 wtd->prefix);
3394
3395 /* Only write the function once. */
3396 if (s->u.s.wrote_user_func_for_ptr[wtd->kind])
3397 return;
3398 s->u.s.wrote_user_func_for_ptr[wtd->kind] = true;
3399
3400 oprintf (o: of, format: "\nvoid\n");
3401 oprintf (o: of, format: "gt_%sx (", wtd->prefix);
3402 write_type_decl (out: of, ty: s);
3403 oprintf (o: of, format: " *& x)\n");
3404 oprintf (o: of, format: "{\n");
3405 oprintf (o: of, format: " if (x)\n ");
3406 write_marker_function_name (of,
3407 s: alias_of ? alias_of : get_ultimate_base_class (s),
3408 prefix: wtd->prefix);
3409 oprintf (o: of, format: " ((void *) x);\n");
3410 oprintf (o: of, format: "}\n");
3411}
3412
3413
3414/* Write a function to mark all the fields of type S on OF. PREFIX
3415 and D are as in write_user_marking_functions. */
3416
3417static void
3418write_user_func_for_structure_body (type_p s, const char *prefix,
3419 struct walk_type_data *d)
3420{
3421 oprintf (o: d->of, format: "\nvoid\n");
3422 oprintf (o: d->of, format: "gt_%sx (", prefix);
3423 write_type_decl (out: d->of, ty: s);
3424 oprintf (o: d->of, format: "& x_r ATTRIBUTE_UNUSED)\n");
3425 oprintf (o: d->of, format: "{\n");
3426 oprintf (o: d->of, format: " ");
3427 write_type_decl (out: d->of, ty: s);
3428 oprintf (o: d->of, format: " * ATTRIBUTE_UNUSED x = &x_r;\n");
3429 d->val = "(*x)";
3430 d->indent = 2;
3431 walk_type (t: s, d);
3432 oprintf (o: d->of, format: "}\n");
3433}
3434
3435/* Emit the user-callable functions needed to mark all the types used
3436 by the user structure S. PREFIX is the prefix to use to
3437 distinguish ggc and pch markers. D contains data needed to pass to
3438 walk_type when traversing the fields of a type.
3439
3440 For every type T referenced by S, two routines are generated: one
3441 that takes 'T *', marks the pointer and calls the second routine,
3442 which just marks the fields of T. */
3443
3444static void
3445write_user_marking_functions (type_p s,
3446 const write_types_data *w,
3447 struct walk_type_data *d)
3448{
3449 gcc_assert (s->kind == TYPE_USER_STRUCT);
3450
3451 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3452 {
3453 type_p fld_type = fld->type;
3454 if (fld_type->kind == TYPE_POINTER)
3455 {
3456 type_p pointed_to_type = fld_type->u.p;
3457 if (union_or_struct_p (x: pointed_to_type))
3458 write_user_func_for_structure_ptr (of: d->of, s: pointed_to_type, wtd: w);
3459 }
3460 else if (union_or_struct_p (x: fld_type))
3461 write_user_func_for_structure_body (s: fld_type, prefix: w->prefix, d);
3462 }
3463}
3464
3465
3466/* For S, a structure that's part of ORIG_S write out a routine that:
3467 - Takes a parameter, a void * but actually of type *S
3468 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3469 field of S or its substructures and (in some cases) things
3470 that are pointed to by S. */
3471
3472static void
3473write_func_for_structure (type_p orig_s, type_p s,
3474 const struct write_types_data *wtd)
3475{
3476 const char *chain_next = NULL;
3477 const char *chain_prev = NULL;
3478 const char *chain_circular = NULL;
3479 options_p opt;
3480 struct walk_type_data d;
3481
3482 if (s->u.s.base_class)
3483 {
3484 /* Verify that the base class has a "desc", since otherwise
3485 the traversal hooks there won't attempt to visit fields of
3486 subclasses such as this one. */
3487 const_type_p ubc = get_ultimate_base_class (s);
3488 if ((!opts_have (opts: ubc->u.s.opt, str: "user")
3489 && !opts_have (opts: ubc->u.s.opt, str: "desc")))
3490 error_at_line (pos: &s->u.s.line,
3491 msg: ("'%s' is a subclass of non-GTY(user) GTY class '%s'"
3492 ", but '%s' lacks a discriminator 'desc' option"),
3493 s->u.s.tag, ubc->u.s.tag, ubc->u.s.tag);
3494
3495 /* Don't write fns for subclasses, only for the ultimate base class
3496 within an inheritance hierarchy. */
3497 return;
3498 }
3499
3500 memset (s: &d, c: 0, n: sizeof (d));
3501 d.of = get_output_file_for_structure (s);
3502
3503 bool for_user = false;
3504 for (opt = s->u.s.opt; opt; opt = opt->next)
3505 if (strcmp (s1: opt->name, s2: "chain_next") == 0
3506 && opt->kind == OPTION_STRING)
3507 chain_next = opt->info.string;
3508 else if (strcmp (s1: opt->name, s2: "chain_prev") == 0
3509 && opt->kind == OPTION_STRING)
3510 chain_prev = opt->info.string;
3511 else if (strcmp (s1: opt->name, s2: "chain_circular") == 0
3512 && opt->kind == OPTION_STRING)
3513 chain_circular = opt->info.string;
3514 else if (strcmp (s1: opt->name, s2: "for_user") == 0)
3515 for_user = true;
3516 if (chain_prev != NULL && chain_next == NULL)
3517 error_at_line (pos: &s->u.s.line, msg: "chain_prev without chain_next");
3518 if (chain_circular != NULL && chain_next != NULL)
3519 error_at_line (pos: &s->u.s.line, msg: "chain_circular with chain_next");
3520 if (chain_circular != NULL)
3521 chain_next = chain_circular;
3522
3523 d.process_field = write_types_process_field;
3524 d.cookie = wtd;
3525 d.orig_s = orig_s;
3526 d.opt = s->u.s.opt;
3527 d.line = &s->u.s.line;
3528 d.bitmap = s->u.s.bitmap;
3529 d.prev_val[0] = "*x";
3530 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3531 d.prev_val[3] = "x";
3532 d.val = "(*x)";
3533 d.have_this_obj = false;
3534
3535 oprintf (o: d.of, format: "\n");
3536 oprintf (o: d.of, format: "void\n");
3537 write_marker_function_name (of: d.of, s: orig_s, prefix: wtd->prefix);
3538 oprintf (o: d.of, format: " (void *x_p)\n");
3539 oprintf (o: d.of, format: "{\n ");
3540 write_type_decl (out: d.of, ty: s);
3541 oprintf (o: d.of, format: " * %sx = (", chain_next == NULL ? "const " : "");
3542 write_type_decl (out: d.of, ty: s);
3543 oprintf (o: d.of, format: " *)x_p;\n");
3544 if (chain_next != NULL)
3545 {
3546 /* TYPE_USER_STRUCTs should not occur here. These structures
3547 are completely handled by user code. */
3548 gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
3549
3550 oprintf (o: d.of, format: " ");
3551 write_type_decl (out: d.of, ty: s);
3552 oprintf (o: d.of, format: " * xlimit = x;\n");
3553 }
3554 if (chain_next == NULL)
3555 {
3556 oprintf (o: d.of, format: " if (%s (x", wtd->marker_routine);
3557 if (wtd->param_prefix)
3558 {
3559 oprintf (o: d.of, format: ", x, gt_%s_", wtd->param_prefix);
3560 output_mangled_typename (of: d.of, t: orig_s);
3561 }
3562 oprintf (o: d.of, format: "))\n");
3563 }
3564 else
3565 {
3566 if (chain_circular != NULL)
3567 oprintf (o: d.of, format: " if (!%s (xlimit", wtd->marker_routine);
3568 else
3569 oprintf (o: d.of, format: " while (%s (xlimit", wtd->marker_routine);
3570 if (wtd->param_prefix)
3571 {
3572 oprintf (o: d.of, format: ", xlimit, gt_%s_", wtd->param_prefix);
3573 output_mangled_typename (of: d.of, t: orig_s);
3574 }
3575 oprintf (o: d.of, format: "))\n");
3576 if (chain_circular != NULL)
3577 oprintf (o: d.of, format: " return;\n do\n");
3578
3579 oprintf (o: d.of, format: " xlimit = (");
3580 d.prev_val[2] = "*xlimit";
3581 output_escaped_param (d: &d, param: chain_next, oname: "chain_next");
3582 oprintf (o: d.of, format: ");\n");
3583 if (chain_prev != NULL)
3584 {
3585 oprintf (o: d.of, format: " if (x != xlimit)\n");
3586 oprintf (o: d.of, format: " for (;;)\n");
3587 oprintf (o: d.of, format: " {\n");
3588 oprintf (o: d.of, format: " %s %s * const xprev = (",
3589 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3590
3591 d.prev_val[2] = "*x";
3592 output_escaped_param (d: &d, param: chain_prev, oname: "chain_prev");
3593 oprintf (o: d.of, format: ");\n");
3594 oprintf (o: d.of, format: " if (xprev == NULL) break;\n");
3595 oprintf (o: d.of, format: " x = xprev;\n");
3596 oprintf (o: d.of, format: " (void) %s (xprev", wtd->marker_routine);
3597 if (wtd->param_prefix)
3598 {
3599 oprintf (o: d.of, format: ", xprev, gt_%s_", wtd->param_prefix);
3600 output_mangled_typename (of: d.of, t: orig_s);
3601 }
3602 oprintf (o: d.of, format: ");\n");
3603 oprintf (o: d.of, format: " }\n");
3604 }
3605 if (chain_circular != NULL)
3606 {
3607 oprintf (o: d.of, format: " while (%s (xlimit", wtd->marker_routine);
3608 if (wtd->param_prefix)
3609 {
3610 oprintf (o: d.of, format: ", xlimit, gt_%s_", wtd->param_prefix);
3611 output_mangled_typename (of: d.of, t: orig_s);
3612 }
3613 oprintf (o: d.of, format: "));\n");
3614 oprintf (o: d.of, format: " do\n");
3615 }
3616 else
3617 oprintf (o: d.of, format: " while (x != xlimit)\n");
3618 }
3619 oprintf (o: d.of, format: " {\n");
3620
3621 d.prev_val[2] = "*x";
3622 d.indent = 6;
3623 if (orig_s->kind != TYPE_USER_STRUCT)
3624 walk_type (t: s, d: &d);
3625 else
3626 {
3627 /* User structures have no fields to walk. Simply generate a call
3628 to the user-provided structure marker. */
3629 oprintf (o: d.of, format: "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3630 }
3631
3632 if (chain_next != NULL)
3633 {
3634 oprintf (o: d.of, format: " x = (");
3635 output_escaped_param (d: &d, param: chain_next, oname: "chain_next");
3636 oprintf (o: d.of, format: ");\n");
3637 }
3638
3639 oprintf (o: d.of, format: " }\n");
3640 if (chain_circular != NULL)
3641 oprintf (o: d.of, format: " while (x != xlimit);\n");
3642 oprintf (o: d.of, format: "}\n");
3643
3644 if (orig_s->kind == TYPE_USER_STRUCT)
3645 write_user_marking_functions (s: orig_s, w: wtd, d: &d);
3646
3647 if (for_user)
3648 {
3649 write_user_func_for_structure_body (s: orig_s, prefix: wtd->prefix, d: &d);
3650 write_user_func_for_structure_ptr (of: d.of, s: orig_s, wtd);
3651 }
3652}
3653
3654
3655/* Write out marker routines for STRUCTURES. */
3656
3657static void
3658write_types (outf_p output_header, type_p structures,
3659 const struct write_types_data *wtd)
3660{
3661 int nbfun = 0; /* Count the emitted functions. */
3662 type_p s;
3663
3664 oprintf (o: output_header, format: "\n/* %s*/\n", wtd->comment);
3665
3666 /* We first emit the macros and the declarations. Functions' code is
3667 emitted afterwards. This is needed in plugin mode. */
3668 oprintf (o: output_header, format: "/* Macros and declarations. */\n");
3669 for (s = structures; s; s = s->next)
3670 /* Do not emit handlers for derived classes; we only ever deal with
3671 the ultimate base class within an inheritance hierarchy. */
3672 if ((s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3673 && !s->u.s.base_class)
3674 {
3675 options_p opt;
3676
3677 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3678 continue;
3679
3680 const char *s_id_for_tag = filter_type_name (type_name: s->u.s.tag);
3681
3682 oprintf (o: output_header, format: "#define gt_%s_", wtd->prefix);
3683 output_mangled_typename (of: output_header, t: s);
3684 oprintf (o: output_header, format: "(X) do { \\\n");
3685 oprintf (o: output_header,
3686 format: " if ((intptr_t)(X) != 0) gt_%sx_%s (X);\\\n",
3687 wtd->prefix, s_id_for_tag);
3688 oprintf (o: output_header, format: " } while (0)\n");
3689
3690 for (opt = s->u.s.opt; opt; opt = opt->next)
3691 if (strcmp (s1: opt->name, s2: "ptr_alias") == 0
3692 && opt->kind == OPTION_TYPE)
3693 {
3694 const_type_p const t = (const_type_p) opt->info.type;
3695 if (t->kind == TYPE_STRUCT
3696 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3697 {
3698 const char *t_id_for_tag = filter_type_name (type_name: t->u.s.tag);
3699 oprintf (o: output_header,
3700 format: "#define gt_%sx_%s gt_%sx_%s\n",
3701 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3702 if (t_id_for_tag != t->u.s.tag)
3703 free (CONST_CAST (char *, t_id_for_tag));
3704 }
3705 else
3706 error_at_line (pos: &s->u.s.line,
3707 msg: "structure alias is not a structure");
3708 break;
3709 }
3710 if (opt)
3711 continue;
3712
3713 /* Declare the marker procedure only once. */
3714 oprintf (o: output_header,
3715 format: "extern void gt_%sx_%s (void *);\n",
3716 wtd->prefix, s_id_for_tag);
3717
3718 if (s_id_for_tag != s->u.s.tag)
3719 free (CONST_CAST (char *, s_id_for_tag));
3720
3721 if (s->u.s.line.file == NULL)
3722 {
3723 fprintf (stderr, format: "warning: structure `%s' used but not defined\n",
3724 s->u.s.tag);
3725 continue;
3726 }
3727 }
3728
3729 /* At last we emit the functions code. */
3730 oprintf (o: output_header, format: "\n/* functions code */\n");
3731 for (s = structures; s; s = s->next)
3732 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3733 {
3734 options_p opt;
3735
3736 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3737 continue;
3738 for (opt = s->u.s.opt; opt; opt = opt->next)
3739 if (strcmp (s1: opt->name, s2: "ptr_alias") == 0)
3740 break;
3741 if (opt)
3742 continue;
3743
3744 if (s->kind == TYPE_LANG_STRUCT)
3745 {
3746 type_p ss;
3747 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3748 {
3749 nbfun++;
3750 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3751 nbfun, (void*) ss, ss->u.s.tag);
3752 write_func_for_structure (orig_s: s, s: ss, wtd);
3753 }
3754 }
3755 else
3756 {
3757 nbfun++;
3758 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3759 nbfun, (void*) s, s->u.s.tag);
3760 write_func_for_structure (orig_s: s, s, wtd);
3761 }
3762 }
3763 else
3764 {
3765 /* Structure s is not possibly pointed to, so can be ignored. */
3766 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3767 (void*)s, s->u.s.tag,
3768 (int) s->gc_used);
3769 }
3770
3771 if (verbosity_level >= 2)
3772 printf (format: "%s emitted %d routines for %s\n",
3773 progname, nbfun, wtd->comment);
3774}
3775
3776static const struct write_types_data ggc_wtd = {
3777 .prefix: "ggc_m", NULL, .subfield_marker_routine: "ggc_mark", .marker_routine: "ggc_test_and_set_mark", NULL,
3778 .comment: "GC marker procedures. ",
3779 .kind: WTK_GGC
3780};
3781
3782static const struct write_types_data pch_wtd = {
3783 .prefix: "pch_n", .param_prefix: "pch_p", .subfield_marker_routine: "gt_pch_note_object", .marker_routine: "gt_pch_note_object",
3784 .reorder_note_routine: "gt_pch_note_reorder",
3785 .comment: "PCH type-walking procedures. ",
3786 .kind: WTK_PCH
3787};
3788
3789/* Write out the local pointer-walking routines. */
3790
3791/* process_field routine for local pointer-walking for user-callable
3792 routines. The difference between this and
3793 write_types_local_process_field is that, in this case, we do not
3794 need to check whether the given pointer matches the address of the
3795 parent structure. This check was already generated by the call
3796 to gt_pch_nx in the main gt_pch_p_*() function that is calling
3797 this code. */
3798
3799static void
3800write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
3801{
3802 switch (f->kind)
3803 {
3804 case TYPE_POINTER:
3805 case TYPE_STRUCT:
3806 case TYPE_UNION:
3807 case TYPE_LANG_STRUCT:
3808 case TYPE_STRING:
3809 if (d->in_nested_ptr)
3810 oprintf (o: d->of, format: "%*s op (&(%s), &(%s), cookie);\n",
3811 d->indent, "", d->val, d->prev_val[2]);
3812 oprintf (o: d->of, format: "%*s op (&(%s), NULL, cookie);\n",
3813 d->indent, "", d->val);
3814 break;
3815
3816 case TYPE_USER_STRUCT:
3817 if (d->in_ptr_field)
3818 oprintf (o: d->of, format: "%*s op (&(%s), NULL, cookie);\n",
3819 d->indent, "", d->val);
3820 else
3821 oprintf (o: d->of, format: "%*s gt_pch_nx (&(%s), op, cookie);\n",
3822 d->indent, "", d->val);
3823 break;
3824
3825 case TYPE_SCALAR:
3826 case TYPE_CALLBACK:
3827 break;
3828
3829 case TYPE_ARRAY:
3830 case TYPE_NONE:
3831 case TYPE_UNDEFINED:
3832 gcc_unreachable ();
3833 }
3834}
3835
3836
3837/* Write a function to PCH walk all the fields of type S on OF.
3838 D contains data needed by walk_type to recurse into the fields of S. */
3839
3840static void
3841write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
3842{
3843 oprintf (o: d->of, format: "\nvoid\n");
3844 oprintf (o: d->of, format: "gt_pch_nx (");
3845 write_type_decl (out: d->of, ty: s);
3846 oprintf (o: d->of, format: "* x ATTRIBUTE_UNUSED,\n"
3847 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3848 "\tATTRIBUTE_UNUSED void *cookie)\n");
3849 oprintf (o: d->of, format: "{\n");
3850 d->val = "(*x)";
3851 d->indent = 2;
3852 d->process_field = write_types_local_user_process_field;
3853 walk_type (t: s, d);
3854 oprintf (o: d->of, format: "}\n");
3855}
3856
3857
3858/* Emit the user-callable functions needed to mark all the types used
3859 by the user structure S. PREFIX is the prefix to use to
3860 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
3861 chain_next option defined. D contains data needed to pass to
3862 walk_type when traversing the fields of a type.
3863
3864 For every type T referenced by S, two routines are generated: one
3865 that takes 'T *', marks the pointer and calls the second routine,
3866 which just marks the fields of T. */
3867
3868static void
3869write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
3870{
3871 gcc_assert (s->kind == TYPE_USER_STRUCT);
3872
3873 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3874 {
3875 type_p fld_type = fld->type;
3876 if (union_or_struct_p (x: fld_type))
3877 write_pch_user_walking_for_structure_body (s: fld_type, d);
3878 }
3879}
3880
3881
3882/* process_field routine for local pointer-walking. */
3883
3884static void
3885write_types_local_process_field (type_p f, const struct walk_type_data *d)
3886{
3887 gcc_assert (d->have_this_obj);
3888 switch (f->kind)
3889 {
3890 case TYPE_POINTER:
3891 case TYPE_STRUCT:
3892 case TYPE_UNION:
3893 case TYPE_LANG_STRUCT:
3894 case TYPE_STRING:
3895 oprintf (o: d->of, format: "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3896 d->prev_val[3]);
3897 if (d->in_nested_ptr)
3898 oprintf (o: d->of, format: "%*s op (&(%s), &(%s), cookie);\n",
3899 d->indent, "", d->val, d->prev_val[2]);
3900 else
3901 oprintf (o: d->of, format: "%*s op (&(%s), NULL, cookie);\n",
3902 d->indent, "", d->val);
3903 break;
3904
3905 case TYPE_USER_STRUCT:
3906 oprintf (o: d->of, format: "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3907 d->prev_val[3]);
3908 if (d->in_ptr_field)
3909 oprintf (o: d->of, format: "%*s op (&(%s), NULL, cookie);\n",
3910 d->indent, "", d->val);
3911 else
3912 oprintf (o: d->of, format: "%*s gt_pch_nx (&(%s), op, cookie);\n",
3913 d->indent, "", d->val);
3914 break;
3915
3916 case TYPE_SCALAR:
3917 break;
3918
3919 case TYPE_CALLBACK:
3920 oprintf (o: d->of, format: "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3921 d->prev_val[3]);
3922 oprintf (o: d->of, format: "%*s gt_pch_note_callback (&(%s), this_obj);\n",
3923 d->indent, "", d->val);
3924 break;
3925
3926 case TYPE_ARRAY:
3927 case TYPE_NONE:
3928 case TYPE_UNDEFINED:
3929 gcc_unreachable ();
3930 }
3931}
3932
3933
3934/* For S, a structure that's part of ORIG_S, and using parameters
3935 PARAM, write out a routine that:
3936 - Is of type gt_note_pointers
3937 - Calls PROCESS_FIELD on each field of S or its substructures.
3938*/
3939
3940static void
3941write_local_func_for_structure (const_type_p orig_s, type_p s)
3942{
3943 struct walk_type_data d;
3944
3945 /* Don't write fns for subclasses, only for the ultimate base class
3946 within an inheritance hierarchy. */
3947 if (s->u.s.base_class)
3948 return;
3949
3950 memset (s: &d, c: 0, n: sizeof (d));
3951 d.of = get_output_file_for_structure (s);
3952 d.process_field = write_types_local_process_field;
3953 d.opt = s->u.s.opt;
3954 d.line = &s->u.s.line;
3955 d.bitmap = s->u.s.bitmap;
3956 d.prev_val[0] = d.prev_val[2] = "*x";
3957 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3958 d.prev_val[3] = "x";
3959 d.val = "(*x)";
3960 d.fn_wants_lvalue = true;
3961
3962 oprintf (o: d.of, format: "\n");
3963 oprintf (o: d.of, format: "void\n");
3964 oprintf (o: d.of, format: "gt_pch_p_");
3965 output_mangled_typename (of: d.of, t: orig_s);
3966 oprintf (o: d.of, format: " (ATTRIBUTE_UNUSED void *this_obj,\n"
3967 "\tvoid *x_p,\n"
3968 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3969 "\tATTRIBUTE_UNUSED void *cookie)\n");
3970 oprintf (o: d.of, format: "{\n");
3971 oprintf (o: d.of, format: " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3972 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3973 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3974 d.indent = 2;
3975 d.have_this_obj = true;
3976
3977 if (s->kind != TYPE_USER_STRUCT)
3978 walk_type (t: s, d: &d);
3979 else
3980 {
3981 /* User structures have no fields to walk. Simply generate a
3982 call to the user-provided PCH walker. */
3983 oprintf (o: d.of, format: "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
3984 d.prev_val[3]);
3985 oprintf (o: d.of, format: "%*s gt_pch_nx (&(%s), op, cookie);\n",
3986 d.indent, "", d.val);
3987 }
3988
3989 oprintf (o: d.of, format: "}\n");
3990
3991 /* Write user-callable entry points for the PCH walking routines. */
3992 if (orig_s->kind == TYPE_USER_STRUCT)
3993 write_pch_user_walking_functions (s, d: &d);
3994
3995 for (options_p o = s->u.s.opt; o; o = o->next)
3996 if (strcmp (s1: o->name, s2: "for_user") == 0)
3997 {
3998 write_pch_user_walking_for_structure_body (s, d: &d);
3999 break;
4000 }
4001}
4002
4003/* Write out local marker routines for STRUCTURES. */
4004
4005static void
4006write_local (outf_p output_header, type_p structures)
4007{
4008 type_p s;
4009
4010 if (!output_header)
4011 return;
4012
4013 oprintf (o: output_header, format: "\n/* Local pointer-walking routines. */\n");
4014 for (s = structures; s; s = s->next)
4015 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
4016 {
4017 options_p opt;
4018
4019 if (s->u.s.line.file == NULL)
4020 continue;
4021 for (opt = s->u.s.opt; opt; opt = opt->next)
4022 if (strcmp (s1: opt->name, s2: "ptr_alias") == 0
4023 && opt->kind == OPTION_TYPE)
4024 {
4025 const_type_p const t = (const_type_p) opt->info.type;
4026 if (t->kind == TYPE_STRUCT
4027 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
4028 {
4029 oprintf (o: output_header, format: "#define gt_pch_p_");
4030 output_mangled_typename (of: output_header, t: s);
4031 oprintf (o: output_header, format: " gt_pch_p_");
4032 output_mangled_typename (of: output_header, t);
4033 oprintf (o: output_header, format: "\n");
4034 }
4035 else
4036 error_at_line (pos: &s->u.s.line,
4037 msg: "structure alias is not a structure");
4038 break;
4039 }
4040 if (opt)
4041 continue;
4042
4043 /* Declare the marker procedure only once. */
4044 oprintf (o: output_header, format: "extern void gt_pch_p_");
4045 output_mangled_typename (of: output_header, t: s);
4046 oprintf (o: output_header,
4047 format: "\n (void *, void *, gt_pointer_operator, void *);\n");
4048
4049 if (s->kind == TYPE_LANG_STRUCT)
4050 {
4051 type_p ss;
4052 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4053 write_local_func_for_structure (orig_s: s, s: ss);
4054 }
4055 else
4056 write_local_func_for_structure (orig_s: s, s);
4057 }
4058}
4059
4060/* Nonzero if S is a type for which typed GC allocators should be output. */
4061
4062#define USED_BY_TYPED_GC_P(s) \
4063 ((s->kind == TYPE_POINTER \
4064 && (s->u.p->gc_used == GC_POINTED_TO \
4065 || s->u.p->gc_used == GC_USED)) \
4066 || (union_or_struct_p (s) \
4067 && ((s)->gc_used == GC_POINTED_TO \
4068 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4069 && s->u.s.line.file != NULL) \
4070 || ((s)->gc_used == GC_USED \
4071 && !startswith (s->u.s.tag, "anonymous")) \
4072 || (s->u.s.base_class && opts_have (s->u.s.opt, "tag")))))
4073
4074
4075
4076/* Might T contain any non-pointer elements? */
4077
4078static int
4079contains_scalar_p (type_p t)
4080{
4081 switch (t->kind)
4082 {
4083 case TYPE_STRING:
4084 case TYPE_POINTER:
4085 return 0;
4086 case TYPE_ARRAY:
4087 return contains_scalar_p (t: t->u.a.p);
4088 case TYPE_USER_STRUCT:
4089 /* User-marked structures will typically contain pointers. */
4090 return 0;
4091 default:
4092 /* Could also check for structures that have no non-pointer
4093 fields, but there aren't enough of those to worry about. */
4094 return 1;
4095 }
4096}
4097
4098/* Mangle INPF and print it to F. */
4099
4100static void
4101put_mangled_filename (outf_p f, const input_file *inpf)
4102{
4103 /* The call to get_output_file_name may indirectly update fn since
4104 get_output_file_with_visibility caches its result inside, so we
4105 need the CONST_CAST. */
4106 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4107 if (!f || !name)
4108 return;
4109 for (; *name != 0; name++)
4110 if (ISALNUM (*name))
4111 oprintf (o: f, format: "%c", *name);
4112 else
4113 oprintf (o: f, format: "%c", '_');
4114}
4115
4116/* Finish off the currently-created root tables in FLP. PFX, TNAME,
4117 LASTNAME, and NAME are all strings to insert in various places in
4118 the resulting code. */
4119
4120static void
4121finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4122 const char *tname, const char *name)
4123{
4124 struct flist *fli2;
4125
4126 for (fli2 = flp; fli2; fli2 = fli2->next)
4127 if (fli2->started_p)
4128 {
4129 oprintf (o: fli2->f, format: " %s\n", lastname);
4130 oprintf (o: fli2->f, format: "};\n\n");
4131 }
4132
4133 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4134 if (fli2->started_p)
4135 {
4136 lang_bitmap bitmap = get_lang_bitmap (inpf: fli2->file);
4137 int fnum;
4138
4139 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4140 if (bitmap & 1)
4141 {
4142 oprintf (o: base_files[fnum],
4143 format: "extern const struct %s gt_%s_", tname, pfx);
4144 put_mangled_filename (f: base_files[fnum], inpf: fli2->file);
4145 oprintf (o: base_files[fnum], format: "[];\n");
4146 }
4147 }
4148
4149 {
4150 size_t fnum;
4151 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4152 oprintf (o: base_files[fnum],
4153 format: "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4154 }
4155
4156
4157 for (fli2 = flp; fli2; fli2 = fli2->next)
4158 if (fli2->started_p)
4159 {
4160 lang_bitmap bitmap = get_lang_bitmap (inpf: fli2->file);
4161 int fnum;
4162
4163 fli2->started_p = 0;
4164
4165 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4166 if (bitmap & 1)
4167 {
4168 oprintf (o: base_files[fnum], format: " gt_%s_", pfx);
4169 put_mangled_filename (f: base_files[fnum], inpf: fli2->file);
4170 oprintf (o: base_files[fnum], format: ",\n");
4171 }
4172 }
4173
4174 {
4175 size_t fnum;
4176 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4177 {
4178 oprintf (o: base_files[fnum], format: " NULL\n");
4179 oprintf (o: base_files[fnum], format: "};\n");
4180 }
4181 }
4182}
4183
4184/* Finish off the created gt_clear_caches_file_c functions. */
4185
4186static void
4187finish_cache_funcs (flist *flp)
4188{
4189 struct flist *fli2;
4190
4191 for (fli2 = flp; fli2; fli2 = fli2->next)
4192 if (fli2->started_p)
4193 {
4194 oprintf (o: fli2->f, format: "}\n\n");
4195 }
4196
4197 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4198 if (fli2->started_p)
4199 {
4200 lang_bitmap bitmap = get_lang_bitmap (inpf: fli2->file);
4201 int fnum;
4202
4203 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4204 if (bitmap & 1)
4205 {
4206 oprintf (o: base_files[fnum], format: "extern void gt_clear_caches_");
4207 put_mangled_filename (f: base_files[fnum], inpf: fli2->file);
4208 oprintf (o: base_files[fnum], format: " ();\n");
4209 }
4210 }
4211
4212 for (size_t fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4213 oprintf (o: base_files[fnum], format: "void\ngt_clear_caches ()\n{\n");
4214
4215 for (fli2 = flp; fli2; fli2 = fli2->next)
4216 if (fli2->started_p)
4217 {
4218 lang_bitmap bitmap = get_lang_bitmap (inpf: fli2->file);
4219 int fnum;
4220
4221 fli2->started_p = 0;
4222
4223 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4224 if (bitmap & 1)
4225 {
4226 oprintf (o: base_files[fnum], format: " gt_clear_caches_");
4227 put_mangled_filename (f: base_files[fnum], inpf: fli2->file);
4228 oprintf (o: base_files[fnum], format: " ();\n");
4229 }
4230 }
4231
4232 for (size_t fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4233 {
4234 oprintf (o: base_files[fnum], format: "}\n");
4235 }
4236}
4237
4238/* Write the first three fields (pointer, count and stride) for
4239 root NAME to F. V and LINE are as for write_root.
4240
4241 Return true if the entry could be written; return false on error. */
4242
4243static bool
4244start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4245{
4246 type_p ap;
4247
4248 if (!v)
4249 {
4250 error_at_line (pos: line, msg: "`%s' is too complex to be a root", name);
4251 return false;
4252 }
4253
4254 oprintf (o: f, format: " {\n");
4255 oprintf (o: f, format: " &%s,\n", name);
4256 oprintf (o: f, format: " 1");
4257
4258 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4259 if (ap->u.a.len[0])
4260 oprintf (o: f, format: " * (%s)", ap->u.a.len);
4261 else if (ap == v->type)
4262 oprintf (o: f, format: " * ARRAY_SIZE (%s)", v->name);
4263 oprintf (o: f, format: ",\n");
4264 oprintf (o: f, format: " sizeof (%s", v->name);
4265 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4266 oprintf (o: f, format: "[0]");
4267 oprintf (o: f, format: "),\n");
4268 return true;
4269}
4270
4271/* A subroutine of write_root for writing the roots for field FIELD_NAME,
4272 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4273 of the caller. */
4274
4275static void
4276write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4277 int has_length, struct fileloc *line,
4278 bool emit_pch, type_p field_type, const char *field_name)
4279{
4280 struct pair newv;
4281 /* If the field reference is relative to V, rather than to some
4282 subcomponent of V, we can mark any subarrays with a single stride.
4283 We're effectively treating the field as a global variable in its
4284 own right. */
4285 if (v && type == v->type)
4286 {
4287 newv = *v;
4288 newv.type = field_type;
4289 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4290 v = &newv;
4291 }
4292 /* Otherwise, any arrays nested in the structure are too complex to
4293 handle. */
4294 else if (field_type->kind == TYPE_ARRAY)
4295 v = NULL;
4296 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4297 has_length, line, emit_pch);
4298}
4299
4300/* Write out to F the table entry and any marker routines needed to
4301 mark NAME as TYPE. V can be one of three values:
4302
4303 - null, if NAME is too complex to represent using a single
4304 count and stride. In this case, it is an error for NAME to
4305 contain any gc-ed data.
4306
4307 - the outermost array that contains NAME, if NAME is part of an array.
4308
4309 - the C variable that contains NAME, if NAME is not part of an array.
4310
4311 LINE is the line of the C source that declares the root variable.
4312 HAS_LENGTH is nonzero iff V was a variable-length array. */
4313
4314static void
4315write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4316 struct fileloc *line, bool emit_pch)
4317{
4318 switch (type->kind)
4319 {
4320 case TYPE_STRUCT:
4321 {
4322 pair_p fld;
4323 for (fld = type->u.s.fields; fld; fld = fld->next)
4324 {
4325 int skip_p = 0;
4326 const char *desc = NULL;
4327 options_p o;
4328
4329 for (o = fld->opt; o; o = o->next)
4330 if (strcmp (s1: o->name, s2: "skip") == 0)
4331 skip_p = 1;
4332 else if (strcmp (s1: o->name, s2: "desc") == 0
4333 && o->kind == OPTION_STRING)
4334 desc = o->info.string;
4335 else if (strcmp (s1: o->name, s2: "string_length") == 0)
4336 /* See 'doc/gty.texi'. */
4337 error_at_line (pos: line,
4338 msg: "option `%s' not supported for field `%s' of global `%s'",
4339 o->name, fld->name, name);
4340 else
4341 error_at_line (pos: line,
4342 msg: "field `%s' of global `%s' has unknown option `%s'",
4343 fld->name, name, o->name);
4344
4345 if (skip_p)
4346 continue;
4347 else if (desc && fld->type->kind == TYPE_UNION)
4348 {
4349 pair_p validf = NULL;
4350 pair_p ufld;
4351
4352 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4353 {
4354 const char *tag = NULL;
4355 options_p oo;
4356 for (oo = ufld->opt; oo; oo = oo->next)
4357 if (strcmp (s1: oo->name, s2: "tag") == 0
4358 && oo->kind == OPTION_STRING)
4359 tag = oo->info.string;
4360 if (tag == NULL || strcmp (s1: tag, s2: desc) != 0)
4361 continue;
4362 if (validf != NULL)
4363 error_at_line (pos: line,
4364 msg: "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4365 name, fld->name, validf->name,
4366 name, fld->name, ufld->name, tag);
4367 validf = ufld;
4368 }
4369 if (validf != NULL)
4370 write_field_root (f, v, type, name, has_length: 0, line, emit_pch,
4371 field_type: validf->type,
4372 ACONCAT ((fld->name, ".",
4373 validf->name, NULL)));
4374 }
4375 else if (desc)
4376 error_at_line (pos: line,
4377 msg: "global `%s.%s' has `desc' option but is not union",
4378 name, fld->name);
4379 else
4380 write_field_root (f, v, type, name, has_length: 0, line, emit_pch, field_type: fld->type,
4381 field_name: fld->name);
4382 }
4383 }
4384 break;
4385
4386 case TYPE_ARRAY:
4387 {
4388 char *newname;
4389 newname = xasprintf ("%s[0]", name);
4390 write_root (f, v, type: type->u.a.p, name: newname, has_length, line, emit_pch);
4391 free (ptr: newname);
4392 }
4393 break;
4394
4395 case TYPE_USER_STRUCT:
4396 error_at_line (pos: line, msg: "`%s' must be a pointer type, because it is "
4397 "a GC root and its type is marked with GTY((user))",
4398 v->name);
4399 break;
4400
4401 case TYPE_POINTER:
4402 {
4403 const_type_p tp;
4404
4405 if (!start_root_entry (f, v, name, line))
4406 return;
4407
4408 tp = type->u.p;
4409
4410 if (!has_length && union_or_struct_p (x: tp))
4411 {
4412 tp = get_ultimate_base_class (s: tp);
4413 const char *id_for_tag = filter_type_name (type_name: tp->u.s.tag);
4414 oprintf (o: f, format: " &gt_ggc_mx_%s,\n", id_for_tag);
4415 if (emit_pch)
4416 oprintf (o: f, format: " &gt_pch_nx_%s", id_for_tag);
4417 else
4418 oprintf (o: f, format: " NULL");
4419 if (id_for_tag != tp->u.s.tag)
4420 free (CONST_CAST (char *, id_for_tag));
4421 }
4422 else if (has_length
4423 && (tp->kind == TYPE_POINTER || union_or_struct_p (x: tp)))
4424 {
4425 oprintf (o: f, format: " &gt_ggc_ma_%s,\n", name);
4426 if (emit_pch)
4427 oprintf (o: f, format: " &gt_pch_na_%s", name);
4428 else
4429 oprintf (o: f, format: " NULL");
4430 }
4431 else
4432 {
4433 error_at_line (pos: line,
4434 msg: "global `%s' is pointer to unimplemented type",
4435 name);
4436 }
4437 oprintf (o: f, format: "\n },\n");
4438 }
4439 break;
4440
4441 case TYPE_STRING:
4442 {
4443 if (!start_root_entry (f, v, name, line))
4444 return;
4445
4446 oprintf (o: f, format: " (gt_pointer_walker) &gt_ggc_m_S,\n");
4447 oprintf (o: f, format: " (gt_pointer_walker) &gt_pch_n_S\n");
4448 oprintf (o: f, format: " },\n");
4449 }
4450 break;
4451
4452 case TYPE_SCALAR:
4453 break;
4454
4455 case TYPE_NONE:
4456 case TYPE_UNDEFINED:
4457 case TYPE_UNION:
4458 case TYPE_LANG_STRUCT:
4459 case TYPE_CALLBACK:
4460 error_at_line (pos: line, msg: "global `%s' is unimplemented type", name);
4461 }
4462}
4463
4464/* This generates a routine to walk an array. */
4465
4466static void
4467write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4468{
4469 struct walk_type_data d;
4470 char *prevval3;
4471
4472 memset (s: &d, c: 0, n: sizeof (d));
4473 d.of = f;
4474 d.cookie = wtd;
4475 d.indent = 2;
4476 d.line = &v->line;
4477 d.opt = v->opt;
4478 d.bitmap = get_lang_bitmap (inpf: v->line.file);
4479
4480 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4481
4482 if (wtd->param_prefix)
4483 {
4484 oprintf (o: f, format: "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4485 oprintf (o: f, format: " (void *, void *, gt_pointer_operator, void *);\n");
4486 oprintf (o: f, format: "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4487 wtd->param_prefix, v->name);
4488 oprintf (o: d.of,
4489 format: " ATTRIBUTE_UNUSED void *x_p,\n"
4490 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4491 " ATTRIBUTE_UNUSED void * cookie)\n");
4492 oprintf (o: d.of, format: "{\n");
4493 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4494 d.process_field = write_types_local_process_field;
4495 d.have_this_obj = true;
4496 walk_type (t: v->type, d: &d);
4497 oprintf (o: f, format: "}\n\n");
4498 }
4499
4500 d.opt = v->opt;
4501 oprintf (o: f, format: "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4502 oprintf (o: f, format: "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4503 wtd->prefix, v->name);
4504 oprintf (o: f, format: "{\n");
4505 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4506 d.process_field = write_types_process_field;
4507 d.have_this_obj = false;
4508 walk_type (t: v->type, d: &d);
4509 free (ptr: prevval3);
4510 oprintf (o: f, format: "}\n\n");
4511}
4512
4513/* Output a table describing the locations and types of VARIABLES. */
4514
4515static void
4516write_roots (pair_p variables, bool emit_pch)
4517{
4518 pair_p v;
4519 struct flist *flp = NULL;
4520
4521 for (v = variables; v; v = v->next)
4522 {
4523 outf_p f =
4524 get_output_file_with_visibility (CONST_CAST (input_file*,
4525 v->line.file));
4526 struct flist *fli;
4527 const char *length = NULL;
4528 int deletable_p = 0;
4529 options_p o;
4530 for (o = v->opt; o; o = o->next)
4531 if (strcmp (s1: o->name, s2: "length") == 0
4532 && o->kind == OPTION_STRING)
4533 length = o->info.string;
4534 else if (strcmp (s1: o->name, s2: "deletable") == 0)
4535 deletable_p = 1;
4536 else if (strcmp (s1: o->name, s2: "cache") == 0)
4537 ;
4538 else if (strcmp (s1: o->name, s2: "string_length") == 0)
4539 /* See 'doc/gty.texi'. */
4540 error_at_line (pos: &v->line,
4541 msg: "option `%s' not supported for global `%s'",
4542 o->name, v->name);
4543 else
4544 error_at_line (pos: &v->line,
4545 msg: "global `%s' has unknown option `%s'",
4546 v->name, o->name);
4547
4548 for (fli = flp; fli; fli = fli->next)
4549 if (fli->f == f && f)
4550 break;
4551 if (fli == NULL)
4552 {
4553 fli = XNEW (struct flist);
4554 fli->f = f;
4555 fli->next = flp;
4556 fli->started_p = 0;
4557 fli->file = v->line.file;
4558 gcc_assert (fli->file);
4559 flp = fli;
4560
4561 oprintf (o: f, format: "\n/* GC roots. */\n\n");
4562 }
4563
4564 if (!deletable_p
4565 && length
4566 && v->type->kind == TYPE_POINTER
4567 && (v->type->u.p->kind == TYPE_POINTER
4568 || v->type->u.p->kind == TYPE_STRUCT))
4569 {
4570 write_array (f, v, wtd: &ggc_wtd);
4571 write_array (f, v, wtd: &pch_wtd);
4572 }
4573 }
4574
4575 for (v = variables; v; v = v->next)
4576 {
4577 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4578 v->line.file));
4579 struct flist *fli;
4580 int skip_p = 0;
4581 int length_p = 0;
4582 options_p o;
4583
4584 for (o = v->opt; o; o = o->next)
4585 if (strcmp (s1: o->name, s2: "length") == 0)
4586 length_p = 1;
4587 else if (strcmp (s1: o->name, s2: "deletable") == 0)
4588 skip_p = 1;
4589
4590 if (skip_p)
4591 continue;
4592
4593 for (fli = flp; fli; fli = fli->next)
4594 if (fli->f == f)
4595 break;
4596 if (!fli->started_p)
4597 {
4598 fli->started_p = 1;
4599
4600 oprintf (o: f, format: "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4601 put_mangled_filename (f, inpf: v->line.file);
4602 oprintf (o: f, format: "[] = {\n");
4603 }
4604
4605 write_root (f, v, type: v->type, name: v->name, has_length: length_p, line: &v->line, emit_pch);
4606 }
4607
4608 finish_root_table (flp, pfx: "ggc_r", lastname: "LAST_GGC_ROOT_TAB", tname: "ggc_root_tab",
4609 name: "gt_ggc_rtab");
4610
4611 for (v = variables; v; v = v->next)
4612 {
4613 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4614 v->line.file));
4615 struct flist *fli;
4616 int skip_p = 1;
4617 options_p o;
4618
4619 for (o = v->opt; o; o = o->next)
4620 if (strcmp (s1: o->name, s2: "deletable") == 0)
4621 skip_p = 0;
4622
4623 if (skip_p)
4624 continue;
4625
4626 for (fli = flp; fli; fli = fli->next)
4627 if (fli->f == f)
4628 break;
4629 if (!fli->started_p)
4630 {
4631 fli->started_p = 1;
4632
4633 oprintf (o: f, format: "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4634 put_mangled_filename (f, inpf: v->line.file);
4635 oprintf (o: f, format: "[] = {\n");
4636 }
4637
4638 oprintf (o: f, format: " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4639 v->name, v->name);
4640 }
4641
4642 finish_root_table (flp, pfx: "ggc_rd", lastname: "LAST_GGC_ROOT_TAB", tname: "ggc_root_tab",
4643 name: "gt_ggc_deletable_rtab");
4644
4645 for (v = variables; v; v = v->next)
4646 {
4647 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4648 v->line.file));
4649 struct flist *fli;
4650 bool cache = false;
4651 options_p o;
4652
4653 for (o = v->opt; o; o = o->next)
4654 if (strcmp (s1: o->name, s2: "cache") == 0)
4655 cache = true;
4656 if (!cache)
4657 continue;
4658
4659 for (fli = flp; fli; fli = fli->next)
4660 if (fli->f == f)
4661 break;
4662 if (!fli->started_p)
4663 {
4664 fli->started_p = 1;
4665
4666 oprintf (o: f, format: "void\ngt_clear_caches_");
4667 put_mangled_filename (f, inpf: v->line.file);
4668 oprintf (o: f, format: " ()\n{\n");
4669 }
4670
4671 oprintf (o: f, format: " gt_cleare_cache (%s);\n", v->name);
4672 }
4673
4674 finish_cache_funcs (flp);
4675
4676 if (!emit_pch)
4677 return;
4678
4679 for (v = variables; v; v = v->next)
4680 {
4681 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4682 v->line.file));
4683 struct flist *fli;
4684 int skip_p = 0;
4685 options_p o;
4686
4687 for (o = v->opt; o; o = o->next)
4688 if (strcmp (s1: o->name, s2: "deletable") == 0)
4689 {
4690 skip_p = 1;
4691 break;
4692 }
4693
4694 if (skip_p)
4695 continue;
4696
4697 if (!contains_scalar_p (t: v->type))
4698 continue;
4699
4700 for (fli = flp; fli; fli = fli->next)
4701 if (fli->f == f)
4702 break;
4703 if (!fli->started_p)
4704 {
4705 fli->started_p = 1;
4706
4707 oprintf (o: f, format: "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4708 put_mangled_filename (f, inpf: v->line.file);
4709 oprintf (o: f, format: "[] = {\n");
4710 }
4711
4712 oprintf (o: f, format: " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4713 v->name, v->name);
4714 }
4715
4716 finish_root_table (flp, pfx: "pch_rs", lastname: "LAST_GGC_ROOT_TAB", tname: "ggc_root_tab",
4717 name: "gt_pch_scalar_rtab");
4718}
4719
4720/* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4721 guaranteee for somewhat increased readability. If name conflicts do happen,
4722 this funcion will have to be adjusted to be more like
4723 output_mangled_typename. */
4724
4725#define INDENT 2
4726
4727/* Dumps the value of typekind KIND. */
4728
4729static void
4730dump_typekind (int indent, enum typekind kind)
4731{
4732 printf (format: "%*ckind = ", indent, ' ');
4733 switch (kind)
4734 {
4735 case TYPE_SCALAR:
4736 printf (format: "TYPE_SCALAR");
4737 break;
4738 case TYPE_STRING:
4739 printf (format: "TYPE_STRING");
4740 break;
4741 case TYPE_STRUCT:
4742 printf (format: "TYPE_STRUCT");
4743 break;
4744 case TYPE_UNDEFINED:
4745 printf (format: "TYPE_UNDEFINED");
4746 break;
4747 case TYPE_USER_STRUCT:
4748 printf (format: "TYPE_USER_STRUCT");
4749 break;
4750 case TYPE_UNION:
4751 printf (format: "TYPE_UNION");
4752 break;
4753 case TYPE_POINTER:
4754 printf (format: "TYPE_POINTER");
4755 break;
4756 case TYPE_ARRAY:
4757 printf (format: "TYPE_ARRAY");
4758 break;
4759 case TYPE_CALLBACK:
4760 printf (format: "TYPE_CALLBACK");
4761 break;
4762 case TYPE_LANG_STRUCT:
4763 printf (format: "TYPE_LANG_STRUCT");
4764 break;
4765 default:
4766 gcc_unreachable ();
4767 }
4768 printf (format: "\n");
4769}
4770
4771/* Dumps the value of GC_USED flag. */
4772
4773static void
4774dump_gc_used (int indent, enum gc_used_enum gc_used)
4775{
4776 printf (format: "%*cgc_used = ", indent, ' ');
4777 switch (gc_used)
4778 {
4779 case GC_UNUSED:
4780 printf (format: "GC_UNUSED");
4781 break;
4782 case GC_USED:
4783 printf (format: "GC_USED");
4784 break;
4785 case GC_MAYBE_POINTED_TO:
4786 printf (format: "GC_MAYBE_POINTED_TO");
4787 break;
4788 case GC_POINTED_TO:
4789 printf (format: "GC_POINTED_TO");
4790 break;
4791 default:
4792 gcc_unreachable ();
4793 }
4794 printf (format: "\n");
4795}
4796
4797/* Dumps the type options OPT. */
4798
4799static void
4800dump_options (int indent, options_p opt)
4801{
4802 options_p o;
4803 printf (format: "%*coptions = ", indent, ' ');
4804 o = opt;
4805 while (o)
4806 {
4807 switch (o->kind)
4808 {
4809 case OPTION_STRING:
4810 printf (format: "%s:string %s ", o->name, o->info.string);
4811 break;
4812 case OPTION_TYPE:
4813 printf (format: "%s:type ", o->name);
4814 dump_type (indent: indent+1, p: o->info.type);
4815 break;
4816 case OPTION_NESTED:
4817 printf (format: "%s:nested ", o->name);
4818 break;
4819 case OPTION_NONE:
4820 gcc_unreachable ();
4821 }
4822 o = o->next;
4823 }
4824 printf (format: "\n");
4825}
4826
4827/* Dumps the source file location in LINE. */
4828
4829static void
4830dump_fileloc (int indent, struct fileloc line)
4831{
4832 printf (format: "%*cfileloc: file = %s, line = %d\n", indent, ' ',
4833 get_input_file_name (inpf: line.file),
4834 line.line);
4835}
4836
4837/* Recursively dumps the struct, union, or a language-specific
4838 struct T. */
4839
4840static void
4841dump_type_u_s (int indent, type_p t)
4842{
4843 pair_p fields;
4844
4845 gcc_assert (union_or_struct_p (t));
4846 printf (format: "%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
4847 dump_fileloc (indent, line: t->u.s.line);
4848 printf (format: "%*cu.s.fields =\n", indent, ' ');
4849 fields = t->u.s.fields;
4850 while (fields)
4851 {
4852 dump_pair (indent: indent + INDENT, p: fields);
4853 fields = fields->next;
4854 }
4855 printf (format: "%*cend of fields of type %p\n", indent, ' ', (void *) t);
4856 dump_options (indent, opt: t->u.s.opt);
4857 printf (format: "%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
4858 if (t->kind == TYPE_LANG_STRUCT)
4859 {
4860 printf (format: "%*cu.s.lang_struct:\n", indent, ' ');
4861 dump_type_list (indent: indent + INDENT, p: t->u.s.lang_struct);
4862 }
4863}
4864
4865/* Recursively dumps the array T. */
4866
4867static void
4868dump_type_u_a (int indent, type_p t)
4869{
4870 gcc_assert (t->kind == TYPE_ARRAY);
4871 printf (format: "%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
4872 dump_type_list (indent: indent + INDENT, p: t->u.a.p);
4873}
4874
4875/* Recursively dumps the type list T. */
4876
4877static void
4878dump_type_list (int indent, type_p t)
4879{
4880 type_p p = t;
4881 while (p)
4882 {
4883 dump_type (indent, p);
4884 p = p->next;
4885 }
4886}
4887
4888static htab_t seen_types;
4889
4890/* Recursively dumps the type T if it was not dumped previously. */
4891
4892static void
4893dump_type (int indent, type_p t)
4894{
4895 void **slot;
4896
4897 printf (format: "%*cType at %p: ", indent, ' ', (void *) t);
4898 if (t->kind == TYPE_UNDEFINED)
4899 {
4900 gcc_assert (t->gc_used == GC_UNUSED);
4901 printf (format: "undefined.\n");
4902 return;
4903 }
4904
4905 if (seen_types == NULL)
4906 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
4907
4908 slot = htab_find_slot (seen_types, t, INSERT);
4909 if (*slot != NULL)
4910 {
4911 printf (format: "already seen.\n");
4912 return;
4913 }
4914 *slot = t;
4915 printf (format: "\n");
4916
4917 dump_typekind (indent, kind: t->kind);
4918 printf (format: "%*cpointer_to = %p\n", indent + INDENT, ' ',
4919 (void *) t->pointer_to);
4920 dump_gc_used (indent: indent + INDENT, gc_used: t->gc_used);
4921 switch (t->kind)
4922 {
4923 case TYPE_SCALAR:
4924 printf (format: "%*cscalar_is_char = %s\n", indent + INDENT, ' ',
4925 t->u.scalar_is_char ? "true" : "false");
4926 break;
4927 case TYPE_STRING:
4928 case TYPE_CALLBACK:
4929 break;
4930 case TYPE_STRUCT:
4931 case TYPE_UNION:
4932 case TYPE_LANG_STRUCT:
4933 case TYPE_USER_STRUCT:
4934 dump_type_u_s (indent: indent + INDENT, t);
4935 break;
4936 case TYPE_POINTER:
4937 printf (format: "%*cp:\n", indent + INDENT, ' ');
4938 dump_type (indent: indent + INDENT, t: t->u.p);
4939 break;
4940 case TYPE_ARRAY:
4941 dump_type_u_a (indent: indent + INDENT, t);
4942 break;
4943 default:
4944 gcc_unreachable ();
4945 }
4946 printf (format: "%*cEnd of type at %p\n", indent, ' ', (void *) t);
4947}
4948
4949/* Dumps the pair P. */
4950
4951static void
4952dump_pair (int indent, pair_p p)
4953{
4954 printf (format: "%*cpair: name = %s\n", indent, ' ', p->name);
4955 dump_type (indent, t: p->type);
4956 dump_fileloc (indent, line: p->line);
4957 dump_options (indent, opt: p->opt);
4958 printf (format: "%*cEnd of pair %s\n", indent, ' ', p->name);
4959}
4960
4961/* Dumps the list of pairs PP. */
4962
4963static void
4964dump_pair_list (const char *name, pair_p pp)
4965{
4966 pair_p p;
4967 printf (format: "%s:\n", name);
4968 for (p = pp; p != NULL; p = p->next)
4969 dump_pair (indent: 0, p);
4970 printf (format: "End of %s\n\n", name);
4971}
4972
4973/* Dumps the STRUCTURES. */
4974
4975static void
4976dump_structures (const char *name, type_p structures)
4977{
4978 printf (format: "%s:\n", name);
4979 dump_type_list (indent: 0, t: structures);
4980 printf (format: "End of %s\n\n", name);
4981}
4982
4983/* Dumps the internal structures of gengtype. This is useful to debug
4984 gengtype itself, or to understand what it does, e.g. for plugin
4985 developers. */
4986
4987static void
4988dump_everything (void)
4989{
4990 dump_pair_list (name: "typedefs", pp: typedefs);
4991 dump_structures (name: "structures", structures);
4992 dump_pair_list (name: "variables", pp: variables);
4993
4994 /* Allocated with the first call to dump_type. */
4995 htab_delete (seen_types);
4996}
4997
4998
4999
5000/* Option specification for getopt_long. */
5001static const struct option gengtype_long_options[] = {
5002 {.name: "help", no_argument, NULL, .val: 'h'},
5003 {.name: "version", no_argument, NULL, .val: 'V'},
5004 {.name: "verbose", no_argument, NULL, .val: 'v'},
5005 {.name: "dump", no_argument, NULL, .val: 'd'},
5006 {.name: "debug", no_argument, NULL, .val: 'D'},
5007 {.name: "plugin", required_argument, NULL, .val: 'P'},
5008 {.name: "srcdir", required_argument, NULL, .val: 'S'},
5009 {.name: "backupdir", required_argument, NULL, .val: 'B'},
5010 {.name: "inputs", required_argument, NULL, .val: 'I'},
5011 {.name: "read-state", required_argument, NULL, .val: 'r'},
5012 {.name: "write-state", required_argument, NULL, .val: 'w'},
5013 /* Terminating NULL placeholder. */
5014 {NULL, no_argument, NULL, .val: 0},
5015};
5016
5017
5018static void
5019print_usage (void)
5020{
5021 printf (format: "Usage: %s\n", progname);
5022 printf (format: "\t -h | --help " " \t# Give this help.\n");
5023 printf (format: "\t -D | --debug "
5024 " \t# Give debug output to debug %s itself.\n", progname);
5025 printf (format: "\t -V | --version " " \t# Give version information.\n");
5026 printf (format: "\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5027 printf (format: "\t -d | --dump " " \t# Dump state for debugging.\n");
5028 printf (format: "\t -P | --plugin <output-file> <plugin-src> ... "
5029 " \t# Generate for plugin.\n");
5030 printf (format: "\t -S | --srcdir <GCC-directory> "
5031 " \t# Specify the GCC source directory.\n");
5032 printf (format: "\t -B | --backupdir <directory> "
5033 " \t# Specify the backup directory for updated files.\n");
5034 printf (format: "\t -I | --inputs <input-list> "
5035 " \t# Specify the file with source files list.\n");
5036 printf (format: "\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5037 printf (format: "\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5038}
5039
5040static void
5041print_version (void)
5042{
5043 printf (format: "%s %s%s\n", progname, pkgversion_string, version_string);
5044 printf (format: "Report bugs: %s\n", bug_report_url);
5045}
5046
5047/* Parse the program options using getopt_long... */
5048static void
5049parse_program_options (int argc, char **argv)
5050{
5051 int opt = -1;
5052 while ((opt = getopt_long (argc, argv, shortopts: "hVvdP:S:B:I:w:r:D",
5053 longopts: gengtype_long_options, NULL)) >= 0)
5054 {
5055 switch (opt)
5056 {
5057 case 'h': /* --help */
5058 print_usage ();
5059 break;
5060 case 'V': /* --version */
5061 print_version ();
5062 break;
5063 case 'd': /* --dump */
5064 do_dump = 1;
5065 break;
5066 case 'D': /* --debug */
5067 do_debug = 1;
5068 break;
5069 case 'v': /* --verbose */
5070 verbosity_level++;
5071 break;
5072 case 'P': /* --plugin */
5073 if (optarg)
5074 plugin_output_filename = optarg;
5075 else
5076 fatal ("missing plugin output file name");
5077 break;
5078 case 'S': /* --srcdir */
5079 if (optarg)
5080 srcdir = optarg;
5081 else
5082 fatal ("missing source directory");
5083 srcdir_len = strlen (s: srcdir);
5084 break;
5085 case 'B': /* --backupdir */
5086 if (optarg)
5087 backup_dir = optarg;
5088 else
5089 fatal ("missing backup directory");
5090 break;
5091 case 'I': /* --inputs */
5092 if (optarg)
5093 inputlist = optarg;
5094 else
5095 fatal ("missing input list");
5096 break;
5097 case 'r': /* --read-state */
5098 if (optarg)
5099 read_state_filename = optarg;
5100 else
5101 fatal ("missing read state file");
5102 DBGPRINTF ("read state %s\n", optarg);
5103 break;
5104 case 'w': /* --write-state */
5105 DBGPRINTF ("write state %s\n", optarg);
5106 if (optarg)
5107 write_state_filename = optarg;
5108 else
5109 fatal ("missing write state file");
5110 break;
5111 default:
5112 fprintf (stderr, format: "%s: unknown flag '%c'\n", progname, opt);
5113 print_usage ();
5114 fatal ("unexpected flag");
5115 }
5116 };
5117 if (plugin_output_filename)
5118 {
5119 /* In plugin mode we require some input files. */
5120 int i = 0;
5121 if (optind >= argc)
5122 fatal ("no source files given in plugin mode");
5123 nb_plugin_files = argc - optind;
5124 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5125 for (i = 0; i < (int) nb_plugin_files; i++)
5126 {
5127 char *name = argv[i + optind];
5128 plugin_files[i] = input_file_by_name (name);
5129 }
5130 }
5131}
5132
5133
5134
5135/******* Manage input files. ******/
5136
5137/* Hash table of unique input file names. */
5138static htab_t input_file_htab;
5139
5140/* Find or allocate a new input_file by hash-consing it. */
5141input_file*
5142input_file_by_name (const char* name)
5143{
5144 void ** slot;
5145 input_file* f = NULL;
5146 int namlen = 0;
5147 if (!name)
5148 return NULL;
5149 namlen = strlen (s: name);
5150 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5151 f->inpbitmap = 0;
5152 f->inpoutf = NULL;
5153 f->inpisplugin = false;
5154 strcpy (dest: f->inpname, src: name);
5155 slot = htab_find_slot (input_file_htab, f, INSERT);
5156 gcc_assert (slot != NULL);
5157 if (*slot)
5158 {
5159 /* Already known input file. */
5160 free (ptr: f);
5161 return (input_file*)(*slot);
5162 }
5163 /* New input file. */
5164 *slot = f;
5165 return f;
5166 }
5167
5168/* Hash table support routines for input_file-s. */
5169static hashval_t
5170htab_hash_inputfile (const void *p)
5171{
5172 const input_file *inpf = (const input_file *) p;
5173 gcc_assert (inpf);
5174 return htab_hash_string (get_input_file_name (inpf));
5175}
5176
5177static int
5178htab_eq_inputfile (const void *x, const void *y)
5179{
5180 const input_file *inpfx = (const input_file *) x;
5181 const input_file *inpfy = (const input_file *) y;
5182 gcc_assert (inpfx != NULL && inpfy != NULL);
5183 return !filename_cmp (s1: get_input_file_name (inpf: inpfx), s2: get_input_file_name (inpf: inpfy));
5184}
5185
5186
5187int
5188main (int argc, char **argv)
5189{
5190 size_t i;
5191 static struct fileloc pos = { NULL, .line: 0 };
5192 outf_p output_header;
5193
5194 /* Mandatory common initializations. */
5195 progname = "gengtype"; /* For fatal and messages. */
5196 /* Create the hash-table used to hash-cons input files. */
5197 input_file_htab =
5198 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5199 /* Initialize our special input files. */
5200 this_file = input_file_by_name (__FILE__);
5201 system_h_file = input_file_by_name (name: "system.h");
5202 /* Set the scalar_is_char union number for predefined scalar types. */
5203 scalar_nonchar.u.scalar_is_char = false;
5204 scalar_char.u.scalar_is_char = true;
5205
5206 parse_program_options (argc, argv);
5207
5208 if (do_debug)
5209 {
5210 time_t now = (time_t) 0;
5211 time (timer: &now);
5212 DBGPRINTF ("gengtype started pid %d at %s",
5213 (int) getpid (), ctime (&now));
5214 }
5215
5216 /* Parse the input list and the input files. */
5217 DBGPRINTF ("inputlist %s", inputlist);
5218 if (read_state_filename)
5219 {
5220 if (inputlist)
5221 fatal ("input list %s cannot be given with a read state file %s",
5222 inputlist, read_state_filename);
5223 read_state (path: read_state_filename);
5224 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5225 }
5226 else if (inputlist)
5227 {
5228 /* These types are set up with #define or else outside of where
5229 we can see them. We should initialize them before calling
5230 read_input_list. */
5231#define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5232 Call;} while (0)
5233 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5234 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5235 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5236 POS_HERE (do_scalar_typedef ("double_int", &pos));
5237 POS_HERE (do_scalar_typedef ("offset_int", &pos));
5238 POS_HERE (do_scalar_typedef ("int64_t", &pos));
5239 POS_HERE (do_scalar_typedef ("poly_int64", &pos));
5240 POS_HERE (do_scalar_typedef ("poly_uint64", &pos));
5241 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5242 POS_HERE (do_scalar_typedef ("uint32_t", &pos));
5243 POS_HERE (do_scalar_typedef ("uint8", &pos));
5244 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5245 POS_HERE (do_scalar_typedef ("jword", &pos));
5246 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5247 POS_HERE (do_scalar_typedef ("void", &pos));
5248 POS_HERE (do_scalar_typedef ("machine_mode", &pos));
5249 POS_HERE (do_scalar_typedef ("fixed_size_mode", &pos));
5250 POS_HERE (do_scalar_typedef ("CONSTEXPR", &pos));
5251 POS_HERE (do_typedef ("void *",
5252 create_pointer (resolve_typedef ("void", &pos)),
5253 &pos));
5254#undef POS_HERE
5255 read_input_list (listname: inputlist);
5256 num_build_headers = 0;
5257 for (i = 0; i < num_gt_files; i++)
5258 {
5259 const char *fname = get_input_file_name (inpf: gt_files[i]);
5260 parse_file (name: fname);
5261 DBGPRINTF ("parsed file #%d %s", (int) i, fname);
5262 /* Check if this is a header file generated during the build. */
5263 int len = strlen (s: fname);
5264 if (len >= 5
5265 && fname[0] == '.'
5266 && IS_DIR_SEPARATOR (fname[1])
5267 && fname[len-2] == '.'
5268 && fname[len-1] == 'h')
5269 num_build_headers++;
5270 }
5271 if (verbosity_level >= 1)
5272 printf (format: "%s parsed %d files with %d GTY types\n",
5273 progname, (int) num_gt_files, type_count);
5274
5275 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5276 }
5277 else
5278 fatal ("either an input list or a read state file should be given");
5279 if (hit_error)
5280 return 1;
5281
5282
5283 if (plugin_output_filename)
5284 {
5285 size_t ix = 0;
5286 /* In plugin mode, we should have read a state file, and have
5287 given at least one plugin file. */
5288 if (!read_state_filename)
5289 fatal ("No read state given in plugin mode for %s",
5290 plugin_output_filename);
5291
5292 if (nb_plugin_files == 0 || !plugin_files)
5293 fatal ("No plugin files given in plugin mode for %s",
5294 plugin_output_filename);
5295
5296 /* Parse our plugin files and augment the state. */
5297 for (ix = 0; ix < nb_plugin_files; ix++)
5298 {
5299 input_file* pluginput = plugin_files [ix];
5300 pluginput->inpisplugin = true;
5301 parse_file (name: get_input_file_name (inpf: pluginput));
5302 }
5303 if (hit_error)
5304 return 1;
5305
5306 plugin_output = create_file (name: "GCC", oname: plugin_output_filename);
5307 DBGPRINTF ("created plugin_output %p named %s",
5308 (void *) plugin_output, plugin_output->name);
5309 }
5310 else
5311 { /* No plugin files, we are in normal mode. */
5312 if (!srcdir)
5313 fatal ("gengtype needs a source directory in normal mode");
5314 }
5315 if (hit_error)
5316 return 1;
5317
5318 gen_rtx_next ();
5319
5320 set_gc_used (variables);
5321
5322 for (type_p t = structures; t; t = t->next)
5323 {
5324 bool for_user = false;
5325 for (options_p o = t->u.s.opt; o; o = o->next)
5326 if (strcmp (s1: o->name, s2: "for_user") == 0)
5327 {
5328 for_user = true;
5329 break;
5330 }
5331
5332 if (for_user)
5333 set_gc_used_type (t, level: GC_POINTED_TO);
5334 }
5335 /* The state at this point is read from the state input file or by
5336 parsing source files and optionally augmented by parsing plugin
5337 source files. Write it now. */
5338 if (write_state_filename)
5339 {
5340 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5341
5342 if (hit_error)
5343 fatal ("didn't write state file %s after errors",
5344 write_state_filename);
5345
5346 DBGPRINTF ("before write_state %s", write_state_filename);
5347 write_state (path: write_state_filename);
5348
5349 if (do_dump)
5350 dump_everything ();
5351
5352 /* After having written the state file we return immediately to
5353 avoid generating any output file. */
5354 if (hit_error)
5355 return 1;
5356 else
5357 return 0;
5358 }
5359
5360
5361 open_base_files ();
5362
5363 output_header = plugin_output ? plugin_output : header_file;
5364 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5365 structures);
5366
5367 write_types (output_header, structures, wtd: &ggc_wtd);
5368 if (plugin_files == NULL)
5369 {
5370 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5371 structures);
5372 write_types (output_header: header_file, structures, wtd: &pch_wtd);
5373 write_local (output_header: header_file, structures);
5374 }
5375 write_roots (variables, emit_pch: plugin_files == NULL);
5376 write_rtx_next ();
5377 close_output_files ();
5378
5379 if (do_dump)
5380 dump_everything ();
5381
5382 /* Don't bother about free-ing any input or plugin file, etc. */
5383
5384 if (hit_error)
5385 return 1;
5386 return 0;
5387}
5388

source code of gcc/gengtype.cc