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