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