OSDN Git Service

2012-10-08 Tobias Burnus <burnus@net-b.de>
[pf3gnuchains/gcc-fork.git] / gcc / gengtype-state.c
1 /* Gengtype persistent state serialization & de-serialization.
2    Useful for gengtype in plugin mode.
3
4    Copyright (C) 2010  Free Software Foundation, Inc.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it under
9    the terms of the GNU General Public License as published by the Free
10    Software Foundation; either version 3, or (at your option) any later
11    version.
12
13    GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14    WARRANTY; without even the implied warranty of MERCHANTABILITY or
15    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16    for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GCC; see the file COPYING3.  If not see
20    <http://www.gnu.org/licenses/>.
21
22    Contributed by Jeremie Salvucci <jeremie.salvucci@free.fr>
23    and Basile Starynkevitch <basile@starynkevitch.net>
24 */
25
26 #ifdef 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"
38
39
40
41 /* Gives the file location of a type, if any.  */
42 static inline struct fileloc*
43 type_lineloc (const_type_p ty)
44 {
45   if (!ty)
46     return NULL;
47   switch (ty->kind)
48     {
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 ();
65     }
66 }
67
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.  */
71
72
73 /* The lexical kind of each lispy token.  */
74 enum state_token_en
75 {
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.  */
82 };
83
84
85 /* Structure and hash-table used to share identifiers or names.  */
86 struct state_ident_st
87 {
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 */
92 };
93 static htab_t state_ident_tab;
94
95
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
103 {
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! */
113   {
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 */
119   }
120   stok_un;
121 };
122
123
124
125
126 #define NULL_STATE_TOKEN (struct state_token_st*)0
127
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;
132
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 */
138
139
140 /* Counter of written types.  */
141 static int state_written_type_count = 0;
142
143
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.  */
148
149
150 /* Fatal message while reading state.  */
151 static inline void 
152 fatal_reading_state (struct state_token_st* tok, const char*msg)
153 {
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);
161 }
162
163
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)
178
179
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)
183 {
184   PTR *slot = NULL;
185   int namlen = 0;
186   struct state_ident_st *stid = NULL;
187
188   if (!name || !name[0])
189     return NULL;
190
191   slot = htab_find_slot (state_ident_tab, name, optins);
192   if (!slot)
193     return NULL;
194
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;
202
203   return stid;
204 }
205
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)
213 {
214   int c = 0;
215   long curoff = 0;
216   struct state_token_st *tk = NULL;
217
218  again: /* Read again, e.g. after a comment or spaces.  */
219   c = getc (state_file);
220   if (c == EOF)
221     return NULL;
222
223   /* Handle spaces, count lines.  */
224   if (c == '\n')
225     {
226       state_line++;
227       state_bol = curoff = ftell (state_file);
228       goto again;
229     };
230   if (ISSPACE (c))
231     goto again;
232   /* Skip comments starting with semi-colon.  */
233   if (c == ';')
234     {   
235       do
236         {
237           c = getc (state_file);
238         }
239       while (c > 0 && c != '\n');
240       if (c == '\n')
241         {
242           state_line++;
243           state_bol = curoff = ftell (state_file);
244         }
245       goto again;
246     };
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;
262
263       return tk;
264     }
265   /* Read an opening left parenthesis.  */
266   else if (c == '(')
267     {
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;
275
276       return tk;
277     }
278   /* Read an closing right parenthesis.  */
279   else if (c == ')')
280     {
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;
288
289       return tk;
290     }
291   /* Read identifiers, using an obstack.  */
292   else if (ISALPHA (c) || c == '_' || c == '$' || c == '!' || c == '#')
293     {
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 == '#')
300         {
301           obstack_1grow (&id_obstack, c);
302           c = getc (state_file);
303           if (c < 0)
304             break;
305         };
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;
320
321       return tk;
322     }
323   /* Read a string, dealing with escape sequences a la C! */
324   else if (c == '"')
325     {
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)
332         {
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 == '\\')
338             {
339               c = getc (state_file);
340               switch (c)
341                 {
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':
383                   {
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;
393                   }
394                 default:
395                   fatal_reading_state
396                     (NULL_STATE_TOKEN,
397                      "Lexical error - unknown string escape");
398                 }
399             }
400           else
401             fatal_reading_state (NULL_STATE_TOKEN, "Lexical error...");
402         };
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);
417
418       return tk;
419     }
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);
425 }
426
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)
432 {
433   int remdepth = depth;
434   struct state_token_st **ptoken = &state_token;
435   struct state_token_st *tok = NULL;
436
437   while (remdepth >= 0)
438     {
439       if (*ptoken == NULL)
440         {
441           *ptoken = tok = read_a_state_token ();
442           if (tok == NULL)
443             return NULL;
444         }
445       tok = *ptoken;
446       ptoken = &((*ptoken)->stok_next);
447       remdepth--;
448     }
449
450   return tok;
451 }
452
453 /* Consume the next DEPTH tokens and free them.  */
454 static void
455 next_state_tokens (int depth)
456 {
457   struct state_token_st *n;
458
459   while (depth > 0)
460     {
461       if (state_token != NULL)
462         {
463           n = state_token->stok_next;
464           free (state_token);
465           state_token = n;
466         }
467       else
468         fatal_reading_state (NULL_STATE_TOKEN, "Tokens stack empty");
469
470       depth--;
471     }
472 }
473
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)
477 {
478   if (p == NULL)
479     return STOK_NONE;
480   else
481     return p->stok_kind;
482 }
483
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)
487 {
488   if (p == NULL)
489     return false;
490
491   if (p->stok_kind != STOK_NAME)
492     return false;
493
494   return !strcmp (p->stok_un.stok_ident->stid_name, name);
495 }
496
497
498 /* Following routines are useful for serializing datas.
499  *
500  * We want to serialize :
501  *          - typedefs list
502  *          - structures list
503  *          - param_structs list
504  *          - variables list
505  *
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.
511  */
512
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;
517
518 /* Return the length of a linked list made of pairs.  */
519 static int pair_list_length (pair_p list);
520
521 /* Write a pair */
522 static void write_state_pair (pair_p);
523
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);
527
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);
532
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);
536
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)
541 {
542   int nbpair = 0;
543   pair_p l = NULL;
544   for (l = list; l; l = l->next)
545     nbpair++;
546   return nbpair;
547 }
548
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)
556 {
557
558   if (floc != NULL && floc->line > 0)
559     {
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)
566         {
567           fprintf (state_file, "\n(!srcfileloc ");
568           write_state_a_string (srcrelpath);
569         }
570       else
571         {
572           fprintf (state_file, "\n(!fileloc ");
573           write_state_a_string (get_input_file_name (floc->file));
574         }
575       fprintf (state_file, " %d", floc->line);
576       fprintf (state_file, ")\n");
577     }
578   else
579     fprintf (state_file, "nil ");
580 }
581
582 /* Write a list of fields.  */
583 static void
584 write_state_fields (pair_p fields)
585 {
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");
592 }
593
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)
598 {
599   char c;
600
601   fputs (" \"", state_file);
602   for (; *s != 0; s++)
603     {
604       c = *s;
605       switch (c)
606         {
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);
639         }
640     }
641   fputs ("\"", state_file);
642 }
643
644 /* Our option-s have three kinds, each with its writer.  */
645 static void
646 write_state_string_option (options_p current)
647 {
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 ");
653 }
654
655 static void
656 write_state_type_option (options_p current)
657 {
658   fprintf (state_file, "type ");
659   write_state_type (current->info.type);
660 }
661
662 static void
663 write_state_nested_option (options_p current)
664 {
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 ");
671
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 ");
676 }
677
678 static void
679 write_state_option (options_p current)
680 {
681   fprintf (state_file, "\n(!option ");
682
683   if (current->name != NULL)
684     fprintf (state_file, "%s ", current->name);
685   else
686     fprintf (state_file, "nil ");
687
688   switch (current->kind)
689     {
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");
701     }
702
703   fprintf (state_file, ")\n");
704 }
705
706
707
708 /* Write a list of GTY options.  */
709 static void
710 write_state_options (options_p opt)
711 {
712   options_p current;
713
714   if (opt == NULL)
715     {
716       fprintf (state_file, "nil ");
717       return;
718     }
719
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");
724 }
725
726
727 /* Write a bitmap representing a set of GCC front-end languages.  */
728 static void
729 write_state_lang_bitmap (lang_bitmap bitmap)
730 {
731   fprintf (state_file, "%d ", (int) bitmap);
732 }
733
734 /* Write version information.  */
735 static void
736 write_state_version (const char *version)
737 {
738   fprintf (state_file, "\n(!version ");
739   write_state_a_string (version);
740   fprintf (state_file, ")\n");
741 }
742
743 /* Common routine to write the common content of all types.  */
744 static void write_state_common_type_content (type_p current);
745
746 /* Write a scalar type.  We have only two of these.  */
747 static void
748 write_state_scalar_type (type_p current)
749 {
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");
756
757   write_state_common_type_content (current);
758 }
759
760 /* Write the string type.  There is only one such thing! */
761 static void
762 write_state_string_type (type_p current)
763 {
764   if (current == &string_type)
765     {
766       fprintf (state_file, "string ");
767       write_state_common_type_content (current);
768     }
769   else
770     fatal ("Unexpected type in write_state_string_type");
771 }
772
773
774 /* Common code to write structure like types.  */
775 static void
776 write_state_struct_union_type (type_p current, const char *kindstr)
777 {
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");
786
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);
791 }
792
793
794 /* Write a GTY struct type.  */
795 static void
796 write_state_struct_type (type_p current)
797 {
798   write_state_struct_union_type (current, "struct");
799   write_state_type (current->u.s.lang_struct);
800 }
801
802 /* Write a GTY user-defined struct type.  */
803 static void
804 write_state_user_struct_type (type_p current)
805 {
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);
816 }
817
818 /* write a GTY union type.  */
819 static void
820 write_state_union_type (type_p current)
821 {
822   write_state_struct_union_type (current, "union");
823   write_state_type (current->u.s.lang_struct);
824 }
825
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)
833 {
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)
843     {
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);
853     }
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");
858 }
859
860 /* Write a parametrized structure GTY type.  */
861 static void
862 write_state_param_struct_type (type_p current)
863 {
864   int i;
865
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++)
870     {
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 ");
875     }
876   write_state_fileloc (&current->u.param_struct.line);
877 }
878
879 /* Write a pointer type.  */
880 static void
881 write_state_pointer_type (type_p current)
882 {
883   fprintf (state_file, "pointer ");
884   write_state_common_type_content (current);
885   write_state_type (current->u.p);
886 }
887
888 /* Write an array type.  */
889 static void
890 write_state_array_type (type_p current)
891 {
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");
898
899   fprintf (state_file, " ");
900   write_state_type (current->u.a.p);
901 }
902
903 /* Write the gc_used information.  */
904 static void
905 write_state_gc_used (enum gc_used_enum gus)
906 {
907   switch (gus)
908     {
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 ();
923     }
924 }
925
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)
930 {
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);
937 }
938
939
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)
945 {
946   if (current == NULL)
947     {
948       fprintf (state_file, "nil ");
949       return;
950     }
951
952   fprintf (state_file, "\n(!type ");
953
954   if (current->state_number > 0)
955     fprintf (state_file, "already_seen %d", current->state_number);
956   else
957     {
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)
963         {
964         case TYPE_STRUCT:
965           write_state_struct_type (current);
966           break;
967         case TYPE_USER_STRUCT:
968           write_state_user_struct_type (current);
969           break;
970         case TYPE_UNION:
971           write_state_union_type (current);
972           break;
973         case TYPE_POINTER:
974           write_state_pointer_type (current);
975           break;
976         case TYPE_ARRAY:
977           write_state_array_type (current);
978           break;
979         case TYPE_LANG_STRUCT:
980           write_state_lang_struct_type (current);
981           break;
982         case TYPE_PARAM_STRUCT:
983           write_state_param_struct_type (current);
984           break;
985         case TYPE_SCALAR:
986           write_state_scalar_type (current);
987           break;
988         case TYPE_STRING:
989           write_state_string_type (current);
990           break;
991
992         default:
993           fatal ("Unexpected type...");
994         }
995     }
996
997   fprintf (state_file, ")\n");
998 }
999
1000
1001 /* Write a pair.  */
1002 static void
1003 write_state_pair (pair_p current)
1004 {
1005   if (current == NULL)
1006     {
1007       fprintf (state_file, "nil)");
1008       return;
1009     }
1010
1011   fprintf (state_file, "\n(!pair ");
1012
1013   if (current->name != NULL)
1014     write_state_a_string (current->name);
1015   else
1016     write_state_a_string ("nil");
1017
1018   write_state_type (current->type);
1019   write_state_fileloc (&(current->line));
1020   write_state_options (current->opt);
1021
1022   fprintf (state_file, ")");
1023 }
1024
1025 /* Write a pair list and return the number of pairs written.  */
1026 static int
1027 write_state_pair_list (pair_p list)
1028 {
1029   int nbpair = 0;
1030   pair_p current;
1031
1032   for (current = list; current != NULL; current = current->next)
1033     {
1034       write_state_pair (current);
1035       nbpair++;
1036     }
1037   return nbpair;
1038
1039 }
1040
1041 /* When writing imported linked lists, like typedefs, structures,
1042    param_structs, ... we count their length first and write it.  These
1043    eases the reading, and enables an extra verification on the number
1044    of actually read items.  */
1045
1046 /* Write our typedefs.  */
1047 static void
1048 write_state_typedefs (void)
1049 {
1050   int nbtypedefs = pair_list_length (typedefs);
1051   int nbpairs = 0;
1052   fprintf (state_file, "\n(!typedefs %d\n", nbtypedefs);
1053   nbpairs = write_state_pair_list (typedefs);
1054   gcc_assert (nbpairs == nbtypedefs);
1055   fprintf (state_file, ")\n");
1056   if (verbosity_level >= 2)
1057     printf ("%s wrote %d typedefs\n", progname, nbtypedefs);
1058 }
1059
1060 /* Write our structures.  */
1061 static void
1062 write_state_structures (void)
1063 {
1064   int nbstruct = 0;
1065   type_p current;
1066
1067   for (current = structures; current != NULL; current = current->next)
1068     nbstruct++;
1069
1070   fprintf (state_file, "\n(!structures %d\n", nbstruct);
1071
1072   for (current = structures; current != NULL; current = current->next)
1073     write_state_type (current);
1074
1075   fprintf (state_file, ")\n");
1076   if (verbosity_level >= 2)
1077     printf ("%s wrote %d structures in state\n", progname, nbstruct);
1078 }
1079
1080 /* Write our param_struct-s.  */
1081 static void
1082 write_state_param_structs (void)
1083 {
1084   int nbparamstruct = 0;
1085   type_p current;
1086
1087   for (current = param_structs; current != NULL; current = current->next)
1088     nbparamstruct++;
1089
1090   fprintf (state_file, "\n(!param_structs %d\n", nbparamstruct);
1091
1092   for (current = param_structs; current != NULL; current = current->next)
1093     write_state_type (current);
1094
1095   fprintf (state_file, ")\n");
1096 }
1097
1098 /* Write our variables.  */
1099 static void
1100 write_state_variables (void)
1101 {
1102   int nbvars = pair_list_length (variables);
1103   int nbpairs = 0;
1104   fprintf (state_file, "\n(!variables %d\n", nbvars);
1105   nbpairs = write_state_pair_list (variables);
1106   gcc_assert (nbpairs == nbvars);
1107   fprintf (state_file, ")\n");
1108   if (verbosity_level >= 2)
1109     printf ("%s wrote %d variables.\n", progname, nbvars);
1110 }
1111
1112 /* Write the source directory.  File locations within the source
1113    directory have been written specifically.  */
1114 static void
1115 write_state_srcdir (void)
1116 {
1117   fprintf (state_file, "\n(!srcdir ");
1118   write_state_a_string (srcdir);
1119   fprintf (state_file, ")\n");
1120 }
1121
1122 /* Count and write the list of our files.  */
1123 static void
1124 write_state_files_list (void)
1125 {
1126   int i = 0;
1127   /* Write the list of files with their lang_bitmap.  */
1128   fprintf (state_file, "\n(!fileslist %d\n", (int) num_gt_files);
1129   for (i = 0; i < (int) num_gt_files; i++)
1130     {
1131       const char *cursrcrelpath = NULL;
1132       const input_file *curfil = gt_files[i];
1133       /* Most of the files are inside $(srcdir) so it is worth to
1134          handle them specially.  */
1135       cursrcrelpath = get_file_srcdir_relative_path (curfil);
1136       if (cursrcrelpath)
1137         {
1138           fprintf (state_file, "(!srcfile %d ", get_lang_bitmap (curfil));
1139           write_state_a_string (cursrcrelpath);
1140         }
1141       else
1142         {
1143           fprintf (state_file, "(!file %d ", get_lang_bitmap (curfil));
1144           write_state_a_string (get_input_file_name (curfil));
1145         }
1146       fprintf (state_file, ")\n");
1147     }
1148   fprintf (state_file, ")\n");
1149 }
1150
1151 /* Write the list of GCC front-end languages.  */
1152 static void
1153 write_state_languages (void)
1154 {
1155   int i = 0;
1156   fprintf (state_file, "\n(!languages %d", (int) num_lang_dirs);
1157   for (i = 0; i < (int) num_lang_dirs; i++)
1158     {
1159       /* Languages names are identifiers, we expect only letters or
1160          underscores or digits in them.  In particular, C++ is not a
1161          valid language name, but cp is valid.  */
1162       fprintf (state_file, " %s", lang_dir_names[i]);
1163     }
1164   fprintf (state_file, ")\n");
1165 }
1166
1167 /* Write the trailer.  */
1168 static void
1169 write_state_trailer (void)
1170 {
1171   /* This test should probably catch IO errors like disk full...  */
1172   if (fputs ("\n(!endfile)\n", state_file) == EOF)
1173     fatal ("failed to write state trailer [%s]", xstrerror (errno));
1174 }
1175
1176 /* The write_state routine is the only writing routine called by main
1177    in gengtype.c.  To avoid messing the state if gengtype is
1178    interrupted or aborted, we write a temporary file and rename it
1179    after having written it in totality.  */
1180 void
1181 write_state (const char *state_path)
1182 {
1183   long statelen = 0;
1184   time_t now = 0;
1185   char *temp_state_path = NULL;
1186   char tempsuffix[40];
1187   time (&now);
1188
1189   /* We write a unique temporary file which is renamed when complete
1190    * only.  So even if gengtype is interrupted, the written state file
1191    * won't be partially written, since the temporary file is not yet
1192    * renamed in that case.  */
1193   memset (tempsuffix, 0, sizeof (tempsuffix));
1194   snprintf (tempsuffix, sizeof (tempsuffix) - 1, "-%ld-%d.tmp", (long) now,
1195             (int) getpid ());
1196   temp_state_path = concat (state_path, tempsuffix, NULL);
1197   state_file = fopen (temp_state_path, "w");
1198   if (state_file == NULL)
1199     fatal ("Failed to open file %s for writing state: %s",
1200            temp_state_path, xstrerror (errno));
1201   if (verbosity_level >= 3)
1202     printf ("%s writing state file %s temporarily in %s\n",
1203             progname, state_path, temp_state_path);
1204   /* This is the first line of the state.  Perhaps the file utility
1205      could know about that, so don't change it often.  */
1206   fprintf (state_file, ";;;;@@@@ GCC gengtype state\n");
1207   /* Output a few comments for humans. */
1208   fprintf (state_file,
1209            ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
1210   fprintf (state_file,
1211            ";;; The format of this file is tied to a particular version of GCC.\n");
1212   fprintf (state_file,
1213            ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1214   fprintf (state_file,
1215            ";;; This file should be parsed by the same %s which wrote it.\n",
1216            progname);
1217   /* The first non-comment significant line gives the version string.  */
1218   write_state_version (version_string);
1219   write_state_srcdir ();
1220   write_state_languages ();
1221   write_state_files_list ();
1222   write_state_structures ();
1223   write_state_typedefs ();
1224   write_state_param_structs ();
1225   write_state_variables ();
1226   write_state_trailer ();
1227   statelen = ftell (state_file);
1228   if (ferror (state_file))
1229     fatal ("output error when writing state file %s [%s]",
1230            temp_state_path, xstrerror (errno));
1231   if (fclose (state_file))
1232     fatal ("failed to close state file %s [%s]",
1233            temp_state_path, xstrerror (errno));
1234   if (rename (temp_state_path, state_path))
1235     fatal ("failed to rename %s to state file %s [%s]", temp_state_path,
1236            state_path, xstrerror (errno));
1237   free (temp_state_path);
1238
1239   if (verbosity_level >= 1)
1240     printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1241             progname, state_path, statelen, state_written_type_count);
1242
1243 }
1244 \f
1245 /** End of writing routines!  The corresponding reading routines follow.  **/
1246
1247
1248
1249 /* Forward declarations, since some read_state_* functions are
1250    recursive! */
1251 static void read_state_fileloc (struct fileloc *line);
1252 static void read_state_options (options_p *opt);
1253 static void read_state_type (type_p *current);
1254 static void read_state_pair (pair_p *pair);
1255 /* Return the number of pairs actually read.  */
1256 static int read_state_pair_list (pair_p *list);
1257 static void read_state_fields (pair_p *fields);
1258 static void read_state_common_type_content (type_p current);
1259
1260
1261
1262
1263 /* Record into the state_seen_types hash-table a type which we are
1264    reading, to enable recursive or circular references to it.  */
1265 static void
1266 record_type (type_p type)
1267 {
1268   PTR *slot;
1269
1270   slot = htab_find_slot (state_seen_types, type, INSERT);
1271   gcc_assert (slot);
1272
1273   *slot = type;
1274 }
1275
1276 /* Read an already seen type.  */
1277 static void
1278 read_state_already_seen_type (type_p *type)
1279 {
1280   struct state_token_st *t0 = peek_state_token (0);
1281
1282   if (state_token_kind (t0) == STOK_INTEGER)
1283     {
1284       PTR *slot = NULL;
1285       struct type loctype = { TYPE_SCALAR, 0, 0, 0, GC_UNUSED, {0} };
1286
1287       loctype.state_number = t0->stok_un.stok_num;
1288       slot = htab_find_slot (state_seen_types, &loctype, NO_INSERT);
1289       if (slot == NULL)
1290         {
1291           fatal_reading_state (t0, "Unknown type");
1292         }
1293
1294       next_state_tokens (1);
1295       *type = (type_p) *slot;
1296     }
1297   else
1298     {
1299       fatal_reading_state (t0, "Bad seen type");
1300     }
1301 }
1302
1303
1304 /* Read the scalar_nonchar type.  */
1305 static void
1306 read_state_scalar_nonchar_type (type_p *type)
1307 {
1308   *type = &scalar_nonchar;
1309   read_state_common_type_content (*type);
1310 }
1311
1312
1313 /* Read the scalar_char type.  */
1314 static void
1315 read_state_scalar_char_type (type_p *type)
1316 {
1317   *type = &scalar_char;
1318   read_state_common_type_content (*type);
1319 }
1320
1321
1322 /* Read the string_type.  */
1323 static void
1324 read_state_string_type (type_p *type)
1325 {
1326   *type = &string_type;
1327   read_state_common_type_content (*type);
1328 }
1329
1330
1331 /* Read a lang_bitmap representing a set of GCC front-end languages.  */
1332 static void
1333 read_state_lang_bitmap (lang_bitmap *bitmap)
1334 {
1335   struct state_token_st *t;
1336
1337   t = peek_state_token (0);
1338   if (state_token_kind (t) == STOK_INTEGER)
1339     {
1340       *bitmap = t->stok_un.stok_num;
1341       next_state_tokens (1);
1342     }
1343   else
1344     {
1345       fatal_reading_state (t, "Bad syntax for bitmap");
1346     }
1347 }
1348
1349
1350 /* Read a GTY-ed struct type.  */
1351 static void
1352 read_state_struct_type (type_p type)
1353 {
1354   struct state_token_st *t0;
1355
1356   type->kind = TYPE_STRUCT;
1357   read_state_common_type_content (type);
1358   t0 = peek_state_token (0);
1359   if (state_token_kind (t0) == STOK_STRING)
1360     {
1361       if (state_token_is_name (t0, "nil"))
1362         {
1363           type->u.s.tag = NULL;
1364           DBGPRINTF ("read anonymous struct type @%p #%d",
1365                      (void *) type, type->state_number);
1366         }
1367       else
1368         {
1369           type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1370           DBGPRINTF ("read struct type @%p #%d '%s'",
1371                      (void *) type, type->state_number, type->u.s.tag);
1372         }
1373
1374       next_state_tokens (1);
1375       read_state_fileloc (&(type->u.s.line));
1376       read_state_fields (&(type->u.s.fields));
1377       read_state_options (&(type->u.s.opt));
1378       read_state_lang_bitmap (&(type->u.s.bitmap));
1379       read_state_type (&(type->u.s.lang_struct));
1380     }
1381   else
1382     {
1383       fatal_reading_state (t0, "Bad tag in struct type");
1384     }
1385 }
1386
1387
1388 /* Read a GTY-ed user-provided struct TYPE.  */
1389
1390 static void
1391 read_state_user_struct_type (type_p type)
1392 {
1393   struct state_token_st *t0;
1394
1395   type->kind = TYPE_USER_STRUCT;
1396   read_state_common_type_content (type);
1397   t0 = peek_state_token (0);
1398   if (state_token_kind (t0) == STOK_STRING)
1399     {
1400       if (state_token_is_name (t0, "nil"))
1401         {
1402           type->u.s.tag = NULL;
1403           DBGPRINTF ("read anonymous struct type @%p #%d",
1404                      (void *) type, type->state_number);
1405         }
1406       else
1407         {
1408           type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1409           DBGPRINTF ("read struct type @%p #%d '%s'",
1410                      (void *) type, type->state_number, type->u.s.tag);
1411         }
1412
1413       next_state_tokens (1);
1414       read_state_fileloc (&(type->u.s.line));
1415       read_state_fields (&(type->u.s.fields));
1416     }
1417   else
1418     {
1419       fatal_reading_state (t0, "Bad tag in user-struct type");
1420     }
1421 }
1422
1423
1424 /* Read a GTY-ed union type.  */
1425 static void
1426 read_state_union_type (type_p type)
1427 {
1428   struct state_token_st *t0;
1429
1430   type->kind = TYPE_UNION;
1431   read_state_common_type_content (type);
1432   t0 = peek_state_token (0);
1433   if (state_token_kind (t0) == STOK_STRING)
1434     {
1435       if (state_token_is_name (t0, "nil"))
1436         {
1437           type->u.s.tag = NULL;
1438           DBGPRINTF ("read anonymous union type @%p #%d",
1439                      (void *) type, type->state_number);
1440         }
1441       else
1442         {
1443           type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1444           DBGPRINTF ("read union type @%p #%d '%s'",
1445                      (void *) type, type->state_number, type->u.s.tag);
1446         }
1447       next_state_tokens (1);
1448       read_state_fileloc (&(type->u.s.line));
1449       read_state_fields (&(type->u.s.fields));
1450       read_state_options (&(type->u.s.opt));
1451       read_state_lang_bitmap (&(type->u.s.bitmap));
1452       read_state_type (&(type->u.s.lang_struct));
1453     }
1454   else
1455     fatal_reading_state (t0, "Bad tag in union type");
1456 }
1457
1458
1459 /* Read a GTY-ed pointer type.  */
1460 static void
1461 read_state_pointer_type (type_p type)
1462 {
1463   type->kind = TYPE_POINTER;
1464   read_state_common_type_content (type);
1465   DBGPRINTF ("read pointer type @%p #%d", (void *) type, type->state_number);
1466   read_state_type (&(type->u.p));
1467 }
1468
1469
1470 /* Read a GTY-ed array type.  */
1471 static void
1472 read_state_array_type (type_p type)
1473 {
1474   struct state_token_st *t0;
1475
1476   type->kind = TYPE_ARRAY;
1477   read_state_common_type_content (type);
1478   t0 = peek_state_token (0);
1479   if (state_token_kind (t0) == STOK_STRING)
1480     {
1481       type->u.a.len = xstrdup (t0->stok_un.stok_string);
1482       DBGPRINTF ("read array type @%p #%d length '%s'",
1483                  (void *) type, type->state_number, type->u.a.len);
1484       next_state_tokens (1);
1485     }
1486
1487   else if (state_token_is_name (t0, "nil"))
1488     {
1489       type->u.a.len = NULL;
1490       DBGPRINTF ("read array type @%p #%d without length",
1491                  (void *) type, type->state_number);
1492       next_state_tokens (1);
1493     }
1494
1495   else
1496     fatal_reading_state (t0, "Bad array name type");
1497   read_state_type (&(type->u.a.p));
1498 }
1499
1500
1501
1502 /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
1503    front-end languages.  This is a tricky function and it was painful
1504    to debug.  Change it with extreme care.  See also
1505    write_state_lang_struct_type.  */
1506 static void
1507 read_state_lang_struct_type (type_p type)
1508 {
1509   struct state_token_st *t0 = NULL;
1510   struct state_token_st *t1 = NULL;
1511   struct state_token_st *t2 = NULL;
1512
1513   type->kind = TYPE_LANG_STRUCT;
1514   read_state_common_type_content (type);
1515   t0 = peek_state_token (0);
1516   if (state_token_kind (t0) == STOK_STRING)
1517     {
1518       if (state_token_is_name (t0, "nil"))
1519         {
1520           DBGPRINTF ("read anonymous lang_struct type @%p #%d",
1521                      (void *) type, type->state_number);
1522           type->u.s.tag = NULL;
1523         }
1524       else
1525         {
1526           type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1527           DBGPRINTF ("read lang_struct type @%p #%d '%s'",
1528                      (void *) type, type->state_number, type->u.s.tag);
1529         }
1530       next_state_tokens (1);
1531     }
1532   else
1533     fatal_reading_state (t0, "Bad tag in lang struct type");
1534   read_state_fileloc (&(type->u.s.line));
1535   read_state_fields (&(type->u.s.fields));
1536   read_state_options (&(type->u.s.opt));
1537   read_state_lang_bitmap (&(type->u.s.bitmap));
1538   /* Within lang_struct-ures, the lang_struct field is a linked list
1539      of homonymous types! */
1540   t0 = peek_state_token (0);
1541   t1 = peek_state_token (1);
1542   t2 = peek_state_token (2);
1543   /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
1544   if (state_token_kind (t0) == STOK_LEFTPAR
1545       && state_token_is_name (t1, "!homotypes")
1546       && state_token_kind (t2) == STOK_INTEGER)
1547     {
1548       type_p *prevty = &type->u.s.lang_struct;
1549       int nbhomotype = t2->stok_un.stok_num;
1550       int i = 0;
1551       t0 = t1 = t2 = NULL;
1552       next_state_tokens (3);
1553       for (i = 0; i < nbhomotype; i++)
1554         {
1555           read_state_type (prevty);
1556           t0 = peek_state_token (0);
1557           if (*prevty)
1558             prevty = &(*prevty)->next;
1559           else
1560               fatal_reading_state (t0,
1561                                    "expecting type in homotype list for lang_struct");
1562         };
1563       if (state_token_kind (t0) != STOK_RIGHTPAR)
1564         fatal_reading_state (t0,
1565                              "expecting ) in homotype list for lang_struct");
1566       next_state_tokens (1);
1567     }
1568   else
1569     fatal_reading_state (t0, "expecting !homotypes for lang_struct");
1570 }
1571
1572
1573 /* Read a param_struct type for GTY parametrized structures.  */
1574 static void
1575 read_state_param_struct_type (type_p type)
1576 {
1577   int i;
1578   struct state_token_st *t0;
1579
1580   type->kind = TYPE_PARAM_STRUCT;
1581   read_state_common_type_content (type);
1582   DBGPRINTF ("read param_struct type @%p #%d",
1583              (void *) type, type->state_number);
1584   read_state_type (&(type->u.param_struct.stru));
1585
1586   for (i = 0; i < NUM_PARAM; i++)
1587     {
1588       t0 = peek_state_token (0);
1589       if (state_token_is_name (t0, "nil"))
1590         {
1591           type->u.param_struct.param[i] = NULL;
1592           next_state_tokens (1);
1593         }
1594       else
1595         read_state_type (&(type->u.param_struct.param[i]));
1596     }
1597   read_state_fileloc (&(type->u.param_struct.line));
1598 }
1599
1600
1601 /* Read the gc used information.  */
1602 static void
1603 read_state_gc_used (enum gc_used_enum *pgus)
1604 {
1605   struct state_token_st *t0 = peek_state_token (0);
1606   if (state_token_is_name (t0, "gc_unused"))
1607     *pgus = GC_UNUSED;
1608   else if (state_token_is_name (t0, "gc_used"))
1609     *pgus = GC_USED;
1610   else if (state_token_is_name (t0, "gc_maybe_pointed_to"))
1611     *pgus = GC_MAYBE_POINTED_TO;
1612   else if (state_token_is_name (t0, "gc_pointed_to"))
1613     *pgus = GC_POINTED_TO;
1614   else
1615     fatal_reading_state (t0, "invalid gc_used information");
1616   next_state_tokens (1);
1617 }
1618
1619
1620 /* Utility function to read the common content of types.  */
1621 static void
1622 read_state_common_type_content (type_p current)
1623 {
1624   struct state_token_st *t0 = peek_state_token (0);
1625
1626   if (state_token_kind (t0) == STOK_INTEGER)
1627     {
1628       current->state_number = t0->stok_un.stok_num;
1629       next_state_tokens (1);
1630       record_type (current);
1631     }
1632   else
1633       fatal_reading_state_printf (t0,
1634                                   "Expected integer for state_number line %d",
1635                                   state_line);
1636   /* We don't read the next field of the type.  */
1637   read_state_type (&current->pointer_to);
1638   read_state_gc_used (&current->gc_used);
1639 }
1640
1641
1642 /* Read a GTY-ed type.  */
1643 void
1644 read_state_type (type_p *current)
1645 {
1646   struct state_token_st *t0 = peek_state_token (0);
1647   struct state_token_st *t1 = peek_state_token (1);
1648
1649   if (state_token_kind (t0) == STOK_LEFTPAR &&
1650       state_token_is_name (t1, "!type"))
1651     {
1652       next_state_tokens (2);
1653       t0 = peek_state_token (0);
1654       if (state_token_is_name (t0, "already_seen"))
1655         {
1656           next_state_tokens (1);
1657           read_state_already_seen_type (current);
1658         }
1659       else
1660         {
1661           t0 = peek_state_token (0);
1662
1663           if (state_token_is_name (t0, "scalar_nonchar"))
1664             {
1665               next_state_tokens (1);
1666               read_state_scalar_nonchar_type (current);
1667             }
1668           else if (state_token_is_name (t0, "scalar_char"))
1669             {
1670               next_state_tokens (1);
1671               read_state_scalar_char_type (current);
1672             }
1673           else if (state_token_is_name (t0, "string"))
1674             {
1675               next_state_tokens (1);
1676               read_state_string_type (current);
1677             }
1678           else if (state_token_is_name (t0, "struct"))
1679             {
1680               *current = XCNEW (struct type);
1681               next_state_tokens (1);
1682               read_state_struct_type (*current);
1683             }
1684           else if (state_token_is_name (t0, "union"))
1685             {
1686               *current = XCNEW (struct type);
1687               next_state_tokens (1);
1688               read_state_union_type (*current);
1689             }
1690           else if (state_token_is_name (t0, "lang_struct"))
1691             {
1692               *current = XCNEW (struct type);
1693               next_state_tokens (1);
1694               read_state_lang_struct_type (*current);
1695             }
1696           else if (state_token_is_name (t0, "param_struct"))
1697             {
1698               *current = XCNEW (struct type);
1699               next_state_tokens (1);
1700               read_state_param_struct_type (*current);
1701             }
1702           else if (state_token_is_name (t0, "pointer"))
1703             {
1704               *current = XCNEW (struct type);
1705               next_state_tokens (1);
1706               read_state_pointer_type (*current);
1707             }
1708           else if (state_token_is_name (t0, "array"))
1709             {
1710               *current = XCNEW (struct type);
1711               next_state_tokens (1);
1712               read_state_array_type (*current);
1713             }
1714           else if (state_token_is_name (t0, "user_struct"))
1715             {
1716               *current = XCNEW (struct type);
1717               next_state_tokens (1);
1718               read_state_user_struct_type (*current);
1719             }
1720           else
1721             fatal_reading_state (t0, "bad type in (!type");
1722         }
1723       t0 = peek_state_token (0);
1724       if (state_token_kind (t0) != STOK_RIGHTPAR)
1725         fatal_reading_state (t0, "missing ) in type");
1726       next_state_tokens (1);
1727     }
1728   else if (state_token_is_name (t0, "nil"))
1729     {
1730       next_state_tokens (1);
1731       *current = NULL;
1732     }
1733   else
1734     fatal_reading_state (t0, "bad type syntax");
1735 }
1736
1737
1738 /* Read a file location.  Files within the source directory are dealt
1739    with specifically.  */
1740 void
1741 read_state_fileloc (struct fileloc *floc)
1742 {
1743   bool issrcfile = false;
1744   struct state_token_st *t0 = peek_state_token (0);
1745   struct state_token_st *t1 = peek_state_token (1);
1746
1747   gcc_assert (floc != NULL);
1748   gcc_assert (srcdir != NULL);
1749
1750   if (state_token_kind (t0) == STOK_LEFTPAR &&
1751       (state_token_is_name (t1, "!fileloc")
1752        || (issrcfile = state_token_is_name (t1, "!srcfileloc"))))
1753     {
1754       next_state_tokens (2);
1755       t0 = peek_state_token (0);
1756       t1 = peek_state_token (1);
1757       if (state_token_kind (t0) == STOK_STRING &&
1758           state_token_kind (t1) == STOK_INTEGER)
1759         {
1760           char *path = t0->stok_un.stok_string;
1761           if (issrcfile)
1762             {
1763               static const char dirsepstr[2] = { DIR_SEPARATOR, (char) 0 };
1764               char *fullpath = concat (srcdir, dirsepstr, path, NULL);
1765               floc->file = input_file_by_name (fullpath);
1766               free (fullpath);
1767             }
1768           else
1769             floc->file = input_file_by_name (path);
1770           floc->line = t1->stok_un.stok_num;
1771           next_state_tokens (2);
1772         }
1773       else
1774         fatal_reading_state (t0,
1775                              "Bad fileloc syntax, expected path string and line");
1776       t0 = peek_state_token (0);
1777       if (state_token_kind (t0) != STOK_RIGHTPAR)
1778         fatal_reading_state (t0, "Bad fileloc syntax, expected )");
1779       next_state_tokens (1);
1780     }
1781   else if (state_token_is_name (t0, "nil"))
1782     {
1783       next_state_tokens (1);
1784       floc->file = NULL;
1785       floc->line = 0;
1786     }
1787   else
1788     fatal_reading_state (t0, "Bad fileloc syntax");
1789 }
1790
1791
1792 /* Read the fields of a GTY-ed type.  */
1793 void
1794 read_state_fields (pair_p *fields)
1795 {
1796   pair_p tmp = NULL;
1797   struct state_token_st *t0 = peek_state_token (0);
1798   struct state_token_st *t1 = peek_state_token (1);
1799   struct state_token_st *t2 = peek_state_token (2);
1800
1801   if (state_token_kind (t0) == STOK_LEFTPAR
1802       && state_token_is_name (t1, "!fields")
1803       && state_token_kind (t2) == STOK_INTEGER)
1804     {
1805       int nbfields = t2->stok_un.stok_num;
1806       int nbpairs = 0;
1807       next_state_tokens (3);
1808       nbpairs = read_state_pair_list (&tmp);
1809       t0 = peek_state_token (0);
1810       if (nbpairs != nbfields)
1811         fatal_reading_state_printf
1812           (t0,
1813            "Mismatched fields number, expected %d got %d", nbpairs, nbfields);
1814       if (state_token_kind (t0) == STOK_RIGHTPAR)
1815         next_state_tokens (1);
1816       else
1817         fatal_reading_state (t0, "Bad fields expecting )");
1818     }
1819
1820   *fields = tmp;
1821 }
1822
1823
1824 /* Read a string option.  */
1825 static void
1826 read_state_string_option (options_p opt)
1827 {
1828   struct state_token_st *t0 = peek_state_token (0);
1829   opt->kind = OPTION_STRING;
1830   if (state_token_kind (t0) == STOK_STRING)
1831     {
1832       opt->info.string = xstrdup (t0->stok_un.stok_string);
1833       next_state_tokens (1);
1834     }
1835   else if (state_token_is_name (t0, "nil"))
1836     {
1837       opt->info.string = NULL;
1838       next_state_tokens (1);
1839     }
1840   else
1841     fatal_reading_state (t0, "Missing name in string option");
1842 }
1843
1844
1845 /* Read a type option.  */
1846 static void
1847 read_state_type_option (options_p opt)
1848 {
1849   opt->kind = OPTION_TYPE;
1850   read_state_type (&(opt->info.type));
1851 }
1852
1853
1854 /* Read a nested option.  */
1855 static void
1856 read_state_nested_option (options_p opt)
1857 {
1858   struct state_token_st *t0;
1859
1860   opt->info.nested = XCNEW (struct nested_ptr_data);
1861   opt->kind = OPTION_NESTED;
1862   read_state_type (&(opt->info.nested->type));
1863   t0 = peek_state_token (0);
1864   if (state_token_kind (t0) == STOK_STRING)
1865     {
1866       opt->info.nested->convert_from = xstrdup (t0->stok_un.stok_string);
1867       next_state_tokens (1);
1868     }
1869   else if (state_token_is_name (t0, "nil"))
1870     {
1871       opt->info.nested->convert_from = NULL;
1872       next_state_tokens (1);
1873     }
1874   else
1875     fatal_reading_state (t0, "Bad nested convert_from option");
1876
1877   t0 = peek_state_token (0);
1878   if (state_token_kind (t0) == STOK_STRING)
1879     {
1880       opt->info.nested->convert_to = xstrdup (t0->stok_un.stok_string);
1881       next_state_tokens (1);
1882     }
1883   else if (state_token_is_name (t0, "nil"))
1884     {
1885       opt->info.nested->convert_to = NULL;
1886       next_state_tokens (1);
1887     }
1888   else
1889     fatal_reading_state (t0, "Bad nested convert_from option");
1890 }
1891
1892
1893 /* Read an GTY option.  */
1894 static void
1895 read_state_option (options_p *opt)
1896 {
1897   struct state_token_st *t0 = peek_state_token (0);
1898   struct state_token_st *t1 = peek_state_token (1);
1899
1900   if (state_token_kind (t0) == STOK_LEFTPAR &&
1901       state_token_is_name (t1, "!option"))
1902     {
1903       next_state_tokens (2);
1904       t0 = peek_state_token (0);
1905       if (state_token_kind (t0) == STOK_NAME)
1906         {
1907           *opt = XCNEW (struct options);
1908           if (state_token_is_name (t0, "nil"))
1909             (*opt)->name = NULL;
1910           else
1911             (*opt)->name = t0->stok_un.stok_ident->stid_name;
1912           next_state_tokens (1);
1913           t0 = peek_state_token (0);
1914           if (state_token_kind (t0) == STOK_NAME)
1915             {
1916               if (state_token_is_name (t0, "string"))
1917                 {
1918                   next_state_tokens (1);
1919                   read_state_string_option (*opt);
1920                 }
1921               else if (state_token_is_name (t0, "type"))
1922                 {
1923                   next_state_tokens (1);
1924                   read_state_type_option (*opt);
1925                 }
1926               else if (state_token_is_name (t0, "nested"))
1927                 {
1928                   next_state_tokens (1);
1929                   read_state_nested_option (*opt);
1930                 }
1931               else
1932                 fatal_reading_state (t0, "Bad option type");
1933               t0 = peek_state_token (0);
1934               if (state_token_kind (t0) != STOK_RIGHTPAR)
1935                 fatal_reading_state (t0, "Bad syntax in option, expecting )");
1936
1937               next_state_tokens (1);
1938             }
1939           else
1940             fatal_reading_state (t0, "Missing option type");
1941         }
1942       else
1943         fatal_reading_state (t0, "Bad name for option");
1944     }
1945   else
1946     fatal_reading_state (t0, "Bad option, waiting for )");
1947 }
1948
1949 /* Read a list of options.  */
1950 void
1951 read_state_options (options_p *opt)
1952 {
1953   options_p head = NULL;
1954   options_p previous = NULL;
1955   options_p current_option = NULL;
1956   struct state_token_st *t0 = peek_state_token (0);
1957   struct state_token_st *t1 = peek_state_token (1);
1958
1959   if (state_token_kind (t0) == STOK_LEFTPAR &&
1960       state_token_is_name (t1, "!options"))
1961     {
1962       next_state_tokens (2);
1963       t0 = peek_state_token (0);
1964       while (state_token_kind (t0) != STOK_RIGHTPAR)
1965         {
1966           read_state_option (&current_option);
1967           if (head == NULL)
1968             {
1969               head = current_option;
1970               previous = head;
1971             }
1972           else
1973             {
1974               previous->next = current_option;
1975               previous = current_option;
1976             }
1977           t0 = peek_state_token (0);
1978         }
1979       next_state_tokens (1);
1980     }
1981   else if (state_token_is_name (t0, "nil"))
1982     {
1983       next_state_tokens (1);
1984     }
1985   else
1986     fatal_reading_state (t0, "Bad options syntax");
1987
1988   *opt = head;
1989 }
1990
1991
1992 /* Read a version, and check against the version of the gengtype.  */
1993 static void
1994 read_state_version (const char *version_string)
1995 {
1996   struct state_token_st *t0 = peek_state_token (0);
1997   struct state_token_st *t1 = peek_state_token (1);
1998
1999   if (state_token_kind (t0) == STOK_LEFTPAR &&
2000       state_token_is_name (t1, "!version"))
2001     {
2002       next_state_tokens (2);
2003       t0 = peek_state_token (0);
2004       t1 = peek_state_token (1);
2005       if (state_token_kind (t0) == STOK_STRING &&
2006           state_token_kind (t1) == STOK_RIGHTPAR)
2007         {
2008           /* Check that the read version string is the same as current
2009              version.  */
2010           if (strcmp (version_string, t0->stok_un.stok_string))
2011             fatal_reading_state_printf (t0,
2012                                         "version string mismatch; expecting %s but got %s",
2013                                         version_string,
2014                                         t0->stok_un.stok_string);
2015           next_state_tokens (2);
2016         }
2017       else
2018         fatal_reading_state (t0, "Missing version or right parenthesis");
2019     }
2020   else
2021     fatal_reading_state (t0, "Bad version syntax");
2022 }
2023
2024
2025 /* Read a pair.  */
2026 void
2027 read_state_pair (pair_p *current)
2028 {
2029   struct state_token_st *t0 = peek_state_token (0);
2030   struct state_token_st *t1 = peek_state_token (1);
2031   if (state_token_kind (t0) == STOK_LEFTPAR &&
2032       state_token_is_name (t1, "!pair"))
2033     {
2034       *current = XCNEW (struct pair);
2035       next_state_tokens (2);
2036       t0 = peek_state_token (0);
2037       if (state_token_kind (t0) == STOK_STRING)
2038         {
2039           if (strcmp (t0->stok_un.stok_string, "nil") == 0)
2040             {
2041               (*current)->name = NULL;
2042             }
2043           else
2044             {
2045               (*current)->name = xstrdup (t0->stok_un.stok_string);
2046             }
2047           next_state_tokens (1);
2048           read_state_type (&((*current)->type));
2049           read_state_fileloc (&((*current)->line));
2050           read_state_options (&((*current)->opt));;
2051           t0 = peek_state_token (0);
2052           if (state_token_kind (t0) == STOK_RIGHTPAR)
2053             {
2054               next_state_tokens (1);
2055             }
2056           else
2057             {
2058               fatal_reading_state (t0, "Bad syntax for pair, )");
2059             }
2060         }
2061       else
2062         {
2063           fatal_reading_state (t0, "Bad name for pair");
2064         }
2065     }
2066   else if (state_token_kind (t0) == STOK_NAME &&
2067            state_token_is_name (t0, "nil"))
2068     {
2069       next_state_tokens (1);
2070       *current = NULL;
2071     }
2072   else
2073     fatal_reading_state_printf (t0, "Bad syntax for pair, (!pair %d",
2074                                 state_token->stok_kind);
2075 }
2076
2077
2078 /* Return the number of pairs actually read.  */
2079 int
2080 read_state_pair_list (pair_p *list)
2081 {
2082   int nbpair = 0;
2083   pair_p head = NULL;
2084   pair_p previous = NULL;
2085   pair_p tmp = NULL;
2086   struct state_token_st *t0 = peek_state_token (0);
2087   while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2088     {
2089       read_state_pair (&tmp);
2090       if (head == NULL)
2091         {
2092           head = tmp;
2093           previous = head;
2094         }
2095       else
2096         {
2097           previous->next = tmp;
2098           previous = tmp;
2099         }
2100       t0 = peek_state_token (0);
2101       nbpair++;
2102     }
2103
2104   /* don't consume the ); the caller will eat it.  */
2105   *list = head;
2106   return nbpair;
2107 }
2108
2109 /* Read the typedefs.  */
2110 static void
2111 read_state_typedefs (pair_p *typedefs)
2112 {
2113   int nbtypedefs = 0;
2114   pair_p list = NULL;
2115   struct state_token_st *t0 = peek_state_token (0);
2116   struct state_token_st *t1 = peek_state_token (1);
2117   struct state_token_st *t2 = peek_state_token (2);
2118
2119   if (state_token_kind (t0) == STOK_LEFTPAR
2120       && state_token_is_name (t1, "!typedefs")
2121       && state_token_kind (t2) == STOK_INTEGER)
2122     {
2123       int nbpairs = 0;
2124       nbtypedefs = t2->stok_un.stok_num;
2125       next_state_tokens (3);
2126       nbpairs = read_state_pair_list (&list);
2127       t0 = peek_state_token (0);
2128       if (nbpairs != nbtypedefs)
2129         fatal_reading_state_printf
2130           (t0,
2131            "invalid number of typedefs, expected %d but got %d",
2132            nbtypedefs, nbpairs);
2133       if (state_token_kind (t0) == STOK_RIGHTPAR)
2134         next_state_tokens (1);
2135       else
2136         fatal_reading_state (t0, "Bad typedefs syntax )");
2137     }
2138   else
2139     fatal_reading_state (t0, "Bad typedefs syntax (!typedefs");
2140
2141   if (verbosity_level >= 2)
2142     printf ("%s read %d typedefs from state\n", progname, nbtypedefs);
2143   *typedefs = list;
2144 }
2145
2146
2147 /* Read the structures.  */
2148 static void
2149 read_state_structures (type_p *structures)
2150 {
2151   type_p head = NULL;
2152   type_p previous = NULL;
2153   type_p tmp;
2154   int nbstruct = 0, countstruct = 0;
2155   struct state_token_st *t0 = peek_state_token (0);
2156   struct state_token_st *t1 = peek_state_token (1);
2157   struct state_token_st *t2 = peek_state_token (2);
2158
2159   if (state_token_kind (t0) == STOK_LEFTPAR
2160       && state_token_is_name (t1, "!structures")
2161       && state_token_kind (t2) == STOK_INTEGER)
2162     {
2163       nbstruct = t2->stok_un.stok_num;
2164       next_state_tokens (3);
2165       t0 = peek_state_token (0);
2166       while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2167         {
2168           tmp = NULL;
2169           read_state_type (&tmp);
2170           countstruct++;
2171           if (head == NULL)
2172             {
2173               head = tmp;
2174               previous = head;
2175             }
2176           else
2177             {
2178               previous->next = tmp;
2179               previous = tmp;
2180             }
2181           t0 = peek_state_token (0);
2182         }
2183       next_state_tokens (1);
2184     }
2185   else
2186     fatal_reading_state (t0, "Bad structures syntax");
2187   if (countstruct != nbstruct)
2188     fatal_reading_state_printf (NULL_STATE_TOKEN, 
2189                                 "expected %d structures but got %d",
2190                                 nbstruct, countstruct);
2191   if (verbosity_level >= 2)
2192     printf ("%s read %d structures from state\n", progname, nbstruct);
2193   *structures = head;
2194 }
2195
2196
2197 /* Read the param_struct-s.  */
2198 static void
2199 read_state_param_structs (type_p *param_structs)
2200 {
2201   int nbparamstructs = 0;
2202   int countparamstructs = 0;
2203   type_p head = NULL;
2204   type_p previous = NULL;
2205   type_p tmp;
2206   struct state_token_st *t0 = peek_state_token (0);
2207   struct state_token_st *t1 = peek_state_token (1);
2208   struct state_token_st *t2 = peek_state_token (2);
2209
2210   if (state_token_kind (t0) == STOK_LEFTPAR
2211       && state_token_is_name (t1, "!param_structs")
2212       && state_token_kind (t2) == STOK_INTEGER)
2213     {
2214       nbparamstructs = t2->stok_un.stok_num;
2215       next_state_tokens (3);
2216       t0 = t1 = t2 = NULL;
2217       t0 = peek_state_token (0);
2218       while (state_token_kind (t0) != STOK_RIGHTPAR)
2219         {
2220           tmp = NULL;
2221           read_state_type (&tmp);
2222           if (head == NULL)
2223             {
2224               head = tmp;
2225               previous = head;
2226             }
2227           else
2228             {
2229               previous->next = tmp;
2230               previous = tmp;
2231             }
2232           t0 = peek_state_token (0);
2233           countparamstructs++;
2234         }
2235       next_state_tokens (1);
2236     }
2237   else
2238     fatal_reading_state (t0, "Bad param_structs syntax");
2239   t0 = peek_state_token (0);
2240   if (countparamstructs != nbparamstructs)
2241     fatal_reading_state_printf
2242       (t0,
2243        "invalid number of param_structs expected %d got %d",
2244        nbparamstructs, countparamstructs);
2245   *param_structs = head;
2246 }
2247
2248
2249 /* Read the variables.  */
2250 static void
2251 read_state_variables (pair_p *variables)
2252 {
2253   pair_p list = NULL;
2254   int nbvars = 0;
2255   struct state_token_st *t0 = peek_state_token (0);
2256   struct state_token_st *t1 = peek_state_token (1);
2257   struct state_token_st *t2 = peek_state_token (2);
2258
2259   if (state_token_kind (t0) == STOK_LEFTPAR
2260       && state_token_is_name (t1, "!variables")
2261       && state_token_kind (t2) == STOK_INTEGER)
2262     {
2263       int nbpairs = 0;
2264       nbvars = t2->stok_un.stok_num;
2265       next_state_tokens (3);
2266       nbpairs = read_state_pair_list (&list);
2267       t0 = peek_state_token (0);
2268       if (nbpairs != nbvars)
2269         fatal_reading_state_printf
2270           (t0, "Invalid number of variables, expected %d but got %d",
2271            nbvars, nbpairs);
2272       if (state_token_kind (t0) == STOK_RIGHTPAR)
2273         next_state_tokens (1);
2274       else
2275         fatal_reading_state (t0, "Waiting for ) in variables");
2276     }
2277   else
2278     fatal_reading_state (t0, "Bad variables syntax");
2279   *variables = list;
2280   if (verbosity_level >= 2)
2281     printf ("%s read %d variables from state\n", progname, nbvars);
2282 }
2283
2284
2285 /* Read the source directory.  */
2286 static void
2287 read_state_srcdir (void)
2288 {
2289   struct state_token_st *t0 = peek_state_token (0);
2290   struct state_token_st *t1 = peek_state_token (1);
2291   if (state_token_kind (t0) == STOK_LEFTPAR &&
2292       state_token_is_name (t1, "!srcdir"))
2293     {
2294       next_state_tokens (2);
2295       t0 = peek_state_token (0);
2296       t1 = peek_state_token (1);
2297       if (state_token_kind (t0) == STOK_STRING &&
2298           state_token_kind (t1) == STOK_RIGHTPAR)
2299         {
2300           srcdir = xstrdup (t0->stok_un.stok_string);
2301           srcdir_len = strlen (srcdir);
2302           next_state_tokens (2);
2303           return;
2304         }
2305     }
2306
2307   fatal_reading_state (t0, "Bad srcdir in state_file");
2308 }
2309
2310
2311 /* Read the sequence of GCC front-end languages.  */
2312 static void
2313 read_state_languages (void)
2314 {
2315   struct state_token_st *t0 = peek_state_token (0);
2316   struct state_token_st *t1 = peek_state_token (1);
2317   struct state_token_st *t2 = peek_state_token (2);
2318   if (state_token_kind (t0) == STOK_LEFTPAR
2319       && state_token_is_name (t1, "!languages")
2320       && state_token_kind (t2) == STOK_INTEGER)
2321     {
2322       int i = 0;
2323       num_lang_dirs = t2->stok_un.stok_num;
2324       lang_dir_names = XCNEWVEC (const char *, num_lang_dirs);
2325       next_state_tokens (3);
2326       t0 = t1 = t2 = NULL;
2327       for (i = 0; i < (int) num_lang_dirs; i++)
2328         {
2329           t0 = peek_state_token (0);
2330           if (state_token_kind (t0) != STOK_NAME)
2331             fatal_reading_state (t0, "expecting language name in state file");
2332           lang_dir_names[i] = t0->stok_un.stok_ident->stid_name;
2333           next_state_tokens (1);
2334         }
2335       t0 = peek_state_token (0);
2336       if (state_token_kind (t0) != STOK_RIGHTPAR)
2337         fatal_reading_state (t0, "missing ) in languages list of state file");
2338       next_state_tokens (1);
2339     }
2340   else
2341     fatal_reading_state (t0, "expecting languages list in state file");
2342
2343 }
2344
2345 /* Read the sequence of files.  */
2346 static void
2347 read_state_files_list (void)
2348 {
2349   struct state_token_st *t0 = peek_state_token (0);
2350   struct state_token_st *t1 = peek_state_token (1);
2351   struct state_token_st *t2 = peek_state_token (2);
2352
2353   if (state_token_kind (t0) == STOK_LEFTPAR
2354       && state_token_is_name (t1, "!fileslist")
2355       && state_token_kind (t2) == STOK_INTEGER)
2356     {
2357       int i = 0;
2358       num_gt_files = t2->stok_un.stok_num;
2359       next_state_tokens (3);
2360       t0 = t1 = t2 = NULL;
2361       gt_files = XCNEWVEC (const input_file *, num_gt_files);
2362       for (i = 0; i < (int) num_gt_files; i++)
2363         {
2364           bool issrcfile = FALSE;
2365           t0 = t1 = t2 = NULL;
2366           t0 = peek_state_token (0);
2367           t1 = peek_state_token (1);
2368           t2 = peek_state_token (2);
2369           if (state_token_kind (t0) == STOK_LEFTPAR
2370               && (state_token_is_name (t1, "!file")
2371                   || (issrcfile = state_token_is_name (t1, "!srcfile")))
2372               && state_token_kind (t2) == STOK_INTEGER)
2373             {
2374               lang_bitmap bmap = t2->stok_un.stok_num;
2375               next_state_tokens (3);
2376               t0 = t1 = t2 = NULL;
2377               t0 = peek_state_token (0);
2378               t1 = peek_state_token (1);
2379               if (state_token_kind (t0) == STOK_STRING
2380                   && state_token_kind (t1) == STOK_RIGHTPAR)
2381                 {
2382                   const char *fnam = t0->stok_un.stok_string;
2383                   /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
2384                   input_file *curgt = NULL;
2385                   if (issrcfile)
2386                     {
2387                       static const char dirsepstr[2] =
2388                         { DIR_SEPARATOR, (char) 0 };
2389                       char *fullpath = concat (srcdir, dirsepstr, fnam, NULL);
2390                       curgt = input_file_by_name (fullpath);
2391                       free (fullpath);
2392                     }
2393                   else
2394                     curgt = input_file_by_name (fnam);
2395                   set_lang_bitmap (curgt, bmap);
2396                   gt_files[i] = curgt;
2397                   next_state_tokens (2);
2398                 }
2399               else
2400                 fatal_reading_state (t0,
2401                                      "bad file in !fileslist of state file");
2402             }
2403           else
2404             fatal_reading_state (t0,
2405                                  "expecting file in !fileslist of state file");
2406         };
2407       t0 = peek_state_token (0);
2408       if (!state_token_kind (t0) == STOK_RIGHTPAR)
2409         fatal_reading_state (t0, "missing ) for !fileslist in state file");
2410       next_state_tokens (1);
2411     }
2412   else
2413     fatal_reading_state (t0, "missing !fileslist in state file");
2414 }
2415
2416
2417 /* Read the trailer.  */
2418 static void
2419 read_state_trailer (void)
2420 {
2421   struct state_token_st *t0 = peek_state_token (0);
2422   struct state_token_st *t1 = peek_state_token (1);
2423   struct state_token_st *t2 = peek_state_token (2);
2424
2425   if (state_token_kind (t0) == STOK_LEFTPAR
2426       && state_token_is_name (t1, "!endfile")
2427       && state_token_kind (t2) == STOK_RIGHTPAR)
2428     next_state_tokens (3);
2429   else
2430     fatal_reading_state (t0, "missing !endfile in state file");
2431 }
2432
2433
2434 /* Utility functions for the state_seen_types hash table.  */
2435 static unsigned
2436 hash_type_number (const void *ty)
2437 {
2438   const struct type *type = (const struct type *) ty;
2439
2440   return type->state_number;
2441 }
2442
2443 static int
2444 equals_type_number (const void *ty1, const void *ty2)
2445 {
2446   const struct type *type1 = (const struct type *) ty1;
2447   const struct type *type2 = (const struct type *) ty2;
2448
2449   return type1->state_number == type2->state_number;
2450 }
2451
2452 static int
2453 string_eq (const void *a, const void *b)
2454 {
2455   const char *a0 = (const char *)a;
2456   const char *b0 = (const char *)b;
2457
2458   return (strcmp (a0, b0) == 0);
2459 }
2460
2461
2462 /* The function reading the state, called by main from gengtype.c.  */
2463 void
2464 read_state (const char *path)
2465 {
2466   state_file = fopen (path, "r");
2467   if (state_file == NULL)
2468     fatal ("Failed to open state file %s for reading [%s]", path,
2469            xstrerror (errno));
2470   state_path = path;
2471   state_line = 1;
2472
2473   if (verbosity_level >= 1)
2474     {
2475       printf ("%s reading state file %s;", progname, state_path);
2476       if (verbosity_level >= 2)
2477         putchar ('\n');
2478       fflush (stdout);
2479     }
2480
2481   state_seen_types =
2482     htab_create (2017, hash_type_number, equals_type_number, NULL);
2483   state_ident_tab =
2484     htab_create (4027, htab_hash_string, string_eq, NULL);
2485   read_state_version (version_string);
2486   read_state_srcdir ();
2487   read_state_languages ();
2488   read_state_files_list ();
2489   read_state_structures (&structures);
2490   if (ferror (state_file))
2491     fatal_reading_state_printf
2492       (NULL_STATE_TOKEN, "input error while reading state [%s]",
2493        xstrerror (errno));
2494   read_state_typedefs (&typedefs);
2495   read_state_param_structs (&param_structs);
2496   read_state_variables (&variables);
2497   read_state_trailer ();
2498
2499   if (verbosity_level >= 1)
2500     {
2501       printf ("%s read %ld bytes.\n", progname, ftell (state_file));
2502       fflush (stdout);
2503     };
2504
2505   if (fclose (state_file))
2506     fatal ("failed to close read state file %s [%s]",
2507            path, xstrerror (errno));
2508   state_file = NULL;
2509   state_path = NULL;
2510 }
2511
2512 /* End of file gengtype-state.c.  */