gcc/testsuite/
[official-gcc.git] / gcc / gengtype-state.c
blobe3317ec36a43787e61e3d5356dc7c40a9319e78b
1 /* Gengtype persistent state serialization & de-serialization.
2 Useful for gengtype in plugin mode.
4 Copyright (C) 2010 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 GENERATOR_FILE
27 #include "bconfig.h"
28 #else
29 #include "config.h"
30 #endif
31 #include "system.h"
32 #include "errors.h" /* For fatal. */
33 #include "double-int.h"
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 return CONST_CAST (struct fileloc*, &ty->u.s.line);
56 case TYPE_PARAM_STRUCT:
57 return CONST_CAST (struct fileloc*, &ty->u.param_struct.line);
58 case TYPE_SCALAR:
59 case TYPE_STRING:
60 case TYPE_POINTER:
61 case TYPE_ARRAY:
62 return NULL;
63 default:
64 gcc_unreachable ();
68 /* The state file has simplistic lispy lexical tokens. Its lexer gives
69 a linked list of struct state_token_st, through the peek_state_token
70 function. Lexical tokens are consumed with next_state_tokens. */
73 /* The lexical kind of each lispy token. */
74 enum state_token_en
76 STOK_NONE, /* Never used. */
77 STOK_INTEGER, /* Integer token. */
78 STOK_STRING, /* String token. */
79 STOK_LEFTPAR, /* Left opening parenthesis. */
80 STOK_RIGHTPAR, /* Right closing parenthesis. */
81 STOK_NAME /* hash-consed name or identifier. */
85 /* Structure and hash-table used to share identifiers or names. */
86 struct state_ident_st
88 /* TODO: We could improve the parser by reserving identifiers for
89 state keywords and adding a keyword number for them. That would
90 mean adding another field in this state_ident_st struct. */
91 char stid_name[1]; /* actually bigger & null terminated */
93 static htab_t state_ident_tab;
96 /* The state_token_st structure is for lexical tokens in the read
97 state file. The stok_kind field discriminates the union. Tokens
98 are allocated by peek_state_token which calls read_a_state_token
99 which allocate them. Tokens are freed by calls to
100 next_state_tokens. Token are organized in a FIFO look-ahead queue
101 filled by peek_state_token. */
102 struct state_token_st
104 enum state_token_en stok_kind; /* the lexical kind
105 discriminates the stok_un
106 union */
107 int stok_line; /* the line number */
108 int stok_col; /* the column number */
109 const char *stok_file; /* the file path */
110 struct state_token_st *stok_next; /* the next token in the
111 queue, when peeked */
112 union /* discriminated by stok_kind! */
114 int stok_num; /* when STOK_INTEGER */
115 char stok_string[1]; /* when STOK_STRING, actual size is
116 bigger and null terminated */
117 struct state_ident_st *stok_ident; /* when STOK_IDENT */
118 void *stok_ptr; /* null otherwise */
120 stok_un;
126 #define NULL_STATE_TOKEN (struct state_token_st*)0
128 /* the state_token pointer contains the leftmost current token. The
129 tokens are organized in a linked queue, using stok_next, for token
130 look-ahead. */
131 struct state_token_st *state_token = NULL_STATE_TOKEN;
133 /* Used by the reading lexer. */
134 static FILE *state_file;
135 static const char *state_path = NULL;
136 static int state_line = 0;
137 static long state_bol = 0; /* offset of beginning of line */
140 /* Counter of written types. */
141 static int state_written_type_count = 0;
144 /* Fatal error messages when reading the state. They are extremely
145 unlikely, and only appear when this gengtype-state.c file is buggy,
146 or when reading a gengtype state which was not generated by the
147 same version of gengtype or GCC. */
150 /* Fatal message while reading state. */
151 static inline void
152 fatal_reading_state (struct state_token_st* tok, const char*msg)
154 if (tok)
155 fatal ("%s:%d:%d: Invalid state file; %s",
156 tok->stok_file, tok->stok_line, tok->stok_col,
157 msg);
158 else
159 fatal ("%s:%d: Invalid state file; %s",
160 state_path, state_line, msg);
164 /* Fatal printf-like message while reading state. This can't be a
165 function, because there is no way to pass a va_arg to a variant of
166 fatal. */
167 #define fatal_reading_state_printf(Tok,Fmt,...) do { \
168 struct state_token_st* badtok = Tok; \
169 if (badtok) \
170 fatal ("%s:%d:%d: Invalid state file; " Fmt, \
171 badtok->stok_file, \
172 badtok->stok_line, \
173 badtok->stok_col, __VA_ARGS__); \
174 else \
175 fatal ("%s:%d: Invalid state file; " Fmt, \
176 state_path, state_line, __VA_ARGS__); \
177 } while(0)
180 /* Find or allocate an identifier in our name hash table. */
181 static struct state_ident_st *
182 state_ident_by_name (const char *name, enum insert_option optins)
184 PTR *slot = NULL;
185 int namlen = 0;
186 struct state_ident_st *stid = NULL;
188 if (!name || !name[0])
189 return NULL;
191 slot = htab_find_slot (state_ident_tab, name, optins);
192 if (!slot)
193 return NULL;
195 namlen = strlen (name);
196 stid =
197 (struct state_ident_st *) xmalloc (sizeof (struct state_ident_st) +
198 namlen);
199 memset (stid, 0, sizeof (struct state_ident_st) + namlen);
200 strcpy (stid->stid_name, name);
201 *slot = stid;
203 return stid;
206 /* Our token lexer is heavily inspired by MELT's lexer, and share some
207 code with the file gcc/melt-runtime.c of the GCC MELT branch! We
208 really want the gengtype state to be easily parsable by MELT. This
209 is a usual lispy lexing routine, dealing with spaces and comments,
210 numbers, parenthesis, names, strings. */
211 static struct state_token_st *
212 read_a_state_token (void)
214 int c = 0;
215 long curoff = 0;
216 struct state_token_st *tk = NULL;
218 again: /* Read again, e.g. after a comment or spaces. */
219 c = getc (state_file);
220 if (c == EOF)
221 return NULL;
223 /* Handle spaces, count lines. */
224 if (c == '\n')
226 state_line++;
227 state_bol = curoff = ftell (state_file);
228 goto again;
230 if (ISSPACE (c))
231 goto again;
232 /* Skip comments starting with semi-colon. */
233 if (c == ';')
237 c = getc (state_file);
239 while (c > 0 && c != '\n');
240 if (c == '\n')
242 state_line++;
243 state_bol = curoff = ftell (state_file);
245 goto again;
247 /* Read signed numbers. */
248 if (ISDIGIT (c) || c == '-' || c == '+')
249 { /* number */
250 int n = 0;
251 ungetc (c, state_file);
252 curoff = ftell (state_file);
253 if (fscanf (state_file, "%d", &n) <= 0)
254 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error in number");
255 tk = XCNEW (struct state_token_st);
256 tk->stok_kind = STOK_INTEGER;
257 tk->stok_line = state_line;
258 tk->stok_col = curoff - state_bol;
259 tk->stok_file = state_path;
260 tk->stok_next = NULL;
261 tk->stok_un.stok_num = n;
263 return tk;
265 /* Read an opening left parenthesis. */
266 else if (c == '(')
268 curoff = ftell (state_file);
269 tk = XCNEW (struct state_token_st);
270 tk->stok_kind = STOK_LEFTPAR;
271 tk->stok_line = state_line;
272 tk->stok_col = curoff - state_bol;
273 tk->stok_file = state_path;
274 tk->stok_next = NULL;
276 return tk;
278 /* Read an closing right parenthesis. */
279 else if (c == ')')
281 curoff = ftell (state_file);
282 tk = XCNEW (struct state_token_st);
283 tk->stok_kind = STOK_RIGHTPAR;
284 tk->stok_line = state_line;
285 tk->stok_col = curoff - state_bol;
286 tk->stok_file = state_path;
287 tk->stok_next = NULL;
289 return tk;
291 /* Read identifiers, using an obstack. */
292 else if (ISALPHA (c) || c == '_' || c == '$' || c == '!' || c == '#')
294 struct obstack id_obstack;
295 struct state_ident_st *sid = NULL;
296 char *ids = NULL;
297 obstack_init (&id_obstack);
298 curoff = ftell (state_file);
299 while (ISALNUM (c) || c == '_' || c == '$' || c == '!' || c == '#')
301 obstack_1grow (&id_obstack, c);
302 c = getc (state_file);
303 if (c < 0)
304 break;
306 if (c >= 0)
307 ungetc (c, state_file);
308 obstack_1grow (&id_obstack, (char) 0);
309 ids = XOBFINISH (&id_obstack, char *);
310 sid = state_ident_by_name (ids, INSERT);
311 obstack_free (&id_obstack, NULL);
312 ids = NULL;
313 tk = XCNEW (struct state_token_st);
314 tk->stok_kind = STOK_NAME;
315 tk->stok_line = state_line;
316 tk->stok_col = curoff - state_bol;
317 tk->stok_file = state_path;
318 tk->stok_next = NULL;
319 tk->stok_un.stok_ident = sid;
321 return tk;
323 /* Read a string, dealing with escape sequences a la C! */
324 else if (c == '"')
326 char *cstr = NULL;
327 int cslen = 0;
328 struct obstack bstring_obstack;
329 obstack_init (&bstring_obstack);
330 curoff = ftell (state_file);
331 while ((c = getc (state_file)) != '"' && c >= 0)
333 if (ISPRINT (c) && c != '\\')
334 obstack_1grow (&bstring_obstack, (char) c);
335 else if (ISSPACE (c) && c != '\n')
336 obstack_1grow (&bstring_obstack, (char) c);
337 else if (c == '\\')
339 c = getc (state_file);
340 switch (c)
342 case 'a':
343 obstack_1grow (&bstring_obstack, '\a');
344 c = getc (state_file);
345 break;
346 case 'b':
347 obstack_1grow (&bstring_obstack, '\b');
348 c = getc (state_file);
349 break;
350 case 't':
351 obstack_1grow (&bstring_obstack, '\t');
352 c = getc (state_file);
353 break;
354 case 'n':
355 obstack_1grow (&bstring_obstack, '\n');
356 c = getc (state_file);
357 break;
358 case 'v':
359 obstack_1grow (&bstring_obstack, '\v');
360 c = getc (state_file);
361 break;
362 case 'f':
363 obstack_1grow (&bstring_obstack, '\f');
364 c = getc (state_file);
365 break;
366 case 'r':
367 obstack_1grow (&bstring_obstack, '\r');
368 c = getc (state_file);
369 break;
370 case '"':
371 obstack_1grow (&bstring_obstack, '\"');
372 c = getc (state_file);
373 break;
374 case '\\':
375 obstack_1grow (&bstring_obstack, '\\');
376 c = getc (state_file);
377 break;
378 case ' ':
379 obstack_1grow (&bstring_obstack, ' ');
380 c = getc (state_file);
381 break;
382 case 'x':
384 unsigned int cx = 0;
385 if (fscanf (state_file, "%02x", &cx) > 0 && cx > 0)
386 obstack_1grow (&bstring_obstack, cx);
387 else
388 fatal_reading_state
389 (NULL_STATE_TOKEN,
390 "Lexical error in string hex escape");
391 c = getc (state_file);
392 break;
394 default:
395 fatal_reading_state
396 (NULL_STATE_TOKEN,
397 "Lexical error - unknown string escape");
400 else
401 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error...");
403 if (c != '"')
404 fatal_reading_state (NULL_STATE_TOKEN, "Unterminated string");
405 obstack_1grow (&bstring_obstack, '\0');
406 cstr = XOBFINISH (&bstring_obstack, char *);
407 cslen = strlen (cstr);
408 tk = (struct state_token_st *)
409 xcalloc (sizeof (struct state_token_st) + cslen, 1);
410 tk->stok_kind = STOK_STRING;
411 tk->stok_line = state_line;
412 tk->stok_col = curoff - state_bol;
413 tk->stok_file = state_path;
414 tk->stok_next = NULL;
415 strcpy (tk->stok_un.stok_string, cstr);
416 obstack_free (&bstring_obstack, NULL);
418 return tk;
420 /* Got an unexpected character. */
421 fatal_reading_state_printf
422 (NULL_STATE_TOKEN,
423 "Lexical error at offset %ld - bad character \\%03o = '%c'",
424 ftell (state_file), c, c);
427 /* Used for lexical look-ahead. Retrieves the lexical token of rank
428 DEPTH, starting with 0 when reading the state file. Gives null on
429 end of file. */
430 static struct state_token_st *
431 peek_state_token (int depth)
433 int remdepth = depth;
434 struct state_token_st **ptoken = &state_token;
435 struct state_token_st *tok = NULL;
437 while (remdepth >= 0)
439 if (*ptoken == NULL)
441 *ptoken = tok = read_a_state_token ();
442 if (tok == NULL)
443 return NULL;
445 tok = *ptoken;
446 ptoken = &((*ptoken)->stok_next);
447 remdepth--;
450 return tok;
453 /* Consume the next DEPTH tokens and free them. */
454 static void
455 next_state_tokens (int depth)
457 struct state_token_st *n;
459 while (depth > 0)
461 if (state_token != NULL)
463 n = state_token->stok_next;
464 free (state_token);
465 state_token = n;
467 else
468 fatal_reading_state (NULL_STATE_TOKEN, "Tokens stack empty");
470 depth--;
474 /* Safely retrieve the lexical kind of a token. */
475 static inline enum state_token_en
476 state_token_kind (struct state_token_st *p)
478 if (p == NULL)
479 return STOK_NONE;
480 else
481 return p->stok_kind;
484 /* Test if a token is a given name i.e. an identifier. */
485 static inline bool
486 state_token_is_name (struct state_token_st *p, const char *name)
488 if (p == NULL)
489 return false;
491 if (p->stok_kind != STOK_NAME)
492 return false;
494 return !strcmp (p->stok_un.stok_ident->stid_name, name);
498 /* Following routines are useful for serializing datas.
500 * We want to serialize :
501 * - typedefs list
502 * - structures list
503 * - param_structs list
504 * - variables list
506 * So, we have one routine for each kind of data. The main writing
507 * routine is write_state. The main reading routine is
508 * read_state. Most writing routines write_state_FOO have a
509 * corresponding reading routine read_state_FOO. Reading is done in a
510 * recursive descending way, and any read error is fatal.
513 /* When reading the state, we need to remember the previously seen
514 types by their state_number, since GTY-ed types are usually
515 shared. */
516 static htab_t state_seen_types;
518 /* Return the length of a linked list made of pairs. */
519 static int pair_list_length (pair_p list);
521 /* Write a pair */
522 static void write_state_pair (pair_p);
524 /* return the number of pairs written. Should match the length given
525 by pair_list_length. */
526 static int write_state_pair_list (pair_p list);
528 /* Write a type. When a type is written, its state_number is updated,
529 to ensure that a "reference" to a seen type is written on next
530 occurrences. */
531 static void write_state_type (type_p);
533 /* Write a null-terminatel string using our Lispy lexical conventions,
534 similar to those of C or MELT. */
535 static void write_state_a_string (const char *s);
537 /* Compute the length of a list of pairs, starting from the first
538 one. */
539 static int
540 pair_list_length (pair_p list)
542 int nbpair = 0;
543 pair_p l = NULL;
544 for (l = list; l; l = l->next)
545 nbpair++;
546 return nbpair;
549 /* Write a file location. Files relative to $(srcdir) are quite
550 frequent and are handled specially. This ensures that two gengtype
551 state file-s produced by gengtype on the same GCC source tree are
552 very similar and can be reasonably compared with diff, even if the
553 two GCC source trees have different absolute paths. */
554 static void
555 write_state_fileloc (struct fileloc *floc)
558 if (floc != NULL && floc->line > 0)
560 const char *srcrelpath = NULL;
561 gcc_assert (floc->file != NULL);
562 /* Most of the files are inside $(srcdir) so it is worth to
563 handle them specially. */
564 srcrelpath = get_file_srcdir_relative_path (floc->file);
565 if (srcrelpath != NULL)
567 fprintf (state_file, "\n(!srcfileloc ");
568 write_state_a_string (srcrelpath);
570 else
572 fprintf (state_file, "\n(!fileloc ");
573 write_state_a_string (get_input_file_name (floc->file));
575 fprintf (state_file, " %d", floc->line);
576 fprintf (state_file, ")\n");
578 else
579 fprintf (state_file, "nil ");
582 /* Write a list of fields. */
583 static void
584 write_state_fields (pair_p fields)
586 int nbfields = pair_list_length (fields);
587 int nbpairs = 0;
588 fprintf (state_file, "\n(!fields %d ", nbfields);
589 nbpairs = write_state_pair_list (fields);
590 gcc_assert (nbpairs == nbfields);
591 fprintf (state_file, ")\n");
594 /* Write a null-terminated string in our lexical convention, very
595 similar to the convention of C. */
596 static void
597 write_state_a_string (const char *s)
599 char c;
601 fputs (" \"", state_file);
602 for (; *s != 0; s++)
604 c = *s;
605 switch (c)
607 case '\a':
608 fputs ("\\a", state_file);
609 break;
610 case '\b':
611 fputs ("\\b", state_file);
612 break;
613 case '\t':
614 fputs ("\\t", state_file);
615 break;
616 case '\n':
617 fputs ("\\n", state_file);
618 break;
619 case '\v':
620 fputs ("\\v", state_file);
621 break;
622 case '\f':
623 fputs ("\\f", state_file);
624 break;
625 case '\r':
626 fputs ("\\r", state_file);
627 break;
628 case '\"':
629 fputs ("\\\"", state_file);
630 break;
631 case '\\':
632 fputs ("\\\\", state_file);
633 break;
634 default:
635 if (ISPRINT (c))
636 putc (c, state_file);
637 else
638 fprintf (state_file, "\\x%02x", (unsigned) c);
641 fputs ("\"", state_file);
644 /* Our option-s have three kinds, each with its writer. */
645 static void
646 write_state_string_option (options_p current)
648 fprintf (state_file, "string ");
649 if (current->info.string != NULL)
650 write_state_a_string (current->info.string);
651 else
652 fprintf (state_file, " nil ");
655 static void
656 write_state_type_option (options_p current)
658 fprintf (state_file, "type ");
659 write_state_type (current->info.type);
662 static void
663 write_state_nested_option (options_p current)
665 fprintf (state_file, "nested ");
666 write_state_type (current->info.nested->type);
667 if (current->info.nested->convert_from != NULL)
668 write_state_a_string (current->info.nested->convert_from);
669 else
670 fprintf (state_file, " nil ");
672 if (current->info.nested->convert_to != NULL)
673 write_state_a_string (current->info.nested->convert_to);
674 else
675 fprintf (state_file, " nil ");
678 static void
679 write_state_option (options_p current)
681 fprintf (state_file, "\n(!option ");
683 if (current->name != NULL)
684 fprintf (state_file, "%s ", current->name);
685 else
686 fprintf (state_file, "nil ");
688 switch (current->kind)
690 case OPTION_STRING:
691 write_state_string_option (current);
692 break;
693 case OPTION_TYPE:
694 write_state_type_option (current);
695 break;
696 case OPTION_NESTED:
697 write_state_nested_option (current);
698 break;
699 default:
700 fatal ("Option tag unknown");
703 fprintf (state_file, ")\n");
708 /* Write a list of GTY options. */
709 static void
710 write_state_options (options_p opt)
712 options_p current;
714 if (opt == NULL)
716 fprintf (state_file, "nil ");
717 return;
720 fprintf (state_file, "\n(!options ");
721 for (current = opt; current != NULL; current = current->next)
722 write_state_option (current);
723 fprintf (state_file, ")\n");
727 /* Write a bitmap representing a set of GCC front-end languages. */
728 static void
729 write_state_lang_bitmap (lang_bitmap bitmap)
731 fprintf (state_file, "%d ", (int) bitmap);
734 /* Write version information. */
735 static void
736 write_state_version (const char *version)
738 fprintf (state_file, "\n(!version ");
739 write_state_a_string (version);
740 fprintf (state_file, ")\n");
743 /* Common routine to write the common content of all types. */
744 static void write_state_common_type_content (type_p current);
746 /* Write a scalar type. We have only two of these. */
747 static void
748 write_state_scalar_type (type_p current)
750 if (current == &scalar_nonchar)
751 fprintf (state_file, "scalar_nonchar ");
752 else if (current == &scalar_char)
753 fprintf (state_file, "scalar_char ");
754 else
755 fatal ("Unexpected type in write_state_scalar_type");
757 write_state_common_type_content (current);
760 /* Write the string type. There is only one such thing! */
761 static void
762 write_state_string_type (type_p current)
764 if (current == &string_type)
766 fprintf (state_file, "string ");
767 write_state_common_type_content (current);
769 else
770 fatal ("Unexpected type in write_state_string_type");
774 /* Common code to write structure like types. */
775 static void
776 write_state_struct_union_type (type_p current, const char *kindstr)
778 DBGPRINTF ("%s type @ %p #%d '%s'", kindstr, (void *) current,
779 current->state_number, current->u.s.tag);
780 fprintf (state_file, "%s ", kindstr);
781 write_state_common_type_content (current);
782 if (current->u.s.tag != NULL)
783 write_state_a_string (current->u.s.tag);
784 else
785 fprintf (state_file, "nil");
787 write_state_fileloc (type_lineloc (current));
788 write_state_fields (current->u.s.fields);
789 write_state_options (current->u.s.opt);
790 write_state_lang_bitmap (current->u.s.bitmap);
794 /* Write a GTY struct type. */
795 static void
796 write_state_struct_type (type_p current)
798 write_state_struct_union_type (current, "struct");
799 write_state_type (current->u.s.lang_struct);
802 /* Write a GTY user-defined struct type. */
803 static void
804 write_state_user_struct_type (type_p current)
806 DBGPRINTF ("user_struct type @ %p #%d '%s'", (void *) current,
807 current->state_number, current->u.s.tag);
808 fprintf (state_file, "user_struct ");
809 write_state_common_type_content (current);
810 if (current->u.s.tag != NULL)
811 write_state_a_string (current->u.s.tag);
812 else
813 fprintf (state_file, "nil");
814 write_state_fileloc (type_lineloc (current));
815 write_state_fields (current->u.s.fields);
818 /* write a GTY union type. */
819 static void
820 write_state_union_type (type_p current)
822 write_state_struct_union_type (current, "union");
823 write_state_type (current->u.s.lang_struct);
826 /* Write a lang_struct type. This is tricky and was painful to debug,
827 we deal with the next field specifically within their lang_struct
828 subfield, which points to a linked list of homonumous types.
829 Change this function with extreme care, see also
830 read_state_lang_struct_type. */
831 static void
832 write_state_lang_struct_type (type_p current)
834 int nbhomontype = 0;
835 type_p hty = NULL;
836 const char *homoname = 0;
837 write_state_struct_union_type (current, "lang_struct");
838 /* lang_struct-ures are particularly tricky, since their
839 u.s.lang_struct field gives a list of homonymous struct-s or
840 union-s! */
841 DBGPRINTF ("lang_struct @ %p #%d", (void *) current, current->state_number);
842 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
844 nbhomontype++;
845 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype,
846 (void *) hty, hty->state_number, hty->u.s.tag);
847 /* Every member of the homonymous list should have the same tag. */
848 gcc_assert (union_or_struct_p (hty));
849 gcc_assert (hty->u.s.lang_struct == current);
850 if (!homoname)
851 homoname = hty->u.s.tag;
852 gcc_assert (strcmp (homoname, hty->u.s.tag) == 0);
854 fprintf (state_file, "(!homotypes %d\n", nbhomontype);
855 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
856 write_state_type (hty);
857 fprintf (state_file, ")\n");
860 /* Write a parametrized structure GTY type. */
861 static void
862 write_state_param_struct_type (type_p current)
864 int i;
866 fprintf (state_file, "param_struct ");
867 write_state_common_type_content (current);
868 write_state_type (current->u.param_struct.stru);
869 for (i = 0; i < NUM_PARAM; i++)
871 if (current->u.param_struct.param[i] != NULL)
872 write_state_type (current->u.param_struct.param[i]);
873 else
874 fprintf (state_file, "nil ");
876 write_state_fileloc (&current->u.param_struct.line);
879 /* Write a pointer type. */
880 static void
881 write_state_pointer_type (type_p current)
883 fprintf (state_file, "pointer ");
884 write_state_common_type_content (current);
885 write_state_type (current->u.p);
888 /* Write an array type. */
889 static void
890 write_state_array_type (type_p current)
892 fprintf (state_file, "array ");
893 write_state_common_type_content (current);
894 if (current->u.a.len != NULL)
895 write_state_a_string (current->u.a.len);
896 else
897 fprintf (state_file, " nil");
899 fprintf (state_file, " ");
900 write_state_type (current->u.a.p);
903 /* Write the gc_used information. */
904 static void
905 write_state_gc_used (enum gc_used_enum gus)
907 switch (gus)
909 case GC_UNUSED:
910 fprintf (state_file, " gc_unused");
911 break;
912 case GC_USED:
913 fprintf (state_file, " gc_used");
914 break;
915 case GC_MAYBE_POINTED_TO:
916 fprintf (state_file, " gc_maybe_pointed_to");
917 break;
918 case GC_POINTED_TO:
919 fprintf (state_file, " gc_pointed_to");
920 break;
921 default:
922 gcc_unreachable ();
926 /* Utility routine to write the common content of all types. Notice
927 that the next field is *not* written on purpose. */
928 static void
929 write_state_common_type_content (type_p current)
931 fprintf (state_file, "%d ", current->state_number);
932 /* We do not write the next type, because list of types are
933 explicitly written. However, lang_struct are special in that
934 respect. See function write_state_lang_struct_type for more. */
935 write_state_type (current->pointer_to);
936 write_state_gc_used (current->gc_used);
940 /* The important and recursive routine writing GTY types as understood
941 by gengtype. Types which have a positive state_number have already
942 been seen and written. */
943 static void
944 write_state_type (type_p current)
946 if (current == NULL)
948 fprintf (state_file, "nil ");
949 return;
952 fprintf (state_file, "\n(!type ");
954 if (current->state_number > 0)
955 fprintf (state_file, "already_seen %d", current->state_number);
956 else
958 state_written_type_count++;
959 DBGPRINTF ("writing type #%d @%p old number %d", state_written_type_count,
960 (void *) current, current->state_number);
961 current->state_number = state_written_type_count;
962 switch (current->kind)
964 case TYPE_NONE:
965 gcc_unreachable ();
966 case TYPE_STRUCT:
967 write_state_struct_type (current);
968 break;
969 case TYPE_USER_STRUCT:
970 write_state_user_struct_type (current);
971 break;
972 case TYPE_UNION:
973 write_state_union_type (current);
974 break;
975 case TYPE_POINTER:
976 write_state_pointer_type (current);
977 break;
978 case TYPE_ARRAY:
979 write_state_array_type (current);
980 break;
981 case TYPE_LANG_STRUCT:
982 write_state_lang_struct_type (current);
983 break;
984 case TYPE_PARAM_STRUCT:
985 write_state_param_struct_type (current);
986 break;
987 case TYPE_SCALAR:
988 write_state_scalar_type (current);
989 break;
990 case TYPE_STRING:
991 write_state_string_type (current);
992 break;
996 fprintf (state_file, ")\n");
1000 /* Write a pair. */
1001 static void
1002 write_state_pair (pair_p current)
1004 if (current == NULL)
1006 fprintf (state_file, "nil)");
1007 return;
1010 fprintf (state_file, "\n(!pair ");
1012 if (current->name != NULL)
1013 write_state_a_string (current->name);
1014 else
1015 write_state_a_string ("nil");
1017 write_state_type (current->type);
1018 write_state_fileloc (&(current->line));
1019 write_state_options (current->opt);
1021 fprintf (state_file, ")");
1024 /* Write a pair list and return the number of pairs written. */
1025 static int
1026 write_state_pair_list (pair_p list)
1028 int nbpair = 0;
1029 pair_p current;
1031 for (current = list; current != NULL; current = current->next)
1033 write_state_pair (current);
1034 nbpair++;
1036 return nbpair;
1040 /* When writing imported linked lists, like typedefs, structures,
1041 param_structs, ... we count their length first and write it. These
1042 eases the reading, and enables an extra verification on the number
1043 of actually read items. */
1045 /* Write our typedefs. */
1046 static void
1047 write_state_typedefs (void)
1049 int nbtypedefs = pair_list_length (typedefs);
1050 int nbpairs = 0;
1051 fprintf (state_file, "\n(!typedefs %d\n", nbtypedefs);
1052 nbpairs = write_state_pair_list (typedefs);
1053 gcc_assert (nbpairs == nbtypedefs);
1054 fprintf (state_file, ")\n");
1055 if (verbosity_level >= 2)
1056 printf ("%s wrote %d typedefs\n", progname, nbtypedefs);
1059 /* Write our structures. */
1060 static void
1061 write_state_structures (void)
1063 int nbstruct = 0;
1064 type_p current;
1066 for (current = structures; current != NULL; current = current->next)
1067 nbstruct++;
1069 fprintf (state_file, "\n(!structures %d\n", nbstruct);
1071 for (current = structures; current != NULL; current = current->next)
1072 write_state_type (current);
1074 fprintf (state_file, ")\n");
1075 if (verbosity_level >= 2)
1076 printf ("%s wrote %d structures in state\n", progname, nbstruct);
1079 /* Write our param_struct-s. */
1080 static void
1081 write_state_param_structs (void)
1083 int nbparamstruct = 0;
1084 type_p current;
1086 for (current = param_structs; current != NULL; current = current->next)
1087 nbparamstruct++;
1089 fprintf (state_file, "\n(!param_structs %d\n", nbparamstruct);
1091 for (current = param_structs; current != NULL; current = current->next)
1092 write_state_type (current);
1094 fprintf (state_file, ")\n");
1097 /* Write our variables. */
1098 static void
1099 write_state_variables (void)
1101 int nbvars = pair_list_length (variables);
1102 int nbpairs = 0;
1103 fprintf (state_file, "\n(!variables %d\n", nbvars);
1104 nbpairs = write_state_pair_list (variables);
1105 gcc_assert (nbpairs == nbvars);
1106 fprintf (state_file, ")\n");
1107 if (verbosity_level >= 2)
1108 printf ("%s wrote %d variables.\n", progname, nbvars);
1111 /* Write the source directory. File locations within the source
1112 directory have been written specifically. */
1113 static void
1114 write_state_srcdir (void)
1116 fprintf (state_file, "\n(!srcdir ");
1117 write_state_a_string (srcdir);
1118 fprintf (state_file, ")\n");
1121 /* Count and write the list of our files. */
1122 static void
1123 write_state_files_list (void)
1125 int i = 0;
1126 /* Write the list of files with their lang_bitmap. */
1127 fprintf (state_file, "\n(!fileslist %d\n", (int) num_gt_files);
1128 for (i = 0; i < (int) num_gt_files; i++)
1130 const char *cursrcrelpath = NULL;
1131 const input_file *curfil = gt_files[i];
1132 /* Most of the files are inside $(srcdir) so it is worth to
1133 handle them specially. */
1134 cursrcrelpath = get_file_srcdir_relative_path (curfil);
1135 if (cursrcrelpath)
1137 fprintf (state_file, "(!srcfile %d ", get_lang_bitmap (curfil));
1138 write_state_a_string (cursrcrelpath);
1140 else
1142 fprintf (state_file, "(!file %d ", get_lang_bitmap (curfil));
1143 write_state_a_string (get_input_file_name (curfil));
1145 fprintf (state_file, ")\n");
1147 fprintf (state_file, ")\n");
1150 /* Write the list of GCC front-end languages. */
1151 static void
1152 write_state_languages (void)
1154 int i = 0;
1155 fprintf (state_file, "\n(!languages %d", (int) num_lang_dirs);
1156 for (i = 0; i < (int) num_lang_dirs; i++)
1158 /* Languages names are identifiers, we expect only letters or
1159 underscores or digits in them. In particular, C++ is not a
1160 valid language name, but cp is valid. */
1161 fprintf (state_file, " %s", lang_dir_names[i]);
1163 fprintf (state_file, ")\n");
1166 /* Write the trailer. */
1167 static void
1168 write_state_trailer (void)
1170 /* This test should probably catch IO errors like disk full... */
1171 if (fputs ("\n(!endfile)\n", state_file) == EOF)
1172 fatal ("failed to write state trailer [%s]", xstrerror (errno));
1175 /* The write_state routine is the only writing routine called by main
1176 in gengtype.c. To avoid messing the state if gengtype is
1177 interrupted or aborted, we write a temporary file and rename it
1178 after having written it in totality. */
1179 void
1180 write_state (const char *state_path)
1182 long statelen = 0;
1183 time_t now = 0;
1184 char *temp_state_path = NULL;
1185 char tempsuffix[40];
1186 time (&now);
1188 /* We write a unique temporary file which is renamed when complete
1189 * only. So even if gengtype is interrupted, the written state file
1190 * won't be partially written, since the temporary file is not yet
1191 * renamed in that case. */
1192 memset (tempsuffix, 0, sizeof (tempsuffix));
1193 snprintf (tempsuffix, sizeof (tempsuffix) - 1, "-%ld-%d.tmp", (long) now,
1194 (int) getpid ());
1195 temp_state_path = concat (state_path, tempsuffix, NULL);
1196 state_file = fopen (temp_state_path, "w");
1197 if (state_file == NULL)
1198 fatal ("Failed to open file %s for writing state: %s",
1199 temp_state_path, xstrerror (errno));
1200 if (verbosity_level >= 3)
1201 printf ("%s writing state file %s temporarily in %s\n",
1202 progname, state_path, temp_state_path);
1203 /* This is the first line of the state. Perhaps the file utility
1204 could know about that, so don't change it often. */
1205 fprintf (state_file, ";;;;@@@@ GCC gengtype state\n");
1206 /* Output a few comments for humans. */
1207 fprintf (state_file,
1208 ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
1209 fprintf (state_file,
1210 ";;; The format of this file is tied to a particular version of GCC.\n");
1211 fprintf (state_file,
1212 ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1213 fprintf (state_file,
1214 ";;; This file should be parsed by the same %s which wrote it.\n",
1215 progname);
1216 /* The first non-comment significant line gives the version string. */
1217 write_state_version (version_string);
1218 write_state_srcdir ();
1219 write_state_languages ();
1220 write_state_files_list ();
1221 write_state_structures ();
1222 write_state_typedefs ();
1223 write_state_param_structs ();
1224 write_state_variables ();
1225 write_state_trailer ();
1226 statelen = ftell (state_file);
1227 if (ferror (state_file))
1228 fatal ("output error when writing state file %s [%s]",
1229 temp_state_path, xstrerror (errno));
1230 if (fclose (state_file))
1231 fatal ("failed to close state file %s [%s]",
1232 temp_state_path, xstrerror (errno));
1233 if (rename (temp_state_path, state_path))
1234 fatal ("failed to rename %s to state file %s [%s]", temp_state_path,
1235 state_path, xstrerror (errno));
1236 free (temp_state_path);
1238 if (verbosity_level >= 1)
1239 printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1240 progname, state_path, statelen, state_written_type_count);
1244 /** End of writing routines! The corresponding reading routines follow. **/
1248 /* Forward declarations, since some read_state_* functions are
1249 recursive! */
1250 static void read_state_fileloc (struct fileloc *line);
1251 static void read_state_options (options_p *opt);
1252 static void read_state_type (type_p *current);
1253 static void read_state_pair (pair_p *pair);
1254 /* Return the number of pairs actually read. */
1255 static int read_state_pair_list (pair_p *list);
1256 static void read_state_fields (pair_p *fields);
1257 static void read_state_common_type_content (type_p current);
1262 /* Record into the state_seen_types hash-table a type which we are
1263 reading, to enable recursive or circular references to it. */
1264 static void
1265 record_type (type_p type)
1267 PTR *slot;
1269 slot = htab_find_slot (state_seen_types, type, INSERT);
1270 gcc_assert (slot);
1272 *slot = type;
1275 /* Read an already seen type. */
1276 static void
1277 read_state_already_seen_type (type_p *type)
1279 struct state_token_st *t0 = peek_state_token (0);
1281 if (state_token_kind (t0) == STOK_INTEGER)
1283 PTR *slot = NULL;
1284 struct type loctype = { TYPE_SCALAR, 0, 0, 0, GC_UNUSED, {0} };
1286 loctype.state_number = t0->stok_un.stok_num;
1287 slot = htab_find_slot (state_seen_types, &loctype, NO_INSERT);
1288 if (slot == NULL)
1290 fatal_reading_state (t0, "Unknown type");
1293 next_state_tokens (1);
1294 *type = (type_p) *slot;
1296 else
1298 fatal_reading_state (t0, "Bad seen type");
1303 /* Read the scalar_nonchar type. */
1304 static void
1305 read_state_scalar_nonchar_type (type_p *type)
1307 *type = &scalar_nonchar;
1308 read_state_common_type_content (*type);
1312 /* Read the scalar_char type. */
1313 static void
1314 read_state_scalar_char_type (type_p *type)
1316 *type = &scalar_char;
1317 read_state_common_type_content (*type);
1320 /* Read the string_type. */
1321 static void
1322 read_state_string_type (type_p *type)
1324 *type = &string_type;
1325 read_state_common_type_content (*type);
1329 /* Read a lang_bitmap representing a set of GCC front-end languages. */
1330 static void
1331 read_state_lang_bitmap (lang_bitmap *bitmap)
1333 struct state_token_st *t;
1335 t = peek_state_token (0);
1336 if (state_token_kind (t) == STOK_INTEGER)
1338 *bitmap = t->stok_un.stok_num;
1339 next_state_tokens (1);
1341 else
1343 fatal_reading_state (t, "Bad syntax for bitmap");
1348 /* Read a GTY-ed struct type. */
1349 static void
1350 read_state_struct_type (type_p type)
1352 struct state_token_st *t0;
1354 type->kind = TYPE_STRUCT;
1355 read_state_common_type_content (type);
1356 t0 = peek_state_token (0);
1357 if (state_token_kind (t0) == STOK_STRING)
1359 if (state_token_is_name (t0, "nil"))
1361 type->u.s.tag = NULL;
1362 DBGPRINTF ("read anonymous struct type @%p #%d",
1363 (void *) type, type->state_number);
1365 else
1367 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1368 DBGPRINTF ("read struct type @%p #%d '%s'",
1369 (void *) type, type->state_number, type->u.s.tag);
1372 next_state_tokens (1);
1373 read_state_fileloc (&(type->u.s.line));
1374 read_state_fields (&(type->u.s.fields));
1375 read_state_options (&(type->u.s.opt));
1376 read_state_lang_bitmap (&(type->u.s.bitmap));
1377 read_state_type (&(type->u.s.lang_struct));
1379 else
1381 fatal_reading_state (t0, "Bad tag in struct type");
1386 /* Read a GTY-ed user-provided struct TYPE. */
1388 static void
1389 read_state_user_struct_type (type_p type)
1391 struct state_token_st *t0;
1393 type->kind = TYPE_USER_STRUCT;
1394 read_state_common_type_content (type);
1395 t0 = peek_state_token (0);
1396 if (state_token_kind (t0) == STOK_STRING)
1398 if (state_token_is_name (t0, "nil"))
1400 type->u.s.tag = NULL;
1401 DBGPRINTF ("read anonymous struct type @%p #%d",
1402 (void *) type, type->state_number);
1404 else
1406 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1407 DBGPRINTF ("read struct type @%p #%d '%s'",
1408 (void *) type, type->state_number, type->u.s.tag);
1411 next_state_tokens (1);
1412 read_state_fileloc (&(type->u.s.line));
1413 read_state_fields (&(type->u.s.fields));
1415 else
1417 fatal_reading_state (t0, "Bad tag in user-struct type");
1422 /* Read a GTY-ed union type. */
1423 static void
1424 read_state_union_type (type_p type)
1426 struct state_token_st *t0;
1428 type->kind = TYPE_UNION;
1429 read_state_common_type_content (type);
1430 t0 = peek_state_token (0);
1431 if (state_token_kind (t0) == STOK_STRING)
1433 if (state_token_is_name (t0, "nil"))
1435 type->u.s.tag = NULL;
1436 DBGPRINTF ("read anonymous union type @%p #%d",
1437 (void *) type, type->state_number);
1439 else
1441 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1442 DBGPRINTF ("read union type @%p #%d '%s'",
1443 (void *) type, type->state_number, type->u.s.tag);
1445 next_state_tokens (1);
1446 read_state_fileloc (&(type->u.s.line));
1447 read_state_fields (&(type->u.s.fields));
1448 read_state_options (&(type->u.s.opt));
1449 read_state_lang_bitmap (&(type->u.s.bitmap));
1450 read_state_type (&(type->u.s.lang_struct));
1452 else
1453 fatal_reading_state (t0, "Bad tag in union type");
1457 /* Read a GTY-ed pointer type. */
1458 static void
1459 read_state_pointer_type (type_p type)
1461 type->kind = TYPE_POINTER;
1462 read_state_common_type_content (type);
1463 DBGPRINTF ("read pointer type @%p #%d", (void *) type, type->state_number);
1464 read_state_type (&(type->u.p));
1468 /* Read a GTY-ed array type. */
1469 static void
1470 read_state_array_type (type_p type)
1472 struct state_token_st *t0;
1474 type->kind = TYPE_ARRAY;
1475 read_state_common_type_content (type);
1476 t0 = peek_state_token (0);
1477 if (state_token_kind (t0) == STOK_STRING)
1479 type->u.a.len = xstrdup (t0->stok_un.stok_string);
1480 DBGPRINTF ("read array type @%p #%d length '%s'",
1481 (void *) type, type->state_number, type->u.a.len);
1482 next_state_tokens (1);
1485 else if (state_token_is_name (t0, "nil"))
1487 type->u.a.len = NULL;
1488 DBGPRINTF ("read array type @%p #%d without length",
1489 (void *) type, type->state_number);
1490 next_state_tokens (1);
1493 else
1494 fatal_reading_state (t0, "Bad array name type");
1495 read_state_type (&(type->u.a.p));
1500 /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
1501 front-end languages. This is a tricky function and it was painful
1502 to debug. Change it with extreme care. See also
1503 write_state_lang_struct_type. */
1504 static void
1505 read_state_lang_struct_type (type_p type)
1507 struct state_token_st *t0 = NULL;
1508 struct state_token_st *t1 = NULL;
1509 struct state_token_st *t2 = NULL;
1511 type->kind = TYPE_LANG_STRUCT;
1512 read_state_common_type_content (type);
1513 t0 = peek_state_token (0);
1514 if (state_token_kind (t0) == STOK_STRING)
1516 if (state_token_is_name (t0, "nil"))
1518 DBGPRINTF ("read anonymous lang_struct type @%p #%d",
1519 (void *) type, type->state_number);
1520 type->u.s.tag = NULL;
1522 else
1524 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1525 DBGPRINTF ("read lang_struct type @%p #%d '%s'",
1526 (void *) type, type->state_number, type->u.s.tag);
1528 next_state_tokens (1);
1530 else
1531 fatal_reading_state (t0, "Bad tag in lang struct type");
1532 read_state_fileloc (&(type->u.s.line));
1533 read_state_fields (&(type->u.s.fields));
1534 read_state_options (&(type->u.s.opt));
1535 read_state_lang_bitmap (&(type->u.s.bitmap));
1536 /* Within lang_struct-ures, the lang_struct field is a linked list
1537 of homonymous types! */
1538 t0 = peek_state_token (0);
1539 t1 = peek_state_token (1);
1540 t2 = peek_state_token (2);
1541 /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
1542 if (state_token_kind (t0) == STOK_LEFTPAR
1543 && state_token_is_name (t1, "!homotypes")
1544 && state_token_kind (t2) == STOK_INTEGER)
1546 type_p *prevty = &type->u.s.lang_struct;
1547 int nbhomotype = t2->stok_un.stok_num;
1548 int i = 0;
1549 t0 = t1 = t2 = NULL;
1550 next_state_tokens (3);
1551 for (i = 0; i < nbhomotype; i++)
1553 read_state_type (prevty);
1554 t0 = peek_state_token (0);
1555 if (*prevty)
1556 prevty = &(*prevty)->next;
1557 else
1558 fatal_reading_state (t0,
1559 "expecting type in homotype list for lang_struct");
1561 if (state_token_kind (t0) != STOK_RIGHTPAR)
1562 fatal_reading_state (t0,
1563 "expecting ) in homotype list for lang_struct");
1564 next_state_tokens (1);
1566 else
1567 fatal_reading_state (t0, "expecting !homotypes for lang_struct");
1571 /* Read a param_struct type for GTY parametrized structures. */
1572 static void
1573 read_state_param_struct_type (type_p type)
1575 int i;
1576 struct state_token_st *t0;
1578 type->kind = TYPE_PARAM_STRUCT;
1579 read_state_common_type_content (type);
1580 DBGPRINTF ("read param_struct type @%p #%d",
1581 (void *) type, type->state_number);
1582 read_state_type (&(type->u.param_struct.stru));
1584 for (i = 0; i < NUM_PARAM; i++)
1586 t0 = peek_state_token (0);
1587 if (state_token_is_name (t0, "nil"))
1589 type->u.param_struct.param[i] = NULL;
1590 next_state_tokens (1);
1592 else
1593 read_state_type (&(type->u.param_struct.param[i]));
1595 read_state_fileloc (&(type->u.param_struct.line));
1599 /* Read the gc used information. */
1600 static void
1601 read_state_gc_used (enum gc_used_enum *pgus)
1603 struct state_token_st *t0 = peek_state_token (0);
1604 if (state_token_is_name (t0, "gc_unused"))
1605 *pgus = GC_UNUSED;
1606 else if (state_token_is_name (t0, "gc_used"))
1607 *pgus = GC_USED;
1608 else if (state_token_is_name (t0, "gc_maybe_pointed_to"))
1609 *pgus = GC_MAYBE_POINTED_TO;
1610 else if (state_token_is_name (t0, "gc_pointed_to"))
1611 *pgus = GC_POINTED_TO;
1612 else
1613 fatal_reading_state (t0, "invalid gc_used information");
1614 next_state_tokens (1);
1618 /* Utility function to read the common content of types. */
1619 static void
1620 read_state_common_type_content (type_p current)
1622 struct state_token_st *t0 = peek_state_token (0);
1624 if (state_token_kind (t0) == STOK_INTEGER)
1626 current->state_number = t0->stok_un.stok_num;
1627 next_state_tokens (1);
1628 record_type (current);
1630 else
1631 fatal_reading_state_printf (t0,
1632 "Expected integer for state_number line %d",
1633 state_line);
1634 /* We don't read the next field of the type. */
1635 read_state_type (&current->pointer_to);
1636 read_state_gc_used (&current->gc_used);
1640 /* Read a GTY-ed type. */
1641 void
1642 read_state_type (type_p *current)
1644 struct state_token_st *t0 = peek_state_token (0);
1645 struct state_token_st *t1 = peek_state_token (1);
1647 if (state_token_kind (t0) == STOK_LEFTPAR &&
1648 state_token_is_name (t1, "!type"))
1650 next_state_tokens (2);
1651 t0 = peek_state_token (0);
1652 if (state_token_is_name (t0, "already_seen"))
1654 next_state_tokens (1);
1655 read_state_already_seen_type (current);
1657 else
1659 t0 = peek_state_token (0);
1661 if (state_token_is_name (t0, "scalar_nonchar"))
1663 next_state_tokens (1);
1664 read_state_scalar_nonchar_type (current);
1666 else if (state_token_is_name (t0, "scalar_char"))
1668 next_state_tokens (1);
1669 read_state_scalar_char_type (current);
1671 else if (state_token_is_name (t0, "string"))
1673 next_state_tokens (1);
1674 read_state_string_type (current);
1676 else if (state_token_is_name (t0, "struct"))
1678 *current = XCNEW (struct type);
1679 next_state_tokens (1);
1680 read_state_struct_type (*current);
1682 else if (state_token_is_name (t0, "union"))
1684 *current = XCNEW (struct type);
1685 next_state_tokens (1);
1686 read_state_union_type (*current);
1688 else if (state_token_is_name (t0, "lang_struct"))
1690 *current = XCNEW (struct type);
1691 next_state_tokens (1);
1692 read_state_lang_struct_type (*current);
1694 else if (state_token_is_name (t0, "param_struct"))
1696 *current = XCNEW (struct type);
1697 next_state_tokens (1);
1698 read_state_param_struct_type (*current);
1700 else if (state_token_is_name (t0, "pointer"))
1702 *current = XCNEW (struct type);
1703 next_state_tokens (1);
1704 read_state_pointer_type (*current);
1706 else if (state_token_is_name (t0, "array"))
1708 *current = XCNEW (struct type);
1709 next_state_tokens (1);
1710 read_state_array_type (*current);
1712 else if (state_token_is_name (t0, "user_struct"))
1714 *current = XCNEW (struct type);
1715 next_state_tokens (1);
1716 read_state_user_struct_type (*current);
1718 else
1719 fatal_reading_state (t0, "bad type in (!type");
1721 t0 = peek_state_token (0);
1722 if (state_token_kind (t0) != STOK_RIGHTPAR)
1723 fatal_reading_state (t0, "missing ) in type");
1724 next_state_tokens (1);
1726 else if (state_token_is_name (t0, "nil"))
1728 next_state_tokens (1);
1729 *current = NULL;
1731 else
1732 fatal_reading_state (t0, "bad type syntax");
1736 /* Read a file location. Files within the source directory are dealt
1737 with specifically. */
1738 void
1739 read_state_fileloc (struct fileloc *floc)
1741 bool issrcfile = false;
1742 struct state_token_st *t0 = peek_state_token (0);
1743 struct state_token_st *t1 = peek_state_token (1);
1745 gcc_assert (floc != NULL);
1746 gcc_assert (srcdir != NULL);
1748 if (state_token_kind (t0) == STOK_LEFTPAR &&
1749 (state_token_is_name (t1, "!fileloc")
1750 || (issrcfile = state_token_is_name (t1, "!srcfileloc"))))
1752 next_state_tokens (2);
1753 t0 = peek_state_token (0);
1754 t1 = peek_state_token (1);
1755 if (state_token_kind (t0) == STOK_STRING &&
1756 state_token_kind (t1) == STOK_INTEGER)
1758 char *path = t0->stok_un.stok_string;
1759 if (issrcfile)
1761 static const char dirsepstr[2] = { DIR_SEPARATOR, (char) 0 };
1762 char *fullpath = concat (srcdir, dirsepstr, path, NULL);
1763 floc->file = input_file_by_name (fullpath);
1764 free (fullpath);
1766 else
1767 floc->file = input_file_by_name (path);
1768 floc->line = t1->stok_un.stok_num;
1769 next_state_tokens (2);
1771 else
1772 fatal_reading_state (t0,
1773 "Bad fileloc syntax, expected path string and line");
1774 t0 = peek_state_token (0);
1775 if (state_token_kind (t0) != STOK_RIGHTPAR)
1776 fatal_reading_state (t0, "Bad fileloc syntax, expected )");
1777 next_state_tokens (1);
1779 else if (state_token_is_name (t0, "nil"))
1781 next_state_tokens (1);
1782 floc->file = NULL;
1783 floc->line = 0;
1785 else
1786 fatal_reading_state (t0, "Bad fileloc syntax");
1790 /* Read the fields of a GTY-ed type. */
1791 void
1792 read_state_fields (pair_p *fields)
1794 pair_p tmp = NULL;
1795 struct state_token_st *t0 = peek_state_token (0);
1796 struct state_token_st *t1 = peek_state_token (1);
1797 struct state_token_st *t2 = peek_state_token (2);
1799 if (state_token_kind (t0) == STOK_LEFTPAR
1800 && state_token_is_name (t1, "!fields")
1801 && state_token_kind (t2) == STOK_INTEGER)
1803 int nbfields = t2->stok_un.stok_num;
1804 int nbpairs = 0;
1805 next_state_tokens (3);
1806 nbpairs = read_state_pair_list (&tmp);
1807 t0 = peek_state_token (0);
1808 if (nbpairs != nbfields)
1809 fatal_reading_state_printf
1810 (t0,
1811 "Mismatched fields number, expected %d got %d", nbpairs, nbfields);
1812 if (state_token_kind (t0) == STOK_RIGHTPAR)
1813 next_state_tokens (1);
1814 else
1815 fatal_reading_state (t0, "Bad fields expecting )");
1818 *fields = tmp;
1822 /* Read a string option. */
1823 static void
1824 read_state_string_option (options_p opt)
1826 struct state_token_st *t0 = peek_state_token (0);
1827 opt->kind = OPTION_STRING;
1828 if (state_token_kind (t0) == STOK_STRING)
1830 opt->info.string = xstrdup (t0->stok_un.stok_string);
1831 next_state_tokens (1);
1833 else if (state_token_is_name (t0, "nil"))
1835 opt->info.string = NULL;
1836 next_state_tokens (1);
1838 else
1839 fatal_reading_state (t0, "Missing name in string option");
1843 /* Read a type option. */
1844 static void
1845 read_state_type_option (options_p opt)
1847 opt->kind = OPTION_TYPE;
1848 read_state_type (&(opt->info.type));
1852 /* Read a nested option. */
1853 static void
1854 read_state_nested_option (options_p opt)
1856 struct state_token_st *t0;
1858 opt->info.nested = XCNEW (struct nested_ptr_data);
1859 opt->kind = OPTION_NESTED;
1860 read_state_type (&(opt->info.nested->type));
1861 t0 = peek_state_token (0);
1862 if (state_token_kind (t0) == STOK_STRING)
1864 opt->info.nested->convert_from = xstrdup (t0->stok_un.stok_string);
1865 next_state_tokens (1);
1867 else if (state_token_is_name (t0, "nil"))
1869 opt->info.nested->convert_from = NULL;
1870 next_state_tokens (1);
1872 else
1873 fatal_reading_state (t0, "Bad nested convert_from option");
1875 t0 = peek_state_token (0);
1876 if (state_token_kind (t0) == STOK_STRING)
1878 opt->info.nested->convert_to = xstrdup (t0->stok_un.stok_string);
1879 next_state_tokens (1);
1881 else if (state_token_is_name (t0, "nil"))
1883 opt->info.nested->convert_to = NULL;
1884 next_state_tokens (1);
1886 else
1887 fatal_reading_state (t0, "Bad nested convert_from option");
1891 /* Read an GTY option. */
1892 static void
1893 read_state_option (options_p *opt)
1895 struct state_token_st *t0 = peek_state_token (0);
1896 struct state_token_st *t1 = peek_state_token (1);
1898 if (state_token_kind (t0) == STOK_LEFTPAR &&
1899 state_token_is_name (t1, "!option"))
1901 next_state_tokens (2);
1902 t0 = peek_state_token (0);
1903 if (state_token_kind (t0) == STOK_NAME)
1905 *opt = XCNEW (struct options);
1906 if (state_token_is_name (t0, "nil"))
1907 (*opt)->name = NULL;
1908 else
1909 (*opt)->name = t0->stok_un.stok_ident->stid_name;
1910 next_state_tokens (1);
1911 t0 = peek_state_token (0);
1912 if (state_token_kind (t0) == STOK_NAME)
1914 if (state_token_is_name (t0, "string"))
1916 next_state_tokens (1);
1917 read_state_string_option (*opt);
1919 else if (state_token_is_name (t0, "type"))
1921 next_state_tokens (1);
1922 read_state_type_option (*opt);
1924 else if (state_token_is_name (t0, "nested"))
1926 next_state_tokens (1);
1927 read_state_nested_option (*opt);
1929 else
1930 fatal_reading_state (t0, "Bad option type");
1931 t0 = peek_state_token (0);
1932 if (state_token_kind (t0) != STOK_RIGHTPAR)
1933 fatal_reading_state (t0, "Bad syntax in option, expecting )");
1935 next_state_tokens (1);
1937 else
1938 fatal_reading_state (t0, "Missing option type");
1940 else
1941 fatal_reading_state (t0, "Bad name for option");
1943 else
1944 fatal_reading_state (t0, "Bad option, waiting for )");
1947 /* Read a list of options. */
1948 void
1949 read_state_options (options_p *opt)
1951 options_p head = NULL;
1952 options_p previous = NULL;
1953 options_p current_option = NULL;
1954 struct state_token_st *t0 = peek_state_token (0);
1955 struct state_token_st *t1 = peek_state_token (1);
1957 if (state_token_kind (t0) == STOK_LEFTPAR &&
1958 state_token_is_name (t1, "!options"))
1960 next_state_tokens (2);
1961 t0 = peek_state_token (0);
1962 while (state_token_kind (t0) != STOK_RIGHTPAR)
1964 read_state_option (&current_option);
1965 if (head == NULL)
1967 head = current_option;
1968 previous = head;
1970 else
1972 previous->next = current_option;
1973 previous = current_option;
1975 t0 = peek_state_token (0);
1977 next_state_tokens (1);
1979 else if (state_token_is_name (t0, "nil"))
1981 next_state_tokens (1);
1983 else
1984 fatal_reading_state (t0, "Bad options syntax");
1986 *opt = head;
1990 /* Read a version, and check against the version of the gengtype. */
1991 static void
1992 read_state_version (const char *version_string)
1994 struct state_token_st *t0 = peek_state_token (0);
1995 struct state_token_st *t1 = peek_state_token (1);
1997 if (state_token_kind (t0) == STOK_LEFTPAR &&
1998 state_token_is_name (t1, "!version"))
2000 next_state_tokens (2);
2001 t0 = peek_state_token (0);
2002 t1 = peek_state_token (1);
2003 if (state_token_kind (t0) == STOK_STRING &&
2004 state_token_kind (t1) == STOK_RIGHTPAR)
2006 /* Check that the read version string is the same as current
2007 version. */
2008 if (strcmp (version_string, t0->stok_un.stok_string))
2009 fatal_reading_state_printf (t0,
2010 "version string mismatch; expecting %s but got %s",
2011 version_string,
2012 t0->stok_un.stok_string);
2013 next_state_tokens (2);
2015 else
2016 fatal_reading_state (t0, "Missing version or right parenthesis");
2018 else
2019 fatal_reading_state (t0, "Bad version syntax");
2023 /* Read a pair. */
2024 void
2025 read_state_pair (pair_p *current)
2027 struct state_token_st *t0 = peek_state_token (0);
2028 struct state_token_st *t1 = peek_state_token (1);
2029 if (state_token_kind (t0) == STOK_LEFTPAR &&
2030 state_token_is_name (t1, "!pair"))
2032 *current = XCNEW (struct pair);
2033 next_state_tokens (2);
2034 t0 = peek_state_token (0);
2035 if (state_token_kind (t0) == STOK_STRING)
2037 if (strcmp (t0->stok_un.stok_string, "nil") == 0)
2039 (*current)->name = NULL;
2041 else
2043 (*current)->name = xstrdup (t0->stok_un.stok_string);
2045 next_state_tokens (1);
2046 read_state_type (&((*current)->type));
2047 read_state_fileloc (&((*current)->line));
2048 read_state_options (&((*current)->opt));;
2049 t0 = peek_state_token (0);
2050 if (state_token_kind (t0) == STOK_RIGHTPAR)
2052 next_state_tokens (1);
2054 else
2056 fatal_reading_state (t0, "Bad syntax for pair, )");
2059 else
2061 fatal_reading_state (t0, "Bad name for pair");
2064 else if (state_token_kind (t0) == STOK_NAME &&
2065 state_token_is_name (t0, "nil"))
2067 next_state_tokens (1);
2068 *current = NULL;
2070 else
2071 fatal_reading_state_printf (t0, "Bad syntax for pair, (!pair %d",
2072 state_token->stok_kind);
2076 /* Return the number of pairs actually read. */
2078 read_state_pair_list (pair_p *list)
2080 int nbpair = 0;
2081 pair_p head = NULL;
2082 pair_p previous = NULL;
2083 pair_p tmp = NULL;
2084 struct state_token_st *t0 = peek_state_token (0);
2085 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2087 read_state_pair (&tmp);
2088 if (head == NULL)
2090 head = tmp;
2091 previous = head;
2093 else
2095 previous->next = tmp;
2096 previous = tmp;
2098 t0 = peek_state_token (0);
2099 nbpair++;
2102 /* don't consume the ); the caller will eat it. */
2103 *list = head;
2104 return nbpair;
2107 /* Read the typedefs. */
2108 static void
2109 read_state_typedefs (pair_p *typedefs)
2111 int nbtypedefs = 0;
2112 pair_p list = NULL;
2113 struct state_token_st *t0 = peek_state_token (0);
2114 struct state_token_st *t1 = peek_state_token (1);
2115 struct state_token_st *t2 = peek_state_token (2);
2117 if (state_token_kind (t0) == STOK_LEFTPAR
2118 && state_token_is_name (t1, "!typedefs")
2119 && state_token_kind (t2) == STOK_INTEGER)
2121 int nbpairs = 0;
2122 nbtypedefs = t2->stok_un.stok_num;
2123 next_state_tokens (3);
2124 nbpairs = read_state_pair_list (&list);
2125 t0 = peek_state_token (0);
2126 if (nbpairs != nbtypedefs)
2127 fatal_reading_state_printf
2128 (t0,
2129 "invalid number of typedefs, expected %d but got %d",
2130 nbtypedefs, nbpairs);
2131 if (state_token_kind (t0) == STOK_RIGHTPAR)
2132 next_state_tokens (1);
2133 else
2134 fatal_reading_state (t0, "Bad typedefs syntax )");
2136 else
2137 fatal_reading_state (t0, "Bad typedefs syntax (!typedefs");
2139 if (verbosity_level >= 2)
2140 printf ("%s read %d typedefs from state\n", progname, nbtypedefs);
2141 *typedefs = list;
2145 /* Read the structures. */
2146 static void
2147 read_state_structures (type_p *structures)
2149 type_p head = NULL;
2150 type_p previous = NULL;
2151 type_p tmp;
2152 int nbstruct = 0, countstruct = 0;
2153 struct state_token_st *t0 = peek_state_token (0);
2154 struct state_token_st *t1 = peek_state_token (1);
2155 struct state_token_st *t2 = peek_state_token (2);
2157 if (state_token_kind (t0) == STOK_LEFTPAR
2158 && state_token_is_name (t1, "!structures")
2159 && state_token_kind (t2) == STOK_INTEGER)
2161 nbstruct = t2->stok_un.stok_num;
2162 next_state_tokens (3);
2163 t0 = peek_state_token (0);
2164 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2166 tmp = NULL;
2167 read_state_type (&tmp);
2168 countstruct++;
2169 if (head == NULL)
2171 head = tmp;
2172 previous = head;
2174 else
2176 previous->next = tmp;
2177 previous = tmp;
2179 t0 = peek_state_token (0);
2181 next_state_tokens (1);
2183 else
2184 fatal_reading_state (t0, "Bad structures syntax");
2185 if (countstruct != nbstruct)
2186 fatal_reading_state_printf (NULL_STATE_TOKEN,
2187 "expected %d structures but got %d",
2188 nbstruct, countstruct);
2189 if (verbosity_level >= 2)
2190 printf ("%s read %d structures from state\n", progname, nbstruct);
2191 *structures = head;
2195 /* Read the param_struct-s. */
2196 static void
2197 read_state_param_structs (type_p *param_structs)
2199 int nbparamstructs = 0;
2200 int countparamstructs = 0;
2201 type_p head = NULL;
2202 type_p previous = NULL;
2203 type_p tmp;
2204 struct state_token_st *t0 = peek_state_token (0);
2205 struct state_token_st *t1 = peek_state_token (1);
2206 struct state_token_st *t2 = peek_state_token (2);
2208 if (state_token_kind (t0) == STOK_LEFTPAR
2209 && state_token_is_name (t1, "!param_structs")
2210 && state_token_kind (t2) == STOK_INTEGER)
2212 nbparamstructs = t2->stok_un.stok_num;
2213 next_state_tokens (3);
2214 t0 = t1 = t2 = NULL;
2215 t0 = peek_state_token (0);
2216 while (state_token_kind (t0) != STOK_RIGHTPAR)
2218 tmp = NULL;
2219 read_state_type (&tmp);
2220 if (head == NULL)
2222 head = tmp;
2223 previous = head;
2225 else
2227 previous->next = tmp;
2228 previous = tmp;
2230 t0 = peek_state_token (0);
2231 countparamstructs++;
2233 next_state_tokens (1);
2235 else
2236 fatal_reading_state (t0, "Bad param_structs syntax");
2237 t0 = peek_state_token (0);
2238 if (countparamstructs != nbparamstructs)
2239 fatal_reading_state_printf
2240 (t0,
2241 "invalid number of param_structs expected %d got %d",
2242 nbparamstructs, countparamstructs);
2243 *param_structs = head;
2247 /* Read the variables. */
2248 static void
2249 read_state_variables (pair_p *variables)
2251 pair_p list = NULL;
2252 int nbvars = 0;
2253 struct state_token_st *t0 = peek_state_token (0);
2254 struct state_token_st *t1 = peek_state_token (1);
2255 struct state_token_st *t2 = peek_state_token (2);
2257 if (state_token_kind (t0) == STOK_LEFTPAR
2258 && state_token_is_name (t1, "!variables")
2259 && state_token_kind (t2) == STOK_INTEGER)
2261 int nbpairs = 0;
2262 nbvars = t2->stok_un.stok_num;
2263 next_state_tokens (3);
2264 nbpairs = read_state_pair_list (&list);
2265 t0 = peek_state_token (0);
2266 if (nbpairs != nbvars)
2267 fatal_reading_state_printf
2268 (t0, "Invalid number of variables, expected %d but got %d",
2269 nbvars, nbpairs);
2270 if (state_token_kind (t0) == STOK_RIGHTPAR)
2271 next_state_tokens (1);
2272 else
2273 fatal_reading_state (t0, "Waiting for ) in variables");
2275 else
2276 fatal_reading_state (t0, "Bad variables syntax");
2277 *variables = list;
2278 if (verbosity_level >= 2)
2279 printf ("%s read %d variables from state\n", progname, nbvars);
2283 /* Read the source directory. */
2284 static void
2285 read_state_srcdir (void)
2287 struct state_token_st *t0 = peek_state_token (0);
2288 struct state_token_st *t1 = peek_state_token (1);
2289 if (state_token_kind (t0) == STOK_LEFTPAR &&
2290 state_token_is_name (t1, "!srcdir"))
2292 next_state_tokens (2);
2293 t0 = peek_state_token (0);
2294 t1 = peek_state_token (1);
2295 if (state_token_kind (t0) == STOK_STRING &&
2296 state_token_kind (t1) == STOK_RIGHTPAR)
2298 srcdir = xstrdup (t0->stok_un.stok_string);
2299 srcdir_len = strlen (srcdir);
2300 next_state_tokens (2);
2301 return;
2305 fatal_reading_state (t0, "Bad srcdir in state_file");
2309 /* Read the sequence of GCC front-end languages. */
2310 static void
2311 read_state_languages (void)
2313 struct state_token_st *t0 = peek_state_token (0);
2314 struct state_token_st *t1 = peek_state_token (1);
2315 struct state_token_st *t2 = peek_state_token (2);
2316 if (state_token_kind (t0) == STOK_LEFTPAR
2317 && state_token_is_name (t1, "!languages")
2318 && state_token_kind (t2) == STOK_INTEGER)
2320 int i = 0;
2321 num_lang_dirs = t2->stok_un.stok_num;
2322 lang_dir_names = XCNEWVEC (const char *, num_lang_dirs);
2323 next_state_tokens (3);
2324 t0 = t1 = t2 = NULL;
2325 for (i = 0; i < (int) num_lang_dirs; i++)
2327 t0 = peek_state_token (0);
2328 if (state_token_kind (t0) != STOK_NAME)
2329 fatal_reading_state (t0, "expecting language name in state file");
2330 lang_dir_names[i] = t0->stok_un.stok_ident->stid_name;
2331 next_state_tokens (1);
2333 t0 = peek_state_token (0);
2334 if (state_token_kind (t0) != STOK_RIGHTPAR)
2335 fatal_reading_state (t0, "missing ) in languages list of state file");
2336 next_state_tokens (1);
2338 else
2339 fatal_reading_state (t0, "expecting languages list in state file");
2343 /* Read the sequence of files. */
2344 static void
2345 read_state_files_list (void)
2347 struct state_token_st *t0 = peek_state_token (0);
2348 struct state_token_st *t1 = peek_state_token (1);
2349 struct state_token_st *t2 = peek_state_token (2);
2351 if (state_token_kind (t0) == STOK_LEFTPAR
2352 && state_token_is_name (t1, "!fileslist")
2353 && state_token_kind (t2) == STOK_INTEGER)
2355 int i = 0;
2356 num_gt_files = t2->stok_un.stok_num;
2357 next_state_tokens (3);
2358 t0 = t1 = t2 = NULL;
2359 gt_files = XCNEWVEC (const input_file *, num_gt_files);
2360 for (i = 0; i < (int) num_gt_files; i++)
2362 bool issrcfile = FALSE;
2363 t0 = t1 = t2 = NULL;
2364 t0 = peek_state_token (0);
2365 t1 = peek_state_token (1);
2366 t2 = peek_state_token (2);
2367 if (state_token_kind (t0) == STOK_LEFTPAR
2368 && (state_token_is_name (t1, "!file")
2369 || (issrcfile = state_token_is_name (t1, "!srcfile")))
2370 && state_token_kind (t2) == STOK_INTEGER)
2372 lang_bitmap bmap = t2->stok_un.stok_num;
2373 next_state_tokens (3);
2374 t0 = t1 = t2 = NULL;
2375 t0 = peek_state_token (0);
2376 t1 = peek_state_token (1);
2377 if (state_token_kind (t0) == STOK_STRING
2378 && state_token_kind (t1) == STOK_RIGHTPAR)
2380 const char *fnam = t0->stok_un.stok_string;
2381 /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
2382 input_file *curgt = NULL;
2383 if (issrcfile)
2385 static const char dirsepstr[2] =
2386 { DIR_SEPARATOR, (char) 0 };
2387 char *fullpath = concat (srcdir, dirsepstr, fnam, NULL);
2388 curgt = input_file_by_name (fullpath);
2389 free (fullpath);
2391 else
2392 curgt = input_file_by_name (fnam);
2393 set_lang_bitmap (curgt, bmap);
2394 gt_files[i] = curgt;
2395 next_state_tokens (2);
2397 else
2398 fatal_reading_state (t0,
2399 "bad file in !fileslist of state file");
2401 else
2402 fatal_reading_state (t0,
2403 "expecting file in !fileslist of state file");
2405 t0 = peek_state_token (0);
2406 if (!state_token_kind (t0) == STOK_RIGHTPAR)
2407 fatal_reading_state (t0, "missing ) for !fileslist in state file");
2408 next_state_tokens (1);
2410 else
2411 fatal_reading_state (t0, "missing !fileslist in state file");
2415 /* Read the trailer. */
2416 static void
2417 read_state_trailer (void)
2419 struct state_token_st *t0 = peek_state_token (0);
2420 struct state_token_st *t1 = peek_state_token (1);
2421 struct state_token_st *t2 = peek_state_token (2);
2423 if (state_token_kind (t0) == STOK_LEFTPAR
2424 && state_token_is_name (t1, "!endfile")
2425 && state_token_kind (t2) == STOK_RIGHTPAR)
2426 next_state_tokens (3);
2427 else
2428 fatal_reading_state (t0, "missing !endfile in state file");
2432 /* Utility functions for the state_seen_types hash table. */
2433 static unsigned
2434 hash_type_number (const void *ty)
2436 const struct type *type = (const struct type *) ty;
2438 return type->state_number;
2441 static int
2442 equals_type_number (const void *ty1, const void *ty2)
2444 const struct type *type1 = (const struct type *) ty1;
2445 const struct type *type2 = (const struct type *) ty2;
2447 return type1->state_number == type2->state_number;
2450 static int
2451 string_eq (const void *a, const void *b)
2453 const char *a0 = (const char *)a;
2454 const char *b0 = (const char *)b;
2456 return (strcmp (a0, b0) == 0);
2460 /* The function reading the state, called by main from gengtype.c. */
2461 void
2462 read_state (const char *path)
2464 state_file = fopen (path, "r");
2465 if (state_file == NULL)
2466 fatal ("Failed to open state file %s for reading [%s]", path,
2467 xstrerror (errno));
2468 state_path = path;
2469 state_line = 1;
2471 if (verbosity_level >= 1)
2473 printf ("%s reading state file %s;", progname, state_path);
2474 if (verbosity_level >= 2)
2475 putchar ('\n');
2476 fflush (stdout);
2479 state_seen_types =
2480 htab_create (2017, hash_type_number, equals_type_number, NULL);
2481 state_ident_tab =
2482 htab_create (4027, htab_hash_string, string_eq, NULL);
2483 read_state_version (version_string);
2484 read_state_srcdir ();
2485 read_state_languages ();
2486 read_state_files_list ();
2487 read_state_structures (&structures);
2488 if (ferror (state_file))
2489 fatal_reading_state_printf
2490 (NULL_STATE_TOKEN, "input error while reading state [%s]",
2491 xstrerror (errno));
2492 read_state_typedefs (&typedefs);
2493 read_state_param_structs (&param_structs);
2494 read_state_variables (&variables);
2495 read_state_trailer ();
2497 if (verbosity_level >= 1)
2499 printf ("%s read %ld bytes.\n", progname, ftell (state_file));
2500 fflush (stdout);
2503 if (fclose (state_file))
2504 fatal ("failed to close read state file %s [%s]",
2505 path, xstrerror (errno));
2506 state_file = NULL;
2507 state_path = NULL;
2510 /* End of file gengtype-state.c. */