[RS6000] Power10 ICE running gcc.target/powerpc/ppc-ne0-1.c
[official-gcc.git] / gcc / gengtype-state.c
blobd1d54e9de7d6519408dc777ee9e4193223db07ff
1 /* Gengtype persistent state serialization & de-serialization.
2 Useful for gengtype in plugin mode.
4 Copyright (C) 2010-2020 Free Software Foundation, Inc.
6 This file is part of GCC.
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.
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.
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/>.
22 Contributed by Jeremie Salvucci <jeremie.salvucci@free.fr>
23 and Basile Starynkevitch <basile@starynkevitch.net>
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"
40 /* Gives the file location of a type, if any. */
41 static inline struct fileloc*
42 type_lineloc (const_type_p ty)
44 if (!ty)
45 return NULL;
46 switch (ty->kind)
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 return NULL;
61 default:
62 gcc_unreachable ();
66 /* The state file has simplistic lispy lexical tokens. Its lexer gives
67 a linked list of struct state_token_st, through the peek_state_token
68 function. Lexical tokens are consumed with next_state_tokens. */
71 /* The lexical kind of each lispy token. */
72 enum state_token_en
74 STOK_NONE, /* Never used. */
75 STOK_INTEGER, /* Integer token. */
76 STOK_STRING, /* String token. */
77 STOK_LEFTPAR, /* Left opening parenthesis. */
78 STOK_RIGHTPAR, /* Right closing parenthesis. */
79 STOK_NAME /* hash-consed name or identifier. */
83 /* Structure and hash-table used to share identifiers or names. */
84 struct state_ident_st
86 /* TODO: We could improve the parser by reserving identifiers for
87 state keywords and adding a keyword number for them. That would
88 mean adding another field in this state_ident_st struct. */
89 char stid_name[1]; /* actually bigger & null terminated */
91 static htab_t state_ident_tab;
94 /* The state_token_st structure is for lexical tokens in the read
95 state file. The stok_kind field discriminates the union. Tokens
96 are allocated by peek_state_token which calls read_a_state_token
97 which allocate them. Tokens are freed by calls to
98 next_state_tokens. Token are organized in a FIFO look-ahead queue
99 filled by peek_state_token. */
100 struct state_token_st
102 enum state_token_en stok_kind; /* the lexical kind
103 discriminates the stok_un
104 union */
105 int stok_line; /* the line number */
106 int stok_col; /* the column number */
107 const char *stok_file; /* the file path */
108 struct state_token_st *stok_next; /* the next token in the
109 queue, when peeked */
110 union /* discriminated by stok_kind! */
112 int stok_num; /* when STOK_INTEGER */
113 char stok_string[1]; /* when STOK_STRING, actual size is
114 bigger and null terminated */
115 struct state_ident_st *stok_ident; /* when STOK_IDENT */
116 void *stok_ptr; /* null otherwise */
118 stok_un;
124 #define NULL_STATE_TOKEN (struct state_token_st*)0
126 /* the state_token pointer contains the leftmost current token. The
127 tokens are organized in a linked queue, using stok_next, for token
128 look-ahead. */
129 struct state_token_st *state_token = NULL_STATE_TOKEN;
131 /* Used by the reading lexer. */
132 static FILE *state_file;
133 static const char *state_path = NULL;
134 static int state_line = 0;
135 static long state_bol = 0; /* offset of beginning of line */
137 /* A class for writing out s-expressions, keeping track of newlines and
138 nested indentation. */
139 class s_expr_writer
141 public:
142 s_expr_writer ();
144 void write_new_line ();
145 void write_any_indent (int leading_spaces);
147 void begin_s_expr (const char *tag);
148 void end_s_expr ();
150 private:
151 int m_indent_amount;
152 int m_had_recent_newline;
153 }; // class s_expr_writer
155 /* A class for writing out "gtype.state". */
156 class state_writer : public s_expr_writer
158 public:
159 state_writer ();
161 private:
162 void write_state_fileloc (struct fileloc *floc);
163 void write_state_fields (pair_p fields);
164 void write_state_a_string (const char *s);
165 void write_state_string_option (options_p current);
166 void write_state_type_option (options_p current);
167 void write_state_nested_option (options_p current);
168 void write_state_option (options_p current);
169 void write_state_options (options_p opt);
170 void write_state_lang_bitmap (lang_bitmap bitmap);
171 void write_state_version (const char *version);
172 void write_state_scalar_type (type_p current);
173 void write_state_string_type (type_p current);
174 void write_state_undefined_type (type_p current);
175 void write_state_struct_union_type (type_p current, const char *kindstr);
176 void write_state_struct_type (type_p current);
177 void write_state_user_struct_type (type_p current);
178 void write_state_union_type (type_p current);
179 void write_state_lang_struct_type (type_p current);
180 void write_state_pointer_type (type_p current);
181 void write_state_array_type (type_p current);
182 void write_state_gc_used (enum gc_used_enum gus);
183 void write_state_common_type_content (type_p current);
184 void write_state_type (type_p current);
185 void write_state_pair (pair_p current);
186 int write_state_pair_list (pair_p list);
187 void write_state_typedefs (void);
188 void write_state_structures (void);
189 void write_state_variables (void);
190 void write_state_srcdir (void);
191 void write_state_files_list (void);
192 void write_state_languages (void);
194 friend void write_state (const char *state_path);
196 private:
197 /* Counter of written types. */
198 int m_state_written_type_count;
199 }; // class state_writer
202 /* class s_expr_writer's trivial constructor. */
203 s_expr_writer::s_expr_writer ()
204 : m_indent_amount (0),
205 m_had_recent_newline (0)
209 /* Write a newline to the output file, merging adjacent newlines. */
210 void
211 s_expr_writer::write_new_line (void)
213 /* Don't add a newline if we've just had one. */
214 if (!m_had_recent_newline)
216 fprintf (state_file, "\n");
217 m_had_recent_newline = 1;
221 /* If we've just had a newline, write the indentation amount, potentially
222 omitting some spaces.
224 LEADING_SPACES exists to support code that writes strings with leading
225 spaces (e.g " foo") which might occur within a line, or could be the first
226 thing on a line. By passing leading_spaces == 1, when such a string is the
227 first thing on a line, write_any_indent () swallows the successive
228 leading spaces into the indentation so that the "foo" begins at the expected
229 column. */
230 void
231 s_expr_writer::write_any_indent (int leading_spaces)
233 int i;
234 int amount = m_indent_amount - leading_spaces;
235 if (m_had_recent_newline)
236 for (i = 0; i < amount; i++)
237 fprintf (state_file, " ");
238 m_had_recent_newline = 0;
241 /* Write the beginning of a new s-expresion e.g. "(!foo "
242 The writer automatically adds whitespace to show the hierarchical
243 structure of the expressions, so each one starts on a new line,
244 and any within it will be at an increased indentation level. */
245 void
246 s_expr_writer::begin_s_expr (const char *tag)
248 write_new_line ();
249 write_any_indent (0);
250 fprintf (state_file, "(!%s ", tag);
251 m_indent_amount++;
254 /* Write out the end of an s-expression: any necssessary indentation,
255 a closing parenthesis, and a new line. */
256 void
257 s_expr_writer::end_s_expr (void)
259 m_indent_amount--;
260 write_any_indent (0);
261 fprintf (state_file, ")");
262 write_new_line ();
266 /* class state_writer's trivial constructor. */
267 state_writer::state_writer ()
268 : s_expr_writer (),
269 m_state_written_type_count (0)
274 /* Fatal error messages when reading the state. They are extremely
275 unlikely, and only appear when this gengtype-state.c file is buggy,
276 or when reading a gengtype state which was not generated by the
277 same version of gengtype or GCC. */
280 /* Fatal message while reading state. */
281 static void
282 fatal_reading_state (struct state_token_st* tok, const char*msg)
284 if (tok)
285 fatal ("%s:%d:%d: Invalid state file; %s",
286 tok->stok_file, tok->stok_line, tok->stok_col,
287 msg);
288 else
289 fatal ("%s:%d: Invalid state file; %s",
290 state_path, state_line, msg);
294 /* Fatal printf-like message while reading state. This can't be a
295 function, because there is no way to pass a va_arg to a variant of
296 fatal. */
297 #define fatal_reading_state_printf(Tok,Fmt,...) do { \
298 struct state_token_st* badtok = Tok; \
299 if (badtok) \
300 fatal ("%s:%d:%d: Invalid state file; " Fmt, \
301 badtok->stok_file, \
302 badtok->stok_line, \
303 badtok->stok_col, __VA_ARGS__); \
304 else \
305 fatal ("%s:%d: Invalid state file; " Fmt, \
306 state_path, state_line, __VA_ARGS__); \
307 } while (0)
310 /* Find or allocate an identifier in our name hash table. */
311 static struct state_ident_st *
312 state_ident_by_name (const char *name, enum insert_option optins)
314 PTR *slot = NULL;
315 int namlen = 0;
316 struct state_ident_st *stid = NULL;
318 if (!name || !name[0])
319 return NULL;
321 slot = htab_find_slot (state_ident_tab, name, optins);
322 if (!slot)
323 return NULL;
325 namlen = strlen (name);
326 stid =
327 (struct state_ident_st *) xmalloc (sizeof (struct state_ident_st) +
328 namlen);
329 memset (stid, 0, sizeof (struct state_ident_st) + namlen);
330 strcpy (stid->stid_name, name);
331 *slot = stid;
333 return stid;
336 /* Our token lexer is heavily inspired by MELT's lexer, and share some
337 code with the file gcc/melt-runtime.c of the GCC MELT branch! We
338 really want the gengtype state to be easily parsable by MELT. This
339 is a usual lispy lexing routine, dealing with spaces and comments,
340 numbers, parenthesis, names, strings. */
341 static struct state_token_st *
342 read_a_state_token (void)
344 int c = 0;
345 long curoff = 0;
346 struct state_token_st *tk = NULL;
348 again: /* Read again, e.g. after a comment or spaces. */
349 c = getc (state_file);
350 if (c == EOF)
351 return NULL;
353 /* Handle spaces, count lines. */
354 if (c == '\n')
356 state_line++;
357 state_bol = curoff = ftell (state_file);
358 goto again;
360 if (ISSPACE (c))
361 goto again;
362 /* Skip comments starting with semi-colon. */
363 if (c == ';')
367 c = getc (state_file);
369 while (c > 0 && c != '\n');
370 if (c == '\n')
372 state_line++;
373 state_bol = curoff = ftell (state_file);
375 goto again;
377 /* Read signed numbers. */
378 if (ISDIGIT (c) || c == '-' || c == '+')
379 { /* number */
380 int n = 0;
381 ungetc (c, state_file);
382 curoff = ftell (state_file);
383 if (fscanf (state_file, "%d", &n) <= 0)
384 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error in number");
385 tk = XCNEW (struct state_token_st);
386 tk->stok_kind = STOK_INTEGER;
387 tk->stok_line = state_line;
388 tk->stok_col = curoff - state_bol;
389 tk->stok_file = state_path;
390 tk->stok_next = NULL;
391 tk->stok_un.stok_num = n;
393 return tk;
395 /* Read an opening left parenthesis. */
396 else if (c == '(')
398 curoff = ftell (state_file);
399 tk = XCNEW (struct state_token_st);
400 tk->stok_kind = STOK_LEFTPAR;
401 tk->stok_line = state_line;
402 tk->stok_col = curoff - state_bol;
403 tk->stok_file = state_path;
404 tk->stok_next = NULL;
406 return tk;
408 /* Read an closing right parenthesis. */
409 else if (c == ')')
411 curoff = ftell (state_file);
412 tk = XCNEW (struct state_token_st);
413 tk->stok_kind = STOK_RIGHTPAR;
414 tk->stok_line = state_line;
415 tk->stok_col = curoff - state_bol;
416 tk->stok_file = state_path;
417 tk->stok_next = NULL;
419 return tk;
421 /* Read identifiers, using an obstack. */
422 else if (ISALPHA (c) || c == '_' || c == '$' || c == '!' || c == '#')
424 struct obstack id_obstack;
425 struct state_ident_st *sid = NULL;
426 char *ids = NULL;
427 obstack_init (&id_obstack);
428 curoff = ftell (state_file);
429 while (ISALNUM (c) || c == '_' || c == '$' || c == '!' || c == '#')
431 obstack_1grow (&id_obstack, c);
432 c = getc (state_file);
433 if (c < 0)
434 break;
436 if (c >= 0)
437 ungetc (c, state_file);
438 obstack_1grow (&id_obstack, (char) 0);
439 ids = XOBFINISH (&id_obstack, char *);
440 sid = state_ident_by_name (ids, INSERT);
441 obstack_free (&id_obstack, NULL);
442 ids = NULL;
443 tk = XCNEW (struct state_token_st);
444 tk->stok_kind = STOK_NAME;
445 tk->stok_line = state_line;
446 tk->stok_col = curoff - state_bol;
447 tk->stok_file = state_path;
448 tk->stok_next = NULL;
449 tk->stok_un.stok_ident = sid;
451 return tk;
453 /* Read a string, dealing with escape sequences a la C! */
454 else if (c == '"')
456 char *cstr = NULL;
457 int cslen = 0;
458 struct obstack bstring_obstack;
459 obstack_init (&bstring_obstack);
460 curoff = ftell (state_file);
461 while ((c = getc (state_file)) != '"' && c >= 0)
463 if (ISPRINT (c) && c != '\\')
464 obstack_1grow (&bstring_obstack, (char) c);
465 else if (ISSPACE (c) && c != '\n')
466 obstack_1grow (&bstring_obstack, (char) c);
467 else if (c == '\\')
469 c = getc (state_file);
470 switch (c)
472 case 'a':
473 obstack_1grow (&bstring_obstack, '\a');
474 c = getc (state_file);
475 break;
476 case 'b':
477 obstack_1grow (&bstring_obstack, '\b');
478 c = getc (state_file);
479 break;
480 case 't':
481 obstack_1grow (&bstring_obstack, '\t');
482 c = getc (state_file);
483 break;
484 case 'n':
485 obstack_1grow (&bstring_obstack, '\n');
486 c = getc (state_file);
487 break;
488 case 'v':
489 obstack_1grow (&bstring_obstack, '\v');
490 c = getc (state_file);
491 break;
492 case 'f':
493 obstack_1grow (&bstring_obstack, '\f');
494 c = getc (state_file);
495 break;
496 case 'r':
497 obstack_1grow (&bstring_obstack, '\r');
498 c = getc (state_file);
499 break;
500 case '"':
501 obstack_1grow (&bstring_obstack, '\"');
502 c = getc (state_file);
503 break;
504 case '\\':
505 obstack_1grow (&bstring_obstack, '\\');
506 c = getc (state_file);
507 break;
508 case ' ':
509 obstack_1grow (&bstring_obstack, ' ');
510 c = getc (state_file);
511 break;
512 case 'x':
514 unsigned int cx = 0;
515 if (fscanf (state_file, "%02x", &cx) > 0 && cx > 0)
516 obstack_1grow (&bstring_obstack, cx);
517 else
518 fatal_reading_state
519 (NULL_STATE_TOKEN,
520 "Lexical error in string hex escape");
521 c = getc (state_file);
522 break;
524 default:
525 fatal_reading_state
526 (NULL_STATE_TOKEN,
527 "Lexical error - unknown string escape");
530 else
531 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error...");
533 if (c != '"')
534 fatal_reading_state (NULL_STATE_TOKEN, "Unterminated string");
535 obstack_1grow (&bstring_obstack, '\0');
536 cstr = XOBFINISH (&bstring_obstack, char *);
537 cslen = strlen (cstr);
538 tk = (struct state_token_st *)
539 xcalloc (sizeof (struct state_token_st) + cslen, 1);
540 tk->stok_kind = STOK_STRING;
541 tk->stok_line = state_line;
542 tk->stok_col = curoff - state_bol;
543 tk->stok_file = state_path;
544 tk->stok_next = NULL;
545 strcpy (tk->stok_un.stok_string, cstr);
546 obstack_free (&bstring_obstack, NULL);
548 return tk;
550 /* Got an unexpected character. */
551 fatal_reading_state_printf
552 (NULL_STATE_TOKEN,
553 "Lexical error at offset %ld - bad character \\%03o = '%c'",
554 ftell (state_file), c, c);
557 /* Used for lexical look-ahead. Retrieves the lexical token of rank
558 DEPTH, starting with 0 when reading the state file. Gives null on
559 end of file. */
560 static struct state_token_st *
561 peek_state_token (int depth)
563 int remdepth = depth;
564 struct state_token_st **ptoken = &state_token;
565 struct state_token_st *tok = NULL;
567 while (remdepth >= 0)
569 if (*ptoken == NULL)
571 *ptoken = tok = read_a_state_token ();
572 if (tok == NULL)
573 return NULL;
575 tok = *ptoken;
576 ptoken = &((*ptoken)->stok_next);
577 remdepth--;
580 return tok;
583 /* Consume the next DEPTH tokens and free them. */
584 static void
585 next_state_tokens (int depth)
587 struct state_token_st *n;
589 while (depth > 0)
591 if (state_token != NULL)
593 n = state_token->stok_next;
594 free (state_token);
595 state_token = n;
597 else
598 fatal_reading_state (NULL_STATE_TOKEN, "Tokens stack empty");
600 depth--;
604 /* Safely retrieve the lexical kind of a token. */
605 static inline enum state_token_en
606 state_token_kind (struct state_token_st *p)
608 if (p == NULL)
609 return STOK_NONE;
610 else
611 return p->stok_kind;
614 /* Test if a token is a given name i.e. an identifier. */
615 static inline bool
616 state_token_is_name (struct state_token_st *p, const char *name)
618 if (p == NULL)
619 return false;
621 if (p->stok_kind != STOK_NAME)
622 return false;
624 return !strcmp (p->stok_un.stok_ident->stid_name, name);
628 /* Following routines are useful for serializing datas.
630 * We want to serialize :
631 * - typedefs list
632 * - structures list
633 * - variables list
635 * So, we have one routine for each kind of data. The main writing
636 * routine is write_state. The main reading routine is
637 * read_state. Most writing routines write_state_FOO have a
638 * corresponding reading routine read_state_FOO. Reading is done in a
639 * recursive descending way, and any read error is fatal.
642 /* When reading the state, we need to remember the previously seen
643 types by their state_number, since GTY-ed types are usually
644 shared. */
645 static htab_t state_seen_types;
647 /* Return the length of a linked list made of pairs. */
648 static int pair_list_length (pair_p list);
650 /* Compute the length of a list of pairs, starting from the first
651 one. */
652 static int
653 pair_list_length (pair_p list)
655 int nbpair = 0;
656 pair_p l = NULL;
657 for (l = list; l; l = l->next)
658 nbpair++;
659 return nbpair;
662 /* Write a file location. Files relative to $(srcdir) are quite
663 frequent and are handled specially. This ensures that two gengtype
664 state file-s produced by gengtype on the same GCC source tree are
665 very similar and can be reasonably compared with diff, even if the
666 two GCC source trees have different absolute paths. */
667 void
668 state_writer::write_state_fileloc (struct fileloc *floc)
671 if (floc != NULL && floc->line > 0)
673 const char *srcrelpath = NULL;
674 gcc_assert (floc->file != NULL);
675 /* Most of the files are inside $(srcdir) so it is worth to
676 handle them specially. */
677 srcrelpath = get_file_srcdir_relative_path (floc->file);
678 if (srcrelpath != NULL)
680 begin_s_expr ("srcfileloc");
681 write_state_a_string (srcrelpath);
683 else
685 begin_s_expr ("fileloc");
686 write_state_a_string (get_input_file_name (floc->file));
688 fprintf (state_file, " %d", floc->line);
689 end_s_expr ();
691 else
692 fprintf (state_file, "nil ");
695 /* Write a list of fields. */
696 void
697 state_writer::write_state_fields (pair_p fields)
699 int nbfields = pair_list_length (fields);
700 int nbpairs = 0;
701 begin_s_expr ("fields");
702 fprintf (state_file, "%d ", nbfields);
703 nbpairs = write_state_pair_list (fields);
704 gcc_assert (nbpairs == nbfields);
705 end_s_expr ();
708 /* Write a null-terminated string in our lexical convention, very
709 similar to the convention of C. */
710 void
711 state_writer::write_state_a_string (const char *s)
713 char c;
715 write_any_indent (1);
717 fputs (" \"", state_file);
718 for (; *s != 0; s++)
720 c = *s;
721 switch (c)
723 case '\a':
724 fputs ("\\a", state_file);
725 break;
726 case '\b':
727 fputs ("\\b", state_file);
728 break;
729 case '\t':
730 fputs ("\\t", state_file);
731 break;
732 case '\n':
733 fputs ("\\n", state_file);
734 break;
735 case '\v':
736 fputs ("\\v", state_file);
737 break;
738 case '\f':
739 fputs ("\\f", state_file);
740 break;
741 case '\r':
742 fputs ("\\r", state_file);
743 break;
744 case '\"':
745 fputs ("\\\"", state_file);
746 break;
747 case '\\':
748 fputs ("\\\\", state_file);
749 break;
750 default:
751 if (ISPRINT (c))
752 putc (c, state_file);
753 else
754 fprintf (state_file, "\\x%02x", (unsigned) c);
757 fputs ("\"", state_file);
760 /* Our option-s have three kinds, each with its writer. */
761 void
762 state_writer::write_state_string_option (options_p current)
764 write_any_indent (0);
765 fprintf (state_file, "string ");
766 if (current->info.string != NULL)
767 write_state_a_string (current->info.string);
768 else
769 fprintf (state_file, " nil ");
772 void
773 state_writer::write_state_type_option (options_p current)
775 write_any_indent (0);
776 fprintf (state_file, "type ");
777 write_state_type (current->info.type);
780 void
781 state_writer::write_state_nested_option (options_p current)
783 write_any_indent (0);
784 fprintf (state_file, "nested ");
785 write_state_type (current->info.nested->type);
786 if (current->info.nested->convert_from != NULL)
787 write_state_a_string (current->info.nested->convert_from);
788 else
790 write_any_indent (1);
791 fprintf (state_file, " nil ");
794 if (current->info.nested->convert_to != NULL)
795 write_state_a_string (current->info.nested->convert_to);
796 else
798 write_any_indent (1);
799 fprintf (state_file, " nil ");
803 void
804 state_writer::write_state_option (options_p current)
806 begin_s_expr ("option");
808 write_any_indent (0);
809 if (current->name != NULL)
810 fprintf (state_file, "%s ", current->name);
811 else
812 fprintf (state_file, "nil ");
814 switch (current->kind)
816 case OPTION_STRING:
817 write_state_string_option (current);
818 break;
819 case OPTION_TYPE:
820 write_state_type_option (current);
821 break;
822 case OPTION_NESTED:
823 write_state_nested_option (current);
824 break;
825 default:
826 fatal ("Option tag unknown");
829 /* Terminate the "option" s-expression. */
830 end_s_expr ();
835 /* Write a list of GTY options. */
836 void
837 state_writer::write_state_options (options_p opt)
839 options_p current;
841 if (opt == NULL)
843 write_any_indent (0);
844 fprintf (state_file, "nil ");
845 return;
848 begin_s_expr ("options");
849 for (current = opt; current != NULL; current = current->next)
850 write_state_option (current);
851 end_s_expr ();
855 /* Write a bitmap representing a set of GCC front-end languages. */
856 void
857 state_writer::write_state_lang_bitmap (lang_bitmap bitmap)
859 write_any_indent (0);
860 fprintf (state_file, "%d ", (int) bitmap);
863 /* Write version information. */
864 void
865 state_writer::write_state_version (const char *version)
867 begin_s_expr ("version");
868 write_state_a_string (version);
869 end_s_expr ();
872 /* Write a scalar type. We have only two of these. */
873 void
874 state_writer::write_state_scalar_type (type_p current)
876 write_any_indent (0);
877 if (current == &scalar_nonchar)
878 fprintf (state_file, "scalar_nonchar ");
879 else if (current == &scalar_char)
880 fprintf (state_file, "scalar_char ");
881 else
882 fatal ("Unexpected type in write_state_scalar_type");
884 write_state_common_type_content (current);
887 /* Write the string type. There is only one such thing! */
888 void
889 state_writer::write_state_string_type (type_p current)
891 if (current == &string_type)
893 write_any_indent (0);
894 fprintf (state_file, "string ");
895 write_state_common_type_content (current);
897 else
898 fatal ("Unexpected type in write_state_string_type");
901 /* Write an undefined type. */
902 void
903 state_writer::write_state_undefined_type (type_p current)
905 DBGPRINTF ("undefined type @ %p #%d '%s'", (void *) current,
906 current->state_number, current->u.s.tag);
907 write_any_indent (0);
908 fprintf (state_file, "undefined ");
909 gcc_assert (current->gc_used == GC_UNUSED);
910 write_state_common_type_content (current);
911 if (current->u.s.tag != NULL)
912 write_state_a_string (current->u.s.tag);
913 else
915 write_any_indent (0);
916 fprintf (state_file, "nil");
919 write_state_fileloc (type_lineloc (current));
923 /* Common code to write structure like types. */
924 void
925 state_writer::write_state_struct_union_type (type_p current,
926 const char *kindstr)
928 DBGPRINTF ("%s type @ %p #%d '%s'", kindstr, (void *) current,
929 current->state_number, current->u.s.tag);
930 write_any_indent (0);
931 fprintf (state_file, "%s ", kindstr);
932 write_state_common_type_content (current);
933 if (current->u.s.tag != NULL)
934 write_state_a_string (current->u.s.tag);
935 else
937 write_any_indent (0);
938 fprintf (state_file, "nil");
941 write_state_fileloc (type_lineloc (current));
942 write_state_fields (current->u.s.fields);
943 write_state_options (current->u.s.opt);
944 write_state_lang_bitmap (current->u.s.bitmap);
948 /* Write a GTY struct type. */
949 void
950 state_writer::write_state_struct_type (type_p current)
952 write_state_struct_union_type (current, "struct");
953 write_state_type (current->u.s.lang_struct);
954 write_state_type (current->u.s.base_class);
957 /* Write a GTY user-defined struct type. */
958 void
959 state_writer::write_state_user_struct_type (type_p current)
961 DBGPRINTF ("user_struct type @ %p #%d '%s'", (void *) current,
962 current->state_number, current->u.s.tag);
963 write_any_indent (0);
964 fprintf (state_file, "user_struct ");
965 write_state_common_type_content (current);
966 if (current->u.s.tag != NULL)
967 write_state_a_string (current->u.s.tag);
968 else
970 write_any_indent (0);
971 fprintf (state_file, "nil");
973 write_state_fileloc (type_lineloc (current));
974 write_state_fields (current->u.s.fields);
977 /* write a GTY union type. */
978 void
979 state_writer::write_state_union_type (type_p current)
981 write_state_struct_union_type (current, "union");
982 write_state_type (current->u.s.lang_struct);
985 /* Write a lang_struct type. This is tricky and was painful to debug,
986 we deal with the next field specifically within their lang_struct
987 subfield, which points to a linked list of homonumous types.
988 Change this function with extreme care, see also
989 read_state_lang_struct_type. */
990 void
991 state_writer::write_state_lang_struct_type (type_p current)
993 int nbhomontype = 0;
994 type_p hty = NULL;
995 const char *homoname = 0;
996 write_state_struct_union_type (current, "lang_struct");
997 /* lang_struct-ures are particularly tricky, since their
998 u.s.lang_struct field gives a list of homonymous struct-s or
999 union-s! */
1000 DBGPRINTF ("lang_struct @ %p #%d", (void *) current, current->state_number);
1001 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
1003 nbhomontype++;
1004 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype,
1005 (void *) hty, hty->state_number, hty->u.s.tag);
1006 /* Every member of the homonymous list should have the same tag. */
1007 gcc_assert (union_or_struct_p (hty));
1008 gcc_assert (hty->u.s.lang_struct == current);
1009 if (!homoname)
1010 homoname = hty->u.s.tag;
1011 gcc_assert (strcmp (homoname, hty->u.s.tag) == 0);
1013 begin_s_expr ("homotypes");
1014 fprintf (state_file, "%d", nbhomontype);
1015 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
1016 write_state_type (hty);
1017 end_s_expr ();
1020 /* Write a pointer type. */
1021 void
1022 state_writer::write_state_pointer_type (type_p current)
1024 write_any_indent (0);
1025 fprintf (state_file, "pointer ");
1026 write_state_common_type_content (current);
1027 write_state_type (current->u.p);
1030 /* Write an array type. */
1031 void
1032 state_writer::write_state_array_type (type_p current)
1034 write_any_indent (0);
1035 fprintf (state_file, "array ");
1036 write_state_common_type_content (current);
1037 if (current->u.a.len != NULL)
1038 write_state_a_string (current->u.a.len);
1039 else
1041 write_any_indent (1);
1042 fprintf (state_file, " nil");
1045 write_any_indent (1);
1046 fprintf (state_file, " ");
1047 write_state_type (current->u.a.p);
1050 /* Write the gc_used information. */
1051 void
1052 state_writer::write_state_gc_used (enum gc_used_enum gus)
1054 write_any_indent (1);
1055 switch (gus)
1057 case GC_UNUSED:
1058 fprintf (state_file, " gc_unused");
1059 break;
1060 case GC_USED:
1061 fprintf (state_file, " gc_used");
1062 break;
1063 case GC_MAYBE_POINTED_TO:
1064 fprintf (state_file, " gc_maybe_pointed_to");
1065 break;
1066 case GC_POINTED_TO:
1067 fprintf (state_file, " gc_pointed_to");
1068 break;
1069 default:
1070 gcc_unreachable ();
1074 /* Utility routine to write the common content of all types. Notice
1075 that the next field is *not* written on purpose. */
1076 void
1077 state_writer::write_state_common_type_content (type_p current)
1079 write_any_indent (0);
1080 fprintf (state_file, "%d ", current->state_number);
1081 /* We do not write the next type, because list of types are
1082 explicitly written. However, lang_struct are special in that
1083 respect. See function write_state_lang_struct_type for more. */
1084 write_state_type (current->pointer_to);
1085 write_state_gc_used (current->gc_used);
1089 /* The important and recursive routine writing GTY types as understood
1090 by gengtype. Types which have a positive state_number have already
1091 been seen and written. */
1092 void
1093 state_writer::write_state_type (type_p current)
1095 write_any_indent (0);
1096 if (current == NULL)
1098 fprintf (state_file, "nil ");
1099 return;
1102 begin_s_expr ("type");
1104 if (current->state_number > 0)
1106 write_any_indent (0);
1107 fprintf (state_file, "already_seen %d", current->state_number);
1109 else
1111 m_state_written_type_count++;
1112 DBGPRINTF ("writing type #%d @%p old number %d", m_state_written_type_count,
1113 (void *) current, current->state_number);
1114 current->state_number = m_state_written_type_count;
1115 switch (current->kind)
1117 case TYPE_NONE:
1118 gcc_unreachable ();
1119 case TYPE_UNDEFINED:
1120 write_state_undefined_type (current);
1121 break;
1122 case TYPE_STRUCT:
1123 write_state_struct_type (current);
1124 break;
1125 case TYPE_USER_STRUCT:
1126 write_state_user_struct_type (current);
1127 break;
1128 case TYPE_UNION:
1129 write_state_union_type (current);
1130 break;
1131 case TYPE_POINTER:
1132 write_state_pointer_type (current);
1133 break;
1134 case TYPE_ARRAY:
1135 write_state_array_type (current);
1136 break;
1137 case TYPE_LANG_STRUCT:
1138 write_state_lang_struct_type (current);
1139 break;
1140 case TYPE_SCALAR:
1141 write_state_scalar_type (current);
1142 break;
1143 case TYPE_STRING:
1144 write_state_string_type (current);
1145 break;
1149 /* Terminate the "type" s-expression. */
1150 end_s_expr ();
1154 /* Write a pair. */
1155 void
1156 state_writer::write_state_pair (pair_p current)
1158 if (current == NULL)
1160 write_any_indent (0);
1161 fprintf (state_file, "nil)");
1162 return;
1165 begin_s_expr ("pair");
1167 if (current->name != NULL)
1168 write_state_a_string (current->name);
1169 else
1170 write_state_a_string ("nil");
1172 write_state_type (current->type);
1173 write_state_fileloc (&(current->line));
1174 write_state_options (current->opt);
1176 /* Terminate the "pair" s-expression. */
1177 end_s_expr ();
1180 /* Write a pair list and return the number of pairs written. */
1182 state_writer::write_state_pair_list (pair_p list)
1184 int nbpair = 0;
1185 pair_p current;
1187 for (current = list; current != NULL; current = current->next)
1189 write_state_pair (current);
1190 nbpair++;
1192 return nbpair;
1196 /* When writing imported linked lists, like typedefs, structures, ... we count
1197 their length first and write it. This eases the reading, and enables an
1198 extra verification on the number of actually read items. */
1200 /* Write our typedefs. */
1201 void
1202 state_writer::write_state_typedefs (void)
1204 int nbtypedefs = pair_list_length (typedefs);
1205 int nbpairs = 0;
1206 begin_s_expr ("typedefs");
1207 fprintf (state_file, "%d", nbtypedefs);
1208 nbpairs = write_state_pair_list (typedefs);
1209 gcc_assert (nbpairs == nbtypedefs);
1210 end_s_expr ();
1211 if (verbosity_level >= 2)
1212 printf ("%s wrote %d typedefs\n", progname, nbtypedefs);
1215 /* Write our structures. */
1216 void
1217 state_writer::write_state_structures (void)
1219 int nbstruct = 0;
1220 type_p current;
1222 for (current = structures; current != NULL; current = current->next)
1223 nbstruct++;
1225 begin_s_expr ("structures");
1226 fprintf (state_file, "%d", nbstruct);
1228 for (current = structures; current != NULL; current = current->next)
1230 write_new_line ();
1231 write_state_type (current);
1234 /* Terminate the "structures" s-expression. */
1235 end_s_expr ();
1236 if (verbosity_level >= 2)
1237 printf ("%s wrote %d structures in state\n", progname, nbstruct);
1240 /* Write our variables. */
1241 void
1242 state_writer::write_state_variables (void)
1244 int nbvars = pair_list_length (variables);
1245 int nbpairs = 0;
1246 begin_s_expr ("variables");
1247 fprintf (state_file, "%d", nbvars);
1248 nbpairs = write_state_pair_list (variables);
1249 gcc_assert (nbpairs == nbvars);
1250 end_s_expr ();
1251 if (verbosity_level >= 2)
1252 printf ("%s wrote %d variables.\n", progname, nbvars);
1255 /* Write the source directory. File locations within the source
1256 directory have been written specifically. */
1257 void
1258 state_writer::write_state_srcdir (void)
1260 begin_s_expr ("srcdir");
1261 write_state_a_string (srcdir);
1262 end_s_expr ();
1265 /* Count and write the list of our files. */
1266 void
1267 state_writer::write_state_files_list (void)
1269 int i = 0;
1270 /* Write the list of files with their lang_bitmap. */
1271 begin_s_expr ("fileslist");
1272 fprintf (state_file, "%d", (int) num_gt_files);
1273 for (i = 0; i < (int) num_gt_files; i++)
1275 const char *cursrcrelpath = NULL;
1276 const input_file *curfil = gt_files[i];
1277 /* Most of the files are inside $(srcdir) so it is worth to
1278 handle them specially. */
1279 cursrcrelpath = get_file_srcdir_relative_path (curfil);
1280 if (cursrcrelpath)
1282 begin_s_expr ("srcfile");
1283 fprintf (state_file, "%d ", get_lang_bitmap (curfil));
1284 write_state_a_string (cursrcrelpath);
1286 else
1288 begin_s_expr ("file");
1289 fprintf (state_file, "%d ", get_lang_bitmap (curfil));
1290 write_state_a_string (get_input_file_name (curfil));
1292 /* Terminate the inner s-expression (either "srcfile" or "file"). */
1293 end_s_expr ();
1295 /* Terminate the "fileslist" s-expression. */
1296 end_s_expr ();
1299 /* Write the list of GCC front-end languages. */
1300 void
1301 state_writer::write_state_languages (void)
1303 int i = 0;
1304 begin_s_expr ("languages");
1305 fprintf (state_file, "%d", (int) num_lang_dirs);
1306 for (i = 0; i < (int) num_lang_dirs; i++)
1308 /* Languages names are identifiers, we expect only letters or
1309 underscores or digits in them. In particular, C++ is not a
1310 valid language name, but cp is valid. */
1311 fprintf (state_file, " %s", lang_dir_names[i]);
1313 end_s_expr ();
1316 /* Write the trailer. */
1317 static void
1318 write_state_trailer (void)
1320 /* This test should probably catch IO errors like disk full... */
1321 if (fputs ("\n(!endfile)\n", state_file) == EOF)
1322 fatal ("failed to write state trailer [%s]", xstrerror (errno));
1325 /* The write_state routine is the only writing routine called by main
1326 in gengtype.c. To avoid messing the state if gengtype is
1327 interrupted or aborted, we write a temporary file and rename it
1328 after having written it in totality. */
1329 void
1330 write_state (const char *state_path)
1332 long statelen = 0;
1333 time_t now = 0;
1334 char *temp_state_path = NULL;
1335 char tempsuffix[40];
1336 time (&now);
1338 /* We write a unique temporary file which is renamed when complete
1339 * only. So even if gengtype is interrupted, the written state file
1340 * won't be partially written, since the temporary file is not yet
1341 * renamed in that case. */
1342 memset (tempsuffix, 0, sizeof (tempsuffix));
1343 snprintf (tempsuffix, sizeof (tempsuffix) - 1, "-%ld-%d.tmp", (long) now,
1344 (int) getpid ());
1345 temp_state_path = concat (state_path, tempsuffix, NULL);
1346 state_file = fopen (temp_state_path, "w");
1347 if (state_file == NULL)
1348 fatal ("Failed to open file %s for writing state: %s",
1349 temp_state_path, xstrerror (errno));
1350 if (verbosity_level >= 3)
1351 printf ("%s writing state file %s temporarily in %s\n",
1352 progname, state_path, temp_state_path);
1353 /* This is the first line of the state. Perhaps the file utility
1354 could know about that, so don't change it often. */
1355 fprintf (state_file, ";;;;@@@@ GCC gengtype state\n");
1356 /* Output a few comments for humans. */
1357 fprintf (state_file,
1358 ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
1359 fprintf (state_file,
1360 ";;; The format of this file is tied to a particular version of GCC.\n");
1361 fprintf (state_file,
1362 ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1363 fprintf (state_file,
1364 ";;; This file should be parsed by the same %s which wrote it.\n",
1365 progname);
1367 state_writer sw;
1369 /* The first non-comment significant line gives the version string. */
1370 sw.write_state_version (version_string);
1371 sw.write_state_srcdir ();
1372 sw.write_state_languages ();
1373 sw.write_state_files_list ();
1374 sw.write_state_structures ();
1375 sw.write_state_typedefs ();
1376 sw.write_state_variables ();
1377 write_state_trailer ();
1378 statelen = ftell (state_file);
1379 if (ferror (state_file))
1380 fatal ("output error when writing state file %s [%s]",
1381 temp_state_path, xstrerror (errno));
1382 if (fclose (state_file))
1383 fatal ("failed to close state file %s [%s]",
1384 temp_state_path, xstrerror (errno));
1385 if (rename (temp_state_path, state_path))
1386 fatal ("failed to rename %s to state file %s [%s]", temp_state_path,
1387 state_path, xstrerror (errno));
1388 free (temp_state_path);
1390 if (verbosity_level >= 1)
1391 printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1392 progname, state_path, statelen, sw.m_state_written_type_count);
1396 /** End of writing routines! The corresponding reading routines follow. **/
1400 /* Forward declarations, since some read_state_* functions are
1401 recursive! */
1402 static void read_state_fileloc (struct fileloc *line);
1403 static void read_state_options (options_p *opt);
1404 static void read_state_type (type_p *current);
1405 static void read_state_pair (pair_p *pair);
1406 /* Return the number of pairs actually read. */
1407 static int read_state_pair_list (pair_p *list);
1408 static void read_state_fields (pair_p *fields);
1409 static void read_state_common_type_content (type_p current);
1414 /* Record into the state_seen_types hash-table a type which we are
1415 reading, to enable recursive or circular references to it. */
1416 static void
1417 record_type (type_p type)
1419 PTR *slot;
1421 slot = htab_find_slot (state_seen_types, type, INSERT);
1422 gcc_assert (slot);
1424 *slot = type;
1427 /* Read an already seen type. */
1428 static void
1429 read_state_already_seen_type (type_p *type)
1431 struct state_token_st *t0 = peek_state_token (0);
1433 if (state_token_kind (t0) == STOK_INTEGER)
1435 PTR *slot = NULL;
1436 struct type loctype = { TYPE_SCALAR, 0, 0, 0, GC_UNUSED, {0} };
1438 loctype.state_number = t0->stok_un.stok_num;
1439 slot = htab_find_slot (state_seen_types, &loctype, NO_INSERT);
1440 if (slot == NULL)
1442 fatal_reading_state (t0, "Unknown type");
1445 next_state_tokens (1);
1446 *type = (type_p) *slot;
1448 else
1450 fatal_reading_state (t0, "Bad seen type");
1455 /* Read the scalar_nonchar type. */
1456 static void
1457 read_state_scalar_nonchar_type (type_p *type)
1459 *type = &scalar_nonchar;
1460 read_state_common_type_content (*type);
1464 /* Read the scalar_char type. */
1465 static void
1466 read_state_scalar_char_type (type_p *type)
1468 *type = &scalar_char;
1469 read_state_common_type_content (*type);
1472 /* Read the string_type. */
1473 static void
1474 read_state_string_type (type_p *type)
1476 *type = &string_type;
1477 read_state_common_type_content (*type);
1481 /* Read a lang_bitmap representing a set of GCC front-end languages. */
1482 static void
1483 read_state_lang_bitmap (lang_bitmap *bitmap)
1485 struct state_token_st *t;
1487 t = peek_state_token (0);
1488 if (state_token_kind (t) == STOK_INTEGER)
1490 *bitmap = t->stok_un.stok_num;
1491 next_state_tokens (1);
1493 else
1495 fatal_reading_state (t, "Bad syntax for bitmap");
1500 /* Read an undefined type. */
1501 static void
1502 read_state_undefined_type (type_p type)
1504 struct state_token_st *t0;
1506 type->kind = TYPE_UNDEFINED;
1507 read_state_common_type_content (type);
1508 t0 = peek_state_token (0);
1509 if (state_token_kind (t0) == STOK_STRING)
1511 if (state_token_is_name (t0, "nil"))
1513 type->u.s.tag = NULL;
1514 DBGPRINTF ("read anonymous undefined type @%p #%d",
1515 (void *) type, type->state_number);
1517 else
1519 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1520 DBGPRINTF ("read undefined type @%p #%d '%s'",
1521 (void *) type, type->state_number, type->u.s.tag);
1524 next_state_tokens (1);
1525 read_state_fileloc (&(type->u.s.line));
1527 else
1529 fatal_reading_state (t0, "Bad tag in undefined type");
1534 /* Read a GTY-ed struct type. */
1535 static void
1536 read_state_struct_type (type_p type)
1538 struct state_token_st *t0;
1540 type->kind = TYPE_STRUCT;
1541 read_state_common_type_content (type);
1542 t0 = peek_state_token (0);
1543 if (state_token_kind (t0) == STOK_STRING)
1545 if (state_token_is_name (t0, "nil"))
1547 type->u.s.tag = NULL;
1548 DBGPRINTF ("read anonymous struct type @%p #%d",
1549 (void *) type, type->state_number);
1551 else
1553 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1554 DBGPRINTF ("read struct type @%p #%d '%s'",
1555 (void *) type, type->state_number, type->u.s.tag);
1558 next_state_tokens (1);
1559 read_state_fileloc (&(type->u.s.line));
1560 read_state_fields (&(type->u.s.fields));
1561 read_state_options (&(type->u.s.opt));
1562 read_state_lang_bitmap (&(type->u.s.bitmap));
1563 read_state_type (&(type->u.s.lang_struct));
1564 read_state_type (&(type->u.s.base_class));
1565 if (type->u.s.base_class)
1566 add_subclass (type->u.s.base_class, type);
1568 else
1570 fatal_reading_state (t0, "Bad tag in struct type");
1575 /* Read a GTY-ed user-provided struct TYPE. */
1577 static void
1578 read_state_user_struct_type (type_p type)
1580 struct state_token_st *t0;
1582 type->kind = TYPE_USER_STRUCT;
1583 read_state_common_type_content (type);
1584 t0 = peek_state_token (0);
1585 if (state_token_kind (t0) == STOK_STRING)
1587 if (state_token_is_name (t0, "nil"))
1589 type->u.s.tag = NULL;
1590 DBGPRINTF ("read anonymous struct type @%p #%d",
1591 (void *) type, type->state_number);
1593 else
1595 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1596 DBGPRINTF ("read struct type @%p #%d '%s'",
1597 (void *) type, type->state_number, type->u.s.tag);
1600 next_state_tokens (1);
1601 read_state_fileloc (&(type->u.s.line));
1602 read_state_fields (&(type->u.s.fields));
1604 else
1606 fatal_reading_state (t0, "Bad tag in user-struct type");
1611 /* Read a GTY-ed union type. */
1612 static void
1613 read_state_union_type (type_p type)
1615 struct state_token_st *t0;
1617 type->kind = TYPE_UNION;
1618 read_state_common_type_content (type);
1619 t0 = peek_state_token (0);
1620 if (state_token_kind (t0) == STOK_STRING)
1622 if (state_token_is_name (t0, "nil"))
1624 type->u.s.tag = NULL;
1625 DBGPRINTF ("read anonymous union type @%p #%d",
1626 (void *) type, type->state_number);
1628 else
1630 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1631 DBGPRINTF ("read union type @%p #%d '%s'",
1632 (void *) type, type->state_number, type->u.s.tag);
1634 next_state_tokens (1);
1635 read_state_fileloc (&(type->u.s.line));
1636 read_state_fields (&(type->u.s.fields));
1637 read_state_options (&(type->u.s.opt));
1638 read_state_lang_bitmap (&(type->u.s.bitmap));
1639 read_state_type (&(type->u.s.lang_struct));
1641 else
1642 fatal_reading_state (t0, "Bad tag in union type");
1646 /* Read a GTY-ed pointer type. */
1647 static void
1648 read_state_pointer_type (type_p type)
1650 type->kind = TYPE_POINTER;
1651 read_state_common_type_content (type);
1652 DBGPRINTF ("read pointer type @%p #%d", (void *) type, type->state_number);
1653 read_state_type (&(type->u.p));
1657 /* Read a GTY-ed array type. */
1658 static void
1659 read_state_array_type (type_p type)
1661 struct state_token_st *t0;
1663 type->kind = TYPE_ARRAY;
1664 read_state_common_type_content (type);
1665 t0 = peek_state_token (0);
1666 if (state_token_kind (t0) == STOK_STRING)
1668 type->u.a.len = xstrdup (t0->stok_un.stok_string);
1669 DBGPRINTF ("read array type @%p #%d length '%s'",
1670 (void *) type, type->state_number, type->u.a.len);
1671 next_state_tokens (1);
1674 else if (state_token_is_name (t0, "nil"))
1676 type->u.a.len = NULL;
1677 DBGPRINTF ("read array type @%p #%d without length",
1678 (void *) type, type->state_number);
1679 next_state_tokens (1);
1682 else
1683 fatal_reading_state (t0, "Bad array name type");
1684 read_state_type (&(type->u.a.p));
1689 /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
1690 front-end languages. This is a tricky function and it was painful
1691 to debug. Change it with extreme care. See also
1692 write_state_lang_struct_type. */
1693 static void
1694 read_state_lang_struct_type (type_p type)
1696 struct state_token_st *t0 = NULL;
1697 struct state_token_st *t1 = NULL;
1698 struct state_token_st *t2 = NULL;
1700 type->kind = TYPE_LANG_STRUCT;
1701 read_state_common_type_content (type);
1702 t0 = peek_state_token (0);
1703 if (state_token_kind (t0) == STOK_STRING)
1705 if (state_token_is_name (t0, "nil"))
1707 DBGPRINTF ("read anonymous lang_struct type @%p #%d",
1708 (void *) type, type->state_number);
1709 type->u.s.tag = NULL;
1711 else
1713 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1714 DBGPRINTF ("read lang_struct type @%p #%d '%s'",
1715 (void *) type, type->state_number, type->u.s.tag);
1717 next_state_tokens (1);
1719 else
1720 fatal_reading_state (t0, "Bad tag in lang struct type");
1721 read_state_fileloc (&(type->u.s.line));
1722 read_state_fields (&(type->u.s.fields));
1723 read_state_options (&(type->u.s.opt));
1724 read_state_lang_bitmap (&(type->u.s.bitmap));
1725 /* Within lang_struct-ures, the lang_struct field is a linked list
1726 of homonymous types! */
1727 t0 = peek_state_token (0);
1728 t1 = peek_state_token (1);
1729 t2 = peek_state_token (2);
1730 /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
1731 if (state_token_kind (t0) == STOK_LEFTPAR
1732 && state_token_is_name (t1, "!homotypes")
1733 && state_token_kind (t2) == STOK_INTEGER)
1735 type_p *prevty = &type->u.s.lang_struct;
1736 int nbhomotype = t2->stok_un.stok_num;
1737 int i = 0;
1738 t0 = t1 = t2 = NULL;
1739 next_state_tokens (3);
1740 for (i = 0; i < nbhomotype; i++)
1742 read_state_type (prevty);
1743 t0 = peek_state_token (0);
1744 if (*prevty)
1745 prevty = &(*prevty)->next;
1746 else
1747 fatal_reading_state (t0,
1748 "expecting type in homotype list for lang_struct");
1750 if (state_token_kind (t0) != STOK_RIGHTPAR)
1751 fatal_reading_state (t0,
1752 "expecting ) in homotype list for lang_struct");
1753 next_state_tokens (1);
1755 else
1756 fatal_reading_state (t0, "expecting !homotypes for lang_struct");
1760 /* Read the gc used information. */
1761 static void
1762 read_state_gc_used (enum gc_used_enum *pgus)
1764 struct state_token_st *t0 = peek_state_token (0);
1765 if (state_token_is_name (t0, "gc_unused"))
1766 *pgus = GC_UNUSED;
1767 else if (state_token_is_name (t0, "gc_used"))
1768 *pgus = GC_USED;
1769 else if (state_token_is_name (t0, "gc_maybe_pointed_to"))
1770 *pgus = GC_MAYBE_POINTED_TO;
1771 else if (state_token_is_name (t0, "gc_pointed_to"))
1772 *pgus = GC_POINTED_TO;
1773 else
1774 fatal_reading_state (t0, "invalid gc_used information");
1775 next_state_tokens (1);
1779 /* Utility function to read the common content of types. */
1780 static void
1781 read_state_common_type_content (type_p current)
1783 struct state_token_st *t0 = peek_state_token (0);
1785 if (state_token_kind (t0) == STOK_INTEGER)
1787 current->state_number = t0->stok_un.stok_num;
1788 next_state_tokens (1);
1789 record_type (current);
1791 else
1792 fatal_reading_state_printf (t0,
1793 "Expected integer for state_number line %d",
1794 state_line);
1795 /* We don't read the next field of the type. */
1796 read_state_type (&current->pointer_to);
1797 read_state_gc_used (&current->gc_used);
1801 /* Read a GTY-ed type. */
1802 void
1803 read_state_type (type_p *current)
1805 struct state_token_st *t0 = peek_state_token (0);
1806 struct state_token_st *t1 = peek_state_token (1);
1808 if (state_token_kind (t0) == STOK_LEFTPAR &&
1809 state_token_is_name (t1, "!type"))
1811 next_state_tokens (2);
1812 t0 = peek_state_token (0);
1813 if (state_token_is_name (t0, "already_seen"))
1815 next_state_tokens (1);
1816 read_state_already_seen_type (current);
1818 else
1820 t0 = peek_state_token (0);
1822 if (state_token_is_name (t0, "scalar_nonchar"))
1824 next_state_tokens (1);
1825 read_state_scalar_nonchar_type (current);
1827 else if (state_token_is_name (t0, "scalar_char"))
1829 next_state_tokens (1);
1830 read_state_scalar_char_type (current);
1832 else if (state_token_is_name (t0, "string"))
1834 next_state_tokens (1);
1835 read_state_string_type (current);
1837 else if (state_token_is_name (t0, "undefined"))
1839 *current = XCNEW (struct type);
1840 next_state_tokens (1);
1841 read_state_undefined_type (*current);
1843 else if (state_token_is_name (t0, "struct"))
1845 *current = XCNEW (struct type);
1846 next_state_tokens (1);
1847 read_state_struct_type (*current);
1849 else if (state_token_is_name (t0, "union"))
1851 *current = XCNEW (struct type);
1852 next_state_tokens (1);
1853 read_state_union_type (*current);
1855 else if (state_token_is_name (t0, "lang_struct"))
1857 *current = XCNEW (struct type);
1858 next_state_tokens (1);
1859 read_state_lang_struct_type (*current);
1861 else if (state_token_is_name (t0, "pointer"))
1863 *current = XCNEW (struct type);
1864 next_state_tokens (1);
1865 read_state_pointer_type (*current);
1867 else if (state_token_is_name (t0, "array"))
1869 *current = XCNEW (struct type);
1870 next_state_tokens (1);
1871 read_state_array_type (*current);
1873 else if (state_token_is_name (t0, "user_struct"))
1875 *current = XCNEW (struct type);
1876 next_state_tokens (1);
1877 read_state_user_struct_type (*current);
1879 else
1880 fatal_reading_state (t0, "bad type in (!type");
1882 t0 = peek_state_token (0);
1883 if (state_token_kind (t0) != STOK_RIGHTPAR)
1884 fatal_reading_state (t0, "missing ) in type");
1885 next_state_tokens (1);
1887 else if (state_token_is_name (t0, "nil"))
1889 next_state_tokens (1);
1890 *current = NULL;
1892 else
1893 fatal_reading_state (t0, "bad type syntax");
1897 /* Read a file location. Files within the source directory are dealt
1898 with specifically. */
1899 void
1900 read_state_fileloc (struct fileloc *floc)
1902 bool issrcfile = false;
1903 struct state_token_st *t0 = peek_state_token (0);
1904 struct state_token_st *t1 = peek_state_token (1);
1906 gcc_assert (floc != NULL);
1907 gcc_assert (srcdir != NULL);
1909 if (state_token_kind (t0) == STOK_LEFTPAR &&
1910 (state_token_is_name (t1, "!fileloc")
1911 || (issrcfile = state_token_is_name (t1, "!srcfileloc"))))
1913 next_state_tokens (2);
1914 t0 = peek_state_token (0);
1915 t1 = peek_state_token (1);
1916 if (state_token_kind (t0) == STOK_STRING &&
1917 state_token_kind (t1) == STOK_INTEGER)
1919 char *path = t0->stok_un.stok_string;
1920 if (issrcfile)
1922 static const char dirsepstr[2] = { DIR_SEPARATOR, (char) 0 };
1923 char *fullpath = concat (srcdir, dirsepstr, path, NULL);
1924 floc->file = input_file_by_name (fullpath);
1925 free (fullpath);
1927 else
1928 floc->file = input_file_by_name (path);
1929 floc->line = t1->stok_un.stok_num;
1930 next_state_tokens (2);
1932 else
1933 fatal_reading_state (t0,
1934 "Bad fileloc syntax, expected path string and line");
1935 t0 = peek_state_token (0);
1936 if (state_token_kind (t0) != STOK_RIGHTPAR)
1937 fatal_reading_state (t0, "Bad fileloc syntax, expected )");
1938 next_state_tokens (1);
1940 else if (state_token_is_name (t0, "nil"))
1942 next_state_tokens (1);
1943 floc->file = NULL;
1944 floc->line = 0;
1946 else
1947 fatal_reading_state (t0, "Bad fileloc syntax");
1951 /* Read the fields of a GTY-ed type. */
1952 void
1953 read_state_fields (pair_p *fields)
1955 pair_p tmp = NULL;
1956 struct state_token_st *t0 = peek_state_token (0);
1957 struct state_token_st *t1 = peek_state_token (1);
1958 struct state_token_st *t2 = peek_state_token (2);
1960 if (state_token_kind (t0) == STOK_LEFTPAR
1961 && state_token_is_name (t1, "!fields")
1962 && state_token_kind (t2) == STOK_INTEGER)
1964 int nbfields = t2->stok_un.stok_num;
1965 int nbpairs = 0;
1966 next_state_tokens (3);
1967 nbpairs = read_state_pair_list (&tmp);
1968 t0 = peek_state_token (0);
1969 if (nbpairs != nbfields)
1970 fatal_reading_state_printf
1971 (t0,
1972 "Mismatched fields number, expected %d got %d", nbpairs, nbfields);
1973 if (state_token_kind (t0) == STOK_RIGHTPAR)
1974 next_state_tokens (1);
1975 else
1976 fatal_reading_state (t0, "Bad fields expecting )");
1979 *fields = tmp;
1983 /* Read a string option. */
1984 static void
1985 read_state_string_option (options_p opt)
1987 struct state_token_st *t0 = peek_state_token (0);
1988 opt->kind = OPTION_STRING;
1989 if (state_token_kind (t0) == STOK_STRING)
1991 opt->info.string = xstrdup (t0->stok_un.stok_string);
1992 next_state_tokens (1);
1994 else if (state_token_is_name (t0, "nil"))
1996 opt->info.string = NULL;
1997 next_state_tokens (1);
1999 else
2000 fatal_reading_state (t0, "Missing name in string option");
2004 /* Read a type option. */
2005 static void
2006 read_state_type_option (options_p opt)
2008 opt->kind = OPTION_TYPE;
2009 read_state_type (&(opt->info.type));
2013 /* Read a nested option. */
2014 static void
2015 read_state_nested_option (options_p opt)
2017 struct state_token_st *t0;
2019 opt->info.nested = XCNEW (struct nested_ptr_data);
2020 opt->kind = OPTION_NESTED;
2021 read_state_type (&(opt->info.nested->type));
2022 t0 = peek_state_token (0);
2023 if (state_token_kind (t0) == STOK_STRING)
2025 opt->info.nested->convert_from = xstrdup (t0->stok_un.stok_string);
2026 next_state_tokens (1);
2028 else if (state_token_is_name (t0, "nil"))
2030 opt->info.nested->convert_from = NULL;
2031 next_state_tokens (1);
2033 else
2034 fatal_reading_state (t0, "Bad nested convert_from option");
2036 t0 = peek_state_token (0);
2037 if (state_token_kind (t0) == STOK_STRING)
2039 opt->info.nested->convert_to = xstrdup (t0->stok_un.stok_string);
2040 next_state_tokens (1);
2042 else if (state_token_is_name (t0, "nil"))
2044 opt->info.nested->convert_to = NULL;
2045 next_state_tokens (1);
2047 else
2048 fatal_reading_state (t0, "Bad nested convert_from option");
2052 /* Read an GTY option. */
2053 static void
2054 read_state_option (options_p *opt)
2056 struct state_token_st *t0 = peek_state_token (0);
2057 struct state_token_st *t1 = peek_state_token (1);
2059 if (state_token_kind (t0) == STOK_LEFTPAR &&
2060 state_token_is_name (t1, "!option"))
2062 next_state_tokens (2);
2063 t0 = peek_state_token (0);
2064 if (state_token_kind (t0) == STOK_NAME)
2066 *opt = XCNEW (struct options);
2067 if (state_token_is_name (t0, "nil"))
2068 (*opt)->name = NULL;
2069 else
2070 (*opt)->name = t0->stok_un.stok_ident->stid_name;
2071 next_state_tokens (1);
2072 t0 = peek_state_token (0);
2073 if (state_token_kind (t0) == STOK_NAME)
2075 if (state_token_is_name (t0, "string"))
2077 next_state_tokens (1);
2078 read_state_string_option (*opt);
2080 else if (state_token_is_name (t0, "type"))
2082 next_state_tokens (1);
2083 read_state_type_option (*opt);
2085 else if (state_token_is_name (t0, "nested"))
2087 next_state_tokens (1);
2088 read_state_nested_option (*opt);
2090 else
2091 fatal_reading_state (t0, "Bad option type");
2092 t0 = peek_state_token (0);
2093 if (state_token_kind (t0) != STOK_RIGHTPAR)
2094 fatal_reading_state (t0, "Bad syntax in option, expecting )");
2096 next_state_tokens (1);
2098 else
2099 fatal_reading_state (t0, "Missing option type");
2101 else
2102 fatal_reading_state (t0, "Bad name for option");
2104 else
2105 fatal_reading_state (t0, "Bad option, waiting for )");
2108 /* Read a list of options. */
2109 void
2110 read_state_options (options_p *opt)
2112 options_p head = NULL;
2113 options_p previous = NULL;
2114 options_p current_option = NULL;
2115 struct state_token_st *t0 = peek_state_token (0);
2116 struct state_token_st *t1 = peek_state_token (1);
2118 if (state_token_kind (t0) == STOK_LEFTPAR &&
2119 state_token_is_name (t1, "!options"))
2121 next_state_tokens (2);
2122 t0 = peek_state_token (0);
2123 while (state_token_kind (t0) != STOK_RIGHTPAR)
2125 read_state_option (&current_option);
2126 if (head == NULL)
2128 head = current_option;
2129 previous = head;
2131 else
2133 previous->next = current_option;
2134 previous = current_option;
2136 t0 = peek_state_token (0);
2138 next_state_tokens (1);
2140 else if (state_token_is_name (t0, "nil"))
2142 next_state_tokens (1);
2144 else
2145 fatal_reading_state (t0, "Bad options syntax");
2147 *opt = head;
2151 /* Read a version, and check against the version of the gengtype. */
2152 static void
2153 read_state_version (const char *version_string)
2155 struct state_token_st *t0 = peek_state_token (0);
2156 struct state_token_st *t1 = peek_state_token (1);
2158 if (state_token_kind (t0) == STOK_LEFTPAR &&
2159 state_token_is_name (t1, "!version"))
2161 next_state_tokens (2);
2162 t0 = peek_state_token (0);
2163 t1 = peek_state_token (1);
2164 if (state_token_kind (t0) == STOK_STRING &&
2165 state_token_kind (t1) == STOK_RIGHTPAR)
2167 /* Check that the read version string is the same as current
2168 version. */
2169 if (strcmp (version_string, t0->stok_un.stok_string))
2170 fatal_reading_state_printf (t0,
2171 "version string mismatch; expecting %s but got %s",
2172 version_string,
2173 t0->stok_un.stok_string);
2174 next_state_tokens (2);
2176 else
2177 fatal_reading_state (t0, "Missing version or right parenthesis");
2179 else
2180 fatal_reading_state (t0, "Bad version syntax");
2184 /* Read a pair. */
2185 void
2186 read_state_pair (pair_p *current)
2188 struct state_token_st *t0 = peek_state_token (0);
2189 struct state_token_st *t1 = peek_state_token (1);
2190 if (state_token_kind (t0) == STOK_LEFTPAR &&
2191 state_token_is_name (t1, "!pair"))
2193 *current = XCNEW (struct pair);
2194 next_state_tokens (2);
2195 t0 = peek_state_token (0);
2196 if (state_token_kind (t0) == STOK_STRING)
2198 if (strcmp (t0->stok_un.stok_string, "nil") == 0)
2200 (*current)->name = NULL;
2202 else
2204 (*current)->name = xstrdup (t0->stok_un.stok_string);
2206 next_state_tokens (1);
2207 read_state_type (&((*current)->type));
2208 read_state_fileloc (&((*current)->line));
2209 read_state_options (&((*current)->opt));
2210 t0 = peek_state_token (0);
2211 if (state_token_kind (t0) == STOK_RIGHTPAR)
2213 next_state_tokens (1);
2215 else
2217 fatal_reading_state (t0, "Bad syntax for pair, )");
2220 else
2222 fatal_reading_state (t0, "Bad name for pair");
2225 else if (state_token_kind (t0) == STOK_NAME &&
2226 state_token_is_name (t0, "nil"))
2228 next_state_tokens (1);
2229 *current = NULL;
2231 else
2232 fatal_reading_state_printf (t0, "Bad syntax for pair, (!pair %d",
2233 state_token->stok_kind);
2237 /* Return the number of pairs actually read. */
2239 read_state_pair_list (pair_p *list)
2241 int nbpair = 0;
2242 pair_p head = NULL;
2243 pair_p previous = NULL;
2244 pair_p tmp = NULL;
2245 struct state_token_st *t0 = peek_state_token (0);
2246 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2248 read_state_pair (&tmp);
2249 if (head == NULL)
2251 head = tmp;
2252 previous = head;
2254 else
2256 previous->next = tmp;
2257 previous = tmp;
2259 t0 = peek_state_token (0);
2260 nbpair++;
2263 /* don't consume the ); the caller will eat it. */
2264 *list = head;
2265 return nbpair;
2268 /* Read the typedefs. */
2269 static void
2270 read_state_typedefs (pair_p *typedefs)
2272 int nbtypedefs = 0;
2273 pair_p list = NULL;
2274 struct state_token_st *t0 = peek_state_token (0);
2275 struct state_token_st *t1 = peek_state_token (1);
2276 struct state_token_st *t2 = peek_state_token (2);
2278 if (state_token_kind (t0) == STOK_LEFTPAR
2279 && state_token_is_name (t1, "!typedefs")
2280 && state_token_kind (t2) == STOK_INTEGER)
2282 int nbpairs = 0;
2283 nbtypedefs = t2->stok_un.stok_num;
2284 next_state_tokens (3);
2285 nbpairs = read_state_pair_list (&list);
2286 t0 = peek_state_token (0);
2287 if (nbpairs != nbtypedefs)
2288 fatal_reading_state_printf
2289 (t0,
2290 "invalid number of typedefs, expected %d but got %d",
2291 nbtypedefs, nbpairs);
2292 if (state_token_kind (t0) == STOK_RIGHTPAR)
2293 next_state_tokens (1);
2294 else
2295 fatal_reading_state (t0, "Bad typedefs syntax )");
2297 else
2298 fatal_reading_state (t0, "Bad typedefs syntax (!typedefs");
2300 if (verbosity_level >= 2)
2301 printf ("%s read %d typedefs from state\n", progname, nbtypedefs);
2302 *typedefs = list;
2306 /* Read the structures. */
2307 static void
2308 read_state_structures (type_p *structures)
2310 type_p head = NULL;
2311 type_p previous = NULL;
2312 type_p tmp;
2313 int nbstruct = 0, countstruct = 0;
2314 struct state_token_st *t0 = peek_state_token (0);
2315 struct state_token_st *t1 = peek_state_token (1);
2316 struct state_token_st *t2 = peek_state_token (2);
2318 if (state_token_kind (t0) == STOK_LEFTPAR
2319 && state_token_is_name (t1, "!structures")
2320 && state_token_kind (t2) == STOK_INTEGER)
2322 nbstruct = t2->stok_un.stok_num;
2323 next_state_tokens (3);
2324 t0 = peek_state_token (0);
2325 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2327 tmp = NULL;
2328 read_state_type (&tmp);
2329 countstruct++;
2330 if (head == NULL)
2332 head = tmp;
2333 previous = head;
2335 else
2337 previous->next = tmp;
2338 previous = tmp;
2340 t0 = peek_state_token (0);
2342 next_state_tokens (1);
2344 else
2345 fatal_reading_state (t0, "Bad structures syntax");
2346 if (countstruct != nbstruct)
2347 fatal_reading_state_printf (NULL_STATE_TOKEN,
2348 "expected %d structures but got %d",
2349 nbstruct, countstruct);
2350 if (verbosity_level >= 2)
2351 printf ("%s read %d structures from state\n", progname, nbstruct);
2352 *structures = head;
2356 /* Read the variables. */
2357 static void
2358 read_state_variables (pair_p *variables)
2360 pair_p list = NULL;
2361 int nbvars = 0;
2362 struct state_token_st *t0 = peek_state_token (0);
2363 struct state_token_st *t1 = peek_state_token (1);
2364 struct state_token_st *t2 = peek_state_token (2);
2366 if (state_token_kind (t0) == STOK_LEFTPAR
2367 && state_token_is_name (t1, "!variables")
2368 && state_token_kind (t2) == STOK_INTEGER)
2370 int nbpairs = 0;
2371 nbvars = t2->stok_un.stok_num;
2372 next_state_tokens (3);
2373 nbpairs = read_state_pair_list (&list);
2374 t0 = peek_state_token (0);
2375 if (nbpairs != nbvars)
2376 fatal_reading_state_printf
2377 (t0, "Invalid number of variables, expected %d but got %d",
2378 nbvars, nbpairs);
2379 if (state_token_kind (t0) == STOK_RIGHTPAR)
2380 next_state_tokens (1);
2381 else
2382 fatal_reading_state (t0, "Waiting for ) in variables");
2384 else
2385 fatal_reading_state (t0, "Bad variables syntax");
2386 *variables = list;
2387 if (verbosity_level >= 2)
2388 printf ("%s read %d variables from state\n", progname, nbvars);
2392 /* Read the source directory. */
2393 static void
2394 read_state_srcdir (void)
2396 struct state_token_st *t0 = peek_state_token (0);
2397 struct state_token_st *t1 = peek_state_token (1);
2398 if (state_token_kind (t0) == STOK_LEFTPAR &&
2399 state_token_is_name (t1, "!srcdir"))
2401 next_state_tokens (2);
2402 t0 = peek_state_token (0);
2403 t1 = peek_state_token (1);
2404 if (state_token_kind (t0) == STOK_STRING &&
2405 state_token_kind (t1) == STOK_RIGHTPAR)
2407 srcdir = xstrdup (t0->stok_un.stok_string);
2408 srcdir_len = strlen (srcdir);
2409 next_state_tokens (2);
2410 return;
2414 fatal_reading_state (t0, "Bad srcdir in state_file");
2418 /* Read the sequence of GCC front-end languages. */
2419 static void
2420 read_state_languages (void)
2422 struct state_token_st *t0 = peek_state_token (0);
2423 struct state_token_st *t1 = peek_state_token (1);
2424 struct state_token_st *t2 = peek_state_token (2);
2425 if (state_token_kind (t0) == STOK_LEFTPAR
2426 && state_token_is_name (t1, "!languages")
2427 && state_token_kind (t2) == STOK_INTEGER)
2429 int i = 0;
2430 num_lang_dirs = t2->stok_un.stok_num;
2431 lang_dir_names = XCNEWVEC (const char *, num_lang_dirs);
2432 next_state_tokens (3);
2433 t0 = t1 = t2 = NULL;
2434 for (i = 0; i < (int) num_lang_dirs; i++)
2436 t0 = peek_state_token (0);
2437 if (state_token_kind (t0) != STOK_NAME)
2438 fatal_reading_state (t0, "expecting language name in state file");
2439 lang_dir_names[i] = t0->stok_un.stok_ident->stid_name;
2440 next_state_tokens (1);
2442 t0 = peek_state_token (0);
2443 if (state_token_kind (t0) != STOK_RIGHTPAR)
2444 fatal_reading_state (t0, "missing ) in languages list of state file");
2445 next_state_tokens (1);
2447 else
2448 fatal_reading_state (t0, "expecting languages list in state file");
2452 /* Read the sequence of files. */
2453 static void
2454 read_state_files_list (void)
2456 struct state_token_st *t0 = peek_state_token (0);
2457 struct state_token_st *t1 = peek_state_token (1);
2458 struct state_token_st *t2 = peek_state_token (2);
2460 if (state_token_kind (t0) == STOK_LEFTPAR
2461 && state_token_is_name (t1, "!fileslist")
2462 && state_token_kind (t2) == STOK_INTEGER)
2464 int i = 0;
2465 num_gt_files = t2->stok_un.stok_num;
2466 next_state_tokens (3);
2467 t0 = t1 = t2 = NULL;
2468 gt_files = XCNEWVEC (const input_file *, num_gt_files);
2469 for (i = 0; i < (int) num_gt_files; i++)
2471 bool issrcfile = FALSE;
2472 t0 = t1 = t2 = NULL;
2473 t0 = peek_state_token (0);
2474 t1 = peek_state_token (1);
2475 t2 = peek_state_token (2);
2476 if (state_token_kind (t0) == STOK_LEFTPAR
2477 && (state_token_is_name (t1, "!file")
2478 || (issrcfile = state_token_is_name (t1, "!srcfile")))
2479 && state_token_kind (t2) == STOK_INTEGER)
2481 lang_bitmap bmap = t2->stok_un.stok_num;
2482 next_state_tokens (3);
2483 t0 = t1 = t2 = NULL;
2484 t0 = peek_state_token (0);
2485 t1 = peek_state_token (1);
2486 if (state_token_kind (t0) == STOK_STRING
2487 && state_token_kind (t1) == STOK_RIGHTPAR)
2489 const char *fnam = t0->stok_un.stok_string;
2490 /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
2491 input_file *curgt = NULL;
2492 if (issrcfile)
2494 static const char dirsepstr[2] =
2495 { DIR_SEPARATOR, (char) 0 };
2496 char *fullpath = concat (srcdir, dirsepstr, fnam, NULL);
2497 curgt = input_file_by_name (fullpath);
2498 free (fullpath);
2500 else
2501 curgt = input_file_by_name (fnam);
2502 set_lang_bitmap (curgt, bmap);
2503 gt_files[i] = curgt;
2504 next_state_tokens (2);
2506 else
2507 fatal_reading_state (t0,
2508 "bad file in !fileslist of state file");
2510 else
2511 fatal_reading_state (t0,
2512 "expecting file in !fileslist of state file");
2514 t0 = peek_state_token (0);
2515 if (state_token_kind (t0) != STOK_RIGHTPAR)
2516 fatal_reading_state (t0, "missing ) for !fileslist in state file");
2517 next_state_tokens (1);
2519 else
2520 fatal_reading_state (t0, "missing !fileslist in state file");
2524 /* Read the trailer. */
2525 static void
2526 read_state_trailer (void)
2528 struct state_token_st *t0 = peek_state_token (0);
2529 struct state_token_st *t1 = peek_state_token (1);
2530 struct state_token_st *t2 = peek_state_token (2);
2532 if (state_token_kind (t0) == STOK_LEFTPAR
2533 && state_token_is_name (t1, "!endfile")
2534 && state_token_kind (t2) == STOK_RIGHTPAR)
2535 next_state_tokens (3);
2536 else
2537 fatal_reading_state (t0, "missing !endfile in state file");
2541 /* Utility functions for the state_seen_types hash table. */
2542 static unsigned
2543 hash_type_number (const void *ty)
2545 const struct type *type = (const struct type *) ty;
2547 return type->state_number;
2550 static int
2551 equals_type_number (const void *ty1, const void *ty2)
2553 const struct type *type1 = (const struct type *) ty1;
2554 const struct type *type2 = (const struct type *) ty2;
2556 return type1->state_number == type2->state_number;
2559 static int
2560 string_eq (const void *a, const void *b)
2562 const char *a0 = (const char *)a;
2563 const char *b0 = (const char *)b;
2565 return (strcmp (a0, b0) == 0);
2569 /* The function reading the state, called by main from gengtype.c. */
2570 void
2571 read_state (const char *path)
2573 state_file = fopen (path, "r");
2574 if (state_file == NULL)
2575 fatal ("Failed to open state file %s for reading [%s]", path,
2576 xstrerror (errno));
2577 state_path = path;
2578 state_line = 1;
2580 if (verbosity_level >= 1)
2582 printf ("%s reading state file %s;", progname, state_path);
2583 if (verbosity_level >= 2)
2584 putchar ('\n');
2585 fflush (stdout);
2588 state_seen_types =
2589 htab_create (2017, hash_type_number, equals_type_number, NULL);
2590 state_ident_tab =
2591 htab_create (4027, htab_hash_string, string_eq, NULL);
2592 read_state_version (version_string);
2593 read_state_srcdir ();
2594 read_state_languages ();
2595 read_state_files_list ();
2596 read_state_structures (&structures);
2597 if (ferror (state_file))
2598 fatal_reading_state_printf
2599 (NULL_STATE_TOKEN, "input error while reading state [%s]",
2600 xstrerror (errno));
2601 read_state_typedefs (&typedefs);
2602 read_state_variables (&variables);
2603 read_state_trailer ();
2605 if (verbosity_level >= 1)
2607 printf ("%s read %ld bytes.\n", progname, ftell (state_file));
2608 fflush (stdout);
2611 if (fclose (state_file))
2612 fatal ("failed to close read state file %s [%s]",
2613 path, xstrerror (errno));
2614 state_file = NULL;
2615 state_path = NULL;
2618 /* End of file gengtype-state.c. */