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