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