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