1/* Gengtype persistent state serialization & de-serialization.
2 Useful for gengtype in plugin mode.
3
4 Copyright (C) 2010-2023 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>.
21
22 Contributed by Jeremie Salvucci <jeremie.salvucci@free.fr>
23 and Basile Starynkevitch <basile@starynkevitch.net>
24*/
25
26#ifdef HOST_GENERATOR_FILE
27#include "config.h"
28#define GENERATOR_FILE 1
29#else
30#include "bconfig.h"
31#endif
32#include "system.h"
33#include "errors.h" /* For fatal. */
34#include "version.h" /* For version_string & pkgversion_string. */
35#include "obstack.h"
36#include "gengtype.h"
37
38
39
40/* Gives the file location of a type, if any. */
41static inline struct fileloc*
42type_lineloc (const_type_p ty)
43{
44 if (!ty)
45 return NULL;
46 switch (ty->kind)
47 {
48 case TYPE_NONE:
49 gcc_unreachable ();
50 case TYPE_STRUCT:
51 case TYPE_UNION:
52 case TYPE_LANG_STRUCT:
53 case TYPE_USER_STRUCT:
54 case TYPE_UNDEFINED:
55 return CONST_CAST (struct fileloc*, &ty->u.s.line);
56 case TYPE_SCALAR:
57 case TYPE_STRING:
58 case TYPE_POINTER:
59 case TYPE_ARRAY:
60 case TYPE_CALLBACK:
61 return NULL;
62 default:
63 gcc_unreachable ();
64 }
65}
66
67/* The state file has simplistic lispy lexical tokens. Its lexer gives
68 a linked list of struct state_token_st, through the peek_state_token
69 function. Lexical tokens are consumed with next_state_tokens. */
70
71
72/* The lexical kind of each lispy token. */
73enum state_token_en
74{
75 STOK_NONE, /* Never used. */
76 STOK_INTEGER, /* Integer token. */
77 STOK_STRING, /* String token. */
78 STOK_LEFTPAR, /* Left opening parenthesis. */
79 STOK_RIGHTPAR, /* Right closing parenthesis. */
80 STOK_NAME /* hash-consed name or identifier. */
81};
82
83
84/* Structure and hash-table used to share identifiers or names. */
85struct state_ident_st
86{
87 /* TODO: We could improve the parser by reserving identifiers for
88 state keywords and adding a keyword number for them. That would
89 mean adding another field in this state_ident_st struct. */
90 char stid_name[1]; /* actually bigger & null terminated */
91};
92static htab_t state_ident_tab;
93
94
95/* The state_token_st structure is for lexical tokens in the read
96 state file. The stok_kind field discriminates the union. Tokens
97 are allocated by peek_state_token which calls read_a_state_token
98 which allocate them. Tokens are freed by calls to
99 next_state_tokens. Token are organized in a FIFO look-ahead queue
100 filled by peek_state_token. */
101struct state_token_st
102{
103 enum state_token_en stok_kind; /* the lexical kind
104 discriminates the stok_un
105 union */
106 int stok_line; /* the line number */
107 int stok_col; /* the column number */
108 const char *stok_file; /* the file path */
109 struct state_token_st *stok_next; /* the next token in the
110 queue, when peeked */
111 union /* discriminated by stok_kind! */
112 {
113 int stok_num; /* when STOK_INTEGER */
114 char stok_string[1]; /* when STOK_STRING, actual size is
115 bigger and null terminated */
116 struct state_ident_st *stok_ident; /* when STOK_IDENT */
117 void *stok_ptr; /* null otherwise */
118 }
119 stok_un;
120};
121
122
123
124
125#define NULL_STATE_TOKEN (struct state_token_st*)0
126
127/* the state_token pointer contains the leftmost current token. The
128 tokens are organized in a linked queue, using stok_next, for token
129 look-ahead. */
130struct state_token_st *state_token = NULL_STATE_TOKEN;
131
132/* Used by the reading lexer. */
133static FILE *state_file;
134static const char *state_path = NULL;
135static int state_line = 0;
136static long state_bol = 0; /* offset of beginning of line */
137
138/* A class for writing out s-expressions, keeping track of newlines and
139 nested indentation. */
140class s_expr_writer
141{
142public:
143 s_expr_writer ();
144
145 void write_new_line ();
146 void write_any_indent (int leading_spaces);
147
148 void begin_s_expr (const char *tag);
149 void end_s_expr ();
150
151private:
152 int m_indent_amount;
153 int m_had_recent_newline;
154}; // class s_expr_writer
155
156/* A class for writing out "gtype.state". */
157class state_writer : public s_expr_writer
158{
159public:
160 state_writer ();
161
162private:
163 void write_state_fileloc (struct fileloc *floc);
164 void write_state_fields (pair_p fields);
165 void write_state_a_string (const char *s);
166 void write_state_string_option (options_p current);
167 void write_state_type_option (options_p current);
168 void write_state_nested_option (options_p current);
169 void write_state_option (options_p current);
170 void write_state_options (options_p opt);
171 void write_state_lang_bitmap (lang_bitmap bitmap);
172 void write_state_version (const char *version);
173 void write_state_scalar_type (type_p current);
174 void write_state_string_type (type_p current);
175 void write_state_callback_type (type_p current);
176 void write_state_undefined_type (type_p current);
177 void write_state_struct_union_type (type_p current, const char *kindstr);
178 void write_state_struct_type (type_p current);
179 void write_state_user_struct_type (type_p current);
180 void write_state_union_type (type_p current);
181 void write_state_lang_struct_type (type_p current);
182 void write_state_pointer_type (type_p current);
183 void write_state_array_type (type_p current);
184 void write_state_gc_used (enum gc_used_enum gus);
185 void write_state_common_type_content (type_p current);
186 void write_state_type (type_p current);
187 void write_state_pair (pair_p current);
188 int write_state_pair_list (pair_p list);
189 void write_state_typedefs (void);
190 void write_state_structures (void);
191 void write_state_variables (void);
192 void write_state_srcdir (void);
193 void write_state_files_list (void);
194 void write_state_languages (void);
195
196 friend void write_state (const char *state_path);
197
198private:
199 /* Counter of written types. */
200 int m_state_written_type_count;
201}; // class state_writer
202
203
204/* class s_expr_writer's trivial constructor. */
205s_expr_writer::s_expr_writer ()
206 : m_indent_amount (0),
207 m_had_recent_newline (0)
208{
209}
210
211/* Write a newline to the output file, merging adjacent newlines. */
212void
213s_expr_writer::write_new_line (void)
214{
215 /* Don't add a newline if we've just had one. */
216 if (!m_had_recent_newline)
217 {
218 fprintf (stream: state_file, format: "\n");
219 m_had_recent_newline = 1;
220 }
221}
222
223/* If we've just had a newline, write the indentation amount, potentially
224 omitting some spaces.
225
226 LEADING_SPACES exists to support code that writes strings with leading
227 spaces (e.g " foo") which might occur within a line, or could be the first
228 thing on a line. By passing leading_spaces == 1, when such a string is the
229 first thing on a line, write_any_indent () swallows the successive
230 leading spaces into the indentation so that the "foo" begins at the expected
231 column. */
232void
233s_expr_writer::write_any_indent (int leading_spaces)
234{
235 int i;
236 int amount = m_indent_amount - leading_spaces;
237 if (m_had_recent_newline)
238 for (i = 0; i < amount; i++)
239 fprintf (stream: state_file, format: " ");
240 m_had_recent_newline = 0;
241}
242
243/* Write the beginning of a new s-expresion e.g. "(!foo "
244 The writer automatically adds whitespace to show the hierarchical
245 structure of the expressions, so each one starts on a new line,
246 and any within it will be at an increased indentation level. */
247void
248s_expr_writer::begin_s_expr (const char *tag)
249{
250 write_new_line ();
251 write_any_indent (leading_spaces: 0);
252 fprintf (stream: state_file, format: "(!%s ", tag);
253 m_indent_amount++;
254}
255
256/* Write out the end of an s-expression: any necssessary indentation,
257 a closing parenthesis, and a new line. */
258void
259s_expr_writer::end_s_expr (void)
260{
261 m_indent_amount--;
262 write_any_indent (leading_spaces: 0);
263 fprintf (stream: state_file, format: ")");
264 write_new_line ();
265}
266
267
268/* class state_writer's trivial constructor. */
269state_writer::state_writer ()
270 : s_expr_writer (),
271 m_state_written_type_count (0)
272{
273}
274
275
276/* Fatal error messages when reading the state. They are extremely
277 unlikely, and only appear when this gengtype-state.cc file is buggy,
278 or when reading a gengtype state which was not generated by the
279 same version of gengtype or GCC. */
280
281
282/* Fatal message while reading state. */
283static void
284fatal_reading_state (struct state_token_st* tok, const char*msg)
285{
286 if (tok)
287 fatal ("%s:%d:%d: Invalid state file; %s",
288 tok->stok_file, tok->stok_line, tok->stok_col,
289 msg);
290 else
291 fatal ("%s:%d: Invalid state file; %s",
292 state_path, state_line, msg);
293}
294
295
296/* Fatal printf-like message while reading state. This can't be a
297 function, because there is no way to pass a va_arg to a variant of
298 fatal. */
299#define fatal_reading_state_printf(Tok,Fmt,...) do { \
300 struct state_token_st* badtok = Tok; \
301 if (badtok) \
302 fatal ("%s:%d:%d: Invalid state file; " Fmt, \
303 badtok->stok_file, \
304 badtok->stok_line, \
305 badtok->stok_col, __VA_ARGS__); \
306 else \
307 fatal ("%s:%d: Invalid state file; " Fmt, \
308 state_path, state_line, __VA_ARGS__); \
309 } while (0)
310
311
312/* Find or allocate an identifier in our name hash table. */
313static struct state_ident_st *
314state_ident_by_name (const char *name, enum insert_option optins)
315{
316 void **slot = NULL;
317 int namlen = 0;
318 struct state_ident_st *stid = NULL;
319
320 if (!name || !name[0])
321 return NULL;
322
323 slot = htab_find_slot (state_ident_tab, name, optins);
324 if (!slot)
325 return NULL;
326
327 namlen = strlen (s: name);
328 stid =
329 (struct state_ident_st *) xmalloc (sizeof (struct state_ident_st) +
330 namlen);
331 memset (s: stid, c: 0, n: sizeof (struct state_ident_st) + namlen);
332 strcpy (dest: stid->stid_name, src: name);
333 *slot = stid;
334
335 return stid;
336}
337
338/* Our token lexer is heavily inspired by MELT's lexer, and share some
339 code with the file gcc/melt-runtime.c of the GCC MELT branch! We
340 really want the gengtype state to be easily parsable by MELT. This
341 is a usual lispy lexing routine, dealing with spaces and comments,
342 numbers, parenthesis, names, strings. */
343static struct state_token_st *
344read_a_state_token (void)
345{
346 int c = 0;
347 long curoff = 0;
348 struct state_token_st *tk = NULL;
349
350 again: /* Read again, e.g. after a comment or spaces. */
351 c = getc (state_file);
352 if (c == EOF)
353 return NULL;
354
355 /* Handle spaces, count lines. */
356 if (c == '\n')
357 {
358 state_line++;
359 state_bol = curoff = ftell (stream: state_file);
360 goto again;
361 };
362 if (ISSPACE (c))
363 goto again;
364 /* Skip comments starting with semi-colon. */
365 if (c == ';')
366 {
367 do
368 {
369 c = getc (state_file);
370 }
371 while (c > 0 && c != '\n');
372 if (c == '\n')
373 {
374 state_line++;
375 state_bol = curoff = ftell (stream: state_file);
376 }
377 goto again;
378 };
379 /* Read signed numbers. */
380 if (ISDIGIT (c) || c == '-' || c == '+')
381 { /* number */
382 int n = 0;
383 ungetc (c: c, stream: state_file);
384 curoff = ftell (stream: state_file);
385 if (fscanf (stream: state_file, format: "%d", &n) <= 0)
386 fatal_reading_state (NULL_STATE_TOKEN, msg: "Lexical error in number");
387 tk = XCNEW (struct state_token_st);
388 tk->stok_kind = STOK_INTEGER;
389 tk->stok_line = state_line;
390 tk->stok_col = curoff - state_bol;
391 tk->stok_file = state_path;
392 tk->stok_next = NULL;
393 tk->stok_un.stok_num = n;
394
395 return tk;
396 }
397 /* Read an opening left parenthesis. */
398 else if (c == '(')
399 {
400 curoff = ftell (stream: state_file);
401 tk = XCNEW (struct state_token_st);
402 tk->stok_kind = STOK_LEFTPAR;
403 tk->stok_line = state_line;
404 tk->stok_col = curoff - state_bol;
405 tk->stok_file = state_path;
406 tk->stok_next = NULL;
407
408 return tk;
409 }
410 /* Read an closing right parenthesis. */
411 else if (c == ')')
412 {
413 curoff = ftell (stream: state_file);
414 tk = XCNEW (struct state_token_st);
415 tk->stok_kind = STOK_RIGHTPAR;
416 tk->stok_line = state_line;
417 tk->stok_col = curoff - state_bol;
418 tk->stok_file = state_path;
419 tk->stok_next = NULL;
420
421 return tk;
422 }
423 /* Read identifiers, using an obstack. */
424 else if (ISALPHA (c) || c == '_' || c == '$' || c == '!' || c == '#')
425 {
426 struct obstack id_obstack;
427 struct state_ident_st *sid = NULL;
428 char *ids = NULL;
429 obstack_init (&id_obstack);
430 curoff = ftell (stream: state_file);
431 while (ISALNUM (c) || c == '_' || c == '$' || c == '!' || c == '#')
432 {
433 obstack_1grow (&id_obstack, c);
434 c = getc (state_file);
435 if (c < 0)
436 break;
437 };
438 if (c >= 0)
439 ungetc (c: c, stream: state_file);
440 obstack_1grow (&id_obstack, (char) 0);
441 ids = XOBFINISH (&id_obstack, char *);
442 sid = state_ident_by_name (name: ids, optins: INSERT);
443 obstack_free (&id_obstack, NULL);
444 ids = NULL;
445 tk = XCNEW (struct state_token_st);
446 tk->stok_kind = STOK_NAME;
447 tk->stok_line = state_line;
448 tk->stok_col = curoff - state_bol;
449 tk->stok_file = state_path;
450 tk->stok_next = NULL;
451 tk->stok_un.stok_ident = sid;
452
453 return tk;
454 }
455 /* Read a string, dealing with escape sequences a la C! */
456 else if (c == '"')
457 {
458 char *cstr = NULL;
459 int cslen = 0;
460 struct obstack bstring_obstack;
461 obstack_init (&bstring_obstack);
462 curoff = ftell (stream: state_file);
463 while ((c = getc (state_file)) != '"' && c >= 0)
464 {
465 if (ISPRINT (c) && c != '\\')
466 obstack_1grow (&bstring_obstack, (char) c);
467 else if (ISSPACE (c) && c != '\n')
468 obstack_1grow (&bstring_obstack, (char) c);
469 else if (c == '\\')
470 {
471 c = getc (state_file);
472 switch (c)
473 {
474 case 'a':
475 obstack_1grow (&bstring_obstack, '\a');
476 break;
477 case 'b':
478 obstack_1grow (&bstring_obstack, '\b');
479 break;
480 case 't':
481 obstack_1grow (&bstring_obstack, '\t');
482 break;
483 case 'n':
484 obstack_1grow (&bstring_obstack, '\n');
485 break;
486 case 'v':
487 obstack_1grow (&bstring_obstack, '\v');
488 break;
489 case 'f':
490 obstack_1grow (&bstring_obstack, '\f');
491 break;
492 case 'r':
493 obstack_1grow (&bstring_obstack, '\r');
494 break;
495 case '"':
496 obstack_1grow (&bstring_obstack, '\"');
497 break;
498 case '\\':
499 obstack_1grow (&bstring_obstack, '\\');
500 break;
501 case ' ':
502 obstack_1grow (&bstring_obstack, ' ');
503 break;
504 case 'x':
505 {
506 unsigned int cx = 0;
507 if (fscanf (stream: state_file, format: "%02x", &cx) > 0 && cx > 0)
508 obstack_1grow (&bstring_obstack, cx);
509 else
510 fatal_reading_state
511 (NULL_STATE_TOKEN,
512 msg: "Lexical error in string hex escape");
513 getc (state_file);
514 break;
515 }
516 default:
517 fatal_reading_state
518 (NULL_STATE_TOKEN,
519 msg: "Lexical error - unknown string escape");
520 }
521 }
522 else
523 fatal_reading_state (NULL_STATE_TOKEN, msg: "Lexical error...");
524 };
525 if (c != '"')
526 fatal_reading_state (NULL_STATE_TOKEN, msg: "Unterminated string");
527 obstack_1grow (&bstring_obstack, '\0');
528 cstr = XOBFINISH (&bstring_obstack, char *);
529 cslen = strlen (s: cstr);
530 tk = (struct state_token_st *)
531 xcalloc (sizeof (struct state_token_st) + cslen, 1);
532 tk->stok_kind = STOK_STRING;
533 tk->stok_line = state_line;
534 tk->stok_col = curoff - state_bol;
535 tk->stok_file = state_path;
536 tk->stok_next = NULL;
537 strcpy (dest: tk->stok_un.stok_string, src: cstr);
538 obstack_free (&bstring_obstack, NULL);
539
540 return tk;
541 }
542 /* Got an unexpected character. */
543 fatal_reading_state_printf
544 (NULL_STATE_TOKEN,
545 "Lexical error at offset %ld - bad character \\%03o = '%c'",
546 ftell (state_file), c, c);
547}
548
549/* Used for lexical look-ahead. Retrieves the lexical token of rank
550 DEPTH, starting with 0 when reading the state file. Gives null on
551 end of file. */
552static struct state_token_st *
553peek_state_token (int depth)
554{
555 int remdepth = depth;
556 struct state_token_st **ptoken = &state_token;
557 struct state_token_st *tok = NULL;
558
559 while (remdepth >= 0)
560 {
561 if (*ptoken == NULL)
562 {
563 *ptoken = tok = read_a_state_token ();
564 if (tok == NULL)
565 return NULL;
566 }
567 tok = *ptoken;
568 ptoken = &((*ptoken)->stok_next);
569 remdepth--;
570 }
571
572 return tok;
573}
574
575/* Consume the next DEPTH tokens and free them. */
576static void
577next_state_tokens (int depth)
578{
579 struct state_token_st *n;
580
581 while (depth > 0)
582 {
583 if (state_token != NULL)
584 {
585 n = state_token->stok_next;
586 free (ptr: state_token);
587 state_token = n;
588 }
589 else
590 fatal_reading_state (NULL_STATE_TOKEN, msg: "Tokens stack empty");
591
592 depth--;
593 }
594}
595
596/* Safely retrieve the lexical kind of a token. */
597static inline enum state_token_en
598state_token_kind (struct state_token_st *p)
599{
600 if (p == NULL)
601 return STOK_NONE;
602 else
603 return p->stok_kind;
604}
605
606/* Test if a token is a given name i.e. an identifier. */
607static inline bool
608state_token_is_name (struct state_token_st *p, const char *name)
609{
610 if (p == NULL)
611 return false;
612
613 if (p->stok_kind != STOK_NAME)
614 return false;
615
616 return !strcmp (s1: p->stok_un.stok_ident->stid_name, s2: name);
617}
618
619
620/* Following routines are useful for serializing datas.
621 *
622 * We want to serialize :
623 * - typedefs list
624 * - structures list
625 * - variables list
626 *
627 * So, we have one routine for each kind of data. The main writing
628 * routine is write_state. The main reading routine is
629 * read_state. Most writing routines write_state_FOO have a
630 * corresponding reading routine read_state_FOO. Reading is done in a
631 * recursive descending way, and any read error is fatal.
632 */
633
634/* When reading the state, we need to remember the previously seen
635 types by their state_number, since GTY-ed types are usually
636 shared. */
637static htab_t state_seen_types;
638
639/* Return the length of a linked list made of pairs. */
640static int pair_list_length (pair_p list);
641
642/* Compute the length of a list of pairs, starting from the first
643 one. */
644static int
645pair_list_length (pair_p list)
646{
647 int nbpair = 0;
648 pair_p l = NULL;
649 for (l = list; l; l = l->next)
650 nbpair++;
651 return nbpair;
652}
653
654/* Write a file location. Files relative to $(srcdir) are quite
655 frequent and are handled specially. This ensures that two gengtype
656 state file-s produced by gengtype on the same GCC source tree are
657 very similar and can be reasonably compared with diff, even if the
658 two GCC source trees have different absolute paths. */
659void
660state_writer::write_state_fileloc (struct fileloc *floc)
661{
662
663 if (floc != NULL && floc->line > 0)
664 {
665 const char *srcrelpath = NULL;
666 gcc_assert (floc->file != NULL);
667 /* Most of the files are inside $(srcdir) so it is worth to
668 handle them specially. */
669 srcrelpath = get_file_srcdir_relative_path (inpf: floc->file);
670 if (srcrelpath != NULL)
671 {
672 begin_s_expr (tag: "srcfileloc");
673 write_state_a_string (s: srcrelpath);
674 }
675 else
676 {
677 begin_s_expr (tag: "fileloc");
678 write_state_a_string (s: get_input_file_name (inpf: floc->file));
679 }
680 fprintf (stream: state_file, format: " %d", floc->line);
681 end_s_expr ();
682 }
683 else
684 fprintf (stream: state_file, format: "nil ");
685}
686
687/* Write a list of fields. */
688void
689state_writer::write_state_fields (pair_p fields)
690{
691 int nbfields = pair_list_length (list: fields);
692 int nbpairs = 0;
693 begin_s_expr (tag: "fields");
694 fprintf (stream: state_file, format: "%d ", nbfields);
695 nbpairs = write_state_pair_list (list: fields);
696 gcc_assert (nbpairs == nbfields);
697 end_s_expr ();
698}
699
700/* Write a null-terminated string in our lexical convention, very
701 similar to the convention of C. */
702void
703state_writer::write_state_a_string (const char *s)
704{
705 char c;
706
707 write_any_indent (leading_spaces: 1);
708
709 fputs (" \"", state_file);
710 for (; *s != 0; s++)
711 {
712 c = *s;
713 switch (c)
714 {
715 case '\a':
716 fputs ("\\a", state_file);
717 break;
718 case '\b':
719 fputs ("\\b", state_file);
720 break;
721 case '\t':
722 fputs ("\\t", state_file);
723 break;
724 case '\n':
725 fputs ("\\n", state_file);
726 break;
727 case '\v':
728 fputs ("\\v", state_file);
729 break;
730 case '\f':
731 fputs ("\\f", state_file);
732 break;
733 case '\r':
734 fputs ("\\r", state_file);
735 break;
736 case '\"':
737 fputs ("\\\"", state_file);
738 break;
739 case '\\':
740 fputs ("\\\\", state_file);
741 break;
742 default:
743 if (ISPRINT (c))
744 putc (c, state_file);
745 else
746 fprintf (stream: state_file, format: "\\x%02x", (unsigned) c);
747 }
748 }
749 fputs ("\"", state_file);
750}
751
752/* Our option-s have three kinds, each with its writer. */
753void
754state_writer::write_state_string_option (options_p current)
755{
756 write_any_indent (leading_spaces: 0);
757 fprintf (stream: state_file, format: "string ");
758 if (current->info.string != NULL)
759 write_state_a_string (s: current->info.string);
760 else
761 fprintf (stream: state_file, format: " nil ");
762}
763
764void
765state_writer::write_state_type_option (options_p current)
766{
767 write_any_indent (leading_spaces: 0);
768 fprintf (stream: state_file, format: "type ");
769 write_state_type (current: current->info.type);
770}
771
772void
773state_writer::write_state_nested_option (options_p current)
774{
775 write_any_indent (leading_spaces: 0);
776 fprintf (stream: state_file, format: "nested ");
777 write_state_type (current: current->info.nested->type);
778 if (current->info.nested->convert_from != NULL)
779 write_state_a_string (s: current->info.nested->convert_from);
780 else
781 {
782 write_any_indent (leading_spaces: 1);
783 fprintf (stream: state_file, format: " nil ");
784 }
785
786 if (current->info.nested->convert_to != NULL)
787 write_state_a_string (s: current->info.nested->convert_to);
788 else
789 {
790 write_any_indent (leading_spaces: 1);
791 fprintf (stream: state_file, format: " nil ");
792 }
793}
794
795void
796state_writer::write_state_option (options_p current)
797{
798 begin_s_expr (tag: "option");
799
800 write_any_indent (leading_spaces: 0);
801 if (current->name != NULL)
802 fprintf (stream: state_file, format: "%s ", current->name);
803 else
804 fprintf (stream: state_file, format: "nil ");
805
806 switch (current->kind)
807 {
808 case OPTION_STRING:
809 write_state_string_option (current);
810 break;
811 case OPTION_TYPE:
812 write_state_type_option (current);
813 break;
814 case OPTION_NESTED:
815 write_state_nested_option (current);
816 break;
817 default:
818 fatal ("Option tag unknown");
819 }
820
821 /* Terminate the "option" s-expression. */
822 end_s_expr ();
823}
824
825
826
827/* Write a list of GTY options. */
828void
829state_writer::write_state_options (options_p opt)
830{
831 options_p current;
832
833 if (opt == NULL)
834 {
835 write_any_indent (leading_spaces: 0);
836 fprintf (stream: state_file, format: "nil ");
837 return;
838 }
839
840 begin_s_expr (tag: "options");
841 for (current = opt; current != NULL; current = current->next)
842 write_state_option (current);
843 end_s_expr ();
844}
845
846
847/* Write a bitmap representing a set of GCC front-end languages. */
848void
849state_writer::write_state_lang_bitmap (lang_bitmap bitmap)
850{
851 write_any_indent (leading_spaces: 0);
852 fprintf (stream: state_file, format: "%d ", (int) bitmap);
853}
854
855/* Write version information. */
856void
857state_writer::write_state_version (const char *version)
858{
859 begin_s_expr (tag: "version");
860 write_state_a_string (s: version);
861 end_s_expr ();
862}
863
864/* Write a scalar type. We have only two of these. */
865void
866state_writer::write_state_scalar_type (type_p current)
867{
868 write_any_indent (leading_spaces: 0);
869 if (current == &scalar_nonchar)
870 fprintf (stream: state_file, format: "scalar_nonchar ");
871 else if (current == &scalar_char)
872 fprintf (stream: state_file, format: "scalar_char ");
873 else
874 fatal ("Unexpected type in write_state_scalar_type");
875
876 write_state_common_type_content (current);
877}
878
879/* Write the string type. There is only one such thing! */
880void
881state_writer::write_state_string_type (type_p current)
882{
883 if (current == &string_type)
884 {
885 write_any_indent (leading_spaces: 0);
886 fprintf (stream: state_file, format: "string ");
887 write_state_common_type_content (current);
888 }
889 else
890 fatal ("Unexpected type in write_state_string_type");
891}
892
893/* Write the callback type. There is only one such thing! */
894void
895state_writer::write_state_callback_type (type_p current)
896{
897 if (current == &callback_type)
898 {
899 write_any_indent (leading_spaces: 0);
900 fprintf (stream: state_file, format: "callback ");
901 write_state_common_type_content (current);
902 }
903 else
904 fatal ("Unexpected type in write_state_callback_type");
905}
906
907/* Write an undefined type. */
908void
909state_writer::write_state_undefined_type (type_p current)
910{
911 DBGPRINTF ("undefined type @ %p #%d '%s'", (void *) current,
912 current->state_number, current->u.s.tag);
913 write_any_indent (leading_spaces: 0);
914 fprintf (stream: state_file, format: "undefined ");
915 gcc_assert (current->gc_used == GC_UNUSED);
916 write_state_common_type_content (current);
917 if (current->u.s.tag != NULL)
918 write_state_a_string (s: current->u.s.tag);
919 else
920 {
921 write_any_indent (leading_spaces: 0);
922 fprintf (stream: state_file, format: "nil");
923 }
924
925 write_state_fileloc (floc: type_lineloc (ty: current));
926}
927
928
929/* Common code to write structure like types. */
930void
931state_writer::write_state_struct_union_type (type_p current,
932 const char *kindstr)
933{
934 DBGPRINTF ("%s type @ %p #%d '%s'", kindstr, (void *) current,
935 current->state_number, current->u.s.tag);
936 write_any_indent (leading_spaces: 0);
937 fprintf (stream: state_file, format: "%s ", kindstr);
938 write_state_common_type_content (current);
939 if (current->u.s.tag != NULL)
940 write_state_a_string (s: current->u.s.tag);
941 else
942 {
943 write_any_indent (leading_spaces: 0);
944 fprintf (stream: state_file, format: "nil");
945 }
946
947 write_state_fileloc (floc: type_lineloc (ty: current));
948 write_state_fields (fields: current->u.s.fields);
949 write_state_options (opt: current->u.s.opt);
950 write_state_lang_bitmap (bitmap: current->u.s.bitmap);
951}
952
953
954/* Write a GTY struct type. */
955void
956state_writer::write_state_struct_type (type_p current)
957{
958 write_state_struct_union_type (current, kindstr: "struct");
959 write_state_type (current: current->u.s.lang_struct);
960 write_state_type (current: current->u.s.base_class);
961}
962
963/* Write a GTY user-defined struct type. */
964void
965state_writer::write_state_user_struct_type (type_p current)
966{
967 DBGPRINTF ("user_struct type @ %p #%d '%s'", (void *) current,
968 current->state_number, current->u.s.tag);
969 write_any_indent (leading_spaces: 0);
970 fprintf (stream: state_file, format: "user_struct ");
971 write_state_common_type_content (current);
972 if (current->u.s.tag != NULL)
973 write_state_a_string (s: current->u.s.tag);
974 else
975 {
976 write_any_indent (leading_spaces: 0);
977 fprintf (stream: state_file, format: "nil");
978 }
979 write_state_fileloc (floc: type_lineloc (ty: current));
980 write_state_fields (fields: current->u.s.fields);
981}
982
983/* write a GTY union type. */
984void
985state_writer::write_state_union_type (type_p current)
986{
987 write_state_struct_union_type (current, kindstr: "union");
988 write_state_type (current: current->u.s.lang_struct);
989}
990
991/* Write a lang_struct type. This is tricky and was painful to debug,
992 we deal with the next field specifically within their lang_struct
993 subfield, which points to a linked list of homonumous types.
994 Change this function with extreme care, see also
995 read_state_lang_struct_type. */
996void
997state_writer::write_state_lang_struct_type (type_p current)
998{
999 int nbhomontype = 0;
1000 type_p hty = NULL;
1001 const char *homoname = 0;
1002 write_state_struct_union_type (current, kindstr: "lang_struct");
1003 /* lang_struct-ures are particularly tricky, since their
1004 u.s.lang_struct field gives a list of homonymous struct-s or
1005 union-s! */
1006 DBGPRINTF ("lang_struct @ %p #%d", (void *) current, current->state_number);
1007 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
1008 {
1009 nbhomontype++;
1010 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype,
1011 (void *) hty, hty->state_number, hty->u.s.tag);
1012 /* Every member of the homonymous list should have the same tag. */
1013 gcc_assert (union_or_struct_p (hty));
1014 gcc_assert (hty->u.s.lang_struct == current);
1015 if (!homoname)
1016 homoname = hty->u.s.tag;
1017 gcc_assert (strcmp (homoname, hty->u.s.tag) == 0);
1018 }
1019 begin_s_expr (tag: "homotypes");
1020 fprintf (stream: state_file, format: "%d", nbhomontype);
1021 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
1022 write_state_type (current: hty);
1023 end_s_expr ();
1024}
1025
1026/* Write a pointer type. */
1027void
1028state_writer::write_state_pointer_type (type_p current)
1029{
1030 write_any_indent (leading_spaces: 0);
1031 fprintf (stream: state_file, format: "pointer ");
1032 write_state_common_type_content (current);
1033 write_state_type (current: current->u.p);
1034}
1035
1036/* Write an array type. */
1037void
1038state_writer::write_state_array_type (type_p current)
1039{
1040 write_any_indent (leading_spaces: 0);
1041 fprintf (stream: state_file, format: "array ");
1042 write_state_common_type_content (current);
1043 if (current->u.a.len != NULL)
1044 write_state_a_string (s: current->u.a.len);
1045 else
1046 {
1047 write_any_indent (leading_spaces: 1);
1048 fprintf (stream: state_file, format: " nil");
1049 }
1050
1051 write_any_indent (leading_spaces: 1);
1052 fprintf (stream: state_file, format: " ");
1053 write_state_type (current: current->u.a.p);
1054}
1055
1056/* Write the gc_used information. */
1057void
1058state_writer::write_state_gc_used (enum gc_used_enum gus)
1059{
1060 write_any_indent (leading_spaces: 1);
1061 switch (gus)
1062 {
1063 case GC_UNUSED:
1064 fprintf (stream: state_file, format: " gc_unused");
1065 break;
1066 case GC_USED:
1067 fprintf (stream: state_file, format: " gc_used");
1068 break;
1069 case GC_MAYBE_POINTED_TO:
1070 fprintf (stream: state_file, format: " gc_maybe_pointed_to");
1071 break;
1072 case GC_POINTED_TO:
1073 fprintf (stream: state_file, format: " gc_pointed_to");
1074 break;
1075 default:
1076 gcc_unreachable ();
1077 }
1078}
1079
1080/* Utility routine to write the common content of all types. Notice
1081 that the next field is *not* written on purpose. */
1082void
1083state_writer::write_state_common_type_content (type_p current)
1084{
1085 write_any_indent (leading_spaces: 0);
1086 fprintf (stream: state_file, format: "%d ", current->state_number);
1087 /* We do not write the next type, because list of types are
1088 explicitly written. However, lang_struct are special in that
1089 respect. See function write_state_lang_struct_type for more. */
1090 write_state_type (current: current->pointer_to);
1091 write_state_gc_used (gus: current->gc_used);
1092}
1093
1094
1095/* The important and recursive routine writing GTY types as understood
1096 by gengtype. Types which have a positive state_number have already
1097 been seen and written. */
1098void
1099state_writer::write_state_type (type_p current)
1100{
1101 write_any_indent (leading_spaces: 0);
1102 if (current == NULL)
1103 {
1104 fprintf (stream: state_file, format: "nil ");
1105 return;
1106 }
1107
1108 begin_s_expr (tag: "type");
1109
1110 if (current->state_number > 0)
1111 {
1112 write_any_indent (leading_spaces: 0);
1113 fprintf (stream: state_file, format: "already_seen %d", current->state_number);
1114 }
1115 else
1116 {
1117 m_state_written_type_count++;
1118 DBGPRINTF ("writing type #%d @%p old number %d", m_state_written_type_count,
1119 (void *) current, current->state_number);
1120 current->state_number = m_state_written_type_count;
1121 switch (current->kind)
1122 {
1123 case TYPE_NONE:
1124 gcc_unreachable ();
1125 case TYPE_UNDEFINED:
1126 write_state_undefined_type (current);
1127 break;
1128 case TYPE_STRUCT:
1129 write_state_struct_type (current);
1130 break;
1131 case TYPE_USER_STRUCT:
1132 write_state_user_struct_type (current);
1133 break;
1134 case TYPE_UNION:
1135 write_state_union_type (current);
1136 break;
1137 case TYPE_POINTER:
1138 write_state_pointer_type (current);
1139 break;
1140 case TYPE_ARRAY:
1141 write_state_array_type (current);
1142 break;
1143 case TYPE_LANG_STRUCT:
1144 write_state_lang_struct_type (current);
1145 break;
1146 case TYPE_SCALAR:
1147 write_state_scalar_type (current);
1148 break;
1149 case TYPE_STRING:
1150 write_state_string_type (current);
1151 break;
1152 case TYPE_CALLBACK:
1153 write_state_callback_type (current);
1154 break;
1155 }
1156 }
1157
1158 /* Terminate the "type" s-expression. */
1159 end_s_expr ();
1160}
1161
1162
1163/* Write a pair. */
1164void
1165state_writer::write_state_pair (pair_p current)
1166{
1167 if (current == NULL)
1168 {
1169 write_any_indent (leading_spaces: 0);
1170 fprintf (stream: state_file, format: "nil)");
1171 return;
1172 }
1173
1174 begin_s_expr (tag: "pair");
1175
1176 if (current->name != NULL)
1177 write_state_a_string (s: current->name);
1178 else
1179 write_state_a_string (s: "nil");
1180
1181 write_state_type (current: current->type);
1182 write_state_fileloc (floc: &(current->line));
1183 write_state_options (opt: current->opt);
1184
1185 /* Terminate the "pair" s-expression. */
1186 end_s_expr ();
1187}
1188
1189/* Write a pair list and return the number of pairs written. */
1190int
1191state_writer::write_state_pair_list (pair_p list)
1192{
1193 int nbpair = 0;
1194 pair_p current;
1195
1196 for (current = list; current != NULL; current = current->next)
1197 {
1198 write_state_pair (current);
1199 nbpair++;
1200 }
1201 return nbpair;
1202
1203}
1204
1205/* When writing imported linked lists, like typedefs, structures, ... we count
1206 their length first and write it. This eases the reading, and enables an
1207 extra verification on the number of actually read items. */
1208
1209/* Write our typedefs. */
1210void
1211state_writer::write_state_typedefs (void)
1212{
1213 int nbtypedefs = pair_list_length (list: typedefs);
1214 int nbpairs = 0;
1215 begin_s_expr (tag: "typedefs");
1216 fprintf (stream: state_file, format: "%d", nbtypedefs);
1217 nbpairs = write_state_pair_list (list: typedefs);
1218 gcc_assert (nbpairs == nbtypedefs);
1219 end_s_expr ();
1220 if (verbosity_level >= 2)
1221 printf (format: "%s wrote %d typedefs\n", progname, nbtypedefs);
1222}
1223
1224/* Write our structures. */
1225void
1226state_writer::write_state_structures (void)
1227{
1228 int nbstruct = 0;
1229 type_p current;
1230
1231 for (current = structures; current != NULL; current = current->next)
1232 nbstruct++;
1233
1234 begin_s_expr (tag: "structures");
1235 fprintf (stream: state_file, format: "%d", nbstruct);
1236
1237 for (current = structures; current != NULL; current = current->next)
1238 {
1239 write_new_line ();
1240 write_state_type (current);
1241 }
1242
1243 /* Terminate the "structures" s-expression. */
1244 end_s_expr ();
1245 if (verbosity_level >= 2)
1246 printf (format: "%s wrote %d structures in state\n", progname, nbstruct);
1247}
1248
1249/* Write our variables. */
1250void
1251state_writer::write_state_variables (void)
1252{
1253 int nbvars = pair_list_length (list: variables);
1254 int nbpairs = 0;
1255 begin_s_expr (tag: "variables");
1256 fprintf (stream: state_file, format: "%d", nbvars);
1257 nbpairs = write_state_pair_list (list: variables);
1258 gcc_assert (nbpairs == nbvars);
1259 end_s_expr ();
1260 if (verbosity_level >= 2)
1261 printf (format: "%s wrote %d variables.\n", progname, nbvars);
1262}
1263
1264/* Write the source directory. File locations within the source
1265 directory have been written specifically. */
1266void
1267state_writer::write_state_srcdir (void)
1268{
1269 begin_s_expr (tag: "srcdir");
1270 write_state_a_string (s: srcdir);
1271 end_s_expr ();
1272}
1273
1274/* Count and write the list of our files. */
1275void
1276state_writer::write_state_files_list (void)
1277{
1278 int i = 0;
1279 /* Write the list of files with their lang_bitmap. */
1280 begin_s_expr (tag: "fileslist");
1281 fprintf (stream: state_file, format: "%d %d", (int) num_gt_files, (int) num_build_headers);
1282 for (i = 0; i < (int) num_gt_files; i++)
1283 {
1284 const char *cursrcrelpath = NULL;
1285 const input_file *curfil = gt_files[i];
1286 /* Most of the files are inside $(srcdir) so it is worth to
1287 handle them specially. */
1288 cursrcrelpath = get_file_srcdir_relative_path (inpf: curfil);
1289 if (cursrcrelpath)
1290 {
1291 begin_s_expr (tag: "srcfile");
1292 fprintf (stream: state_file, format: "%d ", get_lang_bitmap (inpf: curfil));
1293 write_state_a_string (s: cursrcrelpath);
1294 }
1295 else
1296 {
1297 begin_s_expr (tag: "file");
1298 fprintf (stream: state_file, format: "%d ", get_lang_bitmap (inpf: curfil));
1299 write_state_a_string (s: get_input_file_name (inpf: curfil));
1300 }
1301 /* Terminate the inner s-expression (either "srcfile" or "file"). */
1302 end_s_expr ();
1303 }
1304 /* Terminate the "fileslist" s-expression. */
1305 end_s_expr ();
1306}
1307
1308/* Write the list of GCC front-end languages. */
1309void
1310state_writer::write_state_languages (void)
1311{
1312 int i = 0;
1313 begin_s_expr (tag: "languages");
1314 fprintf (stream: state_file, format: "%d", (int) num_lang_dirs);
1315 for (i = 0; i < (int) num_lang_dirs; i++)
1316 {
1317 /* Languages names are identifiers, we expect only letters or
1318 underscores or digits in them. In particular, C++ is not a
1319 valid language name, but cp is valid. */
1320 fprintf (stream: state_file, format: " %s", lang_dir_names[i]);
1321 }
1322 end_s_expr ();
1323}
1324
1325/* Write the trailer. */
1326static void
1327write_state_trailer (void)
1328{
1329 /* This test should probably catch IO errors like disk full... */
1330 if (fputs ("\n(!endfile)\n", state_file) == EOF)
1331 fatal ("failed to write state trailer [%s]", xstrerror (errno));
1332}
1333
1334/* The write_state routine is the only writing routine called by main
1335 in gengtype.cc. To avoid messing the state if gengtype is
1336 interrupted or aborted, we write a temporary file and rename it
1337 after having written it in totality. */
1338void
1339write_state (const char *state_path)
1340{
1341 long statelen = 0;
1342 time_t now = 0;
1343 char *temp_state_path = NULL;
1344 char tempsuffix[40];
1345 time (timer: &now);
1346
1347 /* We write a unique temporary file which is renamed when complete
1348 * only. So even if gengtype is interrupted, the written state file
1349 * won't be partially written, since the temporary file is not yet
1350 * renamed in that case. */
1351 memset (s: tempsuffix, c: 0, n: sizeof (tempsuffix));
1352 snprintf (s: tempsuffix, maxlen: sizeof (tempsuffix) - 1, format: "-%ld-%d.tmp", (long) now,
1353 (int) getpid ());
1354 temp_state_path = concat (state_path, tempsuffix, NULL);
1355 state_file = fopen (temp_state_path, "w");
1356 if (state_file == NULL)
1357 fatal ("Failed to open file %s for writing state: %s",
1358 temp_state_path, xstrerror (errno));
1359 if (verbosity_level >= 3)
1360 printf (format: "%s writing state file %s temporarily in %s\n",
1361 progname, state_path, temp_state_path);
1362 /* This is the first line of the state. Perhaps the file utility
1363 could know about that, so don't change it often. */
1364 fprintf (stream: state_file, format: ";;;;@@@@ GCC gengtype state\n");
1365 /* Output a few comments for humans. */
1366 fprintf (stream: state_file,
1367 format: ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
1368 fprintf (stream: state_file,
1369 format: ";;; The format of this file is tied to a particular version of GCC.\n");
1370 fprintf (stream: state_file,
1371 format: ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1372 fprintf (stream: state_file,
1373 format: ";;; This file should be parsed by the same %s which wrote it.\n",
1374 progname);
1375
1376 state_writer sw;
1377
1378 /* The first non-comment significant line gives the version string. */
1379 sw.write_state_version (version_string);
1380 sw.write_state_srcdir ();
1381 sw.write_state_languages ();
1382 sw.write_state_files_list ();
1383 sw.write_state_structures ();
1384 sw.write_state_typedefs ();
1385 sw.write_state_variables ();
1386 write_state_trailer ();
1387 statelen = ftell (stream: state_file);
1388 if (ferror (state_file))
1389 fatal ("output error when writing state file %s [%s]",
1390 temp_state_path, xstrerror (errno));
1391 if (fclose (stream: state_file))
1392 fatal ("failed to close state file %s [%s]",
1393 temp_state_path, xstrerror (errno));
1394 if (rename (old: temp_state_path, new: state_path))
1395 fatal ("failed to rename %s to state file %s [%s]", temp_state_path,
1396 state_path, xstrerror (errno));
1397 free (ptr: temp_state_path);
1398
1399 if (verbosity_level >= 1)
1400 printf (format: "%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1401 progname, state_path, statelen, sw.m_state_written_type_count);
1402
1403}
1404
1405/** End of writing routines! The corresponding reading routines follow. **/
1406
1407
1408
1409/* Forward declarations, since some read_state_* functions are
1410 recursive! */
1411static void read_state_fileloc (struct fileloc *line);
1412static void read_state_options (options_p *opt);
1413static void read_state_type (type_p *current);
1414static void read_state_pair (pair_p *pair);
1415/* Return the number of pairs actually read. */
1416static int read_state_pair_list (pair_p *list);
1417static void read_state_fields (pair_p *fields);
1418static void read_state_common_type_content (type_p current);
1419
1420
1421
1422
1423/* Record into the state_seen_types hash-table a type which we are
1424 reading, to enable recursive or circular references to it. */
1425static void
1426record_type (type_p type)
1427{
1428 void **slot;
1429
1430 slot = htab_find_slot (state_seen_types, type, INSERT);
1431 gcc_assert (slot);
1432
1433 *slot = type;
1434}
1435
1436/* Read an already seen type. */
1437static void
1438read_state_already_seen_type (type_p *type)
1439{
1440 struct state_token_st *t0 = peek_state_token (depth: 0);
1441
1442 if (state_token_kind (p: t0) == STOK_INTEGER)
1443 {
1444 void **slot = NULL;
1445 struct type loctype = { .kind: TYPE_SCALAR, .next: 0, .state_number: 0, .pointer_to: 0, .gc_used: GC_UNUSED, .u: {.p: 0} };
1446
1447 loctype.state_number = t0->stok_un.stok_num;
1448 slot = htab_find_slot (state_seen_types, &loctype, NO_INSERT);
1449 if (slot == NULL)
1450 {
1451 fatal_reading_state (tok: t0, msg: "Unknown type");
1452 }
1453
1454 next_state_tokens (depth: 1);
1455 *type = (type_p) *slot;
1456 }
1457 else
1458 {
1459 fatal_reading_state (tok: t0, msg: "Bad seen type");
1460 }
1461}
1462
1463
1464/* Read the scalar_nonchar type. */
1465static void
1466read_state_scalar_nonchar_type (type_p *type)
1467{
1468 *type = &scalar_nonchar;
1469 read_state_common_type_content (current: *type);
1470}
1471
1472
1473/* Read the scalar_char type. */
1474static void
1475read_state_scalar_char_type (type_p *type)
1476{
1477 *type = &scalar_char;
1478 read_state_common_type_content (current: *type);
1479}
1480
1481/* Read the string_type. */
1482static void
1483read_state_string_type (type_p *type)
1484{
1485 *type = &string_type;
1486 read_state_common_type_content (current: *type);
1487}
1488
1489/* Read the callback_type. */
1490static void
1491read_state_callback_type (type_p *type)
1492{
1493 *type = &callback_type;
1494 read_state_common_type_content (current: *type);
1495}
1496
1497
1498/* Read a lang_bitmap representing a set of GCC front-end languages. */
1499static void
1500read_state_lang_bitmap (lang_bitmap *bitmap)
1501{
1502 struct state_token_st *t;
1503
1504 t = peek_state_token (depth: 0);
1505 if (state_token_kind (p: t) == STOK_INTEGER)
1506 {
1507 *bitmap = t->stok_un.stok_num;
1508 next_state_tokens (depth: 1);
1509 }
1510 else
1511 {
1512 fatal_reading_state (tok: t, msg: "Bad syntax for bitmap");
1513 }
1514}
1515
1516
1517/* Read an undefined type. */
1518static void
1519read_state_undefined_type (type_p type)
1520{
1521 struct state_token_st *t0;
1522
1523 type->kind = TYPE_UNDEFINED;
1524 read_state_common_type_content (current: type);
1525 t0 = peek_state_token (depth: 0);
1526 if (state_token_kind (p: t0) == STOK_STRING)
1527 {
1528 if (state_token_is_name (p: t0, name: "nil"))
1529 {
1530 type->u.s.tag = NULL;
1531 DBGPRINTF ("read anonymous undefined type @%p #%d",
1532 (void *) type, type->state_number);
1533 }
1534 else
1535 {
1536 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1537 DBGPRINTF ("read undefined type @%p #%d '%s'",
1538 (void *) type, type->state_number, type->u.s.tag);
1539 }
1540
1541 next_state_tokens (depth: 1);
1542 read_state_fileloc (line: &(type->u.s.line));
1543 }
1544 else
1545 {
1546 fatal_reading_state (tok: t0, msg: "Bad tag in undefined type");
1547 }
1548}
1549
1550
1551/* Read a GTY-ed struct type. */
1552static void
1553read_state_struct_type (type_p type)
1554{
1555 struct state_token_st *t0;
1556
1557 type->kind = TYPE_STRUCT;
1558 read_state_common_type_content (current: type);
1559 t0 = peek_state_token (depth: 0);
1560 if (state_token_kind (p: t0) == STOK_STRING)
1561 {
1562 if (state_token_is_name (p: t0, name: "nil"))
1563 {
1564 type->u.s.tag = NULL;
1565 DBGPRINTF ("read anonymous struct type @%p #%d",
1566 (void *) type, type->state_number);
1567 }
1568 else
1569 {
1570 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1571 DBGPRINTF ("read struct type @%p #%d '%s'",
1572 (void *) type, type->state_number, type->u.s.tag);
1573 }
1574
1575 next_state_tokens (depth: 1);
1576 read_state_fileloc (line: &(type->u.s.line));
1577 read_state_fields (fields: &(type->u.s.fields));
1578 read_state_options (opt: &(type->u.s.opt));
1579 read_state_lang_bitmap (bitmap: &(type->u.s.bitmap));
1580 read_state_type (current: &(type->u.s.lang_struct));
1581 read_state_type (current: &(type->u.s.base_class));
1582 if (type->u.s.base_class)
1583 add_subclass (base: type->u.s.base_class, subclass: type);
1584 }
1585 else
1586 {
1587 fatal_reading_state (tok: t0, msg: "Bad tag in struct type");
1588 }
1589}
1590
1591
1592/* Read a GTY-ed user-provided struct TYPE. */
1593
1594static void
1595read_state_user_struct_type (type_p type)
1596{
1597 struct state_token_st *t0;
1598
1599 type->kind = TYPE_USER_STRUCT;
1600 read_state_common_type_content (current: type);
1601 t0 = peek_state_token (depth: 0);
1602 if (state_token_kind (p: t0) == STOK_STRING)
1603 {
1604 if (state_token_is_name (p: t0, name: "nil"))
1605 {
1606 type->u.s.tag = NULL;
1607 DBGPRINTF ("read anonymous struct type @%p #%d",
1608 (void *) type, type->state_number);
1609 }
1610 else
1611 {
1612 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1613 DBGPRINTF ("read struct type @%p #%d '%s'",
1614 (void *) type, type->state_number, type->u.s.tag);
1615 }
1616
1617 next_state_tokens (depth: 1);
1618 read_state_fileloc (line: &(type->u.s.line));
1619 read_state_fields (fields: &(type->u.s.fields));
1620 }
1621 else
1622 {
1623 fatal_reading_state (tok: t0, msg: "Bad tag in user-struct type");
1624 }
1625}
1626
1627
1628/* Read a GTY-ed union type. */
1629static void
1630read_state_union_type (type_p type)
1631{
1632 struct state_token_st *t0;
1633
1634 type->kind = TYPE_UNION;
1635 read_state_common_type_content (current: type);
1636 t0 = peek_state_token (depth: 0);
1637 if (state_token_kind (p: t0) == STOK_STRING)
1638 {
1639 if (state_token_is_name (p: t0, name: "nil"))
1640 {
1641 type->u.s.tag = NULL;
1642 DBGPRINTF ("read anonymous union type @%p #%d",
1643 (void *) type, type->state_number);
1644 }
1645 else
1646 {
1647 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1648 DBGPRINTF ("read union type @%p #%d '%s'",
1649 (void *) type, type->state_number, type->u.s.tag);
1650 }
1651 next_state_tokens (depth: 1);
1652 read_state_fileloc (line: &(type->u.s.line));
1653 read_state_fields (fields: &(type->u.s.fields));
1654 read_state_options (opt: &(type->u.s.opt));
1655 read_state_lang_bitmap (bitmap: &(type->u.s.bitmap));
1656 read_state_type (current: &(type->u.s.lang_struct));
1657 }
1658 else
1659 fatal_reading_state (tok: t0, msg: "Bad tag in union type");
1660}
1661
1662
1663/* Read a GTY-ed pointer type. */
1664static void
1665read_state_pointer_type (type_p type)
1666{
1667 type->kind = TYPE_POINTER;
1668 read_state_common_type_content (current: type);
1669 DBGPRINTF ("read pointer type @%p #%d", (void *) type, type->state_number);
1670 read_state_type (current: &(type->u.p));
1671}
1672
1673
1674/* Read a GTY-ed array type. */
1675static void
1676read_state_array_type (type_p type)
1677{
1678 struct state_token_st *t0;
1679
1680 type->kind = TYPE_ARRAY;
1681 read_state_common_type_content (current: type);
1682 t0 = peek_state_token (depth: 0);
1683 if (state_token_kind (p: t0) == STOK_STRING)
1684 {
1685 type->u.a.len = xstrdup (t0->stok_un.stok_string);
1686 DBGPRINTF ("read array type @%p #%d length '%s'",
1687 (void *) type, type->state_number, type->u.a.len);
1688 next_state_tokens (depth: 1);
1689 }
1690
1691 else if (state_token_is_name (p: t0, name: "nil"))
1692 {
1693 type->u.a.len = NULL;
1694 DBGPRINTF ("read array type @%p #%d without length",
1695 (void *) type, type->state_number);
1696 next_state_tokens (depth: 1);
1697 }
1698
1699 else
1700 fatal_reading_state (tok: t0, msg: "Bad array name type");
1701 read_state_type (current: &(type->u.a.p));
1702}
1703
1704
1705
1706/* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
1707 front-end languages. This is a tricky function and it was painful
1708 to debug. Change it with extreme care. See also
1709 write_state_lang_struct_type. */
1710static void
1711read_state_lang_struct_type (type_p type)
1712{
1713 struct state_token_st *t0 = NULL;
1714 struct state_token_st *t1 = NULL;
1715 struct state_token_st *t2 = NULL;
1716
1717 type->kind = TYPE_LANG_STRUCT;
1718 read_state_common_type_content (current: type);
1719 t0 = peek_state_token (depth: 0);
1720 if (state_token_kind (p: t0) == STOK_STRING)
1721 {
1722 if (state_token_is_name (p: t0, name: "nil"))
1723 {
1724 DBGPRINTF ("read anonymous lang_struct type @%p #%d",
1725 (void *) type, type->state_number);
1726 type->u.s.tag = NULL;
1727 }
1728 else
1729 {
1730 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1731 DBGPRINTF ("read lang_struct type @%p #%d '%s'",
1732 (void *) type, type->state_number, type->u.s.tag);
1733 }
1734 next_state_tokens (depth: 1);
1735 }
1736 else
1737 fatal_reading_state (tok: t0, msg: "Bad tag in lang struct type");
1738 read_state_fileloc (line: &(type->u.s.line));
1739 read_state_fields (fields: &(type->u.s.fields));
1740 read_state_options (opt: &(type->u.s.opt));
1741 read_state_lang_bitmap (bitmap: &(type->u.s.bitmap));
1742 /* Within lang_struct-ures, the lang_struct field is a linked list
1743 of homonymous types! */
1744 t0 = peek_state_token (depth: 0);
1745 t1 = peek_state_token (depth: 1);
1746 t2 = peek_state_token (depth: 2);
1747 /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
1748 if (state_token_kind (p: t0) == STOK_LEFTPAR
1749 && state_token_is_name (p: t1, name: "!homotypes")
1750 && state_token_kind (p: t2) == STOK_INTEGER)
1751 {
1752 type_p *prevty = &type->u.s.lang_struct;
1753 int nbhomotype = t2->stok_un.stok_num;
1754 int i = 0;
1755 t0 = t1 = t2 = NULL;
1756 next_state_tokens (depth: 3);
1757 for (i = 0; i < nbhomotype; i++)
1758 {
1759 read_state_type (current: prevty);
1760 t0 = peek_state_token (depth: 0);
1761 if (*prevty)
1762 prevty = &(*prevty)->next;
1763 else
1764 fatal_reading_state (tok: t0,
1765 msg: "expecting type in homotype list for lang_struct");
1766 };
1767 if (state_token_kind (p: t0) != STOK_RIGHTPAR)
1768 fatal_reading_state (tok: t0,
1769 msg: "expecting ) in homotype list for lang_struct");
1770 next_state_tokens (depth: 1);
1771 }
1772 else
1773 fatal_reading_state (tok: t0, msg: "expecting !homotypes for lang_struct");
1774}
1775
1776
1777/* Read the gc used information. */
1778static void
1779read_state_gc_used (enum gc_used_enum *pgus)
1780{
1781 struct state_token_st *t0 = peek_state_token (depth: 0);
1782 if (state_token_is_name (p: t0, name: "gc_unused"))
1783 *pgus = GC_UNUSED;
1784 else if (state_token_is_name (p: t0, name: "gc_used"))
1785 *pgus = GC_USED;
1786 else if (state_token_is_name (p: t0, name: "gc_maybe_pointed_to"))
1787 *pgus = GC_MAYBE_POINTED_TO;
1788 else if (state_token_is_name (p: t0, name: "gc_pointed_to"))
1789 *pgus = GC_POINTED_TO;
1790 else
1791 fatal_reading_state (tok: t0, msg: "invalid gc_used information");
1792 next_state_tokens (depth: 1);
1793}
1794
1795
1796/* Utility function to read the common content of types. */
1797static void
1798read_state_common_type_content (type_p current)
1799{
1800 struct state_token_st *t0 = peek_state_token (depth: 0);
1801
1802 if (state_token_kind (p: t0) == STOK_INTEGER)
1803 {
1804 current->state_number = t0->stok_un.stok_num;
1805 next_state_tokens (depth: 1);
1806 record_type (type: current);
1807 }
1808 else
1809 fatal_reading_state_printf (t0,
1810 "Expected integer for state_number line %d",
1811 state_line);
1812 /* We don't read the next field of the type. */
1813 read_state_type (current: &current->pointer_to);
1814 read_state_gc_used (pgus: &current->gc_used);
1815}
1816
1817
1818/* Read a GTY-ed type. */
1819void
1820read_state_type (type_p *current)
1821{
1822 struct state_token_st *t0 = peek_state_token (depth: 0);
1823 struct state_token_st *t1 = peek_state_token (depth: 1);
1824
1825 if (state_token_kind (p: t0) == STOK_LEFTPAR &&
1826 state_token_is_name (p: t1, name: "!type"))
1827 {
1828 next_state_tokens (depth: 2);
1829 t0 = peek_state_token (depth: 0);
1830 if (state_token_is_name (p: t0, name: "already_seen"))
1831 {
1832 next_state_tokens (depth: 1);
1833 read_state_already_seen_type (type: current);
1834 }
1835 else
1836 {
1837 t0 = peek_state_token (depth: 0);
1838
1839 if (state_token_is_name (p: t0, name: "scalar_nonchar"))
1840 {
1841 next_state_tokens (depth: 1);
1842 read_state_scalar_nonchar_type (type: current);
1843 }
1844 else if (state_token_is_name (p: t0, name: "scalar_char"))
1845 {
1846 next_state_tokens (depth: 1);
1847 read_state_scalar_char_type (type: current);
1848 }
1849 else if (state_token_is_name (p: t0, name: "string"))
1850 {
1851 next_state_tokens (depth: 1);
1852 read_state_string_type (type: current);
1853 }
1854 else if (state_token_is_name (p: t0, name: "callback"))
1855 {
1856 next_state_tokens (depth: 1);
1857 read_state_callback_type (type: current);
1858 }
1859 else if (state_token_is_name (p: t0, name: "undefined"))
1860 {
1861 *current = XCNEW (struct type);
1862 next_state_tokens (depth: 1);
1863 read_state_undefined_type (type: *current);
1864 }
1865 else if (state_token_is_name (p: t0, name: "struct"))
1866 {
1867 *current = XCNEW (struct type);
1868 next_state_tokens (depth: 1);
1869 read_state_struct_type (type: *current);
1870 }
1871 else if (state_token_is_name (p: t0, name: "union"))
1872 {
1873 *current = XCNEW (struct type);
1874 next_state_tokens (depth: 1);
1875 read_state_union_type (type: *current);
1876 }
1877 else if (state_token_is_name (p: t0, name: "lang_struct"))
1878 {
1879 *current = XCNEW (struct type);
1880 next_state_tokens (depth: 1);
1881 read_state_lang_struct_type (type: *current);
1882 }
1883 else if (state_token_is_name (p: t0, name: "pointer"))
1884 {
1885 *current = XCNEW (struct type);
1886 next_state_tokens (depth: 1);
1887 read_state_pointer_type (type: *current);
1888 }
1889 else if (state_token_is_name (p: t0, name: "array"))
1890 {
1891 *current = XCNEW (struct type);
1892 next_state_tokens (depth: 1);
1893 read_state_array_type (type: *current);
1894 }
1895 else if (state_token_is_name (p: t0, name: "user_struct"))
1896 {
1897 *current = XCNEW (struct type);
1898 next_state_tokens (depth: 1);
1899 read_state_user_struct_type (type: *current);
1900 }
1901 else
1902 fatal_reading_state (tok: t0, msg: "bad type in (!type");
1903 }
1904 t0 = peek_state_token (depth: 0);
1905 if (state_token_kind (p: t0) != STOK_RIGHTPAR)
1906 fatal_reading_state (tok: t0, msg: "missing ) in type");
1907 next_state_tokens (depth: 1);
1908 }
1909 else if (state_token_is_name (p: t0, name: "nil"))
1910 {
1911 next_state_tokens (depth: 1);
1912 *current = NULL;
1913 }
1914 else
1915 fatal_reading_state (tok: t0, msg: "bad type syntax");
1916}
1917
1918
1919/* Read a file location. Files within the source directory are dealt
1920 with specifically. */
1921void
1922read_state_fileloc (struct fileloc *floc)
1923{
1924 bool issrcfile = false;
1925 struct state_token_st *t0 = peek_state_token (depth: 0);
1926 struct state_token_st *t1 = peek_state_token (depth: 1);
1927
1928 gcc_assert (floc != NULL);
1929 gcc_assert (srcdir != NULL);
1930
1931 if (state_token_kind (p: t0) == STOK_LEFTPAR &&
1932 (state_token_is_name (p: t1, name: "!fileloc")
1933 || (issrcfile = state_token_is_name (p: t1, name: "!srcfileloc"))))
1934 {
1935 next_state_tokens (depth: 2);
1936 t0 = peek_state_token (depth: 0);
1937 t1 = peek_state_token (depth: 1);
1938 if (state_token_kind (p: t0) == STOK_STRING &&
1939 state_token_kind (p: t1) == STOK_INTEGER)
1940 {
1941 char *path = t0->stok_un.stok_string;
1942 if (issrcfile)
1943 {
1944 static const char dirsepstr[2] = { DIR_SEPARATOR, (char) 0 };
1945 char *fullpath = concat (srcdir, dirsepstr, path, NULL);
1946 floc->file = input_file_by_name (name: fullpath);
1947 free (ptr: fullpath);
1948 }
1949 else
1950 floc->file = input_file_by_name (name: path);
1951 floc->line = t1->stok_un.stok_num;
1952 next_state_tokens (depth: 2);
1953 }
1954 else
1955 fatal_reading_state (tok: t0,
1956 msg: "Bad fileloc syntax, expected path string and line");
1957 t0 = peek_state_token (depth: 0);
1958 if (state_token_kind (p: t0) != STOK_RIGHTPAR)
1959 fatal_reading_state (tok: t0, msg: "Bad fileloc syntax, expected )");
1960 next_state_tokens (depth: 1);
1961 }
1962 else if (state_token_is_name (p: t0, name: "nil"))
1963 {
1964 next_state_tokens (depth: 1);
1965 floc->file = NULL;
1966 floc->line = 0;
1967 }
1968 else
1969 fatal_reading_state (tok: t0, msg: "Bad fileloc syntax");
1970}
1971
1972
1973/* Read the fields of a GTY-ed type. */
1974void
1975read_state_fields (pair_p *fields)
1976{
1977 pair_p tmp = NULL;
1978 struct state_token_st *t0 = peek_state_token (depth: 0);
1979 struct state_token_st *t1 = peek_state_token (depth: 1);
1980 struct state_token_st *t2 = peek_state_token (depth: 2);
1981
1982 if (state_token_kind (p: t0) == STOK_LEFTPAR
1983 && state_token_is_name (p: t1, name: "!fields")
1984 && state_token_kind (p: t2) == STOK_INTEGER)
1985 {
1986 int nbfields = t2->stok_un.stok_num;
1987 int nbpairs = 0;
1988 next_state_tokens (depth: 3);
1989 nbpairs = read_state_pair_list (list: &tmp);
1990 t0 = peek_state_token (depth: 0);
1991 if (nbpairs != nbfields)
1992 fatal_reading_state_printf
1993 (t0,
1994 "Mismatched fields number, expected %d got %d", nbpairs, nbfields);
1995 if (state_token_kind (p: t0) == STOK_RIGHTPAR)
1996 next_state_tokens (depth: 1);
1997 else
1998 fatal_reading_state (tok: t0, msg: "Bad fields expecting )");
1999 }
2000
2001 *fields = tmp;
2002}
2003
2004
2005/* Read a string option. */
2006static void
2007read_state_string_option (options_p opt)
2008{
2009 struct state_token_st *t0 = peek_state_token (depth: 0);
2010 opt->kind = OPTION_STRING;
2011 if (state_token_kind (p: t0) == STOK_STRING)
2012 {
2013 opt->info.string = xstrdup (t0->stok_un.stok_string);
2014 next_state_tokens (depth: 1);
2015 }
2016 else if (state_token_is_name (p: t0, name: "nil"))
2017 {
2018 opt->info.string = NULL;
2019 next_state_tokens (depth: 1);
2020 }
2021 else
2022 fatal_reading_state (tok: t0, msg: "Missing name in string option");
2023}
2024
2025
2026/* Read a type option. */
2027static void
2028read_state_type_option (options_p opt)
2029{
2030 opt->kind = OPTION_TYPE;
2031 read_state_type (current: &(opt->info.type));
2032}
2033
2034
2035/* Read a nested option. */
2036static void
2037read_state_nested_option (options_p opt)
2038{
2039 struct state_token_st *t0;
2040
2041 opt->info.nested = XCNEW (struct nested_ptr_data);
2042 opt->kind = OPTION_NESTED;
2043 read_state_type (current: &(opt->info.nested->type));
2044 t0 = peek_state_token (depth: 0);
2045 if (state_token_kind (p: t0) == STOK_STRING)
2046 {
2047 opt->info.nested->convert_from = xstrdup (t0->stok_un.stok_string);
2048 next_state_tokens (depth: 1);
2049 }
2050 else if (state_token_is_name (p: t0, name: "nil"))
2051 {
2052 opt->info.nested->convert_from = NULL;
2053 next_state_tokens (depth: 1);
2054 }
2055 else
2056 fatal_reading_state (tok: t0, msg: "Bad nested convert_from option");
2057
2058 t0 = peek_state_token (depth: 0);
2059 if (state_token_kind (p: t0) == STOK_STRING)
2060 {
2061 opt->info.nested->convert_to = xstrdup (t0->stok_un.stok_string);
2062 next_state_tokens (depth: 1);
2063 }
2064 else if (state_token_is_name (p: t0, name: "nil"))
2065 {
2066 opt->info.nested->convert_to = NULL;
2067 next_state_tokens (depth: 1);
2068 }
2069 else
2070 fatal_reading_state (tok: t0, msg: "Bad nested convert_from option");
2071}
2072
2073
2074/* Read an GTY option. */
2075static void
2076read_state_option (options_p *opt)
2077{
2078 struct state_token_st *t0 = peek_state_token (depth: 0);
2079 struct state_token_st *t1 = peek_state_token (depth: 1);
2080
2081 if (state_token_kind (p: t0) == STOK_LEFTPAR &&
2082 state_token_is_name (p: t1, name: "!option"))
2083 {
2084 next_state_tokens (depth: 2);
2085 t0 = peek_state_token (depth: 0);
2086 if (state_token_kind (p: t0) == STOK_NAME)
2087 {
2088 *opt = XCNEW (struct options);
2089 if (state_token_is_name (p: t0, name: "nil"))
2090 (*opt)->name = NULL;
2091 else
2092 (*opt)->name = t0->stok_un.stok_ident->stid_name;
2093 next_state_tokens (depth: 1);
2094 t0 = peek_state_token (depth: 0);
2095 if (state_token_kind (p: t0) == STOK_NAME)
2096 {
2097 if (state_token_is_name (p: t0, name: "string"))
2098 {
2099 next_state_tokens (depth: 1);
2100 read_state_string_option (opt: *opt);
2101 }
2102 else if (state_token_is_name (p: t0, name: "type"))
2103 {
2104 next_state_tokens (depth: 1);
2105 read_state_type_option (opt: *opt);
2106 }
2107 else if (state_token_is_name (p: t0, name: "nested"))
2108 {
2109 next_state_tokens (depth: 1);
2110 read_state_nested_option (opt: *opt);
2111 }
2112 else
2113 fatal_reading_state (tok: t0, msg: "Bad option type");
2114 t0 = peek_state_token (depth: 0);
2115 if (state_token_kind (p: t0) != STOK_RIGHTPAR)
2116 fatal_reading_state (tok: t0, msg: "Bad syntax in option, expecting )");
2117
2118 next_state_tokens (depth: 1);
2119 }
2120 else
2121 fatal_reading_state (tok: t0, msg: "Missing option type");
2122 }
2123 else
2124 fatal_reading_state (tok: t0, msg: "Bad name for option");
2125 }
2126 else
2127 fatal_reading_state (tok: t0, msg: "Bad option, waiting for )");
2128}
2129
2130/* Read a list of options. */
2131void
2132read_state_options (options_p *opt)
2133{
2134 options_p head = NULL;
2135 options_p previous = NULL;
2136 options_p current_option = NULL;
2137 struct state_token_st *t0 = peek_state_token (depth: 0);
2138 struct state_token_st *t1 = peek_state_token (depth: 1);
2139
2140 if (state_token_kind (p: t0) == STOK_LEFTPAR &&
2141 state_token_is_name (p: t1, name: "!options"))
2142 {
2143 next_state_tokens (depth: 2);
2144 t0 = peek_state_token (depth: 0);
2145 while (state_token_kind (p: t0) != STOK_RIGHTPAR)
2146 {
2147 read_state_option (opt: &current_option);
2148 if (head == NULL)
2149 {
2150 head = current_option;
2151 previous = head;
2152 }
2153 else
2154 {
2155 previous->next = current_option;
2156 previous = current_option;
2157 }
2158 t0 = peek_state_token (depth: 0);
2159 }
2160 next_state_tokens (depth: 1);
2161 }
2162 else if (state_token_is_name (p: t0, name: "nil"))
2163 {
2164 next_state_tokens (depth: 1);
2165 }
2166 else
2167 fatal_reading_state (tok: t0, msg: "Bad options syntax");
2168
2169 *opt = head;
2170}
2171
2172
2173/* Read a version, and check against the version of the gengtype. */
2174static void
2175read_state_version (const char *ver_string)
2176{
2177 struct state_token_st *t0 = peek_state_token (depth: 0);
2178 struct state_token_st *t1 = peek_state_token (depth: 1);
2179
2180 if (state_token_kind (p: t0) == STOK_LEFTPAR &&
2181 state_token_is_name (p: t1, name: "!version"))
2182 {
2183 next_state_tokens (depth: 2);
2184 t0 = peek_state_token (depth: 0);
2185 t1 = peek_state_token (depth: 1);
2186 if (state_token_kind (p: t0) == STOK_STRING &&
2187 state_token_kind (p: t1) == STOK_RIGHTPAR)
2188 {
2189 /* Check that the read version string is the same as current
2190 version. */
2191 if (strcmp (s1: ver_string, s2: t0->stok_un.stok_string))
2192 fatal_reading_state_printf (t0,
2193 "version string mismatch; expecting %s but got %s",
2194 ver_string,
2195 t0->stok_un.stok_string);
2196 next_state_tokens (depth: 2);
2197 }
2198 else
2199 fatal_reading_state (tok: t0, msg: "Missing version or right parenthesis");
2200 }
2201 else
2202 fatal_reading_state (tok: t0, msg: "Bad version syntax");
2203}
2204
2205
2206/* Read a pair. */
2207void
2208read_state_pair (pair_p *current)
2209{
2210 struct state_token_st *t0 = peek_state_token (depth: 0);
2211 struct state_token_st *t1 = peek_state_token (depth: 1);
2212 if (state_token_kind (p: t0) == STOK_LEFTPAR &&
2213 state_token_is_name (p: t1, name: "!pair"))
2214 {
2215 *current = XCNEW (struct pair);
2216 next_state_tokens (depth: 2);
2217 t0 = peek_state_token (depth: 0);
2218 if (state_token_kind (p: t0) == STOK_STRING)
2219 {
2220 if (strcmp (s1: t0->stok_un.stok_string, s2: "nil") == 0)
2221 {
2222 (*current)->name = NULL;
2223 }
2224 else
2225 {
2226 (*current)->name = xstrdup (t0->stok_un.stok_string);
2227 }
2228 next_state_tokens (depth: 1);
2229 read_state_type (current: &((*current)->type));
2230 read_state_fileloc (floc: &((*current)->line));
2231 read_state_options (opt: &((*current)->opt));
2232 t0 = peek_state_token (depth: 0);
2233 if (state_token_kind (p: t0) == STOK_RIGHTPAR)
2234 {
2235 next_state_tokens (depth: 1);
2236 }
2237 else
2238 {
2239 fatal_reading_state (tok: t0, msg: "Bad syntax for pair, )");
2240 }
2241 }
2242 else
2243 {
2244 fatal_reading_state (tok: t0, msg: "Bad name for pair");
2245 }
2246 }
2247 else if (state_token_kind (p: t0) == STOK_NAME &&
2248 state_token_is_name (p: t0, name: "nil"))
2249 {
2250 next_state_tokens (depth: 1);
2251 *current = NULL;
2252 }
2253 else
2254 fatal_reading_state_printf (t0, "Bad syntax for pair, (!pair %d",
2255 state_token->stok_kind);
2256}
2257
2258
2259/* Return the number of pairs actually read. */
2260int
2261read_state_pair_list (pair_p *list)
2262{
2263 int nbpair = 0;
2264 pair_p head = NULL;
2265 pair_p previous = NULL;
2266 pair_p tmp = NULL;
2267 struct state_token_st *t0 = peek_state_token (depth: 0);
2268 while (t0 && state_token_kind (p: t0) != STOK_RIGHTPAR)
2269 {
2270 read_state_pair (current: &tmp);
2271 if (head == NULL)
2272 {
2273 head = tmp;
2274 previous = head;
2275 }
2276 else
2277 {
2278 previous->next = tmp;
2279 previous = tmp;
2280 }
2281 t0 = peek_state_token (depth: 0);
2282 nbpair++;
2283 }
2284
2285 /* don't consume the ); the caller will eat it. */
2286 *list = head;
2287 return nbpair;
2288}
2289
2290/* Read the typedefs. */
2291static void
2292read_state_typedefs (pair_p *typedefs)
2293{
2294 int nbtypedefs = 0;
2295 pair_p list = NULL;
2296 struct state_token_st *t0 = peek_state_token (depth: 0);
2297 struct state_token_st *t1 = peek_state_token (depth: 1);
2298 struct state_token_st *t2 = peek_state_token (depth: 2);
2299
2300 if (state_token_kind (p: t0) == STOK_LEFTPAR
2301 && state_token_is_name (p: t1, name: "!typedefs")
2302 && state_token_kind (p: t2) == STOK_INTEGER)
2303 {
2304 int nbpairs = 0;
2305 nbtypedefs = t2->stok_un.stok_num;
2306 next_state_tokens (depth: 3);
2307 nbpairs = read_state_pair_list (list: &list);
2308 t0 = peek_state_token (depth: 0);
2309 if (nbpairs != nbtypedefs)
2310 fatal_reading_state_printf
2311 (t0,
2312 "invalid number of typedefs, expected %d but got %d",
2313 nbtypedefs, nbpairs);
2314 if (state_token_kind (p: t0) == STOK_RIGHTPAR)
2315 next_state_tokens (depth: 1);
2316 else
2317 fatal_reading_state (tok: t0, msg: "Bad typedefs syntax )");
2318 }
2319 else
2320 fatal_reading_state (tok: t0, msg: "Bad typedefs syntax (!typedefs");
2321
2322 if (verbosity_level >= 2)
2323 printf (format: "%s read %d typedefs from state\n", progname, nbtypedefs);
2324 *typedefs = list;
2325}
2326
2327
2328/* Read the structures. */
2329static void
2330read_state_structures (type_p *structures)
2331{
2332 type_p head = NULL;
2333 type_p previous = NULL;
2334 type_p tmp;
2335 int nbstruct = 0, countstruct = 0;
2336 struct state_token_st *t0 = peek_state_token (depth: 0);
2337 struct state_token_st *t1 = peek_state_token (depth: 1);
2338 struct state_token_st *t2 = peek_state_token (depth: 2);
2339
2340 if (state_token_kind (p: t0) == STOK_LEFTPAR
2341 && state_token_is_name (p: t1, name: "!structures")
2342 && state_token_kind (p: t2) == STOK_INTEGER)
2343 {
2344 nbstruct = t2->stok_un.stok_num;
2345 next_state_tokens (depth: 3);
2346 t0 = peek_state_token (depth: 0);
2347 while (t0 && state_token_kind (p: t0) != STOK_RIGHTPAR)
2348 {
2349 tmp = NULL;
2350 read_state_type (current: &tmp);
2351 countstruct++;
2352 if (head == NULL)
2353 {
2354 head = tmp;
2355 previous = head;
2356 }
2357 else
2358 {
2359 previous->next = tmp;
2360 previous = tmp;
2361 }
2362 t0 = peek_state_token (depth: 0);
2363 }
2364 next_state_tokens (depth: 1);
2365 }
2366 else
2367 fatal_reading_state (tok: t0, msg: "Bad structures syntax");
2368 if (countstruct != nbstruct)
2369 fatal_reading_state_printf (NULL_STATE_TOKEN,
2370 "expected %d structures but got %d",
2371 nbstruct, countstruct);
2372 if (verbosity_level >= 2)
2373 printf (format: "%s read %d structures from state\n", progname, nbstruct);
2374 *structures = head;
2375}
2376
2377
2378/* Read the variables. */
2379static void
2380read_state_variables (pair_p *variables)
2381{
2382 pair_p list = NULL;
2383 int nbvars = 0;
2384 struct state_token_st *t0 = peek_state_token (depth: 0);
2385 struct state_token_st *t1 = peek_state_token (depth: 1);
2386 struct state_token_st *t2 = peek_state_token (depth: 2);
2387
2388 if (state_token_kind (p: t0) == STOK_LEFTPAR
2389 && state_token_is_name (p: t1, name: "!variables")
2390 && state_token_kind (p: t2) == STOK_INTEGER)
2391 {
2392 int nbpairs = 0;
2393 nbvars = t2->stok_un.stok_num;
2394 next_state_tokens (depth: 3);
2395 nbpairs = read_state_pair_list (list: &list);
2396 t0 = peek_state_token (depth: 0);
2397 if (nbpairs != nbvars)
2398 fatal_reading_state_printf
2399 (t0, "Invalid number of variables, expected %d but got %d",
2400 nbvars, nbpairs);
2401 if (state_token_kind (p: t0) == STOK_RIGHTPAR)
2402 next_state_tokens (depth: 1);
2403 else
2404 fatal_reading_state (tok: t0, msg: "Waiting for ) in variables");
2405 }
2406 else
2407 fatal_reading_state (tok: t0, msg: "Bad variables syntax");
2408 *variables = list;
2409 if (verbosity_level >= 2)
2410 printf (format: "%s read %d variables from state\n", progname, nbvars);
2411}
2412
2413
2414/* Read the source directory. */
2415static void
2416read_state_srcdir (void)
2417{
2418 struct state_token_st *t0 = peek_state_token (depth: 0);
2419 struct state_token_st *t1 = peek_state_token (depth: 1);
2420 if (state_token_kind (p: t0) == STOK_LEFTPAR &&
2421 state_token_is_name (p: t1, name: "!srcdir"))
2422 {
2423 next_state_tokens (depth: 2);
2424 t0 = peek_state_token (depth: 0);
2425 t1 = peek_state_token (depth: 1);
2426 if (state_token_kind (p: t0) == STOK_STRING &&
2427 state_token_kind (p: t1) == STOK_RIGHTPAR)
2428 {
2429 srcdir = xstrdup (t0->stok_un.stok_string);
2430 srcdir_len = strlen (s: srcdir);
2431 next_state_tokens (depth: 2);
2432 return;
2433 }
2434 }
2435
2436 fatal_reading_state (tok: t0, msg: "Bad srcdir in state_file");
2437}
2438
2439
2440/* Read the sequence of GCC front-end languages. */
2441static void
2442read_state_languages (void)
2443{
2444 struct state_token_st *t0 = peek_state_token (depth: 0);
2445 struct state_token_st *t1 = peek_state_token (depth: 1);
2446 struct state_token_st *t2 = peek_state_token (depth: 2);
2447 if (state_token_kind (p: t0) == STOK_LEFTPAR
2448 && state_token_is_name (p: t1, name: "!languages")
2449 && state_token_kind (p: t2) == STOK_INTEGER)
2450 {
2451 int i = 0;
2452 num_lang_dirs = t2->stok_un.stok_num;
2453 lang_dir_names = XCNEWVEC (const char *, num_lang_dirs);
2454 next_state_tokens (depth: 3);
2455 t0 = t1 = t2 = NULL;
2456 for (i = 0; i < (int) num_lang_dirs; i++)
2457 {
2458 t0 = peek_state_token (depth: 0);
2459 if (state_token_kind (p: t0) != STOK_NAME)
2460 fatal_reading_state (tok: t0, msg: "expecting language name in state file");
2461 lang_dir_names[i] = t0->stok_un.stok_ident->stid_name;
2462 next_state_tokens (depth: 1);
2463 }
2464 t0 = peek_state_token (depth: 0);
2465 if (state_token_kind (p: t0) != STOK_RIGHTPAR)
2466 fatal_reading_state (tok: t0, msg: "missing ) in languages list of state file");
2467 next_state_tokens (depth: 1);
2468 }
2469 else
2470 fatal_reading_state (tok: t0, msg: "expecting languages list in state file");
2471
2472}
2473
2474/* Read the sequence of files. */
2475static void
2476read_state_files_list (void)
2477{
2478 struct state_token_st *t0 = peek_state_token (depth: 0);
2479 struct state_token_st *t1 = peek_state_token (depth: 1);
2480 struct state_token_st *t2 = peek_state_token (depth: 2);
2481 struct state_token_st *t3 = peek_state_token (depth: 3);
2482
2483 if (state_token_kind (p: t0) == STOK_LEFTPAR
2484 && state_token_is_name (p: t1, name: "!fileslist")
2485 && state_token_kind (p: t2) == STOK_INTEGER
2486 && state_token_kind (p: t3) == STOK_INTEGER)
2487 {
2488 int i = 0, j = 0;
2489 num_gt_files = t2->stok_un.stok_num;
2490 num_build_headers = t3->stok_un.stok_num;
2491 next_state_tokens (depth: 4);
2492 t0 = t1 = t2 = t3 = NULL;
2493 gt_files = XCNEWVEC (const input_file *, num_gt_files);
2494 build_headers = XCNEWVEC (const char *, num_build_headers);
2495 for (i = 0; i < (int) num_gt_files; i++)
2496 {
2497 bool issrcfile = false;
2498 t0 = t1 = t2 = NULL;
2499 t0 = peek_state_token (depth: 0);
2500 t1 = peek_state_token (depth: 1);
2501 t2 = peek_state_token (depth: 2);
2502 if (state_token_kind (p: t0) == STOK_LEFTPAR
2503 && (state_token_is_name (p: t1, name: "!file")
2504 || (issrcfile = state_token_is_name (p: t1, name: "!srcfile")))
2505 && state_token_kind (p: t2) == STOK_INTEGER)
2506 {
2507 lang_bitmap bmap = t2->stok_un.stok_num;
2508 next_state_tokens (depth: 3);
2509 t0 = t1 = t2 = NULL;
2510 t0 = peek_state_token (depth: 0);
2511 t1 = peek_state_token (depth: 1);
2512 if (state_token_kind (p: t0) == STOK_STRING
2513 && state_token_kind (p: t1) == STOK_RIGHTPAR)
2514 {
2515 const char *fnam = t0->stok_un.stok_string;
2516 /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
2517 input_file *curgt = NULL;
2518 if (issrcfile)
2519 {
2520 static const char dirsepstr[2] =
2521 { DIR_SEPARATOR, (char) 0 };
2522 char *fullpath = concat (srcdir, dirsepstr, fnam, NULL);
2523 curgt = input_file_by_name (name: fullpath);
2524 free (ptr: fullpath);
2525 }
2526 else
2527 {
2528 curgt = input_file_by_name (name: fnam);
2529 /* Look for a header file created during the build,
2530 which looks like "./<filename>.h". */
2531 int len = strlen (s: fnam);
2532 if (len >= 5
2533 && fnam[0] == '.'
2534 && IS_DIR_SEPARATOR (fnam[1])
2535 && fnam[len-2] == '.'
2536 && fnam[len-1] == 'h')
2537 {
2538 char *buf = (char *) xmalloc (len - 1);
2539 /* Strip the leading "./" from the filename. */
2540 strcpy (dest: buf, src: &fnam[2]);
2541 build_headers[j++] = buf;
2542 }
2543 }
2544 set_lang_bitmap (inpf: curgt, n: bmap);
2545 gt_files[i] = curgt;
2546 next_state_tokens (depth: 2);
2547 }
2548 else
2549 fatal_reading_state (tok: t0,
2550 msg: "bad file in !fileslist of state file");
2551 }
2552 else
2553 fatal_reading_state (tok: t0,
2554 msg: "expecting file in !fileslist of state file");
2555 };
2556 t0 = peek_state_token (depth: 0);
2557 if (state_token_kind (p: t0) != STOK_RIGHTPAR)
2558 fatal_reading_state (tok: t0, msg: "missing ) for !fileslist in state file");
2559 next_state_tokens (depth: 1);
2560 }
2561 else
2562 fatal_reading_state (tok: t0, msg: "missing !fileslist in state file");
2563}
2564
2565
2566/* Read the trailer. */
2567static void
2568read_state_trailer (void)
2569{
2570 struct state_token_st *t0 = peek_state_token (depth: 0);
2571 struct state_token_st *t1 = peek_state_token (depth: 1);
2572 struct state_token_st *t2 = peek_state_token (depth: 2);
2573
2574 if (state_token_kind (p: t0) == STOK_LEFTPAR
2575 && state_token_is_name (p: t1, name: "!endfile")
2576 && state_token_kind (p: t2) == STOK_RIGHTPAR)
2577 next_state_tokens (depth: 3);
2578 else
2579 fatal_reading_state (tok: t0, msg: "missing !endfile in state file");
2580}
2581
2582
2583/* Utility functions for the state_seen_types hash table. */
2584static unsigned
2585hash_type_number (const void *ty)
2586{
2587 const struct type *type = (const struct type *) ty;
2588
2589 return type->state_number;
2590}
2591
2592static int
2593equals_type_number (const void *ty1, const void *ty2)
2594{
2595 const struct type *type1 = (const struct type *) ty1;
2596 const struct type *type2 = (const struct type *) ty2;
2597
2598 return type1->state_number == type2->state_number;
2599}
2600
2601
2602/* The function reading the state, called by main from gengtype.cc. */
2603void
2604read_state (const char *path)
2605{
2606 state_file = fopen (path, "r");
2607 if (state_file == NULL)
2608 fatal ("Failed to open state file %s for reading [%s]", path,
2609 xstrerror (errno));
2610 state_path = path;
2611 state_line = 1;
2612
2613 if (verbosity_level >= 1)
2614 {
2615 printf (format: "%s reading state file %s;", progname, state_path);
2616 if (verbosity_level >= 2)
2617 putchar ('\n');
2618 fflush (stdout);
2619 }
2620
2621 state_seen_types =
2622 htab_create (2017, hash_type_number, equals_type_number, NULL);
2623 state_ident_tab =
2624 htab_create (4027, htab_hash_string, htab_eq_string, NULL);
2625 read_state_version (version_string);
2626 read_state_srcdir ();
2627 read_state_languages ();
2628 read_state_files_list ();
2629 read_state_structures (structures: &structures);
2630 if (ferror (state_file))
2631 fatal_reading_state_printf
2632 (NULL_STATE_TOKEN, "input error while reading state [%s]",
2633 xstrerror (errno));
2634 read_state_typedefs (typedefs: &typedefs);
2635 read_state_variables (variables: &variables);
2636 read_state_trailer ();
2637
2638 if (verbosity_level >= 1)
2639 {
2640 printf (format: "%s read %ld bytes.\n", progname, ftell (stream: state_file));
2641 fflush (stdout);
2642 };
2643
2644 if (fclose (stream: state_file))
2645 fatal ("failed to close read state file %s [%s]",
2646 path, xstrerror (errno));
2647 state_file = NULL;
2648 state_path = NULL;
2649}
2650
2651/* End of file gengtype-state.cc. */
2652

source code of gcc/gengtype-state.cc