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