comparison gcc/gengtype.h @ 67:f6334be47118

update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
author nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
date Tue, 22 Mar 2011 17:18:12 +0900
parents b7f97abdc517
children 04ced10e8804
comparison
equal deleted inserted replaced
65:65488c3d617d 67:f6334be47118
1 /* Process source files and output type information. 1 /* Process source files and output type information.
2 Copyright (C) 2002, 2003, 2004, 2007, 2008 Free Software Foundation, Inc. 2 Copyright (C) 2002, 2003, 2004, 2007, 2008, 2010
3 3 Free Software Foundation, Inc.
4 This file is part of GCC. 4
5 5 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under 6
7 the terms of the GNU General Public License as published by the Free 7 GCC is free software; you can redistribute it and/or modify it under
8 Software Foundation; either version 3, or (at your option) any later 8 the terms of the GNU General Public License as published by the Free
9 version. 9 Software Foundation; either version 3, or (at your option) any later
10 10 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY 11
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 for more details. 14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 15 for more details.
16 You should have received a copy of the GNU General Public License 16
17 along with GCC; see the file COPYING3. If not see 17 You should have received a copy of the GNU General Public License
18 <http://www.gnu.org/licenses/>. */ 18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
19 20
20 #ifndef GCC_GENGTYPE_H 21 #ifndef GCC_GENGTYPE_H
21 #define GCC_GENGTYPE_H 22 #define GCC_GENGTYPE_H
22 23
24 /* Sets of accepted source languages like C, C++, Ada... are
25 represented by a bitmap. */
26 typedef unsigned lang_bitmap;
27
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. */
31 struct input_file_st
32 {
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
37 char. */
38 };
39 typedef struct input_file_st input_file;
40
23 /* A file position, mostly for error messages. 41 /* A file position, mostly for error messages.
24 The FILE element may be compared using pointer equality. */ 42 The FILE element may be compared using pointer equality. */
25 struct fileloc { 43 struct fileloc
26 const char *file; 44 {
45 const input_file *file;
27 int line; 46 int line;
28 }; 47 };
48
49
50 /* Table of all input files and its size. */
51 extern const input_file** gt_files;
52 extern size_t num_gt_files;
53
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;
60
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
63 hash-consing them. */
64 input_file* input_file_by_name (const char* name);
65
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);
69
70 /* Get the name of an input file. */
71 static inline const char*
72 get_input_file_name (const input_file *inpf)
73 {
74 if (inpf)
75 return inpf->inpname;
76 return NULL;
77 }
78
79 /* Return a bitmap which has bit `1 << BASE_FILE_<lang>' set iff
80 INPUT_FILE is used by <lang>.
81
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.
86 */
87
88 static inline lang_bitmap
89 get_lang_bitmap (const input_file* inpf)
90 {
91 if (inpf == NULL)
92 return 0;
93 return inpf->inpbitmap;
94 }
95
96 /* Set the bitmap returned by get_lang_bitmap. The only legitimate
97 callers of this function are read_input_list & read_state_*. */
98 static inline void
99 set_lang_bitmap (input_file* inpf, lang_bitmap n)
100 {
101 gcc_assert (inpf);
102 inpf->inpbitmap = n;
103 }
104
105 /* Vector of per-language directories. */
106 extern const char **lang_dir_names;
107 extern size_t num_lang_dirs;
29 108
30 /* Data types handed around within, but opaque to, the lexer and parser. */ 109 /* Data types handed around within, but opaque to, the lexer and parser. */
31 typedef struct pair *pair_p; 110 typedef struct pair *pair_p;
32 typedef struct type *type_p; 111 typedef struct type *type_p;
33 typedef const struct type *const_type_p; 112 typedef const struct type *const_type_p;
35 114
36 /* Variables used to communicate between the lexer and the parser. */ 115 /* Variables used to communicate between the lexer and the parser. */
37 extern int lexer_toplevel_done; 116 extern int lexer_toplevel_done;
38 extern struct fileloc lexer_line; 117 extern struct fileloc lexer_line;
39 118
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;
125
126
127
128 /* Discrimating kind of types we can understand. */
129
130 enum typekind {
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
145 options. */
146 };
147
148 /* Discriminating kind for options. */
149 enum option_kind {
150 OPTION_NONE=0, /* Never used, so zeroed memory is invalid. */
151 OPTION_STRING, /* A string-valued option. Most options are
152 strings. */
153 OPTION_TYPE, /* A type-valued option. */
154 OPTION_NESTED /* Option data for 'nested_ptr'. */
155 };
156
157
158 /* A way to pass data through to the output end. */
159 struct options {
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. */
163 union {
164 const char* string; /* When OPTION_STRING. */
165 type_p type; /* When OPTION_TYPE. */
166 struct nested_ptr_data* nested; /* when OPTION_NESTED. */
167 } info;
168 };
169
170
171 /* Option data for the 'nested_ptr' option. */
172 struct nested_ptr_data {
173 type_p type;
174 const char *convert_to;
175 const char *convert_from;
176 };
177
178 /* Some functions to create various options structures with name NAME
179 and info INFO. NEXT is the next option in the chain. */
180
181 /* Create a string option. */
182 options_p create_string_option (options_p next, const char* name,
183 const char* info);
184
185 /* Create a type option. */
186 options_p create_type_option (options_p next, const char* name,
187 type_p info);
188
189 /* Create a nested option. */
190 options_p create_nested_option (options_p next, const char* name,
191 struct nested_ptr_data* info);
192
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);
196
197 /* A name and a type. */
198 struct pair {
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. */
204 };
205
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. */
209
210 enum gc_used_enum {
211
212 /* We need that zeroed types are initially unused. */
213 GC_UNUSED=0,
214
215 /* The GTY-ed type is used, e.g by a GTY-ed variable or a field
216 inside a GTY-ed used type. */
217 GC_USED,
218
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
223 defined. */
224 GC_MAYBE_POINTED_TO,
225
226 /* For known GTY-ed structures which are pointed to by GTY-ed
227 variables or fields. */
228 GC_POINTED_TO
229 };
230
231 /* We can have at most ten type parameters in parameterized structures. */
232 #define NUM_PARAM 10
233
234 /* Our type structure describes all types handled by gengtype. */
235 struct type {
236 /* Discriminating kind, cannot be TYPE_NONE. */
237 enum typekind kind;
238
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
244 tricky! */
245 type_p next;
246
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
250 number. */
251 int state_number;
252
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
255 function. */
256 type_p pointer_to;
257
258 /* Type usage information, computed by set_gc_used_type and
259 set_gc_used functions. */
260 enum gc_used_enum gc_used;
261
262 /* The following union is discriminated by the 'kind' field above. */
263 union {
264 /* TYPE__NONE is impossible. */
265
266 /* when TYPE_POINTER: */
267 type_p p;
268
269 /* when TYPE_STRUCT or TYPE_UNION or TYPE_LANG_STRUCT, we have an
270 aggregate type containing fields: */
271 struct {
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. */
283 type_p lang_struct;
284 } s;
285
286 /* when TYPE_SCALAR: */
287 bool scalar_is_char;
288
289 /* when TYPE_ARRAY: */
290 struct {
291 type_p p; /* The array component type. */
292 const char *len; /* The string if any giving its length. */
293 } a;
294
295 /* When TYPE_PARAM_STRUCT for (param_is, use_param, param1_is,
296 param2_is, ... use_param1, use_param_2, ... use_params) GTY
297 options. */
298 struct {
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. */
302 } param_struct;
303
304 } u;
305 };
306
307 /* The one and only TYPE_STRING. */
308 extern struct type string_type;
309
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;
314
315 /* Test if a type is a union, either a plain one or a language
316 specific one. */
317 #define UNION_P(x) \
318 ((x)->kind == TYPE_UNION || \
319 ((x)->kind == TYPE_LANG_STRUCT \
320 && (x)->u.s.lang_struct->kind == TYPE_UNION))
321
322 /* Test if a type is a union or a structure, perhaps a language
323 specific one. */
324 #define UNION_OR_STRUCT_P(x) \
325 ((x)->kind == TYPE_UNION \
326 || (x)->kind == TYPE_STRUCT \
327 || (x)->kind == TYPE_LANG_STRUCT)
328
329
330
331 /* Structure representing an output file. */
332 struct outf
333 {
334 struct outf *next;
335 const char *name;
336 size_t buflength;
337 size_t bufused;
338 char *buf;
339 };
340 typedef struct outf *outf_p;
341
342 /* The list of output files. */
343 extern outf_p output_files;
344
345 /* The output header file that is included into pretty much every
346 source file. */
347 extern outf_p header_file;
348
349 /* Print, like fprintf, to O. No-op if O is NULL. */
350 void
351 oprintf (outf_p o, const char *S, ...)
352 ATTRIBUTE_PRINTF_2;
353
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
358 declared const. */
359 outf_p get_output_file_with_visibility (input_file* inpf);
360
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);
365
366
367 /* Source directory. */
368 extern const char *srcdir; /* (-S) program argument. */
369
370 /* Length of srcdir name. */
371 extern size_t srcdir_len;
372
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. */
376
377 /* Functions reading and writing the entire gengtype state, called from
378 main, and implemented in file gengtype-state.c. */
379 void read_state (const char* path);
380 /* Write the state, and update the state_number field in types. */
381 void write_state (const char* path);
382
383
40 /* Print an error message. */ 384 /* Print an error message. */
41 extern void error_at_line 385 extern void error_at_line
42 (const struct fileloc *pos, const char *msg, ...) ATTRIBUTE_PRINTF_2; 386 (const struct fileloc *pos, const char *msg, ...) ATTRIBUTE_PRINTF_2;
43 387
44 /* Like asprintf, but calls fatal() on out of memory. */ 388 /* Like asprintf, but calls fatal() on out of memory. */
45 extern char *xasprintf(const char *, ...) ATTRIBUTE_PRINTF_1; 389 extern char *xasprintf (const char *, ...) ATTRIBUTE_PRINTF_1;
46 390
47 /* Constructor routines for types. */ 391 /* Constructor routines for types. */
48 extern void do_typedef (const char *s, type_p t, struct fileloc *pos); 392 extern void do_typedef (const char *s, type_p t, struct fileloc *pos);
49 extern void do_scalar_typedef (const char *s, struct fileloc *pos); 393 extern void do_scalar_typedef (const char *s, struct fileloc *pos);
50 extern type_p resolve_typedef (const char *s, struct fileloc *pos); 394 extern type_p resolve_typedef (const char *s, struct fileloc *pos);
53 options_p o); 397 options_p o);
54 extern type_p find_structure (const char *s, int isunion); 398 extern type_p find_structure (const char *s, int isunion);
55 extern type_p create_scalar_type (const char *name); 399 extern type_p create_scalar_type (const char *name);
56 extern type_p create_pointer (type_p t); 400 extern type_p create_pointer (type_p t);
57 extern type_p create_array (type_p t, const char *len); 401 extern type_p create_array (type_p t, const char *len);
58 extern options_p create_option (options_p, const char *name, const void *info); 402 extern pair_p create_field_at (pair_p next, type_p type,
59 extern options_p create_nested_ptr_option (options_p, type_p t, 403 const char *name, options_p opt,
60 const char *from, const char *to); 404 struct fileloc *pos);
61 extern pair_p create_field_at (pair_p next, type_p type, const char *name,
62 options_p opt, struct fileloc *pos);
63 extern pair_p nreverse_pairs (pair_p list); 405 extern pair_p nreverse_pairs (pair_p list);
64 extern type_p adjust_field_type (type_p, options_p); 406 extern type_p adjust_field_type (type_p, options_p);
65 extern void note_variable (const char *s, type_p t, options_p o, 407 extern void note_variable (const char *s, type_p t, options_p o,
66 struct fileloc *pos); 408 struct fileloc *pos);
67 extern void note_def_vec (const char *type_name, bool is_scalar, 409 extern void note_def_vec (const char *type_name, bool is_scalar,
75 extern void yyend (void); 417 extern void yyend (void);
76 extern void parse_file (const char *name); 418 extern void parse_file (const char *name);
77 extern bool hit_error; 419 extern bool hit_error;
78 420
79 /* Token codes. */ 421 /* Token codes. */
80 enum { 422 enum
81 EOF_TOKEN = 0, 423 {
82 424 EOF_TOKEN = 0,
83 /* Per standard convention, codes in the range (0, UCHAR_MAX] 425
84 represent single characters with those character codes. */ 426 /* Per standard convention, codes in the range (0, UCHAR_MAX]
85 427 represent single characters with those character codes. */
86 CHAR_TOKEN_OFFSET = UCHAR_MAX + 1, 428
87 GTY_TOKEN = CHAR_TOKEN_OFFSET, 429 CHAR_TOKEN_OFFSET = UCHAR_MAX + 1,
88 TYPEDEF, 430 GTY_TOKEN = CHAR_TOKEN_OFFSET,
89 EXTERN, 431 TYPEDEF,
90 STATIC, 432 EXTERN,
91 UNION, 433 STATIC,
92 STRUCT, 434 UNION,
93 ENUM, 435 STRUCT,
94 VEC_TOKEN, 436 ENUM,
95 DEFVEC_OP, 437 VEC_TOKEN,
96 DEFVEC_I, 438 DEFVEC_OP,
97 DEFVEC_ALLOC, 439 DEFVEC_I,
98 ELLIPSIS, 440 DEFVEC_ALLOC,
99 PTR_ALIAS, 441 ELLIPSIS,
100 NESTED_PTR, 442 PTR_ALIAS,
101 PARAM_IS, 443 NESTED_PTR,
102 NUM, 444 PARAM_IS,
103 SCALAR, 445 NUM,
104 ID, 446 SCALAR,
105 STRING, 447 ID,
106 CHAR, 448 STRING,
107 ARRAY, 449 CHAR,
108 450 ARRAY,
109 /* print_token assumes that any token >= FIRST_TOKEN_WITH_VALUE may have 451
110 a meaningful value to be printed. */ 452 /* print_token assumes that any token >= FIRST_TOKEN_WITH_VALUE may have
111 FIRST_TOKEN_WITH_VALUE = PARAM_IS 453 a meaningful value to be printed. */
112 }; 454 FIRST_TOKEN_WITH_VALUE = PARAM_IS
455 };
456
457
458 /* Level for verbose messages, e.g. output file generation... */
459 extern int verbosity_level; /* (-v) program argument. */
460
461 /* For debugging purposes we provide two flags. */
462
463 /* Dump everything to understand gengtype's state. Might be useful to
464 gengtype users. */
465 extern int do_dump; /* (-d) program argument. */
466
467 /* Trace the execution by many DBGPRINTF (with the position inside
468 gengtype source code). Only useful to debug gengtype itself. */
469 extern int do_debug; /* (-D) program argument. */
470
471 #if ENABLE_CHECKING
472 #define DBGPRINTF(Fmt,...) do {if (do_debug) \
473 fprintf (stderr, "%s:%d: " Fmt "\n", \
474 lbasename (__FILE__),__LINE__, ##__VA_ARGS__);} while (0)
475 void dbgprint_count_type_at (const char *, int, const char *, type_p);
476 #define DBGPRINT_COUNT_TYPE(Msg,Ty) do {if (do_debug) \
477 dbgprint_count_type_at (__FILE__, __LINE__, Msg, Ty);}while (0)
478 #else
479 #define DBGPRINTF(Fmt,...) do {/*nodbgrintf*/} while (0)
480 #define DBGPRINT_COUNT_TYPE(Msg,Ty) do{/*nodbgprint_count_type*/}while (0)
481 #endif /*ENABLE_CHECKING */
482
113 #endif 483 #endif