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 case TYPE_USER_STRUCT
:
55 return CONST_CAST (struct fileloc
*, &ty
->u
.s
.line
);
56 case TYPE_PARAM_STRUCT
:
57 return CONST_CAST (struct fileloc
*, &ty
->u
.param_struct
.line
);
68 /* The state file has simplistic lispy lexical tokens. Its lexer gives
69 a linked list of struct state_token_st, through the peek_state_token
70 function. Lexical tokens are consumed with next_state_tokens. */
73 /* The lexical kind of each lispy token. */
76 STOK_NONE
, /* Never used. */
77 STOK_INTEGER
, /* Integer token. */
78 STOK_STRING
, /* String token. */
79 STOK_LEFTPAR
, /* Left opening parenthesis. */
80 STOK_RIGHTPAR
, /* Right closing parenthesis. */
81 STOK_NAME
/* hash-consed name or identifier. */
85 /* Structure and hash-table used to share identifiers or names. */
88 /* TODO: We could improve the parser by reserving identifiers for
89 state keywords and adding a keyword number for them. That would
90 mean adding another field in this state_ident_st struct. */
91 char stid_name
[1]; /* actually bigger & null terminated */
93 static htab_t state_ident_tab
;
96 /* The state_token_st structure is for lexical tokens in the read
97 state file. The stok_kind field discriminates the union. Tokens
98 are allocated by peek_state_token which calls read_a_state_token
99 which allocate them. Tokens are freed by calls to
100 next_state_tokens. Token are organized in a FIFO look-ahead queue
101 filled by peek_state_token. */
102 struct state_token_st
104 enum state_token_en stok_kind
; /* the lexical kind
105 discriminates the stok_un
107 int stok_line
; /* the line number */
108 int stok_col
; /* the column number */
109 const char *stok_file
; /* the file path */
110 struct state_token_st
*stok_next
; /* the next token in the
111 queue, when peeked */
112 union /* discriminated by stok_kind! */
114 int stok_num
; /* when STOK_INTEGER */
115 char stok_string
[1]; /* when STOK_STRING, actual size is
116 bigger and null terminated */
117 struct state_ident_st
*stok_ident
; /* when STOK_IDENT */
118 void *stok_ptr
; /* null otherwise */
126 #define NULL_STATE_TOKEN (struct state_token_st*)0
128 /* the state_token pointer contains the leftmost current token. The
129 tokens are organized in a linked queue, using stok_next, for token
131 struct state_token_st
*state_token
= NULL_STATE_TOKEN
;
133 /* Used by the reading lexer. */
134 static FILE *state_file
;
135 static const char *state_path
= NULL
;
136 static int state_line
= 0;
137 static long state_bol
= 0; /* offset of beginning of line */
140 /* Counter of written types. */
141 static int state_written_type_count
= 0;
144 /* Fatal error messages when reading the state. They are extremely
145 unlikely, and only appear when this gengtype-state.c file is buggy,
146 or when reading a gengtype state which was not generated by the
147 same version of gengtype or GCC. */
150 /* Fatal message while reading state. */
152 fatal_reading_state (struct state_token_st
* tok
, const char*msg
)
155 fatal ("%s:%d:%d: Invalid state file; %s",
156 tok
->stok_file
, tok
->stok_line
, tok
->stok_col
,
159 fatal ("%s:%d: Invalid state file; %s",
160 state_path
, state_line
, msg
);
164 /* Fatal printf-like message while reading state. This can't be a
165 function, because there is no way to pass a va_arg to a variant of
167 #define fatal_reading_state_printf(Tok,Fmt,...) do { \
168 struct state_token_st* badtok = Tok; \
170 fatal ("%s:%d:%d: Invalid state file; " Fmt, \
173 badtok->stok_col, __VA_ARGS__); \
175 fatal ("%s:%d: Invalid state file; " Fmt, \
176 state_path, state_line, __VA_ARGS__); \
180 /* Find or allocate an identifier in our name hash table. */
181 static struct state_ident_st
*
182 state_ident_by_name (const char *name
, enum insert_option optins
)
186 struct state_ident_st
*stid
= NULL
;
188 if (!name
|| !name
[0])
191 slot
= htab_find_slot (state_ident_tab
, name
, optins
);
195 namlen
= strlen (name
);
197 (struct state_ident_st
*) xmalloc (sizeof (struct state_ident_st
) +
199 memset (stid
, 0, sizeof (struct state_ident_st
) + namlen
);
200 strcpy (stid
->stid_name
, name
);
206 /* Our token lexer is heavily inspired by MELT's lexer, and share some
207 code with the file gcc/melt-runtime.c of the GCC MELT branch! We
208 really want the gengtype state to be easily parsable by MELT. This
209 is a usual lispy lexing routine, dealing with spaces and comments,
210 numbers, parenthesis, names, strings. */
211 static struct state_token_st
*
212 read_a_state_token (void)
216 struct state_token_st
*tk
= NULL
;
218 again
: /* Read again, e.g. after a comment or spaces. */
219 c
= getc (state_file
);
223 /* Handle spaces, count lines. */
227 state_bol
= curoff
= ftell (state_file
);
232 /* Skip comments starting with semi-colon. */
237 c
= getc (state_file
);
239 while (c
> 0 && c
!= '\n');
243 state_bol
= curoff
= ftell (state_file
);
247 /* Read signed numbers. */
248 if (ISDIGIT (c
) || c
== '-' || c
== '+')
251 ungetc (c
, state_file
);
252 curoff
= ftell (state_file
);
253 if (fscanf (state_file
, "%d", &n
) <= 0)
254 fatal_reading_state (NULL_STATE_TOKEN
, "Lexical error in number");
255 tk
= XCNEW (struct state_token_st
);
256 tk
->stok_kind
= STOK_INTEGER
;
257 tk
->stok_line
= state_line
;
258 tk
->stok_col
= curoff
- state_bol
;
259 tk
->stok_file
= state_path
;
260 tk
->stok_next
= NULL
;
261 tk
->stok_un
.stok_num
= n
;
265 /* Read an opening left parenthesis. */
268 curoff
= ftell (state_file
);
269 tk
= XCNEW (struct state_token_st
);
270 tk
->stok_kind
= STOK_LEFTPAR
;
271 tk
->stok_line
= state_line
;
272 tk
->stok_col
= curoff
- state_bol
;
273 tk
->stok_file
= state_path
;
274 tk
->stok_next
= NULL
;
278 /* Read an closing right parenthesis. */
281 curoff
= ftell (state_file
);
282 tk
= XCNEW (struct state_token_st
);
283 tk
->stok_kind
= STOK_RIGHTPAR
;
284 tk
->stok_line
= state_line
;
285 tk
->stok_col
= curoff
- state_bol
;
286 tk
->stok_file
= state_path
;
287 tk
->stok_next
= NULL
;
291 /* Read identifiers, using an obstack. */
292 else if (ISALPHA (c
) || c
== '_' || c
== '$' || c
== '!' || c
== '#')
294 struct obstack id_obstack
;
295 struct state_ident_st
*sid
= NULL
;
297 obstack_init (&id_obstack
);
298 curoff
= ftell (state_file
);
299 while (ISALNUM (c
) || c
== '_' || c
== '$' || c
== '!' || c
== '#')
301 obstack_1grow (&id_obstack
, c
);
302 c
= getc (state_file
);
307 ungetc (c
, state_file
);
308 obstack_1grow (&id_obstack
, (char) 0);
309 ids
= XOBFINISH (&id_obstack
, char *);
310 sid
= state_ident_by_name (ids
, INSERT
);
311 obstack_free (&id_obstack
, NULL
);
313 tk
= XCNEW (struct state_token_st
);
314 tk
->stok_kind
= STOK_NAME
;
315 tk
->stok_line
= state_line
;
316 tk
->stok_col
= curoff
- state_bol
;
317 tk
->stok_file
= state_path
;
318 tk
->stok_next
= NULL
;
319 tk
->stok_un
.stok_ident
= sid
;
323 /* Read a string, dealing with escape sequences a la C! */
328 struct obstack bstring_obstack
;
329 obstack_init (&bstring_obstack
);
330 curoff
= ftell (state_file
);
331 while ((c
= getc (state_file
)) != '"' && c
>= 0)
333 if (ISPRINT (c
) && c
!= '\\')
334 obstack_1grow (&bstring_obstack
, (char) c
);
335 else if (ISSPACE (c
) && c
!= '\n')
336 obstack_1grow (&bstring_obstack
, (char) c
);
339 c
= getc (state_file
);
343 obstack_1grow (&bstring_obstack
, '\a');
344 c
= getc (state_file
);
347 obstack_1grow (&bstring_obstack
, '\b');
348 c
= getc (state_file
);
351 obstack_1grow (&bstring_obstack
, '\t');
352 c
= getc (state_file
);
355 obstack_1grow (&bstring_obstack
, '\n');
356 c
= getc (state_file
);
359 obstack_1grow (&bstring_obstack
, '\v');
360 c
= getc (state_file
);
363 obstack_1grow (&bstring_obstack
, '\f');
364 c
= getc (state_file
);
367 obstack_1grow (&bstring_obstack
, '\r');
368 c
= getc (state_file
);
371 obstack_1grow (&bstring_obstack
, '\"');
372 c
= getc (state_file
);
375 obstack_1grow (&bstring_obstack
, '\\');
376 c
= getc (state_file
);
379 obstack_1grow (&bstring_obstack
, ' ');
380 c
= getc (state_file
);
385 if (fscanf (state_file
, "%02x", &cx
) > 0 && cx
> 0)
386 obstack_1grow (&bstring_obstack
, cx
);
390 "Lexical error in string hex escape");
391 c
= getc (state_file
);
397 "Lexical error - unknown string escape");
401 fatal_reading_state (NULL_STATE_TOKEN
, "Lexical error...");
404 fatal_reading_state (NULL_STATE_TOKEN
, "Unterminated string");
405 obstack_1grow (&bstring_obstack
, '\0');
406 cstr
= XOBFINISH (&bstring_obstack
, char *);
407 cslen
= strlen (cstr
);
408 tk
= (struct state_token_st
*)
409 xcalloc (sizeof (struct state_token_st
) + cslen
, 1);
410 tk
->stok_kind
= STOK_STRING
;
411 tk
->stok_line
= state_line
;
412 tk
->stok_col
= curoff
- state_bol
;
413 tk
->stok_file
= state_path
;
414 tk
->stok_next
= NULL
;
415 strcpy (tk
->stok_un
.stok_string
, cstr
);
416 obstack_free (&bstring_obstack
, NULL
);
420 /* Got an unexpected character. */
421 fatal_reading_state_printf
423 "Lexical error at offset %ld - bad character \\%03o = '%c'",
424 ftell (state_file
), c
, c
);
427 /* Used for lexical look-ahead. Retrieves the lexical token of rank
428 DEPTH, starting with 0 when reading the state file. Gives null on
430 static struct state_token_st
*
431 peek_state_token (int depth
)
433 int remdepth
= depth
;
434 struct state_token_st
**ptoken
= &state_token
;
435 struct state_token_st
*tok
= NULL
;
437 while (remdepth
>= 0)
441 *ptoken
= tok
= read_a_state_token ();
446 ptoken
= &((*ptoken
)->stok_next
);
453 /* Consume the next DEPTH tokens and free them. */
455 next_state_tokens (int depth
)
457 struct state_token_st
*n
;
461 if (state_token
!= NULL
)
463 n
= state_token
->stok_next
;
468 fatal_reading_state (NULL_STATE_TOKEN
, "Tokens stack empty");
474 /* Safely retrieve the lexical kind of a token. */
475 static inline enum state_token_en
476 state_token_kind (struct state_token_st
*p
)
484 /* Test if a token is a given name i.e. an identifier. */
486 state_token_is_name (struct state_token_st
*p
, const char *name
)
491 if (p
->stok_kind
!= STOK_NAME
)
494 return !strcmp (p
->stok_un
.stok_ident
->stid_name
, name
);
498 /* Following routines are useful for serializing datas.
500 * We want to serialize :
503 * - param_structs list
506 * So, we have one routine for each kind of data. The main writing
507 * routine is write_state. The main reading routine is
508 * read_state. Most writing routines write_state_FOO have a
509 * corresponding reading routine read_state_FOO. Reading is done in a
510 * recursive descending way, and any read error is fatal.
513 /* When reading the state, we need to remember the previously seen
514 types by their state_number, since GTY-ed types are usually
516 static htab_t state_seen_types
;
518 /* Return the length of a linked list made of pairs. */
519 static int pair_list_length (pair_p list
);
522 static void write_state_pair (pair_p
);
524 /* return the number of pairs written. Should match the length given
525 by pair_list_length. */
526 static int write_state_pair_list (pair_p list
);
528 /* Write a type. When a type is written, its state_number is updated,
529 to ensure that a "reference" to a seen type is written on next
531 static void write_state_type (type_p
);
533 /* Write a null-terminatel string using our Lispy lexical conventions,
534 similar to those of C or MELT. */
535 static void write_state_a_string (const char *s
);
537 /* Compute the length of a list of pairs, starting from the first
540 pair_list_length (pair_p list
)
544 for (l
= list
; l
; l
= l
->next
)
549 /* Write a file location. Files relative to $(srcdir) are quite
550 frequent and are handled specially. This ensures that two gengtype
551 state file-s produced by gengtype on the same GCC source tree are
552 very similar and can be reasonably compared with diff, even if the
553 two GCC source trees have different absolute paths. */
555 write_state_fileloc (struct fileloc
*floc
)
558 if (floc
!= NULL
&& floc
->line
> 0)
560 const char *srcrelpath
= NULL
;
561 gcc_assert (floc
->file
!= NULL
);
562 /* Most of the files are inside $(srcdir) so it is worth to
563 handle them specially. */
564 srcrelpath
= get_file_srcdir_relative_path (floc
->file
);
565 if (srcrelpath
!= NULL
)
567 fprintf (state_file
, "\n(!srcfileloc ");
568 write_state_a_string (srcrelpath
);
572 fprintf (state_file
, "\n(!fileloc ");
573 write_state_a_string (get_input_file_name (floc
->file
));
575 fprintf (state_file
, " %d", floc
->line
);
576 fprintf (state_file
, ")\n");
579 fprintf (state_file
, "nil ");
582 /* Write a list of fields. */
584 write_state_fields (pair_p fields
)
586 int nbfields
= pair_list_length (fields
);
588 fprintf (state_file
, "\n(!fields %d ", nbfields
);
589 nbpairs
= write_state_pair_list (fields
);
590 gcc_assert (nbpairs
== nbfields
);
591 fprintf (state_file
, ")\n");
594 /* Write a null-terminated string in our lexical convention, very
595 similar to the convention of C. */
597 write_state_a_string (const char *s
)
601 fputs (" \"", state_file
);
608 fputs ("\\a", state_file
);
611 fputs ("\\b", state_file
);
614 fputs ("\\t", state_file
);
617 fputs ("\\n", state_file
);
620 fputs ("\\v", state_file
);
623 fputs ("\\f", state_file
);
626 fputs ("\\r", state_file
);
629 fputs ("\\\"", state_file
);
632 fputs ("\\\\", state_file
);
636 putc (c
, state_file
);
638 fprintf (state_file
, "\\x%02x", (unsigned) c
);
641 fputs ("\"", state_file
);
644 /* Our option-s have three kinds, each with its writer. */
646 write_state_string_option (options_p current
)
648 fprintf (state_file
, "string ");
649 if (current
->info
.string
!= NULL
)
650 write_state_a_string (current
->info
.string
);
652 fprintf (state_file
, " nil ");
656 write_state_type_option (options_p current
)
658 fprintf (state_file
, "type ");
659 write_state_type (current
->info
.type
);
663 write_state_nested_option (options_p current
)
665 fprintf (state_file
, "nested ");
666 write_state_type (current
->info
.nested
->type
);
667 if (current
->info
.nested
->convert_from
!= NULL
)
668 write_state_a_string (current
->info
.nested
->convert_from
);
670 fprintf (state_file
, " nil ");
672 if (current
->info
.nested
->convert_to
!= NULL
)
673 write_state_a_string (current
->info
.nested
->convert_to
);
675 fprintf (state_file
, " nil ");
679 write_state_option (options_p current
)
681 fprintf (state_file
, "\n(!option ");
683 if (current
->name
!= NULL
)
684 fprintf (state_file
, "%s ", current
->name
);
686 fprintf (state_file
, "nil ");
688 switch (current
->kind
)
691 write_state_string_option (current
);
694 write_state_type_option (current
);
697 write_state_nested_option (current
);
700 fatal ("Option tag unknown");
703 fprintf (state_file
, ")\n");
708 /* Write a list of GTY options. */
710 write_state_options (options_p opt
)
716 fprintf (state_file
, "nil ");
720 fprintf (state_file
, "\n(!options ");
721 for (current
= opt
; current
!= NULL
; current
= current
->next
)
722 write_state_option (current
);
723 fprintf (state_file
, ")\n");
727 /* Write a bitmap representing a set of GCC front-end languages. */
729 write_state_lang_bitmap (lang_bitmap bitmap
)
731 fprintf (state_file
, "%d ", (int) bitmap
);
734 /* Write version information. */
736 write_state_version (const char *version
)
738 fprintf (state_file
, "\n(!version ");
739 write_state_a_string (version
);
740 fprintf (state_file
, ")\n");
743 /* Common routine to write the common content of all types. */
744 static void write_state_common_type_content (type_p current
);
746 /* Write a scalar type. We have only two of these. */
748 write_state_scalar_type (type_p current
)
750 if (current
== &scalar_nonchar
)
751 fprintf (state_file
, "scalar_nonchar ");
752 else if (current
== &scalar_char
)
753 fprintf (state_file
, "scalar_char ");
755 fatal ("Unexpected type in write_state_scalar_type");
757 write_state_common_type_content (current
);
760 /* Write the string type. There is only one such thing! */
762 write_state_string_type (type_p current
)
764 if (current
== &string_type
)
766 fprintf (state_file
, "string ");
767 write_state_common_type_content (current
);
770 fatal ("Unexpected type in write_state_string_type");
774 /* Common code to write structure like types. */
776 write_state_struct_union_type (type_p current
, const char *kindstr
)
778 DBGPRINTF ("%s type @ %p #%d '%s'", kindstr
, (void *) current
,
779 current
->state_number
, current
->u
.s
.tag
);
780 fprintf (state_file
, "%s ", kindstr
);
781 write_state_common_type_content (current
);
782 if (current
->u
.s
.tag
!= NULL
)
783 write_state_a_string (current
->u
.s
.tag
);
785 fprintf (state_file
, "nil");
787 write_state_fileloc (type_lineloc (current
));
788 write_state_fields (current
->u
.s
.fields
);
789 write_state_options (current
->u
.s
.opt
);
790 write_state_lang_bitmap (current
->u
.s
.bitmap
);
794 /* Write a GTY struct type. */
796 write_state_struct_type (type_p current
)
798 write_state_struct_union_type (current
, "struct");
799 write_state_type (current
->u
.s
.lang_struct
);
802 /* Write a GTY user-defined struct type. */
804 write_state_user_struct_type (type_p current
)
806 DBGPRINTF ("user_struct type @ %p #%d '%s'", (void *) current
,
807 current
->state_number
, current
->u
.s
.tag
);
808 fprintf (state_file
, "user_struct ");
809 write_state_common_type_content (current
);
810 if (current
->u
.s
.tag
!= NULL
)
811 write_state_a_string (current
->u
.s
.tag
);
813 fprintf (state_file
, "nil");
814 write_state_fileloc (type_lineloc (current
));
815 write_state_fields (current
->u
.s
.fields
);
818 /* write a GTY union type. */
820 write_state_union_type (type_p current
)
822 write_state_struct_union_type (current
, "union");
823 write_state_type (current
->u
.s
.lang_struct
);
826 /* Write a lang_struct type. This is tricky and was painful to debug,
827 we deal with the next field specifically within their lang_struct
828 subfield, which points to a linked list of homonumous types.
829 Change this function with extreme care, see also
830 read_state_lang_struct_type. */
832 write_state_lang_struct_type (type_p current
)
836 const char *homoname
= 0;
837 write_state_struct_union_type (current
, "lang_struct");
838 /* lang_struct-ures are particularly tricky, since their
839 u.s.lang_struct field gives a list of homonymous struct-s or
841 DBGPRINTF ("lang_struct @ %p #%d", (void *) current
, current
->state_number
);
842 for (hty
= current
->u
.s
.lang_struct
; hty
!= NULL
; hty
= hty
->next
)
845 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype
,
846 (void *) hty
, hty
->state_number
, hty
->u
.s
.tag
);
847 /* Every member of the homonymous list should have the same tag. */
848 gcc_assert (union_or_struct_p (hty
));
849 gcc_assert (hty
->u
.s
.lang_struct
== current
);
851 homoname
= hty
->u
.s
.tag
;
852 gcc_assert (strcmp (homoname
, hty
->u
.s
.tag
) == 0);
854 fprintf (state_file
, "(!homotypes %d\n", nbhomontype
);
855 for (hty
= current
->u
.s
.lang_struct
; hty
!= NULL
; hty
= hty
->next
)
856 write_state_type (hty
);
857 fprintf (state_file
, ")\n");
860 /* Write a parametrized structure GTY type. */
862 write_state_param_struct_type (type_p current
)
866 fprintf (state_file
, "param_struct ");
867 write_state_common_type_content (current
);
868 write_state_type (current
->u
.param_struct
.stru
);
869 for (i
= 0; i
< NUM_PARAM
; i
++)
871 if (current
->u
.param_struct
.param
[i
] != NULL
)
872 write_state_type (current
->u
.param_struct
.param
[i
]);
874 fprintf (state_file
, "nil ");
876 write_state_fileloc (¤t
->u
.param_struct
.line
);
879 /* Write a pointer type. */
881 write_state_pointer_type (type_p current
)
883 fprintf (state_file
, "pointer ");
884 write_state_common_type_content (current
);
885 write_state_type (current
->u
.p
);
888 /* Write an array type. */
890 write_state_array_type (type_p current
)
892 fprintf (state_file
, "array ");
893 write_state_common_type_content (current
);
894 if (current
->u
.a
.len
!= NULL
)
895 write_state_a_string (current
->u
.a
.len
);
897 fprintf (state_file
, " nil");
899 fprintf (state_file
, " ");
900 write_state_type (current
->u
.a
.p
);
903 /* Write the gc_used information. */
905 write_state_gc_used (enum gc_used_enum gus
)
910 fprintf (state_file
, " gc_unused");
913 fprintf (state_file
, " gc_used");
915 case GC_MAYBE_POINTED_TO
:
916 fprintf (state_file
, " gc_maybe_pointed_to");
919 fprintf (state_file
, " gc_pointed_to");
926 /* Utility routine to write the common content of all types. Notice
927 that the next field is *not* written on purpose. */
929 write_state_common_type_content (type_p current
)
931 fprintf (state_file
, "%d ", current
->state_number
);
932 /* We do not write the next type, because list of types are
933 explicitly written. However, lang_struct are special in that
934 respect. See function write_state_lang_struct_type for more. */
935 write_state_type (current
->pointer_to
);
936 write_state_gc_used (current
->gc_used
);
940 /* The important and recursive routine writing GTY types as understood
941 by gengtype. Types which have a positive state_number have already
942 been seen and written. */
944 write_state_type (type_p current
)
948 fprintf (state_file
, "nil ");
952 fprintf (state_file
, "\n(!type ");
954 if (current
->state_number
> 0)
955 fprintf (state_file
, "already_seen %d", current
->state_number
);
958 state_written_type_count
++;
959 DBGPRINTF ("writing type #%d @%p old number %d", state_written_type_count
,
960 (void *) current
, current
->state_number
);
961 current
->state_number
= state_written_type_count
;
962 switch (current
->kind
)
965 write_state_struct_type (current
);
967 case TYPE_USER_STRUCT
:
968 write_state_user_struct_type (current
);
971 write_state_union_type (current
);
974 write_state_pointer_type (current
);
977 write_state_array_type (current
);
979 case TYPE_LANG_STRUCT
:
980 write_state_lang_struct_type (current
);
982 case TYPE_PARAM_STRUCT
:
983 write_state_param_struct_type (current
);
986 write_state_scalar_type (current
);
989 write_state_string_type (current
);
993 fatal ("Unexpected type...");
997 fprintf (state_file
, ")\n");
1003 write_state_pair (pair_p current
)
1005 if (current
== NULL
)
1007 fprintf (state_file
, "nil)");
1011 fprintf (state_file
, "\n(!pair ");
1013 if (current
->name
!= NULL
)
1014 write_state_a_string (current
->name
);
1016 write_state_a_string ("nil");
1018 write_state_type (current
->type
);
1019 write_state_fileloc (&(current
->line
));
1020 write_state_options (current
->opt
);
1022 fprintf (state_file
, ")");
1025 /* Write a pair list and return the number of pairs written. */
1027 write_state_pair_list (pair_p list
)
1032 for (current
= list
; current
!= NULL
; current
= current
->next
)
1034 write_state_pair (current
);
1041 /* When writing imported linked lists, like typedefs, structures,
1042 param_structs, ... we count their length first and write it. These
1043 eases the reading, and enables an extra verification on the number
1044 of actually read items. */
1046 /* Write our typedefs. */
1048 write_state_typedefs (void)
1050 int nbtypedefs
= pair_list_length (typedefs
);
1052 fprintf (state_file
, "\n(!typedefs %d\n", nbtypedefs
);
1053 nbpairs
= write_state_pair_list (typedefs
);
1054 gcc_assert (nbpairs
== nbtypedefs
);
1055 fprintf (state_file
, ")\n");
1056 if (verbosity_level
>= 2)
1057 printf ("%s wrote %d typedefs\n", progname
, nbtypedefs
);
1060 /* Write our structures. */
1062 write_state_structures (void)
1067 for (current
= structures
; current
!= NULL
; current
= current
->next
)
1070 fprintf (state_file
, "\n(!structures %d\n", nbstruct
);
1072 for (current
= structures
; current
!= NULL
; current
= current
->next
)
1073 write_state_type (current
);
1075 fprintf (state_file
, ")\n");
1076 if (verbosity_level
>= 2)
1077 printf ("%s wrote %d structures in state\n", progname
, nbstruct
);
1080 /* Write our param_struct-s. */
1082 write_state_param_structs (void)
1084 int nbparamstruct
= 0;
1087 for (current
= param_structs
; current
!= NULL
; current
= current
->next
)
1090 fprintf (state_file
, "\n(!param_structs %d\n", nbparamstruct
);
1092 for (current
= param_structs
; current
!= NULL
; current
= current
->next
)
1093 write_state_type (current
);
1095 fprintf (state_file
, ")\n");
1098 /* Write our variables. */
1100 write_state_variables (void)
1102 int nbvars
= pair_list_length (variables
);
1104 fprintf (state_file
, "\n(!variables %d\n", nbvars
);
1105 nbpairs
= write_state_pair_list (variables
);
1106 gcc_assert (nbpairs
== nbvars
);
1107 fprintf (state_file
, ")\n");
1108 if (verbosity_level
>= 2)
1109 printf ("%s wrote %d variables.\n", progname
, nbvars
);
1112 /* Write the source directory. File locations within the source
1113 directory have been written specifically. */
1115 write_state_srcdir (void)
1117 fprintf (state_file
, "\n(!srcdir ");
1118 write_state_a_string (srcdir
);
1119 fprintf (state_file
, ")\n");
1122 /* Count and write the list of our files. */
1124 write_state_files_list (void)
1127 /* Write the list of files with their lang_bitmap. */
1128 fprintf (state_file
, "\n(!fileslist %d\n", (int) num_gt_files
);
1129 for (i
= 0; i
< (int) num_gt_files
; i
++)
1131 const char *cursrcrelpath
= NULL
;
1132 const input_file
*curfil
= gt_files
[i
];
1133 /* Most of the files are inside $(srcdir) so it is worth to
1134 handle them specially. */
1135 cursrcrelpath
= get_file_srcdir_relative_path (curfil
);
1138 fprintf (state_file
, "(!srcfile %d ", get_lang_bitmap (curfil
));
1139 write_state_a_string (cursrcrelpath
);
1143 fprintf (state_file
, "(!file %d ", get_lang_bitmap (curfil
));
1144 write_state_a_string (get_input_file_name (curfil
));
1146 fprintf (state_file
, ")\n");
1148 fprintf (state_file
, ")\n");
1151 /* Write the list of GCC front-end languages. */
1153 write_state_languages (void)
1156 fprintf (state_file
, "\n(!languages %d", (int) num_lang_dirs
);
1157 for (i
= 0; i
< (int) num_lang_dirs
; i
++)
1159 /* Languages names are identifiers, we expect only letters or
1160 underscores or digits in them. In particular, C++ is not a
1161 valid language name, but cp is valid. */
1162 fprintf (state_file
, " %s", lang_dir_names
[i
]);
1164 fprintf (state_file
, ")\n");
1167 /* Write the trailer. */
1169 write_state_trailer (void)
1171 /* This test should probably catch IO errors like disk full... */
1172 if (fputs ("\n(!endfile)\n", state_file
) == EOF
)
1173 fatal ("failed to write state trailer [%s]", xstrerror (errno
));
1176 /* The write_state routine is the only writing routine called by main
1177 in gengtype.c. To avoid messing the state if gengtype is
1178 interrupted or aborted, we write a temporary file and rename it
1179 after having written it in totality. */
1181 write_state (const char *state_path
)
1185 char *temp_state_path
= NULL
;
1186 char tempsuffix
[40];
1189 /* We write a unique temporary file which is renamed when complete
1190 * only. So even if gengtype is interrupted, the written state file
1191 * won't be partially written, since the temporary file is not yet
1192 * renamed in that case. */
1193 memset (tempsuffix
, 0, sizeof (tempsuffix
));
1194 snprintf (tempsuffix
, sizeof (tempsuffix
) - 1, "-%ld-%d.tmp", (long) now
,
1196 temp_state_path
= concat (state_path
, tempsuffix
, NULL
);
1197 state_file
= fopen (temp_state_path
, "w");
1198 if (state_file
== NULL
)
1199 fatal ("Failed to open file %s for writing state: %s",
1200 temp_state_path
, xstrerror (errno
));
1201 if (verbosity_level
>= 3)
1202 printf ("%s writing state file %s temporarily in %s\n",
1203 progname
, state_path
, temp_state_path
);
1204 /* This is the first line of the state. Perhaps the file utility
1205 could know about that, so don't change it often. */
1206 fprintf (state_file
, ";;;;@@@@ GCC gengtype state\n");
1207 /* Output a few comments for humans. */
1208 fprintf (state_file
,
1209 ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
1210 fprintf (state_file
,
1211 ";;; The format of this file is tied to a particular version of GCC.\n");
1212 fprintf (state_file
,
1213 ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1214 fprintf (state_file
,
1215 ";;; This file should be parsed by the same %s which wrote it.\n",
1217 /* The first non-comment significant line gives the version string. */
1218 write_state_version (version_string
);
1219 write_state_srcdir ();
1220 write_state_languages ();
1221 write_state_files_list ();
1222 write_state_structures ();
1223 write_state_typedefs ();
1224 write_state_param_structs ();
1225 write_state_variables ();
1226 write_state_trailer ();
1227 statelen
= ftell (state_file
);
1228 if (ferror (state_file
))
1229 fatal ("output error when writing state file %s [%s]",
1230 temp_state_path
, xstrerror (errno
));
1231 if (fclose (state_file
))
1232 fatal ("failed to close state file %s [%s]",
1233 temp_state_path
, xstrerror (errno
));
1234 if (rename (temp_state_path
, state_path
))
1235 fatal ("failed to rename %s to state file %s [%s]", temp_state_path
,
1236 state_path
, xstrerror (errno
));
1237 free (temp_state_path
);
1239 if (verbosity_level
>= 1)
1240 printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1241 progname
, state_path
, statelen
, state_written_type_count
);
1245 /** End of writing routines! The corresponding reading routines follow. **/
1249 /* Forward declarations, since some read_state_* functions are
1251 static void read_state_fileloc (struct fileloc
*line
);
1252 static void read_state_options (options_p
*opt
);
1253 static void read_state_type (type_p
*current
);
1254 static void read_state_pair (pair_p
*pair
);
1255 /* Return the number of pairs actually read. */
1256 static int read_state_pair_list (pair_p
*list
);
1257 static void read_state_fields (pair_p
*fields
);
1258 static void read_state_common_type_content (type_p current
);
1263 /* Record into the state_seen_types hash-table a type which we are
1264 reading, to enable recursive or circular references to it. */
1266 record_type (type_p type
)
1270 slot
= htab_find_slot (state_seen_types
, type
, INSERT
);
1276 /* Read an already seen type. */
1278 read_state_already_seen_type (type_p
*type
)
1280 struct state_token_st
*t0
= peek_state_token (0);
1282 if (state_token_kind (t0
) == STOK_INTEGER
)
1285 struct type loctype
= { TYPE_SCALAR
, 0, 0, 0, GC_UNUSED
, {0} };
1287 loctype
.state_number
= t0
->stok_un
.stok_num
;
1288 slot
= htab_find_slot (state_seen_types
, &loctype
, NO_INSERT
);
1291 fatal_reading_state (t0
, "Unknown type");
1294 next_state_tokens (1);
1295 *type
= (type_p
) *slot
;
1299 fatal_reading_state (t0
, "Bad seen type");
1304 /* Read the scalar_nonchar type. */
1306 read_state_scalar_nonchar_type (type_p
*type
)
1308 *type
= &scalar_nonchar
;
1309 read_state_common_type_content (*type
);
1313 /* Read the scalar_char type. */
1315 read_state_scalar_char_type (type_p
*type
)
1317 *type
= &scalar_char
;
1318 read_state_common_type_content (*type
);
1322 /* Read the string_type. */
1324 read_state_string_type (type_p
*type
)
1326 *type
= &string_type
;
1327 read_state_common_type_content (*type
);
1331 /* Read a lang_bitmap representing a set of GCC front-end languages. */
1333 read_state_lang_bitmap (lang_bitmap
*bitmap
)
1335 struct state_token_st
*t
;
1337 t
= peek_state_token (0);
1338 if (state_token_kind (t
) == STOK_INTEGER
)
1340 *bitmap
= t
->stok_un
.stok_num
;
1341 next_state_tokens (1);
1345 fatal_reading_state (t
, "Bad syntax for bitmap");
1350 /* Read a GTY-ed struct type. */
1352 read_state_struct_type (type_p type
)
1354 struct state_token_st
*t0
;
1356 type
->kind
= TYPE_STRUCT
;
1357 read_state_common_type_content (type
);
1358 t0
= peek_state_token (0);
1359 if (state_token_kind (t0
) == STOK_STRING
)
1361 if (state_token_is_name (t0
, "nil"))
1363 type
->u
.s
.tag
= NULL
;
1364 DBGPRINTF ("read anonymous struct type @%p #%d",
1365 (void *) type
, type
->state_number
);
1369 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1370 DBGPRINTF ("read struct type @%p #%d '%s'",
1371 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1374 next_state_tokens (1);
1375 read_state_fileloc (&(type
->u
.s
.line
));
1376 read_state_fields (&(type
->u
.s
.fields
));
1377 read_state_options (&(type
->u
.s
.opt
));
1378 read_state_lang_bitmap (&(type
->u
.s
.bitmap
));
1379 read_state_type (&(type
->u
.s
.lang_struct
));
1383 fatal_reading_state (t0
, "Bad tag in struct type");
1388 /* Read a GTY-ed user-provided struct TYPE. */
1391 read_state_user_struct_type (type_p type
)
1393 struct state_token_st
*t0
;
1395 type
->kind
= TYPE_USER_STRUCT
;
1396 read_state_common_type_content (type
);
1397 t0
= peek_state_token (0);
1398 if (state_token_kind (t0
) == STOK_STRING
)
1400 if (state_token_is_name (t0
, "nil"))
1402 type
->u
.s
.tag
= NULL
;
1403 DBGPRINTF ("read anonymous struct type @%p #%d",
1404 (void *) type
, type
->state_number
);
1408 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1409 DBGPRINTF ("read struct type @%p #%d '%s'",
1410 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1413 next_state_tokens (1);
1414 read_state_fileloc (&(type
->u
.s
.line
));
1415 read_state_fields (&(type
->u
.s
.fields
));
1419 fatal_reading_state (t0
, "Bad tag in user-struct type");
1424 /* Read a GTY-ed union type. */
1426 read_state_union_type (type_p type
)
1428 struct state_token_st
*t0
;
1430 type
->kind
= TYPE_UNION
;
1431 read_state_common_type_content (type
);
1432 t0
= peek_state_token (0);
1433 if (state_token_kind (t0
) == STOK_STRING
)
1435 if (state_token_is_name (t0
, "nil"))
1437 type
->u
.s
.tag
= NULL
;
1438 DBGPRINTF ("read anonymous union type @%p #%d",
1439 (void *) type
, type
->state_number
);
1443 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1444 DBGPRINTF ("read union type @%p #%d '%s'",
1445 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1447 next_state_tokens (1);
1448 read_state_fileloc (&(type
->u
.s
.line
));
1449 read_state_fields (&(type
->u
.s
.fields
));
1450 read_state_options (&(type
->u
.s
.opt
));
1451 read_state_lang_bitmap (&(type
->u
.s
.bitmap
));
1452 read_state_type (&(type
->u
.s
.lang_struct
));
1455 fatal_reading_state (t0
, "Bad tag in union type");
1459 /* Read a GTY-ed pointer type. */
1461 read_state_pointer_type (type_p type
)
1463 type
->kind
= TYPE_POINTER
;
1464 read_state_common_type_content (type
);
1465 DBGPRINTF ("read pointer type @%p #%d", (void *) type
, type
->state_number
);
1466 read_state_type (&(type
->u
.p
));
1470 /* Read a GTY-ed array type. */
1472 read_state_array_type (type_p type
)
1474 struct state_token_st
*t0
;
1476 type
->kind
= TYPE_ARRAY
;
1477 read_state_common_type_content (type
);
1478 t0
= peek_state_token (0);
1479 if (state_token_kind (t0
) == STOK_STRING
)
1481 type
->u
.a
.len
= xstrdup (t0
->stok_un
.stok_string
);
1482 DBGPRINTF ("read array type @%p #%d length '%s'",
1483 (void *) type
, type
->state_number
, type
->u
.a
.len
);
1484 next_state_tokens (1);
1487 else if (state_token_is_name (t0
, "nil"))
1489 type
->u
.a
.len
= NULL
;
1490 DBGPRINTF ("read array type @%p #%d without length",
1491 (void *) type
, type
->state_number
);
1492 next_state_tokens (1);
1496 fatal_reading_state (t0
, "Bad array name type");
1497 read_state_type (&(type
->u
.a
.p
));
1502 /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
1503 front-end languages. This is a tricky function and it was painful
1504 to debug. Change it with extreme care. See also
1505 write_state_lang_struct_type. */
1507 read_state_lang_struct_type (type_p type
)
1509 struct state_token_st
*t0
= NULL
;
1510 struct state_token_st
*t1
= NULL
;
1511 struct state_token_st
*t2
= NULL
;
1513 type
->kind
= TYPE_LANG_STRUCT
;
1514 read_state_common_type_content (type
);
1515 t0
= peek_state_token (0);
1516 if (state_token_kind (t0
) == STOK_STRING
)
1518 if (state_token_is_name (t0
, "nil"))
1520 DBGPRINTF ("read anonymous lang_struct type @%p #%d",
1521 (void *) type
, type
->state_number
);
1522 type
->u
.s
.tag
= NULL
;
1526 type
->u
.s
.tag
= xstrdup (t0
->stok_un
.stok_string
);
1527 DBGPRINTF ("read lang_struct type @%p #%d '%s'",
1528 (void *) type
, type
->state_number
, type
->u
.s
.tag
);
1530 next_state_tokens (1);
1533 fatal_reading_state (t0
, "Bad tag in lang struct type");
1534 read_state_fileloc (&(type
->u
.s
.line
));
1535 read_state_fields (&(type
->u
.s
.fields
));
1536 read_state_options (&(type
->u
.s
.opt
));
1537 read_state_lang_bitmap (&(type
->u
.s
.bitmap
));
1538 /* Within lang_struct-ures, the lang_struct field is a linked list
1539 of homonymous types! */
1540 t0
= peek_state_token (0);
1541 t1
= peek_state_token (1);
1542 t2
= peek_state_token (2);
1543 /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
1544 if (state_token_kind (t0
) == STOK_LEFTPAR
1545 && state_token_is_name (t1
, "!homotypes")
1546 && state_token_kind (t2
) == STOK_INTEGER
)
1548 type_p
*prevty
= &type
->u
.s
.lang_struct
;
1549 int nbhomotype
= t2
->stok_un
.stok_num
;
1551 t0
= t1
= t2
= NULL
;
1552 next_state_tokens (3);
1553 for (i
= 0; i
< nbhomotype
; i
++)
1555 read_state_type (prevty
);
1556 t0
= peek_state_token (0);
1558 prevty
= &(*prevty
)->next
;
1560 fatal_reading_state (t0
,
1561 "expecting type in homotype list for lang_struct");
1563 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1564 fatal_reading_state (t0
,
1565 "expecting ) in homotype list for lang_struct");
1566 next_state_tokens (1);
1569 fatal_reading_state (t0
, "expecting !homotypes for lang_struct");
1573 /* Read a param_struct type for GTY parametrized structures. */
1575 read_state_param_struct_type (type_p type
)
1578 struct state_token_st
*t0
;
1580 type
->kind
= TYPE_PARAM_STRUCT
;
1581 read_state_common_type_content (type
);
1582 DBGPRINTF ("read param_struct type @%p #%d",
1583 (void *) type
, type
->state_number
);
1584 read_state_type (&(type
->u
.param_struct
.stru
));
1586 for (i
= 0; i
< NUM_PARAM
; i
++)
1588 t0
= peek_state_token (0);
1589 if (state_token_is_name (t0
, "nil"))
1591 type
->u
.param_struct
.param
[i
] = NULL
;
1592 next_state_tokens (1);
1595 read_state_type (&(type
->u
.param_struct
.param
[i
]));
1597 read_state_fileloc (&(type
->u
.param_struct
.line
));
1601 /* Read the gc used information. */
1603 read_state_gc_used (enum gc_used_enum
*pgus
)
1605 struct state_token_st
*t0
= peek_state_token (0);
1606 if (state_token_is_name (t0
, "gc_unused"))
1608 else if (state_token_is_name (t0
, "gc_used"))
1610 else if (state_token_is_name (t0
, "gc_maybe_pointed_to"))
1611 *pgus
= GC_MAYBE_POINTED_TO
;
1612 else if (state_token_is_name (t0
, "gc_pointed_to"))
1613 *pgus
= GC_POINTED_TO
;
1615 fatal_reading_state (t0
, "invalid gc_used information");
1616 next_state_tokens (1);
1620 /* Utility function to read the common content of types. */
1622 read_state_common_type_content (type_p current
)
1624 struct state_token_st
*t0
= peek_state_token (0);
1626 if (state_token_kind (t0
) == STOK_INTEGER
)
1628 current
->state_number
= t0
->stok_un
.stok_num
;
1629 next_state_tokens (1);
1630 record_type (current
);
1633 fatal_reading_state_printf (t0
,
1634 "Expected integer for state_number line %d",
1636 /* We don't read the next field of the type. */
1637 read_state_type (¤t
->pointer_to
);
1638 read_state_gc_used (¤t
->gc_used
);
1642 /* Read a GTY-ed type. */
1644 read_state_type (type_p
*current
)
1646 struct state_token_st
*t0
= peek_state_token (0);
1647 struct state_token_st
*t1
= peek_state_token (1);
1649 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1650 state_token_is_name (t1
, "!type"))
1652 next_state_tokens (2);
1653 t0
= peek_state_token (0);
1654 if (state_token_is_name (t0
, "already_seen"))
1656 next_state_tokens (1);
1657 read_state_already_seen_type (current
);
1661 t0
= peek_state_token (0);
1663 if (state_token_is_name (t0
, "scalar_nonchar"))
1665 next_state_tokens (1);
1666 read_state_scalar_nonchar_type (current
);
1668 else if (state_token_is_name (t0
, "scalar_char"))
1670 next_state_tokens (1);
1671 read_state_scalar_char_type (current
);
1673 else if (state_token_is_name (t0
, "string"))
1675 next_state_tokens (1);
1676 read_state_string_type (current
);
1678 else if (state_token_is_name (t0
, "struct"))
1680 *current
= XCNEW (struct type
);
1681 next_state_tokens (1);
1682 read_state_struct_type (*current
);
1684 else if (state_token_is_name (t0
, "union"))
1686 *current
= XCNEW (struct type
);
1687 next_state_tokens (1);
1688 read_state_union_type (*current
);
1690 else if (state_token_is_name (t0
, "lang_struct"))
1692 *current
= XCNEW (struct type
);
1693 next_state_tokens (1);
1694 read_state_lang_struct_type (*current
);
1696 else if (state_token_is_name (t0
, "param_struct"))
1698 *current
= XCNEW (struct type
);
1699 next_state_tokens (1);
1700 read_state_param_struct_type (*current
);
1702 else if (state_token_is_name (t0
, "pointer"))
1704 *current
= XCNEW (struct type
);
1705 next_state_tokens (1);
1706 read_state_pointer_type (*current
);
1708 else if (state_token_is_name (t0
, "array"))
1710 *current
= XCNEW (struct type
);
1711 next_state_tokens (1);
1712 read_state_array_type (*current
);
1714 else if (state_token_is_name (t0
, "user_struct"))
1716 *current
= XCNEW (struct type
);
1717 next_state_tokens (1);
1718 read_state_user_struct_type (*current
);
1721 fatal_reading_state (t0
, "bad type in (!type");
1723 t0
= peek_state_token (0);
1724 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1725 fatal_reading_state (t0
, "missing ) in type");
1726 next_state_tokens (1);
1728 else if (state_token_is_name (t0
, "nil"))
1730 next_state_tokens (1);
1734 fatal_reading_state (t0
, "bad type syntax");
1738 /* Read a file location. Files within the source directory are dealt
1739 with specifically. */
1741 read_state_fileloc (struct fileloc
*floc
)
1743 bool issrcfile
= false;
1744 struct state_token_st
*t0
= peek_state_token (0);
1745 struct state_token_st
*t1
= peek_state_token (1);
1747 gcc_assert (floc
!= NULL
);
1748 gcc_assert (srcdir
!= NULL
);
1750 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1751 (state_token_is_name (t1
, "!fileloc")
1752 || (issrcfile
= state_token_is_name (t1
, "!srcfileloc"))))
1754 next_state_tokens (2);
1755 t0
= peek_state_token (0);
1756 t1
= peek_state_token (1);
1757 if (state_token_kind (t0
) == STOK_STRING
&&
1758 state_token_kind (t1
) == STOK_INTEGER
)
1760 char *path
= t0
->stok_un
.stok_string
;
1763 static const char dirsepstr
[2] = { DIR_SEPARATOR
, (char) 0 };
1764 char *fullpath
= concat (srcdir
, dirsepstr
, path
, NULL
);
1765 floc
->file
= input_file_by_name (fullpath
);
1769 floc
->file
= input_file_by_name (path
);
1770 floc
->line
= t1
->stok_un
.stok_num
;
1771 next_state_tokens (2);
1774 fatal_reading_state (t0
,
1775 "Bad fileloc syntax, expected path string and line");
1776 t0
= peek_state_token (0);
1777 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1778 fatal_reading_state (t0
, "Bad fileloc syntax, expected )");
1779 next_state_tokens (1);
1781 else if (state_token_is_name (t0
, "nil"))
1783 next_state_tokens (1);
1788 fatal_reading_state (t0
, "Bad fileloc syntax");
1792 /* Read the fields of a GTY-ed type. */
1794 read_state_fields (pair_p
*fields
)
1797 struct state_token_st
*t0
= peek_state_token (0);
1798 struct state_token_st
*t1
= peek_state_token (1);
1799 struct state_token_st
*t2
= peek_state_token (2);
1801 if (state_token_kind (t0
) == STOK_LEFTPAR
1802 && state_token_is_name (t1
, "!fields")
1803 && state_token_kind (t2
) == STOK_INTEGER
)
1805 int nbfields
= t2
->stok_un
.stok_num
;
1807 next_state_tokens (3);
1808 nbpairs
= read_state_pair_list (&tmp
);
1809 t0
= peek_state_token (0);
1810 if (nbpairs
!= nbfields
)
1811 fatal_reading_state_printf
1813 "Mismatched fields number, expected %d got %d", nbpairs
, nbfields
);
1814 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
1815 next_state_tokens (1);
1817 fatal_reading_state (t0
, "Bad fields expecting )");
1824 /* Read a string option. */
1826 read_state_string_option (options_p opt
)
1828 struct state_token_st
*t0
= peek_state_token (0);
1829 opt
->kind
= OPTION_STRING
;
1830 if (state_token_kind (t0
) == STOK_STRING
)
1832 opt
->info
.string
= xstrdup (t0
->stok_un
.stok_string
);
1833 next_state_tokens (1);
1835 else if (state_token_is_name (t0
, "nil"))
1837 opt
->info
.string
= NULL
;
1838 next_state_tokens (1);
1841 fatal_reading_state (t0
, "Missing name in string option");
1845 /* Read a type option. */
1847 read_state_type_option (options_p opt
)
1849 opt
->kind
= OPTION_TYPE
;
1850 read_state_type (&(opt
->info
.type
));
1854 /* Read a nested option. */
1856 read_state_nested_option (options_p opt
)
1858 struct state_token_st
*t0
;
1860 opt
->info
.nested
= XCNEW (struct nested_ptr_data
);
1861 opt
->kind
= OPTION_NESTED
;
1862 read_state_type (&(opt
->info
.nested
->type
));
1863 t0
= peek_state_token (0);
1864 if (state_token_kind (t0
) == STOK_STRING
)
1866 opt
->info
.nested
->convert_from
= xstrdup (t0
->stok_un
.stok_string
);
1867 next_state_tokens (1);
1869 else if (state_token_is_name (t0
, "nil"))
1871 opt
->info
.nested
->convert_from
= NULL
;
1872 next_state_tokens (1);
1875 fatal_reading_state (t0
, "Bad nested convert_from option");
1877 t0
= peek_state_token (0);
1878 if (state_token_kind (t0
) == STOK_STRING
)
1880 opt
->info
.nested
->convert_to
= xstrdup (t0
->stok_un
.stok_string
);
1881 next_state_tokens (1);
1883 else if (state_token_is_name (t0
, "nil"))
1885 opt
->info
.nested
->convert_to
= NULL
;
1886 next_state_tokens (1);
1889 fatal_reading_state (t0
, "Bad nested convert_from option");
1893 /* Read an GTY option. */
1895 read_state_option (options_p
*opt
)
1897 struct state_token_st
*t0
= peek_state_token (0);
1898 struct state_token_st
*t1
= peek_state_token (1);
1900 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
1901 state_token_is_name (t1
, "!option"))
1903 next_state_tokens (2);
1904 t0
= peek_state_token (0);
1905 if (state_token_kind (t0
) == STOK_NAME
)
1907 *opt
= XCNEW (struct options
);
1908 if (state_token_is_name (t0
, "nil"))
1909 (*opt
)->name
= NULL
;
1911 (*opt
)->name
= t0
->stok_un
.stok_ident
->stid_name
;
1912 next_state_tokens (1);
1913 t0
= peek_state_token (0);
1914 if (state_token_kind (t0
) == STOK_NAME
)
1916 if (state_token_is_name (t0
, "string"))
1918 next_state_tokens (1);
1919 read_state_string_option (*opt
);
1921 else if (state_token_is_name (t0
, "type"))
1923 next_state_tokens (1);
1924 read_state_type_option (*opt
);
1926 else if (state_token_is_name (t0
, "nested"))
1928 next_state_tokens (1);
1929 read_state_nested_option (*opt
);
1932 fatal_reading_state (t0
, "Bad option type");
1933 t0
= peek_state_token (0);
1934 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
1935 fatal_reading_state (t0
, "Bad syntax in option, expecting )");
1937 next_state_tokens (1);
1940 fatal_reading_state (t0
, "Missing option type");
1943 fatal_reading_state (t0
, "Bad name for option");
1946 fatal_reading_state (t0
, "Bad option, waiting for )");
1949 /* Read a list of options. */
1951 read_state_options (options_p
*opt
)
1953 options_p head
= NULL
;
1954 options_p previous
= NULL
;
1955 options_p current_option
= NULL
;
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
, "!options"))
1962 next_state_tokens (2);
1963 t0
= peek_state_token (0);
1964 while (state_token_kind (t0
) != STOK_RIGHTPAR
)
1966 read_state_option (¤t_option
);
1969 head
= current_option
;
1974 previous
->next
= current_option
;
1975 previous
= current_option
;
1977 t0
= peek_state_token (0);
1979 next_state_tokens (1);
1981 else if (state_token_is_name (t0
, "nil"))
1983 next_state_tokens (1);
1986 fatal_reading_state (t0
, "Bad options syntax");
1992 /* Read a version, and check against the version of the gengtype. */
1994 read_state_version (const char *version_string
)
1996 struct state_token_st
*t0
= peek_state_token (0);
1997 struct state_token_st
*t1
= peek_state_token (1);
1999 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2000 state_token_is_name (t1
, "!version"))
2002 next_state_tokens (2);
2003 t0
= peek_state_token (0);
2004 t1
= peek_state_token (1);
2005 if (state_token_kind (t0
) == STOK_STRING
&&
2006 state_token_kind (t1
) == STOK_RIGHTPAR
)
2008 /* Check that the read version string is the same as current
2010 if (strcmp (version_string
, t0
->stok_un
.stok_string
))
2011 fatal_reading_state_printf (t0
,
2012 "version string mismatch; expecting %s but got %s",
2014 t0
->stok_un
.stok_string
);
2015 next_state_tokens (2);
2018 fatal_reading_state (t0
, "Missing version or right parenthesis");
2021 fatal_reading_state (t0
, "Bad version syntax");
2027 read_state_pair (pair_p
*current
)
2029 struct state_token_st
*t0
= peek_state_token (0);
2030 struct state_token_st
*t1
= peek_state_token (1);
2031 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2032 state_token_is_name (t1
, "!pair"))
2034 *current
= XCNEW (struct pair
);
2035 next_state_tokens (2);
2036 t0
= peek_state_token (0);
2037 if (state_token_kind (t0
) == STOK_STRING
)
2039 if (strcmp (t0
->stok_un
.stok_string
, "nil") == 0)
2041 (*current
)->name
= NULL
;
2045 (*current
)->name
= xstrdup (t0
->stok_un
.stok_string
);
2047 next_state_tokens (1);
2048 read_state_type (&((*current
)->type
));
2049 read_state_fileloc (&((*current
)->line
));
2050 read_state_options (&((*current
)->opt
));;
2051 t0
= peek_state_token (0);
2052 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
2054 next_state_tokens (1);
2058 fatal_reading_state (t0
, "Bad syntax for pair, )");
2063 fatal_reading_state (t0
, "Bad name for pair");
2066 else if (state_token_kind (t0
) == STOK_NAME
&&
2067 state_token_is_name (t0
, "nil"))
2069 next_state_tokens (1);
2073 fatal_reading_state_printf (t0
, "Bad syntax for pair, (!pair %d",
2074 state_token
->stok_kind
);
2078 /* Return the number of pairs actually read. */
2080 read_state_pair_list (pair_p
*list
)
2084 pair_p previous
= NULL
;
2086 struct state_token_st
*t0
= peek_state_token (0);
2087 while (t0
&& state_token_kind (t0
) != STOK_RIGHTPAR
)
2089 read_state_pair (&tmp
);
2097 previous
->next
= tmp
;
2100 t0
= peek_state_token (0);
2104 /* don't consume the ); the caller will eat it. */
2109 /* Read the typedefs. */
2111 read_state_typedefs (pair_p
*typedefs
)
2115 struct state_token_st
*t0
= peek_state_token (0);
2116 struct state_token_st
*t1
= peek_state_token (1);
2117 struct state_token_st
*t2
= peek_state_token (2);
2119 if (state_token_kind (t0
) == STOK_LEFTPAR
2120 && state_token_is_name (t1
, "!typedefs")
2121 && state_token_kind (t2
) == STOK_INTEGER
)
2124 nbtypedefs
= t2
->stok_un
.stok_num
;
2125 next_state_tokens (3);
2126 nbpairs
= read_state_pair_list (&list
);
2127 t0
= peek_state_token (0);
2128 if (nbpairs
!= nbtypedefs
)
2129 fatal_reading_state_printf
2131 "invalid number of typedefs, expected %d but got %d",
2132 nbtypedefs
, nbpairs
);
2133 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
2134 next_state_tokens (1);
2136 fatal_reading_state (t0
, "Bad typedefs syntax )");
2139 fatal_reading_state (t0
, "Bad typedefs syntax (!typedefs");
2141 if (verbosity_level
>= 2)
2142 printf ("%s read %d typedefs from state\n", progname
, nbtypedefs
);
2147 /* Read the structures. */
2149 read_state_structures (type_p
*structures
)
2152 type_p previous
= NULL
;
2154 int nbstruct
= 0, countstruct
= 0;
2155 struct state_token_st
*t0
= peek_state_token (0);
2156 struct state_token_st
*t1
= peek_state_token (1);
2157 struct state_token_st
*t2
= peek_state_token (2);
2159 if (state_token_kind (t0
) == STOK_LEFTPAR
2160 && state_token_is_name (t1
, "!structures")
2161 && state_token_kind (t2
) == STOK_INTEGER
)
2163 nbstruct
= t2
->stok_un
.stok_num
;
2164 next_state_tokens (3);
2165 t0
= peek_state_token (0);
2166 while (t0
&& state_token_kind (t0
) != STOK_RIGHTPAR
)
2169 read_state_type (&tmp
);
2178 previous
->next
= tmp
;
2181 t0
= peek_state_token (0);
2183 next_state_tokens (1);
2186 fatal_reading_state (t0
, "Bad structures syntax");
2187 if (countstruct
!= nbstruct
)
2188 fatal_reading_state_printf (NULL_STATE_TOKEN
,
2189 "expected %d structures but got %d",
2190 nbstruct
, countstruct
);
2191 if (verbosity_level
>= 2)
2192 printf ("%s read %d structures from state\n", progname
, nbstruct
);
2197 /* Read the param_struct-s. */
2199 read_state_param_structs (type_p
*param_structs
)
2201 int nbparamstructs
= 0;
2202 int countparamstructs
= 0;
2204 type_p previous
= NULL
;
2206 struct state_token_st
*t0
= peek_state_token (0);
2207 struct state_token_st
*t1
= peek_state_token (1);
2208 struct state_token_st
*t2
= peek_state_token (2);
2210 if (state_token_kind (t0
) == STOK_LEFTPAR
2211 && state_token_is_name (t1
, "!param_structs")
2212 && state_token_kind (t2
) == STOK_INTEGER
)
2214 nbparamstructs
= t2
->stok_un
.stok_num
;
2215 next_state_tokens (3);
2216 t0
= t1
= t2
= NULL
;
2217 t0
= peek_state_token (0);
2218 while (state_token_kind (t0
) != STOK_RIGHTPAR
)
2221 read_state_type (&tmp
);
2229 previous
->next
= tmp
;
2232 t0
= peek_state_token (0);
2233 countparamstructs
++;
2235 next_state_tokens (1);
2238 fatal_reading_state (t0
, "Bad param_structs syntax");
2239 t0
= peek_state_token (0);
2240 if (countparamstructs
!= nbparamstructs
)
2241 fatal_reading_state_printf
2243 "invalid number of param_structs expected %d got %d",
2244 nbparamstructs
, countparamstructs
);
2245 *param_structs
= head
;
2249 /* Read the variables. */
2251 read_state_variables (pair_p
*variables
)
2255 struct state_token_st
*t0
= peek_state_token (0);
2256 struct state_token_st
*t1
= peek_state_token (1);
2257 struct state_token_st
*t2
= peek_state_token (2);
2259 if (state_token_kind (t0
) == STOK_LEFTPAR
2260 && state_token_is_name (t1
, "!variables")
2261 && state_token_kind (t2
) == STOK_INTEGER
)
2264 nbvars
= t2
->stok_un
.stok_num
;
2265 next_state_tokens (3);
2266 nbpairs
= read_state_pair_list (&list
);
2267 t0
= peek_state_token (0);
2268 if (nbpairs
!= nbvars
)
2269 fatal_reading_state_printf
2270 (t0
, "Invalid number of variables, expected %d but got %d",
2272 if (state_token_kind (t0
) == STOK_RIGHTPAR
)
2273 next_state_tokens (1);
2275 fatal_reading_state (t0
, "Waiting for ) in variables");
2278 fatal_reading_state (t0
, "Bad variables syntax");
2280 if (verbosity_level
>= 2)
2281 printf ("%s read %d variables from state\n", progname
, nbvars
);
2285 /* Read the source directory. */
2287 read_state_srcdir (void)
2289 struct state_token_st
*t0
= peek_state_token (0);
2290 struct state_token_st
*t1
= peek_state_token (1);
2291 if (state_token_kind (t0
) == STOK_LEFTPAR
&&
2292 state_token_is_name (t1
, "!srcdir"))
2294 next_state_tokens (2);
2295 t0
= peek_state_token (0);
2296 t1
= peek_state_token (1);
2297 if (state_token_kind (t0
) == STOK_STRING
&&
2298 state_token_kind (t1
) == STOK_RIGHTPAR
)
2300 srcdir
= xstrdup (t0
->stok_un
.stok_string
);
2301 srcdir_len
= strlen (srcdir
);
2302 next_state_tokens (2);
2307 fatal_reading_state (t0
, "Bad srcdir in state_file");
2311 /* Read the sequence of GCC front-end languages. */
2313 read_state_languages (void)
2315 struct state_token_st
*t0
= peek_state_token (0);
2316 struct state_token_st
*t1
= peek_state_token (1);
2317 struct state_token_st
*t2
= peek_state_token (2);
2318 if (state_token_kind (t0
) == STOK_LEFTPAR
2319 && state_token_is_name (t1
, "!languages")
2320 && state_token_kind (t2
) == STOK_INTEGER
)
2323 num_lang_dirs
= t2
->stok_un
.stok_num
;
2324 lang_dir_names
= XCNEWVEC (const char *, num_lang_dirs
);
2325 next_state_tokens (3);
2326 t0
= t1
= t2
= NULL
;
2327 for (i
= 0; i
< (int) num_lang_dirs
; i
++)
2329 t0
= peek_state_token (0);
2330 if (state_token_kind (t0
) != STOK_NAME
)
2331 fatal_reading_state (t0
, "expecting language name in state file");
2332 lang_dir_names
[i
] = t0
->stok_un
.stok_ident
->stid_name
;
2333 next_state_tokens (1);
2335 t0
= peek_state_token (0);
2336 if (state_token_kind (t0
) != STOK_RIGHTPAR
)
2337 fatal_reading_state (t0
, "missing ) in languages list of state file");
2338 next_state_tokens (1);
2341 fatal_reading_state (t0
, "expecting languages list in state file");
2345 /* Read the sequence of files. */
2347 read_state_files_list (void)
2349 struct state_token_st
*t0
= peek_state_token (0);
2350 struct state_token_st
*t1
= peek_state_token (1);
2351 struct state_token_st
*t2
= peek_state_token (2);
2353 if (state_token_kind (t0
) == STOK_LEFTPAR
2354 && state_token_is_name (t1
, "!fileslist")
2355 && state_token_kind (t2
) == STOK_INTEGER
)
2358 num_gt_files
= t2
->stok_un
.stok_num
;
2359 next_state_tokens (3);
2360 t0
= t1
= t2
= NULL
;
2361 gt_files
= XCNEWVEC (const input_file
*, num_gt_files
);
2362 for (i
= 0; i
< (int) num_gt_files
; i
++)
2364 bool issrcfile
= FALSE
;
2365 t0
= t1
= t2
= NULL
;
2366 t0
= peek_state_token (0);
2367 t1
= peek_state_token (1);
2368 t2
= peek_state_token (2);
2369 if (state_token_kind (t0
) == STOK_LEFTPAR
2370 && (state_token_is_name (t1
, "!file")
2371 || (issrcfile
= state_token_is_name (t1
, "!srcfile")))
2372 && state_token_kind (t2
) == STOK_INTEGER
)
2374 lang_bitmap bmap
= t2
->stok_un
.stok_num
;
2375 next_state_tokens (3);
2376 t0
= t1
= t2
= NULL
;
2377 t0
= peek_state_token (0);
2378 t1
= peek_state_token (1);
2379 if (state_token_kind (t0
) == STOK_STRING
2380 && state_token_kind (t1
) == STOK_RIGHTPAR
)
2382 const char *fnam
= t0
->stok_un
.stok_string
;
2383 /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
2384 input_file
*curgt
= NULL
;
2387 static const char dirsepstr
[2] =
2388 { DIR_SEPARATOR
, (char) 0 };
2389 char *fullpath
= concat (srcdir
, dirsepstr
, fnam
, NULL
);
2390 curgt
= input_file_by_name (fullpath
);
2394 curgt
= input_file_by_name (fnam
);
2395 set_lang_bitmap (curgt
, bmap
);
2396 gt_files
[i
] = curgt
;
2397 next_state_tokens (2);
2400 fatal_reading_state (t0
,
2401 "bad file in !fileslist of state file");
2404 fatal_reading_state (t0
,
2405 "expecting file in !fileslist of state file");
2407 t0
= peek_state_token (0);
2408 if (!state_token_kind (t0
) == STOK_RIGHTPAR
)
2409 fatal_reading_state (t0
, "missing ) for !fileslist in state file");
2410 next_state_tokens (1);
2413 fatal_reading_state (t0
, "missing !fileslist in state file");
2417 /* Read the trailer. */
2419 read_state_trailer (void)
2421 struct state_token_st
*t0
= peek_state_token (0);
2422 struct state_token_st
*t1
= peek_state_token (1);
2423 struct state_token_st
*t2
= peek_state_token (2);
2425 if (state_token_kind (t0
) == STOK_LEFTPAR
2426 && state_token_is_name (t1
, "!endfile")
2427 && state_token_kind (t2
) == STOK_RIGHTPAR
)
2428 next_state_tokens (3);
2430 fatal_reading_state (t0
, "missing !endfile in state file");
2434 /* Utility functions for the state_seen_types hash table. */
2436 hash_type_number (const void *ty
)
2438 const struct type
*type
= (const struct type
*) ty
;
2440 return type
->state_number
;
2444 equals_type_number (const void *ty1
, const void *ty2
)
2446 const struct type
*type1
= (const struct type
*) ty1
;
2447 const struct type
*type2
= (const struct type
*) ty2
;
2449 return type1
->state_number
== type2
->state_number
;
2453 string_eq (const void *a
, const void *b
)
2455 const char *a0
= (const char *)a
;
2456 const char *b0
= (const char *)b
;
2458 return (strcmp (a0
, b0
) == 0);
2462 /* The function reading the state, called by main from gengtype.c. */
2464 read_state (const char *path
)
2466 state_file
= fopen (path
, "r");
2467 if (state_file
== NULL
)
2468 fatal ("Failed to open state file %s for reading [%s]", path
,
2473 if (verbosity_level
>= 1)
2475 printf ("%s reading state file %s;", progname
, state_path
);
2476 if (verbosity_level
>= 2)
2482 htab_create (2017, hash_type_number
, equals_type_number
, NULL
);
2484 htab_create (4027, htab_hash_string
, string_eq
, NULL
);
2485 read_state_version (version_string
);
2486 read_state_srcdir ();
2487 read_state_languages ();
2488 read_state_files_list ();
2489 read_state_structures (&structures
);
2490 if (ferror (state_file
))
2491 fatal_reading_state_printf
2492 (NULL_STATE_TOKEN
, "input error while reading state [%s]",
2494 read_state_typedefs (&typedefs
);
2495 read_state_param_structs (¶m_structs
);
2496 read_state_variables (&variables
);
2497 read_state_trailer ();
2499 if (verbosity_level
>= 1)
2501 printf ("%s read %ld bytes.\n", progname
, ftell (state_file
));
2505 if (fclose (state_file
))
2506 fatal ("failed to close read state file %s [%s]",
2507 path
, xstrerror (errno
));
2512 /* End of file gengtype-state.c. */