chiark / gitweb /
Import upstream sources.
[cparse] / cparse.h
1 #ifndef CPARSE_H
2 #define CPARSE_H
3
4 #include <config.h>
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <assert.h>
11
12 #define YYLTYPE struct location
13 #include "c-parse.h"
14
15 struct location {
16   const char *path;
17   int line;
18 };
19
20 /* memory management */
21 void *xmalloc(size_t n);
22 void *xrealloc(void *ptr, size_t n);
23 void *xmalloc_noptr(size_t n);
24 void *xrealloc_noptr(void *ptr, size_t n);
25 char *xstrdup(const char *s);
26 char *xstrndup(const char *s, size_t n);
27 #define NEW(p) ((p) = xmalloc(sizeof *(p)))
28
29 /* error reporting */
30 extern void (*exiter)(int);     /* called by fatal(), normally = exit(3) */
31 void fatal(int errno_value, const char *msg, ...);
32 void error(int errno_value, const char *msg, ...);
33
34 /* dictionaries */
35 struct dict *dict_new(void);
36 int dict_add(struct dict *d, const char *key, void *data);
37 void *dict_get(struct dict *d, const char *key);
38
39 /* scanner/parser stuff */
40 int commandline(int argc, char **argv);
41 struct external_declaration *parse(const char *filename);
42 int yyparse(void);
43 int yylex(void);
44 int yyerror(const char *msg);
45 extern FILE *yyin;
46 extern int yy_flex_debug;
47 extern int yydebug;
48 extern YYLTYPE yylloc;
49 void parser_init(FILE *fp);
50 struct declarator *lookup(const char *name);
51
52 struct declarator *numbertype(const struct expression *e);
53 /* return the type for a constant */
54
55 void inputerror(const struct location *where, const char *msg, ...);
56 enum warning_category {
57   warn_obsolete,                        /* obsolete constructs */
58   warn_compat                           /* gratuitously incompatible
59                                          * constructs */
60 };
61 void inputwarning(const struct location *where, enum warning_category cat,
62                   const char *msg, ...);
63 void suppress_errors(void);
64 void restore_errors(void);
65
66 /* globals */
67 #ifndef DECLARE_EXTERN
68 # define DECLARE_EXTERN extern
69 #endif
70 DECLARE_EXTERN int line;
71 DECLARE_EXTERN const char *path;
72 DECLARE_EXTERN struct external_declaration *translation_unit;
73 DECLARE_EXTERN int errors;
74 DECLARE_EXTERN int constant_string_literals;
75 extern const char *cpp;
76
77 /* various parsed things */
78
79 enum external_declaration_type {
80   ed_declaration,
81   ed_function_definition
82 };
83
84 struct external_declaration {
85   struct external_declaration *next;
86   enum external_declaration_type type;
87   union {
88     struct declaration *declaration;
89     struct function_definition *function_definition;
90   } u;
91 };
92
93 struct function_definition {
94   struct declaration *declaration;      /* function name + type */
95   struct declaration *args;             /* old-style args */
96   struct statement *body;               /* function body */
97 };
98
99 struct enumerator {
100   struct enumerator *next;
101   char *name;
102   struct expression *value;             /* or a null pointer */
103   struct location where;
104   struct declarator *declarator;        /* saved declarator */
105 };
106
107 struct declaration_specifiers {
108   unsigned long type_specifiers;
109 #define TS_VOID                         0x00000001
110 #define TS_BOOL                         0x00000002
111 #define TS_CHAR                         0x00000003
112 #define TS_INT                          0x00000004
113 #define TS_ENUM                         0x00000005
114 #define TS_TYPEDEF                      0x00000006
115 #define TS_FLOAT                        0x00000007
116 #define TS_DOUBLE                       0x00000008
117 #define TS_STRUCT                       0x00000009
118 #define TS_UNION                        0x0000000A
119 #define TS_GCC_VA_LIST                  0x0000000B
120
121 #define TS__BASIC                       0x000000FF
122
123 #define TS_COMPLEX                      0x00000100
124 #define TS_IMAGINARY                    0x00000200
125 #define TS__CI                          0x00000300
126
127 #define TS_LONG                         0x00000400
128 #define TS_SHORT                        0x00000800
129 #define TS_LONGLONG                     0x00000C00
130 #define TS__LENGTH                      0x00000C00
131
132 #define TS_SIGNED                       0x00001000
133 #define TS_UNSIGNED                     0x00002000
134 #define TS__SIGN                        0x00003000
135
136 #define TS_DEFINITION                   0x80000000
137   unsigned storage_class_specifiers;
138 #define SCS_TYPEDEF                     0x0001
139 #define SCS_EXTERN                      0x0002
140 #define SCS_STATIC                      0x0004
141 #define SCS_AUTO                        0x0008
142 #define SCS_REGISTER                    0x0010
143   unsigned type_qualifiers;
144 #define TQ_CONST                        0x0001
145 #define TQ_RESTRICT                     0x0002
146 #define TQ_VOLATILE                     0x0004
147   unsigned function_specifiers;
148 #define FS_INLINE                       0x0001
149   /* XXX name is scoped, really */
150   char *name;                           /* of enum, struct, union or typedef */
151   struct declaration *structure;        /* struct/union */
152   struct enumerator *enumerators;       /* enum */
153   struct declarator *type;              /* typedef */
154   unsigned long enum_compat_type;
155 };
156
157 struct declarator {
158   struct declarator *next;              /* next declarator in list */
159   char *name;                           /* name in middle of declarator */
160   struct expression *bits;              /* for bitfields */
161   struct declarator_type *declarator_type; /* type of declarator */
162   struct declaration_specifiers *declaration_specifiers;
163   struct initializer *initializer;
164   struct location where;
165   unsigned flags;
166 #define DF_PARAM                        0x0001 /* is a parameter */
167   struct declarator *resolved;          /* resolved for typedefs */
168 };
169
170 enum declarator_type_type {
171   dt_pointer,
172   dt_array,
173   dt_old_function,
174   dt_function
175 };
176
177 struct declarator_type {
178   enum declarator_type_type type;
179   unsigned type_qualifiers;             /* TQ_... */
180   unsigned storage_class_specifiers;    /* SCS_STATIC or 0 */
181   struct declarator_type *next;         /* return type, element type, etc */
182   union {
183     struct {
184       struct expression *size;
185       unsigned flags;
186 #define AF_STAR                         0x0001
187     } array;
188     struct {
189       struct identifier_list *args;     /* arguments (no types) */
190     } old_function;
191     struct {
192       struct declaration *args;         /* arguments (one declarator per
193                                          * declaration) */
194       int variadic;                     /* true if ", ..." */
195     } function;
196   } u;
197   struct location where;
198 };
199
200 struct identifier_list {
201   struct identifier_list *next;
202   char *id;
203 };
204
205 struct declaration {
206   struct declaration *next;
207   struct declaration_specifiers *declaration_specifiers;
208   struct declarator *declarator_list;
209   struct location where;
210 };
211
212 enum expression_type {
213   ex_paren,
214   ex_prefix,
215   ex_postfix,
216   ex_binary,
217   ex_id,
218   ex_number,
219   ex_char,
220   ex_string,
221   ex_wchar,
222   ex_wstring,
223   ex_fncall,
224   ex_compound_literal,
225   ex_sizeof_type,
226   ex_cast,
227   ex_implicit_conversion,
228   ex_gcc_va_arg,
229   ex_gcc_expect
230 };
231
232 struct expression {
233   enum expression_type type;
234   int operator;
235   struct declarator *valuetype;
236   union {
237     struct {
238       struct expression *l, *r;
239     } binary;
240     struct expression *unary;
241     char *constant;
242     char *name;
243     struct {
244       struct expression *fn;
245       struct expression_list *args;
246     } fncall;
247     struct {
248       struct expression *arg;
249       struct declaration *type;
250     } gcc_va_arg;
251     struct {
252       struct declaration *type;
253       struct initializer *value;
254     } compound_literal;
255     struct declaration *type;
256     struct expression *cast;            /* target type is valuetype */
257   } u;
258   struct location where;
259 };
260
261 struct expression_list {
262   struct expression_list *next;
263   struct expression *e;
264 };
265
266 enum initializer_type {
267   in_expr,
268   in_list
269 };
270
271 enum designator_syntax {
272   des_c99,
273   des_gcc_colon,
274   des_gcc_raw
275 };
276
277 struct initializer {
278   struct initializer *next;
279   enum initializer_type type;
280   struct designator *designator;
281   union {
282     struct expression *expr;
283     struct initializer *list;
284   } u;
285   enum designator_syntax syntax;
286 };
287
288 enum designator_type {
289   des_expr,
290   des_field
291 };
292
293 struct designator {
294   enum designator_type type;
295   union {
296     struct expression *expr;
297     char *name;
298   } u;
299   struct designator *next;
300   struct location where;
301 };
302
303 enum statement_type {
304   st_label,
305   st_case,
306   st_default,
307   st_declaration,
308   st_expression,
309   st_if,
310   st_switch,
311   st_while,
312   st_do,
313   st_for,
314   st_for_declaration,
315   st_goto,
316   st_continue,
317   st_break,
318   st_return,
319   st_compound
320 };
321
322 struct statement {
323   struct statement *next;
324   enum statement_type type;
325   union {
326     char *goto_;                        /* goto */
327     struct {
328       char *label;
329       struct statement *body;
330     } label;
331     struct {
332       struct expression *value;
333       struct statement *body;
334     } case_;
335     struct statement *default_;
336     struct {
337       struct statement *body;
338       struct location endwhere;         /* closing } */
339       struct scope *scope;
340     } compound;
341     struct declaration *declaration;
342     struct expression *expression;      /* or a null pointer; also return */
343     struct {
344       struct expression *cond;
345       struct statement *true;
346       struct statement *false;
347     } if_;
348     struct {
349       struct expression *cond;
350       struct statement *body;
351     } switch_;
352     struct {
353       struct expression *cond;
354       struct statement *body;
355     } while_;                           /* including do-while */
356     struct {
357       struct expression *init;
358       struct expression *cond;
359       struct expression *iter;
360       struct statement *body;
361     } for_;
362     struct {
363       struct declaration *init;
364       struct expression *cond;
365       struct expression *iter;
366       struct statement *body;
367     } for_declaration;
368   } u;
369   struct location where;
370 };
371
372 /* dump.c ********************************************************************/
373
374 typedef struct dump_state dump_state;
375
376 dump_state *dump_new(FILE *fp);
377
378 int dump_declaration_specifiers(dump_state *dump,
379                                 const struct declaration_specifiers *ds,
380                                 int indent);
381 int dump_declarator(dump_state *dump,
382                     const struct declarator *d,
383                     int indent);
384 int dump_declaration(dump_state *dump,
385                      const struct declaration *d,
386                      int indent);
387 int dump_translation_unit(dump_state *dump,
388                           const struct external_declaration *ed,
389                           int indent);
390 int dump_expression(dump_state *dump,
391                     const struct expression *e,
392                     int ind);
393 int dump_initializer(dump_state *dump,
394                      const struct initializer *i,
395                      int ind);
396 int dump_function_definition(dump_state *dump,
397                              const struct function_definition *fd,
398                              int ind);
399 int dump_statement(dump_state *dump,
400                    const struct statement *fd,
401                    int ind);
402 void dump_locations(dump_state *dump, int flag);
403
404 /* type.c ********************************************************************/
405
406 struct declaration_specifiers *merge_declaration_specifiers
407                                (struct declaration_specifiers *a,
408                                 struct declaration_specifiers *b);
409 /* combine two declaration_specifires structs that are bits of the same list of
410  * declaration specifiers, checking relevant constraints on the way */
411
412 int is_integral_type(const struct declarator *d);
413 /* return true if D has integral type.  typedefs should have been resolved. */
414
415 int is_arithmetic_type(const struct declarator *d);
416 /* return true if D has arithmetic type.  typedefs should have been
417  * resolved. */
418
419 int is_pointer_type(const struct declarator *d);
420 /* return true if D has pointer type.  typedefs should have been resolved. */
421
422 int is_scalar_type(const struct declarator *d);
423 /* return true if D has scalar type.  typedefs should have been resolved. */
424
425 struct declarator *resolve_typedefs(struct declarator *d);
426 /* resolve typedefs in value types */
427
428 struct declarator *target_type(struct declarator *d);
429 /* return (as a declarator) the target type of an array or whatever.  Usually
430  * only makes sense after typedefs have been resolved. */
431
432 size_t width(unsigned long ts);
433 /* return the width of type ts */
434
435 /* expr.c ********************************************************************/
436
437 struct expression *array_decay(struct expression *e);
438 /* array-to-pointer decay (C99 6.3.2.1) */
439
440 struct expression *integer_promotions(struct expression *e);
441 /* integral promotions (C99 6.3.1.1)
442  * anything >= (unsigned) int stays as it is
443  * otherwise if int is good enough we use that
444  * otherwise we use unsigned
445  */
446
447 struct declarator *usual_arithmetic_conversions(struct expression **lp,
448                                                 struct expression **rp,
449                                                 const struct location *where);
450 /* perform usual arithmetic conversions, return the shared value type */
451
452 struct expression *expr(enum expression_type type,
453                         const struct location *where);
454 struct expression *binary(int op,
455                           struct expression *l,
456                           struct expression *r,
457                           const struct location *where);
458 struct expression *prefix(int op, struct expression *r,
459                           const struct location *where);
460 struct expression *postfix(int op, struct expression *l,
461                            const struct location *where);
462 struct expression *paren(struct expression *content,
463                          const struct location *where);
464 struct expression *stringexpr(enum expression_type type,
465                               char *value,
466                               const struct location *where);
467 struct expression *fncallexpr(struct expression *f,
468                               struct expression_list *l,
469                               const struct location *where);
470 /* various expression constructors */
471
472 /* constraints.c *************************************************************/
473
474 void ucn_constraints(unsigned long u);
475
476 enum declarator_context {
477   dc_function_definition,
478   dc_function_parameter,
479   dc_function_definition_parameter,
480   dc_struct_member,
481   dc_cast,
482   dc_compound_literal,
483   dc_sizeof,
484   dc_file_scope,
485   dc_block_scope
486 };
487
488 enum type_category {
489   tc_complete,
490   tc_incomplete,
491   tc_function
492 };
493
494 enum type_category get_type_category(const struct declaration_specifiers *ds,
495                                      const struct declarator_type *dt);
496 int is_void_args(const struct declarator_type *dt);
497 void declarator_constraints(const struct declaration_specifiers *ds,
498                             const struct declarator *d,
499                             enum declarator_context context,
500                             int argno);
501 void declaration_constraints(const struct declaration *decl,
502                              enum declarator_context context);
503
504 /* scope.c *******************************************************************/
505
506 struct scope {
507   struct scope *parent;                 /* containing scope */
508   struct dict *declarations;            /* declarations in this scope */
509 };
510
511 void enter_scope(void);
512 /* enter a new scope */
513
514 void exit_scope(void);
515 /* exit the current scope */
516
517 void add_declaration(struct declarator *d);
518 /* add a declaration to the current scope.  The declaration specifiers need not
519  * have been filled in yet but must get filled in eventually. */
520
521 struct declarator *lookup(const char *name);
522 /* look up a name, starting in the current scope and working back up to file
523  * scope. */
524
525 void scope_init(void);
526 /* initialize scope support before parsing a file.  After calling this you are
527  * in file scope. */
528
529 /* gcc.c *********************************************************************/
530
531 void gcc_extensions(void);
532 /* enable various gcc-isms */
533
534 /*****************************************************************************/
535
536 #include "platform.h"
537
538 #endif /* CPARSE_H */
539
540 /*
541 Local Variables:
542 c-basic-offset:2
543 comment-column:40
544 fill-column:79
545 End:
546 */