gcc/ChangeLog
[official-gcc.git] / gcc / gengtype-state.c
blobc6ecc247a67bf170a63849f664f02114f471a8e0
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 "double-int.h"
34 #include "hashtab.h"
35 #include "version.h" /* For version_string & pkgversion_string. */
36 #include "obstack.h"
37 #include "gengtype.h"
41 /* Gives the file location of a type, if any. */
42 static inline struct fileloc*
43 type_lineloc (const_type_p ty)
45 if (!ty)
46 return NULL;
47 switch (ty->kind)
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 ();
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. */
74 /* The lexical kind of each lispy token. */
75 enum state_token_en
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. */
86 /* Structure and hash-table used to share identifiers or names. */
87 struct state_ident_st
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 */
94 static htab_t state_ident_tab;
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
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! */
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 */
121 stok_un;
127 #define NULL_STATE_TOKEN (struct state_token_st*)0
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;
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 */
140 /* A class for writing out s-expressions, keeping track of newlines and
141 nested indentation. */
142 class s_expr_writer
144 public:
145 s_expr_writer();
147 void write_new_line ();
148 void write_any_indent (int leading_spaces);
150 void begin_s_expr (const char *tag);
151 void end_s_expr ();
153 private:
154 int indent_amount_;
155 int had_recent_newline_;
156 }; // class s_expr_writer
158 /* A class for writing out "gtype.state". */
159 class state_writer : public s_expr_writer
161 public:
162 state_writer();
164 private:
165 void write_state_fileloc (struct fileloc *floc);
166 void write_state_fields (pair_p fields);
167 void write_state_a_string (const char *s);
168 void write_state_string_option (options_p current);
169 void write_state_type_option (options_p current);
170 void write_state_nested_option (options_p current);
171 void write_state_option (options_p current);
172 void write_state_options (options_p opt);
173 void write_state_lang_bitmap (lang_bitmap bitmap);
174 void write_state_version (const char *version);
175 void write_state_scalar_type (type_p current);
176 void write_state_string_type (type_p current);
177 void write_state_undefined_type (type_p current);
178 void write_state_struct_union_type (type_p current, const char *kindstr);
179 void write_state_struct_type (type_p current);
180 void write_state_user_struct_type (type_p current);
181 void write_state_union_type (type_p current);
182 void write_state_lang_struct_type (type_p current);
183 void write_state_param_struct_type (type_p current);
184 void write_state_pointer_type (type_p current);
185 void write_state_array_type (type_p current);
186 void write_state_gc_used (enum gc_used_enum gus);
187 void write_state_common_type_content (type_p current);
188 void write_state_type (type_p current);
189 void write_state_pair (pair_p current);
190 int write_state_pair_list (pair_p list);
191 void write_state_typedefs (void);
192 void write_state_structures (void);
193 void write_state_param_structs (void);
194 void write_state_variables (void);
195 void write_state_srcdir (void);
196 void write_state_files_list (void);
197 void write_state_languages (void);
199 friend void write_state (const char *state_path);
201 private:
202 /* Counter of written types. */
203 int state_written_type_count;
204 }; // class state_writer
207 /* class s_expr_writer's trivial constructor. */
208 s_expr_writer::s_expr_writer()
209 : indent_amount_(0),
210 had_recent_newline_(0)
214 /* Write a newline to the output file, merging adjacent newlines. */
215 void
216 s_expr_writer::write_new_line (void)
218 /* Don't add a newline if we've just had one. */
219 if (!had_recent_newline_)
221 fprintf (state_file, "\n");
222 had_recent_newline_ = 1;
226 /* If we've just had a newline, write the indentation amount, potentially
227 omitting some spaces.
229 LEADING_SPACES exists to support code that writes strings with leading
230 spaces (e.g " foo") which might occur within a line, or could be the first
231 thing on a line. By passing leading_spaces == 1, when such a string is the
232 first thing on a line, write_any_indent () swallows the successive
233 leading spaces into the indentation so that the "foo" begins at the expected
234 column. */
235 void
236 s_expr_writer::write_any_indent (int leading_spaces)
238 int i;
239 int amount = indent_amount_ - leading_spaces;
240 if (had_recent_newline_)
241 for (i = 0; i < amount; i++)
242 fprintf (state_file, " ");
243 had_recent_newline_ = 0;
246 /* Write the beginning of a new s-expresion e.g. "(!foo "
247 The writer automatically adds whitespace to show the hierachical
248 structure of the expressions, so each one starts on a new line,
249 and any within it will be at an increased indentation level. */
250 void
251 s_expr_writer::begin_s_expr (const char *tag)
253 write_new_line ();
254 write_any_indent (0);
255 fprintf (state_file, "(!%s ", tag);
256 indent_amount_++;
259 /* Write out the end of an s-expression: any necssessary indentation,
260 a closing parenthesis, and a new line. */
261 void
262 s_expr_writer::end_s_expr (void)
264 indent_amount_--;
265 write_any_indent (0);
266 fprintf (state_file, ")");
267 write_new_line ();
271 /* class state_writer's trivial constructor. */
272 state_writer::state_writer()
273 : s_expr_writer(),
274 state_written_type_count(0)
279 /* Fatal error messages when reading the state. They are extremely
280 unlikely, and only appear when this gengtype-state.c file is buggy,
281 or when reading a gengtype state which was not generated by the
282 same version of gengtype or GCC. */
285 /* Fatal message while reading state. */
286 static inline void
287 fatal_reading_state (struct state_token_st* tok, const char*msg)
289 if (tok)
290 fatal ("%s:%d:%d: Invalid state file; %s",
291 tok->stok_file, tok->stok_line, tok->stok_col,
292 msg);
293 else
294 fatal ("%s:%d: Invalid state file; %s",
295 state_path, state_line, msg);
299 /* Fatal printf-like message while reading state. This can't be a
300 function, because there is no way to pass a va_arg to a variant of
301 fatal. */
302 #define fatal_reading_state_printf(Tok,Fmt,...) do { \
303 struct state_token_st* badtok = Tok; \
304 if (badtok) \
305 fatal ("%s:%d:%d: Invalid state file; " Fmt, \
306 badtok->stok_file, \
307 badtok->stok_line, \
308 badtok->stok_col, __VA_ARGS__); \
309 else \
310 fatal ("%s:%d: Invalid state file; " Fmt, \
311 state_path, state_line, __VA_ARGS__); \
312 } while(0)
315 /* Find or allocate an identifier in our name hash table. */
316 static struct state_ident_st *
317 state_ident_by_name (const char *name, enum insert_option optins)
319 PTR *slot = NULL;
320 int namlen = 0;
321 struct state_ident_st *stid = NULL;
323 if (!name || !name[0])
324 return NULL;
326 slot = htab_find_slot (state_ident_tab, name, optins);
327 if (!slot)
328 return NULL;
330 namlen = strlen (name);
331 stid =
332 (struct state_ident_st *) xmalloc (sizeof (struct state_ident_st) +
333 namlen);
334 memset (stid, 0, sizeof (struct state_ident_st) + namlen);
335 strcpy (stid->stid_name, name);
336 *slot = stid;
338 return stid;
341 /* Our token lexer is heavily inspired by MELT's lexer, and share some
342 code with the file gcc/melt-runtime.c of the GCC MELT branch! We
343 really want the gengtype state to be easily parsable by MELT. This
344 is a usual lispy lexing routine, dealing with spaces and comments,
345 numbers, parenthesis, names, strings. */
346 static struct state_token_st *
347 read_a_state_token (void)
349 int c = 0;
350 long curoff = 0;
351 struct state_token_st *tk = NULL;
353 again: /* Read again, e.g. after a comment or spaces. */
354 c = getc (state_file);
355 if (c == EOF)
356 return NULL;
358 /* Handle spaces, count lines. */
359 if (c == '\n')
361 state_line++;
362 state_bol = curoff = ftell (state_file);
363 goto again;
365 if (ISSPACE (c))
366 goto again;
367 /* Skip comments starting with semi-colon. */
368 if (c == ';')
372 c = getc (state_file);
374 while (c > 0 && c != '\n');
375 if (c == '\n')
377 state_line++;
378 state_bol = curoff = ftell (state_file);
380 goto again;
382 /* Read signed numbers. */
383 if (ISDIGIT (c) || c == '-' || c == '+')
384 { /* number */
385 int n = 0;
386 ungetc (c, state_file);
387 curoff = ftell (state_file);
388 if (fscanf (state_file, "%d", &n) <= 0)
389 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error in number");
390 tk = XCNEW (struct state_token_st);
391 tk->stok_kind = STOK_INTEGER;
392 tk->stok_line = state_line;
393 tk->stok_col = curoff - state_bol;
394 tk->stok_file = state_path;
395 tk->stok_next = NULL;
396 tk->stok_un.stok_num = n;
398 return tk;
400 /* Read an opening left parenthesis. */
401 else if (c == '(')
403 curoff = ftell (state_file);
404 tk = XCNEW (struct state_token_st);
405 tk->stok_kind = STOK_LEFTPAR;
406 tk->stok_line = state_line;
407 tk->stok_col = curoff - state_bol;
408 tk->stok_file = state_path;
409 tk->stok_next = NULL;
411 return tk;
413 /* Read an closing right parenthesis. */
414 else if (c == ')')
416 curoff = ftell (state_file);
417 tk = XCNEW (struct state_token_st);
418 tk->stok_kind = STOK_RIGHTPAR;
419 tk->stok_line = state_line;
420 tk->stok_col = curoff - state_bol;
421 tk->stok_file = state_path;
422 tk->stok_next = NULL;
424 return tk;
426 /* Read identifiers, using an obstack. */
427 else if (ISALPHA (c) || c == '_' || c == '$' || c == '!' || c == '#')
429 struct obstack id_obstack;
430 struct state_ident_st *sid = NULL;
431 char *ids = NULL;
432 obstack_init (&id_obstack);
433 curoff = ftell (state_file);
434 while (ISALNUM (c) || c == '_' || c == '$' || c == '!' || c == '#')
436 obstack_1grow (&id_obstack, c);
437 c = getc (state_file);
438 if (c < 0)
439 break;
441 if (c >= 0)
442 ungetc (c, state_file);
443 obstack_1grow (&id_obstack, (char) 0);
444 ids = XOBFINISH (&id_obstack, char *);
445 sid = state_ident_by_name (ids, INSERT);
446 obstack_free (&id_obstack, NULL);
447 ids = NULL;
448 tk = XCNEW (struct state_token_st);
449 tk->stok_kind = STOK_NAME;
450 tk->stok_line = state_line;
451 tk->stok_col = curoff - state_bol;
452 tk->stok_file = state_path;
453 tk->stok_next = NULL;
454 tk->stok_un.stok_ident = sid;
456 return tk;
458 /* Read a string, dealing with escape sequences a la C! */
459 else if (c == '"')
461 char *cstr = NULL;
462 int cslen = 0;
463 struct obstack bstring_obstack;
464 obstack_init (&bstring_obstack);
465 curoff = ftell (state_file);
466 while ((c = getc (state_file)) != '"' && c >= 0)
468 if (ISPRINT (c) && c != '\\')
469 obstack_1grow (&bstring_obstack, (char) c);
470 else if (ISSPACE (c) && c != '\n')
471 obstack_1grow (&bstring_obstack, (char) c);
472 else if (c == '\\')
474 c = getc (state_file);
475 switch (c)
477 case 'a':
478 obstack_1grow (&bstring_obstack, '\a');
479 c = getc (state_file);
480 break;
481 case 'b':
482 obstack_1grow (&bstring_obstack, '\b');
483 c = getc (state_file);
484 break;
485 case 't':
486 obstack_1grow (&bstring_obstack, '\t');
487 c = getc (state_file);
488 break;
489 case 'n':
490 obstack_1grow (&bstring_obstack, '\n');
491 c = getc (state_file);
492 break;
493 case 'v':
494 obstack_1grow (&bstring_obstack, '\v');
495 c = getc (state_file);
496 break;
497 case 'f':
498 obstack_1grow (&bstring_obstack, '\f');
499 c = getc (state_file);
500 break;
501 case 'r':
502 obstack_1grow (&bstring_obstack, '\r');
503 c = getc (state_file);
504 break;
505 case '"':
506 obstack_1grow (&bstring_obstack, '\"');
507 c = getc (state_file);
508 break;
509 case '\\':
510 obstack_1grow (&bstring_obstack, '\\');
511 c = getc (state_file);
512 break;
513 case ' ':
514 obstack_1grow (&bstring_obstack, ' ');
515 c = getc (state_file);
516 break;
517 case 'x':
519 unsigned int cx = 0;
520 if (fscanf (state_file, "%02x", &cx) > 0 && cx > 0)
521 obstack_1grow (&bstring_obstack, cx);
522 else
523 fatal_reading_state
524 (NULL_STATE_TOKEN,
525 "Lexical error in string hex escape");
526 c = getc (state_file);
527 break;
529 default:
530 fatal_reading_state
531 (NULL_STATE_TOKEN,
532 "Lexical error - unknown string escape");
535 else
536 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error...");
538 if (c != '"')
539 fatal_reading_state (NULL_STATE_TOKEN, "Unterminated string");
540 obstack_1grow (&bstring_obstack, '\0');
541 cstr = XOBFINISH (&bstring_obstack, char *);
542 cslen = strlen (cstr);
543 tk = (struct state_token_st *)
544 xcalloc (sizeof (struct state_token_st) + cslen, 1);
545 tk->stok_kind = STOK_STRING;
546 tk->stok_line = state_line;
547 tk->stok_col = curoff - state_bol;
548 tk->stok_file = state_path;
549 tk->stok_next = NULL;
550 strcpy (tk->stok_un.stok_string, cstr);
551 obstack_free (&bstring_obstack, NULL);
553 return tk;
555 /* Got an unexpected character. */
556 fatal_reading_state_printf
557 (NULL_STATE_TOKEN,
558 "Lexical error at offset %ld - bad character \\%03o = '%c'",
559 ftell (state_file), c, c);
562 /* Used for lexical look-ahead. Retrieves the lexical token of rank
563 DEPTH, starting with 0 when reading the state file. Gives null on
564 end of file. */
565 static struct state_token_st *
566 peek_state_token (int depth)
568 int remdepth = depth;
569 struct state_token_st **ptoken = &state_token;
570 struct state_token_st *tok = NULL;
572 while (remdepth >= 0)
574 if (*ptoken == NULL)
576 *ptoken = tok = read_a_state_token ();
577 if (tok == NULL)
578 return NULL;
580 tok = *ptoken;
581 ptoken = &((*ptoken)->stok_next);
582 remdepth--;
585 return tok;
588 /* Consume the next DEPTH tokens and free them. */
589 static void
590 next_state_tokens (int depth)
592 struct state_token_st *n;
594 while (depth > 0)
596 if (state_token != NULL)
598 n = state_token->stok_next;
599 free (state_token);
600 state_token = n;
602 else
603 fatal_reading_state (NULL_STATE_TOKEN, "Tokens stack empty");
605 depth--;
609 /* Safely retrieve the lexical kind of a token. */
610 static inline enum state_token_en
611 state_token_kind (struct state_token_st *p)
613 if (p == NULL)
614 return STOK_NONE;
615 else
616 return p->stok_kind;
619 /* Test if a token is a given name i.e. an identifier. */
620 static inline bool
621 state_token_is_name (struct state_token_st *p, const char *name)
623 if (p == NULL)
624 return false;
626 if (p->stok_kind != STOK_NAME)
627 return false;
629 return !strcmp (p->stok_un.stok_ident->stid_name, name);
633 /* Following routines are useful for serializing datas.
635 * We want to serialize :
636 * - typedefs list
637 * - structures list
638 * - param_structs list
639 * - variables list
641 * So, we have one routine for each kind of data. The main writing
642 * routine is write_state. The main reading routine is
643 * read_state. Most writing routines write_state_FOO have a
644 * corresponding reading routine read_state_FOO. Reading is done in a
645 * recursive descending way, and any read error is fatal.
648 /* When reading the state, we need to remember the previously seen
649 types by their state_number, since GTY-ed types are usually
650 shared. */
651 static htab_t state_seen_types;
653 /* Return the length of a linked list made of pairs. */
654 static int pair_list_length (pair_p list);
656 /* Compute the length of a list of pairs, starting from the first
657 one. */
658 static int
659 pair_list_length (pair_p list)
661 int nbpair = 0;
662 pair_p l = NULL;
663 for (l = list; l; l = l->next)
664 nbpair++;
665 return nbpair;
668 /* Write a file location. Files relative to $(srcdir) are quite
669 frequent and are handled specially. This ensures that two gengtype
670 state file-s produced by gengtype on the same GCC source tree are
671 very similar and can be reasonably compared with diff, even if the
672 two GCC source trees have different absolute paths. */
673 void
674 state_writer::write_state_fileloc (struct fileloc *floc)
677 if (floc != NULL && floc->line > 0)
679 const char *srcrelpath = NULL;
680 gcc_assert (floc->file != NULL);
681 /* Most of the files are inside $(srcdir) so it is worth to
682 handle them specially. */
683 srcrelpath = get_file_srcdir_relative_path (floc->file);
684 if (srcrelpath != NULL)
686 begin_s_expr ("srcfileloc");
687 write_state_a_string (srcrelpath);
689 else
691 begin_s_expr ("fileloc");
692 write_state_a_string (get_input_file_name (floc->file));
694 fprintf (state_file, " %d", floc->line);
695 end_s_expr ();
697 else
698 fprintf (state_file, "nil ");
701 /* Write a list of fields. */
702 void
703 state_writer::write_state_fields (pair_p fields)
705 int nbfields = pair_list_length (fields);
706 int nbpairs = 0;
707 begin_s_expr ("fields");
708 fprintf (state_file, "%d ", nbfields);
709 nbpairs = write_state_pair_list (fields);
710 gcc_assert (nbpairs == nbfields);
711 end_s_expr ();
714 /* Write a null-terminated string in our lexical convention, very
715 similar to the convention of C. */
716 void
717 state_writer::write_state_a_string (const char *s)
719 char c;
721 write_any_indent (1);
723 fputs (" \"", state_file);
724 for (; *s != 0; s++)
726 c = *s;
727 switch (c)
729 case '\a':
730 fputs ("\\a", state_file);
731 break;
732 case '\b':
733 fputs ("\\b", state_file);
734 break;
735 case '\t':
736 fputs ("\\t", state_file);
737 break;
738 case '\n':
739 fputs ("\\n", state_file);
740 break;
741 case '\v':
742 fputs ("\\v", state_file);
743 break;
744 case '\f':
745 fputs ("\\f", state_file);
746 break;
747 case '\r':
748 fputs ("\\r", state_file);
749 break;
750 case '\"':
751 fputs ("\\\"", state_file);
752 break;
753 case '\\':
754 fputs ("\\\\", state_file);
755 break;
756 default:
757 if (ISPRINT (c))
758 putc (c, state_file);
759 else
760 fprintf (state_file, "\\x%02x", (unsigned) c);
763 fputs ("\"", state_file);
766 /* Our option-s have three kinds, each with its writer. */
767 void
768 state_writer::write_state_string_option (options_p current)
770 write_any_indent (0);
771 fprintf (state_file, "string ");
772 if (current->info.string != NULL)
773 write_state_a_string (current->info.string);
774 else
775 fprintf (state_file, " nil ");
778 void
779 state_writer::write_state_type_option (options_p current)
781 write_any_indent (0);
782 fprintf (state_file, "type ");
783 write_state_type (current->info.type);
786 void
787 state_writer::write_state_nested_option (options_p current)
789 write_any_indent (0);
790 fprintf (state_file, "nested ");
791 write_state_type (current->info.nested->type);
792 if (current->info.nested->convert_from != NULL)
793 write_state_a_string (current->info.nested->convert_from);
794 else
796 write_any_indent (1);
797 fprintf (state_file, " nil ");
800 if (current->info.nested->convert_to != NULL)
801 write_state_a_string (current->info.nested->convert_to);
802 else
804 write_any_indent (1);
805 fprintf (state_file, " nil ");
809 void
810 state_writer::write_state_option (options_p current)
812 begin_s_expr ("option");
814 write_any_indent (0);
815 if (current->name != NULL)
816 fprintf (state_file, "%s ", current->name);
817 else
818 fprintf (state_file, "nil ");
820 switch (current->kind)
822 case OPTION_STRING:
823 write_state_string_option (current);
824 break;
825 case OPTION_TYPE:
826 write_state_type_option (current);
827 break;
828 case OPTION_NESTED:
829 write_state_nested_option (current);
830 break;
831 default:
832 fatal ("Option tag unknown");
835 /* Terminate the "option" s-expression. */
836 end_s_expr ();
841 /* Write a list of GTY options. */
842 void
843 state_writer::write_state_options (options_p opt)
845 options_p current;
847 if (opt == NULL)
849 write_any_indent (0);
850 fprintf (state_file, "nil ");
851 return;
854 begin_s_expr ("options");
855 for (current = opt; current != NULL; current = current->next)
856 write_state_option (current);
857 end_s_expr ();
861 /* Write a bitmap representing a set of GCC front-end languages. */
862 void
863 state_writer::write_state_lang_bitmap (lang_bitmap bitmap)
865 write_any_indent (0);
866 fprintf (state_file, "%d ", (int) bitmap);
869 /* Write version information. */
870 void
871 state_writer::write_state_version (const char *version)
873 begin_s_expr ("version");
874 write_state_a_string (version);
875 end_s_expr ();
878 /* Write a scalar type. We have only two of these. */
879 void
880 state_writer::write_state_scalar_type (type_p current)
882 write_any_indent (0);
883 if (current == &scalar_nonchar)
884 fprintf (state_file, "scalar_nonchar ");
885 else if (current == &scalar_char)
886 fprintf (state_file, "scalar_char ");
887 else
888 fatal ("Unexpected type in write_state_scalar_type");
890 write_state_common_type_content (current);
893 /* Write the string type. There is only one such thing! */
894 void
895 state_writer::write_state_string_type (type_p current)
897 if (current == &string_type)
899 write_any_indent (0);
900 fprintf (state_file, "string ");
901 write_state_common_type_content (current);
903 else
904 fatal ("Unexpected type in write_state_string_type");
907 /* Write an undefined type. */
908 void
909 state_writer::write_state_undefined_type (type_p current)
911 DBGPRINTF ("undefined type @ %p #%d '%s'", (void *) current,
912 current->state_number, current->u.s.tag);
913 write_any_indent (0);
914 fprintf (state_file, "undefined ");
915 gcc_assert (current->gc_used == GC_UNUSED);
916 write_state_common_type_content (current);
917 if (current->u.s.tag != NULL)
918 write_state_a_string (current->u.s.tag);
919 else
921 write_any_indent (0);
922 fprintf (state_file, "nil");
925 write_state_fileloc (type_lineloc (current));
929 /* Common code to write structure like types. */
930 void
931 state_writer::write_state_struct_union_type (type_p current,
932 const char *kindstr)
934 DBGPRINTF ("%s type @ %p #%d '%s'", kindstr, (void *) current,
935 current->state_number, current->u.s.tag);
936 write_any_indent (0);
937 fprintf (state_file, "%s ", kindstr);
938 write_state_common_type_content (current);
939 if (current->u.s.tag != NULL)
940 write_state_a_string (current->u.s.tag);
941 else
943 write_any_indent (0);
944 fprintf (state_file, "nil");
947 write_state_fileloc (type_lineloc (current));
948 write_state_fields (current->u.s.fields);
949 write_state_options (current->u.s.opt);
950 write_state_lang_bitmap (current->u.s.bitmap);
954 /* Write a GTY struct type. */
955 void
956 state_writer::write_state_struct_type (type_p current)
958 write_state_struct_union_type (current, "struct");
959 write_state_type (current->u.s.lang_struct);
962 /* Write a GTY user-defined struct type. */
963 void
964 state_writer::write_state_user_struct_type (type_p current)
966 DBGPRINTF ("user_struct type @ %p #%d '%s'", (void *) current,
967 current->state_number, current->u.s.tag);
968 write_any_indent (0);
969 fprintf (state_file, "user_struct ");
970 write_state_common_type_content (current);
971 if (current->u.s.tag != NULL)
972 write_state_a_string (current->u.s.tag);
973 else
975 write_any_indent (0);
976 fprintf (state_file, "nil");
978 write_state_fileloc (type_lineloc (current));
979 write_state_fields (current->u.s.fields);
982 /* write a GTY union type. */
983 void
984 state_writer::write_state_union_type (type_p current)
986 write_state_struct_union_type (current, "union");
987 write_state_type (current->u.s.lang_struct);
990 /* Write a lang_struct type. This is tricky and was painful to debug,
991 we deal with the next field specifically within their lang_struct
992 subfield, which points to a linked list of homonumous types.
993 Change this function with extreme care, see also
994 read_state_lang_struct_type. */
995 void
996 state_writer::write_state_lang_struct_type (type_p current)
998 int nbhomontype = 0;
999 type_p hty = NULL;
1000 const char *homoname = 0;
1001 write_state_struct_union_type (current, "lang_struct");
1002 /* lang_struct-ures are particularly tricky, since their
1003 u.s.lang_struct field gives a list of homonymous struct-s or
1004 union-s! */
1005 DBGPRINTF ("lang_struct @ %p #%d", (void *) current, current->state_number);
1006 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
1008 nbhomontype++;
1009 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype,
1010 (void *) hty, hty->state_number, hty->u.s.tag);
1011 /* Every member of the homonymous list should have the same tag. */
1012 gcc_assert (union_or_struct_p (hty));
1013 gcc_assert (hty->u.s.lang_struct == current);
1014 if (!homoname)
1015 homoname = hty->u.s.tag;
1016 gcc_assert (strcmp (homoname, hty->u.s.tag) == 0);
1018 begin_s_expr ("homotypes");
1019 fprintf (state_file, "%d", nbhomontype);
1020 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
1021 write_state_type (hty);
1022 end_s_expr ();
1025 /* Write a parametrized structure GTY type. */
1026 void
1027 state_writer::write_state_param_struct_type (type_p current)
1029 int i;
1031 write_any_indent (0);
1032 fprintf (state_file, "param_struct ");
1033 write_state_common_type_content (current);
1034 write_state_type (current->u.param_struct.stru);
1035 for (i = 0; i < NUM_PARAM; i++)
1037 if (current->u.param_struct.param[i] != NULL)
1038 write_state_type (current->u.param_struct.param[i]);
1039 else
1041 write_any_indent (0);
1042 fprintf (state_file, "nil ");
1045 write_state_fileloc (&current->u.param_struct.line);
1048 /* Write a pointer type. */
1049 void
1050 state_writer::write_state_pointer_type (type_p current)
1052 write_any_indent (0);
1053 fprintf (state_file, "pointer ");
1054 write_state_common_type_content (current);
1055 write_state_type (current->u.p);
1058 /* Write an array type. */
1059 void
1060 state_writer::write_state_array_type (type_p current)
1062 write_any_indent (0);
1063 fprintf (state_file, "array ");
1064 write_state_common_type_content (current);
1065 if (current->u.a.len != NULL)
1066 write_state_a_string (current->u.a.len);
1067 else
1069 write_any_indent (1);
1070 fprintf (state_file, " nil");
1073 write_any_indent (1);
1074 fprintf (state_file, " ");
1075 write_state_type (current->u.a.p);
1078 /* Write the gc_used information. */
1079 void
1080 state_writer::write_state_gc_used (enum gc_used_enum gus)
1082 write_any_indent (1);
1083 switch (gus)
1085 case GC_UNUSED:
1086 fprintf (state_file, " gc_unused");
1087 break;
1088 case GC_USED:
1089 fprintf (state_file, " gc_used");
1090 break;
1091 case GC_MAYBE_POINTED_TO:
1092 fprintf (state_file, " gc_maybe_pointed_to");
1093 break;
1094 case GC_POINTED_TO:
1095 fprintf (state_file, " gc_pointed_to");
1096 break;
1097 default:
1098 gcc_unreachable ();
1102 /* Utility routine to write the common content of all types. Notice
1103 that the next field is *not* written on purpose. */
1104 void
1105 state_writer::write_state_common_type_content (type_p current)
1107 write_any_indent (0);
1108 fprintf (state_file, "%d ", current->state_number);
1109 /* We do not write the next type, because list of types are
1110 explicitly written. However, lang_struct are special in that
1111 respect. See function write_state_lang_struct_type for more. */
1112 write_state_type (current->pointer_to);
1113 write_state_gc_used (current->gc_used);
1117 /* The important and recursive routine writing GTY types as understood
1118 by gengtype. Types which have a positive state_number have already
1119 been seen and written. */
1120 void
1121 state_writer::write_state_type (type_p current)
1123 write_any_indent (0);
1124 if (current == NULL)
1126 fprintf (state_file, "nil ");
1127 return;
1130 begin_s_expr ("type");
1132 if (current->state_number > 0)
1134 write_any_indent (0);
1135 fprintf (state_file, "already_seen %d", current->state_number);
1137 else
1139 state_written_type_count++;
1140 DBGPRINTF ("writing type #%d @%p old number %d", state_written_type_count,
1141 (void *) current, current->state_number);
1142 current->state_number = state_written_type_count;
1143 switch (current->kind)
1145 case TYPE_NONE:
1146 gcc_unreachable ();
1147 case TYPE_UNDEFINED:
1148 write_state_undefined_type (current);
1149 break;
1150 case TYPE_STRUCT:
1151 write_state_struct_type (current);
1152 break;
1153 case TYPE_USER_STRUCT:
1154 write_state_user_struct_type (current);
1155 break;
1156 case TYPE_UNION:
1157 write_state_union_type (current);
1158 break;
1159 case TYPE_POINTER:
1160 write_state_pointer_type (current);
1161 break;
1162 case TYPE_ARRAY:
1163 write_state_array_type (current);
1164 break;
1165 case TYPE_LANG_STRUCT:
1166 write_state_lang_struct_type (current);
1167 break;
1168 case TYPE_PARAM_STRUCT:
1169 write_state_param_struct_type (current);
1170 break;
1171 case TYPE_SCALAR:
1172 write_state_scalar_type (current);
1173 break;
1174 case TYPE_STRING:
1175 write_state_string_type (current);
1176 break;
1180 /* Terminate the "type" s-expression. */
1181 end_s_expr ();
1185 /* Write a pair. */
1186 void
1187 state_writer::write_state_pair (pair_p current)
1189 if (current == NULL)
1191 write_any_indent (0);
1192 fprintf (state_file, "nil)");
1193 return;
1196 begin_s_expr ("pair");
1198 if (current->name != NULL)
1199 write_state_a_string (current->name);
1200 else
1201 write_state_a_string ("nil");
1203 write_state_type (current->type);
1204 write_state_fileloc (&(current->line));
1205 write_state_options (current->opt);
1207 /* Terminate the "pair" s-expression. */
1208 end_s_expr ();
1211 /* Write a pair list and return the number of pairs written. */
1213 state_writer::write_state_pair_list (pair_p list)
1215 int nbpair = 0;
1216 pair_p current;
1218 for (current = list; current != NULL; current = current->next)
1220 write_state_pair (current);
1221 nbpair++;
1223 return nbpair;
1227 /* When writing imported linked lists, like typedefs, structures,
1228 param_structs, ... we count their length first and write it. These
1229 eases the reading, and enables an extra verification on the number
1230 of actually read items. */
1232 /* Write our typedefs. */
1233 void
1234 state_writer::write_state_typedefs (void)
1236 int nbtypedefs = pair_list_length (typedefs);
1237 int nbpairs = 0;
1238 begin_s_expr ("typedefs");
1239 fprintf (state_file, "%d", nbtypedefs);
1240 nbpairs = write_state_pair_list (typedefs);
1241 gcc_assert (nbpairs == nbtypedefs);
1242 end_s_expr ();
1243 if (verbosity_level >= 2)
1244 printf ("%s wrote %d typedefs\n", progname, nbtypedefs);
1247 /* Write our structures. */
1248 void
1249 state_writer::write_state_structures (void)
1251 int nbstruct = 0;
1252 type_p current;
1254 for (current = structures; current != NULL; current = current->next)
1255 nbstruct++;
1257 begin_s_expr ("structures");
1258 fprintf (state_file, "%d", nbstruct);
1260 for (current = structures; current != NULL; current = current->next)
1262 write_new_line ();
1263 write_state_type (current);
1266 /* Terminate the "structures" s-expression. */
1267 end_s_expr ();
1268 if (verbosity_level >= 2)
1269 printf ("%s wrote %d structures in state\n", progname, nbstruct);
1272 /* Write our param_struct-s. */
1273 void
1274 state_writer::write_state_param_structs (void)
1276 int nbparamstruct = 0;
1277 type_p current;
1279 for (current = param_structs; current != NULL; current = current->next)
1280 nbparamstruct++;
1282 begin_s_expr ("param_structs");
1283 fprintf (state_file, "%d", nbparamstruct);
1285 for (current = param_structs; current != NULL; current = current->next)
1286 write_state_type (current);
1288 end_s_expr ();
1291 /* Write our variables. */
1292 void
1293 state_writer::write_state_variables (void)
1295 int nbvars = pair_list_length (variables);
1296 int nbpairs = 0;
1297 begin_s_expr ("variables");
1298 fprintf (state_file, "%d", nbvars);
1299 nbpairs = write_state_pair_list (variables);
1300 gcc_assert (nbpairs == nbvars);
1301 end_s_expr ();
1302 if (verbosity_level >= 2)
1303 printf ("%s wrote %d variables.\n", progname, nbvars);
1306 /* Write the source directory. File locations within the source
1307 directory have been written specifically. */
1308 void
1309 state_writer::write_state_srcdir (void)
1311 begin_s_expr ("srcdir");
1312 write_state_a_string (srcdir);
1313 end_s_expr ();
1316 /* Count and write the list of our files. */
1317 void
1318 state_writer::write_state_files_list (void)
1320 int i = 0;
1321 /* Write the list of files with their lang_bitmap. */
1322 begin_s_expr ("fileslist");
1323 fprintf (state_file, "%d", (int) num_gt_files);
1324 for (i = 0; i < (int) num_gt_files; i++)
1326 const char *cursrcrelpath = NULL;
1327 const input_file *curfil = gt_files[i];
1328 /* Most of the files are inside $(srcdir) so it is worth to
1329 handle them specially. */
1330 cursrcrelpath = get_file_srcdir_relative_path (curfil);
1331 if (cursrcrelpath)
1333 begin_s_expr ("srcfile");
1334 fprintf (state_file, "%d ", get_lang_bitmap (curfil));
1335 write_state_a_string (cursrcrelpath);
1337 else
1339 begin_s_expr ("file");
1340 fprintf (state_file, "%d ", get_lang_bitmap (curfil));
1341 write_state_a_string (get_input_file_name (curfil));
1343 /* Terminate the inner s-expression (either "srcfile" or "file"). */
1344 end_s_expr ();
1346 /* Terminate the "fileslist" s-expression. */
1347 end_s_expr ();
1350 /* Write the list of GCC front-end languages. */
1351 void
1352 state_writer::write_state_languages (void)
1354 int i = 0;
1355 begin_s_expr ("languages");
1356 fprintf (state_file, "%d", (int) num_lang_dirs);
1357 for (i = 0; i < (int) num_lang_dirs; i++)
1359 /* Languages names are identifiers, we expect only letters or
1360 underscores or digits in them. In particular, C++ is not a
1361 valid language name, but cp is valid. */
1362 fprintf (state_file, " %s", lang_dir_names[i]);
1364 end_s_expr ();
1367 /* Write the trailer. */
1368 static void
1369 write_state_trailer (void)
1371 /* This test should probably catch IO errors like disk full... */
1372 if (fputs ("\n(!endfile)\n", state_file) == EOF)
1373 fatal ("failed to write state trailer [%s]", xstrerror (errno));
1376 /* The write_state routine is the only writing routine called by main
1377 in gengtype.c. To avoid messing the state if gengtype is
1378 interrupted or aborted, we write a temporary file and rename it
1379 after having written it in totality. */
1380 void
1381 write_state (const char *state_path)
1383 long statelen = 0;
1384 time_t now = 0;
1385 char *temp_state_path = NULL;
1386 char tempsuffix[40];
1387 time (&now);
1389 /* We write a unique temporary file which is renamed when complete
1390 * only. So even if gengtype is interrupted, the written state file
1391 * won't be partially written, since the temporary file is not yet
1392 * renamed in that case. */
1393 memset (tempsuffix, 0, sizeof (tempsuffix));
1394 snprintf (tempsuffix, sizeof (tempsuffix) - 1, "-%ld-%d.tmp", (long) now,
1395 (int) getpid ());
1396 temp_state_path = concat (state_path, tempsuffix, NULL);
1397 state_file = fopen (temp_state_path, "w");
1398 if (state_file == NULL)
1399 fatal ("Failed to open file %s for writing state: %s",
1400 temp_state_path, xstrerror (errno));
1401 if (verbosity_level >= 3)
1402 printf ("%s writing state file %s temporarily in %s\n",
1403 progname, state_path, temp_state_path);
1404 /* This is the first line of the state. Perhaps the file utility
1405 could know about that, so don't change it often. */
1406 fprintf (state_file, ";;;;@@@@ GCC gengtype state\n");
1407 /* Output a few comments for humans. */
1408 fprintf (state_file,
1409 ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
1410 fprintf (state_file,
1411 ";;; The format of this file is tied to a particular version of GCC.\n");
1412 fprintf (state_file,
1413 ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1414 fprintf (state_file,
1415 ";;; This file should be parsed by the same %s which wrote it.\n",
1416 progname);
1418 state_writer sw;
1420 /* The first non-comment significant line gives the version string. */
1421 sw.write_state_version (version_string);
1422 sw.write_state_srcdir ();
1423 sw.write_state_languages ();
1424 sw.write_state_files_list ();
1425 sw.write_state_structures ();
1426 sw.write_state_typedefs ();
1427 sw.write_state_param_structs ();
1428 sw.write_state_variables ();
1429 write_state_trailer ();
1430 statelen = ftell (state_file);
1431 if (ferror (state_file))
1432 fatal ("output error when writing state file %s [%s]",
1433 temp_state_path, xstrerror (errno));
1434 if (fclose (state_file))
1435 fatal ("failed to close state file %s [%s]",
1436 temp_state_path, xstrerror (errno));
1437 if (rename (temp_state_path, state_path))
1438 fatal ("failed to rename %s to state file %s [%s]", temp_state_path,
1439 state_path, xstrerror (errno));
1440 free (temp_state_path);
1442 if (verbosity_level >= 1)
1443 printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1444 progname, state_path, statelen, sw.state_written_type_count);
1448 /** End of writing routines! The corresponding reading routines follow. **/
1452 /* Forward declarations, since some read_state_* functions are
1453 recursive! */
1454 static void read_state_fileloc (struct fileloc *line);
1455 static void read_state_options (options_p *opt);
1456 static void read_state_type (type_p *current);
1457 static void read_state_pair (pair_p *pair);
1458 /* Return the number of pairs actually read. */
1459 static int read_state_pair_list (pair_p *list);
1460 static void read_state_fields (pair_p *fields);
1461 static void read_state_common_type_content (type_p current);
1466 /* Record into the state_seen_types hash-table a type which we are
1467 reading, to enable recursive or circular references to it. */
1468 static void
1469 record_type (type_p type)
1471 PTR *slot;
1473 slot = htab_find_slot (state_seen_types, type, INSERT);
1474 gcc_assert (slot);
1476 *slot = type;
1479 /* Read an already seen type. */
1480 static void
1481 read_state_already_seen_type (type_p *type)
1483 struct state_token_st *t0 = peek_state_token (0);
1485 if (state_token_kind (t0) == STOK_INTEGER)
1487 PTR *slot = NULL;
1488 struct type loctype = { TYPE_SCALAR, 0, 0, 0, GC_UNUSED, {0} };
1490 loctype.state_number = t0->stok_un.stok_num;
1491 slot = htab_find_slot (state_seen_types, &loctype, NO_INSERT);
1492 if (slot == NULL)
1494 fatal_reading_state (t0, "Unknown type");
1497 next_state_tokens (1);
1498 *type = (type_p) *slot;
1500 else
1502 fatal_reading_state (t0, "Bad seen type");
1507 /* Read the scalar_nonchar type. */
1508 static void
1509 read_state_scalar_nonchar_type (type_p *type)
1511 *type = &scalar_nonchar;
1512 read_state_common_type_content (*type);
1516 /* Read the scalar_char type. */
1517 static void
1518 read_state_scalar_char_type (type_p *type)
1520 *type = &scalar_char;
1521 read_state_common_type_content (*type);
1524 /* Read the string_type. */
1525 static void
1526 read_state_string_type (type_p *type)
1528 *type = &string_type;
1529 read_state_common_type_content (*type);
1533 /* Read a lang_bitmap representing a set of GCC front-end languages. */
1534 static void
1535 read_state_lang_bitmap (lang_bitmap *bitmap)
1537 struct state_token_st *t;
1539 t = peek_state_token (0);
1540 if (state_token_kind (t) == STOK_INTEGER)
1542 *bitmap = t->stok_un.stok_num;
1543 next_state_tokens (1);
1545 else
1547 fatal_reading_state (t, "Bad syntax for bitmap");
1552 /* Read an undefined type. */
1553 static void
1554 read_state_undefined_type (type_p type)
1556 struct state_token_st *t0;
1558 type->kind = TYPE_UNDEFINED;
1559 read_state_common_type_content (type);
1560 t0 = peek_state_token (0);
1561 if (state_token_kind (t0) == STOK_STRING)
1563 if (state_token_is_name (t0, "nil"))
1565 type->u.s.tag = NULL;
1566 DBGPRINTF ("read anonymous undefined type @%p #%d",
1567 (void *) type, type->state_number);
1569 else
1571 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1572 DBGPRINTF ("read undefined type @%p #%d '%s'",
1573 (void *) type, type->state_number, type->u.s.tag);
1576 next_state_tokens (1);
1577 read_state_fileloc (&(type->u.s.line));
1579 else
1581 fatal_reading_state (t0, "Bad tag in undefined type");
1586 /* Read a GTY-ed struct type. */
1587 static void
1588 read_state_struct_type (type_p type)
1590 struct state_token_st *t0;
1592 type->kind = TYPE_STRUCT;
1593 read_state_common_type_content (type);
1594 t0 = peek_state_token (0);
1595 if (state_token_kind (t0) == STOK_STRING)
1597 if (state_token_is_name (t0, "nil"))
1599 type->u.s.tag = NULL;
1600 DBGPRINTF ("read anonymous struct type @%p #%d",
1601 (void *) type, type->state_number);
1603 else
1605 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1606 DBGPRINTF ("read struct type @%p #%d '%s'",
1607 (void *) type, type->state_number, type->u.s.tag);
1610 next_state_tokens (1);
1611 read_state_fileloc (&(type->u.s.line));
1612 read_state_fields (&(type->u.s.fields));
1613 read_state_options (&(type->u.s.opt));
1614 read_state_lang_bitmap (&(type->u.s.bitmap));
1615 read_state_type (&(type->u.s.lang_struct));
1617 else
1619 fatal_reading_state (t0, "Bad tag in struct type");
1624 /* Read a GTY-ed user-provided struct TYPE. */
1626 static void
1627 read_state_user_struct_type (type_p type)
1629 struct state_token_st *t0;
1631 type->kind = TYPE_USER_STRUCT;
1632 read_state_common_type_content (type);
1633 t0 = peek_state_token (0);
1634 if (state_token_kind (t0) == STOK_STRING)
1636 if (state_token_is_name (t0, "nil"))
1638 type->u.s.tag = NULL;
1639 DBGPRINTF ("read anonymous struct type @%p #%d",
1640 (void *) type, type->state_number);
1642 else
1644 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1645 DBGPRINTF ("read struct type @%p #%d '%s'",
1646 (void *) type, type->state_number, type->u.s.tag);
1649 next_state_tokens (1);
1650 read_state_fileloc (&(type->u.s.line));
1651 read_state_fields (&(type->u.s.fields));
1653 else
1655 fatal_reading_state (t0, "Bad tag in user-struct type");
1660 /* Read a GTY-ed union type. */
1661 static void
1662 read_state_union_type (type_p type)
1664 struct state_token_st *t0;
1666 type->kind = TYPE_UNION;
1667 read_state_common_type_content (type);
1668 t0 = peek_state_token (0);
1669 if (state_token_kind (t0) == STOK_STRING)
1671 if (state_token_is_name (t0, "nil"))
1673 type->u.s.tag = NULL;
1674 DBGPRINTF ("read anonymous union type @%p #%d",
1675 (void *) type, type->state_number);
1677 else
1679 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1680 DBGPRINTF ("read union type @%p #%d '%s'",
1681 (void *) type, type->state_number, type->u.s.tag);
1683 next_state_tokens (1);
1684 read_state_fileloc (&(type->u.s.line));
1685 read_state_fields (&(type->u.s.fields));
1686 read_state_options (&(type->u.s.opt));
1687 read_state_lang_bitmap (&(type->u.s.bitmap));
1688 read_state_type (&(type->u.s.lang_struct));
1690 else
1691 fatal_reading_state (t0, "Bad tag in union type");
1695 /* Read a GTY-ed pointer type. */
1696 static void
1697 read_state_pointer_type (type_p type)
1699 type->kind = TYPE_POINTER;
1700 read_state_common_type_content (type);
1701 DBGPRINTF ("read pointer type @%p #%d", (void *) type, type->state_number);
1702 read_state_type (&(type->u.p));
1706 /* Read a GTY-ed array type. */
1707 static void
1708 read_state_array_type (type_p type)
1710 struct state_token_st *t0;
1712 type->kind = TYPE_ARRAY;
1713 read_state_common_type_content (type);
1714 t0 = peek_state_token (0);
1715 if (state_token_kind (t0) == STOK_STRING)
1717 type->u.a.len = xstrdup (t0->stok_un.stok_string);
1718 DBGPRINTF ("read array type @%p #%d length '%s'",
1719 (void *) type, type->state_number, type->u.a.len);
1720 next_state_tokens (1);
1723 else if (state_token_is_name (t0, "nil"))
1725 type->u.a.len = NULL;
1726 DBGPRINTF ("read array type @%p #%d without length",
1727 (void *) type, type->state_number);
1728 next_state_tokens (1);
1731 else
1732 fatal_reading_state (t0, "Bad array name type");
1733 read_state_type (&(type->u.a.p));
1738 /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
1739 front-end languages. This is a tricky function and it was painful
1740 to debug. Change it with extreme care. See also
1741 write_state_lang_struct_type. */
1742 static void
1743 read_state_lang_struct_type (type_p type)
1745 struct state_token_st *t0 = NULL;
1746 struct state_token_st *t1 = NULL;
1747 struct state_token_st *t2 = NULL;
1749 type->kind = TYPE_LANG_STRUCT;
1750 read_state_common_type_content (type);
1751 t0 = peek_state_token (0);
1752 if (state_token_kind (t0) == STOK_STRING)
1754 if (state_token_is_name (t0, "nil"))
1756 DBGPRINTF ("read anonymous lang_struct type @%p #%d",
1757 (void *) type, type->state_number);
1758 type->u.s.tag = NULL;
1760 else
1762 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1763 DBGPRINTF ("read lang_struct type @%p #%d '%s'",
1764 (void *) type, type->state_number, type->u.s.tag);
1766 next_state_tokens (1);
1768 else
1769 fatal_reading_state (t0, "Bad tag in lang struct type");
1770 read_state_fileloc (&(type->u.s.line));
1771 read_state_fields (&(type->u.s.fields));
1772 read_state_options (&(type->u.s.opt));
1773 read_state_lang_bitmap (&(type->u.s.bitmap));
1774 /* Within lang_struct-ures, the lang_struct field is a linked list
1775 of homonymous types! */
1776 t0 = peek_state_token (0);
1777 t1 = peek_state_token (1);
1778 t2 = peek_state_token (2);
1779 /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
1780 if (state_token_kind (t0) == STOK_LEFTPAR
1781 && state_token_is_name (t1, "!homotypes")
1782 && state_token_kind (t2) == STOK_INTEGER)
1784 type_p *prevty = &type->u.s.lang_struct;
1785 int nbhomotype = t2->stok_un.stok_num;
1786 int i = 0;
1787 t0 = t1 = t2 = NULL;
1788 next_state_tokens (3);
1789 for (i = 0; i < nbhomotype; i++)
1791 read_state_type (prevty);
1792 t0 = peek_state_token (0);
1793 if (*prevty)
1794 prevty = &(*prevty)->next;
1795 else
1796 fatal_reading_state (t0,
1797 "expecting type in homotype list for lang_struct");
1799 if (state_token_kind (t0) != STOK_RIGHTPAR)
1800 fatal_reading_state (t0,
1801 "expecting ) in homotype list for lang_struct");
1802 next_state_tokens (1);
1804 else
1805 fatal_reading_state (t0, "expecting !homotypes for lang_struct");
1809 /* Read a param_struct type for GTY parametrized structures. */
1810 static void
1811 read_state_param_struct_type (type_p type)
1813 int i;
1814 struct state_token_st *t0;
1816 type->kind = TYPE_PARAM_STRUCT;
1817 read_state_common_type_content (type);
1818 DBGPRINTF ("read param_struct type @%p #%d",
1819 (void *) type, type->state_number);
1820 read_state_type (&(type->u.param_struct.stru));
1822 for (i = 0; i < NUM_PARAM; i++)
1824 t0 = peek_state_token (0);
1825 if (state_token_is_name (t0, "nil"))
1827 type->u.param_struct.param[i] = NULL;
1828 next_state_tokens (1);
1830 else
1831 read_state_type (&(type->u.param_struct.param[i]));
1833 read_state_fileloc (&(type->u.param_struct.line));
1837 /* Read the gc used information. */
1838 static void
1839 read_state_gc_used (enum gc_used_enum *pgus)
1841 struct state_token_st *t0 = peek_state_token (0);
1842 if (state_token_is_name (t0, "gc_unused"))
1843 *pgus = GC_UNUSED;
1844 else if (state_token_is_name (t0, "gc_used"))
1845 *pgus = GC_USED;
1846 else if (state_token_is_name (t0, "gc_maybe_pointed_to"))
1847 *pgus = GC_MAYBE_POINTED_TO;
1848 else if (state_token_is_name (t0, "gc_pointed_to"))
1849 *pgus = GC_POINTED_TO;
1850 else
1851 fatal_reading_state (t0, "invalid gc_used information");
1852 next_state_tokens (1);
1856 /* Utility function to read the common content of types. */
1857 static void
1858 read_state_common_type_content (type_p current)
1860 struct state_token_st *t0 = peek_state_token (0);
1862 if (state_token_kind (t0) == STOK_INTEGER)
1864 current->state_number = t0->stok_un.stok_num;
1865 next_state_tokens (1);
1866 record_type (current);
1868 else
1869 fatal_reading_state_printf (t0,
1870 "Expected integer for state_number line %d",
1871 state_line);
1872 /* We don't read the next field of the type. */
1873 read_state_type (&current->pointer_to);
1874 read_state_gc_used (&current->gc_used);
1878 /* Read a GTY-ed type. */
1879 void
1880 read_state_type (type_p *current)
1882 struct state_token_st *t0 = peek_state_token (0);
1883 struct state_token_st *t1 = peek_state_token (1);
1885 if (state_token_kind (t0) == STOK_LEFTPAR &&
1886 state_token_is_name (t1, "!type"))
1888 next_state_tokens (2);
1889 t0 = peek_state_token (0);
1890 if (state_token_is_name (t0, "already_seen"))
1892 next_state_tokens (1);
1893 read_state_already_seen_type (current);
1895 else
1897 t0 = peek_state_token (0);
1899 if (state_token_is_name (t0, "scalar_nonchar"))
1901 next_state_tokens (1);
1902 read_state_scalar_nonchar_type (current);
1904 else if (state_token_is_name (t0, "scalar_char"))
1906 next_state_tokens (1);
1907 read_state_scalar_char_type (current);
1909 else if (state_token_is_name (t0, "string"))
1911 next_state_tokens (1);
1912 read_state_string_type (current);
1914 else if (state_token_is_name (t0, "undefined"))
1916 *current = XCNEW (struct type);
1917 next_state_tokens (1);
1918 read_state_undefined_type (*current);
1920 else if (state_token_is_name (t0, "struct"))
1922 *current = XCNEW (struct type);
1923 next_state_tokens (1);
1924 read_state_struct_type (*current);
1926 else if (state_token_is_name (t0, "union"))
1928 *current = XCNEW (struct type);
1929 next_state_tokens (1);
1930 read_state_union_type (*current);
1932 else if (state_token_is_name (t0, "lang_struct"))
1934 *current = XCNEW (struct type);
1935 next_state_tokens (1);
1936 read_state_lang_struct_type (*current);
1938 else if (state_token_is_name (t0, "param_struct"))
1940 *current = XCNEW (struct type);
1941 next_state_tokens (1);
1942 read_state_param_struct_type (*current);
1944 else if (state_token_is_name (t0, "pointer"))
1946 *current = XCNEW (struct type);
1947 next_state_tokens (1);
1948 read_state_pointer_type (*current);
1950 else if (state_token_is_name (t0, "array"))
1952 *current = XCNEW (struct type);
1953 next_state_tokens (1);
1954 read_state_array_type (*current);
1956 else if (state_token_is_name (t0, "user_struct"))
1958 *current = XCNEW (struct type);
1959 next_state_tokens (1);
1960 read_state_user_struct_type (*current);
1962 else
1963 fatal_reading_state (t0, "bad type in (!type");
1965 t0 = peek_state_token (0);
1966 if (state_token_kind (t0) != STOK_RIGHTPAR)
1967 fatal_reading_state (t0, "missing ) in type");
1968 next_state_tokens (1);
1970 else if (state_token_is_name (t0, "nil"))
1972 next_state_tokens (1);
1973 *current = NULL;
1975 else
1976 fatal_reading_state (t0, "bad type syntax");
1980 /* Read a file location. Files within the source directory are dealt
1981 with specifically. */
1982 void
1983 read_state_fileloc (struct fileloc *floc)
1985 bool issrcfile = false;
1986 struct state_token_st *t0 = peek_state_token (0);
1987 struct state_token_st *t1 = peek_state_token (1);
1989 gcc_assert (floc != NULL);
1990 gcc_assert (srcdir != NULL);
1992 if (state_token_kind (t0) == STOK_LEFTPAR &&
1993 (state_token_is_name (t1, "!fileloc")
1994 || (issrcfile = state_token_is_name (t1, "!srcfileloc"))))
1996 next_state_tokens (2);
1997 t0 = peek_state_token (0);
1998 t1 = peek_state_token (1);
1999 if (state_token_kind (t0) == STOK_STRING &&
2000 state_token_kind (t1) == STOK_INTEGER)
2002 char *path = t0->stok_un.stok_string;
2003 if (issrcfile)
2005 static const char dirsepstr[2] = { DIR_SEPARATOR, (char) 0 };
2006 char *fullpath = concat (srcdir, dirsepstr, path, NULL);
2007 floc->file = input_file_by_name (fullpath);
2008 free (fullpath);
2010 else
2011 floc->file = input_file_by_name (path);
2012 floc->line = t1->stok_un.stok_num;
2013 next_state_tokens (2);
2015 else
2016 fatal_reading_state (t0,
2017 "Bad fileloc syntax, expected path string and line");
2018 t0 = peek_state_token (0);
2019 if (state_token_kind (t0) != STOK_RIGHTPAR)
2020 fatal_reading_state (t0, "Bad fileloc syntax, expected )");
2021 next_state_tokens (1);
2023 else if (state_token_is_name (t0, "nil"))
2025 next_state_tokens (1);
2026 floc->file = NULL;
2027 floc->line = 0;
2029 else
2030 fatal_reading_state (t0, "Bad fileloc syntax");
2034 /* Read the fields of a GTY-ed type. */
2035 void
2036 read_state_fields (pair_p *fields)
2038 pair_p tmp = NULL;
2039 struct state_token_st *t0 = peek_state_token (0);
2040 struct state_token_st *t1 = peek_state_token (1);
2041 struct state_token_st *t2 = peek_state_token (2);
2043 if (state_token_kind (t0) == STOK_LEFTPAR
2044 && state_token_is_name (t1, "!fields")
2045 && state_token_kind (t2) == STOK_INTEGER)
2047 int nbfields = t2->stok_un.stok_num;
2048 int nbpairs = 0;
2049 next_state_tokens (3);
2050 nbpairs = read_state_pair_list (&tmp);
2051 t0 = peek_state_token (0);
2052 if (nbpairs != nbfields)
2053 fatal_reading_state_printf
2054 (t0,
2055 "Mismatched fields number, expected %d got %d", nbpairs, nbfields);
2056 if (state_token_kind (t0) == STOK_RIGHTPAR)
2057 next_state_tokens (1);
2058 else
2059 fatal_reading_state (t0, "Bad fields expecting )");
2062 *fields = tmp;
2066 /* Read a string option. */
2067 static void
2068 read_state_string_option (options_p opt)
2070 struct state_token_st *t0 = peek_state_token (0);
2071 opt->kind = OPTION_STRING;
2072 if (state_token_kind (t0) == STOK_STRING)
2074 opt->info.string = xstrdup (t0->stok_un.stok_string);
2075 next_state_tokens (1);
2077 else if (state_token_is_name (t0, "nil"))
2079 opt->info.string = NULL;
2080 next_state_tokens (1);
2082 else
2083 fatal_reading_state (t0, "Missing name in string option");
2087 /* Read a type option. */
2088 static void
2089 read_state_type_option (options_p opt)
2091 opt->kind = OPTION_TYPE;
2092 read_state_type (&(opt->info.type));
2096 /* Read a nested option. */
2097 static void
2098 read_state_nested_option (options_p opt)
2100 struct state_token_st *t0;
2102 opt->info.nested = XCNEW (struct nested_ptr_data);
2103 opt->kind = OPTION_NESTED;
2104 read_state_type (&(opt->info.nested->type));
2105 t0 = peek_state_token (0);
2106 if (state_token_kind (t0) == STOK_STRING)
2108 opt->info.nested->convert_from = xstrdup (t0->stok_un.stok_string);
2109 next_state_tokens (1);
2111 else if (state_token_is_name (t0, "nil"))
2113 opt->info.nested->convert_from = NULL;
2114 next_state_tokens (1);
2116 else
2117 fatal_reading_state (t0, "Bad nested convert_from option");
2119 t0 = peek_state_token (0);
2120 if (state_token_kind (t0) == STOK_STRING)
2122 opt->info.nested->convert_to = xstrdup (t0->stok_un.stok_string);
2123 next_state_tokens (1);
2125 else if (state_token_is_name (t0, "nil"))
2127 opt->info.nested->convert_to = NULL;
2128 next_state_tokens (1);
2130 else
2131 fatal_reading_state (t0, "Bad nested convert_from option");
2135 /* Read an GTY option. */
2136 static void
2137 read_state_option (options_p *opt)
2139 struct state_token_st *t0 = peek_state_token (0);
2140 struct state_token_st *t1 = peek_state_token (1);
2142 if (state_token_kind (t0) == STOK_LEFTPAR &&
2143 state_token_is_name (t1, "!option"))
2145 next_state_tokens (2);
2146 t0 = peek_state_token (0);
2147 if (state_token_kind (t0) == STOK_NAME)
2149 *opt = XCNEW (struct options);
2150 if (state_token_is_name (t0, "nil"))
2151 (*opt)->name = NULL;
2152 else
2153 (*opt)->name = t0->stok_un.stok_ident->stid_name;
2154 next_state_tokens (1);
2155 t0 = peek_state_token (0);
2156 if (state_token_kind (t0) == STOK_NAME)
2158 if (state_token_is_name (t0, "string"))
2160 next_state_tokens (1);
2161 read_state_string_option (*opt);
2163 else if (state_token_is_name (t0, "type"))
2165 next_state_tokens (1);
2166 read_state_type_option (*opt);
2168 else if (state_token_is_name (t0, "nested"))
2170 next_state_tokens (1);
2171 read_state_nested_option (*opt);
2173 else
2174 fatal_reading_state (t0, "Bad option type");
2175 t0 = peek_state_token (0);
2176 if (state_token_kind (t0) != STOK_RIGHTPAR)
2177 fatal_reading_state (t0, "Bad syntax in option, expecting )");
2179 next_state_tokens (1);
2181 else
2182 fatal_reading_state (t0, "Missing option type");
2184 else
2185 fatal_reading_state (t0, "Bad name for option");
2187 else
2188 fatal_reading_state (t0, "Bad option, waiting for )");
2191 /* Read a list of options. */
2192 void
2193 read_state_options (options_p *opt)
2195 options_p head = NULL;
2196 options_p previous = NULL;
2197 options_p current_option = NULL;
2198 struct state_token_st *t0 = peek_state_token (0);
2199 struct state_token_st *t1 = peek_state_token (1);
2201 if (state_token_kind (t0) == STOK_LEFTPAR &&
2202 state_token_is_name (t1, "!options"))
2204 next_state_tokens (2);
2205 t0 = peek_state_token (0);
2206 while (state_token_kind (t0) != STOK_RIGHTPAR)
2208 read_state_option (&current_option);
2209 if (head == NULL)
2211 head = current_option;
2212 previous = head;
2214 else
2216 previous->next = current_option;
2217 previous = current_option;
2219 t0 = peek_state_token (0);
2221 next_state_tokens (1);
2223 else if (state_token_is_name (t0, "nil"))
2225 next_state_tokens (1);
2227 else
2228 fatal_reading_state (t0, "Bad options syntax");
2230 *opt = head;
2234 /* Read a version, and check against the version of the gengtype. */
2235 static void
2236 read_state_version (const char *version_string)
2238 struct state_token_st *t0 = peek_state_token (0);
2239 struct state_token_st *t1 = peek_state_token (1);
2241 if (state_token_kind (t0) == STOK_LEFTPAR &&
2242 state_token_is_name (t1, "!version"))
2244 next_state_tokens (2);
2245 t0 = peek_state_token (0);
2246 t1 = peek_state_token (1);
2247 if (state_token_kind (t0) == STOK_STRING &&
2248 state_token_kind (t1) == STOK_RIGHTPAR)
2250 /* Check that the read version string is the same as current
2251 version. */
2252 if (strcmp (version_string, t0->stok_un.stok_string))
2253 fatal_reading_state_printf (t0,
2254 "version string mismatch; expecting %s but got %s",
2255 version_string,
2256 t0->stok_un.stok_string);
2257 next_state_tokens (2);
2259 else
2260 fatal_reading_state (t0, "Missing version or right parenthesis");
2262 else
2263 fatal_reading_state (t0, "Bad version syntax");
2267 /* Read a pair. */
2268 void
2269 read_state_pair (pair_p *current)
2271 struct state_token_st *t0 = peek_state_token (0);
2272 struct state_token_st *t1 = peek_state_token (1);
2273 if (state_token_kind (t0) == STOK_LEFTPAR &&
2274 state_token_is_name (t1, "!pair"))
2276 *current = XCNEW (struct pair);
2277 next_state_tokens (2);
2278 t0 = peek_state_token (0);
2279 if (state_token_kind (t0) == STOK_STRING)
2281 if (strcmp (t0->stok_un.stok_string, "nil") == 0)
2283 (*current)->name = NULL;
2285 else
2287 (*current)->name = xstrdup (t0->stok_un.stok_string);
2289 next_state_tokens (1);
2290 read_state_type (&((*current)->type));
2291 read_state_fileloc (&((*current)->line));
2292 read_state_options (&((*current)->opt));;
2293 t0 = peek_state_token (0);
2294 if (state_token_kind (t0) == STOK_RIGHTPAR)
2296 next_state_tokens (1);
2298 else
2300 fatal_reading_state (t0, "Bad syntax for pair, )");
2303 else
2305 fatal_reading_state (t0, "Bad name for pair");
2308 else if (state_token_kind (t0) == STOK_NAME &&
2309 state_token_is_name (t0, "nil"))
2311 next_state_tokens (1);
2312 *current = NULL;
2314 else
2315 fatal_reading_state_printf (t0, "Bad syntax for pair, (!pair %d",
2316 state_token->stok_kind);
2320 /* Return the number of pairs actually read. */
2322 read_state_pair_list (pair_p *list)
2324 int nbpair = 0;
2325 pair_p head = NULL;
2326 pair_p previous = NULL;
2327 pair_p tmp = NULL;
2328 struct state_token_st *t0 = peek_state_token (0);
2329 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2331 read_state_pair (&tmp);
2332 if (head == NULL)
2334 head = tmp;
2335 previous = head;
2337 else
2339 previous->next = tmp;
2340 previous = tmp;
2342 t0 = peek_state_token (0);
2343 nbpair++;
2346 /* don't consume the ); the caller will eat it. */
2347 *list = head;
2348 return nbpair;
2351 /* Read the typedefs. */
2352 static void
2353 read_state_typedefs (pair_p *typedefs)
2355 int nbtypedefs = 0;
2356 pair_p list = NULL;
2357 struct state_token_st *t0 = peek_state_token (0);
2358 struct state_token_st *t1 = peek_state_token (1);
2359 struct state_token_st *t2 = peek_state_token (2);
2361 if (state_token_kind (t0) == STOK_LEFTPAR
2362 && state_token_is_name (t1, "!typedefs")
2363 && state_token_kind (t2) == STOK_INTEGER)
2365 int nbpairs = 0;
2366 nbtypedefs = t2->stok_un.stok_num;
2367 next_state_tokens (3);
2368 nbpairs = read_state_pair_list (&list);
2369 t0 = peek_state_token (0);
2370 if (nbpairs != nbtypedefs)
2371 fatal_reading_state_printf
2372 (t0,
2373 "invalid number of typedefs, expected %d but got %d",
2374 nbtypedefs, nbpairs);
2375 if (state_token_kind (t0) == STOK_RIGHTPAR)
2376 next_state_tokens (1);
2377 else
2378 fatal_reading_state (t0, "Bad typedefs syntax )");
2380 else
2381 fatal_reading_state (t0, "Bad typedefs syntax (!typedefs");
2383 if (verbosity_level >= 2)
2384 printf ("%s read %d typedefs from state\n", progname, nbtypedefs);
2385 *typedefs = list;
2389 /* Read the structures. */
2390 static void
2391 read_state_structures (type_p *structures)
2393 type_p head = NULL;
2394 type_p previous = NULL;
2395 type_p tmp;
2396 int nbstruct = 0, countstruct = 0;
2397 struct state_token_st *t0 = peek_state_token (0);
2398 struct state_token_st *t1 = peek_state_token (1);
2399 struct state_token_st *t2 = peek_state_token (2);
2401 if (state_token_kind (t0) == STOK_LEFTPAR
2402 && state_token_is_name (t1, "!structures")
2403 && state_token_kind (t2) == STOK_INTEGER)
2405 nbstruct = t2->stok_un.stok_num;
2406 next_state_tokens (3);
2407 t0 = peek_state_token (0);
2408 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2410 tmp = NULL;
2411 read_state_type (&tmp);
2412 countstruct++;
2413 if (head == NULL)
2415 head = tmp;
2416 previous = head;
2418 else
2420 previous->next = tmp;
2421 previous = tmp;
2423 t0 = peek_state_token (0);
2425 next_state_tokens (1);
2427 else
2428 fatal_reading_state (t0, "Bad structures syntax");
2429 if (countstruct != nbstruct)
2430 fatal_reading_state_printf (NULL_STATE_TOKEN,
2431 "expected %d structures but got %d",
2432 nbstruct, countstruct);
2433 if (verbosity_level >= 2)
2434 printf ("%s read %d structures from state\n", progname, nbstruct);
2435 *structures = head;
2439 /* Read the param_struct-s. */
2440 static void
2441 read_state_param_structs (type_p *param_structs)
2443 int nbparamstructs = 0;
2444 int countparamstructs = 0;
2445 type_p head = NULL;
2446 type_p previous = NULL;
2447 type_p tmp;
2448 struct state_token_st *t0 = peek_state_token (0);
2449 struct state_token_st *t1 = peek_state_token (1);
2450 struct state_token_st *t2 = peek_state_token (2);
2452 if (state_token_kind (t0) == STOK_LEFTPAR
2453 && state_token_is_name (t1, "!param_structs")
2454 && state_token_kind (t2) == STOK_INTEGER)
2456 nbparamstructs = t2->stok_un.stok_num;
2457 next_state_tokens (3);
2458 t0 = t1 = t2 = NULL;
2459 t0 = peek_state_token (0);
2460 while (state_token_kind (t0) != STOK_RIGHTPAR)
2462 tmp = NULL;
2463 read_state_type (&tmp);
2464 if (head == NULL)
2466 head = tmp;
2467 previous = head;
2469 else
2471 previous->next = tmp;
2472 previous = tmp;
2474 t0 = peek_state_token (0);
2475 countparamstructs++;
2477 next_state_tokens (1);
2479 else
2480 fatal_reading_state (t0, "Bad param_structs syntax");
2481 t0 = peek_state_token (0);
2482 if (countparamstructs != nbparamstructs)
2483 fatal_reading_state_printf
2484 (t0,
2485 "invalid number of param_structs expected %d got %d",
2486 nbparamstructs, countparamstructs);
2487 *param_structs = head;
2491 /* Read the variables. */
2492 static void
2493 read_state_variables (pair_p *variables)
2495 pair_p list = NULL;
2496 int nbvars = 0;
2497 struct state_token_st *t0 = peek_state_token (0);
2498 struct state_token_st *t1 = peek_state_token (1);
2499 struct state_token_st *t2 = peek_state_token (2);
2501 if (state_token_kind (t0) == STOK_LEFTPAR
2502 && state_token_is_name (t1, "!variables")
2503 && state_token_kind (t2) == STOK_INTEGER)
2505 int nbpairs = 0;
2506 nbvars = t2->stok_un.stok_num;
2507 next_state_tokens (3);
2508 nbpairs = read_state_pair_list (&list);
2509 t0 = peek_state_token (0);
2510 if (nbpairs != nbvars)
2511 fatal_reading_state_printf
2512 (t0, "Invalid number of variables, expected %d but got %d",
2513 nbvars, nbpairs);
2514 if (state_token_kind (t0) == STOK_RIGHTPAR)
2515 next_state_tokens (1);
2516 else
2517 fatal_reading_state (t0, "Waiting for ) in variables");
2519 else
2520 fatal_reading_state (t0, "Bad variables syntax");
2521 *variables = list;
2522 if (verbosity_level >= 2)
2523 printf ("%s read %d variables from state\n", progname, nbvars);
2527 /* Read the source directory. */
2528 static void
2529 read_state_srcdir (void)
2531 struct state_token_st *t0 = peek_state_token (0);
2532 struct state_token_st *t1 = peek_state_token (1);
2533 if (state_token_kind (t0) == STOK_LEFTPAR &&
2534 state_token_is_name (t1, "!srcdir"))
2536 next_state_tokens (2);
2537 t0 = peek_state_token (0);
2538 t1 = peek_state_token (1);
2539 if (state_token_kind (t0) == STOK_STRING &&
2540 state_token_kind (t1) == STOK_RIGHTPAR)
2542 srcdir = xstrdup (t0->stok_un.stok_string);
2543 srcdir_len = strlen (srcdir);
2544 next_state_tokens (2);
2545 return;
2549 fatal_reading_state (t0, "Bad srcdir in state_file");
2553 /* Read the sequence of GCC front-end languages. */
2554 static void
2555 read_state_languages (void)
2557 struct state_token_st *t0 = peek_state_token (0);
2558 struct state_token_st *t1 = peek_state_token (1);
2559 struct state_token_st *t2 = peek_state_token (2);
2560 if (state_token_kind (t0) == STOK_LEFTPAR
2561 && state_token_is_name (t1, "!languages")
2562 && state_token_kind (t2) == STOK_INTEGER)
2564 int i = 0;
2565 num_lang_dirs = t2->stok_un.stok_num;
2566 lang_dir_names = XCNEWVEC (const char *, num_lang_dirs);
2567 next_state_tokens (3);
2568 t0 = t1 = t2 = NULL;
2569 for (i = 0; i < (int) num_lang_dirs; i++)
2571 t0 = peek_state_token (0);
2572 if (state_token_kind (t0) != STOK_NAME)
2573 fatal_reading_state (t0, "expecting language name in state file");
2574 lang_dir_names[i] = t0->stok_un.stok_ident->stid_name;
2575 next_state_tokens (1);
2577 t0 = peek_state_token (0);
2578 if (state_token_kind (t0) != STOK_RIGHTPAR)
2579 fatal_reading_state (t0, "missing ) in languages list of state file");
2580 next_state_tokens (1);
2582 else
2583 fatal_reading_state (t0, "expecting languages list in state file");
2587 /* Read the sequence of files. */
2588 static void
2589 read_state_files_list (void)
2591 struct state_token_st *t0 = peek_state_token (0);
2592 struct state_token_st *t1 = peek_state_token (1);
2593 struct state_token_st *t2 = peek_state_token (2);
2595 if (state_token_kind (t0) == STOK_LEFTPAR
2596 && state_token_is_name (t1, "!fileslist")
2597 && state_token_kind (t2) == STOK_INTEGER)
2599 int i = 0;
2600 num_gt_files = t2->stok_un.stok_num;
2601 next_state_tokens (3);
2602 t0 = t1 = t2 = NULL;
2603 gt_files = XCNEWVEC (const input_file *, num_gt_files);
2604 for (i = 0; i < (int) num_gt_files; i++)
2606 bool issrcfile = FALSE;
2607 t0 = t1 = t2 = NULL;
2608 t0 = peek_state_token (0);
2609 t1 = peek_state_token (1);
2610 t2 = peek_state_token (2);
2611 if (state_token_kind (t0) == STOK_LEFTPAR
2612 && (state_token_is_name (t1, "!file")
2613 || (issrcfile = state_token_is_name (t1, "!srcfile")))
2614 && state_token_kind (t2) == STOK_INTEGER)
2616 lang_bitmap bmap = t2->stok_un.stok_num;
2617 next_state_tokens (3);
2618 t0 = t1 = t2 = NULL;
2619 t0 = peek_state_token (0);
2620 t1 = peek_state_token (1);
2621 if (state_token_kind (t0) == STOK_STRING
2622 && state_token_kind (t1) == STOK_RIGHTPAR)
2624 const char *fnam = t0->stok_un.stok_string;
2625 /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
2626 input_file *curgt = NULL;
2627 if (issrcfile)
2629 static const char dirsepstr[2] =
2630 { DIR_SEPARATOR, (char) 0 };
2631 char *fullpath = concat (srcdir, dirsepstr, fnam, NULL);
2632 curgt = input_file_by_name (fullpath);
2633 free (fullpath);
2635 else
2636 curgt = input_file_by_name (fnam);
2637 set_lang_bitmap (curgt, bmap);
2638 gt_files[i] = curgt;
2639 next_state_tokens (2);
2641 else
2642 fatal_reading_state (t0,
2643 "bad file in !fileslist of state file");
2645 else
2646 fatal_reading_state (t0,
2647 "expecting file in !fileslist of state file");
2649 t0 = peek_state_token (0);
2650 if (!state_token_kind (t0) == STOK_RIGHTPAR)
2651 fatal_reading_state (t0, "missing ) for !fileslist in state file");
2652 next_state_tokens (1);
2654 else
2655 fatal_reading_state (t0, "missing !fileslist in state file");
2659 /* Read the trailer. */
2660 static void
2661 read_state_trailer (void)
2663 struct state_token_st *t0 = peek_state_token (0);
2664 struct state_token_st *t1 = peek_state_token (1);
2665 struct state_token_st *t2 = peek_state_token (2);
2667 if (state_token_kind (t0) == STOK_LEFTPAR
2668 && state_token_is_name (t1, "!endfile")
2669 && state_token_kind (t2) == STOK_RIGHTPAR)
2670 next_state_tokens (3);
2671 else
2672 fatal_reading_state (t0, "missing !endfile in state file");
2676 /* Utility functions for the state_seen_types hash table. */
2677 static unsigned
2678 hash_type_number (const void *ty)
2680 const struct type *type = (const struct type *) ty;
2682 return type->state_number;
2685 static int
2686 equals_type_number (const void *ty1, const void *ty2)
2688 const struct type *type1 = (const struct type *) ty1;
2689 const struct type *type2 = (const struct type *) ty2;
2691 return type1->state_number == type2->state_number;
2694 static int
2695 string_eq (const void *a, const void *b)
2697 const char *a0 = (const char *)a;
2698 const char *b0 = (const char *)b;
2700 return (strcmp (a0, b0) == 0);
2704 /* The function reading the state, called by main from gengtype.c. */
2705 void
2706 read_state (const char *path)
2708 state_file = fopen (path, "r");
2709 if (state_file == NULL)
2710 fatal ("Failed to open state file %s for reading [%s]", path,
2711 xstrerror (errno));
2712 state_path = path;
2713 state_line = 1;
2715 if (verbosity_level >= 1)
2717 printf ("%s reading state file %s;", progname, state_path);
2718 if (verbosity_level >= 2)
2719 putchar ('\n');
2720 fflush (stdout);
2723 state_seen_types =
2724 htab_create (2017, hash_type_number, equals_type_number, NULL);
2725 state_ident_tab =
2726 htab_create (4027, htab_hash_string, string_eq, NULL);
2727 read_state_version (version_string);
2728 read_state_srcdir ();
2729 read_state_languages ();
2730 read_state_files_list ();
2731 read_state_structures (&structures);
2732 if (ferror (state_file))
2733 fatal_reading_state_printf
2734 (NULL_STATE_TOKEN, "input error while reading state [%s]",
2735 xstrerror (errno));
2736 read_state_typedefs (&typedefs);
2737 read_state_param_structs (&param_structs);
2738 read_state_variables (&variables);
2739 read_state_trailer ();
2741 if (verbosity_level >= 1)
2743 printf ("%s read %ld bytes.\n", progname, ftell (state_file));
2744 fflush (stdout);
2747 if (fclose (state_file))
2748 fatal ("failed to close read state file %s [%s]",
2749 path, xstrerror (errno));
2750 state_file = NULL;
2751 state_path = NULL;
2754 /* End of file gengtype-state.c. */