OSDN Git Service

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