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