comparison gcc/gengtype-state.c @ 16:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents 561a7518be6b
children 84e7813d76e9
comparison
equal deleted inserted replaced
15:561a7518be6b 16:04ced10e8804
1 /* Gengtype persistent state serialization & de-serialization. 1 /* Gengtype persistent state serialization & de-serialization.
2 Useful for gengtype in plugin mode. 2 Useful for gengtype in plugin mode.
3 3
4 Copyright (C) 2010 Free Software Foundation, Inc. 4 Copyright (C) 2010-2017 Free Software Foundation, Inc.
5 5
6 This file is part of GCC. 6 This file is part of GCC.
7 7
8 GCC is free software; you can redistribute it and/or modify it under 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 9 the terms of the GNU General Public License as published by the Free
21 21
22 Contributed by Jeremie Salvucci <jeremie.salvucci@free.fr> 22 Contributed by Jeremie Salvucci <jeremie.salvucci@free.fr>
23 and Basile Starynkevitch <basile@starynkevitch.net> 23 and Basile Starynkevitch <basile@starynkevitch.net>
24 */ 24 */
25 25
26 #ifdef HOST_GENERATOR_FILE
27 #include "config.h"
28 #define GENERATOR_FILE 1
29 #else
26 #include "bconfig.h" 30 #include "bconfig.h"
31 #endif
27 #include "system.h" 32 #include "system.h"
28 #include "errors.h" /* For fatal. */ 33 #include "errors.h" /* For fatal. */
29 #include "double-int.h"
30 #include "hashtab.h"
31 #include "version.h" /* For version_string & pkgversion_string. */ 34 #include "version.h" /* For version_string & pkgversion_string. */
32 #include "obstack.h" 35 #include "obstack.h"
33 #include "gengtype.h" 36 #include "gengtype.h"
34 37
35 38
45 case TYPE_NONE: 48 case TYPE_NONE:
46 gcc_unreachable (); 49 gcc_unreachable ();
47 case TYPE_STRUCT: 50 case TYPE_STRUCT:
48 case TYPE_UNION: 51 case TYPE_UNION:
49 case TYPE_LANG_STRUCT: 52 case TYPE_LANG_STRUCT:
53 case TYPE_USER_STRUCT:
54 case TYPE_UNDEFINED:
50 return CONST_CAST (struct fileloc*, &ty->u.s.line); 55 return CONST_CAST (struct fileloc*, &ty->u.s.line);
51 case TYPE_PARAM_STRUCT:
52 return CONST_CAST (struct fileloc*, &ty->u.param_struct.line);
53 case TYPE_SCALAR: 56 case TYPE_SCALAR:
54 case TYPE_STRING: 57 case TYPE_STRING:
55 case TYPE_POINTER: 58 case TYPE_POINTER:
56 case TYPE_ARRAY: 59 case TYPE_ARRAY:
57 return NULL; 60 return NULL;
59 gcc_unreachable (); 62 gcc_unreachable ();
60 } 63 }
61 } 64 }
62 65
63 /* The state file has simplistic lispy lexical tokens. Its lexer gives 66 /* The state file has simplistic lispy lexical tokens. Its lexer gives
64 a linked list of struct state_token_st, thru the peek_state_token 67 a linked list of struct state_token_st, through the peek_state_token
65 function. Lexical tokens are consumed with next_state_tokens. */ 68 function. Lexical tokens are consumed with next_state_tokens. */
66 69
67 70
68 /* The lexical kind of each lispy token. */ 71 /* The lexical kind of each lispy token. */
69 enum state_token_en 72 enum state_token_en
129 static FILE *state_file; 132 static FILE *state_file;
130 static const char *state_path = NULL; 133 static const char *state_path = NULL;
131 static int state_line = 0; 134 static int state_line = 0;
132 static long state_bol = 0; /* offset of beginning of line */ 135 static long state_bol = 0; /* offset of beginning of line */
133 136
134 137 /* A class for writing out s-expressions, keeping track of newlines and
135 /* Counter of written types. */ 138 nested indentation. */
136 static int state_written_type_count = 0; 139 class s_expr_writer
140 {
141 public:
142 s_expr_writer ();
143
144 void write_new_line ();
145 void write_any_indent (int leading_spaces);
146
147 void begin_s_expr (const char *tag);
148 void end_s_expr ();
149
150 private:
151 int m_indent_amount;
152 int m_had_recent_newline;
153 }; // class s_expr_writer
154
155 /* A class for writing out "gtype.state". */
156 class state_writer : public s_expr_writer
157 {
158 public:
159 state_writer ();
160
161 private:
162 void write_state_fileloc (struct fileloc *floc);
163 void write_state_fields (pair_p fields);
164 void write_state_a_string (const char *s);
165 void write_state_string_option (options_p current);
166 void write_state_type_option (options_p current);
167 void write_state_nested_option (options_p current);
168 void write_state_option (options_p current);
169 void write_state_options (options_p opt);
170 void write_state_lang_bitmap (lang_bitmap bitmap);
171 void write_state_version (const char *version);
172 void write_state_scalar_type (type_p current);
173 void write_state_string_type (type_p current);
174 void write_state_undefined_type (type_p current);
175 void write_state_struct_union_type (type_p current, const char *kindstr);
176 void write_state_struct_type (type_p current);
177 void write_state_user_struct_type (type_p current);
178 void write_state_union_type (type_p current);
179 void write_state_lang_struct_type (type_p current);
180 void write_state_pointer_type (type_p current);
181 void write_state_array_type (type_p current);
182 void write_state_gc_used (enum gc_used_enum gus);
183 void write_state_common_type_content (type_p current);
184 void write_state_type (type_p current);
185 void write_state_pair (pair_p current);
186 int write_state_pair_list (pair_p list);
187 void write_state_typedefs (void);
188 void write_state_structures (void);
189 void write_state_variables (void);
190 void write_state_srcdir (void);
191 void write_state_files_list (void);
192 void write_state_languages (void);
193
194 friend void write_state (const char *state_path);
195
196 private:
197 /* Counter of written types. */
198 int m_state_written_type_count;
199 }; // class state_writer
200
201
202 /* class s_expr_writer's trivial constructor. */
203 s_expr_writer::s_expr_writer ()
204 : m_indent_amount (0),
205 m_had_recent_newline (0)
206 {
207 }
208
209 /* Write a newline to the output file, merging adjacent newlines. */
210 void
211 s_expr_writer::write_new_line (void)
212 {
213 /* Don't add a newline if we've just had one. */
214 if (!m_had_recent_newline)
215 {
216 fprintf (state_file, "\n");
217 m_had_recent_newline = 1;
218 }
219 }
220
221 /* If we've just had a newline, write the indentation amount, potentially
222 omitting some spaces.
223
224 LEADING_SPACES exists to support code that writes strings with leading
225 spaces (e.g " foo") which might occur within a line, or could be the first
226 thing on a line. By passing leading_spaces == 1, when such a string is the
227 first thing on a line, write_any_indent () swallows the successive
228 leading spaces into the indentation so that the "foo" begins at the expected
229 column. */
230 void
231 s_expr_writer::write_any_indent (int leading_spaces)
232 {
233 int i;
234 int amount = m_indent_amount - leading_spaces;
235 if (m_had_recent_newline)
236 for (i = 0; i < amount; i++)
237 fprintf (state_file, " ");
238 m_had_recent_newline = 0;
239 }
240
241 /* Write the beginning of a new s-expresion e.g. "(!foo "
242 The writer automatically adds whitespace to show the hierarchical
243 structure of the expressions, so each one starts on a new line,
244 and any within it will be at an increased indentation level. */
245 void
246 s_expr_writer::begin_s_expr (const char *tag)
247 {
248 write_new_line ();
249 write_any_indent (0);
250 fprintf (state_file, "(!%s ", tag);
251 m_indent_amount++;
252 }
253
254 /* Write out the end of an s-expression: any necssessary indentation,
255 a closing parenthesis, and a new line. */
256 void
257 s_expr_writer::end_s_expr (void)
258 {
259 m_indent_amount--;
260 write_any_indent (0);
261 fprintf (state_file, ")");
262 write_new_line ();
263 }
264
265
266 /* class state_writer's trivial constructor. */
267 state_writer::state_writer ()
268 : s_expr_writer (),
269 m_state_written_type_count (0)
270 {
271 }
137 272
138 273
139 /* Fatal error messages when reading the state. They are extremely 274 /* Fatal error messages when reading the state. They are extremely
140 unlikely, and only appear when this gengtype-state.c file is buggy, 275 unlikely, and only appear when this gengtype-state.c file is buggy,
141 or when reading a gengtype state which was not generated by the 276 or when reading a gengtype state which was not generated by the
142 same version of gengtype or GCC. */ 277 same version of gengtype or GCC. */
143 278
144 279
145 /* Fatal message while reading state. */ 280 /* Fatal message while reading state. */
146 static inline void 281 static void
147 fatal_reading_state (struct state_token_st* tok, const char*msg) 282 fatal_reading_state (struct state_token_st* tok, const char*msg)
148 { 283 {
149 if (tok) 284 if (tok)
150 fatal ("%s:%d:%d: Invalid state file; %s", 285 fatal ("%s:%d:%d: Invalid state file; %s",
151 tok->stok_file, tok->stok_line, tok->stok_col, 286 tok->stok_file, tok->stok_line, tok->stok_col,
167 badtok->stok_line, \ 302 badtok->stok_line, \
168 badtok->stok_col, __VA_ARGS__); \ 303 badtok->stok_col, __VA_ARGS__); \
169 else \ 304 else \
170 fatal ("%s:%d: Invalid state file; " Fmt, \ 305 fatal ("%s:%d: Invalid state file; " Fmt, \
171 state_path, state_line, __VA_ARGS__); \ 306 state_path, state_line, __VA_ARGS__); \
172 } while(0) 307 } while (0)
173 308
174 309
175 /* Find or allocate an identifier in our name hash table. */ 310 /* Find or allocate an identifier in our name hash table. */
176 static struct state_ident_st * 311 static struct state_ident_st *
177 state_ident_by_name (const char *name, enum insert_option optins) 312 state_ident_by_name (const char *name, enum insert_option optins)
301 if (c >= 0) 436 if (c >= 0)
302 ungetc (c, state_file); 437 ungetc (c, state_file);
303 obstack_1grow (&id_obstack, (char) 0); 438 obstack_1grow (&id_obstack, (char) 0);
304 ids = XOBFINISH (&id_obstack, char *); 439 ids = XOBFINISH (&id_obstack, char *);
305 sid = state_ident_by_name (ids, INSERT); 440 sid = state_ident_by_name (ids, INSERT);
306 obstack_free (&id_obstack, ids); 441 obstack_free (&id_obstack, NULL);
307 ids = NULL; 442 ids = NULL;
308 tk = XCNEW (struct state_token_st); 443 tk = XCNEW (struct state_token_st);
309 tk->stok_kind = STOK_NAME; 444 tk->stok_kind = STOK_NAME;
310 tk->stok_line = state_line; 445 tk->stok_line = state_line;
311 tk->stok_col = curoff - state_bol; 446 tk->stok_col = curoff - state_bol;
406 tk->stok_line = state_line; 541 tk->stok_line = state_line;
407 tk->stok_col = curoff - state_bol; 542 tk->stok_col = curoff - state_bol;
408 tk->stok_file = state_path; 543 tk->stok_file = state_path;
409 tk->stok_next = NULL; 544 tk->stok_next = NULL;
410 strcpy (tk->stok_un.stok_string, cstr); 545 strcpy (tk->stok_un.stok_string, cstr);
411 obstack_free (&bstring_obstack, cstr); 546 obstack_free (&bstring_obstack, NULL);
412 547
413 return tk; 548 return tk;
414 } 549 }
415 /* Got an unexpected character. */ 550 /* Got an unexpected character. */
416 fatal_reading_state_printf 551 fatal_reading_state_printf
493 /* Following routines are useful for serializing datas. 628 /* Following routines are useful for serializing datas.
494 * 629 *
495 * We want to serialize : 630 * We want to serialize :
496 * - typedefs list 631 * - typedefs list
497 * - structures list 632 * - structures list
498 * - param_structs list
499 * - variables list 633 * - variables list
500 * 634 *
501 * So, we have one routine for each kind of data. The main writing 635 * So, we have one routine for each kind of data. The main writing
502 * routine is write_state. The main reading routine is 636 * routine is write_state. The main reading routine is
503 * read_state. Most writing routines write_state_FOO have a 637 * read_state. Most writing routines write_state_FOO have a
511 static htab_t state_seen_types; 645 static htab_t state_seen_types;
512 646
513 /* Return the length of a linked list made of pairs. */ 647 /* Return the length of a linked list made of pairs. */
514 static int pair_list_length (pair_p list); 648 static int pair_list_length (pair_p list);
515 649
516 /* Write a pair */
517 static void write_state_pair (pair_p);
518
519 /* return the number of pairs written. Should match the length given
520 by pair_list_length. */
521 static int write_state_pair_list (pair_p list);
522
523 /* Write a type. When a type is written, its state_number is updated,
524 to ensure that a "reference" to a seen type is written on next
525 occurrences. */
526 static void write_state_type (type_p);
527
528 /* Write a null-terminatel string using our Lispy lexical conventions,
529 similar to those of C or MELT. */
530 static void write_state_a_string (const char *s);
531
532 /* Compute the length of a list of pairs, starting from the first 650 /* Compute the length of a list of pairs, starting from the first
533 one. */ 651 one. */
534 static int 652 static int
535 pair_list_length (pair_p list) 653 pair_list_length (pair_p list)
536 { 654 {
544 /* Write a file location. Files relative to $(srcdir) are quite 662 /* Write a file location. Files relative to $(srcdir) are quite
545 frequent and are handled specially. This ensures that two gengtype 663 frequent and are handled specially. This ensures that two gengtype
546 state file-s produced by gengtype on the same GCC source tree are 664 state file-s produced by gengtype on the same GCC source tree are
547 very similar and can be reasonably compared with diff, even if the 665 very similar and can be reasonably compared with diff, even if the
548 two GCC source trees have different absolute paths. */ 666 two GCC source trees have different absolute paths. */
549 static void 667 void
550 write_state_fileloc (struct fileloc *floc) 668 state_writer::write_state_fileloc (struct fileloc *floc)
551 { 669 {
552 670
553 if (floc != NULL && floc->line > 0) 671 if (floc != NULL && floc->line > 0)
554 { 672 {
555 const char *srcrelpath = NULL; 673 const char *srcrelpath = NULL;
557 /* Most of the files are inside $(srcdir) so it is worth to 675 /* Most of the files are inside $(srcdir) so it is worth to
558 handle them specially. */ 676 handle them specially. */
559 srcrelpath = get_file_srcdir_relative_path (floc->file); 677 srcrelpath = get_file_srcdir_relative_path (floc->file);
560 if (srcrelpath != NULL) 678 if (srcrelpath != NULL)
561 { 679 {
562 fprintf (state_file, "\n(!srcfileloc "); 680 begin_s_expr ("srcfileloc");
563 write_state_a_string (srcrelpath); 681 write_state_a_string (srcrelpath);
564 } 682 }
565 else 683 else
566 { 684 {
567 fprintf (state_file, "\n(!fileloc "); 685 begin_s_expr ("fileloc");
568 write_state_a_string (get_input_file_name (floc->file)); 686 write_state_a_string (get_input_file_name (floc->file));
569 } 687 }
570 fprintf (state_file, " %d", floc->line); 688 fprintf (state_file, " %d", floc->line);
571 fprintf (state_file, ")\n"); 689 end_s_expr ();
572 } 690 }
573 else 691 else
574 fprintf (state_file, "nil "); 692 fprintf (state_file, "nil ");
575 } 693 }
576 694
577 /* Write a list of fields. */ 695 /* Write a list of fields. */
578 static void 696 void
579 write_state_fields (pair_p fields) 697 state_writer::write_state_fields (pair_p fields)
580 { 698 {
581 int nbfields = pair_list_length (fields); 699 int nbfields = pair_list_length (fields);
582 int nbpairs = 0; 700 int nbpairs = 0;
583 fprintf (state_file, "\n(!fields %d ", nbfields); 701 begin_s_expr ("fields");
702 fprintf (state_file, "%d ", nbfields);
584 nbpairs = write_state_pair_list (fields); 703 nbpairs = write_state_pair_list (fields);
585 gcc_assert (nbpairs == nbfields); 704 gcc_assert (nbpairs == nbfields);
586 fprintf (state_file, ")\n"); 705 end_s_expr ();
587 } 706 }
588 707
589 /* Write a null-terminated string in our lexical convention, very 708 /* Write a null-terminated string in our lexical convention, very
590 similar to the convention of C. */ 709 similar to the convention of C. */
591 static void 710 void
592 write_state_a_string (const char *s) 711 state_writer::write_state_a_string (const char *s)
593 { 712 {
594 char c; 713 char c;
714
715 write_any_indent (1);
595 716
596 fputs (" \"", state_file); 717 fputs (" \"", state_file);
597 for (; *s != 0; s++) 718 for (; *s != 0; s++)
598 { 719 {
599 c = *s; 720 c = *s;
635 } 756 }
636 fputs ("\"", state_file); 757 fputs ("\"", state_file);
637 } 758 }
638 759
639 /* Our option-s have three kinds, each with its writer. */ 760 /* Our option-s have three kinds, each with its writer. */
640 static void 761 void
641 write_state_string_option (options_p current) 762 state_writer::write_state_string_option (options_p current)
642 { 763 {
764 write_any_indent (0);
643 fprintf (state_file, "string "); 765 fprintf (state_file, "string ");
644 if (current->info.string != NULL) 766 if (current->info.string != NULL)
645 write_state_a_string (current->info.string); 767 write_state_a_string (current->info.string);
646 else 768 else
647 fprintf (state_file, " nil "); 769 fprintf (state_file, " nil ");
648 } 770 }
649 771
650 static void 772 void
651 write_state_type_option (options_p current) 773 state_writer::write_state_type_option (options_p current)
652 { 774 {
775 write_any_indent (0);
653 fprintf (state_file, "type "); 776 fprintf (state_file, "type ");
654 write_state_type (current->info.type); 777 write_state_type (current->info.type);
655 } 778 }
656 779
657 static void 780 void
658 write_state_nested_option (options_p current) 781 state_writer::write_state_nested_option (options_p current)
659 { 782 {
783 write_any_indent (0);
660 fprintf (state_file, "nested "); 784 fprintf (state_file, "nested ");
661 write_state_type (current->info.nested->type); 785 write_state_type (current->info.nested->type);
662 if (current->info.nested->convert_from != NULL) 786 if (current->info.nested->convert_from != NULL)
663 write_state_a_string (current->info.nested->convert_from); 787 write_state_a_string (current->info.nested->convert_from);
664 else 788 else
665 fprintf (state_file, " nil "); 789 {
790 write_any_indent (1);
791 fprintf (state_file, " nil ");
792 }
666 793
667 if (current->info.nested->convert_to != NULL) 794 if (current->info.nested->convert_to != NULL)
668 write_state_a_string (current->info.nested->convert_to); 795 write_state_a_string (current->info.nested->convert_to);
669 else 796 else
670 fprintf (state_file, " nil "); 797 {
671 } 798 write_any_indent (1);
672 799 fprintf (state_file, " nil ");
673 static void 800 }
674 write_state_option (options_p current) 801 }
675 { 802
676 fprintf (state_file, "\n(!option "); 803 void
677 804 state_writer::write_state_option (options_p current)
805 {
806 begin_s_expr ("option");
807
808 write_any_indent (0);
678 if (current->name != NULL) 809 if (current->name != NULL)
679 fprintf (state_file, "%s ", current->name); 810 fprintf (state_file, "%s ", current->name);
680 else 811 else
681 fprintf (state_file, "nil "); 812 fprintf (state_file, "nil ");
682 813
693 break; 824 break;
694 default: 825 default:
695 fatal ("Option tag unknown"); 826 fatal ("Option tag unknown");
696 } 827 }
697 828
698 fprintf (state_file, ")\n"); 829 /* Terminate the "option" s-expression. */
830 end_s_expr ();
699 } 831 }
700 832
701 833
702 834
703 /* Write a list of GTY options. */ 835 /* Write a list of GTY options. */
704 static void 836 void
705 write_state_options (options_p opt) 837 state_writer::write_state_options (options_p opt)
706 { 838 {
707 options_p current; 839 options_p current;
708 840
709 if (opt == NULL) 841 if (opt == NULL)
710 { 842 {
711 fprintf (state_file, "nil "); 843 write_any_indent (0);
844 fprintf (state_file, "nil ");
712 return; 845 return;
713 } 846 }
714 847
715 fprintf (state_file, "\n(!options "); 848 begin_s_expr ("options");
716 for (current = opt; current != NULL; current = current->next) 849 for (current = opt; current != NULL; current = current->next)
717 write_state_option (current); 850 write_state_option (current);
718 fprintf (state_file, ")\n"); 851 end_s_expr ();
719 } 852 }
720 853
721 854
722 /* Write a bitmap representing a set of GCC front-end languages. */ 855 /* Write a bitmap representing a set of GCC front-end languages. */
723 static void 856 void
724 write_state_lang_bitmap (lang_bitmap bitmap) 857 state_writer::write_state_lang_bitmap (lang_bitmap bitmap)
725 { 858 {
859 write_any_indent (0);
726 fprintf (state_file, "%d ", (int) bitmap); 860 fprintf (state_file, "%d ", (int) bitmap);
727 } 861 }
728 862
729 /* Write version information. */ 863 /* Write version information. */
730 static void 864 void
731 write_state_version (const char *version) 865 state_writer::write_state_version (const char *version)
732 { 866 {
733 fprintf (state_file, "\n(!version "); 867 begin_s_expr ("version");
734 write_state_a_string (version); 868 write_state_a_string (version);
735 fprintf (state_file, ")\n"); 869 end_s_expr ();
736 } 870 }
737
738 /* Common routine to write the common content of all types. */
739 static void write_state_common_type_content (type_p current);
740 871
741 /* Write a scalar type. We have only two of these. */ 872 /* Write a scalar type. We have only two of these. */
742 static void 873 void
743 write_state_scalar_type (type_p current) 874 state_writer::write_state_scalar_type (type_p current)
744 { 875 {
876 write_any_indent (0);
745 if (current == &scalar_nonchar) 877 if (current == &scalar_nonchar)
746 fprintf (state_file, "scalar_nonchar "); 878 fprintf (state_file, "scalar_nonchar ");
747 else if (current == &scalar_char) 879 else if (current == &scalar_char)
748 fprintf (state_file, "scalar_char "); 880 fprintf (state_file, "scalar_char ");
749 else 881 else
751 883
752 write_state_common_type_content (current); 884 write_state_common_type_content (current);
753 } 885 }
754 886
755 /* Write the string type. There is only one such thing! */ 887 /* Write the string type. There is only one such thing! */
756 static void 888 void
757 write_state_string_type (type_p current) 889 state_writer::write_state_string_type (type_p current)
758 { 890 {
759 if (current == &string_type) 891 if (current == &string_type)
760 { 892 {
893 write_any_indent (0);
761 fprintf (state_file, "string "); 894 fprintf (state_file, "string ");
762 write_state_common_type_content (current); 895 write_state_common_type_content (current);
763 } 896 }
764 else 897 else
765 fatal ("Unexpected type in write_state_string_type"); 898 fatal ("Unexpected type in write_state_string_type");
766 } 899 }
767 900
901 /* Write an undefined type. */
902 void
903 state_writer::write_state_undefined_type (type_p current)
904 {
905 DBGPRINTF ("undefined type @ %p #%d '%s'", (void *) current,
906 current->state_number, current->u.s.tag);
907 write_any_indent (0);
908 fprintf (state_file, "undefined ");
909 gcc_assert (current->gc_used == GC_UNUSED);
910 write_state_common_type_content (current);
911 if (current->u.s.tag != NULL)
912 write_state_a_string (current->u.s.tag);
913 else
914 {
915 write_any_indent (0);
916 fprintf (state_file, "nil");
917 }
918
919 write_state_fileloc (type_lineloc (current));
920 }
921
768 922
769 /* Common code to write structure like types. */ 923 /* Common code to write structure like types. */
770 static void 924 void
771 write_state_struct_union_type (type_p current, const char *kindstr) 925 state_writer::write_state_struct_union_type (type_p current,
926 const char *kindstr)
772 { 927 {
773 DBGPRINTF ("%s type @ %p #%d '%s'", kindstr, (void *) current, 928 DBGPRINTF ("%s type @ %p #%d '%s'", kindstr, (void *) current,
774 current->state_number, current->u.s.tag); 929 current->state_number, current->u.s.tag);
930 write_any_indent (0);
775 fprintf (state_file, "%s ", kindstr); 931 fprintf (state_file, "%s ", kindstr);
776 write_state_common_type_content (current); 932 write_state_common_type_content (current);
777 if (current->u.s.tag != NULL) 933 if (current->u.s.tag != NULL)
778 write_state_a_string (current->u.s.tag); 934 write_state_a_string (current->u.s.tag);
779 else 935 else
780 fprintf (state_file, "nil"); 936 {
937 write_any_indent (0);
938 fprintf (state_file, "nil");
939 }
781 940
782 write_state_fileloc (type_lineloc (current)); 941 write_state_fileloc (type_lineloc (current));
783 write_state_fields (current->u.s.fields); 942 write_state_fields (current->u.s.fields);
784 write_state_options (current->u.s.opt); 943 write_state_options (current->u.s.opt);
785 write_state_lang_bitmap (current->u.s.bitmap); 944 write_state_lang_bitmap (current->u.s.bitmap);
786 } 945 }
787 946
788 947
789 /* Write a GTY struct type. */ 948 /* Write a GTY struct type. */
790 static void 949 void
791 write_state_struct_type (type_p current) 950 state_writer::write_state_struct_type (type_p current)
792 { 951 {
793 write_state_struct_union_type (current, "struct"); 952 write_state_struct_union_type (current, "struct");
794 write_state_type (current->u.s.lang_struct); 953 write_state_type (current->u.s.lang_struct);
954 write_state_type (current->u.s.base_class);
955 }
956
957 /* Write a GTY user-defined struct type. */
958 void
959 state_writer::write_state_user_struct_type (type_p current)
960 {
961 DBGPRINTF ("user_struct type @ %p #%d '%s'", (void *) current,
962 current->state_number, current->u.s.tag);
963 write_any_indent (0);
964 fprintf (state_file, "user_struct ");
965 write_state_common_type_content (current);
966 if (current->u.s.tag != NULL)
967 write_state_a_string (current->u.s.tag);
968 else
969 {
970 write_any_indent (0);
971 fprintf (state_file, "nil");
972 }
973 write_state_fileloc (type_lineloc (current));
974 write_state_fields (current->u.s.fields);
795 } 975 }
796 976
797 /* write a GTY union type. */ 977 /* write a GTY union type. */
798 static void 978 void
799 write_state_union_type (type_p current) 979 state_writer::write_state_union_type (type_p current)
800 { 980 {
801 write_state_struct_union_type (current, "union"); 981 write_state_struct_union_type (current, "union");
802 write_state_type (current->u.s.lang_struct); 982 write_state_type (current->u.s.lang_struct);
803 } 983 }
804 984
805 /* Write a lang_struct type. This is tricky and was painful to debug, 985 /* Write a lang_struct type. This is tricky and was painful to debug,
806 we deal with the next field specifically within their lang_struct 986 we deal with the next field specifically within their lang_struct
807 subfield, which points to a linked list of homonumous types. 987 subfield, which points to a linked list of homonumous types.
808 Change this function with extreme care, see also 988 Change this function with extreme care, see also
809 read_state_lang_struct_type. */ 989 read_state_lang_struct_type. */
810 static void 990 void
811 write_state_lang_struct_type (type_p current) 991 state_writer::write_state_lang_struct_type (type_p current)
812 { 992 {
813 int nbhomontype = 0; 993 int nbhomontype = 0;
814 type_p hty = NULL; 994 type_p hty = NULL;
815 const char *homoname = 0; 995 const char *homoname = 0;
816 write_state_struct_union_type (current, "lang_struct"); 996 write_state_struct_union_type (current, "lang_struct");
817 /* lang_struct-ures are particularily tricky, since their 997 /* lang_struct-ures are particularly tricky, since their
818 u.s.lang_struct field gives a list of homonymous struct-s or 998 u.s.lang_struct field gives a list of homonymous struct-s or
819 union-s! */ 999 union-s! */
820 DBGPRINTF ("lang_struct @ %p #%d", (void *) current, current->state_number); 1000 DBGPRINTF ("lang_struct @ %p #%d", (void *) current, current->state_number);
821 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next) 1001 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
822 { 1002 {
823 nbhomontype++; 1003 nbhomontype++;
824 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype, 1004 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype,
825 (void *) hty, hty->state_number, hty->u.s.tag); 1005 (void *) hty, hty->state_number, hty->u.s.tag);
826 /* Every member of the homonymous list should have the same tag. */ 1006 /* Every member of the homonymous list should have the same tag. */
827 gcc_assert (UNION_OR_STRUCT_P (hty)); 1007 gcc_assert (union_or_struct_p (hty));
828 gcc_assert (hty->u.s.lang_struct == current); 1008 gcc_assert (hty->u.s.lang_struct == current);
829 if (!homoname) 1009 if (!homoname)
830 homoname = hty->u.s.tag; 1010 homoname = hty->u.s.tag;
831 gcc_assert (strcmp (homoname, hty->u.s.tag) == 0); 1011 gcc_assert (strcmp (homoname, hty->u.s.tag) == 0);
832 } 1012 }
833 fprintf (state_file, "(!homotypes %d\n", nbhomontype); 1013 begin_s_expr ("homotypes");
1014 fprintf (state_file, "%d", nbhomontype);
834 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next) 1015 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
835 write_state_type (hty); 1016 write_state_type (hty);
836 fprintf (state_file, ")\n"); 1017 end_s_expr ();
837 }
838
839 /* Write a parametrized structure GTY type. */
840 static void
841 write_state_param_struct_type (type_p current)
842 {
843 int i;
844
845 fprintf (state_file, "param_struct ");
846 write_state_common_type_content (current);
847 write_state_type (current->u.param_struct.stru);
848 for (i = 0; i < NUM_PARAM; i++)
849 {
850 if (current->u.param_struct.param[i] != NULL)
851 write_state_type (current->u.param_struct.param[i]);
852 else
853 fprintf (state_file, "nil ");
854 }
855 write_state_fileloc (&current->u.param_struct.line);
856 } 1018 }
857 1019
858 /* Write a pointer type. */ 1020 /* Write a pointer type. */
859 static void 1021 void
860 write_state_pointer_type (type_p current) 1022 state_writer::write_state_pointer_type (type_p current)
861 { 1023 {
1024 write_any_indent (0);
862 fprintf (state_file, "pointer "); 1025 fprintf (state_file, "pointer ");
863 write_state_common_type_content (current); 1026 write_state_common_type_content (current);
864 write_state_type (current->u.p); 1027 write_state_type (current->u.p);
865 } 1028 }
866 1029
867 /* Write an array type. */ 1030 /* Write an array type. */
868 static void 1031 void
869 write_state_array_type (type_p current) 1032 state_writer::write_state_array_type (type_p current)
870 { 1033 {
1034 write_any_indent (0);
871 fprintf (state_file, "array "); 1035 fprintf (state_file, "array ");
872 write_state_common_type_content (current); 1036 write_state_common_type_content (current);
873 if (current->u.a.len != NULL) 1037 if (current->u.a.len != NULL)
874 write_state_a_string (current->u.a.len); 1038 write_state_a_string (current->u.a.len);
875 else 1039 else
876 fprintf (state_file, " nil"); 1040 {
877 1041 write_any_indent (1);
1042 fprintf (state_file, " nil");
1043 }
1044
1045 write_any_indent (1);
878 fprintf (state_file, " "); 1046 fprintf (state_file, " ");
879 write_state_type (current->u.a.p); 1047 write_state_type (current->u.a.p);
880 } 1048 }
881 1049
882 /* Write the gc_used information. */ 1050 /* Write the gc_used information. */
883 static void 1051 void
884 write_state_gc_used (enum gc_used_enum gus) 1052 state_writer::write_state_gc_used (enum gc_used_enum gus)
885 { 1053 {
1054 write_any_indent (1);
886 switch (gus) 1055 switch (gus)
887 { 1056 {
888 case GC_UNUSED: 1057 case GC_UNUSED:
889 fprintf (state_file, " gc_unused"); 1058 fprintf (state_file, " gc_unused");
890 break; 1059 break;
902 } 1071 }
903 } 1072 }
904 1073
905 /* Utility routine to write the common content of all types. Notice 1074 /* Utility routine to write the common content of all types. Notice
906 that the next field is *not* written on purpose. */ 1075 that the next field is *not* written on purpose. */
907 static void 1076 void
908 write_state_common_type_content (type_p current) 1077 state_writer::write_state_common_type_content (type_p current)
909 { 1078 {
1079 write_any_indent (0);
910 fprintf (state_file, "%d ", current->state_number); 1080 fprintf (state_file, "%d ", current->state_number);
911 /* We do not write the next type, because list of types are 1081 /* We do not write the next type, because list of types are
912 explicitly written. However, lang_struct are special in that 1082 explicitly written. However, lang_struct are special in that
913 respect. See function write_state_lang_struct_type for more. */ 1083 respect. See function write_state_lang_struct_type for more. */
914 write_state_type (current->pointer_to); 1084 write_state_type (current->pointer_to);
917 1087
918 1088
919 /* The important and recursive routine writing GTY types as understood 1089 /* The important and recursive routine writing GTY types as understood
920 by gengtype. Types which have a positive state_number have already 1090 by gengtype. Types which have a positive state_number have already
921 been seen and written. */ 1091 been seen and written. */
922 static void 1092 void
923 write_state_type (type_p current) 1093 state_writer::write_state_type (type_p current)
924 { 1094 {
1095 write_any_indent (0);
925 if (current == NULL) 1096 if (current == NULL)
926 { 1097 {
927 fprintf (state_file, "nil "); 1098 fprintf (state_file, "nil ");
928 return; 1099 return;
929 } 1100 }
930 1101
931 fprintf (state_file, "\n(!type "); 1102 begin_s_expr ("type");
932 1103
933 if (current->state_number > 0) 1104 if (current->state_number > 0)
934 fprintf (state_file, "already_seen %d", current->state_number); 1105 {
935 else 1106 write_any_indent (0);
936 { 1107 fprintf (state_file, "already_seen %d", current->state_number);
937 state_written_type_count++; 1108 }
938 DBGPRINTF ("writing type #%d @%p old number %d", state_written_type_count, 1109 else
1110 {
1111 m_state_written_type_count++;
1112 DBGPRINTF ("writing type #%d @%p old number %d", m_state_written_type_count,
939 (void *) current, current->state_number); 1113 (void *) current, current->state_number);
940 current->state_number = state_written_type_count; 1114 current->state_number = m_state_written_type_count;
941 switch (current->kind) 1115 switch (current->kind)
942 { 1116 {
1117 case TYPE_NONE:
1118 gcc_unreachable ();
1119 case TYPE_UNDEFINED:
1120 write_state_undefined_type (current);
1121 break;
943 case TYPE_STRUCT: 1122 case TYPE_STRUCT:
944 write_state_struct_type (current); 1123 write_state_struct_type (current);
1124 break;
1125 case TYPE_USER_STRUCT:
1126 write_state_user_struct_type (current);
945 break; 1127 break;
946 case TYPE_UNION: 1128 case TYPE_UNION:
947 write_state_union_type (current); 1129 write_state_union_type (current);
948 break; 1130 break;
949 case TYPE_POINTER: 1131 case TYPE_POINTER:
953 write_state_array_type (current); 1135 write_state_array_type (current);
954 break; 1136 break;
955 case TYPE_LANG_STRUCT: 1137 case TYPE_LANG_STRUCT:
956 write_state_lang_struct_type (current); 1138 write_state_lang_struct_type (current);
957 break; 1139 break;
958 case TYPE_PARAM_STRUCT:
959 write_state_param_struct_type (current);
960 break;
961 case TYPE_SCALAR: 1140 case TYPE_SCALAR:
962 write_state_scalar_type (current); 1141 write_state_scalar_type (current);
963 break; 1142 break;
964 case TYPE_STRING: 1143 case TYPE_STRING:
965 write_state_string_type (current); 1144 write_state_string_type (current);
966 break; 1145 break;
967 1146 }
968 default: 1147 }
969 fatal ("Unexpected type..."); 1148
970 } 1149 /* Terminate the "type" s-expression. */
971 } 1150 end_s_expr ();
972
973 fprintf (state_file, ")\n");
974 } 1151 }
975 1152
976 1153
977 /* Write a pair. */ 1154 /* Write a pair. */
978 static void 1155 void
979 write_state_pair (pair_p current) 1156 state_writer::write_state_pair (pair_p current)
980 { 1157 {
981 if (current == NULL) 1158 if (current == NULL)
982 { 1159 {
1160 write_any_indent (0);
983 fprintf (state_file, "nil)"); 1161 fprintf (state_file, "nil)");
984 return; 1162 return;
985 } 1163 }
986 1164
987 fprintf (state_file, "\n(!pair "); 1165 begin_s_expr ("pair");
988 1166
989 if (current->name != NULL) 1167 if (current->name != NULL)
990 write_state_a_string (current->name); 1168 write_state_a_string (current->name);
991 else 1169 else
992 write_state_a_string ("nil"); 1170 write_state_a_string ("nil");
993 1171
994 write_state_type (current->type); 1172 write_state_type (current->type);
995 write_state_fileloc (&(current->line)); 1173 write_state_fileloc (&(current->line));
996 write_state_options (current->opt); 1174 write_state_options (current->opt);
997 1175
998 fprintf (state_file, ")"); 1176 /* Terminate the "pair" s-expression. */
1177 end_s_expr ();
999 } 1178 }
1000 1179
1001 /* Write a pair list and return the number of pairs written. */ 1180 /* Write a pair list and return the number of pairs written. */
1002 static int 1181 int
1003 write_state_pair_list (pair_p list) 1182 state_writer::write_state_pair_list (pair_p list)
1004 { 1183 {
1005 int nbpair = 0; 1184 int nbpair = 0;
1006 pair_p current; 1185 pair_p current;
1007 1186
1008 for (current = list; current != NULL; current = current->next) 1187 for (current = list; current != NULL; current = current->next)
1012 } 1191 }
1013 return nbpair; 1192 return nbpair;
1014 1193
1015 } 1194 }
1016 1195
1017 /* When writing imported linked lists, like typedefs, structures, 1196 /* When writing imported linked lists, like typedefs, structures, ... we count
1018 param_structs, ... we count their length first and write it. These 1197 their length first and write it. This eases the reading, and enables an
1019 eases the reading, and enables an extra verification on the number 1198 extra verification on the number of actually read items. */
1020 of actually read items. */
1021 1199
1022 /* Write our typedefs. */ 1200 /* Write our typedefs. */
1023 static void 1201 void
1024 write_state_typedefs (void) 1202 state_writer::write_state_typedefs (void)
1025 { 1203 {
1026 int nbtypedefs = pair_list_length (typedefs); 1204 int nbtypedefs = pair_list_length (typedefs);
1027 int nbpairs = 0; 1205 int nbpairs = 0;
1028 fprintf (state_file, "\n(!typedefs %d\n", nbtypedefs); 1206 begin_s_expr ("typedefs");
1207 fprintf (state_file, "%d", nbtypedefs);
1029 nbpairs = write_state_pair_list (typedefs); 1208 nbpairs = write_state_pair_list (typedefs);
1030 gcc_assert (nbpairs == nbtypedefs); 1209 gcc_assert (nbpairs == nbtypedefs);
1031 fprintf (state_file, ")\n"); 1210 end_s_expr ();
1032 if (verbosity_level >= 2) 1211 if (verbosity_level >= 2)
1033 printf ("%s wrote %d typedefs\n", progname, nbtypedefs); 1212 printf ("%s wrote %d typedefs\n", progname, nbtypedefs);
1034 } 1213 }
1035 1214
1036 /* Write our structures. */ 1215 /* Write our structures. */
1037 static void 1216 void
1038 write_state_structures (void) 1217 state_writer::write_state_structures (void)
1039 { 1218 {
1040 int nbstruct = 0; 1219 int nbstruct = 0;
1041 type_p current; 1220 type_p current;
1042 1221
1043 for (current = structures; current != NULL; current = current->next) 1222 for (current = structures; current != NULL; current = current->next)
1044 nbstruct++; 1223 nbstruct++;
1045 1224
1046 fprintf (state_file, "\n(!structures %d\n", nbstruct); 1225 begin_s_expr ("structures");
1226 fprintf (state_file, "%d", nbstruct);
1047 1227
1048 for (current = structures; current != NULL; current = current->next) 1228 for (current = structures; current != NULL; current = current->next)
1049 write_state_type (current); 1229 {
1050 1230 write_new_line ();
1051 fprintf (state_file, ")\n"); 1231 write_state_type (current);
1232 }
1233
1234 /* Terminate the "structures" s-expression. */
1235 end_s_expr ();
1052 if (verbosity_level >= 2) 1236 if (verbosity_level >= 2)
1053 printf ("%s wrote %d structures in state\n", progname, nbstruct); 1237 printf ("%s wrote %d structures in state\n", progname, nbstruct);
1054 } 1238 }
1055 1239
1056 /* Write our param_struct-s. */
1057 static void
1058 write_state_param_structs (void)
1059 {
1060 int nbparamstruct = 0;
1061 type_p current;
1062
1063 for (current = param_structs; current != NULL; current = current->next)
1064 nbparamstruct++;
1065
1066 fprintf (state_file, "\n(!param_structs %d\n", nbparamstruct);
1067
1068 for (current = param_structs; current != NULL; current = current->next)
1069 write_state_type (current);
1070
1071 fprintf (state_file, ")\n");
1072 }
1073
1074 /* Write our variables. */ 1240 /* Write our variables. */
1075 static void 1241 void
1076 write_state_variables (void) 1242 state_writer::write_state_variables (void)
1077 { 1243 {
1078 int nbvars = pair_list_length (variables); 1244 int nbvars = pair_list_length (variables);
1079 int nbpairs = 0; 1245 int nbpairs = 0;
1080 fprintf (state_file, "\n(!variables %d\n", nbvars); 1246 begin_s_expr ("variables");
1247 fprintf (state_file, "%d", nbvars);
1081 nbpairs = write_state_pair_list (variables); 1248 nbpairs = write_state_pair_list (variables);
1082 gcc_assert (nbpairs == nbvars); 1249 gcc_assert (nbpairs == nbvars);
1083 fprintf (state_file, ")\n"); 1250 end_s_expr ();
1084 if (verbosity_level >= 2) 1251 if (verbosity_level >= 2)
1085 printf ("%s wrote %d variables.\n", progname, nbvars); 1252 printf ("%s wrote %d variables.\n", progname, nbvars);
1086 } 1253 }
1087 1254
1088 /* Write the source directory. File locations within the source 1255 /* Write the source directory. File locations within the source
1089 directory have been written specifically. */ 1256 directory have been written specifically. */
1090 static void 1257 void
1091 write_state_srcdir (void) 1258 state_writer::write_state_srcdir (void)
1092 { 1259 {
1093 fprintf (state_file, "\n(!srcdir "); 1260 begin_s_expr ("srcdir");
1094 write_state_a_string (srcdir); 1261 write_state_a_string (srcdir);
1095 fprintf (state_file, ")\n"); 1262 end_s_expr ();
1096 } 1263 }
1097 1264
1098 /* Count and write the list of our files. */ 1265 /* Count and write the list of our files. */
1099 static void 1266 void
1100 write_state_files_list (void) 1267 state_writer::write_state_files_list (void)
1101 { 1268 {
1102 int i = 0; 1269 int i = 0;
1103 /* Write the list of files with their lang_bitmap. */ 1270 /* Write the list of files with their lang_bitmap. */
1104 fprintf (state_file, "\n(!fileslist %d\n", (int) num_gt_files); 1271 begin_s_expr ("fileslist");
1272 fprintf (state_file, "%d", (int) num_gt_files);
1105 for (i = 0; i < (int) num_gt_files; i++) 1273 for (i = 0; i < (int) num_gt_files; i++)
1106 { 1274 {
1107 const char *cursrcrelpath = NULL; 1275 const char *cursrcrelpath = NULL;
1108 const input_file *curfil = gt_files[i]; 1276 const input_file *curfil = gt_files[i];
1109 /* Most of the files are inside $(srcdir) so it is worth to 1277 /* Most of the files are inside $(srcdir) so it is worth to
1110 handle them specially. */ 1278 handle them specially. */
1111 cursrcrelpath = get_file_srcdir_relative_path (curfil); 1279 cursrcrelpath = get_file_srcdir_relative_path (curfil);
1112 if (cursrcrelpath) 1280 if (cursrcrelpath)
1113 { 1281 {
1114 fprintf (state_file, "(!srcfile %d ", get_lang_bitmap (curfil)); 1282 begin_s_expr ("srcfile");
1283 fprintf (state_file, "%d ", get_lang_bitmap (curfil));
1115 write_state_a_string (cursrcrelpath); 1284 write_state_a_string (cursrcrelpath);
1116 } 1285 }
1117 else 1286 else
1118 { 1287 {
1119 fprintf (state_file, "(!file %d ", get_lang_bitmap (curfil)); 1288 begin_s_expr ("file");
1289 fprintf (state_file, "%d ", get_lang_bitmap (curfil));
1120 write_state_a_string (get_input_file_name (curfil)); 1290 write_state_a_string (get_input_file_name (curfil));
1121 } 1291 }
1122 fprintf (state_file, ")\n"); 1292 /* Terminate the inner s-expression (either "srcfile" or "file"). */
1123 } 1293 end_s_expr ();
1124 fprintf (state_file, ")\n"); 1294 }
1295 /* Terminate the "fileslist" s-expression. */
1296 end_s_expr ();
1125 } 1297 }
1126 1298
1127 /* Write the list of GCC front-end languages. */ 1299 /* Write the list of GCC front-end languages. */
1128 static void 1300 void
1129 write_state_languages (void) 1301 state_writer::write_state_languages (void)
1130 { 1302 {
1131 int i = 0; 1303 int i = 0;
1132 fprintf (state_file, "\n(!languages %d", (int) num_lang_dirs); 1304 begin_s_expr ("languages");
1305 fprintf (state_file, "%d", (int) num_lang_dirs);
1133 for (i = 0; i < (int) num_lang_dirs; i++) 1306 for (i = 0; i < (int) num_lang_dirs; i++)
1134 { 1307 {
1135 /* Languages names are identifiers, we expect only letters or 1308 /* Languages names are identifiers, we expect only letters or
1136 underscores or digits in them. In particular, C++ is not a 1309 underscores or digits in them. In particular, C++ is not a
1137 valid language name, but cp is valid. */ 1310 valid language name, but cp is valid. */
1138 fprintf (state_file, " %s", lang_dir_names[i]); 1311 fprintf (state_file, " %s", lang_dir_names[i]);
1139 } 1312 }
1140 fprintf (state_file, ")\n"); 1313 end_s_expr ();
1141 } 1314 }
1142 1315
1143 /* Write the trailer. */ 1316 /* Write the trailer. */
1144 static void 1317 static void
1145 write_state_trailer (void) 1318 write_state_trailer (void)
1188 fprintf (state_file, 1361 fprintf (state_file,
1189 ";;; Don't parse this file wihout knowing GCC gengtype internals.\n"); 1362 ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1190 fprintf (state_file, 1363 fprintf (state_file,
1191 ";;; This file should be parsed by the same %s which wrote it.\n", 1364 ";;; This file should be parsed by the same %s which wrote it.\n",
1192 progname); 1365 progname);
1193 fprintf (state_file, ";;; file %s generated on %s\n", state_path, 1366
1194 ctime (&now)); 1367 state_writer sw;
1368
1195 /* The first non-comment significant line gives the version string. */ 1369 /* The first non-comment significant line gives the version string. */
1196 write_state_version (version_string); 1370 sw.write_state_version (version_string);
1197 write_state_srcdir (); 1371 sw.write_state_srcdir ();
1198 write_state_languages (); 1372 sw.write_state_languages ();
1199 write_state_files_list (); 1373 sw.write_state_files_list ();
1200 write_state_structures (); 1374 sw.write_state_structures ();
1201 write_state_typedefs (); 1375 sw.write_state_typedefs ();
1202 write_state_param_structs (); 1376 sw.write_state_variables ();
1203 write_state_variables ();
1204 write_state_trailer (); 1377 write_state_trailer ();
1205 statelen = ftell (state_file); 1378 statelen = ftell (state_file);
1206 if (ferror (state_file)) 1379 if (ferror (state_file))
1207 fatal ("output error when writing state file %s [%s]", 1380 fatal ("output error when writing state file %s [%s]",
1208 temp_state_path, xstrerror (errno)); 1381 temp_state_path, xstrerror (errno));
1214 state_path, xstrerror (errno)); 1387 state_path, xstrerror (errno));
1215 free (temp_state_path); 1388 free (temp_state_path);
1216 1389
1217 if (verbosity_level >= 1) 1390 if (verbosity_level >= 1)
1218 printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n", 1391 printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1219 progname, state_path, statelen, state_written_type_count); 1392 progname, state_path, statelen, sw.m_state_written_type_count);
1220 1393
1221 } 1394 }
1222 1395
1223 /** End of writing routines! The corresponding reading routines follow. **/ 1396 /** End of writing routines! The corresponding reading routines follow. **/
1224 1397
1294 { 1467 {
1295 *type = &scalar_char; 1468 *type = &scalar_char;
1296 read_state_common_type_content (*type); 1469 read_state_common_type_content (*type);
1297 } 1470 }
1298 1471
1299
1300 /* Read the string_type. */ 1472 /* Read the string_type. */
1301 static void 1473 static void
1302 read_state_string_type (type_p *type) 1474 read_state_string_type (type_p *type)
1303 { 1475 {
1304 *type = &string_type; 1476 *type = &string_type;
1319 next_state_tokens (1); 1491 next_state_tokens (1);
1320 } 1492 }
1321 else 1493 else
1322 { 1494 {
1323 fatal_reading_state (t, "Bad syntax for bitmap"); 1495 fatal_reading_state (t, "Bad syntax for bitmap");
1496 }
1497 }
1498
1499
1500 /* Read an undefined type. */
1501 static void
1502 read_state_undefined_type (type_p type)
1503 {
1504 struct state_token_st *t0;
1505
1506 type->kind = TYPE_UNDEFINED;
1507 read_state_common_type_content (type);
1508 t0 = peek_state_token (0);
1509 if (state_token_kind (t0) == STOK_STRING)
1510 {
1511 if (state_token_is_name (t0, "nil"))
1512 {
1513 type->u.s.tag = NULL;
1514 DBGPRINTF ("read anonymous undefined type @%p #%d",
1515 (void *) type, type->state_number);
1516 }
1517 else
1518 {
1519 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1520 DBGPRINTF ("read undefined type @%p #%d '%s'",
1521 (void *) type, type->state_number, type->u.s.tag);
1522 }
1523
1524 next_state_tokens (1);
1525 read_state_fileloc (&(type->u.s.line));
1526 }
1527 else
1528 {
1529 fatal_reading_state (t0, "Bad tag in undefined type");
1324 } 1530 }
1325 } 1531 }
1326 1532
1327 1533
1328 /* Read a GTY-ed struct type. */ 1534 /* Read a GTY-ed struct type. */
1353 read_state_fileloc (&(type->u.s.line)); 1559 read_state_fileloc (&(type->u.s.line));
1354 read_state_fields (&(type->u.s.fields)); 1560 read_state_fields (&(type->u.s.fields));
1355 read_state_options (&(type->u.s.opt)); 1561 read_state_options (&(type->u.s.opt));
1356 read_state_lang_bitmap (&(type->u.s.bitmap)); 1562 read_state_lang_bitmap (&(type->u.s.bitmap));
1357 read_state_type (&(type->u.s.lang_struct)); 1563 read_state_type (&(type->u.s.lang_struct));
1564 read_state_type (&(type->u.s.base_class));
1565 if (type->u.s.base_class)
1566 add_subclass (type->u.s.base_class, type);
1358 } 1567 }
1359 else 1568 else
1360 { 1569 {
1361 fatal_reading_state (t0, "Bad tag in struct type"); 1570 fatal_reading_state (t0, "Bad tag in struct type");
1571 }
1572 }
1573
1574
1575 /* Read a GTY-ed user-provided struct TYPE. */
1576
1577 static void
1578 read_state_user_struct_type (type_p type)
1579 {
1580 struct state_token_st *t0;
1581
1582 type->kind = TYPE_USER_STRUCT;
1583 read_state_common_type_content (type);
1584 t0 = peek_state_token (0);
1585 if (state_token_kind (t0) == STOK_STRING)
1586 {
1587 if (state_token_is_name (t0, "nil"))
1588 {
1589 type->u.s.tag = NULL;
1590 DBGPRINTF ("read anonymous struct type @%p #%d",
1591 (void *) type, type->state_number);
1592 }
1593 else
1594 {
1595 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1596 DBGPRINTF ("read struct type @%p #%d '%s'",
1597 (void *) type, type->state_number, type->u.s.tag);
1598 }
1599
1600 next_state_tokens (1);
1601 read_state_fileloc (&(type->u.s.line));
1602 read_state_fields (&(type->u.s.fields));
1603 }
1604 else
1605 {
1606 fatal_reading_state (t0, "Bad tag in user-struct type");
1362 } 1607 }
1363 } 1608 }
1364 1609
1365 1610
1366 /* Read a GTY-ed union type. */ 1611 /* Read a GTY-ed union type. */
1510 else 1755 else
1511 fatal_reading_state (t0, "expecting !homotypes for lang_struct"); 1756 fatal_reading_state (t0, "expecting !homotypes for lang_struct");
1512 } 1757 }
1513 1758
1514 1759
1515 /* Read a param_struct type for GTY parametrized structures. */
1516 static void
1517 read_state_param_struct_type (type_p type)
1518 {
1519 int i;
1520 struct state_token_st *t0;
1521
1522 type->kind = TYPE_PARAM_STRUCT;
1523 read_state_common_type_content (type);
1524 DBGPRINTF ("read param_struct type @%p #%d",
1525 (void *) type, type->state_number);
1526 read_state_type (&(type->u.param_struct.stru));
1527
1528 for (i = 0; i < NUM_PARAM; i++)
1529 {
1530 t0 = peek_state_token (0);
1531 if (state_token_is_name (t0, "nil"))
1532 {
1533 type->u.param_struct.param[i] = NULL;
1534 next_state_tokens (1);
1535 }
1536 else
1537 read_state_type (&(type->u.param_struct.param[i]));
1538 }
1539 read_state_fileloc (&(type->u.param_struct.line));
1540 }
1541
1542
1543 /* Read the gc used information. */ 1760 /* Read the gc used information. */
1544 static void 1761 static void
1545 read_state_gc_used (enum gc_used_enum *pgus) 1762 read_state_gc_used (enum gc_used_enum *pgus)
1546 { 1763 {
1547 struct state_token_st *t0 = peek_state_token (0); 1764 struct state_token_st *t0 = peek_state_token (0);
1614 } 1831 }
1615 else if (state_token_is_name (t0, "string")) 1832 else if (state_token_is_name (t0, "string"))
1616 { 1833 {
1617 next_state_tokens (1); 1834 next_state_tokens (1);
1618 read_state_string_type (current); 1835 read_state_string_type (current);
1836 }
1837 else if (state_token_is_name (t0, "undefined"))
1838 {
1839 *current = XCNEW (struct type);
1840 next_state_tokens (1);
1841 read_state_undefined_type (*current);
1619 } 1842 }
1620 else if (state_token_is_name (t0, "struct")) 1843 else if (state_token_is_name (t0, "struct"))
1621 { 1844 {
1622 *current = XCNEW (struct type); 1845 *current = XCNEW (struct type);
1623 next_state_tokens (1); 1846 next_state_tokens (1);
1633 { 1856 {
1634 *current = XCNEW (struct type); 1857 *current = XCNEW (struct type);
1635 next_state_tokens (1); 1858 next_state_tokens (1);
1636 read_state_lang_struct_type (*current); 1859 read_state_lang_struct_type (*current);
1637 } 1860 }
1638 else if (state_token_is_name (t0, "param_struct"))
1639 {
1640 *current = XCNEW (struct type);
1641 next_state_tokens (1);
1642 read_state_param_struct_type (*current);
1643 }
1644 else if (state_token_is_name (t0, "pointer")) 1861 else if (state_token_is_name (t0, "pointer"))
1645 { 1862 {
1646 *current = XCNEW (struct type); 1863 *current = XCNEW (struct type);
1647 next_state_tokens (1); 1864 next_state_tokens (1);
1648 read_state_pointer_type (*current); 1865 read_state_pointer_type (*current);
1650 else if (state_token_is_name (t0, "array")) 1867 else if (state_token_is_name (t0, "array"))
1651 { 1868 {
1652 *current = XCNEW (struct type); 1869 *current = XCNEW (struct type);
1653 next_state_tokens (1); 1870 next_state_tokens (1);
1654 read_state_array_type (*current); 1871 read_state_array_type (*current);
1872 }
1873 else if (state_token_is_name (t0, "user_struct"))
1874 {
1875 *current = XCNEW (struct type);
1876 next_state_tokens (1);
1877 read_state_user_struct_type (*current);
1655 } 1878 }
1656 else 1879 else
1657 fatal_reading_state (t0, "bad type in (!type"); 1880 fatal_reading_state (t0, "bad type in (!type");
1658 } 1881 }
1659 t0 = peek_state_token (0); 1882 t0 = peek_state_token (0);
2125 "expected %d structures but got %d", 2348 "expected %d structures but got %d",
2126 nbstruct, countstruct); 2349 nbstruct, countstruct);
2127 if (verbosity_level >= 2) 2350 if (verbosity_level >= 2)
2128 printf ("%s read %d structures from state\n", progname, nbstruct); 2351 printf ("%s read %d structures from state\n", progname, nbstruct);
2129 *structures = head; 2352 *structures = head;
2130 }
2131
2132
2133 /* Read the param_struct-s. */
2134 static void
2135 read_state_param_structs (type_p *param_structs)
2136 {
2137 int nbparamstructs = 0;
2138 int countparamstructs = 0;
2139 type_p head = NULL;
2140 type_p previous;
2141 type_p tmp;
2142 struct state_token_st *t0 = peek_state_token (0);
2143 struct state_token_st *t1 = peek_state_token (1);
2144 struct state_token_st *t2 = peek_state_token (2);
2145
2146 if (state_token_kind (t0) == STOK_LEFTPAR
2147 && state_token_is_name (t1, "!param_structs")
2148 && state_token_kind (t2) == STOK_INTEGER)
2149 {
2150 nbparamstructs = t2->stok_un.stok_num;
2151 next_state_tokens (3);
2152 t0 = t1 = t2 = NULL;
2153 t0 = peek_state_token (0);
2154 while (state_token_kind (t0) != STOK_RIGHTPAR)
2155 {
2156 tmp = NULL;
2157 read_state_type (&tmp);
2158 if (head == NULL)
2159 {
2160 head = tmp;
2161 previous = head;
2162 }
2163 else
2164 {
2165 previous->next = tmp;
2166 previous = tmp;
2167 }
2168 t0 = peek_state_token (0);
2169 countparamstructs++;
2170 }
2171 next_state_tokens (1);
2172 }
2173 else
2174 fatal_reading_state (t0, "Bad param_structs syntax");
2175 t0 = peek_state_token (0);
2176 if (countparamstructs != nbparamstructs)
2177 fatal_reading_state_printf
2178 (t0,
2179 "invalid number of param_structs expected %d got %d",
2180 nbparamstructs, countparamstructs);
2181 *param_structs = head;
2182 } 2353 }
2183 2354
2184 2355
2185 /* Read the variables. */ 2356 /* Read the variables. */
2186 static void 2357 static void
2339 else 2510 else
2340 fatal_reading_state (t0, 2511 fatal_reading_state (t0,
2341 "expecting file in !fileslist of state file"); 2512 "expecting file in !fileslist of state file");
2342 }; 2513 };
2343 t0 = peek_state_token (0); 2514 t0 = peek_state_token (0);
2344 if (!state_token_kind (t0) == STOK_RIGHTPAR) 2515 if (state_token_kind (t0) != STOK_RIGHTPAR)
2345 fatal_reading_state (t0, "missing ) for !fileslist in state file"); 2516 fatal_reading_state (t0, "missing ) for !fileslist in state file");
2346 next_state_tokens (1); 2517 next_state_tokens (1);
2347 } 2518 }
2348 else 2519 else
2349 fatal_reading_state (t0, "missing !fileslist in state file"); 2520 fatal_reading_state (t0, "missing !fileslist in state file");
2381 { 2552 {
2382 const struct type *type1 = (const struct type *) ty1; 2553 const struct type *type1 = (const struct type *) ty1;
2383 const struct type *type2 = (const struct type *) ty2; 2554 const struct type *type2 = (const struct type *) ty2;
2384 2555
2385 return type1->state_number == type2->state_number; 2556 return type1->state_number == type2->state_number;
2557 }
2558
2559 static int
2560 string_eq (const void *a, const void *b)
2561 {
2562 const char *a0 = (const char *)a;
2563 const char *b0 = (const char *)b;
2564
2565 return (strcmp (a0, b0) == 0);
2386 } 2566 }
2387 2567
2388 2568
2389 /* The function reading the state, called by main from gengtype.c. */ 2569 /* The function reading the state, called by main from gengtype.c. */
2390 void 2570 void
2406 } 2586 }
2407 2587
2408 state_seen_types = 2588 state_seen_types =
2409 htab_create (2017, hash_type_number, equals_type_number, NULL); 2589 htab_create (2017, hash_type_number, equals_type_number, NULL);
2410 state_ident_tab = 2590 state_ident_tab =
2411 htab_create (4027, htab_hash_string, (htab_eq) strcmp, NULL); 2591 htab_create (4027, htab_hash_string, string_eq, NULL);
2412 read_state_version (version_string); 2592 read_state_version (version_string);
2413 read_state_srcdir (); 2593 read_state_srcdir ();
2414 read_state_languages (); 2594 read_state_languages ();
2415 read_state_files_list (); 2595 read_state_files_list ();
2416 read_state_structures (&structures); 2596 read_state_structures (&structures);
2417 if (ferror (state_file)) 2597 if (ferror (state_file))
2418 fatal_reading_state_printf 2598 fatal_reading_state_printf
2419 (NULL_STATE_TOKEN, "input error while reading state [%s]", 2599 (NULL_STATE_TOKEN, "input error while reading state [%s]",
2420 xstrerror (errno)); 2600 xstrerror (errno));
2421 read_state_typedefs (&typedefs); 2601 read_state_typedefs (&typedefs);
2422 read_state_param_structs (&param_structs);
2423 read_state_variables (&variables); 2602 read_state_variables (&variables);
2424 read_state_trailer (); 2603 read_state_trailer ();
2425 2604
2426 if (verbosity_level >= 1) 2605 if (verbosity_level >= 1)
2427 { 2606 {