1 /* Gengtype persistent state serialization & de-serialization.
2 Useful for gengtype in plugin mode.
4 Copyright (C) 2010-2013 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>.
22 Contributed by Jeremie Salvucci <jeremie.salvucci@free.fr>
23 and Basile Starynkevitch <basile@starynkevitch.net>
32 #include "errors.h" /* For fatal. */
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 case TYPE_USER_STRUCT
:
56 return CONST_CAST (struct fileloc
*, &ty
->u
.s
.line
);
57 case TYPE_PARAM_STRUCT
:
58 return CONST_CAST (struct fileloc
*, &ty
->u
.param_struct
.line
);
69 /* The state file has simplistic lispy lexical tokens. Its lexer gives
70 a linked list of struct state_token_st, through the peek_state_token
71 function. Lexical tokens are consumed with next_state_tokens. */
74 /* The lexical kind of each lispy token. */
77 STOK_NONE
, /* Never used. */
78 STOK_INTEGER
, /* Integer token. */
79 STOK_STRING
, /* String token. */
80 STOK_LEFTPAR
, /* Left opening parenthesis. */
81 STOK_RIGHTPAR
, /* Right closing parenthesis. */
82 STOK_NAME
/* hash-consed name or identifier. */
86 /* Structure and hash-table used to share identifiers or names. */
89 /* TODO: We could improve the parser by reserving identifiers for
90 state keywords and adding a keyword number for them. That would
91 mean adding another field in this state_ident_st struct. */
92 char stid_name
[1]; /* actually bigger & null terminated */
94 static htab_t state_ident_tab
;
97 /* The state_token_st structure is for lexical tokens in the read
98 state file. The stok_kind field discriminates the union. Tokens
99 are allocated by peek_state_token which calls read_a_state_token
100 which allocate them. Tokens are freed by calls to
101 next_state_tokens. Token are organized in a FIFO look-ahead queue
102 filled by peek_state_token. */
103 struct state_token_st
105 enum state_token_en stok_kind
; /* the lexical kind
106 discriminates the stok_un
108 int stok_line
; /* the line number */
109 int stok_col
; /* the column number */
110 const char *stok_file
; /* the file path */
111 struct state_token_st
*stok_next
; /* the next token in the
112 queue, when peeked */
113 union /* discriminated by stok_kind! */
115 int stok_num
; /* when STOK_INTEGER */
116 char stok_string
[1]; /* when STOK_STRING, actual size is
117 bigger and null terminated */
118 struct state_ident_st
*stok_ident
; /* when STOK_IDENT */
119 void *stok_ptr
; /* null otherwise */
127 #define NULL_STATE_TOKEN (struct state_token_st*)0
129 /* the state_token pointer contains the leftmost current token. The
130 tokens are organized in a linked queue, using stok_next, for token
132 struct state_token_st
*state_token
= NULL_STATE_TOKEN
;
134 /* Used by the reading lexer. */
135 static FILE *state_file
;
136 static const char *state_path
= NULL
;
137 static int state_line
= 0;
138 static long state_bol
= 0; /* offset of beginning of line */
141 /* Counter of written types. */
142 static int state_written_type_count
= 0;
145 /* Fatal error messages when reading the state. They are extremely
146 unlikely, and only appear when this gengtype-state.c file is buggy,
147 or when reading a gengtype state which was not generated by the
148 same version of gengtype or GCC. */
151 /* Fatal message while reading state. */
153 fatal_reading_state (struct state_token_st
* tok
, const char*msg
)
156 fatal ("%s:%d:%d: Invalid state file; %s",
157 tok
->stok_file
, tok
->stok_line
, tok
->stok_col
,
160 fatal ("%s:%d: Invalid state file; %s",
161 state_path
, state_line
, msg
);
165 /* Fatal printf-like message while reading state. This can't be a
166 function, because there is no way to pass a va_arg to a variant of
168 #define fatal_reading_state_printf(Tok,Fmt,...) do { \
169 struct state_token_st* badtok = Tok; \
171 fatal ("%s:%d:%d: Invalid state file; " Fmt, \
174 badtok->stok_col, __VA_ARGS__); \
176 fatal ("%s:%d: Invalid state file; " Fmt, \
177 state_path, state_line, __VA_ARGS__); \
181 /* Find or allocate an identifier in our name hash table. */
182 static struct state_ident_st
*
183 state_ident_by_name (const char *name
, enum insert_option optins
)
187 struct state_ident_st
*stid
= NULL
;
189 if (!name
|| !name
[0])
192 slot
= htab_find_slot (state_ident_tab
, name
, optins
);
196 namlen
= strlen (name
);
198 (struct state_ident_st
*) xmalloc (sizeof (struct state_ident_st
) +
200 memset (stid
, 0, sizeof (struct state_ident_st
) + namlen
);
201 strcpy (stid
->stid_name
, name
);
207 /* Our token lexer is heavily inspired by MELT's lexer, and share some
208 code with the file gcc/melt-runtime.c of the GCC MELT branch! We
209 really want the gengtype state to be easily parsable by MELT. This
210 is a usual lispy lexing routine, dealing with spaces and comments,
211 numbers, parenthesis, names, strings. */
212 static struct state_token_st
*
213 read_a_state_token (void)
217 struct state_token_st
*tk
= NULL
;
219 again
: /* Read again, e.g. after a comment or spaces. */
220 c
= getc (state_file
);
224 /* Handle spaces, count lines. */
228 state_bol
= curoff
= ftell (state_file
);
233 /* Skip comments starting with semi-colon. */
238 c
= getc (state_file
);
240 while (c
> 0 && c
!= '\n');
244 state_bol
= curoff
= ftell (state_file
);
248 /* Read signed numbers. */
249 if (ISDIGIT (c
) || c
== '-' || c
== '+')
252 ungetc (c
, state_file
);
253 curoff
= ftell (state_file
);
254 if (fscanf (state_file
, "%d", &n
) <= 0)
255 fatal_reading_state (NULL_STATE_TOKEN
, "Lexical error in number");
256 tk
= XCNEW (struct state_token_st
);
257 tk
->stok_kind
= STOK_INTEGER
;
258 tk
->stok_line
= state_line
;
259 tk
->stok_col
= curoff
- state_bol
;
260 tk
->stok_file
= state_path
;
261 tk
->stok_next
= NULL
;
262 tk
->stok_un
.stok_num
= n
;
266 /* Read an opening left parenthesis. */
269 curoff
= ftell (state_file
);
270 tk
= XCNEW (struct state_token_st
);
271 tk
->stok_kind
= STOK_LEFTPAR
;
272 tk
->stok_line
= state_line
;
273 tk
->stok_col
= curoff
- state_bol
;
274 tk
->stok_file
= state_path
;
275 tk
->stok_next
= NULL
;
279 /* Read an closing right parenthesis. */
282 curoff
= ftell (state_file
);
283 tk
= XCNEW (struct state_token_st
);
284 tk
->stok_kind
= STOK_RIGHTPAR
;
285 tk
->stok_line
= state_line
;
286 tk
->stok_col
= curoff
- state_bol
;
287 tk
->stok_file
= state_path
;
288 tk
->stok_next
= NULL
;
292 /* Read identifiers, using an obstack. */
293 else if (ISALPHA (c
) || c
== '_' || c
== '$' || c
== '!' || c
== '#')
295 struct obstack id_obstack
;
296 struct state_ident_st
*sid
= NULL
;
298 obstack_init (&id_obstack
);
299 curoff
= ftell (state_file
);
300 while (ISALNUM (c
) || c
== '_' || c
== '$' || c
== '!' || c
== '#')
302 obstack_1grow (&id_obstack
, c
);
303 c
= getc (state_file
);
308 ungetc (c
, state_file
);
309 obstack_1grow (&id_obstack
, (char) 0);
310 ids
= XOBFINISH (&id_obstack
, char *);
311 sid
= state_ident_by_name (ids
, INSERT
);
312 obstack_free (&id_obstack
, NULL
);
314 tk
= XCNEW (struct state_token_st
);
315 tk
->stok_kind
= STOK_NAME
;
316 tk
->stok_line
= state_line
;
317 tk
->stok_col
= curoff
- state_bol
;
318 tk
->stok_file
= state_path
;
319 tk
->stok_next
= NULL
;
320 tk
->stok_un
.stok_ident
= sid
;
324 /* Read a string, dealing with escape sequences a la C! */
329 struct obstack bstring_obstack
;
330 obstack_init (&bstring_obstack
);
331 curoff
= ftell (state_file
);
332 while ((c
= getc (state_file
)) != '"' && c
>= 0)
334 if (ISPRINT (c
) && c
!= '\\')
335 obstack_1grow (&bstring_obstack
, (char) c
);
336 else if (ISSPACE (c
) && c
!= '\n')
337 obstack_1grow (&bstring_obstack
, (char) c
);
340 c
= getc (state_file
);
344 obstack_1grow (&bstring_obstack
, '\a');
345 c
= getc (state_file
);
348 obstack_1grow (&bstring_obstack
, '\b');
349 c
= getc (state_file
);
352 obstack_1grow (&bstring_obstack
, '\t');
353 c
= getc (state_file
);
356 obstack_1grow (&bstring_obstack
, '\n');
357 c
= getc (state_file
);
360 obstack_1grow (&bstring_obstack
, '\v');
361 c
= getc (state_file
);
364 obstack_1grow (&bstring_obstack
, '\f');
365 c
= getc (state_file
);
368 obstack_1grow (&bstring_obstack
, '\r');
369 c
= getc (state_file
);
372 obstack_1grow (&bstring_obstack
, '\"');
373 c
= getc (state_file
);
376 obstack_1grow (&bstring_obstack
, '\\');
377 c
= getc (state_file
);
380 obstack_1grow (&bstring_obstack
, ' ');
381 c
= getc (state_file
);
386 if (fscanf (state_file
, "%02x", &cx
) > 0 && cx
> 0)
387 obstack_1grow (&bstring_obstack
, cx
);
391 "Lexical error in string hex escape");
392 c
= getc (state_file
);
398 "Lexical error - unknown string escape");
402 fatal_reading_state (NULL_STATE_TOKEN
, "Lexical error...");
405 fatal_reading_state (NULL_STATE_TOKEN
, "Unterminated string");
406 obstack_1grow (&bstring_obstack
, '\0');
407 cstr
= XOBFINISH (&bstring_obstack
, char *);
408 cslen
= strlen (cstr
);
409 tk
= (struct state_token_st
*)
410 xcalloc (sizeof (struct state_token_st
) + cslen
, 1);
411 tk
->stok_kind
= STOK_STRING
;
412 tk
->stok_line
= state_line
;
413 tk
->stok_col
= curoff
- state_bol
;
414 tk
->stok_file
= state_path
;
415 tk
->stok_next
= NULL
;
416 strcpy (tk
->stok_un
.stok_string
, cstr
);
417 obstack_free (&bstring_obstack
, NULL
);
421 /* Got an unexpected character. */
422 fatal_reading_state_printf
424 "Lexical error at offset %ld - bad character \\%03o = '%c'",
425 ftell (state_file
), c
, c
);
428 /* Used for lexical look-ahead. Retrieves the lexical token of rank
429 DEPTH, starting with 0 when reading the state file. Gives null on
431 static struct state_token_st
*
432 peek_state_token (int depth
)
434 int remdepth
= depth
;
435 struct state_token_st
**ptoken
= &state_token
;
436 struct state_token_st
*tok
= NULL
;
438 while (remdepth
>= 0)
442 *ptoken
= tok
= read_a_state_token ();
447 ptoken
= &((*ptoken
)->stok_next
);
454 /* Consume the next DEPTH tokens and free them. */
456 next_state_tokens (int depth
)
458 struct state_token_st
*n
;
462 if (state_token
!= NULL
)
464 n
= state_token
->stok_next
;
469 fatal_reading_state (NULL_STATE_TOKEN
, "Tokens stack empty");
475 /* Safely retrieve the lexical kind of a token. */
476 static inline enum state_token_en
477 state_token_kind (struct state_token_st
*p
)
485 /* Test if a token is a given name i.e. an identifier. */
487 state_token_is_name (struct state_token_st
*p
, const char *name
)
492 if (p
->stok_kind
!= STOK_NAME
)
495 return !strcmp (p
->stok_un
.stok_ident
->stid_name
, name
);
499 /* Following routines are useful for serializing datas.
501 * We want to serialize :
504 * - param_structs list
507 * So, we have one routine for each kind of data. The main writing
508 * routine is write_state. The main reading routine is
509 * read_state. Most writing routines write_state_FOO have a
510 * corresponding reading routine read_state_FOO. Reading is done in a
511 * recursive descending way, and any read error is fatal.
514 /* When reading the state, we need to remember the previously seen
515 types by their state_number, since GTY-ed types are usually
517 static htab_t state_seen_types
;
519 /* Return the length of a linked list made of pairs. */
520 static int pair_list_length (pair_p list
);
523 static void write_state_pair (pair_p
);
525 /* return the number of pairs written. Should match the length given
526 by pair_list_length. */
527 static int write_state_pair_list (pair_p list
);
529 /* Write a type. When a type is written, its state_number is updated,
530 to ensure that a "reference" to a seen type is written on next
532 static void write_state_type (type_p
);
534 /* Write a null-terminatel string using our Lispy lexical conventions,
535 similar to those of C or MELT. */
536 static void write_state_a_string (const char *s
);
538 /* Compute the length of a list of pairs, starting from the first
541 pair_list_length (pair_p list
)
545 for (l
= list
; l
; l
= l
->next
)
550 /* Write a file location. Files relative to $(srcdir) are quite
551 frequent and are handled specially. This ensures that two gengtype
552 state file-s produced by gengtype on the same GCC source tree are
553 very similar and can be reasonably compared with diff, even if the
554 two GCC source trees have different absolute paths. */
556 write_state_fileloc (struct fileloc
*floc
)
559 if (floc
!= NULL
&& floc
->line
> 0)
561 const char *srcrelpath
= NULL
;
562 gcc_assert (floc
->file
!= NULL
);
563 /* Most of the files are inside $(srcdir) so it is worth to
564 handle them specially. */
565 srcrelpath
= get_file_srcdir_relative_path (floc
->file
);
566 if (srcrelpath
!= NULL
)
568 fprintf (state_file
, "\n(!srcfileloc ");
569 write_state_a_string (srcrelpath
);
573 fprintf (state_file
, "\n(!fileloc ");
574 write_state_a_string (get_input_file_name (floc
->file
));
576 fprintf (state_file
, " %d", floc
->line
);
577 fprintf (state_file
, ")\n");
580 fprintf (state_file
, "nil ");
583 /* Write a list of fields. */
585 write_state_fields (pair_p fields
)
587 int nbfields
= pair_list_length (fields
);
589 fprintf (state_file
, "\n(!fields %d ", nbfields
);
590 nbpairs
= write_state_pair_list (fields
);
591 gcc_assert (nbpairs
== nbfields
);
592 fprintf (state_file
, ")\n");
595 /* Write a null-terminated string in our lexical convention, very
596 similar to the convention of C. */
598 write_state_a_string (const char *s
)
602 fputs (" \"", state_file
);
609 fputs ("\\a", state_file
);
612 fputs ("\\b", state_file
);
615 fputs ("\\t", state_file
);
618 fputs ("\\n", state_file
);
621 fputs ("\\v", state_file
);
624 fputs ("\\f", state_file
);
627 fputs ("\\r", state_file
);
630 fputs ("\\\"", state_file
);
633 fputs ("\\\\", state_file
);
637 putc (c
, state_file
);
639 fprintf (state_file
, "\\x%02x", (unsigned) c
);
642 fputs ("\"", state_file
);
645 /* Our option-s have three kinds, each with its writer. */
647 write_state_string_option (options_p current
)
649 fprintf (state_file
, "string ");
650 if (current
->info
.string
!= NULL
)
651 write_state_a_string (current
->info
.string
);
653 fprintf (state_file
, " nil ");
657 write_state_type_option (options_p current
)
659 fprintf (state_file
, "type ");
660 write_state_type (current
->info
.type
);
664 write_state_nested_option (options_p current
)
666 fprintf (state_file
, "nested ");
667 write_state_type (current
->info
.nested
->type
);
668 if (current
->info
.nested
->convert_from
!= NULL
)
669 write_state_a_string (current
->info
.nested
->convert_from
);
671 fprintf (state_file
, " nil ");
673 if (current
->info
.nested
->convert_to
!= NULL
)
674 write_state_a_string (current
->info
.nested
->convert_to
);
676 fprintf (state_file
, " nil ");
680 write_state_option (options_p current
)
682 fprintf (state_file
, "\n(!option ");
684 if (current
->name
!= NULL
)
685 fprintf (state_file
, "%s ", current
->name
);
687 fprintf (state_file
, "nil ");
689 switch (current
->kind
)
692 write_state_string_option (current
);
695 write_state_type_option (current
);
698 write_state_nested_option (current
);
701 fatal ("Option tag unknown");
704 fprintf (state_file
, ")\n");
709 /* Write a list of GTY options. */
711 write_state_options (options_p opt
)
717 fprintf (state_file
, "nil ");
721 fprintf (state_file
, "\n(!options ");
722 for (current
= opt
; current
!= NULL
; current
= current
->next
)
723 write_state_option (current
);
724 fprintf (state_file
, ")\n");
728 /* Write a bitmap representing a set of GCC front-end languages. */
730 write_state_lang_bitmap (lang_bitmap bitmap
)
732 fprintf (state_file
, "%d ", (int) bitmap
);
735 /* Write version information. */
737 write_state_version (const char *version
)
739 fprintf (state_file
, "\n(!version ");
740 write_state_a_string (version
);
741 fprintf (state_file
, ")\n");
744 /* Common routine to write the common content of all types. */
745 static void write_state_common_type_content (type_p current
);
747 /* Write a scalar type. We have only two of these. */
749 write_state_scalar_type (type_p current
)
751 if (current
== &scalar_nonchar
)
752 fprintf (state_file
, "scalar_nonchar ");
753 else if (current
== &scalar_char
)
754 fprintf (state_file
, "scalar_char ");
756 fatal ("Unexpected type in write_state_scalar_type");
758 write_state_common_type_content (current
);
761 /* Write the string type. There is only one such thing! */
763 write_state_string_type (type_p current
)
765 if (current
== &string_type
)
767 fprintf (state_file
, "string ");
768 write_state_common_type_content (current
);
771 fatal ("Unexpected type in write_state_string_type");
774 /* Write an undefined type. */
776 write_state_undefined_type (type_p current
)
778 DBGPRINTF ("undefined type @ %p #%d '%s'", (void *) current
,
779 current
->state_number
, current
->u
.s
.tag
);
780 fprintf (state_file
, "undefined ");
781 gcc_assert (current
->gc_used
== GC_UNUSED
);
782 write_state_common_type_content (current
);
783 if (current
->u
.s
.tag
!= NULL
)
784 write_state_a_string (current
->u
.s
.tag
);
786 fprintf (state_file
, "nil");
788 write_state_fileloc (type_lineloc (current
));
792 /* Common code to write structure like types. */
794 write_state_struct_union_type (type_p current
, const char *kindstr
)
796 DBGPRINTF ("%s type @ %p #%d '%s'", kindstr
, (void *) current
,
797 current
->state_number
, current
->u
.s
.tag
);
798 fprintf (state_file
, "%s ", kindstr
);
799 write_state_common_type_content (current
);
800 if (current
->u
.s
.tag
!= NULL
)
801 write_state_a_string (current
->u
.s
.tag
);
803 fprintf (state_file
, "nil");
805 write_state_fileloc (type_lineloc (current
));
806 write_state_fields (current
->u
.s
.fields
);
807 write_state_options (current
->u
.s
.opt
);
808 write_state_lang_bitmap (current
->u
.s
.bitmap
);
812 /* Write a GTY struct type. */
814 write_state_struct_type (type_p current
)
816 write_state_struct_union_type (current
, "struct");
817 write_state_type (current
->u
.s
.lang_struct
);
820 /* Write a GTY user-defined struct type. */
822 write_state_user_struct_type (type_p current
)
824 DBGPRINTF ("user_struct type @ %p #%d '%s'", (void *) current
,
825 current
->state_number
, current
->u
.s
.tag
);
826 fprintf (state_file
, "user_struct ");
827 write_state_common_type_content (current
);
828 if (current
->u
.s
.tag
!= NULL
)
829 write_state_a_string (current
->u
.s
.tag
);
831 fprintf (state_file
, "nil");
832 write_state_fileloc (type_lineloc (current
));
833 write_state_fields (current
->u
.s
.fields
);
836 /* write a GTY union type. */
838 write_state_union_type (type_p current
)
840 write_state_struct_union_type (current
, "union");
841 write_state_type (current
->u
.s
.lang_struct
);
844 /* Write a lang_struct type. This is tricky and was painful to debug,
845 we deal with the next field specifically within their lang_struct
846 subfield, which points to a linked list of homonumous types.
847 Change this function with extreme care, see also
848 read_state_lang_struct_type. */
850 write_state_lang_struct_type (type_p current
)
854 const char *homoname
= 0;
855 write_state_struct_union_type (current
, "lang_struct");
856 /* lang_struct-ures are particularly tricky, since their
857 u.s.lang_struct field gives a list of homonymous struct-s or
859 DBGPRINTF ("lang_struct @ %p #%d", (void *) current
, current
->state_number
);
860 for (hty
= current
->u
.s
.lang_struct
; hty
!= NULL
; hty
= hty
->next
)
863 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype
,
864 (void *) hty
, hty
->state_number
, hty
->u
.s
.tag
);
865 /* Every member of the homonymous list should have the same tag. */
866 gcc_assert (union_or_struct_p (hty
));
867 gcc_assert (hty
->u
.s
.lang_struct
== current
);
869 homoname
= hty
->u
.s
.tag
;
870 gcc_assert (strcmp (homoname
, hty
->u
.s
.tag
) == 0);
872 fprintf (state_file
, "(!homotypes %d\n", nbhomontype
);
873 for (hty
= current
->u
.s
.lang_struct
; hty
!= NULL
; hty
= hty
->next
)
874 write_state_type (hty
);
875 fprintf (state_file
, ")\n");
878 /* Write a parametrized structure GTY type. */
880 write_state_param_struct_type (type_p current
)
884 fprintf (state_file
, "param_struct ");
885 write_state_common_type_content (current
);
886 write_state_type (current
->u
.param_struct
.stru
);
887 for (i
= 0; i
< NUM_PARAM
; i
++)
889 if (current
->u
.param_struct
.param
[i
] != NULL
)
890 write_state_type (current
->u
.param_struct
.param
[i
]);
892 fprintf (state_file
, "nil ");
894 write_state_fileloc (¤t
->u
.param_struct
.line
);
897 /* Write a pointer type. */
899 write_state_pointer_type (type_p current
)
901 fprintf (state_file
, "pointer ");
902 write_state_common_type_content (current
);
903 write_state_type (current
->u
.p
);
906 /* Write an array type. */
908 write_state_array_type (type_p current
)
910 fprintf (state_file
, "array ");
911 write_state_common_type_content (current
);
912 if (current
->u
.a
.len
!= NULL
)
913 write_state_a_string (current
->u
.a
.len
);
915 fprintf (state_file
, " nil");
917 fprintf (state_file
, " ");
918 write_state_type (current
->u
.a
.p
);
921 /* Write the gc_used information. */
923 write_state_gc_used (enum gc_used_enum gus
)
928 fprintf (state_file
, " gc_unused");
931 fprintf (state_file
, " gc_used");
933 case GC_MAYBE_POINTED_TO
:
934 fprintf (state_file
, " gc_maybe_pointed_to");
937 fprintf (state_file
, " gc_pointed_to");
944 /* Utility routine to write the common content of all types. Notice
945 that the next field is *not* written on purpose. */
947 write_state_common_type_content (type_p current
)
949 fprintf (state_file
, "%d ", current
->state_number
);
950 /* We do not write the next type, because list of types are
951 explicitly written. However, lang_struct are special in that
952 respect. See function write_state_lang_struct_type for more. */
953 write_state_type (current
->pointer_to
);
954 write_state_gc_used (current
->gc_used
);
958 /* The important and recursive routine writing GTY types as understood
959 by gengtype. Types which have a positive state_number have already
960 been seen and written. */
962 write_state_type (type_p current
)
966 fprintf (state_file
, "nil ");
970 fprintf (state_file
, "\n(!type ");
972 if (current
->state_number
> 0)
973 fprintf (state_file
, "already_seen %d", current
->state_number
);
976 state_written_type_count
++;
977 DBGPRINTF ("writing type #%d @%p old number %d", state_written_type_count
,
978 (void *) current
, current
->state_number
);
979 current
->state_number
= state_written_type_count
;
980 switch (current
->kind
)
985 write_state_undefined_type (current
);
988 write_state_struct_type (current
);
990 case TYPE_USER_STRUCT
:
991 write_state_user_struct_type (current
);
994 write_state_union_type (current
);
997 write_state_pointer_type (current
);
1000 write_state_array_type (current
);
1002 case TYPE_LANG_STRUCT
:
1003 write_state_lang_struct_type (current
);
1005 case TYPE_PARAM_STRUCT
:
1006 write_state_param_struct_type (current
);
1009 write_state_scalar_type (current
);
1012 write_state_string_type (current
);
1017 fprintf (state_file
, ")\n");
1023 write_state_pair (pair_p current
)
1025 if (current
== NULL
)
1027 fprintf (state_file
, "nil)");
1031 fprintf (state_file
, "\n(!pair ");
1033 if (current
->name
!= NULL
)
1034 write_state_a_string (current
->name
);
1036 write_state_a_string ("nil");
1038 write_state_type (current
->type
);
1039 write_state_fileloc (&(current
->line
));
1040 write_state_options (current
->opt
);
1042 fprintf (state_file
, ")");
1045 /* Write a pair list and return the number of pairs written. */
1047 write_state_pair_list (pair_p list
)
1052 for (current
= list
; current
!= NULL
; current
= current
->next
)
1054 write_state_pair (current
);
1061 /* When writing imported linked lists, like typedefs, structures,
1062 param_structs, ... we count their length first and write it. These
1063 eases the reading, and enables an extra verification on the number
1064 of actually read items. */
1066 /* Write our typedefs. */
1068 write_state_typedefs (void)
1070 int nbtypedefs
= pair_list_length (typedefs
);
1072 fprintf (state_file
, "\n(!typedefs %d\n", nbtypedefs
);
1073 nbpairs
= write_state_pair_list (typedefs
);
1074 gcc_assert (nbpairs
== nbtypedefs
);
1075 fprintf (state_file
, ")\n");
1076 if (verbosity_level
>= 2)
1077 printf ("%s wrote %d typedefs\n", progname
, nbtypedefs
);
1080 /* Write our structures. */
1082 write_state_structures (void)
1087 for (current
= structures
; current
!= NULL
; current
= current
->next
)
1090 fprintf (state_file
, "\n(!structures %d\n", nbstruct
);
1092 for (current
= structures
; current
!= NULL
; current
= current
->next
)
1093 write_state_type (current
);
1095 fprintf (state_file
, ")\n");
1096 if (verbosity_level
>= 2)
1097 printf ("%s wrote %d structures in state\n", progname
, nbstruct
);
1100 /* Write our param_struct-s. */
1102 write_state_param_structs (void)
1104 int nbparamstruct
= 0;
1107 for (current
= param_structs
; current
!= NULL
; current
= current
->next
)
1110 fprintf (state_file
, "\n(!param_structs %d\n", nbparamstruct
);
1112 for (current
= param_structs
; current
!= NULL
; current
= current
->next
)
1113 write_state_type (current
);
1115 fprintf (state_file
, ")\n");
1118 /* Write our variables. */
1120 write_state_variables (void)
1122 int nbvars
= pair_list_length (variables
);
1124 fprintf (state_file
, "\n(!variables %d\n", nbvars
);
1125 nbpairs
= write_state_pair_list (variables
);
1126 gcc_assert (nbpairs
== nbvars
);
1127 fprintf (state_file
, ")\n");
1128 if (verbosity_level
>= 2)
1129 printf ("%s wrote %d variables.\n", progname
, nbvars
);
1132 /* Write the source directory. File locations within the source
1133 directory have been written specifically. */
1135 write_state_srcdir (void)
1137 fprintf (state_file
, "\n(!srcdir ");
1138 write_state_a_string (srcdir
);
1139 fprintf (state_file
, ")\n");
1142 /* Count and write the list of our files. */
1144 write_state_files_list (void)
1147 /* Write the list of files with their lang_bitmap. */
1148 fprintf (state_file
, "\n(!fileslist %d\n", (int) num_gt_files
);
1149 for (i
= 0; i
< (int) num_gt_files
; i
++)
1151 const char *cursrcrelpath
= NULL
;
1152 const input_file
*curfil
= gt_files
[i
];
1153 /* Most of the files are inside $(srcdir) so it is worth to
1154 handle them specially. */
1155 cursrcrelpath
= get_file_srcdir_relative_path (curfil
);
1158 fprintf (state_file
, "(!srcfile %d ", get_lang_bitmap (curfil
));
1159 write_state_a_string (cursrcrelpath
);
1163 fprintf (state_file
, "(!file %d ", get_lang_bitmap (curfil
));
1164 write_state_a_string (get_input_file_name (curfil
));
1166 fprintf (state_file
, ")\n");
1168 fprintf (state_file
, ")\n");
1171 /* Write the list of GCC front-end languages. */
1173 write_state_languages (void)
1176 fprintf (state_file
, "\n(!languages %d", (int) num_lang_dirs
);
1177 for (i
= 0; i
< (int) num_lang_dirs
; i
++)
1179 /* Languages names are identifiers, we expect only letters or
1180 underscores or digits in them. In particular, C++ is not a
1181 valid language name, but cp is valid. */
1182 fprintf (state_file
, " %s", lang_dir_names
[i
]);
1184 fprintf (state_file
, ")\n");
1187 /* Write the trailer. */
1189 write_state_trailer (void)
1191 /* This test should probably catch IO errors like disk full... */
1192 if (fputs ("\n(!endfile)\n", state_file
) == EOF
)
1193 fatal ("failed to write state trailer [%s]", xstrerror (errno
));
1196 /* The write_state routine is the only writing routine called by main
1197 in gengtype.c. To avoid messing the state if gengtype is
1198 interrupted or aborted, we write a temporary file and rename it
1199 after having written it in totality. */
1201 write_state (const char *state_path
)
1205 char *temp_state_path
= NULL
;
1206 char tempsuffix
[40];
1209 /* We write a unique temporary file which is renamed when complete
1210 * only. So even if gengtype is interrupted, the written state file
1211 * won't be partially written, since the temporary file is not yet
1212 * renamed in that case. */
1213 memset (tempsuffix
, 0, sizeof (tempsuffix
));
1214 snprintf (tempsuffix
, sizeof (tempsuffix
) - 1, "-%ld-%d.tmp", (long) now
,
1216 temp_state_path
= concat (state_path
, tempsuffix
, NULL
);
1217 state_file
= fopen (temp_state_path
, "w");
1218 if (state_file
== NULL
)
1219 fatal ("Failed to open file %s for writing state: %s",
1220 temp_state_path
, xstrerror (errno
));
1221 if (verbosity_level
>= 3)
1222 printf ("%s writing state file %s temporarily in %s\n",
1223 progname
, state_path
, temp_state_path
);
1224 /* This is the first line of the state. Perhaps the file utility
1225 could know about that, so don't change it often. */
1226 fprintf (state_file
, ";;;;@@@@ GCC gengtype state\n");
1227 /* Output a few comments for humans. */
1228 fprintf (state_file
,
1229 ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
1230 fprintf (state_file
,
1231 ";;; The format of this file is tied to a particular version of GCC.\n");
1232 fprintf (state_file
,
1233 ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1234 fprintf (state_file
,
1235 ";;; This file should be parsed by the same %s which wrote it.\n",
1237 /* The first non-comment significant line gives the version string. */
1238 write_state_version (version_string
);
1239 write_state_srcdir ();
1240 write_state_languages ();
1241 write_state_files_list ();
1242 write_state_structures ();
1243 write_state_typedefs ();
1244 write_state_param_structs ();
1245 write_state_variables ();
1246 write_state_trailer ();
1247 statelen
= ftell (state_file
);
1248 if (ferror (state_file
))
1249 fatal ("output error when writing state file %s [%s]",
1250 temp_state_path
, xstrerror (errno
));
1251 if (fclose (state_file
))
1252 fatal ("failed to close state file %s [%s]",
1253 temp_state_path
, xstrerror (errno
));
1254 if (rename (temp_state_path
, state_path
))
1255 fatal ("failed to rename %s to state file %s [%s]", temp_state_path
,
1256 state_path
, xstrerror (errno
));
1257 free (temp_state_path
);
1259 if (verbosity_level
>= 1)
1260 printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1261 progname
, state_path
, statelen
, state_written_type_count
);
1265 /** End of writing routines! The corresponding reading routines follow. **/
1269 /* Forward declarations, since some read_state_* functions are
1271 static void read_state_fileloc (struct fileloc
*line
);
1272 static void read_state_options (options_p
*opt
);
1273 static void read_state_type (type_p
*current
);
1274 static void read_state_pair (pair_p
*pair
);
1275 /* Return the number of pairs actually read. */
1276 static int read_state_pair_list (pair_p
*list
);
1277 static void read_state_fields (pair_p
*fields
);
1278 static void read_state_common_type_content (type_p current
);
1283 /* Record into the state_seen_types hash-table a type which we are
1284 reading, to enable recursive or circular references to it. */
1286 record_type (type_p type
)
1290 slot
= htab_find_slot (state_seen_types
, type
, INSERT
);
1296 /* Read an already seen type. */
1298 read_state_already_seen_type (type_p
*type
)
1300 struct state_token_st
*t0
= peek_state_token (0);
1302 if (state_token_kind (t0
) == STOK_INTEGER
)
1305 struct type loctype
= { TYPE_SCALAR
, 0, 0, 0, GC_UNUSED
, {0} };
1307 loctype
.state_number
= t0
->stok_un
.stok_num
;
1308 slot
= htab_find_slot (state_seen_types
, &loctype
, NO_INSERT
);
1311 fatal_reading_state (t0
, "Unknown type");
1314 next_state_tokens (1);
1315 *type
= (type_p
) *slot
;
1319 fatal_reading_state (t0
, "Bad seen type");
1324 /* Read the scalar_nonchar type. */
1326 read_state_scalar_nonchar_type (type_p
*type
)
1328 *type
= &scalar_nonchar
;
1329 read_state_common_type_content (*type
);
1333 /* Read the scalar_char type. */
1335 read_state_scalar_char_type (type_p
*type
)
1337 *type
= &scalar_char
;
1338 read_state_common_type_content (*type
);
1341 /* Read the string_type. */
1343 read_state_string_type (type_p
*type
)
1345 *type
= &string_type
;
1346 read_state_common_type_content (*type
);
1350 /* Read a lang_bitmap representing a set of GCC front-end languages. */
1352 read_state_lang_bitmap (lang_bitmap
*bitmap
)
1354 struct state_token_st
*t
;
1356 t
= peek_state_token (0);
1357 if (state_token_kind (t
) == STOK_INTEGER
)
1359 *bitmap
= t
->stok_un
.stok_num
;
1360 next_state_tokens (1);
1364 fatal_reading_state (t
, "Bad syntax for bitmap");
1369 /* Read an undefined type. */
1371 read_state_undefined_type (type_p type
)
1373 struct state_token_st
*t0
;
1375 type
->kind
= TYPE_UNDEFINED
;
1376 read_state_common_type_content (type
);
1377 t0
= peek_state_token (0);
1378 if (state_token_kind (t0
) == STOK_STRING
)
1380 if (state_token_is_name (t0
, "nil"))
1382 type
->u
.s
.tag
= NULL
;
1383 DBGPRINTF ("read anonymous undefined type @%p #%d",
1384 (void *) type
, type
->state_number
);
1388 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1389 DBGPRINTF ("read undefined type @%p #%d '%s'",
1390 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1393 next_state_tokens (1);
1394 read_state_fileloc (&(type
->u
.s
.line
));
1398 fatal_reading_state (t0
, "Bad tag in undefined type");
1403 /* Read a GTY-ed struct type. */
1405 read_state_struct_type (type_p type
)
1407 struct state_token_st
*t0
;
1409 type
->kind
= TYPE_STRUCT
;
1410 read_state_common_type_content (type
);
1411 t0
= peek_state_token (0);
1412 if (state_token_kind (t0
) == STOK_STRING
)
1414 if (state_token_is_name (t0
, "nil"))
1416 type
->u
.s
.tag
= NULL
;
1417 DBGPRINTF ("read anonymous struct type @%p #%d",
1418 (void *) type
, type
->state_number
);
1422 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1423 DBGPRINTF ("read struct type @%p #%d '%s'",
1424 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1427 next_state_tokens (1);
1428 read_state_fileloc (&(type
->u
.s
.line
));
1429 read_state_fields (&(type
->u
.s
.fields
));
1430 read_state_options (&(type
->u
.s
.opt
));
1431 read_state_lang_bitmap (&(type
->u
.s
.bitmap
));
1432 read_state_type (&(type
->u
.s
.lang_struct
));
1436 fatal_reading_state (t0
, "Bad tag in struct type");
1441 /* Read a GTY-ed user-provided struct TYPE. */
1444 read_state_user_struct_type (type_p type
)
1446 struct state_token_st
*t0
;
1448 type
->kind
= TYPE_USER_STRUCT
;
1449 read_state_common_type_content (type
);
1450 t0
= peek_state_token (0);
1451 if (state_token_kind (t0
) == STOK_STRING
)
1453 if (state_token_is_name (t0
, "nil"))
1455 type
->u
.s
.tag
= NULL
;
1456 DBGPRINTF ("read anonymous struct type @%p #%d",
1457 (void *) type
, type
->state_number
);
1461 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1462 DBGPRINTF ("read struct type @%p #%d '%s'",
1463 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1466 next_state_tokens (1);
1467 read_state_fileloc (&(type
->u
.s
.line
));
1468 read_state_fields (&(type
->u
.s
.fields
));
1472 fatal_reading_state (t0
, "Bad tag in user-struct type");
1477 /* Read a GTY-ed union type. */
1479 read_state_union_type (type_p type
)
1481 struct state_token_st
*t0
;
1483 type
->kind
= TYPE_UNION
;
1484 read_state_common_type_content (type
);
1485 t0
= peek_state_token (0);
1486 if (state_token_kind (t0
) == STOK_STRING
)
1488 if (state_token_is_name (t0
, "nil"))
1490 type
->u
.s
.tag
= NULL
;
1491 DBGPRINTF ("read anonymous union type @%p #%d",
1492 (void *) type
, type
->state_number
);
1496 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1497 DBGPRINTF ("read union type @%p #%d '%s'",
1498 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1500 next_state_tokens (1);
1501 read_state_fileloc (&(type
->u
.s
.line
));
1502 read_state_fields (&(type
->u
.s
.fields
));
1503 read_state_options (&(type
->u
.s
.opt
));
1504 read_state_lang_bitmap (&(type
->u
.s
.bitmap
));
1505 read_state_type (&(type
->u
.s
.lang_struct
));
1508 fatal_reading_state (t0
, "Bad tag in union type");
1512 /* Read a GTY-ed pointer type. */
1514 read_state_pointer_type (type_p type
)
1516 type
->kind
= TYPE_POINTER
;
1517 read_state_common_type_content (type
);
1518 DBGPRINTF ("read pointer type @%p #%d", (void *) type
, type
->state_number
);
1519 read_state_type (&(type
->u
.p
));
1523 /* Read a GTY-ed array type. */
1525 read_state_array_type (type_p type
)
1527 struct state_token_st
*t0
;
1529 type
->kind
= TYPE_ARRAY
;
1530 read_state_common_type_content (type
);
1531 t0
= peek_state_token (0);
1532 if (state_token_kind (t0
) == STOK_STRING
)
1534 type
->u
.a
.len
= xstrdup (t0
->stok_un
.stok_string
);
1535 DBGPRINTF ("read array type @%p #%d length '%s'",
1536 (void *) type
, type
->state_number
, type
->u
.a
.len
);
1537 next_state_tokens (1);
1540 else if (state_token_is_name (t0
, "nil"))
1542 type
->u
.a
.len
= NULL
;
1543 DBGPRINTF ("read array type @%p #%d without length",
1544 (void *) type
, type
->state_number
);
1545 next_state_tokens (1);
1549 fatal_reading_state (t0
, "Bad array name type");
1550 read_state_type (&(type
->u
.a
.p
));
1555 /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
1556 front-end languages. This is a tricky function and it was painful
1557 to debug. Change it with extreme care. See also
1558 write_state_lang_struct_type. */
1560 read_state_lang_struct_type (type_p type
)
1562 struct state_token_st
*t0
= NULL
;
1563 struct state_token_st
*t1
= NULL
;
1564 struct state_token_st
*t2
= NULL
;
1566 type
->kind
= TYPE_LANG_STRUCT
;
1567 read_state_common_type_content (type
);
1568 t0
= peek_state_token (0);
1569 if (state_token_kind (t0
) == STOK_STRING
)
1571 if (state_token_is_name (t0
, "nil"))
1573 DBGPRINTF ("read anonymous lang_struct type @%p #%d",
1574 (void *) type
, type
->state_number
);
1575 type
->u
.s
.tag
= NULL
;
1579 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1580 DBGPRINTF ("read lang_struct type @%p #%d '%s'",
1581 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1583 next_state_tokens (1);
1586 fatal_reading_state (t0
, "Bad tag in lang struct type");
1587 read_state_fileloc (&(type
->u
.s
.line
));
1588 read_state_fields (&(type
->u
.s
.fields
));
1589 read_state_options (&(type
->u
.s
.opt
));
1590 read_state_lang_bitmap (&(type
->u
.s
.bitmap
));
1591 /* Within lang_struct-ures, the lang_struct field is a linked list
1592 of homonymous types! */
1593 t0
= peek_state_token (0);
1594 t1
= peek_state_token (1);
1595 t2
= peek_state_token (2);
1596 /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
1597 if (state_token_kind (t0
) == STOK_LEFTPAR
1598 && state_token_is_name (t1
, "!homotypes")
1599 && state_token_kind (t2
) == STOK_INTEGER
)
1601 type_p
*prevty
= &type
->u
.s
.lang_struct
;
1602 int nbhomotype
= t2
->stok_un
.stok_num
;
1604 t0
= t1
= t2
= NULL
;
1605 next_state_tokens (3);
1606 for (i
= 0; i
< nbhomotype
; i
++)
1608 read_state_type (prevty
);
1609 t0
= peek_state_token (0);
1611 prevty
= &(*prevty
)->next
;
1613 fatal_reading_state (t0
,
1614 "expecting type in homotype list for lang_struct");
1616 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1617 fatal_reading_state (t0
,
1618 "expecting ) in homotype list for lang_struct");
1619 next_state_tokens (1);
1622 fatal_reading_state (t0
, "expecting !homotypes for lang_struct");
1626 /* Read a param_struct type for GTY parametrized structures. */
1628 read_state_param_struct_type (type_p type
)
1631 struct state_token_st
*t0
;
1633 type
->kind
= TYPE_PARAM_STRUCT
;
1634 read_state_common_type_content (type
);
1635 DBGPRINTF ("read param_struct type @%p #%d",
1636 (void *) type
, type
->state_number
);
1637 read_state_type (&(type
->u
.param_struct
.stru
));
1639 for (i
= 0; i
< NUM_PARAM
; i
++)
1641 t0
= peek_state_token (0);
1642 if (state_token_is_name (t0
, "nil"))
1644 type
->u
.param_struct
.param
[i
] = NULL
;
1645 next_state_tokens (1);
1648 read_state_type (&(type
->u
.param_struct
.param
[i
]));
1650 read_state_fileloc (&(type
->u
.param_struct
.line
));
1654 /* Read the gc used information. */
1656 read_state_gc_used (enum gc_used_enum
*pgus
)
1658 struct state_token_st
*t0
= peek_state_token (0);
1659 if (state_token_is_name (t0
, "gc_unused"))
1661 else if (state_token_is_name (t0
, "gc_used"))
1663 else if (state_token_is_name (t0
, "gc_maybe_pointed_to"))
1664 *pgus
= GC_MAYBE_POINTED_TO
;
1665 else if (state_token_is_name (t0
, "gc_pointed_to"))
1666 *pgus
= GC_POINTED_TO
;
1668 fatal_reading_state (t0
, "invalid gc_used information");
1669 next_state_tokens (1);
1673 /* Utility function to read the common content of types. */
1675 read_state_common_type_content (type_p current
)
1677 struct state_token_st
*t0
= peek_state_token (0);
1679 if (state_token_kind (t0
) == STOK_INTEGER
)
1681 current
->state_number
= t0
->stok_un
.stok_num
;
1682 next_state_tokens (1);
1683 record_type (current
);
1686 fatal_reading_state_printf (t0
,
1687 "Expected integer for state_number line %d",
1689 /* We don't read the next field of the type. */
1690 read_state_type (¤t
->pointer_to
);
1691 read_state_gc_used (¤t
->gc_used
);
1695 /* Read a GTY-ed type. */
1697 read_state_type (type_p
*current
)
1699 struct state_token_st
*t0
= peek_state_token (0);
1700 struct state_token_st
*t1
= peek_state_token (1);
1702 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1703 state_token_is_name (t1
, "!type"))
1705 next_state_tokens (2);
1706 t0
= peek_state_token (0);
1707 if (state_token_is_name (t0
, "already_seen"))
1709 next_state_tokens (1);
1710 read_state_already_seen_type (current
);
1714 t0
= peek_state_token (0);
1716 if (state_token_is_name (t0
, "scalar_nonchar"))
1718 next_state_tokens (1);
1719 read_state_scalar_nonchar_type (current
);
1721 else if (state_token_is_name (t0
, "scalar_char"))
1723 next_state_tokens (1);
1724 read_state_scalar_char_type (current
);
1726 else if (state_token_is_name (t0
, "string"))
1728 next_state_tokens (1);
1729 read_state_string_type (current
);
1731 else if (state_token_is_name (t0
, "undefined"))
1733 *current
= XCNEW (struct type
);
1734 next_state_tokens (1);
1735 read_state_undefined_type (*current
);
1737 else if (state_token_is_name (t0
, "struct"))
1739 *current
= XCNEW (struct type
);
1740 next_state_tokens (1);
1741 read_state_struct_type (*current
);
1743 else if (state_token_is_name (t0
, "union"))
1745 *current
= XCNEW (struct type
);
1746 next_state_tokens (1);
1747 read_state_union_type (*current
);
1749 else if (state_token_is_name (t0
, "lang_struct"))
1751 *current
= XCNEW (struct type
);
1752 next_state_tokens (1);
1753 read_state_lang_struct_type (*current
);
1755 else if (state_token_is_name (t0
, "param_struct"))
1757 *current
= XCNEW (struct type
);
1758 next_state_tokens (1);
1759 read_state_param_struct_type (*current
);
1761 else if (state_token_is_name (t0
, "pointer"))
1763 *current
= XCNEW (struct type
);
1764 next_state_tokens (1);
1765 read_state_pointer_type (*current
);
1767 else if (state_token_is_name (t0
, "array"))
1769 *current
= XCNEW (struct type
);
1770 next_state_tokens (1);
1771 read_state_array_type (*current
);
1773 else if (state_token_is_name (t0
, "user_struct"))
1775 *current
= XCNEW (struct type
);
1776 next_state_tokens (1);
1777 read_state_user_struct_type (*current
);
1780 fatal_reading_state (t0
, "bad type in (!type");
1782 t0
= peek_state_token (0);
1783 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1784 fatal_reading_state (t0
, "missing ) in type");
1785 next_state_tokens (1);
1787 else if (state_token_is_name (t0
, "nil"))
1789 next_state_tokens (1);
1793 fatal_reading_state (t0
, "bad type syntax");
1797 /* Read a file location. Files within the source directory are dealt
1798 with specifically. */
1800 read_state_fileloc (struct fileloc
*floc
)
1802 bool issrcfile
= false;
1803 struct state_token_st
*t0
= peek_state_token (0);
1804 struct state_token_st
*t1
= peek_state_token (1);
1806 gcc_assert (floc
!= NULL
);
1807 gcc_assert (srcdir
!= NULL
);
1809 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1810 (state_token_is_name (t1
, "!fileloc")
1811 || (issrcfile
= state_token_is_name (t1
, "!srcfileloc"))))
1813 next_state_tokens (2);
1814 t0
= peek_state_token (0);
1815 t1
= peek_state_token (1);
1816 if (state_token_kind (t0
) == STOK_STRING
&&
1817 state_token_kind (t1
) == STOK_INTEGER
)
1819 char *path
= t0
->stok_un
.stok_string
;
1822 static const char dirsepstr
[2] = { DIR_SEPARATOR
, (char) 0 };
1823 char *fullpath
= concat (srcdir
, dirsepstr
, path
, NULL
);
1824 floc
->file
= input_file_by_name (fullpath
);
1828 floc
->file
= input_file_by_name (path
);
1829 floc
->line
= t1
->stok_un
.stok_num
;
1830 next_state_tokens (2);
1833 fatal_reading_state (t0
,
1834 "Bad fileloc syntax, expected path string and line");
1835 t0
= peek_state_token (0);
1836 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1837 fatal_reading_state (t0
, "Bad fileloc syntax, expected )");
1838 next_state_tokens (1);
1840 else if (state_token_is_name (t0
, "nil"))
1842 next_state_tokens (1);
1847 fatal_reading_state (t0
, "Bad fileloc syntax");
1851 /* Read the fields of a GTY-ed type. */
1853 read_state_fields (pair_p
*fields
)
1856 struct state_token_st
*t0
= peek_state_token (0);
1857 struct state_token_st
*t1
= peek_state_token (1);
1858 struct state_token_st
*t2
= peek_state_token (2);
1860 if (state_token_kind (t0
) == STOK_LEFTPAR
1861 && state_token_is_name (t1
, "!fields")
1862 && state_token_kind (t2
) == STOK_INTEGER
)
1864 int nbfields
= t2
->stok_un
.stok_num
;
1866 next_state_tokens (3);
1867 nbpairs
= read_state_pair_list (&tmp
);
1868 t0
= peek_state_token (0);
1869 if (nbpairs
!= nbfields
)
1870 fatal_reading_state_printf
1872 "Mismatched fields number, expected %d got %d", nbpairs
, nbfields
);
1873 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
1874 next_state_tokens (1);
1876 fatal_reading_state (t0
, "Bad fields expecting )");
1883 /* Read a string option. */
1885 read_state_string_option (options_p opt
)
1887 struct state_token_st
*t0
= peek_state_token (0);
1888 opt
->kind
= OPTION_STRING
;
1889 if (state_token_kind (t0
) == STOK_STRING
)
1891 opt
->info
.string
= xstrdup (t0
->stok_un
.stok_string
);
1892 next_state_tokens (1);
1894 else if (state_token_is_name (t0
, "nil"))
1896 opt
->info
.string
= NULL
;
1897 next_state_tokens (1);
1900 fatal_reading_state (t0
, "Missing name in string option");
1904 /* Read a type option. */
1906 read_state_type_option (options_p opt
)
1908 opt
->kind
= OPTION_TYPE
;
1909 read_state_type (&(opt
->info
.type
));
1913 /* Read a nested option. */
1915 read_state_nested_option (options_p opt
)
1917 struct state_token_st
*t0
;
1919 opt
->info
.nested
= XCNEW (struct nested_ptr_data
);
1920 opt
->kind
= OPTION_NESTED
;
1921 read_state_type (&(opt
->info
.nested
->type
));
1922 t0
= peek_state_token (0);
1923 if (state_token_kind (t0
) == STOK_STRING
)
1925 opt
->info
.nested
->convert_from
= xstrdup (t0
->stok_un
.stok_string
);
1926 next_state_tokens (1);
1928 else if (state_token_is_name (t0
, "nil"))
1930 opt
->info
.nested
->convert_from
= NULL
;
1931 next_state_tokens (1);
1934 fatal_reading_state (t0
, "Bad nested convert_from option");
1936 t0
= peek_state_token (0);
1937 if (state_token_kind (t0
) == STOK_STRING
)
1939 opt
->info
.nested
->convert_to
= xstrdup (t0
->stok_un
.stok_string
);
1940 next_state_tokens (1);
1942 else if (state_token_is_name (t0
, "nil"))
1944 opt
->info
.nested
->convert_to
= NULL
;
1945 next_state_tokens (1);
1948 fatal_reading_state (t0
, "Bad nested convert_from option");
1952 /* Read an GTY option. */
1954 read_state_option (options_p
*opt
)
1956 struct state_token_st
*t0
= peek_state_token (0);
1957 struct state_token_st
*t1
= peek_state_token (1);
1959 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1960 state_token_is_name (t1
, "!option"))
1962 next_state_tokens (2);
1963 t0
= peek_state_token (0);
1964 if (state_token_kind (t0
) == STOK_NAME
)
1966 *opt
= XCNEW (struct options
);
1967 if (state_token_is_name (t0
, "nil"))
1968 (*opt
)->name
= NULL
;
1970 (*opt
)->name
= t0
->stok_un
.stok_ident
->stid_name
;
1971 next_state_tokens (1);
1972 t0
= peek_state_token (0);
1973 if (state_token_kind (t0
) == STOK_NAME
)
1975 if (state_token_is_name (t0
, "string"))
1977 next_state_tokens (1);
1978 read_state_string_option (*opt
);
1980 else if (state_token_is_name (t0
, "type"))
1982 next_state_tokens (1);
1983 read_state_type_option (*opt
);
1985 else if (state_token_is_name (t0
, "nested"))
1987 next_state_tokens (1);
1988 read_state_nested_option (*opt
);
1991 fatal_reading_state (t0
, "Bad option type");
1992 t0
= peek_state_token (0);
1993 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1994 fatal_reading_state (t0
, "Bad syntax in option, expecting )");
1996 next_state_tokens (1);
1999 fatal_reading_state (t0
, "Missing option type");
2002 fatal_reading_state (t0
, "Bad name for option");
2005 fatal_reading_state (t0
, "Bad option, waiting for )");
2008 /* Read a list of options. */
2010 read_state_options (options_p
*opt
)
2012 options_p head
= NULL
;
2013 options_p previous
= NULL
;
2014 options_p current_option
= NULL
;
2015 struct state_token_st
*t0
= peek_state_token (0);
2016 struct state_token_st
*t1
= peek_state_token (1);
2018 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2019 state_token_is_name (t1
, "!options"))
2021 next_state_tokens (2);
2022 t0
= peek_state_token (0);
2023 while (state_token_kind (t0
) != STOK_RIGHTPAR
)
2025 read_state_option (¤t_option
);
2028 head
= current_option
;
2033 previous
->next
= current_option
;
2034 previous
= current_option
;
2036 t0
= peek_state_token (0);
2038 next_state_tokens (1);
2040 else if (state_token_is_name (t0
, "nil"))
2042 next_state_tokens (1);
2045 fatal_reading_state (t0
, "Bad options syntax");
2051 /* Read a version, and check against the version of the gengtype. */
2053 read_state_version (const char *version_string
)
2055 struct state_token_st
*t0
= peek_state_token (0);
2056 struct state_token_st
*t1
= peek_state_token (1);
2058 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2059 state_token_is_name (t1
, "!version"))
2061 next_state_tokens (2);
2062 t0
= peek_state_token (0);
2063 t1
= peek_state_token (1);
2064 if (state_token_kind (t0
) == STOK_STRING
&&
2065 state_token_kind (t1
) == STOK_RIGHTPAR
)
2067 /* Check that the read version string is the same as current
2069 if (strcmp (version_string
, t0
->stok_un
.stok_string
))
2070 fatal_reading_state_printf (t0
,
2071 "version string mismatch; expecting %s but got %s",
2073 t0
->stok_un
.stok_string
);
2074 next_state_tokens (2);
2077 fatal_reading_state (t0
, "Missing version or right parenthesis");
2080 fatal_reading_state (t0
, "Bad version syntax");
2086 read_state_pair (pair_p
*current
)
2088 struct state_token_st
*t0
= peek_state_token (0);
2089 struct state_token_st
*t1
= peek_state_token (1);
2090 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2091 state_token_is_name (t1
, "!pair"))
2093 *current
= XCNEW (struct pair
);
2094 next_state_tokens (2);
2095 t0
= peek_state_token (0);
2096 if (state_token_kind (t0
) == STOK_STRING
)
2098 if (strcmp (t0
->stok_un
.stok_string
, "nil") == 0)
2100 (*current
)->name
= NULL
;
2104 (*current
)->name
= xstrdup (t0
->stok_un
.stok_string
);
2106 next_state_tokens (1);
2107 read_state_type (&((*current
)->type
));
2108 read_state_fileloc (&((*current
)->line
));
2109 read_state_options (&((*current
)->opt
));;
2110 t0
= peek_state_token (0);
2111 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
2113 next_state_tokens (1);
2117 fatal_reading_state (t0
, "Bad syntax for pair, )");
2122 fatal_reading_state (t0
, "Bad name for pair");
2125 else if (state_token_kind (t0
) == STOK_NAME
&&
2126 state_token_is_name (t0
, "nil"))
2128 next_state_tokens (1);
2132 fatal_reading_state_printf (t0
, "Bad syntax for pair, (!pair %d",
2133 state_token
->stok_kind
);
2137 /* Return the number of pairs actually read. */
2139 read_state_pair_list (pair_p
*list
)
2143 pair_p previous
= NULL
;
2145 struct state_token_st
*t0
= peek_state_token (0);
2146 while (t0
&& state_token_kind (t0
) != STOK_RIGHTPAR
)
2148 read_state_pair (&tmp
);
2156 previous
->next
= tmp
;
2159 t0
= peek_state_token (0);
2163 /* don't consume the ); the caller will eat it. */
2168 /* Read the typedefs. */
2170 read_state_typedefs (pair_p
*typedefs
)
2174 struct state_token_st
*t0
= peek_state_token (0);
2175 struct state_token_st
*t1
= peek_state_token (1);
2176 struct state_token_st
*t2
= peek_state_token (2);
2178 if (state_token_kind (t0
) == STOK_LEFTPAR
2179 && state_token_is_name (t1
, "!typedefs")
2180 && state_token_kind (t2
) == STOK_INTEGER
)
2183 nbtypedefs
= t2
->stok_un
.stok_num
;
2184 next_state_tokens (3);
2185 nbpairs
= read_state_pair_list (&list
);
2186 t0
= peek_state_token (0);
2187 if (nbpairs
!= nbtypedefs
)
2188 fatal_reading_state_printf
2190 "invalid number of typedefs, expected %d but got %d",
2191 nbtypedefs
, nbpairs
);
2192 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
2193 next_state_tokens (1);
2195 fatal_reading_state (t0
, "Bad typedefs syntax )");
2198 fatal_reading_state (t0
, "Bad typedefs syntax (!typedefs");
2200 if (verbosity_level
>= 2)
2201 printf ("%s read %d typedefs from state\n", progname
, nbtypedefs
);
2206 /* Read the structures. */
2208 read_state_structures (type_p
*structures
)
2211 type_p previous
= NULL
;
2213 int nbstruct
= 0, countstruct
= 0;
2214 struct state_token_st
*t0
= peek_state_token (0);
2215 struct state_token_st
*t1
= peek_state_token (1);
2216 struct state_token_st
*t2
= peek_state_token (2);
2218 if (state_token_kind (t0
) == STOK_LEFTPAR
2219 && state_token_is_name (t1
, "!structures")
2220 && state_token_kind (t2
) == STOK_INTEGER
)
2222 nbstruct
= t2
->stok_un
.stok_num
;
2223 next_state_tokens (3);
2224 t0
= peek_state_token (0);
2225 while (t0
&& state_token_kind (t0
) != STOK_RIGHTPAR
)
2228 read_state_type (&tmp
);
2237 previous
->next
= tmp
;
2240 t0
= peek_state_token (0);
2242 next_state_tokens (1);
2245 fatal_reading_state (t0
, "Bad structures syntax");
2246 if (countstruct
!= nbstruct
)
2247 fatal_reading_state_printf (NULL_STATE_TOKEN
,
2248 "expected %d structures but got %d",
2249 nbstruct
, countstruct
);
2250 if (verbosity_level
>= 2)
2251 printf ("%s read %d structures from state\n", progname
, nbstruct
);
2256 /* Read the param_struct-s. */
2258 read_state_param_structs (type_p
*param_structs
)
2260 int nbparamstructs
= 0;
2261 int countparamstructs
= 0;
2263 type_p previous
= NULL
;
2265 struct state_token_st
*t0
= peek_state_token (0);
2266 struct state_token_st
*t1
= peek_state_token (1);
2267 struct state_token_st
*t2
= peek_state_token (2);
2269 if (state_token_kind (t0
) == STOK_LEFTPAR
2270 && state_token_is_name (t1
, "!param_structs")
2271 && state_token_kind (t2
) == STOK_INTEGER
)
2273 nbparamstructs
= t2
->stok_un
.stok_num
;
2274 next_state_tokens (3);
2275 t0
= t1
= t2
= NULL
;
2276 t0
= peek_state_token (0);
2277 while (state_token_kind (t0
) != STOK_RIGHTPAR
)
2280 read_state_type (&tmp
);
2288 previous
->next
= tmp
;
2291 t0
= peek_state_token (0);
2292 countparamstructs
++;
2294 next_state_tokens (1);
2297 fatal_reading_state (t0
, "Bad param_structs syntax");
2298 t0
= peek_state_token (0);
2299 if (countparamstructs
!= nbparamstructs
)
2300 fatal_reading_state_printf
2302 "invalid number of param_structs expected %d got %d",
2303 nbparamstructs
, countparamstructs
);
2304 *param_structs
= head
;
2308 /* Read the variables. */
2310 read_state_variables (pair_p
*variables
)
2314 struct state_token_st
*t0
= peek_state_token (0);
2315 struct state_token_st
*t1
= peek_state_token (1);
2316 struct state_token_st
*t2
= peek_state_token (2);
2318 if (state_token_kind (t0
) == STOK_LEFTPAR
2319 && state_token_is_name (t1
, "!variables")
2320 && state_token_kind (t2
) == STOK_INTEGER
)
2323 nbvars
= t2
->stok_un
.stok_num
;
2324 next_state_tokens (3);
2325 nbpairs
= read_state_pair_list (&list
);
2326 t0
= peek_state_token (0);
2327 if (nbpairs
!= nbvars
)
2328 fatal_reading_state_printf
2329 (t0
, "Invalid number of variables, expected %d but got %d",
2331 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
2332 next_state_tokens (1);
2334 fatal_reading_state (t0
, "Waiting for ) in variables");
2337 fatal_reading_state (t0
, "Bad variables syntax");
2339 if (verbosity_level
>= 2)
2340 printf ("%s read %d variables from state\n", progname
, nbvars
);
2344 /* Read the source directory. */
2346 read_state_srcdir (void)
2348 struct state_token_st
*t0
= peek_state_token (0);
2349 struct state_token_st
*t1
= peek_state_token (1);
2350 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2351 state_token_is_name (t1
, "!srcdir"))
2353 next_state_tokens (2);
2354 t0
= peek_state_token (0);
2355 t1
= peek_state_token (1);
2356 if (state_token_kind (t0
) == STOK_STRING
&&
2357 state_token_kind (t1
) == STOK_RIGHTPAR
)
2359 srcdir
= xstrdup (t0
->stok_un
.stok_string
);
2360 srcdir_len
= strlen (srcdir
);
2361 next_state_tokens (2);
2366 fatal_reading_state (t0
, "Bad srcdir in state_file");
2370 /* Read the sequence of GCC front-end languages. */
2372 read_state_languages (void)
2374 struct state_token_st
*t0
= peek_state_token (0);
2375 struct state_token_st
*t1
= peek_state_token (1);
2376 struct state_token_st
*t2
= peek_state_token (2);
2377 if (state_token_kind (t0
) == STOK_LEFTPAR
2378 && state_token_is_name (t1
, "!languages")
2379 && state_token_kind (t2
) == STOK_INTEGER
)
2382 num_lang_dirs
= t2
->stok_un
.stok_num
;
2383 lang_dir_names
= XCNEWVEC (const char *, num_lang_dirs
);
2384 next_state_tokens (3);
2385 t0
= t1
= t2
= NULL
;
2386 for (i
= 0; i
< (int) num_lang_dirs
; i
++)
2388 t0
= peek_state_token (0);
2389 if (state_token_kind (t0
) != STOK_NAME
)
2390 fatal_reading_state (t0
, "expecting language name in state file");
2391 lang_dir_names
[i
] = t0
->stok_un
.stok_ident
->stid_name
;
2392 next_state_tokens (1);
2394 t0
= peek_state_token (0);
2395 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
2396 fatal_reading_state (t0
, "missing ) in languages list of state file");
2397 next_state_tokens (1);
2400 fatal_reading_state (t0
, "expecting languages list in state file");
2404 /* Read the sequence of files. */
2406 read_state_files_list (void)
2408 struct state_token_st
*t0
= peek_state_token (0);
2409 struct state_token_st
*t1
= peek_state_token (1);
2410 struct state_token_st
*t2
= peek_state_token (2);
2412 if (state_token_kind (t0
) == STOK_LEFTPAR
2413 && state_token_is_name (t1
, "!fileslist")
2414 && state_token_kind (t2
) == STOK_INTEGER
)
2417 num_gt_files
= t2
->stok_un
.stok_num
;
2418 next_state_tokens (3);
2419 t0
= t1
= t2
= NULL
;
2420 gt_files
= XCNEWVEC (const input_file
*, num_gt_files
);
2421 for (i
= 0; i
< (int) num_gt_files
; i
++)
2423 bool issrcfile
= FALSE
;
2424 t0
= t1
= t2
= NULL
;
2425 t0
= peek_state_token (0);
2426 t1
= peek_state_token (1);
2427 t2
= peek_state_token (2);
2428 if (state_token_kind (t0
) == STOK_LEFTPAR
2429 && (state_token_is_name (t1
, "!file")
2430 || (issrcfile
= state_token_is_name (t1
, "!srcfile")))
2431 && state_token_kind (t2
) == STOK_INTEGER
)
2433 lang_bitmap bmap
= t2
->stok_un
.stok_num
;
2434 next_state_tokens (3);
2435 t0
= t1
= t2
= NULL
;
2436 t0
= peek_state_token (0);
2437 t1
= peek_state_token (1);
2438 if (state_token_kind (t0
) == STOK_STRING
2439 && state_token_kind (t1
) == STOK_RIGHTPAR
)
2441 const char *fnam
= t0
->stok_un
.stok_string
;
2442 /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
2443 input_file
*curgt
= NULL
;
2446 static const char dirsepstr
[2] =
2447 { DIR_SEPARATOR
, (char) 0 };
2448 char *fullpath
= concat (srcdir
, dirsepstr
, fnam
, NULL
);
2449 curgt
= input_file_by_name (fullpath
);
2453 curgt
= input_file_by_name (fnam
);
2454 set_lang_bitmap (curgt
, bmap
);
2455 gt_files
[i
] = curgt
;
2456 next_state_tokens (2);
2459 fatal_reading_state (t0
,
2460 "bad file in !fileslist of state file");
2463 fatal_reading_state (t0
,
2464 "expecting file in !fileslist of state file");
2466 t0
= peek_state_token (0);
2467 if (!state_token_kind (t0
) == STOK_RIGHTPAR
)
2468 fatal_reading_state (t0
, "missing ) for !fileslist in state file");
2469 next_state_tokens (1);
2472 fatal_reading_state (t0
, "missing !fileslist in state file");
2476 /* Read the trailer. */
2478 read_state_trailer (void)
2480 struct state_token_st
*t0
= peek_state_token (0);
2481 struct state_token_st
*t1
= peek_state_token (1);
2482 struct state_token_st
*t2
= peek_state_token (2);
2484 if (state_token_kind (t0
) == STOK_LEFTPAR
2485 && state_token_is_name (t1
, "!endfile")
2486 && state_token_kind (t2
) == STOK_RIGHTPAR
)
2487 next_state_tokens (3);
2489 fatal_reading_state (t0
, "missing !endfile in state file");
2493 /* Utility functions for the state_seen_types hash table. */
2495 hash_type_number (const void *ty
)
2497 const struct type
*type
= (const struct type
*) ty
;
2499 return type
->state_number
;
2503 equals_type_number (const void *ty1
, const void *ty2
)
2505 const struct type
*type1
= (const struct type
*) ty1
;
2506 const struct type
*type2
= (const struct type
*) ty2
;
2508 return type1
->state_number
== type2
->state_number
;
2512 string_eq (const void *a
, const void *b
)
2514 const char *a0
= (const char *)a
;
2515 const char *b0
= (const char *)b
;
2517 return (strcmp (a0
, b0
) == 0);
2521 /* The function reading the state, called by main from gengtype.c. */
2523 read_state (const char *path
)
2525 state_file
= fopen (path
, "r");
2526 if (state_file
== NULL
)
2527 fatal ("Failed to open state file %s for reading [%s]", path
,
2532 if (verbosity_level
>= 1)
2534 printf ("%s reading state file %s;", progname
, state_path
);
2535 if (verbosity_level
>= 2)
2541 htab_create (2017, hash_type_number
, equals_type_number
, NULL
);
2543 htab_create (4027, htab_hash_string
, string_eq
, NULL
);
2544 read_state_version (version_string
);
2545 read_state_srcdir ();
2546 read_state_languages ();
2547 read_state_files_list ();
2548 read_state_structures (&structures
);
2549 if (ferror (state_file
))
2550 fatal_reading_state_printf
2551 (NULL_STATE_TOKEN
, "input error while reading state [%s]",
2553 read_state_typedefs (&typedefs
);
2554 read_state_param_structs (¶m_structs
);
2555 read_state_variables (&variables
);
2556 read_state_trailer ();
2558 if (verbosity_level
>= 1)
2560 printf ("%s read %ld bytes.\n", progname
, ftell (state_file
));
2564 if (fclose (state_file
))
2565 fatal ("failed to close read state file %s [%s]",
2566 path
, xstrerror (errno
));
2571 /* End of file gengtype-state.c. */