Add a (partial) format string checker for wide string functions (currently wprintf...
[cparser] / ast_t.h
1 #ifndef AST_T_H
2 #define AST_T_H
3
4 #include <libfirm/firm_types.h>
5 #include <assert.h>
6
7 #include "ast.h"
8 #include "symbol.h"
9 #include "token_t.h"
10 #include "type.h"
11 #include "adt/obst.h"
12
13 extern struct obstack ast_obstack;
14
15 typedef enum {
16         EXPR_UNKNOWN = 0,
17         EXPR_INVALID,
18         EXPR_REFERENCE,
19         EXPR_CONST,
20         EXPR_STRING_LITERAL,
21         EXPR_WIDE_STRING_LITERAL,
22         EXPR_CALL,
23         EXPR_CONDITIONAL,
24         EXPR_SELECT,
25         EXPR_ARRAY_ACCESS,
26         EXPR_SIZEOF,
27         EXPR_CLASSIFY_TYPE,
28
29         EXPR_FUNCTION,
30         EXPR_PRETTY_FUNCTION,
31         EXPR_BUILTIN_SYMBOL,
32         EXPR_OFFSETOF,
33         EXPR_VA_START,
34         EXPR_VA_ARG,
35         EXPR_STATEMENT,
36
37         EXPR_UNARY_FIRST,
38         EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
39         EXPR_UNARY_PLUS,
40         EXPR_UNARY_BITWISE_NEGATE,
41         EXPR_UNARY_NOT,
42         EXPR_UNARY_DEREFERENCE,
43         EXPR_UNARY_TAKE_ADDRESS,
44         EXPR_UNARY_POSTFIX_INCREMENT,
45         EXPR_UNARY_POSTFIX_DECREMENT,
46         EXPR_UNARY_PREFIX_INCREMENT,
47         EXPR_UNARY_PREFIX_DECREMENT,
48         EXPR_UNARY_CAST,
49         EXPR_UNARY_CAST_IMPLICIT, /* compiler generated cast */
50         EXPR_UNARY_LAST = EXPR_UNARY_CAST_IMPLICIT,
51
52         EXPR_BINARY_FIRST,
53         EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
54         EXPR_BINARY_SUB,
55         EXPR_BINARY_MUL,
56         EXPR_BINARY_DIV,
57         EXPR_BINARY_MOD,
58         EXPR_BINARY_EQUAL,
59         EXPR_BINARY_NOTEQUAL,
60         EXPR_BINARY_LESS,
61         EXPR_BINARY_LESSEQUAL,
62         EXPR_BINARY_GREATER,
63         EXPR_BINARY_GREATEREQUAL,
64         EXPR_BINARY_BITWISE_AND,
65         EXPR_BINARY_BITWISE_OR,
66         EXPR_BINARY_BITWISE_XOR,
67         EXPR_BINARY_LOGICAL_AND,
68         EXPR_BINARY_LOGICAL_OR,
69         EXPR_BINARY_SHIFTLEFT,
70         EXPR_BINARY_SHIFTRIGHT,
71         EXPR_BINARY_ASSIGN,
72         EXPR_BINARY_MUL_ASSIGN,
73         EXPR_BINARY_DIV_ASSIGN,
74         EXPR_BINARY_MOD_ASSIGN,
75         EXPR_BINARY_ADD_ASSIGN,
76         EXPR_BINARY_SUB_ASSIGN,
77         EXPR_BINARY_SHIFTLEFT_ASSIGN,
78         EXPR_BINARY_SHIFTRIGHT_ASSIGN,
79         EXPR_BINARY_BITWISE_AND_ASSIGN,
80         EXPR_BINARY_BITWISE_XOR_ASSIGN,
81         EXPR_BINARY_BITWISE_OR_ASSIGN,
82         EXPR_BINARY_COMMA,
83
84         EXPR_BINARY_BUILTIN_EXPECT,
85         EXPR_BINARY_ISGREATER,
86         EXPR_BINARY_ISGREATEREQUAL,
87         EXPR_BINARY_ISLESS,
88         EXPR_BINARY_ISLESSEQUAL,
89         EXPR_BINARY_ISLESSGREATER,
90         EXPR_BINARY_ISUNORDERED,
91         EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
92 } expression_type_t;
93
94 /* convenience macros */
95 #define EXPR_BINARY_CASES                  \
96         case EXPR_BINARY_ADD:                  \
97         case EXPR_BINARY_SUB:                  \
98         case EXPR_BINARY_MUL:                  \
99         case EXPR_BINARY_DIV:                  \
100         case EXPR_BINARY_MOD:                  \
101         case EXPR_BINARY_EQUAL:                \
102         case EXPR_BINARY_NOTEQUAL:             \
103         case EXPR_BINARY_LESS:                 \
104         case EXPR_BINARY_LESSEQUAL:            \
105         case EXPR_BINARY_GREATER:              \
106         case EXPR_BINARY_GREATEREQUAL:         \
107         case EXPR_BINARY_BITWISE_AND:          \
108         case EXPR_BINARY_BITWISE_OR:           \
109         case EXPR_BINARY_BITWISE_XOR:          \
110         case EXPR_BINARY_LOGICAL_AND:          \
111         case EXPR_BINARY_LOGICAL_OR:           \
112         case EXPR_BINARY_SHIFTLEFT:            \
113         case EXPR_BINARY_SHIFTRIGHT:           \
114         case EXPR_BINARY_ASSIGN:               \
115         case EXPR_BINARY_MUL_ASSIGN:           \
116         case EXPR_BINARY_DIV_ASSIGN:           \
117         case EXPR_BINARY_MOD_ASSIGN:           \
118         case EXPR_BINARY_ADD_ASSIGN:           \
119         case EXPR_BINARY_SUB_ASSIGN:           \
120         case EXPR_BINARY_SHIFTLEFT_ASSIGN:     \
121         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:    \
122         case EXPR_BINARY_BITWISE_AND_ASSIGN:   \
123         case EXPR_BINARY_BITWISE_XOR_ASSIGN:   \
124         case EXPR_BINARY_BITWISE_OR_ASSIGN:    \
125         case EXPR_BINARY_COMMA:                \
126         case EXPR_BINARY_BUILTIN_EXPECT:       \
127         case EXPR_BINARY_ISGREATER:            \
128         case EXPR_BINARY_ISGREATEREQUAL:       \
129         case EXPR_BINARY_ISLESS:               \
130         case EXPR_BINARY_ISLESSEQUAL:          \
131         case EXPR_BINARY_ISLESSGREATER:        \
132         case EXPR_BINARY_ISUNORDERED:
133
134 #define EXPR_UNARY_CASES                   \
135         case EXPR_UNARY_NEGATE:                \
136         case EXPR_UNARY_PLUS:                  \
137         case EXPR_UNARY_BITWISE_NEGATE:        \
138         case EXPR_UNARY_NOT:                   \
139         case EXPR_UNARY_DEREFERENCE:           \
140         case EXPR_UNARY_TAKE_ADDRESS:          \
141         case EXPR_UNARY_POSTFIX_INCREMENT:     \
142         case EXPR_UNARY_POSTFIX_DECREMENT:     \
143         case EXPR_UNARY_PREFIX_INCREMENT:      \
144         case EXPR_UNARY_PREFIX_DECREMENT:      \
145         case EXPR_UNARY_CAST:                  \
146         case EXPR_UNARY_CAST_IMPLICIT:
147
148 struct context_t {
149         declaration_t *declarations;
150 };
151
152 struct expression_base_t {
153         expression_type_t   type;
154         type_t             *datatype;
155         source_position_t   source_position;
156 };
157
158 struct const_expression_t {
159         expression_base_t  expression;
160         union {
161                 long long   int_value;
162                 long double float_value;
163         } v;
164 };
165
166 struct string_literal_expression_t {
167         expression_base_t  expression;
168         const char        *value;
169 };
170
171 struct wide_string_literal_expression_t {
172         expression_base_t  expression;
173         wide_string_t      value;
174 };
175
176 struct builtin_symbol_expression_t {
177         expression_base_t  expression;
178         symbol_t          *symbol;
179 };
180
181 struct reference_expression_t {
182         expression_base_t  expression;
183         symbol_t          *symbol;
184         declaration_t     *declaration;
185 };
186
187 struct call_argument_t {
188         expression_t    *expression;
189         call_argument_t *next;
190 };
191
192 struct call_expression_t {
193         expression_base_t  expression;
194         expression_t      *function;
195         call_argument_t   *arguments;
196 };
197
198 struct unary_expression_t {
199         expression_base_t  expression;
200         expression_t      *value;
201 };
202
203 struct binary_expression_t {
204         expression_base_t  expression;
205         expression_t      *left;
206         expression_t      *right;
207 };
208
209 struct select_expression_t {
210         expression_base_t  expression;
211         expression_t      *compound;
212         symbol_t          *symbol;
213
214         declaration_t     *compound_entry;
215 };
216
217 struct array_access_expression_t {
218         expression_base_t  expression;
219         expression_t      *array_ref;
220         expression_t      *index;
221         bool               flipped; /* index/ref was written in a 5[a] way */
222 };
223
224 struct sizeof_expression_t {
225         expression_base_t  expression;
226         type_t            *type;
227         expression_t      *size_expression;
228 };
229
230 struct designator_t {
231         symbol_t     *symbol;
232         expression_t *array_access;
233         designator_t *next;
234 };
235
236 struct offsetof_expression_t {
237         expression_base_t  expression;
238         type_t            *type;
239         designator_t      *designator;
240 };
241
242 struct va_start_expression_t {
243         expression_base_t  expression;
244         expression_t      *ap;
245         declaration_t     *parameter;
246 };
247
248 struct va_arg_expression_t {
249         expression_base_t  expression;
250         expression_t      *ap;
251 };
252
253 struct conditional_expression_t {
254         expression_base_t  expression;
255         expression_t      *condition;
256         expression_t      *true_expression;
257         expression_t      *false_expression;
258 };
259
260 struct statement_expression_t {
261         expression_base_t  expression;
262         statement_t       *statement;
263 };
264
265 struct classify_type_expression_t {
266         expression_base_t  expression;
267         expression_t      *type_expression;
268 };
269
270 union expression_t {
271         expression_type_t                type;
272         expression_base_t                base;
273         const_expression_t               conste;
274         string_literal_expression_t      string;
275         wide_string_literal_expression_t wide_string;
276         builtin_symbol_expression_t      builtin_symbol;
277         reference_expression_t           reference;
278         call_expression_t                call;
279         unary_expression_t               unary;
280         binary_expression_t              binary;
281         select_expression_t              select;
282         array_access_expression_t        array_access;
283         sizeof_expression_t              sizeofe;
284         offsetof_expression_t            offsetofe;
285         va_start_expression_t            va_starte;
286         va_arg_expression_t              va_arge;
287         conditional_expression_t         conditional;
288         statement_expression_t           statement;
289         classify_type_expression_t       classify_type;
290 };
291
292 typedef enum {
293         STORAGE_CLASS_NONE,
294         STORAGE_CLASS_TYPEDEF,
295         STORAGE_CLASS_EXTERN,
296         STORAGE_CLASS_STATIC,
297         STORAGE_CLASS_AUTO,
298         STORAGE_CLASS_REGISTER,
299         STORAGE_CLASS_ENUM_ENTRY,
300         STORAGE_CLASS_THREAD,
301         STORAGE_CLASS_THREAD_EXTERN,
302         STORAGE_CLASS_THREAD_STATIC
303 } storage_class_tag_t;
304
305 typedef enum {
306         NAMESPACE_NORMAL,
307         NAMESPACE_STRUCT,
308         NAMESPACE_UNION,
309         NAMESPACE_ENUM,
310         NAMESPACE_LABEL,
311 } namespace_t;
312
313 typedef enum {
314         INITIALIZER_VALUE,
315         INITIALIZER_LIST,
316         INITIALIZER_STRING,
317         INITIALIZER_WIDE_STRING
318 } initializer_type_t;
319
320 struct initializer_base_t {
321         initializer_type_t type;
322 };
323
324 struct initializer_value_t {
325         initializer_base_t  initializer;
326         expression_t       *value;
327 };
328
329 struct initializer_list_t {
330         initializer_base_t  initializer;
331         size_t              len;
332         initializer_t      *initializers[];
333 };
334
335 struct initializer_string_t {
336         initializer_base_t  initializer;
337         const char         *string;
338 };
339
340 struct initializer_wide_string_t {
341         initializer_base_t  initializer;
342         wide_string_t       string;
343 };
344
345 union initializer_t {
346         initializer_type_t        type;
347         initializer_base_t        base;
348         initializer_value_t       value;
349         initializer_list_t        list;
350         initializer_string_t      string;
351         initializer_wide_string_t wide_string;
352 };
353
354 typedef enum {
355         DM_DLLIMPORT   = (1 << 0),
356         DM_DLLEXPORT   = (1 << 1),
357         DM_THREAD      = (1 << 2),
358         DM_NAKED       = (1 << 3),
359         DM_FORCEINLINE = (1 << 4),
360         DM_NOTHROW     = (1 << 5),
361         DM_NORETURN    = (1 << 6),
362         DM_NOINLINE    = (1 << 7)
363 } decl_modifier_t;
364
365 typedef unsigned short decl_modifiers_t;
366
367 struct declaration_t {
368         unsigned char       namespc;
369         unsigned char       storage_class;
370         decl_modifiers_t    decl_modifiers;
371         unsigned int        address_taken : 1;
372         unsigned int        is_inline     : 1;
373         type_t             *type;
374         symbol_t           *symbol;
375         source_position_t   source_position;
376         union {
377                 bool            is_defined;
378                 statement_t    *statement;
379                 initializer_t  *initializer;
380                 expression_t   *enum_value;
381         } init;
382         context_t           context;
383         context_t          *parent_context;
384
385         /** next declaration in a context */
386         declaration_t      *next;
387         /** next declaration with same symbol */
388         declaration_t      *symbol_next;
389
390         unsigned char       declaration_type; /* used in ast2firm module */
391         union {
392                 unsigned int    value_number;     /* used in ast2firm module */
393                 ir_entity      *entity;           /* used in ast2firm module */
394                 ir_node        *block;            /* used in ast2firm module */
395                 tarval         *enum_val;         /* used in ast2firm module */
396         } v;
397 };
398
399 typedef enum {
400         STATEMENT_INVALID,
401         STATEMENT_COMPOUND,
402         STATEMENT_RETURN,
403         STATEMENT_DECLARATION,
404         STATEMENT_IF,
405         STATEMENT_SWITCH,
406         STATEMENT_EXPRESSION,
407         STATEMENT_CONTINUE,
408         STATEMENT_BREAK,
409         STATEMENT_GOTO,
410         STATEMENT_LABEL,
411         STATEMENT_CASE_LABEL,
412         STATEMENT_WHILE,
413         STATEMENT_DO_WHILE,
414         STATEMENT_FOR,
415         STATEMENT_ASM
416 } statement_type_t;
417
418 struct statement_base_t {
419         statement_type_t   type;
420         statement_t       *next;
421         source_position_t  source_position;
422 };
423
424 struct return_statement_t {
425         statement_base_t  statement;
426         expression_t     *return_value;
427 };
428
429 struct compound_statement_t {
430         statement_base_t  statement;
431         statement_t      *statements;
432         context_t         context;
433 };
434
435 struct declaration_statement_t {
436         statement_base_t  statement;
437         declaration_t    *declarations_begin;
438         declaration_t    *declarations_end;
439 };
440
441 struct if_statement_t {
442         statement_base_t  statement;
443         expression_t     *condition;
444         statement_t      *true_statement;
445         statement_t      *false_statement;
446 };
447
448 struct switch_statement_t {
449         statement_base_t  statement;
450         expression_t     *expression;
451         statement_t      *body;
452 };
453
454 struct goto_statement_t {
455         statement_base_t  statement;
456         declaration_t    *label;
457 };
458
459 struct case_label_statement_t {
460         statement_base_t  statement;
461         expression_t     *expression;
462         statement_t      *label_statement;
463 };
464
465 struct label_statement_t {
466         statement_base_t  statement;
467         declaration_t    *label;
468         statement_t      *label_statement;
469 };
470
471 struct expression_statement_t {
472         statement_base_t  statement;
473         expression_t     *expression;
474 };
475
476 struct while_statement_t {
477         statement_base_t  statement;
478         expression_t     *condition;
479         statement_t      *body;
480 };
481
482 struct do_while_statement_t {
483         statement_base_t  statement;
484         expression_t     *condition;
485         statement_t      *body;
486 };
487
488 struct for_statement_t {
489         statement_base_t  statement;
490         expression_t     *initialisation;
491         expression_t     *condition;
492         expression_t     *step;
493         statement_t      *body;
494         context_t         context;
495 };
496
497 struct asm_constraint_t {
498         const char       *constraints;
499         expression_t     *expression;
500         symbol_t         *symbol;
501         asm_constraint_t *next;
502 };
503
504 struct asm_clobber_t {
505         const char    *clobber;
506         asm_clobber_t *next;
507 };
508
509 struct asm_statement_t {
510         statement_base_t  statement;
511         const char       *asm_text;
512         asm_constraint_t *inputs;
513         asm_constraint_t *outputs;
514         asm_clobber_t    *clobbers;
515         bool              is_volatile;
516 };
517
518 union statement_t {
519         statement_type_t         type;
520         statement_base_t         base;
521         return_statement_t       returns;
522         compound_statement_t     compound;
523         declaration_statement_t  declaration;
524         if_statement_t           ifs;
525         switch_statement_t       switchs;
526         goto_statement_t         gotos;
527         case_label_statement_t   case_label;
528         label_statement_t        label;
529         expression_statement_t   expression;
530         while_statement_t        whiles;
531         do_while_statement_t     do_while;
532         for_statement_t          fors;
533         asm_statement_t          asms;
534 };
535
536 struct translation_unit_t {
537         context_t context;
538 };
539
540 static inline
541 void *_allocate_ast(size_t size)
542 {
543         return obstack_alloc(&ast_obstack, size);
544 }
545
546 #define allocate_ast(size)                 _allocate_ast(size)
547
548 #endif