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