2013-01-29 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / gcc / gengtype-state.c
blob6092dad4179b29f94daba13c3eed1abc0dae5a96
1 /* Gengtype persistent state serialization & de-serialization.
2 Useful for gengtype in plugin mode.
4 Copyright (C) 2010-2013 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 case TYPE_UNDEFINED:
56 return CONST_CAST (struct fileloc*, &ty->u.s.line);
57 case TYPE_PARAM_STRUCT:
58 return CONST_CAST (struct fileloc*, &ty->u.param_struct.line);
59 case TYPE_SCALAR:
60 case TYPE_STRING:
61 case TYPE_POINTER:
62 case TYPE_ARRAY:
63 return NULL;
64 default:
65 gcc_unreachable ();
69 /* The state file has simplistic lispy lexical tokens. Its lexer gives
70 a linked list of struct state_token_st, through the peek_state_token
71 function. Lexical tokens are consumed with next_state_tokens. */
74 /* The lexical kind of each lispy token. */
75 enum state_token_en
77 STOK_NONE, /* Never used. */
78 STOK_INTEGER, /* Integer token. */
79 STOK_STRING, /* String token. */
80 STOK_LEFTPAR, /* Left opening parenthesis. */
81 STOK_RIGHTPAR, /* Right closing parenthesis. */
82 STOK_NAME /* hash-consed name or identifier. */
86 /* Structure and hash-table used to share identifiers or names. */
87 struct state_ident_st
89 /* TODO: We could improve the parser by reserving identifiers for
90 state keywords and adding a keyword number for them. That would
91 mean adding another field in this state_ident_st struct. */
92 char stid_name[1]; /* actually bigger & null terminated */
94 static htab_t state_ident_tab;
97 /* The state_token_st structure is for lexical tokens in the read
98 state file. The stok_kind field discriminates the union. Tokens
99 are allocated by peek_state_token which calls read_a_state_token
100 which allocate them. Tokens are freed by calls to
101 next_state_tokens. Token are organized in a FIFO look-ahead queue
102 filled by peek_state_token. */
103 struct state_token_st
105 enum state_token_en stok_kind; /* the lexical kind
106 discriminates the stok_un
107 union */
108 int stok_line; /* the line number */
109 int stok_col; /* the column number */
110 const char *stok_file; /* the file path */
111 struct state_token_st *stok_next; /* the next token in the
112 queue, when peeked */
113 union /* discriminated by stok_kind! */
115 int stok_num; /* when STOK_INTEGER */
116 char stok_string[1]; /* when STOK_STRING, actual size is
117 bigger and null terminated */
118 struct state_ident_st *stok_ident; /* when STOK_IDENT */
119 void *stok_ptr; /* null otherwise */
121 stok_un;
127 #define NULL_STATE_TOKEN (struct state_token_st*)0
129 /* the state_token pointer contains the leftmost current token. The
130 tokens are organized in a linked queue, using stok_next, for token
131 look-ahead. */
132 struct state_token_st *state_token = NULL_STATE_TOKEN;
134 /* Used by the reading lexer. */
135 static FILE *state_file;
136 static const char *state_path = NULL;
137 static int state_line = 0;
138 static long state_bol = 0; /* offset of beginning of line */
141 /* Counter of written types. */
142 static int state_written_type_count = 0;
145 /* Fatal error messages when reading the state. They are extremely
146 unlikely, and only appear when this gengtype-state.c file is buggy,
147 or when reading a gengtype state which was not generated by the
148 same version of gengtype or GCC. */
151 /* Fatal message while reading state. */
152 static inline void
153 fatal_reading_state (struct state_token_st* tok, const char*msg)
155 if (tok)
156 fatal ("%s:%d:%d: Invalid state file; %s",
157 tok->stok_file, tok->stok_line, tok->stok_col,
158 msg);
159 else
160 fatal ("%s:%d: Invalid state file; %s",
161 state_path, state_line, msg);
165 /* Fatal printf-like message while reading state. This can't be a
166 function, because there is no way to pass a va_arg to a variant of
167 fatal. */
168 #define fatal_reading_state_printf(Tok,Fmt,...) do { \
169 struct state_token_st* badtok = Tok; \
170 if (badtok) \
171 fatal ("%s:%d:%d: Invalid state file; " Fmt, \
172 badtok->stok_file, \
173 badtok->stok_line, \
174 badtok->stok_col, __VA_ARGS__); \
175 else \
176 fatal ("%s:%d: Invalid state file; " Fmt, \
177 state_path, state_line, __VA_ARGS__); \
178 } while(0)
181 /* Find or allocate an identifier in our name hash table. */
182 static struct state_ident_st *
183 state_ident_by_name (const char *name, enum insert_option optins)
185 PTR *slot = NULL;
186 int namlen = 0;
187 struct state_ident_st *stid = NULL;
189 if (!name || !name[0])
190 return NULL;
192 slot = htab_find_slot (state_ident_tab, name, optins);
193 if (!slot)
194 return NULL;
196 namlen = strlen (name);
197 stid =
198 (struct state_ident_st *) xmalloc (sizeof (struct state_ident_st) +
199 namlen);
200 memset (stid, 0, sizeof (struct state_ident_st) + namlen);
201 strcpy (stid->stid_name, name);
202 *slot = stid;
204 return stid;
207 /* Our token lexer is heavily inspired by MELT's lexer, and share some
208 code with the file gcc/melt-runtime.c of the GCC MELT branch! We
209 really want the gengtype state to be easily parsable by MELT. This
210 is a usual lispy lexing routine, dealing with spaces and comments,
211 numbers, parenthesis, names, strings. */
212 static struct state_token_st *
213 read_a_state_token (void)
215 int c = 0;
216 long curoff = 0;
217 struct state_token_st *tk = NULL;
219 again: /* Read again, e.g. after a comment or spaces. */
220 c = getc (state_file);
221 if (c == EOF)
222 return NULL;
224 /* Handle spaces, count lines. */
225 if (c == '\n')
227 state_line++;
228 state_bol = curoff = ftell (state_file);
229 goto again;
231 if (ISSPACE (c))
232 goto again;
233 /* Skip comments starting with semi-colon. */
234 if (c == ';')
238 c = getc (state_file);
240 while (c > 0 && c != '\n');
241 if (c == '\n')
243 state_line++;
244 state_bol = curoff = ftell (state_file);
246 goto again;
248 /* Read signed numbers. */
249 if (ISDIGIT (c) || c == '-' || c == '+')
250 { /* number */
251 int n = 0;
252 ungetc (c, state_file);
253 curoff = ftell (state_file);
254 if (fscanf (state_file, "%d", &n) <= 0)
255 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error in number");
256 tk = XCNEW (struct state_token_st);
257 tk->stok_kind = STOK_INTEGER;
258 tk->stok_line = state_line;
259 tk->stok_col = curoff - state_bol;
260 tk->stok_file = state_path;
261 tk->stok_next = NULL;
262 tk->stok_un.stok_num = n;
264 return tk;
266 /* Read an opening left parenthesis. */
267 else if (c == '(')
269 curoff = ftell (state_file);
270 tk = XCNEW (struct state_token_st);
271 tk->stok_kind = STOK_LEFTPAR;
272 tk->stok_line = state_line;
273 tk->stok_col = curoff - state_bol;
274 tk->stok_file = state_path;
275 tk->stok_next = NULL;
277 return tk;
279 /* Read an closing right parenthesis. */
280 else if (c == ')')
282 curoff = ftell (state_file);
283 tk = XCNEW (struct state_token_st);
284 tk->stok_kind = STOK_RIGHTPAR;
285 tk->stok_line = state_line;
286 tk->stok_col = curoff - state_bol;
287 tk->stok_file = state_path;
288 tk->stok_next = NULL;
290 return tk;
292 /* Read identifiers, using an obstack. */
293 else if (ISALPHA (c) || c == '_' || c == '$' || c == '!' || c == '#')
295 struct obstack id_obstack;
296 struct state_ident_st *sid = NULL;
297 char *ids = NULL;
298 obstack_init (&id_obstack);
299 curoff = ftell (state_file);
300 while (ISALNUM (c) || c == '_' || c == '$' || c == '!' || c == '#')
302 obstack_1grow (&id_obstack, c);
303 c = getc (state_file);
304 if (c < 0)
305 break;
307 if (c >= 0)
308 ungetc (c, state_file);
309 obstack_1grow (&id_obstack, (char) 0);
310 ids = XOBFINISH (&id_obstack, char *);
311 sid = state_ident_by_name (ids, INSERT);
312 obstack_free (&id_obstack, NULL);
313 ids = NULL;
314 tk = XCNEW (struct state_token_st);
315 tk->stok_kind = STOK_NAME;
316 tk->stok_line = state_line;
317 tk->stok_col = curoff - state_bol;
318 tk->stok_file = state_path;
319 tk->stok_next = NULL;
320 tk->stok_un.stok_ident = sid;
322 return tk;
324 /* Read a string, dealing with escape sequences a la C! */
325 else if (c == '"')
327 char *cstr = NULL;
328 int cslen = 0;
329 struct obstack bstring_obstack;
330 obstack_init (&bstring_obstack);
331 curoff = ftell (state_file);
332 while ((c = getc (state_file)) != '"' && c >= 0)
334 if (ISPRINT (c) && c != '\\')
335 obstack_1grow (&bstring_obstack, (char) c);
336 else if (ISSPACE (c) && c != '\n')
337 obstack_1grow (&bstring_obstack, (char) c);
338 else if (c == '\\')
340 c = getc (state_file);
341 switch (c)
343 case 'a':
344 obstack_1grow (&bstring_obstack, '\a');
345 c = getc (state_file);
346 break;
347 case 'b':
348 obstack_1grow (&bstring_obstack, '\b');
349 c = getc (state_file);
350 break;
351 case 't':
352 obstack_1grow (&bstring_obstack, '\t');
353 c = getc (state_file);
354 break;
355 case 'n':
356 obstack_1grow (&bstring_obstack, '\n');
357 c = getc (state_file);
358 break;
359 case 'v':
360 obstack_1grow (&bstring_obstack, '\v');
361 c = getc (state_file);
362 break;
363 case 'f':
364 obstack_1grow (&bstring_obstack, '\f');
365 c = getc (state_file);
366 break;
367 case 'r':
368 obstack_1grow (&bstring_obstack, '\r');
369 c = getc (state_file);
370 break;
371 case '"':
372 obstack_1grow (&bstring_obstack, '\"');
373 c = getc (state_file);
374 break;
375 case '\\':
376 obstack_1grow (&bstring_obstack, '\\');
377 c = getc (state_file);
378 break;
379 case ' ':
380 obstack_1grow (&bstring_obstack, ' ');
381 c = getc (state_file);
382 break;
383 case 'x':
385 unsigned int cx = 0;
386 if (fscanf (state_file, "%02x", &cx) > 0 && cx > 0)
387 obstack_1grow (&bstring_obstack, cx);
388 else
389 fatal_reading_state
390 (NULL_STATE_TOKEN,
391 "Lexical error in string hex escape");
392 c = getc (state_file);
393 break;
395 default:
396 fatal_reading_state
397 (NULL_STATE_TOKEN,
398 "Lexical error - unknown string escape");
401 else
402 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error...");
404 if (c != '"')
405 fatal_reading_state (NULL_STATE_TOKEN, "Unterminated string");
406 obstack_1grow (&bstring_obstack, '\0');
407 cstr = XOBFINISH (&bstring_obstack, char *);
408 cslen = strlen (cstr);
409 tk = (struct state_token_st *)
410 xcalloc (sizeof (struct state_token_st) + cslen, 1);
411 tk->stok_kind = STOK_STRING;
412 tk->stok_line = state_line;
413 tk->stok_col = curoff - state_bol;
414 tk->stok_file = state_path;
415 tk->stok_next = NULL;
416 strcpy (tk->stok_un.stok_string, cstr);
417 obstack_free (&bstring_obstack, NULL);
419 return tk;
421 /* Got an unexpected character. */
422 fatal_reading_state_printf
423 (NULL_STATE_TOKEN,
424 "Lexical error at offset %ld - bad character \\%03o = '%c'",
425 ftell (state_file), c, c);
428 /* Used for lexical look-ahead. Retrieves the lexical token of rank
429 DEPTH, starting with 0 when reading the state file. Gives null on
430 end of file. */
431 static struct state_token_st *
432 peek_state_token (int depth)
434 int remdepth = depth;
435 struct state_token_st **ptoken = &state_token;
436 struct state_token_st *tok = NULL;
438 while (remdepth >= 0)
440 if (*ptoken == NULL)
442 *ptoken = tok = read_a_state_token ();
443 if (tok == NULL)
444 return NULL;
446 tok = *ptoken;
447 ptoken = &((*ptoken)->stok_next);
448 remdepth--;
451 return tok;
454 /* Consume the next DEPTH tokens and free them. */
455 static void
456 next_state_tokens (int depth)
458 struct state_token_st *n;
460 while (depth > 0)
462 if (state_token != NULL)
464 n = state_token->stok_next;
465 free (state_token);
466 state_token = n;
468 else
469 fatal_reading_state (NULL_STATE_TOKEN, "Tokens stack empty");
471 depth--;
475 /* Safely retrieve the lexical kind of a token. */
476 static inline enum state_token_en
477 state_token_kind (struct state_token_st *p)
479 if (p == NULL)
480 return STOK_NONE;
481 else
482 return p->stok_kind;
485 /* Test if a token is a given name i.e. an identifier. */
486 static inline bool
487 state_token_is_name (struct state_token_st *p, const char *name)
489 if (p == NULL)
490 return false;
492 if (p->stok_kind != STOK_NAME)
493 return false;
495 return !strcmp (p->stok_un.stok_ident->stid_name, name);
499 /* Following routines are useful for serializing datas.
501 * We want to serialize :
502 * - typedefs list
503 * - structures list
504 * - param_structs list
505 * - variables list
507 * So, we have one routine for each kind of data. The main writing
508 * routine is write_state. The main reading routine is
509 * read_state. Most writing routines write_state_FOO have a
510 * corresponding reading routine read_state_FOO. Reading is done in a
511 * recursive descending way, and any read error is fatal.
514 /* When reading the state, we need to remember the previously seen
515 types by their state_number, since GTY-ed types are usually
516 shared. */
517 static htab_t state_seen_types;
519 /* Return the length of a linked list made of pairs. */
520 static int pair_list_length (pair_p list);
522 /* Write a pair */
523 static void write_state_pair (pair_p);
525 /* return the number of pairs written. Should match the length given
526 by pair_list_length. */
527 static int write_state_pair_list (pair_p list);
529 /* Write a type. When a type is written, its state_number is updated,
530 to ensure that a "reference" to a seen type is written on next
531 occurrences. */
532 static void write_state_type (type_p);
534 /* Write a null-terminatel string using our Lispy lexical conventions,
535 similar to those of C or MELT. */
536 static void write_state_a_string (const char *s);
538 /* Compute the length of a list of pairs, starting from the first
539 one. */
540 static int
541 pair_list_length (pair_p list)
543 int nbpair = 0;
544 pair_p l = NULL;
545 for (l = list; l; l = l->next)
546 nbpair++;
547 return nbpair;
550 /* Write a file location. Files relative to $(srcdir) are quite
551 frequent and are handled specially. This ensures that two gengtype
552 state file-s produced by gengtype on the same GCC source tree are
553 very similar and can be reasonably compared with diff, even if the
554 two GCC source trees have different absolute paths. */
555 static void
556 write_state_fileloc (struct fileloc *floc)
559 if (floc != NULL && floc->line > 0)
561 const char *srcrelpath = NULL;
562 gcc_assert (floc->file != NULL);
563 /* Most of the files are inside $(srcdir) so it is worth to
564 handle them specially. */
565 srcrelpath = get_file_srcdir_relative_path (floc->file);
566 if (srcrelpath != NULL)
568 fprintf (state_file, "\n(!srcfileloc ");
569 write_state_a_string (srcrelpath);
571 else
573 fprintf (state_file, "\n(!fileloc ");
574 write_state_a_string (get_input_file_name (floc->file));
576 fprintf (state_file, " %d", floc->line);
577 fprintf (state_file, ")\n");
579 else
580 fprintf (state_file, "nil ");
583 /* Write a list of fields. */
584 static void
585 write_state_fields (pair_p fields)
587 int nbfields = pair_list_length (fields);
588 int nbpairs = 0;
589 fprintf (state_file, "\n(!fields %d ", nbfields);
590 nbpairs = write_state_pair_list (fields);
591 gcc_assert (nbpairs == nbfields);
592 fprintf (state_file, ")\n");
595 /* Write a null-terminated string in our lexical convention, very
596 similar to the convention of C. */
597 static void
598 write_state_a_string (const char *s)
600 char c;
602 fputs (" \"", state_file);
603 for (; *s != 0; s++)
605 c = *s;
606 switch (c)
608 case '\a':
609 fputs ("\\a", state_file);
610 break;
611 case '\b':
612 fputs ("\\b", state_file);
613 break;
614 case '\t':
615 fputs ("\\t", state_file);
616 break;
617 case '\n':
618 fputs ("\\n", state_file);
619 break;
620 case '\v':
621 fputs ("\\v", state_file);
622 break;
623 case '\f':
624 fputs ("\\f", state_file);
625 break;
626 case '\r':
627 fputs ("\\r", state_file);
628 break;
629 case '\"':
630 fputs ("\\\"", state_file);
631 break;
632 case '\\':
633 fputs ("\\\\", state_file);
634 break;
635 default:
636 if (ISPRINT (c))
637 putc (c, state_file);
638 else
639 fprintf (state_file, "\\x%02x", (unsigned) c);
642 fputs ("\"", state_file);
645 /* Our option-s have three kinds, each with its writer. */
646 static void
647 write_state_string_option (options_p current)
649 fprintf (state_file, "string ");
650 if (current->info.string != NULL)
651 write_state_a_string (current->info.string);
652 else
653 fprintf (state_file, " nil ");
656 static void
657 write_state_type_option (options_p current)
659 fprintf (state_file, "type ");
660 write_state_type (current->info.type);
663 static void
664 write_state_nested_option (options_p current)
666 fprintf (state_file, "nested ");
667 write_state_type (current->info.nested->type);
668 if (current->info.nested->convert_from != NULL)
669 write_state_a_string (current->info.nested->convert_from);
670 else
671 fprintf (state_file, " nil ");
673 if (current->info.nested->convert_to != NULL)
674 write_state_a_string (current->info.nested->convert_to);
675 else
676 fprintf (state_file, " nil ");
679 static void
680 write_state_option (options_p current)
682 fprintf (state_file, "\n(!option ");
684 if (current->name != NULL)
685 fprintf (state_file, "%s ", current->name);
686 else
687 fprintf (state_file, "nil ");
689 switch (current->kind)
691 case OPTION_STRING:
692 write_state_string_option (current);
693 break;
694 case OPTION_TYPE:
695 write_state_type_option (current);
696 break;
697 case OPTION_NESTED:
698 write_state_nested_option (current);
699 break;
700 default:
701 fatal ("Option tag unknown");
704 fprintf (state_file, ")\n");
709 /* Write a list of GTY options. */
710 static void
711 write_state_options (options_p opt)
713 options_p current;
715 if (opt == NULL)
717 fprintf (state_file, "nil ");
718 return;
721 fprintf (state_file, "\n(!options ");
722 for (current = opt; current != NULL; current = current->next)
723 write_state_option (current);
724 fprintf (state_file, ")\n");
728 /* Write a bitmap representing a set of GCC front-end languages. */
729 static void
730 write_state_lang_bitmap (lang_bitmap bitmap)
732 fprintf (state_file, "%d ", (int) bitmap);
735 /* Write version information. */
736 static void
737 write_state_version (const char *version)
739 fprintf (state_file, "\n(!version ");
740 write_state_a_string (version);
741 fprintf (state_file, ")\n");
744 /* Common routine to write the common content of all types. */
745 static void write_state_common_type_content (type_p current);
747 /* Write a scalar type. We have only two of these. */
748 static void
749 write_state_scalar_type (type_p current)
751 if (current == &scalar_nonchar)
752 fprintf (state_file, "scalar_nonchar ");
753 else if (current == &scalar_char)
754 fprintf (state_file, "scalar_char ");
755 else
756 fatal ("Unexpected type in write_state_scalar_type");
758 write_state_common_type_content (current);
761 /* Write the string type. There is only one such thing! */
762 static void
763 write_state_string_type (type_p current)
765 if (current == &string_type)
767 fprintf (state_file, "string ");
768 write_state_common_type_content (current);
770 else
771 fatal ("Unexpected type in write_state_string_type");
774 /* Write an undefined type. */
775 static void
776 write_state_undefined_type (type_p current)
778 DBGPRINTF ("undefined type @ %p #%d '%s'", (void *) current,
779 current->state_number, current->u.s.tag);
780 fprintf (state_file, "undefined ");
781 gcc_assert (current->gc_used == GC_UNUSED);
782 write_state_common_type_content (current);
783 if (current->u.s.tag != NULL)
784 write_state_a_string (current->u.s.tag);
785 else
786 fprintf (state_file, "nil");
788 write_state_fileloc (type_lineloc (current));
792 /* Common code to write structure like types. */
793 static void
794 write_state_struct_union_type (type_p current, const char *kindstr)
796 DBGPRINTF ("%s type @ %p #%d '%s'", kindstr, (void *) current,
797 current->state_number, current->u.s.tag);
798 fprintf (state_file, "%s ", kindstr);
799 write_state_common_type_content (current);
800 if (current->u.s.tag != NULL)
801 write_state_a_string (current->u.s.tag);
802 else
803 fprintf (state_file, "nil");
805 write_state_fileloc (type_lineloc (current));
806 write_state_fields (current->u.s.fields);
807 write_state_options (current->u.s.opt);
808 write_state_lang_bitmap (current->u.s.bitmap);
812 /* Write a GTY struct type. */
813 static void
814 write_state_struct_type (type_p current)
816 write_state_struct_union_type (current, "struct");
817 write_state_type (current->u.s.lang_struct);
820 /* Write a GTY user-defined struct type. */
821 static void
822 write_state_user_struct_type (type_p current)
824 DBGPRINTF ("user_struct type @ %p #%d '%s'", (void *) current,
825 current->state_number, current->u.s.tag);
826 fprintf (state_file, "user_struct ");
827 write_state_common_type_content (current);
828 if (current->u.s.tag != NULL)
829 write_state_a_string (current->u.s.tag);
830 else
831 fprintf (state_file, "nil");
832 write_state_fileloc (type_lineloc (current));
833 write_state_fields (current->u.s.fields);
836 /* write a GTY union type. */
837 static void
838 write_state_union_type (type_p current)
840 write_state_struct_union_type (current, "union");
841 write_state_type (current->u.s.lang_struct);
844 /* Write a lang_struct type. This is tricky and was painful to debug,
845 we deal with the next field specifically within their lang_struct
846 subfield, which points to a linked list of homonumous types.
847 Change this function with extreme care, see also
848 read_state_lang_struct_type. */
849 static void
850 write_state_lang_struct_type (type_p current)
852 int nbhomontype = 0;
853 type_p hty = NULL;
854 const char *homoname = 0;
855 write_state_struct_union_type (current, "lang_struct");
856 /* lang_struct-ures are particularly tricky, since their
857 u.s.lang_struct field gives a list of homonymous struct-s or
858 union-s! */
859 DBGPRINTF ("lang_struct @ %p #%d", (void *) current, current->state_number);
860 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
862 nbhomontype++;
863 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype,
864 (void *) hty, hty->state_number, hty->u.s.tag);
865 /* Every member of the homonymous list should have the same tag. */
866 gcc_assert (union_or_struct_p (hty));
867 gcc_assert (hty->u.s.lang_struct == current);
868 if (!homoname)
869 homoname = hty->u.s.tag;
870 gcc_assert (strcmp (homoname, hty->u.s.tag) == 0);
872 fprintf (state_file, "(!homotypes %d\n", nbhomontype);
873 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
874 write_state_type (hty);
875 fprintf (state_file, ")\n");
878 /* Write a parametrized structure GTY type. */
879 static void
880 write_state_param_struct_type (type_p current)
882 int i;
884 fprintf (state_file, "param_struct ");
885 write_state_common_type_content (current);
886 write_state_type (current->u.param_struct.stru);
887 for (i = 0; i < NUM_PARAM; i++)
889 if (current->u.param_struct.param[i] != NULL)
890 write_state_type (current->u.param_struct.param[i]);
891 else
892 fprintf (state_file, "nil ");
894 write_state_fileloc (&current->u.param_struct.line);
897 /* Write a pointer type. */
898 static void
899 write_state_pointer_type (type_p current)
901 fprintf (state_file, "pointer ");
902 write_state_common_type_content (current);
903 write_state_type (current->u.p);
906 /* Write an array type. */
907 static void
908 write_state_array_type (type_p current)
910 fprintf (state_file, "array ");
911 write_state_common_type_content (current);
912 if (current->u.a.len != NULL)
913 write_state_a_string (current->u.a.len);
914 else
915 fprintf (state_file, " nil");
917 fprintf (state_file, " ");
918 write_state_type (current->u.a.p);
921 /* Write the gc_used information. */
922 static void
923 write_state_gc_used (enum gc_used_enum gus)
925 switch (gus)
927 case GC_UNUSED:
928 fprintf (state_file, " gc_unused");
929 break;
930 case GC_USED:
931 fprintf (state_file, " gc_used");
932 break;
933 case GC_MAYBE_POINTED_TO:
934 fprintf (state_file, " gc_maybe_pointed_to");
935 break;
936 case GC_POINTED_TO:
937 fprintf (state_file, " gc_pointed_to");
938 break;
939 default:
940 gcc_unreachable ();
944 /* Utility routine to write the common content of all types. Notice
945 that the next field is *not* written on purpose. */
946 static void
947 write_state_common_type_content (type_p current)
949 fprintf (state_file, "%d ", current->state_number);
950 /* We do not write the next type, because list of types are
951 explicitly written. However, lang_struct are special in that
952 respect. See function write_state_lang_struct_type for more. */
953 write_state_type (current->pointer_to);
954 write_state_gc_used (current->gc_used);
958 /* The important and recursive routine writing GTY types as understood
959 by gengtype. Types which have a positive state_number have already
960 been seen and written. */
961 static void
962 write_state_type (type_p current)
964 if (current == NULL)
966 fprintf (state_file, "nil ");
967 return;
970 fprintf (state_file, "\n(!type ");
972 if (current->state_number > 0)
973 fprintf (state_file, "already_seen %d", current->state_number);
974 else
976 state_written_type_count++;
977 DBGPRINTF ("writing type #%d @%p old number %d", state_written_type_count,
978 (void *) current, current->state_number);
979 current->state_number = state_written_type_count;
980 switch (current->kind)
982 case TYPE_NONE:
983 gcc_unreachable ();
984 case TYPE_UNDEFINED:
985 write_state_undefined_type (current);
986 break;
987 case TYPE_STRUCT:
988 write_state_struct_type (current);
989 break;
990 case TYPE_USER_STRUCT:
991 write_state_user_struct_type (current);
992 break;
993 case TYPE_UNION:
994 write_state_union_type (current);
995 break;
996 case TYPE_POINTER:
997 write_state_pointer_type (current);
998 break;
999 case TYPE_ARRAY:
1000 write_state_array_type (current);
1001 break;
1002 case TYPE_LANG_STRUCT:
1003 write_state_lang_struct_type (current);
1004 break;
1005 case TYPE_PARAM_STRUCT:
1006 write_state_param_struct_type (current);
1007 break;
1008 case TYPE_SCALAR:
1009 write_state_scalar_type (current);
1010 break;
1011 case TYPE_STRING:
1012 write_state_string_type (current);
1013 break;
1017 fprintf (state_file, ")\n");
1021 /* Write a pair. */
1022 static void
1023 write_state_pair (pair_p current)
1025 if (current == NULL)
1027 fprintf (state_file, "nil)");
1028 return;
1031 fprintf (state_file, "\n(!pair ");
1033 if (current->name != NULL)
1034 write_state_a_string (current->name);
1035 else
1036 write_state_a_string ("nil");
1038 write_state_type (current->type);
1039 write_state_fileloc (&(current->line));
1040 write_state_options (current->opt);
1042 fprintf (state_file, ")");
1045 /* Write a pair list and return the number of pairs written. */
1046 static int
1047 write_state_pair_list (pair_p list)
1049 int nbpair = 0;
1050 pair_p current;
1052 for (current = list; current != NULL; current = current->next)
1054 write_state_pair (current);
1055 nbpair++;
1057 return nbpair;
1061 /* When writing imported linked lists, like typedefs, structures,
1062 param_structs, ... we count their length first and write it. These
1063 eases the reading, and enables an extra verification on the number
1064 of actually read items. */
1066 /* Write our typedefs. */
1067 static void
1068 write_state_typedefs (void)
1070 int nbtypedefs = pair_list_length (typedefs);
1071 int nbpairs = 0;
1072 fprintf (state_file, "\n(!typedefs %d\n", nbtypedefs);
1073 nbpairs = write_state_pair_list (typedefs);
1074 gcc_assert (nbpairs == nbtypedefs);
1075 fprintf (state_file, ")\n");
1076 if (verbosity_level >= 2)
1077 printf ("%s wrote %d typedefs\n", progname, nbtypedefs);
1080 /* Write our structures. */
1081 static void
1082 write_state_structures (void)
1084 int nbstruct = 0;
1085 type_p current;
1087 for (current = structures; current != NULL; current = current->next)
1088 nbstruct++;
1090 fprintf (state_file, "\n(!structures %d\n", nbstruct);
1092 for (current = structures; current != NULL; current = current->next)
1093 write_state_type (current);
1095 fprintf (state_file, ")\n");
1096 if (verbosity_level >= 2)
1097 printf ("%s wrote %d structures in state\n", progname, nbstruct);
1100 /* Write our param_struct-s. */
1101 static void
1102 write_state_param_structs (void)
1104 int nbparamstruct = 0;
1105 type_p current;
1107 for (current = param_structs; current != NULL; current = current->next)
1108 nbparamstruct++;
1110 fprintf (state_file, "\n(!param_structs %d\n", nbparamstruct);
1112 for (current = param_structs; current != NULL; current = current->next)
1113 write_state_type (current);
1115 fprintf (state_file, ")\n");
1118 /* Write our variables. */
1119 static void
1120 write_state_variables (void)
1122 int nbvars = pair_list_length (variables);
1123 int nbpairs = 0;
1124 fprintf (state_file, "\n(!variables %d\n", nbvars);
1125 nbpairs = write_state_pair_list (variables);
1126 gcc_assert (nbpairs == nbvars);
1127 fprintf (state_file, ")\n");
1128 if (verbosity_level >= 2)
1129 printf ("%s wrote %d variables.\n", progname, nbvars);
1132 /* Write the source directory. File locations within the source
1133 directory have been written specifically. */
1134 static void
1135 write_state_srcdir (void)
1137 fprintf (state_file, "\n(!srcdir ");
1138 write_state_a_string (srcdir);
1139 fprintf (state_file, ")\n");
1142 /* Count and write the list of our files. */
1143 static void
1144 write_state_files_list (void)
1146 int i = 0;
1147 /* Write the list of files with their lang_bitmap. */
1148 fprintf (state_file, "\n(!fileslist %d\n", (int) num_gt_files);
1149 for (i = 0; i < (int) num_gt_files; i++)
1151 const char *cursrcrelpath = NULL;
1152 const input_file *curfil = gt_files[i];
1153 /* Most of the files are inside $(srcdir) so it is worth to
1154 handle them specially. */
1155 cursrcrelpath = get_file_srcdir_relative_path (curfil);
1156 if (cursrcrelpath)
1158 fprintf (state_file, "(!srcfile %d ", get_lang_bitmap (curfil));
1159 write_state_a_string (cursrcrelpath);
1161 else
1163 fprintf (state_file, "(!file %d ", get_lang_bitmap (curfil));
1164 write_state_a_string (get_input_file_name (curfil));
1166 fprintf (state_file, ")\n");
1168 fprintf (state_file, ")\n");
1171 /* Write the list of GCC front-end languages. */
1172 static void
1173 write_state_languages (void)
1175 int i = 0;
1176 fprintf (state_file, "\n(!languages %d", (int) num_lang_dirs);
1177 for (i = 0; i < (int) num_lang_dirs; i++)
1179 /* Languages names are identifiers, we expect only letters or
1180 underscores or digits in them. In particular, C++ is not a
1181 valid language name, but cp is valid. */
1182 fprintf (state_file, " %s", lang_dir_names[i]);
1184 fprintf (state_file, ")\n");
1187 /* Write the trailer. */
1188 static void
1189 write_state_trailer (void)
1191 /* This test should probably catch IO errors like disk full... */
1192 if (fputs ("\n(!endfile)\n", state_file) == EOF)
1193 fatal ("failed to write state trailer [%s]", xstrerror (errno));
1196 /* The write_state routine is the only writing routine called by main
1197 in gengtype.c. To avoid messing the state if gengtype is
1198 interrupted or aborted, we write a temporary file and rename it
1199 after having written it in totality. */
1200 void
1201 write_state (const char *state_path)
1203 long statelen = 0;
1204 time_t now = 0;
1205 char *temp_state_path = NULL;
1206 char tempsuffix[40];
1207 time (&now);
1209 /* We write a unique temporary file which is renamed when complete
1210 * only. So even if gengtype is interrupted, the written state file
1211 * won't be partially written, since the temporary file is not yet
1212 * renamed in that case. */
1213 memset (tempsuffix, 0, sizeof (tempsuffix));
1214 snprintf (tempsuffix, sizeof (tempsuffix) - 1, "-%ld-%d.tmp", (long) now,
1215 (int) getpid ());
1216 temp_state_path = concat (state_path, tempsuffix, NULL);
1217 state_file = fopen (temp_state_path, "w");
1218 if (state_file == NULL)
1219 fatal ("Failed to open file %s for writing state: %s",
1220 temp_state_path, xstrerror (errno));
1221 if (verbosity_level >= 3)
1222 printf ("%s writing state file %s temporarily in %s\n",
1223 progname, state_path, temp_state_path);
1224 /* This is the first line of the state. Perhaps the file utility
1225 could know about that, so don't change it often. */
1226 fprintf (state_file, ";;;;@@@@ GCC gengtype state\n");
1227 /* Output a few comments for humans. */
1228 fprintf (state_file,
1229 ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
1230 fprintf (state_file,
1231 ";;; The format of this file is tied to a particular version of GCC.\n");
1232 fprintf (state_file,
1233 ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1234 fprintf (state_file,
1235 ";;; This file should be parsed by the same %s which wrote it.\n",
1236 progname);
1237 /* The first non-comment significant line gives the version string. */
1238 write_state_version (version_string);
1239 write_state_srcdir ();
1240 write_state_languages ();
1241 write_state_files_list ();
1242 write_state_structures ();
1243 write_state_typedefs ();
1244 write_state_param_structs ();
1245 write_state_variables ();
1246 write_state_trailer ();
1247 statelen = ftell (state_file);
1248 if (ferror (state_file))
1249 fatal ("output error when writing state file %s [%s]",
1250 temp_state_path, xstrerror (errno));
1251 if (fclose (state_file))
1252 fatal ("failed to close state file %s [%s]",
1253 temp_state_path, xstrerror (errno));
1254 if (rename (temp_state_path, state_path))
1255 fatal ("failed to rename %s to state file %s [%s]", temp_state_path,
1256 state_path, xstrerror (errno));
1257 free (temp_state_path);
1259 if (verbosity_level >= 1)
1260 printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1261 progname, state_path, statelen, state_written_type_count);
1265 /** End of writing routines! The corresponding reading routines follow. **/
1269 /* Forward declarations, since some read_state_* functions are
1270 recursive! */
1271 static void read_state_fileloc (struct fileloc *line);
1272 static void read_state_options (options_p *opt);
1273 static void read_state_type (type_p *current);
1274 static void read_state_pair (pair_p *pair);
1275 /* Return the number of pairs actually read. */
1276 static int read_state_pair_list (pair_p *list);
1277 static void read_state_fields (pair_p *fields);
1278 static void read_state_common_type_content (type_p current);
1283 /* Record into the state_seen_types hash-table a type which we are
1284 reading, to enable recursive or circular references to it. */
1285 static void
1286 record_type (type_p type)
1288 PTR *slot;
1290 slot = htab_find_slot (state_seen_types, type, INSERT);
1291 gcc_assert (slot);
1293 *slot = type;
1296 /* Read an already seen type. */
1297 static void
1298 read_state_already_seen_type (type_p *type)
1300 struct state_token_st *t0 = peek_state_token (0);
1302 if (state_token_kind (t0) == STOK_INTEGER)
1304 PTR *slot = NULL;
1305 struct type loctype = { TYPE_SCALAR, 0, 0, 0, GC_UNUSED, {0} };
1307 loctype.state_number = t0->stok_un.stok_num;
1308 slot = htab_find_slot (state_seen_types, &loctype, NO_INSERT);
1309 if (slot == NULL)
1311 fatal_reading_state (t0, "Unknown type");
1314 next_state_tokens (1);
1315 *type = (type_p) *slot;
1317 else
1319 fatal_reading_state (t0, "Bad seen type");
1324 /* Read the scalar_nonchar type. */
1325 static void
1326 read_state_scalar_nonchar_type (type_p *type)
1328 *type = &scalar_nonchar;
1329 read_state_common_type_content (*type);
1333 /* Read the scalar_char type. */
1334 static void
1335 read_state_scalar_char_type (type_p *type)
1337 *type = &scalar_char;
1338 read_state_common_type_content (*type);
1341 /* Read the string_type. */
1342 static void
1343 read_state_string_type (type_p *type)
1345 *type = &string_type;
1346 read_state_common_type_content (*type);
1350 /* Read a lang_bitmap representing a set of GCC front-end languages. */
1351 static void
1352 read_state_lang_bitmap (lang_bitmap *bitmap)
1354 struct state_token_st *t;
1356 t = peek_state_token (0);
1357 if (state_token_kind (t) == STOK_INTEGER)
1359 *bitmap = t->stok_un.stok_num;
1360 next_state_tokens (1);
1362 else
1364 fatal_reading_state (t, "Bad syntax for bitmap");
1369 /* Read an undefined type. */
1370 static void
1371 read_state_undefined_type (type_p type)
1373 struct state_token_st *t0;
1375 type->kind = TYPE_UNDEFINED;
1376 read_state_common_type_content (type);
1377 t0 = peek_state_token (0);
1378 if (state_token_kind (t0) == STOK_STRING)
1380 if (state_token_is_name (t0, "nil"))
1382 type->u.s.tag = NULL;
1383 DBGPRINTF ("read anonymous undefined type @%p #%d",
1384 (void *) type, type->state_number);
1386 else
1388 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1389 DBGPRINTF ("read undefined type @%p #%d '%s'",
1390 (void *) type, type->state_number, type->u.s.tag);
1393 next_state_tokens (1);
1394 read_state_fileloc (&(type->u.s.line));
1396 else
1398 fatal_reading_state (t0, "Bad tag in undefined type");
1403 /* Read a GTY-ed struct type. */
1404 static void
1405 read_state_struct_type (type_p type)
1407 struct state_token_st *t0;
1409 type->kind = TYPE_STRUCT;
1410 read_state_common_type_content (type);
1411 t0 = peek_state_token (0);
1412 if (state_token_kind (t0) == STOK_STRING)
1414 if (state_token_is_name (t0, "nil"))
1416 type->u.s.tag = NULL;
1417 DBGPRINTF ("read anonymous struct type @%p #%d",
1418 (void *) type, type->state_number);
1420 else
1422 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1423 DBGPRINTF ("read struct type @%p #%d '%s'",
1424 (void *) type, type->state_number, type->u.s.tag);
1427 next_state_tokens (1);
1428 read_state_fileloc (&(type->u.s.line));
1429 read_state_fields (&(type->u.s.fields));
1430 read_state_options (&(type->u.s.opt));
1431 read_state_lang_bitmap (&(type->u.s.bitmap));
1432 read_state_type (&(type->u.s.lang_struct));
1434 else
1436 fatal_reading_state (t0, "Bad tag in struct type");
1441 /* Read a GTY-ed user-provided struct TYPE. */
1443 static void
1444 read_state_user_struct_type (type_p type)
1446 struct state_token_st *t0;
1448 type->kind = TYPE_USER_STRUCT;
1449 read_state_common_type_content (type);
1450 t0 = peek_state_token (0);
1451 if (state_token_kind (t0) == STOK_STRING)
1453 if (state_token_is_name (t0, "nil"))
1455 type->u.s.tag = NULL;
1456 DBGPRINTF ("read anonymous struct type @%p #%d",
1457 (void *) type, type->state_number);
1459 else
1461 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1462 DBGPRINTF ("read struct type @%p #%d '%s'",
1463 (void *) type, type->state_number, type->u.s.tag);
1466 next_state_tokens (1);
1467 read_state_fileloc (&(type->u.s.line));
1468 read_state_fields (&(type->u.s.fields));
1470 else
1472 fatal_reading_state (t0, "Bad tag in user-struct type");
1477 /* Read a GTY-ed union type. */
1478 static void
1479 read_state_union_type (type_p type)
1481 struct state_token_st *t0;
1483 type->kind = TYPE_UNION;
1484 read_state_common_type_content (type);
1485 t0 = peek_state_token (0);
1486 if (state_token_kind (t0) == STOK_STRING)
1488 if (state_token_is_name (t0, "nil"))
1490 type->u.s.tag = NULL;
1491 DBGPRINTF ("read anonymous union type @%p #%d",
1492 (void *) type, type->state_number);
1494 else
1496 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1497 DBGPRINTF ("read union type @%p #%d '%s'",
1498 (void *) type, type->state_number, type->u.s.tag);
1500 next_state_tokens (1);
1501 read_state_fileloc (&(type->u.s.line));
1502 read_state_fields (&(type->u.s.fields));
1503 read_state_options (&(type->u.s.opt));
1504 read_state_lang_bitmap (&(type->u.s.bitmap));
1505 read_state_type (&(type->u.s.lang_struct));
1507 else
1508 fatal_reading_state (t0, "Bad tag in union type");
1512 /* Read a GTY-ed pointer type. */
1513 static void
1514 read_state_pointer_type (type_p type)
1516 type->kind = TYPE_POINTER;
1517 read_state_common_type_content (type);
1518 DBGPRINTF ("read pointer type @%p #%d", (void *) type, type->state_number);
1519 read_state_type (&(type->u.p));
1523 /* Read a GTY-ed array type. */
1524 static void
1525 read_state_array_type (type_p type)
1527 struct state_token_st *t0;
1529 type->kind = TYPE_ARRAY;
1530 read_state_common_type_content (type);
1531 t0 = peek_state_token (0);
1532 if (state_token_kind (t0) == STOK_STRING)
1534 type->u.a.len = xstrdup (t0->stok_un.stok_string);
1535 DBGPRINTF ("read array type @%p #%d length '%s'",
1536 (void *) type, type->state_number, type->u.a.len);
1537 next_state_tokens (1);
1540 else if (state_token_is_name (t0, "nil"))
1542 type->u.a.len = NULL;
1543 DBGPRINTF ("read array type @%p #%d without length",
1544 (void *) type, type->state_number);
1545 next_state_tokens (1);
1548 else
1549 fatal_reading_state (t0, "Bad array name type");
1550 read_state_type (&(type->u.a.p));
1555 /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
1556 front-end languages. This is a tricky function and it was painful
1557 to debug. Change it with extreme care. See also
1558 write_state_lang_struct_type. */
1559 static void
1560 read_state_lang_struct_type (type_p type)
1562 struct state_token_st *t0 = NULL;
1563 struct state_token_st *t1 = NULL;
1564 struct state_token_st *t2 = NULL;
1566 type->kind = TYPE_LANG_STRUCT;
1567 read_state_common_type_content (type);
1568 t0 = peek_state_token (0);
1569 if (state_token_kind (t0) == STOK_STRING)
1571 if (state_token_is_name (t0, "nil"))
1573 DBGPRINTF ("read anonymous lang_struct type @%p #%d",
1574 (void *) type, type->state_number);
1575 type->u.s.tag = NULL;
1577 else
1579 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1580 DBGPRINTF ("read lang_struct type @%p #%d '%s'",
1581 (void *) type, type->state_number, type->u.s.tag);
1583 next_state_tokens (1);
1585 else
1586 fatal_reading_state (t0, "Bad tag in lang struct type");
1587 read_state_fileloc (&(type->u.s.line));
1588 read_state_fields (&(type->u.s.fields));
1589 read_state_options (&(type->u.s.opt));
1590 read_state_lang_bitmap (&(type->u.s.bitmap));
1591 /* Within lang_struct-ures, the lang_struct field is a linked list
1592 of homonymous types! */
1593 t0 = peek_state_token (0);
1594 t1 = peek_state_token (1);
1595 t2 = peek_state_token (2);
1596 /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
1597 if (state_token_kind (t0) == STOK_LEFTPAR
1598 && state_token_is_name (t1, "!homotypes")
1599 && state_token_kind (t2) == STOK_INTEGER)
1601 type_p *prevty = &type->u.s.lang_struct;
1602 int nbhomotype = t2->stok_un.stok_num;
1603 int i = 0;
1604 t0 = t1 = t2 = NULL;
1605 next_state_tokens (3);
1606 for (i = 0; i < nbhomotype; i++)
1608 read_state_type (prevty);
1609 t0 = peek_state_token (0);
1610 if (*prevty)
1611 prevty = &(*prevty)->next;
1612 else
1613 fatal_reading_state (t0,
1614 "expecting type in homotype list for lang_struct");
1616 if (state_token_kind (t0) != STOK_RIGHTPAR)
1617 fatal_reading_state (t0,
1618 "expecting ) in homotype list for lang_struct");
1619 next_state_tokens (1);
1621 else
1622 fatal_reading_state (t0, "expecting !homotypes for lang_struct");
1626 /* Read a param_struct type for GTY parametrized structures. */
1627 static void
1628 read_state_param_struct_type (type_p type)
1630 int i;
1631 struct state_token_st *t0;
1633 type->kind = TYPE_PARAM_STRUCT;
1634 read_state_common_type_content (type);
1635 DBGPRINTF ("read param_struct type @%p #%d",
1636 (void *) type, type->state_number);
1637 read_state_type (&(type->u.param_struct.stru));
1639 for (i = 0; i < NUM_PARAM; i++)
1641 t0 = peek_state_token (0);
1642 if (state_token_is_name (t0, "nil"))
1644 type->u.param_struct.param[i] = NULL;
1645 next_state_tokens (1);
1647 else
1648 read_state_type (&(type->u.param_struct.param[i]));
1650 read_state_fileloc (&(type->u.param_struct.line));
1654 /* Read the gc used information. */
1655 static void
1656 read_state_gc_used (enum gc_used_enum *pgus)
1658 struct state_token_st *t0 = peek_state_token (0);
1659 if (state_token_is_name (t0, "gc_unused"))
1660 *pgus = GC_UNUSED;
1661 else if (state_token_is_name (t0, "gc_used"))
1662 *pgus = GC_USED;
1663 else if (state_token_is_name (t0, "gc_maybe_pointed_to"))
1664 *pgus = GC_MAYBE_POINTED_TO;
1665 else if (state_token_is_name (t0, "gc_pointed_to"))
1666 *pgus = GC_POINTED_TO;
1667 else
1668 fatal_reading_state (t0, "invalid gc_used information");
1669 next_state_tokens (1);
1673 /* Utility function to read the common content of types. */
1674 static void
1675 read_state_common_type_content (type_p current)
1677 struct state_token_st *t0 = peek_state_token (0);
1679 if (state_token_kind (t0) == STOK_INTEGER)
1681 current->state_number = t0->stok_un.stok_num;
1682 next_state_tokens (1);
1683 record_type (current);
1685 else
1686 fatal_reading_state_printf (t0,
1687 "Expected integer for state_number line %d",
1688 state_line);
1689 /* We don't read the next field of the type. */
1690 read_state_type (&current->pointer_to);
1691 read_state_gc_used (&current->gc_used);
1695 /* Read a GTY-ed type. */
1696 void
1697 read_state_type (type_p *current)
1699 struct state_token_st *t0 = peek_state_token (0);
1700 struct state_token_st *t1 = peek_state_token (1);
1702 if (state_token_kind (t0) == STOK_LEFTPAR &&
1703 state_token_is_name (t1, "!type"))
1705 next_state_tokens (2);
1706 t0 = peek_state_token (0);
1707 if (state_token_is_name (t0, "already_seen"))
1709 next_state_tokens (1);
1710 read_state_already_seen_type (current);
1712 else
1714 t0 = peek_state_token (0);
1716 if (state_token_is_name (t0, "scalar_nonchar"))
1718 next_state_tokens (1);
1719 read_state_scalar_nonchar_type (current);
1721 else if (state_token_is_name (t0, "scalar_char"))
1723 next_state_tokens (1);
1724 read_state_scalar_char_type (current);
1726 else if (state_token_is_name (t0, "string"))
1728 next_state_tokens (1);
1729 read_state_string_type (current);
1731 else if (state_token_is_name (t0, "undefined"))
1733 *current = XCNEW (struct type);
1734 next_state_tokens (1);
1735 read_state_undefined_type (*current);
1737 else if (state_token_is_name (t0, "struct"))
1739 *current = XCNEW (struct type);
1740 next_state_tokens (1);
1741 read_state_struct_type (*current);
1743 else if (state_token_is_name (t0, "union"))
1745 *current = XCNEW (struct type);
1746 next_state_tokens (1);
1747 read_state_union_type (*current);
1749 else if (state_token_is_name (t0, "lang_struct"))
1751 *current = XCNEW (struct type);
1752 next_state_tokens (1);
1753 read_state_lang_struct_type (*current);
1755 else if (state_token_is_name (t0, "param_struct"))
1757 *current = XCNEW (struct type);
1758 next_state_tokens (1);
1759 read_state_param_struct_type (*current);
1761 else if (state_token_is_name (t0, "pointer"))
1763 *current = XCNEW (struct type);
1764 next_state_tokens (1);
1765 read_state_pointer_type (*current);
1767 else if (state_token_is_name (t0, "array"))
1769 *current = XCNEW (struct type);
1770 next_state_tokens (1);
1771 read_state_array_type (*current);
1773 else if (state_token_is_name (t0, "user_struct"))
1775 *current = XCNEW (struct type);
1776 next_state_tokens (1);
1777 read_state_user_struct_type (*current);
1779 else
1780 fatal_reading_state (t0, "bad type in (!type");
1782 t0 = peek_state_token (0);
1783 if (state_token_kind (t0) != STOK_RIGHTPAR)
1784 fatal_reading_state (t0, "missing ) in type");
1785 next_state_tokens (1);
1787 else if (state_token_is_name (t0, "nil"))
1789 next_state_tokens (1);
1790 *current = NULL;
1792 else
1793 fatal_reading_state (t0, "bad type syntax");
1797 /* Read a file location. Files within the source directory are dealt
1798 with specifically. */
1799 void
1800 read_state_fileloc (struct fileloc *floc)
1802 bool issrcfile = false;
1803 struct state_token_st *t0 = peek_state_token (0);
1804 struct state_token_st *t1 = peek_state_token (1);
1806 gcc_assert (floc != NULL);
1807 gcc_assert (srcdir != NULL);
1809 if (state_token_kind (t0) == STOK_LEFTPAR &&
1810 (state_token_is_name (t1, "!fileloc")
1811 || (issrcfile = state_token_is_name (t1, "!srcfileloc"))))
1813 next_state_tokens (2);
1814 t0 = peek_state_token (0);
1815 t1 = peek_state_token (1);
1816 if (state_token_kind (t0) == STOK_STRING &&
1817 state_token_kind (t1) == STOK_INTEGER)
1819 char *path = t0->stok_un.stok_string;
1820 if (issrcfile)
1822 static const char dirsepstr[2] = { DIR_SEPARATOR, (char) 0 };
1823 char *fullpath = concat (srcdir, dirsepstr, path, NULL);
1824 floc->file = input_file_by_name (fullpath);
1825 free (fullpath);
1827 else
1828 floc->file = input_file_by_name (path);
1829 floc->line = t1->stok_un.stok_num;
1830 next_state_tokens (2);
1832 else
1833 fatal_reading_state (t0,
1834 "Bad fileloc syntax, expected path string and line");
1835 t0 = peek_state_token (0);
1836 if (state_token_kind (t0) != STOK_RIGHTPAR)
1837 fatal_reading_state (t0, "Bad fileloc syntax, expected )");
1838 next_state_tokens (1);
1840 else if (state_token_is_name (t0, "nil"))
1842 next_state_tokens (1);
1843 floc->file = NULL;
1844 floc->line = 0;
1846 else
1847 fatal_reading_state (t0, "Bad fileloc syntax");
1851 /* Read the fields of a GTY-ed type. */
1852 void
1853 read_state_fields (pair_p *fields)
1855 pair_p tmp = NULL;
1856 struct state_token_st *t0 = peek_state_token (0);
1857 struct state_token_st *t1 = peek_state_token (1);
1858 struct state_token_st *t2 = peek_state_token (2);
1860 if (state_token_kind (t0) == STOK_LEFTPAR
1861 && state_token_is_name (t1, "!fields")
1862 && state_token_kind (t2) == STOK_INTEGER)
1864 int nbfields = t2->stok_un.stok_num;
1865 int nbpairs = 0;
1866 next_state_tokens (3);
1867 nbpairs = read_state_pair_list (&tmp);
1868 t0 = peek_state_token (0);
1869 if (nbpairs != nbfields)
1870 fatal_reading_state_printf
1871 (t0,
1872 "Mismatched fields number, expected %d got %d", nbpairs, nbfields);
1873 if (state_token_kind (t0) == STOK_RIGHTPAR)
1874 next_state_tokens (1);
1875 else
1876 fatal_reading_state (t0, "Bad fields expecting )");
1879 *fields = tmp;
1883 /* Read a string option. */
1884 static void
1885 read_state_string_option (options_p opt)
1887 struct state_token_st *t0 = peek_state_token (0);
1888 opt->kind = OPTION_STRING;
1889 if (state_token_kind (t0) == STOK_STRING)
1891 opt->info.string = xstrdup (t0->stok_un.stok_string);
1892 next_state_tokens (1);
1894 else if (state_token_is_name (t0, "nil"))
1896 opt->info.string = NULL;
1897 next_state_tokens (1);
1899 else
1900 fatal_reading_state (t0, "Missing name in string option");
1904 /* Read a type option. */
1905 static void
1906 read_state_type_option (options_p opt)
1908 opt->kind = OPTION_TYPE;
1909 read_state_type (&(opt->info.type));
1913 /* Read a nested option. */
1914 static void
1915 read_state_nested_option (options_p opt)
1917 struct state_token_st *t0;
1919 opt->info.nested = XCNEW (struct nested_ptr_data);
1920 opt->kind = OPTION_NESTED;
1921 read_state_type (&(opt->info.nested->type));
1922 t0 = peek_state_token (0);
1923 if (state_token_kind (t0) == STOK_STRING)
1925 opt->info.nested->convert_from = xstrdup (t0->stok_un.stok_string);
1926 next_state_tokens (1);
1928 else if (state_token_is_name (t0, "nil"))
1930 opt->info.nested->convert_from = NULL;
1931 next_state_tokens (1);
1933 else
1934 fatal_reading_state (t0, "Bad nested convert_from option");
1936 t0 = peek_state_token (0);
1937 if (state_token_kind (t0) == STOK_STRING)
1939 opt->info.nested->convert_to = xstrdup (t0->stok_un.stok_string);
1940 next_state_tokens (1);
1942 else if (state_token_is_name (t0, "nil"))
1944 opt->info.nested->convert_to = NULL;
1945 next_state_tokens (1);
1947 else
1948 fatal_reading_state (t0, "Bad nested convert_from option");
1952 /* Read an GTY option. */
1953 static void
1954 read_state_option (options_p *opt)
1956 struct state_token_st *t0 = peek_state_token (0);
1957 struct state_token_st *t1 = peek_state_token (1);
1959 if (state_token_kind (t0) == STOK_LEFTPAR &&
1960 state_token_is_name (t1, "!option"))
1962 next_state_tokens (2);
1963 t0 = peek_state_token (0);
1964 if (state_token_kind (t0) == STOK_NAME)
1966 *opt = XCNEW (struct options);
1967 if (state_token_is_name (t0, "nil"))
1968 (*opt)->name = NULL;
1969 else
1970 (*opt)->name = t0->stok_un.stok_ident->stid_name;
1971 next_state_tokens (1);
1972 t0 = peek_state_token (0);
1973 if (state_token_kind (t0) == STOK_NAME)
1975 if (state_token_is_name (t0, "string"))
1977 next_state_tokens (1);
1978 read_state_string_option (*opt);
1980 else if (state_token_is_name (t0, "type"))
1982 next_state_tokens (1);
1983 read_state_type_option (*opt);
1985 else if (state_token_is_name (t0, "nested"))
1987 next_state_tokens (1);
1988 read_state_nested_option (*opt);
1990 else
1991 fatal_reading_state (t0, "Bad option type");
1992 t0 = peek_state_token (0);
1993 if (state_token_kind (t0) != STOK_RIGHTPAR)
1994 fatal_reading_state (t0, "Bad syntax in option, expecting )");
1996 next_state_tokens (1);
1998 else
1999 fatal_reading_state (t0, "Missing option type");
2001 else
2002 fatal_reading_state (t0, "Bad name for option");
2004 else
2005 fatal_reading_state (t0, "Bad option, waiting for )");
2008 /* Read a list of options. */
2009 void
2010 read_state_options (options_p *opt)
2012 options_p head = NULL;
2013 options_p previous = NULL;
2014 options_p current_option = NULL;
2015 struct state_token_st *t0 = peek_state_token (0);
2016 struct state_token_st *t1 = peek_state_token (1);
2018 if (state_token_kind (t0) == STOK_LEFTPAR &&
2019 state_token_is_name (t1, "!options"))
2021 next_state_tokens (2);
2022 t0 = peek_state_token (0);
2023 while (state_token_kind (t0) != STOK_RIGHTPAR)
2025 read_state_option (&current_option);
2026 if (head == NULL)
2028 head = current_option;
2029 previous = head;
2031 else
2033 previous->next = current_option;
2034 previous = current_option;
2036 t0 = peek_state_token (0);
2038 next_state_tokens (1);
2040 else if (state_token_is_name (t0, "nil"))
2042 next_state_tokens (1);
2044 else
2045 fatal_reading_state (t0, "Bad options syntax");
2047 *opt = head;
2051 /* Read a version, and check against the version of the gengtype. */
2052 static void
2053 read_state_version (const char *version_string)
2055 struct state_token_st *t0 = peek_state_token (0);
2056 struct state_token_st *t1 = peek_state_token (1);
2058 if (state_token_kind (t0) == STOK_LEFTPAR &&
2059 state_token_is_name (t1, "!version"))
2061 next_state_tokens (2);
2062 t0 = peek_state_token (0);
2063 t1 = peek_state_token (1);
2064 if (state_token_kind (t0) == STOK_STRING &&
2065 state_token_kind (t1) == STOK_RIGHTPAR)
2067 /* Check that the read version string is the same as current
2068 version. */
2069 if (strcmp (version_string, t0->stok_un.stok_string))
2070 fatal_reading_state_printf (t0,
2071 "version string mismatch; expecting %s but got %s",
2072 version_string,
2073 t0->stok_un.stok_string);
2074 next_state_tokens (2);
2076 else
2077 fatal_reading_state (t0, "Missing version or right parenthesis");
2079 else
2080 fatal_reading_state (t0, "Bad version syntax");
2084 /* Read a pair. */
2085 void
2086 read_state_pair (pair_p *current)
2088 struct state_token_st *t0 = peek_state_token (0);
2089 struct state_token_st *t1 = peek_state_token (1);
2090 if (state_token_kind (t0) == STOK_LEFTPAR &&
2091 state_token_is_name (t1, "!pair"))
2093 *current = XCNEW (struct pair);
2094 next_state_tokens (2);
2095 t0 = peek_state_token (0);
2096 if (state_token_kind (t0) == STOK_STRING)
2098 if (strcmp (t0->stok_un.stok_string, "nil") == 0)
2100 (*current)->name = NULL;
2102 else
2104 (*current)->name = xstrdup (t0->stok_un.stok_string);
2106 next_state_tokens (1);
2107 read_state_type (&((*current)->type));
2108 read_state_fileloc (&((*current)->line));
2109 read_state_options (&((*current)->opt));;
2110 t0 = peek_state_token (0);
2111 if (state_token_kind (t0) == STOK_RIGHTPAR)
2113 next_state_tokens (1);
2115 else
2117 fatal_reading_state (t0, "Bad syntax for pair, )");
2120 else
2122 fatal_reading_state (t0, "Bad name for pair");
2125 else if (state_token_kind (t0) == STOK_NAME &&
2126 state_token_is_name (t0, "nil"))
2128 next_state_tokens (1);
2129 *current = NULL;
2131 else
2132 fatal_reading_state_printf (t0, "Bad syntax for pair, (!pair %d",
2133 state_token->stok_kind);
2137 /* Return the number of pairs actually read. */
2139 read_state_pair_list (pair_p *list)
2141 int nbpair = 0;
2142 pair_p head = NULL;
2143 pair_p previous = NULL;
2144 pair_p tmp = NULL;
2145 struct state_token_st *t0 = peek_state_token (0);
2146 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2148 read_state_pair (&tmp);
2149 if (head == NULL)
2151 head = tmp;
2152 previous = head;
2154 else
2156 previous->next = tmp;
2157 previous = tmp;
2159 t0 = peek_state_token (0);
2160 nbpair++;
2163 /* don't consume the ); the caller will eat it. */
2164 *list = head;
2165 return nbpair;
2168 /* Read the typedefs. */
2169 static void
2170 read_state_typedefs (pair_p *typedefs)
2172 int nbtypedefs = 0;
2173 pair_p list = NULL;
2174 struct state_token_st *t0 = peek_state_token (0);
2175 struct state_token_st *t1 = peek_state_token (1);
2176 struct state_token_st *t2 = peek_state_token (2);
2178 if (state_token_kind (t0) == STOK_LEFTPAR
2179 && state_token_is_name (t1, "!typedefs")
2180 && state_token_kind (t2) == STOK_INTEGER)
2182 int nbpairs = 0;
2183 nbtypedefs = t2->stok_un.stok_num;
2184 next_state_tokens (3);
2185 nbpairs = read_state_pair_list (&list);
2186 t0 = peek_state_token (0);
2187 if (nbpairs != nbtypedefs)
2188 fatal_reading_state_printf
2189 (t0,
2190 "invalid number of typedefs, expected %d but got %d",
2191 nbtypedefs, nbpairs);
2192 if (state_token_kind (t0) == STOK_RIGHTPAR)
2193 next_state_tokens (1);
2194 else
2195 fatal_reading_state (t0, "Bad typedefs syntax )");
2197 else
2198 fatal_reading_state (t0, "Bad typedefs syntax (!typedefs");
2200 if (verbosity_level >= 2)
2201 printf ("%s read %d typedefs from state\n", progname, nbtypedefs);
2202 *typedefs = list;
2206 /* Read the structures. */
2207 static void
2208 read_state_structures (type_p *structures)
2210 type_p head = NULL;
2211 type_p previous = NULL;
2212 type_p tmp;
2213 int nbstruct = 0, countstruct = 0;
2214 struct state_token_st *t0 = peek_state_token (0);
2215 struct state_token_st *t1 = peek_state_token (1);
2216 struct state_token_st *t2 = peek_state_token (2);
2218 if (state_token_kind (t0) == STOK_LEFTPAR
2219 && state_token_is_name (t1, "!structures")
2220 && state_token_kind (t2) == STOK_INTEGER)
2222 nbstruct = t2->stok_un.stok_num;
2223 next_state_tokens (3);
2224 t0 = peek_state_token (0);
2225 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2227 tmp = NULL;
2228 read_state_type (&tmp);
2229 countstruct++;
2230 if (head == NULL)
2232 head = tmp;
2233 previous = head;
2235 else
2237 previous->next = tmp;
2238 previous = tmp;
2240 t0 = peek_state_token (0);
2242 next_state_tokens (1);
2244 else
2245 fatal_reading_state (t0, "Bad structures syntax");
2246 if (countstruct != nbstruct)
2247 fatal_reading_state_printf (NULL_STATE_TOKEN,
2248 "expected %d structures but got %d",
2249 nbstruct, countstruct);
2250 if (verbosity_level >= 2)
2251 printf ("%s read %d structures from state\n", progname, nbstruct);
2252 *structures = head;
2256 /* Read the param_struct-s. */
2257 static void
2258 read_state_param_structs (type_p *param_structs)
2260 int nbparamstructs = 0;
2261 int countparamstructs = 0;
2262 type_p head = NULL;
2263 type_p previous = NULL;
2264 type_p tmp;
2265 struct state_token_st *t0 = peek_state_token (0);
2266 struct state_token_st *t1 = peek_state_token (1);
2267 struct state_token_st *t2 = peek_state_token (2);
2269 if (state_token_kind (t0) == STOK_LEFTPAR
2270 && state_token_is_name (t1, "!param_structs")
2271 && state_token_kind (t2) == STOK_INTEGER)
2273 nbparamstructs = t2->stok_un.stok_num;
2274 next_state_tokens (3);
2275 t0 = t1 = t2 = NULL;
2276 t0 = peek_state_token (0);
2277 while (state_token_kind (t0) != STOK_RIGHTPAR)
2279 tmp = NULL;
2280 read_state_type (&tmp);
2281 if (head == NULL)
2283 head = tmp;
2284 previous = head;
2286 else
2288 previous->next = tmp;
2289 previous = tmp;
2291 t0 = peek_state_token (0);
2292 countparamstructs++;
2294 next_state_tokens (1);
2296 else
2297 fatal_reading_state (t0, "Bad param_structs syntax");
2298 t0 = peek_state_token (0);
2299 if (countparamstructs != nbparamstructs)
2300 fatal_reading_state_printf
2301 (t0,
2302 "invalid number of param_structs expected %d got %d",
2303 nbparamstructs, countparamstructs);
2304 *param_structs = head;
2308 /* Read the variables. */
2309 static void
2310 read_state_variables (pair_p *variables)
2312 pair_p list = NULL;
2313 int nbvars = 0;
2314 struct state_token_st *t0 = peek_state_token (0);
2315 struct state_token_st *t1 = peek_state_token (1);
2316 struct state_token_st *t2 = peek_state_token (2);
2318 if (state_token_kind (t0) == STOK_LEFTPAR
2319 && state_token_is_name (t1, "!variables")
2320 && state_token_kind (t2) == STOK_INTEGER)
2322 int nbpairs = 0;
2323 nbvars = t2->stok_un.stok_num;
2324 next_state_tokens (3);
2325 nbpairs = read_state_pair_list (&list);
2326 t0 = peek_state_token (0);
2327 if (nbpairs != nbvars)
2328 fatal_reading_state_printf
2329 (t0, "Invalid number of variables, expected %d but got %d",
2330 nbvars, nbpairs);
2331 if (state_token_kind (t0) == STOK_RIGHTPAR)
2332 next_state_tokens (1);
2333 else
2334 fatal_reading_state (t0, "Waiting for ) in variables");
2336 else
2337 fatal_reading_state (t0, "Bad variables syntax");
2338 *variables = list;
2339 if (verbosity_level >= 2)
2340 printf ("%s read %d variables from state\n", progname, nbvars);
2344 /* Read the source directory. */
2345 static void
2346 read_state_srcdir (void)
2348 struct state_token_st *t0 = peek_state_token (0);
2349 struct state_token_st *t1 = peek_state_token (1);
2350 if (state_token_kind (t0) == STOK_LEFTPAR &&
2351 state_token_is_name (t1, "!srcdir"))
2353 next_state_tokens (2);
2354 t0 = peek_state_token (0);
2355 t1 = peek_state_token (1);
2356 if (state_token_kind (t0) == STOK_STRING &&
2357 state_token_kind (t1) == STOK_RIGHTPAR)
2359 srcdir = xstrdup (t0->stok_un.stok_string);
2360 srcdir_len = strlen (srcdir);
2361 next_state_tokens (2);
2362 return;
2366 fatal_reading_state (t0, "Bad srcdir in state_file");
2370 /* Read the sequence of GCC front-end languages. */
2371 static void
2372 read_state_languages (void)
2374 struct state_token_st *t0 = peek_state_token (0);
2375 struct state_token_st *t1 = peek_state_token (1);
2376 struct state_token_st *t2 = peek_state_token (2);
2377 if (state_token_kind (t0) == STOK_LEFTPAR
2378 && state_token_is_name (t1, "!languages")
2379 && state_token_kind (t2) == STOK_INTEGER)
2381 int i = 0;
2382 num_lang_dirs = t2->stok_un.stok_num;
2383 lang_dir_names = XCNEWVEC (const char *, num_lang_dirs);
2384 next_state_tokens (3);
2385 t0 = t1 = t2 = NULL;
2386 for (i = 0; i < (int) num_lang_dirs; i++)
2388 t0 = peek_state_token (0);
2389 if (state_token_kind (t0) != STOK_NAME)
2390 fatal_reading_state (t0, "expecting language name in state file");
2391 lang_dir_names[i] = t0->stok_un.stok_ident->stid_name;
2392 next_state_tokens (1);
2394 t0 = peek_state_token (0);
2395 if (state_token_kind (t0) != STOK_RIGHTPAR)
2396 fatal_reading_state (t0, "missing ) in languages list of state file");
2397 next_state_tokens (1);
2399 else
2400 fatal_reading_state (t0, "expecting languages list in state file");
2404 /* Read the sequence of files. */
2405 static void
2406 read_state_files_list (void)
2408 struct state_token_st *t0 = peek_state_token (0);
2409 struct state_token_st *t1 = peek_state_token (1);
2410 struct state_token_st *t2 = peek_state_token (2);
2412 if (state_token_kind (t0) == STOK_LEFTPAR
2413 && state_token_is_name (t1, "!fileslist")
2414 && state_token_kind (t2) == STOK_INTEGER)
2416 int i = 0;
2417 num_gt_files = t2->stok_un.stok_num;
2418 next_state_tokens (3);
2419 t0 = t1 = t2 = NULL;
2420 gt_files = XCNEWVEC (const input_file *, num_gt_files);
2421 for (i = 0; i < (int) num_gt_files; i++)
2423 bool issrcfile = FALSE;
2424 t0 = t1 = t2 = NULL;
2425 t0 = peek_state_token (0);
2426 t1 = peek_state_token (1);
2427 t2 = peek_state_token (2);
2428 if (state_token_kind (t0) == STOK_LEFTPAR
2429 && (state_token_is_name (t1, "!file")
2430 || (issrcfile = state_token_is_name (t1, "!srcfile")))
2431 && state_token_kind (t2) == STOK_INTEGER)
2433 lang_bitmap bmap = t2->stok_un.stok_num;
2434 next_state_tokens (3);
2435 t0 = t1 = t2 = NULL;
2436 t0 = peek_state_token (0);
2437 t1 = peek_state_token (1);
2438 if (state_token_kind (t0) == STOK_STRING
2439 && state_token_kind (t1) == STOK_RIGHTPAR)
2441 const char *fnam = t0->stok_un.stok_string;
2442 /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
2443 input_file *curgt = NULL;
2444 if (issrcfile)
2446 static const char dirsepstr[2] =
2447 { DIR_SEPARATOR, (char) 0 };
2448 char *fullpath = concat (srcdir, dirsepstr, fnam, NULL);
2449 curgt = input_file_by_name (fullpath);
2450 free (fullpath);
2452 else
2453 curgt = input_file_by_name (fnam);
2454 set_lang_bitmap (curgt, bmap);
2455 gt_files[i] = curgt;
2456 next_state_tokens (2);
2458 else
2459 fatal_reading_state (t0,
2460 "bad file in !fileslist of state file");
2462 else
2463 fatal_reading_state (t0,
2464 "expecting file in !fileslist of state file");
2466 t0 = peek_state_token (0);
2467 if (!state_token_kind (t0) == STOK_RIGHTPAR)
2468 fatal_reading_state (t0, "missing ) for !fileslist in state file");
2469 next_state_tokens (1);
2471 else
2472 fatal_reading_state (t0, "missing !fileslist in state file");
2476 /* Read the trailer. */
2477 static void
2478 read_state_trailer (void)
2480 struct state_token_st *t0 = peek_state_token (0);
2481 struct state_token_st *t1 = peek_state_token (1);
2482 struct state_token_st *t2 = peek_state_token (2);
2484 if (state_token_kind (t0) == STOK_LEFTPAR
2485 && state_token_is_name (t1, "!endfile")
2486 && state_token_kind (t2) == STOK_RIGHTPAR)
2487 next_state_tokens (3);
2488 else
2489 fatal_reading_state (t0, "missing !endfile in state file");
2493 /* Utility functions for the state_seen_types hash table. */
2494 static unsigned
2495 hash_type_number (const void *ty)
2497 const struct type *type = (const struct type *) ty;
2499 return type->state_number;
2502 static int
2503 equals_type_number (const void *ty1, const void *ty2)
2505 const struct type *type1 = (const struct type *) ty1;
2506 const struct type *type2 = (const struct type *) ty2;
2508 return type1->state_number == type2->state_number;
2511 static int
2512 string_eq (const void *a, const void *b)
2514 const char *a0 = (const char *)a;
2515 const char *b0 = (const char *)b;
2517 return (strcmp (a0, b0) == 0);
2521 /* The function reading the state, called by main from gengtype.c. */
2522 void
2523 read_state (const char *path)
2525 state_file = fopen (path, "r");
2526 if (state_file == NULL)
2527 fatal ("Failed to open state file %s for reading [%s]", path,
2528 xstrerror (errno));
2529 state_path = path;
2530 state_line = 1;
2532 if (verbosity_level >= 1)
2534 printf ("%s reading state file %s;", progname, state_path);
2535 if (verbosity_level >= 2)
2536 putchar ('\n');
2537 fflush (stdout);
2540 state_seen_types =
2541 htab_create (2017, hash_type_number, equals_type_number, NULL);
2542 state_ident_tab =
2543 htab_create (4027, htab_hash_string, string_eq, NULL);
2544 read_state_version (version_string);
2545 read_state_srcdir ();
2546 read_state_languages ();
2547 read_state_files_list ();
2548 read_state_structures (&structures);
2549 if (ferror (state_file))
2550 fatal_reading_state_printf
2551 (NULL_STATE_TOKEN, "input error while reading state [%s]",
2552 xstrerror (errno));
2553 read_state_typedefs (&typedefs);
2554 read_state_param_structs (&param_structs);
2555 read_state_variables (&variables);
2556 read_state_trailer ();
2558 if (verbosity_level >= 1)
2560 printf ("%s read %ld bytes.\n", progname, ftell (state_file));
2561 fflush (stdout);
2564 if (fclose (state_file))
2565 fatal ("failed to close read state file %s [%s]",
2566 path, xstrerror (errno));
2567 state_file = NULL;
2568 state_path = NULL;
2571 /* End of file gengtype-state.c. */