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