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