1 /* Process source files and output type information.
2 Copyright (C) 2002-2014 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 /* 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
;
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
68 input_file
* input_file_by_name (const char* name
);
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
);
74 /* Get the name of an input file. */
75 static inline const char*
76 get_input_file_name (const input_file
*inpf
)
83 /* Return a bitmap which has bit `1 << BASE_FILE_<lang>' set iff
84 INPUT_FILE is used by <lang>.
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.
92 static inline lang_bitmap
93 get_lang_bitmap (const input_file
* inpf
)
97 return inpf
->inpbitmap
;
100 /* Set the bitmap returned by get_lang_bitmap. The only legitimate
101 callers of this function are read_input_list & read_state_*. */
103 set_lang_bitmap (input_file
* inpf
, lang_bitmap n
)
109 /* Vector of per-language directories. */
110 extern const char **lang_dir_names
;
111 extern size_t num_lang_dirs
;
113 /* Data types handed around within, but opaque to, the lexer and parser. */
114 typedef struct pair
*pair_p
;
115 typedef struct type
*type_p
;
116 typedef const struct type
*const_type_p
;
117 typedef struct options
*options_p
;
119 /* Variables used to communicate between the lexer and the parser. */
120 extern int lexer_toplevel_done
;
121 extern struct fileloc lexer_line
;
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
;
127 extern type_p param_structs
;
128 extern pair_p variables
;
130 /* An enum for distinguishing GGC vs PCH. */
132 enum write_types_kinds
140 /* Discrimating kind of types we can understand. */
143 TYPE_NONE
=0, /* Never used, so zeroed memory is invalid. */
144 TYPE_UNDEFINED
, /* We have not yet seen a definition for this type.
145 If a type is still undefined when generating code,
146 an error will be generated. */
147 TYPE_SCALAR
, /* Scalar types like char. */
148 TYPE_STRING
, /* The string type. */
149 TYPE_STRUCT
, /* Type for GTY-ed structs. */
150 TYPE_UNION
, /* Type for GTY-ed discriminated unions. */
151 TYPE_POINTER
, /* Pointer type to GTY-ed type. */
152 TYPE_ARRAY
, /* Array of GTY-ed types. */
153 TYPE_LANG_STRUCT
, /* GCC front-end language specific structs.
154 Various languages may have homonymous but
155 different structs. */
156 TYPE_PARAM_STRUCT
, /* Type for parametrized structs, e.g. hash_t
157 hash-tables, ... See (param_is, use_param,
158 param1_is, param2_is,... use_param1,
159 use_param_2,... use_params) GTY
161 TYPE_USER_STRUCT
/* User defined type. Walkers and markers for
162 this type are assumed to be provided by the
166 /* Discriminating kind for options. */
168 OPTION_NONE
=0, /* Never used, so zeroed memory is invalid. */
169 OPTION_STRING
, /* A string-valued option. Most options are
171 OPTION_TYPE
, /* A type-valued option. */
172 OPTION_NESTED
/* Option data for 'nested_ptr'. */
176 /* A way to pass data through to the output end. */
178 struct options
*next
; /* next option of the same pair. */
179 const char *name
; /* GTY option name. */
180 enum option_kind kind
; /* discriminating option kind. */
182 const char* string
; /* When OPTION_STRING. */
183 type_p type
; /* When OPTION_TYPE. */
184 struct nested_ptr_data
* nested
; /* when OPTION_NESTED. */
189 /* Option data for the 'nested_ptr' option. */
190 struct nested_ptr_data
{
192 const char *convert_to
;
193 const char *convert_from
;
196 /* Some functions to create various options structures with name NAME
197 and info INFO. NEXT is the next option in the chain. */
199 /* Create a string option. */
200 options_p
create_string_option (options_p next
, const char* name
,
203 /* Create a type option. */
204 options_p
create_type_option (options_p next
, const char* name
,
207 /* Create a nested option. */
208 options_p
create_nested_option (options_p next
, const char* name
,
209 struct nested_ptr_data
* info
);
211 /* Create a nested pointer option. */
212 options_p
create_nested_ptr_option (options_p
, type_p t
,
213 const char *from
, const char *to
);
215 /* A name and a type. */
217 pair_p next
; /* The next pair in the linked list. */
218 const char *name
; /* The defined name. */
219 type_p type
; /* Its GTY-ed type. */
220 struct fileloc line
; /* The file location. */
221 options_p opt
; /* GTY options, as a linked list. */
224 /* Usage information for GTY-ed types. Gengtype has to care only of
225 used GTY-ed types. Types are initially unused, and their usage is
226 computed by set_gc_used_type and set_gc_used functions. */
230 /* We need that zeroed types are initially unused. */
233 /* The GTY-ed type is used, e.g by a GTY-ed variable or a field
234 inside a GTY-ed used type. */
237 /* For GTY-ed structures whose definitions we haven't seen so far
238 when we encounter a pointer to it that is annotated with
239 ``maybe_undef''. If after reading in everything we don't have
240 source file information for it, we assume that it never has been
244 /* For known GTY-ed structures which are pointed to by GTY-ed
245 variables or fields. */
249 /* We can have at most ten type parameters in parameterized structures. */
252 /* Our type structure describes all types handled by gengtype. */
254 /* Discriminating kind, cannot be TYPE_NONE. */
257 /* For top-level structs or unions, the 'next' field links the
258 global list 'structures' or 'param_structs'; for lang_structs,
259 their homonymous structs are linked using this 'next' field. The
260 homonymous list starts at the s.lang_struct field of the
261 lang_struct. See the new_structure function for details. This is
265 /* State number used when writing & reading the persistent state. A
266 type with a positive number has already been written. For ease
267 of debugging, newly allocated types have a unique negative
271 /* Each GTY-ed type which is pointed to by some GTY-ed type knows
272 the GTY pointer type pointing to it. See create_pointer
276 /* Type usage information, computed by set_gc_used_type and
277 set_gc_used functions. */
278 enum gc_used_enum gc_used
;
280 /* The following union is discriminated by the 'kind' field above. */
282 /* TYPE__NONE is impossible. */
284 /* when TYPE_POINTER: */
287 /* when TYPE_STRUCT or TYPE_UNION or TYPE_LANG_STRUCT, we have an
288 aggregate type containing fields: */
290 const char *tag
; /* the aggragate tag, if any. */
291 struct fileloc line
; /* the source location. */
292 pair_p fields
; /* the linked list of fields. */
293 options_p opt
; /* the GTY options if any. */
294 lang_bitmap bitmap
; /* the set of front-end languages
295 using that GTY-ed aggregate. */
296 /* For TYPE_LANG_STRUCT, the lang_struct field gives the first
297 element of a linked list of homonymous struct or union types.
298 Within this list, each homonymous type has as its lang_struct
299 field the original TYPE_LANG_STRUCT type. This is a dirty
300 trick, see the new_structure function for details. */
303 type_p base_class
; /* the parent class, if any. */
305 /* The following two fields are not serialized in state files, and
306 are instead reconstructed on load. */
308 /* The head of a singly-linked list of immediate descendents in
309 the inheritance hierarchy. */
310 type_p first_subclass
;
311 /* The next in that list. */
312 type_p next_sibling_class
;
314 /* Have we already written ggc/pch user func for ptr to this?
315 (in write_user_func_for_structure_ptr). */
316 bool wrote_user_func_for_ptr
[NUM_WTK
];
319 /* when TYPE_SCALAR: */
322 /* when TYPE_ARRAY: */
324 type_p p
; /* The array component type. */
325 const char *len
; /* The string if any giving its length. */
328 /* When TYPE_PARAM_STRUCT for (param_is, use_param, param1_is,
329 param2_is, ... use_param1, use_param_2, ... use_params) GTY
332 type_p stru
; /* The generic GTY-ed type. */
333 type_p param
[NUM_PARAM
]; /* The actual parameter types. */
334 struct fileloc line
; /* The source location. */
339 /* The one and only TYPE_STRING. */
340 extern struct type string_type
;
342 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
343 set early in main. */
344 extern struct type scalar_nonchar
;
345 extern struct type scalar_char
;
347 /* Test if a type is a union, either a plain one or a language
350 ((x)->kind == TYPE_UNION \
351 || ((x)->kind == TYPE_LANG_STRUCT \
352 && (x)->u.s.lang_struct->kind == TYPE_UNION))
354 /* Test if a type is a union or a structure, perhaps a language
357 union_or_struct_p (enum typekind kind
)
359 return (kind
== TYPE_UNION
360 || kind
== TYPE_STRUCT
361 || kind
== TYPE_LANG_STRUCT
362 || kind
== TYPE_USER_STRUCT
);
366 union_or_struct_p (const_type_p x
)
368 return union_or_struct_p (x
->kind
);
371 /* Give the file location of a type, if any. */
372 static inline struct fileloc
*
373 type_fileloc (type_p t
)
377 if (union_or_struct_p (t
))
379 if (t
->kind
== TYPE_PARAM_STRUCT
)
380 return &t
->u
.param_struct
.line
;
384 /* Structure representing an output file. */
393 typedef struct outf
*outf_p
;
395 /* The list of output files. */
396 extern outf_p output_files
;
398 /* The output header file that is included into pretty much every
400 extern outf_p header_file
;
402 /* Print, like fprintf, to O. No-op if O is NULL. */
404 oprintf (outf_p o
, const char *S
, ...)
407 /* An output file, suitable for definitions, that can see declarations
408 made in INPF and is linked into every language that uses INPF. May
409 return NULL in plugin mode. The INPF argument is almost const, but
410 since the result is cached in its inpoutf field it cannot be
412 outf_p
get_output_file_with_visibility (input_file
* inpf
);
414 /* The name of an output file, suitable for definitions, that can see
415 declarations made in INPF and is linked into every language that
416 uses INPF. May return NULL. */
417 const char *get_output_file_name (input_file
*inpf
);
420 /* Source directory. */
421 extern const char *srcdir
; /* (-S) program argument. */
423 /* Length of srcdir name. */
424 extern size_t srcdir_len
;
426 /* Variable used for reading and writing the state. */
427 extern const char *read_state_filename
; /* (-r) program argument. */
428 extern const char *write_state_filename
; /* (-w) program argument. */
430 /* Functions reading and writing the entire gengtype state, called from
431 main, and implemented in file gengtype-state.c. */
432 void read_state (const char* path
);
433 /* Write the state, and update the state_number field in types. */
434 void write_state (const char* path
);
437 /* Print an error message. */
438 extern void error_at_line
439 (const struct fileloc
*pos
, const char *msg
, ...) ATTRIBUTE_PRINTF_2
;
441 /* Like asprintf, but calls fatal() on out of memory. */
442 extern char *xasprintf (const char *, ...) ATTRIBUTE_PRINTF_1
;
444 /* Constructor routines for types. */
445 extern void do_typedef (const char *s
, type_p t
, struct fileloc
*pos
);
446 extern void do_scalar_typedef (const char *s
, struct fileloc
*pos
);
447 extern type_p
resolve_typedef (const char *s
, struct fileloc
*pos
);
448 extern void add_subclass (type_p base
, type_p subclass
);
449 extern type_p
new_structure (const char *name
, enum typekind kind
,
450 struct fileloc
*pos
, pair_p fields
,
451 options_p o
, type_p base
);
452 type_p
create_user_defined_type (const char *, struct fileloc
*);
453 extern type_p
find_structure (const char *s
, enum typekind kind
);
454 extern type_p
create_scalar_type (const char *name
);
455 extern type_p
create_pointer (type_p t
);
456 extern type_p
create_array (type_p t
, const char *len
);
457 extern pair_p
create_field_at (pair_p next
, type_p type
,
458 const char *name
, options_p opt
,
459 struct fileloc
*pos
);
460 extern pair_p
nreverse_pairs (pair_p list
);
461 extern type_p
adjust_field_type (type_p
, options_p
);
462 extern void note_variable (const char *s
, type_p t
, options_p o
,
463 struct fileloc
*pos
);
465 /* Lexer and parser routines. */
466 extern int yylex (const char **yylval
);
467 extern void yybegin (const char *fname
);
468 extern void yyend (void);
469 extern void parse_file (const char *name
);
470 extern bool hit_error
;
477 /* Per standard convention, codes in the range (0, UCHAR_MAX]
478 represent single characters with those character codes. */
479 CHAR_TOKEN_OFFSET
= UCHAR_MAX
+ 1,
480 GTY_TOKEN
= CHAR_TOKEN_OFFSET
,
498 IGNORABLE_CXX_KEYWORD
,
500 /* print_token assumes that any token >= FIRST_TOKEN_WITH_VALUE may have
501 a meaningful value to be printed. */
502 FIRST_TOKEN_WITH_VALUE
= PARAM_IS
506 /* Level for verbose messages, e.g. output file generation... */
507 extern int verbosity_level
; /* (-v) program argument. */
509 /* For debugging purposes we provide two flags. */
511 /* Dump everything to understand gengtype's state. Might be useful to
513 extern int do_dump
; /* (-d) program argument. */
515 /* Trace the execution by many DBGPRINTF (with the position inside
516 gengtype source code). Only useful to debug gengtype itself. */
517 extern int do_debug
; /* (-D) program argument. */
520 #define DBGPRINTF(Fmt,...) do {if (do_debug) \
521 fprintf (stderr, "%s:%d: " Fmt "\n", \
522 lbasename (__FILE__),__LINE__, ##__VA_ARGS__);} while (0)
523 void dbgprint_count_type_at (const char *, int, const char *, type_p
);
524 #define DBGPRINT_COUNT_TYPE(Msg,Ty) do {if (do_debug) \
525 dbgprint_count_type_at (__FILE__, __LINE__, Msg, Ty);}while (0)
527 #define DBGPRINTF(Fmt,...) do {/*nodbgrintf*/} while (0)
528 #define DBGPRINT_COUNT_TYPE(Msg,Ty) do{/*nodbgprint_count_type*/}while (0)
529 #endif /*ENABLE_CHECKING */
531 #define FOR_ALL_INHERITED_FIELDS(TYPE, FIELD_VAR) \
532 for (type_p sub = (TYPE); sub; sub = sub->u.s.base_class) \
533 for (FIELD_VAR = sub->u.s.fields; FIELD_VAR; FIELD_VAR = FIELD_VAR->next)
536 opts_have (options_p opts
, const char *str
);