2015-05-22 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / gengtype-state.c
blob5588b9e180f9c678927c6032e97f6fdf0b6ea284
1 /* Gengtype persistent state serialization & de-serialization.
2 Useful for gengtype in plugin mode.
4 Copyright (C) 2010-2015 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 "hashtab.h"
35 #include "version.h" /* For version_string & pkgversion_string. */
36 #include "obstack.h"
37 #include "gengtype.h"
41 /* Gives the file location of a type, if any. */
42 static inline struct fileloc*
43 type_lineloc (const_type_p ty)
45 if (!ty)
46 return NULL;
47 switch (ty->kind)
49 case TYPE_NONE:
50 gcc_unreachable ();
51 case TYPE_STRUCT:
52 case TYPE_UNION:
53 case TYPE_LANG_STRUCT:
54 case TYPE_USER_STRUCT:
55 case TYPE_UNDEFINED:
56 return CONST_CAST (struct fileloc*, &ty->u.s.line);
57 case TYPE_SCALAR:
58 case TYPE_STRING:
59 case TYPE_POINTER:
60 case TYPE_ARRAY:
61 return NULL;
62 default:
63 gcc_unreachable ();
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. */
72 /* The lexical kind of each lispy token. */
73 enum state_token_en
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. */
84 /* Structure and hash-table used to share identifiers or names. */
85 struct state_ident_st
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 */
92 static htab_t state_ident_tab;
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. */
101 struct state_token_st
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! */
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 */
119 stok_un;
125 #define NULL_STATE_TOKEN (struct state_token_st*)0
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. */
130 struct state_token_st *state_token = NULL_STATE_TOKEN;
132 /* Used by the reading lexer. */
133 static FILE *state_file;
134 static const char *state_path = NULL;
135 static int state_line = 0;
136 static long state_bol = 0; /* offset of beginning of line */
138 /* A class for writing out s-expressions, keeping track of newlines and
139 nested indentation. */
140 class s_expr_writer
142 public:
143 s_expr_writer ();
145 void write_new_line ();
146 void write_any_indent (int leading_spaces);
148 void begin_s_expr (const char *tag);
149 void end_s_expr ();
151 private:
152 int m_indent_amount;
153 int m_had_recent_newline;
154 }; // class s_expr_writer
156 /* A class for writing out "gtype.state". */
157 class state_writer : public s_expr_writer
159 public:
160 state_writer ();
162 private:
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_undefined_type (type_p current);
176 void write_state_struct_union_type (type_p current, const char *kindstr);
177 void write_state_struct_type (type_p current);
178 void write_state_user_struct_type (type_p current);
179 void write_state_union_type (type_p current);
180 void write_state_lang_struct_type (type_p current);
181 void write_state_pointer_type (type_p current);
182 void write_state_array_type (type_p current);
183 void write_state_gc_used (enum gc_used_enum gus);
184 void write_state_common_type_content (type_p current);
185 void write_state_type (type_p current);
186 void write_state_pair (pair_p current);
187 int write_state_pair_list (pair_p list);
188 void write_state_typedefs (void);
189 void write_state_structures (void);
190 void write_state_variables (void);
191 void write_state_srcdir (void);
192 void write_state_files_list (void);
193 void write_state_languages (void);
195 friend void write_state (const char *state_path);
197 private:
198 /* Counter of written types. */
199 int m_state_written_type_count;
200 }; // class state_writer
203 /* class s_expr_writer's trivial constructor. */
204 s_expr_writer::s_expr_writer ()
205 : m_indent_amount (0),
206 m_had_recent_newline (0)
210 /* Write a newline to the output file, merging adjacent newlines. */
211 void
212 s_expr_writer::write_new_line (void)
214 /* Don't add a newline if we've just had one. */
215 if (!m_had_recent_newline)
217 fprintf (state_file, "\n");
218 m_had_recent_newline = 1;
222 /* If we've just had a newline, write the indentation amount, potentially
223 omitting some spaces.
225 LEADING_SPACES exists to support code that writes strings with leading
226 spaces (e.g " foo") which might occur within a line, or could be the first
227 thing on a line. By passing leading_spaces == 1, when such a string is the
228 first thing on a line, write_any_indent () swallows the successive
229 leading spaces into the indentation so that the "foo" begins at the expected
230 column. */
231 void
232 s_expr_writer::write_any_indent (int leading_spaces)
234 int i;
235 int amount = m_indent_amount - leading_spaces;
236 if (m_had_recent_newline)
237 for (i = 0; i < amount; i++)
238 fprintf (state_file, " ");
239 m_had_recent_newline = 0;
242 /* Write the beginning of a new s-expresion e.g. "(!foo "
243 The writer automatically adds whitespace to show the hierarchical
244 structure of the expressions, so each one starts on a new line,
245 and any within it will be at an increased indentation level. */
246 void
247 s_expr_writer::begin_s_expr (const char *tag)
249 write_new_line ();
250 write_any_indent (0);
251 fprintf (state_file, "(!%s ", tag);
252 m_indent_amount++;
255 /* Write out the end of an s-expression: any necssessary indentation,
256 a closing parenthesis, and a new line. */
257 void
258 s_expr_writer::end_s_expr (void)
260 m_indent_amount--;
261 write_any_indent (0);
262 fprintf (state_file, ")");
263 write_new_line ();
267 /* class state_writer's trivial constructor. */
268 state_writer::state_writer ()
269 : s_expr_writer (),
270 m_state_written_type_count (0)
275 /* Fatal error messages when reading the state. They are extremely
276 unlikely, and only appear when this gengtype-state.c file is buggy,
277 or when reading a gengtype state which was not generated by the
278 same version of gengtype or GCC. */
281 /* Fatal message while reading state. */
282 static void
283 fatal_reading_state (struct state_token_st* tok, const char*msg)
285 if (tok)
286 fatal ("%s:%d:%d: Invalid state file; %s",
287 tok->stok_file, tok->stok_line, tok->stok_col,
288 msg);
289 else
290 fatal ("%s:%d: Invalid state file; %s",
291 state_path, state_line, msg);
295 /* Fatal printf-like message while reading state. This can't be a
296 function, because there is no way to pass a va_arg to a variant of
297 fatal. */
298 #define fatal_reading_state_printf(Tok,Fmt,...) do { \
299 struct state_token_st* badtok = Tok; \
300 if (badtok) \
301 fatal ("%s:%d:%d: Invalid state file; " Fmt, \
302 badtok->stok_file, \
303 badtok->stok_line, \
304 badtok->stok_col, __VA_ARGS__); \
305 else \
306 fatal ("%s:%d: Invalid state file; " Fmt, \
307 state_path, state_line, __VA_ARGS__); \
308 } while (0)
311 /* Find or allocate an identifier in our name hash table. */
312 static struct state_ident_st *
313 state_ident_by_name (const char *name, enum insert_option optins)
315 PTR *slot = NULL;
316 int namlen = 0;
317 struct state_ident_st *stid = NULL;
319 if (!name || !name[0])
320 return NULL;
322 slot = htab_find_slot (state_ident_tab, name, optins);
323 if (!slot)
324 return NULL;
326 namlen = strlen (name);
327 stid =
328 (struct state_ident_st *) xmalloc (sizeof (struct state_ident_st) +
329 namlen);
330 memset (stid, 0, sizeof (struct state_ident_st) + namlen);
331 strcpy (stid->stid_name, name);
332 *slot = stid;
334 return stid;
337 /* Our token lexer is heavily inspired by MELT's lexer, and share some
338 code with the file gcc/melt-runtime.c of the GCC MELT branch! We
339 really want the gengtype state to be easily parsable by MELT. This
340 is a usual lispy lexing routine, dealing with spaces and comments,
341 numbers, parenthesis, names, strings. */
342 static struct state_token_st *
343 read_a_state_token (void)
345 int c = 0;
346 long curoff = 0;
347 struct state_token_st *tk = NULL;
349 again: /* Read again, e.g. after a comment or spaces. */
350 c = getc (state_file);
351 if (c == EOF)
352 return NULL;
354 /* Handle spaces, count lines. */
355 if (c == '\n')
357 state_line++;
358 state_bol = curoff = ftell (state_file);
359 goto again;
361 if (ISSPACE (c))
362 goto again;
363 /* Skip comments starting with semi-colon. */
364 if (c == ';')
368 c = getc (state_file);
370 while (c > 0 && c != '\n');
371 if (c == '\n')
373 state_line++;
374 state_bol = curoff = ftell (state_file);
376 goto again;
378 /* Read signed numbers. */
379 if (ISDIGIT (c) || c == '-' || c == '+')
380 { /* number */
381 int n = 0;
382 ungetc (c, state_file);
383 curoff = ftell (state_file);
384 if (fscanf (state_file, "%d", &n) <= 0)
385 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error in number");
386 tk = XCNEW (struct state_token_st);
387 tk->stok_kind = STOK_INTEGER;
388 tk->stok_line = state_line;
389 tk->stok_col = curoff - state_bol;
390 tk->stok_file = state_path;
391 tk->stok_next = NULL;
392 tk->stok_un.stok_num = n;
394 return tk;
396 /* Read an opening left parenthesis. */
397 else if (c == '(')
399 curoff = ftell (state_file);
400 tk = XCNEW (struct state_token_st);
401 tk->stok_kind = STOK_LEFTPAR;
402 tk->stok_line = state_line;
403 tk->stok_col = curoff - state_bol;
404 tk->stok_file = state_path;
405 tk->stok_next = NULL;
407 return tk;
409 /* Read an closing right parenthesis. */
410 else if (c == ')')
412 curoff = ftell (state_file);
413 tk = XCNEW (struct state_token_st);
414 tk->stok_kind = STOK_RIGHTPAR;
415 tk->stok_line = state_line;
416 tk->stok_col = curoff - state_bol;
417 tk->stok_file = state_path;
418 tk->stok_next = NULL;
420 return tk;
422 /* Read identifiers, using an obstack. */
423 else if (ISALPHA (c) || c == '_' || c == '$' || c == '!' || c == '#')
425 struct obstack id_obstack;
426 struct state_ident_st *sid = NULL;
427 char *ids = NULL;
428 obstack_init (&id_obstack);
429 curoff = ftell (state_file);
430 while (ISALNUM (c) || c == '_' || c == '$' || c == '!' || c == '#')
432 obstack_1grow (&id_obstack, c);
433 c = getc (state_file);
434 if (c < 0)
435 break;
437 if (c >= 0)
438 ungetc (c, state_file);
439 obstack_1grow (&id_obstack, (char) 0);
440 ids = XOBFINISH (&id_obstack, char *);
441 sid = state_ident_by_name (ids, INSERT);
442 obstack_free (&id_obstack, NULL);
443 ids = NULL;
444 tk = XCNEW (struct state_token_st);
445 tk->stok_kind = STOK_NAME;
446 tk->stok_line = state_line;
447 tk->stok_col = curoff - state_bol;
448 tk->stok_file = state_path;
449 tk->stok_next = NULL;
450 tk->stok_un.stok_ident = sid;
452 return tk;
454 /* Read a string, dealing with escape sequences a la C! */
455 else if (c == '"')
457 char *cstr = NULL;
458 int cslen = 0;
459 struct obstack bstring_obstack;
460 obstack_init (&bstring_obstack);
461 curoff = ftell (state_file);
462 while ((c = getc (state_file)) != '"' && c >= 0)
464 if (ISPRINT (c) && c != '\\')
465 obstack_1grow (&bstring_obstack, (char) c);
466 else if (ISSPACE (c) && c != '\n')
467 obstack_1grow (&bstring_obstack, (char) c);
468 else if (c == '\\')
470 c = getc (state_file);
471 switch (c)
473 case 'a':
474 obstack_1grow (&bstring_obstack, '\a');
475 c = getc (state_file);
476 break;
477 case 'b':
478 obstack_1grow (&bstring_obstack, '\b');
479 c = getc (state_file);
480 break;
481 case 't':
482 obstack_1grow (&bstring_obstack, '\t');
483 c = getc (state_file);
484 break;
485 case 'n':
486 obstack_1grow (&bstring_obstack, '\n');
487 c = getc (state_file);
488 break;
489 case 'v':
490 obstack_1grow (&bstring_obstack, '\v');
491 c = getc (state_file);
492 break;
493 case 'f':
494 obstack_1grow (&bstring_obstack, '\f');
495 c = getc (state_file);
496 break;
497 case 'r':
498 obstack_1grow (&bstring_obstack, '\r');
499 c = getc (state_file);
500 break;
501 case '"':
502 obstack_1grow (&bstring_obstack, '\"');
503 c = getc (state_file);
504 break;
505 case '\\':
506 obstack_1grow (&bstring_obstack, '\\');
507 c = getc (state_file);
508 break;
509 case ' ':
510 obstack_1grow (&bstring_obstack, ' ');
511 c = getc (state_file);
512 break;
513 case 'x':
515 unsigned int cx = 0;
516 if (fscanf (state_file, "%02x", &cx) > 0 && cx > 0)
517 obstack_1grow (&bstring_obstack, cx);
518 else
519 fatal_reading_state
520 (NULL_STATE_TOKEN,
521 "Lexical error in string hex escape");
522 c = getc (state_file);
523 break;
525 default:
526 fatal_reading_state
527 (NULL_STATE_TOKEN,
528 "Lexical error - unknown string escape");
531 else
532 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error...");
534 if (c != '"')
535 fatal_reading_state (NULL_STATE_TOKEN, "Unterminated string");
536 obstack_1grow (&bstring_obstack, '\0');
537 cstr = XOBFINISH (&bstring_obstack, char *);
538 cslen = strlen (cstr);
539 tk = (struct state_token_st *)
540 xcalloc (sizeof (struct state_token_st) + cslen, 1);
541 tk->stok_kind = STOK_STRING;
542 tk->stok_line = state_line;
543 tk->stok_col = curoff - state_bol;
544 tk->stok_file = state_path;
545 tk->stok_next = NULL;
546 strcpy (tk->stok_un.stok_string, cstr);
547 obstack_free (&bstring_obstack, NULL);
549 return tk;
551 /* Got an unexpected character. */
552 fatal_reading_state_printf
553 (NULL_STATE_TOKEN,
554 "Lexical error at offset %ld - bad character \\%03o = '%c'",
555 ftell (state_file), c, c);
558 /* Used for lexical look-ahead. Retrieves the lexical token of rank
559 DEPTH, starting with 0 when reading the state file. Gives null on
560 end of file. */
561 static struct state_token_st *
562 peek_state_token (int depth)
564 int remdepth = depth;
565 struct state_token_st **ptoken = &state_token;
566 struct state_token_st *tok = NULL;
568 while (remdepth >= 0)
570 if (*ptoken == NULL)
572 *ptoken = tok = read_a_state_token ();
573 if (tok == NULL)
574 return NULL;
576 tok = *ptoken;
577 ptoken = &((*ptoken)->stok_next);
578 remdepth--;
581 return tok;
584 /* Consume the next DEPTH tokens and free them. */
585 static void
586 next_state_tokens (int depth)
588 struct state_token_st *n;
590 while (depth > 0)
592 if (state_token != NULL)
594 n = state_token->stok_next;
595 free (state_token);
596 state_token = n;
598 else
599 fatal_reading_state (NULL_STATE_TOKEN, "Tokens stack empty");
601 depth--;
605 /* Safely retrieve the lexical kind of a token. */
606 static inline enum state_token_en
607 state_token_kind (struct state_token_st *p)
609 if (p == NULL)
610 return STOK_NONE;
611 else
612 return p->stok_kind;
615 /* Test if a token is a given name i.e. an identifier. */
616 static inline bool
617 state_token_is_name (struct state_token_st *p, const char *name)
619 if (p == NULL)
620 return false;
622 if (p->stok_kind != STOK_NAME)
623 return false;
625 return !strcmp (p->stok_un.stok_ident->stid_name, name);
629 /* Following routines are useful for serializing datas.
631 * We want to serialize :
632 * - typedefs list
633 * - structures list
634 * - variables list
636 * So, we have one routine for each kind of data. The main writing
637 * routine is write_state. The main reading routine is
638 * read_state. Most writing routines write_state_FOO have a
639 * corresponding reading routine read_state_FOO. Reading is done in a
640 * recursive descending way, and any read error is fatal.
643 /* When reading the state, we need to remember the previously seen
644 types by their state_number, since GTY-ed types are usually
645 shared. */
646 static htab_t state_seen_types;
648 /* Return the length of a linked list made of pairs. */
649 static int pair_list_length (pair_p list);
651 /* Compute the length of a list of pairs, starting from the first
652 one. */
653 static int
654 pair_list_length (pair_p list)
656 int nbpair = 0;
657 pair_p l = NULL;
658 for (l = list; l; l = l->next)
659 nbpair++;
660 return nbpair;
663 /* Write a file location. Files relative to $(srcdir) are quite
664 frequent and are handled specially. This ensures that two gengtype
665 state file-s produced by gengtype on the same GCC source tree are
666 very similar and can be reasonably compared with diff, even if the
667 two GCC source trees have different absolute paths. */
668 void
669 state_writer::write_state_fileloc (struct fileloc *floc)
672 if (floc != NULL && floc->line > 0)
674 const char *srcrelpath = NULL;
675 gcc_assert (floc->file != NULL);
676 /* Most of the files are inside $(srcdir) so it is worth to
677 handle them specially. */
678 srcrelpath = get_file_srcdir_relative_path (floc->file);
679 if (srcrelpath != NULL)
681 begin_s_expr ("srcfileloc");
682 write_state_a_string (srcrelpath);
684 else
686 begin_s_expr ("fileloc");
687 write_state_a_string (get_input_file_name (floc->file));
689 fprintf (state_file, " %d", floc->line);
690 end_s_expr ();
692 else
693 fprintf (state_file, "nil ");
696 /* Write a list of fields. */
697 void
698 state_writer::write_state_fields (pair_p fields)
700 int nbfields = pair_list_length (fields);
701 int nbpairs = 0;
702 begin_s_expr ("fields");
703 fprintf (state_file, "%d ", nbfields);
704 nbpairs = write_state_pair_list (fields);
705 gcc_assert (nbpairs == nbfields);
706 end_s_expr ();
709 /* Write a null-terminated string in our lexical convention, very
710 similar to the convention of C. */
711 void
712 state_writer::write_state_a_string (const char *s)
714 char c;
716 write_any_indent (1);
718 fputs (" \"", state_file);
719 for (; *s != 0; s++)
721 c = *s;
722 switch (c)
724 case '\a':
725 fputs ("\\a", state_file);
726 break;
727 case '\b':
728 fputs ("\\b", state_file);
729 break;
730 case '\t':
731 fputs ("\\t", state_file);
732 break;
733 case '\n':
734 fputs ("\\n", state_file);
735 break;
736 case '\v':
737 fputs ("\\v", state_file);
738 break;
739 case '\f':
740 fputs ("\\f", state_file);
741 break;
742 case '\r':
743 fputs ("\\r", state_file);
744 break;
745 case '\"':
746 fputs ("\\\"", state_file);
747 break;
748 case '\\':
749 fputs ("\\\\", state_file);
750 break;
751 default:
752 if (ISPRINT (c))
753 putc (c, state_file);
754 else
755 fprintf (state_file, "\\x%02x", (unsigned) c);
758 fputs ("\"", state_file);
761 /* Our option-s have three kinds, each with its writer. */
762 void
763 state_writer::write_state_string_option (options_p current)
765 write_any_indent (0);
766 fprintf (state_file, "string ");
767 if (current->info.string != NULL)
768 write_state_a_string (current->info.string);
769 else
770 fprintf (state_file, " nil ");
773 void
774 state_writer::write_state_type_option (options_p current)
776 write_any_indent (0);
777 fprintf (state_file, "type ");
778 write_state_type (current->info.type);
781 void
782 state_writer::write_state_nested_option (options_p current)
784 write_any_indent (0);
785 fprintf (state_file, "nested ");
786 write_state_type (current->info.nested->type);
787 if (current->info.nested->convert_from != NULL)
788 write_state_a_string (current->info.nested->convert_from);
789 else
791 write_any_indent (1);
792 fprintf (state_file, " nil ");
795 if (current->info.nested->convert_to != NULL)
796 write_state_a_string (current->info.nested->convert_to);
797 else
799 write_any_indent (1);
800 fprintf (state_file, " nil ");
804 void
805 state_writer::write_state_option (options_p current)
807 begin_s_expr ("option");
809 write_any_indent (0);
810 if (current->name != NULL)
811 fprintf (state_file, "%s ", current->name);
812 else
813 fprintf (state_file, "nil ");
815 switch (current->kind)
817 case OPTION_STRING:
818 write_state_string_option (current);
819 break;
820 case OPTION_TYPE:
821 write_state_type_option (current);
822 break;
823 case OPTION_NESTED:
824 write_state_nested_option (current);
825 break;
826 default:
827 fatal ("Option tag unknown");
830 /* Terminate the "option" s-expression. */
831 end_s_expr ();
836 /* Write a list of GTY options. */
837 void
838 state_writer::write_state_options (options_p opt)
840 options_p current;
842 if (opt == NULL)
844 write_any_indent (0);
845 fprintf (state_file, "nil ");
846 return;
849 begin_s_expr ("options");
850 for (current = opt; current != NULL; current = current->next)
851 write_state_option (current);
852 end_s_expr ();
856 /* Write a bitmap representing a set of GCC front-end languages. */
857 void
858 state_writer::write_state_lang_bitmap (lang_bitmap bitmap)
860 write_any_indent (0);
861 fprintf (state_file, "%d ", (int) bitmap);
864 /* Write version information. */
865 void
866 state_writer::write_state_version (const char *version)
868 begin_s_expr ("version");
869 write_state_a_string (version);
870 end_s_expr ();
873 /* Write a scalar type. We have only two of these. */
874 void
875 state_writer::write_state_scalar_type (type_p current)
877 write_any_indent (0);
878 if (current == &scalar_nonchar)
879 fprintf (state_file, "scalar_nonchar ");
880 else if (current == &scalar_char)
881 fprintf (state_file, "scalar_char ");
882 else
883 fatal ("Unexpected type in write_state_scalar_type");
885 write_state_common_type_content (current);
888 /* Write the string type. There is only one such thing! */
889 void
890 state_writer::write_state_string_type (type_p current)
892 if (current == &string_type)
894 write_any_indent (0);
895 fprintf (state_file, "string ");
896 write_state_common_type_content (current);
898 else
899 fatal ("Unexpected type in write_state_string_type");
902 /* Write an undefined type. */
903 void
904 state_writer::write_state_undefined_type (type_p current)
906 DBGPRINTF ("undefined type @ %p #%d '%s'", (void *) current,
907 current->state_number, current->u.s.tag);
908 write_any_indent (0);
909 fprintf (state_file, "undefined ");
910 gcc_assert (current->gc_used == GC_UNUSED);
911 write_state_common_type_content (current);
912 if (current->u.s.tag != NULL)
913 write_state_a_string (current->u.s.tag);
914 else
916 write_any_indent (0);
917 fprintf (state_file, "nil");
920 write_state_fileloc (type_lineloc (current));
924 /* Common code to write structure like types. */
925 void
926 state_writer::write_state_struct_union_type (type_p current,
927 const char *kindstr)
929 DBGPRINTF ("%s type @ %p #%d '%s'", kindstr, (void *) current,
930 current->state_number, current->u.s.tag);
931 write_any_indent (0);
932 fprintf (state_file, "%s ", kindstr);
933 write_state_common_type_content (current);
934 if (current->u.s.tag != NULL)
935 write_state_a_string (current->u.s.tag);
936 else
938 write_any_indent (0);
939 fprintf (state_file, "nil");
942 write_state_fileloc (type_lineloc (current));
943 write_state_fields (current->u.s.fields);
944 write_state_options (current->u.s.opt);
945 write_state_lang_bitmap (current->u.s.bitmap);
949 /* Write a GTY struct type. */
950 void
951 state_writer::write_state_struct_type (type_p current)
953 write_state_struct_union_type (current, "struct");
954 write_state_type (current->u.s.lang_struct);
955 write_state_type (current->u.s.base_class);
958 /* Write a GTY user-defined struct type. */
959 void
960 state_writer::write_state_user_struct_type (type_p current)
962 DBGPRINTF ("user_struct type @ %p #%d '%s'", (void *) current,
963 current->state_number, current->u.s.tag);
964 write_any_indent (0);
965 fprintf (state_file, "user_struct ");
966 write_state_common_type_content (current);
967 if (current->u.s.tag != NULL)
968 write_state_a_string (current->u.s.tag);
969 else
971 write_any_indent (0);
972 fprintf (state_file, "nil");
974 write_state_fileloc (type_lineloc (current));
975 write_state_fields (current->u.s.fields);
978 /* write a GTY union type. */
979 void
980 state_writer::write_state_union_type (type_p current)
982 write_state_struct_union_type (current, "union");
983 write_state_type (current->u.s.lang_struct);
986 /* Write a lang_struct type. This is tricky and was painful to debug,
987 we deal with the next field specifically within their lang_struct
988 subfield, which points to a linked list of homonumous types.
989 Change this function with extreme care, see also
990 read_state_lang_struct_type. */
991 void
992 state_writer::write_state_lang_struct_type (type_p current)
994 int nbhomontype = 0;
995 type_p hty = NULL;
996 const char *homoname = 0;
997 write_state_struct_union_type (current, "lang_struct");
998 /* lang_struct-ures are particularly tricky, since their
999 u.s.lang_struct field gives a list of homonymous struct-s or
1000 union-s! */
1001 DBGPRINTF ("lang_struct @ %p #%d", (void *) current, current->state_number);
1002 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
1004 nbhomontype++;
1005 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype,
1006 (void *) hty, hty->state_number, hty->u.s.tag);
1007 /* Every member of the homonymous list should have the same tag. */
1008 gcc_assert (union_or_struct_p (hty));
1009 gcc_assert (hty->u.s.lang_struct == current);
1010 if (!homoname)
1011 homoname = hty->u.s.tag;
1012 gcc_assert (strcmp (homoname, hty->u.s.tag) == 0);
1014 begin_s_expr ("homotypes");
1015 fprintf (state_file, "%d", nbhomontype);
1016 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
1017 write_state_type (hty);
1018 end_s_expr ();
1021 /* Write a pointer type. */
1022 void
1023 state_writer::write_state_pointer_type (type_p current)
1025 write_any_indent (0);
1026 fprintf (state_file, "pointer ");
1027 write_state_common_type_content (current);
1028 write_state_type (current->u.p);
1031 /* Write an array type. */
1032 void
1033 state_writer::write_state_array_type (type_p current)
1035 write_any_indent (0);
1036 fprintf (state_file, "array ");
1037 write_state_common_type_content (current);
1038 if (current->u.a.len != NULL)
1039 write_state_a_string (current->u.a.len);
1040 else
1042 write_any_indent (1);
1043 fprintf (state_file, " nil");
1046 write_any_indent (1);
1047 fprintf (state_file, " ");
1048 write_state_type (current->u.a.p);
1051 /* Write the gc_used information. */
1052 void
1053 state_writer::write_state_gc_used (enum gc_used_enum gus)
1055 write_any_indent (1);
1056 switch (gus)
1058 case GC_UNUSED:
1059 fprintf (state_file, " gc_unused");
1060 break;
1061 case GC_USED:
1062 fprintf (state_file, " gc_used");
1063 break;
1064 case GC_MAYBE_POINTED_TO:
1065 fprintf (state_file, " gc_maybe_pointed_to");
1066 break;
1067 case GC_POINTED_TO:
1068 fprintf (state_file, " gc_pointed_to");
1069 break;
1070 default:
1071 gcc_unreachable ();
1075 /* Utility routine to write the common content of all types. Notice
1076 that the next field is *not* written on purpose. */
1077 void
1078 state_writer::write_state_common_type_content (type_p current)
1080 write_any_indent (0);
1081 fprintf (state_file, "%d ", current->state_number);
1082 /* We do not write the next type, because list of types are
1083 explicitly written. However, lang_struct are special in that
1084 respect. See function write_state_lang_struct_type for more. */
1085 write_state_type (current->pointer_to);
1086 write_state_gc_used (current->gc_used);
1090 /* The important and recursive routine writing GTY types as understood
1091 by gengtype. Types which have a positive state_number have already
1092 been seen and written. */
1093 void
1094 state_writer::write_state_type (type_p current)
1096 write_any_indent (0);
1097 if (current == NULL)
1099 fprintf (state_file, "nil ");
1100 return;
1103 begin_s_expr ("type");
1105 if (current->state_number > 0)
1107 write_any_indent (0);
1108 fprintf (state_file, "already_seen %d", current->state_number);
1110 else
1112 m_state_written_type_count++;
1113 DBGPRINTF ("writing type #%d @%p old number %d", m_state_written_type_count,
1114 (void *) current, current->state_number);
1115 current->state_number = m_state_written_type_count;
1116 switch (current->kind)
1118 case TYPE_NONE:
1119 gcc_unreachable ();
1120 case TYPE_UNDEFINED:
1121 write_state_undefined_type (current);
1122 break;
1123 case TYPE_STRUCT:
1124 write_state_struct_type (current);
1125 break;
1126 case TYPE_USER_STRUCT:
1127 write_state_user_struct_type (current);
1128 break;
1129 case TYPE_UNION:
1130 write_state_union_type (current);
1131 break;
1132 case TYPE_POINTER:
1133 write_state_pointer_type (current);
1134 break;
1135 case TYPE_ARRAY:
1136 write_state_array_type (current);
1137 break;
1138 case TYPE_LANG_STRUCT:
1139 write_state_lang_struct_type (current);
1140 break;
1141 case TYPE_SCALAR:
1142 write_state_scalar_type (current);
1143 break;
1144 case TYPE_STRING:
1145 write_state_string_type (current);
1146 break;
1150 /* Terminate the "type" s-expression. */
1151 end_s_expr ();
1155 /* Write a pair. */
1156 void
1157 state_writer::write_state_pair (pair_p current)
1159 if (current == NULL)
1161 write_any_indent (0);
1162 fprintf (state_file, "nil)");
1163 return;
1166 begin_s_expr ("pair");
1168 if (current->name != NULL)
1169 write_state_a_string (current->name);
1170 else
1171 write_state_a_string ("nil");
1173 write_state_type (current->type);
1174 write_state_fileloc (&(current->line));
1175 write_state_options (current->opt);
1177 /* Terminate the "pair" s-expression. */
1178 end_s_expr ();
1181 /* Write a pair list and return the number of pairs written. */
1183 state_writer::write_state_pair_list (pair_p list)
1185 int nbpair = 0;
1186 pair_p current;
1188 for (current = list; current != NULL; current = current->next)
1190 write_state_pair (current);
1191 nbpair++;
1193 return nbpair;
1197 /* When writing imported linked lists, like typedefs, structures, ... we count
1198 their length first and write it. This eases the reading, and enables an
1199 extra verification on the number of actually read items. */
1201 /* Write our typedefs. */
1202 void
1203 state_writer::write_state_typedefs (void)
1205 int nbtypedefs = pair_list_length (typedefs);
1206 int nbpairs = 0;
1207 begin_s_expr ("typedefs");
1208 fprintf (state_file, "%d", nbtypedefs);
1209 nbpairs = write_state_pair_list (typedefs);
1210 gcc_assert (nbpairs == nbtypedefs);
1211 end_s_expr ();
1212 if (verbosity_level >= 2)
1213 printf ("%s wrote %d typedefs\n", progname, nbtypedefs);
1216 /* Write our structures. */
1217 void
1218 state_writer::write_state_structures (void)
1220 int nbstruct = 0;
1221 type_p current;
1223 for (current = structures; current != NULL; current = current->next)
1224 nbstruct++;
1226 begin_s_expr ("structures");
1227 fprintf (state_file, "%d", nbstruct);
1229 for (current = structures; current != NULL; current = current->next)
1231 write_new_line ();
1232 write_state_type (current);
1235 /* Terminate the "structures" s-expression. */
1236 end_s_expr ();
1237 if (verbosity_level >= 2)
1238 printf ("%s wrote %d structures in state\n", progname, nbstruct);
1241 /* Write our variables. */
1242 void
1243 state_writer::write_state_variables (void)
1245 int nbvars = pair_list_length (variables);
1246 int nbpairs = 0;
1247 begin_s_expr ("variables");
1248 fprintf (state_file, "%d", nbvars);
1249 nbpairs = write_state_pair_list (variables);
1250 gcc_assert (nbpairs == nbvars);
1251 end_s_expr ();
1252 if (verbosity_level >= 2)
1253 printf ("%s wrote %d variables.\n", progname, nbvars);
1256 /* Write the source directory. File locations within the source
1257 directory have been written specifically. */
1258 void
1259 state_writer::write_state_srcdir (void)
1261 begin_s_expr ("srcdir");
1262 write_state_a_string (srcdir);
1263 end_s_expr ();
1266 /* Count and write the list of our files. */
1267 void
1268 state_writer::write_state_files_list (void)
1270 int i = 0;
1271 /* Write the list of files with their lang_bitmap. */
1272 begin_s_expr ("fileslist");
1273 fprintf (state_file, "%d", (int) num_gt_files);
1274 for (i = 0; i < (int) num_gt_files; i++)
1276 const char *cursrcrelpath = NULL;
1277 const input_file *curfil = gt_files[i];
1278 /* Most of the files are inside $(srcdir) so it is worth to
1279 handle them specially. */
1280 cursrcrelpath = get_file_srcdir_relative_path (curfil);
1281 if (cursrcrelpath)
1283 begin_s_expr ("srcfile");
1284 fprintf (state_file, "%d ", get_lang_bitmap (curfil));
1285 write_state_a_string (cursrcrelpath);
1287 else
1289 begin_s_expr ("file");
1290 fprintf (state_file, "%d ", get_lang_bitmap (curfil));
1291 write_state_a_string (get_input_file_name (curfil));
1293 /* Terminate the inner s-expression (either "srcfile" or "file"). */
1294 end_s_expr ();
1296 /* Terminate the "fileslist" s-expression. */
1297 end_s_expr ();
1300 /* Write the list of GCC front-end languages. */
1301 void
1302 state_writer::write_state_languages (void)
1304 int i = 0;
1305 begin_s_expr ("languages");
1306 fprintf (state_file, "%d", (int) num_lang_dirs);
1307 for (i = 0; i < (int) num_lang_dirs; i++)
1309 /* Languages names are identifiers, we expect only letters or
1310 underscores or digits in them. In particular, C++ is not a
1311 valid language name, but cp is valid. */
1312 fprintf (state_file, " %s", lang_dir_names[i]);
1314 end_s_expr ();
1317 /* Write the trailer. */
1318 static void
1319 write_state_trailer (void)
1321 /* This test should probably catch IO errors like disk full... */
1322 if (fputs ("\n(!endfile)\n", state_file) == EOF)
1323 fatal ("failed to write state trailer [%s]", xstrerror (errno));
1326 /* The write_state routine is the only writing routine called by main
1327 in gengtype.c. To avoid messing the state if gengtype is
1328 interrupted or aborted, we write a temporary file and rename it
1329 after having written it in totality. */
1330 void
1331 write_state (const char *state_path)
1333 long statelen = 0;
1334 time_t now = 0;
1335 char *temp_state_path = NULL;
1336 char tempsuffix[40];
1337 time (&now);
1339 /* We write a unique temporary file which is renamed when complete
1340 * only. So even if gengtype is interrupted, the written state file
1341 * won't be partially written, since the temporary file is not yet
1342 * renamed in that case. */
1343 memset (tempsuffix, 0, sizeof (tempsuffix));
1344 snprintf (tempsuffix, sizeof (tempsuffix) - 1, "-%ld-%d.tmp", (long) now,
1345 (int) getpid ());
1346 temp_state_path = concat (state_path, tempsuffix, NULL);
1347 state_file = fopen (temp_state_path, "w");
1348 if (state_file == NULL)
1349 fatal ("Failed to open file %s for writing state: %s",
1350 temp_state_path, xstrerror (errno));
1351 if (verbosity_level >= 3)
1352 printf ("%s writing state file %s temporarily in %s\n",
1353 progname, state_path, temp_state_path);
1354 /* This is the first line of the state. Perhaps the file utility
1355 could know about that, so don't change it often. */
1356 fprintf (state_file, ";;;;@@@@ GCC gengtype state\n");
1357 /* Output a few comments for humans. */
1358 fprintf (state_file,
1359 ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
1360 fprintf (state_file,
1361 ";;; The format of this file is tied to a particular version of GCC.\n");
1362 fprintf (state_file,
1363 ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1364 fprintf (state_file,
1365 ";;; This file should be parsed by the same %s which wrote it.\n",
1366 progname);
1368 state_writer sw;
1370 /* The first non-comment significant line gives the version string. */
1371 sw.write_state_version (version_string);
1372 sw.write_state_srcdir ();
1373 sw.write_state_languages ();
1374 sw.write_state_files_list ();
1375 sw.write_state_structures ();
1376 sw.write_state_typedefs ();
1377 sw.write_state_variables ();
1378 write_state_trailer ();
1379 statelen = ftell (state_file);
1380 if (ferror (state_file))
1381 fatal ("output error when writing state file %s [%s]",
1382 temp_state_path, xstrerror (errno));
1383 if (fclose (state_file))
1384 fatal ("failed to close state file %s [%s]",
1385 temp_state_path, xstrerror (errno));
1386 if (rename (temp_state_path, state_path))
1387 fatal ("failed to rename %s to state file %s [%s]", temp_state_path,
1388 state_path, xstrerror (errno));
1389 free (temp_state_path);
1391 if (verbosity_level >= 1)
1392 printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1393 progname, state_path, statelen, sw.m_state_written_type_count);
1397 /** End of writing routines! The corresponding reading routines follow. **/
1401 /* Forward declarations, since some read_state_* functions are
1402 recursive! */
1403 static void read_state_fileloc (struct fileloc *line);
1404 static void read_state_options (options_p *opt);
1405 static void read_state_type (type_p *current);
1406 static void read_state_pair (pair_p *pair);
1407 /* Return the number of pairs actually read. */
1408 static int read_state_pair_list (pair_p *list);
1409 static void read_state_fields (pair_p *fields);
1410 static void read_state_common_type_content (type_p current);
1415 /* Record into the state_seen_types hash-table a type which we are
1416 reading, to enable recursive or circular references to it. */
1417 static void
1418 record_type (type_p type)
1420 PTR *slot;
1422 slot = htab_find_slot (state_seen_types, type, INSERT);
1423 gcc_assert (slot);
1425 *slot = type;
1428 /* Read an already seen type. */
1429 static void
1430 read_state_already_seen_type (type_p *type)
1432 struct state_token_st *t0 = peek_state_token (0);
1434 if (state_token_kind (t0) == STOK_INTEGER)
1436 PTR *slot = NULL;
1437 struct type loctype = { TYPE_SCALAR, 0, 0, 0, GC_UNUSED, {0} };
1439 loctype.state_number = t0->stok_un.stok_num;
1440 slot = htab_find_slot (state_seen_types, &loctype, NO_INSERT);
1441 if (slot == NULL)
1443 fatal_reading_state (t0, "Unknown type");
1446 next_state_tokens (1);
1447 *type = (type_p) *slot;
1449 else
1451 fatal_reading_state (t0, "Bad seen type");
1456 /* Read the scalar_nonchar type. */
1457 static void
1458 read_state_scalar_nonchar_type (type_p *type)
1460 *type = &scalar_nonchar;
1461 read_state_common_type_content (*type);
1465 /* Read the scalar_char type. */
1466 static void
1467 read_state_scalar_char_type (type_p *type)
1469 *type = &scalar_char;
1470 read_state_common_type_content (*type);
1473 /* Read the string_type. */
1474 static void
1475 read_state_string_type (type_p *type)
1477 *type = &string_type;
1478 read_state_common_type_content (*type);
1482 /* Read a lang_bitmap representing a set of GCC front-end languages. */
1483 static void
1484 read_state_lang_bitmap (lang_bitmap *bitmap)
1486 struct state_token_st *t;
1488 t = peek_state_token (0);
1489 if (state_token_kind (t) == STOK_INTEGER)
1491 *bitmap = t->stok_un.stok_num;
1492 next_state_tokens (1);
1494 else
1496 fatal_reading_state (t, "Bad syntax for bitmap");
1501 /* Read an undefined type. */
1502 static void
1503 read_state_undefined_type (type_p type)
1505 struct state_token_st *t0;
1507 type->kind = TYPE_UNDEFINED;
1508 read_state_common_type_content (type);
1509 t0 = peek_state_token (0);
1510 if (state_token_kind (t0) == STOK_STRING)
1512 if (state_token_is_name (t0, "nil"))
1514 type->u.s.tag = NULL;
1515 DBGPRINTF ("read anonymous undefined type @%p #%d",
1516 (void *) type, type->state_number);
1518 else
1520 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1521 DBGPRINTF ("read undefined type @%p #%d '%s'",
1522 (void *) type, type->state_number, type->u.s.tag);
1525 next_state_tokens (1);
1526 read_state_fileloc (&(type->u.s.line));
1528 else
1530 fatal_reading_state (t0, "Bad tag in undefined type");
1535 /* Read a GTY-ed struct type. */
1536 static void
1537 read_state_struct_type (type_p type)
1539 struct state_token_st *t0;
1541 type->kind = TYPE_STRUCT;
1542 read_state_common_type_content (type);
1543 t0 = peek_state_token (0);
1544 if (state_token_kind (t0) == STOK_STRING)
1546 if (state_token_is_name (t0, "nil"))
1548 type->u.s.tag = NULL;
1549 DBGPRINTF ("read anonymous struct type @%p #%d",
1550 (void *) type, type->state_number);
1552 else
1554 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1555 DBGPRINTF ("read struct type @%p #%d '%s'",
1556 (void *) type, type->state_number, type->u.s.tag);
1559 next_state_tokens (1);
1560 read_state_fileloc (&(type->u.s.line));
1561 read_state_fields (&(type->u.s.fields));
1562 read_state_options (&(type->u.s.opt));
1563 read_state_lang_bitmap (&(type->u.s.bitmap));
1564 read_state_type (&(type->u.s.lang_struct));
1565 read_state_type (&(type->u.s.base_class));
1566 if (type->u.s.base_class)
1567 add_subclass (type->u.s.base_class, type);
1569 else
1571 fatal_reading_state (t0, "Bad tag in struct type");
1576 /* Read a GTY-ed user-provided struct TYPE. */
1578 static void
1579 read_state_user_struct_type (type_p type)
1581 struct state_token_st *t0;
1583 type->kind = TYPE_USER_STRUCT;
1584 read_state_common_type_content (type);
1585 t0 = peek_state_token (0);
1586 if (state_token_kind (t0) == STOK_STRING)
1588 if (state_token_is_name (t0, "nil"))
1590 type->u.s.tag = NULL;
1591 DBGPRINTF ("read anonymous struct type @%p #%d",
1592 (void *) type, type->state_number);
1594 else
1596 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1597 DBGPRINTF ("read struct type @%p #%d '%s'",
1598 (void *) type, type->state_number, type->u.s.tag);
1601 next_state_tokens (1);
1602 read_state_fileloc (&(type->u.s.line));
1603 read_state_fields (&(type->u.s.fields));
1605 else
1607 fatal_reading_state (t0, "Bad tag in user-struct type");
1612 /* Read a GTY-ed union type. */
1613 static void
1614 read_state_union_type (type_p type)
1616 struct state_token_st *t0;
1618 type->kind = TYPE_UNION;
1619 read_state_common_type_content (type);
1620 t0 = peek_state_token (0);
1621 if (state_token_kind (t0) == STOK_STRING)
1623 if (state_token_is_name (t0, "nil"))
1625 type->u.s.tag = NULL;
1626 DBGPRINTF ("read anonymous union type @%p #%d",
1627 (void *) type, type->state_number);
1629 else
1631 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1632 DBGPRINTF ("read union type @%p #%d '%s'",
1633 (void *) type, type->state_number, type->u.s.tag);
1635 next_state_tokens (1);
1636 read_state_fileloc (&(type->u.s.line));
1637 read_state_fields (&(type->u.s.fields));
1638 read_state_options (&(type->u.s.opt));
1639 read_state_lang_bitmap (&(type->u.s.bitmap));
1640 read_state_type (&(type->u.s.lang_struct));
1642 else
1643 fatal_reading_state (t0, "Bad tag in union type");
1647 /* Read a GTY-ed pointer type. */
1648 static void
1649 read_state_pointer_type (type_p type)
1651 type->kind = TYPE_POINTER;
1652 read_state_common_type_content (type);
1653 DBGPRINTF ("read pointer type @%p #%d", (void *) type, type->state_number);
1654 read_state_type (&(type->u.p));
1658 /* Read a GTY-ed array type. */
1659 static void
1660 read_state_array_type (type_p type)
1662 struct state_token_st *t0;
1664 type->kind = TYPE_ARRAY;
1665 read_state_common_type_content (type);
1666 t0 = peek_state_token (0);
1667 if (state_token_kind (t0) == STOK_STRING)
1669 type->u.a.len = xstrdup (t0->stok_un.stok_string);
1670 DBGPRINTF ("read array type @%p #%d length '%s'",
1671 (void *) type, type->state_number, type->u.a.len);
1672 next_state_tokens (1);
1675 else if (state_token_is_name (t0, "nil"))
1677 type->u.a.len = NULL;
1678 DBGPRINTF ("read array type @%p #%d without length",
1679 (void *) type, type->state_number);
1680 next_state_tokens (1);
1683 else
1684 fatal_reading_state (t0, "Bad array name type");
1685 read_state_type (&(type->u.a.p));
1690 /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
1691 front-end languages. This is a tricky function and it was painful
1692 to debug. Change it with extreme care. See also
1693 write_state_lang_struct_type. */
1694 static void
1695 read_state_lang_struct_type (type_p type)
1697 struct state_token_st *t0 = NULL;
1698 struct state_token_st *t1 = NULL;
1699 struct state_token_st *t2 = NULL;
1701 type->kind = TYPE_LANG_STRUCT;
1702 read_state_common_type_content (type);
1703 t0 = peek_state_token (0);
1704 if (state_token_kind (t0) == STOK_STRING)
1706 if (state_token_is_name (t0, "nil"))
1708 DBGPRINTF ("read anonymous lang_struct type @%p #%d",
1709 (void *) type, type->state_number);
1710 type->u.s.tag = NULL;
1712 else
1714 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1715 DBGPRINTF ("read lang_struct type @%p #%d '%s'",
1716 (void *) type, type->state_number, type->u.s.tag);
1718 next_state_tokens (1);
1720 else
1721 fatal_reading_state (t0, "Bad tag in lang struct type");
1722 read_state_fileloc (&(type->u.s.line));
1723 read_state_fields (&(type->u.s.fields));
1724 read_state_options (&(type->u.s.opt));
1725 read_state_lang_bitmap (&(type->u.s.bitmap));
1726 /* Within lang_struct-ures, the lang_struct field is a linked list
1727 of homonymous types! */
1728 t0 = peek_state_token (0);
1729 t1 = peek_state_token (1);
1730 t2 = peek_state_token (2);
1731 /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
1732 if (state_token_kind (t0) == STOK_LEFTPAR
1733 && state_token_is_name (t1, "!homotypes")
1734 && state_token_kind (t2) == STOK_INTEGER)
1736 type_p *prevty = &type->u.s.lang_struct;
1737 int nbhomotype = t2->stok_un.stok_num;
1738 int i = 0;
1739 t0 = t1 = t2 = NULL;
1740 next_state_tokens (3);
1741 for (i = 0; i < nbhomotype; i++)
1743 read_state_type (prevty);
1744 t0 = peek_state_token (0);
1745 if (*prevty)
1746 prevty = &(*prevty)->next;
1747 else
1748 fatal_reading_state (t0,
1749 "expecting type in homotype list for lang_struct");
1751 if (state_token_kind (t0) != STOK_RIGHTPAR)
1752 fatal_reading_state (t0,
1753 "expecting ) in homotype list for lang_struct");
1754 next_state_tokens (1);
1756 else
1757 fatal_reading_state (t0, "expecting !homotypes for lang_struct");
1761 /* Read the gc used information. */
1762 static void
1763 read_state_gc_used (enum gc_used_enum *pgus)
1765 struct state_token_st *t0 = peek_state_token (0);
1766 if (state_token_is_name (t0, "gc_unused"))
1767 *pgus = GC_UNUSED;
1768 else if (state_token_is_name (t0, "gc_used"))
1769 *pgus = GC_USED;
1770 else if (state_token_is_name (t0, "gc_maybe_pointed_to"))
1771 *pgus = GC_MAYBE_POINTED_TO;
1772 else if (state_token_is_name (t0, "gc_pointed_to"))
1773 *pgus = GC_POINTED_TO;
1774 else
1775 fatal_reading_state (t0, "invalid gc_used information");
1776 next_state_tokens (1);
1780 /* Utility function to read the common content of types. */
1781 static void
1782 read_state_common_type_content (type_p current)
1784 struct state_token_st *t0 = peek_state_token (0);
1786 if (state_token_kind (t0) == STOK_INTEGER)
1788 current->state_number = t0->stok_un.stok_num;
1789 next_state_tokens (1);
1790 record_type (current);
1792 else
1793 fatal_reading_state_printf (t0,
1794 "Expected integer for state_number line %d",
1795 state_line);
1796 /* We don't read the next field of the type. */
1797 read_state_type (&current->pointer_to);
1798 read_state_gc_used (&current->gc_used);
1802 /* Read a GTY-ed type. */
1803 void
1804 read_state_type (type_p *current)
1806 struct state_token_st *t0 = peek_state_token (0);
1807 struct state_token_st *t1 = peek_state_token (1);
1809 if (state_token_kind (t0) == STOK_LEFTPAR &&
1810 state_token_is_name (t1, "!type"))
1812 next_state_tokens (2);
1813 t0 = peek_state_token (0);
1814 if (state_token_is_name (t0, "already_seen"))
1816 next_state_tokens (1);
1817 read_state_already_seen_type (current);
1819 else
1821 t0 = peek_state_token (0);
1823 if (state_token_is_name (t0, "scalar_nonchar"))
1825 next_state_tokens (1);
1826 read_state_scalar_nonchar_type (current);
1828 else if (state_token_is_name (t0, "scalar_char"))
1830 next_state_tokens (1);
1831 read_state_scalar_char_type (current);
1833 else if (state_token_is_name (t0, "string"))
1835 next_state_tokens (1);
1836 read_state_string_type (current);
1838 else if (state_token_is_name (t0, "undefined"))
1840 *current = XCNEW (struct type);
1841 next_state_tokens (1);
1842 read_state_undefined_type (*current);
1844 else if (state_token_is_name (t0, "struct"))
1846 *current = XCNEW (struct type);
1847 next_state_tokens (1);
1848 read_state_struct_type (*current);
1850 else if (state_token_is_name (t0, "union"))
1852 *current = XCNEW (struct type);
1853 next_state_tokens (1);
1854 read_state_union_type (*current);
1856 else if (state_token_is_name (t0, "lang_struct"))
1858 *current = XCNEW (struct type);
1859 next_state_tokens (1);
1860 read_state_lang_struct_type (*current);
1862 else if (state_token_is_name (t0, "pointer"))
1864 *current = XCNEW (struct type);
1865 next_state_tokens (1);
1866 read_state_pointer_type (*current);
1868 else if (state_token_is_name (t0, "array"))
1870 *current = XCNEW (struct type);
1871 next_state_tokens (1);
1872 read_state_array_type (*current);
1874 else if (state_token_is_name (t0, "user_struct"))
1876 *current = XCNEW (struct type);
1877 next_state_tokens (1);
1878 read_state_user_struct_type (*current);
1880 else
1881 fatal_reading_state (t0, "bad type in (!type");
1883 t0 = peek_state_token (0);
1884 if (state_token_kind (t0) != STOK_RIGHTPAR)
1885 fatal_reading_state (t0, "missing ) in type");
1886 next_state_tokens (1);
1888 else if (state_token_is_name (t0, "nil"))
1890 next_state_tokens (1);
1891 *current = NULL;
1893 else
1894 fatal_reading_state (t0, "bad type syntax");
1898 /* Read a file location. Files within the source directory are dealt
1899 with specifically. */
1900 void
1901 read_state_fileloc (struct fileloc *floc)
1903 bool issrcfile = false;
1904 struct state_token_st *t0 = peek_state_token (0);
1905 struct state_token_st *t1 = peek_state_token (1);
1907 gcc_assert (floc != NULL);
1908 gcc_assert (srcdir != NULL);
1910 if (state_token_kind (t0) == STOK_LEFTPAR &&
1911 (state_token_is_name (t1, "!fileloc")
1912 || (issrcfile = state_token_is_name (t1, "!srcfileloc"))))
1914 next_state_tokens (2);
1915 t0 = peek_state_token (0);
1916 t1 = peek_state_token (1);
1917 if (state_token_kind (t0) == STOK_STRING &&
1918 state_token_kind (t1) == STOK_INTEGER)
1920 char *path = t0->stok_un.stok_string;
1921 if (issrcfile)
1923 static const char dirsepstr[2] = { DIR_SEPARATOR, (char) 0 };
1924 char *fullpath = concat (srcdir, dirsepstr, path, NULL);
1925 floc->file = input_file_by_name (fullpath);
1926 free (fullpath);
1928 else
1929 floc->file = input_file_by_name (path);
1930 floc->line = t1->stok_un.stok_num;
1931 next_state_tokens (2);
1933 else
1934 fatal_reading_state (t0,
1935 "Bad fileloc syntax, expected path string and line");
1936 t0 = peek_state_token (0);
1937 if (state_token_kind (t0) != STOK_RIGHTPAR)
1938 fatal_reading_state (t0, "Bad fileloc syntax, expected )");
1939 next_state_tokens (1);
1941 else if (state_token_is_name (t0, "nil"))
1943 next_state_tokens (1);
1944 floc->file = NULL;
1945 floc->line = 0;
1947 else
1948 fatal_reading_state (t0, "Bad fileloc syntax");
1952 /* Read the fields of a GTY-ed type. */
1953 void
1954 read_state_fields (pair_p *fields)
1956 pair_p tmp = NULL;
1957 struct state_token_st *t0 = peek_state_token (0);
1958 struct state_token_st *t1 = peek_state_token (1);
1959 struct state_token_st *t2 = peek_state_token (2);
1961 if (state_token_kind (t0) == STOK_LEFTPAR
1962 && state_token_is_name (t1, "!fields")
1963 && state_token_kind (t2) == STOK_INTEGER)
1965 int nbfields = t2->stok_un.stok_num;
1966 int nbpairs = 0;
1967 next_state_tokens (3);
1968 nbpairs = read_state_pair_list (&tmp);
1969 t0 = peek_state_token (0);
1970 if (nbpairs != nbfields)
1971 fatal_reading_state_printf
1972 (t0,
1973 "Mismatched fields number, expected %d got %d", nbpairs, nbfields);
1974 if (state_token_kind (t0) == STOK_RIGHTPAR)
1975 next_state_tokens (1);
1976 else
1977 fatal_reading_state (t0, "Bad fields expecting )");
1980 *fields = tmp;
1984 /* Read a string option. */
1985 static void
1986 read_state_string_option (options_p opt)
1988 struct state_token_st *t0 = peek_state_token (0);
1989 opt->kind = OPTION_STRING;
1990 if (state_token_kind (t0) == STOK_STRING)
1992 opt->info.string = xstrdup (t0->stok_un.stok_string);
1993 next_state_tokens (1);
1995 else if (state_token_is_name (t0, "nil"))
1997 opt->info.string = NULL;
1998 next_state_tokens (1);
2000 else
2001 fatal_reading_state (t0, "Missing name in string option");
2005 /* Read a type option. */
2006 static void
2007 read_state_type_option (options_p opt)
2009 opt->kind = OPTION_TYPE;
2010 read_state_type (&(opt->info.type));
2014 /* Read a nested option. */
2015 static void
2016 read_state_nested_option (options_p opt)
2018 struct state_token_st *t0;
2020 opt->info.nested = XCNEW (struct nested_ptr_data);
2021 opt->kind = OPTION_NESTED;
2022 read_state_type (&(opt->info.nested->type));
2023 t0 = peek_state_token (0);
2024 if (state_token_kind (t0) == STOK_STRING)
2026 opt->info.nested->convert_from = xstrdup (t0->stok_un.stok_string);
2027 next_state_tokens (1);
2029 else if (state_token_is_name (t0, "nil"))
2031 opt->info.nested->convert_from = NULL;
2032 next_state_tokens (1);
2034 else
2035 fatal_reading_state (t0, "Bad nested convert_from option");
2037 t0 = peek_state_token (0);
2038 if (state_token_kind (t0) == STOK_STRING)
2040 opt->info.nested->convert_to = xstrdup (t0->stok_un.stok_string);
2041 next_state_tokens (1);
2043 else if (state_token_is_name (t0, "nil"))
2045 opt->info.nested->convert_to = NULL;
2046 next_state_tokens (1);
2048 else
2049 fatal_reading_state (t0, "Bad nested convert_from option");
2053 /* Read an GTY option. */
2054 static void
2055 read_state_option (options_p *opt)
2057 struct state_token_st *t0 = peek_state_token (0);
2058 struct state_token_st *t1 = peek_state_token (1);
2060 if (state_token_kind (t0) == STOK_LEFTPAR &&
2061 state_token_is_name (t1, "!option"))
2063 next_state_tokens (2);
2064 t0 = peek_state_token (0);
2065 if (state_token_kind (t0) == STOK_NAME)
2067 *opt = XCNEW (struct options);
2068 if (state_token_is_name (t0, "nil"))
2069 (*opt)->name = NULL;
2070 else
2071 (*opt)->name = t0->stok_un.stok_ident->stid_name;
2072 next_state_tokens (1);
2073 t0 = peek_state_token (0);
2074 if (state_token_kind (t0) == STOK_NAME)
2076 if (state_token_is_name (t0, "string"))
2078 next_state_tokens (1);
2079 read_state_string_option (*opt);
2081 else if (state_token_is_name (t0, "type"))
2083 next_state_tokens (1);
2084 read_state_type_option (*opt);
2086 else if (state_token_is_name (t0, "nested"))
2088 next_state_tokens (1);
2089 read_state_nested_option (*opt);
2091 else
2092 fatal_reading_state (t0, "Bad option type");
2093 t0 = peek_state_token (0);
2094 if (state_token_kind (t0) != STOK_RIGHTPAR)
2095 fatal_reading_state (t0, "Bad syntax in option, expecting )");
2097 next_state_tokens (1);
2099 else
2100 fatal_reading_state (t0, "Missing option type");
2102 else
2103 fatal_reading_state (t0, "Bad name for option");
2105 else
2106 fatal_reading_state (t0, "Bad option, waiting for )");
2109 /* Read a list of options. */
2110 void
2111 read_state_options (options_p *opt)
2113 options_p head = NULL;
2114 options_p previous = NULL;
2115 options_p current_option = NULL;
2116 struct state_token_st *t0 = peek_state_token (0);
2117 struct state_token_st *t1 = peek_state_token (1);
2119 if (state_token_kind (t0) == STOK_LEFTPAR &&
2120 state_token_is_name (t1, "!options"))
2122 next_state_tokens (2);
2123 t0 = peek_state_token (0);
2124 while (state_token_kind (t0) != STOK_RIGHTPAR)
2126 read_state_option (&current_option);
2127 if (head == NULL)
2129 head = current_option;
2130 previous = head;
2132 else
2134 previous->next = current_option;
2135 previous = current_option;
2137 t0 = peek_state_token (0);
2139 next_state_tokens (1);
2141 else if (state_token_is_name (t0, "nil"))
2143 next_state_tokens (1);
2145 else
2146 fatal_reading_state (t0, "Bad options syntax");
2148 *opt = head;
2152 /* Read a version, and check against the version of the gengtype. */
2153 static void
2154 read_state_version (const char *version_string)
2156 struct state_token_st *t0 = peek_state_token (0);
2157 struct state_token_st *t1 = peek_state_token (1);
2159 if (state_token_kind (t0) == STOK_LEFTPAR &&
2160 state_token_is_name (t1, "!version"))
2162 next_state_tokens (2);
2163 t0 = peek_state_token (0);
2164 t1 = peek_state_token (1);
2165 if (state_token_kind (t0) == STOK_STRING &&
2166 state_token_kind (t1) == STOK_RIGHTPAR)
2168 /* Check that the read version string is the same as current
2169 version. */
2170 if (strcmp (version_string, t0->stok_un.stok_string))
2171 fatal_reading_state_printf (t0,
2172 "version string mismatch; expecting %s but got %s",
2173 version_string,
2174 t0->stok_un.stok_string);
2175 next_state_tokens (2);
2177 else
2178 fatal_reading_state (t0, "Missing version or right parenthesis");
2180 else
2181 fatal_reading_state (t0, "Bad version syntax");
2185 /* Read a pair. */
2186 void
2187 read_state_pair (pair_p *current)
2189 struct state_token_st *t0 = peek_state_token (0);
2190 struct state_token_st *t1 = peek_state_token (1);
2191 if (state_token_kind (t0) == STOK_LEFTPAR &&
2192 state_token_is_name (t1, "!pair"))
2194 *current = XCNEW (struct pair);
2195 next_state_tokens (2);
2196 t0 = peek_state_token (0);
2197 if (state_token_kind (t0) == STOK_STRING)
2199 if (strcmp (t0->stok_un.stok_string, "nil") == 0)
2201 (*current)->name = NULL;
2203 else
2205 (*current)->name = xstrdup (t0->stok_un.stok_string);
2207 next_state_tokens (1);
2208 read_state_type (&((*current)->type));
2209 read_state_fileloc (&((*current)->line));
2210 read_state_options (&((*current)->opt));;
2211 t0 = peek_state_token (0);
2212 if (state_token_kind (t0) == STOK_RIGHTPAR)
2214 next_state_tokens (1);
2216 else
2218 fatal_reading_state (t0, "Bad syntax for pair, )");
2221 else
2223 fatal_reading_state (t0, "Bad name for pair");
2226 else if (state_token_kind (t0) == STOK_NAME &&
2227 state_token_is_name (t0, "nil"))
2229 next_state_tokens (1);
2230 *current = NULL;
2232 else
2233 fatal_reading_state_printf (t0, "Bad syntax for pair, (!pair %d",
2234 state_token->stok_kind);
2238 /* Return the number of pairs actually read. */
2240 read_state_pair_list (pair_p *list)
2242 int nbpair = 0;
2243 pair_p head = NULL;
2244 pair_p previous = NULL;
2245 pair_p tmp = NULL;
2246 struct state_token_st *t0 = peek_state_token (0);
2247 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2249 read_state_pair (&tmp);
2250 if (head == NULL)
2252 head = tmp;
2253 previous = head;
2255 else
2257 previous->next = tmp;
2258 previous = tmp;
2260 t0 = peek_state_token (0);
2261 nbpair++;
2264 /* don't consume the ); the caller will eat it. */
2265 *list = head;
2266 return nbpair;
2269 /* Read the typedefs. */
2270 static void
2271 read_state_typedefs (pair_p *typedefs)
2273 int nbtypedefs = 0;
2274 pair_p list = NULL;
2275 struct state_token_st *t0 = peek_state_token (0);
2276 struct state_token_st *t1 = peek_state_token (1);
2277 struct state_token_st *t2 = peek_state_token (2);
2279 if (state_token_kind (t0) == STOK_LEFTPAR
2280 && state_token_is_name (t1, "!typedefs")
2281 && state_token_kind (t2) == STOK_INTEGER)
2283 int nbpairs = 0;
2284 nbtypedefs = t2->stok_un.stok_num;
2285 next_state_tokens (3);
2286 nbpairs = read_state_pair_list (&list);
2287 t0 = peek_state_token (0);
2288 if (nbpairs != nbtypedefs)
2289 fatal_reading_state_printf
2290 (t0,
2291 "invalid number of typedefs, expected %d but got %d",
2292 nbtypedefs, nbpairs);
2293 if (state_token_kind (t0) == STOK_RIGHTPAR)
2294 next_state_tokens (1);
2295 else
2296 fatal_reading_state (t0, "Bad typedefs syntax )");
2298 else
2299 fatal_reading_state (t0, "Bad typedefs syntax (!typedefs");
2301 if (verbosity_level >= 2)
2302 printf ("%s read %d typedefs from state\n", progname, nbtypedefs);
2303 *typedefs = list;
2307 /* Read the structures. */
2308 static void
2309 read_state_structures (type_p *structures)
2311 type_p head = NULL;
2312 type_p previous = NULL;
2313 type_p tmp;
2314 int nbstruct = 0, countstruct = 0;
2315 struct state_token_st *t0 = peek_state_token (0);
2316 struct state_token_st *t1 = peek_state_token (1);
2317 struct state_token_st *t2 = peek_state_token (2);
2319 if (state_token_kind (t0) == STOK_LEFTPAR
2320 && state_token_is_name (t1, "!structures")
2321 && state_token_kind (t2) == STOK_INTEGER)
2323 nbstruct = t2->stok_un.stok_num;
2324 next_state_tokens (3);
2325 t0 = peek_state_token (0);
2326 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2328 tmp = NULL;
2329 read_state_type (&tmp);
2330 countstruct++;
2331 if (head == NULL)
2333 head = tmp;
2334 previous = head;
2336 else
2338 previous->next = tmp;
2339 previous = tmp;
2341 t0 = peek_state_token (0);
2343 next_state_tokens (1);
2345 else
2346 fatal_reading_state (t0, "Bad structures syntax");
2347 if (countstruct != nbstruct)
2348 fatal_reading_state_printf (NULL_STATE_TOKEN,
2349 "expected %d structures but got %d",
2350 nbstruct, countstruct);
2351 if (verbosity_level >= 2)
2352 printf ("%s read %d structures from state\n", progname, nbstruct);
2353 *structures = head;
2357 /* Read the variables. */
2358 static void
2359 read_state_variables (pair_p *variables)
2361 pair_p list = NULL;
2362 int nbvars = 0;
2363 struct state_token_st *t0 = peek_state_token (0);
2364 struct state_token_st *t1 = peek_state_token (1);
2365 struct state_token_st *t2 = peek_state_token (2);
2367 if (state_token_kind (t0) == STOK_LEFTPAR
2368 && state_token_is_name (t1, "!variables")
2369 && state_token_kind (t2) == STOK_INTEGER)
2371 int nbpairs = 0;
2372 nbvars = t2->stok_un.stok_num;
2373 next_state_tokens (3);
2374 nbpairs = read_state_pair_list (&list);
2375 t0 = peek_state_token (0);
2376 if (nbpairs != nbvars)
2377 fatal_reading_state_printf
2378 (t0, "Invalid number of variables, expected %d but got %d",
2379 nbvars, nbpairs);
2380 if (state_token_kind (t0) == STOK_RIGHTPAR)
2381 next_state_tokens (1);
2382 else
2383 fatal_reading_state (t0, "Waiting for ) in variables");
2385 else
2386 fatal_reading_state (t0, "Bad variables syntax");
2387 *variables = list;
2388 if (verbosity_level >= 2)
2389 printf ("%s read %d variables from state\n", progname, nbvars);
2393 /* Read the source directory. */
2394 static void
2395 read_state_srcdir (void)
2397 struct state_token_st *t0 = peek_state_token (0);
2398 struct state_token_st *t1 = peek_state_token (1);
2399 if (state_token_kind (t0) == STOK_LEFTPAR &&
2400 state_token_is_name (t1, "!srcdir"))
2402 next_state_tokens (2);
2403 t0 = peek_state_token (0);
2404 t1 = peek_state_token (1);
2405 if (state_token_kind (t0) == STOK_STRING &&
2406 state_token_kind (t1) == STOK_RIGHTPAR)
2408 srcdir = xstrdup (t0->stok_un.stok_string);
2409 srcdir_len = strlen (srcdir);
2410 next_state_tokens (2);
2411 return;
2415 fatal_reading_state (t0, "Bad srcdir in state_file");
2419 /* Read the sequence of GCC front-end languages. */
2420 static void
2421 read_state_languages (void)
2423 struct state_token_st *t0 = peek_state_token (0);
2424 struct state_token_st *t1 = peek_state_token (1);
2425 struct state_token_st *t2 = peek_state_token (2);
2426 if (state_token_kind (t0) == STOK_LEFTPAR
2427 && state_token_is_name (t1, "!languages")
2428 && state_token_kind (t2) == STOK_INTEGER)
2430 int i = 0;
2431 num_lang_dirs = t2->stok_un.stok_num;
2432 lang_dir_names = XCNEWVEC (const char *, num_lang_dirs);
2433 next_state_tokens (3);
2434 t0 = t1 = t2 = NULL;
2435 for (i = 0; i < (int) num_lang_dirs; i++)
2437 t0 = peek_state_token (0);
2438 if (state_token_kind (t0) != STOK_NAME)
2439 fatal_reading_state (t0, "expecting language name in state file");
2440 lang_dir_names[i] = t0->stok_un.stok_ident->stid_name;
2441 next_state_tokens (1);
2443 t0 = peek_state_token (0);
2444 if (state_token_kind (t0) != STOK_RIGHTPAR)
2445 fatal_reading_state (t0, "missing ) in languages list of state file");
2446 next_state_tokens (1);
2448 else
2449 fatal_reading_state (t0, "expecting languages list in state file");
2453 /* Read the sequence of files. */
2454 static void
2455 read_state_files_list (void)
2457 struct state_token_st *t0 = peek_state_token (0);
2458 struct state_token_st *t1 = peek_state_token (1);
2459 struct state_token_st *t2 = peek_state_token (2);
2461 if (state_token_kind (t0) == STOK_LEFTPAR
2462 && state_token_is_name (t1, "!fileslist")
2463 && state_token_kind (t2) == STOK_INTEGER)
2465 int i = 0;
2466 num_gt_files = t2->stok_un.stok_num;
2467 next_state_tokens (3);
2468 t0 = t1 = t2 = NULL;
2469 gt_files = XCNEWVEC (const input_file *, num_gt_files);
2470 for (i = 0; i < (int) num_gt_files; i++)
2472 bool issrcfile = FALSE;
2473 t0 = t1 = t2 = NULL;
2474 t0 = peek_state_token (0);
2475 t1 = peek_state_token (1);
2476 t2 = peek_state_token (2);
2477 if (state_token_kind (t0) == STOK_LEFTPAR
2478 && (state_token_is_name (t1, "!file")
2479 || (issrcfile = state_token_is_name (t1, "!srcfile")))
2480 && state_token_kind (t2) == STOK_INTEGER)
2482 lang_bitmap bmap = t2->stok_un.stok_num;
2483 next_state_tokens (3);
2484 t0 = t1 = t2 = NULL;
2485 t0 = peek_state_token (0);
2486 t1 = peek_state_token (1);
2487 if (state_token_kind (t0) == STOK_STRING
2488 && state_token_kind (t1) == STOK_RIGHTPAR)
2490 const char *fnam = t0->stok_un.stok_string;
2491 /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
2492 input_file *curgt = NULL;
2493 if (issrcfile)
2495 static const char dirsepstr[2] =
2496 { DIR_SEPARATOR, (char) 0 };
2497 char *fullpath = concat (srcdir, dirsepstr, fnam, NULL);
2498 curgt = input_file_by_name (fullpath);
2499 free (fullpath);
2501 else
2502 curgt = input_file_by_name (fnam);
2503 set_lang_bitmap (curgt, bmap);
2504 gt_files[i] = curgt;
2505 next_state_tokens (2);
2507 else
2508 fatal_reading_state (t0,
2509 "bad file in !fileslist of state file");
2511 else
2512 fatal_reading_state (t0,
2513 "expecting file in !fileslist of state file");
2515 t0 = peek_state_token (0);
2516 if (state_token_kind (t0) != STOK_RIGHTPAR)
2517 fatal_reading_state (t0, "missing ) for !fileslist in state file");
2518 next_state_tokens (1);
2520 else
2521 fatal_reading_state (t0, "missing !fileslist in state file");
2525 /* Read the trailer. */
2526 static void
2527 read_state_trailer (void)
2529 struct state_token_st *t0 = peek_state_token (0);
2530 struct state_token_st *t1 = peek_state_token (1);
2531 struct state_token_st *t2 = peek_state_token (2);
2533 if (state_token_kind (t0) == STOK_LEFTPAR
2534 && state_token_is_name (t1, "!endfile")
2535 && state_token_kind (t2) == STOK_RIGHTPAR)
2536 next_state_tokens (3);
2537 else
2538 fatal_reading_state (t0, "missing !endfile in state file");
2542 /* Utility functions for the state_seen_types hash table. */
2543 static unsigned
2544 hash_type_number (const void *ty)
2546 const struct type *type = (const struct type *) ty;
2548 return type->state_number;
2551 static int
2552 equals_type_number (const void *ty1, const void *ty2)
2554 const struct type *type1 = (const struct type *) ty1;
2555 const struct type *type2 = (const struct type *) ty2;
2557 return type1->state_number == type2->state_number;
2560 static int
2561 string_eq (const void *a, const void *b)
2563 const char *a0 = (const char *)a;
2564 const char *b0 = (const char *)b;
2566 return (strcmp (a0, b0) == 0);
2570 /* The function reading the state, called by main from gengtype.c. */
2571 void
2572 read_state (const char *path)
2574 state_file = fopen (path, "r");
2575 if (state_file == NULL)
2576 fatal ("Failed to open state file %s for reading [%s]", path,
2577 xstrerror (errno));
2578 state_path = path;
2579 state_line = 1;
2581 if (verbosity_level >= 1)
2583 printf ("%s reading state file %s;", progname, state_path);
2584 if (verbosity_level >= 2)
2585 putchar ('\n');
2586 fflush (stdout);
2589 state_seen_types =
2590 htab_create (2017, hash_type_number, equals_type_number, NULL);
2591 state_ident_tab =
2592 htab_create (4027, htab_hash_string, string_eq, NULL);
2593 read_state_version (version_string);
2594 read_state_srcdir ();
2595 read_state_languages ();
2596 read_state_files_list ();
2597 read_state_structures (&structures);
2598 if (ferror (state_file))
2599 fatal_reading_state_printf
2600 (NULL_STATE_TOKEN, "input error while reading state [%s]",
2601 xstrerror (errno));
2602 read_state_typedefs (&typedefs);
2603 read_state_variables (&variables);
2604 read_state_trailer ();
2606 if (verbosity_level >= 1)
2608 printf ("%s read %ld bytes.\n", progname, ftell (state_file));
2609 fflush (stdout);
2612 if (fclose (state_file))
2613 fatal ("failed to close read state file %s [%s]",
2614 path, xstrerror (errno));
2615 state_file = NULL;
2616 state_path = NULL;
2619 /* End of file gengtype-state.c. */