1 /* Process source files and output type information.
2 Copyright (C) 2002, 2003, 2004, 2007, 2008, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #ifndef GCC_GENGTYPE_H
22 #define GCC_GENGTYPE_H
24 /* Sets of accepted source languages like C, C++, Ada... are
25 represented by a bitmap. */
26 typedef unsigned lang_bitmap
;
28 /* Variable length structure representing an input file. A hash table
29 ensure uniqueness for a given input file name. The only function
30 allocating input_file-s is input_file_by_name. */
33 struct outf
* inpoutf
; /* Cached corresponding output file, computed
34 in get_output_file_with_visibility. */
35 lang_bitmap inpbitmap
; /* The set of languages using this file. */
36 char inpname
[1]; /* A variable-length array, ended by a null
39 typedef struct input_file_st input_file
;
41 /* A file position, mostly for error messages.
42 The FILE element may be compared using pointer equality. */
45 const input_file
*file
;
50 /* Table of all input files and its size. */
51 extern const input_file
** gt_files
;
52 extern size_t num_gt_files
;
54 /* A number of places use the name of this "gengtype.c" file for a
55 location for things that we can't rely on the source to define. We
56 also need to refer to the "system.h" file specifically. These two
57 pointers are initialized early in main. */
58 extern input_file
* this_file
;
59 extern input_file
* system_h_file
;
61 /* Retrieve or create the input_file for a given name, which is a file
62 path. This is the only function allocating input_file-s and it is
64 input_file
* input_file_by_name (const char* name
);
66 /* For F an input_file, return the relative path to F from $(srcdir)
67 if the latter is a prefix in F, NULL otherwise. */
68 const char *get_file_srcdir_relative_path (const input_file
*inpf
);
70 /* Get the name of an input file. */
71 static inline const char*
72 get_input_file_name (const input_file
*inpf
)
79 /* Return a bitmap which has bit `1 << BASE_FILE_<lang>' set iff
80 INPUT_FILE is used by <lang>.
82 This function should be written to assume that a file _is_ used
83 if the situation is unclear. If it wrongly assumes a file _is_ used,
84 a linker error will result. If it wrongly assumes a file _is not_ used,
85 some GC roots may be missed, which is a much harder-to-debug problem.
88 static inline lang_bitmap
89 get_lang_bitmap (const input_file
* inpf
)
93 return inpf
->inpbitmap
;
96 /* Set the bitmap returned by get_lang_bitmap. The only legitimate
97 callers of this function are read_input_list & read_state_*. */
99 set_lang_bitmap (input_file
* inpf
, lang_bitmap n
)
105 /* Vector of per-language directories. */
106 extern const char **lang_dir_names
;
107 extern size_t num_lang_dirs
;
109 /* Data types handed around within, but opaque to, the lexer and parser. */
110 typedef struct pair
*pair_p
;
111 typedef struct type
*type_p
;
112 typedef const struct type
*const_type_p
;
113 typedef struct options
*options_p
;
115 /* Variables used to communicate between the lexer and the parser. */
116 extern int lexer_toplevel_done
;
117 extern struct fileloc lexer_line
;
119 /* Various things, organized as linked lists, needed both in
120 gengtype.c & in gengtype-state.c files. */
121 extern pair_p typedefs
;
122 extern type_p structures
;
123 extern type_p param_structs
;
124 extern pair_p variables
;
128 /* Discrimating kind of types we can understand. */
131 TYPE_NONE
=0, /* Never used, so zeroed memory is invalid. */
132 TYPE_SCALAR
, /* Scalar types like char. */
133 TYPE_STRING
, /* The string type. */
134 TYPE_STRUCT
, /* Type for GTY-ed structs. */
135 TYPE_UNION
, /* Type for GTY-ed discriminated unions. */
136 TYPE_POINTER
, /* Pointer type to GTY-ed type. */
137 TYPE_ARRAY
, /* Array of GTY-ed types. */
138 TYPE_LANG_STRUCT
, /* GCC front-end language specific structs.
139 Various languages may have homonymous but
140 different structs. */
141 TYPE_PARAM_STRUCT
/* Type for parametrized structs, e.g. hash_t
142 hash-tables, ... See (param_is, use_param,
143 param1_is, param2_is,... use_param1,
144 use_param_2,... use_params) GTY
148 /* Discriminating kind for options. */
150 OPTION_NONE
=0, /* Never used, so zeroed memory is invalid. */
151 OPTION_STRING
, /* A string-valued option. Most options are
153 OPTION_TYPE
, /* A type-valued option. */
154 OPTION_NESTED
/* Option data for 'nested_ptr'. */
158 /* A way to pass data through to the output end. */
160 struct options
*next
; /* next option of the same pair. */
161 const char *name
; /* GTY option name. */
162 enum option_kind kind
; /* discriminating option kind. */
164 const char* string
; /* When OPTION_STRING. */
165 type_p type
; /* When OPTION_TYPE. */
166 struct nested_ptr_data
* nested
; /* when OPTION_NESTED. */
171 /* Option data for the 'nested_ptr' option. */
172 struct nested_ptr_data
{
174 const char *convert_to
;
175 const char *convert_from
;
178 /* Some functions to create various options structures with name NAME
179 and info INFO. NEXT is the next option in the chain. */
181 /* Create a string option. */
182 options_p
create_string_option (options_p next
, const char* name
,
185 /* Create a type option. */
186 options_p
create_type_option (options_p next
, const char* name
,
189 /* Create a nested option. */
190 options_p
create_nested_option (options_p next
, const char* name
,
191 struct nested_ptr_data
* info
);
193 /* Create a nested pointer option. */
194 options_p
create_nested_ptr_option (options_p
, type_p t
,
195 const char *from
, const char *to
);
197 /* A name and a type. */
199 pair_p next
; /* The next pair in the linked list. */
200 const char *name
; /* The defined name. */
201 type_p type
; /* Its GTY-ed type. */
202 struct fileloc line
; /* The file location. */
203 options_p opt
; /* GTY options, as a linked list. */
206 /* Usage information for GTY-ed types. Gengtype has to care only of
207 used GTY-ed types. Types are initially unused, and their usage is
208 computed by set_gc_used_type and set_gc_used functions. */
212 /* We need that zeroed types are initially unused. */
215 /* The GTY-ed type is used, e.g by a GTY-ed variable or a field
216 inside a GTY-ed used type. */
219 /* For GTY-ed structures whose definitions we haven't seen so far
220 when we encounter a pointer to it that is annotated with
221 ``maybe_undef''. If after reading in everything we don't have
222 source file information for it, we assume that it never has been
226 /* For known GTY-ed structures which are pointed to by GTY-ed
227 variables or fields. */
231 /* We can have at most ten type parameters in parameterized structures. */
234 /* Our type structure describes all types handled by gengtype. */
236 /* Discriminating kind, cannot be TYPE_NONE. */
239 /* For top-level structs or unions, the 'next' field links the
240 global list 'structures' or 'param_structs'; for lang_structs,
241 their homonymous structs are linked using this 'next' field. The
242 homonymous list starts at the s.lang_struct field of the
243 lang_struct. See the new_structure function for details. This is
247 /* State number used when writing & reading the persistent state. A
248 type with a positive number has already been written. For ease
249 of debugging, newly allocated types have a unique negative
253 /* Each GTY-ed type which is pointed to by some GTY-ed type knows
254 the GTY pointer type pointing to it. See create_pointer
258 /* Type usage information, computed by set_gc_used_type and
259 set_gc_used functions. */
260 enum gc_used_enum gc_used
;
262 /* The following union is discriminated by the 'kind' field above. */
264 /* TYPE__NONE is impossible. */
266 /* when TYPE_POINTER: */
269 /* when TYPE_STRUCT or TYPE_UNION or TYPE_LANG_STRUCT, we have an
270 aggregate type containing fields: */
272 const char *tag
; /* the aggragate tag, if any. */
273 struct fileloc line
; /* the source location. */
274 pair_p fields
; /* the linked list of fields. */
275 options_p opt
; /* the GTY options if any. */
276 lang_bitmap bitmap
; /* the set of front-end languages
277 using that GTY-ed aggregate. */
278 /* For TYPE_LANG_STRUCT, the lang_struct field gives the first
279 element of a linked list of homonymous struct or union types.
280 Within this list, each homonymous type has as its lang_struct
281 field the original TYPE_LANG_STRUCT type. This is a dirty
282 trick, see the new_structure function for details. */
286 /* when TYPE_SCALAR: */
289 /* when TYPE_ARRAY: */
291 type_p p
; /* The array component type. */
292 const char *len
; /* The string if any giving its length. */
295 /* When TYPE_PARAM_STRUCT for (param_is, use_param, param1_is,
296 param2_is, ... use_param1, use_param_2, ... use_params) GTY
299 type_p stru
; /* The generic GTY-ed type. */
300 type_p param
[NUM_PARAM
]; /* The actual parameter types. */
301 struct fileloc line
; /* The source location. */
307 /* The one and only TYPE_STRING. */
308 extern struct type string_type
;
310 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
311 set early in main. */
312 extern struct type scalar_nonchar
;
313 extern struct type scalar_char
;
315 /* Test if a type is a union, either a plain one or a language
318 ((x)->kind == TYPE_UNION || \
319 ((x)->kind == TYPE_LANG_STRUCT \
320 && (x)->u.s.lang_struct->kind == TYPE_UNION))
322 /* Test if a type is a union or a structure, perhaps a language
324 #define UNION_OR_STRUCT_P(x) \
325 ((x)->kind == TYPE_UNION \
326 || (x)->kind == TYPE_STRUCT \
327 || (x)->kind == TYPE_LANG_STRUCT)
331 /* Structure representing an output file. */
340 typedef struct outf
*outf_p
;
342 /* The list of output files. */
343 extern outf_p output_files
;
345 /* The output header file that is included into pretty much every
347 extern outf_p header_file
;
349 /* Print, like fprintf, to O. No-op if O is NULL. */
351 oprintf (outf_p o
, const char *S
, ...)
354 /* An output file, suitable for definitions, that can see declarations
355 made in INPF and is linked into every language that uses INPF. May
356 return NULL in plugin mode. The INPF argument is almost const, but
357 since the result is cached in its inpoutf field it cannot be
359 outf_p
get_output_file_with_visibility (input_file
* inpf
);
361 /* The name of an output file, suitable for definitions, that can see
362 declarations made in INPF and is linked into every language that
363 uses INPF. May return NULL. */
364 const char *get_output_file_name (input_file
*inpf
);
367 /* Source directory. */
368 extern const char *srcdir
; /* (-S) program argument. */
370 /* Length of srcdir name. */
371 extern size_t srcdir_len
;
373 /* Variable used for reading and writing the state. */
374 extern const char *read_state_filename
; /* (-r) program argument. */
375 extern const char *write_state_filename
; /* (-w) program argument. */
377 /* Print an error message. */
378 extern void error_at_line
379 (const struct fileloc
*pos
, const char *msg
, ...) ATTRIBUTE_PRINTF_2
;
381 /* Like asprintf, but calls fatal() on out of memory. */
382 extern char *xasprintf (const char *, ...) ATTRIBUTE_PRINTF_1
;
384 /* Constructor routines for types. */
385 extern void do_typedef (const char *s
, type_p t
, struct fileloc
*pos
);
386 extern void do_scalar_typedef (const char *s
, struct fileloc
*pos
);
387 extern type_p
resolve_typedef (const char *s
, struct fileloc
*pos
);
388 extern type_p
new_structure (const char *name
, int isunion
,
389 struct fileloc
*pos
, pair_p fields
,
391 extern type_p
find_structure (const char *s
, int isunion
);
392 extern type_p
create_scalar_type (const char *name
);
393 extern type_p
create_pointer (type_p t
);
394 extern type_p
create_array (type_p t
, const char *len
);
395 extern pair_p
create_field_at (pair_p next
, type_p type
,
396 const char *name
, options_p opt
,
397 struct fileloc
*pos
);
398 extern pair_p
nreverse_pairs (pair_p list
);
399 extern type_p
adjust_field_type (type_p
, options_p
);
400 extern void note_variable (const char *s
, type_p t
, options_p o
,
401 struct fileloc
*pos
);
402 extern void note_def_vec (const char *type_name
, bool is_scalar
,
403 struct fileloc
*pos
);
404 extern void note_def_vec_alloc (const char *type
, const char *astrat
,
405 struct fileloc
*pos
);
407 /* Lexer and parser routines. */
408 extern int yylex (const char **yylval
);
409 extern void yybegin (const char *fname
);
410 extern void yyend (void);
411 extern void parse_file (const char *name
);
412 extern bool hit_error
;
419 /* Per standard convention, codes in the range (0, UCHAR_MAX]
420 represent single characters with those character codes. */
422 CHAR_TOKEN_OFFSET
= UCHAR_MAX
+ 1,
423 GTY_TOKEN
= CHAR_TOKEN_OFFSET
,
445 /* print_token assumes that any token >= FIRST_TOKEN_WITH_VALUE may have
446 a meaningful value to be printed. */
447 FIRST_TOKEN_WITH_VALUE
= PARAM_IS
451 /* Level for verbose messages, e.g. output file generation... */
452 extern int verbosity_level
; /* (-v) program argument. */
454 /* For debugging purposes we provide two flags. */
456 /* Dump everything to understand gengtype's state. Might be useful to
458 extern int do_dump
; /* (-d) program argument. */
460 /* Trace the execution by many DBGPRINTF (with the position inside
461 gengtype source code). Only useful to debug gengtype itself. */
462 extern int do_debug
; /* (-D) program argument. */
465 #define DBGPRINTF(Fmt,...) do {if (do_debug) \
466 fprintf (stderr, "%s:%d: " Fmt "\n", \
467 lbasename (__FILE__),__LINE__, ##__VA_ARGS__);} while (0)
468 void dbgprint_count_type_at (const char *, int, const char *, type_p
);
469 #define DBGPRINT_COUNT_TYPE(Msg,Ty) do {if (do_debug) \
470 dbgprint_count_type_at (__FILE__, __LINE__, Msg, Ty);}while (0)
472 #define DBGPRINTF(Fmt,...) do {/*nodbgrintf*/} while (0)
473 #define DBGPRINT_COUNT_TYPE(Msg,Ty) do{/*nodbgprint_count_type*/}while (0)
474 #endif /*ENABLE_CHECKING */