Concretize gimple_label_label
[official-gcc.git] / gcc / gengtype-state.c
blob5b7017f1e3e114f97efbd6d863bc870d6221e291
1 /* Gengtype persistent state serialization & de-serialization.
2 Useful for gengtype in plugin mode.
4 Copyright (C) 2010-2014 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 GENERATOR_FILE
27 #include "bconfig.h"
28 #else
29 #include "config.h"
30 #endif
31 #include "system.h"
32 #include "errors.h" /* For fatal. */
33 #include "hashtab.h"
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_PARAM_STRUCT:
57 return CONST_CAST (struct fileloc*, &ty->u.param_struct.line);
58 case TYPE_SCALAR:
59 case TYPE_STRING:
60 case TYPE_POINTER:
61 case TYPE_ARRAY:
62 return NULL;
63 default:
64 gcc_unreachable ();
68 /* The state file has simplistic lispy lexical tokens. Its lexer gives
69 a linked list of struct state_token_st, through the peek_state_token
70 function. Lexical tokens are consumed with next_state_tokens. */
73 /* The lexical kind of each lispy token. */
74 enum state_token_en
76 STOK_NONE, /* Never used. */
77 STOK_INTEGER, /* Integer token. */
78 STOK_STRING, /* String token. */
79 STOK_LEFTPAR, /* Left opening parenthesis. */
80 STOK_RIGHTPAR, /* Right closing parenthesis. */
81 STOK_NAME /* hash-consed name or identifier. */
85 /* Structure and hash-table used to share identifiers or names. */
86 struct state_ident_st
88 /* TODO: We could improve the parser by reserving identifiers for
89 state keywords and adding a keyword number for them. That would
90 mean adding another field in this state_ident_st struct. */
91 char stid_name[1]; /* actually bigger & null terminated */
93 static htab_t state_ident_tab;
96 /* The state_token_st structure is for lexical tokens in the read
97 state file. The stok_kind field discriminates the union. Tokens
98 are allocated by peek_state_token which calls read_a_state_token
99 which allocate them. Tokens are freed by calls to
100 next_state_tokens. Token are organized in a FIFO look-ahead queue
101 filled by peek_state_token. */
102 struct state_token_st
104 enum state_token_en stok_kind; /* the lexical kind
105 discriminates the stok_un
106 union */
107 int stok_line; /* the line number */
108 int stok_col; /* the column number */
109 const char *stok_file; /* the file path */
110 struct state_token_st *stok_next; /* the next token in the
111 queue, when peeked */
112 union /* discriminated by stok_kind! */
114 int stok_num; /* when STOK_INTEGER */
115 char stok_string[1]; /* when STOK_STRING, actual size is
116 bigger and null terminated */
117 struct state_ident_st *stok_ident; /* when STOK_IDENT */
118 void *stok_ptr; /* null otherwise */
120 stok_un;
126 #define NULL_STATE_TOKEN (struct state_token_st*)0
128 /* the state_token pointer contains the leftmost current token. The
129 tokens are organized in a linked queue, using stok_next, for token
130 look-ahead. */
131 struct state_token_st *state_token = NULL_STATE_TOKEN;
133 /* Used by the reading lexer. */
134 static FILE *state_file;
135 static const char *state_path = NULL;
136 static int state_line = 0;
137 static long state_bol = 0; /* offset of beginning of line */
139 /* A class for writing out s-expressions, keeping track of newlines and
140 nested indentation. */
141 class s_expr_writer
143 public:
144 s_expr_writer ();
146 void write_new_line ();
147 void write_any_indent (int leading_spaces);
149 void begin_s_expr (const char *tag);
150 void end_s_expr ();
152 private:
153 int m_indent_amount;
154 int m_had_recent_newline;
155 }; // class s_expr_writer
157 /* A class for writing out "gtype.state". */
158 class state_writer : public s_expr_writer
160 public:
161 state_writer ();
163 private:
164 void write_state_fileloc (struct fileloc *floc);
165 void write_state_fields (pair_p fields);
166 void write_state_a_string (const char *s);
167 void write_state_string_option (options_p current);
168 void write_state_type_option (options_p current);
169 void write_state_nested_option (options_p current);
170 void write_state_option (options_p current);
171 void write_state_options (options_p opt);
172 void write_state_lang_bitmap (lang_bitmap bitmap);
173 void write_state_version (const char *version);
174 void write_state_scalar_type (type_p current);
175 void write_state_string_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_param_struct_type (type_p current);
183 void write_state_pointer_type (type_p current);
184 void write_state_array_type (type_p current);
185 void write_state_gc_used (enum gc_used_enum gus);
186 void write_state_common_type_content (type_p current);
187 void write_state_type (type_p current);
188 void write_state_pair (pair_p current);
189 int write_state_pair_list (pair_p list);
190 void write_state_typedefs (void);
191 void write_state_structures (void);
192 void write_state_param_structs (void);
193 void write_state_variables (void);
194 void write_state_srcdir (void);
195 void write_state_files_list (void);
196 void write_state_languages (void);
198 friend void write_state (const char *state_path);
200 private:
201 /* Counter of written types. */
202 int m_state_written_type_count;
203 }; // class state_writer
206 /* class s_expr_writer's trivial constructor. */
207 s_expr_writer::s_expr_writer ()
208 : m_indent_amount (0),
209 m_had_recent_newline (0)
213 /* Write a newline to the output file, merging adjacent newlines. */
214 void
215 s_expr_writer::write_new_line (void)
217 /* Don't add a newline if we've just had one. */
218 if (!m_had_recent_newline)
220 fprintf (state_file, "\n");
221 m_had_recent_newline = 1;
225 /* If we've just had a newline, write the indentation amount, potentially
226 omitting some spaces.
228 LEADING_SPACES exists to support code that writes strings with leading
229 spaces (e.g " foo") which might occur within a line, or could be the first
230 thing on a line. By passing leading_spaces == 1, when such a string is the
231 first thing on a line, write_any_indent () swallows the successive
232 leading spaces into the indentation so that the "foo" begins at the expected
233 column. */
234 void
235 s_expr_writer::write_any_indent (int leading_spaces)
237 int i;
238 int amount = m_indent_amount - leading_spaces;
239 if (m_had_recent_newline)
240 for (i = 0; i < amount; i++)
241 fprintf (state_file, " ");
242 m_had_recent_newline = 0;
245 /* Write the beginning of a new s-expresion e.g. "(!foo "
246 The writer automatically adds whitespace to show the hierarchical
247 structure of the expressions, so each one starts on a new line,
248 and any within it will be at an increased indentation level. */
249 void
250 s_expr_writer::begin_s_expr (const char *tag)
252 write_new_line ();
253 write_any_indent (0);
254 fprintf (state_file, "(!%s ", tag);
255 m_indent_amount++;
258 /* Write out the end of an s-expression: any necssessary indentation,
259 a closing parenthesis, and a new line. */
260 void
261 s_expr_writer::end_s_expr (void)
263 m_indent_amount--;
264 write_any_indent (0);
265 fprintf (state_file, ")");
266 write_new_line ();
270 /* class state_writer's trivial constructor. */
271 state_writer::state_writer ()
272 : s_expr_writer (),
273 m_state_written_type_count (0)
278 /* Fatal error messages when reading the state. They are extremely
279 unlikely, and only appear when this gengtype-state.c file is buggy,
280 or when reading a gengtype state which was not generated by the
281 same version of gengtype or GCC. */
284 /* Fatal message while reading state. */
285 static void
286 fatal_reading_state (struct state_token_st* tok, const char*msg)
288 if (tok)
289 fatal ("%s:%d:%d: Invalid state file; %s",
290 tok->stok_file, tok->stok_line, tok->stok_col,
291 msg);
292 else
293 fatal ("%s:%d: Invalid state file; %s",
294 state_path, state_line, msg);
298 /* Fatal printf-like message while reading state. This can't be a
299 function, because there is no way to pass a va_arg to a variant of
300 fatal. */
301 #define fatal_reading_state_printf(Tok,Fmt,...) do { \
302 struct state_token_st* badtok = Tok; \
303 if (badtok) \
304 fatal ("%s:%d:%d: Invalid state file; " Fmt, \
305 badtok->stok_file, \
306 badtok->stok_line, \
307 badtok->stok_col, __VA_ARGS__); \
308 else \
309 fatal ("%s:%d: Invalid state file; " Fmt, \
310 state_path, state_line, __VA_ARGS__); \
311 } while (0)
314 /* Find or allocate an identifier in our name hash table. */
315 static struct state_ident_st *
316 state_ident_by_name (const char *name, enum insert_option optins)
318 PTR *slot = NULL;
319 int namlen = 0;
320 struct state_ident_st *stid = NULL;
322 if (!name || !name[0])
323 return NULL;
325 slot = htab_find_slot (state_ident_tab, name, optins);
326 if (!slot)
327 return NULL;
329 namlen = strlen (name);
330 stid =
331 (struct state_ident_st *) xmalloc (sizeof (struct state_ident_st) +
332 namlen);
333 memset (stid, 0, sizeof (struct state_ident_st) + namlen);
334 strcpy (stid->stid_name, name);
335 *slot = stid;
337 return stid;
340 /* Our token lexer is heavily inspired by MELT's lexer, and share some
341 code with the file gcc/melt-runtime.c of the GCC MELT branch! We
342 really want the gengtype state to be easily parsable by MELT. This
343 is a usual lispy lexing routine, dealing with spaces and comments,
344 numbers, parenthesis, names, strings. */
345 static struct state_token_st *
346 read_a_state_token (void)
348 int c = 0;
349 long curoff = 0;
350 struct state_token_st *tk = NULL;
352 again: /* Read again, e.g. after a comment or spaces. */
353 c = getc (state_file);
354 if (c == EOF)
355 return NULL;
357 /* Handle spaces, count lines. */
358 if (c == '\n')
360 state_line++;
361 state_bol = curoff = ftell (state_file);
362 goto again;
364 if (ISSPACE (c))
365 goto again;
366 /* Skip comments starting with semi-colon. */
367 if (c == ';')
371 c = getc (state_file);
373 while (c > 0 && c != '\n');
374 if (c == '\n')
376 state_line++;
377 state_bol = curoff = ftell (state_file);
379 goto again;
381 /* Read signed numbers. */
382 if (ISDIGIT (c) || c == '-' || c == '+')
383 { /* number */
384 int n = 0;
385 ungetc (c, state_file);
386 curoff = ftell (state_file);
387 if (fscanf (state_file, "%d", &n) <= 0)
388 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error in number");
389 tk = XCNEW (struct state_token_st);
390 tk->stok_kind = STOK_INTEGER;
391 tk->stok_line = state_line;
392 tk->stok_col = curoff - state_bol;
393 tk->stok_file = state_path;
394 tk->stok_next = NULL;
395 tk->stok_un.stok_num = n;
397 return tk;
399 /* Read an opening left parenthesis. */
400 else if (c == '(')
402 curoff = ftell (state_file);
403 tk = XCNEW (struct state_token_st);
404 tk->stok_kind = STOK_LEFTPAR;
405 tk->stok_line = state_line;
406 tk->stok_col = curoff - state_bol;
407 tk->stok_file = state_path;
408 tk->stok_next = NULL;
410 return tk;
412 /* Read an closing right parenthesis. */
413 else if (c == ')')
415 curoff = ftell (state_file);
416 tk = XCNEW (struct state_token_st);
417 tk->stok_kind = STOK_RIGHTPAR;
418 tk->stok_line = state_line;
419 tk->stok_col = curoff - state_bol;
420 tk->stok_file = state_path;
421 tk->stok_next = NULL;
423 return tk;
425 /* Read identifiers, using an obstack. */
426 else if (ISALPHA (c) || c == '_' || c == '$' || c == '!' || c == '#')
428 struct obstack id_obstack;
429 struct state_ident_st *sid = NULL;
430 char *ids = NULL;
431 obstack_init (&id_obstack);
432 curoff = ftell (state_file);
433 while (ISALNUM (c) || c == '_' || c == '$' || c == '!' || c == '#')
435 obstack_1grow (&id_obstack, c);
436 c = getc (state_file);
437 if (c < 0)
438 break;
440 if (c >= 0)
441 ungetc (c, state_file);
442 obstack_1grow (&id_obstack, (char) 0);
443 ids = XOBFINISH (&id_obstack, char *);
444 sid = state_ident_by_name (ids, INSERT);
445 obstack_free (&id_obstack, NULL);
446 ids = NULL;
447 tk = XCNEW (struct state_token_st);
448 tk->stok_kind = STOK_NAME;
449 tk->stok_line = state_line;
450 tk->stok_col = curoff - state_bol;
451 tk->stok_file = state_path;
452 tk->stok_next = NULL;
453 tk->stok_un.stok_ident = sid;
455 return tk;
457 /* Read a string, dealing with escape sequences a la C! */
458 else if (c == '"')
460 char *cstr = NULL;
461 int cslen = 0;
462 struct obstack bstring_obstack;
463 obstack_init (&bstring_obstack);
464 curoff = ftell (state_file);
465 while ((c = getc (state_file)) != '"' && c >= 0)
467 if (ISPRINT (c) && c != '\\')
468 obstack_1grow (&bstring_obstack, (char) c);
469 else if (ISSPACE (c) && c != '\n')
470 obstack_1grow (&bstring_obstack, (char) c);
471 else if (c == '\\')
473 c = getc (state_file);
474 switch (c)
476 case 'a':
477 obstack_1grow (&bstring_obstack, '\a');
478 c = getc (state_file);
479 break;
480 case 'b':
481 obstack_1grow (&bstring_obstack, '\b');
482 c = getc (state_file);
483 break;
484 case 't':
485 obstack_1grow (&bstring_obstack, '\t');
486 c = getc (state_file);
487 break;
488 case 'n':
489 obstack_1grow (&bstring_obstack, '\n');
490 c = getc (state_file);
491 break;
492 case 'v':
493 obstack_1grow (&bstring_obstack, '\v');
494 c = getc (state_file);
495 break;
496 case 'f':
497 obstack_1grow (&bstring_obstack, '\f');
498 c = getc (state_file);
499 break;
500 case 'r':
501 obstack_1grow (&bstring_obstack, '\r');
502 c = getc (state_file);
503 break;
504 case '"':
505 obstack_1grow (&bstring_obstack, '\"');
506 c = getc (state_file);
507 break;
508 case '\\':
509 obstack_1grow (&bstring_obstack, '\\');
510 c = getc (state_file);
511 break;
512 case ' ':
513 obstack_1grow (&bstring_obstack, ' ');
514 c = getc (state_file);
515 break;
516 case 'x':
518 unsigned int cx = 0;
519 if (fscanf (state_file, "%02x", &cx) > 0 && cx > 0)
520 obstack_1grow (&bstring_obstack, cx);
521 else
522 fatal_reading_state
523 (NULL_STATE_TOKEN,
524 "Lexical error in string hex escape");
525 c = getc (state_file);
526 break;
528 default:
529 fatal_reading_state
530 (NULL_STATE_TOKEN,
531 "Lexical error - unknown string escape");
534 else
535 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error...");
537 if (c != '"')
538 fatal_reading_state (NULL_STATE_TOKEN, "Unterminated string");
539 obstack_1grow (&bstring_obstack, '\0');
540 cstr = XOBFINISH (&bstring_obstack, char *);
541 cslen = strlen (cstr);
542 tk = (struct state_token_st *)
543 xcalloc (sizeof (struct state_token_st) + cslen, 1);
544 tk->stok_kind = STOK_STRING;
545 tk->stok_line = state_line;
546 tk->stok_col = curoff - state_bol;
547 tk->stok_file = state_path;
548 tk->stok_next = NULL;
549 strcpy (tk->stok_un.stok_string, cstr);
550 obstack_free (&bstring_obstack, NULL);
552 return tk;
554 /* Got an unexpected character. */
555 fatal_reading_state_printf
556 (NULL_STATE_TOKEN,
557 "Lexical error at offset %ld - bad character \\%03o = '%c'",
558 ftell (state_file), c, c);
561 /* Used for lexical look-ahead. Retrieves the lexical token of rank
562 DEPTH, starting with 0 when reading the state file. Gives null on
563 end of file. */
564 static struct state_token_st *
565 peek_state_token (int depth)
567 int remdepth = depth;
568 struct state_token_st **ptoken = &state_token;
569 struct state_token_st *tok = NULL;
571 while (remdepth >= 0)
573 if (*ptoken == NULL)
575 *ptoken = tok = read_a_state_token ();
576 if (tok == NULL)
577 return NULL;
579 tok = *ptoken;
580 ptoken = &((*ptoken)->stok_next);
581 remdepth--;
584 return tok;
587 /* Consume the next DEPTH tokens and free them. */
588 static void
589 next_state_tokens (int depth)
591 struct state_token_st *n;
593 while (depth > 0)
595 if (state_token != NULL)
597 n = state_token->stok_next;
598 free (state_token);
599 state_token = n;
601 else
602 fatal_reading_state (NULL_STATE_TOKEN, "Tokens stack empty");
604 depth--;
608 /* Safely retrieve the lexical kind of a token. */
609 static inline enum state_token_en
610 state_token_kind (struct state_token_st *p)
612 if (p == NULL)
613 return STOK_NONE;
614 else
615 return p->stok_kind;
618 /* Test if a token is a given name i.e. an identifier. */
619 static inline bool
620 state_token_is_name (struct state_token_st *p, const char *name)
622 if (p == NULL)
623 return false;
625 if (p->stok_kind != STOK_NAME)
626 return false;
628 return !strcmp (p->stok_un.stok_ident->stid_name, name);
632 /* Following routines are useful for serializing datas.
634 * We want to serialize :
635 * - typedefs list
636 * - structures list
637 * - param_structs list
638 * - variables list
640 * So, we have one routine for each kind of data. The main writing
641 * routine is write_state. The main reading routine is
642 * read_state. Most writing routines write_state_FOO have a
643 * corresponding reading routine read_state_FOO. Reading is done in a
644 * recursive descending way, and any read error is fatal.
647 /* When reading the state, we need to remember the previously seen
648 types by their state_number, since GTY-ed types are usually
649 shared. */
650 static htab_t state_seen_types;
652 /* Return the length of a linked list made of pairs. */
653 static int pair_list_length (pair_p list);
655 /* Compute the length of a list of pairs, starting from the first
656 one. */
657 static int
658 pair_list_length (pair_p list)
660 int nbpair = 0;
661 pair_p l = NULL;
662 for (l = list; l; l = l->next)
663 nbpair++;
664 return nbpair;
667 /* Write a file location. Files relative to $(srcdir) are quite
668 frequent and are handled specially. This ensures that two gengtype
669 state file-s produced by gengtype on the same GCC source tree are
670 very similar and can be reasonably compared with diff, even if the
671 two GCC source trees have different absolute paths. */
672 void
673 state_writer::write_state_fileloc (struct fileloc *floc)
676 if (floc != NULL && floc->line > 0)
678 const char *srcrelpath = NULL;
679 gcc_assert (floc->file != NULL);
680 /* Most of the files are inside $(srcdir) so it is worth to
681 handle them specially. */
682 srcrelpath = get_file_srcdir_relative_path (floc->file);
683 if (srcrelpath != NULL)
685 begin_s_expr ("srcfileloc");
686 write_state_a_string (srcrelpath);
688 else
690 begin_s_expr ("fileloc");
691 write_state_a_string (get_input_file_name (floc->file));
693 fprintf (state_file, " %d", floc->line);
694 end_s_expr ();
696 else
697 fprintf (state_file, "nil ");
700 /* Write a list of fields. */
701 void
702 state_writer::write_state_fields (pair_p fields)
704 int nbfields = pair_list_length (fields);
705 int nbpairs = 0;
706 begin_s_expr ("fields");
707 fprintf (state_file, "%d ", nbfields);
708 nbpairs = write_state_pair_list (fields);
709 gcc_assert (nbpairs == nbfields);
710 end_s_expr ();
713 /* Write a null-terminated string in our lexical convention, very
714 similar to the convention of C. */
715 void
716 state_writer::write_state_a_string (const char *s)
718 char c;
720 write_any_indent (1);
722 fputs (" \"", state_file);
723 for (; *s != 0; s++)
725 c = *s;
726 switch (c)
728 case '\a':
729 fputs ("\\a", state_file);
730 break;
731 case '\b':
732 fputs ("\\b", state_file);
733 break;
734 case '\t':
735 fputs ("\\t", state_file);
736 break;
737 case '\n':
738 fputs ("\\n", state_file);
739 break;
740 case '\v':
741 fputs ("\\v", state_file);
742 break;
743 case '\f':
744 fputs ("\\f", state_file);
745 break;
746 case '\r':
747 fputs ("\\r", state_file);
748 break;
749 case '\"':
750 fputs ("\\\"", state_file);
751 break;
752 case '\\':
753 fputs ("\\\\", state_file);
754 break;
755 default:
756 if (ISPRINT (c))
757 putc (c, state_file);
758 else
759 fprintf (state_file, "\\x%02x", (unsigned) c);
762 fputs ("\"", state_file);
765 /* Our option-s have three kinds, each with its writer. */
766 void
767 state_writer::write_state_string_option (options_p current)
769 write_any_indent (0);
770 fprintf (state_file, "string ");
771 if (current->info.string != NULL)
772 write_state_a_string (current->info.string);
773 else
774 fprintf (state_file, " nil ");
777 void
778 state_writer::write_state_type_option (options_p current)
780 write_any_indent (0);
781 fprintf (state_file, "type ");
782 write_state_type (current->info.type);
785 void
786 state_writer::write_state_nested_option (options_p current)
788 write_any_indent (0);
789 fprintf (state_file, "nested ");
790 write_state_type (current->info.nested->type);
791 if (current->info.nested->convert_from != NULL)
792 write_state_a_string (current->info.nested->convert_from);
793 else
795 write_any_indent (1);
796 fprintf (state_file, " nil ");
799 if (current->info.nested->convert_to != NULL)
800 write_state_a_string (current->info.nested->convert_to);
801 else
803 write_any_indent (1);
804 fprintf (state_file, " nil ");
808 void
809 state_writer::write_state_option (options_p current)
811 begin_s_expr ("option");
813 write_any_indent (0);
814 if (current->name != NULL)
815 fprintf (state_file, "%s ", current->name);
816 else
817 fprintf (state_file, "nil ");
819 switch (current->kind)
821 case OPTION_STRING:
822 write_state_string_option (current);
823 break;
824 case OPTION_TYPE:
825 write_state_type_option (current);
826 break;
827 case OPTION_NESTED:
828 write_state_nested_option (current);
829 break;
830 default:
831 fatal ("Option tag unknown");
834 /* Terminate the "option" s-expression. */
835 end_s_expr ();
840 /* Write a list of GTY options. */
841 void
842 state_writer::write_state_options (options_p opt)
844 options_p current;
846 if (opt == NULL)
848 write_any_indent (0);
849 fprintf (state_file, "nil ");
850 return;
853 begin_s_expr ("options");
854 for (current = opt; current != NULL; current = current->next)
855 write_state_option (current);
856 end_s_expr ();
860 /* Write a bitmap representing a set of GCC front-end languages. */
861 void
862 state_writer::write_state_lang_bitmap (lang_bitmap bitmap)
864 write_any_indent (0);
865 fprintf (state_file, "%d ", (int) bitmap);
868 /* Write version information. */
869 void
870 state_writer::write_state_version (const char *version)
872 begin_s_expr ("version");
873 write_state_a_string (version);
874 end_s_expr ();
877 /* Write a scalar type. We have only two of these. */
878 void
879 state_writer::write_state_scalar_type (type_p current)
881 write_any_indent (0);
882 if (current == &scalar_nonchar)
883 fprintf (state_file, "scalar_nonchar ");
884 else if (current == &scalar_char)
885 fprintf (state_file, "scalar_char ");
886 else
887 fatal ("Unexpected type in write_state_scalar_type");
889 write_state_common_type_content (current);
892 /* Write the string type. There is only one such thing! */
893 void
894 state_writer::write_state_string_type (type_p current)
896 if (current == &string_type)
898 write_any_indent (0);
899 fprintf (state_file, "string ");
900 write_state_common_type_content (current);
902 else
903 fatal ("Unexpected type in write_state_string_type");
906 /* Write an undefined type. */
907 void
908 state_writer::write_state_undefined_type (type_p current)
910 DBGPRINTF ("undefined type @ %p #%d '%s'", (void *) current,
911 current->state_number, current->u.s.tag);
912 write_any_indent (0);
913 fprintf (state_file, "undefined ");
914 gcc_assert (current->gc_used == GC_UNUSED);
915 write_state_common_type_content (current);
916 if (current->u.s.tag != NULL)
917 write_state_a_string (current->u.s.tag);
918 else
920 write_any_indent (0);
921 fprintf (state_file, "nil");
924 write_state_fileloc (type_lineloc (current));
928 /* Common code to write structure like types. */
929 void
930 state_writer::write_state_struct_union_type (type_p current,
931 const char *kindstr)
933 DBGPRINTF ("%s type @ %p #%d '%s'", kindstr, (void *) current,
934 current->state_number, current->u.s.tag);
935 write_any_indent (0);
936 fprintf (state_file, "%s ", kindstr);
937 write_state_common_type_content (current);
938 if (current->u.s.tag != NULL)
939 write_state_a_string (current->u.s.tag);
940 else
942 write_any_indent (0);
943 fprintf (state_file, "nil");
946 write_state_fileloc (type_lineloc (current));
947 write_state_fields (current->u.s.fields);
948 write_state_options (current->u.s.opt);
949 write_state_lang_bitmap (current->u.s.bitmap);
953 /* Write a GTY struct type. */
954 void
955 state_writer::write_state_struct_type (type_p current)
957 write_state_struct_union_type (current, "struct");
958 write_state_type (current->u.s.lang_struct);
959 write_state_type (current->u.s.base_class);
962 /* Write a GTY user-defined struct type. */
963 void
964 state_writer::write_state_user_struct_type (type_p current)
966 DBGPRINTF ("user_struct type @ %p #%d '%s'", (void *) current,
967 current->state_number, current->u.s.tag);
968 write_any_indent (0);
969 fprintf (state_file, "user_struct ");
970 write_state_common_type_content (current);
971 if (current->u.s.tag != NULL)
972 write_state_a_string (current->u.s.tag);
973 else
975 write_any_indent (0);
976 fprintf (state_file, "nil");
978 write_state_fileloc (type_lineloc (current));
979 write_state_fields (current->u.s.fields);
982 /* write a GTY union type. */
983 void
984 state_writer::write_state_union_type (type_p current)
986 write_state_struct_union_type (current, "union");
987 write_state_type (current->u.s.lang_struct);
990 /* Write a lang_struct type. This is tricky and was painful to debug,
991 we deal with the next field specifically within their lang_struct
992 subfield, which points to a linked list of homonumous types.
993 Change this function with extreme care, see also
994 read_state_lang_struct_type. */
995 void
996 state_writer::write_state_lang_struct_type (type_p current)
998 int nbhomontype = 0;
999 type_p hty = NULL;
1000 const char *homoname = 0;
1001 write_state_struct_union_type (current, "lang_struct");
1002 /* lang_struct-ures are particularly tricky, since their
1003 u.s.lang_struct field gives a list of homonymous struct-s or
1004 union-s! */
1005 DBGPRINTF ("lang_struct @ %p #%d", (void *) current, current->state_number);
1006 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
1008 nbhomontype++;
1009 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype,
1010 (void *) hty, hty->state_number, hty->u.s.tag);
1011 /* Every member of the homonymous list should have the same tag. */
1012 gcc_assert (union_or_struct_p (hty));
1013 gcc_assert (hty->u.s.lang_struct == current);
1014 if (!homoname)
1015 homoname = hty->u.s.tag;
1016 gcc_assert (strcmp (homoname, hty->u.s.tag) == 0);
1018 begin_s_expr ("homotypes");
1019 fprintf (state_file, "%d", nbhomontype);
1020 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next)
1021 write_state_type (hty);
1022 end_s_expr ();
1025 /* Write a parametrized structure GTY type. */
1026 void
1027 state_writer::write_state_param_struct_type (type_p current)
1029 int i;
1031 write_any_indent (0);
1032 fprintf (state_file, "param_struct ");
1033 write_state_common_type_content (current);
1034 write_state_type (current->u.param_struct.stru);
1035 for (i = 0; i < NUM_PARAM; i++)
1037 if (current->u.param_struct.param[i] != NULL)
1038 write_state_type (current->u.param_struct.param[i]);
1039 else
1041 write_any_indent (0);
1042 fprintf (state_file, "nil ");
1045 write_state_fileloc (&current->u.param_struct.line);
1048 /* Write a pointer type. */
1049 void
1050 state_writer::write_state_pointer_type (type_p current)
1052 write_any_indent (0);
1053 fprintf (state_file, "pointer ");
1054 write_state_common_type_content (current);
1055 write_state_type (current->u.p);
1058 /* Write an array type. */
1059 void
1060 state_writer::write_state_array_type (type_p current)
1062 write_any_indent (0);
1063 fprintf (state_file, "array ");
1064 write_state_common_type_content (current);
1065 if (current->u.a.len != NULL)
1066 write_state_a_string (current->u.a.len);
1067 else
1069 write_any_indent (1);
1070 fprintf (state_file, " nil");
1073 write_any_indent (1);
1074 fprintf (state_file, " ");
1075 write_state_type (current->u.a.p);
1078 /* Write the gc_used information. */
1079 void
1080 state_writer::write_state_gc_used (enum gc_used_enum gus)
1082 write_any_indent (1);
1083 switch (gus)
1085 case GC_UNUSED:
1086 fprintf (state_file, " gc_unused");
1087 break;
1088 case GC_USED:
1089 fprintf (state_file, " gc_used");
1090 break;
1091 case GC_MAYBE_POINTED_TO:
1092 fprintf (state_file, " gc_maybe_pointed_to");
1093 break;
1094 case GC_POINTED_TO:
1095 fprintf (state_file, " gc_pointed_to");
1096 break;
1097 default:
1098 gcc_unreachable ();
1102 /* Utility routine to write the common content of all types. Notice
1103 that the next field is *not* written on purpose. */
1104 void
1105 state_writer::write_state_common_type_content (type_p current)
1107 write_any_indent (0);
1108 fprintf (state_file, "%d ", current->state_number);
1109 /* We do not write the next type, because list of types are
1110 explicitly written. However, lang_struct are special in that
1111 respect. See function write_state_lang_struct_type for more. */
1112 write_state_type (current->pointer_to);
1113 write_state_gc_used (current->gc_used);
1117 /* The important and recursive routine writing GTY types as understood
1118 by gengtype. Types which have a positive state_number have already
1119 been seen and written. */
1120 void
1121 state_writer::write_state_type (type_p current)
1123 write_any_indent (0);
1124 if (current == NULL)
1126 fprintf (state_file, "nil ");
1127 return;
1130 begin_s_expr ("type");
1132 if (current->state_number > 0)
1134 write_any_indent (0);
1135 fprintf (state_file, "already_seen %d", current->state_number);
1137 else
1139 m_state_written_type_count++;
1140 DBGPRINTF ("writing type #%d @%p old number %d", m_state_written_type_count,
1141 (void *) current, current->state_number);
1142 current->state_number = m_state_written_type_count;
1143 switch (current->kind)
1145 case TYPE_NONE:
1146 gcc_unreachable ();
1147 case TYPE_UNDEFINED:
1148 write_state_undefined_type (current);
1149 break;
1150 case TYPE_STRUCT:
1151 write_state_struct_type (current);
1152 break;
1153 case TYPE_USER_STRUCT:
1154 write_state_user_struct_type (current);
1155 break;
1156 case TYPE_UNION:
1157 write_state_union_type (current);
1158 break;
1159 case TYPE_POINTER:
1160 write_state_pointer_type (current);
1161 break;
1162 case TYPE_ARRAY:
1163 write_state_array_type (current);
1164 break;
1165 case TYPE_LANG_STRUCT:
1166 write_state_lang_struct_type (current);
1167 break;
1168 case TYPE_PARAM_STRUCT:
1169 write_state_param_struct_type (current);
1170 break;
1171 case TYPE_SCALAR:
1172 write_state_scalar_type (current);
1173 break;
1174 case TYPE_STRING:
1175 write_state_string_type (current);
1176 break;
1180 /* Terminate the "type" s-expression. */
1181 end_s_expr ();
1185 /* Write a pair. */
1186 void
1187 state_writer::write_state_pair (pair_p current)
1189 if (current == NULL)
1191 write_any_indent (0);
1192 fprintf (state_file, "nil)");
1193 return;
1196 begin_s_expr ("pair");
1198 if (current->name != NULL)
1199 write_state_a_string (current->name);
1200 else
1201 write_state_a_string ("nil");
1203 write_state_type (current->type);
1204 write_state_fileloc (&(current->line));
1205 write_state_options (current->opt);
1207 /* Terminate the "pair" s-expression. */
1208 end_s_expr ();
1211 /* Write a pair list and return the number of pairs written. */
1213 state_writer::write_state_pair_list (pair_p list)
1215 int nbpair = 0;
1216 pair_p current;
1218 for (current = list; current != NULL; current = current->next)
1220 write_state_pair (current);
1221 nbpair++;
1223 return nbpair;
1227 /* When writing imported linked lists, like typedefs, structures,
1228 param_structs, ... we count their length first and write it. These
1229 eases the reading, and enables an extra verification on the number
1230 of actually read items. */
1232 /* Write our typedefs. */
1233 void
1234 state_writer::write_state_typedefs (void)
1236 int nbtypedefs = pair_list_length (typedefs);
1237 int nbpairs = 0;
1238 begin_s_expr ("typedefs");
1239 fprintf (state_file, "%d", nbtypedefs);
1240 nbpairs = write_state_pair_list (typedefs);
1241 gcc_assert (nbpairs == nbtypedefs);
1242 end_s_expr ();
1243 if (verbosity_level >= 2)
1244 printf ("%s wrote %d typedefs\n", progname, nbtypedefs);
1247 /* Write our structures. */
1248 void
1249 state_writer::write_state_structures (void)
1251 int nbstruct = 0;
1252 type_p current;
1254 for (current = structures; current != NULL; current = current->next)
1255 nbstruct++;
1257 begin_s_expr ("structures");
1258 fprintf (state_file, "%d", nbstruct);
1260 for (current = structures; current != NULL; current = current->next)
1262 write_new_line ();
1263 write_state_type (current);
1266 /* Terminate the "structures" s-expression. */
1267 end_s_expr ();
1268 if (verbosity_level >= 2)
1269 printf ("%s wrote %d structures in state\n", progname, nbstruct);
1272 /* Write our param_struct-s. */
1273 void
1274 state_writer::write_state_param_structs (void)
1276 int nbparamstruct = 0;
1277 type_p current;
1279 for (current = param_structs; current != NULL; current = current->next)
1280 nbparamstruct++;
1282 begin_s_expr ("param_structs");
1283 fprintf (state_file, "%d", nbparamstruct);
1285 for (current = param_structs; current != NULL; current = current->next)
1286 write_state_type (current);
1288 end_s_expr ();
1291 /* Write our variables. */
1292 void
1293 state_writer::write_state_variables (void)
1295 int nbvars = pair_list_length (variables);
1296 int nbpairs = 0;
1297 begin_s_expr ("variables");
1298 fprintf (state_file, "%d", nbvars);
1299 nbpairs = write_state_pair_list (variables);
1300 gcc_assert (nbpairs == nbvars);
1301 end_s_expr ();
1302 if (verbosity_level >= 2)
1303 printf ("%s wrote %d variables.\n", progname, nbvars);
1306 /* Write the source directory. File locations within the source
1307 directory have been written specifically. */
1308 void
1309 state_writer::write_state_srcdir (void)
1311 begin_s_expr ("srcdir");
1312 write_state_a_string (srcdir);
1313 end_s_expr ();
1316 /* Count and write the list of our files. */
1317 void
1318 state_writer::write_state_files_list (void)
1320 int i = 0;
1321 /* Write the list of files with their lang_bitmap. */
1322 begin_s_expr ("fileslist");
1323 fprintf (state_file, "%d", (int) num_gt_files);
1324 for (i = 0; i < (int) num_gt_files; i++)
1326 const char *cursrcrelpath = NULL;
1327 const input_file *curfil = gt_files[i];
1328 /* Most of the files are inside $(srcdir) so it is worth to
1329 handle them specially. */
1330 cursrcrelpath = get_file_srcdir_relative_path (curfil);
1331 if (cursrcrelpath)
1333 begin_s_expr ("srcfile");
1334 fprintf (state_file, "%d ", get_lang_bitmap (curfil));
1335 write_state_a_string (cursrcrelpath);
1337 else
1339 begin_s_expr ("file");
1340 fprintf (state_file, "%d ", get_lang_bitmap (curfil));
1341 write_state_a_string (get_input_file_name (curfil));
1343 /* Terminate the inner s-expression (either "srcfile" or "file"). */
1344 end_s_expr ();
1346 /* Terminate the "fileslist" s-expression. */
1347 end_s_expr ();
1350 /* Write the list of GCC front-end languages. */
1351 void
1352 state_writer::write_state_languages (void)
1354 int i = 0;
1355 begin_s_expr ("languages");
1356 fprintf (state_file, "%d", (int) num_lang_dirs);
1357 for (i = 0; i < (int) num_lang_dirs; i++)
1359 /* Languages names are identifiers, we expect only letters or
1360 underscores or digits in them. In particular, C++ is not a
1361 valid language name, but cp is valid. */
1362 fprintf (state_file, " %s", lang_dir_names[i]);
1364 end_s_expr ();
1367 /* Write the trailer. */
1368 static void
1369 write_state_trailer (void)
1371 /* This test should probably catch IO errors like disk full... */
1372 if (fputs ("\n(!endfile)\n", state_file) == EOF)
1373 fatal ("failed to write state trailer [%s]", xstrerror (errno));
1376 /* The write_state routine is the only writing routine called by main
1377 in gengtype.c. To avoid messing the state if gengtype is
1378 interrupted or aborted, we write a temporary file and rename it
1379 after having written it in totality. */
1380 void
1381 write_state (const char *state_path)
1383 long statelen = 0;
1384 time_t now = 0;
1385 char *temp_state_path = NULL;
1386 char tempsuffix[40];
1387 time (&now);
1389 /* We write a unique temporary file which is renamed when complete
1390 * only. So even if gengtype is interrupted, the written state file
1391 * won't be partially written, since the temporary file is not yet
1392 * renamed in that case. */
1393 memset (tempsuffix, 0, sizeof (tempsuffix));
1394 snprintf (tempsuffix, sizeof (tempsuffix) - 1, "-%ld-%d.tmp", (long) now,
1395 (int) getpid ());
1396 temp_state_path = concat (state_path, tempsuffix, NULL);
1397 state_file = fopen (temp_state_path, "w");
1398 if (state_file == NULL)
1399 fatal ("Failed to open file %s for writing state: %s",
1400 temp_state_path, xstrerror (errno));
1401 if (verbosity_level >= 3)
1402 printf ("%s writing state file %s temporarily in %s\n",
1403 progname, state_path, temp_state_path);
1404 /* This is the first line of the state. Perhaps the file utility
1405 could know about that, so don't change it often. */
1406 fprintf (state_file, ";;;;@@@@ GCC gengtype state\n");
1407 /* Output a few comments for humans. */
1408 fprintf (state_file,
1409 ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n");
1410 fprintf (state_file,
1411 ";;; The format of this file is tied to a particular version of GCC.\n");
1412 fprintf (state_file,
1413 ";;; Don't parse this file wihout knowing GCC gengtype internals.\n");
1414 fprintf (state_file,
1415 ";;; This file should be parsed by the same %s which wrote it.\n",
1416 progname);
1418 state_writer sw;
1420 /* The first non-comment significant line gives the version string. */
1421 sw.write_state_version (version_string);
1422 sw.write_state_srcdir ();
1423 sw.write_state_languages ();
1424 sw.write_state_files_list ();
1425 sw.write_state_structures ();
1426 sw.write_state_typedefs ();
1427 sw.write_state_param_structs ();
1428 sw.write_state_variables ();
1429 write_state_trailer ();
1430 statelen = ftell (state_file);
1431 if (ferror (state_file))
1432 fatal ("output error when writing state file %s [%s]",
1433 temp_state_path, xstrerror (errno));
1434 if (fclose (state_file))
1435 fatal ("failed to close state file %s [%s]",
1436 temp_state_path, xstrerror (errno));
1437 if (rename (temp_state_path, state_path))
1438 fatal ("failed to rename %s to state file %s [%s]", temp_state_path,
1439 state_path, xstrerror (errno));
1440 free (temp_state_path);
1442 if (verbosity_level >= 1)
1443 printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n",
1444 progname, state_path, statelen, sw.m_state_written_type_count);
1448 /** End of writing routines! The corresponding reading routines follow. **/
1452 /* Forward declarations, since some read_state_* functions are
1453 recursive! */
1454 static void read_state_fileloc (struct fileloc *line);
1455 static void read_state_options (options_p *opt);
1456 static void read_state_type (type_p *current);
1457 static void read_state_pair (pair_p *pair);
1458 /* Return the number of pairs actually read. */
1459 static int read_state_pair_list (pair_p *list);
1460 static void read_state_fields (pair_p *fields);
1461 static void read_state_common_type_content (type_p current);
1466 /* Record into the state_seen_types hash-table a type which we are
1467 reading, to enable recursive or circular references to it. */
1468 static void
1469 record_type (type_p type)
1471 PTR *slot;
1473 slot = htab_find_slot (state_seen_types, type, INSERT);
1474 gcc_assert (slot);
1476 *slot = type;
1479 /* Read an already seen type. */
1480 static void
1481 read_state_already_seen_type (type_p *type)
1483 struct state_token_st *t0 = peek_state_token (0);
1485 if (state_token_kind (t0) == STOK_INTEGER)
1487 PTR *slot = NULL;
1488 struct type loctype = { TYPE_SCALAR, 0, 0, 0, GC_UNUSED, {0} };
1490 loctype.state_number = t0->stok_un.stok_num;
1491 slot = htab_find_slot (state_seen_types, &loctype, NO_INSERT);
1492 if (slot == NULL)
1494 fatal_reading_state (t0, "Unknown type");
1497 next_state_tokens (1);
1498 *type = (type_p) *slot;
1500 else
1502 fatal_reading_state (t0, "Bad seen type");
1507 /* Read the scalar_nonchar type. */
1508 static void
1509 read_state_scalar_nonchar_type (type_p *type)
1511 *type = &scalar_nonchar;
1512 read_state_common_type_content (*type);
1516 /* Read the scalar_char type. */
1517 static void
1518 read_state_scalar_char_type (type_p *type)
1520 *type = &scalar_char;
1521 read_state_common_type_content (*type);
1524 /* Read the string_type. */
1525 static void
1526 read_state_string_type (type_p *type)
1528 *type = &string_type;
1529 read_state_common_type_content (*type);
1533 /* Read a lang_bitmap representing a set of GCC front-end languages. */
1534 static void
1535 read_state_lang_bitmap (lang_bitmap *bitmap)
1537 struct state_token_st *t;
1539 t = peek_state_token (0);
1540 if (state_token_kind (t) == STOK_INTEGER)
1542 *bitmap = t->stok_un.stok_num;
1543 next_state_tokens (1);
1545 else
1547 fatal_reading_state (t, "Bad syntax for bitmap");
1552 /* Read an undefined type. */
1553 static void
1554 read_state_undefined_type (type_p type)
1556 struct state_token_st *t0;
1558 type->kind = TYPE_UNDEFINED;
1559 read_state_common_type_content (type);
1560 t0 = peek_state_token (0);
1561 if (state_token_kind (t0) == STOK_STRING)
1563 if (state_token_is_name (t0, "nil"))
1565 type->u.s.tag = NULL;
1566 DBGPRINTF ("read anonymous undefined type @%p #%d",
1567 (void *) type, type->state_number);
1569 else
1571 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1572 DBGPRINTF ("read undefined type @%p #%d '%s'",
1573 (void *) type, type->state_number, type->u.s.tag);
1576 next_state_tokens (1);
1577 read_state_fileloc (&(type->u.s.line));
1579 else
1581 fatal_reading_state (t0, "Bad tag in undefined type");
1586 /* Read a GTY-ed struct type. */
1587 static void
1588 read_state_struct_type (type_p type)
1590 struct state_token_st *t0;
1592 type->kind = TYPE_STRUCT;
1593 read_state_common_type_content (type);
1594 t0 = peek_state_token (0);
1595 if (state_token_kind (t0) == STOK_STRING)
1597 if (state_token_is_name (t0, "nil"))
1599 type->u.s.tag = NULL;
1600 DBGPRINTF ("read anonymous struct type @%p #%d",
1601 (void *) type, type->state_number);
1603 else
1605 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1606 DBGPRINTF ("read struct type @%p #%d '%s'",
1607 (void *) type, type->state_number, type->u.s.tag);
1610 next_state_tokens (1);
1611 read_state_fileloc (&(type->u.s.line));
1612 read_state_fields (&(type->u.s.fields));
1613 read_state_options (&(type->u.s.opt));
1614 read_state_lang_bitmap (&(type->u.s.bitmap));
1615 read_state_type (&(type->u.s.lang_struct));
1616 read_state_type (&(type->u.s.base_class));
1617 if (type->u.s.base_class)
1618 add_subclass (type->u.s.base_class, type);
1620 else
1622 fatal_reading_state (t0, "Bad tag in struct type");
1627 /* Read a GTY-ed user-provided struct TYPE. */
1629 static void
1630 read_state_user_struct_type (type_p type)
1632 struct state_token_st *t0;
1634 type->kind = TYPE_USER_STRUCT;
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 struct 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 struct type @%p #%d '%s'",
1649 (void *) type, type->state_number, type->u.s.tag);
1652 next_state_tokens (1);
1653 read_state_fileloc (&(type->u.s.line));
1654 read_state_fields (&(type->u.s.fields));
1656 else
1658 fatal_reading_state (t0, "Bad tag in user-struct type");
1663 /* Read a GTY-ed union type. */
1664 static void
1665 read_state_union_type (type_p type)
1667 struct state_token_st *t0;
1669 type->kind = TYPE_UNION;
1670 read_state_common_type_content (type);
1671 t0 = peek_state_token (0);
1672 if (state_token_kind (t0) == STOK_STRING)
1674 if (state_token_is_name (t0, "nil"))
1676 type->u.s.tag = NULL;
1677 DBGPRINTF ("read anonymous union type @%p #%d",
1678 (void *) type, type->state_number);
1680 else
1682 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1683 DBGPRINTF ("read union type @%p #%d '%s'",
1684 (void *) type, type->state_number, type->u.s.tag);
1686 next_state_tokens (1);
1687 read_state_fileloc (&(type->u.s.line));
1688 read_state_fields (&(type->u.s.fields));
1689 read_state_options (&(type->u.s.opt));
1690 read_state_lang_bitmap (&(type->u.s.bitmap));
1691 read_state_type (&(type->u.s.lang_struct));
1693 else
1694 fatal_reading_state (t0, "Bad tag in union type");
1698 /* Read a GTY-ed pointer type. */
1699 static void
1700 read_state_pointer_type (type_p type)
1702 type->kind = TYPE_POINTER;
1703 read_state_common_type_content (type);
1704 DBGPRINTF ("read pointer type @%p #%d", (void *) type, type->state_number);
1705 read_state_type (&(type->u.p));
1709 /* Read a GTY-ed array type. */
1710 static void
1711 read_state_array_type (type_p type)
1713 struct state_token_st *t0;
1715 type->kind = TYPE_ARRAY;
1716 read_state_common_type_content (type);
1717 t0 = peek_state_token (0);
1718 if (state_token_kind (t0) == STOK_STRING)
1720 type->u.a.len = xstrdup (t0->stok_un.stok_string);
1721 DBGPRINTF ("read array type @%p #%d length '%s'",
1722 (void *) type, type->state_number, type->u.a.len);
1723 next_state_tokens (1);
1726 else if (state_token_is_name (t0, "nil"))
1728 type->u.a.len = NULL;
1729 DBGPRINTF ("read array type @%p #%d without length",
1730 (void *) type, type->state_number);
1731 next_state_tokens (1);
1734 else
1735 fatal_reading_state (t0, "Bad array name type");
1736 read_state_type (&(type->u.a.p));
1741 /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC
1742 front-end languages. This is a tricky function and it was painful
1743 to debug. Change it with extreme care. See also
1744 write_state_lang_struct_type. */
1745 static void
1746 read_state_lang_struct_type (type_p type)
1748 struct state_token_st *t0 = NULL;
1749 struct state_token_st *t1 = NULL;
1750 struct state_token_st *t2 = NULL;
1752 type->kind = TYPE_LANG_STRUCT;
1753 read_state_common_type_content (type);
1754 t0 = peek_state_token (0);
1755 if (state_token_kind (t0) == STOK_STRING)
1757 if (state_token_is_name (t0, "nil"))
1759 DBGPRINTF ("read anonymous lang_struct type @%p #%d",
1760 (void *) type, type->state_number);
1761 type->u.s.tag = NULL;
1763 else
1765 type->u.s.tag = xstrdup (t0->stok_un.stok_string);
1766 DBGPRINTF ("read lang_struct type @%p #%d '%s'",
1767 (void *) type, type->state_number, type->u.s.tag);
1769 next_state_tokens (1);
1771 else
1772 fatal_reading_state (t0, "Bad tag in lang struct type");
1773 read_state_fileloc (&(type->u.s.line));
1774 read_state_fields (&(type->u.s.fields));
1775 read_state_options (&(type->u.s.opt));
1776 read_state_lang_bitmap (&(type->u.s.bitmap));
1777 /* Within lang_struct-ures, the lang_struct field is a linked list
1778 of homonymous types! */
1779 t0 = peek_state_token (0);
1780 t1 = peek_state_token (1);
1781 t2 = peek_state_token (2);
1782 /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */
1783 if (state_token_kind (t0) == STOK_LEFTPAR
1784 && state_token_is_name (t1, "!homotypes")
1785 && state_token_kind (t2) == STOK_INTEGER)
1787 type_p *prevty = &type->u.s.lang_struct;
1788 int nbhomotype = t2->stok_un.stok_num;
1789 int i = 0;
1790 t0 = t1 = t2 = NULL;
1791 next_state_tokens (3);
1792 for (i = 0; i < nbhomotype; i++)
1794 read_state_type (prevty);
1795 t0 = peek_state_token (0);
1796 if (*prevty)
1797 prevty = &(*prevty)->next;
1798 else
1799 fatal_reading_state (t0,
1800 "expecting type in homotype list for lang_struct");
1802 if (state_token_kind (t0) != STOK_RIGHTPAR)
1803 fatal_reading_state (t0,
1804 "expecting ) in homotype list for lang_struct");
1805 next_state_tokens (1);
1807 else
1808 fatal_reading_state (t0, "expecting !homotypes for lang_struct");
1812 /* Read a param_struct type for GTY parametrized structures. */
1813 static void
1814 read_state_param_struct_type (type_p type)
1816 int i;
1817 struct state_token_st *t0;
1819 type->kind = TYPE_PARAM_STRUCT;
1820 read_state_common_type_content (type);
1821 DBGPRINTF ("read param_struct type @%p #%d",
1822 (void *) type, type->state_number);
1823 read_state_type (&(type->u.param_struct.stru));
1825 for (i = 0; i < NUM_PARAM; i++)
1827 t0 = peek_state_token (0);
1828 if (state_token_is_name (t0, "nil"))
1830 type->u.param_struct.param[i] = NULL;
1831 next_state_tokens (1);
1833 else
1834 read_state_type (&(type->u.param_struct.param[i]));
1836 read_state_fileloc (&(type->u.param_struct.line));
1840 /* Read the gc used information. */
1841 static void
1842 read_state_gc_used (enum gc_used_enum *pgus)
1844 struct state_token_st *t0 = peek_state_token (0);
1845 if (state_token_is_name (t0, "gc_unused"))
1846 *pgus = GC_UNUSED;
1847 else if (state_token_is_name (t0, "gc_used"))
1848 *pgus = GC_USED;
1849 else if (state_token_is_name (t0, "gc_maybe_pointed_to"))
1850 *pgus = GC_MAYBE_POINTED_TO;
1851 else if (state_token_is_name (t0, "gc_pointed_to"))
1852 *pgus = GC_POINTED_TO;
1853 else
1854 fatal_reading_state (t0, "invalid gc_used information");
1855 next_state_tokens (1);
1859 /* Utility function to read the common content of types. */
1860 static void
1861 read_state_common_type_content (type_p current)
1863 struct state_token_st *t0 = peek_state_token (0);
1865 if (state_token_kind (t0) == STOK_INTEGER)
1867 current->state_number = t0->stok_un.stok_num;
1868 next_state_tokens (1);
1869 record_type (current);
1871 else
1872 fatal_reading_state_printf (t0,
1873 "Expected integer for state_number line %d",
1874 state_line);
1875 /* We don't read the next field of the type. */
1876 read_state_type (&current->pointer_to);
1877 read_state_gc_used (&current->gc_used);
1881 /* Read a GTY-ed type. */
1882 void
1883 read_state_type (type_p *current)
1885 struct state_token_st *t0 = peek_state_token (0);
1886 struct state_token_st *t1 = peek_state_token (1);
1888 if (state_token_kind (t0) == STOK_LEFTPAR &&
1889 state_token_is_name (t1, "!type"))
1891 next_state_tokens (2);
1892 t0 = peek_state_token (0);
1893 if (state_token_is_name (t0, "already_seen"))
1895 next_state_tokens (1);
1896 read_state_already_seen_type (current);
1898 else
1900 t0 = peek_state_token (0);
1902 if (state_token_is_name (t0, "scalar_nonchar"))
1904 next_state_tokens (1);
1905 read_state_scalar_nonchar_type (current);
1907 else if (state_token_is_name (t0, "scalar_char"))
1909 next_state_tokens (1);
1910 read_state_scalar_char_type (current);
1912 else if (state_token_is_name (t0, "string"))
1914 next_state_tokens (1);
1915 read_state_string_type (current);
1917 else if (state_token_is_name (t0, "undefined"))
1919 *current = XCNEW (struct type);
1920 next_state_tokens (1);
1921 read_state_undefined_type (*current);
1923 else if (state_token_is_name (t0, "struct"))
1925 *current = XCNEW (struct type);
1926 next_state_tokens (1);
1927 read_state_struct_type (*current);
1929 else if (state_token_is_name (t0, "union"))
1931 *current = XCNEW (struct type);
1932 next_state_tokens (1);
1933 read_state_union_type (*current);
1935 else if (state_token_is_name (t0, "lang_struct"))
1937 *current = XCNEW (struct type);
1938 next_state_tokens (1);
1939 read_state_lang_struct_type (*current);
1941 else if (state_token_is_name (t0, "param_struct"))
1943 *current = XCNEW (struct type);
1944 next_state_tokens (1);
1945 read_state_param_struct_type (*current);
1947 else if (state_token_is_name (t0, "pointer"))
1949 *current = XCNEW (struct type);
1950 next_state_tokens (1);
1951 read_state_pointer_type (*current);
1953 else if (state_token_is_name (t0, "array"))
1955 *current = XCNEW (struct type);
1956 next_state_tokens (1);
1957 read_state_array_type (*current);
1959 else if (state_token_is_name (t0, "user_struct"))
1961 *current = XCNEW (struct type);
1962 next_state_tokens (1);
1963 read_state_user_struct_type (*current);
1965 else
1966 fatal_reading_state (t0, "bad type in (!type");
1968 t0 = peek_state_token (0);
1969 if (state_token_kind (t0) != STOK_RIGHTPAR)
1970 fatal_reading_state (t0, "missing ) in type");
1971 next_state_tokens (1);
1973 else if (state_token_is_name (t0, "nil"))
1975 next_state_tokens (1);
1976 *current = NULL;
1978 else
1979 fatal_reading_state (t0, "bad type syntax");
1983 /* Read a file location. Files within the source directory are dealt
1984 with specifically. */
1985 void
1986 read_state_fileloc (struct fileloc *floc)
1988 bool issrcfile = false;
1989 struct state_token_st *t0 = peek_state_token (0);
1990 struct state_token_st *t1 = peek_state_token (1);
1992 gcc_assert (floc != NULL);
1993 gcc_assert (srcdir != NULL);
1995 if (state_token_kind (t0) == STOK_LEFTPAR &&
1996 (state_token_is_name (t1, "!fileloc")
1997 || (issrcfile = state_token_is_name (t1, "!srcfileloc"))))
1999 next_state_tokens (2);
2000 t0 = peek_state_token (0);
2001 t1 = peek_state_token (1);
2002 if (state_token_kind (t0) == STOK_STRING &&
2003 state_token_kind (t1) == STOK_INTEGER)
2005 char *path = t0->stok_un.stok_string;
2006 if (issrcfile)
2008 static const char dirsepstr[2] = { DIR_SEPARATOR, (char) 0 };
2009 char *fullpath = concat (srcdir, dirsepstr, path, NULL);
2010 floc->file = input_file_by_name (fullpath);
2011 free (fullpath);
2013 else
2014 floc->file = input_file_by_name (path);
2015 floc->line = t1->stok_un.stok_num;
2016 next_state_tokens (2);
2018 else
2019 fatal_reading_state (t0,
2020 "Bad fileloc syntax, expected path string and line");
2021 t0 = peek_state_token (0);
2022 if (state_token_kind (t0) != STOK_RIGHTPAR)
2023 fatal_reading_state (t0, "Bad fileloc syntax, expected )");
2024 next_state_tokens (1);
2026 else if (state_token_is_name (t0, "nil"))
2028 next_state_tokens (1);
2029 floc->file = NULL;
2030 floc->line = 0;
2032 else
2033 fatal_reading_state (t0, "Bad fileloc syntax");
2037 /* Read the fields of a GTY-ed type. */
2038 void
2039 read_state_fields (pair_p *fields)
2041 pair_p tmp = NULL;
2042 struct state_token_st *t0 = peek_state_token (0);
2043 struct state_token_st *t1 = peek_state_token (1);
2044 struct state_token_st *t2 = peek_state_token (2);
2046 if (state_token_kind (t0) == STOK_LEFTPAR
2047 && state_token_is_name (t1, "!fields")
2048 && state_token_kind (t2) == STOK_INTEGER)
2050 int nbfields = t2->stok_un.stok_num;
2051 int nbpairs = 0;
2052 next_state_tokens (3);
2053 nbpairs = read_state_pair_list (&tmp);
2054 t0 = peek_state_token (0);
2055 if (nbpairs != nbfields)
2056 fatal_reading_state_printf
2057 (t0,
2058 "Mismatched fields number, expected %d got %d", nbpairs, nbfields);
2059 if (state_token_kind (t0) == STOK_RIGHTPAR)
2060 next_state_tokens (1);
2061 else
2062 fatal_reading_state (t0, "Bad fields expecting )");
2065 *fields = tmp;
2069 /* Read a string option. */
2070 static void
2071 read_state_string_option (options_p opt)
2073 struct state_token_st *t0 = peek_state_token (0);
2074 opt->kind = OPTION_STRING;
2075 if (state_token_kind (t0) == STOK_STRING)
2077 opt->info.string = xstrdup (t0->stok_un.stok_string);
2078 next_state_tokens (1);
2080 else if (state_token_is_name (t0, "nil"))
2082 opt->info.string = NULL;
2083 next_state_tokens (1);
2085 else
2086 fatal_reading_state (t0, "Missing name in string option");
2090 /* Read a type option. */
2091 static void
2092 read_state_type_option (options_p opt)
2094 opt->kind = OPTION_TYPE;
2095 read_state_type (&(opt->info.type));
2099 /* Read a nested option. */
2100 static void
2101 read_state_nested_option (options_p opt)
2103 struct state_token_st *t0;
2105 opt->info.nested = XCNEW (struct nested_ptr_data);
2106 opt->kind = OPTION_NESTED;
2107 read_state_type (&(opt->info.nested->type));
2108 t0 = peek_state_token (0);
2109 if (state_token_kind (t0) == STOK_STRING)
2111 opt->info.nested->convert_from = xstrdup (t0->stok_un.stok_string);
2112 next_state_tokens (1);
2114 else if (state_token_is_name (t0, "nil"))
2116 opt->info.nested->convert_from = NULL;
2117 next_state_tokens (1);
2119 else
2120 fatal_reading_state (t0, "Bad nested convert_from option");
2122 t0 = peek_state_token (0);
2123 if (state_token_kind (t0) == STOK_STRING)
2125 opt->info.nested->convert_to = xstrdup (t0->stok_un.stok_string);
2126 next_state_tokens (1);
2128 else if (state_token_is_name (t0, "nil"))
2130 opt->info.nested->convert_to = NULL;
2131 next_state_tokens (1);
2133 else
2134 fatal_reading_state (t0, "Bad nested convert_from option");
2138 /* Read an GTY option. */
2139 static void
2140 read_state_option (options_p *opt)
2142 struct state_token_st *t0 = peek_state_token (0);
2143 struct state_token_st *t1 = peek_state_token (1);
2145 if (state_token_kind (t0) == STOK_LEFTPAR &&
2146 state_token_is_name (t1, "!option"))
2148 next_state_tokens (2);
2149 t0 = peek_state_token (0);
2150 if (state_token_kind (t0) == STOK_NAME)
2152 *opt = XCNEW (struct options);
2153 if (state_token_is_name (t0, "nil"))
2154 (*opt)->name = NULL;
2155 else
2156 (*opt)->name = t0->stok_un.stok_ident->stid_name;
2157 next_state_tokens (1);
2158 t0 = peek_state_token (0);
2159 if (state_token_kind (t0) == STOK_NAME)
2161 if (state_token_is_name (t0, "string"))
2163 next_state_tokens (1);
2164 read_state_string_option (*opt);
2166 else if (state_token_is_name (t0, "type"))
2168 next_state_tokens (1);
2169 read_state_type_option (*opt);
2171 else if (state_token_is_name (t0, "nested"))
2173 next_state_tokens (1);
2174 read_state_nested_option (*opt);
2176 else
2177 fatal_reading_state (t0, "Bad option type");
2178 t0 = peek_state_token (0);
2179 if (state_token_kind (t0) != STOK_RIGHTPAR)
2180 fatal_reading_state (t0, "Bad syntax in option, expecting )");
2182 next_state_tokens (1);
2184 else
2185 fatal_reading_state (t0, "Missing option type");
2187 else
2188 fatal_reading_state (t0, "Bad name for option");
2190 else
2191 fatal_reading_state (t0, "Bad option, waiting for )");
2194 /* Read a list of options. */
2195 void
2196 read_state_options (options_p *opt)
2198 options_p head = NULL;
2199 options_p previous = NULL;
2200 options_p current_option = NULL;
2201 struct state_token_st *t0 = peek_state_token (0);
2202 struct state_token_st *t1 = peek_state_token (1);
2204 if (state_token_kind (t0) == STOK_LEFTPAR &&
2205 state_token_is_name (t1, "!options"))
2207 next_state_tokens (2);
2208 t0 = peek_state_token (0);
2209 while (state_token_kind (t0) != STOK_RIGHTPAR)
2211 read_state_option (&current_option);
2212 if (head == NULL)
2214 head = current_option;
2215 previous = head;
2217 else
2219 previous->next = current_option;
2220 previous = current_option;
2222 t0 = peek_state_token (0);
2224 next_state_tokens (1);
2226 else if (state_token_is_name (t0, "nil"))
2228 next_state_tokens (1);
2230 else
2231 fatal_reading_state (t0, "Bad options syntax");
2233 *opt = head;
2237 /* Read a version, and check against the version of the gengtype. */
2238 static void
2239 read_state_version (const char *version_string)
2241 struct state_token_st *t0 = peek_state_token (0);
2242 struct state_token_st *t1 = peek_state_token (1);
2244 if (state_token_kind (t0) == STOK_LEFTPAR &&
2245 state_token_is_name (t1, "!version"))
2247 next_state_tokens (2);
2248 t0 = peek_state_token (0);
2249 t1 = peek_state_token (1);
2250 if (state_token_kind (t0) == STOK_STRING &&
2251 state_token_kind (t1) == STOK_RIGHTPAR)
2253 /* Check that the read version string is the same as current
2254 version. */
2255 if (strcmp (version_string, t0->stok_un.stok_string))
2256 fatal_reading_state_printf (t0,
2257 "version string mismatch; expecting %s but got %s",
2258 version_string,
2259 t0->stok_un.stok_string);
2260 next_state_tokens (2);
2262 else
2263 fatal_reading_state (t0, "Missing version or right parenthesis");
2265 else
2266 fatal_reading_state (t0, "Bad version syntax");
2270 /* Read a pair. */
2271 void
2272 read_state_pair (pair_p *current)
2274 struct state_token_st *t0 = peek_state_token (0);
2275 struct state_token_st *t1 = peek_state_token (1);
2276 if (state_token_kind (t0) == STOK_LEFTPAR &&
2277 state_token_is_name (t1, "!pair"))
2279 *current = XCNEW (struct pair);
2280 next_state_tokens (2);
2281 t0 = peek_state_token (0);
2282 if (state_token_kind (t0) == STOK_STRING)
2284 if (strcmp (t0->stok_un.stok_string, "nil") == 0)
2286 (*current)->name = NULL;
2288 else
2290 (*current)->name = xstrdup (t0->stok_un.stok_string);
2292 next_state_tokens (1);
2293 read_state_type (&((*current)->type));
2294 read_state_fileloc (&((*current)->line));
2295 read_state_options (&((*current)->opt));;
2296 t0 = peek_state_token (0);
2297 if (state_token_kind (t0) == STOK_RIGHTPAR)
2299 next_state_tokens (1);
2301 else
2303 fatal_reading_state (t0, "Bad syntax for pair, )");
2306 else
2308 fatal_reading_state (t0, "Bad name for pair");
2311 else if (state_token_kind (t0) == STOK_NAME &&
2312 state_token_is_name (t0, "nil"))
2314 next_state_tokens (1);
2315 *current = NULL;
2317 else
2318 fatal_reading_state_printf (t0, "Bad syntax for pair, (!pair %d",
2319 state_token->stok_kind);
2323 /* Return the number of pairs actually read. */
2325 read_state_pair_list (pair_p *list)
2327 int nbpair = 0;
2328 pair_p head = NULL;
2329 pair_p previous = NULL;
2330 pair_p tmp = NULL;
2331 struct state_token_st *t0 = peek_state_token (0);
2332 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2334 read_state_pair (&tmp);
2335 if (head == NULL)
2337 head = tmp;
2338 previous = head;
2340 else
2342 previous->next = tmp;
2343 previous = tmp;
2345 t0 = peek_state_token (0);
2346 nbpair++;
2349 /* don't consume the ); the caller will eat it. */
2350 *list = head;
2351 return nbpair;
2354 /* Read the typedefs. */
2355 static void
2356 read_state_typedefs (pair_p *typedefs)
2358 int nbtypedefs = 0;
2359 pair_p list = NULL;
2360 struct state_token_st *t0 = peek_state_token (0);
2361 struct state_token_st *t1 = peek_state_token (1);
2362 struct state_token_st *t2 = peek_state_token (2);
2364 if (state_token_kind (t0) == STOK_LEFTPAR
2365 && state_token_is_name (t1, "!typedefs")
2366 && state_token_kind (t2) == STOK_INTEGER)
2368 int nbpairs = 0;
2369 nbtypedefs = t2->stok_un.stok_num;
2370 next_state_tokens (3);
2371 nbpairs = read_state_pair_list (&list);
2372 t0 = peek_state_token (0);
2373 if (nbpairs != nbtypedefs)
2374 fatal_reading_state_printf
2375 (t0,
2376 "invalid number of typedefs, expected %d but got %d",
2377 nbtypedefs, nbpairs);
2378 if (state_token_kind (t0) == STOK_RIGHTPAR)
2379 next_state_tokens (1);
2380 else
2381 fatal_reading_state (t0, "Bad typedefs syntax )");
2383 else
2384 fatal_reading_state (t0, "Bad typedefs syntax (!typedefs");
2386 if (verbosity_level >= 2)
2387 printf ("%s read %d typedefs from state\n", progname, nbtypedefs);
2388 *typedefs = list;
2392 /* Read the structures. */
2393 static void
2394 read_state_structures (type_p *structures)
2396 type_p head = NULL;
2397 type_p previous = NULL;
2398 type_p tmp;
2399 int nbstruct = 0, countstruct = 0;
2400 struct state_token_st *t0 = peek_state_token (0);
2401 struct state_token_st *t1 = peek_state_token (1);
2402 struct state_token_st *t2 = peek_state_token (2);
2404 if (state_token_kind (t0) == STOK_LEFTPAR
2405 && state_token_is_name (t1, "!structures")
2406 && state_token_kind (t2) == STOK_INTEGER)
2408 nbstruct = t2->stok_un.stok_num;
2409 next_state_tokens (3);
2410 t0 = peek_state_token (0);
2411 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR)
2413 tmp = NULL;
2414 read_state_type (&tmp);
2415 countstruct++;
2416 if (head == NULL)
2418 head = tmp;
2419 previous = head;
2421 else
2423 previous->next = tmp;
2424 previous = tmp;
2426 t0 = peek_state_token (0);
2428 next_state_tokens (1);
2430 else
2431 fatal_reading_state (t0, "Bad structures syntax");
2432 if (countstruct != nbstruct)
2433 fatal_reading_state_printf (NULL_STATE_TOKEN,
2434 "expected %d structures but got %d",
2435 nbstruct, countstruct);
2436 if (verbosity_level >= 2)
2437 printf ("%s read %d structures from state\n", progname, nbstruct);
2438 *structures = head;
2442 /* Read the param_struct-s. */
2443 static void
2444 read_state_param_structs (type_p *param_structs)
2446 int nbparamstructs = 0;
2447 int countparamstructs = 0;
2448 type_p head = NULL;
2449 type_p previous = NULL;
2450 type_p tmp;
2451 struct state_token_st *t0 = peek_state_token (0);
2452 struct state_token_st *t1 = peek_state_token (1);
2453 struct state_token_st *t2 = peek_state_token (2);
2455 if (state_token_kind (t0) == STOK_LEFTPAR
2456 && state_token_is_name (t1, "!param_structs")
2457 && state_token_kind (t2) == STOK_INTEGER)
2459 nbparamstructs = t2->stok_un.stok_num;
2460 next_state_tokens (3);
2461 t0 = t1 = t2 = NULL;
2462 t0 = peek_state_token (0);
2463 while (state_token_kind (t0) != STOK_RIGHTPAR)
2465 tmp = NULL;
2466 read_state_type (&tmp);
2467 if (head == NULL)
2469 head = tmp;
2470 previous = head;
2472 else
2474 previous->next = tmp;
2475 previous = tmp;
2477 t0 = peek_state_token (0);
2478 countparamstructs++;
2480 next_state_tokens (1);
2482 else
2483 fatal_reading_state (t0, "Bad param_structs syntax");
2484 t0 = peek_state_token (0);
2485 if (countparamstructs != nbparamstructs)
2486 fatal_reading_state_printf
2487 (t0,
2488 "invalid number of param_structs expected %d got %d",
2489 nbparamstructs, countparamstructs);
2490 *param_structs = head;
2494 /* Read the variables. */
2495 static void
2496 read_state_variables (pair_p *variables)
2498 pair_p list = NULL;
2499 int nbvars = 0;
2500 struct state_token_st *t0 = peek_state_token (0);
2501 struct state_token_st *t1 = peek_state_token (1);
2502 struct state_token_st *t2 = peek_state_token (2);
2504 if (state_token_kind (t0) == STOK_LEFTPAR
2505 && state_token_is_name (t1, "!variables")
2506 && state_token_kind (t2) == STOK_INTEGER)
2508 int nbpairs = 0;
2509 nbvars = t2->stok_un.stok_num;
2510 next_state_tokens (3);
2511 nbpairs = read_state_pair_list (&list);
2512 t0 = peek_state_token (0);
2513 if (nbpairs != nbvars)
2514 fatal_reading_state_printf
2515 (t0, "Invalid number of variables, expected %d but got %d",
2516 nbvars, nbpairs);
2517 if (state_token_kind (t0) == STOK_RIGHTPAR)
2518 next_state_tokens (1);
2519 else
2520 fatal_reading_state (t0, "Waiting for ) in variables");
2522 else
2523 fatal_reading_state (t0, "Bad variables syntax");
2524 *variables = list;
2525 if (verbosity_level >= 2)
2526 printf ("%s read %d variables from state\n", progname, nbvars);
2530 /* Read the source directory. */
2531 static void
2532 read_state_srcdir (void)
2534 struct state_token_st *t0 = peek_state_token (0);
2535 struct state_token_st *t1 = peek_state_token (1);
2536 if (state_token_kind (t0) == STOK_LEFTPAR &&
2537 state_token_is_name (t1, "!srcdir"))
2539 next_state_tokens (2);
2540 t0 = peek_state_token (0);
2541 t1 = peek_state_token (1);
2542 if (state_token_kind (t0) == STOK_STRING &&
2543 state_token_kind (t1) == STOK_RIGHTPAR)
2545 srcdir = xstrdup (t0->stok_un.stok_string);
2546 srcdir_len = strlen (srcdir);
2547 next_state_tokens (2);
2548 return;
2552 fatal_reading_state (t0, "Bad srcdir in state_file");
2556 /* Read the sequence of GCC front-end languages. */
2557 static void
2558 read_state_languages (void)
2560 struct state_token_st *t0 = peek_state_token (0);
2561 struct state_token_st *t1 = peek_state_token (1);
2562 struct state_token_st *t2 = peek_state_token (2);
2563 if (state_token_kind (t0) == STOK_LEFTPAR
2564 && state_token_is_name (t1, "!languages")
2565 && state_token_kind (t2) == STOK_INTEGER)
2567 int i = 0;
2568 num_lang_dirs = t2->stok_un.stok_num;
2569 lang_dir_names = XCNEWVEC (const char *, num_lang_dirs);
2570 next_state_tokens (3);
2571 t0 = t1 = t2 = NULL;
2572 for (i = 0; i < (int) num_lang_dirs; i++)
2574 t0 = peek_state_token (0);
2575 if (state_token_kind (t0) != STOK_NAME)
2576 fatal_reading_state (t0, "expecting language name in state file");
2577 lang_dir_names[i] = t0->stok_un.stok_ident->stid_name;
2578 next_state_tokens (1);
2580 t0 = peek_state_token (0);
2581 if (state_token_kind (t0) != STOK_RIGHTPAR)
2582 fatal_reading_state (t0, "missing ) in languages list of state file");
2583 next_state_tokens (1);
2585 else
2586 fatal_reading_state (t0, "expecting languages list in state file");
2590 /* Read the sequence of files. */
2591 static void
2592 read_state_files_list (void)
2594 struct state_token_st *t0 = peek_state_token (0);
2595 struct state_token_st *t1 = peek_state_token (1);
2596 struct state_token_st *t2 = peek_state_token (2);
2598 if (state_token_kind (t0) == STOK_LEFTPAR
2599 && state_token_is_name (t1, "!fileslist")
2600 && state_token_kind (t2) == STOK_INTEGER)
2602 int i = 0;
2603 num_gt_files = t2->stok_un.stok_num;
2604 next_state_tokens (3);
2605 t0 = t1 = t2 = NULL;
2606 gt_files = XCNEWVEC (const input_file *, num_gt_files);
2607 for (i = 0; i < (int) num_gt_files; i++)
2609 bool issrcfile = FALSE;
2610 t0 = t1 = t2 = NULL;
2611 t0 = peek_state_token (0);
2612 t1 = peek_state_token (1);
2613 t2 = peek_state_token (2);
2614 if (state_token_kind (t0) == STOK_LEFTPAR
2615 && (state_token_is_name (t1, "!file")
2616 || (issrcfile = state_token_is_name (t1, "!srcfile")))
2617 && state_token_kind (t2) == STOK_INTEGER)
2619 lang_bitmap bmap = t2->stok_un.stok_num;
2620 next_state_tokens (3);
2621 t0 = t1 = t2 = NULL;
2622 t0 = peek_state_token (0);
2623 t1 = peek_state_token (1);
2624 if (state_token_kind (t0) == STOK_STRING
2625 && state_token_kind (t1) == STOK_RIGHTPAR)
2627 const char *fnam = t0->stok_un.stok_string;
2628 /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */
2629 input_file *curgt = NULL;
2630 if (issrcfile)
2632 static const char dirsepstr[2] =
2633 { DIR_SEPARATOR, (char) 0 };
2634 char *fullpath = concat (srcdir, dirsepstr, fnam, NULL);
2635 curgt = input_file_by_name (fullpath);
2636 free (fullpath);
2638 else
2639 curgt = input_file_by_name (fnam);
2640 set_lang_bitmap (curgt, bmap);
2641 gt_files[i] = curgt;
2642 next_state_tokens (2);
2644 else
2645 fatal_reading_state (t0,
2646 "bad file in !fileslist of state file");
2648 else
2649 fatal_reading_state (t0,
2650 "expecting file in !fileslist of state file");
2652 t0 = peek_state_token (0);
2653 if (state_token_kind (t0) != STOK_RIGHTPAR)
2654 fatal_reading_state (t0, "missing ) for !fileslist in state file");
2655 next_state_tokens (1);
2657 else
2658 fatal_reading_state (t0, "missing !fileslist in state file");
2662 /* Read the trailer. */
2663 static void
2664 read_state_trailer (void)
2666 struct state_token_st *t0 = peek_state_token (0);
2667 struct state_token_st *t1 = peek_state_token (1);
2668 struct state_token_st *t2 = peek_state_token (2);
2670 if (state_token_kind (t0) == STOK_LEFTPAR
2671 && state_token_is_name (t1, "!endfile")
2672 && state_token_kind (t2) == STOK_RIGHTPAR)
2673 next_state_tokens (3);
2674 else
2675 fatal_reading_state (t0, "missing !endfile in state file");
2679 /* Utility functions for the state_seen_types hash table. */
2680 static unsigned
2681 hash_type_number (const void *ty)
2683 const struct type *type = (const struct type *) ty;
2685 return type->state_number;
2688 static int
2689 equals_type_number (const void *ty1, const void *ty2)
2691 const struct type *type1 = (const struct type *) ty1;
2692 const struct type *type2 = (const struct type *) ty2;
2694 return type1->state_number == type2->state_number;
2697 static int
2698 string_eq (const void *a, const void *b)
2700 const char *a0 = (const char *)a;
2701 const char *b0 = (const char *)b;
2703 return (strcmp (a0, b0) == 0);
2707 /* The function reading the state, called by main from gengtype.c. */
2708 void
2709 read_state (const char *path)
2711 state_file = fopen (path, "r");
2712 if (state_file == NULL)
2713 fatal ("Failed to open state file %s for reading [%s]", path,
2714 xstrerror (errno));
2715 state_path = path;
2716 state_line = 1;
2718 if (verbosity_level >= 1)
2720 printf ("%s reading state file %s;", progname, state_path);
2721 if (verbosity_level >= 2)
2722 putchar ('\n');
2723 fflush (stdout);
2726 state_seen_types =
2727 htab_create (2017, hash_type_number, equals_type_number, NULL);
2728 state_ident_tab =
2729 htab_create (4027, htab_hash_string, string_eq, NULL);
2730 read_state_version (version_string);
2731 read_state_srcdir ();
2732 read_state_languages ();
2733 read_state_files_list ();
2734 read_state_structures (&structures);
2735 if (ferror (state_file))
2736 fatal_reading_state_printf
2737 (NULL_STATE_TOKEN, "input error while reading state [%s]",
2738 xstrerror (errno));
2739 read_state_typedefs (&typedefs);
2740 read_state_param_structs (&param_structs);
2741 read_state_variables (&variables);
2742 read_state_trailer ();
2744 if (verbosity_level >= 1)
2746 printf ("%s read %ld bytes.\n", progname, ftell (state_file));
2747 fflush (stdout);
2750 if (fclose (state_file))
2751 fatal ("failed to close read state file %s [%s]",
2752 path, xstrerror (errno));
2753 state_file = NULL;
2754 state_path = NULL;
2757 /* End of file gengtype-state.c. */