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