1 /* Gengtype persistent state serialization & de-serialization. 2 Useful for gengtype in plugin mode. 3 4 Copyright (C) 2010-2018 Free Software Foundation, Inc. 5 6 This file is part of GCC. 7 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. 12 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. 17 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/>. 21 22 Contributed by Jeremie Salvucci <jeremie.salvucci@free.fr> 23 and Basile Starynkevitch <basile@starynkevitch.net> 24 */ 25 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" 37 38 39 40 /* Gives the file location of a type, if any. */ 41 static inline struct fileloc* 42 type_lineloc (const_type_p ty) 43 { 44 if (!ty) 45 return NULL; 46 switch (ty->kind) 47 { 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 return NULL; 61 default: 62 gcc_unreachable (); 63 } 64 } 65 66 /* The state file has simplistic lispy lexical tokens. Its lexer gives 67 a linked list of struct state_token_st, through the peek_state_token 68 function. Lexical tokens are consumed with next_state_tokens. */ 69 70 71 /* The lexical kind of each lispy token. */ 72 enum state_token_en 73 { 74 STOK_NONE, /* Never used. */ 75 STOK_INTEGER, /* Integer token. */ 76 STOK_STRING, /* String token. */ 77 STOK_LEFTPAR, /* Left opening parenthesis. */ 78 STOK_RIGHTPAR, /* Right closing parenthesis. */ 79 STOK_NAME /* hash-consed name or identifier. */ 80 }; 81 82 83 /* Structure and hash-table used to share identifiers or names. */ 84 struct state_ident_st 85 { 86 /* TODO: We could improve the parser by reserving identifiers for 87 state keywords and adding a keyword number for them. That would 88 mean adding another field in this state_ident_st struct. */ 89 char stid_name[1]; /* actually bigger & null terminated */ 90 }; 91 static htab_t state_ident_tab; 92 93 94 /* The state_token_st structure is for lexical tokens in the read 95 state file. The stok_kind field discriminates the union. Tokens 96 are allocated by peek_state_token which calls read_a_state_token 97 which allocate them. Tokens are freed by calls to 98 next_state_tokens. Token are organized in a FIFO look-ahead queue 99 filled by peek_state_token. */ 100 struct state_token_st 101 { 102 enum state_token_en stok_kind; /* the lexical kind 103 discriminates the stok_un 104 union */ 105 int stok_line; /* the line number */ 106 int stok_col; /* the column number */ 107 const char *stok_file; /* the file path */ 108 struct state_token_st *stok_next; /* the next token in the 109 queue, when peeked */ 110 union /* discriminated by stok_kind! */ 111 { 112 int stok_num; /* when STOK_INTEGER */ 113 char stok_string[1]; /* when STOK_STRING, actual size is 114 bigger and null terminated */ 115 struct state_ident_st *stok_ident; /* when STOK_IDENT */ 116 void *stok_ptr; /* null otherwise */ 117 } 118 stok_un; 119 }; 120 121 122 123 124 #define NULL_STATE_TOKEN (struct state_token_st*)0 125 126 /* the state_token pointer contains the leftmost current token. The 127 tokens are organized in a linked queue, using stok_next, for token 128 look-ahead. */ 129 struct state_token_st *state_token = NULL_STATE_TOKEN; 130 131 /* Used by the reading lexer. */ 132 static FILE *state_file; 133 static const char *state_path = NULL; 134 static int state_line = 0; 135 static long state_bol = 0; /* offset of beginning of line */ 136 137 /* A class for writing out s-expressions, keeping track of newlines and 138 nested indentation. */ 139 class s_expr_writer 140 { 141 public: 142 s_expr_writer (); 143 144 void write_new_line (); 145 void write_any_indent (int leading_spaces); 146 147 void begin_s_expr (const char *tag); 148 void end_s_expr (); 149 150 private: 151 int m_indent_amount; 152 int m_had_recent_newline; 153 }; // class s_expr_writer 154 155 /* A class for writing out "gtype.state". */ 156 class state_writer : public s_expr_writer 157 { 158 public: 159 state_writer (); 160 161 private: 162 void write_state_fileloc (struct fileloc *floc); 163 void write_state_fields (pair_p fields); 164 void write_state_a_string (const char *s); 165 void write_state_string_option (options_p current); 166 void write_state_type_option (options_p current); 167 void write_state_nested_option (options_p current); 168 void write_state_option (options_p current); 169 void write_state_options (options_p opt); 170 void write_state_lang_bitmap (lang_bitmap bitmap); 171 void write_state_version (const char *version); 172 void write_state_scalar_type (type_p current); 173 void write_state_string_type (type_p current); 174 void write_state_undefined_type (type_p current); 175 void write_state_struct_union_type (type_p current, const char *kindstr); 176 void write_state_struct_type (type_p current); 177 void write_state_user_struct_type (type_p current); 178 void write_state_union_type (type_p current); 179 void write_state_lang_struct_type (type_p current); 180 void write_state_pointer_type (type_p current); 181 void write_state_array_type (type_p current); 182 void write_state_gc_used (enum gc_used_enum gus); 183 void write_state_common_type_content (type_p current); 184 void write_state_type (type_p current); 185 void write_state_pair (pair_p current); 186 int write_state_pair_list (pair_p list); 187 void write_state_typedefs (void); 188 void write_state_structures (void); 189 void write_state_variables (void); 190 void write_state_srcdir (void); 191 void write_state_files_list (void); 192 void write_state_languages (void); 193 194 friend void write_state (const char *state_path); 195 196 private: 197 /* Counter of written types. */ 198 int m_state_written_type_count; 199 }; // class state_writer 200 201 202 /* class s_expr_writer's trivial constructor. */ 203 s_expr_writer::s_expr_writer () 204 : m_indent_amount (0), 205 m_had_recent_newline (0) 206 { 207 } 208 209 /* Write a newline to the output file, merging adjacent newlines. */ 210 void 211 s_expr_writer::write_new_line (void) 212 { 213 /* Don't add a newline if we've just had one. */ 214 if (!m_had_recent_newline) 215 { 216 fprintf (state_file, "\n"); 217 m_had_recent_newline = 1; 218 } 219 } 220 221 /* If we've just had a newline, write the indentation amount, potentially 222 omitting some spaces. 223 224 LEADING_SPACES exists to support code that writes strings with leading 225 spaces (e.g " foo") which might occur within a line, or could be the first 226 thing on a line. By passing leading_spaces == 1, when such a string is the 227 first thing on a line, write_any_indent () swallows the successive 228 leading spaces into the indentation so that the "foo" begins at the expected 229 column. */ 230 void 231 s_expr_writer::write_any_indent (int leading_spaces) 232 { 233 int i; 234 int amount = m_indent_amount - leading_spaces; 235 if (m_had_recent_newline) 236 for (i = 0; i < amount; i++) 237 fprintf (state_file, " "); 238 m_had_recent_newline = 0; 239 } 240 241 /* Write the beginning of a new s-expresion e.g. "(!foo " 242 The writer automatically adds whitespace to show the hierarchical 243 structure of the expressions, so each one starts on a new line, 244 and any within it will be at an increased indentation level. */ 245 void 246 s_expr_writer::begin_s_expr (const char *tag) 247 { 248 write_new_line (); 249 write_any_indent (0); 250 fprintf (state_file, "(!%s ", tag); 251 m_indent_amount++; 252 } 253 254 /* Write out the end of an s-expression: any necssessary indentation, 255 a closing parenthesis, and a new line. */ 256 void 257 s_expr_writer::end_s_expr (void) 258 { 259 m_indent_amount--; 260 write_any_indent (0); 261 fprintf (state_file, ")"); 262 write_new_line (); 263 } 264 265 266 /* class state_writer's trivial constructor. */ 267 state_writer::state_writer () 268 : s_expr_writer (), 269 m_state_written_type_count (0) 270 { 271 } 272 273 274 /* Fatal error messages when reading the state. They are extremely 275 unlikely, and only appear when this gengtype-state.c file is buggy, 276 or when reading a gengtype state which was not generated by the 277 same version of gengtype or GCC. */ 278 279 280 /* Fatal message while reading state. */ 281 static void 282 fatal_reading_state (struct state_token_st* tok, const char*msg) 283 { 284 if (tok) 285 fatal ("%s:%d:%d: Invalid state file; %s", 286 tok->stok_file, tok->stok_line, tok->stok_col, 287 msg); 288 else 289 fatal ("%s:%d: Invalid state file; %s", 290 state_path, state_line, msg); 291 } 292 293 294 /* Fatal printf-like message while reading state. This can't be a 295 function, because there is no way to pass a va_arg to a variant of 296 fatal. */ 297 #define fatal_reading_state_printf(Tok,Fmt,...) do { \ 298 struct state_token_st* badtok = Tok; \ 299 if (badtok) \ 300 fatal ("%s:%d:%d: Invalid state file; " Fmt, \ 301 badtok->stok_file, \ 302 badtok->stok_line, \ 303 badtok->stok_col, __VA_ARGS__); \ 304 else \ 305 fatal ("%s:%d: Invalid state file; " Fmt, \ 306 state_path, state_line, __VA_ARGS__); \ 307 } while (0) 308 309 310 /* Find or allocate an identifier in our name hash table. */ 311 static struct state_ident_st * 312 state_ident_by_name (const char *name, enum insert_option optins) 313 { 314 PTR *slot = NULL; 315 int namlen = 0; 316 struct state_ident_st *stid = NULL; 317 318 if (!name || !name[0]) 319 return NULL; 320 321 slot = htab_find_slot (state_ident_tab, name, optins); 322 if (!slot) 323 return NULL; 324 325 namlen = strlen (name); 326 stid = 327 (struct state_ident_st *) xmalloc (sizeof (struct state_ident_st) + 328 namlen); 329 memset (stid, 0, sizeof (struct state_ident_st) + namlen); 330 strcpy (stid->stid_name, name); 331 *slot = stid; 332 333 return stid; 334 } 335 336 /* Our token lexer is heavily inspired by MELT's lexer, and share some 337 code with the file gcc/melt-runtime.c of the GCC MELT branch! We 338 really want the gengtype state to be easily parsable by MELT. This 339 is a usual lispy lexing routine, dealing with spaces and comments, 340 numbers, parenthesis, names, strings. */ 341 static struct state_token_st * 342 read_a_state_token (void) 343 { 344 int c = 0; 345 long curoff = 0; 346 struct state_token_st *tk = NULL; 347 348 again: /* Read again, e.g. after a comment or spaces. */ 349 c = getc (state_file); 350 if (c == EOF) 351 return NULL; 352 353 /* Handle spaces, count lines. */ 354 if (c == '\n') 355 { 356 state_line++; 357 state_bol = curoff = ftell (state_file); 358 goto again; 359 }; 360 if (ISSPACE (c)) 361 goto again; 362 /* Skip comments starting with semi-colon. */ 363 if (c == ';') 364 { 365 do 366 { 367 c = getc (state_file); 368 } 369 while (c > 0 && c != '\n'); 370 if (c == '\n') 371 { 372 state_line++; 373 state_bol = curoff = ftell (state_file); 374 } 375 goto again; 376 }; 377 /* Read signed numbers. */ 378 if (ISDIGIT (c) || c == '-' || c == '+') 379 { /* number */ 380 int n = 0; 381 ungetc (c, state_file); 382 curoff = ftell (state_file); 383 if (fscanf (state_file, "%d", &n) <= 0) 384 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error in number"); 385 tk = XCNEW (struct state_token_st); 386 tk->stok_kind = STOK_INTEGER; 387 tk->stok_line = state_line; 388 tk->stok_col = curoff - state_bol; 389 tk->stok_file = state_path; 390 tk->stok_next = NULL; 391 tk->stok_un.stok_num = n; 392 393 return tk; 394 } 395 /* Read an opening left parenthesis. */ 396 else if (c == '(') 397 { 398 curoff = ftell (state_file); 399 tk = XCNEW (struct state_token_st); 400 tk->stok_kind = STOK_LEFTPAR; 401 tk->stok_line = state_line; 402 tk->stok_col = curoff - state_bol; 403 tk->stok_file = state_path; 404 tk->stok_next = NULL; 405 406 return tk; 407 } 408 /* Read an closing right parenthesis. */ 409 else if (c == ')') 410 { 411 curoff = ftell (state_file); 412 tk = XCNEW (struct state_token_st); 413 tk->stok_kind = STOK_RIGHTPAR; 414 tk->stok_line = state_line; 415 tk->stok_col = curoff - state_bol; 416 tk->stok_file = state_path; 417 tk->stok_next = NULL; 418 419 return tk; 420 } 421 /* Read identifiers, using an obstack. */ 422 else if (ISALPHA (c) || c == '_' || c == '$' || c == '!' || c == '#') 423 { 424 struct obstack id_obstack; 425 struct state_ident_st *sid = NULL; 426 char *ids = NULL; 427 obstack_init (&id_obstack); 428 curoff = ftell (state_file); 429 while (ISALNUM (c) || c == '_' || c == '$' || c == '!' || c == '#') 430 { 431 obstack_1grow (&id_obstack, c); 432 c = getc (state_file); 433 if (c < 0) 434 break; 435 }; 436 if (c >= 0) 437 ungetc (c, state_file); 438 obstack_1grow (&id_obstack, (char) 0); 439 ids = XOBFINISH (&id_obstack, char *); 440 sid = state_ident_by_name (ids, INSERT); 441 obstack_free (&id_obstack, NULL); 442 ids = NULL; 443 tk = XCNEW (struct state_token_st); 444 tk->stok_kind = STOK_NAME; 445 tk->stok_line = state_line; 446 tk->stok_col = curoff - state_bol; 447 tk->stok_file = state_path; 448 tk->stok_next = NULL; 449 tk->stok_un.stok_ident = sid; 450 451 return tk; 452 } 453 /* Read a string, dealing with escape sequences a la C! */ 454 else if (c == '"') 455 { 456 char *cstr = NULL; 457 int cslen = 0; 458 struct obstack bstring_obstack; 459 obstack_init (&bstring_obstack); 460 curoff = ftell (state_file); 461 while ((c = getc (state_file)) != '"' && c >= 0) 462 { 463 if (ISPRINT (c) && c != '\\') 464 obstack_1grow (&bstring_obstack, (char) c); 465 else if (ISSPACE (c) && c != '\n') 466 obstack_1grow (&bstring_obstack, (char) c); 467 else if (c == '\\') 468 { 469 c = getc (state_file); 470 switch (c) 471 { 472 case 'a': 473 obstack_1grow (&bstring_obstack, '\a'); 474 c = getc (state_file); 475 break; 476 case 'b': 477 obstack_1grow (&bstring_obstack, '\b'); 478 c = getc (state_file); 479 break; 480 case 't': 481 obstack_1grow (&bstring_obstack, '\t'); 482 c = getc (state_file); 483 break; 484 case 'n': 485 obstack_1grow (&bstring_obstack, '\n'); 486 c = getc (state_file); 487 break; 488 case 'v': 489 obstack_1grow (&bstring_obstack, '\v'); 490 c = getc (state_file); 491 break; 492 case 'f': 493 obstack_1grow (&bstring_obstack, '\f'); 494 c = getc (state_file); 495 break; 496 case 'r': 497 obstack_1grow (&bstring_obstack, '\r'); 498 c = getc (state_file); 499 break; 500 case '"': 501 obstack_1grow (&bstring_obstack, '\"'); 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 'x': 513 { 514 unsigned int cx = 0; 515 if (fscanf (state_file, "%02x", &cx) > 0 && cx > 0) 516 obstack_1grow (&bstring_obstack, cx); 517 else 518 fatal_reading_state 519 (NULL_STATE_TOKEN, 520 "Lexical error in string hex escape"); 521 c = getc (state_file); 522 break; 523 } 524 default: 525 fatal_reading_state 526 (NULL_STATE_TOKEN, 527 "Lexical error - unknown string escape"); 528 } 529 } 530 else 531 fatal_reading_state (NULL_STATE_TOKEN, "Lexical error..."); 532 }; 533 if (c != '"') 534 fatal_reading_state (NULL_STATE_TOKEN, "Unterminated string"); 535 obstack_1grow (&bstring_obstack, '\0'); 536 cstr = XOBFINISH (&bstring_obstack, char *); 537 cslen = strlen (cstr); 538 tk = (struct state_token_st *) 539 xcalloc (sizeof (struct state_token_st) + cslen, 1); 540 tk->stok_kind = STOK_STRING; 541 tk->stok_line = state_line; 542 tk->stok_col = curoff - state_bol; 543 tk->stok_file = state_path; 544 tk->stok_next = NULL; 545 strcpy (tk->stok_un.stok_string, cstr); 546 obstack_free (&bstring_obstack, NULL); 547 548 return tk; 549 } 550 /* Got an unexpected character. */ 551 fatal_reading_state_printf 552 (NULL_STATE_TOKEN, 553 "Lexical error at offset %ld - bad character \\%03o = '%c'", 554 ftell (state_file), c, c); 555 } 556 557 /* Used for lexical look-ahead. Retrieves the lexical token of rank 558 DEPTH, starting with 0 when reading the state file. Gives null on 559 end of file. */ 560 static struct state_token_st * 561 peek_state_token (int depth) 562 { 563 int remdepth = depth; 564 struct state_token_st **ptoken = &state_token; 565 struct state_token_st *tok = NULL; 566 567 while (remdepth >= 0) 568 { 569 if (*ptoken == NULL) 570 { 571 *ptoken = tok = read_a_state_token (); 572 if (tok == NULL) 573 return NULL; 574 } 575 tok = *ptoken; 576 ptoken = &((*ptoken)->stok_next); 577 remdepth--; 578 } 579 580 return tok; 581 } 582 583 /* Consume the next DEPTH tokens and free them. */ 584 static void 585 next_state_tokens (int depth) 586 { 587 struct state_token_st *n; 588 589 while (depth > 0) 590 { 591 if (state_token != NULL) 592 { 593 n = state_token->stok_next; 594 free (state_token); 595 state_token = n; 596 } 597 else 598 fatal_reading_state (NULL_STATE_TOKEN, "Tokens stack empty"); 599 600 depth--; 601 } 602 } 603 604 /* Safely retrieve the lexical kind of a token. */ 605 static inline enum state_token_en 606 state_token_kind (struct state_token_st *p) 607 { 608 if (p == NULL) 609 return STOK_NONE; 610 else 611 return p->stok_kind; 612 } 613 614 /* Test if a token is a given name i.e. an identifier. */ 615 static inline bool 616 state_token_is_name (struct state_token_st *p, const char *name) 617 { 618 if (p == NULL) 619 return false; 620 621 if (p->stok_kind != STOK_NAME) 622 return false; 623 624 return !strcmp (p->stok_un.stok_ident->stid_name, name); 625 } 626 627 628 /* Following routines are useful for serializing datas. 629 * 630 * We want to serialize : 631 * - typedefs list 632 * - structures list 633 * - variables list 634 * 635 * So, we have one routine for each kind of data. The main writing 636 * routine is write_state. The main reading routine is 637 * read_state. Most writing routines write_state_FOO have a 638 * corresponding reading routine read_state_FOO. Reading is done in a 639 * recursive descending way, and any read error is fatal. 640 */ 641 642 /* When reading the state, we need to remember the previously seen 643 types by their state_number, since GTY-ed types are usually 644 shared. */ 645 static htab_t state_seen_types; 646 647 /* Return the length of a linked list made of pairs. */ 648 static int pair_list_length (pair_p list); 649 650 /* Compute the length of a list of pairs, starting from the first 651 one. */ 652 static int 653 pair_list_length (pair_p list) 654 { 655 int nbpair = 0; 656 pair_p l = NULL; 657 for (l = list; l; l = l->next) 658 nbpair++; 659 return nbpair; 660 } 661 662 /* Write a file location. Files relative to $(srcdir) are quite 663 frequent and are handled specially. This ensures that two gengtype 664 state file-s produced by gengtype on the same GCC source tree are 665 very similar and can be reasonably compared with diff, even if the 666 two GCC source trees have different absolute paths. */ 667 void 668 state_writer::write_state_fileloc (struct fileloc *floc) 669 { 670 671 if (floc != NULL && floc->line > 0) 672 { 673 const char *srcrelpath = NULL; 674 gcc_assert (floc->file != NULL); 675 /* Most of the files are inside $(srcdir) so it is worth to 676 handle them specially. */ 677 srcrelpath = get_file_srcdir_relative_path (floc->file); 678 if (srcrelpath != NULL) 679 { 680 begin_s_expr ("srcfileloc"); 681 write_state_a_string (srcrelpath); 682 } 683 else 684 { 685 begin_s_expr ("fileloc"); 686 write_state_a_string (get_input_file_name (floc->file)); 687 } 688 fprintf (state_file, " %d", floc->line); 689 end_s_expr (); 690 } 691 else 692 fprintf (state_file, "nil "); 693 } 694 695 /* Write a list of fields. */ 696 void 697 state_writer::write_state_fields (pair_p fields) 698 { 699 int nbfields = pair_list_length (fields); 700 int nbpairs = 0; 701 begin_s_expr ("fields"); 702 fprintf (state_file, "%d ", nbfields); 703 nbpairs = write_state_pair_list (fields); 704 gcc_assert (nbpairs == nbfields); 705 end_s_expr (); 706 } 707 708 /* Write a null-terminated string in our lexical convention, very 709 similar to the convention of C. */ 710 void 711 state_writer::write_state_a_string (const char *s) 712 { 713 char c; 714 715 write_any_indent (1); 716 717 fputs (" \"", state_file); 718 for (; *s != 0; s++) 719 { 720 c = *s; 721 switch (c) 722 { 723 case '\a': 724 fputs ("\\a", state_file); 725 break; 726 case '\b': 727 fputs ("\\b", state_file); 728 break; 729 case '\t': 730 fputs ("\\t", state_file); 731 break; 732 case '\n': 733 fputs ("\\n", state_file); 734 break; 735 case '\v': 736 fputs ("\\v", state_file); 737 break; 738 case '\f': 739 fputs ("\\f", state_file); 740 break; 741 case '\r': 742 fputs ("\\r", state_file); 743 break; 744 case '\"': 745 fputs ("\\\"", state_file); 746 break; 747 case '\\': 748 fputs ("\\\\", state_file); 749 break; 750 default: 751 if (ISPRINT (c)) 752 putc (c, state_file); 753 else 754 fprintf (state_file, "\\x%02x", (unsigned) c); 755 } 756 } 757 fputs ("\"", state_file); 758 } 759 760 /* Our option-s have three kinds, each with its writer. */ 761 void 762 state_writer::write_state_string_option (options_p current) 763 { 764 write_any_indent (0); 765 fprintf (state_file, "string "); 766 if (current->info.string != NULL) 767 write_state_a_string (current->info.string); 768 else 769 fprintf (state_file, " nil "); 770 } 771 772 void 773 state_writer::write_state_type_option (options_p current) 774 { 775 write_any_indent (0); 776 fprintf (state_file, "type "); 777 write_state_type (current->info.type); 778 } 779 780 void 781 state_writer::write_state_nested_option (options_p current) 782 { 783 write_any_indent (0); 784 fprintf (state_file, "nested "); 785 write_state_type (current->info.nested->type); 786 if (current->info.nested->convert_from != NULL) 787 write_state_a_string (current->info.nested->convert_from); 788 else 789 { 790 write_any_indent (1); 791 fprintf (state_file, " nil "); 792 } 793 794 if (current->info.nested->convert_to != NULL) 795 write_state_a_string (current->info.nested->convert_to); 796 else 797 { 798 write_any_indent (1); 799 fprintf (state_file, " nil "); 800 } 801 } 802 803 void 804 state_writer::write_state_option (options_p current) 805 { 806 begin_s_expr ("option"); 807 808 write_any_indent (0); 809 if (current->name != NULL) 810 fprintf (state_file, "%s ", current->name); 811 else 812 fprintf (state_file, "nil "); 813 814 switch (current->kind) 815 { 816 case OPTION_STRING: 817 write_state_string_option (current); 818 break; 819 case OPTION_TYPE: 820 write_state_type_option (current); 821 break; 822 case OPTION_NESTED: 823 write_state_nested_option (current); 824 break; 825 default: 826 fatal ("Option tag unknown"); 827 } 828 829 /* Terminate the "option" s-expression. */ 830 end_s_expr (); 831 } 832 833 834 835 /* Write a list of GTY options. */ 836 void 837 state_writer::write_state_options (options_p opt) 838 { 839 options_p current; 840 841 if (opt == NULL) 842 { 843 write_any_indent (0); 844 fprintf (state_file, "nil "); 845 return; 846 } 847 848 begin_s_expr ("options"); 849 for (current = opt; current != NULL; current = current->next) 850 write_state_option (current); 851 end_s_expr (); 852 } 853 854 855 /* Write a bitmap representing a set of GCC front-end languages. */ 856 void 857 state_writer::write_state_lang_bitmap (lang_bitmap bitmap) 858 { 859 write_any_indent (0); 860 fprintf (state_file, "%d ", (int) bitmap); 861 } 862 863 /* Write version information. */ 864 void 865 state_writer::write_state_version (const char *version) 866 { 867 begin_s_expr ("version"); 868 write_state_a_string (version); 869 end_s_expr (); 870 } 871 872 /* Write a scalar type. We have only two of these. */ 873 void 874 state_writer::write_state_scalar_type (type_p current) 875 { 876 write_any_indent (0); 877 if (current == &scalar_nonchar) 878 fprintf (state_file, "scalar_nonchar "); 879 else if (current == &scalar_char) 880 fprintf (state_file, "scalar_char "); 881 else 882 fatal ("Unexpected type in write_state_scalar_type"); 883 884 write_state_common_type_content (current); 885 } 886 887 /* Write the string type. There is only one such thing! */ 888 void 889 state_writer::write_state_string_type (type_p current) 890 { 891 if (current == &string_type) 892 { 893 write_any_indent (0); 894 fprintf (state_file, "string "); 895 write_state_common_type_content (current); 896 } 897 else 898 fatal ("Unexpected type in write_state_string_type"); 899 } 900 901 /* Write an undefined type. */ 902 void 903 state_writer::write_state_undefined_type (type_p current) 904 { 905 DBGPRINTF ("undefined type @ %p #%d '%s'", (void *) current, 906 current->state_number, current->u.s.tag); 907 write_any_indent (0); 908 fprintf (state_file, "undefined "); 909 gcc_assert (current->gc_used == GC_UNUSED); 910 write_state_common_type_content (current); 911 if (current->u.s.tag != NULL) 912 write_state_a_string (current->u.s.tag); 913 else 914 { 915 write_any_indent (0); 916 fprintf (state_file, "nil"); 917 } 918 919 write_state_fileloc (type_lineloc (current)); 920 } 921 922 923 /* Common code to write structure like types. */ 924 void 925 state_writer::write_state_struct_union_type (type_p current, 926 const char *kindstr) 927 { 928 DBGPRINTF ("%s type @ %p #%d '%s'", kindstr, (void *) current, 929 current->state_number, current->u.s.tag); 930 write_any_indent (0); 931 fprintf (state_file, "%s ", kindstr); 932 write_state_common_type_content (current); 933 if (current->u.s.tag != NULL) 934 write_state_a_string (current->u.s.tag); 935 else 936 { 937 write_any_indent (0); 938 fprintf (state_file, "nil"); 939 } 940 941 write_state_fileloc (type_lineloc (current)); 942 write_state_fields (current->u.s.fields); 943 write_state_options (current->u.s.opt); 944 write_state_lang_bitmap (current->u.s.bitmap); 945 } 946 947 948 /* Write a GTY struct type. */ 949 void 950 state_writer::write_state_struct_type (type_p current) 951 { 952 write_state_struct_union_type (current, "struct"); 953 write_state_type (current->u.s.lang_struct); 954 write_state_type (current->u.s.base_class); 955 } 956 957 /* Write a GTY user-defined struct type. */ 958 void 959 state_writer::write_state_user_struct_type (type_p current) 960 { 961 DBGPRINTF ("user_struct type @ %p #%d '%s'", (void *) current, 962 current->state_number, current->u.s.tag); 963 write_any_indent (0); 964 fprintf (state_file, "user_struct "); 965 write_state_common_type_content (current); 966 if (current->u.s.tag != NULL) 967 write_state_a_string (current->u.s.tag); 968 else 969 { 970 write_any_indent (0); 971 fprintf (state_file, "nil"); 972 } 973 write_state_fileloc (type_lineloc (current)); 974 write_state_fields (current->u.s.fields); 975 } 976 977 /* write a GTY union type. */ 978 void 979 state_writer::write_state_union_type (type_p current) 980 { 981 write_state_struct_union_type (current, "union"); 982 write_state_type (current->u.s.lang_struct); 983 } 984 985 /* Write a lang_struct type. This is tricky and was painful to debug, 986 we deal with the next field specifically within their lang_struct 987 subfield, which points to a linked list of homonumous types. 988 Change this function with extreme care, see also 989 read_state_lang_struct_type. */ 990 void 991 state_writer::write_state_lang_struct_type (type_p current) 992 { 993 int nbhomontype = 0; 994 type_p hty = NULL; 995 const char *homoname = 0; 996 write_state_struct_union_type (current, "lang_struct"); 997 /* lang_struct-ures are particularly tricky, since their 998 u.s.lang_struct field gives a list of homonymous struct-s or 999 union-s! */ 1000 DBGPRINTF ("lang_struct @ %p #%d", (void *) current, current->state_number); 1001 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next) 1002 { 1003 nbhomontype++; 1004 DBGPRINTF ("homonymous #%d hty @ %p #%d '%s'", nbhomontype, 1005 (void *) hty, hty->state_number, hty->u.s.tag); 1006 /* Every member of the homonymous list should have the same tag. */ 1007 gcc_assert (union_or_struct_p (hty)); 1008 gcc_assert (hty->u.s.lang_struct == current); 1009 if (!homoname) 1010 homoname = hty->u.s.tag; 1011 gcc_assert (strcmp (homoname, hty->u.s.tag) == 0); 1012 } 1013 begin_s_expr ("homotypes"); 1014 fprintf (state_file, "%d", nbhomontype); 1015 for (hty = current->u.s.lang_struct; hty != NULL; hty = hty->next) 1016 write_state_type (hty); 1017 end_s_expr (); 1018 } 1019 1020 /* Write a pointer type. */ 1021 void 1022 state_writer::write_state_pointer_type (type_p current) 1023 { 1024 write_any_indent (0); 1025 fprintf (state_file, "pointer "); 1026 write_state_common_type_content (current); 1027 write_state_type (current->u.p); 1028 } 1029 1030 /* Write an array type. */ 1031 void 1032 state_writer::write_state_array_type (type_p current) 1033 { 1034 write_any_indent (0); 1035 fprintf (state_file, "array "); 1036 write_state_common_type_content (current); 1037 if (current->u.a.len != NULL) 1038 write_state_a_string (current->u.a.len); 1039 else 1040 { 1041 write_any_indent (1); 1042 fprintf (state_file, " nil"); 1043 } 1044 1045 write_any_indent (1); 1046 fprintf (state_file, " "); 1047 write_state_type (current->u.a.p); 1048 } 1049 1050 /* Write the gc_used information. */ 1051 void 1052 state_writer::write_state_gc_used (enum gc_used_enum gus) 1053 { 1054 write_any_indent (1); 1055 switch (gus) 1056 { 1057 case GC_UNUSED: 1058 fprintf (state_file, " gc_unused"); 1059 break; 1060 case GC_USED: 1061 fprintf (state_file, " gc_used"); 1062 break; 1063 case GC_MAYBE_POINTED_TO: 1064 fprintf (state_file, " gc_maybe_pointed_to"); 1065 break; 1066 case GC_POINTED_TO: 1067 fprintf (state_file, " gc_pointed_to"); 1068 break; 1069 default: 1070 gcc_unreachable (); 1071 } 1072 } 1073 1074 /* Utility routine to write the common content of all types. Notice 1075 that the next field is *not* written on purpose. */ 1076 void 1077 state_writer::write_state_common_type_content (type_p current) 1078 { 1079 write_any_indent (0); 1080 fprintf (state_file, "%d ", current->state_number); 1081 /* We do not write the next type, because list of types are 1082 explicitly written. However, lang_struct are special in that 1083 respect. See function write_state_lang_struct_type for more. */ 1084 write_state_type (current->pointer_to); 1085 write_state_gc_used (current->gc_used); 1086 } 1087 1088 1089 /* The important and recursive routine writing GTY types as understood 1090 by gengtype. Types which have a positive state_number have already 1091 been seen and written. */ 1092 void 1093 state_writer::write_state_type (type_p current) 1094 { 1095 write_any_indent (0); 1096 if (current == NULL) 1097 { 1098 fprintf (state_file, "nil "); 1099 return; 1100 } 1101 1102 begin_s_expr ("type"); 1103 1104 if (current->state_number > 0) 1105 { 1106 write_any_indent (0); 1107 fprintf (state_file, "already_seen %d", current->state_number); 1108 } 1109 else 1110 { 1111 m_state_written_type_count++; 1112 DBGPRINTF ("writing type #%d @%p old number %d", m_state_written_type_count, 1113 (void *) current, current->state_number); 1114 current->state_number = m_state_written_type_count; 1115 switch (current->kind) 1116 { 1117 case TYPE_NONE: 1118 gcc_unreachable (); 1119 case TYPE_UNDEFINED: 1120 write_state_undefined_type (current); 1121 break; 1122 case TYPE_STRUCT: 1123 write_state_struct_type (current); 1124 break; 1125 case TYPE_USER_STRUCT: 1126 write_state_user_struct_type (current); 1127 break; 1128 case TYPE_UNION: 1129 write_state_union_type (current); 1130 break; 1131 case TYPE_POINTER: 1132 write_state_pointer_type (current); 1133 break; 1134 case TYPE_ARRAY: 1135 write_state_array_type (current); 1136 break; 1137 case TYPE_LANG_STRUCT: 1138 write_state_lang_struct_type (current); 1139 break; 1140 case TYPE_SCALAR: 1141 write_state_scalar_type (current); 1142 break; 1143 case TYPE_STRING: 1144 write_state_string_type (current); 1145 break; 1146 } 1147 } 1148 1149 /* Terminate the "type" s-expression. */ 1150 end_s_expr (); 1151 } 1152 1153 1154 /* Write a pair. */ 1155 void 1156 state_writer::write_state_pair (pair_p current) 1157 { 1158 if (current == NULL) 1159 { 1160 write_any_indent (0); 1161 fprintf (state_file, "nil)"); 1162 return; 1163 } 1164 1165 begin_s_expr ("pair"); 1166 1167 if (current->name != NULL) 1168 write_state_a_string (current->name); 1169 else 1170 write_state_a_string ("nil"); 1171 1172 write_state_type (current->type); 1173 write_state_fileloc (&(current->line)); 1174 write_state_options (current->opt); 1175 1176 /* Terminate the "pair" s-expression. */ 1177 end_s_expr (); 1178 } 1179 1180 /* Write a pair list and return the number of pairs written. */ 1181 int 1182 state_writer::write_state_pair_list (pair_p list) 1183 { 1184 int nbpair = 0; 1185 pair_p current; 1186 1187 for (current = list; current != NULL; current = current->next) 1188 { 1189 write_state_pair (current); 1190 nbpair++; 1191 } 1192 return nbpair; 1193 1194 } 1195 1196 /* When writing imported linked lists, like typedefs, structures, ... we count 1197 their length first and write it. This eases the reading, and enables an 1198 extra verification on the number of actually read items. */ 1199 1200 /* Write our typedefs. */ 1201 void 1202 state_writer::write_state_typedefs (void) 1203 { 1204 int nbtypedefs = pair_list_length (typedefs); 1205 int nbpairs = 0; 1206 begin_s_expr ("typedefs"); 1207 fprintf (state_file, "%d", nbtypedefs); 1208 nbpairs = write_state_pair_list (typedefs); 1209 gcc_assert (nbpairs == nbtypedefs); 1210 end_s_expr (); 1211 if (verbosity_level >= 2) 1212 printf ("%s wrote %d typedefs\n", progname, nbtypedefs); 1213 } 1214 1215 /* Write our structures. */ 1216 void 1217 state_writer::write_state_structures (void) 1218 { 1219 int nbstruct = 0; 1220 type_p current; 1221 1222 for (current = structures; current != NULL; current = current->next) 1223 nbstruct++; 1224 1225 begin_s_expr ("structures"); 1226 fprintf (state_file, "%d", nbstruct); 1227 1228 for (current = structures; current != NULL; current = current->next) 1229 { 1230 write_new_line (); 1231 write_state_type (current); 1232 } 1233 1234 /* Terminate the "structures" s-expression. */ 1235 end_s_expr (); 1236 if (verbosity_level >= 2) 1237 printf ("%s wrote %d structures in state\n", progname, nbstruct); 1238 } 1239 1240 /* Write our variables. */ 1241 void 1242 state_writer::write_state_variables (void) 1243 { 1244 int nbvars = pair_list_length (variables); 1245 int nbpairs = 0; 1246 begin_s_expr ("variables"); 1247 fprintf (state_file, "%d", nbvars); 1248 nbpairs = write_state_pair_list (variables); 1249 gcc_assert (nbpairs == nbvars); 1250 end_s_expr (); 1251 if (verbosity_level >= 2) 1252 printf ("%s wrote %d variables.\n", progname, nbvars); 1253 } 1254 1255 /* Write the source directory. File locations within the source 1256 directory have been written specifically. */ 1257 void 1258 state_writer::write_state_srcdir (void) 1259 { 1260 begin_s_expr ("srcdir"); 1261 write_state_a_string (srcdir); 1262 end_s_expr (); 1263 } 1264 1265 /* Count and write the list of our files. */ 1266 void 1267 state_writer::write_state_files_list (void) 1268 { 1269 int i = 0; 1270 /* Write the list of files with their lang_bitmap. */ 1271 begin_s_expr ("fileslist"); 1272 fprintf (state_file, "%d", (int) num_gt_files); 1273 for (i = 0; i < (int) num_gt_files; i++) 1274 { 1275 const char *cursrcrelpath = NULL; 1276 const input_file *curfil = gt_files[i]; 1277 /* Most of the files are inside $(srcdir) so it is worth to 1278 handle them specially. */ 1279 cursrcrelpath = get_file_srcdir_relative_path (curfil); 1280 if (cursrcrelpath) 1281 { 1282 begin_s_expr ("srcfile"); 1283 fprintf (state_file, "%d ", get_lang_bitmap (curfil)); 1284 write_state_a_string (cursrcrelpath); 1285 } 1286 else 1287 { 1288 begin_s_expr ("file"); 1289 fprintf (state_file, "%d ", get_lang_bitmap (curfil)); 1290 write_state_a_string (get_input_file_name (curfil)); 1291 } 1292 /* Terminate the inner s-expression (either "srcfile" or "file"). */ 1293 end_s_expr (); 1294 } 1295 /* Terminate the "fileslist" s-expression. */ 1296 end_s_expr (); 1297 } 1298 1299 /* Write the list of GCC front-end languages. */ 1300 void 1301 state_writer::write_state_languages (void) 1302 { 1303 int i = 0; 1304 begin_s_expr ("languages"); 1305 fprintf (state_file, "%d", (int) num_lang_dirs); 1306 for (i = 0; i < (int) num_lang_dirs; i++) 1307 { 1308 /* Languages names are identifiers, we expect only letters or 1309 underscores or digits in them. In particular, C++ is not a 1310 valid language name, but cp is valid. */ 1311 fprintf (state_file, " %s", lang_dir_names[i]); 1312 } 1313 end_s_expr (); 1314 } 1315 1316 /* Write the trailer. */ 1317 static void 1318 write_state_trailer (void) 1319 { 1320 /* This test should probably catch IO errors like disk full... */ 1321 if (fputs ("\n(!endfile)\n", state_file) == EOF) 1322 fatal ("failed to write state trailer [%s]", xstrerror (errno)); 1323 } 1324 1325 /* The write_state routine is the only writing routine called by main 1326 in gengtype.c. To avoid messing the state if gengtype is 1327 interrupted or aborted, we write a temporary file and rename it 1328 after having written it in totality. */ 1329 void 1330 write_state (const char *state_path) 1331 { 1332 long statelen = 0; 1333 time_t now = 0; 1334 char *temp_state_path = NULL; 1335 char tempsuffix[40]; 1336 time (&now); 1337 1338 /* We write a unique temporary file which is renamed when complete 1339 * only. So even if gengtype is interrupted, the written state file 1340 * won't be partially written, since the temporary file is not yet 1341 * renamed in that case. */ 1342 memset (tempsuffix, 0, sizeof (tempsuffix)); 1343 snprintf (tempsuffix, sizeof (tempsuffix) - 1, "-%ld-%d.tmp", (long) now, 1344 (int) getpid ()); 1345 temp_state_path = concat (state_path, tempsuffix, NULL); 1346 state_file = fopen (temp_state_path, "w"); 1347 if (state_file == NULL) 1348 fatal ("Failed to open file %s for writing state: %s", 1349 temp_state_path, xstrerror (errno)); 1350 if (verbosity_level >= 3) 1351 printf ("%s writing state file %s temporarily in %s\n", 1352 progname, state_path, temp_state_path); 1353 /* This is the first line of the state. Perhaps the file utility 1354 could know about that, so don't change it often. */ 1355 fprintf (state_file, ";;;;@@@@ GCC gengtype state\n"); 1356 /* Output a few comments for humans. */ 1357 fprintf (state_file, 1358 ";;; DON'T EDIT THIS FILE, since generated by GCC's gengtype\n"); 1359 fprintf (state_file, 1360 ";;; The format of this file is tied to a particular version of GCC.\n"); 1361 fprintf (state_file, 1362 ";;; Don't parse this file wihout knowing GCC gengtype internals.\n"); 1363 fprintf (state_file, 1364 ";;; This file should be parsed by the same %s which wrote it.\n", 1365 progname); 1366 1367 state_writer sw; 1368 1369 /* The first non-comment significant line gives the version string. */ 1370 sw.write_state_version (version_string); 1371 sw.write_state_srcdir (); 1372 sw.write_state_languages (); 1373 sw.write_state_files_list (); 1374 sw.write_state_structures (); 1375 sw.write_state_typedefs (); 1376 sw.write_state_variables (); 1377 write_state_trailer (); 1378 statelen = ftell (state_file); 1379 if (ferror (state_file)) 1380 fatal ("output error when writing state file %s [%s]", 1381 temp_state_path, xstrerror (errno)); 1382 if (fclose (state_file)) 1383 fatal ("failed to close state file %s [%s]", 1384 temp_state_path, xstrerror (errno)); 1385 if (rename (temp_state_path, state_path)) 1386 fatal ("failed to rename %s to state file %s [%s]", temp_state_path, 1387 state_path, xstrerror (errno)); 1388 free (temp_state_path); 1389 1390 if (verbosity_level >= 1) 1391 printf ("%s wrote state file %s of %ld bytes with %d GTY-ed types\n", 1392 progname, state_path, statelen, sw.m_state_written_type_count); 1393 1394 } 1395 1396 /** End of writing routines! The corresponding reading routines follow. **/ 1397 1398 1399 1400 /* Forward declarations, since some read_state_* functions are 1401 recursive! */ 1402 static void read_state_fileloc (struct fileloc *line); 1403 static void read_state_options (options_p *opt); 1404 static void read_state_type (type_p *current); 1405 static void read_state_pair (pair_p *pair); 1406 /* Return the number of pairs actually read. */ 1407 static int read_state_pair_list (pair_p *list); 1408 static void read_state_fields (pair_p *fields); 1409 static void read_state_common_type_content (type_p current); 1410 1411 1412 1413 1414 /* Record into the state_seen_types hash-table a type which we are 1415 reading, to enable recursive or circular references to it. */ 1416 static void 1417 record_type (type_p type) 1418 { 1419 PTR *slot; 1420 1421 slot = htab_find_slot (state_seen_types, type, INSERT); 1422 gcc_assert (slot); 1423 1424 *slot = type; 1425 } 1426 1427 /* Read an already seen type. */ 1428 static void 1429 read_state_already_seen_type (type_p *type) 1430 { 1431 struct state_token_st *t0 = peek_state_token (0); 1432 1433 if (state_token_kind (t0) == STOK_INTEGER) 1434 { 1435 PTR *slot = NULL; 1436 struct type loctype = { TYPE_SCALAR, 0, 0, 0, GC_UNUSED, {0} }; 1437 1438 loctype.state_number = t0->stok_un.stok_num; 1439 slot = htab_find_slot (state_seen_types, &loctype, NO_INSERT); 1440 if (slot == NULL) 1441 { 1442 fatal_reading_state (t0, "Unknown type"); 1443 } 1444 1445 next_state_tokens (1); 1446 *type = (type_p) *slot; 1447 } 1448 else 1449 { 1450 fatal_reading_state (t0, "Bad seen type"); 1451 } 1452 } 1453 1454 1455 /* Read the scalar_nonchar type. */ 1456 static void 1457 read_state_scalar_nonchar_type (type_p *type) 1458 { 1459 *type = &scalar_nonchar; 1460 read_state_common_type_content (*type); 1461 } 1462 1463 1464 /* Read the scalar_char type. */ 1465 static void 1466 read_state_scalar_char_type (type_p *type) 1467 { 1468 *type = &scalar_char; 1469 read_state_common_type_content (*type); 1470 } 1471 1472 /* Read the string_type. */ 1473 static void 1474 read_state_string_type (type_p *type) 1475 { 1476 *type = &string_type; 1477 read_state_common_type_content (*type); 1478 } 1479 1480 1481 /* Read a lang_bitmap representing a set of GCC front-end languages. */ 1482 static void 1483 read_state_lang_bitmap (lang_bitmap *bitmap) 1484 { 1485 struct state_token_st *t; 1486 1487 t = peek_state_token (0); 1488 if (state_token_kind (t) == STOK_INTEGER) 1489 { 1490 *bitmap = t->stok_un.stok_num; 1491 next_state_tokens (1); 1492 } 1493 else 1494 { 1495 fatal_reading_state (t, "Bad syntax for bitmap"); 1496 } 1497 } 1498 1499 1500 /* Read an undefined type. */ 1501 static void 1502 read_state_undefined_type (type_p type) 1503 { 1504 struct state_token_st *t0; 1505 1506 type->kind = TYPE_UNDEFINED; 1507 read_state_common_type_content (type); 1508 t0 = peek_state_token (0); 1509 if (state_token_kind (t0) == STOK_STRING) 1510 { 1511 if (state_token_is_name (t0, "nil")) 1512 { 1513 type->u.s.tag = NULL; 1514 DBGPRINTF ("read anonymous undefined type @%p #%d", 1515 (void *) type, type->state_number); 1516 } 1517 else 1518 { 1519 type->u.s.tag = xstrdup (t0->stok_un.stok_string); 1520 DBGPRINTF ("read undefined type @%p #%d '%s'", 1521 (void *) type, type->state_number, type->u.s.tag); 1522 } 1523 1524 next_state_tokens (1); 1525 read_state_fileloc (&(type->u.s.line)); 1526 } 1527 else 1528 { 1529 fatal_reading_state (t0, "Bad tag in undefined type"); 1530 } 1531 } 1532 1533 1534 /* Read a GTY-ed struct type. */ 1535 static void 1536 read_state_struct_type (type_p type) 1537 { 1538 struct state_token_st *t0; 1539 1540 type->kind = TYPE_STRUCT; 1541 read_state_common_type_content (type); 1542 t0 = peek_state_token (0); 1543 if (state_token_kind (t0) == STOK_STRING) 1544 { 1545 if (state_token_is_name (t0, "nil")) 1546 { 1547 type->u.s.tag = NULL; 1548 DBGPRINTF ("read anonymous struct type @%p #%d", 1549 (void *) type, type->state_number); 1550 } 1551 else 1552 { 1553 type->u.s.tag = xstrdup (t0->stok_un.stok_string); 1554 DBGPRINTF ("read struct type @%p #%d '%s'", 1555 (void *) type, type->state_number, type->u.s.tag); 1556 } 1557 1558 next_state_tokens (1); 1559 read_state_fileloc (&(type->u.s.line)); 1560 read_state_fields (&(type->u.s.fields)); 1561 read_state_options (&(type->u.s.opt)); 1562 read_state_lang_bitmap (&(type->u.s.bitmap)); 1563 read_state_type (&(type->u.s.lang_struct)); 1564 read_state_type (&(type->u.s.base_class)); 1565 if (type->u.s.base_class) 1566 add_subclass (type->u.s.base_class, type); 1567 } 1568 else 1569 { 1570 fatal_reading_state (t0, "Bad tag in struct type"); 1571 } 1572 } 1573 1574 1575 /* Read a GTY-ed user-provided struct TYPE. */ 1576 1577 static void 1578 read_state_user_struct_type (type_p type) 1579 { 1580 struct state_token_st *t0; 1581 1582 type->kind = TYPE_USER_STRUCT; 1583 read_state_common_type_content (type); 1584 t0 = peek_state_token (0); 1585 if (state_token_kind (t0) == STOK_STRING) 1586 { 1587 if (state_token_is_name (t0, "nil")) 1588 { 1589 type->u.s.tag = NULL; 1590 DBGPRINTF ("read anonymous struct type @%p #%d", 1591 (void *) type, type->state_number); 1592 } 1593 else 1594 { 1595 type->u.s.tag = xstrdup (t0->stok_un.stok_string); 1596 DBGPRINTF ("read struct type @%p #%d '%s'", 1597 (void *) type, type->state_number, type->u.s.tag); 1598 } 1599 1600 next_state_tokens (1); 1601 read_state_fileloc (&(type->u.s.line)); 1602 read_state_fields (&(type->u.s.fields)); 1603 } 1604 else 1605 { 1606 fatal_reading_state (t0, "Bad tag in user-struct type"); 1607 } 1608 } 1609 1610 1611 /* Read a GTY-ed union type. */ 1612 static void 1613 read_state_union_type (type_p type) 1614 { 1615 struct state_token_st *t0; 1616 1617 type->kind = TYPE_UNION; 1618 read_state_common_type_content (type); 1619 t0 = peek_state_token (0); 1620 if (state_token_kind (t0) == STOK_STRING) 1621 { 1622 if (state_token_is_name (t0, "nil")) 1623 { 1624 type->u.s.tag = NULL; 1625 DBGPRINTF ("read anonymous union type @%p #%d", 1626 (void *) type, type->state_number); 1627 } 1628 else 1629 { 1630 type->u.s.tag = xstrdup (t0->stok_un.stok_string); 1631 DBGPRINTF ("read union type @%p #%d '%s'", 1632 (void *) type, type->state_number, type->u.s.tag); 1633 } 1634 next_state_tokens (1); 1635 read_state_fileloc (&(type->u.s.line)); 1636 read_state_fields (&(type->u.s.fields)); 1637 read_state_options (&(type->u.s.opt)); 1638 read_state_lang_bitmap (&(type->u.s.bitmap)); 1639 read_state_type (&(type->u.s.lang_struct)); 1640 } 1641 else 1642 fatal_reading_state (t0, "Bad tag in union type"); 1643 } 1644 1645 1646 /* Read a GTY-ed pointer type. */ 1647 static void 1648 read_state_pointer_type (type_p type) 1649 { 1650 type->kind = TYPE_POINTER; 1651 read_state_common_type_content (type); 1652 DBGPRINTF ("read pointer type @%p #%d", (void *) type, type->state_number); 1653 read_state_type (&(type->u.p)); 1654 } 1655 1656 1657 /* Read a GTY-ed array type. */ 1658 static void 1659 read_state_array_type (type_p type) 1660 { 1661 struct state_token_st *t0; 1662 1663 type->kind = TYPE_ARRAY; 1664 read_state_common_type_content (type); 1665 t0 = peek_state_token (0); 1666 if (state_token_kind (t0) == STOK_STRING) 1667 { 1668 type->u.a.len = xstrdup (t0->stok_un.stok_string); 1669 DBGPRINTF ("read array type @%p #%d length '%s'", 1670 (void *) type, type->state_number, type->u.a.len); 1671 next_state_tokens (1); 1672 } 1673 1674 else if (state_token_is_name (t0, "nil")) 1675 { 1676 type->u.a.len = NULL; 1677 DBGPRINTF ("read array type @%p #%d without length", 1678 (void *) type, type->state_number); 1679 next_state_tokens (1); 1680 } 1681 1682 else 1683 fatal_reading_state (t0, "Bad array name type"); 1684 read_state_type (&(type->u.a.p)); 1685 } 1686 1687 1688 1689 /* Read a lang_struct type for GTY-ed struct-s which depends upon GCC 1690 front-end languages. This is a tricky function and it was painful 1691 to debug. Change it with extreme care. See also 1692 write_state_lang_struct_type. */ 1693 static void 1694 read_state_lang_struct_type (type_p type) 1695 { 1696 struct state_token_st *t0 = NULL; 1697 struct state_token_st *t1 = NULL; 1698 struct state_token_st *t2 = NULL; 1699 1700 type->kind = TYPE_LANG_STRUCT; 1701 read_state_common_type_content (type); 1702 t0 = peek_state_token (0); 1703 if (state_token_kind (t0) == STOK_STRING) 1704 { 1705 if (state_token_is_name (t0, "nil")) 1706 { 1707 DBGPRINTF ("read anonymous lang_struct type @%p #%d", 1708 (void *) type, type->state_number); 1709 type->u.s.tag = NULL; 1710 } 1711 else 1712 { 1713 type->u.s.tag = xstrdup (t0->stok_un.stok_string); 1714 DBGPRINTF ("read lang_struct type @%p #%d '%s'", 1715 (void *) type, type->state_number, type->u.s.tag); 1716 } 1717 next_state_tokens (1); 1718 } 1719 else 1720 fatal_reading_state (t0, "Bad tag in lang struct type"); 1721 read_state_fileloc (&(type->u.s.line)); 1722 read_state_fields (&(type->u.s.fields)); 1723 read_state_options (&(type->u.s.opt)); 1724 read_state_lang_bitmap (&(type->u.s.bitmap)); 1725 /* Within lang_struct-ures, the lang_struct field is a linked list 1726 of homonymous types! */ 1727 t0 = peek_state_token (0); 1728 t1 = peek_state_token (1); 1729 t2 = peek_state_token (2); 1730 /* Parse (!homotypes <number-types> <type-1> .... <type-n>) */ 1731 if (state_token_kind (t0) == STOK_LEFTPAR 1732 && state_token_is_name (t1, "!homotypes") 1733 && state_token_kind (t2) == STOK_INTEGER) 1734 { 1735 type_p *prevty = &type->u.s.lang_struct; 1736 int nbhomotype = t2->stok_un.stok_num; 1737 int i = 0; 1738 t0 = t1 = t2 = NULL; 1739 next_state_tokens (3); 1740 for (i = 0; i < nbhomotype; i++) 1741 { 1742 read_state_type (prevty); 1743 t0 = peek_state_token (0); 1744 if (*prevty) 1745 prevty = &(*prevty)->next; 1746 else 1747 fatal_reading_state (t0, 1748 "expecting type in homotype list for lang_struct"); 1749 }; 1750 if (state_token_kind (t0) != STOK_RIGHTPAR) 1751 fatal_reading_state (t0, 1752 "expecting ) in homotype list for lang_struct"); 1753 next_state_tokens (1); 1754 } 1755 else 1756 fatal_reading_state (t0, "expecting !homotypes for lang_struct"); 1757 } 1758 1759 1760 /* Read the gc used information. */ 1761 static void 1762 read_state_gc_used (enum gc_used_enum *pgus) 1763 { 1764 struct state_token_st *t0 = peek_state_token (0); 1765 if (state_token_is_name (t0, "gc_unused")) 1766 *pgus = GC_UNUSED; 1767 else if (state_token_is_name (t0, "gc_used")) 1768 *pgus = GC_USED; 1769 else if (state_token_is_name (t0, "gc_maybe_pointed_to")) 1770 *pgus = GC_MAYBE_POINTED_TO; 1771 else if (state_token_is_name (t0, "gc_pointed_to")) 1772 *pgus = GC_POINTED_TO; 1773 else 1774 fatal_reading_state (t0, "invalid gc_used information"); 1775 next_state_tokens (1); 1776 } 1777 1778 1779 /* Utility function to read the common content of types. */ 1780 static void 1781 read_state_common_type_content (type_p current) 1782 { 1783 struct state_token_st *t0 = peek_state_token (0); 1784 1785 if (state_token_kind (t0) == STOK_INTEGER) 1786 { 1787 current->state_number = t0->stok_un.stok_num; 1788 next_state_tokens (1); 1789 record_type (current); 1790 } 1791 else 1792 fatal_reading_state_printf (t0, 1793 "Expected integer for state_number line %d", 1794 state_line); 1795 /* We don't read the next field of the type. */ 1796 read_state_type (¤t->pointer_to); 1797 read_state_gc_used (¤t->gc_used); 1798 } 1799 1800 1801 /* Read a GTY-ed type. */ 1802 void 1803 read_state_type (type_p *current) 1804 { 1805 struct state_token_st *t0 = peek_state_token (0); 1806 struct state_token_st *t1 = peek_state_token (1); 1807 1808 if (state_token_kind (t0) == STOK_LEFTPAR && 1809 state_token_is_name (t1, "!type")) 1810 { 1811 next_state_tokens (2); 1812 t0 = peek_state_token (0); 1813 if (state_token_is_name (t0, "already_seen")) 1814 { 1815 next_state_tokens (1); 1816 read_state_already_seen_type (current); 1817 } 1818 else 1819 { 1820 t0 = peek_state_token (0); 1821 1822 if (state_token_is_name (t0, "scalar_nonchar")) 1823 { 1824 next_state_tokens (1); 1825 read_state_scalar_nonchar_type (current); 1826 } 1827 else if (state_token_is_name (t0, "scalar_char")) 1828 { 1829 next_state_tokens (1); 1830 read_state_scalar_char_type (current); 1831 } 1832 else if (state_token_is_name (t0, "string")) 1833 { 1834 next_state_tokens (1); 1835 read_state_string_type (current); 1836 } 1837 else if (state_token_is_name (t0, "undefined")) 1838 { 1839 *current = XCNEW (struct type); 1840 next_state_tokens (1); 1841 read_state_undefined_type (*current); 1842 } 1843 else if (state_token_is_name (t0, "struct")) 1844 { 1845 *current = XCNEW (struct type); 1846 next_state_tokens (1); 1847 read_state_struct_type (*current); 1848 } 1849 else if (state_token_is_name (t0, "union")) 1850 { 1851 *current = XCNEW (struct type); 1852 next_state_tokens (1); 1853 read_state_union_type (*current); 1854 } 1855 else if (state_token_is_name (t0, "lang_struct")) 1856 { 1857 *current = XCNEW (struct type); 1858 next_state_tokens (1); 1859 read_state_lang_struct_type (*current); 1860 } 1861 else if (state_token_is_name (t0, "pointer")) 1862 { 1863 *current = XCNEW (struct type); 1864 next_state_tokens (1); 1865 read_state_pointer_type (*current); 1866 } 1867 else if (state_token_is_name (t0, "array")) 1868 { 1869 *current = XCNEW (struct type); 1870 next_state_tokens (1); 1871 read_state_array_type (*current); 1872 } 1873 else if (state_token_is_name (t0, "user_struct")) 1874 { 1875 *current = XCNEW (struct type); 1876 next_state_tokens (1); 1877 read_state_user_struct_type (*current); 1878 } 1879 else 1880 fatal_reading_state (t0, "bad type in (!type"); 1881 } 1882 t0 = peek_state_token (0); 1883 if (state_token_kind (t0) != STOK_RIGHTPAR) 1884 fatal_reading_state (t0, "missing ) in type"); 1885 next_state_tokens (1); 1886 } 1887 else if (state_token_is_name (t0, "nil")) 1888 { 1889 next_state_tokens (1); 1890 *current = NULL; 1891 } 1892 else 1893 fatal_reading_state (t0, "bad type syntax"); 1894 } 1895 1896 1897 /* Read a file location. Files within the source directory are dealt 1898 with specifically. */ 1899 void 1900 read_state_fileloc (struct fileloc *floc) 1901 { 1902 bool issrcfile = false; 1903 struct state_token_st *t0 = peek_state_token (0); 1904 struct state_token_st *t1 = peek_state_token (1); 1905 1906 gcc_assert (floc != NULL); 1907 gcc_assert (srcdir != NULL); 1908 1909 if (state_token_kind (t0) == STOK_LEFTPAR && 1910 (state_token_is_name (t1, "!fileloc") 1911 || (issrcfile = state_token_is_name (t1, "!srcfileloc")))) 1912 { 1913 next_state_tokens (2); 1914 t0 = peek_state_token (0); 1915 t1 = peek_state_token (1); 1916 if (state_token_kind (t0) == STOK_STRING && 1917 state_token_kind (t1) == STOK_INTEGER) 1918 { 1919 char *path = t0->stok_un.stok_string; 1920 if (issrcfile) 1921 { 1922 static const char dirsepstr[2] = { DIR_SEPARATOR, (char) 0 }; 1923 char *fullpath = concat (srcdir, dirsepstr, path, NULL); 1924 floc->file = input_file_by_name (fullpath); 1925 free (fullpath); 1926 } 1927 else 1928 floc->file = input_file_by_name (path); 1929 floc->line = t1->stok_un.stok_num; 1930 next_state_tokens (2); 1931 } 1932 else 1933 fatal_reading_state (t0, 1934 "Bad fileloc syntax, expected path string and line"); 1935 t0 = peek_state_token (0); 1936 if (state_token_kind (t0) != STOK_RIGHTPAR) 1937 fatal_reading_state (t0, "Bad fileloc syntax, expected )"); 1938 next_state_tokens (1); 1939 } 1940 else if (state_token_is_name (t0, "nil")) 1941 { 1942 next_state_tokens (1); 1943 floc->file = NULL; 1944 floc->line = 0; 1945 } 1946 else 1947 fatal_reading_state (t0, "Bad fileloc syntax"); 1948 } 1949 1950 1951 /* Read the fields of a GTY-ed type. */ 1952 void 1953 read_state_fields (pair_p *fields) 1954 { 1955 pair_p tmp = NULL; 1956 struct state_token_st *t0 = peek_state_token (0); 1957 struct state_token_st *t1 = peek_state_token (1); 1958 struct state_token_st *t2 = peek_state_token (2); 1959 1960 if (state_token_kind (t0) == STOK_LEFTPAR 1961 && state_token_is_name (t1, "!fields") 1962 && state_token_kind (t2) == STOK_INTEGER) 1963 { 1964 int nbfields = t2->stok_un.stok_num; 1965 int nbpairs = 0; 1966 next_state_tokens (3); 1967 nbpairs = read_state_pair_list (&tmp); 1968 t0 = peek_state_token (0); 1969 if (nbpairs != nbfields) 1970 fatal_reading_state_printf 1971 (t0, 1972 "Mismatched fields number, expected %d got %d", nbpairs, nbfields); 1973 if (state_token_kind (t0) == STOK_RIGHTPAR) 1974 next_state_tokens (1); 1975 else 1976 fatal_reading_state (t0, "Bad fields expecting )"); 1977 } 1978 1979 *fields = tmp; 1980 } 1981 1982 1983 /* Read a string option. */ 1984 static void 1985 read_state_string_option (options_p opt) 1986 { 1987 struct state_token_st *t0 = peek_state_token (0); 1988 opt->kind = OPTION_STRING; 1989 if (state_token_kind (t0) == STOK_STRING) 1990 { 1991 opt->info.string = xstrdup (t0->stok_un.stok_string); 1992 next_state_tokens (1); 1993 } 1994 else if (state_token_is_name (t0, "nil")) 1995 { 1996 opt->info.string = NULL; 1997 next_state_tokens (1); 1998 } 1999 else 2000 fatal_reading_state (t0, "Missing name in string option"); 2001 } 2002 2003 2004 /* Read a type option. */ 2005 static void 2006 read_state_type_option (options_p opt) 2007 { 2008 opt->kind = OPTION_TYPE; 2009 read_state_type (&(opt->info.type)); 2010 } 2011 2012 2013 /* Read a nested option. */ 2014 static void 2015 read_state_nested_option (options_p opt) 2016 { 2017 struct state_token_st *t0; 2018 2019 opt->info.nested = XCNEW (struct nested_ptr_data); 2020 opt->kind = OPTION_NESTED; 2021 read_state_type (&(opt->info.nested->type)); 2022 t0 = peek_state_token (0); 2023 if (state_token_kind (t0) == STOK_STRING) 2024 { 2025 opt->info.nested->convert_from = xstrdup (t0->stok_un.stok_string); 2026 next_state_tokens (1); 2027 } 2028 else if (state_token_is_name (t0, "nil")) 2029 { 2030 opt->info.nested->convert_from = NULL; 2031 next_state_tokens (1); 2032 } 2033 else 2034 fatal_reading_state (t0, "Bad nested convert_from option"); 2035 2036 t0 = peek_state_token (0); 2037 if (state_token_kind (t0) == STOK_STRING) 2038 { 2039 opt->info.nested->convert_to = xstrdup (t0->stok_un.stok_string); 2040 next_state_tokens (1); 2041 } 2042 else if (state_token_is_name (t0, "nil")) 2043 { 2044 opt->info.nested->convert_to = NULL; 2045 next_state_tokens (1); 2046 } 2047 else 2048 fatal_reading_state (t0, "Bad nested convert_from option"); 2049 } 2050 2051 2052 /* Read an GTY option. */ 2053 static void 2054 read_state_option (options_p *opt) 2055 { 2056 struct state_token_st *t0 = peek_state_token (0); 2057 struct state_token_st *t1 = peek_state_token (1); 2058 2059 if (state_token_kind (t0) == STOK_LEFTPAR && 2060 state_token_is_name (t1, "!option")) 2061 { 2062 next_state_tokens (2); 2063 t0 = peek_state_token (0); 2064 if (state_token_kind (t0) == STOK_NAME) 2065 { 2066 *opt = XCNEW (struct options); 2067 if (state_token_is_name (t0, "nil")) 2068 (*opt)->name = NULL; 2069 else 2070 (*opt)->name = t0->stok_un.stok_ident->stid_name; 2071 next_state_tokens (1); 2072 t0 = peek_state_token (0); 2073 if (state_token_kind (t0) == STOK_NAME) 2074 { 2075 if (state_token_is_name (t0, "string")) 2076 { 2077 next_state_tokens (1); 2078 read_state_string_option (*opt); 2079 } 2080 else if (state_token_is_name (t0, "type")) 2081 { 2082 next_state_tokens (1); 2083 read_state_type_option (*opt); 2084 } 2085 else if (state_token_is_name (t0, "nested")) 2086 { 2087 next_state_tokens (1); 2088 read_state_nested_option (*opt); 2089 } 2090 else 2091 fatal_reading_state (t0, "Bad option type"); 2092 t0 = peek_state_token (0); 2093 if (state_token_kind (t0) != STOK_RIGHTPAR) 2094 fatal_reading_state (t0, "Bad syntax in option, expecting )"); 2095 2096 next_state_tokens (1); 2097 } 2098 else 2099 fatal_reading_state (t0, "Missing option type"); 2100 } 2101 else 2102 fatal_reading_state (t0, "Bad name for option"); 2103 } 2104 else 2105 fatal_reading_state (t0, "Bad option, waiting for )"); 2106 } 2107 2108 /* Read a list of options. */ 2109 void 2110 read_state_options (options_p *opt) 2111 { 2112 options_p head = NULL; 2113 options_p previous = NULL; 2114 options_p current_option = NULL; 2115 struct state_token_st *t0 = peek_state_token (0); 2116 struct state_token_st *t1 = peek_state_token (1); 2117 2118 if (state_token_kind (t0) == STOK_LEFTPAR && 2119 state_token_is_name (t1, "!options")) 2120 { 2121 next_state_tokens (2); 2122 t0 = peek_state_token (0); 2123 while (state_token_kind (t0) != STOK_RIGHTPAR) 2124 { 2125 read_state_option (¤t_option); 2126 if (head == NULL) 2127 { 2128 head = current_option; 2129 previous = head; 2130 } 2131 else 2132 { 2133 previous->next = current_option; 2134 previous = current_option; 2135 } 2136 t0 = peek_state_token (0); 2137 } 2138 next_state_tokens (1); 2139 } 2140 else if (state_token_is_name (t0, "nil")) 2141 { 2142 next_state_tokens (1); 2143 } 2144 else 2145 fatal_reading_state (t0, "Bad options syntax"); 2146 2147 *opt = head; 2148 } 2149 2150 2151 /* Read a version, and check against the version of the gengtype. */ 2152 static void 2153 read_state_version (const char *version_string) 2154 { 2155 struct state_token_st *t0 = peek_state_token (0); 2156 struct state_token_st *t1 = peek_state_token (1); 2157 2158 if (state_token_kind (t0) == STOK_LEFTPAR && 2159 state_token_is_name (t1, "!version")) 2160 { 2161 next_state_tokens (2); 2162 t0 = peek_state_token (0); 2163 t1 = peek_state_token (1); 2164 if (state_token_kind (t0) == STOK_STRING && 2165 state_token_kind (t1) == STOK_RIGHTPAR) 2166 { 2167 /* Check that the read version string is the same as current 2168 version. */ 2169 if (strcmp (version_string, t0->stok_un.stok_string)) 2170 fatal_reading_state_printf (t0, 2171 "version string mismatch; expecting %s but got %s", 2172 version_string, 2173 t0->stok_un.stok_string); 2174 next_state_tokens (2); 2175 } 2176 else 2177 fatal_reading_state (t0, "Missing version or right parenthesis"); 2178 } 2179 else 2180 fatal_reading_state (t0, "Bad version syntax"); 2181 } 2182 2183 2184 /* Read a pair. */ 2185 void 2186 read_state_pair (pair_p *current) 2187 { 2188 struct state_token_st *t0 = peek_state_token (0); 2189 struct state_token_st *t1 = peek_state_token (1); 2190 if (state_token_kind (t0) == STOK_LEFTPAR && 2191 state_token_is_name (t1, "!pair")) 2192 { 2193 *current = XCNEW (struct pair); 2194 next_state_tokens (2); 2195 t0 = peek_state_token (0); 2196 if (state_token_kind (t0) == STOK_STRING) 2197 { 2198 if (strcmp (t0->stok_un.stok_string, "nil") == 0) 2199 { 2200 (*current)->name = NULL; 2201 } 2202 else 2203 { 2204 (*current)->name = xstrdup (t0->stok_un.stok_string); 2205 } 2206 next_state_tokens (1); 2207 read_state_type (&((*current)->type)); 2208 read_state_fileloc (&((*current)->line)); 2209 read_state_options (&((*current)->opt)); 2210 t0 = peek_state_token (0); 2211 if (state_token_kind (t0) == STOK_RIGHTPAR) 2212 { 2213 next_state_tokens (1); 2214 } 2215 else 2216 { 2217 fatal_reading_state (t0, "Bad syntax for pair, )"); 2218 } 2219 } 2220 else 2221 { 2222 fatal_reading_state (t0, "Bad name for pair"); 2223 } 2224 } 2225 else if (state_token_kind (t0) == STOK_NAME && 2226 state_token_is_name (t0, "nil")) 2227 { 2228 next_state_tokens (1); 2229 *current = NULL; 2230 } 2231 else 2232 fatal_reading_state_printf (t0, "Bad syntax for pair, (!pair %d", 2233 state_token->stok_kind); 2234 } 2235 2236 2237 /* Return the number of pairs actually read. */ 2238 int 2239 read_state_pair_list (pair_p *list) 2240 { 2241 int nbpair = 0; 2242 pair_p head = NULL; 2243 pair_p previous = NULL; 2244 pair_p tmp = NULL; 2245 struct state_token_st *t0 = peek_state_token (0); 2246 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR) 2247 { 2248 read_state_pair (&tmp); 2249 if (head == NULL) 2250 { 2251 head = tmp; 2252 previous = head; 2253 } 2254 else 2255 { 2256 previous->next = tmp; 2257 previous = tmp; 2258 } 2259 t0 = peek_state_token (0); 2260 nbpair++; 2261 } 2262 2263 /* don't consume the ); the caller will eat it. */ 2264 *list = head; 2265 return nbpair; 2266 } 2267 2268 /* Read the typedefs. */ 2269 static void 2270 read_state_typedefs (pair_p *typedefs) 2271 { 2272 int nbtypedefs = 0; 2273 pair_p list = NULL; 2274 struct state_token_st *t0 = peek_state_token (0); 2275 struct state_token_st *t1 = peek_state_token (1); 2276 struct state_token_st *t2 = peek_state_token (2); 2277 2278 if (state_token_kind (t0) == STOK_LEFTPAR 2279 && state_token_is_name (t1, "!typedefs") 2280 && state_token_kind (t2) == STOK_INTEGER) 2281 { 2282 int nbpairs = 0; 2283 nbtypedefs = t2->stok_un.stok_num; 2284 next_state_tokens (3); 2285 nbpairs = read_state_pair_list (&list); 2286 t0 = peek_state_token (0); 2287 if (nbpairs != nbtypedefs) 2288 fatal_reading_state_printf 2289 (t0, 2290 "invalid number of typedefs, expected %d but got %d", 2291 nbtypedefs, nbpairs); 2292 if (state_token_kind (t0) == STOK_RIGHTPAR) 2293 next_state_tokens (1); 2294 else 2295 fatal_reading_state (t0, "Bad typedefs syntax )"); 2296 } 2297 else 2298 fatal_reading_state (t0, "Bad typedefs syntax (!typedefs"); 2299 2300 if (verbosity_level >= 2) 2301 printf ("%s read %d typedefs from state\n", progname, nbtypedefs); 2302 *typedefs = list; 2303 } 2304 2305 2306 /* Read the structures. */ 2307 static void 2308 read_state_structures (type_p *structures) 2309 { 2310 type_p head = NULL; 2311 type_p previous = NULL; 2312 type_p tmp; 2313 int nbstruct = 0, countstruct = 0; 2314 struct state_token_st *t0 = peek_state_token (0); 2315 struct state_token_st *t1 = peek_state_token (1); 2316 struct state_token_st *t2 = peek_state_token (2); 2317 2318 if (state_token_kind (t0) == STOK_LEFTPAR 2319 && state_token_is_name (t1, "!structures") 2320 && state_token_kind (t2) == STOK_INTEGER) 2321 { 2322 nbstruct = t2->stok_un.stok_num; 2323 next_state_tokens (3); 2324 t0 = peek_state_token (0); 2325 while (t0 && state_token_kind (t0) != STOK_RIGHTPAR) 2326 { 2327 tmp = NULL; 2328 read_state_type (&tmp); 2329 countstruct++; 2330 if (head == NULL) 2331 { 2332 head = tmp; 2333 previous = head; 2334 } 2335 else 2336 { 2337 previous->next = tmp; 2338 previous = tmp; 2339 } 2340 t0 = peek_state_token (0); 2341 } 2342 next_state_tokens (1); 2343 } 2344 else 2345 fatal_reading_state (t0, "Bad structures syntax"); 2346 if (countstruct != nbstruct) 2347 fatal_reading_state_printf (NULL_STATE_TOKEN, 2348 "expected %d structures but got %d", 2349 nbstruct, countstruct); 2350 if (verbosity_level >= 2) 2351 printf ("%s read %d structures from state\n", progname, nbstruct); 2352 *structures = head; 2353 } 2354 2355 2356 /* Read the variables. */ 2357 static void 2358 read_state_variables (pair_p *variables) 2359 { 2360 pair_p list = NULL; 2361 int nbvars = 0; 2362 struct state_token_st *t0 = peek_state_token (0); 2363 struct state_token_st *t1 = peek_state_token (1); 2364 struct state_token_st *t2 = peek_state_token (2); 2365 2366 if (state_token_kind (t0) == STOK_LEFTPAR 2367 && state_token_is_name (t1, "!variables") 2368 && state_token_kind (t2) == STOK_INTEGER) 2369 { 2370 int nbpairs = 0; 2371 nbvars = t2->stok_un.stok_num; 2372 next_state_tokens (3); 2373 nbpairs = read_state_pair_list (&list); 2374 t0 = peek_state_token (0); 2375 if (nbpairs != nbvars) 2376 fatal_reading_state_printf 2377 (t0, "Invalid number of variables, expected %d but got %d", 2378 nbvars, nbpairs); 2379 if (state_token_kind (t0) == STOK_RIGHTPAR) 2380 next_state_tokens (1); 2381 else 2382 fatal_reading_state (t0, "Waiting for ) in variables"); 2383 } 2384 else 2385 fatal_reading_state (t0, "Bad variables syntax"); 2386 *variables = list; 2387 if (verbosity_level >= 2) 2388 printf ("%s read %d variables from state\n", progname, nbvars); 2389 } 2390 2391 2392 /* Read the source directory. */ 2393 static void 2394 read_state_srcdir (void) 2395 { 2396 struct state_token_st *t0 = peek_state_token (0); 2397 struct state_token_st *t1 = peek_state_token (1); 2398 if (state_token_kind (t0) == STOK_LEFTPAR && 2399 state_token_is_name (t1, "!srcdir")) 2400 { 2401 next_state_tokens (2); 2402 t0 = peek_state_token (0); 2403 t1 = peek_state_token (1); 2404 if (state_token_kind (t0) == STOK_STRING && 2405 state_token_kind (t1) == STOK_RIGHTPAR) 2406 { 2407 srcdir = xstrdup (t0->stok_un.stok_string); 2408 srcdir_len = strlen (srcdir); 2409 next_state_tokens (2); 2410 return; 2411 } 2412 } 2413 2414 fatal_reading_state (t0, "Bad srcdir in state_file"); 2415 } 2416 2417 2418 /* Read the sequence of GCC front-end languages. */ 2419 static void 2420 read_state_languages (void) 2421 { 2422 struct state_token_st *t0 = peek_state_token (0); 2423 struct state_token_st *t1 = peek_state_token (1); 2424 struct state_token_st *t2 = peek_state_token (2); 2425 if (state_token_kind (t0) == STOK_LEFTPAR 2426 && state_token_is_name (t1, "!languages") 2427 && state_token_kind (t2) == STOK_INTEGER) 2428 { 2429 int i = 0; 2430 num_lang_dirs = t2->stok_un.stok_num; 2431 lang_dir_names = XCNEWVEC (const char *, num_lang_dirs); 2432 next_state_tokens (3); 2433 t0 = t1 = t2 = NULL; 2434 for (i = 0; i < (int) num_lang_dirs; i++) 2435 { 2436 t0 = peek_state_token (0); 2437 if (state_token_kind (t0) != STOK_NAME) 2438 fatal_reading_state (t0, "expecting language name in state file"); 2439 lang_dir_names[i] = t0->stok_un.stok_ident->stid_name; 2440 next_state_tokens (1); 2441 } 2442 t0 = peek_state_token (0); 2443 if (state_token_kind (t0) != STOK_RIGHTPAR) 2444 fatal_reading_state (t0, "missing ) in languages list of state file"); 2445 next_state_tokens (1); 2446 } 2447 else 2448 fatal_reading_state (t0, "expecting languages list in state file"); 2449 2450 } 2451 2452 /* Read the sequence of files. */ 2453 static void 2454 read_state_files_list (void) 2455 { 2456 struct state_token_st *t0 = peek_state_token (0); 2457 struct state_token_st *t1 = peek_state_token (1); 2458 struct state_token_st *t2 = peek_state_token (2); 2459 2460 if (state_token_kind (t0) == STOK_LEFTPAR 2461 && state_token_is_name (t1, "!fileslist") 2462 && state_token_kind (t2) == STOK_INTEGER) 2463 { 2464 int i = 0; 2465 num_gt_files = t2->stok_un.stok_num; 2466 next_state_tokens (3); 2467 t0 = t1 = t2 = NULL; 2468 gt_files = XCNEWVEC (const input_file *, num_gt_files); 2469 for (i = 0; i < (int) num_gt_files; i++) 2470 { 2471 bool issrcfile = FALSE; 2472 t0 = t1 = t2 = NULL; 2473 t0 = peek_state_token (0); 2474 t1 = peek_state_token (1); 2475 t2 = peek_state_token (2); 2476 if (state_token_kind (t0) == STOK_LEFTPAR 2477 && (state_token_is_name (t1, "!file") 2478 || (issrcfile = state_token_is_name (t1, "!srcfile"))) 2479 && state_token_kind (t2) == STOK_INTEGER) 2480 { 2481 lang_bitmap bmap = t2->stok_un.stok_num; 2482 next_state_tokens (3); 2483 t0 = t1 = t2 = NULL; 2484 t0 = peek_state_token (0); 2485 t1 = peek_state_token (1); 2486 if (state_token_kind (t0) == STOK_STRING 2487 && state_token_kind (t1) == STOK_RIGHTPAR) 2488 { 2489 const char *fnam = t0->stok_un.stok_string; 2490 /* Allocate & fill a gt_file entry with space for the lang_bitmap before! */ 2491 input_file *curgt = NULL; 2492 if (issrcfile) 2493 { 2494 static const char dirsepstr[2] = 2495 { DIR_SEPARATOR, (char) 0 }; 2496 char *fullpath = concat (srcdir, dirsepstr, fnam, NULL); 2497 curgt = input_file_by_name (fullpath); 2498 free (fullpath); 2499 } 2500 else 2501 curgt = input_file_by_name (fnam); 2502 set_lang_bitmap (curgt, bmap); 2503 gt_files[i] = curgt; 2504 next_state_tokens (2); 2505 } 2506 else 2507 fatal_reading_state (t0, 2508 "bad file in !fileslist of state file"); 2509 } 2510 else 2511 fatal_reading_state (t0, 2512 "expecting file in !fileslist of state file"); 2513 }; 2514 t0 = peek_state_token (0); 2515 if (state_token_kind (t0) != STOK_RIGHTPAR) 2516 fatal_reading_state (t0, "missing ) for !fileslist in state file"); 2517 next_state_tokens (1); 2518 } 2519 else 2520 fatal_reading_state (t0, "missing !fileslist in state file"); 2521 } 2522 2523 2524 /* Read the trailer. */ 2525 static void 2526 read_state_trailer (void) 2527 { 2528 struct state_token_st *t0 = peek_state_token (0); 2529 struct state_token_st *t1 = peek_state_token (1); 2530 struct state_token_st *t2 = peek_state_token (2); 2531 2532 if (state_token_kind (t0) == STOK_LEFTPAR 2533 && state_token_is_name (t1, "!endfile") 2534 && state_token_kind (t2) == STOK_RIGHTPAR) 2535 next_state_tokens (3); 2536 else 2537 fatal_reading_state (t0, "missing !endfile in state file"); 2538 } 2539 2540 2541 /* Utility functions for the state_seen_types hash table. */ 2542 static unsigned 2543 hash_type_number (const void *ty) 2544 { 2545 const struct type *type = (const struct type *) ty; 2546 2547 return type->state_number; 2548 } 2549 2550 static int 2551 equals_type_number (const void *ty1, const void *ty2) 2552 { 2553 const struct type *type1 = (const struct type *) ty1; 2554 const struct type *type2 = (const struct type *) ty2; 2555 2556 return type1->state_number == type2->state_number; 2557 } 2558 2559 static int 2560 string_eq (const void *a, const void *b) 2561 { 2562 const char *a0 = (const char *)a; 2563 const char *b0 = (const char *)b; 2564 2565 return (strcmp (a0, b0) == 0); 2566 } 2567 2568 2569 /* The function reading the state, called by main from gengtype.c. */ 2570 void 2571 read_state (const char *path) 2572 { 2573 state_file = fopen (path, "r"); 2574 if (state_file == NULL) 2575 fatal ("Failed to open state file %s for reading [%s]", path, 2576 xstrerror (errno)); 2577 state_path = path; 2578 state_line = 1; 2579 2580 if (verbosity_level >= 1) 2581 { 2582 printf ("%s reading state file %s;", progname, state_path); 2583 if (verbosity_level >= 2) 2584 putchar ('\n'); 2585 fflush (stdout); 2586 } 2587 2588 state_seen_types = 2589 htab_create (2017, hash_type_number, equals_type_number, NULL); 2590 state_ident_tab = 2591 htab_create (4027, htab_hash_string, string_eq, NULL); 2592 read_state_version (version_string); 2593 read_state_srcdir (); 2594 read_state_languages (); 2595 read_state_files_list (); 2596 read_state_structures (&structures); 2597 if (ferror (state_file)) 2598 fatal_reading_state_printf 2599 (NULL_STATE_TOKEN, "input error while reading state [%s]", 2600 xstrerror (errno)); 2601 read_state_typedefs (&typedefs); 2602 read_state_variables (&variables); 2603 read_state_trailer (); 2604 2605 if (verbosity_level >= 1) 2606 { 2607 printf ("%s read %ld bytes.\n", progname, ftell (state_file)); 2608 fflush (stdout); 2609 }; 2610 2611 if (fclose (state_file)) 2612 fatal ("failed to close read state file %s [%s]", 2613 path, xstrerror (errno)); 2614 state_file = NULL; 2615 state_path = NULL; 2616 } 2617 2618 /* End of file gengtype-state.c. */ 2619