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