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