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