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>
32 #include "errors.h" /* For fatal. */
33 #include "double-int.h"
35 #include "version.h" /* For version_string & pkgversion_string. */
41 /* Gives the file location of a type, if any. */
42 static inline struct fileloc
*
43 type_lineloc (const_type_p ty
)
53 case TYPE_LANG_STRUCT
:
54 return CONST_CAST (struct fileloc
*, &ty
->u
.s
.line
);
55 case TYPE_PARAM_STRUCT
:
56 return CONST_CAST (struct fileloc
*, &ty
->u
.param_struct
.line
);
67 /* The state file has simplistic lispy lexical tokens. Its lexer gives
68 a linked list of struct state_token_st, thru the peek_state_token
69 function. Lexical tokens are consumed with next_state_tokens. */
72 /* The lexical kind of each lispy token. */
75 STOK_NONE
, /* Never used. */
76 STOK_INTEGER
, /* Integer token. */
77 STOK_STRING
, /* String token. */
78 STOK_LEFTPAR
, /* Left opening parenthesis. */
79 STOK_RIGHTPAR
, /* Right closing parenthesis. */
80 STOK_NAME
/* hash-consed name or identifier. */
84 /* Structure and hash-table used to share identifiers or names. */
87 /* TODO: We could improve the parser by reserving identifiers for
88 state keywords and adding a keyword number for them. That would
89 mean adding another field in this state_ident_st struct. */
90 char stid_name
[1]; /* actually bigger & null terminated */
92 static htab_t state_ident_tab
;
95 /* The state_token_st structure is for lexical tokens in the read
96 state file. The stok_kind field discriminates the union. Tokens
97 are allocated by peek_state_token which calls read_a_state_token
98 which allocate them. Tokens are freed by calls to
99 next_state_tokens. Token are organized in a FIFO look-ahead queue
100 filled by peek_state_token. */
101 struct state_token_st
103 enum state_token_en stok_kind
; /* the lexical kind
104 discriminates the stok_un
106 int stok_line
; /* the line number */
107 int stok_col
; /* the column number */
108 const char *stok_file
; /* the file path */
109 struct state_token_st
*stok_next
; /* the next token in the
110 queue, when peeked */
111 union /* discriminated by stok_kind! */
113 int stok_num
; /* when STOK_INTEGER */
114 char stok_string
[1]; /* when STOK_STRING, actual size is
115 bigger and null terminated */
116 struct state_ident_st
*stok_ident
; /* when STOK_IDENT */
117 void *stok_ptr
; /* null otherwise */
125 #define NULL_STATE_TOKEN (struct state_token_st*)0
127 /* the state_token pointer contains the leftmost current token. The
128 tokens are organized in a linked queue, using stok_next, for token
130 struct state_token_st
*state_token
= NULL_STATE_TOKEN
;
132 /* Used by the reading lexer. */
133 static FILE *state_file
;
134 static const char *state_path
= NULL
;
135 static int state_line
= 0;
136 static long state_bol
= 0; /* offset of beginning of line */
139 /* Counter of written types. */
140 static int state_written_type_count
= 0;
143 /* Fatal error messages when reading the state. They are extremely
144 unlikely, and only appear when this gengtype-state.c file is buggy,
145 or when reading a gengtype state which was not generated by the
146 same version of gengtype or GCC. */
149 /* Fatal message while reading state. */
151 fatal_reading_state (struct state_token_st
* tok
, const char*msg
)
154 fatal ("%s:%d:%d: Invalid state file; %s",
155 tok
->stok_file
, tok
->stok_line
, tok
->stok_col
,
158 fatal ("%s:%d: Invalid state file; %s",
159 state_path
, state_line
, msg
);
163 /* Fatal printf-like message while reading state. This can't be a
164 function, because there is no way to pass a va_arg to a variant of
166 #define fatal_reading_state_printf(Tok,Fmt,...) do { \
167 struct state_token_st* badtok = Tok; \
169 fatal ("%s:%d:%d: Invalid state file; " Fmt, \
172 badtok->stok_col, __VA_ARGS__); \
174 fatal ("%s:%d: Invalid state file; " Fmt, \
175 state_path, state_line, __VA_ARGS__); \
179 /* Find or allocate an identifier in our name hash table. */
180 static struct state_ident_st
*
181 state_ident_by_name (const char *name
, enum insert_option optins
)
185 struct state_ident_st
*stid
= NULL
;
187 if (!name
|| !name
[0])
190 slot
= htab_find_slot (state_ident_tab
, name
, optins
);
194 namlen
= strlen (name
);
196 (struct state_ident_st
*) xmalloc (sizeof (struct state_ident_st
) +
198 memset (stid
, 0, sizeof (struct state_ident_st
) + namlen
);
199 strcpy (stid
->stid_name
, name
);
205 /* Our token lexer is heavily inspired by MELT's lexer, and share some
206 code with the file gcc/melt-runtime.c of the GCC MELT branch! We
207 really want the gengtype state to be easily parsable by MELT. This
208 is a usual lispy lexing routine, dealing with spaces and comments,
209 numbers, parenthesis, names, strings. */
210 static struct state_token_st
*
211 read_a_state_token (void)
215 struct state_token_st
*tk
= NULL
;
217 again
: /* Read again, e.g. after a comment or spaces. */
218 c
= getc (state_file
);
222 /* Handle spaces, count lines. */
226 state_bol
= curoff
= ftell (state_file
);
231 /* Skip comments starting with semi-colon. */
236 c
= getc (state_file
);
238 while (c
> 0 && c
!= '\n');
242 state_bol
= curoff
= ftell (state_file
);
246 /* Read signed numbers. */
247 if (ISDIGIT (c
) || c
== '-' || c
== '+')
250 ungetc (c
, state_file
);
251 curoff
= ftell (state_file
);
252 if (fscanf (state_file
, "%d", &n
) <= 0)
253 fatal_reading_state (NULL_STATE_TOKEN
, "Lexical error in number");
254 tk
= XCNEW (struct state_token_st
);
255 tk
->stok_kind
= STOK_INTEGER
;
256 tk
->stok_line
= state_line
;
257 tk
->stok_col
= curoff
- state_bol
;
258 tk
->stok_file
= state_path
;
259 tk
->stok_next
= NULL
;
260 tk
->stok_un
.stok_num
= n
;
264 /* Read an opening left parenthesis. */
267 curoff
= ftell (state_file
);
268 tk
= XCNEW (struct state_token_st
);
269 tk
->stok_kind
= STOK_LEFTPAR
;
270 tk
->stok_line
= state_line
;
271 tk
->stok_col
= curoff
- state_bol
;
272 tk
->stok_file
= state_path
;
273 tk
->stok_next
= NULL
;
277 /* Read an closing right parenthesis. */
280 curoff
= ftell (state_file
);
281 tk
= XCNEW (struct state_token_st
);
282 tk
->stok_kind
= STOK_RIGHTPAR
;
283 tk
->stok_line
= state_line
;
284 tk
->stok_col
= curoff
- state_bol
;
285 tk
->stok_file
= state_path
;
286 tk
->stok_next
= NULL
;
290 /* Read identifiers, using an obstack. */
291 else if (ISALPHA (c
) || c
== '_' || c
== '$' || c
== '!' || c
== '#')
293 struct obstack id_obstack
;
294 struct state_ident_st
*sid
= NULL
;
296 obstack_init (&id_obstack
);
297 curoff
= ftell (state_file
);
298 while (ISALNUM (c
) || c
== '_' || c
== '$' || c
== '!' || c
== '#')
300 obstack_1grow (&id_obstack
, c
);
301 c
= getc (state_file
);
306 ungetc (c
, state_file
);
307 obstack_1grow (&id_obstack
, (char) 0);
308 ids
= XOBFINISH (&id_obstack
, char *);
309 sid
= state_ident_by_name (ids
, INSERT
);
310 obstack_free (&id_obstack
, NULL
);
312 tk
= XCNEW (struct state_token_st
);
313 tk
->stok_kind
= STOK_NAME
;
314 tk
->stok_line
= state_line
;
315 tk
->stok_col
= curoff
- state_bol
;
316 tk
->stok_file
= state_path
;
317 tk
->stok_next
= NULL
;
318 tk
->stok_un
.stok_ident
= sid
;
322 /* Read a string, dealing with escape sequences a la C! */
327 struct obstack bstring_obstack
;
328 obstack_init (&bstring_obstack
);
329 curoff
= ftell (state_file
);
330 while ((c
= getc (state_file
)) != '"' && c
>= 0)
332 if (ISPRINT (c
) && c
!= '\\')
333 obstack_1grow (&bstring_obstack
, (char) c
);
334 else if (ISSPACE (c
) && c
!= '\n')
335 obstack_1grow (&bstring_obstack
, (char) c
);
338 c
= getc (state_file
);
342 obstack_1grow (&bstring_obstack
, '\a');
343 c
= getc (state_file
);
346 obstack_1grow (&bstring_obstack
, '\b');
347 c
= getc (state_file
);
350 obstack_1grow (&bstring_obstack
, '\t');
351 c
= getc (state_file
);
354 obstack_1grow (&bstring_obstack
, '\n');
355 c
= getc (state_file
);
358 obstack_1grow (&bstring_obstack
, '\v');
359 c
= getc (state_file
);
362 obstack_1grow (&bstring_obstack
, '\f');
363 c
= getc (state_file
);
366 obstack_1grow (&bstring_obstack
, '\r');
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
);
378 obstack_1grow (&bstring_obstack
, ' ');
379 c
= getc (state_file
);
384 if (fscanf (state_file
, "%02x", &cx
) > 0 && cx
> 0)
385 obstack_1grow (&bstring_obstack
, cx
);
389 "Lexical error in string hex escape");
390 c
= getc (state_file
);
396 "Lexical error - unknown string escape");
400 fatal_reading_state (NULL_STATE_TOKEN
, "Lexical error...");
403 fatal_reading_state (NULL_STATE_TOKEN
, "Unterminated string");
404 obstack_1grow (&bstring_obstack
, '\0');
405 cstr
= XOBFINISH (&bstring_obstack
, char *);
406 cslen
= strlen (cstr
);
407 tk
= (struct state_token_st
*)
408 xcalloc (sizeof (struct state_token_st
) + cslen
, 1);
409 tk
->stok_kind
= STOK_STRING
;
410 tk
->stok_line
= state_line
;
411 tk
->stok_col
= curoff
- state_bol
;
412 tk
->stok_file
= state_path
;
413 tk
->stok_next
= NULL
;
414 strcpy (tk
->stok_un
.stok_string
, cstr
);
415 obstack_free (&bstring_obstack
, NULL
);
419 /* Got an unexpected character. */
420 fatal_reading_state_printf
422 "Lexical error at offset %ld - bad character \\%03o = '%c'",
423 ftell (state_file
), c
, c
);
426 /* Used for lexical look-ahead. Retrieves the lexical token of rank
427 DEPTH, starting with 0 when reading the state file. Gives null on
429 static struct state_token_st
*
430 peek_state_token (int depth
)
432 int remdepth
= depth
;
433 struct state_token_st
**ptoken
= &state_token
;
434 struct state_token_st
*tok
= NULL
;
436 while (remdepth
>= 0)
440 *ptoken
= tok
= read_a_state_token ();
445 ptoken
= &((*ptoken
)->stok_next
);
452 /* Consume the next DEPTH tokens and free them. */
454 next_state_tokens (int depth
)
456 struct state_token_st
*n
;
460 if (state_token
!= NULL
)
462 n
= state_token
->stok_next
;
467 fatal_reading_state (NULL_STATE_TOKEN
, "Tokens stack empty");
473 /* Safely retrieve the lexical kind of a token. */
474 static inline enum state_token_en
475 state_token_kind (struct state_token_st
*p
)
483 /* Test if a token is a given name i.e. an identifier. */
485 state_token_is_name (struct state_token_st
*p
, const char *name
)
490 if (p
->stok_kind
!= STOK_NAME
)
493 return !strcmp (p
->stok_un
.stok_ident
->stid_name
, name
);
497 /* Following routines are useful for serializing datas.
499 * We want to serialize :
502 * - param_structs list
505 * So, we have one routine for each kind of data. The main writing
506 * routine is write_state. The main reading routine is
507 * read_state. Most writing routines write_state_FOO have a
508 * corresponding reading routine read_state_FOO. Reading is done in a
509 * recursive descending way, and any read error is fatal.
512 /* When reading the state, we need to remember the previously seen
513 types by their state_number, since GTY-ed types are usually
515 static htab_t state_seen_types
;
517 /* Return the length of a linked list made of pairs. */
518 static int pair_list_length (pair_p list
);
521 static void write_state_pair (pair_p
);
523 /* return the number of pairs written. Should match the length given
524 by pair_list_length. */
525 static int write_state_pair_list (pair_p list
);
527 /* Write a type. When a type is written, its state_number is updated,
528 to ensure that a "reference" to a seen type is written on next
530 static void write_state_type (type_p
);
532 /* Write a null-terminatel string using our Lispy lexical conventions,
533 similar to those of C or MELT. */
534 static void write_state_a_string (const char *s
);
536 /* Compute the length of a list of pairs, starting from the first
539 pair_list_length (pair_p list
)
543 for (l
= list
; l
; l
= l
->next
)
548 /* Write a file location. Files relative to $(srcdir) are quite
549 frequent and are handled specially. This ensures that two gengtype
550 state file-s produced by gengtype on the same GCC source tree are
551 very similar and can be reasonably compared with diff, even if the
552 two GCC source trees have different absolute paths. */
554 write_state_fileloc (struct fileloc
*floc
)
557 if (floc
!= NULL
&& floc
->line
> 0)
559 const char *srcrelpath
= NULL
;
560 gcc_assert (floc
->file
!= NULL
);
561 /* Most of the files are inside $(srcdir) so it is worth to
562 handle them specially. */
563 srcrelpath
= get_file_srcdir_relative_path (floc
->file
);
564 if (srcrelpath
!= NULL
)
566 fprintf (state_file
, "\n(!srcfileloc ");
567 write_state_a_string (srcrelpath
);
571 fprintf (state_file
, "\n(!fileloc ");
572 write_state_a_string (get_input_file_name (floc
->file
));
574 fprintf (state_file
, " %d", floc
->line
);
575 fprintf (state_file
, ")\n");
578 fprintf (state_file
, "nil ");
581 /* Write a list of fields. */
583 write_state_fields (pair_p fields
)
585 int nbfields
= pair_list_length (fields
);
587 fprintf (state_file
, "\n(!fields %d ", nbfields
);
588 nbpairs
= write_state_pair_list (fields
);
589 gcc_assert (nbpairs
== nbfields
);
590 fprintf (state_file
, ")\n");
593 /* Write a null-terminated string in our lexical convention, very
594 similar to the convention of C. */
596 write_state_a_string (const char *s
)
600 fputs (" \"", state_file
);
607 fputs ("\\a", state_file
);
610 fputs ("\\b", state_file
);
613 fputs ("\\t", state_file
);
616 fputs ("\\n", state_file
);
619 fputs ("\\v", state_file
);
622 fputs ("\\f", state_file
);
625 fputs ("\\r", state_file
);
628 fputs ("\\\"", state_file
);
631 fputs ("\\\\", state_file
);
635 putc (c
, state_file
);
637 fprintf (state_file
, "\\x%02x", (unsigned) c
);
640 fputs ("\"", state_file
);
643 /* Our option-s have three kinds, each with its writer. */
645 write_state_string_option (options_p current
)
647 fprintf (state_file
, "string ");
648 if (current
->info
.string
!= NULL
)
649 write_state_a_string (current
->info
.string
);
651 fprintf (state_file
, " nil ");
655 write_state_type_option (options_p current
)
657 fprintf (state_file
, "type ");
658 write_state_type (current
->info
.type
);
662 write_state_nested_option (options_p current
)
664 fprintf (state_file
, "nested ");
665 write_state_type (current
->info
.nested
->type
);
666 if (current
->info
.nested
->convert_from
!= NULL
)
667 write_state_a_string (current
->info
.nested
->convert_from
);
669 fprintf (state_file
, " nil ");
671 if (current
->info
.nested
->convert_to
!= NULL
)
672 write_state_a_string (current
->info
.nested
->convert_to
);
674 fprintf (state_file
, " nil ");
678 write_state_option (options_p current
)
680 fprintf (state_file
, "\n(!option ");
682 if (current
->name
!= NULL
)
683 fprintf (state_file
, "%s ", current
->name
);
685 fprintf (state_file
, "nil ");
687 switch (current
->kind
)
690 write_state_string_option (current
);
693 write_state_type_option (current
);
696 write_state_nested_option (current
);
699 fatal ("Option tag unknown");
702 fprintf (state_file
, ")\n");
707 /* Write a list of GTY options. */
709 write_state_options (options_p opt
)
715 fprintf (state_file
, "nil ");
719 fprintf (state_file
, "\n(!options ");
720 for (current
= opt
; current
!= NULL
; current
= current
->next
)
721 write_state_option (current
);
722 fprintf (state_file
, ")\n");
726 /* Write a bitmap representing a set of GCC front-end languages. */
728 write_state_lang_bitmap (lang_bitmap bitmap
)
730 fprintf (state_file
, "%d ", (int) bitmap
);
733 /* Write version information. */
735 write_state_version (const char *version
)
737 fprintf (state_file
, "\n(!version ");
738 write_state_a_string (version
);
739 fprintf (state_file
, ")\n");
742 /* Common routine to write the common content of all types. */
743 static void write_state_common_type_content (type_p current
);
745 /* Write a scalar type. We have only two of these. */
747 write_state_scalar_type (type_p current
)
749 if (current
== &scalar_nonchar
)
750 fprintf (state_file
, "scalar_nonchar ");
751 else if (current
== &scalar_char
)
752 fprintf (state_file
, "scalar_char ");
754 fatal ("Unexpected type in write_state_scalar_type");
756 write_state_common_type_content (current
);
759 /* Write the string type. There is only one such thing! */
761 write_state_string_type (type_p current
)
763 if (current
== &string_type
)
765 fprintf (state_file
, "string ");
766 write_state_common_type_content (current
);
769 fatal ("Unexpected type in write_state_string_type");
773 /* Common code to write structure like types. */
775 write_state_struct_union_type (type_p current
, const char *kindstr
)
777 DBGPRINTF ("%s type @ %p #%d '%s'", kindstr
, (void *) current
,
778 current
->state_number
, current
->u
.s
.tag
);
779 fprintf (state_file
, "%s ", kindstr
);
780 write_state_common_type_content (current
);
781 if (current
->u
.s
.tag
!= NULL
)
782 write_state_a_string (current
->u
.s
.tag
);
784 fprintf (state_file
, "nil");
786 write_state_fileloc (type_lineloc (current
));
787 write_state_fields (current
->u
.s
.fields
);
788 write_state_options (current
->u
.s
.opt
);
789 write_state_lang_bitmap (current
->u
.s
.bitmap
);
793 /* Write a GTY struct type. */
795 write_state_struct_type (type_p current
)
797 write_state_struct_union_type (current
, "struct");
798 write_state_type (current
->u
.s
.lang_struct
);
801 /* write a GTY union type. */
803 write_state_union_type (type_p current
)
805 write_state_struct_union_type (current
, "union");
806 write_state_type (current
->u
.s
.lang_struct
);
809 /* Write a lang_struct type. This is tricky and was painful to debug,
810 we deal with the next field specifically within their lang_struct
811 subfield, which points to a linked list of homonumous types.
812 Change this function with extreme care, see also
813 read_state_lang_struct_type. */
815 write_state_lang_struct_type (type_p current
)
819 const char *homoname
= 0;
820 write_state_struct_union_type (current
, "lang_struct");
821 /* lang_struct-ures are particularily tricky, since their
822 u.s.lang_struct field gives a list of homonymous struct-s or
824 DBGPRINTF ("lang_struct @ %p #%d", (void *) current
, current
->state_number
);
825 for (hty
= current
->u
.s
.lang_struct
; hty
!= NULL
; hty
= hty
->next
)
828 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype
,
829 (void *) hty
, hty
->state_number
, hty
->u
.s
.tag
);
830 /* Every member of the homonymous list should have the same tag. */
831 gcc_assert (UNION_OR_STRUCT_P (hty
));
832 gcc_assert (hty
->u
.s
.lang_struct
== current
);
834 homoname
= hty
->u
.s
.tag
;
835 gcc_assert (strcmp (homoname
, hty
->u
.s
.tag
) == 0);
837 fprintf (state_file
, "(!homotypes %d\n", nbhomontype
);
838 for (hty
= current
->u
.s
.lang_struct
; hty
!= NULL
; hty
= hty
->next
)
839 write_state_type (hty
);
840 fprintf (state_file
, ")\n");
843 /* Write a parametrized structure GTY type. */
845 write_state_param_struct_type (type_p current
)
849 fprintf (state_file
, "param_struct ");
850 write_state_common_type_content (current
);
851 write_state_type (current
->u
.param_struct
.stru
);
852 for (i
= 0; i
< NUM_PARAM
; i
++)
854 if (current
->u
.param_struct
.param
[i
] != NULL
)
855 write_state_type (current
->u
.param_struct
.param
[i
]);
857 fprintf (state_file
, "nil ");
859 write_state_fileloc (¤t
->u
.param_struct
.line
);
862 /* Write a pointer type. */
864 write_state_pointer_type (type_p current
)
866 fprintf (state_file
, "pointer ");
867 write_state_common_type_content (current
);
868 write_state_type (current
->u
.p
);
871 /* Write an array type. */
873 write_state_array_type (type_p current
)
875 fprintf (state_file
, "array ");
876 write_state_common_type_content (current
);
877 if (current
->u
.a
.len
!= NULL
)
878 write_state_a_string (current
->u
.a
.len
);
880 fprintf (state_file
, " nil");
882 fprintf (state_file
, " ");
883 write_state_type (current
->u
.a
.p
);
886 /* Write the gc_used information. */
888 write_state_gc_used (enum gc_used_enum gus
)
893 fprintf (state_file
, " gc_unused");
896 fprintf (state_file
, " gc_used");
898 case GC_MAYBE_POINTED_TO
:
899 fprintf (state_file
, " gc_maybe_pointed_to");
902 fprintf (state_file
, " gc_pointed_to");
909 /* Utility routine to write the common content of all types. Notice
910 that the next field is *not* written on purpose. */
912 write_state_common_type_content (type_p current
)
914 fprintf (state_file
, "%d ", current
->state_number
);
915 /* We do not write the next type, because list of types are
916 explicitly written. However, lang_struct are special in that
917 respect. See function write_state_lang_struct_type for more. */
918 write_state_type (current
->pointer_to
);
919 write_state_gc_used (current
->gc_used
);
923 /* The important and recursive routine writing GTY types as understood
924 by gengtype. Types which have a positive state_number have already
925 been seen and written. */
927 write_state_type (type_p current
)
931 fprintf (state_file
, "nil ");
935 fprintf (state_file
, "\n(!type ");
937 if (current
->state_number
> 0)
938 fprintf (state_file
, "already_seen %d", current
->state_number
);
941 state_written_type_count
++;
942 DBGPRINTF ("writing type #%d @%p old number %d", state_written_type_count
,
943 (void *) current
, current
->state_number
);
944 current
->state_number
= state_written_type_count
;
945 switch (current
->kind
)
948 write_state_struct_type (current
);
951 write_state_union_type (current
);
954 write_state_pointer_type (current
);
957 write_state_array_type (current
);
959 case TYPE_LANG_STRUCT
:
960 write_state_lang_struct_type (current
);
962 case TYPE_PARAM_STRUCT
:
963 write_state_param_struct_type (current
);
966 write_state_scalar_type (current
);
969 write_state_string_type (current
);
973 fatal ("Unexpected type...");
977 fprintf (state_file
, ")\n");
983 write_state_pair (pair_p current
)
987 fprintf (state_file
, "nil)");
991 fprintf (state_file
, "\n(!pair ");
993 if (current
->name
!= NULL
)
994 write_state_a_string (current
->name
);
996 write_state_a_string ("nil");
998 write_state_type (current
->type
);
999 write_state_fileloc (&(current
->line
));
1000 write_state_options (current
->opt
);
1002 fprintf (state_file
, ")");
1005 /* Write a pair list and return the number of pairs written. */
1007 write_state_pair_list (pair_p list
)
1012 for (current
= list
; current
!= NULL
; current
= current
->next
)
1014 write_state_pair (current
);
1021 /* When writing imported linked lists, like typedefs, structures,
1022 param_structs, ... we count their length first and write it. These
1023 eases the reading, and enables an extra verification on the number
1024 of actually read items. */
1026 /* Write our typedefs. */
1028 write_state_typedefs (void)
1030 int nbtypedefs
= pair_list_length (typedefs
);
1032 fprintf (state_file
, "\n(!typedefs %d\n", nbtypedefs
);
1033 nbpairs
= write_state_pair_list (typedefs
);
1034 gcc_assert (nbpairs
== nbtypedefs
);
1035 fprintf (state_file
, ")\n");
1036 if (verbosity_level
>= 2)
1037 printf ("%s wrote %d typedefs\n", progname
, nbtypedefs
);
1040 /* Write our structures. */
1042 write_state_structures (void)
1047 for (current
= structures
; current
!= NULL
; current
= current
->next
)
1050 fprintf (state_file
, "\n(!structures %d\n", nbstruct
);
1052 for (current
= structures
; current
!= NULL
; current
= current
->next
)
1053 write_state_type (current
);
1055 fprintf (state_file
, ")\n");
1056 if (verbosity_level
>= 2)
1057 printf ("%s wrote %d structures in state\n", progname
, nbstruct
);
1060 /* Write our param_struct-s. */
1062 write_state_param_structs (void)
1064 int nbparamstruct
= 0;
1067 for (current
= param_structs
; current
!= NULL
; current
= current
->next
)
1070 fprintf (state_file
, "\n(!param_structs %d\n", nbparamstruct
);
1072 for (current
= param_structs
; current
!= NULL
; current
= current
->next
)
1073 write_state_type (current
);
1075 fprintf (state_file
, ")\n");
1078 /* Write our variables. */
1080 write_state_variables (void)
1082 int nbvars
= pair_list_length (variables
);
1084 fprintf (state_file
, "\n(!variables %d\n", nbvars
);
1085 nbpairs
= write_state_pair_list (variables
);
1086 gcc_assert (nbpairs
== nbvars
);
1087 fprintf (state_file
, ")\n");
1088 if (verbosity_level
>= 2)
1089 printf ("%s wrote %d variables.\n", progname
, nbvars
);
1092 /* Write the source directory. File locations within the source
1093 directory have been written specifically. */
1095 write_state_srcdir (void)
1097 fprintf (state_file
, "\n(!srcdir ");
1098 write_state_a_string (srcdir
);
1099 fprintf (state_file
, ")\n");
1102 /* Count and write the list of our files. */
1104 write_state_files_list (void)
1107 /* Write the list of files with their lang_bitmap. */
1108 fprintf (state_file
, "\n(!fileslist %d\n", (int) num_gt_files
);
1109 for (i
= 0; i
< (int) num_gt_files
; i
++)
1111 const char *cursrcrelpath
= NULL
;
1112 const input_file
*curfil
= gt_files
[i
];
1113 /* Most of the files are inside $(srcdir) so it is worth to
1114 handle them specially. */
1115 cursrcrelpath
= get_file_srcdir_relative_path (curfil
);
1118 fprintf (state_file
, "(!srcfile %d ", get_lang_bitmap (curfil
));
1119 write_state_a_string (cursrcrelpath
);
1123 fprintf (state_file
, "(!file %d ", get_lang_bitmap (curfil
));
1124 write_state_a_string (get_input_file_name (curfil
));
1126 fprintf (state_file
, ")\n");
1128 fprintf (state_file
, ")\n");
1131 /* Write the list of GCC front-end languages. */
1133 write_state_languages (void)
1136 fprintf (state_file
, "\n(!languages %d", (int) num_lang_dirs
);
1137 for (i
= 0; i
< (int) num_lang_dirs
; i
++)
1139 /* Languages names are identifiers, we expect only letters or
1140 underscores or digits in them. In particular, C++ is not a
1141 valid language name, but cp is valid. */
1142 fprintf (state_file
, " %s", lang_dir_names
[i
]);
1144 fprintf (state_file
, ")\n");
1147 /* Write the trailer. */
1149 write_state_trailer (void)
1151 /* This test should probably catch IO errors like disk full... */
1152 if (fputs ("\n(!endfile)\n", state_file
) == EOF
)
1153 fatal ("failed to write state trailer [%s]", xstrerror (errno
));
1156 /* The write_state routine is the only writing routine called by main
1157 in gengtype.c. To avoid messing the state if gengtype is
1158 interrupted or aborted, we write a temporary file and rename it
1159 after having written it in totality. */
1161 write_state (const char *state_path
)
1165 char *temp_state_path
= NULL
;
1166 char tempsuffix
[40];
1169 /* We write a unique temporary file which is renamed when complete
1170 * only. So even if gengtype is interrupted, the written state file
1171 * won't be partially written, since the temporary file is not yet
1172 * renamed in that case. */
1173 memset (tempsuffix
, 0, sizeof (tempsuffix
));
1174 snprintf (tempsuffix
, sizeof (tempsuffix
) - 1, "-%ld-%d.tmp", (long) now
,
1176 temp_state_path
= concat (state_path
, tempsuffix
, NULL
);
1177 state_file
= fopen (temp_state_path
, "w");
1178 if (state_file
== NULL
)
1179 fatal ("Failed to open file %s for writing state: %s",
1180 temp_state_path
, xstrerror (errno
));
1181 if (verbosity_level
>= 3)
1182 printf ("%s writing state file %s temporarily in %s\n",
1183 progname
, state_path
, temp_state_path
);
1184 /* This is the first line of the state. Perhaps the file utility
1185 could know about that, so don't change it often. */
1186 fprintf (state_file
, ";;;;@@@@ GCC gengtype state\n");
1187 /* Output a few comments for humans. */
1188 fprintf (state_file
,
1189 ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
1190 fprintf (state_file
,
1191 ";;; The format of this file is tied to a particular version of GCC.\n");
1192 fprintf (state_file
,
1193 ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1194 fprintf (state_file
,
1195 ";;; This file should be parsed by the same %s which wrote it.\n",
1197 /* The first non-comment significant line gives the version string. */
1198 write_state_version (version_string
);
1199 write_state_srcdir ();
1200 write_state_languages ();
1201 write_state_files_list ();
1202 write_state_structures ();
1203 write_state_typedefs ();
1204 write_state_param_structs ();
1205 write_state_variables ();
1206 write_state_trailer ();
1207 statelen
= ftell (state_file
);
1208 if (ferror (state_file
))
1209 fatal ("output error when writing state file %s [%s]",
1210 temp_state_path
, xstrerror (errno
));
1211 if (fclose (state_file
))
1212 fatal ("failed to close state file %s [%s]",
1213 temp_state_path
, xstrerror (errno
));
1214 if (rename (temp_state_path
, state_path
))
1215 fatal ("failed to rename %s to state file %s [%s]", temp_state_path
,
1216 state_path
, xstrerror (errno
));
1217 free (temp_state_path
);
1219 if (verbosity_level
>= 1)
1220 printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1221 progname
, state_path
, statelen
, state_written_type_count
);
1225 /** End of writing routines! The corresponding reading routines follow. **/
1229 /* Forward declarations, since some read_state_* functions are
1231 static void read_state_fileloc (struct fileloc
*line
);
1232 static void read_state_options (options_p
*opt
);
1233 static void read_state_type (type_p
*current
);
1234 static void read_state_pair (pair_p
*pair
);
1235 /* Return the number of pairs actually read. */
1236 static int read_state_pair_list (pair_p
*list
);
1237 static void read_state_fields (pair_p
*fields
);
1238 static void read_state_common_type_content (type_p current
);
1243 /* Record into the state_seen_types hash-table a type which we are
1244 reading, to enable recursive or circular references to it. */
1246 record_type (type_p type
)
1250 slot
= htab_find_slot (state_seen_types
, type
, INSERT
);
1256 /* Read an already seen type. */
1258 read_state_already_seen_type (type_p
*type
)
1260 struct state_token_st
*t0
= peek_state_token (0);
1262 if (state_token_kind (t0
) == STOK_INTEGER
)
1265 struct type loctype
= { TYPE_SCALAR
, 0, 0, 0, GC_UNUSED
, {0} };
1267 loctype
.state_number
= t0
->stok_un
.stok_num
;
1268 slot
= htab_find_slot (state_seen_types
, &loctype
, NO_INSERT
);
1271 fatal_reading_state (t0
, "Unknown type");
1274 next_state_tokens (1);
1275 *type
= (type_p
) *slot
;
1279 fatal_reading_state (t0
, "Bad seen type");
1284 /* Read the scalar_nonchar type. */
1286 read_state_scalar_nonchar_type (type_p
*type
)
1288 *type
= &scalar_nonchar
;
1289 read_state_common_type_content (*type
);
1293 /* Read the scalar_char type. */
1295 read_state_scalar_char_type (type_p
*type
)
1297 *type
= &scalar_char
;
1298 read_state_common_type_content (*type
);
1302 /* Read the string_type. */
1304 read_state_string_type (type_p
*type
)
1306 *type
= &string_type
;
1307 read_state_common_type_content (*type
);
1311 /* Read a lang_bitmap representing a set of GCC front-end languages. */
1313 read_state_lang_bitmap (lang_bitmap
*bitmap
)
1315 struct state_token_st
*t
;
1317 t
= peek_state_token (0);
1318 if (state_token_kind (t
) == STOK_INTEGER
)
1320 *bitmap
= t
->stok_un
.stok_num
;
1321 next_state_tokens (1);
1325 fatal_reading_state (t
, "Bad syntax for bitmap");
1330 /* Read a GTY-ed struct type. */
1332 read_state_struct_type (type_p type
)
1334 struct state_token_st
*t0
;
1336 type
->kind
= TYPE_STRUCT
;
1337 read_state_common_type_content (type
);
1338 t0
= peek_state_token (0);
1339 if (state_token_kind (t0
) == STOK_STRING
)
1341 if (state_token_is_name (t0
, "nil"))
1343 type
->u
.s
.tag
= NULL
;
1344 DBGPRINTF ("read anonymous struct type @%p #%d",
1345 (void *) type
, type
->state_number
);
1349 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1350 DBGPRINTF ("read struct type @%p #%d '%s'",
1351 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1354 next_state_tokens (1);
1355 read_state_fileloc (&(type
->u
.s
.line
));
1356 read_state_fields (&(type
->u
.s
.fields
));
1357 read_state_options (&(type
->u
.s
.opt
));
1358 read_state_lang_bitmap (&(type
->u
.s
.bitmap
));
1359 read_state_type (&(type
->u
.s
.lang_struct
));
1363 fatal_reading_state (t0
, "Bad tag in struct type");
1368 /* Read a GTY-ed union type. */
1370 read_state_union_type (type_p type
)
1372 struct state_token_st
*t0
;
1374 type
->kind
= TYPE_UNION
;
1375 read_state_common_type_content (type
);
1376 t0
= peek_state_token (0);
1377 if (state_token_kind (t0
) == STOK_STRING
)
1379 if (state_token_is_name (t0
, "nil"))
1381 type
->u
.s
.tag
= NULL
;
1382 DBGPRINTF ("read anonymous union type @%p #%d",
1383 (void *) type
, type
->state_number
);
1387 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1388 DBGPRINTF ("read union type @%p #%d '%s'",
1389 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1391 next_state_tokens (1);
1392 read_state_fileloc (&(type
->u
.s
.line
));
1393 read_state_fields (&(type
->u
.s
.fields
));
1394 read_state_options (&(type
->u
.s
.opt
));
1395 read_state_lang_bitmap (&(type
->u
.s
.bitmap
));
1396 read_state_type (&(type
->u
.s
.lang_struct
));
1399 fatal_reading_state (t0
, "Bad tag in union type");
1403 /* Read a GTY-ed pointer type. */
1405 read_state_pointer_type (type_p type
)
1407 type
->kind
= TYPE_POINTER
;
1408 read_state_common_type_content (type
);
1409 DBGPRINTF ("read pointer type @%p #%d", (void *) type
, type
->state_number
);
1410 read_state_type (&(type
->u
.p
));
1414 /* Read a GTY-ed array type. */
1416 read_state_array_type (type_p type
)
1418 struct state_token_st
*t0
;
1420 type
->kind
= TYPE_ARRAY
;
1421 read_state_common_type_content (type
);
1422 t0
= peek_state_token (0);
1423 if (state_token_kind (t0
) == STOK_STRING
)
1425 type
->u
.a
.len
= xstrdup (t0
->stok_un
.stok_string
);
1426 DBGPRINTF ("read array type @%p #%d length '%s'",
1427 (void *) type
, type
->state_number
, type
->u
.a
.len
);
1428 next_state_tokens (1);
1431 else if (state_token_is_name (t0
, "nil"))
1433 type
->u
.a
.len
= NULL
;
1434 DBGPRINTF ("read array type @%p #%d without length",
1435 (void *) type
, type
->state_number
);
1436 next_state_tokens (1);
1440 fatal_reading_state (t0
, "Bad array name type");
1441 read_state_type (&(type
->u
.a
.p
));
1446 /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
1447 front-end languages. This is a tricky function and it was painful
1448 to debug. Change it with extreme care. See also
1449 write_state_lang_struct_type. */
1451 read_state_lang_struct_type (type_p type
)
1453 struct state_token_st
*t0
= NULL
;
1454 struct state_token_st
*t1
= NULL
;
1455 struct state_token_st
*t2
= NULL
;
1457 type
->kind
= TYPE_LANG_STRUCT
;
1458 read_state_common_type_content (type
);
1459 t0
= peek_state_token (0);
1460 if (state_token_kind (t0
) == STOK_STRING
)
1462 if (state_token_is_name (t0
, "nil"))
1464 DBGPRINTF ("read anonymous lang_struct type @%p #%d",
1465 (void *) type
, type
->state_number
);
1466 type
->u
.s
.tag
= NULL
;
1470 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1471 DBGPRINTF ("read lang_struct type @%p #%d '%s'",
1472 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1474 next_state_tokens (1);
1477 fatal_reading_state (t0
, "Bad tag in lang struct type");
1478 read_state_fileloc (&(type
->u
.s
.line
));
1479 read_state_fields (&(type
->u
.s
.fields
));
1480 read_state_options (&(type
->u
.s
.opt
));
1481 read_state_lang_bitmap (&(type
->u
.s
.bitmap
));
1482 /* Within lang_struct-ures, the lang_struct field is a linked list
1483 of homonymous types! */
1484 t0
= peek_state_token (0);
1485 t1
= peek_state_token (1);
1486 t2
= peek_state_token (2);
1487 /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
1488 if (state_token_kind (t0
) == STOK_LEFTPAR
1489 && state_token_is_name (t1
, "!homotypes")
1490 && state_token_kind (t2
) == STOK_INTEGER
)
1492 type_p
*prevty
= &type
->u
.s
.lang_struct
;
1493 int nbhomotype
= t2
->stok_un
.stok_num
;
1495 t0
= t1
= t2
= NULL
;
1496 next_state_tokens (3);
1497 for (i
= 0; i
< nbhomotype
; i
++)
1499 read_state_type (prevty
);
1500 t0
= peek_state_token (0);
1502 prevty
= &(*prevty
)->next
;
1504 fatal_reading_state (t0
,
1505 "expecting type in homotype list for lang_struct");
1507 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1508 fatal_reading_state (t0
,
1509 "expecting ) in homotype list for lang_struct");
1510 next_state_tokens (1);
1513 fatal_reading_state (t0
, "expecting !homotypes for lang_struct");
1517 /* Read a param_struct type for GTY parametrized structures. */
1519 read_state_param_struct_type (type_p type
)
1522 struct state_token_st
*t0
;
1524 type
->kind
= TYPE_PARAM_STRUCT
;
1525 read_state_common_type_content (type
);
1526 DBGPRINTF ("read param_struct type @%p #%d",
1527 (void *) type
, type
->state_number
);
1528 read_state_type (&(type
->u
.param_struct
.stru
));
1530 for (i
= 0; i
< NUM_PARAM
; i
++)
1532 t0
= peek_state_token (0);
1533 if (state_token_is_name (t0
, "nil"))
1535 type
->u
.param_struct
.param
[i
] = NULL
;
1536 next_state_tokens (1);
1539 read_state_type (&(type
->u
.param_struct
.param
[i
]));
1541 read_state_fileloc (&(type
->u
.param_struct
.line
));
1545 /* Read the gc used information. */
1547 read_state_gc_used (enum gc_used_enum
*pgus
)
1549 struct state_token_st
*t0
= peek_state_token (0);
1550 if (state_token_is_name (t0
, "gc_unused"))
1552 else if (state_token_is_name (t0
, "gc_used"))
1554 else if (state_token_is_name (t0
, "gc_maybe_pointed_to"))
1555 *pgus
= GC_MAYBE_POINTED_TO
;
1556 else if (state_token_is_name (t0
, "gc_pointed_to"))
1557 *pgus
= GC_POINTED_TO
;
1559 fatal_reading_state (t0
, "invalid gc_used information");
1560 next_state_tokens (1);
1564 /* Utility function to read the common content of types. */
1566 read_state_common_type_content (type_p current
)
1568 struct state_token_st
*t0
= peek_state_token (0);
1570 if (state_token_kind (t0
) == STOK_INTEGER
)
1572 current
->state_number
= t0
->stok_un
.stok_num
;
1573 next_state_tokens (1);
1574 record_type (current
);
1577 fatal_reading_state_printf (t0
,
1578 "Expected integer for state_number line %d",
1580 /* We don't read the next field of the type. */
1581 read_state_type (¤t
->pointer_to
);
1582 read_state_gc_used (¤t
->gc_used
);
1586 /* Read a GTY-ed type. */
1588 read_state_type (type_p
*current
)
1590 struct state_token_st
*t0
= peek_state_token (0);
1591 struct state_token_st
*t1
= peek_state_token (1);
1593 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1594 state_token_is_name (t1
, "!type"))
1596 next_state_tokens (2);
1597 t0
= peek_state_token (0);
1598 if (state_token_is_name (t0
, "already_seen"))
1600 next_state_tokens (1);
1601 read_state_already_seen_type (current
);
1605 t0
= peek_state_token (0);
1607 if (state_token_is_name (t0
, "scalar_nonchar"))
1609 next_state_tokens (1);
1610 read_state_scalar_nonchar_type (current
);
1612 else if (state_token_is_name (t0
, "scalar_char"))
1614 next_state_tokens (1);
1615 read_state_scalar_char_type (current
);
1617 else if (state_token_is_name (t0
, "string"))
1619 next_state_tokens (1);
1620 read_state_string_type (current
);
1622 else if (state_token_is_name (t0
, "struct"))
1624 *current
= XCNEW (struct type
);
1625 next_state_tokens (1);
1626 read_state_struct_type (*current
);
1628 else if (state_token_is_name (t0
, "union"))
1630 *current
= XCNEW (struct type
);
1631 next_state_tokens (1);
1632 read_state_union_type (*current
);
1634 else if (state_token_is_name (t0
, "lang_struct"))
1636 *current
= XCNEW (struct type
);
1637 next_state_tokens (1);
1638 read_state_lang_struct_type (*current
);
1640 else if (state_token_is_name (t0
, "param_struct"))
1642 *current
= XCNEW (struct type
);
1643 next_state_tokens (1);
1644 read_state_param_struct_type (*current
);
1646 else if (state_token_is_name (t0
, "pointer"))
1648 *current
= XCNEW (struct type
);
1649 next_state_tokens (1);
1650 read_state_pointer_type (*current
);
1652 else if (state_token_is_name (t0
, "array"))
1654 *current
= XCNEW (struct type
);
1655 next_state_tokens (1);
1656 read_state_array_type (*current
);
1659 fatal_reading_state (t0
, "bad type in (!type");
1661 t0
= peek_state_token (0);
1662 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1663 fatal_reading_state (t0
, "missing ) in type");
1664 next_state_tokens (1);
1666 else if (state_token_is_name (t0
, "nil"))
1668 next_state_tokens (1);
1672 fatal_reading_state (t0
, "bad type syntax");
1676 /* Read a file location. Files within the source directory are dealt
1677 with specifically. */
1679 read_state_fileloc (struct fileloc
*floc
)
1681 bool issrcfile
= false;
1682 struct state_token_st
*t0
= peek_state_token (0);
1683 struct state_token_st
*t1
= peek_state_token (1);
1685 gcc_assert (floc
!= NULL
);
1686 gcc_assert (srcdir
!= NULL
);
1688 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1689 (state_token_is_name (t1
, "!fileloc")
1690 || (issrcfile
= state_token_is_name (t1
, "!srcfileloc"))))
1692 next_state_tokens (2);
1693 t0
= peek_state_token (0);
1694 t1
= peek_state_token (1);
1695 if (state_token_kind (t0
) == STOK_STRING
&&
1696 state_token_kind (t1
) == STOK_INTEGER
)
1698 char *path
= t0
->stok_un
.stok_string
;
1701 static const char dirsepstr
[2] = { DIR_SEPARATOR
, (char) 0 };
1702 char *fullpath
= concat (srcdir
, dirsepstr
, path
, NULL
);
1703 floc
->file
= input_file_by_name (fullpath
);
1707 floc
->file
= input_file_by_name (path
);
1708 floc
->line
= t1
->stok_un
.stok_num
;
1709 next_state_tokens (2);
1712 fatal_reading_state (t0
,
1713 "Bad fileloc syntax, expected path string and line");
1714 t0
= peek_state_token (0);
1715 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1716 fatal_reading_state (t0
, "Bad fileloc syntax, expected )");
1717 next_state_tokens (1);
1719 else if (state_token_is_name (t0
, "nil"))
1721 next_state_tokens (1);
1726 fatal_reading_state (t0
, "Bad fileloc syntax");
1730 /* Read the fields of a GTY-ed type. */
1732 read_state_fields (pair_p
*fields
)
1735 struct state_token_st
*t0
= peek_state_token (0);
1736 struct state_token_st
*t1
= peek_state_token (1);
1737 struct state_token_st
*t2
= peek_state_token (2);
1739 if (state_token_kind (t0
) == STOK_LEFTPAR
1740 && state_token_is_name (t1
, "!fields")
1741 && state_token_kind (t2
) == STOK_INTEGER
)
1743 int nbfields
= t2
->stok_un
.stok_num
;
1745 next_state_tokens (3);
1746 nbpairs
= read_state_pair_list (&tmp
);
1747 t0
= peek_state_token (0);
1748 if (nbpairs
!= nbfields
)
1749 fatal_reading_state_printf
1751 "Mismatched fields number, expected %d got %d", nbpairs
, nbfields
);
1752 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
1753 next_state_tokens (1);
1755 fatal_reading_state (t0
, "Bad fields expecting )");
1762 /* Read a string option. */
1764 read_state_string_option (options_p opt
)
1766 struct state_token_st
*t0
= peek_state_token (0);
1767 opt
->kind
= OPTION_STRING
;
1768 if (state_token_kind (t0
) == STOK_STRING
)
1770 opt
->info
.string
= xstrdup (t0
->stok_un
.stok_string
);
1771 next_state_tokens (1);
1773 else if (state_token_is_name (t0
, "nil"))
1775 opt
->info
.string
= NULL
;
1776 next_state_tokens (1);
1779 fatal_reading_state (t0
, "Missing name in string option");
1783 /* Read a type option. */
1785 read_state_type_option (options_p opt
)
1787 opt
->kind
= OPTION_TYPE
;
1788 read_state_type (&(opt
->info
.type
));
1792 /* Read a nested option. */
1794 read_state_nested_option (options_p opt
)
1796 struct state_token_st
*t0
;
1798 opt
->info
.nested
= XCNEW (struct nested_ptr_data
);
1799 opt
->kind
= OPTION_NESTED
;
1800 read_state_type (&(opt
->info
.nested
->type
));
1801 t0
= peek_state_token (0);
1802 if (state_token_kind (t0
) == STOK_STRING
)
1804 opt
->info
.nested
->convert_from
= xstrdup (t0
->stok_un
.stok_string
);
1805 next_state_tokens (1);
1807 else if (state_token_is_name (t0
, "nil"))
1809 opt
->info
.nested
->convert_from
= NULL
;
1810 next_state_tokens (1);
1813 fatal_reading_state (t0
, "Bad nested convert_from option");
1815 t0
= peek_state_token (0);
1816 if (state_token_kind (t0
) == STOK_STRING
)
1818 opt
->info
.nested
->convert_to
= xstrdup (t0
->stok_un
.stok_string
);
1819 next_state_tokens (1);
1821 else if (state_token_is_name (t0
, "nil"))
1823 opt
->info
.nested
->convert_to
= NULL
;
1824 next_state_tokens (1);
1827 fatal_reading_state (t0
, "Bad nested convert_from option");
1831 /* Read an GTY option. */
1833 read_state_option (options_p
*opt
)
1835 struct state_token_st
*t0
= peek_state_token (0);
1836 struct state_token_st
*t1
= peek_state_token (1);
1838 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1839 state_token_is_name (t1
, "!option"))
1841 next_state_tokens (2);
1842 t0
= peek_state_token (0);
1843 if (state_token_kind (t0
) == STOK_NAME
)
1845 *opt
= XCNEW (struct options
);
1846 if (state_token_is_name (t0
, "nil"))
1847 (*opt
)->name
= NULL
;
1849 (*opt
)->name
= t0
->stok_un
.stok_ident
->stid_name
;
1850 next_state_tokens (1);
1851 t0
= peek_state_token (0);
1852 if (state_token_kind (t0
) == STOK_NAME
)
1854 if (state_token_is_name (t0
, "string"))
1856 next_state_tokens (1);
1857 read_state_string_option (*opt
);
1859 else if (state_token_is_name (t0
, "type"))
1861 next_state_tokens (1);
1862 read_state_type_option (*opt
);
1864 else if (state_token_is_name (t0
, "nested"))
1866 next_state_tokens (1);
1867 read_state_nested_option (*opt
);
1870 fatal_reading_state (t0
, "Bad option type");
1871 t0
= peek_state_token (0);
1872 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1873 fatal_reading_state (t0
, "Bad syntax in option, expecting )");
1875 next_state_tokens (1);
1878 fatal_reading_state (t0
, "Missing option type");
1881 fatal_reading_state (t0
, "Bad name for option");
1884 fatal_reading_state (t0
, "Bad option, waiting for )");
1887 /* Read a list of options. */
1889 read_state_options (options_p
*opt
)
1891 options_p head
= NULL
;
1892 options_p previous
= NULL
;
1893 options_p current_option
= NULL
;
1894 struct state_token_st
*t0
= peek_state_token (0);
1895 struct state_token_st
*t1
= peek_state_token (1);
1897 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1898 state_token_is_name (t1
, "!options"))
1900 next_state_tokens (2);
1901 t0
= peek_state_token (0);
1902 while (state_token_kind (t0
) != STOK_RIGHTPAR
)
1904 read_state_option (¤t_option
);
1907 head
= current_option
;
1912 previous
->next
= current_option
;
1913 previous
= current_option
;
1915 t0
= peek_state_token (0);
1917 next_state_tokens (1);
1919 else if (state_token_is_name (t0
, "nil"))
1921 next_state_tokens (1);
1924 fatal_reading_state (t0
, "Bad options syntax");
1930 /* Read a version, and check against the version of the gengtype. */
1932 read_state_version (const char *version_string
)
1934 struct state_token_st
*t0
= peek_state_token (0);
1935 struct state_token_st
*t1
= peek_state_token (1);
1937 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1938 state_token_is_name (t1
, "!version"))
1940 next_state_tokens (2);
1941 t0
= peek_state_token (0);
1942 t1
= peek_state_token (1);
1943 if (state_token_kind (t0
) == STOK_STRING
&&
1944 state_token_kind (t1
) == STOK_RIGHTPAR
)
1946 /* Check that the read version string is the same as current
1948 if (strcmp (version_string
, t0
->stok_un
.stok_string
))
1949 fatal_reading_state_printf (t0
,
1950 "version string mismatch; expecting %s but got %s",
1952 t0
->stok_un
.stok_string
);
1953 next_state_tokens (2);
1956 fatal_reading_state (t0
, "Missing version or right parenthesis");
1959 fatal_reading_state (t0
, "Bad version syntax");
1965 read_state_pair (pair_p
*current
)
1967 struct state_token_st
*t0
= peek_state_token (0);
1968 struct state_token_st
*t1
= peek_state_token (1);
1969 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1970 state_token_is_name (t1
, "!pair"))
1972 *current
= XCNEW (struct pair
);
1973 next_state_tokens (2);
1974 t0
= peek_state_token (0);
1975 if (state_token_kind (t0
) == STOK_STRING
)
1977 if (strcmp (t0
->stok_un
.stok_string
, "nil") == 0)
1979 (*current
)->name
= NULL
;
1983 (*current
)->name
= xstrdup (t0
->stok_un
.stok_string
);
1985 next_state_tokens (1);
1986 read_state_type (&((*current
)->type
));
1987 read_state_fileloc (&((*current
)->line
));
1988 read_state_options (&((*current
)->opt
));;
1989 t0
= peek_state_token (0);
1990 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
1992 next_state_tokens (1);
1996 fatal_reading_state (t0
, "Bad syntax for pair, )");
2001 fatal_reading_state (t0
, "Bad name for pair");
2004 else if (state_token_kind (t0
) == STOK_NAME
&&
2005 state_token_is_name (t0
, "nil"))
2007 next_state_tokens (1);
2011 fatal_reading_state_printf (t0
, "Bad syntax for pair, (!pair %d",
2012 state_token
->stok_kind
);
2016 /* Return the number of pairs actually read. */
2018 read_state_pair_list (pair_p
*list
)
2022 pair_p previous
= NULL
;
2024 struct state_token_st
*t0
= peek_state_token (0);
2025 while (t0
&& state_token_kind (t0
) != STOK_RIGHTPAR
)
2027 read_state_pair (&tmp
);
2035 previous
->next
= tmp
;
2038 t0
= peek_state_token (0);
2042 /* don't consume the ); the caller will eat it. */
2047 /* Read the typedefs. */
2049 read_state_typedefs (pair_p
*typedefs
)
2053 struct state_token_st
*t0
= peek_state_token (0);
2054 struct state_token_st
*t1
= peek_state_token (1);
2055 struct state_token_st
*t2
= peek_state_token (2);
2057 if (state_token_kind (t0
) == STOK_LEFTPAR
2058 && state_token_is_name (t1
, "!typedefs")
2059 && state_token_kind (t2
) == STOK_INTEGER
)
2062 nbtypedefs
= t2
->stok_un
.stok_num
;
2063 next_state_tokens (3);
2064 nbpairs
= read_state_pair_list (&list
);
2065 t0
= peek_state_token (0);
2066 if (nbpairs
!= nbtypedefs
)
2067 fatal_reading_state_printf
2069 "invalid number of typedefs, expected %d but got %d",
2070 nbtypedefs
, nbpairs
);
2071 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
2072 next_state_tokens (1);
2074 fatal_reading_state (t0
, "Bad typedefs syntax )");
2077 fatal_reading_state (t0
, "Bad typedefs syntax (!typedefs");
2079 if (verbosity_level
>= 2)
2080 printf ("%s read %d typedefs from state\n", progname
, nbtypedefs
);
2085 /* Read the structures. */
2087 read_state_structures (type_p
*structures
)
2090 type_p previous
= NULL
;
2092 int nbstruct
= 0, countstruct
= 0;
2093 struct state_token_st
*t0
= peek_state_token (0);
2094 struct state_token_st
*t1
= peek_state_token (1);
2095 struct state_token_st
*t2
= peek_state_token (2);
2097 if (state_token_kind (t0
) == STOK_LEFTPAR
2098 && state_token_is_name (t1
, "!structures")
2099 && state_token_kind (t2
) == STOK_INTEGER
)
2101 nbstruct
= t2
->stok_un
.stok_num
;
2102 next_state_tokens (3);
2103 t0
= peek_state_token (0);
2104 while (t0
&& state_token_kind (t0
) != STOK_RIGHTPAR
)
2107 read_state_type (&tmp
);
2116 previous
->next
= tmp
;
2119 t0
= peek_state_token (0);
2121 next_state_tokens (1);
2124 fatal_reading_state (t0
, "Bad structures syntax");
2125 if (countstruct
!= nbstruct
)
2126 fatal_reading_state_printf (NULL_STATE_TOKEN
,
2127 "expected %d structures but got %d",
2128 nbstruct
, countstruct
);
2129 if (verbosity_level
>= 2)
2130 printf ("%s read %d structures from state\n", progname
, nbstruct
);
2135 /* Read the param_struct-s. */
2137 read_state_param_structs (type_p
*param_structs
)
2139 int nbparamstructs
= 0;
2140 int countparamstructs
= 0;
2142 type_p previous
= NULL
;
2144 struct state_token_st
*t0
= peek_state_token (0);
2145 struct state_token_st
*t1
= peek_state_token (1);
2146 struct state_token_st
*t2
= peek_state_token (2);
2148 if (state_token_kind (t0
) == STOK_LEFTPAR
2149 && state_token_is_name (t1
, "!param_structs")
2150 && state_token_kind (t2
) == STOK_INTEGER
)
2152 nbparamstructs
= t2
->stok_un
.stok_num
;
2153 next_state_tokens (3);
2154 t0
= t1
= t2
= NULL
;
2155 t0
= peek_state_token (0);
2156 while (state_token_kind (t0
) != STOK_RIGHTPAR
)
2159 read_state_type (&tmp
);
2167 previous
->next
= tmp
;
2170 t0
= peek_state_token (0);
2171 countparamstructs
++;
2173 next_state_tokens (1);
2176 fatal_reading_state (t0
, "Bad param_structs syntax");
2177 t0
= peek_state_token (0);
2178 if (countparamstructs
!= nbparamstructs
)
2179 fatal_reading_state_printf
2181 "invalid number of param_structs expected %d got %d",
2182 nbparamstructs
, countparamstructs
);
2183 *param_structs
= head
;
2187 /* Read the variables. */
2189 read_state_variables (pair_p
*variables
)
2193 struct state_token_st
*t0
= peek_state_token (0);
2194 struct state_token_st
*t1
= peek_state_token (1);
2195 struct state_token_st
*t2
= peek_state_token (2);
2197 if (state_token_kind (t0
) == STOK_LEFTPAR
2198 && state_token_is_name (t1
, "!variables")
2199 && state_token_kind (t2
) == STOK_INTEGER
)
2202 nbvars
= t2
->stok_un
.stok_num
;
2203 next_state_tokens (3);
2204 nbpairs
= read_state_pair_list (&list
);
2205 t0
= peek_state_token (0);
2206 if (nbpairs
!= nbvars
)
2207 fatal_reading_state_printf
2208 (t0
, "Invalid number of variables, expected %d but got %d",
2210 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
2211 next_state_tokens (1);
2213 fatal_reading_state (t0
, "Waiting for ) in variables");
2216 fatal_reading_state (t0
, "Bad variables syntax");
2218 if (verbosity_level
>= 2)
2219 printf ("%s read %d variables from state\n", progname
, nbvars
);
2223 /* Read the source directory. */
2225 read_state_srcdir (void)
2227 struct state_token_st
*t0
= peek_state_token (0);
2228 struct state_token_st
*t1
= peek_state_token (1);
2229 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2230 state_token_is_name (t1
, "!srcdir"))
2232 next_state_tokens (2);
2233 t0
= peek_state_token (0);
2234 t1
= peek_state_token (1);
2235 if (state_token_kind (t0
) == STOK_STRING
&&
2236 state_token_kind (t1
) == STOK_RIGHTPAR
)
2238 srcdir
= xstrdup (t0
->stok_un
.stok_string
);
2239 srcdir_len
= strlen (srcdir
);
2240 next_state_tokens (2);
2245 fatal_reading_state (t0
, "Bad srcdir in state_file");
2249 /* Read the sequence of GCC front-end languages. */
2251 read_state_languages (void)
2253 struct state_token_st
*t0
= peek_state_token (0);
2254 struct state_token_st
*t1
= peek_state_token (1);
2255 struct state_token_st
*t2
= peek_state_token (2);
2256 if (state_token_kind (t0
) == STOK_LEFTPAR
2257 && state_token_is_name (t1
, "!languages")
2258 && state_token_kind (t2
) == STOK_INTEGER
)
2261 num_lang_dirs
= t2
->stok_un
.stok_num
;
2262 lang_dir_names
= XCNEWVEC (const char *, num_lang_dirs
);
2263 next_state_tokens (3);
2264 t0
= t1
= t2
= NULL
;
2265 for (i
= 0; i
< (int) num_lang_dirs
; i
++)
2267 t0
= peek_state_token (0);
2268 if (state_token_kind (t0
) != STOK_NAME
)
2269 fatal_reading_state (t0
, "expecting language name in state file");
2270 lang_dir_names
[i
] = t0
->stok_un
.stok_ident
->stid_name
;
2271 next_state_tokens (1);
2273 t0
= peek_state_token (0);
2274 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
2275 fatal_reading_state (t0
, "missing ) in languages list of state file");
2276 next_state_tokens (1);
2279 fatal_reading_state (t0
, "expecting languages list in state file");
2283 /* Read the sequence of files. */
2285 read_state_files_list (void)
2287 struct state_token_st
*t0
= peek_state_token (0);
2288 struct state_token_st
*t1
= peek_state_token (1);
2289 struct state_token_st
*t2
= peek_state_token (2);
2291 if (state_token_kind (t0
) == STOK_LEFTPAR
2292 && state_token_is_name (t1
, "!fileslist")
2293 && state_token_kind (t2
) == STOK_INTEGER
)
2296 num_gt_files
= t2
->stok_un
.stok_num
;
2297 next_state_tokens (3);
2298 t0
= t1
= t2
= NULL
;
2299 gt_files
= XCNEWVEC (const input_file
*, num_gt_files
);
2300 for (i
= 0; i
< (int) num_gt_files
; i
++)
2302 bool issrcfile
= FALSE
;
2303 t0
= t1
= t2
= NULL
;
2304 t0
= peek_state_token (0);
2305 t1
= peek_state_token (1);
2306 t2
= peek_state_token (2);
2307 if (state_token_kind (t0
) == STOK_LEFTPAR
2308 && (state_token_is_name (t1
, "!file")
2309 || (issrcfile
= state_token_is_name (t1
, "!srcfile")))
2310 && state_token_kind (t2
) == STOK_INTEGER
)
2312 lang_bitmap bmap
= t2
->stok_un
.stok_num
;
2313 next_state_tokens (3);
2314 t0
= t1
= t2
= NULL
;
2315 t0
= peek_state_token (0);
2316 t1
= peek_state_token (1);
2317 if (state_token_kind (t0
) == STOK_STRING
2318 && state_token_kind (t1
) == STOK_RIGHTPAR
)
2320 const char *fnam
= t0
->stok_un
.stok_string
;
2321 /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
2322 input_file
*curgt
= NULL
;
2325 static const char dirsepstr
[2] =
2326 { DIR_SEPARATOR
, (char) 0 };
2327 char *fullpath
= concat (srcdir
, dirsepstr
, fnam
, NULL
);
2328 curgt
= input_file_by_name (fullpath
);
2332 curgt
= input_file_by_name (fnam
);
2333 set_lang_bitmap (curgt
, bmap
);
2334 gt_files
[i
] = curgt
;
2335 next_state_tokens (2);
2338 fatal_reading_state (t0
,
2339 "bad file in !fileslist of state file");
2342 fatal_reading_state (t0
,
2343 "expecting file in !fileslist of state file");
2345 t0
= peek_state_token (0);
2346 if (!state_token_kind (t0
) == STOK_RIGHTPAR
)
2347 fatal_reading_state (t0
, "missing ) for !fileslist in state file");
2348 next_state_tokens (1);
2351 fatal_reading_state (t0
, "missing !fileslist in state file");
2355 /* Read the trailer. */
2357 read_state_trailer (void)
2359 struct state_token_st
*t0
= peek_state_token (0);
2360 struct state_token_st
*t1
= peek_state_token (1);
2361 struct state_token_st
*t2
= peek_state_token (2);
2363 if (state_token_kind (t0
) == STOK_LEFTPAR
2364 && state_token_is_name (t1
, "!endfile")
2365 && state_token_kind (t2
) == STOK_RIGHTPAR
)
2366 next_state_tokens (3);
2368 fatal_reading_state (t0
, "missing !endfile in state file");
2372 /* Utility functions for the state_seen_types hash table. */
2374 hash_type_number (const void *ty
)
2376 const struct type
*type
= (const struct type
*) ty
;
2378 return type
->state_number
;
2382 equals_type_number (const void *ty1
, const void *ty2
)
2384 const struct type
*type1
= (const struct type
*) ty1
;
2385 const struct type
*type2
= (const struct type
*) ty2
;
2387 return type1
->state_number
== type2
->state_number
;
2391 string_eq (const void *a
, const void *b
)
2393 const char *a0
= (const char *)a
;
2394 const char *b0
= (const char *)b
;
2396 return (strcmp (a0
, b0
) == 0);
2400 /* The function reading the state, called by main from gengtype.c. */
2402 read_state (const char *path
)
2404 state_file
= fopen (path
, "r");
2405 if (state_file
== NULL
)
2406 fatal ("Failed to open state file %s for reading [%s]", path
,
2411 if (verbosity_level
>= 1)
2413 printf ("%s reading state file %s;", progname
, state_path
);
2414 if (verbosity_level
>= 2)
2420 htab_create (2017, hash_type_number
, equals_type_number
, NULL
);
2422 htab_create (4027, htab_hash_string
, string_eq
, NULL
);
2423 read_state_version (version_string
);
2424 read_state_srcdir ();
2425 read_state_languages ();
2426 read_state_files_list ();
2427 read_state_structures (&structures
);
2428 if (ferror (state_file
))
2429 fatal_reading_state_printf
2430 (NULL_STATE_TOKEN
, "input error while reading state [%s]",
2432 read_state_typedefs (&typedefs
);
2433 read_state_param_structs (¶m_structs
);
2434 read_state_variables (&variables
);
2435 read_state_trailer ();
2437 if (verbosity_level
>= 1)
2439 printf ("%s read %ld bytes.\n", progname
, ftell (state_file
));
2443 if (fclose (state_file
))
2444 fatal ("failed to close read state file %s [%s]",
2445 path
, xstrerror (errno
));
2450 /* End of file gengtype-state.c. */