OSDN Git Service

2010-12-09 Martin Jambor <mjambor@suse.cz>
[pf3gnuchains/gcc-fork.git] / gcc / gengtype.h
1 /* Process source files and output type information.
2    Copyright (C) 2002, 2003, 2004, 2007, 2008, 2010 
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 /* 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
41 /* A file position, mostly for error messages.
42    The FILE element may be compared using pointer equality.  */
43 struct fileloc
44 {
45   const input_file *file;
46   int line;
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;
108
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;
114
115 /* Variables used to communicate between the lexer and the parser.  */
116 extern int lexer_toplevel_done;
117 extern struct fileloc lexer_line;
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
384 /* Print an error message.  */
385 extern void error_at_line
386 (const struct fileloc *pos, const char *msg, ...) ATTRIBUTE_PRINTF_2;
387
388 /* Like asprintf, but calls fatal() on out of memory.  */
389 extern char *xasprintf (const char *, ...) ATTRIBUTE_PRINTF_1;
390
391 /* Constructor routines for types.  */
392 extern void do_typedef (const char *s, type_p t, struct fileloc *pos);
393 extern void do_scalar_typedef (const char *s, struct fileloc *pos);
394 extern type_p resolve_typedef (const char *s, struct fileloc *pos);
395 extern type_p new_structure (const char *name, int isunion,
396                              struct fileloc *pos, pair_p fields,
397                              options_p o);
398 extern type_p find_structure (const char *s, int isunion);
399 extern type_p create_scalar_type (const char *name);
400 extern type_p create_pointer (type_p t);
401 extern type_p create_array (type_p t, const char *len);
402 extern pair_p create_field_at (pair_p next, type_p type,
403                                const char *name, options_p opt,
404                                struct fileloc *pos);
405 extern pair_p nreverse_pairs (pair_p list);
406 extern type_p adjust_field_type (type_p, options_p);
407 extern void note_variable (const char *s, type_p t, options_p o,
408                            struct fileloc *pos);
409 extern void note_def_vec (const char *type_name, bool is_scalar,
410                           struct fileloc *pos);
411 extern void note_def_vec_alloc (const char *type, const char *astrat,
412                                 struct fileloc *pos);
413
414 /* Lexer and parser routines.  */
415 extern int yylex (const char **yylval);
416 extern void yybegin (const char *fname);
417 extern void yyend (void);
418 extern void parse_file (const char *name);
419 extern bool hit_error;
420
421 /* Token codes.  */
422 enum
423   {
424     EOF_TOKEN = 0,
425
426     /* Per standard convention, codes in the range (0, UCHAR_MAX]
427        represent single characters with those character codes.  */
428
429     CHAR_TOKEN_OFFSET = UCHAR_MAX + 1,
430     GTY_TOKEN = CHAR_TOKEN_OFFSET,
431     TYPEDEF,
432     EXTERN,
433     STATIC,
434     UNION,
435     STRUCT,
436     ENUM,
437     VEC_TOKEN,
438     DEFVEC_OP,
439     DEFVEC_I,
440     DEFVEC_ALLOC,
441     ELLIPSIS,
442     PTR_ALIAS,
443     NESTED_PTR,
444     PARAM_IS,
445     NUM,
446     SCALAR,
447     ID,
448     STRING,
449     CHAR,
450     ARRAY,
451
452     /* print_token assumes that any token >= FIRST_TOKEN_WITH_VALUE may have
453        a meaningful value to be printed.  */
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
483 #endif