fix single argument static_assert
[official-gcc.git] / gcc / gengtype-state.cc
blobf178ade69746667d2fcd33bb78683b7f41162e61
1 /* Gengtype persistent state serialization & de-serialization.
2 Useful for gengtype in plugin mode.
4 Copyright (C) 2010-2024 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
11 version.
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
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>.
22 Contributed by Jeremie Salvucci <jeremie.salvucci@free.fr>
23 and Basile Starynkevitch <basile@starynkevitch.net>
26 #ifdef HOST_GENERATOR_FILE
27 #include "config.h"
28 #define GENERATOR_FILE 1
29 #else
30 #include "bconfig.h"
31 #endif
32 #include "system.h"
33 #include "errors.h" /* For fatal. */
34 #include "version.h" /* For version_string & pkgversion_string. */
35 #include "obstack.h"
36 #include "gengtype.h"
40 /* Gives the file location of a type, if any. */
41 static inline struct fileloc*
42 type_lineloc (const_type_p ty)
44 if (!ty)
45 return NULL;
46 switch (ty->kind)
48 case TYPE_NONE:
49 gcc_unreachable ();
50 case TYPE_STRUCT:
51 case TYPE_UNION:
52 case TYPE_LANG_STRUCT:
53 case TYPE_USER_STRUCT:
54 case TYPE_UNDEFINED:
55 return CONST_CAST (struct fileloc*, &ty->u.s.line);
56 case TYPE_SCALAR:
57 case TYPE_STRING:
58 case TYPE_POINTER:
59 case TYPE_ARRAY:
60 case TYPE_CALLBACK:
61 return NULL;
62 default:
63 gcc_unreachable ();
67 /* The state file has simplistic lispy lexical tokens. Its lexer gives
68 a linked list of struct state_token_st, through the peek_state_token
69 function. Lexical tokens are consumed with next_state_tokens. */
72 /* The lexical kind of each lispy token. */
73 enum state_token_en
75 STOK_NONE, /* Never used. */
76 STOK_INTEGER, /* Integer token. */
77 STOK_STRING, /* String token. */
78 STOK_LEFTPAR, /* Left opening parenthesis. */
79 STOK_RIGHTPAR, /* Right closing parenthesis. */
80 STOK_NAME /* hash-consed name or identifier. */
84 /* Structure and hash-table used to share identifiers or names. */
85 struct state_ident_st
87 /* TODO: We could improve the parser by reserving identifiers for
88 state keywords and adding a keyword number for them. That would
89 mean adding another field in this state_ident_st struct. */
90 char stid_name[1]; /* actually bigger & null terminated */
92 static htab_t state_ident_tab;
95 /* The state_token_st structure is for lexical tokens in the read
96 state file. The stok_kind field discriminates the union. Tokens
97 are allocated by peek_state_token which calls read_a_state_token
98 which allocate them. Tokens are freed by calls to
99 next_state_tokens. Token are organized in a FIFO look-ahead queue
100 filled by peek_state_token. */
101 struct state_token_st
103 enum state_token_en stok_kind; /* the lexical kind
104 discriminates the stok_un
105 union */
106 int stok_line; /* the line number */
107 int stok_col; /* the column number */
108 const char *stok_file; /* the file path */
109 struct state_token_st *stok_next; /* the next token in the
110 queue, when peeked */
111 union /* discriminated by stok_kind! */
113 int stok_num; /* when STOK_INTEGER */
114 char stok_string[1]; /* when STOK_STRING, actual size is
115 bigger and null terminated */
116 struct state_ident_st *stok_ident; /* when STOK_IDENT */
117 void *stok_ptr; /* null otherwise */
119 stok_un;
125 #define NULL_STATE_TOKEN (struct state_token_st*)0
127 /* the state_token pointer contains the leftmost current token. The
128 tokens are organized in a linked queue, using stok_next, for token
129 look-ahead. */
130 struct state_token_st *state_token = NULL_STATE_TOKEN;
132 /* Used by the reading lexer. */
133 static FILE *state_file;
134 static const char *state_path = NULL;
135 static int state_line = 0;
136 static long state_bol = 0; /* offset of beginning of line */
138 /* A class for writing out s-expressions, keeping track of newlines and
139 nested indentation. */
140 class s_expr_writer
142 public:
143 s_expr_writer ();
145 void write_new_line ();
146 void write_any_indent (int leading_spaces);
148 void begin_s_expr (const char *tag);
149 void end_s_expr ();
151 private:
152 int m_indent_amount;
153 int m_had_recent_newline;
154 }; // class s_expr_writer
156 /* A class for writing out "gtype.state". */
157 class state_writer : public s_expr_writer
159 public:
160 state_writer ();
162 private:
163 void write_state_fileloc (struct fileloc *floc);
164 void write_state_fields (pair_p fields);
165 void write_state_a_string (const char *s);
166 void write_state_string_option (options_p current);
167 void write_state_type_option (options_p current);
168 void write_state_nested_option (options_p current);
169 void write_state_option (options_p current);
170 void write_state_options (options_p opt);
171 void write_state_lang_bitmap (lang_bitmap bitmap);
172 void write_state_version (const char *version);
173 void write_state_scalar_type (type_p current);
174 void write_state_string_type (type_p current);
175 void write_state_callback_type (type_p current);
176 void write_state_undefined_type (type_p current);
177 void write_state_struct_union_type (type_p current, const char *kindstr);
178 void write_state_struct_type (type_p current);
179 void write_state_user_struct_type (type_p current);
180 void write_state_union_type (type_p current);
181 void write_state_lang_struct_type (type_p current);
182 void write_state_pointer_type (type_p current);
183 void write_state_array_type (type_p current);
184 void write_state_gc_used (enum gc_used_enum gus);
185 void write_state_common_type_content (type_p current);
186 void write_state_type (type_p current);
187 void write_state_pair (pair_p current);
188 int write_state_pair_list (pair_p list);
189 void write_state_typedefs (void);
190 void write_state_structures (void);
191 void write_state_variables (void);
192 void write_state_srcdir (void);
193 void write_state_files_list (void);
194 void write_state_languages (void);
196 friend void write_state (const char *state_path);
198 private:
199 /* Counter of written types. */
200 int m_state_written_type_count;
201 }; // class state_writer
204 /* class s_expr_writer's trivial constructor. */
205 s_expr_writer::s_expr_writer ()
206 : m_indent_amount (0),
207 m_had_recent_newline (0)
211 /* Write a newline to the output file, merging adjacent newlines. */
212 void
213 s_expr_writer::write_new_line (void)
215 /* Don't add a newline if we've just had one. */
216 if (!m_had_recent_newline)
218 fprintf (state_file, "\n");
219 m_had_recent_newline = 1;
223 /* If we've just had a newline, write the indentation amount, potentially
224 omitting some spaces.
226 LEADING_SPACES exists to support code that writes strings with leading
227 spaces (e.g " foo") which might occur within a line, or could be the first
228 thing on a line. By passing leading_spaces == 1, when such a string is the
229 first thing on a line, write_any_indent () swallows the successive
230 leading spaces into the indentation so that the "foo" begins at the expected
231 column. */
232 void
233 s_expr_writer::write_any_indent (int leading_spaces)
235 int i;
236 int amount = m_indent_amount - leading_spaces;
237 if (m_had_recent_newline)
238 for (i = 0; i < amount; i++)
239 fprintf (state_file, " ");
240 m_had_recent_newline = 0;
243 /* Write the beginning of a new s-expresion e.g. "(!foo "
244 The writer automatically adds whitespace to show the hierarchical
245 structure of the expressions, so each one starts on a new line,
246 and any within it will be at an increased indentation level. */
247 void
248 s_expr_writer::begin_s_expr (const char *tag)
250 write_new_line ();
251 write_any_indent (0);
252 fprintf (state_file, "(!%s ", tag);
253 m_indent_amount++;
256 /* Write out the end of an s-expression: any necssessary indentation,
257 a closing parenthesis, and a new line. */
258 void
259 s_expr_writer::end_s_expr (void)
261 m_indent_amount--;
262 write_any_indent (0);
263 fprintf (state_file, ")");
264 write_new_line ();
268 /* class state_writer's trivial constructor. */
269 state_writer::state_writer ()
270 : s_expr_writer (),
271 m_state_written_type_count (0)
276 /* Fatal error messages when reading the state. They are extremely
277 unlikely, and only appear when this gengtype-state.cc file is buggy,
278 or when reading a gengtype state which was not generated by the
279 same version of gengtype or GCC. */
282 /* Fatal message while reading state. */
283 static void
284 fatal_reading_state (struct state_token_st* tok, const char*msg)
286 if (tok)
287 fatal ("%s:%d:%d: Invalid state file; %s",
288 tok->stok_file, tok->stok_line, tok->stok_col,
289 msg);
290 else
291 fatal ("%s:%d: Invalid state file; %s",
292 state_path, state_line, msg);
296 /* Fatal printf-like message while reading state. This can't be a
297 function, because there is no way to pass a va_arg to a variant of
298 fatal. */
299 #define fatal_reading_state_printf(Tok,Fmt,...) do { \
300 struct state_token_st* badtok = Tok; \
301 if (badtok) \
302 fatal ("%s:%d:%d: Invalid state file; " Fmt, \
303 badtok->stok_file, \
304 badtok->stok_line, \
305 badtok->stok_col, __VA_ARGS__); \
306 else \
307 fatal ("%s:%d: Invalid state file; " Fmt, \
308 state_path, state_line, __VA_ARGS__); \
309 } while (0)
312 /* Find or allocate an identifier in our name hash table. */
313 static struct state_ident_st *
314 state_ident_by_name (const char *name, enum insert_option optins)
316 void **slot = NULL;
317 int namlen = 0;
318 struct state_ident_st *stid = NULL;
320 if (!name || !name[0])
321 return NULL;
323 slot = htab_find_slot (state_ident_tab, name, optins);
324 if (!slot)
325 return NULL;
327 namlen = strlen (name);
328 stid =
329 (struct state_ident_st *) xmalloc (sizeof (struct state_ident_st) +
330 namlen);
331 memset (stid, 0, sizeof (struct state_ident_st) + namlen);
332 strcpy (stid->stid_name, name);
333 *slot = stid;
335 return stid;
338 /* Our token lexer is heavily inspired by MELT's lexer, and share some
339 code with the file gcc/melt-runtime.c of the GCC MELT branch! We
340 really want the gengtype state to be easily parsable by MELT. This
341 is a usual lispy lexing routine, dealing with spaces and comments,
342 numbers, parenthesis, names, strings. */
343 static struct state_token_st *
344 read_a_state_token (void)
346 int c = 0;
347 long curoff = 0;
348 struct state_token_st *tk = NULL;
350 again: /* Read again, e.g. after a comment or spaces. */
351 c = getc (state_file);
352 if (c == EOF)
353 return NULL;
355 /* Handle spaces, count lines. */
356 if (c == '\n')
358 state_line++;
359 state_bol = curoff = ftell (state_file);
360 goto again;
362 if (ISSPACE (c))
363 goto again;
364 /* Skip comments starting with semi-colon. */
365 if (c == ';')
369 c = getc (state_file);
371 while (c > 0 && c != '\n');
372 if (c == '\n')
374 state_line++;
375 state_bol = curoff = ftell (state_file);
377 goto again;
379 /* Read signed numbers. */
380 if (ISDIGIT (c) || c == '-' || c == '+')
381 { /* number */
382 int n = 0;
383 ungetc (c, state_file);
384 curoff = ftell (state_file);
385 if (fscanf (state_file, "%d", &n) <= 0)
386 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error in number");
387 tk = XCNEW (struct state_token_st);
388 tk->stok_kind = STOK_INTEGER;
389 tk->stok_line = state_line;
390 tk->stok_col = curoff - state_bol;
391 tk->stok_file = state_path;
392 tk->stok_next = NULL;
393 tk->stok_un.stok_num = n;
395 return tk;
397 /* Read an opening left parenthesis. */
398 else if (c == '(')
400 curoff = ftell (state_file);
401 tk = XCNEW (struct state_token_st);
402 tk->stok_kind = STOK_LEFTPAR;
403 tk->stok_line = state_line;
404 tk->stok_col = curoff - state_bol;
405 tk->stok_file = state_path;
406 tk->stok_next = NULL;
408 return tk;
410 /* Read an closing right parenthesis. */
411 else if (c == ')')
413 curoff = ftell (state_file);
414 tk = XCNEW (struct state_token_st);
415 tk->stok_kind = STOK_RIGHTPAR;
416 tk->stok_line = state_line;
417 tk->stok_col = curoff - state_bol;
418 tk->stok_file = state_path;
419 tk->stok_next = NULL;
421 return tk;
423 /* Read identifiers, using an obstack. */
424 else if (ISALPHA (c) || c == '_' || c == '$' || c == '!' || c == '#')
426 struct obstack id_obstack;
427 struct state_ident_st *sid = NULL;
428 char *ids = NULL;
429 obstack_init (&id_obstack);
430 curoff = ftell (state_file);
431 while (ISALNUM (c) || c == '_' || c == '$' || c == '!' || c == '#')
433 obstack_1grow (&id_obstack, c);
434 c = getc (state_file);
435 if (c < 0)
436 break;
438 if (c >= 0)
439 ungetc (c, state_file);
440 obstack_1grow (&id_obstack, (char) 0);
441 ids = XOBFINISH (&id_obstack, char *);
442 sid = state_ident_by_name (ids, INSERT);
443 obstack_free (&id_obstack, NULL);
444 ids = NULL;
445 tk = XCNEW (struct state_token_st);
446 tk->stok_kind = STOK_NAME;
447 tk->stok_line = state_line;
448 tk->stok_col = curoff - state_bol;
449 tk->stok_file = state_path;
450 tk->stok_next = NULL;
451 tk->stok_un.stok_ident = sid;
453 return tk;
455 /* Read a string, dealing with escape sequences a la C! */
456 else if (c == '"')
458 char *cstr = NULL;
459 int cslen = 0;
460 struct obstack bstring_obstack;
461 obstack_init (&bstring_obstack);
462 curoff = ftell (state_file);
463 while ((c = getc (state_file)) != '"' && c >= 0)
465 if (ISPRINT (c) && c != '\\')
466 obstack_1grow (&bstring_obstack, (char) c);
467 else if (ISSPACE (c) && c != '\n')
468 obstack_1grow (&bstring_obstack, (char) c);
469 else if (c == '\\')
471 c = getc (state_file);
472 switch (c)
474 case 'a':
475 obstack_1grow (&bstring_obstack, '\a');
476 break;
477 case 'b':
478 obstack_1grow (&bstring_obstack, '\b');
479 break;
480 case 't':
481 obstack_1grow (&bstring_obstack, '\t');
482 break;
483 case 'n':
484 obstack_1grow (&bstring_obstack, '\n');
485 break;
486 case 'v':
487 obstack_1grow (&bstring_obstack, '\v');
488 break;
489 case 'f':
490 obstack_1grow (&bstring_obstack, '\f');
491 break;
492 case 'r':
493 obstack_1grow (&bstring_obstack, '\r');
494 break;
495 case '"':
496 obstack_1grow (&bstring_obstack, '\"');
497 break;
498 case '\\':
499 obstack_1grow (&bstring_obstack, '\\');
500 break;
501 case ' ':
502 obstack_1grow (&bstring_obstack, ' ');
503 break;
504 case 'x':
506 unsigned int cx = 0;
507 if (fscanf (state_file, "%02x", &cx) > 0 && cx > 0)
508 obstack_1grow (&bstring_obstack, cx);
509 else
510 fatal_reading_state
511 (NULL_STATE_TOKEN,
512 "Lexical error in string hex escape");
513 getc (state_file);
514 break;
516 default:
517 fatal_reading_state
518 (NULL_STATE_TOKEN,
519 "Lexical error - unknown string escape");
522 else
523 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error...");
525 if (c != '"')
526 fatal_reading_state (NULL_STATE_TOKEN, "Unterminated string");
527 obstack_1grow (&bstring_obstack, '\0');
528 cstr = XOBFINISH (&bstring_obstack, char *);
529 cslen = strlen (cstr);
530 tk = (struct state_token_st *)
531 xcalloc (sizeof (struct state_token_st) + cslen, 1);
532 tk->stok_kind = STOK_STRING;
533 tk->stok_line = state_line;
534 tk->stok_col = curoff - state_bol;
535 tk->stok_file = state_path;
536 tk->stok_next = NULL;
537 strcpy (tk->stok_un.stok_string, cstr);
538 obstack_free (&bstring_obstack, NULL);
540 return tk;
542 /* Got an unexpected character. */
543 fatal_reading_state_printf
544 (NULL_STATE_TOKEN,
545 "Lexical error at offset %ld - bad character \\%03o = '%c'",
546 ftell (state_file), c, c);
549 /* Used for lexical look-ahead. Retrieves the lexical token of rank
550 DEPTH, starting with 0 when reading the state file. Gives null on
551 end of file. */
552 static struct state_token_st *
553 peek_state_token (int depth)
555 int remdepth = depth;
556 struct state_token_st **ptoken = &state_token;
557 struct state_token_st *tok = NULL;
559 while (remdepth >= 0)
561 if (*ptoken == NULL)
563 *ptoken = tok = read_a_state_token ();
564 if (tok == NULL)
565 return NULL;
567 tok = *ptoken;
568 ptoken = &((*ptoken)->stok_next);
569 remdepth--;
572 return tok;
575 /* Consume the next DEPTH tokens and free them. */
576 static void
577 next_state_tokens (int depth)
579 struct state_token_st *n;
581 while (depth > 0)
583 if (state_token != NULL)
585 n = state_token->stok_next;
586 free (state_token);
587 state_token = n;
589 else
590 fatal_reading_state (NULL_STATE_TOKEN, "Tokens stack empty");
592 depth--;
596 /* Safely retrieve the lexical kind of a token. */
597 static inline enum state_token_en
598 state_token_kind (struct state_token_st *p)
600 if (p == NULL)
601 return STOK_NONE;
602 else
603 return p->stok_kind;
606 /* Test if a token is a given name i.e. an identifier. */
607 static inline bool
608 state_token_is_name (struct state_token_st *p, const char *name)
610 if (p == NULL)
611 return false;
613 if (p->stok_kind != STOK_NAME)
614 return false;
616 return !strcmp (p->stok_un.stok_ident->stid_name, name);
620 /* Following routines are useful for serializing datas.
622 * We want to serialize :
623 * - typedefs list
624 * - structures list
625 * - variables list
627 * So, we have one routine for each kind of data. The main writing
628 * routine is write_state. The main reading routine is
629 * read_state. Most writing routines write_state_FOO have a
630 * corresponding reading routine read_state_FOO. Reading is done in a
631 * recursive descending way, and any read error is fatal.
634 /* When reading the state, we need to remember the previously seen
635 types by their state_number, since GTY-ed types are usually
636 shared. */
637 static htab_t state_seen_types;
639 /* Return the length of a linked list made of pairs. */
640 static int pair_list_length (pair_p list);
642 /* Compute the length of a list of pairs, starting from the first
643 one. */
644 static int
645 pair_list_length (pair_p list)
647 int nbpair = 0;
648 pair_p l = NULL;
649 for (l = list; l; l = l->next)
650 nbpair++;
651 return nbpair;
654 /* Write a file location. Files relative to $(srcdir) are quite
655 frequent and are handled specially. This ensures that two gengtype
656 state file-s produced by gengtype on the same GCC source tree are
657 very similar and can be reasonably compared with diff, even if the
658 two GCC source trees have different absolute paths. */
659 void
660 state_writer::write_state_fileloc (struct fileloc *floc)
663 if (floc != NULL && floc->line > 0)
665 const char *srcrelpath = NULL;
666 gcc_assert (floc->file != NULL);
667 /* Most of the files are inside $(srcdir) so it is worth to
668 handle them specially. */
669 srcrelpath = get_file_srcdir_relative_path (floc->file);
670 if (srcrelpath != NULL)
672 begin_s_expr ("srcfileloc");
673 write_state_a_string (srcrelpath);
675 else
677 begin_s_expr ("fileloc");
678 write_state_a_string (get_input_file_name (floc->file));
680 fprintf (state_file, " %d", floc->line);
681 end_s_expr ();
683 else
684 fprintf (state_file, "nil ");
687 /* Write a list of fields. */
688 void
689 state_writer::write_state_fields (pair_p fields)
691 int nbfields = pair_list_length (fields);
692 int nbpairs = 0;
693 begin_s_expr ("fields");
694 fprintf (state_file, "%d ", nbfields);
695 nbpairs = write_state_pair_list (fields);
696 gcc_assert (nbpairs == nbfields);
697 end_s_expr ();
700 /* Write a null-terminated string in our lexical convention, very
701 similar to the convention of C. */
702 void
703 state_writer::write_state_a_string (const char *s)
705 char c;
707 write_any_indent (1);
709 fputs (" \"", state_file);
710 for (; *s != 0; s++)
712 c = *s;
713 switch (c)
715 case '\a':
716 fputs ("\\a", state_file);
717 break;
718 case '\b':
719 fputs ("\\b", state_file);
720 break;
721 case '\t':
722 fputs ("\\t", state_file);
723 break;
724 case '\n':
725 fputs ("\\n", state_file);
726 break;
727 case '\v':
728 fputs ("\\v", state_file);
729 break;
730 case '\f':
731 fputs ("\\f", state_file);
732 break;
733 case '\r':
734 fputs ("\\r", state_file);
735 break;
736 case '\"':
737 fputs ("\\\"", state_file);
738 break;
739 case '\\':
740 fputs ("\\\\", state_file);
741 break;
742 default:
743 if (ISPRINT (c))
744 putc (c, state_file);
745 else
746 fprintf (state_file, "\\x%02x", (unsigned) c);
749 fputs ("\"", state_file);
752 /* Our option-s have three kinds, each with its writer. */
753 void
754 state_writer::write_state_string_option (options_p current)
756 write_any_indent (0);
757 fprintf (state_file, "string ");
758 if (current->info.string != NULL)
759 write_state_a_string (current->info.string);
760 else
761 fprintf (state_file, " nil ");
764 void
765 state_writer::write_state_type_option (options_p current)
767 write_any_indent (0);
768 fprintf (state_file, "type ");
769 write_state_type (current->info.type);
772 void
773 state_writer::write_state_nested_option (options_p current)
775 write_any_indent (0);
776 fprintf (state_file, "nested ");
777 write_state_type (current->info.nested->type);
778 if (current->info.nested->convert_from != NULL)
779 write_state_a_string (current->info.nested->convert_from);
780 else
782 write_any_indent (1);
783 fprintf (state_file, " nil ");
786 if (current->info.nested->convert_to != NULL)
787 write_state_a_string (current->info.nested->convert_to);
788 else
790 write_any_indent (1);
791 fprintf (state_file, " nil ");
795 void
796 state_writer::write_state_option (options_p current)
798 begin_s_expr ("option");
800 write_any_indent (0);
801 if (current->name != NULL)
802 fprintf (state_file, "%s ", current->name);
803 else
804 fprintf (state_file, "nil ");
806 switch (current->kind)
808 case OPTION_STRING:
809 write_state_string_option (current);
810 break;
811 case OPTION_TYPE:
812 write_state_type_option (current);
813 break;
814 case OPTION_NESTED:
815 write_state_nested_option (current);
816 break;
817 default:
818 fatal ("Option tag unknown");
821 /* Terminate the "option" s-expression. */
822 end_s_expr ();
827 /* Write a list of GTY options. */
828 void
829 state_writer::write_state_options (options_p opt)
831 options_p current;
833 if (opt == NULL)
835 write_any_indent (0);
836 fprintf (state_file, "nil ");
837 return;
840 begin_s_expr ("options");
841 for (current = opt; current != NULL; current = current->next)
842 write_state_option (current);
843 end_s_expr ();
847 /* Write a bitmap representing a set of GCC front-end languages. */
848 void
849 state_writer::write_state_lang_bitmap (lang_bitmap bitmap)
851 write_any_indent (0);
852 fprintf (state_file, "%d ", (int) bitmap);
855 /* Write version information. */
856 void
857 state_writer::write_state_version (const char *version)
859 begin_s_expr ("version");
860 write_state_a_string (version);
861 end_s_expr ();
864 /* Write a scalar type. We have only two of these. */
865 void
866 state_writer::write_state_scalar_type (type_p current)
868 write_any_indent (0);
869 if (current == &scalar_nonchar)
870 fprintf (state_file, "scalar_nonchar ");
871 else if (current == &scalar_char)
872 fprintf (state_file, "scalar_char ");
873 else
874 fatal ("Unexpected type in write_state_scalar_type");
876 write_state_common_type_content (current);
879 /* Write the string type. There is only one such thing! */
880 void
881 state_writer::write_state_string_type (type_p current)
883 if (current == &string_type)
885 write_any_indent (0);
886 fprintf (state_file, "string ");
887 write_state_common_type_content (current);
889 else
890 fatal ("Unexpected type in write_state_string_type");
893 /* Write the callback type. There is only one such thing! */
894 void
895 state_writer::write_state_callback_type (type_p current)
897 if (current == &callback_type)
899 write_any_indent (0);
900 fprintf (state_file, "callback ");
901 write_state_common_type_content (current);
903 else
904 fatal ("Unexpected type in write_state_callback_type");
907 /* Write an undefined type. */
908 void
909 state_writer::write_state_undefined_type (type_p current)
911 DBGPRINTF ("undefined type @ %p #%d '%s'", (void *) current,
912 current->state_number, current->u.s.tag);
913 write_any_indent (0);
914 fprintf (state_file, "undefined ");
915 gcc_assert (current->gc_used == GC_UNUSED);
916 write_state_common_type_content (current);
917 if (current->u.s.tag != NULL)
918 write_state_a_string (current->u.s.tag);
919 else
921 write_any_indent (0);
922 fprintf (state_file, "nil");
925 write_state_fileloc (type_lineloc (current));
929 /* Common code to write structure like types. */
930 void
931 state_writer::write_state_struct_union_type (type_p current,
932 const char *kindstr)
934 DBGPRINTF ("%s type @ %p #%d '%s'", kindstr, (void *) current,
935 current->state_number, current->u.s.tag);
936 write_any_indent (0);
937 fprintf (state_file, "%s ", kindstr);
938 write_state_common_type_content (current);
939 if (current->u.s.tag != NULL)
940 write_state_a_string (current->u.s.tag);
941 else
943 write_any_indent (0);
944 fprintf (state_file, "nil");
947 write_state_fileloc (type_lineloc (current));
948 write_state_fields (current->u.s.fields);
949 write_state_options (current->u.s.opt);
950 write_state_lang_bitmap (current->u.s.bitmap);
954 /* Write a GTY struct type. */
955 void
956 state_writer::write_state_struct_type (type_p current)
958 write_state_struct_union_type (current, "struct");
959 write_state_type (current->u.s.lang_struct);
960 write_state_type (current->u.s.base_class);
963 /* Write a GTY user-defined struct type. */
964 void
965 state_writer::write_state_user_struct_type (type_p current)
967 DBGPRINTF ("user_struct type @ %p #%d '%s'", (void *) current,
968 current->state_number, current->u.s.tag);
969 write_any_indent (0);
970 fprintf (state_file, "user_struct ");
971 write_state_common_type_content (current);
972 if (current->u.s.tag != NULL)
973 write_state_a_string (current->u.s.tag);
974 else
976 write_any_indent (0);
977 fprintf (state_file, "nil");
979 write_state_fileloc (type_lineloc (current));
980 write_state_fields (current->u.s.fields);
983 /* write a GTY union type. */
984 void
985 state_writer::write_state_union_type (type_p current)
987 write_state_struct_union_type (current, "union");
988 write_state_type (current->u.s.lang_struct);
991 /* Write a lang_struct type. This is tricky and was painful to debug,
992 we deal with the next field specifically within their lang_struct
993 subfield, which points to a linked list of homonumous types.
994 Change this function with extreme care, see also
995 read_state_lang_struct_type. */
996 void
997 state_writer::write_state_lang_struct_type (type_p current)
999 int nbhomontype = 0;
1000 type_p hty = NULL;
1001 const char *homoname = 0;
1002 write_state_struct_union_type (current, "lang_struct");
1003 /* lang_struct-ures are particularly tricky, since their
1004 u.s.lang_struct field gives a list of homonymous struct-s or
1005 union-s! */
1006 DBGPRINTF ("lang_struct @ %p #%d", (void *) current, current->state_number);
1007 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
1009 nbhomontype++;
1010 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype,
1011 (void *) hty, hty->state_number, hty->u.s.tag);
1012 /* Every member of the homonymous list should have the same tag. */
1013 gcc_assert (union_or_struct_p (hty));
1014 gcc_assert (hty->u.s.lang_struct == current);
1015 if (!homoname)
1016 homoname = hty->u.s.tag;
1017 gcc_assert (strcmp (homoname, hty->u.s.tag) == 0);
1019 begin_s_expr ("homotypes");
1020 fprintf (state_file, "%d", nbhomontype);
1021 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
1022 write_state_type (hty);
1023 end_s_expr ();
1026 /* Write a pointer type. */
1027 void
1028 state_writer::write_state_pointer_type (type_p current)
1030 write_any_indent (0);
1031 fprintf (state_file, "pointer ");
1032 write_state_common_type_content (current);
1033 write_state_type (current->u.p);
1036 /* Write an array type. */
1037 void
1038 state_writer::write_state_array_type (type_p current)
1040 write_any_indent (0);
1041 fprintf (state_file, "array ");
1042 write_state_common_type_content (current);
1043 if (current->u.a.len != NULL)
1044 write_state_a_string (current->u.a.len);
1045 else
1047 write_any_indent (1);
1048 fprintf (state_file, " nil");
1051 write_any_indent (1);
1052 fprintf (state_file, " ");
1053 write_state_type (current->u.a.p);
1056 /* Write the gc_used information. */
1057 void
1058 state_writer::write_state_gc_used (enum gc_used_enum gus)
1060 write_any_indent (1);
1061 switch (gus)
1063 case GC_UNUSED:
1064 fprintf (state_file, " gc_unused");
1065 break;
1066 case GC_USED:
1067 fprintf (state_file, " gc_used");
1068 break;
1069 case GC_MAYBE_POINTED_TO:
1070 fprintf (state_file, " gc_maybe_pointed_to");
1071 break;
1072 case GC_POINTED_TO:
1073 fprintf (state_file, " gc_pointed_to");
1074 break;
1075 default:
1076 gcc_unreachable ();
1080 /* Utility routine to write the common content of all types. Notice
1081 that the next field is *not* written on purpose. */
1082 void
1083 state_writer::write_state_common_type_content (type_p current)
1085 write_any_indent (0);
1086 fprintf (state_file, "%d ", current->state_number);
1087 /* We do not write the next type, because list of types are
1088 explicitly written. However, lang_struct are special in that
1089 respect. See function write_state_lang_struct_type for more. */
1090 write_state_type (current->pointer_to);
1091 write_state_gc_used (current->gc_used);
1095 /* The important and recursive routine writing GTY types as understood
1096 by gengtype. Types which have a positive state_number have already
1097 been seen and written. */
1098 void
1099 state_writer::write_state_type (type_p current)
1101 write_any_indent (0);
1102 if (current == NULL)
1104 fprintf (state_file, "nil ");
1105 return;
1108 begin_s_expr ("type");
1110 if (current->state_number > 0)
1112 write_any_indent (0);
1113 fprintf (state_file, "already_seen %d", current->state_number);
1115 else
1117 m_state_written_type_count++;
1118 DBGPRINTF ("writing type #%d @%p old number %d", m_state_written_type_count,
1119 (void *) current, current->state_number);
1120 current->state_number = m_state_written_type_count;
1121 switch (current->kind)
1123 case TYPE_NONE:
1124 gcc_unreachable ();
1125 case TYPE_UNDEFINED:
1126 write_state_undefined_type (current);
1127 break;
1128 case TYPE_STRUCT:
1129 write_state_struct_type (current);
1130 break;
1131 case TYPE_USER_STRUCT:
1132 write_state_user_struct_type (current);
1133 break;
1134 case TYPE_UNION:
1135 write_state_union_type (current);
1136 break;
1137 case TYPE_POINTER:
1138 write_state_pointer_type (current);
1139 break;
1140 case TYPE_ARRAY:
1141 write_state_array_type (current);
1142 break;
1143 case TYPE_LANG_STRUCT:
1144 write_state_lang_struct_type (current);
1145 break;
1146 case TYPE_SCALAR:
1147 write_state_scalar_type (current);
1148 break;
1149 case TYPE_STRING:
1150 write_state_string_type (current);
1151 break;
1152 case TYPE_CALLBACK:
1153 write_state_callback_type (current);
1154 break;
1158 /* Terminate the "type" s-expression. */
1159 end_s_expr ();
1163 /* Write a pair. */
1164 void
1165 state_writer::write_state_pair (pair_p current)
1167 if (current == NULL)
1169 write_any_indent (0);
1170 fprintf (state_file, "nil)");
1171 return;
1174 begin_s_expr ("pair");
1176 if (current->name != NULL)
1177 write_state_a_string (current->name);
1178 else
1179 write_state_a_string ("nil");
1181 write_state_type (current->type);
1182 write_state_fileloc (&(current->line));
1183 write_state_options (current->opt);
1185 /* Terminate the "pair" s-expression. */
1186 end_s_expr ();
1189 /* Write a pair list and return the number of pairs written. */
1191 state_writer::write_state_pair_list (pair_p list)
1193 int nbpair = 0;
1194 pair_p current;
1196 for (current = list; current != NULL; current = current->next)
1198 write_state_pair (current);
1199 nbpair++;
1201 return nbpair;
1205 /* When writing imported linked lists, like typedefs, structures, ... we count
1206 their length first and write it. This eases the reading, and enables an
1207 extra verification on the number of actually read items. */
1209 /* Write our typedefs. */
1210 void
1211 state_writer::write_state_typedefs (void)
1213 int nbtypedefs = pair_list_length (typedefs);
1214 int nbpairs = 0;
1215 begin_s_expr ("typedefs");
1216 fprintf (state_file, "%d", nbtypedefs);
1217 nbpairs = write_state_pair_list (typedefs);
1218 gcc_assert (nbpairs == nbtypedefs);
1219 end_s_expr ();
1220 if (verbosity_level >= 2)
1221 printf ("%s wrote %d typedefs\n", progname, nbtypedefs);
1224 /* Write our structures. */
1225 void
1226 state_writer::write_state_structures (void)
1228 int nbstruct = 0;
1229 type_p current;
1231 for (current = structures; current != NULL; current = current->next)
1232 nbstruct++;
1234 begin_s_expr ("structures");
1235 fprintf (state_file, "%d", nbstruct);
1237 for (current = structures; current != NULL; current = current->next)
1239 write_new_line ();
1240 write_state_type (current);
1243 /* Terminate the "structures" s-expression. */
1244 end_s_expr ();
1245 if (verbosity_level >= 2)
1246 printf ("%s wrote %d structures in state\n", progname, nbstruct);
1249 /* Write our variables. */
1250 void
1251 state_writer::write_state_variables (void)
1253 int nbvars = pair_list_length (variables);
1254 int nbpairs = 0;
1255 begin_s_expr ("variables");
1256 fprintf (state_file, "%d", nbvars);
1257 nbpairs = write_state_pair_list (variables);
1258 gcc_assert (nbpairs == nbvars);
1259 end_s_expr ();
1260 if (verbosity_level >= 2)
1261 printf ("%s wrote %d variables.\n", progname, nbvars);
1264 /* Write the source directory. File locations within the source
1265 directory have been written specifically. */
1266 void
1267 state_writer::write_state_srcdir (void)
1269 begin_s_expr ("srcdir");
1270 write_state_a_string (srcdir);
1271 end_s_expr ();
1274 /* Count and write the list of our files. */
1275 void
1276 state_writer::write_state_files_list (void)
1278 int i = 0;
1279 /* Write the list of files with their lang_bitmap. */
1280 begin_s_expr ("fileslist");
1281 fprintf (state_file, "%d %d", (int) num_gt_files, (int) num_build_headers);
1282 for (i = 0; i < (int) num_gt_files; i++)
1284 const char *cursrcrelpath = NULL;
1285 const input_file *curfil = gt_files[i];
1286 /* Most of the files are inside $(srcdir) so it is worth to
1287 handle them specially. */
1288 cursrcrelpath = get_file_srcdir_relative_path (curfil);
1289 if (cursrcrelpath)
1291 begin_s_expr ("srcfile");
1292 fprintf (state_file, "%d ", get_lang_bitmap (curfil));
1293 write_state_a_string (cursrcrelpath);
1295 else
1297 begin_s_expr ("file");
1298 fprintf (state_file, "%d ", get_lang_bitmap (curfil));
1299 write_state_a_string (get_input_file_name (curfil));
1301 /* Terminate the inner s-expression (either "srcfile" or "file"). */
1302 end_s_expr ();
1304 /* Terminate the "fileslist" s-expression. */
1305 end_s_expr ();
1308 /* Write the list of GCC front-end languages. */
1309 void
1310 state_writer::write_state_languages (void)
1312 int i = 0;
1313 begin_s_expr ("languages");
1314 fprintf (state_file, "%d", (int) num_lang_dirs);
1315 for (i = 0; i < (int) num_lang_dirs; i++)
1317 /* Languages names are identifiers, we expect only letters or
1318 underscores or digits in them. In particular, C++ is not a
1319 valid language name, but cp is valid. */
1320 fprintf (state_file, " %s", lang_dir_names[i]);
1322 end_s_expr ();
1325 /* Write the trailer. */
1326 static void
1327 write_state_trailer (void)
1329 /* This test should probably catch IO errors like disk full... */
1330 if (fputs ("\n(!endfile)\n", state_file) == EOF)
1331 fatal ("failed to write state trailer [%s]", xstrerror (errno));
1334 /* The write_state routine is the only writing routine called by main
1335 in gengtype.cc. To avoid messing the state if gengtype is
1336 interrupted or aborted, we write a temporary file and rename it
1337 after having written it in totality. */
1338 void
1339 write_state (const char *state_path)
1341 long statelen = 0;
1342 time_t now = 0;
1343 char *temp_state_path = NULL;
1344 char tempsuffix[40];
1345 time (&now);
1347 /* We write a unique temporary file which is renamed when complete
1348 * only. So even if gengtype is interrupted, the written state file
1349 * won't be partially written, since the temporary file is not yet
1350 * renamed in that case. */
1351 memset (tempsuffix, 0, sizeof (tempsuffix));
1352 snprintf (tempsuffix, sizeof (tempsuffix) - 1, "-%ld-%d.tmp", (long) now,
1353 (int) getpid ());
1354 temp_state_path = concat (state_path, tempsuffix, NULL);
1355 state_file = fopen (temp_state_path, "w");
1356 if (state_file == NULL)
1357 fatal ("Failed to open file %s for writing state: %s",
1358 temp_state_path, xstrerror (errno));
1359 if (verbosity_level >= 3)
1360 printf ("%s writing state file %s temporarily in %s\n",
1361 progname, state_path, temp_state_path);
1362 /* This is the first line of the state. Perhaps the file utility
1363 could know about that, so don't change it often. */
1364 fprintf (state_file, ";;;;@@@@ GCC gengtype state\n");
1365 /* Output a few comments for humans. */
1366 fprintf (state_file,
1367 ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
1368 fprintf (state_file,
1369 ";;; The format of this file is tied to a particular version of GCC.\n");
1370 fprintf (state_file,
1371 ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1372 fprintf (state_file,
1373 ";;; This file should be parsed by the same %s which wrote it.\n",
1374 progname);
1376 state_writer sw;
1378 /* The first non-comment significant line gives the version string. */
1379 sw.write_state_version (version_string);
1380 sw.write_state_srcdir ();
1381 sw.write_state_languages ();
1382 sw.write_state_files_list ();
1383 sw.write_state_structures ();
1384 sw.write_state_typedefs ();
1385 sw.write_state_variables ();
1386 write_state_trailer ();
1387 statelen = ftell (state_file);
1388 if (ferror (state_file))
1389 fatal ("output error when writing state file %s [%s]",
1390 temp_state_path, xstrerror (errno));
1391 if (fclose (state_file))
1392 fatal ("failed to close state file %s [%s]",
1393 temp_state_path, xstrerror (errno));
1394 if (rename (temp_state_path, state_path))
1395 fatal ("failed to rename %s to state file %s [%s]", temp_state_path,
1396 state_path, xstrerror (errno));
1397 free (temp_state_path);
1399 if (verbosity_level >= 1)
1400 printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1401 progname, state_path, statelen, sw.m_state_written_type_count);
1405 /** End of writing routines! The corresponding reading routines follow. **/
1409 /* Forward declarations, since some read_state_* functions are
1410 recursive! */
1411 static void read_state_fileloc (struct fileloc *line);
1412 static void read_state_options (options_p *opt);
1413 static void read_state_type (type_p *current);
1414 static void read_state_pair (pair_p *pair);
1415 /* Return the number of pairs actually read. */
1416 static int read_state_pair_list (pair_p *list);
1417 static void read_state_fields (pair_p *fields);
1418 static void read_state_common_type_content (type_p current);
1423 /* Record into the state_seen_types hash-table a type which we are
1424 reading, to enable recursive or circular references to it. */
1425 static void
1426 record_type (type_p type)
1428 void **slot;
1430 slot = htab_find_slot (state_seen_types, type, INSERT);
1431 gcc_assert (slot);
1433 *slot = type;
1436 /* Read an already seen type. */
1437 static void
1438 read_state_already_seen_type (type_p *type)
1440 struct state_token_st *t0 = peek_state_token (0);
1442 if (state_token_kind (t0) == STOK_INTEGER)
1444 void **slot = NULL;
1445 struct type loctype = { TYPE_SCALAR, 0, 0, 0, GC_UNUSED, {0} };
1447 loctype.state_number = t0->stok_un.stok_num;
1448 slot = htab_find_slot (state_seen_types, &loctype, NO_INSERT);
1449 if (slot == NULL)
1451 fatal_reading_state (t0, "Unknown type");
1454 next_state_tokens (1);
1455 *type = (type_p) *slot;
1457 else
1459 fatal_reading_state (t0, "Bad seen type");
1464 /* Read the scalar_nonchar type. */
1465 static void
1466 read_state_scalar_nonchar_type (type_p *type)
1468 *type = &scalar_nonchar;
1469 read_state_common_type_content (*type);
1473 /* Read the scalar_char type. */
1474 static void
1475 read_state_scalar_char_type (type_p *type)
1477 *type = &scalar_char;
1478 read_state_common_type_content (*type);
1481 /* Read the string_type. */
1482 static void
1483 read_state_string_type (type_p *type)
1485 *type = &string_type;
1486 read_state_common_type_content (*type);
1489 /* Read the callback_type. */
1490 static void
1491 read_state_callback_type (type_p *type)
1493 *type = &callback_type;
1494 read_state_common_type_content (*type);
1498 /* Read a lang_bitmap representing a set of GCC front-end languages. */
1499 static void
1500 read_state_lang_bitmap (lang_bitmap *bitmap)
1502 struct state_token_st *t;
1504 t = peek_state_token (0);
1505 if (state_token_kind (t) == STOK_INTEGER)
1507 *bitmap = t->stok_un.stok_num;
1508 next_state_tokens (1);
1510 else
1512 fatal_reading_state (t, "Bad syntax for bitmap");
1517 /* Read an undefined type. */
1518 static void
1519 read_state_undefined_type (type_p type)
1521 struct state_token_st *t0;
1523 type->kind = TYPE_UNDEFINED;
1524 read_state_common_type_content (type);
1525 t0 = peek_state_token (0);
1526 if (state_token_kind (t0) == STOK_STRING)
1528 if (state_token_is_name (t0, "nil"))
1530 type->u.s.tag = NULL;
1531 DBGPRINTF ("read anonymous undefined type @%p #%d",
1532 (void *) type, type->state_number);
1534 else
1536 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1537 DBGPRINTF ("read undefined type @%p #%d '%s'",
1538 (void *) type, type->state_number, type->u.s.tag);
1541 next_state_tokens (1);
1542 read_state_fileloc (&(type->u.s.line));
1544 else
1546 fatal_reading_state (t0, "Bad tag in undefined type");
1551 /* Read a GTY-ed struct type. */
1552 static void
1553 read_state_struct_type (type_p type)
1555 struct state_token_st *t0;
1557 type->kind = TYPE_STRUCT;
1558 read_state_common_type_content (type);
1559 t0 = peek_state_token (0);
1560 if (state_token_kind (t0) == STOK_STRING)
1562 if (state_token_is_name (t0, "nil"))
1564 type->u.s.tag = NULL;
1565 DBGPRINTF ("read anonymous struct type @%p #%d",
1566 (void *) type, type->state_number);
1568 else
1570 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1571 DBGPRINTF ("read struct type @%p #%d '%s'",
1572 (void *) type, type->state_number, type->u.s.tag);
1575 next_state_tokens (1);
1576 read_state_fileloc (&(type->u.s.line));
1577 read_state_fields (&(type->u.s.fields));
1578 read_state_options (&(type->u.s.opt));
1579 read_state_lang_bitmap (&(type->u.s.bitmap));
1580 read_state_type (&(type->u.s.lang_struct));
1581 read_state_type (&(type->u.s.base_class));
1582 if (type->u.s.base_class)
1583 add_subclass (type->u.s.base_class, type);
1585 else
1587 fatal_reading_state (t0, "Bad tag in struct type");
1592 /* Read a GTY-ed user-provided struct TYPE. */
1594 static void
1595 read_state_user_struct_type (type_p type)
1597 struct state_token_st *t0;
1599 type->kind = TYPE_USER_STRUCT;
1600 read_state_common_type_content (type);
1601 t0 = peek_state_token (0);
1602 if (state_token_kind (t0) == STOK_STRING)
1604 if (state_token_is_name (t0, "nil"))
1606 type->u.s.tag = NULL;
1607 DBGPRINTF ("read anonymous struct type @%p #%d",
1608 (void *) type, type->state_number);
1610 else
1612 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1613 DBGPRINTF ("read struct type @%p #%d '%s'",
1614 (void *) type, type->state_number, type->u.s.tag);
1617 next_state_tokens (1);
1618 read_state_fileloc (&(type->u.s.line));
1619 read_state_fields (&(type->u.s.fields));
1621 else
1623 fatal_reading_state (t0, "Bad tag in user-struct type");
1628 /* Read a GTY-ed union type. */
1629 static void
1630 read_state_union_type (type_p type)
1632 struct state_token_st *t0;
1634 type->kind = TYPE_UNION;
1635 read_state_common_type_content (type);
1636 t0 = peek_state_token (0);
1637 if (state_token_kind (t0) == STOK_STRING)
1639 if (state_token_is_name (t0, "nil"))
1641 type->u.s.tag = NULL;
1642 DBGPRINTF ("read anonymous union type @%p #%d",
1643 (void *) type, type->state_number);
1645 else
1647 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1648 DBGPRINTF ("read union type @%p #%d '%s'",
1649 (void *) type, type->state_number, type->u.s.tag);
1651 next_state_tokens (1);
1652 read_state_fileloc (&(type->u.s.line));
1653 read_state_fields (&(type->u.s.fields));
1654 read_state_options (&(type->u.s.opt));
1655 read_state_lang_bitmap (&(type->u.s.bitmap));
1656 read_state_type (&(type->u.s.lang_struct));
1658 else
1659 fatal_reading_state (t0, "Bad tag in union type");
1663 /* Read a GTY-ed pointer type. */
1664 static void
1665 read_state_pointer_type (type_p type)
1667 type->kind = TYPE_POINTER;
1668 read_state_common_type_content (type);
1669 DBGPRINTF ("read pointer type @%p #%d", (void *) type, type->state_number);
1670 read_state_type (&(type->u.p));
1674 /* Read a GTY-ed array type. */
1675 static void
1676 read_state_array_type (type_p type)
1678 struct state_token_st *t0;
1680 type->kind = TYPE_ARRAY;
1681 read_state_common_type_content (type);
1682 t0 = peek_state_token (0);
1683 if (state_token_kind (t0) == STOK_STRING)
1685 type->u.a.len = xstrdup (t0->stok_un.stok_string);
1686 DBGPRINTF ("read array type @%p #%d length '%s'",
1687 (void *) type, type->state_number, type->u.a.len);
1688 next_state_tokens (1);
1691 else if (state_token_is_name (t0, "nil"))
1693 type->u.a.len = NULL;
1694 DBGPRINTF ("read array type @%p #%d without length",
1695 (void *) type, type->state_number);
1696 next_state_tokens (1);
1699 else
1700 fatal_reading_state (t0, "Bad array name type");
1701 read_state_type (&(type->u.a.p));
1706 /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
1707 front-end languages. This is a tricky function and it was painful
1708 to debug. Change it with extreme care. See also
1709 write_state_lang_struct_type. */
1710 static void
1711 read_state_lang_struct_type (type_p type)
1713 struct state_token_st *t0 = NULL;
1714 struct state_token_st *t1 = NULL;
1715 struct state_token_st *t2 = NULL;
1717 type->kind = TYPE_LANG_STRUCT;
1718 read_state_common_type_content (type);
1719 t0 = peek_state_token (0);
1720 if (state_token_kind (t0) == STOK_STRING)
1722 if (state_token_is_name (t0, "nil"))
1724 DBGPRINTF ("read anonymous lang_struct type @%p #%d",
1725 (void *) type, type->state_number);
1726 type->u.s.tag = NULL;
1728 else
1730 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1731 DBGPRINTF ("read lang_struct type @%p #%d '%s'",
1732 (void *) type, type->state_number, type->u.s.tag);
1734 next_state_tokens (1);
1736 else
1737 fatal_reading_state (t0, "Bad tag in lang struct type");
1738 read_state_fileloc (&(type->u.s.line));
1739 read_state_fields (&(type->u.s.fields));
1740 read_state_options (&(type->u.s.opt));
1741 read_state_lang_bitmap (&(type->u.s.bitmap));
1742 /* Within lang_struct-ures, the lang_struct field is a linked list
1743 of homonymous types! */
1744 t0 = peek_state_token (0);
1745 t1 = peek_state_token (1);
1746 t2 = peek_state_token (2);
1747 /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
1748 if (state_token_kind (t0) == STOK_LEFTPAR
1749 && state_token_is_name (t1, "!homotypes")
1750 && state_token_kind (t2) == STOK_INTEGER)
1752 type_p *prevty = &type->u.s.lang_struct;
1753 int nbhomotype = t2->stok_un.stok_num;
1754 int i = 0;
1755 t0 = t1 = t2 = NULL;
1756 next_state_tokens (3);
1757 for (i = 0; i < nbhomotype; i++)
1759 read_state_type (prevty);
1760 t0 = peek_state_token (0);
1761 if (*prevty)
1762 prevty = &(*prevty)->next;
1763 else
1764 fatal_reading_state (t0,
1765 "expecting type in homotype list for lang_struct");
1767 if (state_token_kind (t0) != STOK_RIGHTPAR)
1768 fatal_reading_state (t0,
1769 "expecting ) in homotype list for lang_struct");
1770 next_state_tokens (1);
1772 else
1773 fatal_reading_state (t0, "expecting !homotypes for lang_struct");
1777 /* Read the gc used information. */
1778 static void
1779 read_state_gc_used (enum gc_used_enum *pgus)
1781 struct state_token_st *t0 = peek_state_token (0);
1782 if (state_token_is_name (t0, "gc_unused"))
1783 *pgus = GC_UNUSED;
1784 else if (state_token_is_name (t0, "gc_used"))
1785 *pgus = GC_USED;
1786 else if (state_token_is_name (t0, "gc_maybe_pointed_to"))
1787 *pgus = GC_MAYBE_POINTED_TO;
1788 else if (state_token_is_name (t0, "gc_pointed_to"))
1789 *pgus = GC_POINTED_TO;
1790 else
1791 fatal_reading_state (t0, "invalid gc_used information");
1792 next_state_tokens (1);
1796 /* Utility function to read the common content of types. */
1797 static void
1798 read_state_common_type_content (type_p current)
1800 struct state_token_st *t0 = peek_state_token (0);
1802 if (state_token_kind (t0) == STOK_INTEGER)
1804 current->state_number = t0->stok_un.stok_num;
1805 next_state_tokens (1);
1806 record_type (current);
1808 else
1809 fatal_reading_state_printf (t0,
1810 "Expected integer for state_number line %d",
1811 state_line);
1812 /* We don't read the next field of the type. */
1813 read_state_type (&current->pointer_to);
1814 read_state_gc_used (&current->gc_used);
1818 /* Read a GTY-ed type. */
1819 void
1820 read_state_type (type_p *current)
1822 struct state_token_st *t0 = peek_state_token (0);
1823 struct state_token_st *t1 = peek_state_token (1);
1825 if (state_token_kind (t0) == STOK_LEFTPAR &&
1826 state_token_is_name (t1, "!type"))
1828 next_state_tokens (2);
1829 t0 = peek_state_token (0);
1830 if (state_token_is_name (t0, "already_seen"))
1832 next_state_tokens (1);
1833 read_state_already_seen_type (current);
1835 else
1837 t0 = peek_state_token (0);
1839 if (state_token_is_name (t0, "scalar_nonchar"))
1841 next_state_tokens (1);
1842 read_state_scalar_nonchar_type (current);
1844 else if (state_token_is_name (t0, "scalar_char"))
1846 next_state_tokens (1);
1847 read_state_scalar_char_type (current);
1849 else if (state_token_is_name (t0, "string"))
1851 next_state_tokens (1);
1852 read_state_string_type (current);
1854 else if (state_token_is_name (t0, "callback"))
1856 next_state_tokens (1);
1857 read_state_callback_type (current);
1859 else if (state_token_is_name (t0, "undefined"))
1861 *current = XCNEW (struct type);
1862 next_state_tokens (1);
1863 read_state_undefined_type (*current);
1865 else if (state_token_is_name (t0, "struct"))
1867 *current = XCNEW (struct type);
1868 next_state_tokens (1);
1869 read_state_struct_type (*current);
1871 else if (state_token_is_name (t0, "union"))
1873 *current = XCNEW (struct type);
1874 next_state_tokens (1);
1875 read_state_union_type (*current);
1877 else if (state_token_is_name (t0, "lang_struct"))
1879 *current = XCNEW (struct type);
1880 next_state_tokens (1);
1881 read_state_lang_struct_type (*current);
1883 else if (state_token_is_name (t0, "pointer"))
1885 *current = XCNEW (struct type);
1886 next_state_tokens (1);
1887 read_state_pointer_type (*current);
1889 else if (state_token_is_name (t0, "array"))
1891 *current = XCNEW (struct type);
1892 next_state_tokens (1);
1893 read_state_array_type (*current);
1895 else if (state_token_is_name (t0, "user_struct"))
1897 *current = XCNEW (struct type);
1898 next_state_tokens (1);
1899 read_state_user_struct_type (*current);
1901 else
1902 fatal_reading_state (t0, "bad type in (!type");
1904 t0 = peek_state_token (0);
1905 if (state_token_kind (t0) != STOK_RIGHTPAR)
1906 fatal_reading_state (t0, "missing ) in type");
1907 next_state_tokens (1);
1909 else if (state_token_is_name (t0, "nil"))
1911 next_state_tokens (1);
1912 *current = NULL;
1914 else
1915 fatal_reading_state (t0, "bad type syntax");
1919 /* Read a file location. Files within the source directory are dealt
1920 with specifically. */
1921 void
1922 read_state_fileloc (struct fileloc *floc)
1924 bool issrcfile = false;
1925 struct state_token_st *t0 = peek_state_token (0);
1926 struct state_token_st *t1 = peek_state_token (1);
1928 gcc_assert (floc != NULL);
1929 gcc_assert (srcdir != NULL);
1931 if (state_token_kind (t0) == STOK_LEFTPAR &&
1932 (state_token_is_name (t1, "!fileloc")
1933 || (issrcfile = state_token_is_name (t1, "!srcfileloc"))))
1935 next_state_tokens (2);
1936 t0 = peek_state_token (0);
1937 t1 = peek_state_token (1);
1938 if (state_token_kind (t0) == STOK_STRING &&
1939 state_token_kind (t1) == STOK_INTEGER)
1941 char *path = t0->stok_un.stok_string;
1942 if (issrcfile)
1944 static const char dirsepstr[2] = { DIR_SEPARATOR, (char) 0 };
1945 char *fullpath = concat (srcdir, dirsepstr, path, NULL);
1946 floc->file = input_file_by_name (fullpath);
1947 free (fullpath);
1949 else
1950 floc->file = input_file_by_name (path);
1951 floc->line = t1->stok_un.stok_num;
1952 next_state_tokens (2);
1954 else
1955 fatal_reading_state (t0,
1956 "Bad fileloc syntax, expected path string and line");
1957 t0 = peek_state_token (0);
1958 if (state_token_kind (t0) != STOK_RIGHTPAR)
1959 fatal_reading_state (t0, "Bad fileloc syntax, expected )");
1960 next_state_tokens (1);
1962 else if (state_token_is_name (t0, "nil"))
1964 next_state_tokens (1);
1965 floc->file = NULL;
1966 floc->line = 0;
1968 else
1969 fatal_reading_state (t0, "Bad fileloc syntax");
1973 /* Read the fields of a GTY-ed type. */
1974 void
1975 read_state_fields (pair_p *fields)
1977 pair_p tmp = NULL;
1978 struct state_token_st *t0 = peek_state_token (0);
1979 struct state_token_st *t1 = peek_state_token (1);
1980 struct state_token_st *t2 = peek_state_token (2);
1982 if (state_token_kind (t0) == STOK_LEFTPAR
1983 && state_token_is_name (t1, "!fields")
1984 && state_token_kind (t2) == STOK_INTEGER)
1986 int nbfields = t2->stok_un.stok_num;
1987 int nbpairs = 0;
1988 next_state_tokens (3);
1989 nbpairs = read_state_pair_list (&tmp);
1990 t0 = peek_state_token (0);
1991 if (nbpairs != nbfields)
1992 fatal_reading_state_printf
1993 (t0,
1994 "Mismatched fields number, expected %d got %d", nbpairs, nbfields);
1995 if (state_token_kind (t0) == STOK_RIGHTPAR)
1996 next_state_tokens (1);
1997 else
1998 fatal_reading_state (t0, "Bad fields expecting )");
2001 *fields = tmp;
2005 /* Read a string option. */
2006 static void
2007 read_state_string_option (options_p opt)
2009 struct state_token_st *t0 = peek_state_token (0);
2010 opt->kind = OPTION_STRING;
2011 if (state_token_kind (t0) == STOK_STRING)
2013 opt->info.string = xstrdup (t0->stok_un.stok_string);
2014 next_state_tokens (1);
2016 else if (state_token_is_name (t0, "nil"))
2018 opt->info.string = NULL;
2019 next_state_tokens (1);
2021 else
2022 fatal_reading_state (t0, "Missing name in string option");
2026 /* Read a type option. */
2027 static void
2028 read_state_type_option (options_p opt)
2030 opt->kind = OPTION_TYPE;
2031 read_state_type (&(opt->info.type));
2035 /* Read a nested option. */
2036 static void
2037 read_state_nested_option (options_p opt)
2039 struct state_token_st *t0;
2041 opt->info.nested = XCNEW (struct nested_ptr_data);
2042 opt->kind = OPTION_NESTED;
2043 read_state_type (&(opt->info.nested->type));
2044 t0 = peek_state_token (0);
2045 if (state_token_kind (t0) == STOK_STRING)
2047 opt->info.nested->convert_from = xstrdup (t0->stok_un.stok_string);
2048 next_state_tokens (1);
2050 else if (state_token_is_name (t0, "nil"))
2052 opt->info.nested->convert_from = NULL;
2053 next_state_tokens (1);
2055 else
2056 fatal_reading_state (t0, "Bad nested convert_from option");
2058 t0 = peek_state_token (0);
2059 if (state_token_kind (t0) == STOK_STRING)
2061 opt->info.nested->convert_to = xstrdup (t0->stok_un.stok_string);
2062 next_state_tokens (1);
2064 else if (state_token_is_name (t0, "nil"))
2066 opt->info.nested->convert_to = NULL;
2067 next_state_tokens (1);
2069 else
2070 fatal_reading_state (t0, "Bad nested convert_from option");
2074 /* Read an GTY option. */
2075 static void
2076 read_state_option (options_p *opt)
2078 struct state_token_st *t0 = peek_state_token (0);
2079 struct state_token_st *t1 = peek_state_token (1);
2081 if (state_token_kind (t0) == STOK_LEFTPAR &&
2082 state_token_is_name (t1, "!option"))
2084 next_state_tokens (2);
2085 t0 = peek_state_token (0);
2086 if (state_token_kind (t0) == STOK_NAME)
2088 *opt = XCNEW (struct options);
2089 if (state_token_is_name (t0, "nil"))
2090 (*opt)->name = NULL;
2091 else
2092 (*opt)->name = t0->stok_un.stok_ident->stid_name;
2093 next_state_tokens (1);
2094 t0 = peek_state_token (0);
2095 if (state_token_kind (t0) == STOK_NAME)
2097 if (state_token_is_name (t0, "string"))
2099 next_state_tokens (1);
2100 read_state_string_option (*opt);
2102 else if (state_token_is_name (t0, "type"))
2104 next_state_tokens (1);
2105 read_state_type_option (*opt);
2107 else if (state_token_is_name (t0, "nested"))
2109 next_state_tokens (1);
2110 read_state_nested_option (*opt);
2112 else
2113 fatal_reading_state (t0, "Bad option type");
2114 t0 = peek_state_token (0);
2115 if (state_token_kind (t0) != STOK_RIGHTPAR)
2116 fatal_reading_state (t0, "Bad syntax in option, expecting )");
2118 next_state_tokens (1);
2120 else
2121 fatal_reading_state (t0, "Missing option type");
2123 else
2124 fatal_reading_state (t0, "Bad name for option");
2126 else
2127 fatal_reading_state (t0, "Bad option, waiting for )");
2130 /* Read a list of options. */
2131 void
2132 read_state_options (options_p *opt)
2134 options_p head = NULL;
2135 options_p previous = NULL;
2136 options_p current_option = NULL;
2137 struct state_token_st *t0 = peek_state_token (0);
2138 struct state_token_st *t1 = peek_state_token (1);
2140 if (state_token_kind (t0) == STOK_LEFTPAR &&
2141 state_token_is_name (t1, "!options"))
2143 next_state_tokens (2);
2144 t0 = peek_state_token (0);
2145 while (state_token_kind (t0) != STOK_RIGHTPAR)
2147 read_state_option (&current_option);
2148 if (head == NULL)
2150 head = current_option;
2151 previous = head;
2153 else
2155 previous->next = current_option;
2156 previous = current_option;
2158 t0 = peek_state_token (0);
2160 next_state_tokens (1);
2162 else if (state_token_is_name (t0, "nil"))
2164 next_state_tokens (1);
2166 else
2167 fatal_reading_state (t0, "Bad options syntax");
2169 *opt = head;
2173 /* Read a version, and check against the version of the gengtype. */
2174 static void
2175 read_state_version (const char *ver_string)
2177 struct state_token_st *t0 = peek_state_token (0);
2178 struct state_token_st *t1 = peek_state_token (1);
2180 if (state_token_kind (t0) == STOK_LEFTPAR &&
2181 state_token_is_name (t1, "!version"))
2183 next_state_tokens (2);
2184 t0 = peek_state_token (0);
2185 t1 = peek_state_token (1);
2186 if (state_token_kind (t0) == STOK_STRING &&
2187 state_token_kind (t1) == STOK_RIGHTPAR)
2189 /* Check that the read version string is the same as current
2190 version. */
2191 if (strcmp (ver_string, t0->stok_un.stok_string))
2192 fatal_reading_state_printf (t0,
2193 "version string mismatch; expecting %s but got %s",
2194 ver_string,
2195 t0->stok_un.stok_string);
2196 next_state_tokens (2);
2198 else
2199 fatal_reading_state (t0, "Missing version or right parenthesis");
2201 else
2202 fatal_reading_state (t0, "Bad version syntax");
2206 /* Read a pair. */
2207 void
2208 read_state_pair (pair_p *current)
2210 struct state_token_st *t0 = peek_state_token (0);
2211 struct state_token_st *t1 = peek_state_token (1);
2212 if (state_token_kind (t0) == STOK_LEFTPAR &&
2213 state_token_is_name (t1, "!pair"))
2215 *current = XCNEW (struct pair);
2216 next_state_tokens (2);
2217 t0 = peek_state_token (0);
2218 if (state_token_kind (t0) == STOK_STRING)
2220 if (strcmp (t0->stok_un.stok_string, "nil") == 0)
2222 (*current)->name = NULL;
2224 else
2226 (*current)->name = xstrdup (t0->stok_un.stok_string);
2228 next_state_tokens (1);
2229 read_state_type (&((*current)->type));
2230 read_state_fileloc (&((*current)->line));
2231 read_state_options (&((*current)->opt));
2232 t0 = peek_state_token (0);
2233 if (state_token_kind (t0) == STOK_RIGHTPAR)
2235 next_state_tokens (1);
2237 else
2239 fatal_reading_state (t0, "Bad syntax for pair, )");
2242 else
2244 fatal_reading_state (t0, "Bad name for pair");
2247 else if (state_token_kind (t0) == STOK_NAME &&
2248 state_token_is_name (t0, "nil"))
2250 next_state_tokens (1);
2251 *current = NULL;
2253 else
2254 fatal_reading_state_printf (t0, "Bad syntax for pair, (!pair %d",
2255 state_token->stok_kind);
2259 /* Return the number of pairs actually read. */
2261 read_state_pair_list (pair_p *list)
2263 int nbpair = 0;
2264 pair_p head = NULL;
2265 pair_p previous = NULL;
2266 pair_p tmp = NULL;
2267 struct state_token_st *t0 = peek_state_token (0);
2268 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2270 read_state_pair (&tmp);
2271 if (head == NULL)
2273 head = tmp;
2274 previous = head;
2276 else
2278 previous->next = tmp;
2279 previous = tmp;
2281 t0 = peek_state_token (0);
2282 nbpair++;
2285 /* don't consume the ); the caller will eat it. */
2286 *list = head;
2287 return nbpair;
2290 /* Read the typedefs. */
2291 static void
2292 read_state_typedefs (pair_p *typedefs)
2294 int nbtypedefs = 0;
2295 pair_p list = NULL;
2296 struct state_token_st *t0 = peek_state_token (0);
2297 struct state_token_st *t1 = peek_state_token (1);
2298 struct state_token_st *t2 = peek_state_token (2);
2300 if (state_token_kind (t0) == STOK_LEFTPAR
2301 && state_token_is_name (t1, "!typedefs")
2302 && state_token_kind (t2) == STOK_INTEGER)
2304 int nbpairs = 0;
2305 nbtypedefs = t2->stok_un.stok_num;
2306 next_state_tokens (3);
2307 nbpairs = read_state_pair_list (&list);
2308 t0 = peek_state_token (0);
2309 if (nbpairs != nbtypedefs)
2310 fatal_reading_state_printf
2311 (t0,
2312 "invalid number of typedefs, expected %d but got %d",
2313 nbtypedefs, nbpairs);
2314 if (state_token_kind (t0) == STOK_RIGHTPAR)
2315 next_state_tokens (1);
2316 else
2317 fatal_reading_state (t0, "Bad typedefs syntax )");
2319 else
2320 fatal_reading_state (t0, "Bad typedefs syntax (!typedefs");
2322 if (verbosity_level >= 2)
2323 printf ("%s read %d typedefs from state\n", progname, nbtypedefs);
2324 *typedefs = list;
2328 /* Read the structures. */
2329 static void
2330 read_state_structures (type_p *structures)
2332 type_p head = NULL;
2333 type_p previous = NULL;
2334 type_p tmp;
2335 int nbstruct = 0, countstruct = 0;
2336 struct state_token_st *t0 = peek_state_token (0);
2337 struct state_token_st *t1 = peek_state_token (1);
2338 struct state_token_st *t2 = peek_state_token (2);
2340 if (state_token_kind (t0) == STOK_LEFTPAR
2341 && state_token_is_name (t1, "!structures")
2342 && state_token_kind (t2) == STOK_INTEGER)
2344 nbstruct = t2->stok_un.stok_num;
2345 next_state_tokens (3);
2346 t0 = peek_state_token (0);
2347 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2349 tmp = NULL;
2350 read_state_type (&tmp);
2351 countstruct++;
2352 if (head == NULL)
2354 head = tmp;
2355 previous = head;
2357 else
2359 previous->next = tmp;
2360 previous = tmp;
2362 t0 = peek_state_token (0);
2364 next_state_tokens (1);
2366 else
2367 fatal_reading_state (t0, "Bad structures syntax");
2368 if (countstruct != nbstruct)
2369 fatal_reading_state_printf (NULL_STATE_TOKEN,
2370 "expected %d structures but got %d",
2371 nbstruct, countstruct);
2372 if (verbosity_level >= 2)
2373 printf ("%s read %d structures from state\n", progname, nbstruct);
2374 *structures = head;
2378 /* Read the variables. */
2379 static void
2380 read_state_variables (pair_p *variables)
2382 pair_p list = NULL;
2383 int nbvars = 0;
2384 struct state_token_st *t0 = peek_state_token (0);
2385 struct state_token_st *t1 = peek_state_token (1);
2386 struct state_token_st *t2 = peek_state_token (2);
2388 if (state_token_kind (t0) == STOK_LEFTPAR
2389 && state_token_is_name (t1, "!variables")
2390 && state_token_kind (t2) == STOK_INTEGER)
2392 int nbpairs = 0;
2393 nbvars = t2->stok_un.stok_num;
2394 next_state_tokens (3);
2395 nbpairs = read_state_pair_list (&list);
2396 t0 = peek_state_token (0);
2397 if (nbpairs != nbvars)
2398 fatal_reading_state_printf
2399 (t0, "Invalid number of variables, expected %d but got %d",
2400 nbvars, nbpairs);
2401 if (state_token_kind (t0) == STOK_RIGHTPAR)
2402 next_state_tokens (1);
2403 else
2404 fatal_reading_state (t0, "Waiting for ) in variables");
2406 else
2407 fatal_reading_state (t0, "Bad variables syntax");
2408 *variables = list;
2409 if (verbosity_level >= 2)
2410 printf ("%s read %d variables from state\n", progname, nbvars);
2414 /* Read the source directory. */
2415 static void
2416 read_state_srcdir (void)
2418 struct state_token_st *t0 = peek_state_token (0);
2419 struct state_token_st *t1 = peek_state_token (1);
2420 if (state_token_kind (t0) == STOK_LEFTPAR &&
2421 state_token_is_name (t1, "!srcdir"))
2423 next_state_tokens (2);
2424 t0 = peek_state_token (0);
2425 t1 = peek_state_token (1);
2426 if (state_token_kind (t0) == STOK_STRING &&
2427 state_token_kind (t1) == STOK_RIGHTPAR)
2429 srcdir = xstrdup (t0->stok_un.stok_string);
2430 srcdir_len = strlen (srcdir);
2431 next_state_tokens (2);
2432 return;
2436 fatal_reading_state (t0, "Bad srcdir in state_file");
2440 /* Read the sequence of GCC front-end languages. */
2441 static void
2442 read_state_languages (void)
2444 struct state_token_st *t0 = peek_state_token (0);
2445 struct state_token_st *t1 = peek_state_token (1);
2446 struct state_token_st *t2 = peek_state_token (2);
2447 if (state_token_kind (t0) == STOK_LEFTPAR
2448 && state_token_is_name (t1, "!languages")
2449 && state_token_kind (t2) == STOK_INTEGER)
2451 int i = 0;
2452 num_lang_dirs = t2->stok_un.stok_num;
2453 lang_dir_names = XCNEWVEC (const char *, num_lang_dirs);
2454 next_state_tokens (3);
2455 t0 = t1 = t2 = NULL;
2456 for (i = 0; i < (int) num_lang_dirs; i++)
2458 t0 = peek_state_token (0);
2459 if (state_token_kind (t0) != STOK_NAME)
2460 fatal_reading_state (t0, "expecting language name in state file");
2461 lang_dir_names[i] = t0->stok_un.stok_ident->stid_name;
2462 next_state_tokens (1);
2464 t0 = peek_state_token (0);
2465 if (state_token_kind (t0) != STOK_RIGHTPAR)
2466 fatal_reading_state (t0, "missing ) in languages list of state file");
2467 next_state_tokens (1);
2469 else
2470 fatal_reading_state (t0, "expecting languages list in state file");
2474 /* Read the sequence of files. */
2475 static void
2476 read_state_files_list (void)
2478 struct state_token_st *t0 = peek_state_token (0);
2479 struct state_token_st *t1 = peek_state_token (1);
2480 struct state_token_st *t2 = peek_state_token (2);
2481 struct state_token_st *t3 = peek_state_token (3);
2483 if (state_token_kind (t0) == STOK_LEFTPAR
2484 && state_token_is_name (t1, "!fileslist")
2485 && state_token_kind (t2) == STOK_INTEGER
2486 && state_token_kind (t3) == STOK_INTEGER)
2488 int i = 0, j = 0;
2489 num_gt_files = t2->stok_un.stok_num;
2490 num_build_headers = t3->stok_un.stok_num;
2491 next_state_tokens (4);
2492 t0 = t1 = t2 = t3 = NULL;
2493 gt_files = XCNEWVEC (const input_file *, num_gt_files);
2494 build_headers = XCNEWVEC (const char *, num_build_headers);
2495 for (i = 0; i < (int) num_gt_files; i++)
2497 bool issrcfile = false;
2498 t0 = t1 = t2 = NULL;
2499 t0 = peek_state_token (0);
2500 t1 = peek_state_token (1);
2501 t2 = peek_state_token (2);
2502 if (state_token_kind (t0) == STOK_LEFTPAR
2503 && (state_token_is_name (t1, "!file")
2504 || (issrcfile = state_token_is_name (t1, "!srcfile")))
2505 && state_token_kind (t2) == STOK_INTEGER)
2507 lang_bitmap bmap = t2->stok_un.stok_num;
2508 next_state_tokens (3);
2509 t0 = t1 = t2 = NULL;
2510 t0 = peek_state_token (0);
2511 t1 = peek_state_token (1);
2512 if (state_token_kind (t0) == STOK_STRING
2513 && state_token_kind (t1) == STOK_RIGHTPAR)
2515 const char *fnam = t0->stok_un.stok_string;
2516 /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
2517 input_file *curgt = NULL;
2518 if (issrcfile)
2520 static const char dirsepstr[2] =
2521 { DIR_SEPARATOR, (char) 0 };
2522 char *fullpath = concat (srcdir, dirsepstr, fnam, NULL);
2523 curgt = input_file_by_name (fullpath);
2524 free (fullpath);
2526 else
2528 curgt = input_file_by_name (fnam);
2529 /* Look for a header file created during the build,
2530 which looks like "./<filename>.h". */
2531 int len = strlen (fnam);
2532 if (len >= 5
2533 && fnam[0] == '.'
2534 && IS_DIR_SEPARATOR (fnam[1])
2535 && fnam[len-2] == '.'
2536 && fnam[len-1] == 'h')
2538 char *buf = (char *) xmalloc (len - 1);
2539 /* Strip the leading "./" from the filename. */
2540 strcpy (buf, &fnam[2]);
2541 build_headers[j++] = buf;
2544 set_lang_bitmap (curgt, bmap);
2545 gt_files[i] = curgt;
2546 next_state_tokens (2);
2548 else
2549 fatal_reading_state (t0,
2550 "bad file in !fileslist of state file");
2552 else
2553 fatal_reading_state (t0,
2554 "expecting file in !fileslist of state file");
2556 t0 = peek_state_token (0);
2557 if (state_token_kind (t0) != STOK_RIGHTPAR)
2558 fatal_reading_state (t0, "missing ) for !fileslist in state file");
2559 next_state_tokens (1);
2561 else
2562 fatal_reading_state (t0, "missing !fileslist in state file");
2566 /* Read the trailer. */
2567 static void
2568 read_state_trailer (void)
2570 struct state_token_st *t0 = peek_state_token (0);
2571 struct state_token_st *t1 = peek_state_token (1);
2572 struct state_token_st *t2 = peek_state_token (2);
2574 if (state_token_kind (t0) == STOK_LEFTPAR
2575 && state_token_is_name (t1, "!endfile")
2576 && state_token_kind (t2) == STOK_RIGHTPAR)
2577 next_state_tokens (3);
2578 else
2579 fatal_reading_state (t0, "missing !endfile in state file");
2583 /* Utility functions for the state_seen_types hash table. */
2584 static unsigned
2585 hash_type_number (const void *ty)
2587 const struct type *type = (const struct type *) ty;
2589 return type->state_number;
2592 static int
2593 equals_type_number (const void *ty1, const void *ty2)
2595 const struct type *type1 = (const struct type *) ty1;
2596 const struct type *type2 = (const struct type *) ty2;
2598 return type1->state_number == type2->state_number;
2602 /* The function reading the state, called by main from gengtype.cc. */
2603 void
2604 read_state (const char *path)
2606 state_file = fopen (path, "r");
2607 if (state_file == NULL)
2608 fatal ("Failed to open state file %s for reading [%s]", path,
2609 xstrerror (errno));
2610 state_path = path;
2611 state_line = 1;
2613 if (verbosity_level >= 1)
2615 printf ("%s reading state file %s;", progname, state_path);
2616 if (verbosity_level >= 2)
2617 putchar ('\n');
2618 fflush (stdout);
2621 state_seen_types =
2622 htab_create (2017, hash_type_number, equals_type_number, NULL);
2623 state_ident_tab =
2624 htab_create (4027, htab_hash_string, htab_eq_string, NULL);
2625 read_state_version (version_string);
2626 read_state_srcdir ();
2627 read_state_languages ();
2628 read_state_files_list ();
2629 read_state_structures (&structures);
2630 if (ferror (state_file))
2631 fatal_reading_state_printf
2632 (NULL_STATE_TOKEN, "input error while reading state [%s]",
2633 xstrerror (errno));
2634 read_state_typedefs (&typedefs);
2635 read_state_variables (&variables);
2636 read_state_trailer ();
2638 if (verbosity_level >= 1)
2640 printf ("%s read %ld bytes.\n", progname, ftell (state_file));
2641 fflush (stdout);
2644 if (fclose (state_file))
2645 fatal ("failed to close read state file %s [%s]",
2646 path, xstrerror (errno));
2647 state_file = NULL;
2648 state_path = NULL;
2651 /* End of file gengtype-state.cc. */