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