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