1 /* Gengtype persistent state serialization & de-serialization.
2 Useful for gengtype in plugin mode.
4 Copyright (C) 2010-2014 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
);
959 write_state_type (current
->u
.s
.base_class
);
962 /* Write a GTY user-defined struct type. */
964 state_writer::write_state_user_struct_type (type_p current
)
966 DBGPRINTF ("user_struct type @ %p #%d '%s'", (void *) current
,
967 current
->state_number
, current
->u
.s
.tag
);
968 write_any_indent (0);
969 fprintf (state_file
, "user_struct ");
970 write_state_common_type_content (current
);
971 if (current
->u
.s
.tag
!= NULL
)
972 write_state_a_string (current
->u
.s
.tag
);
975 write_any_indent (0);
976 fprintf (state_file
, "nil");
978 write_state_fileloc (type_lineloc (current
));
979 write_state_fields (current
->u
.s
.fields
);
982 /* write a GTY union type. */
984 state_writer::write_state_union_type (type_p current
)
986 write_state_struct_union_type (current
, "union");
987 write_state_type (current
->u
.s
.lang_struct
);
990 /* Write a lang_struct type. This is tricky and was painful to debug,
991 we deal with the next field specifically within their lang_struct
992 subfield, which points to a linked list of homonumous types.
993 Change this function with extreme care, see also
994 read_state_lang_struct_type. */
996 state_writer::write_state_lang_struct_type (type_p current
)
1000 const char *homoname
= 0;
1001 write_state_struct_union_type (current
, "lang_struct");
1002 /* lang_struct-ures are particularly tricky, since their
1003 u.s.lang_struct field gives a list of homonymous struct-s or
1005 DBGPRINTF ("lang_struct @ %p #%d", (void *) current
, current
->state_number
);
1006 for (hty
= current
->u
.s
.lang_struct
; hty
!= NULL
; hty
= hty
->next
)
1009 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype
,
1010 (void *) hty
, hty
->state_number
, hty
->u
.s
.tag
);
1011 /* Every member of the homonymous list should have the same tag. */
1012 gcc_assert (union_or_struct_p (hty
));
1013 gcc_assert (hty
->u
.s
.lang_struct
== current
);
1015 homoname
= hty
->u
.s
.tag
;
1016 gcc_assert (strcmp (homoname
, hty
->u
.s
.tag
) == 0);
1018 begin_s_expr ("homotypes");
1019 fprintf (state_file
, "%d", nbhomontype
);
1020 for (hty
= current
->u
.s
.lang_struct
; hty
!= NULL
; hty
= hty
->next
)
1021 write_state_type (hty
);
1025 /* Write a parametrized structure GTY type. */
1027 state_writer::write_state_param_struct_type (type_p current
)
1031 write_any_indent (0);
1032 fprintf (state_file
, "param_struct ");
1033 write_state_common_type_content (current
);
1034 write_state_type (current
->u
.param_struct
.stru
);
1035 for (i
= 0; i
< NUM_PARAM
; i
++)
1037 if (current
->u
.param_struct
.param
[i
] != NULL
)
1038 write_state_type (current
->u
.param_struct
.param
[i
]);
1041 write_any_indent (0);
1042 fprintf (state_file
, "nil ");
1045 write_state_fileloc (¤t
->u
.param_struct
.line
);
1048 /* Write a pointer type. */
1050 state_writer::write_state_pointer_type (type_p current
)
1052 write_any_indent (0);
1053 fprintf (state_file
, "pointer ");
1054 write_state_common_type_content (current
);
1055 write_state_type (current
->u
.p
);
1058 /* Write an array type. */
1060 state_writer::write_state_array_type (type_p current
)
1062 write_any_indent (0);
1063 fprintf (state_file
, "array ");
1064 write_state_common_type_content (current
);
1065 if (current
->u
.a
.len
!= NULL
)
1066 write_state_a_string (current
->u
.a
.len
);
1069 write_any_indent (1);
1070 fprintf (state_file
, " nil");
1073 write_any_indent (1);
1074 fprintf (state_file
, " ");
1075 write_state_type (current
->u
.a
.p
);
1078 /* Write the gc_used information. */
1080 state_writer::write_state_gc_used (enum gc_used_enum gus
)
1082 write_any_indent (1);
1086 fprintf (state_file
, " gc_unused");
1089 fprintf (state_file
, " gc_used");
1091 case GC_MAYBE_POINTED_TO
:
1092 fprintf (state_file
, " gc_maybe_pointed_to");
1095 fprintf (state_file
, " gc_pointed_to");
1102 /* Utility routine to write the common content of all types. Notice
1103 that the next field is *not* written on purpose. */
1105 state_writer::write_state_common_type_content (type_p current
)
1107 write_any_indent (0);
1108 fprintf (state_file
, "%d ", current
->state_number
);
1109 /* We do not write the next type, because list of types are
1110 explicitly written. However, lang_struct are special in that
1111 respect. See function write_state_lang_struct_type for more. */
1112 write_state_type (current
->pointer_to
);
1113 write_state_gc_used (current
->gc_used
);
1117 /* The important and recursive routine writing GTY types as understood
1118 by gengtype. Types which have a positive state_number have already
1119 been seen and written. */
1121 state_writer::write_state_type (type_p current
)
1123 write_any_indent (0);
1124 if (current
== NULL
)
1126 fprintf (state_file
, "nil ");
1130 begin_s_expr ("type");
1132 if (current
->state_number
> 0)
1134 write_any_indent (0);
1135 fprintf (state_file
, "already_seen %d", current
->state_number
);
1139 m_state_written_type_count
++;
1140 DBGPRINTF ("writing type #%d @%p old number %d", m_state_written_type_count
,
1141 (void *) current
, current
->state_number
);
1142 current
->state_number
= m_state_written_type_count
;
1143 switch (current
->kind
)
1147 case TYPE_UNDEFINED
:
1148 write_state_undefined_type (current
);
1151 write_state_struct_type (current
);
1153 case TYPE_USER_STRUCT
:
1154 write_state_user_struct_type (current
);
1157 write_state_union_type (current
);
1160 write_state_pointer_type (current
);
1163 write_state_array_type (current
);
1165 case TYPE_LANG_STRUCT
:
1166 write_state_lang_struct_type (current
);
1168 case TYPE_PARAM_STRUCT
:
1169 write_state_param_struct_type (current
);
1172 write_state_scalar_type (current
);
1175 write_state_string_type (current
);
1180 /* Terminate the "type" s-expression. */
1187 state_writer::write_state_pair (pair_p current
)
1189 if (current
== NULL
)
1191 write_any_indent (0);
1192 fprintf (state_file
, "nil)");
1196 begin_s_expr ("pair");
1198 if (current
->name
!= NULL
)
1199 write_state_a_string (current
->name
);
1201 write_state_a_string ("nil");
1203 write_state_type (current
->type
);
1204 write_state_fileloc (&(current
->line
));
1205 write_state_options (current
->opt
);
1207 /* Terminate the "pair" s-expression. */
1211 /* Write a pair list and return the number of pairs written. */
1213 state_writer::write_state_pair_list (pair_p list
)
1218 for (current
= list
; current
!= NULL
; current
= current
->next
)
1220 write_state_pair (current
);
1227 /* When writing imported linked lists, like typedefs, structures,
1228 param_structs, ... we count their length first and write it. These
1229 eases the reading, and enables an extra verification on the number
1230 of actually read items. */
1232 /* Write our typedefs. */
1234 state_writer::write_state_typedefs (void)
1236 int nbtypedefs
= pair_list_length (typedefs
);
1238 begin_s_expr ("typedefs");
1239 fprintf (state_file
, "%d", nbtypedefs
);
1240 nbpairs
= write_state_pair_list (typedefs
);
1241 gcc_assert (nbpairs
== nbtypedefs
);
1243 if (verbosity_level
>= 2)
1244 printf ("%s wrote %d typedefs\n", progname
, nbtypedefs
);
1247 /* Write our structures. */
1249 state_writer::write_state_structures (void)
1254 for (current
= structures
; current
!= NULL
; current
= current
->next
)
1257 begin_s_expr ("structures");
1258 fprintf (state_file
, "%d", nbstruct
);
1260 for (current
= structures
; current
!= NULL
; current
= current
->next
)
1263 write_state_type (current
);
1266 /* Terminate the "structures" s-expression. */
1268 if (verbosity_level
>= 2)
1269 printf ("%s wrote %d structures in state\n", progname
, nbstruct
);
1272 /* Write our param_struct-s. */
1274 state_writer::write_state_param_structs (void)
1276 int nbparamstruct
= 0;
1279 for (current
= param_structs
; current
!= NULL
; current
= current
->next
)
1282 begin_s_expr ("param_structs");
1283 fprintf (state_file
, "%d", nbparamstruct
);
1285 for (current
= param_structs
; current
!= NULL
; current
= current
->next
)
1286 write_state_type (current
);
1291 /* Write our variables. */
1293 state_writer::write_state_variables (void)
1295 int nbvars
= pair_list_length (variables
);
1297 begin_s_expr ("variables");
1298 fprintf (state_file
, "%d", nbvars
);
1299 nbpairs
= write_state_pair_list (variables
);
1300 gcc_assert (nbpairs
== nbvars
);
1302 if (verbosity_level
>= 2)
1303 printf ("%s wrote %d variables.\n", progname
, nbvars
);
1306 /* Write the source directory. File locations within the source
1307 directory have been written specifically. */
1309 state_writer::write_state_srcdir (void)
1311 begin_s_expr ("srcdir");
1312 write_state_a_string (srcdir
);
1316 /* Count and write the list of our files. */
1318 state_writer::write_state_files_list (void)
1321 /* Write the list of files with their lang_bitmap. */
1322 begin_s_expr ("fileslist");
1323 fprintf (state_file
, "%d", (int) num_gt_files
);
1324 for (i
= 0; i
< (int) num_gt_files
; i
++)
1326 const char *cursrcrelpath
= NULL
;
1327 const input_file
*curfil
= gt_files
[i
];
1328 /* Most of the files are inside $(srcdir) so it is worth to
1329 handle them specially. */
1330 cursrcrelpath
= get_file_srcdir_relative_path (curfil
);
1333 begin_s_expr ("srcfile");
1334 fprintf (state_file
, "%d ", get_lang_bitmap (curfil
));
1335 write_state_a_string (cursrcrelpath
);
1339 begin_s_expr ("file");
1340 fprintf (state_file
, "%d ", get_lang_bitmap (curfil
));
1341 write_state_a_string (get_input_file_name (curfil
));
1343 /* Terminate the inner s-expression (either "srcfile" or "file"). */
1346 /* Terminate the "fileslist" s-expression. */
1350 /* Write the list of GCC front-end languages. */
1352 state_writer::write_state_languages (void)
1355 begin_s_expr ("languages");
1356 fprintf (state_file
, "%d", (int) num_lang_dirs
);
1357 for (i
= 0; i
< (int) num_lang_dirs
; i
++)
1359 /* Languages names are identifiers, we expect only letters or
1360 underscores or digits in them. In particular, C++ is not a
1361 valid language name, but cp is valid. */
1362 fprintf (state_file
, " %s", lang_dir_names
[i
]);
1367 /* Write the trailer. */
1369 write_state_trailer (void)
1371 /* This test should probably catch IO errors like disk full... */
1372 if (fputs ("\n(!endfile)\n", state_file
) == EOF
)
1373 fatal ("failed to write state trailer [%s]", xstrerror (errno
));
1376 /* The write_state routine is the only writing routine called by main
1377 in gengtype.c. To avoid messing the state if gengtype is
1378 interrupted or aborted, we write a temporary file and rename it
1379 after having written it in totality. */
1381 write_state (const char *state_path
)
1385 char *temp_state_path
= NULL
;
1386 char tempsuffix
[40];
1389 /* We write a unique temporary file which is renamed when complete
1390 * only. So even if gengtype is interrupted, the written state file
1391 * won't be partially written, since the temporary file is not yet
1392 * renamed in that case. */
1393 memset (tempsuffix
, 0, sizeof (tempsuffix
));
1394 snprintf (tempsuffix
, sizeof (tempsuffix
) - 1, "-%ld-%d.tmp", (long) now
,
1396 temp_state_path
= concat (state_path
, tempsuffix
, NULL
);
1397 state_file
= fopen (temp_state_path
, "w");
1398 if (state_file
== NULL
)
1399 fatal ("Failed to open file %s for writing state: %s",
1400 temp_state_path
, xstrerror (errno
));
1401 if (verbosity_level
>= 3)
1402 printf ("%s writing state file %s temporarily in %s\n",
1403 progname
, state_path
, temp_state_path
);
1404 /* This is the first line of the state. Perhaps the file utility
1405 could know about that, so don't change it often. */
1406 fprintf (state_file
, ";;;;@@@@ GCC gengtype state\n");
1407 /* Output a few comments for humans. */
1408 fprintf (state_file
,
1409 ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
1410 fprintf (state_file
,
1411 ";;; The format of this file is tied to a particular version of GCC.\n");
1412 fprintf (state_file
,
1413 ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1414 fprintf (state_file
,
1415 ";;; This file should be parsed by the same %s which wrote it.\n",
1420 /* The first non-comment significant line gives the version string. */
1421 sw
.write_state_version (version_string
);
1422 sw
.write_state_srcdir ();
1423 sw
.write_state_languages ();
1424 sw
.write_state_files_list ();
1425 sw
.write_state_structures ();
1426 sw
.write_state_typedefs ();
1427 sw
.write_state_param_structs ();
1428 sw
.write_state_variables ();
1429 write_state_trailer ();
1430 statelen
= ftell (state_file
);
1431 if (ferror (state_file
))
1432 fatal ("output error when writing state file %s [%s]",
1433 temp_state_path
, xstrerror (errno
));
1434 if (fclose (state_file
))
1435 fatal ("failed to close state file %s [%s]",
1436 temp_state_path
, xstrerror (errno
));
1437 if (rename (temp_state_path
, state_path
))
1438 fatal ("failed to rename %s to state file %s [%s]", temp_state_path
,
1439 state_path
, xstrerror (errno
));
1440 free (temp_state_path
);
1442 if (verbosity_level
>= 1)
1443 printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1444 progname
, state_path
, statelen
, sw
.m_state_written_type_count
);
1448 /** End of writing routines! The corresponding reading routines follow. **/
1452 /* Forward declarations, since some read_state_* functions are
1454 static void read_state_fileloc (struct fileloc
*line
);
1455 static void read_state_options (options_p
*opt
);
1456 static void read_state_type (type_p
*current
);
1457 static void read_state_pair (pair_p
*pair
);
1458 /* Return the number of pairs actually read. */
1459 static int read_state_pair_list (pair_p
*list
);
1460 static void read_state_fields (pair_p
*fields
);
1461 static void read_state_common_type_content (type_p current
);
1466 /* Record into the state_seen_types hash-table a type which we are
1467 reading, to enable recursive or circular references to it. */
1469 record_type (type_p type
)
1473 slot
= htab_find_slot (state_seen_types
, type
, INSERT
);
1479 /* Read an already seen type. */
1481 read_state_already_seen_type (type_p
*type
)
1483 struct state_token_st
*t0
= peek_state_token (0);
1485 if (state_token_kind (t0
) == STOK_INTEGER
)
1488 struct type loctype
= { TYPE_SCALAR
, 0, 0, 0, GC_UNUSED
, {0} };
1490 loctype
.state_number
= t0
->stok_un
.stok_num
;
1491 slot
= htab_find_slot (state_seen_types
, &loctype
, NO_INSERT
);
1494 fatal_reading_state (t0
, "Unknown type");
1497 next_state_tokens (1);
1498 *type
= (type_p
) *slot
;
1502 fatal_reading_state (t0
, "Bad seen type");
1507 /* Read the scalar_nonchar type. */
1509 read_state_scalar_nonchar_type (type_p
*type
)
1511 *type
= &scalar_nonchar
;
1512 read_state_common_type_content (*type
);
1516 /* Read the scalar_char type. */
1518 read_state_scalar_char_type (type_p
*type
)
1520 *type
= &scalar_char
;
1521 read_state_common_type_content (*type
);
1524 /* Read the string_type. */
1526 read_state_string_type (type_p
*type
)
1528 *type
= &string_type
;
1529 read_state_common_type_content (*type
);
1533 /* Read a lang_bitmap representing a set of GCC front-end languages. */
1535 read_state_lang_bitmap (lang_bitmap
*bitmap
)
1537 struct state_token_st
*t
;
1539 t
= peek_state_token (0);
1540 if (state_token_kind (t
) == STOK_INTEGER
)
1542 *bitmap
= t
->stok_un
.stok_num
;
1543 next_state_tokens (1);
1547 fatal_reading_state (t
, "Bad syntax for bitmap");
1552 /* Read an undefined type. */
1554 read_state_undefined_type (type_p type
)
1556 struct state_token_st
*t0
;
1558 type
->kind
= TYPE_UNDEFINED
;
1559 read_state_common_type_content (type
);
1560 t0
= peek_state_token (0);
1561 if (state_token_kind (t0
) == STOK_STRING
)
1563 if (state_token_is_name (t0
, "nil"))
1565 type
->u
.s
.tag
= NULL
;
1566 DBGPRINTF ("read anonymous undefined type @%p #%d",
1567 (void *) type
, type
->state_number
);
1571 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1572 DBGPRINTF ("read undefined type @%p #%d '%s'",
1573 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1576 next_state_tokens (1);
1577 read_state_fileloc (&(type
->u
.s
.line
));
1581 fatal_reading_state (t0
, "Bad tag in undefined type");
1586 /* Read a GTY-ed struct type. */
1588 read_state_struct_type (type_p type
)
1590 struct state_token_st
*t0
;
1592 type
->kind
= TYPE_STRUCT
;
1593 read_state_common_type_content (type
);
1594 t0
= peek_state_token (0);
1595 if (state_token_kind (t0
) == STOK_STRING
)
1597 if (state_token_is_name (t0
, "nil"))
1599 type
->u
.s
.tag
= NULL
;
1600 DBGPRINTF ("read anonymous struct type @%p #%d",
1601 (void *) type
, type
->state_number
);
1605 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1606 DBGPRINTF ("read struct type @%p #%d '%s'",
1607 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1610 next_state_tokens (1);
1611 read_state_fileloc (&(type
->u
.s
.line
));
1612 read_state_fields (&(type
->u
.s
.fields
));
1613 read_state_options (&(type
->u
.s
.opt
));
1614 read_state_lang_bitmap (&(type
->u
.s
.bitmap
));
1615 read_state_type (&(type
->u
.s
.lang_struct
));
1616 read_state_type (&(type
->u
.s
.base_class
));
1617 if (type
->u
.s
.base_class
)
1618 add_subclass (type
->u
.s
.base_class
, type
);
1622 fatal_reading_state (t0
, "Bad tag in struct type");
1627 /* Read a GTY-ed user-provided struct TYPE. */
1630 read_state_user_struct_type (type_p type
)
1632 struct state_token_st
*t0
;
1634 type
->kind
= TYPE_USER_STRUCT
;
1635 read_state_common_type_content (type
);
1636 t0
= peek_state_token (0);
1637 if (state_token_kind (t0
) == STOK_STRING
)
1639 if (state_token_is_name (t0
, "nil"))
1641 type
->u
.s
.tag
= NULL
;
1642 DBGPRINTF ("read anonymous struct type @%p #%d",
1643 (void *) type
, type
->state_number
);
1647 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1648 DBGPRINTF ("read struct type @%p #%d '%s'",
1649 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1652 next_state_tokens (1);
1653 read_state_fileloc (&(type
->u
.s
.line
));
1654 read_state_fields (&(type
->u
.s
.fields
));
1658 fatal_reading_state (t0
, "Bad tag in user-struct type");
1663 /* Read a GTY-ed union type. */
1665 read_state_union_type (type_p type
)
1667 struct state_token_st
*t0
;
1669 type
->kind
= TYPE_UNION
;
1670 read_state_common_type_content (type
);
1671 t0
= peek_state_token (0);
1672 if (state_token_kind (t0
) == STOK_STRING
)
1674 if (state_token_is_name (t0
, "nil"))
1676 type
->u
.s
.tag
= NULL
;
1677 DBGPRINTF ("read anonymous union type @%p #%d",
1678 (void *) type
, type
->state_number
);
1682 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1683 DBGPRINTF ("read union type @%p #%d '%s'",
1684 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1686 next_state_tokens (1);
1687 read_state_fileloc (&(type
->u
.s
.line
));
1688 read_state_fields (&(type
->u
.s
.fields
));
1689 read_state_options (&(type
->u
.s
.opt
));
1690 read_state_lang_bitmap (&(type
->u
.s
.bitmap
));
1691 read_state_type (&(type
->u
.s
.lang_struct
));
1694 fatal_reading_state (t0
, "Bad tag in union type");
1698 /* Read a GTY-ed pointer type. */
1700 read_state_pointer_type (type_p type
)
1702 type
->kind
= TYPE_POINTER
;
1703 read_state_common_type_content (type
);
1704 DBGPRINTF ("read pointer type @%p #%d", (void *) type
, type
->state_number
);
1705 read_state_type (&(type
->u
.p
));
1709 /* Read a GTY-ed array type. */
1711 read_state_array_type (type_p type
)
1713 struct state_token_st
*t0
;
1715 type
->kind
= TYPE_ARRAY
;
1716 read_state_common_type_content (type
);
1717 t0
= peek_state_token (0);
1718 if (state_token_kind (t0
) == STOK_STRING
)
1720 type
->u
.a
.len
= xstrdup (t0
->stok_un
.stok_string
);
1721 DBGPRINTF ("read array type @%p #%d length '%s'",
1722 (void *) type
, type
->state_number
, type
->u
.a
.len
);
1723 next_state_tokens (1);
1726 else if (state_token_is_name (t0
, "nil"))
1728 type
->u
.a
.len
= NULL
;
1729 DBGPRINTF ("read array type @%p #%d without length",
1730 (void *) type
, type
->state_number
);
1731 next_state_tokens (1);
1735 fatal_reading_state (t0
, "Bad array name type");
1736 read_state_type (&(type
->u
.a
.p
));
1741 /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
1742 front-end languages. This is a tricky function and it was painful
1743 to debug. Change it with extreme care. See also
1744 write_state_lang_struct_type. */
1746 read_state_lang_struct_type (type_p type
)
1748 struct state_token_st
*t0
= NULL
;
1749 struct state_token_st
*t1
= NULL
;
1750 struct state_token_st
*t2
= NULL
;
1752 type
->kind
= TYPE_LANG_STRUCT
;
1753 read_state_common_type_content (type
);
1754 t0
= peek_state_token (0);
1755 if (state_token_kind (t0
) == STOK_STRING
)
1757 if (state_token_is_name (t0
, "nil"))
1759 DBGPRINTF ("read anonymous lang_struct type @%p #%d",
1760 (void *) type
, type
->state_number
);
1761 type
->u
.s
.tag
= NULL
;
1765 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1766 DBGPRINTF ("read lang_struct type @%p #%d '%s'",
1767 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1769 next_state_tokens (1);
1772 fatal_reading_state (t0
, "Bad tag in lang struct type");
1773 read_state_fileloc (&(type
->u
.s
.line
));
1774 read_state_fields (&(type
->u
.s
.fields
));
1775 read_state_options (&(type
->u
.s
.opt
));
1776 read_state_lang_bitmap (&(type
->u
.s
.bitmap
));
1777 /* Within lang_struct-ures, the lang_struct field is a linked list
1778 of homonymous types! */
1779 t0
= peek_state_token (0);
1780 t1
= peek_state_token (1);
1781 t2
= peek_state_token (2);
1782 /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
1783 if (state_token_kind (t0
) == STOK_LEFTPAR
1784 && state_token_is_name (t1
, "!homotypes")
1785 && state_token_kind (t2
) == STOK_INTEGER
)
1787 type_p
*prevty
= &type
->u
.s
.lang_struct
;
1788 int nbhomotype
= t2
->stok_un
.stok_num
;
1790 t0
= t1
= t2
= NULL
;
1791 next_state_tokens (3);
1792 for (i
= 0; i
< nbhomotype
; i
++)
1794 read_state_type (prevty
);
1795 t0
= peek_state_token (0);
1797 prevty
= &(*prevty
)->next
;
1799 fatal_reading_state (t0
,
1800 "expecting type in homotype list for lang_struct");
1802 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1803 fatal_reading_state (t0
,
1804 "expecting ) in homotype list for lang_struct");
1805 next_state_tokens (1);
1808 fatal_reading_state (t0
, "expecting !homotypes for lang_struct");
1812 /* Read a param_struct type for GTY parametrized structures. */
1814 read_state_param_struct_type (type_p type
)
1817 struct state_token_st
*t0
;
1819 type
->kind
= TYPE_PARAM_STRUCT
;
1820 read_state_common_type_content (type
);
1821 DBGPRINTF ("read param_struct type @%p #%d",
1822 (void *) type
, type
->state_number
);
1823 read_state_type (&(type
->u
.param_struct
.stru
));
1825 for (i
= 0; i
< NUM_PARAM
; i
++)
1827 t0
= peek_state_token (0);
1828 if (state_token_is_name (t0
, "nil"))
1830 type
->u
.param_struct
.param
[i
] = NULL
;
1831 next_state_tokens (1);
1834 read_state_type (&(type
->u
.param_struct
.param
[i
]));
1836 read_state_fileloc (&(type
->u
.param_struct
.line
));
1840 /* Read the gc used information. */
1842 read_state_gc_used (enum gc_used_enum
*pgus
)
1844 struct state_token_st
*t0
= peek_state_token (0);
1845 if (state_token_is_name (t0
, "gc_unused"))
1847 else if (state_token_is_name (t0
, "gc_used"))
1849 else if (state_token_is_name (t0
, "gc_maybe_pointed_to"))
1850 *pgus
= GC_MAYBE_POINTED_TO
;
1851 else if (state_token_is_name (t0
, "gc_pointed_to"))
1852 *pgus
= GC_POINTED_TO
;
1854 fatal_reading_state (t0
, "invalid gc_used information");
1855 next_state_tokens (1);
1859 /* Utility function to read the common content of types. */
1861 read_state_common_type_content (type_p current
)
1863 struct state_token_st
*t0
= peek_state_token (0);
1865 if (state_token_kind (t0
) == STOK_INTEGER
)
1867 current
->state_number
= t0
->stok_un
.stok_num
;
1868 next_state_tokens (1);
1869 record_type (current
);
1872 fatal_reading_state_printf (t0
,
1873 "Expected integer for state_number line %d",
1875 /* We don't read the next field of the type. */
1876 read_state_type (¤t
->pointer_to
);
1877 read_state_gc_used (¤t
->gc_used
);
1881 /* Read a GTY-ed type. */
1883 read_state_type (type_p
*current
)
1885 struct state_token_st
*t0
= peek_state_token (0);
1886 struct state_token_st
*t1
= peek_state_token (1);
1888 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1889 state_token_is_name (t1
, "!type"))
1891 next_state_tokens (2);
1892 t0
= peek_state_token (0);
1893 if (state_token_is_name (t0
, "already_seen"))
1895 next_state_tokens (1);
1896 read_state_already_seen_type (current
);
1900 t0
= peek_state_token (0);
1902 if (state_token_is_name (t0
, "scalar_nonchar"))
1904 next_state_tokens (1);
1905 read_state_scalar_nonchar_type (current
);
1907 else if (state_token_is_name (t0
, "scalar_char"))
1909 next_state_tokens (1);
1910 read_state_scalar_char_type (current
);
1912 else if (state_token_is_name (t0
, "string"))
1914 next_state_tokens (1);
1915 read_state_string_type (current
);
1917 else if (state_token_is_name (t0
, "undefined"))
1919 *current
= XCNEW (struct type
);
1920 next_state_tokens (1);
1921 read_state_undefined_type (*current
);
1923 else if (state_token_is_name (t0
, "struct"))
1925 *current
= XCNEW (struct type
);
1926 next_state_tokens (1);
1927 read_state_struct_type (*current
);
1929 else if (state_token_is_name (t0
, "union"))
1931 *current
= XCNEW (struct type
);
1932 next_state_tokens (1);
1933 read_state_union_type (*current
);
1935 else if (state_token_is_name (t0
, "lang_struct"))
1937 *current
= XCNEW (struct type
);
1938 next_state_tokens (1);
1939 read_state_lang_struct_type (*current
);
1941 else if (state_token_is_name (t0
, "param_struct"))
1943 *current
= XCNEW (struct type
);
1944 next_state_tokens (1);
1945 read_state_param_struct_type (*current
);
1947 else if (state_token_is_name (t0
, "pointer"))
1949 *current
= XCNEW (struct type
);
1950 next_state_tokens (1);
1951 read_state_pointer_type (*current
);
1953 else if (state_token_is_name (t0
, "array"))
1955 *current
= XCNEW (struct type
);
1956 next_state_tokens (1);
1957 read_state_array_type (*current
);
1959 else if (state_token_is_name (t0
, "user_struct"))
1961 *current
= XCNEW (struct type
);
1962 next_state_tokens (1);
1963 read_state_user_struct_type (*current
);
1966 fatal_reading_state (t0
, "bad type in (!type");
1968 t0
= peek_state_token (0);
1969 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1970 fatal_reading_state (t0
, "missing ) in type");
1971 next_state_tokens (1);
1973 else if (state_token_is_name (t0
, "nil"))
1975 next_state_tokens (1);
1979 fatal_reading_state (t0
, "bad type syntax");
1983 /* Read a file location. Files within the source directory are dealt
1984 with specifically. */
1986 read_state_fileloc (struct fileloc
*floc
)
1988 bool issrcfile
= false;
1989 struct state_token_st
*t0
= peek_state_token (0);
1990 struct state_token_st
*t1
= peek_state_token (1);
1992 gcc_assert (floc
!= NULL
);
1993 gcc_assert (srcdir
!= NULL
);
1995 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1996 (state_token_is_name (t1
, "!fileloc")
1997 || (issrcfile
= state_token_is_name (t1
, "!srcfileloc"))))
1999 next_state_tokens (2);
2000 t0
= peek_state_token (0);
2001 t1
= peek_state_token (1);
2002 if (state_token_kind (t0
) == STOK_STRING
&&
2003 state_token_kind (t1
) == STOK_INTEGER
)
2005 char *path
= t0
->stok_un
.stok_string
;
2008 static const char dirsepstr
[2] = { DIR_SEPARATOR
, (char) 0 };
2009 char *fullpath
= concat (srcdir
, dirsepstr
, path
, NULL
);
2010 floc
->file
= input_file_by_name (fullpath
);
2014 floc
->file
= input_file_by_name (path
);
2015 floc
->line
= t1
->stok_un
.stok_num
;
2016 next_state_tokens (2);
2019 fatal_reading_state (t0
,
2020 "Bad fileloc syntax, expected path string and line");
2021 t0
= peek_state_token (0);
2022 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
2023 fatal_reading_state (t0
, "Bad fileloc syntax, expected )");
2024 next_state_tokens (1);
2026 else if (state_token_is_name (t0
, "nil"))
2028 next_state_tokens (1);
2033 fatal_reading_state (t0
, "Bad fileloc syntax");
2037 /* Read the fields of a GTY-ed type. */
2039 read_state_fields (pair_p
*fields
)
2042 struct state_token_st
*t0
= peek_state_token (0);
2043 struct state_token_st
*t1
= peek_state_token (1);
2044 struct state_token_st
*t2
= peek_state_token (2);
2046 if (state_token_kind (t0
) == STOK_LEFTPAR
2047 && state_token_is_name (t1
, "!fields")
2048 && state_token_kind (t2
) == STOK_INTEGER
)
2050 int nbfields
= t2
->stok_un
.stok_num
;
2052 next_state_tokens (3);
2053 nbpairs
= read_state_pair_list (&tmp
);
2054 t0
= peek_state_token (0);
2055 if (nbpairs
!= nbfields
)
2056 fatal_reading_state_printf
2058 "Mismatched fields number, expected %d got %d", nbpairs
, nbfields
);
2059 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
2060 next_state_tokens (1);
2062 fatal_reading_state (t0
, "Bad fields expecting )");
2069 /* Read a string option. */
2071 read_state_string_option (options_p opt
)
2073 struct state_token_st
*t0
= peek_state_token (0);
2074 opt
->kind
= OPTION_STRING
;
2075 if (state_token_kind (t0
) == STOK_STRING
)
2077 opt
->info
.string
= xstrdup (t0
->stok_un
.stok_string
);
2078 next_state_tokens (1);
2080 else if (state_token_is_name (t0
, "nil"))
2082 opt
->info
.string
= NULL
;
2083 next_state_tokens (1);
2086 fatal_reading_state (t0
, "Missing name in string option");
2090 /* Read a type option. */
2092 read_state_type_option (options_p opt
)
2094 opt
->kind
= OPTION_TYPE
;
2095 read_state_type (&(opt
->info
.type
));
2099 /* Read a nested option. */
2101 read_state_nested_option (options_p opt
)
2103 struct state_token_st
*t0
;
2105 opt
->info
.nested
= XCNEW (struct nested_ptr_data
);
2106 opt
->kind
= OPTION_NESTED
;
2107 read_state_type (&(opt
->info
.nested
->type
));
2108 t0
= peek_state_token (0);
2109 if (state_token_kind (t0
) == STOK_STRING
)
2111 opt
->info
.nested
->convert_from
= xstrdup (t0
->stok_un
.stok_string
);
2112 next_state_tokens (1);
2114 else if (state_token_is_name (t0
, "nil"))
2116 opt
->info
.nested
->convert_from
= NULL
;
2117 next_state_tokens (1);
2120 fatal_reading_state (t0
, "Bad nested convert_from option");
2122 t0
= peek_state_token (0);
2123 if (state_token_kind (t0
) == STOK_STRING
)
2125 opt
->info
.nested
->convert_to
= xstrdup (t0
->stok_un
.stok_string
);
2126 next_state_tokens (1);
2128 else if (state_token_is_name (t0
, "nil"))
2130 opt
->info
.nested
->convert_to
= NULL
;
2131 next_state_tokens (1);
2134 fatal_reading_state (t0
, "Bad nested convert_from option");
2138 /* Read an GTY option. */
2140 read_state_option (options_p
*opt
)
2142 struct state_token_st
*t0
= peek_state_token (0);
2143 struct state_token_st
*t1
= peek_state_token (1);
2145 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2146 state_token_is_name (t1
, "!option"))
2148 next_state_tokens (2);
2149 t0
= peek_state_token (0);
2150 if (state_token_kind (t0
) == STOK_NAME
)
2152 *opt
= XCNEW (struct options
);
2153 if (state_token_is_name (t0
, "nil"))
2154 (*opt
)->name
= NULL
;
2156 (*opt
)->name
= t0
->stok_un
.stok_ident
->stid_name
;
2157 next_state_tokens (1);
2158 t0
= peek_state_token (0);
2159 if (state_token_kind (t0
) == STOK_NAME
)
2161 if (state_token_is_name (t0
, "string"))
2163 next_state_tokens (1);
2164 read_state_string_option (*opt
);
2166 else if (state_token_is_name (t0
, "type"))
2168 next_state_tokens (1);
2169 read_state_type_option (*opt
);
2171 else if (state_token_is_name (t0
, "nested"))
2173 next_state_tokens (1);
2174 read_state_nested_option (*opt
);
2177 fatal_reading_state (t0
, "Bad option type");
2178 t0
= peek_state_token (0);
2179 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
2180 fatal_reading_state (t0
, "Bad syntax in option, expecting )");
2182 next_state_tokens (1);
2185 fatal_reading_state (t0
, "Missing option type");
2188 fatal_reading_state (t0
, "Bad name for option");
2191 fatal_reading_state (t0
, "Bad option, waiting for )");
2194 /* Read a list of options. */
2196 read_state_options (options_p
*opt
)
2198 options_p head
= NULL
;
2199 options_p previous
= NULL
;
2200 options_p current_option
= NULL
;
2201 struct state_token_st
*t0
= peek_state_token (0);
2202 struct state_token_st
*t1
= peek_state_token (1);
2204 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2205 state_token_is_name (t1
, "!options"))
2207 next_state_tokens (2);
2208 t0
= peek_state_token (0);
2209 while (state_token_kind (t0
) != STOK_RIGHTPAR
)
2211 read_state_option (¤t_option
);
2214 head
= current_option
;
2219 previous
->next
= current_option
;
2220 previous
= current_option
;
2222 t0
= peek_state_token (0);
2224 next_state_tokens (1);
2226 else if (state_token_is_name (t0
, "nil"))
2228 next_state_tokens (1);
2231 fatal_reading_state (t0
, "Bad options syntax");
2237 /* Read a version, and check against the version of the gengtype. */
2239 read_state_version (const char *version_string
)
2241 struct state_token_st
*t0
= peek_state_token (0);
2242 struct state_token_st
*t1
= peek_state_token (1);
2244 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2245 state_token_is_name (t1
, "!version"))
2247 next_state_tokens (2);
2248 t0
= peek_state_token (0);
2249 t1
= peek_state_token (1);
2250 if (state_token_kind (t0
) == STOK_STRING
&&
2251 state_token_kind (t1
) == STOK_RIGHTPAR
)
2253 /* Check that the read version string is the same as current
2255 if (strcmp (version_string
, t0
->stok_un
.stok_string
))
2256 fatal_reading_state_printf (t0
,
2257 "version string mismatch; expecting %s but got %s",
2259 t0
->stok_un
.stok_string
);
2260 next_state_tokens (2);
2263 fatal_reading_state (t0
, "Missing version or right parenthesis");
2266 fatal_reading_state (t0
, "Bad version syntax");
2272 read_state_pair (pair_p
*current
)
2274 struct state_token_st
*t0
= peek_state_token (0);
2275 struct state_token_st
*t1
= peek_state_token (1);
2276 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2277 state_token_is_name (t1
, "!pair"))
2279 *current
= XCNEW (struct pair
);
2280 next_state_tokens (2);
2281 t0
= peek_state_token (0);
2282 if (state_token_kind (t0
) == STOK_STRING
)
2284 if (strcmp (t0
->stok_un
.stok_string
, "nil") == 0)
2286 (*current
)->name
= NULL
;
2290 (*current
)->name
= xstrdup (t0
->stok_un
.stok_string
);
2292 next_state_tokens (1);
2293 read_state_type (&((*current
)->type
));
2294 read_state_fileloc (&((*current
)->line
));
2295 read_state_options (&((*current
)->opt
));;
2296 t0
= peek_state_token (0);
2297 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
2299 next_state_tokens (1);
2303 fatal_reading_state (t0
, "Bad syntax for pair, )");
2308 fatal_reading_state (t0
, "Bad name for pair");
2311 else if (state_token_kind (t0
) == STOK_NAME
&&
2312 state_token_is_name (t0
, "nil"))
2314 next_state_tokens (1);
2318 fatal_reading_state_printf (t0
, "Bad syntax for pair, (!pair %d",
2319 state_token
->stok_kind
);
2323 /* Return the number of pairs actually read. */
2325 read_state_pair_list (pair_p
*list
)
2329 pair_p previous
= NULL
;
2331 struct state_token_st
*t0
= peek_state_token (0);
2332 while (t0
&& state_token_kind (t0
) != STOK_RIGHTPAR
)
2334 read_state_pair (&tmp
);
2342 previous
->next
= tmp
;
2345 t0
= peek_state_token (0);
2349 /* don't consume the ); the caller will eat it. */
2354 /* Read the typedefs. */
2356 read_state_typedefs (pair_p
*typedefs
)
2360 struct state_token_st
*t0
= peek_state_token (0);
2361 struct state_token_st
*t1
= peek_state_token (1);
2362 struct state_token_st
*t2
= peek_state_token (2);
2364 if (state_token_kind (t0
) == STOK_LEFTPAR
2365 && state_token_is_name (t1
, "!typedefs")
2366 && state_token_kind (t2
) == STOK_INTEGER
)
2369 nbtypedefs
= t2
->stok_un
.stok_num
;
2370 next_state_tokens (3);
2371 nbpairs
= read_state_pair_list (&list
);
2372 t0
= peek_state_token (0);
2373 if (nbpairs
!= nbtypedefs
)
2374 fatal_reading_state_printf
2376 "invalid number of typedefs, expected %d but got %d",
2377 nbtypedefs
, nbpairs
);
2378 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
2379 next_state_tokens (1);
2381 fatal_reading_state (t0
, "Bad typedefs syntax )");
2384 fatal_reading_state (t0
, "Bad typedefs syntax (!typedefs");
2386 if (verbosity_level
>= 2)
2387 printf ("%s read %d typedefs from state\n", progname
, nbtypedefs
);
2392 /* Read the structures. */
2394 read_state_structures (type_p
*structures
)
2397 type_p previous
= NULL
;
2399 int nbstruct
= 0, countstruct
= 0;
2400 struct state_token_st
*t0
= peek_state_token (0);
2401 struct state_token_st
*t1
= peek_state_token (1);
2402 struct state_token_st
*t2
= peek_state_token (2);
2404 if (state_token_kind (t0
) == STOK_LEFTPAR
2405 && state_token_is_name (t1
, "!structures")
2406 && state_token_kind (t2
) == STOK_INTEGER
)
2408 nbstruct
= t2
->stok_un
.stok_num
;
2409 next_state_tokens (3);
2410 t0
= peek_state_token (0);
2411 while (t0
&& state_token_kind (t0
) != STOK_RIGHTPAR
)
2414 read_state_type (&tmp
);
2423 previous
->next
= tmp
;
2426 t0
= peek_state_token (0);
2428 next_state_tokens (1);
2431 fatal_reading_state (t0
, "Bad structures syntax");
2432 if (countstruct
!= nbstruct
)
2433 fatal_reading_state_printf (NULL_STATE_TOKEN
,
2434 "expected %d structures but got %d",
2435 nbstruct
, countstruct
);
2436 if (verbosity_level
>= 2)
2437 printf ("%s read %d structures from state\n", progname
, nbstruct
);
2442 /* Read the param_struct-s. */
2444 read_state_param_structs (type_p
*param_structs
)
2446 int nbparamstructs
= 0;
2447 int countparamstructs
= 0;
2449 type_p previous
= NULL
;
2451 struct state_token_st
*t0
= peek_state_token (0);
2452 struct state_token_st
*t1
= peek_state_token (1);
2453 struct state_token_st
*t2
= peek_state_token (2);
2455 if (state_token_kind (t0
) == STOK_LEFTPAR
2456 && state_token_is_name (t1
, "!param_structs")
2457 && state_token_kind (t2
) == STOK_INTEGER
)
2459 nbparamstructs
= t2
->stok_un
.stok_num
;
2460 next_state_tokens (3);
2461 t0
= t1
= t2
= NULL
;
2462 t0
= peek_state_token (0);
2463 while (state_token_kind (t0
) != STOK_RIGHTPAR
)
2466 read_state_type (&tmp
);
2474 previous
->next
= tmp
;
2477 t0
= peek_state_token (0);
2478 countparamstructs
++;
2480 next_state_tokens (1);
2483 fatal_reading_state (t0
, "Bad param_structs syntax");
2484 t0
= peek_state_token (0);
2485 if (countparamstructs
!= nbparamstructs
)
2486 fatal_reading_state_printf
2488 "invalid number of param_structs expected %d got %d",
2489 nbparamstructs
, countparamstructs
);
2490 *param_structs
= head
;
2494 /* Read the variables. */
2496 read_state_variables (pair_p
*variables
)
2500 struct state_token_st
*t0
= peek_state_token (0);
2501 struct state_token_st
*t1
= peek_state_token (1);
2502 struct state_token_st
*t2
= peek_state_token (2);
2504 if (state_token_kind (t0
) == STOK_LEFTPAR
2505 && state_token_is_name (t1
, "!variables")
2506 && state_token_kind (t2
) == STOK_INTEGER
)
2509 nbvars
= t2
->stok_un
.stok_num
;
2510 next_state_tokens (3);
2511 nbpairs
= read_state_pair_list (&list
);
2512 t0
= peek_state_token (0);
2513 if (nbpairs
!= nbvars
)
2514 fatal_reading_state_printf
2515 (t0
, "Invalid number of variables, expected %d but got %d",
2517 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
2518 next_state_tokens (1);
2520 fatal_reading_state (t0
, "Waiting for ) in variables");
2523 fatal_reading_state (t0
, "Bad variables syntax");
2525 if (verbosity_level
>= 2)
2526 printf ("%s read %d variables from state\n", progname
, nbvars
);
2530 /* Read the source directory. */
2532 read_state_srcdir (void)
2534 struct state_token_st
*t0
= peek_state_token (0);
2535 struct state_token_st
*t1
= peek_state_token (1);
2536 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2537 state_token_is_name (t1
, "!srcdir"))
2539 next_state_tokens (2);
2540 t0
= peek_state_token (0);
2541 t1
= peek_state_token (1);
2542 if (state_token_kind (t0
) == STOK_STRING
&&
2543 state_token_kind (t1
) == STOK_RIGHTPAR
)
2545 srcdir
= xstrdup (t0
->stok_un
.stok_string
);
2546 srcdir_len
= strlen (srcdir
);
2547 next_state_tokens (2);
2552 fatal_reading_state (t0
, "Bad srcdir in state_file");
2556 /* Read the sequence of GCC front-end languages. */
2558 read_state_languages (void)
2560 struct state_token_st
*t0
= peek_state_token (0);
2561 struct state_token_st
*t1
= peek_state_token (1);
2562 struct state_token_st
*t2
= peek_state_token (2);
2563 if (state_token_kind (t0
) == STOK_LEFTPAR
2564 && state_token_is_name (t1
, "!languages")
2565 && state_token_kind (t2
) == STOK_INTEGER
)
2568 num_lang_dirs
= t2
->stok_un
.stok_num
;
2569 lang_dir_names
= XCNEWVEC (const char *, num_lang_dirs
);
2570 next_state_tokens (3);
2571 t0
= t1
= t2
= NULL
;
2572 for (i
= 0; i
< (int) num_lang_dirs
; i
++)
2574 t0
= peek_state_token (0);
2575 if (state_token_kind (t0
) != STOK_NAME
)
2576 fatal_reading_state (t0
, "expecting language name in state file");
2577 lang_dir_names
[i
] = t0
->stok_un
.stok_ident
->stid_name
;
2578 next_state_tokens (1);
2580 t0
= peek_state_token (0);
2581 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
2582 fatal_reading_state (t0
, "missing ) in languages list of state file");
2583 next_state_tokens (1);
2586 fatal_reading_state (t0
, "expecting languages list in state file");
2590 /* Read the sequence of files. */
2592 read_state_files_list (void)
2594 struct state_token_st
*t0
= peek_state_token (0);
2595 struct state_token_st
*t1
= peek_state_token (1);
2596 struct state_token_st
*t2
= peek_state_token (2);
2598 if (state_token_kind (t0
) == STOK_LEFTPAR
2599 && state_token_is_name (t1
, "!fileslist")
2600 && state_token_kind (t2
) == STOK_INTEGER
)
2603 num_gt_files
= t2
->stok_un
.stok_num
;
2604 next_state_tokens (3);
2605 t0
= t1
= t2
= NULL
;
2606 gt_files
= XCNEWVEC (const input_file
*, num_gt_files
);
2607 for (i
= 0; i
< (int) num_gt_files
; i
++)
2609 bool issrcfile
= FALSE
;
2610 t0
= t1
= t2
= NULL
;
2611 t0
= peek_state_token (0);
2612 t1
= peek_state_token (1);
2613 t2
= peek_state_token (2);
2614 if (state_token_kind (t0
) == STOK_LEFTPAR
2615 && (state_token_is_name (t1
, "!file")
2616 || (issrcfile
= state_token_is_name (t1
, "!srcfile")))
2617 && state_token_kind (t2
) == STOK_INTEGER
)
2619 lang_bitmap bmap
= t2
->stok_un
.stok_num
;
2620 next_state_tokens (3);
2621 t0
= t1
= t2
= NULL
;
2622 t0
= peek_state_token (0);
2623 t1
= peek_state_token (1);
2624 if (state_token_kind (t0
) == STOK_STRING
2625 && state_token_kind (t1
) == STOK_RIGHTPAR
)
2627 const char *fnam
= t0
->stok_un
.stok_string
;
2628 /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
2629 input_file
*curgt
= NULL
;
2632 static const char dirsepstr
[2] =
2633 { DIR_SEPARATOR
, (char) 0 };
2634 char *fullpath
= concat (srcdir
, dirsepstr
, fnam
, NULL
);
2635 curgt
= input_file_by_name (fullpath
);
2639 curgt
= input_file_by_name (fnam
);
2640 set_lang_bitmap (curgt
, bmap
);
2641 gt_files
[i
] = curgt
;
2642 next_state_tokens (2);
2645 fatal_reading_state (t0
,
2646 "bad file in !fileslist of state file");
2649 fatal_reading_state (t0
,
2650 "expecting file in !fileslist of state file");
2652 t0
= peek_state_token (0);
2653 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
2654 fatal_reading_state (t0
, "missing ) for !fileslist in state file");
2655 next_state_tokens (1);
2658 fatal_reading_state (t0
, "missing !fileslist in state file");
2662 /* Read the trailer. */
2664 read_state_trailer (void)
2666 struct state_token_st
*t0
= peek_state_token (0);
2667 struct state_token_st
*t1
= peek_state_token (1);
2668 struct state_token_st
*t2
= peek_state_token (2);
2670 if (state_token_kind (t0
) == STOK_LEFTPAR
2671 && state_token_is_name (t1
, "!endfile")
2672 && state_token_kind (t2
) == STOK_RIGHTPAR
)
2673 next_state_tokens (3);
2675 fatal_reading_state (t0
, "missing !endfile in state file");
2679 /* Utility functions for the state_seen_types hash table. */
2681 hash_type_number (const void *ty
)
2683 const struct type
*type
= (const struct type
*) ty
;
2685 return type
->state_number
;
2689 equals_type_number (const void *ty1
, const void *ty2
)
2691 const struct type
*type1
= (const struct type
*) ty1
;
2692 const struct type
*type2
= (const struct type
*) ty2
;
2694 return type1
->state_number
== type2
->state_number
;
2698 string_eq (const void *a
, const void *b
)
2700 const char *a0
= (const char *)a
;
2701 const char *b0
= (const char *)b
;
2703 return (strcmp (a0
, b0
) == 0);
2707 /* The function reading the state, called by main from gengtype.c. */
2709 read_state (const char *path
)
2711 state_file
= fopen (path
, "r");
2712 if (state_file
== NULL
)
2713 fatal ("Failed to open state file %s for reading [%s]", path
,
2718 if (verbosity_level
>= 1)
2720 printf ("%s reading state file %s;", progname
, state_path
);
2721 if (verbosity_level
>= 2)
2727 htab_create (2017, hash_type_number
, equals_type_number
, NULL
);
2729 htab_create (4027, htab_hash_string
, string_eq
, NULL
);
2730 read_state_version (version_string
);
2731 read_state_srcdir ();
2732 read_state_languages ();
2733 read_state_files_list ();
2734 read_state_structures (&structures
);
2735 if (ferror (state_file
))
2736 fatal_reading_state_printf
2737 (NULL_STATE_TOKEN
, "input error while reading state [%s]",
2739 read_state_typedefs (&typedefs
);
2740 read_state_param_structs (¶m_structs
);
2741 read_state_variables (&variables
);
2742 read_state_trailer ();
2744 if (verbosity_level
>= 1)
2746 printf ("%s read %ld bytes.\n", progname
, ftell (state_file
));
2750 if (fclose (state_file
))
2751 fatal ("failed to close read state file %s [%s]",
2752 path
, xstrerror (errno
));
2757 /* End of file gengtype-state.c. */