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