parser: Parse and reject GCC range initializers "[0 ... 9]".
[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_COMPLEMENT,
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_REAL,
110         EXPR_UNARY_IMAG,
111         EXPR_UNARY_LAST = EXPR_UNARY_IMAG,
112
113         EXPR_BINARY_FIRST,
114         EXPR_BINARY_ADD = EXPR_BINARY_FIRST,
115         EXPR_BINARY_SUB,
116         EXPR_BINARY_MUL,
117         EXPR_BINARY_DIV,
118         EXPR_BINARY_MOD,
119         EXPR_BINARY_EQUAL,
120         EXPR_BINARY_NOTEQUAL,
121         EXPR_BINARY_LESS,
122         EXPR_BINARY_LESSEQUAL,
123         EXPR_BINARY_GREATER,
124         EXPR_BINARY_GREATEREQUAL,
125         EXPR_BINARY_BITWISE_AND,
126         EXPR_BINARY_BITWISE_OR,
127         EXPR_BINARY_BITWISE_XOR,
128         EXPR_BINARY_LOGICAL_AND,
129         EXPR_BINARY_LOGICAL_OR,
130         EXPR_BINARY_SHIFTLEFT,
131         EXPR_BINARY_SHIFTRIGHT,
132         EXPR_BINARY_ASSIGN,
133         EXPR_BINARY_MUL_ASSIGN,
134         EXPR_BINARY_DIV_ASSIGN,
135         EXPR_BINARY_MOD_ASSIGN,
136         EXPR_BINARY_ADD_ASSIGN,
137         EXPR_BINARY_SUB_ASSIGN,
138         EXPR_BINARY_SHIFTLEFT_ASSIGN,
139         EXPR_BINARY_SHIFTRIGHT_ASSIGN,
140         EXPR_BINARY_BITWISE_AND_ASSIGN,
141         EXPR_BINARY_BITWISE_XOR_ASSIGN,
142         EXPR_BINARY_BITWISE_OR_ASSIGN,
143         EXPR_BINARY_COMMA,
144
145         EXPR_BINARY_ISGREATER,
146         EXPR_BINARY_ISGREATEREQUAL,
147         EXPR_BINARY_ISLESS,
148         EXPR_BINARY_ISLESSEQUAL,
149         EXPR_BINARY_ISLESSGREATER,
150         EXPR_BINARY_ISUNORDERED,
151         EXPR_BINARY_LAST = EXPR_BINARY_ISUNORDERED,
152 } expression_kind_t;
153
154 typedef enum funcname_kind_t {
155         FUNCNAME_FUNCTION,           /**< C99 __func__, older __FUNCTION__ */
156         FUNCNAME_PRETTY_FUNCTION,    /**< GNUC __PRETTY_FUNCTION__ */
157         FUNCNAME_FUNCSIG,            /**< MS __FUNCSIG__ */
158         FUNCNAME_FUNCDNAME           /**< MS __FUNCDNAME__ */
159 } funcname_kind_t;
160
161 /* convenience macros */
162 #define EXPR_BINARY_CASES              \
163              EXPR_BINARY_ADD:                \
164         case EXPR_BINARY_SUB:                \
165         case EXPR_BINARY_MUL:                \
166         case EXPR_BINARY_DIV:                \
167         case EXPR_BINARY_MOD:                \
168         case EXPR_BINARY_EQUAL:              \
169         case EXPR_BINARY_NOTEQUAL:           \
170         case EXPR_BINARY_LESS:               \
171         case EXPR_BINARY_LESSEQUAL:          \
172         case EXPR_BINARY_GREATER:            \
173         case EXPR_BINARY_GREATEREQUAL:       \
174         case EXPR_BINARY_BITWISE_AND:        \
175         case EXPR_BINARY_BITWISE_OR:         \
176         case EXPR_BINARY_BITWISE_XOR:        \
177         case EXPR_BINARY_LOGICAL_AND:        \
178         case EXPR_BINARY_LOGICAL_OR:         \
179         case EXPR_BINARY_SHIFTLEFT:          \
180         case EXPR_BINARY_SHIFTRIGHT:         \
181         case EXPR_BINARY_ASSIGN:             \
182         case EXPR_BINARY_MUL_ASSIGN:         \
183         case EXPR_BINARY_DIV_ASSIGN:         \
184         case EXPR_BINARY_MOD_ASSIGN:         \
185         case EXPR_BINARY_ADD_ASSIGN:         \
186         case EXPR_BINARY_SUB_ASSIGN:         \
187         case EXPR_BINARY_SHIFTLEFT_ASSIGN:   \
188         case EXPR_BINARY_SHIFTRIGHT_ASSIGN:  \
189         case EXPR_BINARY_BITWISE_AND_ASSIGN: \
190         case EXPR_BINARY_BITWISE_XOR_ASSIGN: \
191         case EXPR_BINARY_BITWISE_OR_ASSIGN:  \
192         case EXPR_BINARY_COMMA:              \
193         case EXPR_BINARY_ISGREATER:          \
194         case EXPR_BINARY_ISGREATEREQUAL:     \
195         case EXPR_BINARY_ISLESS:             \
196         case EXPR_BINARY_ISLESSEQUAL:        \
197         case EXPR_BINARY_ISLESSGREATER:      \
198         case EXPR_BINARY_ISUNORDERED
199
200 /**
201  * unary expression with mandatory operand
202  */
203 #define EXPR_UNARY_CASES_MANDATORY   \
204              EXPR_UNARY_NEGATE:            \
205         case EXPR_UNARY_PLUS:              \
206         case EXPR_UNARY_COMPLEMENT:        \
207         case EXPR_UNARY_NOT:               \
208         case EXPR_UNARY_DEREFERENCE:       \
209         case EXPR_UNARY_TAKE_ADDRESS:      \
210         case EXPR_UNARY_POSTFIX_INCREMENT: \
211         case EXPR_UNARY_POSTFIX_DECREMENT: \
212         case EXPR_UNARY_PREFIX_INCREMENT:  \
213         case EXPR_UNARY_PREFIX_DECREMENT:  \
214         case EXPR_UNARY_CAST:              \
215         case EXPR_UNARY_ASSUME:            \
216         case EXPR_UNARY_DELETE:            \
217         case EXPR_UNARY_DELETE_ARRAY:      \
218         case EXPR_UNARY_IMAG:              \
219         case EXPR_UNARY_REAL
220
221 /**
222  * unary expression with optional operand
223  */
224 #define EXPR_UNARY_CASES_OPTIONAL \
225         EXPR_UNARY_THROW
226
227 #define EXPR_UNARY_CASES           \
228              EXPR_UNARY_CASES_MANDATORY: \
229         case EXPR_UNARY_CASES_OPTIONAL
230
231 #define EXPR_LITERAL_CASES                     \
232              EXPR_LITERAL_BOOLEAN:                   \
233         case EXPR_LITERAL_INTEGER:                   \
234         case EXPR_LITERAL_FLOATINGPOINT:             \
235         case EXPR_LITERAL_MS_NOOP
236
237 /**
238  * The base class of every expression.
239  */
240 struct expression_base_t {
241         expression_kind_t kind;     /**< The expression kind. */
242         type_t           *type;     /**< The type of the expression. */
243         position_t        pos;      /**< The source position of this expression. */
244         bool              parenthesized : 1;
245 #ifndef NDEBUG
246         bool              transformed : 1; /**< Set if this expression was transformed. */
247 #endif
248         bool              implicit : 1;  /**< compiler generated expression.
249                                             Examples: select into anonymous structs,
250                                             implicit casts */
251 };
252
253 /**
254  * integer, float and boolean constants
255  */
256 struct literal_expression_t {
257         expression_base_t base;
258         string_t          value;
259         char const       *suffix; /**< Start of the suffix in value. */
260
261         /* ast2firm data */
262         ir_tarval        *target_value;
263 };
264
265 /**
266  * string and character literals
267  */
268 struct string_literal_expression_t {
269         expression_base_t base;
270         string_t          value;
271 };
272
273 struct funcname_expression_t {
274         expression_base_t  base;
275         funcname_kind_t    kind;
276 };
277
278 struct compound_literal_expression_t {
279         expression_base_t  base;
280         type_t            *type;
281         initializer_t     *initializer;
282         bool               global_scope;
283 };
284
285 struct builtin_constant_expression_t {
286         expression_base_t  base;
287         expression_t      *value;
288 };
289
290 struct builtin_types_compatible_expression_t {
291         expression_base_t  base;
292         type_t            *left;
293         type_t            *right;
294 };
295
296 struct reference_expression_t {
297         expression_base_t  base;
298         entity_t          *entity;
299 };
300
301 /**
302  * An argument of a call.
303  */
304 struct call_argument_t {
305         expression_t    *expression;  /**< The expression which value is transmitted. */
306         call_argument_t *next;        /**< Links to the next argument of this call. */
307 };
308
309
310 struct call_expression_t {
311         expression_base_t  base;
312         expression_t      *function;  /**< The address of the function to call. */
313         call_argument_t   *arguments; /**< List of arguments of this call. */
314 };
315
316
317 struct unary_expression_t {
318         expression_base_t  base;
319         expression_t      *value;     /**< The unary operand. */
320 };
321
322 struct binary_expression_t {
323         expression_base_t  base;
324         expression_t      *left;
325         expression_t      *right;
326 };
327
328 struct select_expression_t {
329         expression_base_t  base;
330         expression_t      *compound;
331         entity_t          *compound_entry;
332 };
333
334 struct array_access_expression_t {
335         expression_base_t  base;
336         expression_t      *array_ref; /**< the referenced array */
337         expression_t      *index;     /**< the index used */
338         bool               flipped;   /**< True if index/ref was written in a 5[a] way */
339 };
340
341 struct typeprop_expression_t {
342         expression_base_t  base;
343         type_t            *type;
344         expression_t      *tp_expression;
345 };
346
347 struct designator_t {
348         position_t    pos;
349         symbol_t     *symbol;      /**< the symbol if any */
350         expression_t *array_index; /**< the array index if any */
351         expression_t *range_last;  /**< last index of a range initializer, if any */
352         designator_t *next;
353 };
354
355 struct offsetof_expression_t {
356         expression_base_t  base;
357         type_t            *type;
358         designator_t      *designator;
359 };
360
361 struct va_start_expression_t {
362         expression_base_t  base;
363         expression_t      *ap;
364         expression_t      *parameter;
365 };
366
367 struct va_arg_expression_t {
368         expression_base_t  base;
369         expression_t      *ap;
370 };
371
372 struct va_copy_expression_t {
373         expression_base_t  base;
374         expression_t      *dst;    /**< destination argument */
375         expression_t      *src;    /**< source argument */
376 };
377
378 struct conditional_expression_t {
379         expression_base_t  base;
380         expression_t      *condition;
381         expression_t      *true_expression;
382         expression_t      *false_expression;
383 };
384
385 struct statement_expression_t {
386         expression_base_t  base;
387         statement_t       *statement;
388 };
389
390 struct classify_type_expression_t {
391         expression_base_t  base;
392         expression_t      *type_expression;
393 };
394
395 struct label_address_expression_t {
396         expression_base_t  base;
397         label_t           *label;
398 };
399
400 union expression_t {
401         expression_kind_t                     kind;
402         expression_base_t                     base;
403         literal_expression_t                  literal;
404         string_literal_expression_t           string_literal;
405         funcname_expression_t                 funcname;
406         compound_literal_expression_t         compound_literal;
407         builtin_constant_expression_t         builtin_constant;
408         builtin_types_compatible_expression_t builtin_types_compatible;
409         reference_expression_t                reference;
410         call_expression_t                     call;
411         unary_expression_t                    unary;
412         binary_expression_t                   binary;
413         select_expression_t                   select;
414         array_access_expression_t             array_access;
415         typeprop_expression_t                 typeprop;
416         offsetof_expression_t                 offsetofe;
417         va_start_expression_t                 va_starte;
418         va_arg_expression_t                   va_arge;
419         va_copy_expression_t                  va_copye;
420         conditional_expression_t              conditional;
421         statement_expression_t                statement;
422         classify_type_expression_t            classify_type;
423         label_address_expression_t            label_address;
424 };
425
426 typedef enum initializer_kind_t {
427         INITIALIZER_VALUE,
428         INITIALIZER_LIST,
429         INITIALIZER_STRING,
430         INITIALIZER_DESIGNATOR
431 } initializer_kind_t;
432
433 struct initializer_base_t {
434         initializer_kind_t kind;
435 };
436
437 struct initializer_value_t {
438         initializer_base_t  base;
439         expression_t       *value;
440 };
441
442 struct initializer_list_t {
443         initializer_base_t  base;
444         size_t              len;
445         initializer_t      *initializers[];
446 };
447
448 struct initializer_designator_t {
449         initializer_base_t  base;
450         designator_t       *designator;
451 };
452
453 union initializer_t {
454         initializer_kind_t        kind;
455         initializer_base_t        base;
456         initializer_value_t       value;
457         initializer_list_t        list;
458         initializer_designator_t  designator;
459 };
460
461 static inline string_literal_expression_t const *get_init_string(initializer_t const *const init)
462 {
463         assert(init->kind == INITIALIZER_STRING);
464         assert(init->value.value->kind == EXPR_STRING_LITERAL);
465         return &init->value.value->string_literal;
466 }
467
468 /**
469  * The statement kinds.
470  */
471 typedef enum statement_kind_t {
472         STATEMENT_ERROR = 1,
473         STATEMENT_EMPTY,
474         STATEMENT_COMPOUND,
475         STATEMENT_RETURN,
476         STATEMENT_DECLARATION,
477         STATEMENT_IF,
478         STATEMENT_SWITCH,
479         STATEMENT_EXPRESSION,
480         STATEMENT_CONTINUE,
481         STATEMENT_BREAK,
482         STATEMENT_COMPUTED_GOTO,
483         STATEMENT_GOTO,
484         STATEMENT_LABEL,
485         STATEMENT_CASE_LABEL,
486         STATEMENT_DO_WHILE,
487         STATEMENT_FOR,
488         STATEMENT_ASM,
489         STATEMENT_MS_TRY,          /**< MS __try/__finally or __try/__except */
490         STATEMENT_LEAVE            /**< MS __leave */
491 } statement_kind_t;
492
493 /**
494  * The base class of every statement.
495  */
496 struct statement_base_t {
497         statement_kind_t kind;
498         statement_t     *next;      /**< Point to the next statement in a compound statement. */
499         position_t       pos;
500         statement_t     *parent;    /**< The Parent statement that controls the execution. */
501         bool             reachable; /**< True, if this statement is reachable. */
502 #ifndef NDEBUG
503         bool             transformed;
504 #endif
505 };
506
507 struct return_statement_t {
508         statement_base_t  base;
509         expression_t     *value;    /**< The return value if any. */
510 };
511
512 struct compound_statement_t {
513         statement_base_t  base;
514         statement_t      *statements;
515         scope_t           scope;
516         bool              stmt_expr; /**< True if this compound statement is a statement expression. */
517 };
518
519 struct declaration_statement_t {
520         statement_base_t  base;
521         entity_t         *declarations_begin;
522         entity_t         *declarations_end;
523 };
524
525 struct if_statement_t {
526         statement_base_t  base;
527         scope_t           scope;
528         expression_t     *condition;
529         statement_t      *true_statement;
530         statement_t      *false_statement;
531 };
532
533 struct switch_statement_t {
534         statement_base_t        base;
535         scope_t                 scope;
536         expression_t           *expression;
537         statement_t            *body;
538         case_label_statement_t *first_case, *last_case; /**< List of all cases, including default. */
539         case_label_statement_t *default_label;          /**< The default label if existent. */
540 };
541
542 struct goto_statement_t {
543         statement_base_t  base;
544         label_t          *label;         /**< The destination label. */
545         goto_statement_t *next;          /**< links all goto statements of a function */
546 };
547
548 struct computed_goto_statement_t {
549         statement_base_t  base;
550         expression_t     *expression; /**< The expression for the computed goto. */
551 };
552
553 struct case_label_statement_t {
554         statement_base_t        base;
555         expression_t           *expression;    /**< The case label expression, NULL for default label. */
556         expression_t           *end_range;     /**< For GNUC case a .. b: the end range expression, NULL else. */
557         case_label_statement_t *next;          /**< link to the next case label in switch */
558         statement_t            *statement;
559         ir_tarval              *first_case;
560         ir_tarval              *last_case;
561         bool                   is_bad;         /**< If set marked as bad to suppress warnings. */
562         bool                   is_empty_range; /**< If set marked this as an empty range. */
563         long                   pn;
564 };
565
566 struct label_statement_t {
567         statement_base_t   base;
568         label_t           *label;
569         statement_t       *statement;
570         label_statement_t *next;    /**< links all label statements of a function */
571 };
572
573 struct expression_statement_t {
574         statement_base_t  base;
575         expression_t     *expression;
576 };
577
578 struct do_while_statement_t {
579         statement_base_t  base;
580         scope_t           scope;
581         expression_t     *condition;
582         statement_t      *body;
583 };
584
585 struct for_statement_t {
586         statement_base_t  base;
587         scope_t           scope;
588         expression_t     *initialisation;
589         expression_t     *condition;
590         expression_t     *step;
591         statement_t      *body;
592         bool              condition_reachable:1;
593         bool              step_reachable:1;
594 };
595
596 struct asm_argument_t {
597         string_t        constraints;
598         expression_t   *expression;
599         symbol_t       *symbol;
600         asm_argument_t *next;
601 };
602
603 struct asm_clobber_t {
604         string_t       clobber;
605         asm_clobber_t *next;
606 };
607
608 struct asm_label_t {
609         label_t     *label;
610         asm_label_t *next;
611 };
612
613 struct asm_statement_t {
614         statement_base_t base;
615         string_t         asm_text;
616         asm_argument_t  *inputs;
617         asm_argument_t  *outputs;
618         asm_clobber_t   *clobbers;
619         asm_label_t     *labels;
620         bool             is_volatile;
621 };
622
623 struct ms_try_statement_t {
624         statement_base_t  base;
625         statement_t      *try_statement;
626         expression_t     *except_expression; /**< non-null for except, NULL for finally */
627         statement_t      *final_statement;
628 };
629
630 struct leave_statement_t {
631         statement_base_t  base;
632 };
633
634 union statement_t {
635         statement_kind_t          kind;
636         statement_base_t          base;
637         return_statement_t        returns;
638         compound_statement_t      compound;
639         declaration_statement_t   declaration;
640         if_statement_t            ifs;
641         switch_statement_t        switchs;
642         computed_goto_statement_t computed_goto;
643         goto_statement_t          gotos;
644         case_label_statement_t    case_label;
645         label_statement_t         label;
646         expression_statement_t    expression;
647         do_while_statement_t      do_while;
648         for_statement_t           fors;
649         asm_statement_t           asms;
650         ms_try_statement_t        ms_try;
651         leave_statement_t         leave;
652 };
653
654 struct translation_unit_t {
655         scope_t      scope;
656         statement_t *global_asm;
657 };
658
659 /**
660  * Allocate an AST node with given size and
661  * initialize all fields with zero.
662  */
663 static inline void *allocate_ast_zero(size_t size)
664 {
665         return memset(obstack_alloc(&ast_obstack, size), 0, size);
666 }
667
668 /** If set, implicit casts are printed. */
669 extern bool print_implicit_casts;
670 /** If set parenthesis are printed to indicate operator precedence. */
671 extern bool print_parenthesis;
672
673 #endif