1 /* Process source files and output type information.
2 Copyright (C) 2002-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
20 #ifndef GCC_GENGTYPE_H
21 #define GCC_GENGTYPE_H
23 #define obstack_chunk_alloc xmalloc
24 #define obstack_chunk_free free
25 #define OBSTACK_CHUNK_SIZE 0
27 /* Sets of accepted source languages like C, C++, Ada... are
28 represented by a bitmap. */
29 typedef unsigned lang_bitmap
;
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. */
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
43 typedef struct input_file_st input_file
;
45 /* A file position, mostly for error messages.
46 The FILE element may be compared using pointer equality. */
49 const input_file
*file
;
54 /* Table of all input files and its size. */
55 extern const input_file
** gt_files
;
56 extern size_t num_gt_files
;
58 /* Table of headers to be included in gtype-desc.c that are generated
59 during the build. These are identified as "./<filename>.h". */
60 extern const char **build_headers
;
61 extern size_t num_build_headers
;
63 /* A number of places use the name of this "gengtype.c" 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. */
67 extern input_file
* this_file
;
68 extern input_file
* system_h_file
;
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
73 input_file
* input_file_by_name (const char* name
);
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. */
77 const char *get_file_srcdir_relative_path (const input_file
*inpf
);
79 /* Get the name of an input file. */
80 static inline const char*
81 get_input_file_name (const input_file
*inpf
)
88 /* Return a bitmap which has bit `1 << BASE_FILE_<lang>' set iff
89 INPUT_FILE is used by <lang>.
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.
97 static inline lang_bitmap
98 get_lang_bitmap (const input_file
* inpf
)
102 return inpf
->inpbitmap
;
105 /* Set the bitmap returned by get_lang_bitmap. The only legitimate
106 callers of this function are read_input_list & read_state_*. */
108 set_lang_bitmap (input_file
* inpf
, lang_bitmap n
)
114 /* Vector of per-language directories. */
115 extern const char **lang_dir_names
;
116 extern size_t num_lang_dirs
;
118 /* Data types handed around within, but opaque to, the lexer and parser. */
119 typedef struct pair
*pair_p
;
120 typedef struct type
*type_p
;
121 typedef const struct type
*const_type_p
;
122 typedef struct options
*options_p
;
124 /* Variables used to communicate between the lexer and the parser. */
125 extern int lexer_toplevel_done
;
126 extern struct fileloc lexer_line
;
128 /* Various things, organized as linked lists, needed both in
129 gengtype.c & in gengtype-state.c files. */
130 extern pair_p typedefs
;
131 extern type_p structures
;
132 extern pair_p variables
;
134 /* An enum for distinguishing GGC vs PCH. */
136 enum write_types_kinds
144 /* Discrimating kind of types we can understand. */
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_LANG_STRUCT
, /* GCC front-end language specific structs.
158 Various languages may have homonymous but
159 different structs. */
160 TYPE_USER_STRUCT
/* User defined type. Walkers and markers for
161 this type are assumed to be provided by the
165 /* Discriminating kind for options. */
167 OPTION_NONE
=0, /* Never used, so zeroed memory is invalid. */
168 OPTION_STRING
, /* A string-valued option. Most options are
170 OPTION_TYPE
, /* A type-valued option. */
171 OPTION_NESTED
/* Option data for 'nested_ptr'. */
175 /* A way to pass data through to the output end. */
177 struct options
*next
; /* next option of the same pair. */
178 const char *name
; /* GTY option name. */
179 enum option_kind kind
; /* discriminating option kind. */
181 const char* string
; /* When OPTION_STRING. */
182 type_p type
; /* When OPTION_TYPE. */
183 struct nested_ptr_data
* nested
; /* when OPTION_NESTED. */
188 /* Option data for the 'nested_ptr' option. */
189 struct nested_ptr_data
{
191 const char *convert_to
;
192 const char *convert_from
;
195 /* Some functions to create various options structures with name NAME
196 and info INFO. NEXT is the next option in the chain. */
198 /* Create a string option. */
199 options_p
create_string_option (options_p next
, const char* name
,
202 /* Create a type option. */
203 options_p
create_type_option (options_p next
, const char* name
,
206 /* Create a nested option. */
207 options_p
create_nested_option (options_p next
, const char* name
,
208 struct nested_ptr_data
* info
);
210 /* Create a nested pointer option. */
211 options_p
create_nested_ptr_option (options_p next
, type_p t
,
212 const char *to
, const char *from
);
214 /* A name and a type. */
216 pair_p next
; /* The next pair in the linked list. */
217 const char *name
; /* The defined name. */
218 type_p type
; /* Its GTY-ed type. */
219 struct fileloc line
; /* The file location. */
220 options_p opt
; /* GTY options, as a linked list. */
223 /* Usage information for GTY-ed types. Gengtype has to care only of
224 used GTY-ed types. Types are initially unused, and their usage is
225 computed by set_gc_used_type and set_gc_used functions. */
229 /* We need that zeroed types are initially unused. */
232 /* The GTY-ed type is used, e.g by a GTY-ed variable or a field
233 inside a GTY-ed used type. */
236 /* For GTY-ed structures whose definitions we haven't seen so far
237 when we encounter a pointer to it that is annotated with
238 ``maybe_undef''. If after reading in everything we don't have
239 source file information for it, we assume that it never has been
243 /* For known GTY-ed structures which are pointed to by GTY-ed
244 variables or fields. */
248 /* Our type structure describes all types handled by gengtype. */
250 /* Discriminating kind, cannot be TYPE_NONE. */
253 /* For top-level structs or unions, the 'next' field links the
254 global list 'structures'; for lang_structs, their homonymous structs are
255 linked using this 'next' field. The homonymous list starts at the
256 s.lang_struct field of the lang_struct. See the new_structure function
257 for details. This is tricky! */
260 /* State number used when writing & reading the persistent state. A
261 type with a positive number has already been written. For ease
262 of debugging, newly allocated types have a unique negative
266 /* Each GTY-ed type which is pointed to by some GTY-ed type knows
267 the GTY pointer type pointing to it. See create_pointer
271 /* Type usage information, computed by set_gc_used_type and
272 set_gc_used functions. */
273 enum gc_used_enum gc_used
;
275 /* The following union is discriminated by the 'kind' field above. */
277 /* TYPE__NONE is impossible. */
279 /* when TYPE_POINTER: */
282 /* when TYPE_STRUCT or TYPE_UNION or TYPE_LANG_STRUCT, we have an
283 aggregate type containing fields: */
285 const char *tag
; /* the aggregate tag, if any. */
286 struct fileloc line
; /* the source location. */
287 pair_p fields
; /* the linked list of fields. */
288 options_p opt
; /* the GTY options if any. */
289 lang_bitmap bitmap
; /* the set of front-end languages
290 using that GTY-ed aggregate. */
291 /* For TYPE_LANG_STRUCT, the lang_struct field gives the first
292 element of a linked list of homonymous struct or union types.
293 Within this list, each homonymous type has as its lang_struct
294 field the original TYPE_LANG_STRUCT type. This is a dirty
295 trick, see the new_structure function for details. */
298 type_p base_class
; /* the parent class, if any. */
300 /* The following two fields are not serialized in state files, and
301 are instead reconstructed on load. */
303 /* The head of a singly-linked list of immediate descendents in
304 the inheritance hierarchy. */
305 type_p first_subclass
;
306 /* The next in that list. */
307 type_p next_sibling_class
;
309 /* Have we already written ggc/pch user func for ptr to this?
310 (in write_user_func_for_structure_ptr). */
311 bool wrote_user_func_for_ptr
[NUM_WTK
];
314 /* when TYPE_SCALAR: */
317 /* when TYPE_ARRAY: */
319 type_p p
; /* The array component type. */
320 const char *len
; /* The string if any giving its length. */
326 /* The one and only TYPE_STRING. */
327 extern struct type string_type
;
329 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
330 set early in main. */
331 extern struct type scalar_nonchar
;
332 extern struct type scalar_char
;
334 /* Test if a type is a union, either a plain one or a language
337 ((x)->kind == TYPE_UNION \
338 || ((x)->kind == TYPE_LANG_STRUCT \
339 && (x)->u.s.lang_struct->kind == TYPE_UNION))
341 /* Test if a type is a union or a structure, perhaps a language
344 union_or_struct_p (enum typekind kind
)
346 return (kind
== TYPE_UNION
347 || kind
== TYPE_STRUCT
348 || kind
== TYPE_LANG_STRUCT
349 || kind
== TYPE_USER_STRUCT
);
353 union_or_struct_p (const_type_p x
)
355 return union_or_struct_p (x
->kind
);
358 /* Give the file location of a type, if any. */
359 static inline struct fileloc
*
360 type_fileloc (type_p t
)
364 if (union_or_struct_p (t
))
369 /* Structure representing an output file. */
378 typedef struct outf
*outf_p
;
380 /* The list of output files. */
381 extern outf_p output_files
;
383 /* The output header file that is included into pretty much every
385 extern outf_p header_file
;
387 /* Print, like fprintf, to O. No-op if O is NULL. */
389 oprintf (outf_p o
, const char *S
, ...)
392 /* An output file, suitable for definitions, that can see declarations
393 made in INPF and is linked into every language that uses INPF. May
394 return NULL in plugin mode. The INPF argument is almost const, but
395 since the result is cached in its inpoutf field it cannot be
397 outf_p
get_output_file_with_visibility (input_file
* inpf
);
399 /* The name of an output file, suitable for definitions, that can see
400 declarations made in INPF and is linked into every language that
401 uses INPF. May return NULL. */
402 const char *get_output_file_name (input_file
*inpf
);
405 /* Source directory. */
406 extern const char *srcdir
; /* (-S) program argument. */
408 /* Length of srcdir name. */
409 extern size_t srcdir_len
;
411 /* Variable used for reading and writing the state. */
412 extern const char *read_state_filename
; /* (-r) program argument. */
413 extern const char *write_state_filename
; /* (-w) program argument. */
415 /* Functions reading and writing the entire gengtype state, called from
416 main, and implemented in file gengtype-state.c. */
417 void read_state (const char* path
);
418 /* Write the state, and update the state_number field in types. */
419 void write_state (const char* path
);
422 /* Print an error message. */
423 extern void error_at_line
424 (const struct fileloc
*pos
, const char *msg
, ...) ATTRIBUTE_PRINTF_2
;
426 /* Constructor routines for types. */
427 extern void do_typedef (const char *s
, type_p t
, struct fileloc
*pos
);
428 extern void do_scalar_typedef (const char *s
, struct fileloc
*pos
);
429 extern type_p
resolve_typedef (const char *s
, struct fileloc
*pos
);
430 extern void add_subclass (type_p base
, type_p subclass
);
431 extern type_p
new_structure (const char *name
, enum typekind kind
,
432 struct fileloc
*pos
, pair_p fields
,
433 options_p o
, type_p base
);
434 type_p
create_user_defined_type (const char *, struct fileloc
*);
435 extern type_p
find_structure (const char *s
, enum typekind kind
);
436 extern type_p
create_scalar_type (const char *name
);
437 extern type_p
create_pointer (type_p t
);
438 extern type_p
create_array (type_p t
, const char *len
);
439 extern pair_p
create_field_at (pair_p next
, type_p type
,
440 const char *name
, options_p opt
,
441 struct fileloc
*pos
);
442 extern pair_p
nreverse_pairs (pair_p list
);
443 extern type_p
adjust_field_type (type_p
, options_p
);
444 extern void note_variable (const char *s
, type_p t
, options_p o
,
445 struct fileloc
*pos
);
447 /* Lexer and parser routines. */
448 extern int yylex (const char **yylval
);
449 extern void yybegin (const char *fname
);
450 extern void yyend (void);
451 extern void parse_file (const char *name
);
452 extern bool hit_error
;
459 /* Per standard convention, codes in the range (0, UCHAR_MAX]
460 represent single characters with those character codes. */
461 CHAR_TOKEN_OFFSET
= UCHAR_MAX
+ 1,
462 GTY_TOKEN
= CHAR_TOKEN_OFFSET
,
479 IGNORABLE_CXX_KEYWORD
,
481 /* print_token assumes that any token >= FIRST_TOKEN_WITH_VALUE may have
482 a meaningful value to be printed. */
483 FIRST_TOKEN_WITH_VALUE
= USER_GTY
487 /* Level for verbose messages, e.g. output file generation... */
488 extern int verbosity_level
; /* (-v) program argument. */
490 /* For debugging purposes we provide two flags. */
492 /* Dump everything to understand gengtype's state. Might be useful to
494 extern int do_dump
; /* (-d) program argument. */
496 /* Trace the execution by many DBGPRINTF (with the position inside
497 gengtype source code). Only useful to debug gengtype itself. */
498 extern int do_debug
; /* (-D) program argument. */
500 #define DBGPRINTF(Fmt,...) do {if (do_debug) \
501 fprintf (stderr, "%s:%d: " Fmt "\n", \
502 lbasename (__FILE__),__LINE__, ##__VA_ARGS__);} while (0)
503 void dbgprint_count_type_at (const char *, int, const char *, type_p
);
504 #define DBGPRINT_COUNT_TYPE(Msg,Ty) do {if (do_debug) \
505 dbgprint_count_type_at (__FILE__, __LINE__, Msg, Ty);}while (0)
507 #define FOR_ALL_INHERITED_FIELDS(TYPE, FIELD_VAR) \
508 for (type_p sub = (TYPE); sub; sub = sub->u.s.base_class) \
509 for (FIELD_VAR = sub->u.s.fields; FIELD_VAR; FIELD_VAR = FIELD_VAR->next)
512 opts_have (options_p opts
, const char *str
);