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