Simplify convert_modes, ignoring invalid old modes for CONST_INTs.
[official-gcc.git] / gcc / gengtype-state.c
blob0b5cf8f08662ff36c2443faf486a2e189d2d9369
1 /* Gengtype persistent state serialization & de-serialization.
2 Useful for gengtype in plugin mode.
4 Copyright (C) 2010-2013 Free Software Foundation, Inc.
6 This file is part of GCC.
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.
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.
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/>.
22 Contributed by Jeremie Salvucci <jeremie.salvucci@free.fr>
23 and Basile Starynkevitch <basile@starynkevitch.net>
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 "hashtab.h"
34 #include "version.h" /* For version_string & pkgversion_string. */
35 #include "obstack.h"
36 #include "gengtype.h"
40 /* Gives the file location of a type, if any. */
41 static inline struct fileloc*
42 type_lineloc (const_type_p ty)
44 if (!ty)
45 return NULL;
46 switch (ty->kind)
48 case TYPE_NONE:
49 gcc_unreachable ();
50 case TYPE_STRUCT:
51 case TYPE_UNION:
52 case TYPE_LANG_STRUCT:
53 case TYPE_USER_STRUCT:
54 case TYPE_UNDEFINED:
55 return CONST_CAST (struct fileloc*, &ty->u.s.line);
56 case TYPE_PARAM_STRUCT:
57 return CONST_CAST (struct fileloc*, &ty->u.param_struct.line);
58 case TYPE_SCALAR:
59 case TYPE_STRING:
60 case TYPE_POINTER:
61 case TYPE_ARRAY:
62 return NULL;
63 default:
64 gcc_unreachable ();
68 /* The state file has simplistic lispy lexical tokens. Its lexer gives
69 a linked list of struct state_token_st, through the peek_state_token
70 function. Lexical tokens are consumed with next_state_tokens. */
73 /* The lexical kind of each lispy token. */
74 enum state_token_en
76 STOK_NONE, /* Never used. */
77 STOK_INTEGER, /* Integer token. */
78 STOK_STRING, /* String token. */
79 STOK_LEFTPAR, /* Left opening parenthesis. */
80 STOK_RIGHTPAR, /* Right closing parenthesis. */
81 STOK_NAME /* hash-consed name or identifier. */
85 /* Structure and hash-table used to share identifiers or names. */
86 struct state_ident_st
88 /* TODO: We could improve the parser by reserving identifiers for
89 state keywords and adding a keyword number for them. That would
90 mean adding another field in this state_ident_st struct. */
91 char stid_name[1]; /* actually bigger & null terminated */
93 static htab_t state_ident_tab;
96 /* The state_token_st structure is for lexical tokens in the read
97 state file. The stok_kind field discriminates the union. Tokens
98 are allocated by peek_state_token which calls read_a_state_token
99 which allocate them. Tokens are freed by calls to
100 next_state_tokens. Token are organized in a FIFO look-ahead queue
101 filled by peek_state_token. */
102 struct state_token_st
104 enum state_token_en stok_kind; /* the lexical kind
105 discriminates the stok_un
106 union */
107 int stok_line; /* the line number */
108 int stok_col; /* the column number */
109 const char *stok_file; /* the file path */
110 struct state_token_st *stok_next; /* the next token in the
111 queue, when peeked */
112 union /* discriminated by stok_kind! */
114 int stok_num; /* when STOK_INTEGER */
115 char stok_string[1]; /* when STOK_STRING, actual size is
116 bigger and null terminated */
117 struct state_ident_st *stok_ident; /* when STOK_IDENT */
118 void *stok_ptr; /* null otherwise */
120 stok_un;
126 #define NULL_STATE_TOKEN (struct state_token_st*)0
128 /* the state_token pointer contains the leftmost current token. The
129 tokens are organized in a linked queue, using stok_next, for token
130 look-ahead. */
131 struct state_token_st *state_token = NULL_STATE_TOKEN;
133 /* Used by the reading lexer. */
134 static FILE *state_file;
135 static const char *state_path = NULL;
136 static int state_line = 0;
137 static long state_bol = 0; /* offset of beginning of line */
139 /* A class for writing out s-expressions, keeping track of newlines and
140 nested indentation. */
141 class s_expr_writer
143 public:
144 s_expr_writer ();
146 void write_new_line ();
147 void write_any_indent (int leading_spaces);
149 void begin_s_expr (const char *tag);
150 void end_s_expr ();
152 private:
153 int m_indent_amount;
154 int m_had_recent_newline;
155 }; // class s_expr_writer
157 /* A class for writing out "gtype.state". */
158 class state_writer : public s_expr_writer
160 public:
161 state_writer ();
163 private:
164 void write_state_fileloc (struct fileloc *floc);
165 void write_state_fields (pair_p fields);
166 void write_state_a_string (const char *s);
167 void write_state_string_option (options_p current);
168 void write_state_type_option (options_p current);
169 void write_state_nested_option (options_p current);
170 void write_state_option (options_p current);
171 void write_state_options (options_p opt);
172 void write_state_lang_bitmap (lang_bitmap bitmap);
173 void write_state_version (const char *version);
174 void write_state_scalar_type (type_p current);
175 void write_state_string_type (type_p current);
176 void write_state_undefined_type (type_p current);
177 void write_state_struct_union_type (type_p current, const char *kindstr);
178 void write_state_struct_type (type_p current);
179 void write_state_user_struct_type (type_p current);
180 void write_state_union_type (type_p current);
181 void write_state_lang_struct_type (type_p current);
182 void write_state_param_struct_type (type_p current);
183 void write_state_pointer_type (type_p current);
184 void write_state_array_type (type_p current);
185 void write_state_gc_used (enum gc_used_enum gus);
186 void write_state_common_type_content (type_p current);
187 void write_state_type (type_p current);
188 void write_state_pair (pair_p current);
189 int write_state_pair_list (pair_p list);
190 void write_state_typedefs (void);
191 void write_state_structures (void);
192 void write_state_param_structs (void);
193 void write_state_variables (void);
194 void write_state_srcdir (void);
195 void write_state_files_list (void);
196 void write_state_languages (void);
198 friend void write_state (const char *state_path);
200 private:
201 /* Counter of written types. */
202 int m_state_written_type_count;
203 }; // class state_writer
206 /* class s_expr_writer's trivial constructor. */
207 s_expr_writer::s_expr_writer ()
208 : m_indent_amount (0),
209 m_had_recent_newline (0)
213 /* Write a newline to the output file, merging adjacent newlines. */
214 void
215 s_expr_writer::write_new_line (void)
217 /* Don't add a newline if we've just had one. */
218 if (!m_had_recent_newline)
220 fprintf (state_file, "\n");
221 m_had_recent_newline = 1;
225 /* If we've just had a newline, write the indentation amount, potentially
226 omitting some spaces.
228 LEADING_SPACES exists to support code that writes strings with leading
229 spaces (e.g " foo") which might occur within a line, or could be the first
230 thing on a line. By passing leading_spaces == 1, when such a string is the
231 first thing on a line, write_any_indent () swallows the successive
232 leading spaces into the indentation so that the "foo" begins at the expected
233 column. */
234 void
235 s_expr_writer::write_any_indent (int leading_spaces)
237 int i;
238 int amount = m_indent_amount - leading_spaces;
239 if (m_had_recent_newline)
240 for (i = 0; i < amount; i++)
241 fprintf (state_file, " ");
242 m_had_recent_newline = 0;
245 /* Write the beginning of a new s-expresion e.g. "(!foo "
246 The writer automatically adds whitespace to show the hierarchical
247 structure of the expressions, so each one starts on a new line,
248 and any within it will be at an increased indentation level. */
249 void
250 s_expr_writer::begin_s_expr (const char *tag)
252 write_new_line ();
253 write_any_indent (0);
254 fprintf (state_file, "(!%s ", tag);
255 m_indent_amount++;
258 /* Write out the end of an s-expression: any necssessary indentation,
259 a closing parenthesis, and a new line. */
260 void
261 s_expr_writer::end_s_expr (void)
263 m_indent_amount--;
264 write_any_indent (0);
265 fprintf (state_file, ")");
266 write_new_line ();
270 /* class state_writer's trivial constructor. */
271 state_writer::state_writer ()
272 : s_expr_writer (),
273 m_state_written_type_count (0)
278 /* Fatal error messages when reading the state. They are extremely
279 unlikely, and only appear when this gengtype-state.c file is buggy,
280 or when reading a gengtype state which was not generated by the
281 same version of gengtype or GCC. */
284 /* Fatal message while reading state. */
285 static inline void
286 fatal_reading_state (struct state_token_st* tok, const char*msg)
288 if (tok)
289 fatal ("%s:%d:%d: Invalid state file; %s",
290 tok->stok_file, tok->stok_line, tok->stok_col,
291 msg);
292 else
293 fatal ("%s:%d: Invalid state file; %s",
294 state_path, state_line, msg);
298 /* Fatal printf-like message while reading state. This can't be a
299 function, because there is no way to pass a va_arg to a variant of
300 fatal. */
301 #define fatal_reading_state_printf(Tok,Fmt,...) do { \
302 struct state_token_st* badtok = Tok; \
303 if (badtok) \
304 fatal ("%s:%d:%d: Invalid state file; " Fmt, \
305 badtok->stok_file, \
306 badtok->stok_line, \
307 badtok->stok_col, __VA_ARGS__); \
308 else \
309 fatal ("%s:%d: Invalid state file; " Fmt, \
310 state_path, state_line, __VA_ARGS__); \
311 } while (0)
314 /* Find or allocate an identifier in our name hash table. */
315 static struct state_ident_st *
316 state_ident_by_name (const char *name, enum insert_option optins)
318 PTR *slot = NULL;
319 int namlen = 0;
320 struct state_ident_st *stid = NULL;
322 if (!name || !name[0])
323 return NULL;
325 slot = htab_find_slot (state_ident_tab, name, optins);
326 if (!slot)
327 return NULL;
329 namlen = strlen (name);
330 stid =
331 (struct state_ident_st *) xmalloc (sizeof (struct state_ident_st) +
332 namlen);
333 memset (stid, 0, sizeof (struct state_ident_st) + namlen);
334 strcpy (stid->stid_name, name);
335 *slot = stid;
337 return stid;
340 /* Our token lexer is heavily inspired by MELT's lexer, and share some
341 code with the file gcc/melt-runtime.c of the GCC MELT branch! We
342 really want the gengtype state to be easily parsable by MELT. This
343 is a usual lispy lexing routine, dealing with spaces and comments,
344 numbers, parenthesis, names, strings. */
345 static struct state_token_st *
346 read_a_state_token (void)
348 int c = 0;
349 long curoff = 0;
350 struct state_token_st *tk = NULL;
352 again: /* Read again, e.g. after a comment or spaces. */
353 c = getc (state_file);
354 if (c == EOF)
355 return NULL;
357 /* Handle spaces, count lines. */
358 if (c == '\n')
360 state_line++;
361 state_bol = curoff = ftell (state_file);
362 goto again;
364 if (ISSPACE (c))
365 goto again;
366 /* Skip comments starting with semi-colon. */
367 if (c == ';')
371 c = getc (state_file);
373 while (c > 0 && c != '\n');
374 if (c == '\n')
376 state_line++;
377 state_bol = curoff = ftell (state_file);
379 goto again;
381 /* Read signed numbers. */
382 if (ISDIGIT (c) || c == '-' || c == '+')
383 { /* number */
384 int n = 0;
385 ungetc (c, state_file);
386 curoff = ftell (state_file);
387 if (fscanf (state_file, "%d", &n) <= 0)
388 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error in number");
389 tk = XCNEW (struct state_token_st);
390 tk->stok_kind = STOK_INTEGER;
391 tk->stok_line = state_line;
392 tk->stok_col = curoff - state_bol;
393 tk->stok_file = state_path;
394 tk->stok_next = NULL;
395 tk->stok_un.stok_num = n;
397 return tk;
399 /* Read an opening left parenthesis. */
400 else if (c == '(')
402 curoff = ftell (state_file);
403 tk = XCNEW (struct state_token_st);
404 tk->stok_kind = STOK_LEFTPAR;
405 tk->stok_line = state_line;
406 tk->stok_col = curoff - state_bol;
407 tk->stok_file = state_path;
408 tk->stok_next = NULL;
410 return tk;
412 /* Read an closing right parenthesis. */
413 else if (c == ')')
415 curoff = ftell (state_file);
416 tk = XCNEW (struct state_token_st);
417 tk->stok_kind = STOK_RIGHTPAR;
418 tk->stok_line = state_line;
419 tk->stok_col = curoff - state_bol;
420 tk->stok_file = state_path;
421 tk->stok_next = NULL;
423 return tk;
425 /* Read identifiers, using an obstack. */
426 else if (ISALPHA (c) || c == '_' || c == '$' || c == '!' || c == '#')
428 struct obstack id_obstack;
429 struct state_ident_st *sid = NULL;
430 char *ids = NULL;
431 obstack_init (&id_obstack);
432 curoff = ftell (state_file);
433 while (ISALNUM (c) || c == '_' || c == '$' || c == '!' || c == '#')
435 obstack_1grow (&id_obstack, c);
436 c = getc (state_file);
437 if (c < 0)
438 break;
440 if (c >= 0)
441 ungetc (c, state_file);
442 obstack_1grow (&id_obstack, (char) 0);
443 ids = XOBFINISH (&id_obstack, char *);
444 sid = state_ident_by_name (ids, INSERT);
445 obstack_free (&id_obstack, NULL);
446 ids = NULL;
447 tk = XCNEW (struct state_token_st);
448 tk->stok_kind = STOK_NAME;
449 tk->stok_line = state_line;
450 tk->stok_col = curoff - state_bol;
451 tk->stok_file = state_path;
452 tk->stok_next = NULL;
453 tk->stok_un.stok_ident = sid;
455 return tk;
457 /* Read a string, dealing with escape sequences a la C! */
458 else if (c == '"')
460 char *cstr = NULL;
461 int cslen = 0;
462 struct obstack bstring_obstack;
463 obstack_init (&bstring_obstack);
464 curoff = ftell (state_file);
465 while ((c = getc (state_file)) != '"' && c >= 0)
467 if (ISPRINT (c) && c != '\\')
468 obstack_1grow (&bstring_obstack, (char) c);
469 else if (ISSPACE (c) && c != '\n')
470 obstack_1grow (&bstring_obstack, (char) c);
471 else if (c == '\\')
473 c = getc (state_file);
474 switch (c)
476 case 'a':
477 obstack_1grow (&bstring_obstack, '\a');
478 c = getc (state_file);
479 break;
480 case 'b':
481 obstack_1grow (&bstring_obstack, '\b');
482 c = getc (state_file);
483 break;
484 case 't':
485 obstack_1grow (&bstring_obstack, '\t');
486 c = getc (state_file);
487 break;
488 case 'n':
489 obstack_1grow (&bstring_obstack, '\n');
490 c = getc (state_file);
491 break;
492 case 'v':
493 obstack_1grow (&bstring_obstack, '\v');
494 c = getc (state_file);
495 break;
496 case 'f':
497 obstack_1grow (&bstring_obstack, '\f');
498 c = getc (state_file);
499 break;
500 case 'r':
501 obstack_1grow (&bstring_obstack, '\r');
502 c = getc (state_file);
503 break;
504 case '"':
505 obstack_1grow (&bstring_obstack, '\"');
506 c = getc (state_file);
507 break;
508 case '\\':
509 obstack_1grow (&bstring_obstack, '\\');
510 c = getc (state_file);
511 break;
512 case ' ':
513 obstack_1grow (&bstring_obstack, ' ');
514 c = getc (state_file);
515 break;
516 case 'x':
518 unsigned int cx = 0;
519 if (fscanf (state_file, "%02x", &cx) > 0 && cx > 0)
520 obstack_1grow (&bstring_obstack, cx);
521 else
522 fatal_reading_state
523 (NULL_STATE_TOKEN,
524 "Lexical error in string hex escape");
525 c = getc (state_file);
526 break;
528 default:
529 fatal_reading_state
530 (NULL_STATE_TOKEN,
531 "Lexical error - unknown string escape");
534 else
535 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error...");
537 if (c != '"')
538 fatal_reading_state (NULL_STATE_TOKEN, "Unterminated string");
539 obstack_1grow (&bstring_obstack, '\0');
540 cstr = XOBFINISH (&bstring_obstack, char *);
541 cslen = strlen (cstr);
542 tk = (struct state_token_st *)
543 xcalloc (sizeof (struct state_token_st) + cslen, 1);
544 tk->stok_kind = STOK_STRING;
545 tk->stok_line = state_line;
546 tk->stok_col = curoff - state_bol;
547 tk->stok_file = state_path;
548 tk->stok_next = NULL;
549 strcpy (tk->stok_un.stok_string, cstr);
550 obstack_free (&bstring_obstack, NULL);
552 return tk;
554 /* Got an unexpected character. */
555 fatal_reading_state_printf
556 (NULL_STATE_TOKEN,
557 "Lexical error at offset %ld - bad character \\%03o = '%c'",
558 ftell (state_file), c, c);
561 /* Used for lexical look-ahead. Retrieves the lexical token of rank
562 DEPTH, starting with 0 when reading the state file. Gives null on
563 end of file. */
564 static struct state_token_st *
565 peek_state_token (int depth)
567 int remdepth = depth;
568 struct state_token_st **ptoken = &state_token;
569 struct state_token_st *tok = NULL;
571 while (remdepth >= 0)
573 if (*ptoken == NULL)
575 *ptoken = tok = read_a_state_token ();
576 if (tok == NULL)
577 return NULL;
579 tok = *ptoken;
580 ptoken = &((*ptoken)->stok_next);
581 remdepth--;
584 return tok;
587 /* Consume the next DEPTH tokens and free them. */
588 static void
589 next_state_tokens (int depth)
591 struct state_token_st *n;
593 while (depth > 0)
595 if (state_token != NULL)
597 n = state_token->stok_next;
598 free (state_token);
599 state_token = n;
601 else
602 fatal_reading_state (NULL_STATE_TOKEN, "Tokens stack empty");
604 depth--;
608 /* Safely retrieve the lexical kind of a token. */
609 static inline enum state_token_en
610 state_token_kind (struct state_token_st *p)
612 if (p == NULL)
613 return STOK_NONE;
614 else
615 return p->stok_kind;
618 /* Test if a token is a given name i.e. an identifier. */
619 static inline bool
620 state_token_is_name (struct state_token_st *p, const char *name)
622 if (p == NULL)
623 return false;
625 if (p->stok_kind != STOK_NAME)
626 return false;
628 return !strcmp (p->stok_un.stok_ident->stid_name, name);
632 /* Following routines are useful for serializing datas.
634 * We want to serialize :
635 * - typedefs list
636 * - structures list
637 * - param_structs list
638 * - variables list
640 * So, we have one routine for each kind of data. The main writing
641 * routine is write_state. The main reading routine is
642 * read_state. Most writing routines write_state_FOO have a
643 * corresponding reading routine read_state_FOO. Reading is done in a
644 * recursive descending way, and any read error is fatal.
647 /* When reading the state, we need to remember the previously seen
648 types by their state_number, since GTY-ed types are usually
649 shared. */
650 static htab_t state_seen_types;
652 /* Return the length of a linked list made of pairs. */
653 static int pair_list_length (pair_p list);
655 /* Compute the length of a list of pairs, starting from the first
656 one. */
657 static int
658 pair_list_length (pair_p list)
660 int nbpair = 0;
661 pair_p l = NULL;
662 for (l = list; l; l = l->next)
663 nbpair++;
664 return nbpair;
667 /* Write a file location. Files relative to $(srcdir) are quite
668 frequent and are handled specially. This ensures that two gengtype
669 state file-s produced by gengtype on the same GCC source tree are
670 very similar and can be reasonably compared with diff, even if the
671 two GCC source trees have different absolute paths. */
672 void
673 state_writer::write_state_fileloc (struct fileloc *floc)
676 if (floc != NULL && floc->line > 0)
678 const char *srcrelpath = NULL;
679 gcc_assert (floc->file != NULL);
680 /* Most of the files are inside $(srcdir) so it is worth to
681 handle them specially. */
682 srcrelpath = get_file_srcdir_relative_path (floc->file);
683 if (srcrelpath != NULL)
685 begin_s_expr ("srcfileloc");
686 write_state_a_string (srcrelpath);
688 else
690 begin_s_expr ("fileloc");
691 write_state_a_string (get_input_file_name (floc->file));
693 fprintf (state_file, " %d", floc->line);
694 end_s_expr ();
696 else
697 fprintf (state_file, "nil ");
700 /* Write a list of fields. */
701 void
702 state_writer::write_state_fields (pair_p fields)
704 int nbfields = pair_list_length (fields);
705 int nbpairs = 0;
706 begin_s_expr ("fields");
707 fprintf (state_file, "%d ", nbfields);
708 nbpairs = write_state_pair_list (fields);
709 gcc_assert (nbpairs == nbfields);
710 end_s_expr ();
713 /* Write a null-terminated string in our lexical convention, very
714 similar to the convention of C. */
715 void
716 state_writer::write_state_a_string (const char *s)
718 char c;
720 write_any_indent (1);
722 fputs (" \"", state_file);
723 for (; *s != 0; s++)
725 c = *s;
726 switch (c)
728 case '\a':
729 fputs ("\\a", state_file);
730 break;
731 case '\b':
732 fputs ("\\b", state_file);
733 break;
734 case '\t':
735 fputs ("\\t", state_file);
736 break;
737 case '\n':
738 fputs ("\\n", state_file);
739 break;
740 case '\v':
741 fputs ("\\v", state_file);
742 break;
743 case '\f':
744 fputs ("\\f", state_file);
745 break;
746 case '\r':
747 fputs ("\\r", state_file);
748 break;
749 case '\"':
750 fputs ("\\\"", state_file);
751 break;
752 case '\\':
753 fputs ("\\\\", state_file);
754 break;
755 default:
756 if (ISPRINT (c))
757 putc (c, state_file);
758 else
759 fprintf (state_file, "\\x%02x", (unsigned) c);
762 fputs ("\"", state_file);
765 /* Our option-s have three kinds, each with its writer. */
766 void
767 state_writer::write_state_string_option (options_p current)
769 write_any_indent (0);
770 fprintf (state_file, "string ");
771 if (current->info.string != NULL)
772 write_state_a_string (current->info.string);
773 else
774 fprintf (state_file, " nil ");
777 void
778 state_writer::write_state_type_option (options_p current)
780 write_any_indent (0);
781 fprintf (state_file, "type ");
782 write_state_type (current->info.type);
785 void
786 state_writer::write_state_nested_option (options_p current)
788 write_any_indent (0);
789 fprintf (state_file, "nested ");
790 write_state_type (current->info.nested->type);
791 if (current->info.nested->convert_from != NULL)
792 write_state_a_string (current->info.nested->convert_from);
793 else
795 write_any_indent (1);
796 fprintf (state_file, " nil ");
799 if (current->info.nested->convert_to != NULL)
800 write_state_a_string (current->info.nested->convert_to);
801 else
803 write_any_indent (1);
804 fprintf (state_file, " nil ");
808 void
809 state_writer::write_state_option (options_p current)
811 begin_s_expr ("option");
813 write_any_indent (0);
814 if (current->name != NULL)
815 fprintf (state_file, "%s ", current->name);
816 else
817 fprintf (state_file, "nil ");
819 switch (current->kind)
821 case OPTION_STRING:
822 write_state_string_option (current);
823 break;
824 case OPTION_TYPE:
825 write_state_type_option (current);
826 break;
827 case OPTION_NESTED:
828 write_state_nested_option (current);
829 break;
830 default:
831 fatal ("Option tag unknown");
834 /* Terminate the "option" s-expression. */
835 end_s_expr ();
840 /* Write a list of GTY options. */
841 void
842 state_writer::write_state_options (options_p opt)
844 options_p current;
846 if (opt == NULL)
848 write_any_indent (0);
849 fprintf (state_file, "nil ");
850 return;
853 begin_s_expr ("options");
854 for (current = opt; current != NULL; current = current->next)
855 write_state_option (current);
856 end_s_expr ();
860 /* Write a bitmap representing a set of GCC front-end languages. */
861 void
862 state_writer::write_state_lang_bitmap (lang_bitmap bitmap)
864 write_any_indent (0);
865 fprintf (state_file, "%d ", (int) bitmap);
868 /* Write version information. */
869 void
870 state_writer::write_state_version (const char *version)
872 begin_s_expr ("version");
873 write_state_a_string (version);
874 end_s_expr ();
877 /* Write a scalar type. We have only two of these. */
878 void
879 state_writer::write_state_scalar_type (type_p current)
881 write_any_indent (0);
882 if (current == &scalar_nonchar)
883 fprintf (state_file, "scalar_nonchar ");
884 else if (current == &scalar_char)
885 fprintf (state_file, "scalar_char ");
886 else
887 fatal ("Unexpected type in write_state_scalar_type");
889 write_state_common_type_content (current);
892 /* Write the string type. There is only one such thing! */
893 void
894 state_writer::write_state_string_type (type_p current)
896 if (current == &string_type)
898 write_any_indent (0);
899 fprintf (state_file, "string ");
900 write_state_common_type_content (current);
902 else
903 fatal ("Unexpected type in write_state_string_type");
906 /* Write an undefined type. */
907 void
908 state_writer::write_state_undefined_type (type_p current)
910 DBGPRINTF ("undefined type @ %p #%d '%s'", (void *) current,
911 current->state_number, current->u.s.tag);
912 write_any_indent (0);
913 fprintf (state_file, "undefined ");
914 gcc_assert (current->gc_used == GC_UNUSED);
915 write_state_common_type_content (current);
916 if (current->u.s.tag != NULL)
917 write_state_a_string (current->u.s.tag);
918 else
920 write_any_indent (0);
921 fprintf (state_file, "nil");
924 write_state_fileloc (type_lineloc (current));
928 /* Common code to write structure like types. */
929 void
930 state_writer::write_state_struct_union_type (type_p current,
931 const char *kindstr)
933 DBGPRINTF ("%s type @ %p #%d '%s'", kindstr, (void *) current,
934 current->state_number, current->u.s.tag);
935 write_any_indent (0);
936 fprintf (state_file, "%s ", kindstr);
937 write_state_common_type_content (current);
938 if (current->u.s.tag != NULL)
939 write_state_a_string (current->u.s.tag);
940 else
942 write_any_indent (0);
943 fprintf (state_file, "nil");
946 write_state_fileloc (type_lineloc (current));
947 write_state_fields (current->u.s.fields);
948 write_state_options (current->u.s.opt);
949 write_state_lang_bitmap (current->u.s.bitmap);
953 /* Write a GTY struct type. */
954 void
955 state_writer::write_state_struct_type (type_p current)
957 write_state_struct_union_type (current, "struct");
958 write_state_type (current->u.s.lang_struct);
961 /* Write a GTY user-defined struct type. */
962 void
963 state_writer::write_state_user_struct_type (type_p current)
965 DBGPRINTF ("user_struct type @ %p #%d '%s'", (void *) current,
966 current->state_number, current->u.s.tag);
967 write_any_indent (0);
968 fprintf (state_file, "user_struct ");
969 write_state_common_type_content (current);
970 if (current->u.s.tag != NULL)
971 write_state_a_string (current->u.s.tag);
972 else
974 write_any_indent (0);
975 fprintf (state_file, "nil");
977 write_state_fileloc (type_lineloc (current));
978 write_state_fields (current->u.s.fields);
981 /* write a GTY union type. */
982 void
983 state_writer::write_state_union_type (type_p current)
985 write_state_struct_union_type (current, "union");
986 write_state_type (current->u.s.lang_struct);
989 /* Write a lang_struct type. This is tricky and was painful to debug,
990 we deal with the next field specifically within their lang_struct
991 subfield, which points to a linked list of homonumous types.
992 Change this function with extreme care, see also
993 read_state_lang_struct_type. */
994 void
995 state_writer::write_state_lang_struct_type (type_p current)
997 int nbhomontype = 0;
998 type_p hty = NULL;
999 const char *homoname = 0;
1000 write_state_struct_union_type (current, "lang_struct");
1001 /* lang_struct-ures are particularly tricky, since their
1002 u.s.lang_struct field gives a list of homonymous struct-s or
1003 union-s! */
1004 DBGPRINTF ("lang_struct @ %p #%d", (void *) current, current->state_number);
1005 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
1007 nbhomontype++;
1008 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype,
1009 (void *) hty, hty->state_number, hty->u.s.tag);
1010 /* Every member of the homonymous list should have the same tag. */
1011 gcc_assert (union_or_struct_p (hty));
1012 gcc_assert (hty->u.s.lang_struct == current);
1013 if (!homoname)
1014 homoname = hty->u.s.tag;
1015 gcc_assert (strcmp (homoname, hty->u.s.tag) == 0);
1017 begin_s_expr ("homotypes");
1018 fprintf (state_file, "%d", nbhomontype);
1019 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
1020 write_state_type (hty);
1021 end_s_expr ();
1024 /* Write a parametrized structure GTY type. */
1025 void
1026 state_writer::write_state_param_struct_type (type_p current)
1028 int i;
1030 write_any_indent (0);
1031 fprintf (state_file, "param_struct ");
1032 write_state_common_type_content (current);
1033 write_state_type (current->u.param_struct.stru);
1034 for (i = 0; i < NUM_PARAM; i++)
1036 if (current->u.param_struct.param[i] != NULL)
1037 write_state_type (current->u.param_struct.param[i]);
1038 else
1040 write_any_indent (0);
1041 fprintf (state_file, "nil ");
1044 write_state_fileloc (&current->u.param_struct.line);
1047 /* Write a pointer type. */
1048 void
1049 state_writer::write_state_pointer_type (type_p current)
1051 write_any_indent (0);
1052 fprintf (state_file, "pointer ");
1053 write_state_common_type_content (current);
1054 write_state_type (current->u.p);
1057 /* Write an array type. */
1058 void
1059 state_writer::write_state_array_type (type_p current)
1061 write_any_indent (0);
1062 fprintf (state_file, "array ");
1063 write_state_common_type_content (current);
1064 if (current->u.a.len != NULL)
1065 write_state_a_string (current->u.a.len);
1066 else
1068 write_any_indent (1);
1069 fprintf (state_file, " nil");
1072 write_any_indent (1);
1073 fprintf (state_file, " ");
1074 write_state_type (current->u.a.p);
1077 /* Write the gc_used information. */
1078 void
1079 state_writer::write_state_gc_used (enum gc_used_enum gus)
1081 write_any_indent (1);
1082 switch (gus)
1084 case GC_UNUSED:
1085 fprintf (state_file, " gc_unused");
1086 break;
1087 case GC_USED:
1088 fprintf (state_file, " gc_used");
1089 break;
1090 case GC_MAYBE_POINTED_TO:
1091 fprintf (state_file, " gc_maybe_pointed_to");
1092 break;
1093 case GC_POINTED_TO:
1094 fprintf (state_file, " gc_pointed_to");
1095 break;
1096 default:
1097 gcc_unreachable ();
1101 /* Utility routine to write the common content of all types. Notice
1102 that the next field is *not* written on purpose. */
1103 void
1104 state_writer::write_state_common_type_content (type_p current)
1106 write_any_indent (0);
1107 fprintf (state_file, "%d ", current->state_number);
1108 /* We do not write the next type, because list of types are
1109 explicitly written. However, lang_struct are special in that
1110 respect. See function write_state_lang_struct_type for more. */
1111 write_state_type (current->pointer_to);
1112 write_state_gc_used (current->gc_used);
1116 /* The important and recursive routine writing GTY types as understood
1117 by gengtype. Types which have a positive state_number have already
1118 been seen and written. */
1119 void
1120 state_writer::write_state_type (type_p current)
1122 write_any_indent (0);
1123 if (current == NULL)
1125 fprintf (state_file, "nil ");
1126 return;
1129 begin_s_expr ("type");
1131 if (current->state_number > 0)
1133 write_any_indent (0);
1134 fprintf (state_file, "already_seen %d", current->state_number);
1136 else
1138 m_state_written_type_count++;
1139 DBGPRINTF ("writing type #%d @%p old number %d", m_state_written_type_count,
1140 (void *) current, current->state_number);
1141 current->state_number = m_state_written_type_count;
1142 switch (current->kind)
1144 case TYPE_NONE:
1145 gcc_unreachable ();
1146 case TYPE_UNDEFINED:
1147 write_state_undefined_type (current);
1148 break;
1149 case TYPE_STRUCT:
1150 write_state_struct_type (current);
1151 break;
1152 case TYPE_USER_STRUCT:
1153 write_state_user_struct_type (current);
1154 break;
1155 case TYPE_UNION:
1156 write_state_union_type (current);
1157 break;
1158 case TYPE_POINTER:
1159 write_state_pointer_type (current);
1160 break;
1161 case TYPE_ARRAY:
1162 write_state_array_type (current);
1163 break;
1164 case TYPE_LANG_STRUCT:
1165 write_state_lang_struct_type (current);
1166 break;
1167 case TYPE_PARAM_STRUCT:
1168 write_state_param_struct_type (current);
1169 break;
1170 case TYPE_SCALAR:
1171 write_state_scalar_type (current);
1172 break;
1173 case TYPE_STRING:
1174 write_state_string_type (current);
1175 break;
1179 /* Terminate the "type" s-expression. */
1180 end_s_expr ();
1184 /* Write a pair. */
1185 void
1186 state_writer::write_state_pair (pair_p current)
1188 if (current == NULL)
1190 write_any_indent (0);
1191 fprintf (state_file, "nil)");
1192 return;
1195 begin_s_expr ("pair");
1197 if (current->name != NULL)
1198 write_state_a_string (current->name);
1199 else
1200 write_state_a_string ("nil");
1202 write_state_type (current->type);
1203 write_state_fileloc (&(current->line));
1204 write_state_options (current->opt);
1206 /* Terminate the "pair" s-expression. */
1207 end_s_expr ();
1210 /* Write a pair list and return the number of pairs written. */
1212 state_writer::write_state_pair_list (pair_p list)
1214 int nbpair = 0;
1215 pair_p current;
1217 for (current = list; current != NULL; current = current->next)
1219 write_state_pair (current);
1220 nbpair++;
1222 return nbpair;
1226 /* When writing imported linked lists, like typedefs, structures,
1227 param_structs, ... we count their length first and write it. These
1228 eases the reading, and enables an extra verification on the number
1229 of actually read items. */
1231 /* Write our typedefs. */
1232 void
1233 state_writer::write_state_typedefs (void)
1235 int nbtypedefs = pair_list_length (typedefs);
1236 int nbpairs = 0;
1237 begin_s_expr ("typedefs");
1238 fprintf (state_file, "%d", nbtypedefs);
1239 nbpairs = write_state_pair_list (typedefs);
1240 gcc_assert (nbpairs == nbtypedefs);
1241 end_s_expr ();
1242 if (verbosity_level >= 2)
1243 printf ("%s wrote %d typedefs\n", progname, nbtypedefs);
1246 /* Write our structures. */
1247 void
1248 state_writer::write_state_structures (void)
1250 int nbstruct = 0;
1251 type_p current;
1253 for (current = structures; current != NULL; current = current->next)
1254 nbstruct++;
1256 begin_s_expr ("structures");
1257 fprintf (state_file, "%d", nbstruct);
1259 for (current = structures; current != NULL; current = current->next)
1261 write_new_line ();
1262 write_state_type (current);
1265 /* Terminate the "structures" s-expression. */
1266 end_s_expr ();
1267 if (verbosity_level >= 2)
1268 printf ("%s wrote %d structures in state\n", progname, nbstruct);
1271 /* Write our param_struct-s. */
1272 void
1273 state_writer::write_state_param_structs (void)
1275 int nbparamstruct = 0;
1276 type_p current;
1278 for (current = param_structs; current != NULL; current = current->next)
1279 nbparamstruct++;
1281 begin_s_expr ("param_structs");
1282 fprintf (state_file, "%d", nbparamstruct);
1284 for (current = param_structs; current != NULL; current = current->next)
1285 write_state_type (current);
1287 end_s_expr ();
1290 /* Write our variables. */
1291 void
1292 state_writer::write_state_variables (void)
1294 int nbvars = pair_list_length (variables);
1295 int nbpairs = 0;
1296 begin_s_expr ("variables");
1297 fprintf (state_file, "%d", nbvars);
1298 nbpairs = write_state_pair_list (variables);
1299 gcc_assert (nbpairs == nbvars);
1300 end_s_expr ();
1301 if (verbosity_level >= 2)
1302 printf ("%s wrote %d variables.\n", progname, nbvars);
1305 /* Write the source directory. File locations within the source
1306 directory have been written specifically. */
1307 void
1308 state_writer::write_state_srcdir (void)
1310 begin_s_expr ("srcdir");
1311 write_state_a_string (srcdir);
1312 end_s_expr ();
1315 /* Count and write the list of our files. */
1316 void
1317 state_writer::write_state_files_list (void)
1319 int i = 0;
1320 /* Write the list of files with their lang_bitmap. */
1321 begin_s_expr ("fileslist");
1322 fprintf (state_file, "%d", (int) num_gt_files);
1323 for (i = 0; i < (int) num_gt_files; i++)
1325 const char *cursrcrelpath = NULL;
1326 const input_file *curfil = gt_files[i];
1327 /* Most of the files are inside $(srcdir) so it is worth to
1328 handle them specially. */
1329 cursrcrelpath = get_file_srcdir_relative_path (curfil);
1330 if (cursrcrelpath)
1332 begin_s_expr ("srcfile");
1333 fprintf (state_file, "%d ", get_lang_bitmap (curfil));
1334 write_state_a_string (cursrcrelpath);
1336 else
1338 begin_s_expr ("file");
1339 fprintf (state_file, "%d ", get_lang_bitmap (curfil));
1340 write_state_a_string (get_input_file_name (curfil));
1342 /* Terminate the inner s-expression (either "srcfile" or "file"). */
1343 end_s_expr ();
1345 /* Terminate the "fileslist" s-expression. */
1346 end_s_expr ();
1349 /* Write the list of GCC front-end languages. */
1350 void
1351 state_writer::write_state_languages (void)
1353 int i = 0;
1354 begin_s_expr ("languages");
1355 fprintf (state_file, "%d", (int) num_lang_dirs);
1356 for (i = 0; i < (int) num_lang_dirs; i++)
1358 /* Languages names are identifiers, we expect only letters or
1359 underscores or digits in them. In particular, C++ is not a
1360 valid language name, but cp is valid. */
1361 fprintf (state_file, " %s", lang_dir_names[i]);
1363 end_s_expr ();
1366 /* Write the trailer. */
1367 static void
1368 write_state_trailer (void)
1370 /* This test should probably catch IO errors like disk full... */
1371 if (fputs ("\n(!endfile)\n", state_file) == EOF)
1372 fatal ("failed to write state trailer [%s]", xstrerror (errno));
1375 /* The write_state routine is the only writing routine called by main
1376 in gengtype.c. To avoid messing the state if gengtype is
1377 interrupted or aborted, we write a temporary file and rename it
1378 after having written it in totality. */
1379 void
1380 write_state (const char *state_path)
1382 long statelen = 0;
1383 time_t now = 0;
1384 char *temp_state_path = NULL;
1385 char tempsuffix[40];
1386 time (&now);
1388 /* We write a unique temporary file which is renamed when complete
1389 * only. So even if gengtype is interrupted, the written state file
1390 * won't be partially written, since the temporary file is not yet
1391 * renamed in that case. */
1392 memset (tempsuffix, 0, sizeof (tempsuffix));
1393 snprintf (tempsuffix, sizeof (tempsuffix) - 1, "-%ld-%d.tmp", (long) now,
1394 (int) getpid ());
1395 temp_state_path = concat (state_path, tempsuffix, NULL);
1396 state_file = fopen (temp_state_path, "w");
1397 if (state_file == NULL)
1398 fatal ("Failed to open file %s for writing state: %s",
1399 temp_state_path, xstrerror (errno));
1400 if (verbosity_level >= 3)
1401 printf ("%s writing state file %s temporarily in %s\n",
1402 progname, state_path, temp_state_path);
1403 /* This is the first line of the state. Perhaps the file utility
1404 could know about that, so don't change it often. */
1405 fprintf (state_file, ";;;;@@@@ GCC gengtype state\n");
1406 /* Output a few comments for humans. */
1407 fprintf (state_file,
1408 ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
1409 fprintf (state_file,
1410 ";;; The format of this file is tied to a particular version of GCC.\n");
1411 fprintf (state_file,
1412 ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1413 fprintf (state_file,
1414 ";;; This file should be parsed by the same %s which wrote it.\n",
1415 progname);
1417 state_writer sw;
1419 /* The first non-comment significant line gives the version string. */
1420 sw.write_state_version (version_string);
1421 sw.write_state_srcdir ();
1422 sw.write_state_languages ();
1423 sw.write_state_files_list ();
1424 sw.write_state_structures ();
1425 sw.write_state_typedefs ();
1426 sw.write_state_param_structs ();
1427 sw.write_state_variables ();
1428 write_state_trailer ();
1429 statelen = ftell (state_file);
1430 if (ferror (state_file))
1431 fatal ("output error when writing state file %s [%s]",
1432 temp_state_path, xstrerror (errno));
1433 if (fclose (state_file))
1434 fatal ("failed to close state file %s [%s]",
1435 temp_state_path, xstrerror (errno));
1436 if (rename (temp_state_path, state_path))
1437 fatal ("failed to rename %s to state file %s [%s]", temp_state_path,
1438 state_path, xstrerror (errno));
1439 free (temp_state_path);
1441 if (verbosity_level >= 1)
1442 printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1443 progname, state_path, statelen, sw.m_state_written_type_count);
1447 /** End of writing routines! The corresponding reading routines follow. **/
1451 /* Forward declarations, since some read_state_* functions are
1452 recursive! */
1453 static void read_state_fileloc (struct fileloc *line);
1454 static void read_state_options (options_p *opt);
1455 static void read_state_type (type_p *current);
1456 static void read_state_pair (pair_p *pair);
1457 /* Return the number of pairs actually read. */
1458 static int read_state_pair_list (pair_p *list);
1459 static void read_state_fields (pair_p *fields);
1460 static void read_state_common_type_content (type_p current);
1465 /* Record into the state_seen_types hash-table a type which we are
1466 reading, to enable recursive or circular references to it. */
1467 static void
1468 record_type (type_p type)
1470 PTR *slot;
1472 slot = htab_find_slot (state_seen_types, type, INSERT);
1473 gcc_assert (slot);
1475 *slot = type;
1478 /* Read an already seen type. */
1479 static void
1480 read_state_already_seen_type (type_p *type)
1482 struct state_token_st *t0 = peek_state_token (0);
1484 if (state_token_kind (t0) == STOK_INTEGER)
1486 PTR *slot = NULL;
1487 struct type loctype = { TYPE_SCALAR, 0, 0, 0, GC_UNUSED, {0} };
1489 loctype.state_number = t0->stok_un.stok_num;
1490 slot = htab_find_slot (state_seen_types, &loctype, NO_INSERT);
1491 if (slot == NULL)
1493 fatal_reading_state (t0, "Unknown type");
1496 next_state_tokens (1);
1497 *type = (type_p) *slot;
1499 else
1501 fatal_reading_state (t0, "Bad seen type");
1506 /* Read the scalar_nonchar type. */
1507 static void
1508 read_state_scalar_nonchar_type (type_p *type)
1510 *type = &scalar_nonchar;
1511 read_state_common_type_content (*type);
1515 /* Read the scalar_char type. */
1516 static void
1517 read_state_scalar_char_type (type_p *type)
1519 *type = &scalar_char;
1520 read_state_common_type_content (*type);
1523 /* Read the string_type. */
1524 static void
1525 read_state_string_type (type_p *type)
1527 *type = &string_type;
1528 read_state_common_type_content (*type);
1532 /* Read a lang_bitmap representing a set of GCC front-end languages. */
1533 static void
1534 read_state_lang_bitmap (lang_bitmap *bitmap)
1536 struct state_token_st *t;
1538 t = peek_state_token (0);
1539 if (state_token_kind (t) == STOK_INTEGER)
1541 *bitmap = t->stok_un.stok_num;
1542 next_state_tokens (1);
1544 else
1546 fatal_reading_state (t, "Bad syntax for bitmap");
1551 /* Read an undefined type. */
1552 static void
1553 read_state_undefined_type (type_p type)
1555 struct state_token_st *t0;
1557 type->kind = TYPE_UNDEFINED;
1558 read_state_common_type_content (type);
1559 t0 = peek_state_token (0);
1560 if (state_token_kind (t0) == STOK_STRING)
1562 if (state_token_is_name (t0, "nil"))
1564 type->u.s.tag = NULL;
1565 DBGPRINTF ("read anonymous undefined type @%p #%d",
1566 (void *) type, type->state_number);
1568 else
1570 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1571 DBGPRINTF ("read undefined type @%p #%d '%s'",
1572 (void *) type, type->state_number, type->u.s.tag);
1575 next_state_tokens (1);
1576 read_state_fileloc (&(type->u.s.line));
1578 else
1580 fatal_reading_state (t0, "Bad tag in undefined type");
1585 /* Read a GTY-ed struct type. */
1586 static void
1587 read_state_struct_type (type_p type)
1589 struct state_token_st *t0;
1591 type->kind = TYPE_STRUCT;
1592 read_state_common_type_content (type);
1593 t0 = peek_state_token (0);
1594 if (state_token_kind (t0) == STOK_STRING)
1596 if (state_token_is_name (t0, "nil"))
1598 type->u.s.tag = NULL;
1599 DBGPRINTF ("read anonymous struct type @%p #%d",
1600 (void *) type, type->state_number);
1602 else
1604 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1605 DBGPRINTF ("read struct type @%p #%d '%s'",
1606 (void *) type, type->state_number, type->u.s.tag);
1609 next_state_tokens (1);
1610 read_state_fileloc (&(type->u.s.line));
1611 read_state_fields (&(type->u.s.fields));
1612 read_state_options (&(type->u.s.opt));
1613 read_state_lang_bitmap (&(type->u.s.bitmap));
1614 read_state_type (&(type->u.s.lang_struct));
1616 else
1618 fatal_reading_state (t0, "Bad tag in struct type");
1623 /* Read a GTY-ed user-provided struct TYPE. */
1625 static void
1626 read_state_user_struct_type (type_p type)
1628 struct state_token_st *t0;
1630 type->kind = TYPE_USER_STRUCT;
1631 read_state_common_type_content (type);
1632 t0 = peek_state_token (0);
1633 if (state_token_kind (t0) == STOK_STRING)
1635 if (state_token_is_name (t0, "nil"))
1637 type->u.s.tag = NULL;
1638 DBGPRINTF ("read anonymous struct type @%p #%d",
1639 (void *) type, type->state_number);
1641 else
1643 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1644 DBGPRINTF ("read struct type @%p #%d '%s'",
1645 (void *) type, type->state_number, type->u.s.tag);
1648 next_state_tokens (1);
1649 read_state_fileloc (&(type->u.s.line));
1650 read_state_fields (&(type->u.s.fields));
1652 else
1654 fatal_reading_state (t0, "Bad tag in user-struct type");
1659 /* Read a GTY-ed union type. */
1660 static void
1661 read_state_union_type (type_p type)
1663 struct state_token_st *t0;
1665 type->kind = TYPE_UNION;
1666 read_state_common_type_content (type);
1667 t0 = peek_state_token (0);
1668 if (state_token_kind (t0) == STOK_STRING)
1670 if (state_token_is_name (t0, "nil"))
1672 type->u.s.tag = NULL;
1673 DBGPRINTF ("read anonymous union type @%p #%d",
1674 (void *) type, type->state_number);
1676 else
1678 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1679 DBGPRINTF ("read union type @%p #%d '%s'",
1680 (void *) type, type->state_number, type->u.s.tag);
1682 next_state_tokens (1);
1683 read_state_fileloc (&(type->u.s.line));
1684 read_state_fields (&(type->u.s.fields));
1685 read_state_options (&(type->u.s.opt));
1686 read_state_lang_bitmap (&(type->u.s.bitmap));
1687 read_state_type (&(type->u.s.lang_struct));
1689 else
1690 fatal_reading_state (t0, "Bad tag in union type");
1694 /* Read a GTY-ed pointer type. */
1695 static void
1696 read_state_pointer_type (type_p type)
1698 type->kind = TYPE_POINTER;
1699 read_state_common_type_content (type);
1700 DBGPRINTF ("read pointer type @%p #%d", (void *) type, type->state_number);
1701 read_state_type (&(type->u.p));
1705 /* Read a GTY-ed array type. */
1706 static void
1707 read_state_array_type (type_p type)
1709 struct state_token_st *t0;
1711 type->kind = TYPE_ARRAY;
1712 read_state_common_type_content (type);
1713 t0 = peek_state_token (0);
1714 if (state_token_kind (t0) == STOK_STRING)
1716 type->u.a.len = xstrdup (t0->stok_un.stok_string);
1717 DBGPRINTF ("read array type @%p #%d length '%s'",
1718 (void *) type, type->state_number, type->u.a.len);
1719 next_state_tokens (1);
1722 else if (state_token_is_name (t0, "nil"))
1724 type->u.a.len = NULL;
1725 DBGPRINTF ("read array type @%p #%d without length",
1726 (void *) type, type->state_number);
1727 next_state_tokens (1);
1730 else
1731 fatal_reading_state (t0, "Bad array name type");
1732 read_state_type (&(type->u.a.p));
1737 /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
1738 front-end languages. This is a tricky function and it was painful
1739 to debug. Change it with extreme care. See also
1740 write_state_lang_struct_type. */
1741 static void
1742 read_state_lang_struct_type (type_p type)
1744 struct state_token_st *t0 = NULL;
1745 struct state_token_st *t1 = NULL;
1746 struct state_token_st *t2 = NULL;
1748 type->kind = TYPE_LANG_STRUCT;
1749 read_state_common_type_content (type);
1750 t0 = peek_state_token (0);
1751 if (state_token_kind (t0) == STOK_STRING)
1753 if (state_token_is_name (t0, "nil"))
1755 DBGPRINTF ("read anonymous lang_struct type @%p #%d",
1756 (void *) type, type->state_number);
1757 type->u.s.tag = NULL;
1759 else
1761 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1762 DBGPRINTF ("read lang_struct type @%p #%d '%s'",
1763 (void *) type, type->state_number, type->u.s.tag);
1765 next_state_tokens (1);
1767 else
1768 fatal_reading_state (t0, "Bad tag in lang struct type");
1769 read_state_fileloc (&(type->u.s.line));
1770 read_state_fields (&(type->u.s.fields));
1771 read_state_options (&(type->u.s.opt));
1772 read_state_lang_bitmap (&(type->u.s.bitmap));
1773 /* Within lang_struct-ures, the lang_struct field is a linked list
1774 of homonymous types! */
1775 t0 = peek_state_token (0);
1776 t1 = peek_state_token (1);
1777 t2 = peek_state_token (2);
1778 /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
1779 if (state_token_kind (t0) == STOK_LEFTPAR
1780 && state_token_is_name (t1, "!homotypes")
1781 && state_token_kind (t2) == STOK_INTEGER)
1783 type_p *prevty = &type->u.s.lang_struct;
1784 int nbhomotype = t2->stok_un.stok_num;
1785 int i = 0;
1786 t0 = t1 = t2 = NULL;
1787 next_state_tokens (3);
1788 for (i = 0; i < nbhomotype; i++)
1790 read_state_type (prevty);
1791 t0 = peek_state_token (0);
1792 if (*prevty)
1793 prevty = &(*prevty)->next;
1794 else
1795 fatal_reading_state (t0,
1796 "expecting type in homotype list for lang_struct");
1798 if (state_token_kind (t0) != STOK_RIGHTPAR)
1799 fatal_reading_state (t0,
1800 "expecting ) in homotype list for lang_struct");
1801 next_state_tokens (1);
1803 else
1804 fatal_reading_state (t0, "expecting !homotypes for lang_struct");
1808 /* Read a param_struct type for GTY parametrized structures. */
1809 static void
1810 read_state_param_struct_type (type_p type)
1812 int i;
1813 struct state_token_st *t0;
1815 type->kind = TYPE_PARAM_STRUCT;
1816 read_state_common_type_content (type);
1817 DBGPRINTF ("read param_struct type @%p #%d",
1818 (void *) type, type->state_number);
1819 read_state_type (&(type->u.param_struct.stru));
1821 for (i = 0; i < NUM_PARAM; i++)
1823 t0 = peek_state_token (0);
1824 if (state_token_is_name (t0, "nil"))
1826 type->u.param_struct.param[i] = NULL;
1827 next_state_tokens (1);
1829 else
1830 read_state_type (&(type->u.param_struct.param[i]));
1832 read_state_fileloc (&(type->u.param_struct.line));
1836 /* Read the gc used information. */
1837 static void
1838 read_state_gc_used (enum gc_used_enum *pgus)
1840 struct state_token_st *t0 = peek_state_token (0);
1841 if (state_token_is_name (t0, "gc_unused"))
1842 *pgus = GC_UNUSED;
1843 else if (state_token_is_name (t0, "gc_used"))
1844 *pgus = GC_USED;
1845 else if (state_token_is_name (t0, "gc_maybe_pointed_to"))
1846 *pgus = GC_MAYBE_POINTED_TO;
1847 else if (state_token_is_name (t0, "gc_pointed_to"))
1848 *pgus = GC_POINTED_TO;
1849 else
1850 fatal_reading_state (t0, "invalid gc_used information");
1851 next_state_tokens (1);
1855 /* Utility function to read the common content of types. */
1856 static void
1857 read_state_common_type_content (type_p current)
1859 struct state_token_st *t0 = peek_state_token (0);
1861 if (state_token_kind (t0) == STOK_INTEGER)
1863 current->state_number = t0->stok_un.stok_num;
1864 next_state_tokens (1);
1865 record_type (current);
1867 else
1868 fatal_reading_state_printf (t0,
1869 "Expected integer for state_number line %d",
1870 state_line);
1871 /* We don't read the next field of the type. */
1872 read_state_type (&current->pointer_to);
1873 read_state_gc_used (&current->gc_used);
1877 /* Read a GTY-ed type. */
1878 void
1879 read_state_type (type_p *current)
1881 struct state_token_st *t0 = peek_state_token (0);
1882 struct state_token_st *t1 = peek_state_token (1);
1884 if (state_token_kind (t0) == STOK_LEFTPAR &&
1885 state_token_is_name (t1, "!type"))
1887 next_state_tokens (2);
1888 t0 = peek_state_token (0);
1889 if (state_token_is_name (t0, "already_seen"))
1891 next_state_tokens (1);
1892 read_state_already_seen_type (current);
1894 else
1896 t0 = peek_state_token (0);
1898 if (state_token_is_name (t0, "scalar_nonchar"))
1900 next_state_tokens (1);
1901 read_state_scalar_nonchar_type (current);
1903 else if (state_token_is_name (t0, "scalar_char"))
1905 next_state_tokens (1);
1906 read_state_scalar_char_type (current);
1908 else if (state_token_is_name (t0, "string"))
1910 next_state_tokens (1);
1911 read_state_string_type (current);
1913 else if (state_token_is_name (t0, "undefined"))
1915 *current = XCNEW (struct type);
1916 next_state_tokens (1);
1917 read_state_undefined_type (*current);
1919 else if (state_token_is_name (t0, "struct"))
1921 *current = XCNEW (struct type);
1922 next_state_tokens (1);
1923 read_state_struct_type (*current);
1925 else if (state_token_is_name (t0, "union"))
1927 *current = XCNEW (struct type);
1928 next_state_tokens (1);
1929 read_state_union_type (*current);
1931 else if (state_token_is_name (t0, "lang_struct"))
1933 *current = XCNEW (struct type);
1934 next_state_tokens (1);
1935 read_state_lang_struct_type (*current);
1937 else if (state_token_is_name (t0, "param_struct"))
1939 *current = XCNEW (struct type);
1940 next_state_tokens (1);
1941 read_state_param_struct_type (*current);
1943 else if (state_token_is_name (t0, "pointer"))
1945 *current = XCNEW (struct type);
1946 next_state_tokens (1);
1947 read_state_pointer_type (*current);
1949 else if (state_token_is_name (t0, "array"))
1951 *current = XCNEW (struct type);
1952 next_state_tokens (1);
1953 read_state_array_type (*current);
1955 else if (state_token_is_name (t0, "user_struct"))
1957 *current = XCNEW (struct type);
1958 next_state_tokens (1);
1959 read_state_user_struct_type (*current);
1961 else
1962 fatal_reading_state (t0, "bad type in (!type");
1964 t0 = peek_state_token (0);
1965 if (state_token_kind (t0) != STOK_RIGHTPAR)
1966 fatal_reading_state (t0, "missing ) in type");
1967 next_state_tokens (1);
1969 else if (state_token_is_name (t0, "nil"))
1971 next_state_tokens (1);
1972 *current = NULL;
1974 else
1975 fatal_reading_state (t0, "bad type syntax");
1979 /* Read a file location. Files within the source directory are dealt
1980 with specifically. */
1981 void
1982 read_state_fileloc (struct fileloc *floc)
1984 bool issrcfile = false;
1985 struct state_token_st *t0 = peek_state_token (0);
1986 struct state_token_st *t1 = peek_state_token (1);
1988 gcc_assert (floc != NULL);
1989 gcc_assert (srcdir != NULL);
1991 if (state_token_kind (t0) == STOK_LEFTPAR &&
1992 (state_token_is_name (t1, "!fileloc")
1993 || (issrcfile = state_token_is_name (t1, "!srcfileloc"))))
1995 next_state_tokens (2);
1996 t0 = peek_state_token (0);
1997 t1 = peek_state_token (1);
1998 if (state_token_kind (t0) == STOK_STRING &&
1999 state_token_kind (t1) == STOK_INTEGER)
2001 char *path = t0->stok_un.stok_string;
2002 if (issrcfile)
2004 static const char dirsepstr[2] = { DIR_SEPARATOR, (char) 0 };
2005 char *fullpath = concat (srcdir, dirsepstr, path, NULL);
2006 floc->file = input_file_by_name (fullpath);
2007 free (fullpath);
2009 else
2010 floc->file = input_file_by_name (path);
2011 floc->line = t1->stok_un.stok_num;
2012 next_state_tokens (2);
2014 else
2015 fatal_reading_state (t0,
2016 "Bad fileloc syntax, expected path string and line");
2017 t0 = peek_state_token (0);
2018 if (state_token_kind (t0) != STOK_RIGHTPAR)
2019 fatal_reading_state (t0, "Bad fileloc syntax, expected )");
2020 next_state_tokens (1);
2022 else if (state_token_is_name (t0, "nil"))
2024 next_state_tokens (1);
2025 floc->file = NULL;
2026 floc->line = 0;
2028 else
2029 fatal_reading_state (t0, "Bad fileloc syntax");
2033 /* Read the fields of a GTY-ed type. */
2034 void
2035 read_state_fields (pair_p *fields)
2037 pair_p tmp = NULL;
2038 struct state_token_st *t0 = peek_state_token (0);
2039 struct state_token_st *t1 = peek_state_token (1);
2040 struct state_token_st *t2 = peek_state_token (2);
2042 if (state_token_kind (t0) == STOK_LEFTPAR
2043 && state_token_is_name (t1, "!fields")
2044 && state_token_kind (t2) == STOK_INTEGER)
2046 int nbfields = t2->stok_un.stok_num;
2047 int nbpairs = 0;
2048 next_state_tokens (3);
2049 nbpairs = read_state_pair_list (&tmp);
2050 t0 = peek_state_token (0);
2051 if (nbpairs != nbfields)
2052 fatal_reading_state_printf
2053 (t0,
2054 "Mismatched fields number, expected %d got %d", nbpairs, nbfields);
2055 if (state_token_kind (t0) == STOK_RIGHTPAR)
2056 next_state_tokens (1);
2057 else
2058 fatal_reading_state (t0, "Bad fields expecting )");
2061 *fields = tmp;
2065 /* Read a string option. */
2066 static void
2067 read_state_string_option (options_p opt)
2069 struct state_token_st *t0 = peek_state_token (0);
2070 opt->kind = OPTION_STRING;
2071 if (state_token_kind (t0) == STOK_STRING)
2073 opt->info.string = xstrdup (t0->stok_un.stok_string);
2074 next_state_tokens (1);
2076 else if (state_token_is_name (t0, "nil"))
2078 opt->info.string = NULL;
2079 next_state_tokens (1);
2081 else
2082 fatal_reading_state (t0, "Missing name in string option");
2086 /* Read a type option. */
2087 static void
2088 read_state_type_option (options_p opt)
2090 opt->kind = OPTION_TYPE;
2091 read_state_type (&(opt->info.type));
2095 /* Read a nested option. */
2096 static void
2097 read_state_nested_option (options_p opt)
2099 struct state_token_st *t0;
2101 opt->info.nested = XCNEW (struct nested_ptr_data);
2102 opt->kind = OPTION_NESTED;
2103 read_state_type (&(opt->info.nested->type));
2104 t0 = peek_state_token (0);
2105 if (state_token_kind (t0) == STOK_STRING)
2107 opt->info.nested->convert_from = xstrdup (t0->stok_un.stok_string);
2108 next_state_tokens (1);
2110 else if (state_token_is_name (t0, "nil"))
2112 opt->info.nested->convert_from = NULL;
2113 next_state_tokens (1);
2115 else
2116 fatal_reading_state (t0, "Bad nested convert_from option");
2118 t0 = peek_state_token (0);
2119 if (state_token_kind (t0) == STOK_STRING)
2121 opt->info.nested->convert_to = xstrdup (t0->stok_un.stok_string);
2122 next_state_tokens (1);
2124 else if (state_token_is_name (t0, "nil"))
2126 opt->info.nested->convert_to = NULL;
2127 next_state_tokens (1);
2129 else
2130 fatal_reading_state (t0, "Bad nested convert_from option");
2134 /* Read an GTY option. */
2135 static void
2136 read_state_option (options_p *opt)
2138 struct state_token_st *t0 = peek_state_token (0);
2139 struct state_token_st *t1 = peek_state_token (1);
2141 if (state_token_kind (t0) == STOK_LEFTPAR &&
2142 state_token_is_name (t1, "!option"))
2144 next_state_tokens (2);
2145 t0 = peek_state_token (0);
2146 if (state_token_kind (t0) == STOK_NAME)
2148 *opt = XCNEW (struct options);
2149 if (state_token_is_name (t0, "nil"))
2150 (*opt)->name = NULL;
2151 else
2152 (*opt)->name = t0->stok_un.stok_ident->stid_name;
2153 next_state_tokens (1);
2154 t0 = peek_state_token (0);
2155 if (state_token_kind (t0) == STOK_NAME)
2157 if (state_token_is_name (t0, "string"))
2159 next_state_tokens (1);
2160 read_state_string_option (*opt);
2162 else if (state_token_is_name (t0, "type"))
2164 next_state_tokens (1);
2165 read_state_type_option (*opt);
2167 else if (state_token_is_name (t0, "nested"))
2169 next_state_tokens (1);
2170 read_state_nested_option (*opt);
2172 else
2173 fatal_reading_state (t0, "Bad option type");
2174 t0 = peek_state_token (0);
2175 if (state_token_kind (t0) != STOK_RIGHTPAR)
2176 fatal_reading_state (t0, "Bad syntax in option, expecting )");
2178 next_state_tokens (1);
2180 else
2181 fatal_reading_state (t0, "Missing option type");
2183 else
2184 fatal_reading_state (t0, "Bad name for option");
2186 else
2187 fatal_reading_state (t0, "Bad option, waiting for )");
2190 /* Read a list of options. */
2191 void
2192 read_state_options (options_p *opt)
2194 options_p head = NULL;
2195 options_p previous = NULL;
2196 options_p current_option = NULL;
2197 struct state_token_st *t0 = peek_state_token (0);
2198 struct state_token_st *t1 = peek_state_token (1);
2200 if (state_token_kind (t0) == STOK_LEFTPAR &&
2201 state_token_is_name (t1, "!options"))
2203 next_state_tokens (2);
2204 t0 = peek_state_token (0);
2205 while (state_token_kind (t0) != STOK_RIGHTPAR)
2207 read_state_option (&current_option);
2208 if (head == NULL)
2210 head = current_option;
2211 previous = head;
2213 else
2215 previous->next = current_option;
2216 previous = current_option;
2218 t0 = peek_state_token (0);
2220 next_state_tokens (1);
2222 else if (state_token_is_name (t0, "nil"))
2224 next_state_tokens (1);
2226 else
2227 fatal_reading_state (t0, "Bad options syntax");
2229 *opt = head;
2233 /* Read a version, and check against the version of the gengtype. */
2234 static void
2235 read_state_version (const char *version_string)
2237 struct state_token_st *t0 = peek_state_token (0);
2238 struct state_token_st *t1 = peek_state_token (1);
2240 if (state_token_kind (t0) == STOK_LEFTPAR &&
2241 state_token_is_name (t1, "!version"))
2243 next_state_tokens (2);
2244 t0 = peek_state_token (0);
2245 t1 = peek_state_token (1);
2246 if (state_token_kind (t0) == STOK_STRING &&
2247 state_token_kind (t1) == STOK_RIGHTPAR)
2249 /* Check that the read version string is the same as current
2250 version. */
2251 if (strcmp (version_string, t0->stok_un.stok_string))
2252 fatal_reading_state_printf (t0,
2253 "version string mismatch; expecting %s but got %s",
2254 version_string,
2255 t0->stok_un.stok_string);
2256 next_state_tokens (2);
2258 else
2259 fatal_reading_state (t0, "Missing version or right parenthesis");
2261 else
2262 fatal_reading_state (t0, "Bad version syntax");
2266 /* Read a pair. */
2267 void
2268 read_state_pair (pair_p *current)
2270 struct state_token_st *t0 = peek_state_token (0);
2271 struct state_token_st *t1 = peek_state_token (1);
2272 if (state_token_kind (t0) == STOK_LEFTPAR &&
2273 state_token_is_name (t1, "!pair"))
2275 *current = XCNEW (struct pair);
2276 next_state_tokens (2);
2277 t0 = peek_state_token (0);
2278 if (state_token_kind (t0) == STOK_STRING)
2280 if (strcmp (t0->stok_un.stok_string, "nil") == 0)
2282 (*current)->name = NULL;
2284 else
2286 (*current)->name = xstrdup (t0->stok_un.stok_string);
2288 next_state_tokens (1);
2289 read_state_type (&((*current)->type));
2290 read_state_fileloc (&((*current)->line));
2291 read_state_options (&((*current)->opt));;
2292 t0 = peek_state_token (0);
2293 if (state_token_kind (t0) == STOK_RIGHTPAR)
2295 next_state_tokens (1);
2297 else
2299 fatal_reading_state (t0, "Bad syntax for pair, )");
2302 else
2304 fatal_reading_state (t0, "Bad name for pair");
2307 else if (state_token_kind (t0) == STOK_NAME &&
2308 state_token_is_name (t0, "nil"))
2310 next_state_tokens (1);
2311 *current = NULL;
2313 else
2314 fatal_reading_state_printf (t0, "Bad syntax for pair, (!pair %d",
2315 state_token->stok_kind);
2319 /* Return the number of pairs actually read. */
2321 read_state_pair_list (pair_p *list)
2323 int nbpair = 0;
2324 pair_p head = NULL;
2325 pair_p previous = NULL;
2326 pair_p tmp = NULL;
2327 struct state_token_st *t0 = peek_state_token (0);
2328 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2330 read_state_pair (&tmp);
2331 if (head == NULL)
2333 head = tmp;
2334 previous = head;
2336 else
2338 previous->next = tmp;
2339 previous = tmp;
2341 t0 = peek_state_token (0);
2342 nbpair++;
2345 /* don't consume the ); the caller will eat it. */
2346 *list = head;
2347 return nbpair;
2350 /* Read the typedefs. */
2351 static void
2352 read_state_typedefs (pair_p *typedefs)
2354 int nbtypedefs = 0;
2355 pair_p list = NULL;
2356 struct state_token_st *t0 = peek_state_token (0);
2357 struct state_token_st *t1 = peek_state_token (1);
2358 struct state_token_st *t2 = peek_state_token (2);
2360 if (state_token_kind (t0) == STOK_LEFTPAR
2361 && state_token_is_name (t1, "!typedefs")
2362 && state_token_kind (t2) == STOK_INTEGER)
2364 int nbpairs = 0;
2365 nbtypedefs = t2->stok_un.stok_num;
2366 next_state_tokens (3);
2367 nbpairs = read_state_pair_list (&list);
2368 t0 = peek_state_token (0);
2369 if (nbpairs != nbtypedefs)
2370 fatal_reading_state_printf
2371 (t0,
2372 "invalid number of typedefs, expected %d but got %d",
2373 nbtypedefs, nbpairs);
2374 if (state_token_kind (t0) == STOK_RIGHTPAR)
2375 next_state_tokens (1);
2376 else
2377 fatal_reading_state (t0, "Bad typedefs syntax )");
2379 else
2380 fatal_reading_state (t0, "Bad typedefs syntax (!typedefs");
2382 if (verbosity_level >= 2)
2383 printf ("%s read %d typedefs from state\n", progname, nbtypedefs);
2384 *typedefs = list;
2388 /* Read the structures. */
2389 static void
2390 read_state_structures (type_p *structures)
2392 type_p head = NULL;
2393 type_p previous = NULL;
2394 type_p tmp;
2395 int nbstruct = 0, countstruct = 0;
2396 struct state_token_st *t0 = peek_state_token (0);
2397 struct state_token_st *t1 = peek_state_token (1);
2398 struct state_token_st *t2 = peek_state_token (2);
2400 if (state_token_kind (t0) == STOK_LEFTPAR
2401 && state_token_is_name (t1, "!structures")
2402 && state_token_kind (t2) == STOK_INTEGER)
2404 nbstruct = t2->stok_un.stok_num;
2405 next_state_tokens (3);
2406 t0 = peek_state_token (0);
2407 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2409 tmp = NULL;
2410 read_state_type (&tmp);
2411 countstruct++;
2412 if (head == NULL)
2414 head = tmp;
2415 previous = head;
2417 else
2419 previous->next = tmp;
2420 previous = tmp;
2422 t0 = peek_state_token (0);
2424 next_state_tokens (1);
2426 else
2427 fatal_reading_state (t0, "Bad structures syntax");
2428 if (countstruct != nbstruct)
2429 fatal_reading_state_printf (NULL_STATE_TOKEN,
2430 "expected %d structures but got %d",
2431 nbstruct, countstruct);
2432 if (verbosity_level >= 2)
2433 printf ("%s read %d structures from state\n", progname, nbstruct);
2434 *structures = head;
2438 /* Read the param_struct-s. */
2439 static void
2440 read_state_param_structs (type_p *param_structs)
2442 int nbparamstructs = 0;
2443 int countparamstructs = 0;
2444 type_p head = NULL;
2445 type_p previous = NULL;
2446 type_p tmp;
2447 struct state_token_st *t0 = peek_state_token (0);
2448 struct state_token_st *t1 = peek_state_token (1);
2449 struct state_token_st *t2 = peek_state_token (2);
2451 if (state_token_kind (t0) == STOK_LEFTPAR
2452 && state_token_is_name (t1, "!param_structs")
2453 && state_token_kind (t2) == STOK_INTEGER)
2455 nbparamstructs = t2->stok_un.stok_num;
2456 next_state_tokens (3);
2457 t0 = t1 = t2 = NULL;
2458 t0 = peek_state_token (0);
2459 while (state_token_kind (t0) != STOK_RIGHTPAR)
2461 tmp = NULL;
2462 read_state_type (&tmp);
2463 if (head == NULL)
2465 head = tmp;
2466 previous = head;
2468 else
2470 previous->next = tmp;
2471 previous = tmp;
2473 t0 = peek_state_token (0);
2474 countparamstructs++;
2476 next_state_tokens (1);
2478 else
2479 fatal_reading_state (t0, "Bad param_structs syntax");
2480 t0 = peek_state_token (0);
2481 if (countparamstructs != nbparamstructs)
2482 fatal_reading_state_printf
2483 (t0,
2484 "invalid number of param_structs expected %d got %d",
2485 nbparamstructs, countparamstructs);
2486 *param_structs = head;
2490 /* Read the variables. */
2491 static void
2492 read_state_variables (pair_p *variables)
2494 pair_p list = NULL;
2495 int nbvars = 0;
2496 struct state_token_st *t0 = peek_state_token (0);
2497 struct state_token_st *t1 = peek_state_token (1);
2498 struct state_token_st *t2 = peek_state_token (2);
2500 if (state_token_kind (t0) == STOK_LEFTPAR
2501 && state_token_is_name (t1, "!variables")
2502 && state_token_kind (t2) == STOK_INTEGER)
2504 int nbpairs = 0;
2505 nbvars = t2->stok_un.stok_num;
2506 next_state_tokens (3);
2507 nbpairs = read_state_pair_list (&list);
2508 t0 = peek_state_token (0);
2509 if (nbpairs != nbvars)
2510 fatal_reading_state_printf
2511 (t0, "Invalid number of variables, expected %d but got %d",
2512 nbvars, nbpairs);
2513 if (state_token_kind (t0) == STOK_RIGHTPAR)
2514 next_state_tokens (1);
2515 else
2516 fatal_reading_state (t0, "Waiting for ) in variables");
2518 else
2519 fatal_reading_state (t0, "Bad variables syntax");
2520 *variables = list;
2521 if (verbosity_level >= 2)
2522 printf ("%s read %d variables from state\n", progname, nbvars);
2526 /* Read the source directory. */
2527 static void
2528 read_state_srcdir (void)
2530 struct state_token_st *t0 = peek_state_token (0);
2531 struct state_token_st *t1 = peek_state_token (1);
2532 if (state_token_kind (t0) == STOK_LEFTPAR &&
2533 state_token_is_name (t1, "!srcdir"))
2535 next_state_tokens (2);
2536 t0 = peek_state_token (0);
2537 t1 = peek_state_token (1);
2538 if (state_token_kind (t0) == STOK_STRING &&
2539 state_token_kind (t1) == STOK_RIGHTPAR)
2541 srcdir = xstrdup (t0->stok_un.stok_string);
2542 srcdir_len = strlen (srcdir);
2543 next_state_tokens (2);
2544 return;
2548 fatal_reading_state (t0, "Bad srcdir in state_file");
2552 /* Read the sequence of GCC front-end languages. */
2553 static void
2554 read_state_languages (void)
2556 struct state_token_st *t0 = peek_state_token (0);
2557 struct state_token_st *t1 = peek_state_token (1);
2558 struct state_token_st *t2 = peek_state_token (2);
2559 if (state_token_kind (t0) == STOK_LEFTPAR
2560 && state_token_is_name (t1, "!languages")
2561 && state_token_kind (t2) == STOK_INTEGER)
2563 int i = 0;
2564 num_lang_dirs = t2->stok_un.stok_num;
2565 lang_dir_names = XCNEWVEC (const char *, num_lang_dirs);
2566 next_state_tokens (3);
2567 t0 = t1 = t2 = NULL;
2568 for (i = 0; i < (int) num_lang_dirs; i++)
2570 t0 = peek_state_token (0);
2571 if (state_token_kind (t0) != STOK_NAME)
2572 fatal_reading_state (t0, "expecting language name in state file");
2573 lang_dir_names[i] = t0->stok_un.stok_ident->stid_name;
2574 next_state_tokens (1);
2576 t0 = peek_state_token (0);
2577 if (state_token_kind (t0) != STOK_RIGHTPAR)
2578 fatal_reading_state (t0, "missing ) in languages list of state file");
2579 next_state_tokens (1);
2581 else
2582 fatal_reading_state (t0, "expecting languages list in state file");
2586 /* Read the sequence of files. */
2587 static void
2588 read_state_files_list (void)
2590 struct state_token_st *t0 = peek_state_token (0);
2591 struct state_token_st *t1 = peek_state_token (1);
2592 struct state_token_st *t2 = peek_state_token (2);
2594 if (state_token_kind (t0) == STOK_LEFTPAR
2595 && state_token_is_name (t1, "!fileslist")
2596 && state_token_kind (t2) == STOK_INTEGER)
2598 int i = 0;
2599 num_gt_files = t2->stok_un.stok_num;
2600 next_state_tokens (3);
2601 t0 = t1 = t2 = NULL;
2602 gt_files = XCNEWVEC (const input_file *, num_gt_files);
2603 for (i = 0; i < (int) num_gt_files; i++)
2605 bool issrcfile = FALSE;
2606 t0 = t1 = t2 = NULL;
2607 t0 = peek_state_token (0);
2608 t1 = peek_state_token (1);
2609 t2 = peek_state_token (2);
2610 if (state_token_kind (t0) == STOK_LEFTPAR
2611 && (state_token_is_name (t1, "!file")
2612 || (issrcfile = state_token_is_name (t1, "!srcfile")))
2613 && state_token_kind (t2) == STOK_INTEGER)
2615 lang_bitmap bmap = t2->stok_un.stok_num;
2616 next_state_tokens (3);
2617 t0 = t1 = t2 = NULL;
2618 t0 = peek_state_token (0);
2619 t1 = peek_state_token (1);
2620 if (state_token_kind (t0) == STOK_STRING
2621 && state_token_kind (t1) == STOK_RIGHTPAR)
2623 const char *fnam = t0->stok_un.stok_string;
2624 /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
2625 input_file *curgt = NULL;
2626 if (issrcfile)
2628 static const char dirsepstr[2] =
2629 { DIR_SEPARATOR, (char) 0 };
2630 char *fullpath = concat (srcdir, dirsepstr, fnam, NULL);
2631 curgt = input_file_by_name (fullpath);
2632 free (fullpath);
2634 else
2635 curgt = input_file_by_name (fnam);
2636 set_lang_bitmap (curgt, bmap);
2637 gt_files[i] = curgt;
2638 next_state_tokens (2);
2640 else
2641 fatal_reading_state (t0,
2642 "bad file in !fileslist of state file");
2644 else
2645 fatal_reading_state (t0,
2646 "expecting file in !fileslist of state file");
2648 t0 = peek_state_token (0);
2649 if (!state_token_kind (t0) == STOK_RIGHTPAR)
2650 fatal_reading_state (t0, "missing ) for !fileslist in state file");
2651 next_state_tokens (1);
2653 else
2654 fatal_reading_state (t0, "missing !fileslist in state file");
2658 /* Read the trailer. */
2659 static void
2660 read_state_trailer (void)
2662 struct state_token_st *t0 = peek_state_token (0);
2663 struct state_token_st *t1 = peek_state_token (1);
2664 struct state_token_st *t2 = peek_state_token (2);
2666 if (state_token_kind (t0) == STOK_LEFTPAR
2667 && state_token_is_name (t1, "!endfile")
2668 && state_token_kind (t2) == STOK_RIGHTPAR)
2669 next_state_tokens (3);
2670 else
2671 fatal_reading_state (t0, "missing !endfile in state file");
2675 /* Utility functions for the state_seen_types hash table. */
2676 static unsigned
2677 hash_type_number (const void *ty)
2679 const struct type *type = (const struct type *) ty;
2681 return type->state_number;
2684 static int
2685 equals_type_number (const void *ty1, const void *ty2)
2687 const struct type *type1 = (const struct type *) ty1;
2688 const struct type *type2 = (const struct type *) ty2;
2690 return type1->state_number == type2->state_number;
2693 static int
2694 string_eq (const void *a, const void *b)
2696 const char *a0 = (const char *)a;
2697 const char *b0 = (const char *)b;
2699 return (strcmp (a0, b0) == 0);
2703 /* The function reading the state, called by main from gengtype.c. */
2704 void
2705 read_state (const char *path)
2707 state_file = fopen (path, "r");
2708 if (state_file == NULL)
2709 fatal ("Failed to open state file %s for reading [%s]", path,
2710 xstrerror (errno));
2711 state_path = path;
2712 state_line = 1;
2714 if (verbosity_level >= 1)
2716 printf ("%s reading state file %s;", progname, state_path);
2717 if (verbosity_level >= 2)
2718 putchar ('\n');
2719 fflush (stdout);
2722 state_seen_types =
2723 htab_create (2017, hash_type_number, equals_type_number, NULL);
2724 state_ident_tab =
2725 htab_create (4027, htab_hash_string, string_eq, NULL);
2726 read_state_version (version_string);
2727 read_state_srcdir ();
2728 read_state_languages ();
2729 read_state_files_list ();
2730 read_state_structures (&structures);
2731 if (ferror (state_file))
2732 fatal_reading_state_printf
2733 (NULL_STATE_TOKEN, "input error while reading state [%s]",
2734 xstrerror (errno));
2735 read_state_typedefs (&typedefs);
2736 read_state_param_structs (&param_structs);
2737 read_state_variables (&variables);
2738 read_state_trailer ();
2740 if (verbosity_level >= 1)
2742 printf ("%s read %ld bytes.\n", progname, ftell (state_file));
2743 fflush (stdout);
2746 if (fclose (state_file))
2747 fatal ("failed to close read state file %s [%s]",
2748 path, xstrerror (errno));
2749 state_file = NULL;
2750 state_path = NULL;
2753 /* End of file gengtype-state.c. */