OSDN Git Service

* config/mmix/mmix.c (TARGET_PREFERRED_OUTPUT_RELOAD_CLASS): Redefine
[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       return CONST_CAST (struct fileloc*, &ty->u.s.line);
55     case TYPE_PARAM_STRUCT:
56       return CONST_CAST (struct fileloc*, &ty->u.param_struct.line);
57     case TYPE_SCALAR:
58     case TYPE_STRING:
59     case TYPE_POINTER:
60     case TYPE_ARRAY:
61       return NULL;
62     default:
63       gcc_unreachable ();
64     }
65 }
66
67 /* The state file has simplistic lispy lexical tokens.  Its lexer gives
68    a linked list of struct state_token_st, thru the peek_state_token
69    function.  Lexical tokens are consumed with next_state_tokens.  */
70
71
72 /* The lexical kind of each lispy token.  */
73 enum state_token_en
74 {
75   STOK_NONE,                    /* Never used.  */
76   STOK_INTEGER,                 /* Integer token.  */
77   STOK_STRING,                  /* String token.  */
78   STOK_LEFTPAR,                 /* Left opening parenthesis.  */
79   STOK_RIGHTPAR,                /* Right closing parenthesis.  */
80   STOK_NAME                     /* hash-consed name or identifier.  */
81 };
82
83
84 /* Structure and hash-table used to share identifiers or names.  */
85 struct state_ident_st
86 {
87   /* TODO: We could improve the parser by reserving identifiers for
88      state keywords and adding a keyword number for them.  That would
89      mean adding another field in this state_ident_st struct.  */
90   char stid_name[1];            /* actually bigger & null terminated */
91 };
92 static htab_t state_ident_tab;
93
94
95 /* The state_token_st structure is for lexical tokens in the read
96    state file.  The stok_kind field discriminates the union.  Tokens
97    are allocated by peek_state_token which calls read_a_state_token
98    which allocate them.  Tokens are freed by calls to
99    next_state_tokens.  Token are organized in a FIFO look-ahead queue
100    filled by peek_state_token.  */
101 struct state_token_st
102 {
103   enum state_token_en stok_kind;        /* the lexical kind
104                                            discriminates the stok_un
105                                            union  */
106   int stok_line;                        /* the line number */
107   int stok_col;                         /* the column number */
108   const char *stok_file;                /* the file path */
109   struct state_token_st *stok_next;     /* the next token in the
110                                            queue, when peeked */
111   union                                 /* discriminated by stok_kind! */
112   {
113     int stok_num;                       /* when STOK_INTEGER */
114     char stok_string[1];                /* when STOK_STRING, actual size is
115                                            bigger and null terminated */
116     struct state_ident_st *stok_ident;  /* when STOK_IDENT */
117     void *stok_ptr;                     /* null otherwise */
118   }
119   stok_un;
120 };
121
122
123
124
125 #define NULL_STATE_TOKEN (struct state_token_st*)0
126
127 /* the state_token pointer contains the leftmost current token.  The
128    tokens are organized in a linked queue, using stok_next, for token
129    look-ahead.  */
130 struct state_token_st *state_token = NULL_STATE_TOKEN;
131
132 /* Used by the reading lexer.  */
133 static FILE *state_file;
134 static const char *state_path = NULL;
135 static int state_line = 0;
136 static long state_bol = 0;      /* offset of beginning of line */
137
138
139 /* Counter of written types.  */
140 static int state_written_type_count = 0;
141
142
143 /* Fatal error messages when reading the state.  They are extremely
144    unlikely, and only appear when this gengtype-state.c file is buggy,
145    or when reading a gengtype state which was not generated by the
146    same version of gengtype or GCC.  */
147
148
149 /* Fatal message while reading state.  */
150 static inline void 
151 fatal_reading_state (struct state_token_st* tok, const char*msg)
152 {
153   if (tok)
154     fatal ("%s:%d:%d: Invalid state file; %s",
155            tok->stok_file, tok->stok_line, tok->stok_col, 
156            msg); 
157   else
158     fatal ("%s:%d: Invalid state file; %s", 
159            state_path, state_line, msg);
160 }
161
162
163 /* Fatal printf-like message while reading state.  This can't be a
164    function, because there is no way to pass a va_arg to a variant of
165    fatal.  */
166 #define fatal_reading_state_printf(Tok,Fmt,...) do {    \
167     struct state_token_st* badtok = Tok;                \
168     if (badtok)                                         \
169       fatal ("%s:%d:%d: Invalid state file; " Fmt,      \
170               badtok->stok_file,                        \
171               badtok->stok_line,                        \
172               badtok->stok_col, __VA_ARGS__);           \
173     else                                                \
174       fatal ("%s:%d: Invalid state file; " Fmt,         \
175              state_path, state_line, __VA_ARGS__);      \
176   } while(0)
177
178
179 /* Find or allocate an identifier in our name hash table.  */
180 static struct state_ident_st *
181 state_ident_by_name (const char *name, enum insert_option optins)
182 {
183   PTR *slot = NULL;
184   int namlen = 0;
185   struct state_ident_st *stid = NULL;
186
187   if (!name || !name[0])
188     return NULL;
189
190   slot = htab_find_slot (state_ident_tab, name, optins);
191   if (!slot)
192     return NULL;
193
194   namlen = strlen (name);
195   stid =
196     (struct state_ident_st *) xmalloc (sizeof (struct state_ident_st) +
197                                        namlen);
198   memset (stid, 0, sizeof (struct state_ident_st) + namlen);
199   strcpy (stid->stid_name, name);
200   *slot = stid;
201
202   return stid;
203 }
204
205 /* Our token lexer is heavily inspired by MELT's lexer, and share some
206    code with the file gcc/melt-runtime.c of the GCC MELT branch!  We
207    really want the gengtype state to be easily parsable by MELT.  This
208    is a usual lispy lexing routine, dealing with spaces and comments,
209    numbers, parenthesis, names, strings.  */
210 static struct state_token_st *
211 read_a_state_token (void)
212 {
213   int c = 0;
214   long curoff = 0;
215   struct state_token_st *tk = NULL;
216
217  again: /* Read again, e.g. after a comment or spaces.  */
218   c = getc (state_file);
219   if (c == EOF)
220     return NULL;
221
222   /* Handle spaces, count lines.  */
223   if (c == '\n')
224     {
225       state_line++;
226       state_bol = curoff = ftell (state_file);
227       goto again;
228     };
229   if (ISSPACE (c))
230     goto again;
231   /* Skip comments starting with semi-colon.  */
232   if (c == ';')
233     {   
234       do
235         {
236           c = getc (state_file);
237         }
238       while (c > 0 && c != '\n');
239       if (c == '\n')
240         {
241           state_line++;
242           state_bol = curoff = ftell (state_file);
243         }
244       goto again;
245     };
246   /* Read signed numbers.  */
247   if (ISDIGIT (c) || c == '-' || c == '+')
248     {                           /* number */
249       int n = 0;
250       ungetc (c, state_file);
251       curoff = ftell (state_file);
252       if (fscanf (state_file, "%d", &n) <= 0)
253         fatal_reading_state (NULL_STATE_TOKEN, "Lexical error in number");
254       tk = XCNEW (struct state_token_st);
255       tk->stok_kind = STOK_INTEGER;
256       tk->stok_line = state_line;
257       tk->stok_col = curoff - state_bol;
258       tk->stok_file = state_path;
259       tk->stok_next = NULL;
260       tk->stok_un.stok_num = n;
261
262       return tk;
263     }
264   /* Read an opening left parenthesis.  */
265   else if (c == '(')
266     {
267       curoff = ftell (state_file);
268       tk = XCNEW (struct state_token_st);
269       tk->stok_kind = STOK_LEFTPAR;
270       tk->stok_line = state_line;
271       tk->stok_col = curoff - state_bol;
272       tk->stok_file = state_path;
273       tk->stok_next = NULL;
274
275       return tk;
276     }
277   /* Read an closing right parenthesis.  */
278   else if (c == ')')
279     {
280       curoff = ftell (state_file);
281       tk = XCNEW (struct state_token_st);
282       tk->stok_kind = STOK_RIGHTPAR;
283       tk->stok_line = state_line;
284       tk->stok_col = curoff - state_bol;
285       tk->stok_file = state_path;
286       tk->stok_next = NULL;
287
288       return tk;
289     }
290   /* Read identifiers, using an obstack.  */
291   else if (ISALPHA (c) || c == '_' || c == '$' || c == '!' || c == '#')
292     {
293       struct obstack id_obstack;
294       struct state_ident_st *sid = NULL;
295       char *ids = NULL;
296       obstack_init (&id_obstack);
297       curoff = ftell (state_file);
298       while (ISALNUM (c) || c == '_' || c == '$' || c == '!' || c == '#')
299         {
300           obstack_1grow (&id_obstack, c);
301           c = getc (state_file);
302           if (c < 0)
303             break;
304         };
305       if (c >= 0)
306         ungetc (c, state_file);
307       obstack_1grow (&id_obstack, (char) 0);
308       ids = XOBFINISH (&id_obstack, char *);
309       sid = state_ident_by_name (ids, INSERT);
310       obstack_free (&id_obstack, NULL);
311       ids = NULL;
312       tk = XCNEW (struct state_token_st);
313       tk->stok_kind = STOK_NAME;
314       tk->stok_line = state_line;
315       tk->stok_col = curoff - state_bol;
316       tk->stok_file = state_path;
317       tk->stok_next = NULL;
318       tk->stok_un.stok_ident = sid;
319
320       return tk;
321     }
322   /* Read a string, dealing with escape sequences a la C! */
323   else if (c == '"')
324     {
325       char *cstr = NULL;
326       int cslen = 0;
327       struct obstack bstring_obstack;
328       obstack_init (&bstring_obstack);
329       curoff = ftell (state_file);
330       while ((c = getc (state_file)) != '"' && c >= 0)
331         {
332           if (ISPRINT (c) && c != '\\')
333             obstack_1grow (&bstring_obstack, (char) c);
334           else if (ISSPACE (c) && c != '\n')
335             obstack_1grow (&bstring_obstack, (char) c);
336           else if (c == '\\')
337             {
338               c = getc (state_file);
339               switch (c)
340                 {
341                 case 'a':
342                   obstack_1grow (&bstring_obstack, '\a');
343                   c = getc (state_file);
344                   break;
345                 case 'b':
346                   obstack_1grow (&bstring_obstack, '\b');
347                   c = getc (state_file);
348                   break;
349                 case 't':
350                   obstack_1grow (&bstring_obstack, '\t');
351                   c = getc (state_file);
352                   break;
353                 case 'n':
354                   obstack_1grow (&bstring_obstack, '\n');
355                   c = getc (state_file);
356                   break;
357                 case 'v':
358                   obstack_1grow (&bstring_obstack, '\v');
359                   c = getc (state_file);
360                   break;
361                 case 'f':
362                   obstack_1grow (&bstring_obstack, '\f');
363                   c = getc (state_file);
364                   break;
365                 case 'r':
366                   obstack_1grow (&bstring_obstack, '\r');
367                   c = getc (state_file);
368                   break;
369                 case '"':
370                   obstack_1grow (&bstring_obstack, '\"');
371                   c = getc (state_file);
372                   break;
373                 case '\\':
374                   obstack_1grow (&bstring_obstack, '\\');
375                   c = getc (state_file);
376                   break;
377                 case ' ':
378                   obstack_1grow (&bstring_obstack, ' ');
379                   c = getc (state_file);
380                   break;
381                 case 'x':
382                   {
383                     unsigned int cx = 0;
384                     if (fscanf (state_file, "%02x", &cx) > 0 && cx > 0)
385                       obstack_1grow (&bstring_obstack, cx);
386                     else
387                       fatal_reading_state
388                         (NULL_STATE_TOKEN,
389                          "Lexical error in string hex escape");
390                     c = getc (state_file);
391                     break;
392                   }
393                 default:
394                   fatal_reading_state
395                     (NULL_STATE_TOKEN,
396                      "Lexical error - unknown string escape");
397                 }
398             }
399           else
400             fatal_reading_state (NULL_STATE_TOKEN, "Lexical error...");
401         };
402       if (c != '"')
403         fatal_reading_state (NULL_STATE_TOKEN, "Unterminated string");
404       obstack_1grow (&bstring_obstack, '\0');
405       cstr = XOBFINISH (&bstring_obstack, char *);
406       cslen = strlen (cstr);
407       tk = (struct state_token_st *)
408         xcalloc (sizeof (struct state_token_st) + cslen, 1);
409       tk->stok_kind = STOK_STRING;
410       tk->stok_line = state_line;
411       tk->stok_col = curoff - state_bol;
412       tk->stok_file = state_path;
413       tk->stok_next = NULL;
414       strcpy (tk->stok_un.stok_string, cstr);
415       obstack_free (&bstring_obstack, NULL);
416
417       return tk;
418     }
419   /* Got an unexpected character.  */
420   fatal_reading_state_printf
421     (NULL_STATE_TOKEN,
422      "Lexical error at offset %ld - bad character \\%03o = '%c'",
423      ftell (state_file), c, c);
424 }
425
426 /* Used for lexical look-ahead.  Retrieves the lexical token of rank
427    DEPTH, starting with 0 when reading the state file.  Gives null on
428    end of file.  */
429 static struct state_token_st *
430 peek_state_token (int depth)
431 {
432   int remdepth = depth;
433   struct state_token_st **ptoken = &state_token;
434   struct state_token_st *tok = NULL;
435
436   while (remdepth >= 0)
437     {
438       if (*ptoken == NULL)
439         {
440           *ptoken = tok = read_a_state_token ();
441           if (tok == NULL)
442             return NULL;
443         }
444       tok = *ptoken;
445       ptoken = &((*ptoken)->stok_next);
446       remdepth--;
447     }
448
449   return tok;
450 }
451
452 /* Consume the next DEPTH tokens and free them.  */
453 static void
454 next_state_tokens (int depth)
455 {
456   struct state_token_st *n;
457
458   while (depth > 0)
459     {
460       if (state_token != NULL)
461         {
462           n = state_token->stok_next;
463           free (state_token);
464           state_token = n;
465         }
466       else
467         fatal_reading_state (NULL_STATE_TOKEN, "Tokens stack empty");
468
469       depth--;
470     }
471 }
472
473 /* Safely retrieve the lexical kind of a token.  */
474 static inline enum state_token_en
475 state_token_kind (struct state_token_st *p)
476 {
477   if (p == NULL)
478     return STOK_NONE;
479   else
480     return p->stok_kind;
481 }
482
483 /* Test if a token is a given name i.e. an identifier.  */
484 static inline bool
485 state_token_is_name (struct state_token_st *p, const char *name)
486 {
487   if (p == NULL)
488     return false;
489
490   if (p->stok_kind != STOK_NAME)
491     return false;
492
493   return !strcmp (p->stok_un.stok_ident->stid_name, name);
494 }
495
496
497 /* Following routines are useful for serializing datas.
498  *
499  * We want to serialize :
500  *          - typedefs list
501  *          - structures list
502  *          - param_structs list
503  *          - variables list
504  *
505  * So, we have one routine for each kind of data.  The main writing
506  * routine is write_state.  The main reading routine is
507  * read_state.  Most writing routines write_state_FOO have a
508  * corresponding reading routine read_state_FOO.  Reading is done in a
509  * recursive descending way, and any read error is fatal.
510  */
511
512 /* When reading the state, we need to remember the previously seen
513    types by their state_number, since GTY-ed types are usually
514    shared.  */
515 static htab_t state_seen_types;
516
517 /* Return the length of a linked list made of pairs.  */
518 static int pair_list_length (pair_p list);
519
520 /* Write a pair */
521 static void write_state_pair (pair_p);
522
523 /* return the number of pairs written.  Should match the length given
524    by pair_list_length.  */
525 static int write_state_pair_list (pair_p list);
526
527 /* Write a type.  When a type is written, its state_number is updated,
528    to ensure that a "reference" to a seen type is written on next
529    occurrences.  */
530 static void write_state_type (type_p);
531
532 /* Write a null-terminatel string using our Lispy lexical conventions,
533    similar to those of C or MELT.  */
534 static void write_state_a_string (const char *s);
535
536 /* Compute the length of a list of pairs, starting from the first
537    one.  */
538 static int
539 pair_list_length (pair_p list)
540 {
541   int nbpair = 0;
542   pair_p l = NULL;
543   for (l = list; l; l = l->next)
544     nbpair++;
545   return nbpair;
546 }
547
548 /* Write a file location.  Files relative to $(srcdir) are quite
549    frequent and are handled specially.  This ensures that two gengtype
550    state file-s produced by gengtype on the same GCC source tree are
551    very similar and can be reasonably compared with diff, even if the
552    two GCC source trees have different absolute paths.  */
553 static void
554 write_state_fileloc (struct fileloc *floc)
555 {
556
557   if (floc != NULL && floc->line > 0)
558     {
559       const char *srcrelpath = NULL;
560       gcc_assert (floc->file != NULL);
561       /* Most of the files are inside $(srcdir) so it is worth to
562          handle them specially.  */
563       srcrelpath = get_file_srcdir_relative_path (floc->file);
564       if (srcrelpath != NULL)
565         {
566           fprintf (state_file, "\n(!srcfileloc ");
567           write_state_a_string (srcrelpath);
568         }
569       else
570         {
571           fprintf (state_file, "\n(!fileloc ");
572           write_state_a_string (get_input_file_name (floc->file));
573         }
574       fprintf (state_file, " %d", floc->line);
575       fprintf (state_file, ")\n");
576     }
577   else
578     fprintf (state_file, "nil ");
579 }
580
581 /* Write a list of fields.  */
582 static void
583 write_state_fields (pair_p fields)
584 {
585   int nbfields = pair_list_length (fields);
586   int nbpairs = 0;
587   fprintf (state_file, "\n(!fields %d ", nbfields);
588   nbpairs = write_state_pair_list (fields);
589   gcc_assert (nbpairs == nbfields);
590   fprintf (state_file, ")\n");
591 }
592
593 /* Write a null-terminated string in our lexical convention, very
594    similar to the convention of C.  */
595 static void
596 write_state_a_string (const char *s)
597 {
598   char c;
599
600   fputs (" \"", state_file);
601   for (; *s != 0; s++)
602     {
603       c = *s;
604       switch (c)
605         {
606         case '\a':
607           fputs ("\\a", state_file);
608           break;
609         case '\b':
610           fputs ("\\b", state_file);
611           break;
612         case '\t':
613           fputs ("\\t", state_file);
614           break;
615         case '\n':
616           fputs ("\\n", state_file);
617           break;
618         case '\v':
619           fputs ("\\v", state_file);
620           break;
621         case '\f':
622           fputs ("\\f", state_file);
623           break;
624         case '\r':
625           fputs ("\\r", state_file);
626           break;
627         case '\"':
628           fputs ("\\\"", state_file);
629           break;
630         case '\\':
631           fputs ("\\\\", state_file);
632           break;
633         default:
634           if (ISPRINT (c))
635             putc (c, state_file);
636           else
637             fprintf (state_file, "\\x%02x", (unsigned) c);
638         }
639     }
640   fputs ("\"", state_file);
641 }
642
643 /* Our option-s have three kinds, each with its writer.  */
644 static void
645 write_state_string_option (options_p current)
646 {
647   fprintf (state_file, "string ");
648   if (current->info.string != NULL)
649     write_state_a_string (current->info.string);
650   else
651     fprintf (state_file, " nil ");
652 }
653
654 static void
655 write_state_type_option (options_p current)
656 {
657   fprintf (state_file, "type ");
658   write_state_type (current->info.type);
659 }
660
661 static void
662 write_state_nested_option (options_p current)
663 {
664   fprintf (state_file, "nested ");
665   write_state_type (current->info.nested->type);
666   if (current->info.nested->convert_from != NULL)
667     write_state_a_string (current->info.nested->convert_from);
668   else
669     fprintf (state_file, " nil ");
670
671   if (current->info.nested->convert_to != NULL)
672     write_state_a_string (current->info.nested->convert_to);
673   else
674     fprintf (state_file, " nil ");
675 }
676
677 static void
678 write_state_option (options_p current)
679 {
680   fprintf (state_file, "\n(!option ");
681
682   if (current->name != NULL)
683     fprintf (state_file, "%s ", current->name);
684   else
685     fprintf (state_file, "nil ");
686
687   switch (current->kind)
688     {
689     case OPTION_STRING:
690       write_state_string_option (current);
691       break;
692     case OPTION_TYPE:
693       write_state_type_option (current);
694       break;
695     case OPTION_NESTED:
696       write_state_nested_option (current);
697       break;
698     default:
699       fatal ("Option tag unknown");
700     }
701
702   fprintf (state_file, ")\n");
703 }
704
705
706
707 /* Write a list of GTY options.  */
708 static void
709 write_state_options (options_p opt)
710 {
711   options_p current;
712
713   if (opt == NULL)
714     {
715       fprintf (state_file, "nil ");
716       return;
717     }
718
719   fprintf (state_file, "\n(!options ");
720   for (current = opt; current != NULL; current = current->next)
721       write_state_option (current);
722   fprintf (state_file, ")\n");
723 }
724
725
726 /* Write a bitmap representing a set of GCC front-end languages.  */
727 static void
728 write_state_lang_bitmap (lang_bitmap bitmap)
729 {
730   fprintf (state_file, "%d ", (int) bitmap);
731 }
732
733 /* Write version information.  */
734 static void
735 write_state_version (const char *version)
736 {
737   fprintf (state_file, "\n(!version ");
738   write_state_a_string (version);
739   fprintf (state_file, ")\n");
740 }
741
742 /* Common routine to write the common content of all types.  */
743 static void write_state_common_type_content (type_p current);
744
745 /* Write a scalar type.  We have only two of these.  */
746 static void
747 write_state_scalar_type (type_p current)
748 {
749   if (current == &scalar_nonchar)
750     fprintf (state_file, "scalar_nonchar ");
751   else if (current == &scalar_char)
752     fprintf (state_file, "scalar_char ");
753   else
754     fatal ("Unexpected type in write_state_scalar_type");
755
756   write_state_common_type_content (current);
757 }
758
759 /* Write the string type.  There is only one such thing! */
760 static void
761 write_state_string_type (type_p current)
762 {
763   if (current == &string_type)
764     {
765       fprintf (state_file, "string ");
766       write_state_common_type_content (current);
767     }
768   else
769     fatal ("Unexpected type in write_state_string_type");
770 }
771
772
773 /* Common code to write structure like types.  */
774 static void
775 write_state_struct_union_type (type_p current, const char *kindstr)
776 {
777   DBGPRINTF ("%s type @ %p #%d '%s'", kindstr, (void *) current,
778              current->state_number, current->u.s.tag);
779   fprintf (state_file, "%s ", kindstr);
780   write_state_common_type_content (current);
781   if (current->u.s.tag != NULL)
782     write_state_a_string (current->u.s.tag);
783   else
784     fprintf (state_file, "nil");
785
786   write_state_fileloc (type_lineloc (current));
787   write_state_fields (current->u.s.fields);
788   write_state_options (current->u.s.opt);
789   write_state_lang_bitmap (current->u.s.bitmap);
790 }
791
792
793 /* Write a GTY struct type.  */
794 static void
795 write_state_struct_type (type_p current)
796 {
797   write_state_struct_union_type (current, "struct");
798   write_state_type (current->u.s.lang_struct);
799 }
800
801 /* write a GTY union type.  */
802 static void
803 write_state_union_type (type_p current)
804 {
805   write_state_struct_union_type (current, "union");
806   write_state_type (current->u.s.lang_struct);
807 }
808
809 /* Write a lang_struct type.  This is tricky and was painful to debug,
810    we deal with the next field specifically within their lang_struct
811    subfield, which points to a linked list of homonumous types.
812    Change this function with extreme care, see also
813    read_state_lang_struct_type.  */
814 static void
815 write_state_lang_struct_type (type_p current)
816 {
817   int nbhomontype = 0;
818   type_p hty = NULL;
819   const char *homoname = 0;
820   write_state_struct_union_type (current, "lang_struct");
821   /* lang_struct-ures are particularily tricky, since their
822      u.s.lang_struct field gives a list of homonymous struct-s or
823      union-s! */
824   DBGPRINTF ("lang_struct @ %p #%d", (void *) current, current->state_number);
825   for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
826     {
827       nbhomontype++;
828       DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype,
829                  (void *) hty, hty->state_number, hty->u.s.tag);
830       /* Every member of the homonymous list should have the same tag.  */
831       gcc_assert (UNION_OR_STRUCT_P (hty));
832       gcc_assert (hty->u.s.lang_struct == current);
833       if (!homoname)
834         homoname = hty->u.s.tag;
835       gcc_assert (strcmp (homoname, hty->u.s.tag) == 0);
836     }
837   fprintf (state_file, "(!homotypes %d\n", nbhomontype);
838   for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
839     write_state_type (hty);
840   fprintf (state_file, ")\n");
841 }
842
843 /* Write a parametrized structure GTY type.  */
844 static void
845 write_state_param_struct_type (type_p current)
846 {
847   int i;
848
849   fprintf (state_file, "param_struct ");
850   write_state_common_type_content (current);
851   write_state_type (current->u.param_struct.stru);
852   for (i = 0; i < NUM_PARAM; i++)
853     {
854       if (current->u.param_struct.param[i] != NULL)
855         write_state_type (current->u.param_struct.param[i]);
856       else
857         fprintf (state_file, "nil ");
858     }
859   write_state_fileloc (&current->u.param_struct.line);
860 }
861
862 /* Write a pointer type.  */
863 static void
864 write_state_pointer_type (type_p current)
865 {
866   fprintf (state_file, "pointer ");
867   write_state_common_type_content (current);
868   write_state_type (current->u.p);
869 }
870
871 /* Write an array type.  */
872 static void
873 write_state_array_type (type_p current)
874 {
875   fprintf (state_file, "array ");
876   write_state_common_type_content (current);
877   if (current->u.a.len != NULL)
878     write_state_a_string (current->u.a.len);
879   else
880     fprintf (state_file, " nil");
881
882   fprintf (state_file, " ");
883   write_state_type (current->u.a.p);
884 }
885
886 /* Write the gc_used information.  */
887 static void
888 write_state_gc_used (enum gc_used_enum gus)
889 {
890   switch (gus)
891     {
892     case GC_UNUSED:
893       fprintf (state_file, " gc_unused");
894       break;
895     case GC_USED:
896       fprintf (state_file, " gc_used");
897       break;
898     case GC_MAYBE_POINTED_TO:
899       fprintf (state_file, " gc_maybe_pointed_to");
900       break;
901     case GC_POINTED_TO:
902       fprintf (state_file, " gc_pointed_to");
903       break;
904     default:
905       gcc_unreachable ();
906     }
907 }
908
909 /* Utility routine to write the common content of all types.  Notice
910    that the next field is *not* written on purpose.  */
911 static void
912 write_state_common_type_content (type_p current)
913 {
914   fprintf (state_file, "%d ", current->state_number);
915   /* We do not write the next type, because list of types are
916      explicitly written.  However, lang_struct are special in that
917      respect.  See function write_state_lang_struct_type for more.  */
918   write_state_type (current->pointer_to);
919   write_state_gc_used (current->gc_used);
920 }
921
922
923 /* The important and recursive routine writing GTY types as understood
924    by gengtype.  Types which have a positive state_number have already
925    been seen and written.  */
926 static void
927 write_state_type (type_p current)
928 {
929   if (current == NULL)
930     {
931       fprintf (state_file, "nil ");
932       return;
933     }
934
935   fprintf (state_file, "\n(!type ");
936
937   if (current->state_number > 0)
938     fprintf (state_file, "already_seen %d", current->state_number);
939   else
940     {
941       state_written_type_count++;
942       DBGPRINTF ("writing type #%d @%p old number %d", state_written_type_count,
943                  (void *) current, current->state_number);
944       current->state_number = state_written_type_count;
945       switch (current->kind)
946         {
947         case TYPE_STRUCT:
948           write_state_struct_type (current);
949           break;
950         case TYPE_UNION:
951           write_state_union_type (current);
952           break;
953         case TYPE_POINTER:
954           write_state_pointer_type (current);
955           break;
956         case TYPE_ARRAY:
957           write_state_array_type (current);
958           break;
959         case TYPE_LANG_STRUCT:
960           write_state_lang_struct_type (current);
961           break;
962         case TYPE_PARAM_STRUCT:
963           write_state_param_struct_type (current);
964           break;
965         case TYPE_SCALAR:
966           write_state_scalar_type (current);
967           break;
968         case TYPE_STRING:
969           write_state_string_type (current);
970           break;
971
972         default:
973           fatal ("Unexpected type...");
974         }
975     }
976
977   fprintf (state_file, ")\n");
978 }
979
980
981 /* Write a pair.  */
982 static void
983 write_state_pair (pair_p current)
984 {
985   if (current == NULL)
986     {
987       fprintf (state_file, "nil)");
988       return;
989     }
990
991   fprintf (state_file, "\n(!pair ");
992
993   if (current->name != NULL)
994     write_state_a_string (current->name);
995   else
996     write_state_a_string ("nil");
997
998   write_state_type (current->type);
999   write_state_fileloc (&(current->line));
1000   write_state_options (current->opt);
1001
1002   fprintf (state_file, ")");
1003 }
1004
1005 /* Write a pair list and return the number of pairs written.  */
1006 static int
1007 write_state_pair_list (pair_p list)
1008 {
1009   int nbpair = 0;
1010   pair_p current;
1011
1012   for (current = list; current != NULL; current = current->next)
1013     {
1014       write_state_pair (current);
1015       nbpair++;
1016     }
1017   return nbpair;
1018
1019 }
1020
1021 /* When writing imported linked lists, like typedefs, structures,
1022    param_structs, ... we count their length first and write it.  These
1023    eases the reading, and enables an extra verification on the number
1024    of actually read items.  */
1025
1026 /* Write our typedefs.  */
1027 static void
1028 write_state_typedefs (void)
1029 {
1030   int nbtypedefs = pair_list_length (typedefs);
1031   int nbpairs = 0;
1032   fprintf (state_file, "\n(!typedefs %d\n", nbtypedefs);
1033   nbpairs = write_state_pair_list (typedefs);
1034   gcc_assert (nbpairs == nbtypedefs);
1035   fprintf (state_file, ")\n");
1036   if (verbosity_level >= 2)
1037     printf ("%s wrote %d typedefs\n", progname, nbtypedefs);
1038 }
1039
1040 /* Write our structures.  */
1041 static void
1042 write_state_structures (void)
1043 {
1044   int nbstruct = 0;
1045   type_p current;
1046
1047   for (current = structures; current != NULL; current = current->next)
1048     nbstruct++;
1049
1050   fprintf (state_file, "\n(!structures %d\n", nbstruct);
1051
1052   for (current = structures; current != NULL; current = current->next)
1053     write_state_type (current);
1054
1055   fprintf (state_file, ")\n");
1056   if (verbosity_level >= 2)
1057     printf ("%s wrote %d structures in state\n", progname, nbstruct);
1058 }
1059
1060 /* Write our param_struct-s.  */
1061 static void
1062 write_state_param_structs (void)
1063 {
1064   int nbparamstruct = 0;
1065   type_p current;
1066
1067   for (current = param_structs; current != NULL; current = current->next)
1068     nbparamstruct++;
1069
1070   fprintf (state_file, "\n(!param_structs %d\n", nbparamstruct);
1071
1072   for (current = param_structs; current != NULL; current = current->next)
1073     write_state_type (current);
1074
1075   fprintf (state_file, ")\n");
1076 }
1077
1078 /* Write our variables.  */
1079 static void
1080 write_state_variables (void)
1081 {
1082   int nbvars = pair_list_length (variables);
1083   int nbpairs = 0;
1084   fprintf (state_file, "\n(!variables %d\n", nbvars);
1085   nbpairs = write_state_pair_list (variables);
1086   gcc_assert (nbpairs == nbvars);
1087   fprintf (state_file, ")\n");
1088   if (verbosity_level >= 2)
1089     printf ("%s wrote %d variables.\n", progname, nbvars);
1090 }
1091
1092 /* Write the source directory.  File locations within the source
1093    directory have been written specifically.  */
1094 static void
1095 write_state_srcdir (void)
1096 {
1097   fprintf (state_file, "\n(!srcdir ");
1098   write_state_a_string (srcdir);
1099   fprintf (state_file, ")\n");
1100 }
1101
1102 /* Count and write the list of our files.  */
1103 static void
1104 write_state_files_list (void)
1105 {
1106   int i = 0;
1107   /* Write the list of files with their lang_bitmap.  */
1108   fprintf (state_file, "\n(!fileslist %d\n", (int) num_gt_files);
1109   for (i = 0; i < (int) num_gt_files; i++)
1110     {
1111       const char *cursrcrelpath = NULL;
1112       const input_file *curfil = gt_files[i];
1113       /* Most of the files are inside $(srcdir) so it is worth to
1114          handle them specially.  */
1115       cursrcrelpath = get_file_srcdir_relative_path (curfil);
1116       if (cursrcrelpath)
1117         {
1118           fprintf (state_file, "(!srcfile %d ", get_lang_bitmap (curfil));
1119           write_state_a_string (cursrcrelpath);
1120         }
1121       else
1122         {
1123           fprintf (state_file, "(!file %d ", get_lang_bitmap (curfil));
1124           write_state_a_string (get_input_file_name (curfil));
1125         }
1126       fprintf (state_file, ")\n");
1127     }
1128   fprintf (state_file, ")\n");
1129 }
1130
1131 /* Write the list of GCC front-end languages.  */
1132 static void
1133 write_state_languages (void)
1134 {
1135   int i = 0;
1136   fprintf (state_file, "\n(!languages %d", (int) num_lang_dirs);
1137   for (i = 0; i < (int) num_lang_dirs; i++)
1138     {
1139       /* Languages names are identifiers, we expect only letters or
1140          underscores or digits in them.  In particular, C++ is not a
1141          valid language name, but cp is valid.  */
1142       fprintf (state_file, " %s", lang_dir_names[i]);
1143     }
1144   fprintf (state_file, ")\n");
1145 }
1146
1147 /* Write the trailer.  */
1148 static void
1149 write_state_trailer (void)
1150 {
1151   /* This test should probably catch IO errors like disk full...  */
1152   if (fputs ("\n(!endfile)\n", state_file) == EOF)
1153     fatal ("failed to write state trailer [%s]", xstrerror (errno));
1154 }
1155
1156 /* The write_state routine is the only writing routine called by main
1157    in gengtype.c.  To avoid messing the state if gengtype is
1158    interrupted or aborted, we write a temporary file and rename it
1159    after having written it in totality.  */
1160 void
1161 write_state (const char *state_path)
1162 {
1163   long statelen = 0;
1164   time_t now = 0;
1165   char *temp_state_path = NULL;
1166   char tempsuffix[40];
1167   time (&now);
1168
1169   /* We write a unique temporary file which is renamed when complete
1170    * only.  So even if gengtype is interrupted, the written state file
1171    * won't be partially written, since the temporary file is not yet
1172    * renamed in that case.  */
1173   memset (tempsuffix, 0, sizeof (tempsuffix));
1174   snprintf (tempsuffix, sizeof (tempsuffix) - 1, "-%ld-%d.tmp", (long) now,
1175             (int) getpid ());
1176   temp_state_path = concat (state_path, tempsuffix, NULL);
1177   state_file = fopen (temp_state_path, "w");
1178   if (state_file == NULL)
1179     fatal ("Failed to open file %s for writing state: %s",
1180            temp_state_path, xstrerror (errno));
1181   if (verbosity_level >= 3)
1182     printf ("%s writing state file %s temporarily in %s\n",
1183             progname, state_path, temp_state_path);
1184   /* This is the first line of the state.  Perhaps the file utility
1185      could know about that, so don't change it often.  */
1186   fprintf (state_file, ";;;;@@@@ GCC gengtype state\n");
1187   /* Output a few comments for humans. */
1188   fprintf (state_file,
1189            ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
1190   fprintf (state_file,
1191            ";;; The format of this file is tied to a particular version of GCC.\n");
1192   fprintf (state_file,
1193            ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1194   fprintf (state_file,
1195            ";;; This file should be parsed by the same %s which wrote it.\n",
1196            progname);
1197   fprintf (state_file, ";;; file %s generated on %s\n", state_path,
1198            ctime (&now));
1199   /* The first non-comment significant line gives the version string.  */
1200   write_state_version (version_string);
1201   write_state_srcdir ();
1202   write_state_languages ();
1203   write_state_files_list ();
1204   write_state_structures ();
1205   write_state_typedefs ();
1206   write_state_param_structs ();
1207   write_state_variables ();
1208   write_state_trailer ();
1209   statelen = ftell (state_file);
1210   if (ferror (state_file))
1211     fatal ("output error when writing state file %s [%s]",
1212            temp_state_path, xstrerror (errno));
1213   if (fclose (state_file))
1214     fatal ("failed to close state file %s [%s]",
1215            temp_state_path, xstrerror (errno));
1216   if (rename (temp_state_path, state_path))
1217     fatal ("failed to rename %s to state file %s [%s]", temp_state_path,
1218            state_path, xstrerror (errno));
1219   free (temp_state_path);
1220
1221   if (verbosity_level >= 1)
1222     printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1223             progname, state_path, statelen, state_written_type_count);
1224
1225 }
1226 \f
1227 /** End of writing routines!  The corresponding reading routines follow.  **/
1228
1229
1230
1231 /* Forward declarations, since some read_state_* functions are
1232    recursive! */
1233 static void read_state_fileloc (struct fileloc *line);
1234 static void read_state_options (options_p *opt);
1235 static void read_state_type (type_p *current);
1236 static void read_state_pair (pair_p *pair);
1237 /* Return the number of pairs actually read.  */
1238 static int read_state_pair_list (pair_p *list);
1239 static void read_state_fields (pair_p *fields);
1240 static void read_state_common_type_content (type_p current);
1241
1242
1243
1244
1245 /* Record into the state_seen_types hash-table a type which we are
1246    reading, to enable recursive or circular references to it.  */
1247 static void
1248 record_type (type_p type)
1249 {
1250   PTR *slot;
1251
1252   slot = htab_find_slot (state_seen_types, type, INSERT);
1253   gcc_assert (slot);
1254
1255   *slot = type;
1256 }
1257
1258 /* Read an already seen type.  */
1259 static void
1260 read_state_already_seen_type (type_p *type)
1261 {
1262   struct state_token_st *t0 = peek_state_token (0);
1263
1264   if (state_token_kind (t0) == STOK_INTEGER)
1265     {
1266       PTR *slot = NULL;
1267       struct type loctype = { TYPE_SCALAR, 0, 0, 0, GC_UNUSED, {0} };
1268
1269       loctype.state_number = t0->stok_un.stok_num;
1270       slot = htab_find_slot (state_seen_types, &loctype, NO_INSERT);
1271       if (slot == NULL)
1272         {
1273           fatal_reading_state (t0, "Unknown type");
1274         }
1275
1276       next_state_tokens (1);
1277       *type = (type_p) *slot;
1278     }
1279   else
1280     {
1281       fatal_reading_state (t0, "Bad seen type");
1282     }
1283 }
1284
1285
1286 /* Read the scalar_nonchar type.  */
1287 static void
1288 read_state_scalar_nonchar_type (type_p *type)
1289 {
1290   *type = &scalar_nonchar;
1291   read_state_common_type_content (*type);
1292 }
1293
1294
1295 /* Read the scalar_char type.  */
1296 static void
1297 read_state_scalar_char_type (type_p *type)
1298 {
1299   *type = &scalar_char;
1300   read_state_common_type_content (*type);
1301 }
1302
1303
1304 /* Read the string_type.  */
1305 static void
1306 read_state_string_type (type_p *type)
1307 {
1308   *type = &string_type;
1309   read_state_common_type_content (*type);
1310 }
1311
1312
1313 /* Read a lang_bitmap representing a set of GCC front-end languages.  */
1314 static void
1315 read_state_lang_bitmap (lang_bitmap *bitmap)
1316 {
1317   struct state_token_st *t;
1318
1319   t = peek_state_token (0);
1320   if (state_token_kind (t) == STOK_INTEGER)
1321     {
1322       *bitmap = t->stok_un.stok_num;
1323       next_state_tokens (1);
1324     }
1325   else
1326     {
1327       fatal_reading_state (t, "Bad syntax for bitmap");
1328     }
1329 }
1330
1331
1332 /* Read a GTY-ed struct type.  */
1333 static void
1334 read_state_struct_type (type_p type)
1335 {
1336   struct state_token_st *t0;
1337
1338   type->kind = TYPE_STRUCT;
1339   read_state_common_type_content (type);
1340   t0 = peek_state_token (0);
1341   if (state_token_kind (t0) == STOK_STRING)
1342     {
1343       if (state_token_is_name (t0, "nil"))
1344         {
1345           type->u.s.tag = NULL;
1346           DBGPRINTF ("read anonymous struct type @%p #%d",
1347                      (void *) type, type->state_number);
1348         }
1349       else
1350         {
1351           type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1352           DBGPRINTF ("read struct type @%p #%d '%s'",
1353                      (void *) type, type->state_number, type->u.s.tag);
1354         }
1355
1356       next_state_tokens (1);
1357       read_state_fileloc (&(type->u.s.line));
1358       read_state_fields (&(type->u.s.fields));
1359       read_state_options (&(type->u.s.opt));
1360       read_state_lang_bitmap (&(type->u.s.bitmap));
1361       read_state_type (&(type->u.s.lang_struct));
1362     }
1363   else
1364     {
1365       fatal_reading_state (t0, "Bad tag in struct type");
1366     }
1367 }
1368
1369
1370 /* Read a GTY-ed union type.  */
1371 static void
1372 read_state_union_type (type_p type)
1373 {
1374   struct state_token_st *t0;
1375
1376   type->kind = TYPE_UNION;
1377   read_state_common_type_content (type);
1378   t0 = peek_state_token (0);
1379   if (state_token_kind (t0) == STOK_STRING)
1380     {
1381       if (state_token_is_name (t0, "nil"))
1382         {
1383           type->u.s.tag = NULL;
1384           DBGPRINTF ("read anonymous union type @%p #%d",
1385                      (void *) type, type->state_number);
1386         }
1387       else
1388         {
1389           type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1390           DBGPRINTF ("read union type @%p #%d '%s'",
1391                      (void *) type, type->state_number, type->u.s.tag);
1392         }
1393       next_state_tokens (1);
1394       read_state_fileloc (&(type->u.s.line));
1395       read_state_fields (&(type->u.s.fields));
1396       read_state_options (&(type->u.s.opt));
1397       read_state_lang_bitmap (&(type->u.s.bitmap));
1398       read_state_type (&(type->u.s.lang_struct));
1399     }
1400   else
1401     fatal_reading_state (t0, "Bad tag in union type");
1402 }
1403
1404
1405 /* Read a GTY-ed pointer type.  */
1406 static void
1407 read_state_pointer_type (type_p type)
1408 {
1409   type->kind = TYPE_POINTER;
1410   read_state_common_type_content (type);
1411   DBGPRINTF ("read pointer type @%p #%d", (void *) type, type->state_number);
1412   read_state_type (&(type->u.p));
1413 }
1414
1415
1416 /* Read a GTY-ed array type.  */
1417 static void
1418 read_state_array_type (type_p type)
1419 {
1420   struct state_token_st *t0;
1421
1422   type->kind = TYPE_ARRAY;
1423   read_state_common_type_content (type);
1424   t0 = peek_state_token (0);
1425   if (state_token_kind (t0) == STOK_STRING)
1426     {
1427       type->u.a.len = xstrdup (t0->stok_un.stok_string);
1428       DBGPRINTF ("read array type @%p #%d length '%s'",
1429                  (void *) type, type->state_number, type->u.a.len);
1430       next_state_tokens (1);
1431     }
1432
1433   else if (state_token_is_name (t0, "nil"))
1434     {
1435       type->u.a.len = NULL;
1436       DBGPRINTF ("read array type @%p #%d without length",
1437                  (void *) type, type->state_number);
1438       next_state_tokens (1);
1439     }
1440
1441   else
1442     fatal_reading_state (t0, "Bad array name type");
1443   read_state_type (&(type->u.a.p));
1444 }
1445
1446
1447
1448 /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
1449    front-end languages.  This is a tricky function and it was painful
1450    to debug.  Change it with extreme care.  See also
1451    write_state_lang_struct_type.  */
1452 static void
1453 read_state_lang_struct_type (type_p type)
1454 {
1455   struct state_token_st *t0 = NULL;
1456   struct state_token_st *t1 = NULL;
1457   struct state_token_st *t2 = NULL;
1458
1459   type->kind = TYPE_LANG_STRUCT;
1460   read_state_common_type_content (type);
1461   t0 = peek_state_token (0);
1462   if (state_token_kind (t0) == STOK_STRING)
1463     {
1464       if (state_token_is_name (t0, "nil"))
1465         {
1466           DBGPRINTF ("read anonymous lang_struct type @%p #%d",
1467                      (void *) type, type->state_number);
1468           type->u.s.tag = NULL;
1469         }
1470       else
1471         {
1472           type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1473           DBGPRINTF ("read lang_struct type @%p #%d '%s'",
1474                      (void *) type, type->state_number, type->u.s.tag);
1475         }
1476       next_state_tokens (1);
1477     }
1478   else
1479     fatal_reading_state (t0, "Bad tag in lang struct type");
1480   read_state_fileloc (&(type->u.s.line));
1481   read_state_fields (&(type->u.s.fields));
1482   read_state_options (&(type->u.s.opt));
1483   read_state_lang_bitmap (&(type->u.s.bitmap));
1484   /* Within lang_struct-ures, the lang_struct field is a linked list
1485      of homonymous types! */
1486   t0 = peek_state_token (0);
1487   t1 = peek_state_token (1);
1488   t2 = peek_state_token (2);
1489   /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
1490   if (state_token_kind (t0) == STOK_LEFTPAR
1491       && state_token_is_name (t1, "!homotypes")
1492       && state_token_kind (t2) == STOK_INTEGER)
1493     {
1494       type_p *prevty = &type->u.s.lang_struct;
1495       int nbhomotype = t2->stok_un.stok_num;
1496       int i = 0;
1497       t0 = t1 = t2 = NULL;
1498       next_state_tokens (3);
1499       for (i = 0; i < nbhomotype; i++)
1500         {
1501           read_state_type (prevty);
1502           t0 = peek_state_token (0);
1503           if (*prevty)
1504             prevty = &(*prevty)->next;
1505           else
1506               fatal_reading_state (t0,
1507                                    "expecting type in homotype list for lang_struct");
1508         };
1509       if (state_token_kind (t0) != STOK_RIGHTPAR)
1510         fatal_reading_state (t0,
1511                              "expecting ) in homotype list for lang_struct");
1512       next_state_tokens (1);
1513     }
1514   else
1515     fatal_reading_state (t0, "expecting !homotypes for lang_struct");
1516 }
1517
1518
1519 /* Read a param_struct type for GTY parametrized structures.  */
1520 static void
1521 read_state_param_struct_type (type_p type)
1522 {
1523   int i;
1524   struct state_token_st *t0;
1525
1526   type->kind = TYPE_PARAM_STRUCT;
1527   read_state_common_type_content (type);
1528   DBGPRINTF ("read param_struct type @%p #%d",
1529              (void *) type, type->state_number);
1530   read_state_type (&(type->u.param_struct.stru));
1531
1532   for (i = 0; i < NUM_PARAM; i++)
1533     {
1534       t0 = peek_state_token (0);
1535       if (state_token_is_name (t0, "nil"))
1536         {
1537           type->u.param_struct.param[i] = NULL;
1538           next_state_tokens (1);
1539         }
1540       else
1541         read_state_type (&(type->u.param_struct.param[i]));
1542     }
1543   read_state_fileloc (&(type->u.param_struct.line));
1544 }
1545
1546
1547 /* Read the gc used information.  */
1548 static void
1549 read_state_gc_used (enum gc_used_enum *pgus)
1550 {
1551   struct state_token_st *t0 = peek_state_token (0);
1552   if (state_token_is_name (t0, "gc_unused"))
1553     *pgus = GC_UNUSED;
1554   else if (state_token_is_name (t0, "gc_used"))
1555     *pgus = GC_USED;
1556   else if (state_token_is_name (t0, "gc_maybe_pointed_to"))
1557     *pgus = GC_MAYBE_POINTED_TO;
1558   else if (state_token_is_name (t0, "gc_pointed_to"))
1559     *pgus = GC_POINTED_TO;
1560   else
1561     fatal_reading_state (t0, "invalid gc_used information");
1562   next_state_tokens (1);
1563 }
1564
1565
1566 /* Utility function to read the common content of types.  */
1567 static void
1568 read_state_common_type_content (type_p current)
1569 {
1570   struct state_token_st *t0 = peek_state_token (0);
1571
1572   if (state_token_kind (t0) == STOK_INTEGER)
1573     {
1574       current->state_number = t0->stok_un.stok_num;
1575       next_state_tokens (1);
1576       record_type (current);
1577     }
1578   else
1579       fatal_reading_state_printf (t0,
1580                                   "Expected integer for state_number line %d",
1581                                   state_line);
1582   /* We don't read the next field of the type.  */
1583   read_state_type (&current->pointer_to);
1584   read_state_gc_used (&current->gc_used);
1585 }
1586
1587
1588 /* Read a GTY-ed type.  */
1589 void
1590 read_state_type (type_p *current)
1591 {
1592   struct state_token_st *t0 = peek_state_token (0);
1593   struct state_token_st *t1 = peek_state_token (1);
1594
1595   if (state_token_kind (t0) == STOK_LEFTPAR &&
1596       state_token_is_name (t1, "!type"))
1597     {
1598       next_state_tokens (2);
1599       t0 = peek_state_token (0);
1600       if (state_token_is_name (t0, "already_seen"))
1601         {
1602           next_state_tokens (1);
1603           read_state_already_seen_type (current);
1604         }
1605       else
1606         {
1607           t0 = peek_state_token (0);
1608
1609           if (state_token_is_name (t0, "scalar_nonchar"))
1610             {
1611               next_state_tokens (1);
1612               read_state_scalar_nonchar_type (current);
1613             }
1614           else if (state_token_is_name (t0, "scalar_char"))
1615             {
1616               next_state_tokens (1);
1617               read_state_scalar_char_type (current);
1618             }
1619           else if (state_token_is_name (t0, "string"))
1620             {
1621               next_state_tokens (1);
1622               read_state_string_type (current);
1623             }
1624           else if (state_token_is_name (t0, "struct"))
1625             {
1626               *current = XCNEW (struct type);
1627               next_state_tokens (1);
1628               read_state_struct_type (*current);
1629             }
1630           else if (state_token_is_name (t0, "union"))
1631             {
1632               *current = XCNEW (struct type);
1633               next_state_tokens (1);
1634               read_state_union_type (*current);
1635             }
1636           else if (state_token_is_name (t0, "lang_struct"))
1637             {
1638               *current = XCNEW (struct type);
1639               next_state_tokens (1);
1640               read_state_lang_struct_type (*current);
1641             }
1642           else if (state_token_is_name (t0, "param_struct"))
1643             {
1644               *current = XCNEW (struct type);
1645               next_state_tokens (1);
1646               read_state_param_struct_type (*current);
1647             }
1648           else if (state_token_is_name (t0, "pointer"))
1649             {
1650               *current = XCNEW (struct type);
1651               next_state_tokens (1);
1652               read_state_pointer_type (*current);
1653             }
1654           else if (state_token_is_name (t0, "array"))
1655             {
1656               *current = XCNEW (struct type);
1657               next_state_tokens (1);
1658               read_state_array_type (*current);
1659             }
1660           else
1661             fatal_reading_state (t0, "bad type in (!type");
1662         }
1663       t0 = peek_state_token (0);
1664       if (state_token_kind (t0) != STOK_RIGHTPAR)
1665         fatal_reading_state (t0, "missing ) in type");
1666       next_state_tokens (1);
1667     }
1668   else if (state_token_is_name (t0, "nil"))
1669     {
1670       next_state_tokens (1);
1671       *current = NULL;
1672     }
1673   else
1674     fatal_reading_state (t0, "bad type syntax");
1675 }
1676
1677
1678 /* Read a file location.  Files within the source directory are dealt
1679    with specifically.  */
1680 void
1681 read_state_fileloc (struct fileloc *floc)
1682 {
1683   bool issrcfile = false;
1684   struct state_token_st *t0 = peek_state_token (0);
1685   struct state_token_st *t1 = peek_state_token (1);
1686
1687   gcc_assert (floc != NULL);
1688   gcc_assert (srcdir != NULL);
1689
1690   if (state_token_kind (t0) == STOK_LEFTPAR &&
1691       (state_token_is_name (t1, "!fileloc")
1692        || (issrcfile = state_token_is_name (t1, "!srcfileloc"))))
1693     {
1694       next_state_tokens (2);
1695       t0 = peek_state_token (0);
1696       t1 = peek_state_token (1);
1697       if (state_token_kind (t0) == STOK_STRING &&
1698           state_token_kind (t1) == STOK_INTEGER)
1699         {
1700           char *path = t0->stok_un.stok_string;
1701           if (issrcfile)
1702             {
1703               static const char dirsepstr[2] = { DIR_SEPARATOR, (char) 0 };
1704               char *fullpath = concat (srcdir, dirsepstr, path, NULL);
1705               floc->file = input_file_by_name (fullpath);
1706               free (fullpath);
1707             }
1708           else
1709             floc->file = input_file_by_name (path);
1710           floc->line = t1->stok_un.stok_num;
1711           next_state_tokens (2);
1712         }
1713       else
1714         fatal_reading_state (t0,
1715                              "Bad fileloc syntax, expected path string and line");
1716       t0 = peek_state_token (0);
1717       if (state_token_kind (t0) != STOK_RIGHTPAR)
1718         fatal_reading_state (t0, "Bad fileloc syntax, expected )");
1719       next_state_tokens (1);
1720     }
1721   else if (state_token_is_name (t0, "nil"))
1722     {
1723       next_state_tokens (1);
1724       floc->file = NULL;
1725       floc->line = 0;
1726     }
1727   else
1728     fatal_reading_state (t0, "Bad fileloc syntax");
1729 }
1730
1731
1732 /* Read the fields of a GTY-ed type.  */
1733 void
1734 read_state_fields (pair_p *fields)
1735 {
1736   pair_p tmp = NULL;
1737   struct state_token_st *t0 = peek_state_token (0);
1738   struct state_token_st *t1 = peek_state_token (1);
1739   struct state_token_st *t2 = peek_state_token (2);
1740
1741   if (state_token_kind (t0) == STOK_LEFTPAR
1742       && state_token_is_name (t1, "!fields")
1743       && state_token_kind (t2) == STOK_INTEGER)
1744     {
1745       int nbfields = t2->stok_un.stok_num;
1746       int nbpairs = 0;
1747       next_state_tokens (3);
1748       nbpairs = read_state_pair_list (&tmp);
1749       t0 = peek_state_token (0);
1750       if (nbpairs != nbfields)
1751         fatal_reading_state_printf
1752           (t0,
1753            "Mismatched fields number, expected %d got %d", nbpairs, nbfields);
1754       if (state_token_kind (t0) == STOK_RIGHTPAR)
1755         next_state_tokens (1);
1756       else
1757         fatal_reading_state (t0, "Bad fields expecting )");
1758     }
1759
1760   *fields = tmp;
1761 }
1762
1763
1764 /* Read a string option.  */
1765 static void
1766 read_state_string_option (options_p opt)
1767 {
1768   struct state_token_st *t0 = peek_state_token (0);
1769   opt->kind = OPTION_STRING;
1770   if (state_token_kind (t0) == STOK_STRING)
1771     {
1772       opt->info.string = xstrdup (t0->stok_un.stok_string);
1773       next_state_tokens (1);
1774     }
1775   else if (state_token_is_name (t0, "nil"))
1776     {
1777       opt->info.string = NULL;
1778       next_state_tokens (1);
1779     }
1780   else
1781     fatal_reading_state (t0, "Missing name in string option");
1782 }
1783
1784
1785 /* Read a type option.  */
1786 static void
1787 read_state_type_option (options_p opt)
1788 {
1789   opt->kind = OPTION_TYPE;
1790   read_state_type (&(opt->info.type));
1791 }
1792
1793
1794 /* Read a nested option.  */
1795 static void
1796 read_state_nested_option (options_p opt)
1797 {
1798   struct state_token_st *t0;
1799
1800   opt->info.nested = XCNEW (struct nested_ptr_data);
1801   opt->kind = OPTION_NESTED;
1802   read_state_type (&(opt->info.nested->type));
1803   t0 = peek_state_token (0);
1804   if (state_token_kind (t0) == STOK_STRING)
1805     {
1806       opt->info.nested->convert_from = xstrdup (t0->stok_un.stok_string);
1807       next_state_tokens (1);
1808     }
1809   else if (state_token_is_name (t0, "nil"))
1810     {
1811       opt->info.nested->convert_from = NULL;
1812       next_state_tokens (1);
1813     }
1814   else
1815     fatal_reading_state (t0, "Bad nested convert_from option");
1816
1817   t0 = peek_state_token (0);
1818   if (state_token_kind (t0) == STOK_STRING)
1819     {
1820       opt->info.nested->convert_to = xstrdup (t0->stok_un.stok_string);
1821       next_state_tokens (1);
1822     }
1823   else if (state_token_is_name (t0, "nil"))
1824     {
1825       opt->info.nested->convert_to = NULL;
1826       next_state_tokens (1);
1827     }
1828   else
1829     fatal_reading_state (t0, "Bad nested convert_from option");
1830 }
1831
1832
1833 /* Read an GTY option.  */
1834 static void
1835 read_state_option (options_p *opt)
1836 {
1837   struct state_token_st *t0 = peek_state_token (0);
1838   struct state_token_st *t1 = peek_state_token (1);
1839
1840   if (state_token_kind (t0) == STOK_LEFTPAR &&
1841       state_token_is_name (t1, "!option"))
1842     {
1843       next_state_tokens (2);
1844       t0 = peek_state_token (0);
1845       if (state_token_kind (t0) == STOK_NAME)
1846         {
1847           *opt = XCNEW (struct options);
1848           if (state_token_is_name (t0, "nil"))
1849             (*opt)->name = NULL;
1850           else
1851             (*opt)->name = t0->stok_un.stok_ident->stid_name;
1852           next_state_tokens (1);
1853           t0 = peek_state_token (0);
1854           if (state_token_kind (t0) == STOK_NAME)
1855             {
1856               if (state_token_is_name (t0, "string"))
1857                 {
1858                   next_state_tokens (1);
1859                   read_state_string_option (*opt);
1860                 }
1861               else if (state_token_is_name (t0, "type"))
1862                 {
1863                   next_state_tokens (1);
1864                   read_state_type_option (*opt);
1865                 }
1866               else if (state_token_is_name (t0, "nested"))
1867                 {
1868                   next_state_tokens (1);
1869                   read_state_nested_option (*opt);
1870                 }
1871               else
1872                 fatal_reading_state (t0, "Bad option type");
1873               t0 = peek_state_token (0);
1874               if (state_token_kind (t0) != STOK_RIGHTPAR)
1875                 fatal_reading_state (t0, "Bad syntax in option, expecting )");
1876
1877               next_state_tokens (1);
1878             }
1879           else
1880             fatal_reading_state (t0, "Missing option type");
1881         }
1882       else
1883         fatal_reading_state (t0, "Bad name for option");
1884     }
1885   else
1886     fatal_reading_state (t0, "Bad option, waiting for )");
1887 }
1888
1889 /* Read a list of options.  */
1890 void
1891 read_state_options (options_p *opt)
1892 {
1893   options_p head = NULL;
1894   options_p previous = NULL;
1895   options_p current_option = NULL;
1896   struct state_token_st *t0 = peek_state_token (0);
1897   struct state_token_st *t1 = peek_state_token (1);
1898
1899   if (state_token_kind (t0) == STOK_LEFTPAR &&
1900       state_token_is_name (t1, "!options"))
1901     {
1902       next_state_tokens (2);
1903       t0 = peek_state_token (0);
1904       while (state_token_kind (t0) != STOK_RIGHTPAR)
1905         {
1906           read_state_option (&current_option);
1907           if (head == NULL)
1908             {
1909               head = current_option;
1910               previous = head;
1911             }
1912           else
1913             {
1914               previous->next = current_option;
1915               previous = current_option;
1916             }
1917           t0 = peek_state_token (0);
1918         }
1919       next_state_tokens (1);
1920     }
1921   else if (state_token_is_name (t0, "nil"))
1922     {
1923       next_state_tokens (1);
1924     }
1925   else
1926     fatal_reading_state (t0, "Bad options syntax");
1927
1928   *opt = head;
1929 }
1930
1931
1932 /* Read a version, and check against the version of the gengtype.  */
1933 static void
1934 read_state_version (const char *version_string)
1935 {
1936   struct state_token_st *t0 = peek_state_token (0);
1937   struct state_token_st *t1 = peek_state_token (1);
1938
1939   if (state_token_kind (t0) == STOK_LEFTPAR &&
1940       state_token_is_name (t1, "!version"))
1941     {
1942       next_state_tokens (2);
1943       t0 = peek_state_token (0);
1944       t1 = peek_state_token (1);
1945       if (state_token_kind (t0) == STOK_STRING &&
1946           state_token_kind (t1) == STOK_RIGHTPAR)
1947         {
1948           /* Check that the read version string is the same as current
1949              version.  */
1950           if (strcmp (version_string, t0->stok_un.stok_string))
1951             fatal_reading_state_printf (t0,
1952                                         "version string mismatch; expecting %s but got %s",
1953                                         version_string,
1954                                         t0->stok_un.stok_string);
1955           next_state_tokens (2);
1956         }
1957       else
1958         fatal_reading_state (t0, "Missing version or right parenthesis");
1959     }
1960   else
1961     fatal_reading_state (t0, "Bad version syntax");
1962 }
1963
1964
1965 /* Read a pair.  */
1966 void
1967 read_state_pair (pair_p *current)
1968 {
1969   struct state_token_st *t0 = peek_state_token (0);
1970   struct state_token_st *t1 = peek_state_token (1);
1971   if (state_token_kind (t0) == STOK_LEFTPAR &&
1972       state_token_is_name (t1, "!pair"))
1973     {
1974       *current = XCNEW (struct pair);
1975       next_state_tokens (2);
1976       t0 = peek_state_token (0);
1977       if (state_token_kind (t0) == STOK_STRING)
1978         {
1979           if (strcmp (t0->stok_un.stok_string, "nil") == 0)
1980             {
1981               (*current)->name = NULL;
1982             }
1983           else
1984             {
1985               (*current)->name = xstrdup (t0->stok_un.stok_string);
1986             }
1987           next_state_tokens (1);
1988           read_state_type (&((*current)->type));
1989           read_state_fileloc (&((*current)->line));
1990           read_state_options (&((*current)->opt));;
1991           t0 = peek_state_token (0);
1992           if (state_token_kind (t0) == STOK_RIGHTPAR)
1993             {
1994               next_state_tokens (1);
1995             }
1996           else
1997             {
1998               fatal_reading_state (t0, "Bad syntax for pair, )");
1999             }
2000         }
2001       else
2002         {
2003           fatal_reading_state (t0, "Bad name for pair");
2004         }
2005     }
2006   else if (state_token_kind (t0) == STOK_NAME &&
2007            state_token_is_name (t0, "nil"))
2008     {
2009       next_state_tokens (1);
2010       *current = NULL;
2011     }
2012   else
2013     fatal_reading_state_printf (t0, "Bad syntax for pair, (!pair %d",
2014                                 state_token->stok_kind);
2015 }
2016
2017
2018 /* Return the number of pairs actually read.  */
2019 int
2020 read_state_pair_list (pair_p *list)
2021 {
2022   int nbpair = 0;
2023   pair_p head = NULL;
2024   pair_p previous = NULL;
2025   pair_p tmp = NULL;
2026   struct state_token_st *t0 = peek_state_token (0);
2027   while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2028     {
2029       read_state_pair (&tmp);
2030       if (head == NULL)
2031         {
2032           head = tmp;
2033           previous = head;
2034         }
2035       else
2036         {
2037           previous->next = tmp;
2038           previous = tmp;
2039         }
2040       t0 = peek_state_token (0);
2041       nbpair++;
2042     }
2043
2044   /* don't consume the ); the caller will eat it.  */
2045   *list = head;
2046   return nbpair;
2047 }
2048
2049 /* Read the typedefs.  */
2050 static void
2051 read_state_typedefs (pair_p *typedefs)
2052 {
2053   int nbtypedefs = 0;
2054   pair_p list = NULL;
2055   struct state_token_st *t0 = peek_state_token (0);
2056   struct state_token_st *t1 = peek_state_token (1);
2057   struct state_token_st *t2 = peek_state_token (2);
2058
2059   if (state_token_kind (t0) == STOK_LEFTPAR
2060       && state_token_is_name (t1, "!typedefs")
2061       && state_token_kind (t2) == STOK_INTEGER)
2062     {
2063       int nbpairs = 0;
2064       nbtypedefs = t2->stok_un.stok_num;
2065       next_state_tokens (3);
2066       nbpairs = read_state_pair_list (&list);
2067       t0 = peek_state_token (0);
2068       if (nbpairs != nbtypedefs)
2069         fatal_reading_state_printf
2070           (t0,
2071            "invalid number of typedefs, expected %d but got %d",
2072            nbtypedefs, nbpairs);
2073       if (state_token_kind (t0) == STOK_RIGHTPAR)
2074         next_state_tokens (1);
2075       else
2076         fatal_reading_state (t0, "Bad typedefs syntax )");
2077     }
2078   else
2079     fatal_reading_state (t0, "Bad typedefs syntax (!typedefs");
2080
2081   if (verbosity_level >= 2)
2082     printf ("%s read %d typedefs from state\n", progname, nbtypedefs);
2083   *typedefs = list;
2084 }
2085
2086
2087 /* Read the structures.  */
2088 static void
2089 read_state_structures (type_p *structures)
2090 {
2091   type_p head = NULL;
2092   type_p previous = NULL;
2093   type_p tmp;
2094   int nbstruct = 0, countstruct = 0;
2095   struct state_token_st *t0 = peek_state_token (0);
2096   struct state_token_st *t1 = peek_state_token (1);
2097   struct state_token_st *t2 = peek_state_token (2);
2098
2099   if (state_token_kind (t0) == STOK_LEFTPAR
2100       && state_token_is_name (t1, "!structures")
2101       && state_token_kind (t2) == STOK_INTEGER)
2102     {
2103       nbstruct = t2->stok_un.stok_num;
2104       next_state_tokens (3);
2105       t0 = peek_state_token (0);
2106       while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2107         {
2108           tmp = NULL;
2109           read_state_type (&tmp);
2110           countstruct++;
2111           if (head == NULL)
2112             {
2113               head = tmp;
2114               previous = head;
2115             }
2116           else
2117             {
2118               previous->next = tmp;
2119               previous = tmp;
2120             }
2121           t0 = peek_state_token (0);
2122         }
2123       next_state_tokens (1);
2124     }
2125   else
2126     fatal_reading_state (t0, "Bad structures syntax");
2127   if (countstruct != nbstruct)
2128     fatal_reading_state_printf (NULL_STATE_TOKEN, 
2129                                 "expected %d structures but got %d",
2130                                 nbstruct, countstruct);
2131   if (verbosity_level >= 2)
2132     printf ("%s read %d structures from state\n", progname, nbstruct);
2133   *structures = head;
2134 }
2135
2136
2137 /* Read the param_struct-s.  */
2138 static void
2139 read_state_param_structs (type_p *param_structs)
2140 {
2141   int nbparamstructs = 0;
2142   int countparamstructs = 0;
2143   type_p head = NULL;
2144   type_p previous = NULL;
2145   type_p tmp;
2146   struct state_token_st *t0 = peek_state_token (0);
2147   struct state_token_st *t1 = peek_state_token (1);
2148   struct state_token_st *t2 = peek_state_token (2);
2149
2150   if (state_token_kind (t0) == STOK_LEFTPAR
2151       && state_token_is_name (t1, "!param_structs")
2152       && state_token_kind (t2) == STOK_INTEGER)
2153     {
2154       nbparamstructs = t2->stok_un.stok_num;
2155       next_state_tokens (3);
2156       t0 = t1 = t2 = NULL;
2157       t0 = peek_state_token (0);
2158       while (state_token_kind (t0) != STOK_RIGHTPAR)
2159         {
2160           tmp = NULL;
2161           read_state_type (&tmp);
2162           if (head == NULL)
2163             {
2164               head = tmp;
2165               previous = head;
2166             }
2167           else
2168             {
2169               previous->next = tmp;
2170               previous = tmp;
2171             }
2172           t0 = peek_state_token (0);
2173           countparamstructs++;
2174         }
2175       next_state_tokens (1);
2176     }
2177   else
2178     fatal_reading_state (t0, "Bad param_structs syntax");
2179   t0 = peek_state_token (0);
2180   if (countparamstructs != nbparamstructs)
2181     fatal_reading_state_printf
2182       (t0,
2183        "invalid number of param_structs expected %d got %d",
2184        nbparamstructs, countparamstructs);
2185   *param_structs = head;
2186 }
2187
2188
2189 /* Read the variables.  */
2190 static void
2191 read_state_variables (pair_p *variables)
2192 {
2193   pair_p list = NULL;
2194   int nbvars = 0;
2195   struct state_token_st *t0 = peek_state_token (0);
2196   struct state_token_st *t1 = peek_state_token (1);
2197   struct state_token_st *t2 = peek_state_token (2);
2198
2199   if (state_token_kind (t0) == STOK_LEFTPAR
2200       && state_token_is_name (t1, "!variables")
2201       && state_token_kind (t2) == STOK_INTEGER)
2202     {
2203       int nbpairs = 0;
2204       nbvars = t2->stok_un.stok_num;
2205       next_state_tokens (3);
2206       nbpairs = read_state_pair_list (&list);
2207       t0 = peek_state_token (0);
2208       if (nbpairs != nbvars)
2209         fatal_reading_state_printf
2210           (t0, "Invalid number of variables, expected %d but got %d",
2211            nbvars, nbpairs);
2212       if (state_token_kind (t0) == STOK_RIGHTPAR)
2213         next_state_tokens (1);
2214       else
2215         fatal_reading_state (t0, "Waiting for ) in variables");
2216     }
2217   else
2218     fatal_reading_state (t0, "Bad variables syntax");
2219   *variables = list;
2220   if (verbosity_level >= 2)
2221     printf ("%s read %d variables from state\n", progname, nbvars);
2222 }
2223
2224
2225 /* Read the source directory.  */
2226 static void
2227 read_state_srcdir (void)
2228 {
2229   struct state_token_st *t0 = peek_state_token (0);
2230   struct state_token_st *t1 = peek_state_token (1);
2231   if (state_token_kind (t0) == STOK_LEFTPAR &&
2232       state_token_is_name (t1, "!srcdir"))
2233     {
2234       next_state_tokens (2);
2235       t0 = peek_state_token (0);
2236       t1 = peek_state_token (1);
2237       if (state_token_kind (t0) == STOK_STRING &&
2238           state_token_kind (t1) == STOK_RIGHTPAR)
2239         {
2240           srcdir = xstrdup (t0->stok_un.stok_string);
2241           srcdir_len = strlen (srcdir);
2242           next_state_tokens (2);
2243           return;
2244         }
2245     }
2246
2247   fatal_reading_state (t0, "Bad srcdir in state_file");
2248 }
2249
2250
2251 /* Read the sequence of GCC front-end languages.  */
2252 static void
2253 read_state_languages (void)
2254 {
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   if (state_token_kind (t0) == STOK_LEFTPAR
2259       && state_token_is_name (t1, "!languages")
2260       && state_token_kind (t2) == STOK_INTEGER)
2261     {
2262       int i = 0;
2263       num_lang_dirs = t2->stok_un.stok_num;
2264       lang_dir_names = XCNEWVEC (const char *, num_lang_dirs);
2265       next_state_tokens (3);
2266       t0 = t1 = t2 = NULL;
2267       for (i = 0; i < (int) num_lang_dirs; i++)
2268         {
2269           t0 = peek_state_token (0);
2270           if (state_token_kind (t0) != STOK_NAME)
2271             fatal_reading_state (t0, "expecting language name in state file");
2272           lang_dir_names[i] = t0->stok_un.stok_ident->stid_name;
2273           next_state_tokens (1);
2274         }
2275       t0 = peek_state_token (0);
2276       if (state_token_kind (t0) != STOK_RIGHTPAR)
2277         fatal_reading_state (t0, "missing ) in languages list of state file");
2278       next_state_tokens (1);
2279     }
2280   else
2281     fatal_reading_state (t0, "expecting languages list in state file");
2282
2283 }
2284
2285 /* Read the sequence of files.  */
2286 static void
2287 read_state_files_list (void)
2288 {
2289   struct state_token_st *t0 = peek_state_token (0);
2290   struct state_token_st *t1 = peek_state_token (1);
2291   struct state_token_st *t2 = peek_state_token (2);
2292
2293   if (state_token_kind (t0) == STOK_LEFTPAR
2294       && state_token_is_name (t1, "!fileslist")
2295       && state_token_kind (t2) == STOK_INTEGER)
2296     {
2297       int i = 0;
2298       num_gt_files = t2->stok_un.stok_num;
2299       next_state_tokens (3);
2300       t0 = t1 = t2 = NULL;
2301       gt_files = XCNEWVEC (const input_file *, num_gt_files);
2302       for (i = 0; i < (int) num_gt_files; i++)
2303         {
2304           bool issrcfile = FALSE;
2305           t0 = t1 = t2 = NULL;
2306           t0 = peek_state_token (0);
2307           t1 = peek_state_token (1);
2308           t2 = peek_state_token (2);
2309           if (state_token_kind (t0) == STOK_LEFTPAR
2310               && (state_token_is_name (t1, "!file")
2311                   || (issrcfile = state_token_is_name (t1, "!srcfile")))
2312               && state_token_kind (t2) == STOK_INTEGER)
2313             {
2314               lang_bitmap bmap = t2->stok_un.stok_num;
2315               next_state_tokens (3);
2316               t0 = t1 = t2 = NULL;
2317               t0 = peek_state_token (0);
2318               t1 = peek_state_token (1);
2319               if (state_token_kind (t0) == STOK_STRING
2320                   && state_token_kind (t1) == STOK_RIGHTPAR)
2321                 {
2322                   const char *fnam = t0->stok_un.stok_string;
2323                   /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
2324                   input_file *curgt = NULL;
2325                   if (issrcfile)
2326                     {
2327                       static const char dirsepstr[2] =
2328                         { DIR_SEPARATOR, (char) 0 };
2329                       char *fullpath = concat (srcdir, dirsepstr, fnam, NULL);
2330                       curgt = input_file_by_name (fullpath);
2331                       free (fullpath);
2332                     }
2333                   else
2334                     curgt = input_file_by_name (fnam);
2335                   set_lang_bitmap (curgt, bmap);
2336                   gt_files[i] = curgt;
2337                   next_state_tokens (2);
2338                 }
2339               else
2340                 fatal_reading_state (t0,
2341                                      "bad file in !fileslist of state file");
2342             }
2343           else
2344             fatal_reading_state (t0,
2345                                  "expecting file in !fileslist of state file");
2346         };
2347       t0 = peek_state_token (0);
2348       if (!state_token_kind (t0) == STOK_RIGHTPAR)
2349         fatal_reading_state (t0, "missing ) for !fileslist in state file");
2350       next_state_tokens (1);
2351     }
2352   else
2353     fatal_reading_state (t0, "missing !fileslist in state file");
2354 }
2355
2356
2357 /* Read the trailer.  */
2358 static void
2359 read_state_trailer (void)
2360 {
2361   struct state_token_st *t0 = peek_state_token (0);
2362   struct state_token_st *t1 = peek_state_token (1);
2363   struct state_token_st *t2 = peek_state_token (2);
2364
2365   if (state_token_kind (t0) == STOK_LEFTPAR
2366       && state_token_is_name (t1, "!endfile")
2367       && state_token_kind (t2) == STOK_RIGHTPAR)
2368     next_state_tokens (3);
2369   else
2370     fatal_reading_state (t0, "missing !endfile in state file");
2371 }
2372
2373
2374 /* Utility functions for the state_seen_types hash table.  */
2375 static unsigned
2376 hash_type_number (const void *ty)
2377 {
2378   const struct type *type = (const struct type *) ty;
2379
2380   return type->state_number;
2381 }
2382
2383 static int
2384 equals_type_number (const void *ty1, const void *ty2)
2385 {
2386   const struct type *type1 = (const struct type *) ty1;
2387   const struct type *type2 = (const struct type *) ty2;
2388
2389   return type1->state_number == type2->state_number;
2390 }
2391
2392 static int
2393 string_eq (const void *a, const void *b)
2394 {
2395   const char *a0 = (const char *)a;
2396   const char *b0 = (const char *)b;
2397
2398   return (strcmp (a0, b0) == 0);
2399 }
2400
2401
2402 /* The function reading the state, called by main from gengtype.c.  */
2403 void
2404 read_state (const char *path)
2405 {
2406   state_file = fopen (path, "r");
2407   if (state_file == NULL)
2408     fatal ("Failed to open state file %s for reading [%s]", path,
2409            xstrerror (errno));
2410   state_path = path;
2411   state_line = 1;
2412
2413   if (verbosity_level >= 1)
2414     {
2415       printf ("%s reading state file %s;", progname, state_path);
2416       if (verbosity_level >= 2)
2417         putchar ('\n');
2418       fflush (stdout);
2419     }
2420
2421   state_seen_types =
2422     htab_create (2017, hash_type_number, equals_type_number, NULL);
2423   state_ident_tab =
2424     htab_create (4027, htab_hash_string, string_eq, NULL);
2425   read_state_version (version_string);
2426   read_state_srcdir ();
2427   read_state_languages ();
2428   read_state_files_list ();
2429   read_state_structures (&structures);
2430   if (ferror (state_file))
2431     fatal_reading_state_printf
2432       (NULL_STATE_TOKEN, "input error while reading state [%s]",
2433        xstrerror (errno));
2434   read_state_typedefs (&typedefs);
2435   read_state_param_structs (&param_structs);
2436   read_state_variables (&variables);
2437   read_state_trailer ();
2438
2439   if (verbosity_level >= 1)
2440     {
2441       printf ("%s read %ld bytes.\n", progname, ftell (state_file));
2442       fflush (stdout);
2443     };
2444
2445   if (fclose (state_file))
2446     fatal ("failed to close read state file %s [%s]",
2447            path, xstrerror (errno));
2448   state_file = NULL;
2449   state_path = NULL;
2450 }
2451
2452 /* End of file gengtype-state.c.  */