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