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