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