]>
Commit | Line | Data |
---|---|---|
e2500fed | 1 | /* Process source files and output type information. |
8d9254fc | 2 | Copyright (C) 2002-2020 Free Software Foundation, Inc. |
e2500fed | 3 | |
e1b793e7 | 4 | This file is part of GCC. |
e2500fed | 5 | |
e1b793e7 BS |
6 | GCC is free software; you can redistribute it and/or modify it under |
7 | the terms of the GNU General Public License as published by the Free | |
8 | Software Foundation; either version 3, or (at your option) any later | |
9 | version. | |
e2500fed | 10 | |
e1b793e7 BS |
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
e2500fed | 15 | |
e1b793e7 BS |
16 | You should have received a copy of the GNU General Public License |
17 | along with GCC; see the file COPYING3. If not see | |
18 | <http://www.gnu.org/licenses/>. */ | |
e2500fed | 19 | |
065ae611 ZW |
20 | #ifndef GCC_GENGTYPE_H |
21 | #define GCC_GENGTYPE_H | |
22 | ||
19a9ba64 AM |
23 | #define obstack_chunk_alloc xmalloc |
24 | #define obstack_chunk_free free | |
79bdca32 SB |
25 | #define OBSTACK_CHUNK_SIZE 0 |
26 | ||
f8ed6dc5 JS |
27 | /* Sets of accepted source languages like C, C++, Ada... are |
28 | represented by a bitmap. */ | |
29 | typedef unsigned lang_bitmap; | |
30 | ||
14c4815e BS |
31 | /* Variable length structure representing an input file. A hash table |
32 | ensure uniqueness for a given input file name. The only function | |
33 | allocating input_file-s is input_file_by_name. */ | |
34 | struct input_file_st | |
35 | { | |
36 | struct outf* inpoutf; /* Cached corresponding output file, computed | |
37 | in get_output_file_with_visibility. */ | |
38 | lang_bitmap inpbitmap; /* The set of languages using this file. */ | |
fbb20b29 | 39 | bool inpisplugin; /* Flag set for plugin input files. */ |
14c4815e BS |
40 | char inpname[1]; /* A variable-length array, ended by a null |
41 | char. */ | |
42 | }; | |
43 | typedef struct input_file_st input_file; | |
44 | ||
b8698a0f | 45 | /* A file position, mostly for error messages. |
e2500fed | 46 | The FILE element may be compared using pointer equality. */ |
e1b793e7 BS |
47 | struct fileloc |
48 | { | |
14c4815e | 49 | const input_file *file; |
e2500fed GK |
50 | int line; |
51 | }; | |
52 | ||
14c4815e BS |
53 | |
54 | /* Table of all input files and its size. */ | |
55 | extern const input_file** gt_files; | |
56 | extern size_t num_gt_files; | |
57 | ||
58 | /* A number of places use the name of this "gengtype.c" file for a | |
59 | location for things that we can't rely on the source to define. We | |
60 | also need to refer to the "system.h" file specifically. These two | |
61 | pointers are initialized early in main. */ | |
62 | extern input_file* this_file; | |
63 | extern input_file* system_h_file; | |
64 | ||
65 | /* Retrieve or create the input_file for a given name, which is a file | |
66 | path. This is the only function allocating input_file-s and it is | |
67 | hash-consing them. */ | |
68 | input_file* input_file_by_name (const char* name); | |
69 | ||
70 | /* For F an input_file, return the relative path to F from $(srcdir) | |
71 | if the latter is a prefix in F, NULL otherwise. */ | |
72 | const char *get_file_srcdir_relative_path (const input_file *inpf); | |
73 | ||
74 | /* Get the name of an input file. */ | |
75 | static inline const char* | |
76 | get_input_file_name (const input_file *inpf) | |
77 | { | |
78 | if (inpf) | |
79 | return inpf->inpname; | |
80 | return NULL; | |
81 | } | |
82 | ||
83 | /* Return a bitmap which has bit `1 << BASE_FILE_<lang>' set iff | |
84 | INPUT_FILE is used by <lang>. | |
85 | ||
86 | This function should be written to assume that a file _is_ used | |
87 | if the situation is unclear. If it wrongly assumes a file _is_ used, | |
88 | a linker error will result. If it wrongly assumes a file _is not_ used, | |
89 | some GC roots may be missed, which is a much harder-to-debug problem. | |
90 | */ | |
91 | ||
92 | static inline lang_bitmap | |
93 | get_lang_bitmap (const input_file* inpf) | |
94 | { | |
95 | if (inpf == NULL) | |
96 | return 0; | |
97 | return inpf->inpbitmap; | |
98 | } | |
99 | ||
100 | /* Set the bitmap returned by get_lang_bitmap. The only legitimate | |
101 | callers of this function are read_input_list & read_state_*. */ | |
102 | static inline void | |
103 | set_lang_bitmap (input_file* inpf, lang_bitmap n) | |
104 | { | |
105 | gcc_assert (inpf); | |
106 | inpf->inpbitmap = n; | |
107 | } | |
108 | ||
109 | /* Vector of per-language directories. */ | |
110 | extern const char **lang_dir_names; | |
111 | extern size_t num_lang_dirs; | |
112 | ||
065ae611 | 113 | /* Data types handed around within, but opaque to, the lexer and parser. */ |
b453c95f GK |
114 | typedef struct pair *pair_p; |
115 | typedef struct type *type_p; | |
e5cfc29f | 116 | typedef const struct type *const_type_p; |
065ae611 | 117 | typedef struct options *options_p; |
e2500fed | 118 | |
e2500fed GK |
119 | /* Variables used to communicate between the lexer and the parser. */ |
120 | extern int lexer_toplevel_done; | |
121 | extern struct fileloc lexer_line; | |
122 | ||
412dc29d BS |
123 | /* Various things, organized as linked lists, needed both in |
124 | gengtype.c & in gengtype-state.c files. */ | |
125 | extern pair_p typedefs; | |
126 | extern type_p structures; | |
412dc29d BS |
127 | extern pair_p variables; |
128 | ||
9aa54cc9 | 129 | /* An enum for distinguishing GGC vs PCH. */ |
412dc29d | 130 | |
9aa54cc9 DM |
131 | enum write_types_kinds |
132 | { | |
133 | WTK_GGC, | |
134 | WTK_PCH, | |
135 | ||
136 | NUM_WTK | |
137 | }; | |
412dc29d BS |
138 | |
139 | /* Discrimating kind of types we can understand. */ | |
140 | ||
141 | enum typekind { | |
142 | TYPE_NONE=0, /* Never used, so zeroed memory is invalid. */ | |
9771b263 DN |
143 | TYPE_UNDEFINED, /* We have not yet seen a definition for this type. |
144 | If a type is still undefined when generating code, | |
145 | an error will be generated. */ | |
412dc29d BS |
146 | TYPE_SCALAR, /* Scalar types like char. */ |
147 | TYPE_STRING, /* The string type. */ | |
148 | TYPE_STRUCT, /* Type for GTY-ed structs. */ | |
149 | TYPE_UNION, /* Type for GTY-ed discriminated unions. */ | |
150 | TYPE_POINTER, /* Pointer type to GTY-ed type. */ | |
151 | TYPE_ARRAY, /* Array of GTY-ed types. */ | |
152 | TYPE_LANG_STRUCT, /* GCC front-end language specific structs. | |
153 | Various languages may have homonymous but | |
154 | different structs. */ | |
0823efed DN |
155 | TYPE_USER_STRUCT /* User defined type. Walkers and markers for |
156 | this type are assumed to be provided by the | |
157 | user. */ | |
412dc29d BS |
158 | }; |
159 | ||
160 | /* Discriminating kind for options. */ | |
161 | enum option_kind { | |
162 | OPTION_NONE=0, /* Never used, so zeroed memory is invalid. */ | |
163 | OPTION_STRING, /* A string-valued option. Most options are | |
164 | strings. */ | |
165 | OPTION_TYPE, /* A type-valued option. */ | |
166 | OPTION_NESTED /* Option data for 'nested_ptr'. */ | |
167 | }; | |
168 | ||
169 | ||
170 | /* A way to pass data through to the output end. */ | |
171 | struct options { | |
172 | struct options *next; /* next option of the same pair. */ | |
173 | const char *name; /* GTY option name. */ | |
174 | enum option_kind kind; /* discriminating option kind. */ | |
175 | union { | |
176 | const char* string; /* When OPTION_STRING. */ | |
177 | type_p type; /* When OPTION_TYPE. */ | |
178 | struct nested_ptr_data* nested; /* when OPTION_NESTED. */ | |
179 | } info; | |
180 | }; | |
181 | ||
182 | ||
183 | /* Option data for the 'nested_ptr' option. */ | |
184 | struct nested_ptr_data { | |
185 | type_p type; | |
186 | const char *convert_to; | |
187 | const char *convert_from; | |
188 | }; | |
189 | ||
190 | /* Some functions to create various options structures with name NAME | |
191 | and info INFO. NEXT is the next option in the chain. */ | |
192 | ||
193 | /* Create a string option. */ | |
194 | options_p create_string_option (options_p next, const char* name, | |
195 | const char* info); | |
196 | ||
197 | /* Create a type option. */ | |
198 | options_p create_type_option (options_p next, const char* name, | |
199 | type_p info); | |
200 | ||
201 | /* Create a nested option. */ | |
202 | options_p create_nested_option (options_p next, const char* name, | |
203 | struct nested_ptr_data* info); | |
204 | ||
205 | /* Create a nested pointer option. */ | |
92cf93d9 JW |
206 | options_p create_nested_ptr_option (options_p next, type_p t, |
207 | const char *to, const char *from); | |
412dc29d BS |
208 | |
209 | /* A name and a type. */ | |
210 | struct pair { | |
211 | pair_p next; /* The next pair in the linked list. */ | |
212 | const char *name; /* The defined name. */ | |
213 | type_p type; /* Its GTY-ed type. */ | |
214 | struct fileloc line; /* The file location. */ | |
215 | options_p opt; /* GTY options, as a linked list. */ | |
216 | }; | |
217 | ||
218 | /* Usage information for GTY-ed types. Gengtype has to care only of | |
219 | used GTY-ed types. Types are initially unused, and their usage is | |
220 | computed by set_gc_used_type and set_gc_used functions. */ | |
221 | ||
222 | enum gc_used_enum { | |
223 | ||
224 | /* We need that zeroed types are initially unused. */ | |
225 | GC_UNUSED=0, | |
226 | ||
227 | /* The GTY-ed type is used, e.g by a GTY-ed variable or a field | |
228 | inside a GTY-ed used type. */ | |
229 | GC_USED, | |
230 | ||
231 | /* For GTY-ed structures whose definitions we haven't seen so far | |
232 | when we encounter a pointer to it that is annotated with | |
233 | ``maybe_undef''. If after reading in everything we don't have | |
234 | source file information for it, we assume that it never has been | |
235 | defined. */ | |
236 | GC_MAYBE_POINTED_TO, | |
237 | ||
238 | /* For known GTY-ed structures which are pointed to by GTY-ed | |
239 | variables or fields. */ | |
240 | GC_POINTED_TO | |
241 | }; | |
242 | ||
412dc29d BS |
243 | /* Our type structure describes all types handled by gengtype. */ |
244 | struct type { | |
245 | /* Discriminating kind, cannot be TYPE_NONE. */ | |
246 | enum typekind kind; | |
247 | ||
248 | /* For top-level structs or unions, the 'next' field links the | |
63f5d5b8 TS |
249 | global list 'structures'; for lang_structs, their homonymous structs are |
250 | linked using this 'next' field. The homonymous list starts at the | |
251 | s.lang_struct field of the lang_struct. See the new_structure function | |
252 | for details. This is tricky! */ | |
412dc29d BS |
253 | type_p next; |
254 | ||
255 | /* State number used when writing & reading the persistent state. A | |
256 | type with a positive number has already been written. For ease | |
257 | of debugging, newly allocated types have a unique negative | |
258 | number. */ | |
259 | int state_number; | |
260 | ||
261 | /* Each GTY-ed type which is pointed to by some GTY-ed type knows | |
262 | the GTY pointer type pointing to it. See create_pointer | |
263 | function. */ | |
264 | type_p pointer_to; | |
265 | ||
266 | /* Type usage information, computed by set_gc_used_type and | |
267 | set_gc_used functions. */ | |
268 | enum gc_used_enum gc_used; | |
269 | ||
270 | /* The following union is discriminated by the 'kind' field above. */ | |
271 | union { | |
272 | /* TYPE__NONE is impossible. */ | |
273 | ||
274 | /* when TYPE_POINTER: */ | |
275 | type_p p; | |
276 | ||
277 | /* when TYPE_STRUCT or TYPE_UNION or TYPE_LANG_STRUCT, we have an | |
278 | aggregate type containing fields: */ | |
279 | struct { | |
5764ee3c | 280 | const char *tag; /* the aggregate tag, if any. */ |
412dc29d BS |
281 | struct fileloc line; /* the source location. */ |
282 | pair_p fields; /* the linked list of fields. */ | |
283 | options_p opt; /* the GTY options if any. */ | |
284 | lang_bitmap bitmap; /* the set of front-end languages | |
285 | using that GTY-ed aggregate. */ | |
286 | /* For TYPE_LANG_STRUCT, the lang_struct field gives the first | |
287 | element of a linked list of homonymous struct or union types. | |
288 | Within this list, each homonymous type has as its lang_struct | |
289 | field the original TYPE_LANG_STRUCT type. This is a dirty | |
290 | trick, see the new_structure function for details. */ | |
291 | type_p lang_struct; | |
18aa2b04 DM |
292 | |
293 | type_p base_class; /* the parent class, if any. */ | |
32fe5271 DM |
294 | |
295 | /* The following two fields are not serialized in state files, and | |
296 | are instead reconstructed on load. */ | |
297 | ||
298 | /* The head of a singly-linked list of immediate descendents in | |
299 | the inheritance hierarchy. */ | |
300 | type_p first_subclass; | |
301 | /* The next in that list. */ | |
302 | type_p next_sibling_class; | |
9aa54cc9 DM |
303 | |
304 | /* Have we already written ggc/pch user func for ptr to this? | |
305 | (in write_user_func_for_structure_ptr). */ | |
306 | bool wrote_user_func_for_ptr[NUM_WTK]; | |
412dc29d BS |
307 | } s; |
308 | ||
309 | /* when TYPE_SCALAR: */ | |
310 | bool scalar_is_char; | |
311 | ||
312 | /* when TYPE_ARRAY: */ | |
313 | struct { | |
314 | type_p p; /* The array component type. */ | |
315 | const char *len; /* The string if any giving its length. */ | |
316 | } a; | |
317 | ||
412dc29d BS |
318 | } u; |
319 | }; | |
320 | ||
321 | /* The one and only TYPE_STRING. */ | |
322 | extern struct type string_type; | |
323 | ||
324 | /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are | |
325 | set early in main. */ | |
326 | extern struct type scalar_nonchar; | |
327 | extern struct type scalar_char; | |
328 | ||
329 | /* Test if a type is a union, either a plain one or a language | |
330 | specific one. */ | |
0823efed DN |
331 | #define UNION_P(x) \ |
332 | ((x)->kind == TYPE_UNION \ | |
333 | || ((x)->kind == TYPE_LANG_STRUCT \ | |
334 | && (x)->u.s.lang_struct->kind == TYPE_UNION)) | |
412dc29d BS |
335 | |
336 | /* Test if a type is a union or a structure, perhaps a language | |
337 | specific one. */ | |
0823efed DN |
338 | static inline bool |
339 | union_or_struct_p (enum typekind kind) | |
340 | { | |
341 | return (kind == TYPE_UNION | |
342 | || kind == TYPE_STRUCT | |
343 | || kind == TYPE_LANG_STRUCT | |
344 | || kind == TYPE_USER_STRUCT); | |
345 | } | |
412dc29d | 346 | |
0823efed DN |
347 | static inline bool |
348 | union_or_struct_p (const_type_p x) | |
349 | { | |
350 | return union_or_struct_p (x->kind); | |
351 | } | |
412dc29d | 352 | |
fbb20b29 BS |
353 | /* Give the file location of a type, if any. */ |
354 | static inline struct fileloc* | |
355 | type_fileloc (type_p t) | |
356 | { | |
357 | if (!t) | |
358 | return NULL; | |
0823efed | 359 | if (union_or_struct_p (t)) |
fbb20b29 | 360 | return &t->u.s.line; |
fbb20b29 BS |
361 | return NULL; |
362 | } | |
363 | ||
f8ed6dc5 JS |
364 | /* Structure representing an output file. */ |
365 | struct outf | |
366 | { | |
367 | struct outf *next; | |
368 | const char *name; | |
369 | size_t buflength; | |
370 | size_t bufused; | |
371 | char *buf; | |
372 | }; | |
373 | typedef struct outf *outf_p; | |
374 | ||
375 | /* The list of output files. */ | |
376 | extern outf_p output_files; | |
377 | ||
378 | /* The output header file that is included into pretty much every | |
379 | source file. */ | |
380 | extern outf_p header_file; | |
381 | ||
382 | /* Print, like fprintf, to O. No-op if O is NULL. */ | |
383 | void | |
384 | oprintf (outf_p o, const char *S, ...) | |
385 | ATTRIBUTE_PRINTF_2; | |
386 | ||
387 | /* An output file, suitable for definitions, that can see declarations | |
14c4815e BS |
388 | made in INPF and is linked into every language that uses INPF. May |
389 | return NULL in plugin mode. The INPF argument is almost const, but | |
390 | since the result is cached in its inpoutf field it cannot be | |
391 | declared const. */ | |
392 | outf_p get_output_file_with_visibility (input_file* inpf); | |
393 | ||
394 | /* The name of an output file, suitable for definitions, that can see | |
395 | declarations made in INPF and is linked into every language that | |
396 | uses INPF. May return NULL. */ | |
397 | const char *get_output_file_name (input_file *inpf); | |
398 | ||
f8ed6dc5 JS |
399 | |
400 | /* Source directory. */ | |
401 | extern const char *srcdir; /* (-S) program argument. */ | |
402 | ||
403 | /* Length of srcdir name. */ | |
404 | extern size_t srcdir_len; | |
405 | ||
406 | /* Variable used for reading and writing the state. */ | |
407 | extern const char *read_state_filename; /* (-r) program argument. */ | |
408 | extern const char *write_state_filename; /* (-w) program argument. */ | |
409 | ||
92724e1d BS |
410 | /* Functions reading and writing the entire gengtype state, called from |
411 | main, and implemented in file gengtype-state.c. */ | |
412 | void read_state (const char* path); | |
413 | /* Write the state, and update the state_number field in types. */ | |
414 | void write_state (const char* path); | |
415 | ||
416 | ||
e2500fed | 417 | /* Print an error message. */ |
b8698a0f | 418 | extern void error_at_line |
e1b793e7 | 419 | (const struct fileloc *pos, const char *msg, ...) ATTRIBUTE_PRINTF_2; |
e2500fed GK |
420 | |
421 | /* Constructor routines for types. */ | |
3d7aafde | 422 | extern void do_typedef (const char *s, type_p t, struct fileloc *pos); |
95161faf | 423 | extern void do_scalar_typedef (const char *s, struct fileloc *pos); |
3d7aafde | 424 | extern type_p resolve_typedef (const char *s, struct fileloc *pos); |
32fe5271 | 425 | extern void add_subclass (type_p base, type_p subclass); |
0823efed | 426 | extern type_p new_structure (const char *name, enum typekind kind, |
0f01f026 | 427 | struct fileloc *pos, pair_p fields, |
18aa2b04 | 428 | options_p o, type_p base); |
9771b263 | 429 | type_p create_user_defined_type (const char *, struct fileloc *); |
0823efed | 430 | extern type_p find_structure (const char *s, enum typekind kind); |
95161faf | 431 | extern type_p create_scalar_type (const char *name); |
3d7aafde AJ |
432 | extern type_p create_pointer (type_p t); |
433 | extern type_p create_array (type_p t, const char *len); | |
e1b793e7 BS |
434 | extern pair_p create_field_at (pair_p next, type_p type, |
435 | const char *name, options_p opt, | |
436 | struct fileloc *pos); | |
01d419ae | 437 | extern pair_p nreverse_pairs (pair_p list); |
3d7aafde AJ |
438 | extern type_p adjust_field_type (type_p, options_p); |
439 | extern void note_variable (const char *s, type_p t, options_p o, | |
440 | struct fileloc *pos); | |
e2500fed | 441 | |
01d419ae ZW |
442 | /* Lexer and parser routines. */ |
443 | extern int yylex (const char **yylval); | |
444 | extern void yybegin (const char *fname); | |
445 | extern void yyend (void); | |
3d7aafde | 446 | extern void parse_file (const char *name); |
01d419ae ZW |
447 | extern bool hit_error; |
448 | ||
449 | /* Token codes. */ | |
313465bb DN |
450 | enum gty_token |
451 | { | |
452 | EOF_TOKEN = 0, | |
453 | ||
454 | /* Per standard convention, codes in the range (0, UCHAR_MAX] | |
455 | represent single characters with those character codes. */ | |
456 | CHAR_TOKEN_OFFSET = UCHAR_MAX + 1, | |
457 | GTY_TOKEN = CHAR_TOKEN_OFFSET, | |
458 | TYPEDEF, | |
459 | EXTERN, | |
460 | STATIC, | |
461 | UNION, | |
462 | STRUCT, | |
463 | ENUM, | |
313465bb DN |
464 | ELLIPSIS, |
465 | PTR_ALIAS, | |
466 | NESTED_PTR, | |
467 | USER_GTY, | |
313465bb DN |
468 | NUM, |
469 | SCALAR, | |
470 | ID, | |
471 | STRING, | |
472 | CHAR, | |
473 | ARRAY, | |
474 | IGNORABLE_CXX_KEYWORD, | |
475 | ||
476 | /* print_token assumes that any token >= FIRST_TOKEN_WITH_VALUE may have | |
477 | a meaningful value to be printed. */ | |
63f5d5b8 | 478 | FIRST_TOKEN_WITH_VALUE = USER_GTY |
313465bb | 479 | }; |
f8ed6dc5 JS |
480 | |
481 | ||
1d32bbcd BS |
482 | /* Level for verbose messages, e.g. output file generation... */ |
483 | extern int verbosity_level; /* (-v) program argument. */ | |
484 | ||
f8ed6dc5 JS |
485 | /* For debugging purposes we provide two flags. */ |
486 | ||
487 | /* Dump everything to understand gengtype's state. Might be useful to | |
488 | gengtype users. */ | |
489 | extern int do_dump; /* (-d) program argument. */ | |
490 | ||
491 | /* Trace the execution by many DBGPRINTF (with the position inside | |
492 | gengtype source code). Only useful to debug gengtype itself. */ | |
493 | extern int do_debug; /* (-D) program argument. */ | |
494 | ||
f8ed6dc5 JS |
495 | #define DBGPRINTF(Fmt,...) do {if (do_debug) \ |
496 | fprintf (stderr, "%s:%d: " Fmt "\n", \ | |
497 | lbasename (__FILE__),__LINE__, ##__VA_ARGS__);} while (0) | |
498 | void dbgprint_count_type_at (const char *, int, const char *, type_p); | |
499 | #define DBGPRINT_COUNT_TYPE(Msg,Ty) do {if (do_debug) \ | |
500 | dbgprint_count_type_at (__FILE__, __LINE__, Msg, Ty);}while (0) | |
f8ed6dc5 | 501 | |
52a7fb3c DM |
502 | #define FOR_ALL_INHERITED_FIELDS(TYPE, FIELD_VAR) \ |
503 | for (type_p sub = (TYPE); sub; sub = sub->u.s.base_class) \ | |
504 | for (FIELD_VAR = sub->u.s.fields; FIELD_VAR; FIELD_VAR = FIELD_VAR->next) | |
505 | ||
506 | extern bool | |
507 | opts_have (options_p opts, const char *str); | |
508 | ||
509 | ||
065ae611 | 510 | #endif |