1 /* Gengtype persistent state serialization & de-serialization.
2 Useful for gengtype in plugin mode.
4 Copyright (C) 2010 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>
28 #include "errors.h" /* For fatal. */
29 #include "double-int.h"
31 #include "version.h" /* For version_string & pkgversion_string. */
37 /* Gives the file location of a type, if any. */
38 static inline struct fileloc
*
39 type_lineloc (const_type_p ty
)
49 case TYPE_LANG_STRUCT
:
50 return CONST_CAST (struct fileloc
*, &ty
->u
.s
.line
);
51 case TYPE_PARAM_STRUCT
:
52 return CONST_CAST (struct fileloc
*, &ty
->u
.param_struct
.line
);
63 /* The state file has simplistic lispy lexical tokens. Its lexer gives
64 a linked list of struct state_token_st, thru the peek_state_token
65 function. Lexical tokens are consumed with next_state_tokens. */
68 /* The lexical kind of each lispy token. */
71 STOK_NONE
, /* Never used. */
72 STOK_INTEGER
, /* Integer token. */
73 STOK_STRING
, /* String token. */
74 STOK_LEFTPAR
, /* Left opening parenthesis. */
75 STOK_RIGHTPAR
, /* Right closing parenthesis. */
76 STOK_NAME
/* hash-consed name or identifier. */
80 /* Structure and hash-table used to share identifiers or names. */
83 /* TODO: We could improve the parser by reserving identifiers for
84 state keywords and adding a keyword number for them. That would
85 mean adding another field in this state_ident_st struct. */
86 char stid_name
[1]; /* actually bigger & null terminated */
88 static htab_t state_ident_tab
;
91 /* The state_token_st structure is for lexical tokens in the read
92 state file. The stok_kind field discriminates the union. Tokens
93 are allocated by peek_state_token which calls read_a_state_token
94 which allocate them. Tokens are freed by calls to
95 next_state_tokens. Token are organized in a FIFO look-ahead queue
96 filled by peek_state_token. */
99 enum state_token_en stok_kind
; /* the lexical kind
100 discriminates the stok_un
102 int stok_line
; /* the line number */
103 int stok_col
; /* the column number */
104 const char *stok_file
; /* the file path */
105 struct state_token_st
*stok_next
; /* the next token in the
106 queue, when peeked */
107 union /* discriminated by stok_kind! */
109 int stok_num
; /* when STOK_INTEGER */
110 char stok_string
[1]; /* when STOK_STRING, actual size is
111 bigger and null terminated */
112 struct state_ident_st
*stok_ident
; /* when STOK_IDENT */
113 void *stok_ptr
; /* null otherwise */
121 #define NULL_STATE_TOKEN (struct state_token_st*)0
123 /* the state_token pointer contains the leftmost current token. The
124 tokens are organized in a linked queue, using stok_next, for token
126 struct state_token_st
*state_token
= NULL_STATE_TOKEN
;
128 /* Used by the reading lexer. */
129 static FILE *state_file
;
130 static const char *state_path
= NULL
;
131 static int state_line
= 0;
132 static long state_bol
= 0; /* offset of beginning of line */
135 /* Counter of written types. */
136 static int state_written_type_count
= 0;
139 /* Fatal error messages when reading the state. They are extremely
140 unlikely, and only appear when this gengtype-state.c file is buggy,
141 or when reading a gengtype state which was not generated by the
142 same version of gengtype or GCC. */
145 /* Fatal message while reading state. */
147 fatal_reading_state (struct state_token_st
* tok
, const char*msg
)
150 fatal ("%s:%d:%d: Invalid state file; %s",
151 tok
->stok_file
, tok
->stok_line
, tok
->stok_col
,
154 fatal ("%s:%d: Invalid state file; %s",
155 state_path
, state_line
, msg
);
159 /* Fatal printf-like message while reading state. This can't be a
160 function, because there is no way to pass a va_arg to a variant of
162 #define fatal_reading_state_printf(Tok,Fmt,...) do { \
163 struct state_token_st* badtok = Tok; \
165 fatal ("%s:%d:%d: Invalid state file; " Fmt, \
168 badtok->stok_col, __VA_ARGS__); \
170 fatal ("%s:%d: Invalid state file; " Fmt, \
171 state_path, state_line, __VA_ARGS__); \
175 /* Find or allocate an identifier in our name hash table. */
176 static struct state_ident_st
*
177 state_ident_by_name (const char *name
, enum insert_option optins
)
181 struct state_ident_st
*stid
= NULL
;
183 if (!name
|| !name
[0])
186 slot
= htab_find_slot (state_ident_tab
, name
, optins
);
190 namlen
= strlen (name
);
192 (struct state_ident_st
*) xmalloc (sizeof (struct state_ident_st
) +
194 memset (stid
, 0, sizeof (struct state_ident_st
) + namlen
);
195 strcpy (stid
->stid_name
, name
);
201 /* Our token lexer is heavily inspired by MELT's lexer, and share some
202 code with the file gcc/melt-runtime.c of the GCC MELT branch! We
203 really want the gengtype state to be easily parsable by MELT. This
204 is a usual lispy lexing routine, dealing with spaces and comments,
205 numbers, parenthesis, names, strings. */
206 static struct state_token_st
*
207 read_a_state_token (void)
211 struct state_token_st
*tk
= NULL
;
213 again
: /* Read again, e.g. after a comment or spaces. */
214 c
= getc (state_file
);
218 /* Handle spaces, count lines. */
222 state_bol
= curoff
= ftell (state_file
);
227 /* Skip comments starting with semi-colon. */
232 c
= getc (state_file
);
234 while (c
> 0 && c
!= '\n');
238 state_bol
= curoff
= ftell (state_file
);
242 /* Read signed numbers. */
243 if (ISDIGIT (c
) || c
== '-' || c
== '+')
246 ungetc (c
, state_file
);
247 curoff
= ftell (state_file
);
248 if (fscanf (state_file
, "%d", &n
) <= 0)
249 fatal_reading_state (NULL_STATE_TOKEN
, "Lexical error in number");
250 tk
= XCNEW (struct state_token_st
);
251 tk
->stok_kind
= STOK_INTEGER
;
252 tk
->stok_line
= state_line
;
253 tk
->stok_col
= curoff
- state_bol
;
254 tk
->stok_file
= state_path
;
255 tk
->stok_next
= NULL
;
256 tk
->stok_un
.stok_num
= n
;
260 /* Read an opening left parenthesis. */
263 curoff
= ftell (state_file
);
264 tk
= XCNEW (struct state_token_st
);
265 tk
->stok_kind
= STOK_LEFTPAR
;
266 tk
->stok_line
= state_line
;
267 tk
->stok_col
= curoff
- state_bol
;
268 tk
->stok_file
= state_path
;
269 tk
->stok_next
= NULL
;
273 /* Read an closing right parenthesis. */
276 curoff
= ftell (state_file
);
277 tk
= XCNEW (struct state_token_st
);
278 tk
->stok_kind
= STOK_RIGHTPAR
;
279 tk
->stok_line
= state_line
;
280 tk
->stok_col
= curoff
- state_bol
;
281 tk
->stok_file
= state_path
;
282 tk
->stok_next
= NULL
;
286 /* Read identifiers, using an obstack. */
287 else if (ISALPHA (c
) || c
== '_' || c
== '$' || c
== '!' || c
== '#')
289 struct obstack id_obstack
;
290 struct state_ident_st
*sid
= NULL
;
292 obstack_init (&id_obstack
);
293 curoff
= ftell (state_file
);
294 while (ISALNUM (c
) || c
== '_' || c
== '$' || c
== '!' || c
== '#')
296 obstack_1grow (&id_obstack
, c
);
297 c
= getc (state_file
);
302 ungetc (c
, state_file
);
303 obstack_1grow (&id_obstack
, (char) 0);
304 ids
= XOBFINISH (&id_obstack
, char *);
305 sid
= state_ident_by_name (ids
, INSERT
);
306 obstack_free (&id_obstack
, NULL
);
308 tk
= XCNEW (struct state_token_st
);
309 tk
->stok_kind
= STOK_NAME
;
310 tk
->stok_line
= state_line
;
311 tk
->stok_col
= curoff
- state_bol
;
312 tk
->stok_file
= state_path
;
313 tk
->stok_next
= NULL
;
314 tk
->stok_un
.stok_ident
= sid
;
318 /* Read a string, dealing with escape sequences a la C! */
323 struct obstack bstring_obstack
;
324 obstack_init (&bstring_obstack
);
325 curoff
= ftell (state_file
);
326 while ((c
= getc (state_file
)) != '"' && c
>= 0)
328 if (ISPRINT (c
) && c
!= '\\')
329 obstack_1grow (&bstring_obstack
, (char) c
);
330 else if (ISSPACE (c
) && c
!= '\n')
331 obstack_1grow (&bstring_obstack
, (char) c
);
334 c
= getc (state_file
);
338 obstack_1grow (&bstring_obstack
, '\a');
339 c
= getc (state_file
);
342 obstack_1grow (&bstring_obstack
, '\b');
343 c
= getc (state_file
);
346 obstack_1grow (&bstring_obstack
, '\t');
347 c
= getc (state_file
);
350 obstack_1grow (&bstring_obstack
, '\n');
351 c
= getc (state_file
);
354 obstack_1grow (&bstring_obstack
, '\v');
355 c
= getc (state_file
);
358 obstack_1grow (&bstring_obstack
, '\f');
359 c
= getc (state_file
);
362 obstack_1grow (&bstring_obstack
, '\r');
363 c
= getc (state_file
);
366 obstack_1grow (&bstring_obstack
, '\"');
367 c
= getc (state_file
);
370 obstack_1grow (&bstring_obstack
, '\\');
371 c
= getc (state_file
);
374 obstack_1grow (&bstring_obstack
, ' ');
375 c
= getc (state_file
);
380 if (fscanf (state_file
, "%02x", &cx
) > 0 && cx
> 0)
381 obstack_1grow (&bstring_obstack
, cx
);
385 "Lexical error in string hex escape");
386 c
= getc (state_file
);
392 "Lexical error - unknown string escape");
396 fatal_reading_state (NULL_STATE_TOKEN
, "Lexical error...");
399 fatal_reading_state (NULL_STATE_TOKEN
, "Unterminated string");
400 obstack_1grow (&bstring_obstack
, '\0');
401 cstr
= XOBFINISH (&bstring_obstack
, char *);
402 cslen
= strlen (cstr
);
403 tk
= (struct state_token_st
*)
404 xcalloc (sizeof (struct state_token_st
) + cslen
, 1);
405 tk
->stok_kind
= STOK_STRING
;
406 tk
->stok_line
= state_line
;
407 tk
->stok_col
= curoff
- state_bol
;
408 tk
->stok_file
= state_path
;
409 tk
->stok_next
= NULL
;
410 strcpy (tk
->stok_un
.stok_string
, cstr
);
411 obstack_free (&bstring_obstack
, NULL
);
415 /* Got an unexpected character. */
416 fatal_reading_state_printf
418 "Lexical error at offset %ld - bad character \\%03o = '%c'",
419 ftell (state_file
), c
, c
);
422 /* Used for lexical look-ahead. Retrieves the lexical token of rank
423 DEPTH, starting with 0 when reading the state file. Gives null on
425 static struct state_token_st
*
426 peek_state_token (int depth
)
428 int remdepth
= depth
;
429 struct state_token_st
**ptoken
= &state_token
;
430 struct state_token_st
*tok
= NULL
;
432 while (remdepth
>= 0)
436 *ptoken
= tok
= read_a_state_token ();
441 ptoken
= &((*ptoken
)->stok_next
);
448 /* Consume the next DEPTH tokens and free them. */
450 next_state_tokens (int depth
)
452 struct state_token_st
*n
;
456 if (state_token
!= NULL
)
458 n
= state_token
->stok_next
;
463 fatal_reading_state (NULL_STATE_TOKEN
, "Tokens stack empty");
469 /* Safely retrieve the lexical kind of a token. */
470 static inline enum state_token_en
471 state_token_kind (struct state_token_st
*p
)
479 /* Test if a token is a given name i.e. an identifier. */
481 state_token_is_name (struct state_token_st
*p
, const char *name
)
486 if (p
->stok_kind
!= STOK_NAME
)
489 return !strcmp (p
->stok_un
.stok_ident
->stid_name
, name
);
493 /* Following routines are useful for serializing datas.
495 * We want to serialize :
498 * - param_structs list
501 * So, we have one routine for each kind of data. The main writing
502 * routine is write_state. The main reading routine is
503 * read_state. Most writing routines write_state_FOO have a
504 * corresponding reading routine read_state_FOO. Reading is done in a
505 * recursive descending way, and any read error is fatal.
508 /* When reading the state, we need to remember the previously seen
509 types by their state_number, since GTY-ed types are usually
511 static htab_t state_seen_types
;
513 /* Return the length of a linked list made of pairs. */
514 static int pair_list_length (pair_p list
);
517 static void write_state_pair (pair_p
);
519 /* return the number of pairs written. Should match the length given
520 by pair_list_length. */
521 static int write_state_pair_list (pair_p list
);
523 /* Write a type. When a type is written, its state_number is updated,
524 to ensure that a "reference" to a seen type is written on next
526 static void write_state_type (type_p
);
528 /* Write a null-terminatel string using our Lispy lexical conventions,
529 similar to those of C or MELT. */
530 static void write_state_a_string (const char *s
);
532 /* Compute the length of a list of pairs, starting from the first
535 pair_list_length (pair_p list
)
539 for (l
= list
; l
; l
= l
->next
)
544 /* Write a file location. Files relative to $(srcdir) are quite
545 frequent and are handled specially. This ensures that two gengtype
546 state file-s produced by gengtype on the same GCC source tree are
547 very similar and can be reasonably compared with diff, even if the
548 two GCC source trees have different absolute paths. */
550 write_state_fileloc (struct fileloc
*floc
)
553 if (floc
!= NULL
&& floc
->line
> 0)
555 const char *srcrelpath
= NULL
;
556 gcc_assert (floc
->file
!= NULL
);
557 /* Most of the files are inside $(srcdir) so it is worth to
558 handle them specially. */
559 srcrelpath
= get_file_srcdir_relative_path (floc
->file
);
560 if (srcrelpath
!= NULL
)
562 fprintf (state_file
, "\n(!srcfileloc ");
563 write_state_a_string (srcrelpath
);
567 fprintf (state_file
, "\n(!fileloc ");
568 write_state_a_string (get_input_file_name (floc
->file
));
570 fprintf (state_file
, " %d", floc
->line
);
571 fprintf (state_file
, ")\n");
574 fprintf (state_file
, "nil ");
577 /* Write a list of fields. */
579 write_state_fields (pair_p fields
)
581 int nbfields
= pair_list_length (fields
);
583 fprintf (state_file
, "\n(!fields %d ", nbfields
);
584 nbpairs
= write_state_pair_list (fields
);
585 gcc_assert (nbpairs
== nbfields
);
586 fprintf (state_file
, ")\n");
589 /* Write a null-terminated string in our lexical convention, very
590 similar to the convention of C. */
592 write_state_a_string (const char *s
)
596 fputs (" \"", state_file
);
603 fputs ("\\a", state_file
);
606 fputs ("\\b", state_file
);
609 fputs ("\\t", state_file
);
612 fputs ("\\n", state_file
);
615 fputs ("\\v", state_file
);
618 fputs ("\\f", state_file
);
621 fputs ("\\r", state_file
);
624 fputs ("\\\"", state_file
);
627 fputs ("\\\\", state_file
);
631 putc (c
, state_file
);
633 fprintf (state_file
, "\\x%02x", (unsigned) c
);
636 fputs ("\"", state_file
);
639 /* Our option-s have three kinds, each with its writer. */
641 write_state_string_option (options_p current
)
643 fprintf (state_file
, "string ");
644 if (current
->info
.string
!= NULL
)
645 write_state_a_string (current
->info
.string
);
647 fprintf (state_file
, " nil ");
651 write_state_type_option (options_p current
)
653 fprintf (state_file
, "type ");
654 write_state_type (current
->info
.type
);
658 write_state_nested_option (options_p current
)
660 fprintf (state_file
, "nested ");
661 write_state_type (current
->info
.nested
->type
);
662 if (current
->info
.nested
->convert_from
!= NULL
)
663 write_state_a_string (current
->info
.nested
->convert_from
);
665 fprintf (state_file
, " nil ");
667 if (current
->info
.nested
->convert_to
!= NULL
)
668 write_state_a_string (current
->info
.nested
->convert_to
);
670 fprintf (state_file
, " nil ");
674 write_state_option (options_p current
)
676 fprintf (state_file
, "\n(!option ");
678 if (current
->name
!= NULL
)
679 fprintf (state_file
, "%s ", current
->name
);
681 fprintf (state_file
, "nil ");
683 switch (current
->kind
)
686 write_state_string_option (current
);
689 write_state_type_option (current
);
692 write_state_nested_option (current
);
695 fatal ("Option tag unknown");
698 fprintf (state_file
, ")\n");
703 /* Write a list of GTY options. */
705 write_state_options (options_p opt
)
711 fprintf (state_file
, "nil ");
715 fprintf (state_file
, "\n(!options ");
716 for (current
= opt
; current
!= NULL
; current
= current
->next
)
717 write_state_option (current
);
718 fprintf (state_file
, ")\n");
722 /* Write a bitmap representing a set of GCC front-end languages. */
724 write_state_lang_bitmap (lang_bitmap bitmap
)
726 fprintf (state_file
, "%d ", (int) bitmap
);
729 /* Write version information. */
731 write_state_version (const char *version
)
733 fprintf (state_file
, "\n(!version ");
734 write_state_a_string (version
);
735 fprintf (state_file
, ")\n");
738 /* Common routine to write the common content of all types. */
739 static void write_state_common_type_content (type_p current
);
741 /* Write a scalar type. We have only two of these. */
743 write_state_scalar_type (type_p current
)
745 if (current
== &scalar_nonchar
)
746 fprintf (state_file
, "scalar_nonchar ");
747 else if (current
== &scalar_char
)
748 fprintf (state_file
, "scalar_char ");
750 fatal ("Unexpected type in write_state_scalar_type");
752 write_state_common_type_content (current
);
755 /* Write the string type. There is only one such thing! */
757 write_state_string_type (type_p current
)
759 if (current
== &string_type
)
761 fprintf (state_file
, "string ");
762 write_state_common_type_content (current
);
765 fatal ("Unexpected type in write_state_string_type");
769 /* Common code to write structure like types. */
771 write_state_struct_union_type (type_p current
, const char *kindstr
)
773 DBGPRINTF ("%s type @ %p #%d '%s'", kindstr
, (void *) current
,
774 current
->state_number
, current
->u
.s
.tag
);
775 fprintf (state_file
, "%s ", kindstr
);
776 write_state_common_type_content (current
);
777 if (current
->u
.s
.tag
!= NULL
)
778 write_state_a_string (current
->u
.s
.tag
);
780 fprintf (state_file
, "nil");
782 write_state_fileloc (type_lineloc (current
));
783 write_state_fields (current
->u
.s
.fields
);
784 write_state_options (current
->u
.s
.opt
);
785 write_state_lang_bitmap (current
->u
.s
.bitmap
);
789 /* Write a GTY struct type. */
791 write_state_struct_type (type_p current
)
793 write_state_struct_union_type (current
, "struct");
794 write_state_type (current
->u
.s
.lang_struct
);
797 /* write a GTY union type. */
799 write_state_union_type (type_p current
)
801 write_state_struct_union_type (current
, "union");
802 write_state_type (current
->u
.s
.lang_struct
);
805 /* Write a lang_struct type. This is tricky and was painful to debug,
806 we deal with the next field specifically within their lang_struct
807 subfield, which points to a linked list of homonumous types.
808 Change this function with extreme care, see also
809 read_state_lang_struct_type. */
811 write_state_lang_struct_type (type_p current
)
815 const char *homoname
= 0;
816 write_state_struct_union_type (current
, "lang_struct");
817 /* lang_struct-ures are particularily tricky, since their
818 u.s.lang_struct field gives a list of homonymous struct-s or
820 DBGPRINTF ("lang_struct @ %p #%d", (void *) current
, current
->state_number
);
821 for (hty
= current
->u
.s
.lang_struct
; hty
!= NULL
; hty
= hty
->next
)
824 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype
,
825 (void *) hty
, hty
->state_number
, hty
->u
.s
.tag
);
826 /* Every member of the homonymous list should have the same tag. */
827 gcc_assert (UNION_OR_STRUCT_P (hty
));
828 gcc_assert (hty
->u
.s
.lang_struct
== current
);
830 homoname
= hty
->u
.s
.tag
;
831 gcc_assert (strcmp (homoname
, hty
->u
.s
.tag
) == 0);
833 fprintf (state_file
, "(!homotypes %d\n", nbhomontype
);
834 for (hty
= current
->u
.s
.lang_struct
; hty
!= NULL
; hty
= hty
->next
)
835 write_state_type (hty
);
836 fprintf (state_file
, ")\n");
839 /* Write a parametrized structure GTY type. */
841 write_state_param_struct_type (type_p current
)
845 fprintf (state_file
, "param_struct ");
846 write_state_common_type_content (current
);
847 write_state_type (current
->u
.param_struct
.stru
);
848 for (i
= 0; i
< NUM_PARAM
; i
++)
850 if (current
->u
.param_struct
.param
[i
] != NULL
)
851 write_state_type (current
->u
.param_struct
.param
[i
]);
853 fprintf (state_file
, "nil ");
855 write_state_fileloc (¤t
->u
.param_struct
.line
);
858 /* Write a pointer type. */
860 write_state_pointer_type (type_p current
)
862 fprintf (state_file
, "pointer ");
863 write_state_common_type_content (current
);
864 write_state_type (current
->u
.p
);
867 /* Write an array type. */
869 write_state_array_type (type_p current
)
871 fprintf (state_file
, "array ");
872 write_state_common_type_content (current
);
873 if (current
->u
.a
.len
!= NULL
)
874 write_state_a_string (current
->u
.a
.len
);
876 fprintf (state_file
, " nil");
878 fprintf (state_file
, " ");
879 write_state_type (current
->u
.a
.p
);
882 /* Write the gc_used information. */
884 write_state_gc_used (enum gc_used_enum gus
)
889 fprintf (state_file
, " gc_unused");
892 fprintf (state_file
, " gc_used");
894 case GC_MAYBE_POINTED_TO
:
895 fprintf (state_file
, " gc_maybe_pointed_to");
898 fprintf (state_file
, " gc_pointed_to");
905 /* Utility routine to write the common content of all types. Notice
906 that the next field is *not* written on purpose. */
908 write_state_common_type_content (type_p current
)
910 fprintf (state_file
, "%d ", current
->state_number
);
911 /* We do not write the next type, because list of types are
912 explicitly written. However, lang_struct are special in that
913 respect. See function write_state_lang_struct_type for more. */
914 write_state_type (current
->pointer_to
);
915 write_state_gc_used (current
->gc_used
);
919 /* The important and recursive routine writing GTY types as understood
920 by gengtype. Types which have a positive state_number have already
921 been seen and written. */
923 write_state_type (type_p current
)
927 fprintf (state_file
, "nil ");
931 fprintf (state_file
, "\n(!type ");
933 if (current
->state_number
> 0)
934 fprintf (state_file
, "already_seen %d", current
->state_number
);
937 state_written_type_count
++;
938 DBGPRINTF ("writing type #%d @%p old number %d", state_written_type_count
,
939 (void *) current
, current
->state_number
);
940 current
->state_number
= state_written_type_count
;
941 switch (current
->kind
)
944 write_state_struct_type (current
);
947 write_state_union_type (current
);
950 write_state_pointer_type (current
);
953 write_state_array_type (current
);
955 case TYPE_LANG_STRUCT
:
956 write_state_lang_struct_type (current
);
958 case TYPE_PARAM_STRUCT
:
959 write_state_param_struct_type (current
);
962 write_state_scalar_type (current
);
965 write_state_string_type (current
);
969 fatal ("Unexpected type...");
973 fprintf (state_file
, ")\n");
979 write_state_pair (pair_p current
)
983 fprintf (state_file
, "nil)");
987 fprintf (state_file
, "\n(!pair ");
989 if (current
->name
!= NULL
)
990 write_state_a_string (current
->name
);
992 write_state_a_string ("nil");
994 write_state_type (current
->type
);
995 write_state_fileloc (&(current
->line
));
996 write_state_options (current
->opt
);
998 fprintf (state_file
, ")");
1001 /* Write a pair list and return the number of pairs written. */
1003 write_state_pair_list (pair_p list
)
1008 for (current
= list
; current
!= NULL
; current
= current
->next
)
1010 write_state_pair (current
);
1017 /* When writing imported linked lists, like typedefs, structures,
1018 param_structs, ... we count their length first and write it. These
1019 eases the reading, and enables an extra verification on the number
1020 of actually read items. */
1022 /* Write our typedefs. */
1024 write_state_typedefs (void)
1026 int nbtypedefs
= pair_list_length (typedefs
);
1028 fprintf (state_file
, "\n(!typedefs %d\n", nbtypedefs
);
1029 nbpairs
= write_state_pair_list (typedefs
);
1030 gcc_assert (nbpairs
== nbtypedefs
);
1031 fprintf (state_file
, ")\n");
1032 if (verbosity_level
>= 2)
1033 printf ("%s wrote %d typedefs\n", progname
, nbtypedefs
);
1036 /* Write our structures. */
1038 write_state_structures (void)
1043 for (current
= structures
; current
!= NULL
; current
= current
->next
)
1046 fprintf (state_file
, "\n(!structures %d\n", nbstruct
);
1048 for (current
= structures
; current
!= NULL
; current
= current
->next
)
1049 write_state_type (current
);
1051 fprintf (state_file
, ")\n");
1052 if (verbosity_level
>= 2)
1053 printf ("%s wrote %d structures in state\n", progname
, nbstruct
);
1056 /* Write our param_struct-s. */
1058 write_state_param_structs (void)
1060 int nbparamstruct
= 0;
1063 for (current
= param_structs
; current
!= NULL
; current
= current
->next
)
1066 fprintf (state_file
, "\n(!param_structs %d\n", nbparamstruct
);
1068 for (current
= param_structs
; current
!= NULL
; current
= current
->next
)
1069 write_state_type (current
);
1071 fprintf (state_file
, ")\n");
1074 /* Write our variables. */
1076 write_state_variables (void)
1078 int nbvars
= pair_list_length (variables
);
1080 fprintf (state_file
, "\n(!variables %d\n", nbvars
);
1081 nbpairs
= write_state_pair_list (variables
);
1082 gcc_assert (nbpairs
== nbvars
);
1083 fprintf (state_file
, ")\n");
1084 if (verbosity_level
>= 2)
1085 printf ("%s wrote %d variables.\n", progname
, nbvars
);
1088 /* Write the source directory. File locations within the source
1089 directory have been written specifically. */
1091 write_state_srcdir (void)
1093 fprintf (state_file
, "\n(!srcdir ");
1094 write_state_a_string (srcdir
);
1095 fprintf (state_file
, ")\n");
1098 /* Count and write the list of our files. */
1100 write_state_files_list (void)
1103 /* Write the list of files with their lang_bitmap. */
1104 fprintf (state_file
, "\n(!fileslist %d\n", (int) num_gt_files
);
1105 for (i
= 0; i
< (int) num_gt_files
; i
++)
1107 const char *cursrcrelpath
= NULL
;
1108 const input_file
*curfil
= gt_files
[i
];
1109 /* Most of the files are inside $(srcdir) so it is worth to
1110 handle them specially. */
1111 cursrcrelpath
= get_file_srcdir_relative_path (curfil
);
1114 fprintf (state_file
, "(!srcfile %d ", get_lang_bitmap (curfil
));
1115 write_state_a_string (cursrcrelpath
);
1119 fprintf (state_file
, "(!file %d ", get_lang_bitmap (curfil
));
1120 write_state_a_string (get_input_file_name (curfil
));
1122 fprintf (state_file
, ")\n");
1124 fprintf (state_file
, ")\n");
1127 /* Write the list of GCC front-end languages. */
1129 write_state_languages (void)
1132 fprintf (state_file
, "\n(!languages %d", (int) num_lang_dirs
);
1133 for (i
= 0; i
< (int) num_lang_dirs
; i
++)
1135 /* Languages names are identifiers, we expect only letters or
1136 underscores or digits in them. In particular, C++ is not a
1137 valid language name, but cp is valid. */
1138 fprintf (state_file
, " %s", lang_dir_names
[i
]);
1140 fprintf (state_file
, ")\n");
1143 /* Write the trailer. */
1145 write_state_trailer (void)
1147 /* This test should probably catch IO errors like disk full... */
1148 if (fputs ("\n(!endfile)\n", state_file
) == EOF
)
1149 fatal ("failed to write state trailer [%s]", xstrerror (errno
));
1152 /* The write_state routine is the only writing routine called by main
1153 in gengtype.c. To avoid messing the state if gengtype is
1154 interrupted or aborted, we write a temporary file and rename it
1155 after having written it in totality. */
1157 write_state (const char *state_path
)
1161 char *temp_state_path
= NULL
;
1162 char tempsuffix
[40];
1165 /* We write a unique temporary file which is renamed when complete
1166 * only. So even if gengtype is interrupted, the written state file
1167 * won't be partially written, since the temporary file is not yet
1168 * renamed in that case. */
1169 memset (tempsuffix
, 0, sizeof (tempsuffix
));
1170 snprintf (tempsuffix
, sizeof (tempsuffix
) - 1, "-%ld-%d.tmp", (long) now
,
1172 temp_state_path
= concat (state_path
, tempsuffix
, NULL
);
1173 state_file
= fopen (temp_state_path
, "w");
1174 if (state_file
== NULL
)
1175 fatal ("Failed to open file %s for writing state: %s",
1176 temp_state_path
, xstrerror (errno
));
1177 if (verbosity_level
>= 3)
1178 printf ("%s writing state file %s temporarily in %s\n",
1179 progname
, state_path
, temp_state_path
);
1180 /* This is the first line of the state. Perhaps the file utility
1181 could know about that, so don't change it often. */
1182 fprintf (state_file
, ";;;;@@@@ GCC gengtype state\n");
1183 /* Output a few comments for humans. */
1184 fprintf (state_file
,
1185 ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
1186 fprintf (state_file
,
1187 ";;; The format of this file is tied to a particular version of GCC.\n");
1188 fprintf (state_file
,
1189 ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1190 fprintf (state_file
,
1191 ";;; This file should be parsed by the same %s which wrote it.\n",
1193 fprintf (state_file
, ";;; file %s generated on %s\n", state_path
,
1195 /* The first non-comment significant line gives the version string. */
1196 write_state_version (version_string
);
1197 write_state_srcdir ();
1198 write_state_languages ();
1199 write_state_files_list ();
1200 write_state_structures ();
1201 write_state_typedefs ();
1202 write_state_param_structs ();
1203 write_state_variables ();
1204 write_state_trailer ();
1205 statelen
= ftell (state_file
);
1206 if (ferror (state_file
))
1207 fatal ("output error when writing state file %s [%s]",
1208 temp_state_path
, xstrerror (errno
));
1209 if (fclose (state_file
))
1210 fatal ("failed to close state file %s [%s]",
1211 temp_state_path
, xstrerror (errno
));
1212 if (rename (temp_state_path
, state_path
))
1213 fatal ("failed to rename %s to state file %s [%s]", temp_state_path
,
1214 state_path
, xstrerror (errno
));
1215 free (temp_state_path
);
1217 if (verbosity_level
>= 1)
1218 printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1219 progname
, state_path
, statelen
, state_written_type_count
);
1223 /** End of writing routines! The corresponding reading routines follow. **/
1227 /* Forward declarations, since some read_state_* functions are
1229 static void read_state_fileloc (struct fileloc
*line
);
1230 static void read_state_options (options_p
*opt
);
1231 static void read_state_type (type_p
*current
);
1232 static void read_state_pair (pair_p
*pair
);
1233 /* Return the number of pairs actually read. */
1234 static int read_state_pair_list (pair_p
*list
);
1235 static void read_state_fields (pair_p
*fields
);
1236 static void read_state_common_type_content (type_p current
);
1241 /* Record into the state_seen_types hash-table a type which we are
1242 reading, to enable recursive or circular references to it. */
1244 record_type (type_p type
)
1248 slot
= htab_find_slot (state_seen_types
, type
, INSERT
);
1254 /* Read an already seen type. */
1256 read_state_already_seen_type (type_p
*type
)
1258 struct state_token_st
*t0
= peek_state_token (0);
1260 if (state_token_kind (t0
) == STOK_INTEGER
)
1263 struct type loctype
= { TYPE_SCALAR
, 0, 0, 0, GC_UNUSED
, {0} };
1265 loctype
.state_number
= t0
->stok_un
.stok_num
;
1266 slot
= htab_find_slot (state_seen_types
, &loctype
, NO_INSERT
);
1269 fatal_reading_state (t0
, "Unknown type");
1272 next_state_tokens (1);
1273 *type
= (type_p
) *slot
;
1277 fatal_reading_state (t0
, "Bad seen type");
1282 /* Read the scalar_nonchar type. */
1284 read_state_scalar_nonchar_type (type_p
*type
)
1286 *type
= &scalar_nonchar
;
1287 read_state_common_type_content (*type
);
1291 /* Read the scalar_char type. */
1293 read_state_scalar_char_type (type_p
*type
)
1295 *type
= &scalar_char
;
1296 read_state_common_type_content (*type
);
1300 /* Read the string_type. */
1302 read_state_string_type (type_p
*type
)
1304 *type
= &string_type
;
1305 read_state_common_type_content (*type
);
1309 /* Read a lang_bitmap representing a set of GCC front-end languages. */
1311 read_state_lang_bitmap (lang_bitmap
*bitmap
)
1313 struct state_token_st
*t
;
1315 t
= peek_state_token (0);
1316 if (state_token_kind (t
) == STOK_INTEGER
)
1318 *bitmap
= t
->stok_un
.stok_num
;
1319 next_state_tokens (1);
1323 fatal_reading_state (t
, "Bad syntax for bitmap");
1328 /* Read a GTY-ed struct type. */
1330 read_state_struct_type (type_p type
)
1332 struct state_token_st
*t0
;
1334 type
->kind
= TYPE_STRUCT
;
1335 read_state_common_type_content (type
);
1336 t0
= peek_state_token (0);
1337 if (state_token_kind (t0
) == STOK_STRING
)
1339 if (state_token_is_name (t0
, "nil"))
1341 type
->u
.s
.tag
= NULL
;
1342 DBGPRINTF ("read anonymous struct type @%p #%d",
1343 (void *) type
, type
->state_number
);
1347 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1348 DBGPRINTF ("read struct type @%p #%d '%s'",
1349 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1352 next_state_tokens (1);
1353 read_state_fileloc (&(type
->u
.s
.line
));
1354 read_state_fields (&(type
->u
.s
.fields
));
1355 read_state_options (&(type
->u
.s
.opt
));
1356 read_state_lang_bitmap (&(type
->u
.s
.bitmap
));
1357 read_state_type (&(type
->u
.s
.lang_struct
));
1361 fatal_reading_state (t0
, "Bad tag in struct type");
1366 /* Read a GTY-ed union type. */
1368 read_state_union_type (type_p type
)
1370 struct state_token_st
*t0
;
1372 type
->kind
= TYPE_UNION
;
1373 read_state_common_type_content (type
);
1374 t0
= peek_state_token (0);
1375 if (state_token_kind (t0
) == STOK_STRING
)
1377 if (state_token_is_name (t0
, "nil"))
1379 type
->u
.s
.tag
= NULL
;
1380 DBGPRINTF ("read anonymous union type @%p #%d",
1381 (void *) type
, type
->state_number
);
1385 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1386 DBGPRINTF ("read union type @%p #%d '%s'",
1387 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1389 next_state_tokens (1);
1390 read_state_fileloc (&(type
->u
.s
.line
));
1391 read_state_fields (&(type
->u
.s
.fields
));
1392 read_state_options (&(type
->u
.s
.opt
));
1393 read_state_lang_bitmap (&(type
->u
.s
.bitmap
));
1394 read_state_type (&(type
->u
.s
.lang_struct
));
1397 fatal_reading_state (t0
, "Bad tag in union type");
1401 /* Read a GTY-ed pointer type. */
1403 read_state_pointer_type (type_p type
)
1405 type
->kind
= TYPE_POINTER
;
1406 read_state_common_type_content (type
);
1407 DBGPRINTF ("read pointer type @%p #%d", (void *) type
, type
->state_number
);
1408 read_state_type (&(type
->u
.p
));
1412 /* Read a GTY-ed array type. */
1414 read_state_array_type (type_p type
)
1416 struct state_token_st
*t0
;
1418 type
->kind
= TYPE_ARRAY
;
1419 read_state_common_type_content (type
);
1420 t0
= peek_state_token (0);
1421 if (state_token_kind (t0
) == STOK_STRING
)
1423 type
->u
.a
.len
= xstrdup (t0
->stok_un
.stok_string
);
1424 DBGPRINTF ("read array type @%p #%d length '%s'",
1425 (void *) type
, type
->state_number
, type
->u
.a
.len
);
1426 next_state_tokens (1);
1429 else if (state_token_is_name (t0
, "nil"))
1431 type
->u
.a
.len
= NULL
;
1432 DBGPRINTF ("read array type @%p #%d without length",
1433 (void *) type
, type
->state_number
);
1434 next_state_tokens (1);
1438 fatal_reading_state (t0
, "Bad array name type");
1439 read_state_type (&(type
->u
.a
.p
));
1444 /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
1445 front-end languages. This is a tricky function and it was painful
1446 to debug. Change it with extreme care. See also
1447 write_state_lang_struct_type. */
1449 read_state_lang_struct_type (type_p type
)
1451 struct state_token_st
*t0
= NULL
;
1452 struct state_token_st
*t1
= NULL
;
1453 struct state_token_st
*t2
= NULL
;
1455 type
->kind
= TYPE_LANG_STRUCT
;
1456 read_state_common_type_content (type
);
1457 t0
= peek_state_token (0);
1458 if (state_token_kind (t0
) == STOK_STRING
)
1460 if (state_token_is_name (t0
, "nil"))
1462 DBGPRINTF ("read anonymous lang_struct type @%p #%d",
1463 (void *) type
, type
->state_number
);
1464 type
->u
.s
.tag
= NULL
;
1468 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1469 DBGPRINTF ("read lang_struct type @%p #%d '%s'",
1470 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1472 next_state_tokens (1);
1475 fatal_reading_state (t0
, "Bad tag in lang struct type");
1476 read_state_fileloc (&(type
->u
.s
.line
));
1477 read_state_fields (&(type
->u
.s
.fields
));
1478 read_state_options (&(type
->u
.s
.opt
));
1479 read_state_lang_bitmap (&(type
->u
.s
.bitmap
));
1480 /* Within lang_struct-ures, the lang_struct field is a linked list
1481 of homonymous types! */
1482 t0
= peek_state_token (0);
1483 t1
= peek_state_token (1);
1484 t2
= peek_state_token (2);
1485 /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
1486 if (state_token_kind (t0
) == STOK_LEFTPAR
1487 && state_token_is_name (t1
, "!homotypes")
1488 && state_token_kind (t2
) == STOK_INTEGER
)
1490 type_p
*prevty
= &type
->u
.s
.lang_struct
;
1491 int nbhomotype
= t2
->stok_un
.stok_num
;
1493 t0
= t1
= t2
= NULL
;
1494 next_state_tokens (3);
1495 for (i
= 0; i
< nbhomotype
; i
++)
1497 read_state_type (prevty
);
1498 t0
= peek_state_token (0);
1500 prevty
= &(*prevty
)->next
;
1502 fatal_reading_state (t0
,
1503 "expecting type in homotype list for lang_struct");
1505 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1506 fatal_reading_state (t0
,
1507 "expecting ) in homotype list for lang_struct");
1508 next_state_tokens (1);
1511 fatal_reading_state (t0
, "expecting !homotypes for lang_struct");
1515 /* Read a param_struct type for GTY parametrized structures. */
1517 read_state_param_struct_type (type_p type
)
1520 struct state_token_st
*t0
;
1522 type
->kind
= TYPE_PARAM_STRUCT
;
1523 read_state_common_type_content (type
);
1524 DBGPRINTF ("read param_struct type @%p #%d",
1525 (void *) type
, type
->state_number
);
1526 read_state_type (&(type
->u
.param_struct
.stru
));
1528 for (i
= 0; i
< NUM_PARAM
; i
++)
1530 t0
= peek_state_token (0);
1531 if (state_token_is_name (t0
, "nil"))
1533 type
->u
.param_struct
.param
[i
] = NULL
;
1534 next_state_tokens (1);
1537 read_state_type (&(type
->u
.param_struct
.param
[i
]));
1539 read_state_fileloc (&(type
->u
.param_struct
.line
));
1543 /* Read the gc used information. */
1545 read_state_gc_used (enum gc_used_enum
*pgus
)
1547 struct state_token_st
*t0
= peek_state_token (0);
1548 if (state_token_is_name (t0
, "gc_unused"))
1550 else if (state_token_is_name (t0
, "gc_used"))
1552 else if (state_token_is_name (t0
, "gc_maybe_pointed_to"))
1553 *pgus
= GC_MAYBE_POINTED_TO
;
1554 else if (state_token_is_name (t0
, "gc_pointed_to"))
1555 *pgus
= GC_POINTED_TO
;
1557 fatal_reading_state (t0
, "invalid gc_used information");
1558 next_state_tokens (1);
1562 /* Utility function to read the common content of types. */
1564 read_state_common_type_content (type_p current
)
1566 struct state_token_st
*t0
= peek_state_token (0);
1568 if (state_token_kind (t0
) == STOK_INTEGER
)
1570 current
->state_number
= t0
->stok_un
.stok_num
;
1571 next_state_tokens (1);
1572 record_type (current
);
1575 fatal_reading_state_printf (t0
,
1576 "Expected integer for state_number line %d",
1578 /* We don't read the next field of the type. */
1579 read_state_type (¤t
->pointer_to
);
1580 read_state_gc_used (¤t
->gc_used
);
1584 /* Read a GTY-ed type. */
1586 read_state_type (type_p
*current
)
1588 struct state_token_st
*t0
= peek_state_token (0);
1589 struct state_token_st
*t1
= peek_state_token (1);
1591 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1592 state_token_is_name (t1
, "!type"))
1594 next_state_tokens (2);
1595 t0
= peek_state_token (0);
1596 if (state_token_is_name (t0
, "already_seen"))
1598 next_state_tokens (1);
1599 read_state_already_seen_type (current
);
1603 t0
= peek_state_token (0);
1605 if (state_token_is_name (t0
, "scalar_nonchar"))
1607 next_state_tokens (1);
1608 read_state_scalar_nonchar_type (current
);
1610 else if (state_token_is_name (t0
, "scalar_char"))
1612 next_state_tokens (1);
1613 read_state_scalar_char_type (current
);
1615 else if (state_token_is_name (t0
, "string"))
1617 next_state_tokens (1);
1618 read_state_string_type (current
);
1620 else if (state_token_is_name (t0
, "struct"))
1622 *current
= XCNEW (struct type
);
1623 next_state_tokens (1);
1624 read_state_struct_type (*current
);
1626 else if (state_token_is_name (t0
, "union"))
1628 *current
= XCNEW (struct type
);
1629 next_state_tokens (1);
1630 read_state_union_type (*current
);
1632 else if (state_token_is_name (t0
, "lang_struct"))
1634 *current
= XCNEW (struct type
);
1635 next_state_tokens (1);
1636 read_state_lang_struct_type (*current
);
1638 else if (state_token_is_name (t0
, "param_struct"))
1640 *current
= XCNEW (struct type
);
1641 next_state_tokens (1);
1642 read_state_param_struct_type (*current
);
1644 else if (state_token_is_name (t0
, "pointer"))
1646 *current
= XCNEW (struct type
);
1647 next_state_tokens (1);
1648 read_state_pointer_type (*current
);
1650 else if (state_token_is_name (t0
, "array"))
1652 *current
= XCNEW (struct type
);
1653 next_state_tokens (1);
1654 read_state_array_type (*current
);
1657 fatal_reading_state (t0
, "bad type in (!type");
1659 t0
= peek_state_token (0);
1660 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1661 fatal_reading_state (t0
, "missing ) in type");
1662 next_state_tokens (1);
1664 else if (state_token_is_name (t0
, "nil"))
1666 next_state_tokens (1);
1670 fatal_reading_state (t0
, "bad type syntax");
1674 /* Read a file location. Files within the source directory are dealt
1675 with specifically. */
1677 read_state_fileloc (struct fileloc
*floc
)
1679 bool issrcfile
= false;
1680 struct state_token_st
*t0
= peek_state_token (0);
1681 struct state_token_st
*t1
= peek_state_token (1);
1683 gcc_assert (floc
!= NULL
);
1684 gcc_assert (srcdir
!= NULL
);
1686 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1687 (state_token_is_name (t1
, "!fileloc")
1688 || (issrcfile
= state_token_is_name (t1
, "!srcfileloc"))))
1690 next_state_tokens (2);
1691 t0
= peek_state_token (0);
1692 t1
= peek_state_token (1);
1693 if (state_token_kind (t0
) == STOK_STRING
&&
1694 state_token_kind (t1
) == STOK_INTEGER
)
1696 char *path
= t0
->stok_un
.stok_string
;
1699 static const char dirsepstr
[2] = { DIR_SEPARATOR
, (char) 0 };
1700 char *fullpath
= concat (srcdir
, dirsepstr
, path
, NULL
);
1701 floc
->file
= input_file_by_name (fullpath
);
1705 floc
->file
= input_file_by_name (path
);
1706 floc
->line
= t1
->stok_un
.stok_num
;
1707 next_state_tokens (2);
1710 fatal_reading_state (t0
,
1711 "Bad fileloc syntax, expected path string and line");
1712 t0
= peek_state_token (0);
1713 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1714 fatal_reading_state (t0
, "Bad fileloc syntax, expected )");
1715 next_state_tokens (1);
1717 else if (state_token_is_name (t0
, "nil"))
1719 next_state_tokens (1);
1724 fatal_reading_state (t0
, "Bad fileloc syntax");
1728 /* Read the fields of a GTY-ed type. */
1730 read_state_fields (pair_p
*fields
)
1733 struct state_token_st
*t0
= peek_state_token (0);
1734 struct state_token_st
*t1
= peek_state_token (1);
1735 struct state_token_st
*t2
= peek_state_token (2);
1737 if (state_token_kind (t0
) == STOK_LEFTPAR
1738 && state_token_is_name (t1
, "!fields")
1739 && state_token_kind (t2
) == STOK_INTEGER
)
1741 int nbfields
= t2
->stok_un
.stok_num
;
1743 next_state_tokens (3);
1744 nbpairs
= read_state_pair_list (&tmp
);
1745 t0
= peek_state_token (0);
1746 if (nbpairs
!= nbfields
)
1747 fatal_reading_state_printf
1749 "Mismatched fields number, expected %d got %d", nbpairs
, nbfields
);
1750 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
1751 next_state_tokens (1);
1753 fatal_reading_state (t0
, "Bad fields expecting )");
1760 /* Read a string option. */
1762 read_state_string_option (options_p opt
)
1764 struct state_token_st
*t0
= peek_state_token (0);
1765 opt
->kind
= OPTION_STRING
;
1766 if (state_token_kind (t0
) == STOK_STRING
)
1768 opt
->info
.string
= xstrdup (t0
->stok_un
.stok_string
);
1769 next_state_tokens (1);
1771 else if (state_token_is_name (t0
, "nil"))
1773 opt
->info
.string
= NULL
;
1774 next_state_tokens (1);
1777 fatal_reading_state (t0
, "Missing name in string option");
1781 /* Read a type option. */
1783 read_state_type_option (options_p opt
)
1785 opt
->kind
= OPTION_TYPE
;
1786 read_state_type (&(opt
->info
.type
));
1790 /* Read a nested option. */
1792 read_state_nested_option (options_p opt
)
1794 struct state_token_st
*t0
;
1796 opt
->info
.nested
= XCNEW (struct nested_ptr_data
);
1797 opt
->kind
= OPTION_NESTED
;
1798 read_state_type (&(opt
->info
.nested
->type
));
1799 t0
= peek_state_token (0);
1800 if (state_token_kind (t0
) == STOK_STRING
)
1802 opt
->info
.nested
->convert_from
= xstrdup (t0
->stok_un
.stok_string
);
1803 next_state_tokens (1);
1805 else if (state_token_is_name (t0
, "nil"))
1807 opt
->info
.nested
->convert_from
= NULL
;
1808 next_state_tokens (1);
1811 fatal_reading_state (t0
, "Bad nested convert_from option");
1813 t0
= peek_state_token (0);
1814 if (state_token_kind (t0
) == STOK_STRING
)
1816 opt
->info
.nested
->convert_to
= xstrdup (t0
->stok_un
.stok_string
);
1817 next_state_tokens (1);
1819 else if (state_token_is_name (t0
, "nil"))
1821 opt
->info
.nested
->convert_to
= NULL
;
1822 next_state_tokens (1);
1825 fatal_reading_state (t0
, "Bad nested convert_from option");
1829 /* Read an GTY option. */
1831 read_state_option (options_p
*opt
)
1833 struct state_token_st
*t0
= peek_state_token (0);
1834 struct state_token_st
*t1
= peek_state_token (1);
1836 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1837 state_token_is_name (t1
, "!option"))
1839 next_state_tokens (2);
1840 t0
= peek_state_token (0);
1841 if (state_token_kind (t0
) == STOK_NAME
)
1843 *opt
= XCNEW (struct options
);
1844 if (state_token_is_name (t0
, "nil"))
1845 (*opt
)->name
= NULL
;
1847 (*opt
)->name
= t0
->stok_un
.stok_ident
->stid_name
;
1848 next_state_tokens (1);
1849 t0
= peek_state_token (0);
1850 if (state_token_kind (t0
) == STOK_NAME
)
1852 if (state_token_is_name (t0
, "string"))
1854 next_state_tokens (1);
1855 read_state_string_option (*opt
);
1857 else if (state_token_is_name (t0
, "type"))
1859 next_state_tokens (1);
1860 read_state_type_option (*opt
);
1862 else if (state_token_is_name (t0
, "nested"))
1864 next_state_tokens (1);
1865 read_state_nested_option (*opt
);
1868 fatal_reading_state (t0
, "Bad option type");
1869 t0
= peek_state_token (0);
1870 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1871 fatal_reading_state (t0
, "Bad syntax in option, expecting )");
1873 next_state_tokens (1);
1876 fatal_reading_state (t0
, "Missing option type");
1879 fatal_reading_state (t0
, "Bad name for option");
1882 fatal_reading_state (t0
, "Bad option, waiting for )");
1885 /* Read a list of options. */
1887 read_state_options (options_p
*opt
)
1889 options_p head
= NULL
;
1890 options_p previous
= NULL
;
1891 options_p current_option
= NULL
;
1892 struct state_token_st
*t0
= peek_state_token (0);
1893 struct state_token_st
*t1
= peek_state_token (1);
1895 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1896 state_token_is_name (t1
, "!options"))
1898 next_state_tokens (2);
1899 t0
= peek_state_token (0);
1900 while (state_token_kind (t0
) != STOK_RIGHTPAR
)
1902 read_state_option (¤t_option
);
1905 head
= current_option
;
1910 previous
->next
= current_option
;
1911 previous
= current_option
;
1913 t0
= peek_state_token (0);
1915 next_state_tokens (1);
1917 else if (state_token_is_name (t0
, "nil"))
1919 next_state_tokens (1);
1922 fatal_reading_state (t0
, "Bad options syntax");
1928 /* Read a version, and check against the version of the gengtype. */
1930 read_state_version (const char *version_string
)
1932 struct state_token_st
*t0
= peek_state_token (0);
1933 struct state_token_st
*t1
= peek_state_token (1);
1935 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1936 state_token_is_name (t1
, "!version"))
1938 next_state_tokens (2);
1939 t0
= peek_state_token (0);
1940 t1
= peek_state_token (1);
1941 if (state_token_kind (t0
) == STOK_STRING
&&
1942 state_token_kind (t1
) == STOK_RIGHTPAR
)
1944 /* Check that the read version string is the same as current
1946 if (strcmp (version_string
, t0
->stok_un
.stok_string
))
1947 fatal_reading_state_printf (t0
,
1948 "version string mismatch; expecting %s but got %s",
1950 t0
->stok_un
.stok_string
);
1951 next_state_tokens (2);
1954 fatal_reading_state (t0
, "Missing version or right parenthesis");
1957 fatal_reading_state (t0
, "Bad version syntax");
1963 read_state_pair (pair_p
*current
)
1965 struct state_token_st
*t0
= peek_state_token (0);
1966 struct state_token_st
*t1
= peek_state_token (1);
1967 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1968 state_token_is_name (t1
, "!pair"))
1970 *current
= XCNEW (struct pair
);
1971 next_state_tokens (2);
1972 t0
= peek_state_token (0);
1973 if (state_token_kind (t0
) == STOK_STRING
)
1975 if (strcmp (t0
->stok_un
.stok_string
, "nil") == 0)
1977 (*current
)->name
= NULL
;
1981 (*current
)->name
= xstrdup (t0
->stok_un
.stok_string
);
1983 next_state_tokens (1);
1984 read_state_type (&((*current
)->type
));
1985 read_state_fileloc (&((*current
)->line
));
1986 read_state_options (&((*current
)->opt
));;
1987 t0
= peek_state_token (0);
1988 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
1990 next_state_tokens (1);
1994 fatal_reading_state (t0
, "Bad syntax for pair, )");
1999 fatal_reading_state (t0
, "Bad name for pair");
2002 else if (state_token_kind (t0
) == STOK_NAME
&&
2003 state_token_is_name (t0
, "nil"))
2005 next_state_tokens (1);
2009 fatal_reading_state_printf (t0
, "Bad syntax for pair, (!pair %d",
2010 state_token
->stok_kind
);
2014 /* Return the number of pairs actually read. */
2016 read_state_pair_list (pair_p
*list
)
2020 pair_p previous
= NULL
;
2022 struct state_token_st
*t0
= peek_state_token (0);
2023 while (t0
&& state_token_kind (t0
) != STOK_RIGHTPAR
)
2025 read_state_pair (&tmp
);
2033 previous
->next
= tmp
;
2036 t0
= peek_state_token (0);
2040 /* don't consume the ); the caller will eat it. */
2045 /* Read the typedefs. */
2047 read_state_typedefs (pair_p
*typedefs
)
2051 struct state_token_st
*t0
= peek_state_token (0);
2052 struct state_token_st
*t1
= peek_state_token (1);
2053 struct state_token_st
*t2
= peek_state_token (2);
2055 if (state_token_kind (t0
) == STOK_LEFTPAR
2056 && state_token_is_name (t1
, "!typedefs")
2057 && state_token_kind (t2
) == STOK_INTEGER
)
2060 nbtypedefs
= t2
->stok_un
.stok_num
;
2061 next_state_tokens (3);
2062 nbpairs
= read_state_pair_list (&list
);
2063 t0
= peek_state_token (0);
2064 if (nbpairs
!= nbtypedefs
)
2065 fatal_reading_state_printf
2067 "invalid number of typedefs, expected %d but got %d",
2068 nbtypedefs
, nbpairs
);
2069 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
2070 next_state_tokens (1);
2072 fatal_reading_state (t0
, "Bad typedefs syntax )");
2075 fatal_reading_state (t0
, "Bad typedefs syntax (!typedefs");
2077 if (verbosity_level
>= 2)
2078 printf ("%s read %d typedefs from state\n", progname
, nbtypedefs
);
2083 /* Read the structures. */
2085 read_state_structures (type_p
*structures
)
2088 type_p previous
= NULL
;
2090 int nbstruct
= 0, countstruct
= 0;
2091 struct state_token_st
*t0
= peek_state_token (0);
2092 struct state_token_st
*t1
= peek_state_token (1);
2093 struct state_token_st
*t2
= peek_state_token (2);
2095 if (state_token_kind (t0
) == STOK_LEFTPAR
2096 && state_token_is_name (t1
, "!structures")
2097 && state_token_kind (t2
) == STOK_INTEGER
)
2099 nbstruct
= t2
->stok_un
.stok_num
;
2100 next_state_tokens (3);
2101 t0
= peek_state_token (0);
2102 while (t0
&& state_token_kind (t0
) != STOK_RIGHTPAR
)
2105 read_state_type (&tmp
);
2114 previous
->next
= tmp
;
2117 t0
= peek_state_token (0);
2119 next_state_tokens (1);
2122 fatal_reading_state (t0
, "Bad structures syntax");
2123 if (countstruct
!= nbstruct
)
2124 fatal_reading_state_printf (NULL_STATE_TOKEN
,
2125 "expected %d structures but got %d",
2126 nbstruct
, countstruct
);
2127 if (verbosity_level
>= 2)
2128 printf ("%s read %d structures from state\n", progname
, nbstruct
);
2133 /* Read the param_struct-s. */
2135 read_state_param_structs (type_p
*param_structs
)
2137 int nbparamstructs
= 0;
2138 int countparamstructs
= 0;
2140 type_p previous
= NULL
;
2142 struct state_token_st
*t0
= peek_state_token (0);
2143 struct state_token_st
*t1
= peek_state_token (1);
2144 struct state_token_st
*t2
= peek_state_token (2);
2146 if (state_token_kind (t0
) == STOK_LEFTPAR
2147 && state_token_is_name (t1
, "!param_structs")
2148 && state_token_kind (t2
) == STOK_INTEGER
)
2150 nbparamstructs
= t2
->stok_un
.stok_num
;
2151 next_state_tokens (3);
2152 t0
= t1
= t2
= NULL
;
2153 t0
= peek_state_token (0);
2154 while (state_token_kind (t0
) != STOK_RIGHTPAR
)
2157 read_state_type (&tmp
);
2165 previous
->next
= tmp
;
2168 t0
= peek_state_token (0);
2169 countparamstructs
++;
2171 next_state_tokens (1);
2174 fatal_reading_state (t0
, "Bad param_structs syntax");
2175 t0
= peek_state_token (0);
2176 if (countparamstructs
!= nbparamstructs
)
2177 fatal_reading_state_printf
2179 "invalid number of param_structs expected %d got %d",
2180 nbparamstructs
, countparamstructs
);
2181 *param_structs
= head
;
2185 /* Read the variables. */
2187 read_state_variables (pair_p
*variables
)
2191 struct state_token_st
*t0
= peek_state_token (0);
2192 struct state_token_st
*t1
= peek_state_token (1);
2193 struct state_token_st
*t2
= peek_state_token (2);
2195 if (state_token_kind (t0
) == STOK_LEFTPAR
2196 && state_token_is_name (t1
, "!variables")
2197 && state_token_kind (t2
) == STOK_INTEGER
)
2200 nbvars
= t2
->stok_un
.stok_num
;
2201 next_state_tokens (3);
2202 nbpairs
= read_state_pair_list (&list
);
2203 t0
= peek_state_token (0);
2204 if (nbpairs
!= nbvars
)
2205 fatal_reading_state_printf
2206 (t0
, "Invalid number of variables, expected %d but got %d",
2208 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
2209 next_state_tokens (1);
2211 fatal_reading_state (t0
, "Waiting for ) in variables");
2214 fatal_reading_state (t0
, "Bad variables syntax");
2216 if (verbosity_level
>= 2)
2217 printf ("%s read %d variables from state\n", progname
, nbvars
);
2221 /* Read the source directory. */
2223 read_state_srcdir (void)
2225 struct state_token_st
*t0
= peek_state_token (0);
2226 struct state_token_st
*t1
= peek_state_token (1);
2227 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2228 state_token_is_name (t1
, "!srcdir"))
2230 next_state_tokens (2);
2231 t0
= peek_state_token (0);
2232 t1
= peek_state_token (1);
2233 if (state_token_kind (t0
) == STOK_STRING
&&
2234 state_token_kind (t1
) == STOK_RIGHTPAR
)
2236 srcdir
= xstrdup (t0
->stok_un
.stok_string
);
2237 srcdir_len
= strlen (srcdir
);
2238 next_state_tokens (2);
2243 fatal_reading_state (t0
, "Bad srcdir in state_file");
2247 /* Read the sequence of GCC front-end languages. */
2249 read_state_languages (void)
2251 struct state_token_st
*t0
= peek_state_token (0);
2252 struct state_token_st
*t1
= peek_state_token (1);
2253 struct state_token_st
*t2
= peek_state_token (2);
2254 if (state_token_kind (t0
) == STOK_LEFTPAR
2255 && state_token_is_name (t1
, "!languages")
2256 && state_token_kind (t2
) == STOK_INTEGER
)
2259 num_lang_dirs
= t2
->stok_un
.stok_num
;
2260 lang_dir_names
= XCNEWVEC (const char *, num_lang_dirs
);
2261 next_state_tokens (3);
2262 t0
= t1
= t2
= NULL
;
2263 for (i
= 0; i
< (int) num_lang_dirs
; i
++)
2265 t0
= peek_state_token (0);
2266 if (state_token_kind (t0
) != STOK_NAME
)
2267 fatal_reading_state (t0
, "expecting language name in state file");
2268 lang_dir_names
[i
] = t0
->stok_un
.stok_ident
->stid_name
;
2269 next_state_tokens (1);
2271 t0
= peek_state_token (0);
2272 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
2273 fatal_reading_state (t0
, "missing ) in languages list of state file");
2274 next_state_tokens (1);
2277 fatal_reading_state (t0
, "expecting languages list in state file");
2281 /* Read the sequence of files. */
2283 read_state_files_list (void)
2285 struct state_token_st
*t0
= peek_state_token (0);
2286 struct state_token_st
*t1
= peek_state_token (1);
2287 struct state_token_st
*t2
= peek_state_token (2);
2289 if (state_token_kind (t0
) == STOK_LEFTPAR
2290 && state_token_is_name (t1
, "!fileslist")
2291 && state_token_kind (t2
) == STOK_INTEGER
)
2294 num_gt_files
= t2
->stok_un
.stok_num
;
2295 next_state_tokens (3);
2296 t0
= t1
= t2
= NULL
;
2297 gt_files
= XCNEWVEC (const input_file
*, num_gt_files
);
2298 for (i
= 0; i
< (int) num_gt_files
; i
++)
2300 bool issrcfile
= FALSE
;
2301 t0
= t1
= t2
= NULL
;
2302 t0
= peek_state_token (0);
2303 t1
= peek_state_token (1);
2304 t2
= peek_state_token (2);
2305 if (state_token_kind (t0
) == STOK_LEFTPAR
2306 && (state_token_is_name (t1
, "!file")
2307 || (issrcfile
= state_token_is_name (t1
, "!srcfile")))
2308 && state_token_kind (t2
) == STOK_INTEGER
)
2310 lang_bitmap bmap
= t2
->stok_un
.stok_num
;
2311 next_state_tokens (3);
2312 t0
= t1
= t2
= NULL
;
2313 t0
= peek_state_token (0);
2314 t1
= peek_state_token (1);
2315 if (state_token_kind (t0
) == STOK_STRING
2316 && state_token_kind (t1
) == STOK_RIGHTPAR
)
2318 const char *fnam
= t0
->stok_un
.stok_string
;
2319 /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
2320 input_file
*curgt
= NULL
;
2323 static const char dirsepstr
[2] =
2324 { DIR_SEPARATOR
, (char) 0 };
2325 char *fullpath
= concat (srcdir
, dirsepstr
, fnam
, NULL
);
2326 curgt
= input_file_by_name (fullpath
);
2330 curgt
= input_file_by_name (fnam
);
2331 set_lang_bitmap (curgt
, bmap
);
2332 gt_files
[i
] = curgt
;
2333 next_state_tokens (2);
2336 fatal_reading_state (t0
,
2337 "bad file in !fileslist of state file");
2340 fatal_reading_state (t0
,
2341 "expecting file in !fileslist of state file");
2343 t0
= peek_state_token (0);
2344 if (!state_token_kind (t0
) == STOK_RIGHTPAR
)
2345 fatal_reading_state (t0
, "missing ) for !fileslist in state file");
2346 next_state_tokens (1);
2349 fatal_reading_state (t0
, "missing !fileslist in state file");
2353 /* Read the trailer. */
2355 read_state_trailer (void)
2357 struct state_token_st
*t0
= peek_state_token (0);
2358 struct state_token_st
*t1
= peek_state_token (1);
2359 struct state_token_st
*t2
= peek_state_token (2);
2361 if (state_token_kind (t0
) == STOK_LEFTPAR
2362 && state_token_is_name (t1
, "!endfile")
2363 && state_token_kind (t2
) == STOK_RIGHTPAR
)
2364 next_state_tokens (3);
2366 fatal_reading_state (t0
, "missing !endfile in state file");
2370 /* Utility functions for the state_seen_types hash table. */
2372 hash_type_number (const void *ty
)
2374 const struct type
*type
= (const struct type
*) ty
;
2376 return type
->state_number
;
2380 equals_type_number (const void *ty1
, const void *ty2
)
2382 const struct type
*type1
= (const struct type
*) ty1
;
2383 const struct type
*type2
= (const struct type
*) ty2
;
2385 return type1
->state_number
== type2
->state_number
;
2389 string_eq (const void *a
, const void *b
)
2391 const char *a0
= (const char *)a
;
2392 const char *b0
= (const char *)b
;
2394 return (strcmp (a0
, b0
) == 0);
2398 /* The function reading the state, called by main from gengtype.c. */
2400 read_state (const char *path
)
2402 state_file
= fopen (path
, "r");
2403 if (state_file
== NULL
)
2404 fatal ("Failed to open state file %s for reading [%s]", path
,
2409 if (verbosity_level
>= 1)
2411 printf ("%s reading state file %s;", progname
, state_path
);
2412 if (verbosity_level
>= 2)
2418 htab_create (2017, hash_type_number
, equals_type_number
, NULL
);
2420 htab_create (4027, htab_hash_string
, string_eq
, NULL
);
2421 read_state_version (version_string
);
2422 read_state_srcdir ();
2423 read_state_languages ();
2424 read_state_files_list ();
2425 read_state_structures (&structures
);
2426 if (ferror (state_file
))
2427 fatal_reading_state_printf
2428 (NULL_STATE_TOKEN
, "input error while reading state [%s]",
2430 read_state_typedefs (&typedefs
);
2431 read_state_param_structs (¶m_structs
);
2432 read_state_variables (&variables
);
2433 read_state_trailer ();
2435 if (verbosity_level
>= 1)
2437 printf ("%s read %ld bytes.\n", progname
, ftell (state_file
));
2441 if (fclose (state_file
))
2442 fatal ("failed to close read state file %s [%s]",
2443 path
, xstrerror (errno
));
2448 /* End of file gengtype-state.c. */