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