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