testsuite: powerpc/vec_reve_1.c requires VSX.
[official-gcc.git] / gcc / gengtype-state.c
blob36a96e8457467a67fb98cd183c7bf02e4665d793
1 /* Gengtype persistent state serialization & de-serialization.
2 Useful for gengtype in plugin mode.
4 Copyright (C) 2010-2021 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.c 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 PTR *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 c = getc (state_file);
477 break;
478 case 'b':
479 obstack_1grow (&bstring_obstack, '\b');
480 c = getc (state_file);
481 break;
482 case 't':
483 obstack_1grow (&bstring_obstack, '\t');
484 c = getc (state_file);
485 break;
486 case 'n':
487 obstack_1grow (&bstring_obstack, '\n');
488 c = getc (state_file);
489 break;
490 case 'v':
491 obstack_1grow (&bstring_obstack, '\v');
492 c = getc (state_file);
493 break;
494 case 'f':
495 obstack_1grow (&bstring_obstack, '\f');
496 c = getc (state_file);
497 break;
498 case 'r':
499 obstack_1grow (&bstring_obstack, '\r');
500 c = getc (state_file);
501 break;
502 case '"':
503 obstack_1grow (&bstring_obstack, '\"');
504 c = getc (state_file);
505 break;
506 case '\\':
507 obstack_1grow (&bstring_obstack, '\\');
508 c = getc (state_file);
509 break;
510 case ' ':
511 obstack_1grow (&bstring_obstack, ' ');
512 c = getc (state_file);
513 break;
514 case 'x':
516 unsigned int cx = 0;
517 if (fscanf (state_file, "%02x", &cx) > 0 && cx > 0)
518 obstack_1grow (&bstring_obstack, cx);
519 else
520 fatal_reading_state
521 (NULL_STATE_TOKEN,
522 "Lexical error in string hex escape");
523 c = getc (state_file);
524 break;
526 default:
527 fatal_reading_state
528 (NULL_STATE_TOKEN,
529 "Lexical error - unknown string escape");
532 else
533 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error...");
535 if (c != '"')
536 fatal_reading_state (NULL_STATE_TOKEN, "Unterminated string");
537 obstack_1grow (&bstring_obstack, '\0');
538 cstr = XOBFINISH (&bstring_obstack, char *);
539 cslen = strlen (cstr);
540 tk = (struct state_token_st *)
541 xcalloc (sizeof (struct state_token_st) + cslen, 1);
542 tk->stok_kind = STOK_STRING;
543 tk->stok_line = state_line;
544 tk->stok_col = curoff - state_bol;
545 tk->stok_file = state_path;
546 tk->stok_next = NULL;
547 strcpy (tk->stok_un.stok_string, cstr);
548 obstack_free (&bstring_obstack, NULL);
550 return tk;
552 /* Got an unexpected character. */
553 fatal_reading_state_printf
554 (NULL_STATE_TOKEN,
555 "Lexical error at offset %ld - bad character \\%03o = '%c'",
556 ftell (state_file), c, c);
559 /* Used for lexical look-ahead. Retrieves the lexical token of rank
560 DEPTH, starting with 0 when reading the state file. Gives null on
561 end of file. */
562 static struct state_token_st *
563 peek_state_token (int depth)
565 int remdepth = depth;
566 struct state_token_st **ptoken = &state_token;
567 struct state_token_st *tok = NULL;
569 while (remdepth >= 0)
571 if (*ptoken == NULL)
573 *ptoken = tok = read_a_state_token ();
574 if (tok == NULL)
575 return NULL;
577 tok = *ptoken;
578 ptoken = &((*ptoken)->stok_next);
579 remdepth--;
582 return tok;
585 /* Consume the next DEPTH tokens and free them. */
586 static void
587 next_state_tokens (int depth)
589 struct state_token_st *n;
591 while (depth > 0)
593 if (state_token != NULL)
595 n = state_token->stok_next;
596 free (state_token);
597 state_token = n;
599 else
600 fatal_reading_state (NULL_STATE_TOKEN, "Tokens stack empty");
602 depth--;
606 /* Safely retrieve the lexical kind of a token. */
607 static inline enum state_token_en
608 state_token_kind (struct state_token_st *p)
610 if (p == NULL)
611 return STOK_NONE;
612 else
613 return p->stok_kind;
616 /* Test if a token is a given name i.e. an identifier. */
617 static inline bool
618 state_token_is_name (struct state_token_st *p, const char *name)
620 if (p == NULL)
621 return false;
623 if (p->stok_kind != STOK_NAME)
624 return false;
626 return !strcmp (p->stok_un.stok_ident->stid_name, name);
630 /* Following routines are useful for serializing datas.
632 * We want to serialize :
633 * - typedefs list
634 * - structures list
635 * - variables list
637 * So, we have one routine for each kind of data. The main writing
638 * routine is write_state. The main reading routine is
639 * read_state. Most writing routines write_state_FOO have a
640 * corresponding reading routine read_state_FOO. Reading is done in a
641 * recursive descending way, and any read error is fatal.
644 /* When reading the state, we need to remember the previously seen
645 types by their state_number, since GTY-ed types are usually
646 shared. */
647 static htab_t state_seen_types;
649 /* Return the length of a linked list made of pairs. */
650 static int pair_list_length (pair_p list);
652 /* Compute the length of a list of pairs, starting from the first
653 one. */
654 static int
655 pair_list_length (pair_p list)
657 int nbpair = 0;
658 pair_p l = NULL;
659 for (l = list; l; l = l->next)
660 nbpair++;
661 return nbpair;
664 /* Write a file location. Files relative to $(srcdir) are quite
665 frequent and are handled specially. This ensures that two gengtype
666 state file-s produced by gengtype on the same GCC source tree are
667 very similar and can be reasonably compared with diff, even if the
668 two GCC source trees have different absolute paths. */
669 void
670 state_writer::write_state_fileloc (struct fileloc *floc)
673 if (floc != NULL && floc->line > 0)
675 const char *srcrelpath = NULL;
676 gcc_assert (floc->file != NULL);
677 /* Most of the files are inside $(srcdir) so it is worth to
678 handle them specially. */
679 srcrelpath = get_file_srcdir_relative_path (floc->file);
680 if (srcrelpath != NULL)
682 begin_s_expr ("srcfileloc");
683 write_state_a_string (srcrelpath);
685 else
687 begin_s_expr ("fileloc");
688 write_state_a_string (get_input_file_name (floc->file));
690 fprintf (state_file, " %d", floc->line);
691 end_s_expr ();
693 else
694 fprintf (state_file, "nil ");
697 /* Write a list of fields. */
698 void
699 state_writer::write_state_fields (pair_p fields)
701 int nbfields = pair_list_length (fields);
702 int nbpairs = 0;
703 begin_s_expr ("fields");
704 fprintf (state_file, "%d ", nbfields);
705 nbpairs = write_state_pair_list (fields);
706 gcc_assert (nbpairs == nbfields);
707 end_s_expr ();
710 /* Write a null-terminated string in our lexical convention, very
711 similar to the convention of C. */
712 void
713 state_writer::write_state_a_string (const char *s)
715 char c;
717 write_any_indent (1);
719 fputs (" \"", state_file);
720 for (; *s != 0; s++)
722 c = *s;
723 switch (c)
725 case '\a':
726 fputs ("\\a", state_file);
727 break;
728 case '\b':
729 fputs ("\\b", state_file);
730 break;
731 case '\t':
732 fputs ("\\t", state_file);
733 break;
734 case '\n':
735 fputs ("\\n", state_file);
736 break;
737 case '\v':
738 fputs ("\\v", state_file);
739 break;
740 case '\f':
741 fputs ("\\f", state_file);
742 break;
743 case '\r':
744 fputs ("\\r", state_file);
745 break;
746 case '\"':
747 fputs ("\\\"", state_file);
748 break;
749 case '\\':
750 fputs ("\\\\", state_file);
751 break;
752 default:
753 if (ISPRINT (c))
754 putc (c, state_file);
755 else
756 fprintf (state_file, "\\x%02x", (unsigned) c);
759 fputs ("\"", state_file);
762 /* Our option-s have three kinds, each with its writer. */
763 void
764 state_writer::write_state_string_option (options_p current)
766 write_any_indent (0);
767 fprintf (state_file, "string ");
768 if (current->info.string != NULL)
769 write_state_a_string (current->info.string);
770 else
771 fprintf (state_file, " nil ");
774 void
775 state_writer::write_state_type_option (options_p current)
777 write_any_indent (0);
778 fprintf (state_file, "type ");
779 write_state_type (current->info.type);
782 void
783 state_writer::write_state_nested_option (options_p current)
785 write_any_indent (0);
786 fprintf (state_file, "nested ");
787 write_state_type (current->info.nested->type);
788 if (current->info.nested->convert_from != NULL)
789 write_state_a_string (current->info.nested->convert_from);
790 else
792 write_any_indent (1);
793 fprintf (state_file, " nil ");
796 if (current->info.nested->convert_to != NULL)
797 write_state_a_string (current->info.nested->convert_to);
798 else
800 write_any_indent (1);
801 fprintf (state_file, " nil ");
805 void
806 state_writer::write_state_option (options_p current)
808 begin_s_expr ("option");
810 write_any_indent (0);
811 if (current->name != NULL)
812 fprintf (state_file, "%s ", current->name);
813 else
814 fprintf (state_file, "nil ");
816 switch (current->kind)
818 case OPTION_STRING:
819 write_state_string_option (current);
820 break;
821 case OPTION_TYPE:
822 write_state_type_option (current);
823 break;
824 case OPTION_NESTED:
825 write_state_nested_option (current);
826 break;
827 default:
828 fatal ("Option tag unknown");
831 /* Terminate the "option" s-expression. */
832 end_s_expr ();
837 /* Write a list of GTY options. */
838 void
839 state_writer::write_state_options (options_p opt)
841 options_p current;
843 if (opt == NULL)
845 write_any_indent (0);
846 fprintf (state_file, "nil ");
847 return;
850 begin_s_expr ("options");
851 for (current = opt; current != NULL; current = current->next)
852 write_state_option (current);
853 end_s_expr ();
857 /* Write a bitmap representing a set of GCC front-end languages. */
858 void
859 state_writer::write_state_lang_bitmap (lang_bitmap bitmap)
861 write_any_indent (0);
862 fprintf (state_file, "%d ", (int) bitmap);
865 /* Write version information. */
866 void
867 state_writer::write_state_version (const char *version)
869 begin_s_expr ("version");
870 write_state_a_string (version);
871 end_s_expr ();
874 /* Write a scalar type. We have only two of these. */
875 void
876 state_writer::write_state_scalar_type (type_p current)
878 write_any_indent (0);
879 if (current == &scalar_nonchar)
880 fprintf (state_file, "scalar_nonchar ");
881 else if (current == &scalar_char)
882 fprintf (state_file, "scalar_char ");
883 else
884 fatal ("Unexpected type in write_state_scalar_type");
886 write_state_common_type_content (current);
889 /* Write the string type. There is only one such thing! */
890 void
891 state_writer::write_state_string_type (type_p current)
893 if (current == &string_type)
895 write_any_indent (0);
896 fprintf (state_file, "string ");
897 write_state_common_type_content (current);
899 else
900 fatal ("Unexpected type in write_state_string_type");
903 /* Write the callback type. There is only one such thing! */
904 void
905 state_writer::write_state_callback_type (type_p current)
907 if (current == &callback_type)
909 write_any_indent (0);
910 fprintf (state_file, "callback ");
911 write_state_common_type_content (current);
913 else
914 fatal ("Unexpected type in write_state_callback_type");
917 /* Write an undefined type. */
918 void
919 state_writer::write_state_undefined_type (type_p current)
921 DBGPRINTF ("undefined type @ %p #%d '%s'", (void *) current,
922 current->state_number, current->u.s.tag);
923 write_any_indent (0);
924 fprintf (state_file, "undefined ");
925 gcc_assert (current->gc_used == GC_UNUSED);
926 write_state_common_type_content (current);
927 if (current->u.s.tag != NULL)
928 write_state_a_string (current->u.s.tag);
929 else
931 write_any_indent (0);
932 fprintf (state_file, "nil");
935 write_state_fileloc (type_lineloc (current));
939 /* Common code to write structure like types. */
940 void
941 state_writer::write_state_struct_union_type (type_p current,
942 const char *kindstr)
944 DBGPRINTF ("%s type @ %p #%d '%s'", kindstr, (void *) current,
945 current->state_number, current->u.s.tag);
946 write_any_indent (0);
947 fprintf (state_file, "%s ", kindstr);
948 write_state_common_type_content (current);
949 if (current->u.s.tag != NULL)
950 write_state_a_string (current->u.s.tag);
951 else
953 write_any_indent (0);
954 fprintf (state_file, "nil");
957 write_state_fileloc (type_lineloc (current));
958 write_state_fields (current->u.s.fields);
959 write_state_options (current->u.s.opt);
960 write_state_lang_bitmap (current->u.s.bitmap);
964 /* Write a GTY struct type. */
965 void
966 state_writer::write_state_struct_type (type_p current)
968 write_state_struct_union_type (current, "struct");
969 write_state_type (current->u.s.lang_struct);
970 write_state_type (current->u.s.base_class);
973 /* Write a GTY user-defined struct type. */
974 void
975 state_writer::write_state_user_struct_type (type_p current)
977 DBGPRINTF ("user_struct type @ %p #%d '%s'", (void *) current,
978 current->state_number, current->u.s.tag);
979 write_any_indent (0);
980 fprintf (state_file, "user_struct ");
981 write_state_common_type_content (current);
982 if (current->u.s.tag != NULL)
983 write_state_a_string (current->u.s.tag);
984 else
986 write_any_indent (0);
987 fprintf (state_file, "nil");
989 write_state_fileloc (type_lineloc (current));
990 write_state_fields (current->u.s.fields);
993 /* write a GTY union type. */
994 void
995 state_writer::write_state_union_type (type_p current)
997 write_state_struct_union_type (current, "union");
998 write_state_type (current->u.s.lang_struct);
1001 /* Write a lang_struct type. This is tricky and was painful to debug,
1002 we deal with the next field specifically within their lang_struct
1003 subfield, which points to a linked list of homonumous types.
1004 Change this function with extreme care, see also
1005 read_state_lang_struct_type. */
1006 void
1007 state_writer::write_state_lang_struct_type (type_p current)
1009 int nbhomontype = 0;
1010 type_p hty = NULL;
1011 const char *homoname = 0;
1012 write_state_struct_union_type (current, "lang_struct");
1013 /* lang_struct-ures are particularly tricky, since their
1014 u.s.lang_struct field gives a list of homonymous struct-s or
1015 union-s! */
1016 DBGPRINTF ("lang_struct @ %p #%d", (void *) current, current->state_number);
1017 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
1019 nbhomontype++;
1020 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype,
1021 (void *) hty, hty->state_number, hty->u.s.tag);
1022 /* Every member of the homonymous list should have the same tag. */
1023 gcc_assert (union_or_struct_p (hty));
1024 gcc_assert (hty->u.s.lang_struct == current);
1025 if (!homoname)
1026 homoname = hty->u.s.tag;
1027 gcc_assert (strcmp (homoname, hty->u.s.tag) == 0);
1029 begin_s_expr ("homotypes");
1030 fprintf (state_file, "%d", nbhomontype);
1031 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
1032 write_state_type (hty);
1033 end_s_expr ();
1036 /* Write a pointer type. */
1037 void
1038 state_writer::write_state_pointer_type (type_p current)
1040 write_any_indent (0);
1041 fprintf (state_file, "pointer ");
1042 write_state_common_type_content (current);
1043 write_state_type (current->u.p);
1046 /* Write an array type. */
1047 void
1048 state_writer::write_state_array_type (type_p current)
1050 write_any_indent (0);
1051 fprintf (state_file, "array ");
1052 write_state_common_type_content (current);
1053 if (current->u.a.len != NULL)
1054 write_state_a_string (current->u.a.len);
1055 else
1057 write_any_indent (1);
1058 fprintf (state_file, " nil");
1061 write_any_indent (1);
1062 fprintf (state_file, " ");
1063 write_state_type (current->u.a.p);
1066 /* Write the gc_used information. */
1067 void
1068 state_writer::write_state_gc_used (enum gc_used_enum gus)
1070 write_any_indent (1);
1071 switch (gus)
1073 case GC_UNUSED:
1074 fprintf (state_file, " gc_unused");
1075 break;
1076 case GC_USED:
1077 fprintf (state_file, " gc_used");
1078 break;
1079 case GC_MAYBE_POINTED_TO:
1080 fprintf (state_file, " gc_maybe_pointed_to");
1081 break;
1082 case GC_POINTED_TO:
1083 fprintf (state_file, " gc_pointed_to");
1084 break;
1085 default:
1086 gcc_unreachable ();
1090 /* Utility routine to write the common content of all types. Notice
1091 that the next field is *not* written on purpose. */
1092 void
1093 state_writer::write_state_common_type_content (type_p current)
1095 write_any_indent (0);
1096 fprintf (state_file, "%d ", current->state_number);
1097 /* We do not write the next type, because list of types are
1098 explicitly written. However, lang_struct are special in that
1099 respect. See function write_state_lang_struct_type for more. */
1100 write_state_type (current->pointer_to);
1101 write_state_gc_used (current->gc_used);
1105 /* The important and recursive routine writing GTY types as understood
1106 by gengtype. Types which have a positive state_number have already
1107 been seen and written. */
1108 void
1109 state_writer::write_state_type (type_p current)
1111 write_any_indent (0);
1112 if (current == NULL)
1114 fprintf (state_file, "nil ");
1115 return;
1118 begin_s_expr ("type");
1120 if (current->state_number > 0)
1122 write_any_indent (0);
1123 fprintf (state_file, "already_seen %d", current->state_number);
1125 else
1127 m_state_written_type_count++;
1128 DBGPRINTF ("writing type #%d @%p old number %d", m_state_written_type_count,
1129 (void *) current, current->state_number);
1130 current->state_number = m_state_written_type_count;
1131 switch (current->kind)
1133 case TYPE_NONE:
1134 gcc_unreachable ();
1135 case TYPE_UNDEFINED:
1136 write_state_undefined_type (current);
1137 break;
1138 case TYPE_STRUCT:
1139 write_state_struct_type (current);
1140 break;
1141 case TYPE_USER_STRUCT:
1142 write_state_user_struct_type (current);
1143 break;
1144 case TYPE_UNION:
1145 write_state_union_type (current);
1146 break;
1147 case TYPE_POINTER:
1148 write_state_pointer_type (current);
1149 break;
1150 case TYPE_ARRAY:
1151 write_state_array_type (current);
1152 break;
1153 case TYPE_LANG_STRUCT:
1154 write_state_lang_struct_type (current);
1155 break;
1156 case TYPE_SCALAR:
1157 write_state_scalar_type (current);
1158 break;
1159 case TYPE_STRING:
1160 write_state_string_type (current);
1161 break;
1162 case TYPE_CALLBACK:
1163 write_state_callback_type (current);
1164 break;
1168 /* Terminate the "type" s-expression. */
1169 end_s_expr ();
1173 /* Write a pair. */
1174 void
1175 state_writer::write_state_pair (pair_p current)
1177 if (current == NULL)
1179 write_any_indent (0);
1180 fprintf (state_file, "nil)");
1181 return;
1184 begin_s_expr ("pair");
1186 if (current->name != NULL)
1187 write_state_a_string (current->name);
1188 else
1189 write_state_a_string ("nil");
1191 write_state_type (current->type);
1192 write_state_fileloc (&(current->line));
1193 write_state_options (current->opt);
1195 /* Terminate the "pair" s-expression. */
1196 end_s_expr ();
1199 /* Write a pair list and return the number of pairs written. */
1201 state_writer::write_state_pair_list (pair_p list)
1203 int nbpair = 0;
1204 pair_p current;
1206 for (current = list; current != NULL; current = current->next)
1208 write_state_pair (current);
1209 nbpair++;
1211 return nbpair;
1215 /* When writing imported linked lists, like typedefs, structures, ... we count
1216 their length first and write it. This eases the reading, and enables an
1217 extra verification on the number of actually read items. */
1219 /* Write our typedefs. */
1220 void
1221 state_writer::write_state_typedefs (void)
1223 int nbtypedefs = pair_list_length (typedefs);
1224 int nbpairs = 0;
1225 begin_s_expr ("typedefs");
1226 fprintf (state_file, "%d", nbtypedefs);
1227 nbpairs = write_state_pair_list (typedefs);
1228 gcc_assert (nbpairs == nbtypedefs);
1229 end_s_expr ();
1230 if (verbosity_level >= 2)
1231 printf ("%s wrote %d typedefs\n", progname, nbtypedefs);
1234 /* Write our structures. */
1235 void
1236 state_writer::write_state_structures (void)
1238 int nbstruct = 0;
1239 type_p current;
1241 for (current = structures; current != NULL; current = current->next)
1242 nbstruct++;
1244 begin_s_expr ("structures");
1245 fprintf (state_file, "%d", nbstruct);
1247 for (current = structures; current != NULL; current = current->next)
1249 write_new_line ();
1250 write_state_type (current);
1253 /* Terminate the "structures" s-expression. */
1254 end_s_expr ();
1255 if (verbosity_level >= 2)
1256 printf ("%s wrote %d structures in state\n", progname, nbstruct);
1259 /* Write our variables. */
1260 void
1261 state_writer::write_state_variables (void)
1263 int nbvars = pair_list_length (variables);
1264 int nbpairs = 0;
1265 begin_s_expr ("variables");
1266 fprintf (state_file, "%d", nbvars);
1267 nbpairs = write_state_pair_list (variables);
1268 gcc_assert (nbpairs == nbvars);
1269 end_s_expr ();
1270 if (verbosity_level >= 2)
1271 printf ("%s wrote %d variables.\n", progname, nbvars);
1274 /* Write the source directory. File locations within the source
1275 directory have been written specifically. */
1276 void
1277 state_writer::write_state_srcdir (void)
1279 begin_s_expr ("srcdir");
1280 write_state_a_string (srcdir);
1281 end_s_expr ();
1284 /* Count and write the list of our files. */
1285 void
1286 state_writer::write_state_files_list (void)
1288 int i = 0;
1289 /* Write the list of files with their lang_bitmap. */
1290 begin_s_expr ("fileslist");
1291 fprintf (state_file, "%d %d", (int) num_gt_files, (int) num_build_headers);
1292 for (i = 0; i < (int) num_gt_files; i++)
1294 const char *cursrcrelpath = NULL;
1295 const input_file *curfil = gt_files[i];
1296 /* Most of the files are inside $(srcdir) so it is worth to
1297 handle them specially. */
1298 cursrcrelpath = get_file_srcdir_relative_path (curfil);
1299 if (cursrcrelpath)
1301 begin_s_expr ("srcfile");
1302 fprintf (state_file, "%d ", get_lang_bitmap (curfil));
1303 write_state_a_string (cursrcrelpath);
1305 else
1307 begin_s_expr ("file");
1308 fprintf (state_file, "%d ", get_lang_bitmap (curfil));
1309 write_state_a_string (get_input_file_name (curfil));
1311 /* Terminate the inner s-expression (either "srcfile" or "file"). */
1312 end_s_expr ();
1314 /* Terminate the "fileslist" s-expression. */
1315 end_s_expr ();
1318 /* Write the list of GCC front-end languages. */
1319 void
1320 state_writer::write_state_languages (void)
1322 int i = 0;
1323 begin_s_expr ("languages");
1324 fprintf (state_file, "%d", (int) num_lang_dirs);
1325 for (i = 0; i < (int) num_lang_dirs; i++)
1327 /* Languages names are identifiers, we expect only letters or
1328 underscores or digits in them. In particular, C++ is not a
1329 valid language name, but cp is valid. */
1330 fprintf (state_file, " %s", lang_dir_names[i]);
1332 end_s_expr ();
1335 /* Write the trailer. */
1336 static void
1337 write_state_trailer (void)
1339 /* This test should probably catch IO errors like disk full... */
1340 if (fputs ("\n(!endfile)\n", state_file) == EOF)
1341 fatal ("failed to write state trailer [%s]", xstrerror (errno));
1344 /* The write_state routine is the only writing routine called by main
1345 in gengtype.c. To avoid messing the state if gengtype is
1346 interrupted or aborted, we write a temporary file and rename it
1347 after having written it in totality. */
1348 void
1349 write_state (const char *state_path)
1351 long statelen = 0;
1352 time_t now = 0;
1353 char *temp_state_path = NULL;
1354 char tempsuffix[40];
1355 time (&now);
1357 /* We write a unique temporary file which is renamed when complete
1358 * only. So even if gengtype is interrupted, the written state file
1359 * won't be partially written, since the temporary file is not yet
1360 * renamed in that case. */
1361 memset (tempsuffix, 0, sizeof (tempsuffix));
1362 snprintf (tempsuffix, sizeof (tempsuffix) - 1, "-%ld-%d.tmp", (long) now,
1363 (int) getpid ());
1364 temp_state_path = concat (state_path, tempsuffix, NULL);
1365 state_file = fopen (temp_state_path, "w");
1366 if (state_file == NULL)
1367 fatal ("Failed to open file %s for writing state: %s",
1368 temp_state_path, xstrerror (errno));
1369 if (verbosity_level >= 3)
1370 printf ("%s writing state file %s temporarily in %s\n",
1371 progname, state_path, temp_state_path);
1372 /* This is the first line of the state. Perhaps the file utility
1373 could know about that, so don't change it often. */
1374 fprintf (state_file, ";;;;@@@@ GCC gengtype state\n");
1375 /* Output a few comments for humans. */
1376 fprintf (state_file,
1377 ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
1378 fprintf (state_file,
1379 ";;; The format of this file is tied to a particular version of GCC.\n");
1380 fprintf (state_file,
1381 ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1382 fprintf (state_file,
1383 ";;; This file should be parsed by the same %s which wrote it.\n",
1384 progname);
1386 state_writer sw;
1388 /* The first non-comment significant line gives the version string. */
1389 sw.write_state_version (version_string);
1390 sw.write_state_srcdir ();
1391 sw.write_state_languages ();
1392 sw.write_state_files_list ();
1393 sw.write_state_structures ();
1394 sw.write_state_typedefs ();
1395 sw.write_state_variables ();
1396 write_state_trailer ();
1397 statelen = ftell (state_file);
1398 if (ferror (state_file))
1399 fatal ("output error when writing state file %s [%s]",
1400 temp_state_path, xstrerror (errno));
1401 if (fclose (state_file))
1402 fatal ("failed to close state file %s [%s]",
1403 temp_state_path, xstrerror (errno));
1404 if (rename (temp_state_path, state_path))
1405 fatal ("failed to rename %s to state file %s [%s]", temp_state_path,
1406 state_path, xstrerror (errno));
1407 free (temp_state_path);
1409 if (verbosity_level >= 1)
1410 printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1411 progname, state_path, statelen, sw.m_state_written_type_count);
1415 /** End of writing routines! The corresponding reading routines follow. **/
1419 /* Forward declarations, since some read_state_* functions are
1420 recursive! */
1421 static void read_state_fileloc (struct fileloc *line);
1422 static void read_state_options (options_p *opt);
1423 static void read_state_type (type_p *current);
1424 static void read_state_pair (pair_p *pair);
1425 /* Return the number of pairs actually read. */
1426 static int read_state_pair_list (pair_p *list);
1427 static void read_state_fields (pair_p *fields);
1428 static void read_state_common_type_content (type_p current);
1433 /* Record into the state_seen_types hash-table a type which we are
1434 reading, to enable recursive or circular references to it. */
1435 static void
1436 record_type (type_p type)
1438 PTR *slot;
1440 slot = htab_find_slot (state_seen_types, type, INSERT);
1441 gcc_assert (slot);
1443 *slot = type;
1446 /* Read an already seen type. */
1447 static void
1448 read_state_already_seen_type (type_p *type)
1450 struct state_token_st *t0 = peek_state_token (0);
1452 if (state_token_kind (t0) == STOK_INTEGER)
1454 PTR *slot = NULL;
1455 struct type loctype = { TYPE_SCALAR, 0, 0, 0, GC_UNUSED, {0} };
1457 loctype.state_number = t0->stok_un.stok_num;
1458 slot = htab_find_slot (state_seen_types, &loctype, NO_INSERT);
1459 if (slot == NULL)
1461 fatal_reading_state (t0, "Unknown type");
1464 next_state_tokens (1);
1465 *type = (type_p) *slot;
1467 else
1469 fatal_reading_state (t0, "Bad seen type");
1474 /* Read the scalar_nonchar type. */
1475 static void
1476 read_state_scalar_nonchar_type (type_p *type)
1478 *type = &scalar_nonchar;
1479 read_state_common_type_content (*type);
1483 /* Read the scalar_char type. */
1484 static void
1485 read_state_scalar_char_type (type_p *type)
1487 *type = &scalar_char;
1488 read_state_common_type_content (*type);
1491 /* Read the string_type. */
1492 static void
1493 read_state_string_type (type_p *type)
1495 *type = &string_type;
1496 read_state_common_type_content (*type);
1499 /* Read the callback_type. */
1500 static void
1501 read_state_callback_type (type_p *type)
1503 *type = &callback_type;
1504 read_state_common_type_content (*type);
1508 /* Read a lang_bitmap representing a set of GCC front-end languages. */
1509 static void
1510 read_state_lang_bitmap (lang_bitmap *bitmap)
1512 struct state_token_st *t;
1514 t = peek_state_token (0);
1515 if (state_token_kind (t) == STOK_INTEGER)
1517 *bitmap = t->stok_un.stok_num;
1518 next_state_tokens (1);
1520 else
1522 fatal_reading_state (t, "Bad syntax for bitmap");
1527 /* Read an undefined type. */
1528 static void
1529 read_state_undefined_type (type_p type)
1531 struct state_token_st *t0;
1533 type->kind = TYPE_UNDEFINED;
1534 read_state_common_type_content (type);
1535 t0 = peek_state_token (0);
1536 if (state_token_kind (t0) == STOK_STRING)
1538 if (state_token_is_name (t0, "nil"))
1540 type->u.s.tag = NULL;
1541 DBGPRINTF ("read anonymous undefined type @%p #%d",
1542 (void *) type, type->state_number);
1544 else
1546 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1547 DBGPRINTF ("read undefined type @%p #%d '%s'",
1548 (void *) type, type->state_number, type->u.s.tag);
1551 next_state_tokens (1);
1552 read_state_fileloc (&(type->u.s.line));
1554 else
1556 fatal_reading_state (t0, "Bad tag in undefined type");
1561 /* Read a GTY-ed struct type. */
1562 static void
1563 read_state_struct_type (type_p type)
1565 struct state_token_st *t0;
1567 type->kind = TYPE_STRUCT;
1568 read_state_common_type_content (type);
1569 t0 = peek_state_token (0);
1570 if (state_token_kind (t0) == STOK_STRING)
1572 if (state_token_is_name (t0, "nil"))
1574 type->u.s.tag = NULL;
1575 DBGPRINTF ("read anonymous struct type @%p #%d",
1576 (void *) type, type->state_number);
1578 else
1580 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1581 DBGPRINTF ("read struct type @%p #%d '%s'",
1582 (void *) type, type->state_number, type->u.s.tag);
1585 next_state_tokens (1);
1586 read_state_fileloc (&(type->u.s.line));
1587 read_state_fields (&(type->u.s.fields));
1588 read_state_options (&(type->u.s.opt));
1589 read_state_lang_bitmap (&(type->u.s.bitmap));
1590 read_state_type (&(type->u.s.lang_struct));
1591 read_state_type (&(type->u.s.base_class));
1592 if (type->u.s.base_class)
1593 add_subclass (type->u.s.base_class, type);
1595 else
1597 fatal_reading_state (t0, "Bad tag in struct type");
1602 /* Read a GTY-ed user-provided struct TYPE. */
1604 static void
1605 read_state_user_struct_type (type_p type)
1607 struct state_token_st *t0;
1609 type->kind = TYPE_USER_STRUCT;
1610 read_state_common_type_content (type);
1611 t0 = peek_state_token (0);
1612 if (state_token_kind (t0) == STOK_STRING)
1614 if (state_token_is_name (t0, "nil"))
1616 type->u.s.tag = NULL;
1617 DBGPRINTF ("read anonymous struct type @%p #%d",
1618 (void *) type, type->state_number);
1620 else
1622 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1623 DBGPRINTF ("read struct type @%p #%d '%s'",
1624 (void *) type, type->state_number, type->u.s.tag);
1627 next_state_tokens (1);
1628 read_state_fileloc (&(type->u.s.line));
1629 read_state_fields (&(type->u.s.fields));
1631 else
1633 fatal_reading_state (t0, "Bad tag in user-struct type");
1638 /* Read a GTY-ed union type. */
1639 static void
1640 read_state_union_type (type_p type)
1642 struct state_token_st *t0;
1644 type->kind = TYPE_UNION;
1645 read_state_common_type_content (type);
1646 t0 = peek_state_token (0);
1647 if (state_token_kind (t0) == STOK_STRING)
1649 if (state_token_is_name (t0, "nil"))
1651 type->u.s.tag = NULL;
1652 DBGPRINTF ("read anonymous union type @%p #%d",
1653 (void *) type, type->state_number);
1655 else
1657 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1658 DBGPRINTF ("read union type @%p #%d '%s'",
1659 (void *) type, type->state_number, type->u.s.tag);
1661 next_state_tokens (1);
1662 read_state_fileloc (&(type->u.s.line));
1663 read_state_fields (&(type->u.s.fields));
1664 read_state_options (&(type->u.s.opt));
1665 read_state_lang_bitmap (&(type->u.s.bitmap));
1666 read_state_type (&(type->u.s.lang_struct));
1668 else
1669 fatal_reading_state (t0, "Bad tag in union type");
1673 /* Read a GTY-ed pointer type. */
1674 static void
1675 read_state_pointer_type (type_p type)
1677 type->kind = TYPE_POINTER;
1678 read_state_common_type_content (type);
1679 DBGPRINTF ("read pointer type @%p #%d", (void *) type, type->state_number);
1680 read_state_type (&(type->u.p));
1684 /* Read a GTY-ed array type. */
1685 static void
1686 read_state_array_type (type_p type)
1688 struct state_token_st *t0;
1690 type->kind = TYPE_ARRAY;
1691 read_state_common_type_content (type);
1692 t0 = peek_state_token (0);
1693 if (state_token_kind (t0) == STOK_STRING)
1695 type->u.a.len = xstrdup (t0->stok_un.stok_string);
1696 DBGPRINTF ("read array type @%p #%d length '%s'",
1697 (void *) type, type->state_number, type->u.a.len);
1698 next_state_tokens (1);
1701 else if (state_token_is_name (t0, "nil"))
1703 type->u.a.len = NULL;
1704 DBGPRINTF ("read array type @%p #%d without length",
1705 (void *) type, type->state_number);
1706 next_state_tokens (1);
1709 else
1710 fatal_reading_state (t0, "Bad array name type");
1711 read_state_type (&(type->u.a.p));
1716 /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
1717 front-end languages. This is a tricky function and it was painful
1718 to debug. Change it with extreme care. See also
1719 write_state_lang_struct_type. */
1720 static void
1721 read_state_lang_struct_type (type_p type)
1723 struct state_token_st *t0 = NULL;
1724 struct state_token_st *t1 = NULL;
1725 struct state_token_st *t2 = NULL;
1727 type->kind = TYPE_LANG_STRUCT;
1728 read_state_common_type_content (type);
1729 t0 = peek_state_token (0);
1730 if (state_token_kind (t0) == STOK_STRING)
1732 if (state_token_is_name (t0, "nil"))
1734 DBGPRINTF ("read anonymous lang_struct type @%p #%d",
1735 (void *) type, type->state_number);
1736 type->u.s.tag = NULL;
1738 else
1740 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1741 DBGPRINTF ("read lang_struct type @%p #%d '%s'",
1742 (void *) type, type->state_number, type->u.s.tag);
1744 next_state_tokens (1);
1746 else
1747 fatal_reading_state (t0, "Bad tag in lang struct type");
1748 read_state_fileloc (&(type->u.s.line));
1749 read_state_fields (&(type->u.s.fields));
1750 read_state_options (&(type->u.s.opt));
1751 read_state_lang_bitmap (&(type->u.s.bitmap));
1752 /* Within lang_struct-ures, the lang_struct field is a linked list
1753 of homonymous types! */
1754 t0 = peek_state_token (0);
1755 t1 = peek_state_token (1);
1756 t2 = peek_state_token (2);
1757 /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
1758 if (state_token_kind (t0) == STOK_LEFTPAR
1759 && state_token_is_name (t1, "!homotypes")
1760 && state_token_kind (t2) == STOK_INTEGER)
1762 type_p *prevty = &type->u.s.lang_struct;
1763 int nbhomotype = t2->stok_un.stok_num;
1764 int i = 0;
1765 t0 = t1 = t2 = NULL;
1766 next_state_tokens (3);
1767 for (i = 0; i < nbhomotype; i++)
1769 read_state_type (prevty);
1770 t0 = peek_state_token (0);
1771 if (*prevty)
1772 prevty = &(*prevty)->next;
1773 else
1774 fatal_reading_state (t0,
1775 "expecting type in homotype list for lang_struct");
1777 if (state_token_kind (t0) != STOK_RIGHTPAR)
1778 fatal_reading_state (t0,
1779 "expecting ) in homotype list for lang_struct");
1780 next_state_tokens (1);
1782 else
1783 fatal_reading_state (t0, "expecting !homotypes for lang_struct");
1787 /* Read the gc used information. */
1788 static void
1789 read_state_gc_used (enum gc_used_enum *pgus)
1791 struct state_token_st *t0 = peek_state_token (0);
1792 if (state_token_is_name (t0, "gc_unused"))
1793 *pgus = GC_UNUSED;
1794 else if (state_token_is_name (t0, "gc_used"))
1795 *pgus = GC_USED;
1796 else if (state_token_is_name (t0, "gc_maybe_pointed_to"))
1797 *pgus = GC_MAYBE_POINTED_TO;
1798 else if (state_token_is_name (t0, "gc_pointed_to"))
1799 *pgus = GC_POINTED_TO;
1800 else
1801 fatal_reading_state (t0, "invalid gc_used information");
1802 next_state_tokens (1);
1806 /* Utility function to read the common content of types. */
1807 static void
1808 read_state_common_type_content (type_p current)
1810 struct state_token_st *t0 = peek_state_token (0);
1812 if (state_token_kind (t0) == STOK_INTEGER)
1814 current->state_number = t0->stok_un.stok_num;
1815 next_state_tokens (1);
1816 record_type (current);
1818 else
1819 fatal_reading_state_printf (t0,
1820 "Expected integer for state_number line %d",
1821 state_line);
1822 /* We don't read the next field of the type. */
1823 read_state_type (&current->pointer_to);
1824 read_state_gc_used (&current->gc_used);
1828 /* Read a GTY-ed type. */
1829 void
1830 read_state_type (type_p *current)
1832 struct state_token_st *t0 = peek_state_token (0);
1833 struct state_token_st *t1 = peek_state_token (1);
1835 if (state_token_kind (t0) == STOK_LEFTPAR &&
1836 state_token_is_name (t1, "!type"))
1838 next_state_tokens (2);
1839 t0 = peek_state_token (0);
1840 if (state_token_is_name (t0, "already_seen"))
1842 next_state_tokens (1);
1843 read_state_already_seen_type (current);
1845 else
1847 t0 = peek_state_token (0);
1849 if (state_token_is_name (t0, "scalar_nonchar"))
1851 next_state_tokens (1);
1852 read_state_scalar_nonchar_type (current);
1854 else if (state_token_is_name (t0, "scalar_char"))
1856 next_state_tokens (1);
1857 read_state_scalar_char_type (current);
1859 else if (state_token_is_name (t0, "string"))
1861 next_state_tokens (1);
1862 read_state_string_type (current);
1864 else if (state_token_is_name (t0, "callback"))
1866 next_state_tokens (1);
1867 read_state_callback_type (current);
1869 else if (state_token_is_name (t0, "undefined"))
1871 *current = XCNEW (struct type);
1872 next_state_tokens (1);
1873 read_state_undefined_type (*current);
1875 else if (state_token_is_name (t0, "struct"))
1877 *current = XCNEW (struct type);
1878 next_state_tokens (1);
1879 read_state_struct_type (*current);
1881 else if (state_token_is_name (t0, "union"))
1883 *current = XCNEW (struct type);
1884 next_state_tokens (1);
1885 read_state_union_type (*current);
1887 else if (state_token_is_name (t0, "lang_struct"))
1889 *current = XCNEW (struct type);
1890 next_state_tokens (1);
1891 read_state_lang_struct_type (*current);
1893 else if (state_token_is_name (t0, "pointer"))
1895 *current = XCNEW (struct type);
1896 next_state_tokens (1);
1897 read_state_pointer_type (*current);
1899 else if (state_token_is_name (t0, "array"))
1901 *current = XCNEW (struct type);
1902 next_state_tokens (1);
1903 read_state_array_type (*current);
1905 else if (state_token_is_name (t0, "user_struct"))
1907 *current = XCNEW (struct type);
1908 next_state_tokens (1);
1909 read_state_user_struct_type (*current);
1911 else
1912 fatal_reading_state (t0, "bad type in (!type");
1914 t0 = peek_state_token (0);
1915 if (state_token_kind (t0) != STOK_RIGHTPAR)
1916 fatal_reading_state (t0, "missing ) in type");
1917 next_state_tokens (1);
1919 else if (state_token_is_name (t0, "nil"))
1921 next_state_tokens (1);
1922 *current = NULL;
1924 else
1925 fatal_reading_state (t0, "bad type syntax");
1929 /* Read a file location. Files within the source directory are dealt
1930 with specifically. */
1931 void
1932 read_state_fileloc (struct fileloc *floc)
1934 bool issrcfile = false;
1935 struct state_token_st *t0 = peek_state_token (0);
1936 struct state_token_st *t1 = peek_state_token (1);
1938 gcc_assert (floc != NULL);
1939 gcc_assert (srcdir != NULL);
1941 if (state_token_kind (t0) == STOK_LEFTPAR &&
1942 (state_token_is_name (t1, "!fileloc")
1943 || (issrcfile = state_token_is_name (t1, "!srcfileloc"))))
1945 next_state_tokens (2);
1946 t0 = peek_state_token (0);
1947 t1 = peek_state_token (1);
1948 if (state_token_kind (t0) == STOK_STRING &&
1949 state_token_kind (t1) == STOK_INTEGER)
1951 char *path = t0->stok_un.stok_string;
1952 if (issrcfile)
1954 static const char dirsepstr[2] = { DIR_SEPARATOR, (char) 0 };
1955 char *fullpath = concat (srcdir, dirsepstr, path, NULL);
1956 floc->file = input_file_by_name (fullpath);
1957 free (fullpath);
1959 else
1960 floc->file = input_file_by_name (path);
1961 floc->line = t1->stok_un.stok_num;
1962 next_state_tokens (2);
1964 else
1965 fatal_reading_state (t0,
1966 "Bad fileloc syntax, expected path string and line");
1967 t0 = peek_state_token (0);
1968 if (state_token_kind (t0) != STOK_RIGHTPAR)
1969 fatal_reading_state (t0, "Bad fileloc syntax, expected )");
1970 next_state_tokens (1);
1972 else if (state_token_is_name (t0, "nil"))
1974 next_state_tokens (1);
1975 floc->file = NULL;
1976 floc->line = 0;
1978 else
1979 fatal_reading_state (t0, "Bad fileloc syntax");
1983 /* Read the fields of a GTY-ed type. */
1984 void
1985 read_state_fields (pair_p *fields)
1987 pair_p tmp = NULL;
1988 struct state_token_st *t0 = peek_state_token (0);
1989 struct state_token_st *t1 = peek_state_token (1);
1990 struct state_token_st *t2 = peek_state_token (2);
1992 if (state_token_kind (t0) == STOK_LEFTPAR
1993 && state_token_is_name (t1, "!fields")
1994 && state_token_kind (t2) == STOK_INTEGER)
1996 int nbfields = t2->stok_un.stok_num;
1997 int nbpairs = 0;
1998 next_state_tokens (3);
1999 nbpairs = read_state_pair_list (&tmp);
2000 t0 = peek_state_token (0);
2001 if (nbpairs != nbfields)
2002 fatal_reading_state_printf
2003 (t0,
2004 "Mismatched fields number, expected %d got %d", nbpairs, nbfields);
2005 if (state_token_kind (t0) == STOK_RIGHTPAR)
2006 next_state_tokens (1);
2007 else
2008 fatal_reading_state (t0, "Bad fields expecting )");
2011 *fields = tmp;
2015 /* Read a string option. */
2016 static void
2017 read_state_string_option (options_p opt)
2019 struct state_token_st *t0 = peek_state_token (0);
2020 opt->kind = OPTION_STRING;
2021 if (state_token_kind (t0) == STOK_STRING)
2023 opt->info.string = xstrdup (t0->stok_un.stok_string);
2024 next_state_tokens (1);
2026 else if (state_token_is_name (t0, "nil"))
2028 opt->info.string = NULL;
2029 next_state_tokens (1);
2031 else
2032 fatal_reading_state (t0, "Missing name in string option");
2036 /* Read a type option. */
2037 static void
2038 read_state_type_option (options_p opt)
2040 opt->kind = OPTION_TYPE;
2041 read_state_type (&(opt->info.type));
2045 /* Read a nested option. */
2046 static void
2047 read_state_nested_option (options_p opt)
2049 struct state_token_st *t0;
2051 opt->info.nested = XCNEW (struct nested_ptr_data);
2052 opt->kind = OPTION_NESTED;
2053 read_state_type (&(opt->info.nested->type));
2054 t0 = peek_state_token (0);
2055 if (state_token_kind (t0) == STOK_STRING)
2057 opt->info.nested->convert_from = xstrdup (t0->stok_un.stok_string);
2058 next_state_tokens (1);
2060 else if (state_token_is_name (t0, "nil"))
2062 opt->info.nested->convert_from = NULL;
2063 next_state_tokens (1);
2065 else
2066 fatal_reading_state (t0, "Bad nested convert_from option");
2068 t0 = peek_state_token (0);
2069 if (state_token_kind (t0) == STOK_STRING)
2071 opt->info.nested->convert_to = xstrdup (t0->stok_un.stok_string);
2072 next_state_tokens (1);
2074 else if (state_token_is_name (t0, "nil"))
2076 opt->info.nested->convert_to = NULL;
2077 next_state_tokens (1);
2079 else
2080 fatal_reading_state (t0, "Bad nested convert_from option");
2084 /* Read an GTY option. */
2085 static void
2086 read_state_option (options_p *opt)
2088 struct state_token_st *t0 = peek_state_token (0);
2089 struct state_token_st *t1 = peek_state_token (1);
2091 if (state_token_kind (t0) == STOK_LEFTPAR &&
2092 state_token_is_name (t1, "!option"))
2094 next_state_tokens (2);
2095 t0 = peek_state_token (0);
2096 if (state_token_kind (t0) == STOK_NAME)
2098 *opt = XCNEW (struct options);
2099 if (state_token_is_name (t0, "nil"))
2100 (*opt)->name = NULL;
2101 else
2102 (*opt)->name = t0->stok_un.stok_ident->stid_name;
2103 next_state_tokens (1);
2104 t0 = peek_state_token (0);
2105 if (state_token_kind (t0) == STOK_NAME)
2107 if (state_token_is_name (t0, "string"))
2109 next_state_tokens (1);
2110 read_state_string_option (*opt);
2112 else if (state_token_is_name (t0, "type"))
2114 next_state_tokens (1);
2115 read_state_type_option (*opt);
2117 else if (state_token_is_name (t0, "nested"))
2119 next_state_tokens (1);
2120 read_state_nested_option (*opt);
2122 else
2123 fatal_reading_state (t0, "Bad option type");
2124 t0 = peek_state_token (0);
2125 if (state_token_kind (t0) != STOK_RIGHTPAR)
2126 fatal_reading_state (t0, "Bad syntax in option, expecting )");
2128 next_state_tokens (1);
2130 else
2131 fatal_reading_state (t0, "Missing option type");
2133 else
2134 fatal_reading_state (t0, "Bad name for option");
2136 else
2137 fatal_reading_state (t0, "Bad option, waiting for )");
2140 /* Read a list of options. */
2141 void
2142 read_state_options (options_p *opt)
2144 options_p head = NULL;
2145 options_p previous = NULL;
2146 options_p current_option = NULL;
2147 struct state_token_st *t0 = peek_state_token (0);
2148 struct state_token_st *t1 = peek_state_token (1);
2150 if (state_token_kind (t0) == STOK_LEFTPAR &&
2151 state_token_is_name (t1, "!options"))
2153 next_state_tokens (2);
2154 t0 = peek_state_token (0);
2155 while (state_token_kind (t0) != STOK_RIGHTPAR)
2157 read_state_option (&current_option);
2158 if (head == NULL)
2160 head = current_option;
2161 previous = head;
2163 else
2165 previous->next = current_option;
2166 previous = current_option;
2168 t0 = peek_state_token (0);
2170 next_state_tokens (1);
2172 else if (state_token_is_name (t0, "nil"))
2174 next_state_tokens (1);
2176 else
2177 fatal_reading_state (t0, "Bad options syntax");
2179 *opt = head;
2183 /* Read a version, and check against the version of the gengtype. */
2184 static void
2185 read_state_version (const char *ver_string)
2187 struct state_token_st *t0 = peek_state_token (0);
2188 struct state_token_st *t1 = peek_state_token (1);
2190 if (state_token_kind (t0) == STOK_LEFTPAR &&
2191 state_token_is_name (t1, "!version"))
2193 next_state_tokens (2);
2194 t0 = peek_state_token (0);
2195 t1 = peek_state_token (1);
2196 if (state_token_kind (t0) == STOK_STRING &&
2197 state_token_kind (t1) == STOK_RIGHTPAR)
2199 /* Check that the read version string is the same as current
2200 version. */
2201 if (strcmp (ver_string, t0->stok_un.stok_string))
2202 fatal_reading_state_printf (t0,
2203 "version string mismatch; expecting %s but got %s",
2204 ver_string,
2205 t0->stok_un.stok_string);
2206 next_state_tokens (2);
2208 else
2209 fatal_reading_state (t0, "Missing version or right parenthesis");
2211 else
2212 fatal_reading_state (t0, "Bad version syntax");
2216 /* Read a pair. */
2217 void
2218 read_state_pair (pair_p *current)
2220 struct state_token_st *t0 = peek_state_token (0);
2221 struct state_token_st *t1 = peek_state_token (1);
2222 if (state_token_kind (t0) == STOK_LEFTPAR &&
2223 state_token_is_name (t1, "!pair"))
2225 *current = XCNEW (struct pair);
2226 next_state_tokens (2);
2227 t0 = peek_state_token (0);
2228 if (state_token_kind (t0) == STOK_STRING)
2230 if (strcmp (t0->stok_un.stok_string, "nil") == 0)
2232 (*current)->name = NULL;
2234 else
2236 (*current)->name = xstrdup (t0->stok_un.stok_string);
2238 next_state_tokens (1);
2239 read_state_type (&((*current)->type));
2240 read_state_fileloc (&((*current)->line));
2241 read_state_options (&((*current)->opt));
2242 t0 = peek_state_token (0);
2243 if (state_token_kind (t0) == STOK_RIGHTPAR)
2245 next_state_tokens (1);
2247 else
2249 fatal_reading_state (t0, "Bad syntax for pair, )");
2252 else
2254 fatal_reading_state (t0, "Bad name for pair");
2257 else if (state_token_kind (t0) == STOK_NAME &&
2258 state_token_is_name (t0, "nil"))
2260 next_state_tokens (1);
2261 *current = NULL;
2263 else
2264 fatal_reading_state_printf (t0, "Bad syntax for pair, (!pair %d",
2265 state_token->stok_kind);
2269 /* Return the number of pairs actually read. */
2271 read_state_pair_list (pair_p *list)
2273 int nbpair = 0;
2274 pair_p head = NULL;
2275 pair_p previous = NULL;
2276 pair_p tmp = NULL;
2277 struct state_token_st *t0 = peek_state_token (0);
2278 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2280 read_state_pair (&tmp);
2281 if (head == NULL)
2283 head = tmp;
2284 previous = head;
2286 else
2288 previous->next = tmp;
2289 previous = tmp;
2291 t0 = peek_state_token (0);
2292 nbpair++;
2295 /* don't consume the ); the caller will eat it. */
2296 *list = head;
2297 return nbpair;
2300 /* Read the typedefs. */
2301 static void
2302 read_state_typedefs (pair_p *typedefs)
2304 int nbtypedefs = 0;
2305 pair_p list = NULL;
2306 struct state_token_st *t0 = peek_state_token (0);
2307 struct state_token_st *t1 = peek_state_token (1);
2308 struct state_token_st *t2 = peek_state_token (2);
2310 if (state_token_kind (t0) == STOK_LEFTPAR
2311 && state_token_is_name (t1, "!typedefs")
2312 && state_token_kind (t2) == STOK_INTEGER)
2314 int nbpairs = 0;
2315 nbtypedefs = t2->stok_un.stok_num;
2316 next_state_tokens (3);
2317 nbpairs = read_state_pair_list (&list);
2318 t0 = peek_state_token (0);
2319 if (nbpairs != nbtypedefs)
2320 fatal_reading_state_printf
2321 (t0,
2322 "invalid number of typedefs, expected %d but got %d",
2323 nbtypedefs, nbpairs);
2324 if (state_token_kind (t0) == STOK_RIGHTPAR)
2325 next_state_tokens (1);
2326 else
2327 fatal_reading_state (t0, "Bad typedefs syntax )");
2329 else
2330 fatal_reading_state (t0, "Bad typedefs syntax (!typedefs");
2332 if (verbosity_level >= 2)
2333 printf ("%s read %d typedefs from state\n", progname, nbtypedefs);
2334 *typedefs = list;
2338 /* Read the structures. */
2339 static void
2340 read_state_structures (type_p *structures)
2342 type_p head = NULL;
2343 type_p previous = NULL;
2344 type_p tmp;
2345 int nbstruct = 0, countstruct = 0;
2346 struct state_token_st *t0 = peek_state_token (0);
2347 struct state_token_st *t1 = peek_state_token (1);
2348 struct state_token_st *t2 = peek_state_token (2);
2350 if (state_token_kind (t0) == STOK_LEFTPAR
2351 && state_token_is_name (t1, "!structures")
2352 && state_token_kind (t2) == STOK_INTEGER)
2354 nbstruct = t2->stok_un.stok_num;
2355 next_state_tokens (3);
2356 t0 = peek_state_token (0);
2357 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2359 tmp = NULL;
2360 read_state_type (&tmp);
2361 countstruct++;
2362 if (head == NULL)
2364 head = tmp;
2365 previous = head;
2367 else
2369 previous->next = tmp;
2370 previous = tmp;
2372 t0 = peek_state_token (0);
2374 next_state_tokens (1);
2376 else
2377 fatal_reading_state (t0, "Bad structures syntax");
2378 if (countstruct != nbstruct)
2379 fatal_reading_state_printf (NULL_STATE_TOKEN,
2380 "expected %d structures but got %d",
2381 nbstruct, countstruct);
2382 if (verbosity_level >= 2)
2383 printf ("%s read %d structures from state\n", progname, nbstruct);
2384 *structures = head;
2388 /* Read the variables. */
2389 static void
2390 read_state_variables (pair_p *variables)
2392 pair_p list = NULL;
2393 int nbvars = 0;
2394 struct state_token_st *t0 = peek_state_token (0);
2395 struct state_token_st *t1 = peek_state_token (1);
2396 struct state_token_st *t2 = peek_state_token (2);
2398 if (state_token_kind (t0) == STOK_LEFTPAR
2399 && state_token_is_name (t1, "!variables")
2400 && state_token_kind (t2) == STOK_INTEGER)
2402 int nbpairs = 0;
2403 nbvars = t2->stok_un.stok_num;
2404 next_state_tokens (3);
2405 nbpairs = read_state_pair_list (&list);
2406 t0 = peek_state_token (0);
2407 if (nbpairs != nbvars)
2408 fatal_reading_state_printf
2409 (t0, "Invalid number of variables, expected %d but got %d",
2410 nbvars, nbpairs);
2411 if (state_token_kind (t0) == STOK_RIGHTPAR)
2412 next_state_tokens (1);
2413 else
2414 fatal_reading_state (t0, "Waiting for ) in variables");
2416 else
2417 fatal_reading_state (t0, "Bad variables syntax");
2418 *variables = list;
2419 if (verbosity_level >= 2)
2420 printf ("%s read %d variables from state\n", progname, nbvars);
2424 /* Read the source directory. */
2425 static void
2426 read_state_srcdir (void)
2428 struct state_token_st *t0 = peek_state_token (0);
2429 struct state_token_st *t1 = peek_state_token (1);
2430 if (state_token_kind (t0) == STOK_LEFTPAR &&
2431 state_token_is_name (t1, "!srcdir"))
2433 next_state_tokens (2);
2434 t0 = peek_state_token (0);
2435 t1 = peek_state_token (1);
2436 if (state_token_kind (t0) == STOK_STRING &&
2437 state_token_kind (t1) == STOK_RIGHTPAR)
2439 srcdir = xstrdup (t0->stok_un.stok_string);
2440 srcdir_len = strlen (srcdir);
2441 next_state_tokens (2);
2442 return;
2446 fatal_reading_state (t0, "Bad srcdir in state_file");
2450 /* Read the sequence of GCC front-end languages. */
2451 static void
2452 read_state_languages (void)
2454 struct state_token_st *t0 = peek_state_token (0);
2455 struct state_token_st *t1 = peek_state_token (1);
2456 struct state_token_st *t2 = peek_state_token (2);
2457 if (state_token_kind (t0) == STOK_LEFTPAR
2458 && state_token_is_name (t1, "!languages")
2459 && state_token_kind (t2) == STOK_INTEGER)
2461 int i = 0;
2462 num_lang_dirs = t2->stok_un.stok_num;
2463 lang_dir_names = XCNEWVEC (const char *, num_lang_dirs);
2464 next_state_tokens (3);
2465 t0 = t1 = t2 = NULL;
2466 for (i = 0; i < (int) num_lang_dirs; i++)
2468 t0 = peek_state_token (0);
2469 if (state_token_kind (t0) != STOK_NAME)
2470 fatal_reading_state (t0, "expecting language name in state file");
2471 lang_dir_names[i] = t0->stok_un.stok_ident->stid_name;
2472 next_state_tokens (1);
2474 t0 = peek_state_token (0);
2475 if (state_token_kind (t0) != STOK_RIGHTPAR)
2476 fatal_reading_state (t0, "missing ) in languages list of state file");
2477 next_state_tokens (1);
2479 else
2480 fatal_reading_state (t0, "expecting languages list in state file");
2484 /* Read the sequence of files. */
2485 static void
2486 read_state_files_list (void)
2488 struct state_token_st *t0 = peek_state_token (0);
2489 struct state_token_st *t1 = peek_state_token (1);
2490 struct state_token_st *t2 = peek_state_token (2);
2491 struct state_token_st *t3 = peek_state_token (3);
2493 if (state_token_kind (t0) == STOK_LEFTPAR
2494 && state_token_is_name (t1, "!fileslist")
2495 && state_token_kind (t2) == STOK_INTEGER
2496 && state_token_kind (t3) == STOK_INTEGER)
2498 int i = 0, j = 0;
2499 num_gt_files = t2->stok_un.stok_num;
2500 num_build_headers = t3->stok_un.stok_num;
2501 next_state_tokens (4);
2502 t0 = t1 = t2 = t3 = NULL;
2503 gt_files = XCNEWVEC (const input_file *, num_gt_files);
2504 build_headers = XCNEWVEC (const char *, num_build_headers);
2505 for (i = 0; i < (int) num_gt_files; i++)
2507 bool issrcfile = FALSE;
2508 t0 = t1 = t2 = NULL;
2509 t0 = peek_state_token (0);
2510 t1 = peek_state_token (1);
2511 t2 = peek_state_token (2);
2512 if (state_token_kind (t0) == STOK_LEFTPAR
2513 && (state_token_is_name (t1, "!file")
2514 || (issrcfile = state_token_is_name (t1, "!srcfile")))
2515 && state_token_kind (t2) == STOK_INTEGER)
2517 lang_bitmap bmap = t2->stok_un.stok_num;
2518 next_state_tokens (3);
2519 t0 = t1 = t2 = NULL;
2520 t0 = peek_state_token (0);
2521 t1 = peek_state_token (1);
2522 if (state_token_kind (t0) == STOK_STRING
2523 && state_token_kind (t1) == STOK_RIGHTPAR)
2525 const char *fnam = t0->stok_un.stok_string;
2526 /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
2527 input_file *curgt = NULL;
2528 if (issrcfile)
2530 static const char dirsepstr[2] =
2531 { DIR_SEPARATOR, (char) 0 };
2532 char *fullpath = concat (srcdir, dirsepstr, fnam, NULL);
2533 curgt = input_file_by_name (fullpath);
2534 free (fullpath);
2536 else
2538 curgt = input_file_by_name (fnam);
2539 /* Look for a header file created during the build,
2540 which looks like "./<filename>.h". */
2541 int len = strlen (fnam);
2542 if (len >= 5
2543 && fnam[0] == '.'
2544 && IS_DIR_SEPARATOR (fnam[1])
2545 && fnam[len-2] == '.'
2546 && fnam[len-1] == 'h')
2548 char *buf = (char *) xmalloc (len - 1);
2549 /* Strip the leading "./" from the filename. */
2550 strcpy (buf, &fnam[2]);
2551 build_headers[j++] = buf;
2554 set_lang_bitmap (curgt, bmap);
2555 gt_files[i] = curgt;
2556 next_state_tokens (2);
2558 else
2559 fatal_reading_state (t0,
2560 "bad file in !fileslist of state file");
2562 else
2563 fatal_reading_state (t0,
2564 "expecting file in !fileslist of state file");
2566 t0 = peek_state_token (0);
2567 if (state_token_kind (t0) != STOK_RIGHTPAR)
2568 fatal_reading_state (t0, "missing ) for !fileslist in state file");
2569 next_state_tokens (1);
2571 else
2572 fatal_reading_state (t0, "missing !fileslist in state file");
2576 /* Read the trailer. */
2577 static void
2578 read_state_trailer (void)
2580 struct state_token_st *t0 = peek_state_token (0);
2581 struct state_token_st *t1 = peek_state_token (1);
2582 struct state_token_st *t2 = peek_state_token (2);
2584 if (state_token_kind (t0) == STOK_LEFTPAR
2585 && state_token_is_name (t1, "!endfile")
2586 && state_token_kind (t2) == STOK_RIGHTPAR)
2587 next_state_tokens (3);
2588 else
2589 fatal_reading_state (t0, "missing !endfile in state file");
2593 /* Utility functions for the state_seen_types hash table. */
2594 static unsigned
2595 hash_type_number (const void *ty)
2597 const struct type *type = (const struct type *) ty;
2599 return type->state_number;
2602 static int
2603 equals_type_number (const void *ty1, const void *ty2)
2605 const struct type *type1 = (const struct type *) ty1;
2606 const struct type *type2 = (const struct type *) ty2;
2608 return type1->state_number == type2->state_number;
2612 /* The function reading the state, called by main from gengtype.c. */
2613 void
2614 read_state (const char *path)
2616 state_file = fopen (path, "r");
2617 if (state_file == NULL)
2618 fatal ("Failed to open state file %s for reading [%s]", path,
2619 xstrerror (errno));
2620 state_path = path;
2621 state_line = 1;
2623 if (verbosity_level >= 1)
2625 printf ("%s reading state file %s;", progname, state_path);
2626 if (verbosity_level >= 2)
2627 putchar ('\n');
2628 fflush (stdout);
2631 state_seen_types =
2632 htab_create (2017, hash_type_number, equals_type_number, NULL);
2633 state_ident_tab =
2634 htab_create (4027, htab_hash_string, htab_eq_string, NULL);
2635 read_state_version (version_string);
2636 read_state_srcdir ();
2637 read_state_languages ();
2638 read_state_files_list ();
2639 read_state_structures (&structures);
2640 if (ferror (state_file))
2641 fatal_reading_state_printf
2642 (NULL_STATE_TOKEN, "input error while reading state [%s]",
2643 xstrerror (errno));
2644 read_state_typedefs (&typedefs);
2645 read_state_variables (&variables);
2646 read_state_trailer ();
2648 if (verbosity_level >= 1)
2650 printf ("%s read %ld bytes.\n", progname, ftell (state_file));
2651 fflush (stdout);
2654 if (fclose (state_file))
2655 fatal ("failed to close read state file %s [%s]",
2656 path, xstrerror (errno));
2657 state_file = NULL;
2658 state_path = NULL;
2661 /* End of file gengtype-state.c. */