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