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