Remove the unused field value from struct funcname_expression_t.
[cparser] / ast_t.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #ifndef AST_T_H
21 #define AST_T_H
22
23 #include <libfirm/firm_types.h>
24 #include <assert.h>
25
26 #include "ast.h"
27 #include "symbol.h"
28 #include "token_t.h"
29 #include "type.h"
30 #include "entity_t.h"
31 #include "adt/obst.h"
32
33 /** The AST obstack contains all data that must stay in the AST. */
34 extern struct obstack ast_obstack;
35
36 /**
37  * Operator precedence classes
38  */
39 typedef enum precedence_t {
40         PREC_BOTTOM,
41         PREC_EXPRESSION,     /* ,                                  left to right */
42         PREC_ASSIGNMENT,     /* = += -= *= /= %= <<= >>= &= ^= |=  right to left */
43         PREC_CONDITIONAL,    /* ?:                                 right to left */
44         PREC_LOGICAL_OR,     /* ||                                 left to right */
45         PREC_LOGICAL_AND,    /* &&                                 left to right */
46         PREC_OR,             /* |                                  left to right */
47         PREC_XOR,            /* ^                                  left to right */
48         PREC_AND,            /* &                                  left to right */
49         PREC_EQUALITY,       /* == !=                              left to right */
50         PREC_RELATIONAL,     /* < <= > >=                          left to right */
51         PREC_SHIFT,          /* << >>                              left to right */
52         PREC_ADDITIVE,       /* + -                                left to right */
53         PREC_MULTIPLICATIVE, /* * / %                              left to right */
54         PREC_CAST,           /* (type)                             right to left */
55         PREC_UNARY,          /* ! ~ ++ -- + - * & sizeof           right to left */
56         PREC_POSTFIX,        /* () [] -> .                         left to right */
57         PREC_PRIMARY,
58         PREC_TOP
59 } precedence_t;
60
61 /**
62  * Expression kinds.
63  */
64 typedef enum expression_kind_t {
65         EXPR_ERROR = 1,
66         EXPR_REFERENCE,
67         EXPR_ENUM_CONSTANT,
68         EXPR_LITERAL_BOOLEAN,
69         EXPR_LITERAL_INTEGER,
70         EXPR_LITERAL_FLOATINGPOINT,
71         EXPR_LITERAL_CHARACTER,
72         EXPR_LITERAL_MS_NOOP, /**< MS __noop extension */
73         EXPR_STRING_LITERAL,
74         EXPR_COMPOUND_LITERAL,
75         EXPR_CALL,
76         EXPR_CONDITIONAL,
77         EXPR_SELECT,
78         EXPR_ARRAY_ACCESS,
79         EXPR_SIZEOF,
80         EXPR_CLASSIFY_TYPE,
81         EXPR_ALIGNOF,
82
83         EXPR_FUNCNAME,
84         EXPR_BUILTIN_CONSTANT_P,
85         EXPR_BUILTIN_TYPES_COMPATIBLE_P,
86         EXPR_OFFSETOF,
87         EXPR_VA_START,
88         EXPR_VA_ARG,
89         EXPR_VA_COPY,
90         EXPR_STATEMENT,
91         EXPR_LABEL_ADDRESS, /**< GCC extension &&label operator */
92
93         EXPR_UNARY_FIRST,
94         EXPR_UNARY_NEGATE = EXPR_UNARY_FIRST,
95         EXPR_UNARY_PLUS,
96         EXPR_UNARY_BITWISE_NEGATE,
97         EXPR_UNARY_NOT,
98         EXPR_UNARY_DEREFERENCE,
99         EXPR_UNARY_TAKE_ADDRESS,
100         EXPR_UNARY_POSTFIX_INCREMENT,
101         EXPR_UNARY_POSTFIX_DECREMENT,
102         EXPR_UNARY_PREFIX_INCREMENT,
103         EXPR_UNARY_PREFIX_DECREMENT,
104         EXPR_UNARY_CAST,
105         EXPR_UNARY_ASSUME,        /**< MS __assume() */
106         EXPR_UNARY_DELETE,
107         EXPR_UNARY_DELETE_ARRAY,
108         EXPR_UNARY_THROW,
109         EXPR_UNARY_LAST = EXPR_UNARY_THROW,
110
111         EXPR_BINARY_FIRST,
112         EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
113         EXPR_BINARY_SUB,
114         EXPR_BINARY_MUL,
115         EXPR_BINARY_DIV,
116         EXPR_BINARY_MOD,
117         EXPR_BINARY_EQUAL,
118         EXPR_BINARY_NOTEQUAL,
119         EXPR_BINARY_LESS,
120         EXPR_BINARY_LESSEQUAL,
121         EXPR_BINARY_GREATER,
122         EXPR_BINARY_GREATEREQUAL,
123         EXPR_BINARY_BITWISE_AND,
124         EXPR_BINARY_BITWISE_OR,
125         EXPR_BINARY_BITWISE_XOR,
126         EXPR_BINARY_LOGICAL_AND,
127         EXPR_BINARY_LOGICAL_OR,
128         EXPR_BINARY_SHIFTLEFT,
129         EXPR_BINARY_SHIFTRIGHT,
130         EXPR_BINARY_ASSIGN,
131         EXPR_BINARY_MUL_ASSIGN,
132         EXPR_BINARY_DIV_ASSIGN,
133         EXPR_BINARY_MOD_ASSIGN,
134         EXPR_BINARY_ADD_ASSIGN,
135         EXPR_BINARY_SUB_ASSIGN,
136         EXPR_BINARY_SHIFTLEFT_ASSIGN,
137         EXPR_BINARY_SHIFTRIGHT_ASSIGN,
138         EXPR_BINARY_BITWISE_AND_ASSIGN,
139         EXPR_BINARY_BITWISE_XOR_ASSIGN,
140         EXPR_BINARY_BITWISE_OR_ASSIGN,
141         EXPR_BINARY_COMMA,
142
143         EXPR_BINARY_ISGREATER,
144         EXPR_BINARY_ISGREATEREQUAL,
145         EXPR_BINARY_ISLESS,
146         EXPR_BINARY_ISLESSEQUAL,
147         EXPR_BINARY_ISLESSGREATER,
148         EXPR_BINARY_ISUNORDERED,
149         EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
150 } expression_kind_t;
151
152 typedef enum funcname_kind_t {
153         FUNCNAME_FUNCTION,           /**< C99 __func__, older __FUNCTION__ */
154         FUNCNAME_PRETTY_FUNCTION,    /**< GNUC __PRETTY_FUNCTION__ */
155         FUNCNAME_FUNCSIG,            /**< MS __FUNCSIG__ */
156         FUNCNAME_FUNCDNAME           /**< MS __FUNCDNAME__ */
157 } funcname_kind_t;
158
159 /* convenience macros */
160 #define EXPR_BINARY_CASES              \
161              EXPR_BINARY_ADD:                \
162         case EXPR_BINARY_SUB:                \
163         case EXPR_BINARY_MUL:                \
164         case EXPR_BINARY_DIV:                \
165         case EXPR_BINARY_MOD:                \
166         case EXPR_BINARY_EQUAL:              \
167         case EXPR_BINARY_NOTEQUAL:           \
168         case EXPR_BINARY_LESS:               \
169         case EXPR_BINARY_LESSEQUAL:          \
170         case EXPR_BINARY_GREATER:            \
171         case EXPR_BINARY_GREATEREQUAL:       \
172         case EXPR_BINARY_BITWISE_AND:        \
173         case EXPR_BINARY_BITWISE_OR:         \
174         case EXPR_BINARY_BITWISE_XOR:        \
175         case EXPR_BINARY_LOGICAL_AND:        \
176         case EXPR_BINARY_LOGICAL_OR:         \
177         case EXPR_BINARY_SHIFTLEFT:          \
178         case EXPR_BINARY_SHIFTRIGHT:         \
179         case EXPR_BINARY_ASSIGN:             \
180         case EXPR_BINARY_MUL_ASSIGN:         \
181         case EXPR_BINARY_DIV_ASSIGN:         \
182         case EXPR_BINARY_MOD_ASSIGN:         \
183         case EXPR_BINARY_ADD_ASSIGN:         \
184         case EXPR_BINARY_SUB_ASSIGN:         \
185         case EXPR_BINARY_SHIFTLEFT_ASSIGN:   \
186         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:  \
187         case EXPR_BINARY_BITWISE_AND_ASSIGN: \
188         case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
189         case EXPR_BINARY_BITWISE_OR_ASSIGN:  \
190         case EXPR_BINARY_COMMA:              \
191         case EXPR_BINARY_ISGREATER:          \
192         case EXPR_BINARY_ISGREATEREQUAL:     \
193         case EXPR_BINARY_ISLESS:             \
194         case EXPR_BINARY_ISLESSEQUAL:        \
195         case EXPR_BINARY_ISLESSGREATER:      \
196         case EXPR_BINARY_ISUNORDERED
197
198 /**
199  * unary expression with mandatory operand
200  */
201 #define EXPR_UNARY_CASES_MANDATORY   \
202              EXPR_UNARY_NEGATE:            \
203         case EXPR_UNARY_PLUS:              \
204         case EXPR_UNARY_BITWISE_NEGATE:    \
205         case EXPR_UNARY_NOT:               \
206         case EXPR_UNARY_DEREFERENCE:       \
207         case EXPR_UNARY_TAKE_ADDRESS:      \
208         case EXPR_UNARY_POSTFIX_INCREMENT: \
209         case EXPR_UNARY_POSTFIX_DECREMENT: \
210         case EXPR_UNARY_PREFIX_INCREMENT:  \
211         case EXPR_UNARY_PREFIX_DECREMENT:  \
212         case EXPR_UNARY_CAST:              \
213         case EXPR_UNARY_ASSUME:            \
214         case EXPR_UNARY_DELETE:            \
215         case EXPR_UNARY_DELETE_ARRAY
216
217 /**
218  * unary expression with optional operand
219  */
220 #define EXPR_UNARY_CASES_OPTIONAL \
221         EXPR_UNARY_THROW
222
223 #define EXPR_UNARY_CASES           \
224              EXPR_UNARY_CASES_MANDATORY: \
225         case EXPR_UNARY_CASES_OPTIONAL
226
227 #define EXPR_LITERAL_CASES                     \
228              EXPR_LITERAL_BOOLEAN:                   \
229         case EXPR_LITERAL_INTEGER:                   \
230         case EXPR_LITERAL_FLOATINGPOINT:             \
231         case EXPR_LITERAL_MS_NOOP
232
233 /**
234  * The base class of every expression.
235  */
236 struct expression_base_t {
237         expression_kind_t   kind;            /**< The expression kind. */
238         type_t             *type;            /**< The type of the expression. */
239         source_position_t   source_position; /**< The source position of this expression. */
240         bool                parenthesized : 1;
241 #ifndef NDEBUG
242         bool                transformed : 1;     /**< Set if this expression was transformed. */
243 #endif
244         bool                implicit : 1;    /**< compiler generated expression.
245                                                   Examples:
246                                                      select into anonymous structs
247                                                      implicit casts
248                                               */
249 };
250
251 /**
252  * integer, float and boolean constants
253  */
254 struct literal_expression_t {
255         expression_base_t  base;
256         string_t           value;
257         string_t           suffix;
258
259         /* ast2firm data */
260         ir_tarval         *target_value;
261 };
262
263 /**
264  * string and character literals
265  */
266 struct string_literal_expression_t {
267         expression_base_t  base;
268         string_encoding_t  encoding;
269         string_t           value;
270 };
271
272 struct funcname_expression_t {
273         expression_base_t  base;
274         funcname_kind_t    kind;
275 };
276
277 struct compound_literal_expression_t {
278         expression_base_t  base;
279         type_t            *type;
280         initializer_t     *initializer;
281 };
282
283 struct builtin_constant_expression_t {
284         expression_base_t  base;
285         expression_t      *value;
286 };
287
288 struct builtin_types_compatible_expression_t {
289         expression_base_t  base;
290         type_t            *left;
291         type_t            *right;
292 };
293
294 struct reference_expression_t {
295         expression_base_t  base;
296         entity_t          *entity;
297 };
298
299 /**
300  * An argument of a call.
301  */
302 struct call_argument_t {
303         expression_t    *expression;  /**< The expression which value is transmitted. */
304         call_argument_t *next;        /**< Links to the next argument of this call. */
305 };
306
307
308 struct call_expression_t {
309         expression_base_t  base;
310         expression_t      *function;  /**< The address of the function to call. */
311         call_argument_t   *arguments; /**< List of arguments of this call. */
312 };
313
314
315 struct unary_expression_t {
316         expression_base_t  base;
317         expression_t      *value;     /**< The unary operand. */
318 };
319
320 struct binary_expression_t {
321         expression_base_t  base;
322         expression_t      *left;
323         expression_t      *right;
324 };
325
326 struct select_expression_t {
327         expression_base_t  base;
328         expression_t      *compound;
329         entity_t          *compound_entry;
330 };
331
332 struct array_access_expression_t {
333         expression_base_t  base;
334         expression_t      *array_ref; /**< the referenced array */
335         expression_t      *index;     /**< the index used */
336         bool               flipped;   /**< True if index/ref was written in a 5[a] way */
337 };
338
339 struct typeprop_expression_t {
340         expression_base_t  base;
341         type_t            *type;
342         expression_t      *tp_expression;
343 };
344
345 struct designator_t {
346         source_position_t  source_position;
347         symbol_t          *symbol;      /**< the symbol if any */
348         expression_t      *array_index; /**< the array index if any */
349         designator_t      *next;
350 };
351
352 struct offsetof_expression_t {
353         expression_base_t  base;
354         type_t            *type;
355         designator_t      *designator;
356 };
357
358 struct va_start_expression_t {
359         expression_base_t  base;
360         expression_t      *ap;
361         expression_t      *parameter;
362 };
363
364 struct va_arg_expression_t {
365         expression_base_t  base;
366         expression_t      *ap;
367 };
368
369 struct va_copy_expression_t {
370         expression_base_t  base;
371         expression_t      *dst;    /**< destination argument */
372         expression_t      *src;    /**< source argument */
373 };
374
375 struct conditional_expression_t {
376         expression_base_t  base;
377         expression_t      *condition;
378         expression_t      *true_expression;
379         expression_t      *false_expression;
380 };
381
382 struct statement_expression_t {
383         expression_base_t  base;
384         statement_t       *statement;
385 };
386
387 struct classify_type_expression_t {
388         expression_base_t  base;
389         expression_t      *type_expression;
390 };
391
392 struct label_address_expression_t {
393         expression_base_t  base;
394         label_t           *label;
395 };
396
397 union expression_t {
398         expression_kind_t                     kind;
399         expression_base_t                     base;
400         literal_expression_t                  literal;
401         string_literal_expression_t           string_literal;
402         funcname_expression_t                 funcname;
403         compound_literal_expression_t         compound_literal;
404         builtin_constant_expression_t         builtin_constant;
405         builtin_types_compatible_expression_t builtin_types_compatible;
406         reference_expression_t                reference;
407         call_expression_t                     call;
408         unary_expression_t                    unary;
409         binary_expression_t                   binary;
410         select_expression_t                   select;
411         array_access_expression_t             array_access;
412         typeprop_expression_t                 typeprop;
413         offsetof_expression_t                 offsetofe;
414         va_start_expression_t                 va_starte;
415         va_arg_expression_t                   va_arge;
416         va_copy_expression_t                  va_copye;
417         conditional_expression_t              conditional;
418         statement_expression_t                statement;
419         classify_type_expression_t            classify_type;
420         label_address_expression_t            label_address;
421 };
422
423 typedef enum initializer_kind_t {
424         INITIALIZER_VALUE,
425         INITIALIZER_LIST,
426         INITIALIZER_STRING,
427         INITIALIZER_WIDE_STRING,
428         INITIALIZER_DESIGNATOR
429 } initializer_kind_t;
430
431 struct initializer_base_t {
432         initializer_kind_t kind;
433 };
434
435 struct initializer_value_t {
436         initializer_base_t  base;
437         expression_t       *value;
438 };
439
440 struct initializer_list_t {
441         initializer_base_t  base;
442         size_t              len;
443         initializer_t      *initializers[];
444 };
445
446 struct initializer_string_t {
447         initializer_base_t base;
448         string_t           string;
449 };
450
451 struct initializer_wide_string_t {
452         initializer_base_t  base;
453         string_t            string;
454 };
455
456 struct initializer_designator_t {
457         initializer_base_t  base;
458         designator_t       *designator;
459 };
460
461 union initializer_t {
462         initializer_kind_t        kind;
463         initializer_base_t        base;
464         initializer_value_t       value;
465         initializer_list_t        list;
466         initializer_string_t      string;
467         initializer_wide_string_t wide_string;
468         initializer_designator_t  designator;
469 };
470
471 /**
472  * The statement kinds.
473  */
474 typedef enum statement_kind_t {
475         STATEMENT_ERROR = 1,
476         STATEMENT_EMPTY,
477         STATEMENT_COMPOUND,
478         STATEMENT_RETURN,
479         STATEMENT_DECLARATION,
480         STATEMENT_IF,
481         STATEMENT_SWITCH,
482         STATEMENT_EXPRESSION,
483         STATEMENT_CONTINUE,
484         STATEMENT_BREAK,
485         STATEMENT_COMPUTED_GOTO,
486         STATEMENT_GOTO,
487         STATEMENT_LABEL,
488         STATEMENT_CASE_LABEL,
489         STATEMENT_WHILE,
490         STATEMENT_DO_WHILE,
491         STATEMENT_FOR,
492         STATEMENT_ASM,
493         STATEMENT_MS_TRY,          /**< MS __try/__finally or __try/__except */
494         STATEMENT_LEAVE            /**< MS __leave */
495 } statement_kind_t;
496
497 /**
498  * The base class of every statement.
499  */
500 struct statement_base_t {
501         statement_kind_t   kind;
502         statement_t       *next;         /**< Point to the next statement in a compound statement. */
503         source_position_t  source_position;
504         statement_t       *parent;       /**< The Parent statement that controls the execution. */
505         bool               reachable;    /**< True, if this statement is reachable. */
506 #ifndef NDEBUG
507         bool               transformed;
508 #endif
509 };
510
511 struct return_statement_t {
512         statement_base_t  base;
513         expression_t     *value;    /**< The return value if any. */
514 };
515
516 struct compound_statement_t {
517         statement_base_t  base;
518         statement_t      *statements;
519         scope_t           scope;
520         bool              stmt_expr; /**< True if this compound statement is a statement expression. */
521 };
522
523 struct declaration_statement_t {
524         statement_base_t  base;
525         entity_t         *declarations_begin;
526         entity_t         *declarations_end;
527 };
528
529 struct if_statement_t {
530         statement_base_t  base;
531         scope_t           scope;
532         expression_t     *condition;
533         statement_t      *true_statement;
534         statement_t      *false_statement;
535 };
536
537 struct switch_statement_t {
538         statement_base_t        base;
539         scope_t                 scope;
540         expression_t           *expression;
541         statement_t            *body;
542         case_label_statement_t *first_case, *last_case; /**< List of all cases, including default. */
543         case_label_statement_t *default_label;          /**< The default label if existent. */
544 };
545
546 struct goto_statement_t {
547         statement_base_t  base;
548         label_t          *label;         /**< The destination label. */
549         goto_statement_t *next;          /**< links all goto statements of a function */
550 };
551
552 struct computed_goto_statement_t {
553         statement_base_t  base;
554         expression_t     *expression; /**< The expression for the computed goto. */
555 };
556
557 struct case_label_statement_t {
558         statement_base_t        base;
559         expression_t           *expression;    /**< The case label expression, NULL for default label. */
560         expression_t           *end_range;     /**< For GNUC case a .. b: the end range expression, NULL else. */
561         case_label_statement_t *next;          /**< link to the next case label in switch */
562         statement_t            *statement;
563         long                   first_case;     /**< The folded value of expression. */
564         long                   last_case;      /**< The folded value of end_range. */
565         bool                   is_bad;         /**< If set marked as bad to suppress warnings. */
566         bool                   is_empty_range; /**< If set marked this as an empty range. */
567         long                   pn;
568 };
569
570 struct label_statement_t {
571         statement_base_t   base;
572         label_t           *label;
573         statement_t       *statement;
574         label_statement_t *next;    /**< links all label statements of a function */
575 };
576
577 struct expression_statement_t {
578         statement_base_t  base;
579         expression_t     *expression;
580 };
581
582 struct while_statement_t {
583         statement_base_t  base;
584         scope_t           scope;
585         expression_t     *condition;
586         statement_t      *body;
587 };
588
589 struct do_while_statement_t {
590         statement_base_t  base;
591         scope_t           scope;
592         expression_t     *condition;
593         statement_t      *body;
594 };
595
596 struct for_statement_t {
597         statement_base_t  base;
598         scope_t           scope;
599         expression_t     *initialisation;
600         expression_t     *condition;
601         expression_t     *step;
602         statement_t      *body;
603         bool              condition_reachable:1;
604         bool              step_reachable:1;
605 };
606
607 struct asm_argument_t {
608         string_t        constraints;
609         expression_t   *expression;
610         symbol_t       *symbol;
611         asm_argument_t *next;
612 };
613
614 struct asm_clobber_t {
615         string_t       clobber;
616         asm_clobber_t *next;
617 };
618
619 struct asm_statement_t {
620         statement_base_t base;
621         string_t         asm_text;
622         asm_argument_t  *inputs;
623         asm_argument_t  *outputs;
624         asm_clobber_t   *clobbers;
625         bool             is_volatile;
626 };
627
628 struct ms_try_statement_t {
629         statement_base_t  base;
630         statement_t      *try_statement;
631         expression_t     *except_expression; /**< non-null for except, NULL for finally */
632         statement_t      *final_statement;
633 };
634
635 struct leave_statement_t {
636         statement_base_t  base;
637 };
638
639 union statement_t {
640         statement_kind_t          kind;
641         statement_base_t          base;
642         return_statement_t        returns;
643         compound_statement_t      compound;
644         declaration_statement_t   declaration;
645         if_statement_t            ifs;
646         switch_statement_t        switchs;
647         computed_goto_statement_t computed_goto;
648         goto_statement_t          gotos;
649         case_label_statement_t    case_label;
650         label_statement_t         label;
651         expression_statement_t    expression;
652         while_statement_t         whiles;
653         do_while_statement_t      do_while;
654         for_statement_t           fors;
655         asm_statement_t           asms;
656         ms_try_statement_t        ms_try;
657         leave_statement_t         leave;
658 };
659
660 struct translation_unit_t {
661         scope_t      scope;
662         statement_t *global_asm;
663 };
664
665 /**
666  * Allocate an AST node with given size and
667  * initialize all fields with zero.
668  */
669 static inline void *allocate_ast_zero(size_t size)
670 {
671         return memset(obstack_alloc(&ast_obstack, size), 0, size);
672 }
673
674 /** If set, implicit casts are printed. */
675 extern bool print_implicit_casts;
676 /** If set parenthesis are printed to indicate operator precedence. */
677 extern bool print_parenthesis;
678
679 #endif