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