Do the "usual arithmetic conversions" for the second and third operand of the conditi...
[cparser] / parser.c
1 #include <config.h>
2
3 #include <assert.h>
4 #include <stdarg.h>
5 #include <stdbool.h>
6
7 #include "parser.h"
8 #include "lexer.h"
9 #include "token_t.h"
10 #include "type_t.h"
11 #include "type_hash.h"
12 #include "ast_t.h"
13 #include "adt/bitfiddle.h"
14 #include "adt/error.h"
15 #include "adt/array.h"
16
17 //#define PRINT_TOKENS
18 //#define ABORT_ON_ERROR
19 #define MAX_LOOKAHEAD 2
20 //#define STRICT_C99
21
22 typedef struct {
23         declaration_t *old_declaration;
24         symbol_t      *symbol;
25         unsigned short namespc;
26 } stack_entry_t;
27
28 static token_t         token;
29 static token_t         lookahead_buffer[MAX_LOOKAHEAD];
30 static int             lookahead_bufpos;
31 static stack_entry_t  *environment_stack = NULL;
32 static stack_entry_t  *label_stack       = NULL;
33 static context_t      *global_context    = NULL;
34 static context_t      *context           = NULL;
35 static declaration_t  *last_declaration  = NULL;
36 static declaration_t  *current_function  = NULL;
37 static struct obstack  temp_obst;
38 static bool            found_error;
39
40 static type_t         *type_int         = NULL;
41 static type_t         *type_uint        = NULL;
42 static type_t         *type_long_double = NULL;
43 static type_t         *type_double      = NULL;
44 static type_t         *type_float       = NULL;
45 static type_t         *type_const_char  = NULL;
46 static type_t         *type_string      = NULL;
47 static type_t         *type_void        = NULL;
48 static type_t         *type_size_t      = NULL;
49 static type_t         *type_ptrdiff_t   = NULL;
50
51 static statement_t *parse_compound_statement(void);
52 static statement_t *parse_statement(void);
53
54 static expression_t *parse_sub_expression(unsigned precedence);
55 static expression_t *parse_expression(void);
56 static type_t       *parse_typename(void);
57
58 #define STORAGE_CLASSES     \
59         case T_typedef:         \
60         case T_extern:          \
61         case T_static:          \
62         case T_auto:            \
63         case T_register:
64
65 #define TYPE_QUALIFIERS     \
66         case T_const:           \
67         case T_restrict:        \
68         case T_volatile:        \
69         case T_inline:
70
71 #ifdef PROVIDE_COMPLEX
72 #define COMPLEX_SPECIFIERS  \
73         case T__Complex:
74 #else
75 #define COMPLEX_SPECIFIERS
76 #endif
77
78 #ifdef PROVIDE_IMAGINARY
79 #define IMAGINARY_SPECIFIERS \
80         case T__Imaginary:
81 #else
82 #define IMAGINARY_SPECIFIERS
83 #endif
84
85 #define TYPE_SPECIFIERS     \
86         case T_void:            \
87         case T_char:            \
88         case T_short:           \
89         case T_int:             \
90         case T_long:            \
91         case T_float:           \
92         case T_double:          \
93         case T_signed:          \
94         case T_unsigned:        \
95         case T__Bool:           \
96         case T_struct:          \
97         case T_union:           \
98         case T_enum:            \
99         case T___typeof__:      \
100         COMPLEX_SPECIFIERS      \
101         IMAGINARY_SPECIFIERS
102
103 #define DECLARATION_START   \
104         STORAGE_CLASSES         \
105         TYPE_QUALIFIERS         \
106         TYPE_SPECIFIERS
107
108 #define TYPENAME_START      \
109         TYPE_QUALIFIERS         \
110         TYPE_SPECIFIERS
111
112 static inline void *allocate_ast_zero(size_t size)
113 {
114         void *res = allocate_ast(size);
115         memset(res, 0, size);
116         return res;
117 }
118
119 static inline void *allocate_type_zero(size_t size)
120 {
121         void *res = obstack_alloc(type_obst, size);
122         memset(res, 0, size);
123         return res;
124 }
125
126 static inline void free_type(void *type)
127 {
128         obstack_free(type_obst, type);
129 }
130
131 /**
132  * returns the top element of the environment stack
133  */
134 static inline size_t environment_top(void)
135 {
136         return ARR_LEN(environment_stack);
137 }
138
139 static inline size_t label_top(void)
140 {
141         return ARR_LEN(label_stack);
142 }
143
144
145
146 static inline void next_token(void)
147 {
148         token                              = lookahead_buffer[lookahead_bufpos];
149         lookahead_buffer[lookahead_bufpos] = lexer_token;
150         lexer_next_token();
151
152         lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
153
154 #ifdef PRINT_TOKENS
155         print_token(stderr, &token);
156         fprintf(stderr, "\n");
157 #endif
158 }
159
160 static inline const token_t *look_ahead(int num)
161 {
162         assert(num > 0 && num <= MAX_LOOKAHEAD);
163         int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
164         return & lookahead_buffer[pos];
165 }
166
167 #define eat(token_type)  do { assert(token.type == token_type); next_token(); } while(0)
168
169 static void error(void)
170 {
171         found_error = true;
172 #ifdef ABORT_ON_ERROR
173         abort();
174 #endif
175 }
176
177 static void parser_print_prefix_pos(const source_position_t source_position)
178 {
179     fputs(source_position.input_name, stderr);
180     fputc(':', stderr);
181     fprintf(stderr, "%d", source_position.linenr);
182     fputs(": ", stderr);
183 }
184
185 static void parser_print_error_prefix_pos(
186                 const source_position_t source_position)
187 {
188         parser_print_prefix_pos(source_position);
189         fputs("error: ", stderr);
190         error();
191 }
192
193 static void parser_print_error_prefix(void)
194 {
195         parser_print_error_prefix_pos(token.source_position);
196 }
197
198 static void parse_error(const char *message)
199 {
200         parser_print_error_prefix();
201         fprintf(stderr, "parse error: %s\n", message);
202 }
203
204 static void parser_print_warning_prefix_pos(
205                 const source_position_t source_position)
206 {
207         parser_print_prefix_pos(source_position);
208         fputs("warning: ", stderr);
209 }
210
211 static void parse_warning_pos(const source_position_t source_position,
212                               const char *const message)
213 {
214         parser_print_prefix_pos(source_position);
215         fprintf(stderr, "warning: %s\n", message);
216 }
217
218 static void parse_warning(const char *message)
219 {
220         parse_warning_pos(token.source_position, message);
221 }
222
223 static void parse_error_expected(const char *message, ...)
224 {
225         va_list args;
226         int first = 1;
227
228         if(message != NULL) {
229                 parser_print_error_prefix();
230                 fprintf(stderr, "%s\n", message);
231         }
232         parser_print_error_prefix();
233         fputs("Parse error: got ", stderr);
234         print_token(stderr, &token);
235         fputs(", expected ", stderr);
236
237         va_start(args, message);
238         token_type_t token_type = va_arg(args, token_type_t);
239         while(token_type != 0) {
240                 if(first == 1) {
241                         first = 0;
242                 } else {
243                         fprintf(stderr, ", ");
244                 }
245                 print_token_type(stderr, token_type);
246                 token_type = va_arg(args, token_type_t);
247         }
248         va_end(args);
249         fprintf(stderr, "\n");
250 }
251
252 static void print_type_quoted(type_t *type)
253 {
254         fputc('\'', stderr);
255         print_type(type);
256         fputc('\'', stderr);
257 }
258
259 static void type_error(const char *msg, const source_position_t source_position,
260                        type_t *type)
261 {
262         parser_print_error_prefix_pos(source_position);
263         fprintf(stderr, "%s, but found type ", msg);
264         print_type_quoted(type);
265         fputc('\n', stderr);
266 }
267
268 static void type_error_incompatible(const char *msg,
269                 const source_position_t source_position, type_t *type1, type_t *type2)
270 {
271         parser_print_error_prefix_pos(source_position);
272         fprintf(stderr, "%s, incompatible types: ", msg);
273         print_type_quoted(type1);
274         fprintf(stderr, " - ");
275         print_type_quoted(type2);
276         fprintf(stderr, ")\n");
277 }
278
279 static void eat_block(void)
280 {
281         if(token.type == '{')
282                 next_token();
283
284         while(token.type != '}') {
285                 if(token.type == T_EOF)
286                         return;
287                 if(token.type == '{') {
288                         eat_block();
289                         continue;
290                 }
291                 next_token();
292         }
293         eat('}');
294 }
295
296 static void eat_statement(void)
297 {
298         while(token.type != ';') {
299                 if(token.type == T_EOF)
300                         return;
301                 if(token.type == '}')
302                         return;
303                 if(token.type == '{') {
304                         eat_block();
305                         continue;
306                 }
307                 next_token();
308         }
309         eat(';');
310 }
311
312 static void eat_brace(void)
313 {
314         if(token.type == '(')
315                 next_token();
316
317         while(token.type != ')') {
318                 if(token.type == T_EOF)
319                         return;
320                 if(token.type == ')' || token.type == ';' || token.type == '}') {
321                         return;
322                 }
323                 if(token.type == '(') {
324                         eat_brace();
325                         continue;
326                 }
327                 if(token.type == '{') {
328                         eat_block();
329                         continue;
330                 }
331                 next_token();
332         }
333         eat(')');
334 }
335
336 #define expect(expected)                           \
337     if(UNLIKELY(token.type != (expected))) {       \
338         parse_error_expected(NULL, (expected), 0); \
339         eat_statement();                           \
340         return NULL;                               \
341     }                                              \
342     next_token();
343
344 #define expect_block(expected)                     \
345     if(UNLIKELY(token.type != (expected))) {       \
346         parse_error_expected(NULL, (expected), 0); \
347         eat_block();                               \
348         return NULL;                               \
349     }                                              \
350     next_token();
351
352 #define expect_void(expected)                      \
353     if(UNLIKELY(token.type != (expected))) {       \
354         parse_error_expected(NULL, (expected), 0); \
355         eat_statement();                           \
356         return;                                    \
357     }                                              \
358     next_token();
359
360 static void set_context(context_t *new_context)
361 {
362         context = new_context;
363
364         last_declaration = new_context->declarations;
365         if(last_declaration != NULL) {
366                 while(last_declaration->next != NULL) {
367                         last_declaration = last_declaration->next;
368                 }
369         }
370 }
371
372 /**
373  * called when we find a 2nd declarator for an identifier we already have a
374  * declarator for
375  */
376 static bool is_compatible_declaration (declaration_t *declaration,
377                                       declaration_t *previous)
378 {
379         /* TODO: not correct yet */
380         return declaration->type == previous->type;
381 }
382
383 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
384 {
385         declaration_t *declaration = symbol->declaration;
386         for( ; declaration != NULL; declaration = declaration->symbol_next) {
387                 if(declaration->namespc == namespc)
388                         return declaration;
389         }
390
391         return NULL;
392 }
393
394 static const char *get_namespace_prefix(namespace_t namespc)
395 {
396         switch(namespc) {
397         case NAMESPACE_NORMAL:
398                 return "";
399         case NAMESPACE_UNION:
400                 return "union ";
401         case NAMESPACE_STRUCT:
402                 return "struct ";
403         case NAMESPACE_ENUM:
404                 return "enum ";
405         case NAMESPACE_LABEL:
406                 return "label ";
407         }
408         panic("invalid namespace found");
409 }
410
411 /**
412  * pushs an environment_entry on the environment stack and links the
413  * corresponding symbol to the new entry
414  */
415 static declaration_t *stack_push(stack_entry_t **stack_ptr,
416                                  declaration_t *declaration,
417                                  context_t *parent_context)
418 {
419         symbol_t    *symbol    = declaration->symbol;
420         namespace_t  namespc = (namespace_t)declaration->namespc;
421
422         /* a declaration should be only pushed once */
423         assert(declaration->parent_context == NULL);
424         declaration->parent_context = parent_context;
425
426         declaration_t *previous_declaration = get_declaration(symbol, namespc);
427         assert(declaration != previous_declaration);
428         if(previous_declaration != NULL
429                         && previous_declaration->parent_context == context) {
430                 if(!is_compatible_declaration(declaration, previous_declaration)) {
431                         parser_print_error_prefix_pos(declaration->source_position);
432                         fprintf(stderr, "definition of symbol %s%s with type ",
433                                         get_namespace_prefix(namespc), symbol->string);
434                         print_type_quoted(declaration->type);
435                         fputc('\n', stderr);
436                         parser_print_error_prefix_pos(
437                                         previous_declaration->source_position);
438                         fprintf(stderr, "is incompatible with previous declaration "
439                                         "of type ");
440                         print_type_quoted(previous_declaration->type);
441                         fputc('\n', stderr);
442                 } else {
443                         const storage_class_t old_storage = previous_declaration->storage_class;
444                         const storage_class_t new_storage = declaration->storage_class;
445                         if (current_function == NULL) {
446                                 if (old_storage != STORAGE_CLASS_STATIC &&
447                                     new_storage == STORAGE_CLASS_STATIC) {
448                                         parser_print_error_prefix_pos(declaration->source_position);
449                                         fprintf(stderr,
450                                                 "static declaration of '%s' follows non-static declaration\n",
451                                                 symbol->string);
452                                         parser_print_error_prefix_pos(previous_declaration->source_position);
453                                         fprintf(stderr, "previous declaration of '%s' was here\n",
454                                                 symbol->string);
455                                 } else {
456                                         if (old_storage == STORAGE_CLASS_EXTERN) {
457                                                 if (new_storage == STORAGE_CLASS_NONE) {
458                                                         previous_declaration->storage_class = STORAGE_CLASS_NONE;
459                                                 }
460                                         } else {
461                                                 parser_print_warning_prefix_pos(declaration->source_position);
462                                                 fprintf(stderr, "redundant declaration for '%s'\n",
463                                                                                 symbol->string);
464                                                 parser_print_warning_prefix_pos(previous_declaration->source_position);
465                                                 fprintf(stderr, "previous declaration of '%s' was here\n",
466                                                                                 symbol->string);
467                                         }
468                                 }
469                         } else {
470                                 if (old_storage == STORAGE_CLASS_EXTERN &&
471                                                 new_storage == STORAGE_CLASS_EXTERN) {
472                                         parser_print_warning_prefix_pos(declaration->source_position);
473                                         fprintf(stderr, "redundant extern declaration for '%s'\n",
474                                                 symbol->string);
475                                         parser_print_warning_prefix_pos(previous_declaration->source_position);
476                                         fprintf(stderr, "previous declaration of '%s' was here\n",
477                                                 symbol->string);
478                                 } else {
479                                         parser_print_error_prefix_pos(declaration->source_position);
480                                         if (old_storage == new_storage) {
481                                                 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
482                                         } else {
483                                                 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
484                                         }
485                                         parser_print_error_prefix_pos(previous_declaration->source_position);
486                                         fprintf(stderr, "previous declaration of '%s' was here\n",
487                                                 symbol->string);
488                                 }
489                         }
490                 }
491                 return previous_declaration;
492         }
493
494         /* remember old declaration */
495         stack_entry_t entry;
496         entry.symbol          = symbol;
497         entry.old_declaration = symbol->declaration;
498         entry.namespc       = namespc;
499         ARR_APP1(stack_entry_t, *stack_ptr, entry);
500
501         /* replace/add declaration into declaration list of the symbol */
502         if(symbol->declaration == NULL) {
503                 symbol->declaration = declaration;
504         } else {
505                 declaration_t *iter_last = NULL;
506                 declaration_t *iter      = symbol->declaration;
507                 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
508                         /* replace an entry? */
509                         if(iter->namespc == namespc) {
510                                 if(iter_last == NULL) {
511                                         symbol->declaration = declaration;
512                                 } else {
513                                         iter_last->symbol_next = declaration;
514                                 }
515                                 declaration->symbol_next = iter->symbol_next;
516                                 break;
517                         }
518                 }
519                 if(iter == NULL) {
520                         assert(iter_last->symbol_next == NULL);
521                         iter_last->symbol_next = declaration;
522                 }
523         }
524
525         return declaration;
526 }
527
528 static declaration_t *environment_push(declaration_t *declaration)
529 {
530         assert(declaration->source_position.input_name != NULL);
531         return stack_push(&environment_stack, declaration, context);
532 }
533
534 static declaration_t *label_push(declaration_t *declaration)
535 {
536         return stack_push(&label_stack, declaration, &current_function->context);
537 }
538
539 /**
540  * pops symbols from the environment stack until @p new_top is the top element
541  */
542 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
543 {
544         stack_entry_t *stack = *stack_ptr;
545         size_t         top   = ARR_LEN(stack);
546         size_t         i;
547
548         assert(new_top <= top);
549         if(new_top == top)
550                 return;
551
552         for(i = top; i > new_top; --i) {
553                 stack_entry_t *entry = & stack[i - 1];
554
555                 declaration_t *old_declaration = entry->old_declaration;
556                 symbol_t      *symbol          = entry->symbol;
557                 namespace_t    namespc         = (namespace_t)entry->namespc;
558
559                 /* replace/remove declaration */
560                 declaration_t *declaration = symbol->declaration;
561                 assert(declaration != NULL);
562                 if(declaration->namespc == namespc) {
563                         if(old_declaration == NULL) {
564                                 symbol->declaration = declaration->symbol_next;
565                         } else {
566                                 symbol->declaration = old_declaration;
567                         }
568                 } else {
569                         declaration_t *iter_last = declaration;
570                         declaration_t *iter      = declaration->symbol_next;
571                         for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
572                                 /* replace an entry? */
573                                 if(iter->namespc == namespc) {
574                                         assert(iter_last != NULL);
575                                         iter_last->symbol_next = old_declaration;
576                                         old_declaration->symbol_next = iter->symbol_next;
577                                         break;
578                                 }
579                         }
580                         assert(iter != NULL);
581                 }
582         }
583
584         ARR_SHRINKLEN(*stack_ptr, (int) new_top);
585 }
586
587 static void environment_pop_to(size_t new_top)
588 {
589         stack_pop_to(&environment_stack, new_top);
590 }
591
592 static void label_pop_to(size_t new_top)
593 {
594         stack_pop_to(&label_stack, new_top);
595 }
596
597
598 static int get_rank(const type_t *type)
599 {
600         /* The C-standard allows promoting to int or unsigned int (see Â§ 7.2.2
601          * and esp. footnote 108). However we can't fold constants (yet), so we
602          * can't decide wether unsigned int is possible, while int always works.
603          * (unsigned int would be preferable when possible... for stuff like
604          *  struct { enum { ... } bla : 4; } ) */
605         if(type->type == TYPE_ENUM)
606                 return ATOMIC_TYPE_INT;
607
608         assert(type->type == TYPE_ATOMIC);
609         atomic_type_t      *atomic_type = (atomic_type_t*) type;
610         atomic_type_type_t  atype       = atomic_type->atype;
611         return atype;
612 }
613
614 static type_t *promote_integer(type_t *type)
615 {
616         if(get_rank(type) < ATOMIC_TYPE_INT)
617                 type = type_int;
618
619         return type;
620 }
621
622 static expression_t *create_cast_expression(expression_t *expression,
623                                             type_t *dest_type)
624 {
625         unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
626
627         cast->expression.type     = EXPR_UNARY;
628         cast->type                = UNEXPR_CAST;
629         cast->value               = expression;
630         cast->expression.datatype = dest_type;
631
632         return (expression_t*) cast;
633 }
634
635 static bool is_null_expression(const expression_t *const expr)
636 {
637         if (expr->type != EXPR_CONST) return false;
638
639         type_t *const type = skip_typeref(expr->datatype);
640         if (!is_type_integer(type)) return false;
641
642         const const_t *const const_expr = (const const_t*)expr;
643         return const_expr->v.int_value == 0;
644 }
645
646 static expression_t *create_implicit_cast(expression_t *expression,
647                                           type_t *dest_type)
648 {
649         type_t *source_type = expression->datatype;
650
651         if(source_type == NULL)
652                 return expression;
653
654         source_type = skip_typeref(source_type);
655         dest_type   = skip_typeref(dest_type);
656
657         if(source_type == dest_type)
658                 return expression;
659
660         if(dest_type->type == TYPE_ATOMIC) {
661                 if(source_type->type != TYPE_ATOMIC)
662                         panic("casting of non-atomic types not implemented yet");
663
664                 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
665                         type_error_incompatible("can't cast types",
666                                                 expression->source_position,
667                                                 source_type, dest_type);
668                         return expression;
669                 }
670
671                 return create_cast_expression(expression, dest_type);
672         }
673         if(dest_type->type == TYPE_POINTER) {
674                 pointer_type_t *pointer_type
675                         = (pointer_type_t*) dest_type;
676                 switch (source_type->type) {
677                         case TYPE_ATOMIC:
678                                 if (is_null_expression(expression)) {
679                                         return create_cast_expression(expression, dest_type);
680                                 }
681                                 break;
682
683                         case TYPE_POINTER:
684                                 if (pointers_compatible(source_type, dest_type)) {
685                                         return create_cast_expression(expression, dest_type);
686                                 }
687                                 break;
688
689                         case TYPE_ARRAY: {
690                                 array_type_t *const array_type = (array_type_t*) source_type;
691                                 if (types_compatible(array_type->element_type,
692                                                                                                                  pointer_type->points_to)) {
693                                         return create_cast_expression(expression, dest_type);
694                                 }
695                                 break;
696                         }
697
698                         default:
699                                 panic("casting of non-atomic types not implemented yet");
700                 }
701
702                 type_error_incompatible("can't implicitly cast types",
703                                                                                                                 expression->source_position,
704                                                                                                                 source_type, dest_type);
705                 return expression;
706         }
707
708         panic("casting of non-atomic types not implemented yet");
709 }
710
711 static void semantic_assign(type_t *orig_type_left, expression_t **right,
712                             const char *context)
713 {
714         type_t *orig_type_right = (*right)->datatype;
715
716         if(orig_type_right == NULL)
717                 return;
718
719         type_t *const type_left  = skip_typeref(orig_type_left);
720         type_t *const type_right = skip_typeref(orig_type_right);
721
722         if (type_left == type_right) {
723                 return;
724         }
725
726         if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
727             (type_left->type == TYPE_POINTER && is_null_expression(*right)) ||
728             (type_left->type == TYPE_POINTER && type_right->type == TYPE_POINTER)) {
729                 *right = create_implicit_cast(*right, type_left);
730                 return;
731         }
732
733         if (type_left->type == TYPE_POINTER) {
734                 switch (type_right->type) {
735                         case TYPE_FUNCTION: {
736                                 pointer_type_t *const ptr_type = (pointer_type_t*)type_left;
737                                 if (ptr_type->points_to == type_right) {
738                                         return;
739                                 }
740                                 break;
741                         }
742
743                         case TYPE_ARRAY: {
744                                 pointer_type_t *const ptr_type = (pointer_type_t*)type_left;
745                                 array_type_t   *const arr_type = (array_type_t*)type_right;
746                                 if (ptr_type->points_to == arr_type->element_type) {
747                                         return;
748                                 }
749                                 break;
750                         }
751
752                         default: break;
753                 }
754         }
755
756         /* TODO: improve error message */
757         parser_print_error_prefix();
758         fprintf(stderr, "incompatible types in %s\n", context);
759         parser_print_error_prefix();
760         print_type_quoted(type_left);
761         fputs(" <- ", stderr);
762         print_type_quoted(type_right);
763         fputs("\n", stderr);
764 }
765
766 static expression_t *parse_constant_expression(void)
767 {
768         /* start parsing at precedence 7 (conditional expression) */
769         return parse_sub_expression(7);
770 }
771
772 static expression_t *parse_assignment_expression(void)
773 {
774         /* start parsing at precedence 2 (assignment expression) */
775         return parse_sub_expression(2);
776 }
777
778 typedef struct declaration_specifiers_t  declaration_specifiers_t;
779 struct declaration_specifiers_t {
780         storage_class_t  storage_class;
781         bool             is_inline;
782         type_t          *type;
783 };
784
785 static void parse_compound_type_entries(void);
786 static declaration_t *parse_declarator(
787                 const declaration_specifiers_t *specifiers, type_t *type,
788                 bool may_be_abstract);
789 static declaration_t *record_declaration(declaration_t *declaration);
790
791 static const char *parse_string_literals(void)
792 {
793         assert(token.type == T_STRING_LITERAL);
794         const char *result = token.v.string;
795
796         next_token();
797
798         while(token.type == T_STRING_LITERAL) {
799                 result = concat_strings(result, token.v.string);
800                 next_token();
801         }
802
803         return result;
804 }
805
806 static void parse_attributes(void)
807 {
808         while(true) {
809                 switch(token.type) {
810                 case T___attribute__:
811                         next_token();
812
813                         expect_void('(');
814                         int depth = 1;
815                         while(depth > 0) {
816                                 switch(token.type) {
817                                 case T_EOF:
818                                         parse_error("EOF while parsing attribute");
819                                         break;
820                                 case '(':
821                                         next_token();
822                                         depth++;
823                                         break;
824                                 case ')':
825                                         next_token();
826                                         depth--;
827                                         break;
828                                 default:
829                                         next_token();
830                                 }
831                         }
832                         break;
833                 case T_asm:
834                         next_token();
835                         expect_void('(');
836                         if(token.type != T_STRING_LITERAL) {
837                                 parse_error_expected("while parsing assembler attribute",
838                                                      T_STRING_LITERAL);
839                                 eat_brace();
840                                 break;
841                         } else {
842                                 parse_string_literals();
843                         }
844                         expect_void(')');
845                         break;
846                 default:
847                         goto attributes_finished;
848                 }
849         }
850
851 attributes_finished:
852         ;
853 }
854
855 #if 0
856 static designator_t *parse_designation(void)
857 {
858         if(token.type != '[' && token.type != '.')
859                 return NULL;
860
861         designator_t *result = NULL;
862         designator_t *last   = NULL;
863
864         while(1) {
865                 designator_t *designator;
866                 switch(token.type) {
867                 case '[':
868                         designator = allocate_ast_zero(sizeof(designator[0]));
869                         next_token();
870                         designator->array_access = parse_constant_expression();
871                         expect(']');
872                         break;
873                 case '.':
874                         designator = allocate_ast_zero(sizeof(designator[0]));
875                         next_token();
876                         if(token.type != T_IDENTIFIER) {
877                                 parse_error_expected("while parsing designator",
878                                                      T_IDENTIFIER, 0);
879                                 return NULL;
880                         }
881                         designator->symbol = token.v.symbol;
882                         next_token();
883                         break;
884                 default:
885                         expect('=');
886                         return result;
887                 }
888
889                 assert(designator != NULL);
890                 if(last != NULL) {
891                         last->next = designator;
892                 } else {
893                         result = designator;
894                 }
895                 last = designator;
896         }
897 }
898 #endif
899
900 static initializer_t *initializer_from_expression(type_t *type,
901                                                   expression_t *expression)
902 {
903         initializer_value_t *result = allocate_ast_zero(sizeof(result[0]));
904
905         /* TODO check that expression is a constant expression */
906
907         /* Â§ 6.7.8.14/15 char array may be initialized by string literals */
908         if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
909                 array_type_t *array_type   = (array_type_t*) type;
910                 type_t       *element_type = array_type->element_type;
911
912                 if(element_type->type == TYPE_ATOMIC) {
913                         atomic_type_t      *atomic_type = (atomic_type_t*) element_type;
914                         atomic_type_type_t  atype       = atomic_type->atype;
915
916                         /* TODO handle wide strings */
917                         if(atype == ATOMIC_TYPE_CHAR
918                                         || atype == ATOMIC_TYPE_SCHAR
919                                         || atype == ATOMIC_TYPE_UCHAR) {
920                                 /* it's fine TODO: check for length of string array... */
921                                 goto initializer_from_expression_finished;
922                         }
923                 }
924         }
925
926         semantic_assign(type, &expression, "initializer");
927
928 initializer_from_expression_finished:
929         result->initializer.type = INITIALIZER_VALUE;
930         result->value            = expression;
931
932         return (initializer_t*) result;
933 }
934
935 static initializer_t *parse_sub_initializer(type_t *type,
936                                             expression_t *expression,
937                                             type_t *expression_type);
938
939 static initializer_t *parse_sub_initializer_elem(type_t *type)
940 {
941         if(token.type == '{') {
942                 return parse_sub_initializer(type, NULL, NULL);
943         }
944
945         expression_t *expression      = parse_assignment_expression();
946         type_t       *expression_type = skip_typeref(expression->datatype);
947
948         return parse_sub_initializer(type, expression, expression_type);
949 }
950
951 static bool had_initializer_brace_warning;
952
953 static initializer_t *parse_sub_initializer(type_t *type,
954                                             expression_t *expression,
955                                             type_t *expression_type)
956 {
957         if(is_type_scalar(type)) {
958                 /* there might be extra {} hierarchies */
959                 if(token.type == '{') {
960                         next_token();
961                         if(!had_initializer_brace_warning) {
962                                 parse_warning("braces around scalar initializer");
963                                 had_initializer_brace_warning = true;
964                         }
965                         initializer_t *result = parse_sub_initializer(type, NULL, NULL);
966                         if(token.type == ',') {
967                                 next_token();
968                                 /* TODO: warn about excessive elements */
969                         }
970                         expect_block('}');
971                         return result;
972                 }
973
974                 if(expression == NULL) {
975                         expression = parse_assignment_expression();
976                 }
977                 return initializer_from_expression(type, expression);
978         }
979
980         /* TODO: ignore qualifiers, comparing pointers is probably
981          * not correct */
982         if(expression != NULL && expression_type == type) {
983                 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
984                 result->type          = INITIALIZER_VALUE;
985
986                 if(type != NULL) {
987                         semantic_assign(type, &expression, "initializer");
988                 }
989                 //result->v.value = expression;
990
991                 return result;
992         }
993
994         bool read_paren = false;
995         if(token.type == '{') {
996                 next_token();
997                 read_paren = true;
998         }
999
1000         /* descend into subtype */
1001         initializer_t *result = NULL;
1002         if(type->type == TYPE_ARRAY) {
1003                 array_type_t *array_type   = (array_type_t*) type;
1004                 type_t       *element_type = array_type->element_type;
1005                 element_type               = skip_typeref(element_type);
1006
1007                 result
1008                         = parse_sub_initializer(element_type, expression, expression_type);
1009         } else {
1010                 assert(type->type == TYPE_COMPOUND_STRUCT
1011                                 || type->type == TYPE_COMPOUND_UNION);
1012                 compound_type_t *compound_type = (compound_type_t*) type;
1013                 context_t       *context       = & compound_type->declaration->context;
1014
1015                 declaration_t *first = context->declarations;
1016                 if(first == NULL)
1017                         return NULL;
1018                 type_t *first_type = first->type;
1019                 first_type         = skip_typeref(first_type);
1020
1021                 initializer_t *sub;
1022                 had_initializer_brace_warning = false;
1023                 if(expression == NULL) {
1024                         sub = parse_sub_initializer_elem(first_type);
1025                 } else {
1026                         sub = parse_sub_initializer(first_type, expression,expression_type);
1027                 }
1028
1029                 /* didn't match the subtypes -> try our parent type */
1030                 if(sub == NULL) {
1031                         assert(!read_paren);
1032                         return NULL;
1033                 }
1034
1035                 initializer_t **elems = NEW_ARR_F(initializer_t*, 0);
1036                 ARR_APP1(initializer_t*, elems, sub);
1037
1038                 declaration_t *iter  = first->next;
1039                 for( ; iter != NULL; iter = iter->next) {
1040                         if(iter->symbol == NULL)
1041                                 continue;
1042                         if(iter->namespc != NAMESPACE_NORMAL)
1043                                 continue;
1044
1045                         if(token.type == '}')
1046                                 break;
1047                         expect_block(',');
1048
1049                         type_t *iter_type = iter->type;
1050                         iter_type         = skip_typeref(iter_type);
1051
1052                         /* read next token */
1053                         initializer_t *sub = parse_sub_initializer(iter_type, NULL, NULL);
1054                         if(sub == NULL) {
1055                                 /* TODO error, do nicer cleanup*/
1056                                 parse_error("member initializer didn't match");
1057                                 DEL_ARR_F(elems);
1058                                 return NULL;
1059                         }
1060                         ARR_APP1(initializer_t*, elems, sub);
1061                 }
1062
1063                 int    len        = ARR_LEN(elems);
1064                 size_t elems_size = sizeof(initializer_t*) * len;
1065
1066                 initializer_list_t *init
1067                         = allocate_ast_zero(sizeof(init[0]) + elems_size);
1068
1069                 init->initializer.type = INITIALIZER_LIST;
1070                 init->len              = len;
1071                 memcpy(init->initializers, elems, elems_size);
1072                 DEL_ARR_F(elems);
1073
1074                 result = (initializer_t*) init;
1075         }
1076
1077         if(read_paren) {
1078                 if(token.type == ',')
1079                         next_token();
1080                 expect('}');
1081         }
1082         return result;
1083 }
1084
1085 static initializer_t *parse_initializer(type_t *type)
1086 {
1087         initializer_t *result;
1088
1089         type = skip_typeref(type);
1090
1091         if(token.type != '{') {
1092                 expression_t *expression = parse_assignment_expression();
1093                 return initializer_from_expression(type, expression);
1094         }
1095
1096         if(is_type_scalar(type)) {
1097                 /* Â§ 6.7.8.11 */
1098                 eat('{');
1099
1100                 expression_t *expression = parse_assignment_expression();
1101                 result = initializer_from_expression(type, expression);
1102
1103                 if(token.type == ',')
1104                         next_token();
1105
1106                 expect('}');
1107                 return result;
1108         } else {
1109                 result = parse_sub_initializer(type, NULL, NULL);
1110         }
1111
1112         return result;
1113 }
1114
1115
1116
1117 static declaration_t *parse_compound_type_specifier(bool is_struct)
1118 {
1119         if(is_struct) {
1120                 eat(T_struct);
1121         } else {
1122                 eat(T_union);
1123         }
1124
1125         symbol_t      *symbol      = NULL;
1126         declaration_t *declaration = NULL;
1127
1128         if (token.type == T___attribute__) {
1129                 /* TODO */
1130                 parse_attributes();
1131         }
1132
1133         if(token.type == T_IDENTIFIER) {
1134                 symbol = token.v.symbol;
1135                 next_token();
1136
1137                 if(is_struct) {
1138                         declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1139                 } else {
1140                         declaration = get_declaration(symbol, NAMESPACE_UNION);
1141                 }
1142         } else if(token.type != '{') {
1143                 if(is_struct) {
1144                         parse_error_expected("while parsing struct type specifier",
1145                                              T_IDENTIFIER, '{', 0);
1146                 } else {
1147                         parse_error_expected("while parsing union type specifier",
1148                                              T_IDENTIFIER, '{', 0);
1149                 }
1150
1151                 return NULL;
1152         }
1153
1154         if(declaration == NULL) {
1155                 declaration = allocate_type_zero(sizeof(declaration[0]));
1156
1157                 if(is_struct) {
1158                         declaration->namespc = NAMESPACE_STRUCT;
1159                 } else {
1160                         declaration->namespc = NAMESPACE_UNION;
1161                 }
1162                 declaration->source_position = token.source_position;
1163                 declaration->symbol          = symbol;
1164                 record_declaration(declaration);
1165         }
1166
1167         if(token.type == '{') {
1168                 if(declaration->init.is_defined) {
1169                         assert(symbol != NULL);
1170                         parser_print_error_prefix();
1171                         fprintf(stderr, "multiple definition of %s %s\n",
1172                                         is_struct ? "struct" : "union", symbol->string);
1173                         declaration->context.declarations = NULL;
1174                 }
1175                 declaration->init.is_defined = true;
1176
1177                 int         top          = environment_top();
1178                 context_t  *last_context = context;
1179                 set_context(& declaration->context);
1180
1181                 parse_compound_type_entries();
1182                 parse_attributes();
1183
1184                 assert(context == & declaration->context);
1185                 set_context(last_context);
1186                 environment_pop_to(top);
1187         }
1188
1189         return declaration;
1190 }
1191
1192 static void parse_enum_entries(void)
1193 {
1194         eat('{');
1195
1196         if(token.type == '}') {
1197                 next_token();
1198                 parse_error("empty enum not allowed");
1199                 return;
1200         }
1201
1202         do {
1203                 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1204
1205                 if(token.type != T_IDENTIFIER) {
1206                         parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1207                         eat_block();
1208                         return;
1209                 }
1210                 entry->storage_class   = STORAGE_CLASS_ENUM_ENTRY;
1211                 entry->symbol          = token.v.symbol;
1212                 entry->source_position = token.source_position;
1213                 next_token();
1214
1215                 if(token.type == '=') {
1216                         next_token();
1217                         entry->init.initializer = parse_initializer(type_int);
1218                 }
1219
1220                 record_declaration(entry);
1221
1222                 if(token.type != ',')
1223                         break;
1224                 next_token();
1225         } while(token.type != '}');
1226
1227         expect_void('}');
1228 }
1229
1230 static declaration_t *parse_enum_specifier(void)
1231 {
1232         eat(T_enum);
1233
1234         declaration_t *declaration;
1235         symbol_t      *symbol;
1236
1237         if(token.type == T_IDENTIFIER) {
1238                 symbol = token.v.symbol;
1239                 next_token();
1240
1241                 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1242         } else if(token.type != '{') {
1243                 parse_error_expected("while parsing enum type specifier",
1244                                      T_IDENTIFIER, '{', 0);
1245                 return NULL;
1246         } else {
1247                 declaration = NULL;
1248                 symbol      = NULL;
1249         }
1250
1251         if(declaration == NULL) {
1252                 declaration = allocate_type_zero(sizeof(declaration[0]));
1253
1254                 declaration->namespc       = NAMESPACE_ENUM;
1255                 declaration->source_position = token.source_position;
1256                 declaration->symbol          = symbol;
1257         }
1258
1259         if(token.type == '{') {
1260                 if(declaration->init.is_defined) {
1261                         parser_print_error_prefix();
1262                         fprintf(stderr, "multiple definitions of enum %s\n",
1263                                 symbol->string);
1264                 }
1265                 record_declaration(declaration);
1266                 declaration->init.is_defined = 1;
1267
1268                 parse_enum_entries();
1269                 parse_attributes();
1270         }
1271
1272         return declaration;
1273 }
1274
1275 /**
1276  * if a symbol is a typedef to another type, return true
1277  */
1278 static bool is_typedef_symbol(symbol_t *symbol)
1279 {
1280         const declaration_t *const declaration =
1281                 get_declaration(symbol, NAMESPACE_NORMAL);
1282         return
1283                 declaration != NULL &&
1284                 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1285 }
1286
1287 static type_t *parse_typeof(void)
1288 {
1289         eat(T___typeof__);
1290
1291         type_t *type;
1292
1293         expect('(');
1294
1295         expression_t *expression  = NULL;
1296
1297 restart:
1298         switch(token.type) {
1299         case T___extension__:
1300                 /* this can be a prefix to a typename or an expression */
1301                 /* we simply eat it now. */
1302                 do {
1303                         next_token();
1304                 } while(token.type == T___extension__);
1305                 goto restart;
1306
1307         case T_IDENTIFIER:
1308                 if(is_typedef_symbol(token.v.symbol)) {
1309                         type = parse_typename();
1310                 } else {
1311                         expression = parse_expression();
1312                         type       = expression->datatype;
1313                 }
1314                 break;
1315
1316         TYPENAME_START
1317                 type = parse_typename();
1318                 break;
1319
1320         default:
1321                 expression = parse_expression();
1322                 type       = expression->datatype;
1323                 break;
1324         }
1325
1326         expect(')');
1327
1328         typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1329         typeof->type.type     = TYPE_TYPEOF;
1330         typeof->expression    = expression;
1331         typeof->typeof_type   = type;
1332
1333         return (type_t*) typeof;
1334 }
1335
1336 typedef enum {
1337         SPECIFIER_SIGNED    = 1 << 0,
1338         SPECIFIER_UNSIGNED  = 1 << 1,
1339         SPECIFIER_LONG      = 1 << 2,
1340         SPECIFIER_INT       = 1 << 3,
1341         SPECIFIER_DOUBLE    = 1 << 4,
1342         SPECIFIER_CHAR      = 1 << 5,
1343         SPECIFIER_SHORT     = 1 << 6,
1344         SPECIFIER_LONG_LONG = 1 << 7,
1345         SPECIFIER_FLOAT     = 1 << 8,
1346         SPECIFIER_BOOL      = 1 << 9,
1347         SPECIFIER_VOID      = 1 << 10,
1348 #ifdef PROVIDE_COMPLEX
1349         SPECIFIER_COMPLEX   = 1 << 11,
1350 #endif
1351 #ifdef PROVIDE_IMAGINARY
1352         SPECIFIER_IMAGINARY = 1 << 12,
1353 #endif
1354 } specifiers_t;
1355
1356 static type_t *create_builtin_type(symbol_t *symbol)
1357 {
1358         builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1359         type->type.type      = TYPE_BUILTIN;
1360         type->symbol         = symbol;
1361         /* TODO... */
1362         type->real_type      = type_int;
1363
1364         return (type_t*) type;
1365 }
1366
1367 static type_t *get_typedef_type(symbol_t *symbol)
1368 {
1369         declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1370         if(declaration == NULL
1371                         || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1372                 return NULL;
1373
1374         typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1375         typedef_type->type.type    = TYPE_TYPEDEF;
1376         typedef_type->declaration  = declaration;
1377
1378         return (type_t*) typedef_type;
1379 }
1380
1381 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1382 {
1383         type_t        *type            = NULL;
1384         unsigned       type_qualifiers = 0;
1385         unsigned       type_specifiers = 0;
1386         int            newtype         = 0;
1387
1388         while(true) {
1389                 switch(token.type) {
1390
1391                 /* storage class */
1392 #define MATCH_STORAGE_CLASS(token, class)                                \
1393                 case token:                                                      \
1394                         if(specifiers->storage_class != STORAGE_CLASS_NONE) {        \
1395                                 parse_error("multiple storage classes in declaration "   \
1396                                             "specifiers");                               \
1397                         }                                                            \
1398                         specifiers->storage_class = class;                           \
1399                         next_token();                                                \
1400                         break;
1401
1402                 MATCH_STORAGE_CLASS(T_typedef,  STORAGE_CLASS_TYPEDEF)
1403                 MATCH_STORAGE_CLASS(T_extern,   STORAGE_CLASS_EXTERN)
1404                 MATCH_STORAGE_CLASS(T_static,   STORAGE_CLASS_STATIC)
1405                 MATCH_STORAGE_CLASS(T_auto,     STORAGE_CLASS_AUTO)
1406                 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1407
1408                 /* type qualifiers */
1409 #define MATCH_TYPE_QUALIFIER(token, qualifier)                          \
1410                 case token:                                                     \
1411                         type_qualifiers |= qualifier;                               \
1412                         next_token();                                               \
1413                         break;
1414
1415                 MATCH_TYPE_QUALIFIER(T_const,    TYPE_QUALIFIER_CONST);
1416                 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1417                 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1418
1419                 case T___extension__:
1420                         /* TODO */
1421                         next_token();
1422                         break;
1423
1424                 /* type specifiers */
1425 #define MATCH_SPECIFIER(token, specifier, name)                         \
1426                 case token:                                                     \
1427                         next_token();                                               \
1428                         if(type_specifiers & specifier) {                           \
1429                                 parse_error("multiple " name " type specifiers given"); \
1430                         } else {                                                    \
1431                                 type_specifiers |= specifier;                           \
1432                         }                                                           \
1433                         break;
1434
1435                 MATCH_SPECIFIER(T_void,       SPECIFIER_VOID,      "void")
1436                 MATCH_SPECIFIER(T_char,       SPECIFIER_CHAR,      "char")
1437                 MATCH_SPECIFIER(T_short,      SPECIFIER_SHORT,     "short")
1438                 MATCH_SPECIFIER(T_int,        SPECIFIER_INT,       "int")
1439                 MATCH_SPECIFIER(T_float,      SPECIFIER_FLOAT,     "float")
1440                 MATCH_SPECIFIER(T_double,     SPECIFIER_DOUBLE,    "double")
1441                 MATCH_SPECIFIER(T_signed,     SPECIFIER_SIGNED,    "signed")
1442                 MATCH_SPECIFIER(T_unsigned,   SPECIFIER_UNSIGNED,  "unsigned")
1443                 MATCH_SPECIFIER(T__Bool,      SPECIFIER_BOOL,      "_Bool")
1444 #ifdef PROVIDE_COMPLEX
1445                 MATCH_SPECIFIER(T__Complex,   SPECIFIER_COMPLEX,   "_Complex")
1446 #endif
1447 #ifdef PROVIDE_IMAGINARY
1448                 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1449 #endif
1450                 case T_inline:
1451                         next_token();
1452                         specifiers->is_inline = true;
1453                         break;
1454
1455                 case T_long:
1456                         next_token();
1457                         if(type_specifiers & SPECIFIER_LONG_LONG) {
1458                                 parse_error("multiple type specifiers given");
1459                         } else if(type_specifiers & SPECIFIER_LONG) {
1460                                 type_specifiers |= SPECIFIER_LONG_LONG;
1461                         } else {
1462                                 type_specifiers |= SPECIFIER_LONG;
1463                         }
1464                         break;
1465
1466                 /* TODO: if type != NULL for the following rules should issue
1467                  * an error */
1468                 case T_struct: {
1469                         compound_type_t *compound_type
1470                                 = allocate_type_zero(sizeof(compound_type[0]));
1471                         compound_type->type.type = TYPE_COMPOUND_STRUCT;
1472                         compound_type->declaration = parse_compound_type_specifier(true);
1473
1474                         type = (type_t*) compound_type;
1475                         break;
1476                 }
1477                 case T_union: {
1478                         compound_type_t *compound_type
1479                                 = allocate_type_zero(sizeof(compound_type[0]));
1480                         compound_type->type.type = TYPE_COMPOUND_UNION;
1481                         compound_type->declaration = parse_compound_type_specifier(false);
1482
1483                         type = (type_t*) compound_type;
1484                         break;
1485                 }
1486                 case T_enum: {
1487                         enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1488                         enum_type->type.type   = TYPE_ENUM;
1489                         enum_type->declaration = parse_enum_specifier();
1490
1491                         type = (type_t*) enum_type;
1492                         break;
1493                 }
1494                 case T___typeof__:
1495                         type = parse_typeof();
1496                         break;
1497                 case T___builtin_va_list:
1498                         type = create_builtin_type(token.v.symbol);
1499                         next_token();
1500                         break;
1501
1502                 case T___attribute__:
1503                         /* TODO */
1504                         parse_attributes();
1505                         break;
1506
1507                 case T_IDENTIFIER: {
1508                         type_t *typedef_type = get_typedef_type(token.v.symbol);
1509
1510                         if(typedef_type == NULL)
1511                                 goto finish_specifiers;
1512
1513                         next_token();
1514                         type = typedef_type;
1515                         break;
1516                 }
1517
1518                 /* function specifier */
1519                 default:
1520                         goto finish_specifiers;
1521                 }
1522         }
1523
1524 finish_specifiers:
1525
1526         if(type == NULL) {
1527                 atomic_type_type_t atomic_type;
1528
1529                 /* match valid basic types */
1530                 switch(type_specifiers) {
1531                 case SPECIFIER_VOID:
1532                         atomic_type = ATOMIC_TYPE_VOID;
1533                         break;
1534                 case SPECIFIER_CHAR:
1535                         atomic_type = ATOMIC_TYPE_CHAR;
1536                         break;
1537                 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1538                         atomic_type = ATOMIC_TYPE_SCHAR;
1539                         break;
1540                 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1541                         atomic_type = ATOMIC_TYPE_UCHAR;
1542                         break;
1543                 case SPECIFIER_SHORT:
1544                 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1545                 case SPECIFIER_SHORT | SPECIFIER_INT:
1546                 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1547                         atomic_type = ATOMIC_TYPE_SHORT;
1548                         break;
1549                 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1550                 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1551                         atomic_type = ATOMIC_TYPE_USHORT;
1552                         break;
1553                 case SPECIFIER_INT:
1554                 case SPECIFIER_SIGNED:
1555                 case SPECIFIER_SIGNED | SPECIFIER_INT:
1556                         atomic_type = ATOMIC_TYPE_INT;
1557                         break;
1558                 case SPECIFIER_UNSIGNED:
1559                 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1560                         atomic_type = ATOMIC_TYPE_UINT;
1561                         break;
1562                 case SPECIFIER_LONG:
1563                 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1564                 case SPECIFIER_LONG | SPECIFIER_INT:
1565                 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1566                         atomic_type = ATOMIC_TYPE_LONG;
1567                         break;
1568                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1569                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1570                         atomic_type = ATOMIC_TYPE_ULONG;
1571                         break;
1572                 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1573                 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1574                 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1575                 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1576                         | SPECIFIER_INT:
1577                         atomic_type = ATOMIC_TYPE_LONGLONG;
1578                         break;
1579                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1580                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1581                         | SPECIFIER_INT:
1582                         atomic_type = ATOMIC_TYPE_ULONGLONG;
1583                         break;
1584                 case SPECIFIER_FLOAT:
1585                         atomic_type = ATOMIC_TYPE_FLOAT;
1586                         break;
1587                 case SPECIFIER_DOUBLE:
1588                         atomic_type = ATOMIC_TYPE_DOUBLE;
1589                         break;
1590                 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1591                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1592                         break;
1593                 case SPECIFIER_BOOL:
1594                         atomic_type = ATOMIC_TYPE_BOOL;
1595                         break;
1596 #ifdef PROVIDE_COMPLEX
1597                 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1598                         atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1599                         break;
1600                 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1601                         atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1602                         break;
1603                 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1604                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1605                         break;
1606 #endif
1607 #ifdef PROVIDE_IMAGINARY
1608                 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1609                         atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1610                         break;
1611                 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1612                         atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1613                         break;
1614                 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1615                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1616                         break;
1617 #endif
1618                 default:
1619                         /* invalid specifier combination, give an error message */
1620                         if(type_specifiers == 0) {
1621 #ifndef STRICT_C99
1622                                 parse_warning("no type specifiers in declaration (using int)");
1623                                 atomic_type = ATOMIC_TYPE_INT;
1624                                 break;
1625 #else
1626                                 parse_error("no type specifiers given in declaration");
1627 #endif
1628                         } else if((type_specifiers & SPECIFIER_SIGNED) &&
1629                                   (type_specifiers & SPECIFIER_UNSIGNED)) {
1630                                 parse_error("signed and unsigned specifiers gives");
1631                         } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1632                                 parse_error("only integer types can be signed or unsigned");
1633                         } else {
1634                                 parse_error("multiple datatypes in declaration");
1635                         }
1636                         atomic_type = ATOMIC_TYPE_INVALID;
1637                 }
1638
1639                 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1640                 atype->type.type     = TYPE_ATOMIC;
1641                 atype->atype         = atomic_type;
1642                 newtype              = 1;
1643
1644                 type = (type_t*) atype;
1645         } else {
1646                 if(type_specifiers != 0) {
1647                         parse_error("multiple datatypes in declaration");
1648                 }
1649         }
1650
1651         type->qualifiers = (type_qualifier_t)type_qualifiers;
1652
1653         type_t *result = typehash_insert(type);
1654         if(newtype && result != (type_t*) type) {
1655                 free_type(type);
1656         }
1657
1658         specifiers->type = result;
1659 }
1660
1661 static unsigned parse_type_qualifiers(void)
1662 {
1663         unsigned type_qualifiers = TYPE_QUALIFIER_NONE;
1664
1665         while(true) {
1666                 switch(token.type) {
1667                 /* type qualifiers */
1668                 MATCH_TYPE_QUALIFIER(T_const,    TYPE_QUALIFIER_CONST);
1669                 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1670                 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1671
1672                 default:
1673                         return type_qualifiers;
1674                 }
1675         }
1676 }
1677
1678 static void parse_identifier_list(void)
1679 {
1680         while(true) {
1681                 if(token.type != T_IDENTIFIER) {
1682                         parse_error_expected("while parsing parameter identifier list",
1683                                              T_IDENTIFIER, 0);
1684                         return;
1685                 }
1686                 next_token();
1687                 if(token.type != ',')
1688                         break;
1689                 next_token();
1690         }
1691 }
1692
1693 static declaration_t *parse_parameter(void)
1694 {
1695         declaration_specifiers_t specifiers;
1696         memset(&specifiers, 0, sizeof(specifiers));
1697
1698         parse_declaration_specifiers(&specifiers);
1699
1700         declaration_t *declaration
1701                 = parse_declarator(&specifiers, specifiers.type, true);
1702
1703         /* TODO check declaration constraints for parameters */
1704         if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1705                 parse_error("typedef not allowed in parameter list");
1706         }
1707
1708         /* Array as last part of a paramter type is just syntactic sugar.  Turn it
1709          * into a pointer */
1710         if (declaration->type->type == TYPE_ARRAY) {
1711                 const array_type_t *const arr_type =
1712                         (const array_type_t*)declaration->type;
1713                 declaration->type =
1714                         make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
1715         }
1716
1717         return declaration;
1718 }
1719
1720 static declaration_t *parse_parameters(function_type_t *type)
1721 {
1722         if(token.type == T_IDENTIFIER) {
1723                 symbol_t      *symbol = token.v.symbol;
1724                 if(!is_typedef_symbol(symbol)) {
1725                         /* TODO */
1726                         parse_identifier_list();
1727                         return NULL;
1728                 }
1729         }
1730
1731         if(token.type == ')') {
1732                 type->unspecified_parameters = 1;
1733                 return NULL;
1734         }
1735         if(token.type == T_void && look_ahead(1)->type == ')') {
1736                 next_token();
1737                 return NULL;
1738         }
1739
1740         declaration_t        *declarations = NULL;
1741         declaration_t        *declaration;
1742         declaration_t        *last_declaration = NULL;
1743         function_parameter_t *parameter;
1744         function_parameter_t *last_parameter = NULL;
1745
1746         while(true) {
1747                 switch(token.type) {
1748                 case T_DOTDOTDOT:
1749                         next_token();
1750                         type->variadic = 1;
1751                         return declarations;
1752
1753                 case T_IDENTIFIER:
1754                 case T___extension__:
1755                 DECLARATION_START
1756                         declaration = parse_parameter();
1757
1758                         parameter       = allocate_type_zero(sizeof(parameter[0]));
1759                         parameter->type = declaration->type;
1760
1761                         if(last_parameter != NULL) {
1762                                 last_declaration->next = declaration;
1763                                 last_parameter->next   = parameter;
1764                         } else {
1765                                 type->parameters = parameter;
1766                                 declarations     = declaration;
1767                         }
1768                         last_parameter   = parameter;
1769                         last_declaration = declaration;
1770                         break;
1771
1772                 default:
1773                         return declarations;
1774                 }
1775                 if(token.type != ',')
1776                         return declarations;
1777                 next_token();
1778         }
1779 }
1780
1781 typedef enum {
1782         CONSTRUCT_INVALID,
1783         CONSTRUCT_POINTER,
1784         CONSTRUCT_FUNCTION,
1785         CONSTRUCT_ARRAY
1786 } construct_type_type_t;
1787
1788 typedef struct construct_type_t construct_type_t;
1789 struct construct_type_t {
1790         construct_type_type_t  type;
1791         construct_type_t      *next;
1792 };
1793
1794 typedef struct parsed_pointer_t parsed_pointer_t;
1795 struct parsed_pointer_t {
1796         construct_type_t  construct_type;
1797         type_qualifier_t  type_qualifiers;
1798 };
1799
1800 typedef struct construct_function_type_t construct_function_type_t;
1801 struct construct_function_type_t {
1802         construct_type_t    construct_type;
1803         function_type_t    *function_type;
1804 };
1805
1806 typedef struct parsed_array_t parsed_array_t;
1807 struct parsed_array_t {
1808         construct_type_t  construct_type;
1809         type_qualifier_t  type_qualifiers;
1810         bool              is_static;
1811         bool              is_variable;
1812         expression_t     *size;
1813 };
1814
1815 typedef struct construct_base_type_t construct_base_type_t;
1816 struct construct_base_type_t {
1817         construct_type_t  construct_type;
1818         type_t           *type;
1819 };
1820
1821 static construct_type_t *parse_pointer_declarator(void)
1822 {
1823         eat('*');
1824
1825         parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1826         memset(pointer, 0, sizeof(pointer[0]));
1827         pointer->construct_type.type = CONSTRUCT_POINTER;
1828         pointer->type_qualifiers     = parse_type_qualifiers();
1829
1830         return (construct_type_t*) pointer;
1831 }
1832
1833 static construct_type_t *parse_array_declarator(void)
1834 {
1835         eat('[');
1836
1837         parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1838         memset(array, 0, sizeof(array[0]));
1839         array->construct_type.type = CONSTRUCT_ARRAY;
1840
1841         if(token.type == T_static) {
1842                 array->is_static = true;
1843                 next_token();
1844         }
1845
1846         type_qualifier_t type_qualifiers = parse_type_qualifiers();
1847         if(type_qualifiers != 0) {
1848                 if(token.type == T_static) {
1849                         array->is_static = true;
1850                         next_token();
1851                 }
1852         }
1853         array->type_qualifiers = type_qualifiers;
1854
1855         if(token.type == '*' && look_ahead(1)->type == ']') {
1856                 array->is_variable = true;
1857                 next_token();
1858         } else if(token.type != ']') {
1859                 array->size = parse_assignment_expression();
1860         }
1861
1862         expect(']');
1863
1864         return (construct_type_t*) array;
1865 }
1866
1867 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1868 {
1869         eat('(');
1870
1871         function_type_t *type = allocate_type_zero(sizeof(type[0]));
1872         type->type.type       = TYPE_FUNCTION;
1873
1874         declaration_t *parameters = parse_parameters(type);
1875         if(declaration != NULL) {
1876                 declaration->context.declarations = parameters;
1877         }
1878
1879         construct_function_type_t *construct_function_type =
1880                 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1881         memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1882         construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1883         construct_function_type->function_type       = type;
1884
1885         expect(')');
1886
1887         return (construct_type_t*) construct_function_type;
1888 }
1889
1890 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1891                 int may_be_abstract)
1892 {
1893         construct_type_t *result = NULL;
1894         construct_type_t *last   = NULL;
1895
1896         while(token.type == '*') {
1897                 construct_type_t *type = parse_pointer_declarator();
1898                 if(last != NULL) {
1899                         last->next = type;
1900                 } else {
1901                         result = type;
1902                 }
1903                 last = type;
1904         }
1905
1906         /* TODO: find out if this is correct */
1907         parse_attributes();
1908
1909         construct_type_t *inner_types = NULL;
1910
1911         switch(token.type) {
1912         case T_IDENTIFIER:
1913                 if(declaration == NULL) {
1914                         parse_error("no identifier expected in typename");
1915                 } else {
1916                         declaration->symbol          = token.v.symbol;
1917                         declaration->source_position = token.source_position;
1918                 }
1919                 next_token();
1920                 break;
1921         case '(':
1922                 next_token();
1923                 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1924                 expect(')');
1925                 break;
1926         default:
1927                 if(may_be_abstract)
1928                         break;
1929                 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
1930                 /* avoid a loop in the outermost scope, because eat_statement doesn't
1931                  * eat '}' */
1932                 if(token.type == '}' && current_function == NULL) {
1933                         next_token();
1934                 } else {
1935                         eat_statement();
1936                 }
1937                 return NULL;
1938         }
1939
1940         while(true) {
1941                 construct_type_t *type;
1942                 switch(token.type) {
1943                 case '(':
1944                         type = parse_function_declarator(declaration);
1945                         break;
1946                 case '[':
1947                         type = parse_array_declarator();
1948                         break;
1949                 default:
1950                         goto declarator_finished;
1951                 }
1952
1953                 if(last != NULL) {
1954                         last->next = type;
1955                 } else {
1956                         result = type;
1957                 }
1958                 last = type;
1959         }
1960
1961 declarator_finished:
1962         parse_attributes();
1963
1964         if(inner_types != NULL) {
1965                 if(last != NULL) {
1966                         last->next = inner_types;
1967                 } else {
1968                         result = inner_types;
1969                 }
1970                 last = inner_types;
1971         }
1972
1973         return result;
1974 }
1975
1976 static type_t *construct_declarator_type(construct_type_t *construct_list,
1977                                          type_t *type)
1978 {
1979         construct_type_t *iter = construct_list;
1980         for( ; iter != NULL; iter = iter->next) {
1981                 parsed_pointer_t          *parsed_pointer;
1982                 parsed_array_t            *parsed_array;
1983                 construct_function_type_t *construct_function_type;
1984                 function_type_t           *function_type;
1985                 pointer_type_t            *pointer_type;
1986                 array_type_t              *array_type;
1987
1988                 switch(iter->type) {
1989                 case CONSTRUCT_INVALID:
1990                         panic("invalid type construction found");
1991                 case CONSTRUCT_FUNCTION:
1992                         construct_function_type = (construct_function_type_t*) iter;
1993                         function_type           = construct_function_type->function_type;
1994
1995                         function_type->result_type = type;
1996                         type                       = (type_t*) function_type;
1997                         break;
1998
1999                 case CONSTRUCT_POINTER:
2000                         parsed_pointer = (parsed_pointer_t*) iter;
2001                         pointer_type   = allocate_type_zero(sizeof(pointer_type[0]));
2002
2003                         pointer_type->type.type       = TYPE_POINTER;
2004                         pointer_type->points_to       = type;
2005                         pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
2006                         type                          = (type_t*) pointer_type;
2007                         break;
2008
2009                 case CONSTRUCT_ARRAY:
2010                         parsed_array  = (parsed_array_t*) iter;
2011                         array_type    = allocate_type_zero(sizeof(array_type[0]));
2012
2013                         array_type->type.type       = TYPE_ARRAY;
2014                         array_type->element_type    = type;
2015                         array_type->type.qualifiers = parsed_array->type_qualifiers;
2016                         array_type->is_static       = parsed_array->is_static;
2017                         array_type->is_variable     = parsed_array->is_variable;
2018                         array_type->size            = parsed_array->size;
2019                         type                        = (type_t*) array_type;
2020                         break;
2021                 }
2022
2023                 type_t *hashed_type = typehash_insert((type_t*) type);
2024                 if(hashed_type != type) {
2025                         free_type(type);
2026                         type = hashed_type;
2027                 }
2028         }
2029
2030         return type;
2031 }
2032
2033 static declaration_t *parse_declarator(
2034                 const declaration_specifiers_t *specifiers,
2035                 type_t *type, bool may_be_abstract)
2036 {
2037         declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2038         declaration->storage_class = specifiers->storage_class;
2039         declaration->is_inline     = specifiers->is_inline;
2040
2041         construct_type_t *construct_type
2042                 = parse_inner_declarator(declaration, may_be_abstract);
2043         declaration->type = construct_declarator_type(construct_type, type);
2044
2045         if(construct_type != NULL) {
2046                 obstack_free(&temp_obst, construct_type);
2047         }
2048
2049         return declaration;
2050 }
2051
2052 static type_t *parse_abstract_declarator(type_t *base_type)
2053 {
2054         construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2055
2056         type_t *result = construct_declarator_type(construct_type, base_type);
2057         if(construct_type != NULL) {
2058                 obstack_free(&temp_obst, construct_type);
2059         }
2060
2061         return result;
2062 }
2063
2064 static declaration_t *record_declaration(declaration_t *declaration)
2065 {
2066         assert(context != NULL);
2067
2068         symbol_t *symbol = declaration->symbol;
2069         if(symbol != NULL) {
2070                 declaration_t *alias = environment_push(declaration);
2071                 if(alias != declaration)
2072                         return alias;
2073         } else {
2074                 declaration->parent_context = context;
2075         }
2076
2077         if(last_declaration != NULL) {
2078                 last_declaration->next = declaration;
2079         } else {
2080                 context->declarations = declaration;
2081         }
2082         last_declaration = declaration;
2083
2084         return declaration;
2085 }
2086
2087 static void parser_error_multiple_definition(declaration_t *previous,
2088                                              declaration_t *declaration)
2089 {
2090         parser_print_error_prefix_pos(declaration->source_position);
2091         fprintf(stderr, "multiple definition of symbol '%s'\n",
2092                 declaration->symbol->string);
2093         parser_print_error_prefix_pos(previous->source_position);
2094         fprintf(stderr, "this is the location of the previous definition.\n");
2095 }
2096
2097 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
2098 {
2099         while(true) {
2100                 declaration_t *ndeclaration
2101                         = parse_declarator(specifiers, specifiers->type, false);
2102
2103                 declaration_t *declaration = record_declaration(ndeclaration);
2104
2105                 type_t *type = declaration->type;
2106                 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2107                         parser_print_warning_prefix_pos(declaration->source_position);
2108                         fprintf(stderr, "variable '%s' declared 'inline'\n",
2109                                 declaration->symbol->string);
2110                 }
2111
2112                 if(token.type == '=') {
2113                         next_token();
2114
2115                         /* TODO: check that this is an allowed type (no function type) */
2116
2117                         if(declaration->init.initializer != NULL) {
2118                                 parser_error_multiple_definition(declaration, ndeclaration);
2119                         }
2120
2121                         ndeclaration->init.initializer = parse_initializer(declaration->type);
2122                 } else if(token.type == '{') {
2123                         if(declaration->type->type != TYPE_FUNCTION) {
2124                                 parser_print_error_prefix();
2125                                 fprintf(stderr, "Declarator ");
2126                                 print_type_ext(declaration->type, declaration->symbol, NULL);
2127                                 fprintf(stderr, " has a body but is not a function type.\n");
2128                                 eat_block();
2129                                 continue;
2130                         }
2131
2132                         if(declaration->init.statement != NULL) {
2133                                 parser_error_multiple_definition(declaration, ndeclaration);
2134                         }
2135                         if(ndeclaration != declaration) {
2136                                 memcpy(&declaration->context, &ndeclaration->context,
2137                                        sizeof(declaration->context));
2138                         }
2139
2140                         int         top          = environment_top();
2141                         context_t  *last_context = context;
2142                         set_context(&declaration->context);
2143
2144                         /* push function parameters */
2145                         declaration_t *parameter = declaration->context.declarations;
2146                         for( ; parameter != NULL; parameter = parameter->next) {
2147                                 environment_push(parameter);
2148                         }
2149
2150                         int            label_stack_top      = label_top();
2151                         declaration_t *old_current_function = current_function;
2152                         current_function                    = declaration;
2153
2154                         statement_t *statement = parse_compound_statement();
2155
2156                         assert(current_function == declaration);
2157                         current_function = old_current_function;
2158                         label_pop_to(label_stack_top);
2159
2160                         assert(context == &declaration->context);
2161                         set_context(last_context);
2162                         environment_pop_to(top);
2163
2164                         declaration->init.statement = statement;
2165                         return;
2166                 }
2167
2168                 if(token.type != ',')
2169                         break;
2170                 next_token();
2171         }
2172         expect_void(';');
2173 }
2174
2175 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2176 {
2177         while(1) {
2178                 if(token.type == ':') {
2179                         next_token();
2180                         parse_constant_expression();
2181                         /* TODO (bitfields) */
2182                 } else {
2183                         declaration_t *declaration
2184                                 = parse_declarator(specifiers, specifiers->type, true);
2185
2186                         /* TODO: check constraints for struct declarations */
2187                         /* TODO: check for doubled fields */
2188                         record_declaration(declaration);
2189
2190                         if(token.type == ':') {
2191                                 next_token();
2192                                 parse_constant_expression();
2193                                 /* TODO (bitfields) */
2194                         }
2195                 }
2196
2197                 if(token.type != ',')
2198                         break;
2199                 next_token();
2200         }
2201         expect_void(';');
2202 }
2203
2204 static void parse_compound_type_entries(void)
2205 {
2206         eat('{');
2207
2208         while(token.type != '}' && token.type != T_EOF) {
2209                 declaration_specifiers_t specifiers;
2210                 memset(&specifiers, 0, sizeof(specifiers));
2211                 parse_declaration_specifiers(&specifiers);
2212
2213                 parse_struct_declarators(&specifiers);
2214         }
2215         if(token.type == T_EOF) {
2216                 parse_error("unexpected error while parsing struct");
2217         }
2218         next_token();
2219 }
2220
2221 static void parse_declaration(void)
2222 {
2223         source_position_t source_position = token.source_position;
2224
2225         declaration_specifiers_t specifiers;
2226         memset(&specifiers, 0, sizeof(specifiers));
2227         parse_declaration_specifiers(&specifiers);
2228
2229         if(token.type == ';') {
2230                 if (specifiers.storage_class != STORAGE_CLASS_NONE) {
2231                         parse_warning_pos(source_position,
2232                                           "useless keyword in empty declaration");
2233                 }
2234                 switch (specifiers.type->type) {
2235                         case TYPE_COMPOUND_STRUCT:
2236                         case TYPE_COMPOUND_UNION: {
2237                                 const compound_type_t *const comp_type =
2238                                         (const compound_type_t*)specifiers.type;
2239                                 if (comp_type->declaration->symbol == NULL) {
2240                                         parse_warning_pos(source_position,
2241                                                                                                                 "unnamed struct/union that defines no instances");
2242                                 }
2243                                 break;
2244                         }
2245
2246                         case TYPE_ENUM: break;
2247
2248                         default:
2249                                 parse_warning_pos(source_position, "empty declaration");
2250                                 break;
2251                 }
2252
2253                 next_token();
2254
2255                 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2256
2257                 declaration->type            = specifiers.type;
2258                 declaration->storage_class   = specifiers.storage_class;
2259                 declaration->source_position = source_position;
2260                 record_declaration(declaration);
2261                 return;
2262         }
2263         parse_init_declarators(&specifiers);
2264 }
2265
2266 static type_t *parse_typename(void)
2267 {
2268         declaration_specifiers_t specifiers;
2269         memset(&specifiers, 0, sizeof(specifiers));
2270         parse_declaration_specifiers(&specifiers);
2271         if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2272                 /* TODO: improve error message, user does probably not know what a
2273                  * storage class is...
2274                  */
2275                 parse_error("typename may not have a storage class");
2276         }
2277
2278         type_t *result = parse_abstract_declarator(specifiers.type);
2279
2280         return result;
2281 }
2282
2283
2284
2285
2286 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2287 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2288                                                           expression_t *left);
2289
2290 typedef struct expression_parser_function_t expression_parser_function_t;
2291 struct expression_parser_function_t {
2292         unsigned                         precedence;
2293         parse_expression_function        parser;
2294         unsigned                         infix_precedence;
2295         parse_expression_infix_function  infix_parser;
2296 };
2297
2298 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2299
2300 static expression_t *make_invalid_expression(void)
2301 {
2302         expression_t *expression    = allocate_ast_zero(sizeof(expression[0]));
2303         expression->type            = EXPR_INVALID;
2304         expression->source_position = token.source_position;
2305         return expression;
2306 }
2307
2308 static expression_t *expected_expression_error(void)
2309 {
2310         parser_print_error_prefix();
2311         fprintf(stderr, "expected expression, got token ");
2312         print_token(stderr, & token);
2313         fprintf(stderr, "\n");
2314
2315         next_token();
2316
2317         return make_invalid_expression();
2318 }
2319
2320 static expression_t *parse_string_const(void)
2321 {
2322         string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2323
2324         cnst->expression.type     = EXPR_STRING_LITERAL;
2325         cnst->expression.datatype = type_string;
2326         cnst->value               = parse_string_literals();
2327
2328         return (expression_t*) cnst;
2329 }
2330
2331 static expression_t *parse_int_const(void)
2332 {
2333         const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2334
2335         cnst->expression.type     = EXPR_CONST;
2336         cnst->expression.datatype = type_int;
2337         cnst->v.int_value         = token.v.intvalue;
2338
2339         next_token();
2340
2341         return (expression_t*) cnst;
2342 }
2343
2344 static expression_t *parse_float_const(void)
2345 {
2346         const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2347
2348         cnst->expression.type     = EXPR_CONST;
2349         cnst->expression.datatype = type_double;
2350         cnst->v.float_value       = token.v.floatvalue;
2351
2352         next_token();
2353
2354         return (expression_t*) cnst;
2355 }
2356
2357 static declaration_t *create_implicit_function(symbol_t *symbol,
2358                 const source_position_t source_position)
2359 {
2360         function_type_t *function_type
2361                 = allocate_type_zero(sizeof(function_type[0]));
2362
2363         function_type->type.type              = TYPE_FUNCTION;
2364         function_type->result_type            = type_int;
2365         function_type->unspecified_parameters = true;
2366
2367         type_t *type = typehash_insert((type_t*) function_type);
2368         if(type != (type_t*) function_type) {
2369                 free_type(function_type);
2370         }
2371
2372         declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2373
2374         declaration->storage_class   = STORAGE_CLASS_EXTERN;
2375         declaration->type            = type;
2376         declaration->symbol          = symbol;
2377         declaration->source_position = source_position;
2378
2379         /* prepend the implicit definition to the global context
2380          * this is safe since the symbol wasn't declared as anything else yet
2381          */
2382         assert(symbol->declaration == NULL);
2383
2384         context_t *last_context = context;
2385         context = global_context;
2386
2387         environment_push(declaration);
2388         declaration->next     = context->declarations;
2389         context->declarations = declaration;
2390
2391         context = last_context;
2392
2393         return declaration;
2394 }
2395
2396 static expression_t *parse_reference(void)
2397 {
2398         reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2399
2400         ref->expression.type = EXPR_REFERENCE;
2401         ref->symbol          = token.v.symbol;
2402
2403         declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2404
2405         source_position_t source_position = token.source_position;
2406         next_token();
2407
2408         if(declaration == NULL) {
2409 #ifndef STRICT_C99
2410                 /* an implicitly defined function */
2411                 if(token.type == '(') {
2412                         parser_print_prefix_pos(token.source_position);
2413                         fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2414                                 ref->symbol->string);
2415
2416                         declaration = create_implicit_function(ref->symbol,
2417                                                                source_position);
2418                 } else
2419 #endif
2420                 {
2421                         parser_print_error_prefix();
2422                         fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2423                         return (expression_t*) ref;
2424                 }
2425         }
2426
2427         ref->declaration         = declaration;
2428         ref->expression.datatype = declaration->type;
2429
2430         return (expression_t*) ref;
2431 }
2432
2433 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2434 {
2435         (void) expression;
2436         (void) dest_type;
2437         /* TODO check if explicit cast is allowed and issue warnings/errors */
2438 }
2439
2440 static expression_t *parse_cast(void)
2441 {
2442         unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2443
2444         cast->expression.type            = EXPR_UNARY;
2445         cast->type                       = UNEXPR_CAST;
2446         cast->expression.source_position = token.source_position;
2447
2448         type_t *type  = parse_typename();
2449
2450         expect(')');
2451         expression_t *value = parse_sub_expression(20);
2452
2453         check_cast_allowed(value, type);
2454
2455         cast->expression.datatype = type;
2456         cast->value               = value;
2457
2458         return (expression_t*) cast;
2459 }
2460
2461 static expression_t *parse_statement_expression(void)
2462 {
2463         statement_expression_t *expression
2464                 = allocate_ast_zero(sizeof(expression[0]));
2465         expression->expression.type = EXPR_STATEMENT;
2466
2467         statement_t *statement = parse_compound_statement();
2468         expression->statement  = statement;
2469         if(statement == NULL) {
2470                 expect(')');
2471                 return NULL;
2472         }
2473
2474         assert(statement->type == STATEMENT_COMPOUND);
2475         compound_statement_t *compound_statement
2476                 = (compound_statement_t*) statement;
2477
2478         /* find last statement and use it's type */
2479         const statement_t *last_statement = NULL;
2480         const statement_t *iter           = compound_statement->statements;
2481         for( ; iter != NULL; iter = iter->next) {
2482                 last_statement = iter;
2483         }
2484
2485         if(last_statement->type == STATEMENT_EXPRESSION) {
2486                 const expression_statement_t *expression_statement =
2487                         (const expression_statement_t*) last_statement;
2488                 expression->expression.datatype
2489                         = expression_statement->expression->datatype;
2490         } else {
2491                 expression->expression.datatype = type_void;
2492         }
2493
2494         expect(')');
2495
2496         return (expression_t*) expression;
2497 }
2498
2499 static expression_t *parse_brace_expression(void)
2500 {
2501         eat('(');
2502
2503         switch(token.type) {
2504         case '{':
2505                 /* gcc extension: a stement expression */
2506                 return parse_statement_expression();
2507
2508         TYPE_QUALIFIERS
2509         TYPE_SPECIFIERS
2510                 return parse_cast();
2511         case T_IDENTIFIER:
2512                 if(is_typedef_symbol(token.v.symbol)) {
2513                         return parse_cast();
2514                 }
2515         }
2516
2517         expression_t *result = parse_expression();
2518         expect(')');
2519
2520         return result;
2521 }
2522
2523 static expression_t *parse_function_keyword(void)
2524 {
2525         next_token();
2526         /* TODO */
2527
2528         if (current_function == NULL) {
2529                 parse_error("'__func__' used outside of a function");
2530         }
2531
2532         string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2533         expression->expression.type     = EXPR_FUNCTION;
2534         expression->expression.datatype = type_string;
2535         expression->value               = "TODO: FUNCTION";
2536
2537         return (expression_t*) expression;
2538 }
2539
2540 static expression_t *parse_pretty_function_keyword(void)
2541 {
2542         eat(T___PRETTY_FUNCTION__);
2543         /* TODO */
2544
2545         string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2546         expression->expression.type     = EXPR_PRETTY_FUNCTION;
2547         expression->expression.datatype = type_string;
2548         expression->value               = "TODO: PRETTY FUNCTION";
2549
2550         return (expression_t*) expression;
2551 }
2552
2553 static designator_t *parse_designator(void)
2554 {
2555         designator_t *result = allocate_ast_zero(sizeof(result[0]));
2556
2557         if(token.type != T_IDENTIFIER) {
2558                 parse_error_expected("while parsing member designator",
2559                                      T_IDENTIFIER, 0);
2560                 eat_brace();
2561                 return NULL;
2562         }
2563         result->symbol = token.v.symbol;
2564         next_token();
2565
2566         designator_t *last_designator = result;
2567         while(true) {
2568                 if(token.type == '.') {
2569                         next_token();
2570                         if(token.type != T_IDENTIFIER) {
2571                                 parse_error_expected("while parsing member designator",
2572                                                      T_IDENTIFIER, 0);
2573                                 eat_brace();
2574                                 return NULL;
2575                         }
2576                         designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2577                         designator->symbol       = token.v.symbol;
2578                         next_token();
2579
2580                         last_designator->next = designator;
2581                         last_designator       = designator;
2582                         continue;
2583                 }
2584                 if(token.type == '[') {
2585                         next_token();
2586                         designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2587                         designator->array_access = parse_expression();
2588                         if(designator->array_access == NULL) {
2589                                 eat_brace();
2590                                 return NULL;
2591                         }
2592                         expect(']');
2593
2594                         last_designator->next = designator;
2595                         last_designator       = designator;
2596                         continue;
2597                 }
2598                 break;
2599         }
2600
2601         return result;
2602 }
2603
2604 static expression_t *parse_offsetof(void)
2605 {
2606         eat(T___builtin_offsetof);
2607
2608         offsetof_expression_t *expression
2609                 = allocate_ast_zero(sizeof(expression[0]));
2610         expression->expression.type     = EXPR_OFFSETOF;
2611         expression->expression.datatype = type_size_t;
2612
2613         expect('(');
2614         expression->type = parse_typename();
2615         expect(',');
2616         expression->designator = parse_designator();
2617         expect(')');
2618
2619         return (expression_t*) expression;
2620 }
2621
2622 static expression_t *parse_va_arg(void)
2623 {
2624         eat(T___builtin_va_arg);
2625
2626         va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2627         expression->expression.type     = EXPR_VA_ARG;
2628
2629         expect('(');
2630         expression->arg = parse_assignment_expression();
2631         expect(',');
2632         expression->expression.datatype = parse_typename();
2633         expect(')');
2634
2635         return (expression_t*) expression;
2636 }
2637
2638 static expression_t *parse_builtin_symbol(void)
2639 {
2640         builtin_symbol_expression_t *expression
2641                 = allocate_ast_zero(sizeof(expression[0]));
2642         expression->expression.type = EXPR_BUILTIN_SYMBOL;
2643
2644         /* TODO: set datatype */
2645
2646         expression->symbol = token.v.symbol;
2647
2648         next_token();
2649
2650         return (expression_t*) expression;
2651 }
2652
2653 static expression_t *parse_primary_expression(void)
2654 {
2655         switch(token.type) {
2656         case T_INTEGER:
2657                 return parse_int_const();
2658         case T_FLOATINGPOINT:
2659                 return parse_float_const();
2660         case T_STRING_LITERAL:
2661                 return parse_string_const();
2662         case T_IDENTIFIER:
2663                 return parse_reference();
2664         case T___FUNCTION__:
2665         case T___func__:
2666                 return parse_function_keyword();
2667         case T___PRETTY_FUNCTION__:
2668                 return parse_pretty_function_keyword();
2669         case T___builtin_offsetof:
2670                 return parse_offsetof();
2671         case T___builtin_va_arg:
2672                 return parse_va_arg();
2673         case T___builtin_expect:
2674         case T___builtin_va_start:
2675         case T___builtin_va_end:
2676                 return parse_builtin_symbol();
2677
2678         case '(':
2679                 return parse_brace_expression();
2680         }
2681
2682         parser_print_error_prefix();
2683         fprintf(stderr, "unexpected token ");
2684         print_token(stderr, &token);
2685         fprintf(stderr, "\n");
2686         eat_statement();
2687
2688         return make_invalid_expression();
2689 }
2690
2691 static expression_t *parse_array_expression(unsigned precedence,
2692                                             expression_t *array_ref)
2693 {
2694         (void) precedence;
2695
2696         eat('[');
2697
2698         array_access_expression_t *array_access
2699                 = allocate_ast_zero(sizeof(array_access[0]));
2700
2701         array_access->expression.type     = EXPR_ARRAY_ACCESS;
2702         array_access->array_ref           = array_ref;
2703         array_access->index               = parse_expression();
2704
2705         type_t *type = array_ref->datatype;
2706         if(type != NULL) {
2707                 if(type->type == TYPE_POINTER) {
2708                         pointer_type_t *pointer           = (pointer_type_t*) type;
2709                         array_access->expression.datatype = pointer->points_to;
2710                 } else if(type->type == TYPE_ARRAY) {
2711                         array_type_t *array_type          = (array_type_t*) type;
2712                         array_access->expression.datatype = array_type->element_type;
2713                 } else {
2714                         parser_print_error_prefix();
2715                         fprintf(stderr, "array access on object with non-pointer type ");
2716                         print_type_quoted(type);
2717                         fprintf(stderr, "\n");
2718                 }
2719         }
2720
2721         if(token.type != ']') {
2722                 parse_error_expected("Problem while parsing array access", ']', 0);
2723                 return (expression_t*) array_access;
2724         }
2725         next_token();
2726
2727         return (expression_t*) array_access;
2728 }
2729
2730 static bool is_declaration_specifier(const token_t *token,
2731                                      bool only_type_specifiers)
2732 {
2733         switch(token->type) {
2734                 TYPE_SPECIFIERS
2735                         return 1;
2736                 case T_IDENTIFIER:
2737                         return is_typedef_symbol(token->v.symbol);
2738                 STORAGE_CLASSES
2739                 TYPE_QUALIFIERS
2740                         if(only_type_specifiers)
2741                                 return 0;
2742                         return 1;
2743
2744                 default:
2745                         return 0;
2746         }
2747 }
2748
2749 static expression_t *parse_sizeof(unsigned precedence)
2750 {
2751         eat(T_sizeof);
2752
2753         sizeof_expression_t *sizeof_expression
2754                 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2755         sizeof_expression->expression.type     = EXPR_SIZEOF;
2756         sizeof_expression->expression.datatype = type_size_t;
2757
2758         if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2759                 next_token();
2760                 sizeof_expression->type = parse_typename();
2761                 expect(')');
2762         } else {
2763                 expression_t *expression           = parse_sub_expression(precedence);
2764                 sizeof_expression->type            = expression->datatype;
2765                 sizeof_expression->size_expression = expression;
2766         }
2767
2768         return (expression_t*) sizeof_expression;
2769 }
2770
2771 static expression_t *parse_select_expression(unsigned precedence,
2772                                              expression_t *compound)
2773 {
2774         (void) precedence;
2775         assert(token.type == '.' || token.type == T_MINUSGREATER);
2776
2777         bool is_pointer = (token.type == T_MINUSGREATER);
2778         next_token();
2779
2780         select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2781
2782         select->expression.type = EXPR_SELECT;
2783         select->compound        = compound;
2784
2785         if(token.type != T_IDENTIFIER) {
2786                 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
2787                 return (expression_t*) select;
2788         }
2789         symbol_t *symbol = token.v.symbol;
2790         select->symbol   = symbol;
2791         next_token();
2792
2793         type_t *type = compound->datatype;
2794         if(type == NULL)
2795                 return make_invalid_expression();
2796
2797         type_t *type_left = type;
2798         if(is_pointer) {
2799                 if(type->type != TYPE_POINTER) {
2800                         parser_print_error_prefix();
2801                         fprintf(stderr, "left hand side of '->' is not a pointer, but ");
2802                         print_type_quoted(type);
2803                         fputc('\n', stderr);
2804                         return make_invalid_expression();
2805                 }
2806                 pointer_type_t *pointer_type = (pointer_type_t*) type;
2807                 type_left                    = pointer_type->points_to;
2808         }
2809         type_left = skip_typeref(type_left);
2810
2811         if(type_left->type != TYPE_COMPOUND_STRUCT
2812                         && type_left->type != TYPE_COMPOUND_UNION) {
2813                 parser_print_error_prefix();
2814                 fprintf(stderr, "request for member '%s' in something not a struct or "
2815                         "union, but ", symbol->string);
2816                 print_type_quoted(type_left);
2817                 fputc('\n', stderr);
2818                 return make_invalid_expression();
2819         }
2820
2821         compound_type_t *compound_type = (compound_type_t*) type_left;
2822         declaration_t   *declaration   = compound_type->declaration;
2823
2824         if(!declaration->init.is_defined) {
2825                 parser_print_error_prefix();
2826                 fprintf(stderr, "request for member '%s' of incomplete type ",
2827                         symbol->string);
2828                 print_type_quoted(type_left);
2829                 fputc('\n', stderr);
2830                 return make_invalid_expression();
2831         }
2832
2833         declaration_t *iter = declaration->context.declarations;
2834         for( ; iter != NULL; iter = iter->next) {
2835                 if(iter->symbol == symbol) {
2836                         break;
2837                 }
2838         }
2839         if(iter == NULL) {
2840                 parser_print_error_prefix();
2841                 print_type_quoted(type_left);
2842                 fprintf(stderr, " has no member named '%s'\n", symbol->string);
2843                 return make_invalid_expression();
2844         }
2845
2846         select->compound_entry      = iter;
2847         select->expression.datatype = iter->type;
2848         return (expression_t*) select;
2849 }
2850
2851 static expression_t *parse_call_expression(unsigned precedence,
2852                                            expression_t *expression)
2853 {
2854         (void) precedence;
2855         call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2856         call->expression.type   = EXPR_CALL;
2857         call->function          = expression;
2858
2859         function_type_t *function_type;
2860         type_t          *type = expression->datatype;
2861         if (type->type == TYPE_FUNCTION) {
2862                 function_type             = (function_type_t*) type;
2863                 call->expression.datatype = function_type->result_type;
2864         } else if (type->type == TYPE_POINTER &&
2865                    ((pointer_type_t*)type)->points_to->type == TYPE_FUNCTION) {
2866                 pointer_type_t *const ptr_type = (pointer_type_t*)type;
2867                 function_type                  = (function_type_t*)ptr_type->points_to;
2868                 call->expression.datatype      = function_type->result_type;
2869         } else {
2870                 parser_print_error_prefix();
2871                 fputs("called object '", stderr);
2872                 print_expression(expression);
2873                 fputs("' (type ", stderr);
2874                 print_type_quoted(type);
2875                 fputs(") is not a function\n", stderr);
2876
2877                 function_type             = NULL;
2878                 call->expression.datatype = NULL;
2879         }
2880
2881         /* parse arguments */
2882         eat('(');
2883
2884         if(token.type != ')') {
2885                 call_argument_t *last_argument = NULL;
2886
2887                 while(true) {
2888                         call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2889
2890                         argument->expression = parse_assignment_expression();
2891                         if(last_argument == NULL) {
2892                                 call->arguments = argument;
2893                         } else {
2894                                 last_argument->next = argument;
2895                         }
2896                         last_argument = argument;
2897
2898                         if(token.type != ',')
2899                                 break;
2900                         next_token();
2901                 }
2902         }
2903         expect(')');
2904
2905         if(function_type != NULL) {
2906                 function_parameter_t *parameter = function_type->parameters;
2907                 call_argument_t      *argument  = call->arguments;
2908                 for( ; parameter != NULL && argument != NULL;
2909                                 parameter = parameter->next, argument = argument->next) {
2910                         type_t *expected_type = parameter->type;
2911                         /* TODO report context in error messages */
2912                         argument->expression = create_implicit_cast(argument->expression,
2913                                                                     expected_type);
2914                 }
2915                 /* too few parameters */
2916                 if(parameter != NULL) {
2917                         parser_print_error_prefix();
2918                         fprintf(stderr, "too few arguments to function '");
2919                         print_expression(expression);
2920                         fprintf(stderr, "'\n");
2921                 } else if(argument != NULL) {
2922                         /* too many parameters */
2923                         if(!function_type->variadic
2924                                         && !function_type->unspecified_parameters) {
2925                                 parser_print_error_prefix();
2926                                 fprintf(stderr, "too many arguments to function '");
2927                                 print_expression(expression);
2928                                 fprintf(stderr, "'\n");
2929                         } else {
2930                                 /* do default promotion */
2931                                 for( ; argument != NULL; argument = argument->next) {
2932                                         type_t *type = argument->expression->datatype;
2933
2934                                         if(type == NULL)
2935                                                 continue;
2936
2937                                         if(is_type_integer(type)) {
2938                                                 type = promote_integer(type);
2939                                         } else if(type == type_float) {
2940                                                 type = type_double;
2941                                         }
2942                                         argument->expression
2943                                                 = create_implicit_cast(argument->expression, type);
2944                                 }
2945                         }
2946                 }
2947         }
2948
2949         return (expression_t*) call;
2950 }
2951
2952 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
2953
2954 static expression_t *parse_conditional_expression(unsigned precedence,
2955                                                   expression_t *expression)
2956 {
2957         eat('?');
2958
2959         conditional_expression_t *conditional
2960                 = allocate_ast_zero(sizeof(conditional[0]));
2961         conditional->expression.type = EXPR_CONDITIONAL;
2962         conditional->condition = expression;
2963
2964         /* 6.5.15.2 */
2965         type_t *condition_type_orig = conditional->condition->datatype;
2966         type_t *condition_type      = skip_typeref(condition_type_orig);
2967         if(condition_type != NULL && !is_type_scalar(condition_type)) {
2968                 type_error("expected a scalar type", expression->source_position,
2969                            condition_type_orig);
2970         }
2971
2972         expression_t *const t_expr = parse_expression();
2973         conditional->true_expression = t_expr;
2974         expect(':');
2975         expression_t *const f_expr = parse_sub_expression(precedence);
2976         conditional->false_expression = f_expr;
2977
2978         type_t *const true_type  = t_expr->datatype;
2979         if(true_type == NULL)
2980                 return (expression_t*) conditional;
2981         type_t *const false_type = f_expr->datatype;
2982         if(false_type == NULL)
2983                 return (expression_t*) conditional;
2984
2985         type_t *const skipped_true_type  = skip_typeref(true_type);
2986         type_t *const skipped_false_type = skip_typeref(false_type);
2987
2988         /* 6.5.15.3 */
2989         if (skipped_true_type == skipped_false_type) {
2990                 conditional->expression.datatype = skipped_true_type;
2991         } else if (is_type_arithmetic(skipped_true_type) &&
2992                    is_type_arithmetic(skipped_false_type)) {
2993                 type_t *const result = semantic_arithmetic(skipped_true_type,
2994                                                            skipped_false_type);
2995                 conditional->true_expression  = create_implicit_cast(t_expr, result);
2996                 conditional->false_expression = create_implicit_cast(f_expr, result);
2997                 conditional->expression.datatype = result;
2998         } else if (skipped_true_type->type == TYPE_POINTER &&
2999                    skipped_false_type->type == TYPE_POINTER &&
3000                           true /* TODO compatible points_to types */) {
3001                 /* TODO */
3002         } else if(/* (is_null_ptr_const(skipped_true_type) &&
3003                       skipped_false_type->type == TYPE_POINTER)
3004                || (is_null_ptr_const(skipped_false_type) &&
3005                    skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3006                 /* TODO */
3007         } else if(/* 1 is pointer to object type, other is void* */ false) {
3008                 /* TODO */
3009         } else {
3010                 type_error_incompatible("while parsing conditional",
3011                                         expression->source_position, true_type,
3012                                         skipped_false_type);
3013         }
3014
3015         return (expression_t*) conditional;
3016 }
3017
3018 static expression_t *parse_extension(unsigned precedence)
3019 {
3020         eat(T___extension__);
3021
3022         /* TODO enable extensions */
3023
3024         return parse_sub_expression(precedence);
3025 }
3026
3027 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3028 {
3029         eat(T___builtin_classify_type);
3030
3031         classify_type_expression_t *const classify_type_expr =
3032                 allocate_ast_zero(sizeof(classify_type_expr[0]));
3033         classify_type_expr->expression.type     = EXPR_CLASSIFY_TYPE;
3034         classify_type_expr->expression.datatype = type_int;
3035
3036         expect('(');
3037         expression_t *const expression = parse_sub_expression(precedence);
3038         expect(')');
3039         classify_type_expr->type_expression = expression;
3040
3041         return (expression_t*)classify_type_expr;
3042 }
3043
3044 static void semantic_incdec(unary_expression_t *expression)
3045 {
3046         type_t *orig_type = expression->value->datatype;
3047         if(orig_type == NULL)
3048                 return;
3049
3050         type_t *type = skip_typeref(orig_type);
3051         if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3052                 /* TODO: improve error message */
3053                 parser_print_error_prefix();
3054                 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3055                 return;
3056         }
3057
3058         expression->expression.datatype = orig_type;
3059 }
3060
3061 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3062 {
3063         type_t *orig_type = expression->value->datatype;
3064         if(orig_type == NULL)
3065                 return;
3066
3067         type_t *type = skip_typeref(orig_type);
3068         if(!is_type_arithmetic(type)) {
3069                 /* TODO: improve error message */
3070                 parser_print_error_prefix();
3071                 fprintf(stderr, "operation needs an arithmetic type\n");
3072                 return;
3073         }
3074
3075         expression->expression.datatype = orig_type;
3076 }
3077
3078 static void semantic_unexpr_scalar(unary_expression_t *expression)
3079 {
3080         type_t *orig_type = expression->value->datatype;
3081         if(orig_type == NULL)
3082                 return;
3083
3084         type_t *type = skip_typeref(orig_type);
3085         if (!is_type_scalar(type)) {
3086                 parse_error("operand of ! must be of scalar type\n");
3087                 return;
3088         }
3089
3090         expression->expression.datatype = orig_type;
3091 }
3092
3093 static void semantic_unexpr_integer(unary_expression_t *expression)
3094 {
3095         type_t *orig_type = expression->value->datatype;
3096         if(orig_type == NULL)
3097                 return;
3098
3099         type_t *type = skip_typeref(orig_type);
3100         if (!is_type_integer(type)) {
3101                 parse_error("operand of ~ must be of integer type\n");
3102                 return;
3103         }
3104
3105         expression->expression.datatype = orig_type;
3106 }
3107
3108 static void semantic_dereference(unary_expression_t *expression)
3109 {
3110         type_t *orig_type = expression->value->datatype;
3111         if(orig_type == NULL)
3112                 return;
3113
3114         type_t *type = skip_typeref(orig_type);
3115         switch (type->type) {
3116                 case TYPE_ARRAY: {
3117                         array_type_t *const array_type  = (array_type_t*)type;
3118                         expression->expression.datatype = array_type->element_type;
3119                         break;
3120                 }
3121
3122                 case TYPE_POINTER: {
3123                         pointer_type_t *pointer_type    = (pointer_type_t*)type;
3124                         expression->expression.datatype = pointer_type->points_to;
3125                         break;
3126                 }
3127
3128                 default:
3129                         parser_print_error_prefix();
3130                         fputs("'Unary *' needs pointer or arrray type, but type ", stderr);
3131                         print_type_quoted(orig_type);
3132                         fputs(" given.\n", stderr);
3133                         return;
3134         }
3135 }
3136
3137 static void semantic_take_addr(unary_expression_t *expression)
3138 {
3139         type_t *orig_type = expression->value->datatype;
3140         if(orig_type == NULL)
3141                 return;
3142
3143         expression_t *value = expression->value;
3144         if(value->type == EXPR_REFERENCE) {
3145                 reference_expression_t *reference   = (reference_expression_t*) value;
3146                 declaration_t          *declaration = reference->declaration;
3147                 if(declaration != NULL) {
3148                         declaration->address_taken = 1;
3149                 }
3150         }
3151
3152         expression->expression.datatype = make_pointer_type(orig_type, 0);
3153 }
3154
3155 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc)   \
3156 static expression_t *parse_##unexpression_type(unsigned precedence)            \
3157 {                                                                              \
3158         eat(token_type);                                                           \
3159                                                                                \
3160         unary_expression_t *unary_expression                                       \
3161                 = allocate_ast_zero(sizeof(unary_expression[0]));                      \
3162         unary_expression->expression.type     = EXPR_UNARY;                        \
3163         unary_expression->type                = unexpression_type;                 \
3164         unary_expression->value               = parse_sub_expression(precedence);  \
3165                                                                                    \
3166         sfunc(unary_expression);                                                   \
3167                                                                                \
3168         return (expression_t*) unary_expression;                                   \
3169 }
3170
3171 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3172 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS,   semantic_unexpr_arithmetic)
3173 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT,    semantic_unexpr_scalar)
3174 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3175 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3176 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3177                                semantic_unexpr_integer)
3178 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_PREFIX_INCREMENT,
3179                                semantic_incdec)
3180 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3181                                semantic_incdec)
3182
3183 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3184                                                sfunc)                         \
3185 static expression_t *parse_##unexpression_type(unsigned precedence,           \
3186                                                expression_t *left)            \
3187 {                                                                             \
3188         (void) precedence;                                                        \
3189         eat(token_type);                                                          \
3190                                                                               \
3191         unary_expression_t *unary_expression                                      \
3192                 = allocate_ast_zero(sizeof(unary_expression[0]));                     \
3193         unary_expression->expression.type     = EXPR_UNARY;                       \
3194         unary_expression->type                = unexpression_type;                \
3195         unary_expression->value               = left;                             \
3196                                                                                   \
3197         sfunc(unary_expression);                                                  \
3198                                                                               \
3199         return (expression_t*) unary_expression;                                  \
3200 }
3201
3202 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_POSTFIX_INCREMENT,
3203                                        semantic_incdec)
3204 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3205                                        semantic_incdec)
3206
3207 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3208 {
3209         /* TODO: handle complex + imaginary types */
3210
3211         /* Â§ 6.3.1.8 Usual arithmetic conversions */
3212         if(type_left == type_long_double || type_right == type_long_double) {
3213                 return type_long_double;
3214         } else if(type_left == type_double || type_right == type_double) {
3215                 return type_double;
3216         } else if(type_left == type_float || type_right == type_float) {
3217                 return type_float;
3218         }
3219
3220         type_right = promote_integer(type_right);
3221         type_left  = promote_integer(type_left);
3222
3223         if(type_left == type_right)
3224                 return type_left;
3225
3226         bool signed_left  = is_type_signed(type_left);
3227         bool signed_right = is_type_signed(type_right);
3228         if(get_rank(type_left) < get_rank(type_right)) {
3229                 if(signed_left == signed_right || !signed_right) {
3230                         return type_right;
3231                 } else {
3232                         return type_left;
3233                 }
3234         } else {
3235                 if(signed_left == signed_right || !signed_left) {
3236                         return type_left;
3237                 } else {
3238                         return type_right;
3239                 }
3240         }
3241 }
3242
3243 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3244 {
3245         expression_t *left       = expression->left;
3246         expression_t *right      = expression->right;
3247         type_t       *orig_type_left  = left->datatype;
3248         type_t       *orig_type_right = right->datatype;
3249
3250         if(orig_type_left == NULL || orig_type_right == NULL)
3251                 return;
3252
3253         type_t *type_left  = skip_typeref(orig_type_left);
3254         type_t *type_right = skip_typeref(orig_type_right);
3255
3256         if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3257                 /* TODO: improve error message */
3258                 parser_print_error_prefix();
3259                 fprintf(stderr, "operation needs arithmetic types\n");
3260                 return;
3261         }
3262
3263         type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3264         expression->left  = create_implicit_cast(left, arithmetic_type);
3265         expression->right = create_implicit_cast(right, arithmetic_type);
3266         expression->expression.datatype = arithmetic_type;
3267 }
3268
3269 static void semantic_shift_op(binary_expression_t *expression)
3270 {
3271         expression_t *left       = expression->left;
3272         expression_t *right      = expression->right;
3273         type_t       *orig_type_left  = left->datatype;
3274         type_t       *orig_type_right = right->datatype;
3275
3276         if(orig_type_left == NULL || orig_type_right == NULL)
3277                 return;
3278
3279         type_t *type_left  = skip_typeref(orig_type_left);
3280         type_t *type_right = skip_typeref(orig_type_right);
3281
3282         if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3283                 /* TODO: improve error message */
3284                 parser_print_error_prefix();
3285                 fprintf(stderr, "operation needs integer types\n");
3286                 return;
3287         }
3288
3289         type_left  = promote_integer(type_left);
3290         type_right = promote_integer(type_right);
3291
3292         expression->left  = create_implicit_cast(left, type_left);
3293         expression->right = create_implicit_cast(right, type_right);
3294         expression->expression.datatype = type_left;
3295 }
3296
3297 static void semantic_add(binary_expression_t *expression)
3298 {
3299         expression_t *left            = expression->left;
3300         expression_t *right           = expression->right;
3301         type_t       *orig_type_left  = left->datatype;
3302         type_t       *orig_type_right = right->datatype;
3303
3304         if(orig_type_left == NULL || orig_type_right == NULL)
3305                 return;
3306
3307         type_t *type_left  = skip_typeref(orig_type_left);
3308         type_t *type_right = skip_typeref(orig_type_right);
3309
3310         /* Â§ 5.6.5 */
3311         if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3312                 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3313                 expression->left  = create_implicit_cast(left, arithmetic_type);
3314                 expression->right = create_implicit_cast(right, arithmetic_type);
3315                 expression->expression.datatype = arithmetic_type;
3316                 return;
3317         } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3318                 expression->expression.datatype = type_left;
3319         } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
3320                 expression->expression.datatype = type_right;
3321         } else if (type_left->type == TYPE_ARRAY && is_type_integer(type_right)) {
3322                 const array_type_t *const arr_type = (const array_type_t*)type_left;
3323                 expression->expression.datatype =
3324                   make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3325         } else if (type_right->type == TYPE_ARRAY && is_type_integer(type_left)) {
3326                 const array_type_t *const arr_type = (const array_type_t*)type_right;
3327                 expression->expression.datatype =
3328                         make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3329         } else {
3330                 parser_print_error_prefix();
3331                 fprintf(stderr, "invalid operands to binary + (");
3332                 print_type_quoted(orig_type_left);
3333                 fprintf(stderr, ", ");
3334                 print_type_quoted(orig_type_right);
3335                 fprintf(stderr, ")\n");
3336         }
3337 }
3338
3339 static void semantic_sub(binary_expression_t *expression)
3340 {
3341         expression_t *left            = expression->left;
3342         expression_t *right           = expression->right;
3343         type_t       *orig_type_left  = left->datatype;
3344         type_t       *orig_type_right = right->datatype;
3345
3346         if(orig_type_left == NULL || orig_type_right == NULL)
3347                 return;
3348
3349         type_t       *type_left       = skip_typeref(orig_type_left);
3350         type_t       *type_right      = skip_typeref(orig_type_right);
3351
3352         /* Â§ 5.6.5 */
3353         if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3354                 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3355                 expression->left  = create_implicit_cast(left, arithmetic_type);
3356                 expression->right = create_implicit_cast(right, arithmetic_type);
3357                 expression->expression.datatype = arithmetic_type;
3358                 return;
3359         } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3360                 expression->expression.datatype = type_left;
3361         } else if(type_left->type == TYPE_POINTER &&
3362                         type_right->type == TYPE_POINTER) {
3363                 if(!pointers_compatible(type_left, type_right)) {
3364                         parser_print_error_prefix();
3365                         fprintf(stderr, "pointers to incompatible objects to binary - (");
3366                         print_type_quoted(orig_type_left);
3367                         fprintf(stderr, ", ");
3368                         print_type_quoted(orig_type_right);
3369                         fprintf(stderr, ")\n");
3370                 } else {
3371                         expression->expression.datatype = type_ptrdiff_t;
3372                 }
3373         } else {
3374                 parser_print_error_prefix();
3375                 fprintf(stderr, "invalid operands to binary - (");
3376                 print_type_quoted(orig_type_left);
3377                 fprintf(stderr, ", ");
3378                 print_type_quoted(orig_type_right);
3379                 fprintf(stderr, ")\n");
3380         }
3381 }
3382
3383 static void semantic_comparison(binary_expression_t *expression)
3384 {
3385         expression_t *left            = expression->left;
3386         expression_t *right           = expression->right;
3387         type_t       *orig_type_left  = left->datatype;
3388         type_t       *orig_type_right = right->datatype;
3389
3390         if(orig_type_left == NULL || orig_type_right == NULL)
3391                 return;
3392
3393         type_t *type_left  = skip_typeref(orig_type_left);
3394         type_t *type_right = skip_typeref(orig_type_right);
3395
3396         /* TODO non-arithmetic types */
3397         if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3398                 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3399                 expression->left  = create_implicit_cast(left, arithmetic_type);
3400                 expression->right = create_implicit_cast(right, arithmetic_type);
3401                 expression->expression.datatype = arithmetic_type;
3402         } else if (type_left->type  == TYPE_POINTER &&
3403                    type_right->type == TYPE_POINTER) {
3404                 /* TODO check compatibility */
3405         } else if (type_left->type == TYPE_POINTER) {
3406                 expression->right = create_implicit_cast(right, type_left);
3407         } else if (type_right->type == TYPE_POINTER) {
3408                 expression->left = create_implicit_cast(left, type_right);
3409         } else {
3410                 type_error_incompatible("invalid operands in comparison",
3411                                         expression->expression.source_position,
3412                                         type_left, type_right);
3413         }
3414         expression->expression.datatype = type_int;
3415 }
3416
3417 static void semantic_arithmetic_assign(binary_expression_t *expression)
3418 {
3419         expression_t *left            = expression->left;
3420         expression_t *right           = expression->right;
3421         type_t       *orig_type_left  = left->datatype;
3422         type_t       *orig_type_right = right->datatype;
3423
3424         if(orig_type_left == NULL || orig_type_right == NULL)
3425                 return;
3426
3427         type_t *type_left  = skip_typeref(orig_type_left);
3428         type_t *type_right = skip_typeref(orig_type_right);
3429
3430         if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3431                 /* TODO: improve error message */
3432                 parser_print_error_prefix();
3433                 fprintf(stderr, "operation needs arithmetic types\n");
3434                 return;
3435         }
3436
3437         /* combined instructions are tricky. We can't create an implicit cast on
3438          * the left side, because we need the uncasted form for the store.
3439          * The ast2firm pass has to know that left_type must be right_type
3440          * for the arithmeitc operation and create a cast by itself */
3441         type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3442         expression->right       = create_implicit_cast(right, arithmetic_type);
3443         expression->expression.datatype = type_left;
3444 }
3445
3446 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3447 {
3448         expression_t *left            = expression->left;
3449         expression_t *right           = expression->right;
3450         type_t       *orig_type_left  = left->datatype;
3451         type_t       *orig_type_right = right->datatype;
3452
3453         if(orig_type_left == NULL || orig_type_right == NULL)
3454                 return;
3455
3456         type_t *type_left  = skip_typeref(orig_type_left);
3457         type_t *type_right = skip_typeref(orig_type_right);
3458
3459         if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3460                 /* combined instructions are tricky. We can't create an implicit cast on
3461                  * the left side, because we need the uncasted form for the store.
3462                  * The ast2firm pass has to know that left_type must be right_type
3463                  * for the arithmeitc operation and create a cast by itself */
3464                 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3465                 expression->right = create_implicit_cast(right, arithmetic_type);
3466                 expression->expression.datatype = type_left;
3467         } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3468                 expression->expression.datatype = type_left;
3469         } else {
3470                 parser_print_error_prefix();
3471                 fputs("Incompatible types ", stderr);
3472                 print_type_quoted(orig_type_left);
3473                 fputs(" and ", stderr);
3474                 print_type_quoted(orig_type_right);
3475                 fputs(" in assignment\n", stderr);
3476                 return;
3477         }
3478 }
3479
3480 static void semantic_logical_op(binary_expression_t *expression)
3481 {
3482         expression_t *left            = expression->left;
3483         expression_t *right           = expression->right;
3484         type_t       *orig_type_left  = left->datatype;
3485         type_t       *orig_type_right = right->datatype;
3486
3487         if(orig_type_left == NULL || orig_type_right == NULL)
3488                 return;
3489
3490         type_t *type_left  = skip_typeref(orig_type_left);
3491         type_t *type_right = skip_typeref(orig_type_right);
3492
3493         if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3494                 /* TODO: improve error message */
3495                 parser_print_error_prefix();
3496                 fprintf(stderr, "operation needs scalar types\n");
3497                 return;
3498         }
3499
3500         expression->expression.datatype = type_int;
3501 }
3502
3503 static void semantic_binexpr_assign(binary_expression_t *expression)
3504 {
3505         expression_t *left       = expression->left;
3506         type_t       *type_left  = left->datatype;
3507
3508         if (type_left->type == TYPE_ARRAY) {
3509                 parse_error("Cannot assign to arrays.");
3510         } else if (type_left != NULL) {
3511                 semantic_assign(type_left, &expression->right, "assignment");
3512         }
3513
3514         expression->expression.datatype = type_left;
3515 }
3516
3517 static void semantic_comma(binary_expression_t *expression)
3518 {
3519         expression->expression.datatype = expression->right->datatype;
3520 }
3521
3522 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3523 static expression_t *parse_##binexpression_type(unsigned precedence,     \
3524                                                 expression_t *left)      \
3525 {                                                                        \
3526         eat(token_type);                                                     \
3527                                                                          \
3528         expression_t *right = parse_sub_expression(precedence + lr);         \
3529                                                                          \
3530         binary_expression_t *binexpr                                         \
3531                 = allocate_ast_zero(sizeof(binexpr[0]));                         \
3532         binexpr->expression.type     = EXPR_BINARY;                          \
3533         binexpr->type                = binexpression_type;                   \
3534         binexpr->left                = left;                                 \
3535         binexpr->right               = right;                                \
3536         sfunc(binexpr);                                                      \
3537                                                                          \
3538         return (expression_t*) binexpr;                                      \
3539 }
3540
3541 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA,          semantic_comma, 1)
3542 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL,            semantic_binexpr_arithmetic, 1)
3543 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV,            semantic_binexpr_arithmetic, 1)
3544 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD,            semantic_binexpr_arithmetic, 1)
3545 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD,            semantic_add, 1)
3546 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB,            semantic_sub, 1)
3547 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS,           semantic_comparison, 1)
3548 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER,        semantic_comparison, 1)
3549 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN,         semantic_binexpr_assign, 0)
3550 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3551 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3552                       semantic_comparison, 1)
3553 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3554 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3555                       semantic_comparison, 1)
3556 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND,    semantic_binexpr_arithmetic, 1)
3557 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR,     semantic_binexpr_arithmetic, 1)
3558 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR,    semantic_binexpr_arithmetic, 1)
3559 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND,  semantic_logical_op, 1)
3560 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3561 /* TODO shift has a bit special semantic */
3562 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3563                       semantic_shift_op, 1)
3564 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3565                       semantic_shift_op, 1)
3566 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3567                       semantic_arithmetic_addsubb_assign, 0)
3568 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3569                       semantic_arithmetic_addsubb_assign, 0)
3570 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3571                       semantic_arithmetic_assign, 0)
3572 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3573                       semantic_arithmetic_assign, 0)
3574 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3575                       semantic_arithmetic_assign, 0)
3576 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3577                       semantic_arithmetic_assign, 0)
3578 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3579                       semantic_arithmetic_assign, 0)
3580 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3581                       semantic_arithmetic_assign, 0)
3582 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3583                       semantic_arithmetic_assign, 0)
3584 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3585                       semantic_arithmetic_assign, 0)
3586
3587 static expression_t *parse_sub_expression(unsigned precedence)
3588 {
3589         if(token.type < 0) {
3590                 return expected_expression_error();
3591         }
3592
3593         expression_parser_function_t *parser
3594                 = &expression_parsers[token.type];
3595         source_position_t             source_position = token.source_position;
3596         expression_t                 *left;
3597
3598         if(parser->parser != NULL) {
3599                 left = parser->parser(parser->precedence);
3600         } else {
3601                 left = parse_primary_expression();
3602         }
3603         assert(left != NULL);
3604         left->source_position = source_position;
3605
3606         while(true) {
3607                 if(token.type < 0) {
3608                         return expected_expression_error();
3609                 }
3610
3611                 parser = &expression_parsers[token.type];
3612                 if(parser->infix_parser == NULL)
3613                         break;
3614                 if(parser->infix_precedence < precedence)
3615                         break;
3616
3617                 left = parser->infix_parser(parser->infix_precedence, left);
3618
3619                 assert(left != NULL);
3620                 assert(left->type != EXPR_UNKNOWN);
3621                 left->source_position = source_position;
3622         }
3623
3624         return left;
3625 }
3626
3627 static expression_t *parse_expression(void)
3628 {
3629         return parse_sub_expression(1);
3630 }
3631
3632
3633
3634 static void register_expression_parser(parse_expression_function parser,
3635                                        int token_type, unsigned precedence)
3636 {
3637         expression_parser_function_t *entry = &expression_parsers[token_type];
3638
3639         if(entry->parser != NULL) {
3640                 fprintf(stderr, "for token ");
3641                 print_token_type(stderr, token_type);
3642                 fprintf(stderr, "\n");
3643                 panic("trying to register multiple expression parsers for a token");
3644         }
3645         entry->parser     = parser;
3646         entry->precedence = precedence;
3647 }
3648
3649 static void register_expression_infix_parser(
3650                 parse_expression_infix_function parser, int token_type,
3651                 unsigned precedence)
3652 {
3653         expression_parser_function_t *entry = &expression_parsers[token_type];
3654
3655         if(entry->infix_parser != NULL) {
3656                 fprintf(stderr, "for token ");
3657                 print_token_type(stderr, token_type);
3658                 fprintf(stderr, "\n");
3659                 panic("trying to register multiple infix expression parsers for a "
3660                       "token");
3661         }
3662         entry->infix_parser     = parser;
3663         entry->infix_precedence = precedence;
3664 }
3665
3666 static void init_expression_parsers(void)
3667 {
3668         memset(&expression_parsers, 0, sizeof(expression_parsers));
3669
3670         register_expression_infix_parser(parse_BINEXPR_MUL,         '*',        16);
3671         register_expression_infix_parser(parse_BINEXPR_DIV,         '/',        16);
3672         register_expression_infix_parser(parse_BINEXPR_MOD,         '%',        16);
3673         register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,   T_LESSLESS, 16);
3674         register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3675                                                               T_GREATERGREATER, 16);
3676         register_expression_infix_parser(parse_BINEXPR_ADD,         '+',        15);
3677         register_expression_infix_parser(parse_BINEXPR_SUB,         '-',        15);
3678         register_expression_infix_parser(parse_BINEXPR_LESS,        '<',        14);
3679         register_expression_infix_parser(parse_BINEXPR_GREATER,     '>',        14);
3680         register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL,  14);
3681         register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3682                                                                 T_GREATEREQUAL, 14);
3683         register_expression_infix_parser(parse_BINEXPR_EQUAL,     T_EQUALEQUAL, 13);
3684         register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3685                                                         T_EXCLAMATIONMARKEQUAL, 13);
3686         register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&',        12);
3687         register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^',        11);
3688         register_expression_infix_parser(parse_BINEXPR_BITWISE_OR,  '|',        10);
3689         register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND,    9);
3690         register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR,  T_PIPEPIPE,  8);
3691         register_expression_infix_parser(parse_conditional_expression, '?',      7);
3692         register_expression_infix_parser(parse_BINEXPR_ASSIGN,      '=',         2);
3693         register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL,  2);
3694         register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3695         register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3696                                                                 T_ASTERISKEQUAL, 2);
3697         register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3698         register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3699                                                                  T_PERCENTEQUAL, 2);
3700         register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3701                                                                 T_LESSLESSEQUAL, 2);
3702         register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3703                                                           T_GREATERGREATEREQUAL, 2);
3704         register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3705                                                                      T_ANDEQUAL, 2);
3706         register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3707                                                                     T_PIPEEQUAL, 2);
3708         register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3709                                                                    T_CARETEQUAL, 2);
3710
3711         register_expression_infix_parser(parse_BINEXPR_COMMA,       ',',         1);
3712
3713         register_expression_infix_parser(parse_array_expression,        '[',    30);
3714         register_expression_infix_parser(parse_call_expression,         '(',    30);
3715         register_expression_infix_parser(parse_select_expression,       '.',    30);
3716         register_expression_infix_parser(parse_select_expression,
3717                                                                 T_MINUSGREATER, 30);
3718         register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3719                                          T_PLUSPLUS, 30);
3720         register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3721                                          T_MINUSMINUS, 30);
3722
3723         register_expression_parser(parse_UNEXPR_NEGATE,           '-',          25);
3724         register_expression_parser(parse_UNEXPR_PLUS,             '+',          25);
3725         register_expression_parser(parse_UNEXPR_NOT,              '!',          25);
3726         register_expression_parser(parse_UNEXPR_BITWISE_NEGATE,   '~',          25);
3727         register_expression_parser(parse_UNEXPR_DEREFERENCE,      '*',          25);
3728         register_expression_parser(parse_UNEXPR_TAKE_ADDRESS,     '&',          25);
3729         register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS,   25);
3730         register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3731         register_expression_parser(parse_sizeof,                  T_sizeof,     25);
3732         register_expression_parser(parse_extension,            T___extension__, 25);
3733         register_expression_parser(parse_builtin_classify_type,
3734                                                      T___builtin_classify_type, 25);
3735 }
3736
3737
3738 static statement_t *parse_case_statement(void)
3739 {
3740         eat(T_case);
3741         case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3742         label->statement.type            = STATEMENT_CASE_LABEL;
3743         label->statement.source_position = token.source_position;
3744
3745         label->expression = parse_expression();
3746
3747         expect(':');
3748         label->statement.next = parse_statement();
3749
3750         return (statement_t*) label;
3751 }
3752
3753 static statement_t *parse_default_statement(void)
3754 {
3755         eat(T_default);
3756
3757         case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3758         label->statement.type            = STATEMENT_CASE_LABEL;
3759         label->statement.source_position = token.source_position;
3760
3761         expect(':');
3762         label->statement.next = parse_statement();
3763
3764         return (statement_t*) label;
3765 }
3766
3767 static declaration_t *get_label(symbol_t *symbol)
3768 {
3769         declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3770         assert(current_function != NULL);
3771         /* if we found a label in the same function, then we already created the
3772          * declaration */
3773         if(candidate != NULL
3774                         && candidate->parent_context == &current_function->context) {
3775                 return candidate;
3776         }
3777
3778         /* otherwise we need to create a new one */
3779         declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3780         declaration->namespc     = NAMESPACE_LABEL;
3781         declaration->symbol        = symbol;
3782
3783         label_push(declaration);
3784
3785         return declaration;
3786 }
3787
3788 static statement_t *parse_label_statement(void)
3789 {
3790         assert(token.type == T_IDENTIFIER);
3791         symbol_t *symbol = token.v.symbol;
3792         next_token();
3793
3794         declaration_t *label = get_label(symbol);
3795
3796         /* if source position is already set then the label is defined twice,
3797          * otherwise it was just mentioned in a goto so far */
3798         if(label->source_position.input_name != NULL) {
3799                 parser_print_error_prefix();
3800                 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3801                 parser_print_error_prefix_pos(label->source_position);
3802                 fprintf(stderr, "previous definition of '%s' was here\n",
3803                         symbol->string);
3804         } else {
3805                 label->source_position = token.source_position;
3806         }
3807
3808         label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
3809
3810         label_statement->statement.type            = STATEMENT_LABEL;
3811         label_statement->statement.source_position = token.source_position;
3812         label_statement->label                     = label;
3813
3814         expect(':');
3815
3816         if(token.type == '}') {
3817                 parse_error("label at end of compound statement");
3818                 return (statement_t*) label_statement;
3819         } else {
3820                 label_statement->label_statement = parse_statement();
3821         }
3822
3823         return (statement_t*) label_statement;
3824 }
3825
3826 static statement_t *parse_if(void)
3827 {
3828         eat(T_if);
3829
3830         if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3831         statement->statement.type            = STATEMENT_IF;
3832         statement->statement.source_position = token.source_position;
3833
3834         expect('(');
3835         statement->condition = parse_expression();
3836         expect(')');
3837
3838         statement->true_statement = parse_statement();
3839         if(token.type == T_else) {
3840                 next_token();
3841                 statement->false_statement = parse_statement();
3842         }
3843
3844         return (statement_t*) statement;
3845 }
3846
3847 static statement_t *parse_switch(void)
3848 {
3849         eat(T_switch);
3850
3851         switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3852         statement->statement.type            = STATEMENT_SWITCH;
3853         statement->statement.source_position = token.source_position;
3854
3855         expect('(');
3856         statement->expression = parse_expression();
3857         expect(')');
3858         statement->body = parse_statement();
3859
3860         return (statement_t*) statement;
3861 }
3862
3863 static statement_t *parse_while(void)
3864 {
3865         eat(T_while);
3866
3867         while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3868         statement->statement.type            = STATEMENT_WHILE;
3869         statement->statement.source_position = token.source_position;
3870
3871         expect('(');
3872         statement->condition = parse_expression();
3873         expect(')');
3874         statement->body = parse_statement();
3875
3876         return (statement_t*) statement;
3877 }
3878
3879 static statement_t *parse_do(void)
3880 {
3881         eat(T_do);
3882
3883         do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3884         statement->statement.type            = STATEMENT_DO_WHILE;
3885         statement->statement.source_position = token.source_position;
3886
3887         statement->body = parse_statement();
3888         expect(T_while);
3889         expect('(');
3890         statement->condition = parse_expression();
3891         expect(')');
3892         expect(';');
3893
3894         return (statement_t*) statement;
3895 }
3896
3897 static statement_t *parse_for(void)
3898 {
3899         eat(T_for);
3900
3901         for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3902         statement->statement.type            = STATEMENT_FOR;
3903         statement->statement.source_position = token.source_position;
3904
3905         expect('(');
3906
3907         int         top          = environment_top();
3908         context_t  *last_context = context;
3909         set_context(&statement->context);
3910
3911         if(token.type != ';') {
3912                 if(is_declaration_specifier(&token, false)) {
3913                         parse_declaration();
3914                 } else {
3915                         statement->initialisation = parse_expression();
3916                         expect(';');
3917                 }
3918         } else {
3919                 expect(';');
3920         }
3921
3922         if(token.type != ';') {
3923                 statement->condition = parse_expression();
3924         }
3925         expect(';');
3926         if(token.type != ')') {
3927                 statement->step = parse_expression();
3928         }
3929         expect(')');
3930         statement->body = parse_statement();
3931
3932         assert(context == &statement->context);
3933         set_context(last_context);
3934         environment_pop_to(top);
3935
3936         return (statement_t*) statement;
3937 }
3938
3939 static statement_t *parse_goto(void)
3940 {
3941         eat(T_goto);
3942
3943         if(token.type != T_IDENTIFIER) {
3944                 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
3945                 eat_statement();
3946                 return NULL;
3947         }
3948         symbol_t *symbol = token.v.symbol;
3949         next_token();
3950
3951         declaration_t *label = get_label(symbol);
3952
3953         goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3954
3955         statement->statement.type            = STATEMENT_GOTO;
3956         statement->statement.source_position = token.source_position;
3957
3958         statement->label = label;
3959
3960         expect(';');
3961
3962         return (statement_t*) statement;
3963 }
3964
3965 static statement_t *parse_continue(void)
3966 {
3967         eat(T_continue);
3968         expect(';');
3969
3970         statement_t *statement     = allocate_ast_zero(sizeof(statement[0]));
3971         statement->type            = STATEMENT_CONTINUE;
3972         statement->source_position = token.source_position;
3973
3974         return statement;
3975 }
3976
3977 static statement_t *parse_break(void)
3978 {
3979         eat(T_break);
3980         expect(';');
3981
3982         statement_t *statement     = allocate_ast_zero(sizeof(statement[0]));
3983         statement->type            = STATEMENT_BREAK;
3984         statement->source_position = token.source_position;
3985
3986         return statement;
3987 }
3988
3989 static statement_t *parse_return(void)
3990 {
3991         eat(T_return);
3992
3993         return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3994
3995         statement->statement.type            = STATEMENT_RETURN;
3996         statement->statement.source_position = token.source_position;
3997
3998         assert(current_function->type->type == TYPE_FUNCTION);
3999         function_type_t *function_type = (function_type_t*) current_function->type;
4000         type_t          *return_type   = function_type->result_type;
4001
4002         expression_t *return_value;
4003         if(token.type != ';') {
4004                 return_value = parse_expression();
4005
4006                 if(return_type == type_void && return_value->datatype != type_void) {
4007                         parse_warning("'return' with a value, in function returning void");
4008                         return_value = NULL;
4009                 } else {
4010                         if(return_type != NULL) {
4011                                 semantic_assign(return_type, &return_value, "'return'");
4012                         }
4013                 }
4014         } else {
4015                 return_value = NULL;
4016                 if(return_type != type_void) {
4017                         parse_warning("'return' without value, in function returning "
4018                                       "non-void");
4019                 }
4020         }
4021         statement->return_value = return_value;
4022
4023         expect(';');
4024
4025         return (statement_t*) statement;
4026 }
4027
4028 static statement_t *parse_declaration_statement(void)
4029 {
4030         declaration_t *before = last_declaration;
4031
4032         declaration_statement_t *statement
4033                 = allocate_ast_zero(sizeof(statement[0]));
4034         statement->statement.type            = STATEMENT_DECLARATION;
4035         statement->statement.source_position = token.source_position;
4036
4037         declaration_specifiers_t specifiers;
4038         memset(&specifiers, 0, sizeof(specifiers));
4039         parse_declaration_specifiers(&specifiers);
4040
4041         if(token.type == ';') {
4042                 eat(';');
4043         } else {
4044                 parse_init_declarators(&specifiers);
4045         }
4046
4047         if(before == NULL) {
4048                 statement->declarations_begin = context->declarations;
4049         } else {
4050                 statement->declarations_begin = before->next;
4051         }
4052         statement->declarations_end = last_declaration;
4053
4054         return (statement_t*) statement;
4055 }
4056
4057 static statement_t *parse_expression_statement(void)
4058 {
4059         expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4060         statement->statement.type            = STATEMENT_EXPRESSION;
4061         statement->statement.source_position = token.source_position;
4062
4063         statement->expression = parse_expression();
4064
4065         expect(';');
4066
4067         return (statement_t*) statement;
4068 }
4069
4070 static statement_t *parse_statement(void)
4071 {
4072         statement_t   *statement = NULL;
4073
4074         /* declaration or statement */
4075         switch(token.type) {
4076         case T_case:
4077                 statement = parse_case_statement();
4078                 break;
4079
4080         case T_default:
4081                 statement = parse_default_statement();
4082                 break;
4083
4084         case '{':
4085                 statement = parse_compound_statement();
4086                 break;
4087
4088         case T_if:
4089                 statement = parse_if();
4090                 break;
4091
4092         case T_switch:
4093                 statement = parse_switch();
4094                 break;
4095
4096         case T_while:
4097                 statement = parse_while();
4098                 break;
4099
4100         case T_do:
4101                 statement = parse_do();
4102                 break;
4103
4104         case T_for:
4105                 statement = parse_for();
4106                 break;
4107
4108         case T_goto:
4109                 statement = parse_goto();
4110                 break;
4111
4112         case T_continue:
4113                 statement = parse_continue();
4114                 break;
4115
4116         case T_break:
4117                 statement = parse_break();
4118                 break;
4119
4120         case T_return:
4121                 statement = parse_return();
4122                 break;
4123
4124         case ';':
4125                 next_token();
4126                 statement = NULL;
4127                 break;
4128
4129         case T_IDENTIFIER:
4130                 if(look_ahead(1)->type == ':') {
4131                         statement = parse_label_statement();
4132                         break;
4133                 }
4134
4135                 if(is_typedef_symbol(token.v.symbol)) {
4136                         statement = parse_declaration_statement();
4137                         break;
4138                 }
4139
4140                 statement = parse_expression_statement();
4141                 break;
4142
4143         case T___extension__:
4144                 /* this can be a prefix to a declaration or an expression statement */
4145                 /* we simply eat it now and parse the rest with tail recursion */
4146                 do {
4147                         next_token();
4148                 } while(token.type == T___extension__);
4149                 statement = parse_statement();
4150                 break;
4151
4152         DECLARATION_START
4153                 statement = parse_declaration_statement();
4154                 break;
4155
4156         default:
4157                 statement = parse_expression_statement();
4158                 break;
4159         }
4160
4161         assert(statement == NULL || statement->source_position.input_name != NULL);
4162
4163         return statement;
4164 }
4165
4166 static statement_t *parse_compound_statement(void)
4167 {
4168         compound_statement_t *compound_statement
4169                 = allocate_ast_zero(sizeof(compound_statement[0]));
4170         compound_statement->statement.type            = STATEMENT_COMPOUND;
4171         compound_statement->statement.source_position = token.source_position;
4172
4173         eat('{');
4174
4175         int        top          = environment_top();
4176         context_t *last_context = context;
4177         set_context(&compound_statement->context);
4178
4179         statement_t *last_statement = NULL;
4180
4181         while(token.type != '}' && token.type != T_EOF) {
4182                 statement_t *statement = parse_statement();
4183                 if(statement == NULL)
4184                         continue;
4185
4186                 if(last_statement != NULL) {
4187                         last_statement->next = statement;
4188                 } else {
4189                         compound_statement->statements = statement;
4190                 }
4191
4192                 while(statement->next != NULL)
4193                         statement = statement->next;
4194
4195                 last_statement = statement;
4196         }
4197
4198         if(token.type != '}') {
4199                 parser_print_error_prefix_pos(
4200                                 compound_statement->statement.source_position);
4201                 fprintf(stderr, "end of file while looking for closing '}'\n");
4202         }
4203         next_token();
4204
4205         assert(context == &compound_statement->context);
4206         set_context(last_context);
4207         environment_pop_to(top);
4208
4209         return (statement_t*) compound_statement;
4210 }
4211
4212 static translation_unit_t *parse_translation_unit(void)
4213 {
4214         translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4215
4216         assert(global_context == NULL);
4217         global_context = &unit->context;
4218
4219         assert(context == NULL);
4220         set_context(&unit->context);
4221
4222         while(token.type != T_EOF) {
4223                 parse_declaration();
4224         }
4225
4226         assert(context == &unit->context);
4227         context          = NULL;
4228         last_declaration = NULL;
4229
4230         assert(global_context == &unit->context);
4231         global_context = NULL;
4232
4233         return unit;
4234 }
4235
4236 translation_unit_t *parse(void)
4237 {
4238         environment_stack = NEW_ARR_F(stack_entry_t, 0);
4239         label_stack       = NEW_ARR_F(stack_entry_t, 0);
4240         found_error       = false;
4241
4242         type_set_output(stderr);
4243         ast_set_output(stderr);
4244
4245         lookahead_bufpos = 0;
4246         for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4247                 next_token();
4248         }
4249         translation_unit_t *unit = parse_translation_unit();
4250
4251         DEL_ARR_F(environment_stack);
4252         DEL_ARR_F(label_stack);
4253
4254         if(found_error)
4255                 return NULL;
4256
4257         return unit;
4258 }
4259
4260 void init_parser(void)
4261 {
4262         init_expression_parsers();
4263         obstack_init(&temp_obst);
4264
4265         type_int         = make_atomic_type(ATOMIC_TYPE_INT, 0);
4266         type_uint        = make_atomic_type(ATOMIC_TYPE_UINT, 0);
4267         type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
4268         type_double      = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
4269         type_float       = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
4270         type_size_t      = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
4271         type_ptrdiff_t   = make_atomic_type(ATOMIC_TYPE_LONG, 0);
4272         type_const_char  = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4273         type_void        = make_atomic_type(ATOMIC_TYPE_VOID, 0);
4274         type_string      = make_pointer_type(type_const_char, 0);
4275 }
4276
4277 void exit_parser(void)
4278 {
4279         obstack_free(&temp_obst, NULL);
4280 }