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