1 /* Gengtype persistent state serialization & de-serialization.
2 Useful for gengtype in plugin mode.
4 Copyright (C) 2010-2017 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>
26 #ifdef HOST_GENERATOR_FILE
28 #define GENERATOR_FILE 1
33 #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
);
66 /* The state file has simplistic lispy lexical tokens. Its lexer gives
67 a linked list of struct state_token_st, through the peek_state_token
68 function. Lexical tokens are consumed with next_state_tokens. */
71 /* The lexical kind of each lispy token. */
74 STOK_NONE
, /* Never used. */
75 STOK_INTEGER
, /* Integer token. */
76 STOK_STRING
, /* String token. */
77 STOK_LEFTPAR
, /* Left opening parenthesis. */
78 STOK_RIGHTPAR
, /* Right closing parenthesis. */
79 STOK_NAME
/* hash-consed name or identifier. */
83 /* Structure and hash-table used to share identifiers or names. */
86 /* TODO: We could improve the parser by reserving identifiers for
87 state keywords and adding a keyword number for them. That would
88 mean adding another field in this state_ident_st struct. */
89 char stid_name
[1]; /* actually bigger & null terminated */
91 static htab_t state_ident_tab
;
94 /* The state_token_st structure is for lexical tokens in the read
95 state file. The stok_kind field discriminates the union. Tokens
96 are allocated by peek_state_token which calls read_a_state_token
97 which allocate them. Tokens are freed by calls to
98 next_state_tokens. Token are organized in a FIFO look-ahead queue
99 filled by peek_state_token. */
100 struct state_token_st
102 enum state_token_en stok_kind
; /* the lexical kind
103 discriminates the stok_un
105 int stok_line
; /* the line number */
106 int stok_col
; /* the column number */
107 const char *stok_file
; /* the file path */
108 struct state_token_st
*stok_next
; /* the next token in the
109 queue, when peeked */
110 union /* discriminated by stok_kind! */
112 int stok_num
; /* when STOK_INTEGER */
113 char stok_string
[1]; /* when STOK_STRING, actual size is
114 bigger and null terminated */
115 struct state_ident_st
*stok_ident
; /* when STOK_IDENT */
116 void *stok_ptr
; /* null otherwise */
124 #define NULL_STATE_TOKEN (struct state_token_st*)0
126 /* the state_token pointer contains the leftmost current token. The
127 tokens are organized in a linked queue, using stok_next, for token
129 struct state_token_st
*state_token
= NULL_STATE_TOKEN
;
131 /* Used by the reading lexer. */
132 static FILE *state_file
;
133 static const char *state_path
= NULL
;
134 static int state_line
= 0;
135 static long state_bol
= 0; /* offset of beginning of line */
137 /* A class for writing out s-expressions, keeping track of newlines and
138 nested indentation. */
144 void write_new_line ();
145 void write_any_indent (int leading_spaces
);
147 void begin_s_expr (const char *tag
);
152 int m_had_recent_newline
;
153 }; // class s_expr_writer
155 /* A class for writing out "gtype.state". */
156 class state_writer
: public s_expr_writer
162 void write_state_fileloc (struct fileloc
*floc
);
163 void write_state_fields (pair_p fields
);
164 void write_state_a_string (const char *s
);
165 void write_state_string_option (options_p current
);
166 void write_state_type_option (options_p current
);
167 void write_state_nested_option (options_p current
);
168 void write_state_option (options_p current
);
169 void write_state_options (options_p opt
);
170 void write_state_lang_bitmap (lang_bitmap bitmap
);
171 void write_state_version (const char *version
);
172 void write_state_scalar_type (type_p current
);
173 void write_state_string_type (type_p current
);
174 void write_state_undefined_type (type_p current
);
175 void write_state_struct_union_type (type_p current
, const char *kindstr
);
176 void write_state_struct_type (type_p current
);
177 void write_state_user_struct_type (type_p current
);
178 void write_state_union_type (type_p current
);
179 void write_state_lang_struct_type (type_p current
);
180 void write_state_pointer_type (type_p current
);
181 void write_state_array_type (type_p current
);
182 void write_state_gc_used (enum gc_used_enum gus
);
183 void write_state_common_type_content (type_p current
);
184 void write_state_type (type_p current
);
185 void write_state_pair (pair_p current
);
186 int write_state_pair_list (pair_p list
);
187 void write_state_typedefs (void);
188 void write_state_structures (void);
189 void write_state_variables (void);
190 void write_state_srcdir (void);
191 void write_state_files_list (void);
192 void write_state_languages (void);
194 friend void write_state (const char *state_path
);
197 /* Counter of written types. */
198 int m_state_written_type_count
;
199 }; // class state_writer
202 /* class s_expr_writer's trivial constructor. */
203 s_expr_writer::s_expr_writer ()
204 : m_indent_amount (0),
205 m_had_recent_newline (0)
209 /* Write a newline to the output file, merging adjacent newlines. */
211 s_expr_writer::write_new_line (void)
213 /* Don't add a newline if we've just had one. */
214 if (!m_had_recent_newline
)
216 fprintf (state_file
, "\n");
217 m_had_recent_newline
= 1;
221 /* If we've just had a newline, write the indentation amount, potentially
222 omitting some spaces.
224 LEADING_SPACES exists to support code that writes strings with leading
225 spaces (e.g " foo") which might occur within a line, or could be the first
226 thing on a line. By passing leading_spaces == 1, when such a string is the
227 first thing on a line, write_any_indent () swallows the successive
228 leading spaces into the indentation so that the "foo" begins at the expected
231 s_expr_writer::write_any_indent (int leading_spaces
)
234 int amount
= m_indent_amount
- leading_spaces
;
235 if (m_had_recent_newline
)
236 for (i
= 0; i
< amount
; i
++)
237 fprintf (state_file
, " ");
238 m_had_recent_newline
= 0;
241 /* Write the beginning of a new s-expresion e.g. "(!foo "
242 The writer automatically adds whitespace to show the hierarchical
243 structure of the expressions, so each one starts on a new line,
244 and any within it will be at an increased indentation level. */
246 s_expr_writer::begin_s_expr (const char *tag
)
249 write_any_indent (0);
250 fprintf (state_file
, "(!%s ", tag
);
254 /* Write out the end of an s-expression: any necssessary indentation,
255 a closing parenthesis, and a new line. */
257 s_expr_writer::end_s_expr (void)
260 write_any_indent (0);
261 fprintf (state_file
, ")");
266 /* class state_writer's trivial constructor. */
267 state_writer::state_writer ()
269 m_state_written_type_count (0)
274 /* Fatal error messages when reading the state. They are extremely
275 unlikely, and only appear when this gengtype-state.c file is buggy,
276 or when reading a gengtype state which was not generated by the
277 same version of gengtype or GCC. */
280 /* Fatal message while reading state. */
282 fatal_reading_state (struct state_token_st
* tok
, const char*msg
)
285 fatal ("%s:%d:%d: Invalid state file; %s",
286 tok
->stok_file
, tok
->stok_line
, tok
->stok_col
,
289 fatal ("%s:%d: Invalid state file; %s",
290 state_path
, state_line
, msg
);
294 /* Fatal printf-like message while reading state. This can't be a
295 function, because there is no way to pass a va_arg to a variant of
297 #define fatal_reading_state_printf(Tok,Fmt,...) do { \
298 struct state_token_st* badtok = Tok; \
300 fatal ("%s:%d:%d: Invalid state file; " Fmt, \
303 badtok->stok_col, __VA_ARGS__); \
305 fatal ("%s:%d: Invalid state file; " Fmt, \
306 state_path, state_line, __VA_ARGS__); \
310 /* Find or allocate an identifier in our name hash table. */
311 static struct state_ident_st
*
312 state_ident_by_name (const char *name
, enum insert_option optins
)
316 struct state_ident_st
*stid
= NULL
;
318 if (!name
|| !name
[0])
321 slot
= htab_find_slot (state_ident_tab
, name
, optins
);
325 namlen
= strlen (name
);
327 (struct state_ident_st
*) xmalloc (sizeof (struct state_ident_st
) +
329 memset (stid
, 0, sizeof (struct state_ident_st
) + namlen
);
330 strcpy (stid
->stid_name
, name
);
336 /* Our token lexer is heavily inspired by MELT's lexer, and share some
337 code with the file gcc/melt-runtime.c of the GCC MELT branch! We
338 really want the gengtype state to be easily parsable by MELT. This
339 is a usual lispy lexing routine, dealing with spaces and comments,
340 numbers, parenthesis, names, strings. */
341 static struct state_token_st
*
342 read_a_state_token (void)
346 struct state_token_st
*tk
= NULL
;
348 again
: /* Read again, e.g. after a comment or spaces. */
349 c
= getc (state_file
);
353 /* Handle spaces, count lines. */
357 state_bol
= curoff
= ftell (state_file
);
362 /* Skip comments starting with semi-colon. */
367 c
= getc (state_file
);
369 while (c
> 0 && c
!= '\n');
373 state_bol
= curoff
= ftell (state_file
);
377 /* Read signed numbers. */
378 if (ISDIGIT (c
) || c
== '-' || c
== '+')
381 ungetc (c
, state_file
);
382 curoff
= ftell (state_file
);
383 if (fscanf (state_file
, "%d", &n
) <= 0)
384 fatal_reading_state (NULL_STATE_TOKEN
, "Lexical error in number");
385 tk
= XCNEW (struct state_token_st
);
386 tk
->stok_kind
= STOK_INTEGER
;
387 tk
->stok_line
= state_line
;
388 tk
->stok_col
= curoff
- state_bol
;
389 tk
->stok_file
= state_path
;
390 tk
->stok_next
= NULL
;
391 tk
->stok_un
.stok_num
= n
;
395 /* Read an opening left parenthesis. */
398 curoff
= ftell (state_file
);
399 tk
= XCNEW (struct state_token_st
);
400 tk
->stok_kind
= STOK_LEFTPAR
;
401 tk
->stok_line
= state_line
;
402 tk
->stok_col
= curoff
- state_bol
;
403 tk
->stok_file
= state_path
;
404 tk
->stok_next
= NULL
;
408 /* Read an closing right parenthesis. */
411 curoff
= ftell (state_file
);
412 tk
= XCNEW (struct state_token_st
);
413 tk
->stok_kind
= STOK_RIGHTPAR
;
414 tk
->stok_line
= state_line
;
415 tk
->stok_col
= curoff
- state_bol
;
416 tk
->stok_file
= state_path
;
417 tk
->stok_next
= NULL
;
421 /* Read identifiers, using an obstack. */
422 else if (ISALPHA (c
) || c
== '_' || c
== '$' || c
== '!' || c
== '#')
424 struct obstack id_obstack
;
425 struct state_ident_st
*sid
= NULL
;
427 obstack_init (&id_obstack
);
428 curoff
= ftell (state_file
);
429 while (ISALNUM (c
) || c
== '_' || c
== '$' || c
== '!' || c
== '#')
431 obstack_1grow (&id_obstack
, c
);
432 c
= getc (state_file
);
437 ungetc (c
, state_file
);
438 obstack_1grow (&id_obstack
, (char) 0);
439 ids
= XOBFINISH (&id_obstack
, char *);
440 sid
= state_ident_by_name (ids
, INSERT
);
441 obstack_free (&id_obstack
, NULL
);
443 tk
= XCNEW (struct state_token_st
);
444 tk
->stok_kind
= STOK_NAME
;
445 tk
->stok_line
= state_line
;
446 tk
->stok_col
= curoff
- state_bol
;
447 tk
->stok_file
= state_path
;
448 tk
->stok_next
= NULL
;
449 tk
->stok_un
.stok_ident
= sid
;
453 /* Read a string, dealing with escape sequences a la C! */
458 struct obstack bstring_obstack
;
459 obstack_init (&bstring_obstack
);
460 curoff
= ftell (state_file
);
461 while ((c
= getc (state_file
)) != '"' && c
>= 0)
463 if (ISPRINT (c
) && c
!= '\\')
464 obstack_1grow (&bstring_obstack
, (char) c
);
465 else if (ISSPACE (c
) && c
!= '\n')
466 obstack_1grow (&bstring_obstack
, (char) c
);
469 c
= getc (state_file
);
473 obstack_1grow (&bstring_obstack
, '\a');
474 c
= getc (state_file
);
477 obstack_1grow (&bstring_obstack
, '\b');
478 c
= getc (state_file
);
481 obstack_1grow (&bstring_obstack
, '\t');
482 c
= getc (state_file
);
485 obstack_1grow (&bstring_obstack
, '\n');
486 c
= getc (state_file
);
489 obstack_1grow (&bstring_obstack
, '\v');
490 c
= getc (state_file
);
493 obstack_1grow (&bstring_obstack
, '\f');
494 c
= getc (state_file
);
497 obstack_1grow (&bstring_obstack
, '\r');
498 c
= getc (state_file
);
501 obstack_1grow (&bstring_obstack
, '\"');
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
);
515 if (fscanf (state_file
, "%02x", &cx
) > 0 && cx
> 0)
516 obstack_1grow (&bstring_obstack
, cx
);
520 "Lexical error in string hex escape");
521 c
= getc (state_file
);
527 "Lexical error - unknown string escape");
531 fatal_reading_state (NULL_STATE_TOKEN
, "Lexical error...");
534 fatal_reading_state (NULL_STATE_TOKEN
, "Unterminated string");
535 obstack_1grow (&bstring_obstack
, '\0');
536 cstr
= XOBFINISH (&bstring_obstack
, char *);
537 cslen
= strlen (cstr
);
538 tk
= (struct state_token_st
*)
539 xcalloc (sizeof (struct state_token_st
) + cslen
, 1);
540 tk
->stok_kind
= STOK_STRING
;
541 tk
->stok_line
= state_line
;
542 tk
->stok_col
= curoff
- state_bol
;
543 tk
->stok_file
= state_path
;
544 tk
->stok_next
= NULL
;
545 strcpy (tk
->stok_un
.stok_string
, cstr
);
546 obstack_free (&bstring_obstack
, NULL
);
550 /* Got an unexpected character. */
551 fatal_reading_state_printf
553 "Lexical error at offset %ld - bad character \\%03o = '%c'",
554 ftell (state_file
), c
, c
);
557 /* Used for lexical look-ahead. Retrieves the lexical token of rank
558 DEPTH, starting with 0 when reading the state file. Gives null on
560 static struct state_token_st
*
561 peek_state_token (int depth
)
563 int remdepth
= depth
;
564 struct state_token_st
**ptoken
= &state_token
;
565 struct state_token_st
*tok
= NULL
;
567 while (remdepth
>= 0)
571 *ptoken
= tok
= read_a_state_token ();
576 ptoken
= &((*ptoken
)->stok_next
);
583 /* Consume the next DEPTH tokens and free them. */
585 next_state_tokens (int depth
)
587 struct state_token_st
*n
;
591 if (state_token
!= NULL
)
593 n
= state_token
->stok_next
;
598 fatal_reading_state (NULL_STATE_TOKEN
, "Tokens stack empty");
604 /* Safely retrieve the lexical kind of a token. */
605 static inline enum state_token_en
606 state_token_kind (struct state_token_st
*p
)
614 /* Test if a token is a given name i.e. an identifier. */
616 state_token_is_name (struct state_token_st
*p
, const char *name
)
621 if (p
->stok_kind
!= STOK_NAME
)
624 return !strcmp (p
->stok_un
.stok_ident
->stid_name
, name
);
628 /* Following routines are useful for serializing datas.
630 * We want to serialize :
635 * So, we have one routine for each kind of data. The main writing
636 * routine is write_state. The main reading routine is
637 * read_state. Most writing routines write_state_FOO have a
638 * corresponding reading routine read_state_FOO. Reading is done in a
639 * recursive descending way, and any read error is fatal.
642 /* When reading the state, we need to remember the previously seen
643 types by their state_number, since GTY-ed types are usually
645 static htab_t state_seen_types
;
647 /* Return the length of a linked list made of pairs. */
648 static int pair_list_length (pair_p list
);
650 /* Compute the length of a list of pairs, starting from the first
653 pair_list_length (pair_p list
)
657 for (l
= list
; l
; l
= l
->next
)
662 /* Write a file location. Files relative to $(srcdir) are quite
663 frequent and are handled specially. This ensures that two gengtype
664 state file-s produced by gengtype on the same GCC source tree are
665 very similar and can be reasonably compared with diff, even if the
666 two GCC source trees have different absolute paths. */
668 state_writer::write_state_fileloc (struct fileloc
*floc
)
671 if (floc
!= NULL
&& floc
->line
> 0)
673 const char *srcrelpath
= NULL
;
674 gcc_assert (floc
->file
!= NULL
);
675 /* Most of the files are inside $(srcdir) so it is worth to
676 handle them specially. */
677 srcrelpath
= get_file_srcdir_relative_path (floc
->file
);
678 if (srcrelpath
!= NULL
)
680 begin_s_expr ("srcfileloc");
681 write_state_a_string (srcrelpath
);
685 begin_s_expr ("fileloc");
686 write_state_a_string (get_input_file_name (floc
->file
));
688 fprintf (state_file
, " %d", floc
->line
);
692 fprintf (state_file
, "nil ");
695 /* Write a list of fields. */
697 state_writer::write_state_fields (pair_p fields
)
699 int nbfields
= pair_list_length (fields
);
701 begin_s_expr ("fields");
702 fprintf (state_file
, "%d ", nbfields
);
703 nbpairs
= write_state_pair_list (fields
);
704 gcc_assert (nbpairs
== nbfields
);
708 /* Write a null-terminated string in our lexical convention, very
709 similar to the convention of C. */
711 state_writer::write_state_a_string (const char *s
)
715 write_any_indent (1);
717 fputs (" \"", state_file
);
724 fputs ("\\a", state_file
);
727 fputs ("\\b", state_file
);
730 fputs ("\\t", state_file
);
733 fputs ("\\n", state_file
);
736 fputs ("\\v", state_file
);
739 fputs ("\\f", state_file
);
742 fputs ("\\r", state_file
);
745 fputs ("\\\"", state_file
);
748 fputs ("\\\\", state_file
);
752 putc (c
, state_file
);
754 fprintf (state_file
, "\\x%02x", (unsigned) c
);
757 fputs ("\"", state_file
);
760 /* Our option-s have three kinds, each with its writer. */
762 state_writer::write_state_string_option (options_p current
)
764 write_any_indent (0);
765 fprintf (state_file
, "string ");
766 if (current
->info
.string
!= NULL
)
767 write_state_a_string (current
->info
.string
);
769 fprintf (state_file
, " nil ");
773 state_writer::write_state_type_option (options_p current
)
775 write_any_indent (0);
776 fprintf (state_file
, "type ");
777 write_state_type (current
->info
.type
);
781 state_writer::write_state_nested_option (options_p current
)
783 write_any_indent (0);
784 fprintf (state_file
, "nested ");
785 write_state_type (current
->info
.nested
->type
);
786 if (current
->info
.nested
->convert_from
!= NULL
)
787 write_state_a_string (current
->info
.nested
->convert_from
);
790 write_any_indent (1);
791 fprintf (state_file
, " nil ");
794 if (current
->info
.nested
->convert_to
!= NULL
)
795 write_state_a_string (current
->info
.nested
->convert_to
);
798 write_any_indent (1);
799 fprintf (state_file
, " nil ");
804 state_writer::write_state_option (options_p current
)
806 begin_s_expr ("option");
808 write_any_indent (0);
809 if (current
->name
!= NULL
)
810 fprintf (state_file
, "%s ", current
->name
);
812 fprintf (state_file
, "nil ");
814 switch (current
->kind
)
817 write_state_string_option (current
);
820 write_state_type_option (current
);
823 write_state_nested_option (current
);
826 fatal ("Option tag unknown");
829 /* Terminate the "option" s-expression. */
835 /* Write a list of GTY options. */
837 state_writer::write_state_options (options_p opt
)
843 write_any_indent (0);
844 fprintf (state_file
, "nil ");
848 begin_s_expr ("options");
849 for (current
= opt
; current
!= NULL
; current
= current
->next
)
850 write_state_option (current
);
855 /* Write a bitmap representing a set of GCC front-end languages. */
857 state_writer::write_state_lang_bitmap (lang_bitmap bitmap
)
859 write_any_indent (0);
860 fprintf (state_file
, "%d ", (int) bitmap
);
863 /* Write version information. */
865 state_writer::write_state_version (const char *version
)
867 begin_s_expr ("version");
868 write_state_a_string (version
);
872 /* Write a scalar type. We have only two of these. */
874 state_writer::write_state_scalar_type (type_p current
)
876 write_any_indent (0);
877 if (current
== &scalar_nonchar
)
878 fprintf (state_file
, "scalar_nonchar ");
879 else if (current
== &scalar_char
)
880 fprintf (state_file
, "scalar_char ");
882 fatal ("Unexpected type in write_state_scalar_type");
884 write_state_common_type_content (current
);
887 /* Write the string type. There is only one such thing! */
889 state_writer::write_state_string_type (type_p current
)
891 if (current
== &string_type
)
893 write_any_indent (0);
894 fprintf (state_file
, "string ");
895 write_state_common_type_content (current
);
898 fatal ("Unexpected type in write_state_string_type");
901 /* Write an undefined type. */
903 state_writer::write_state_undefined_type (type_p current
)
905 DBGPRINTF ("undefined type @ %p #%d '%s'", (void *) current
,
906 current
->state_number
, current
->u
.s
.tag
);
907 write_any_indent (0);
908 fprintf (state_file
, "undefined ");
909 gcc_assert (current
->gc_used
== GC_UNUSED
);
910 write_state_common_type_content (current
);
911 if (current
->u
.s
.tag
!= NULL
)
912 write_state_a_string (current
->u
.s
.tag
);
915 write_any_indent (0);
916 fprintf (state_file
, "nil");
919 write_state_fileloc (type_lineloc (current
));
923 /* Common code to write structure like types. */
925 state_writer::write_state_struct_union_type (type_p current
,
928 DBGPRINTF ("%s type @ %p #%d '%s'", kindstr
, (void *) current
,
929 current
->state_number
, current
->u
.s
.tag
);
930 write_any_indent (0);
931 fprintf (state_file
, "%s ", kindstr
);
932 write_state_common_type_content (current
);
933 if (current
->u
.s
.tag
!= NULL
)
934 write_state_a_string (current
->u
.s
.tag
);
937 write_any_indent (0);
938 fprintf (state_file
, "nil");
941 write_state_fileloc (type_lineloc (current
));
942 write_state_fields (current
->u
.s
.fields
);
943 write_state_options (current
->u
.s
.opt
);
944 write_state_lang_bitmap (current
->u
.s
.bitmap
);
948 /* Write a GTY struct type. */
950 state_writer::write_state_struct_type (type_p current
)
952 write_state_struct_union_type (current
, "struct");
953 write_state_type (current
->u
.s
.lang_struct
);
954 write_state_type (current
->u
.s
.base_class
);
957 /* Write a GTY user-defined struct type. */
959 state_writer::write_state_user_struct_type (type_p current
)
961 DBGPRINTF ("user_struct type @ %p #%d '%s'", (void *) current
,
962 current
->state_number
, current
->u
.s
.tag
);
963 write_any_indent (0);
964 fprintf (state_file
, "user_struct ");
965 write_state_common_type_content (current
);
966 if (current
->u
.s
.tag
!= NULL
)
967 write_state_a_string (current
->u
.s
.tag
);
970 write_any_indent (0);
971 fprintf (state_file
, "nil");
973 write_state_fileloc (type_lineloc (current
));
974 write_state_fields (current
->u
.s
.fields
);
977 /* write a GTY union type. */
979 state_writer::write_state_union_type (type_p current
)
981 write_state_struct_union_type (current
, "union");
982 write_state_type (current
->u
.s
.lang_struct
);
985 /* Write a lang_struct type. This is tricky and was painful to debug,
986 we deal with the next field specifically within their lang_struct
987 subfield, which points to a linked list of homonumous types.
988 Change this function with extreme care, see also
989 read_state_lang_struct_type. */
991 state_writer::write_state_lang_struct_type (type_p current
)
995 const char *homoname
= 0;
996 write_state_struct_union_type (current
, "lang_struct");
997 /* lang_struct-ures are particularly tricky, since their
998 u.s.lang_struct field gives a list of homonymous struct-s or
1000 DBGPRINTF ("lang_struct @ %p #%d", (void *) current
, current
->state_number
);
1001 for (hty
= current
->u
.s
.lang_struct
; hty
!= NULL
; hty
= hty
->next
)
1004 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype
,
1005 (void *) hty
, hty
->state_number
, hty
->u
.s
.tag
);
1006 /* Every member of the homonymous list should have the same tag. */
1007 gcc_assert (union_or_struct_p (hty
));
1008 gcc_assert (hty
->u
.s
.lang_struct
== current
);
1010 homoname
= hty
->u
.s
.tag
;
1011 gcc_assert (strcmp (homoname
, hty
->u
.s
.tag
) == 0);
1013 begin_s_expr ("homotypes");
1014 fprintf (state_file
, "%d", nbhomontype
);
1015 for (hty
= current
->u
.s
.lang_struct
; hty
!= NULL
; hty
= hty
->next
)
1016 write_state_type (hty
);
1020 /* Write a pointer type. */
1022 state_writer::write_state_pointer_type (type_p current
)
1024 write_any_indent (0);
1025 fprintf (state_file
, "pointer ");
1026 write_state_common_type_content (current
);
1027 write_state_type (current
->u
.p
);
1030 /* Write an array type. */
1032 state_writer::write_state_array_type (type_p current
)
1034 write_any_indent (0);
1035 fprintf (state_file
, "array ");
1036 write_state_common_type_content (current
);
1037 if (current
->u
.a
.len
!= NULL
)
1038 write_state_a_string (current
->u
.a
.len
);
1041 write_any_indent (1);
1042 fprintf (state_file
, " nil");
1045 write_any_indent (1);
1046 fprintf (state_file
, " ");
1047 write_state_type (current
->u
.a
.p
);
1050 /* Write the gc_used information. */
1052 state_writer::write_state_gc_used (enum gc_used_enum gus
)
1054 write_any_indent (1);
1058 fprintf (state_file
, " gc_unused");
1061 fprintf (state_file
, " gc_used");
1063 case GC_MAYBE_POINTED_TO
:
1064 fprintf (state_file
, " gc_maybe_pointed_to");
1067 fprintf (state_file
, " gc_pointed_to");
1074 /* Utility routine to write the common content of all types. Notice
1075 that the next field is *not* written on purpose. */
1077 state_writer::write_state_common_type_content (type_p current
)
1079 write_any_indent (0);
1080 fprintf (state_file
, "%d ", current
->state_number
);
1081 /* We do not write the next type, because list of types are
1082 explicitly written. However, lang_struct are special in that
1083 respect. See function write_state_lang_struct_type for more. */
1084 write_state_type (current
->pointer_to
);
1085 write_state_gc_used (current
->gc_used
);
1089 /* The important and recursive routine writing GTY types as understood
1090 by gengtype. Types which have a positive state_number have already
1091 been seen and written. */
1093 state_writer::write_state_type (type_p current
)
1095 write_any_indent (0);
1096 if (current
== NULL
)
1098 fprintf (state_file
, "nil ");
1102 begin_s_expr ("type");
1104 if (current
->state_number
> 0)
1106 write_any_indent (0);
1107 fprintf (state_file
, "already_seen %d", current
->state_number
);
1111 m_state_written_type_count
++;
1112 DBGPRINTF ("writing type #%d @%p old number %d", m_state_written_type_count
,
1113 (void *) current
, current
->state_number
);
1114 current
->state_number
= m_state_written_type_count
;
1115 switch (current
->kind
)
1119 case TYPE_UNDEFINED
:
1120 write_state_undefined_type (current
);
1123 write_state_struct_type (current
);
1125 case TYPE_USER_STRUCT
:
1126 write_state_user_struct_type (current
);
1129 write_state_union_type (current
);
1132 write_state_pointer_type (current
);
1135 write_state_array_type (current
);
1137 case TYPE_LANG_STRUCT
:
1138 write_state_lang_struct_type (current
);
1141 write_state_scalar_type (current
);
1144 write_state_string_type (current
);
1149 /* Terminate the "type" s-expression. */
1156 state_writer::write_state_pair (pair_p current
)
1158 if (current
== NULL
)
1160 write_any_indent (0);
1161 fprintf (state_file
, "nil)");
1165 begin_s_expr ("pair");
1167 if (current
->name
!= NULL
)
1168 write_state_a_string (current
->name
);
1170 write_state_a_string ("nil");
1172 write_state_type (current
->type
);
1173 write_state_fileloc (&(current
->line
));
1174 write_state_options (current
->opt
);
1176 /* Terminate the "pair" s-expression. */
1180 /* Write a pair list and return the number of pairs written. */
1182 state_writer::write_state_pair_list (pair_p list
)
1187 for (current
= list
; current
!= NULL
; current
= current
->next
)
1189 write_state_pair (current
);
1196 /* When writing imported linked lists, like typedefs, structures, ... we count
1197 their length first and write it. This eases the reading, and enables an
1198 extra verification on the number of actually read items. */
1200 /* Write our typedefs. */
1202 state_writer::write_state_typedefs (void)
1204 int nbtypedefs
= pair_list_length (typedefs
);
1206 begin_s_expr ("typedefs");
1207 fprintf (state_file
, "%d", nbtypedefs
);
1208 nbpairs
= write_state_pair_list (typedefs
);
1209 gcc_assert (nbpairs
== nbtypedefs
);
1211 if (verbosity_level
>= 2)
1212 printf ("%s wrote %d typedefs\n", progname
, nbtypedefs
);
1215 /* Write our structures. */
1217 state_writer::write_state_structures (void)
1222 for (current
= structures
; current
!= NULL
; current
= current
->next
)
1225 begin_s_expr ("structures");
1226 fprintf (state_file
, "%d", nbstruct
);
1228 for (current
= structures
; current
!= NULL
; current
= current
->next
)
1231 write_state_type (current
);
1234 /* Terminate the "structures" s-expression. */
1236 if (verbosity_level
>= 2)
1237 printf ("%s wrote %d structures in state\n", progname
, nbstruct
);
1240 /* Write our variables. */
1242 state_writer::write_state_variables (void)
1244 int nbvars
= pair_list_length (variables
);
1246 begin_s_expr ("variables");
1247 fprintf (state_file
, "%d", nbvars
);
1248 nbpairs
= write_state_pair_list (variables
);
1249 gcc_assert (nbpairs
== nbvars
);
1251 if (verbosity_level
>= 2)
1252 printf ("%s wrote %d variables.\n", progname
, nbvars
);
1255 /* Write the source directory. File locations within the source
1256 directory have been written specifically. */
1258 state_writer::write_state_srcdir (void)
1260 begin_s_expr ("srcdir");
1261 write_state_a_string (srcdir
);
1265 /* Count and write the list of our files. */
1267 state_writer::write_state_files_list (void)
1270 /* Write the list of files with their lang_bitmap. */
1271 begin_s_expr ("fileslist");
1272 fprintf (state_file
, "%d", (int) num_gt_files
);
1273 for (i
= 0; i
< (int) num_gt_files
; i
++)
1275 const char *cursrcrelpath
= NULL
;
1276 const input_file
*curfil
= gt_files
[i
];
1277 /* Most of the files are inside $(srcdir) so it is worth to
1278 handle them specially. */
1279 cursrcrelpath
= get_file_srcdir_relative_path (curfil
);
1282 begin_s_expr ("srcfile");
1283 fprintf (state_file
, "%d ", get_lang_bitmap (curfil
));
1284 write_state_a_string (cursrcrelpath
);
1288 begin_s_expr ("file");
1289 fprintf (state_file
, "%d ", get_lang_bitmap (curfil
));
1290 write_state_a_string (get_input_file_name (curfil
));
1292 /* Terminate the inner s-expression (either "srcfile" or "file"). */
1295 /* Terminate the "fileslist" s-expression. */
1299 /* Write the list of GCC front-end languages. */
1301 state_writer::write_state_languages (void)
1304 begin_s_expr ("languages");
1305 fprintf (state_file
, "%d", (int) num_lang_dirs
);
1306 for (i
= 0; i
< (int) num_lang_dirs
; i
++)
1308 /* Languages names are identifiers, we expect only letters or
1309 underscores or digits in them. In particular, C++ is not a
1310 valid language name, but cp is valid. */
1311 fprintf (state_file
, " %s", lang_dir_names
[i
]);
1316 /* Write the trailer. */
1318 write_state_trailer (void)
1320 /* This test should probably catch IO errors like disk full... */
1321 if (fputs ("\n(!endfile)\n", state_file
) == EOF
)
1322 fatal ("failed to write state trailer [%s]", xstrerror (errno
));
1325 /* The write_state routine is the only writing routine called by main
1326 in gengtype.c. To avoid messing the state if gengtype is
1327 interrupted or aborted, we write a temporary file and rename it
1328 after having written it in totality. */
1330 write_state (const char *state_path
)
1334 char *temp_state_path
= NULL
;
1335 char tempsuffix
[40];
1338 /* We write a unique temporary file which is renamed when complete
1339 * only. So even if gengtype is interrupted, the written state file
1340 * won't be partially written, since the temporary file is not yet
1341 * renamed in that case. */
1342 memset (tempsuffix
, 0, sizeof (tempsuffix
));
1343 snprintf (tempsuffix
, sizeof (tempsuffix
) - 1, "-%ld-%d.tmp", (long) now
,
1345 temp_state_path
= concat (state_path
, tempsuffix
, NULL
);
1346 state_file
= fopen (temp_state_path
, "w");
1347 if (state_file
== NULL
)
1348 fatal ("Failed to open file %s for writing state: %s",
1349 temp_state_path
, xstrerror (errno
));
1350 if (verbosity_level
>= 3)
1351 printf ("%s writing state file %s temporarily in %s\n",
1352 progname
, state_path
, temp_state_path
);
1353 /* This is the first line of the state. Perhaps the file utility
1354 could know about that, so don't change it often. */
1355 fprintf (state_file
, ";;;;@@@@ GCC gengtype state\n");
1356 /* Output a few comments for humans. */
1357 fprintf (state_file
,
1358 ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
1359 fprintf (state_file
,
1360 ";;; The format of this file is tied to a particular version of GCC.\n");
1361 fprintf (state_file
,
1362 ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1363 fprintf (state_file
,
1364 ";;; This file should be parsed by the same %s which wrote it.\n",
1369 /* The first non-comment significant line gives the version string. */
1370 sw
.write_state_version (version_string
);
1371 sw
.write_state_srcdir ();
1372 sw
.write_state_languages ();
1373 sw
.write_state_files_list ();
1374 sw
.write_state_structures ();
1375 sw
.write_state_typedefs ();
1376 sw
.write_state_variables ();
1377 write_state_trailer ();
1378 statelen
= ftell (state_file
);
1379 if (ferror (state_file
))
1380 fatal ("output error when writing state file %s [%s]",
1381 temp_state_path
, xstrerror (errno
));
1382 if (fclose (state_file
))
1383 fatal ("failed to close state file %s [%s]",
1384 temp_state_path
, xstrerror (errno
));
1385 if (rename (temp_state_path
, state_path
))
1386 fatal ("failed to rename %s to state file %s [%s]", temp_state_path
,
1387 state_path
, xstrerror (errno
));
1388 free (temp_state_path
);
1390 if (verbosity_level
>= 1)
1391 printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1392 progname
, state_path
, statelen
, sw
.m_state_written_type_count
);
1396 /** End of writing routines! The corresponding reading routines follow. **/
1400 /* Forward declarations, since some read_state_* functions are
1402 static void read_state_fileloc (struct fileloc
*line
);
1403 static void read_state_options (options_p
*opt
);
1404 static void read_state_type (type_p
*current
);
1405 static void read_state_pair (pair_p
*pair
);
1406 /* Return the number of pairs actually read. */
1407 static int read_state_pair_list (pair_p
*list
);
1408 static void read_state_fields (pair_p
*fields
);
1409 static void read_state_common_type_content (type_p current
);
1414 /* Record into the state_seen_types hash-table a type which we are
1415 reading, to enable recursive or circular references to it. */
1417 record_type (type_p type
)
1421 slot
= htab_find_slot (state_seen_types
, type
, INSERT
);
1427 /* Read an already seen type. */
1429 read_state_already_seen_type (type_p
*type
)
1431 struct state_token_st
*t0
= peek_state_token (0);
1433 if (state_token_kind (t0
) == STOK_INTEGER
)
1436 struct type loctype
= { TYPE_SCALAR
, 0, 0, 0, GC_UNUSED
, {0} };
1438 loctype
.state_number
= t0
->stok_un
.stok_num
;
1439 slot
= htab_find_slot (state_seen_types
, &loctype
, NO_INSERT
);
1442 fatal_reading_state (t0
, "Unknown type");
1445 next_state_tokens (1);
1446 *type
= (type_p
) *slot
;
1450 fatal_reading_state (t0
, "Bad seen type");
1455 /* Read the scalar_nonchar type. */
1457 read_state_scalar_nonchar_type (type_p
*type
)
1459 *type
= &scalar_nonchar
;
1460 read_state_common_type_content (*type
);
1464 /* Read the scalar_char type. */
1466 read_state_scalar_char_type (type_p
*type
)
1468 *type
= &scalar_char
;
1469 read_state_common_type_content (*type
);
1472 /* Read the string_type. */
1474 read_state_string_type (type_p
*type
)
1476 *type
= &string_type
;
1477 read_state_common_type_content (*type
);
1481 /* Read a lang_bitmap representing a set of GCC front-end languages. */
1483 read_state_lang_bitmap (lang_bitmap
*bitmap
)
1485 struct state_token_st
*t
;
1487 t
= peek_state_token (0);
1488 if (state_token_kind (t
) == STOK_INTEGER
)
1490 *bitmap
= t
->stok_un
.stok_num
;
1491 next_state_tokens (1);
1495 fatal_reading_state (t
, "Bad syntax for bitmap");
1500 /* Read an undefined type. */
1502 read_state_undefined_type (type_p type
)
1504 struct state_token_st
*t0
;
1506 type
->kind
= TYPE_UNDEFINED
;
1507 read_state_common_type_content (type
);
1508 t0
= peek_state_token (0);
1509 if (state_token_kind (t0
) == STOK_STRING
)
1511 if (state_token_is_name (t0
, "nil"))
1513 type
->u
.s
.tag
= NULL
;
1514 DBGPRINTF ("read anonymous undefined type @%p #%d",
1515 (void *) type
, type
->state_number
);
1519 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1520 DBGPRINTF ("read undefined type @%p #%d '%s'",
1521 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1524 next_state_tokens (1);
1525 read_state_fileloc (&(type
->u
.s
.line
));
1529 fatal_reading_state (t0
, "Bad tag in undefined type");
1534 /* Read a GTY-ed struct type. */
1536 read_state_struct_type (type_p type
)
1538 struct state_token_st
*t0
;
1540 type
->kind
= TYPE_STRUCT
;
1541 read_state_common_type_content (type
);
1542 t0
= peek_state_token (0);
1543 if (state_token_kind (t0
) == STOK_STRING
)
1545 if (state_token_is_name (t0
, "nil"))
1547 type
->u
.s
.tag
= NULL
;
1548 DBGPRINTF ("read anonymous struct type @%p #%d",
1549 (void *) type
, type
->state_number
);
1553 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1554 DBGPRINTF ("read struct type @%p #%d '%s'",
1555 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1558 next_state_tokens (1);
1559 read_state_fileloc (&(type
->u
.s
.line
));
1560 read_state_fields (&(type
->u
.s
.fields
));
1561 read_state_options (&(type
->u
.s
.opt
));
1562 read_state_lang_bitmap (&(type
->u
.s
.bitmap
));
1563 read_state_type (&(type
->u
.s
.lang_struct
));
1564 read_state_type (&(type
->u
.s
.base_class
));
1565 if (type
->u
.s
.base_class
)
1566 add_subclass (type
->u
.s
.base_class
, type
);
1570 fatal_reading_state (t0
, "Bad tag in struct type");
1575 /* Read a GTY-ed user-provided struct TYPE. */
1578 read_state_user_struct_type (type_p type
)
1580 struct state_token_st
*t0
;
1582 type
->kind
= TYPE_USER_STRUCT
;
1583 read_state_common_type_content (type
);
1584 t0
= peek_state_token (0);
1585 if (state_token_kind (t0
) == STOK_STRING
)
1587 if (state_token_is_name (t0
, "nil"))
1589 type
->u
.s
.tag
= NULL
;
1590 DBGPRINTF ("read anonymous struct type @%p #%d",
1591 (void *) type
, type
->state_number
);
1595 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1596 DBGPRINTF ("read struct type @%p #%d '%s'",
1597 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1600 next_state_tokens (1);
1601 read_state_fileloc (&(type
->u
.s
.line
));
1602 read_state_fields (&(type
->u
.s
.fields
));
1606 fatal_reading_state (t0
, "Bad tag in user-struct type");
1611 /* Read a GTY-ed union type. */
1613 read_state_union_type (type_p type
)
1615 struct state_token_st
*t0
;
1617 type
->kind
= TYPE_UNION
;
1618 read_state_common_type_content (type
);
1619 t0
= peek_state_token (0);
1620 if (state_token_kind (t0
) == STOK_STRING
)
1622 if (state_token_is_name (t0
, "nil"))
1624 type
->u
.s
.tag
= NULL
;
1625 DBGPRINTF ("read anonymous union type @%p #%d",
1626 (void *) type
, type
->state_number
);
1630 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1631 DBGPRINTF ("read union type @%p #%d '%s'",
1632 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1634 next_state_tokens (1);
1635 read_state_fileloc (&(type
->u
.s
.line
));
1636 read_state_fields (&(type
->u
.s
.fields
));
1637 read_state_options (&(type
->u
.s
.opt
));
1638 read_state_lang_bitmap (&(type
->u
.s
.bitmap
));
1639 read_state_type (&(type
->u
.s
.lang_struct
));
1642 fatal_reading_state (t0
, "Bad tag in union type");
1646 /* Read a GTY-ed pointer type. */
1648 read_state_pointer_type (type_p type
)
1650 type
->kind
= TYPE_POINTER
;
1651 read_state_common_type_content (type
);
1652 DBGPRINTF ("read pointer type @%p #%d", (void *) type
, type
->state_number
);
1653 read_state_type (&(type
->u
.p
));
1657 /* Read a GTY-ed array type. */
1659 read_state_array_type (type_p type
)
1661 struct state_token_st
*t0
;
1663 type
->kind
= TYPE_ARRAY
;
1664 read_state_common_type_content (type
);
1665 t0
= peek_state_token (0);
1666 if (state_token_kind (t0
) == STOK_STRING
)
1668 type
->u
.a
.len
= xstrdup (t0
->stok_un
.stok_string
);
1669 DBGPRINTF ("read array type @%p #%d length '%s'",
1670 (void *) type
, type
->state_number
, type
->u
.a
.len
);
1671 next_state_tokens (1);
1674 else if (state_token_is_name (t0
, "nil"))
1676 type
->u
.a
.len
= NULL
;
1677 DBGPRINTF ("read array type @%p #%d without length",
1678 (void *) type
, type
->state_number
);
1679 next_state_tokens (1);
1683 fatal_reading_state (t0
, "Bad array name type");
1684 read_state_type (&(type
->u
.a
.p
));
1689 /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
1690 front-end languages. This is a tricky function and it was painful
1691 to debug. Change it with extreme care. See also
1692 write_state_lang_struct_type. */
1694 read_state_lang_struct_type (type_p type
)
1696 struct state_token_st
*t0
= NULL
;
1697 struct state_token_st
*t1
= NULL
;
1698 struct state_token_st
*t2
= NULL
;
1700 type
->kind
= TYPE_LANG_STRUCT
;
1701 read_state_common_type_content (type
);
1702 t0
= peek_state_token (0);
1703 if (state_token_kind (t0
) == STOK_STRING
)
1705 if (state_token_is_name (t0
, "nil"))
1707 DBGPRINTF ("read anonymous lang_struct type @%p #%d",
1708 (void *) type
, type
->state_number
);
1709 type
->u
.s
.tag
= NULL
;
1713 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1714 DBGPRINTF ("read lang_struct type @%p #%d '%s'",
1715 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1717 next_state_tokens (1);
1720 fatal_reading_state (t0
, "Bad tag in lang struct type");
1721 read_state_fileloc (&(type
->u
.s
.line
));
1722 read_state_fields (&(type
->u
.s
.fields
));
1723 read_state_options (&(type
->u
.s
.opt
));
1724 read_state_lang_bitmap (&(type
->u
.s
.bitmap
));
1725 /* Within lang_struct-ures, the lang_struct field is a linked list
1726 of homonymous types! */
1727 t0
= peek_state_token (0);
1728 t1
= peek_state_token (1);
1729 t2
= peek_state_token (2);
1730 /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
1731 if (state_token_kind (t0
) == STOK_LEFTPAR
1732 && state_token_is_name (t1
, "!homotypes")
1733 && state_token_kind (t2
) == STOK_INTEGER
)
1735 type_p
*prevty
= &type
->u
.s
.lang_struct
;
1736 int nbhomotype
= t2
->stok_un
.stok_num
;
1738 t0
= t1
= t2
= NULL
;
1739 next_state_tokens (3);
1740 for (i
= 0; i
< nbhomotype
; i
++)
1742 read_state_type (prevty
);
1743 t0
= peek_state_token (0);
1745 prevty
= &(*prevty
)->next
;
1747 fatal_reading_state (t0
,
1748 "expecting type in homotype list for lang_struct");
1750 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1751 fatal_reading_state (t0
,
1752 "expecting ) in homotype list for lang_struct");
1753 next_state_tokens (1);
1756 fatal_reading_state (t0
, "expecting !homotypes for lang_struct");
1760 /* Read the gc used information. */
1762 read_state_gc_used (enum gc_used_enum
*pgus
)
1764 struct state_token_st
*t0
= peek_state_token (0);
1765 if (state_token_is_name (t0
, "gc_unused"))
1767 else if (state_token_is_name (t0
, "gc_used"))
1769 else if (state_token_is_name (t0
, "gc_maybe_pointed_to"))
1770 *pgus
= GC_MAYBE_POINTED_TO
;
1771 else if (state_token_is_name (t0
, "gc_pointed_to"))
1772 *pgus
= GC_POINTED_TO
;
1774 fatal_reading_state (t0
, "invalid gc_used information");
1775 next_state_tokens (1);
1779 /* Utility function to read the common content of types. */
1781 read_state_common_type_content (type_p current
)
1783 struct state_token_st
*t0
= peek_state_token (0);
1785 if (state_token_kind (t0
) == STOK_INTEGER
)
1787 current
->state_number
= t0
->stok_un
.stok_num
;
1788 next_state_tokens (1);
1789 record_type (current
);
1792 fatal_reading_state_printf (t0
,
1793 "Expected integer for state_number line %d",
1795 /* We don't read the next field of the type. */
1796 read_state_type (¤t
->pointer_to
);
1797 read_state_gc_used (¤t
->gc_used
);
1801 /* Read a GTY-ed type. */
1803 read_state_type (type_p
*current
)
1805 struct state_token_st
*t0
= peek_state_token (0);
1806 struct state_token_st
*t1
= peek_state_token (1);
1808 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1809 state_token_is_name (t1
, "!type"))
1811 next_state_tokens (2);
1812 t0
= peek_state_token (0);
1813 if (state_token_is_name (t0
, "already_seen"))
1815 next_state_tokens (1);
1816 read_state_already_seen_type (current
);
1820 t0
= peek_state_token (0);
1822 if (state_token_is_name (t0
, "scalar_nonchar"))
1824 next_state_tokens (1);
1825 read_state_scalar_nonchar_type (current
);
1827 else if (state_token_is_name (t0
, "scalar_char"))
1829 next_state_tokens (1);
1830 read_state_scalar_char_type (current
);
1832 else if (state_token_is_name (t0
, "string"))
1834 next_state_tokens (1);
1835 read_state_string_type (current
);
1837 else if (state_token_is_name (t0
, "undefined"))
1839 *current
= XCNEW (struct type
);
1840 next_state_tokens (1);
1841 read_state_undefined_type (*current
);
1843 else if (state_token_is_name (t0
, "struct"))
1845 *current
= XCNEW (struct type
);
1846 next_state_tokens (1);
1847 read_state_struct_type (*current
);
1849 else if (state_token_is_name (t0
, "union"))
1851 *current
= XCNEW (struct type
);
1852 next_state_tokens (1);
1853 read_state_union_type (*current
);
1855 else if (state_token_is_name (t0
, "lang_struct"))
1857 *current
= XCNEW (struct type
);
1858 next_state_tokens (1);
1859 read_state_lang_struct_type (*current
);
1861 else if (state_token_is_name (t0
, "pointer"))
1863 *current
= XCNEW (struct type
);
1864 next_state_tokens (1);
1865 read_state_pointer_type (*current
);
1867 else if (state_token_is_name (t0
, "array"))
1869 *current
= XCNEW (struct type
);
1870 next_state_tokens (1);
1871 read_state_array_type (*current
);
1873 else if (state_token_is_name (t0
, "user_struct"))
1875 *current
= XCNEW (struct type
);
1876 next_state_tokens (1);
1877 read_state_user_struct_type (*current
);
1880 fatal_reading_state (t0
, "bad type in (!type");
1882 t0
= peek_state_token (0);
1883 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1884 fatal_reading_state (t0
, "missing ) in type");
1885 next_state_tokens (1);
1887 else if (state_token_is_name (t0
, "nil"))
1889 next_state_tokens (1);
1893 fatal_reading_state (t0
, "bad type syntax");
1897 /* Read a file location. Files within the source directory are dealt
1898 with specifically. */
1900 read_state_fileloc (struct fileloc
*floc
)
1902 bool issrcfile
= false;
1903 struct state_token_st
*t0
= peek_state_token (0);
1904 struct state_token_st
*t1
= peek_state_token (1);
1906 gcc_assert (floc
!= NULL
);
1907 gcc_assert (srcdir
!= NULL
);
1909 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1910 (state_token_is_name (t1
, "!fileloc")
1911 || (issrcfile
= state_token_is_name (t1
, "!srcfileloc"))))
1913 next_state_tokens (2);
1914 t0
= peek_state_token (0);
1915 t1
= peek_state_token (1);
1916 if (state_token_kind (t0
) == STOK_STRING
&&
1917 state_token_kind (t1
) == STOK_INTEGER
)
1919 char *path
= t0
->stok_un
.stok_string
;
1922 static const char dirsepstr
[2] = { DIR_SEPARATOR
, (char) 0 };
1923 char *fullpath
= concat (srcdir
, dirsepstr
, path
, NULL
);
1924 floc
->file
= input_file_by_name (fullpath
);
1928 floc
->file
= input_file_by_name (path
);
1929 floc
->line
= t1
->stok_un
.stok_num
;
1930 next_state_tokens (2);
1933 fatal_reading_state (t0
,
1934 "Bad fileloc syntax, expected path string and line");
1935 t0
= peek_state_token (0);
1936 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1937 fatal_reading_state (t0
, "Bad fileloc syntax, expected )");
1938 next_state_tokens (1);
1940 else if (state_token_is_name (t0
, "nil"))
1942 next_state_tokens (1);
1947 fatal_reading_state (t0
, "Bad fileloc syntax");
1951 /* Read the fields of a GTY-ed type. */
1953 read_state_fields (pair_p
*fields
)
1956 struct state_token_st
*t0
= peek_state_token (0);
1957 struct state_token_st
*t1
= peek_state_token (1);
1958 struct state_token_st
*t2
= peek_state_token (2);
1960 if (state_token_kind (t0
) == STOK_LEFTPAR
1961 && state_token_is_name (t1
, "!fields")
1962 && state_token_kind (t2
) == STOK_INTEGER
)
1964 int nbfields
= t2
->stok_un
.stok_num
;
1966 next_state_tokens (3);
1967 nbpairs
= read_state_pair_list (&tmp
);
1968 t0
= peek_state_token (0);
1969 if (nbpairs
!= nbfields
)
1970 fatal_reading_state_printf
1972 "Mismatched fields number, expected %d got %d", nbpairs
, nbfields
);
1973 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
1974 next_state_tokens (1);
1976 fatal_reading_state (t0
, "Bad fields expecting )");
1983 /* Read a string option. */
1985 read_state_string_option (options_p opt
)
1987 struct state_token_st
*t0
= peek_state_token (0);
1988 opt
->kind
= OPTION_STRING
;
1989 if (state_token_kind (t0
) == STOK_STRING
)
1991 opt
->info
.string
= xstrdup (t0
->stok_un
.stok_string
);
1992 next_state_tokens (1);
1994 else if (state_token_is_name (t0
, "nil"))
1996 opt
->info
.string
= NULL
;
1997 next_state_tokens (1);
2000 fatal_reading_state (t0
, "Missing name in string option");
2004 /* Read a type option. */
2006 read_state_type_option (options_p opt
)
2008 opt
->kind
= OPTION_TYPE
;
2009 read_state_type (&(opt
->info
.type
));
2013 /* Read a nested option. */
2015 read_state_nested_option (options_p opt
)
2017 struct state_token_st
*t0
;
2019 opt
->info
.nested
= XCNEW (struct nested_ptr_data
);
2020 opt
->kind
= OPTION_NESTED
;
2021 read_state_type (&(opt
->info
.nested
->type
));
2022 t0
= peek_state_token (0);
2023 if (state_token_kind (t0
) == STOK_STRING
)
2025 opt
->info
.nested
->convert_from
= xstrdup (t0
->stok_un
.stok_string
);
2026 next_state_tokens (1);
2028 else if (state_token_is_name (t0
, "nil"))
2030 opt
->info
.nested
->convert_from
= NULL
;
2031 next_state_tokens (1);
2034 fatal_reading_state (t0
, "Bad nested convert_from option");
2036 t0
= peek_state_token (0);
2037 if (state_token_kind (t0
) == STOK_STRING
)
2039 opt
->info
.nested
->convert_to
= xstrdup (t0
->stok_un
.stok_string
);
2040 next_state_tokens (1);
2042 else if (state_token_is_name (t0
, "nil"))
2044 opt
->info
.nested
->convert_to
= NULL
;
2045 next_state_tokens (1);
2048 fatal_reading_state (t0
, "Bad nested convert_from option");
2052 /* Read an GTY option. */
2054 read_state_option (options_p
*opt
)
2056 struct state_token_st
*t0
= peek_state_token (0);
2057 struct state_token_st
*t1
= peek_state_token (1);
2059 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2060 state_token_is_name (t1
, "!option"))
2062 next_state_tokens (2);
2063 t0
= peek_state_token (0);
2064 if (state_token_kind (t0
) == STOK_NAME
)
2066 *opt
= XCNEW (struct options
);
2067 if (state_token_is_name (t0
, "nil"))
2068 (*opt
)->name
= NULL
;
2070 (*opt
)->name
= t0
->stok_un
.stok_ident
->stid_name
;
2071 next_state_tokens (1);
2072 t0
= peek_state_token (0);
2073 if (state_token_kind (t0
) == STOK_NAME
)
2075 if (state_token_is_name (t0
, "string"))
2077 next_state_tokens (1);
2078 read_state_string_option (*opt
);
2080 else if (state_token_is_name (t0
, "type"))
2082 next_state_tokens (1);
2083 read_state_type_option (*opt
);
2085 else if (state_token_is_name (t0
, "nested"))
2087 next_state_tokens (1);
2088 read_state_nested_option (*opt
);
2091 fatal_reading_state (t0
, "Bad option type");
2092 t0
= peek_state_token (0);
2093 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
2094 fatal_reading_state (t0
, "Bad syntax in option, expecting )");
2096 next_state_tokens (1);
2099 fatal_reading_state (t0
, "Missing option type");
2102 fatal_reading_state (t0
, "Bad name for option");
2105 fatal_reading_state (t0
, "Bad option, waiting for )");
2108 /* Read a list of options. */
2110 read_state_options (options_p
*opt
)
2112 options_p head
= NULL
;
2113 options_p previous
= NULL
;
2114 options_p current_option
= NULL
;
2115 struct state_token_st
*t0
= peek_state_token (0);
2116 struct state_token_st
*t1
= peek_state_token (1);
2118 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2119 state_token_is_name (t1
, "!options"))
2121 next_state_tokens (2);
2122 t0
= peek_state_token (0);
2123 while (state_token_kind (t0
) != STOK_RIGHTPAR
)
2125 read_state_option (¤t_option
);
2128 head
= current_option
;
2133 previous
->next
= current_option
;
2134 previous
= current_option
;
2136 t0
= peek_state_token (0);
2138 next_state_tokens (1);
2140 else if (state_token_is_name (t0
, "nil"))
2142 next_state_tokens (1);
2145 fatal_reading_state (t0
, "Bad options syntax");
2151 /* Read a version, and check against the version of the gengtype. */
2153 read_state_version (const char *version_string
)
2155 struct state_token_st
*t0
= peek_state_token (0);
2156 struct state_token_st
*t1
= peek_state_token (1);
2158 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2159 state_token_is_name (t1
, "!version"))
2161 next_state_tokens (2);
2162 t0
= peek_state_token (0);
2163 t1
= peek_state_token (1);
2164 if (state_token_kind (t0
) == STOK_STRING
&&
2165 state_token_kind (t1
) == STOK_RIGHTPAR
)
2167 /* Check that the read version string is the same as current
2169 if (strcmp (version_string
, t0
->stok_un
.stok_string
))
2170 fatal_reading_state_printf (t0
,
2171 "version string mismatch; expecting %s but got %s",
2173 t0
->stok_un
.stok_string
);
2174 next_state_tokens (2);
2177 fatal_reading_state (t0
, "Missing version or right parenthesis");
2180 fatal_reading_state (t0
, "Bad version syntax");
2186 read_state_pair (pair_p
*current
)
2188 struct state_token_st
*t0
= peek_state_token (0);
2189 struct state_token_st
*t1
= peek_state_token (1);
2190 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2191 state_token_is_name (t1
, "!pair"))
2193 *current
= XCNEW (struct pair
);
2194 next_state_tokens (2);
2195 t0
= peek_state_token (0);
2196 if (state_token_kind (t0
) == STOK_STRING
)
2198 if (strcmp (t0
->stok_un
.stok_string
, "nil") == 0)
2200 (*current
)->name
= NULL
;
2204 (*current
)->name
= xstrdup (t0
->stok_un
.stok_string
);
2206 next_state_tokens (1);
2207 read_state_type (&((*current
)->type
));
2208 read_state_fileloc (&((*current
)->line
));
2209 read_state_options (&((*current
)->opt
));;
2210 t0
= peek_state_token (0);
2211 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
2213 next_state_tokens (1);
2217 fatal_reading_state (t0
, "Bad syntax for pair, )");
2222 fatal_reading_state (t0
, "Bad name for pair");
2225 else if (state_token_kind (t0
) == STOK_NAME
&&
2226 state_token_is_name (t0
, "nil"))
2228 next_state_tokens (1);
2232 fatal_reading_state_printf (t0
, "Bad syntax for pair, (!pair %d",
2233 state_token
->stok_kind
);
2237 /* Return the number of pairs actually read. */
2239 read_state_pair_list (pair_p
*list
)
2243 pair_p previous
= NULL
;
2245 struct state_token_st
*t0
= peek_state_token (0);
2246 while (t0
&& state_token_kind (t0
) != STOK_RIGHTPAR
)
2248 read_state_pair (&tmp
);
2256 previous
->next
= tmp
;
2259 t0
= peek_state_token (0);
2263 /* don't consume the ); the caller will eat it. */
2268 /* Read the typedefs. */
2270 read_state_typedefs (pair_p
*typedefs
)
2274 struct state_token_st
*t0
= peek_state_token (0);
2275 struct state_token_st
*t1
= peek_state_token (1);
2276 struct state_token_st
*t2
= peek_state_token (2);
2278 if (state_token_kind (t0
) == STOK_LEFTPAR
2279 && state_token_is_name (t1
, "!typedefs")
2280 && state_token_kind (t2
) == STOK_INTEGER
)
2283 nbtypedefs
= t2
->stok_un
.stok_num
;
2284 next_state_tokens (3);
2285 nbpairs
= read_state_pair_list (&list
);
2286 t0
= peek_state_token (0);
2287 if (nbpairs
!= nbtypedefs
)
2288 fatal_reading_state_printf
2290 "invalid number of typedefs, expected %d but got %d",
2291 nbtypedefs
, nbpairs
);
2292 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
2293 next_state_tokens (1);
2295 fatal_reading_state (t0
, "Bad typedefs syntax )");
2298 fatal_reading_state (t0
, "Bad typedefs syntax (!typedefs");
2300 if (verbosity_level
>= 2)
2301 printf ("%s read %d typedefs from state\n", progname
, nbtypedefs
);
2306 /* Read the structures. */
2308 read_state_structures (type_p
*structures
)
2311 type_p previous
= NULL
;
2313 int nbstruct
= 0, countstruct
= 0;
2314 struct state_token_st
*t0
= peek_state_token (0);
2315 struct state_token_st
*t1
= peek_state_token (1);
2316 struct state_token_st
*t2
= peek_state_token (2);
2318 if (state_token_kind (t0
) == STOK_LEFTPAR
2319 && state_token_is_name (t1
, "!structures")
2320 && state_token_kind (t2
) == STOK_INTEGER
)
2322 nbstruct
= t2
->stok_un
.stok_num
;
2323 next_state_tokens (3);
2324 t0
= peek_state_token (0);
2325 while (t0
&& state_token_kind (t0
) != STOK_RIGHTPAR
)
2328 read_state_type (&tmp
);
2337 previous
->next
= tmp
;
2340 t0
= peek_state_token (0);
2342 next_state_tokens (1);
2345 fatal_reading_state (t0
, "Bad structures syntax");
2346 if (countstruct
!= nbstruct
)
2347 fatal_reading_state_printf (NULL_STATE_TOKEN
,
2348 "expected %d structures but got %d",
2349 nbstruct
, countstruct
);
2350 if (verbosity_level
>= 2)
2351 printf ("%s read %d structures from state\n", progname
, nbstruct
);
2356 /* Read the variables. */
2358 read_state_variables (pair_p
*variables
)
2362 struct state_token_st
*t0
= peek_state_token (0);
2363 struct state_token_st
*t1
= peek_state_token (1);
2364 struct state_token_st
*t2
= peek_state_token (2);
2366 if (state_token_kind (t0
) == STOK_LEFTPAR
2367 && state_token_is_name (t1
, "!variables")
2368 && state_token_kind (t2
) == STOK_INTEGER
)
2371 nbvars
= t2
->stok_un
.stok_num
;
2372 next_state_tokens (3);
2373 nbpairs
= read_state_pair_list (&list
);
2374 t0
= peek_state_token (0);
2375 if (nbpairs
!= nbvars
)
2376 fatal_reading_state_printf
2377 (t0
, "Invalid number of variables, expected %d but got %d",
2379 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
2380 next_state_tokens (1);
2382 fatal_reading_state (t0
, "Waiting for ) in variables");
2385 fatal_reading_state (t0
, "Bad variables syntax");
2387 if (verbosity_level
>= 2)
2388 printf ("%s read %d variables from state\n", progname
, nbvars
);
2392 /* Read the source directory. */
2394 read_state_srcdir (void)
2396 struct state_token_st
*t0
= peek_state_token (0);
2397 struct state_token_st
*t1
= peek_state_token (1);
2398 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2399 state_token_is_name (t1
, "!srcdir"))
2401 next_state_tokens (2);
2402 t0
= peek_state_token (0);
2403 t1
= peek_state_token (1);
2404 if (state_token_kind (t0
) == STOK_STRING
&&
2405 state_token_kind (t1
) == STOK_RIGHTPAR
)
2407 srcdir
= xstrdup (t0
->stok_un
.stok_string
);
2408 srcdir_len
= strlen (srcdir
);
2409 next_state_tokens (2);
2414 fatal_reading_state (t0
, "Bad srcdir in state_file");
2418 /* Read the sequence of GCC front-end languages. */
2420 read_state_languages (void)
2422 struct state_token_st
*t0
= peek_state_token (0);
2423 struct state_token_st
*t1
= peek_state_token (1);
2424 struct state_token_st
*t2
= peek_state_token (2);
2425 if (state_token_kind (t0
) == STOK_LEFTPAR
2426 && state_token_is_name (t1
, "!languages")
2427 && state_token_kind (t2
) == STOK_INTEGER
)
2430 num_lang_dirs
= t2
->stok_un
.stok_num
;
2431 lang_dir_names
= XCNEWVEC (const char *, num_lang_dirs
);
2432 next_state_tokens (3);
2433 t0
= t1
= t2
= NULL
;
2434 for (i
= 0; i
< (int) num_lang_dirs
; i
++)
2436 t0
= peek_state_token (0);
2437 if (state_token_kind (t0
) != STOK_NAME
)
2438 fatal_reading_state (t0
, "expecting language name in state file");
2439 lang_dir_names
[i
] = t0
->stok_un
.stok_ident
->stid_name
;
2440 next_state_tokens (1);
2442 t0
= peek_state_token (0);
2443 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
2444 fatal_reading_state (t0
, "missing ) in languages list of state file");
2445 next_state_tokens (1);
2448 fatal_reading_state (t0
, "expecting languages list in state file");
2452 /* Read the sequence of files. */
2454 read_state_files_list (void)
2456 struct state_token_st
*t0
= peek_state_token (0);
2457 struct state_token_st
*t1
= peek_state_token (1);
2458 struct state_token_st
*t2
= peek_state_token (2);
2460 if (state_token_kind (t0
) == STOK_LEFTPAR
2461 && state_token_is_name (t1
, "!fileslist")
2462 && state_token_kind (t2
) == STOK_INTEGER
)
2465 num_gt_files
= t2
->stok_un
.stok_num
;
2466 next_state_tokens (3);
2467 t0
= t1
= t2
= NULL
;
2468 gt_files
= XCNEWVEC (const input_file
*, num_gt_files
);
2469 for (i
= 0; i
< (int) num_gt_files
; i
++)
2471 bool issrcfile
= FALSE
;
2472 t0
= t1
= t2
= NULL
;
2473 t0
= peek_state_token (0);
2474 t1
= peek_state_token (1);
2475 t2
= peek_state_token (2);
2476 if (state_token_kind (t0
) == STOK_LEFTPAR
2477 && (state_token_is_name (t1
, "!file")
2478 || (issrcfile
= state_token_is_name (t1
, "!srcfile")))
2479 && state_token_kind (t2
) == STOK_INTEGER
)
2481 lang_bitmap bmap
= t2
->stok_un
.stok_num
;
2482 next_state_tokens (3);
2483 t0
= t1
= t2
= NULL
;
2484 t0
= peek_state_token (0);
2485 t1
= peek_state_token (1);
2486 if (state_token_kind (t0
) == STOK_STRING
2487 && state_token_kind (t1
) == STOK_RIGHTPAR
)
2489 const char *fnam
= t0
->stok_un
.stok_string
;
2490 /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
2491 input_file
*curgt
= NULL
;
2494 static const char dirsepstr
[2] =
2495 { DIR_SEPARATOR
, (char) 0 };
2496 char *fullpath
= concat (srcdir
, dirsepstr
, fnam
, NULL
);
2497 curgt
= input_file_by_name (fullpath
);
2501 curgt
= input_file_by_name (fnam
);
2502 set_lang_bitmap (curgt
, bmap
);
2503 gt_files
[i
] = curgt
;
2504 next_state_tokens (2);
2507 fatal_reading_state (t0
,
2508 "bad file in !fileslist of state file");
2511 fatal_reading_state (t0
,
2512 "expecting file in !fileslist of state file");
2514 t0
= peek_state_token (0);
2515 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
2516 fatal_reading_state (t0
, "missing ) for !fileslist in state file");
2517 next_state_tokens (1);
2520 fatal_reading_state (t0
, "missing !fileslist in state file");
2524 /* Read the trailer. */
2526 read_state_trailer (void)
2528 struct state_token_st
*t0
= peek_state_token (0);
2529 struct state_token_st
*t1
= peek_state_token (1);
2530 struct state_token_st
*t2
= peek_state_token (2);
2532 if (state_token_kind (t0
) == STOK_LEFTPAR
2533 && state_token_is_name (t1
, "!endfile")
2534 && state_token_kind (t2
) == STOK_RIGHTPAR
)
2535 next_state_tokens (3);
2537 fatal_reading_state (t0
, "missing !endfile in state file");
2541 /* Utility functions for the state_seen_types hash table. */
2543 hash_type_number (const void *ty
)
2545 const struct type
*type
= (const struct type
*) ty
;
2547 return type
->state_number
;
2551 equals_type_number (const void *ty1
, const void *ty2
)
2553 const struct type
*type1
= (const struct type
*) ty1
;
2554 const struct type
*type2
= (const struct type
*) ty2
;
2556 return type1
->state_number
== type2
->state_number
;
2560 string_eq (const void *a
, const void *b
)
2562 const char *a0
= (const char *)a
;
2563 const char *b0
= (const char *)b
;
2565 return (strcmp (a0
, b0
) == 0);
2569 /* The function reading the state, called by main from gengtype.c. */
2571 read_state (const char *path
)
2573 state_file
= fopen (path
, "r");
2574 if (state_file
== NULL
)
2575 fatal ("Failed to open state file %s for reading [%s]", path
,
2580 if (verbosity_level
>= 1)
2582 printf ("%s reading state file %s;", progname
, state_path
);
2583 if (verbosity_level
>= 2)
2589 htab_create (2017, hash_type_number
, equals_type_number
, NULL
);
2591 htab_create (4027, htab_hash_string
, string_eq
, NULL
);
2592 read_state_version (version_string
);
2593 read_state_srcdir ();
2594 read_state_languages ();
2595 read_state_files_list ();
2596 read_state_structures (&structures
);
2597 if (ferror (state_file
))
2598 fatal_reading_state_printf
2599 (NULL_STATE_TOKEN
, "input error while reading state [%s]",
2601 read_state_typedefs (&typedefs
);
2602 read_state_variables (&variables
);
2603 read_state_trailer ();
2605 if (verbosity_level
>= 1)
2607 printf ("%s read %ld bytes.\n", progname
, ftell (state_file
));
2611 if (fclose (state_file
))
2612 fatal ("failed to close read state file %s [%s]",
2613 path
, xstrerror (errno
));
2618 /* End of file gengtype-state.c. */