Implement __func__, __FUNCTION__ and __PRETTY_FUNCTION__.
[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 *get_type_after_conversion(const type_t *type1,
2953                                          const type_t *type2)
2954 {
2955         /* TODO... */
2956         (void) type2;
2957         return (type_t*) type1;
2958 }
2959
2960 static expression_t *parse_conditional_expression(unsigned precedence,
2961                                                   expression_t *expression)
2962 {
2963         eat('?');
2964
2965         conditional_expression_t *conditional
2966                 = allocate_ast_zero(sizeof(conditional[0]));
2967         conditional->expression.type = EXPR_CONDITIONAL;
2968         conditional->condition = expression;
2969
2970         /* 6.5.15.2 */
2971         type_t *condition_type_orig = conditional->condition->datatype;
2972         type_t *condition_type      = skip_typeref(condition_type_orig);
2973         if(condition_type != NULL && !is_type_scalar(condition_type)) {
2974                 type_error("expected a scalar type", expression->source_position,
2975                            condition_type_orig);
2976         }
2977
2978         conditional->true_expression = parse_expression();
2979         expect(':');
2980         conditional->false_expression = parse_sub_expression(precedence);
2981
2982         type_t *true_type  = conditional->true_expression->datatype;
2983         if(true_type == NULL)
2984                 return (expression_t*) conditional;
2985         type_t *false_type = conditional->false_expression->datatype;
2986         if(false_type == NULL)
2987                 return (expression_t*) conditional;
2988
2989         type_t *const skipped_true_type  = skip_typeref(true_type);
2990         type_t *const skipped_false_type = skip_typeref(false_type);
2991
2992         /* 6.5.15.3 */
2993         if (skipped_true_type == skipped_false_type) {
2994                 conditional->expression.datatype = skipped_true_type;
2995         } else if (is_type_arithmetic(skipped_true_type) &&
2996                    is_type_arithmetic(skipped_false_type)) {
2997                 type_t *const result = get_type_after_conversion(skipped_true_type,
2998                                                                  skipped_false_type);
2999                 /* TODO: create implicit convs if necessary */
3000                 conditional->expression.datatype = result;
3001         } else if (skipped_true_type->type == TYPE_POINTER &&
3002                    skipped_false_type->type == TYPE_POINTER &&
3003                           true /* TODO compatible points_to types */) {
3004                 /* TODO */
3005         } else if(/* (is_null_ptr_const(skipped_true_type) &&
3006                       skipped_false_type->type == TYPE_POINTER)
3007                || (is_null_ptr_const(skipped_false_type) &&
3008                    skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3009                 /* TODO */
3010         } else if(/* 1 is pointer to object type, other is void* */ false) {
3011                 /* TODO */
3012         } else {
3013                 type_error_incompatible("while parsing conditional",
3014                                         expression->source_position, true_type,
3015                                         skipped_false_type);
3016         }
3017
3018         return (expression_t*) conditional;
3019 }
3020
3021 static expression_t *parse_extension(unsigned precedence)
3022 {
3023         eat(T___extension__);
3024
3025         /* TODO enable extensions */
3026
3027         return parse_sub_expression(precedence);
3028 }
3029
3030 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3031 {
3032         eat(T___builtin_classify_type);
3033
3034         classify_type_expression_t *const classify_type_expr =
3035                 allocate_ast_zero(sizeof(classify_type_expr[0]));
3036         classify_type_expr->expression.type     = EXPR_CLASSIFY_TYPE;
3037         classify_type_expr->expression.datatype = type_int;
3038
3039         expect('(');
3040         expression_t *const expression = parse_sub_expression(precedence);
3041         expect(')');
3042         classify_type_expr->type_expression = expression;
3043
3044         return (expression_t*)classify_type_expr;
3045 }
3046
3047 static void semantic_incdec(unary_expression_t *expression)
3048 {
3049         type_t *orig_type = expression->value->datatype;
3050         if(orig_type == NULL)
3051                 return;
3052
3053         type_t *type = skip_typeref(orig_type);
3054         if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3055                 /* TODO: improve error message */
3056                 parser_print_error_prefix();
3057                 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3058                 return;
3059         }
3060
3061         expression->expression.datatype = orig_type;
3062 }
3063
3064 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3065 {
3066         type_t *orig_type = expression->value->datatype;
3067         if(orig_type == NULL)
3068                 return;
3069
3070         type_t *type = skip_typeref(orig_type);
3071         if(!is_type_arithmetic(type)) {
3072                 /* TODO: improve error message */
3073                 parser_print_error_prefix();
3074                 fprintf(stderr, "operation needs an arithmetic type\n");
3075                 return;
3076         }
3077
3078         expression->expression.datatype = orig_type;
3079 }
3080
3081 static void semantic_unexpr_scalar(unary_expression_t *expression)
3082 {
3083         type_t *orig_type = expression->value->datatype;
3084         if(orig_type == NULL)
3085                 return;
3086
3087         type_t *type = skip_typeref(orig_type);
3088         if (!is_type_scalar(type)) {
3089                 parse_error("operand of ! must be of scalar type\n");
3090                 return;
3091         }
3092
3093         expression->expression.datatype = orig_type;
3094 }
3095
3096 static void semantic_unexpr_integer(unary_expression_t *expression)
3097 {
3098         type_t *orig_type = expression->value->datatype;
3099         if(orig_type == NULL)
3100                 return;
3101
3102         type_t *type = skip_typeref(orig_type);
3103         if (!is_type_integer(type)) {
3104                 parse_error("operand of ~ must be of integer type\n");
3105                 return;
3106         }
3107
3108         expression->expression.datatype = orig_type;
3109 }
3110
3111 static void semantic_dereference(unary_expression_t *expression)
3112 {
3113         type_t *orig_type = expression->value->datatype;
3114         if(orig_type == NULL)
3115                 return;
3116
3117         type_t *type = skip_typeref(orig_type);
3118         switch (type->type) {
3119                 case TYPE_ARRAY: {
3120                         array_type_t *const array_type  = (array_type_t*)type;
3121                         expression->expression.datatype = array_type->element_type;
3122                         break;
3123                 }
3124
3125                 case TYPE_POINTER: {
3126                         pointer_type_t *pointer_type    = (pointer_type_t*)type;
3127                         expression->expression.datatype = pointer_type->points_to;
3128                         break;
3129                 }
3130
3131                 default:
3132                         parser_print_error_prefix();
3133                         fputs("'Unary *' needs pointer or arrray type, but type ", stderr);
3134                         print_type_quoted(orig_type);
3135                         fputs(" given.\n", stderr);
3136                         return;
3137         }
3138 }
3139
3140 static void semantic_take_addr(unary_expression_t *expression)
3141 {
3142         type_t *orig_type = expression->value->datatype;
3143         if(orig_type == NULL)
3144                 return;
3145
3146         expression_t *value = expression->value;
3147         if(value->type == EXPR_REFERENCE) {
3148                 reference_expression_t *reference   = (reference_expression_t*) value;
3149                 declaration_t          *declaration = reference->declaration;
3150                 if(declaration != NULL) {
3151                         declaration->address_taken = 1;
3152                 }
3153         }
3154
3155         expression->expression.datatype = make_pointer_type(orig_type, 0);
3156 }
3157
3158 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc)   \
3159 static expression_t *parse_##unexpression_type(unsigned precedence)            \
3160 {                                                                              \
3161         eat(token_type);                                                           \
3162                                                                                \
3163         unary_expression_t *unary_expression                                       \
3164                 = allocate_ast_zero(sizeof(unary_expression[0]));                      \
3165         unary_expression->expression.type     = EXPR_UNARY;                        \
3166         unary_expression->type                = unexpression_type;                 \
3167         unary_expression->value               = parse_sub_expression(precedence);  \
3168                                                                                    \
3169         sfunc(unary_expression);                                                   \
3170                                                                                \
3171         return (expression_t*) unary_expression;                                   \
3172 }
3173
3174 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3175 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS,   semantic_unexpr_arithmetic)
3176 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT,    semantic_unexpr_scalar)
3177 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3178 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3179 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3180                                semantic_unexpr_integer)
3181 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_PREFIX_INCREMENT,
3182                                semantic_incdec)
3183 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3184                                semantic_incdec)
3185
3186 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3187                                                sfunc)                         \
3188 static expression_t *parse_##unexpression_type(unsigned precedence,           \
3189                                                expression_t *left)            \
3190 {                                                                             \
3191         (void) precedence;                                                        \
3192         eat(token_type);                                                          \
3193                                                                               \
3194         unary_expression_t *unary_expression                                      \
3195                 = allocate_ast_zero(sizeof(unary_expression[0]));                     \
3196         unary_expression->expression.type     = EXPR_UNARY;                       \
3197         unary_expression->type                = unexpression_type;                \
3198         unary_expression->value               = left;                             \
3199                                                                                   \
3200         sfunc(unary_expression);                                                  \
3201                                                                               \
3202         return (expression_t*) unary_expression;                                  \
3203 }
3204
3205 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_POSTFIX_INCREMENT,
3206                                        semantic_incdec)
3207 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3208                                        semantic_incdec)
3209
3210 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3211 {
3212         /* TODO: handle complex + imaginary types */
3213
3214         /* Â§ 6.3.1.8 Usual arithmetic conversions */
3215         if(type_left == type_long_double || type_right == type_long_double) {
3216                 return type_long_double;
3217         } else if(type_left == type_double || type_right == type_double) {
3218                 return type_double;
3219         } else if(type_left == type_float || type_right == type_float) {
3220                 return type_float;
3221         }
3222
3223         type_right = promote_integer(type_right);
3224         type_left  = promote_integer(type_left);
3225
3226         if(type_left == type_right)
3227                 return type_left;
3228
3229         bool signed_left  = is_type_signed(type_left);
3230         bool signed_right = is_type_signed(type_right);
3231         if(get_rank(type_left) < get_rank(type_right)) {
3232                 if(signed_left == signed_right || !signed_right) {
3233                         return type_right;
3234                 } else {
3235                         return type_left;
3236                 }
3237         } else {
3238                 if(signed_left == signed_right || !signed_left) {
3239                         return type_left;
3240                 } else {
3241                         return type_right;
3242                 }
3243         }
3244 }
3245
3246 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3247 {
3248         expression_t *left       = expression->left;
3249         expression_t *right      = expression->right;
3250         type_t       *orig_type_left  = left->datatype;
3251         type_t       *orig_type_right = right->datatype;
3252
3253         if(orig_type_left == NULL || orig_type_right == NULL)
3254                 return;
3255
3256         type_t *type_left  = skip_typeref(orig_type_left);
3257         type_t *type_right = skip_typeref(orig_type_right);
3258
3259         if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3260                 /* TODO: improve error message */
3261                 parser_print_error_prefix();
3262                 fprintf(stderr, "operation needs arithmetic types\n");
3263                 return;
3264         }
3265
3266         type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3267         expression->left  = create_implicit_cast(left, arithmetic_type);
3268         expression->right = create_implicit_cast(right, arithmetic_type);
3269         expression->expression.datatype = arithmetic_type;
3270 }
3271
3272 static void semantic_shift_op(binary_expression_t *expression)
3273 {
3274         expression_t *left       = expression->left;
3275         expression_t *right      = expression->right;
3276         type_t       *orig_type_left  = left->datatype;
3277         type_t       *orig_type_right = right->datatype;
3278
3279         if(orig_type_left == NULL || orig_type_right == NULL)
3280                 return;
3281
3282         type_t *type_left  = skip_typeref(orig_type_left);
3283         type_t *type_right = skip_typeref(orig_type_right);
3284
3285         if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3286                 /* TODO: improve error message */
3287                 parser_print_error_prefix();
3288                 fprintf(stderr, "operation needs integer types\n");
3289                 return;
3290         }
3291
3292         type_left  = promote_integer(type_left);
3293         type_right = promote_integer(type_right);
3294
3295         expression->left  = create_implicit_cast(left, type_left);
3296         expression->right = create_implicit_cast(right, type_right);
3297         expression->expression.datatype = type_left;
3298 }
3299
3300 static void semantic_add(binary_expression_t *expression)
3301 {
3302         expression_t *left            = expression->left;
3303         expression_t *right           = expression->right;
3304         type_t       *orig_type_left  = left->datatype;
3305         type_t       *orig_type_right = right->datatype;
3306
3307         if(orig_type_left == NULL || orig_type_right == NULL)
3308                 return;
3309
3310         type_t *type_left  = skip_typeref(orig_type_left);
3311         type_t *type_right = skip_typeref(orig_type_right);
3312
3313         /* Â§ 5.6.5 */
3314         if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3315                 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3316                 expression->left  = create_implicit_cast(left, arithmetic_type);
3317                 expression->right = create_implicit_cast(right, arithmetic_type);
3318                 expression->expression.datatype = arithmetic_type;
3319                 return;
3320         } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3321                 expression->expression.datatype = type_left;
3322         } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
3323                 expression->expression.datatype = type_right;
3324         } else if (type_left->type == TYPE_ARRAY && is_type_integer(type_right)) {
3325                 const array_type_t *const arr_type = (const array_type_t*)type_left;
3326                 expression->expression.datatype =
3327                   make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3328         } else if (type_right->type == TYPE_ARRAY && is_type_integer(type_left)) {
3329                 const array_type_t *const arr_type = (const array_type_t*)type_right;
3330                 expression->expression.datatype =
3331                         make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3332         } else {
3333                 parser_print_error_prefix();
3334                 fprintf(stderr, "invalid operands to binary + (");
3335                 print_type_quoted(orig_type_left);
3336                 fprintf(stderr, ", ");
3337                 print_type_quoted(orig_type_right);
3338                 fprintf(stderr, ")\n");
3339         }
3340 }
3341
3342 static void semantic_sub(binary_expression_t *expression)
3343 {
3344         expression_t *left            = expression->left;
3345         expression_t *right           = expression->right;
3346         type_t       *orig_type_left  = left->datatype;
3347         type_t       *orig_type_right = right->datatype;
3348
3349         if(orig_type_left == NULL || orig_type_right == NULL)
3350                 return;
3351
3352         type_t       *type_left       = skip_typeref(orig_type_left);
3353         type_t       *type_right      = skip_typeref(orig_type_right);
3354
3355         /* Â§ 5.6.5 */
3356         if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3357                 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3358                 expression->left  = create_implicit_cast(left, arithmetic_type);
3359                 expression->right = create_implicit_cast(right, arithmetic_type);
3360                 expression->expression.datatype = arithmetic_type;
3361                 return;
3362         } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3363                 expression->expression.datatype = type_left;
3364         } else if(type_left->type == TYPE_POINTER &&
3365                         type_right->type == TYPE_POINTER) {
3366                 if(!pointers_compatible(type_left, type_right)) {
3367                         parser_print_error_prefix();
3368                         fprintf(stderr, "pointers to incompatible objects to binary - (");
3369                         print_type_quoted(orig_type_left);
3370                         fprintf(stderr, ", ");
3371                         print_type_quoted(orig_type_right);
3372                         fprintf(stderr, ")\n");
3373                 } else {
3374                         expression->expression.datatype = type_ptrdiff_t;
3375                 }
3376         } else {
3377                 parser_print_error_prefix();
3378                 fprintf(stderr, "invalid operands to binary - (");
3379                 print_type_quoted(orig_type_left);
3380                 fprintf(stderr, ", ");
3381                 print_type_quoted(orig_type_right);
3382                 fprintf(stderr, ")\n");
3383         }
3384 }
3385
3386 static void semantic_comparison(binary_expression_t *expression)
3387 {
3388         expression_t *left            = expression->left;
3389         expression_t *right           = expression->right;
3390         type_t       *orig_type_left  = left->datatype;
3391         type_t       *orig_type_right = right->datatype;
3392
3393         if(orig_type_left == NULL || orig_type_right == NULL)
3394                 return;
3395
3396         type_t *type_left  = skip_typeref(orig_type_left);
3397         type_t *type_right = skip_typeref(orig_type_right);
3398
3399         /* TODO non-arithmetic types */
3400         if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3401                 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3402                 expression->left  = create_implicit_cast(left, arithmetic_type);
3403                 expression->right = create_implicit_cast(right, arithmetic_type);
3404                 expression->expression.datatype = arithmetic_type;
3405         } else if (type_left->type  == TYPE_POINTER &&
3406                    type_right->type == TYPE_POINTER) {
3407                 /* TODO check compatibility */
3408         } else if (type_left->type == TYPE_POINTER) {
3409                 expression->right = create_implicit_cast(right, type_left);
3410         } else if (type_right->type == TYPE_POINTER) {
3411                 expression->left = create_implicit_cast(left, type_right);
3412         } else {
3413                 type_error_incompatible("invalid operands in comparison",
3414                                         expression->expression.source_position,
3415                                         type_left, type_right);
3416         }
3417         expression->expression.datatype = type_int;
3418 }
3419
3420 static void semantic_arithmetic_assign(binary_expression_t *expression)
3421 {
3422         expression_t *left            = expression->left;
3423         expression_t *right           = expression->right;
3424         type_t       *orig_type_left  = left->datatype;
3425         type_t       *orig_type_right = right->datatype;
3426
3427         if(orig_type_left == NULL || orig_type_right == NULL)
3428                 return;
3429
3430         type_t *type_left  = skip_typeref(orig_type_left);
3431         type_t *type_right = skip_typeref(orig_type_right);
3432
3433         if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3434                 /* TODO: improve error message */
3435                 parser_print_error_prefix();
3436                 fprintf(stderr, "operation needs arithmetic types\n");
3437                 return;
3438         }
3439
3440         /* combined instructions are tricky. We can't create an implicit cast on
3441          * the left side, because we need the uncasted form for the store.
3442          * The ast2firm pass has to know that left_type must be right_type
3443          * for the arithmeitc operation and create a cast by itself */
3444         type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3445         expression->right       = create_implicit_cast(right, arithmetic_type);
3446         expression->expression.datatype = type_left;
3447 }
3448
3449 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3450 {
3451         expression_t *left            = expression->left;
3452         expression_t *right           = expression->right;
3453         type_t       *orig_type_left  = left->datatype;
3454         type_t       *orig_type_right = right->datatype;
3455
3456         if(orig_type_left == NULL || orig_type_right == NULL)
3457                 return;
3458
3459         type_t *type_left  = skip_typeref(orig_type_left);
3460         type_t *type_right = skip_typeref(orig_type_right);
3461
3462         if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3463                 /* combined instructions are tricky. We can't create an implicit cast on
3464                  * the left side, because we need the uncasted form for the store.
3465                  * The ast2firm pass has to know that left_type must be right_type
3466                  * for the arithmeitc operation and create a cast by itself */
3467                 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3468                 expression->right = create_implicit_cast(right, arithmetic_type);
3469                 expression->expression.datatype = type_left;
3470         } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3471                 expression->expression.datatype = type_left;
3472         } else {
3473                 parser_print_error_prefix();
3474                 fputs("Incompatible types ", stderr);
3475                 print_type_quoted(orig_type_left);
3476                 fputs(" and ", stderr);
3477                 print_type_quoted(orig_type_right);
3478                 fputs(" in assignment\n", stderr);
3479                 return;
3480         }
3481 }
3482
3483 static void semantic_logical_op(binary_expression_t *expression)
3484 {
3485         expression_t *left            = expression->left;
3486         expression_t *right           = expression->right;
3487         type_t       *orig_type_left  = left->datatype;
3488         type_t       *orig_type_right = right->datatype;
3489
3490         if(orig_type_left == NULL || orig_type_right == NULL)
3491                 return;
3492
3493         type_t *type_left  = skip_typeref(orig_type_left);
3494         type_t *type_right = skip_typeref(orig_type_right);
3495
3496         if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3497                 /* TODO: improve error message */
3498                 parser_print_error_prefix();
3499                 fprintf(stderr, "operation needs scalar types\n");
3500                 return;
3501         }
3502
3503         expression->expression.datatype = type_int;
3504 }
3505
3506 static void semantic_binexpr_assign(binary_expression_t *expression)
3507 {
3508         expression_t *left       = expression->left;
3509         type_t       *type_left  = left->datatype;
3510
3511         if (type_left->type == TYPE_ARRAY) {
3512                 parse_error("Cannot assign to arrays.");
3513         } else if (type_left != NULL) {
3514                 semantic_assign(type_left, &expression->right, "assignment");
3515         }
3516
3517         expression->expression.datatype = type_left;
3518 }
3519
3520 static void semantic_comma(binary_expression_t *expression)
3521 {
3522         expression->expression.datatype = expression->right->datatype;
3523 }
3524
3525 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3526 static expression_t *parse_##binexpression_type(unsigned precedence,     \
3527                                                 expression_t *left)      \
3528 {                                                                        \
3529         eat(token_type);                                                     \
3530                                                                          \
3531         expression_t *right = parse_sub_expression(precedence + lr);         \
3532                                                                          \
3533         binary_expression_t *binexpr                                         \
3534                 = allocate_ast_zero(sizeof(binexpr[0]));                         \
3535         binexpr->expression.type     = EXPR_BINARY;                          \
3536         binexpr->type                = binexpression_type;                   \
3537         binexpr->left                = left;                                 \
3538         binexpr->right               = right;                                \
3539         sfunc(binexpr);                                                      \
3540                                                                          \
3541         return (expression_t*) binexpr;                                      \
3542 }
3543
3544 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA,          semantic_comma, 1)
3545 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL,            semantic_binexpr_arithmetic, 1)
3546 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV,            semantic_binexpr_arithmetic, 1)
3547 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD,            semantic_binexpr_arithmetic, 1)
3548 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD,            semantic_add, 1)
3549 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB,            semantic_sub, 1)
3550 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS,           semantic_comparison, 1)
3551 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER,        semantic_comparison, 1)
3552 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN,         semantic_binexpr_assign, 0)
3553 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3554 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3555                       semantic_comparison, 1)
3556 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3557 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3558                       semantic_comparison, 1)
3559 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND,    semantic_binexpr_arithmetic, 1)
3560 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR,     semantic_binexpr_arithmetic, 1)
3561 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR,    semantic_binexpr_arithmetic, 1)
3562 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND,  semantic_logical_op, 1)
3563 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3564 /* TODO shift has a bit special semantic */
3565 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3566                       semantic_shift_op, 1)
3567 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3568                       semantic_shift_op, 1)
3569 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3570                       semantic_arithmetic_addsubb_assign, 0)
3571 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3572                       semantic_arithmetic_addsubb_assign, 0)
3573 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3574                       semantic_arithmetic_assign, 0)
3575 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3576                       semantic_arithmetic_assign, 0)
3577 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3578                       semantic_arithmetic_assign, 0)
3579 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3580                       semantic_arithmetic_assign, 0)
3581 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3582                       semantic_arithmetic_assign, 0)
3583 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3584                       semantic_arithmetic_assign, 0)
3585 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3586                       semantic_arithmetic_assign, 0)
3587 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3588                       semantic_arithmetic_assign, 0)
3589
3590 static expression_t *parse_sub_expression(unsigned precedence)
3591 {
3592         if(token.type < 0) {
3593                 return expected_expression_error();
3594         }
3595
3596         expression_parser_function_t *parser
3597                 = &expression_parsers[token.type];
3598         source_position_t             source_position = token.source_position;
3599         expression_t                 *left;
3600
3601         if(parser->parser != NULL) {
3602                 left = parser->parser(parser->precedence);
3603         } else {
3604                 left = parse_primary_expression();
3605         }
3606         assert(left != NULL);
3607         left->source_position = source_position;
3608
3609         while(true) {
3610                 if(token.type < 0) {
3611                         return expected_expression_error();
3612                 }
3613
3614                 parser = &expression_parsers[token.type];
3615                 if(parser->infix_parser == NULL)
3616                         break;
3617                 if(parser->infix_precedence < precedence)
3618                         break;
3619
3620                 left = parser->infix_parser(parser->infix_precedence, left);
3621
3622                 assert(left != NULL);
3623                 assert(left->type != EXPR_UNKNOWN);
3624                 left->source_position = source_position;
3625         }
3626
3627         return left;
3628 }
3629
3630 static expression_t *parse_expression(void)
3631 {
3632         return parse_sub_expression(1);
3633 }
3634
3635
3636
3637 static void register_expression_parser(parse_expression_function parser,
3638                                        int token_type, unsigned precedence)
3639 {
3640         expression_parser_function_t *entry = &expression_parsers[token_type];
3641
3642         if(entry->parser != NULL) {
3643                 fprintf(stderr, "for token ");
3644                 print_token_type(stderr, token_type);
3645                 fprintf(stderr, "\n");
3646                 panic("trying to register multiple expression parsers for a token");
3647         }
3648         entry->parser     = parser;
3649         entry->precedence = precedence;
3650 }
3651
3652 static void register_expression_infix_parser(
3653                 parse_expression_infix_function parser, int token_type,
3654                 unsigned precedence)
3655 {
3656         expression_parser_function_t *entry = &expression_parsers[token_type];
3657
3658         if(entry->infix_parser != NULL) {
3659                 fprintf(stderr, "for token ");
3660                 print_token_type(stderr, token_type);
3661                 fprintf(stderr, "\n");
3662                 panic("trying to register multiple infix expression parsers for a "
3663                       "token");
3664         }
3665         entry->infix_parser     = parser;
3666         entry->infix_precedence = precedence;
3667 }
3668
3669 static void init_expression_parsers(void)
3670 {
3671         memset(&expression_parsers, 0, sizeof(expression_parsers));
3672
3673         register_expression_infix_parser(parse_BINEXPR_MUL,         '*',        16);
3674         register_expression_infix_parser(parse_BINEXPR_DIV,         '/',        16);
3675         register_expression_infix_parser(parse_BINEXPR_MOD,         '%',        16);
3676         register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,   T_LESSLESS, 16);
3677         register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3678                                                               T_GREATERGREATER, 16);
3679         register_expression_infix_parser(parse_BINEXPR_ADD,         '+',        15);
3680         register_expression_infix_parser(parse_BINEXPR_SUB,         '-',        15);
3681         register_expression_infix_parser(parse_BINEXPR_LESS,        '<',        14);
3682         register_expression_infix_parser(parse_BINEXPR_GREATER,     '>',        14);
3683         register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL,  14);
3684         register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3685                                                                 T_GREATEREQUAL, 14);
3686         register_expression_infix_parser(parse_BINEXPR_EQUAL,     T_EQUALEQUAL, 13);
3687         register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3688                                                         T_EXCLAMATIONMARKEQUAL, 13);
3689         register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&',        12);
3690         register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^',        11);
3691         register_expression_infix_parser(parse_BINEXPR_BITWISE_OR,  '|',        10);
3692         register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND,    9);
3693         register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR,  T_PIPEPIPE,  8);
3694         register_expression_infix_parser(parse_conditional_expression, '?',      7);
3695         register_expression_infix_parser(parse_BINEXPR_ASSIGN,      '=',         2);
3696         register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL,  2);
3697         register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3698         register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3699                                                                 T_ASTERISKEQUAL, 2);
3700         register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3701         register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3702                                                                  T_PERCENTEQUAL, 2);
3703         register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3704                                                                 T_LESSLESSEQUAL, 2);
3705         register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3706                                                           T_GREATERGREATEREQUAL, 2);
3707         register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3708                                                                      T_ANDEQUAL, 2);
3709         register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3710                                                                     T_PIPEEQUAL, 2);
3711         register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3712                                                                    T_CARETEQUAL, 2);
3713
3714         register_expression_infix_parser(parse_BINEXPR_COMMA,       ',',         1);
3715
3716         register_expression_infix_parser(parse_array_expression,        '[',    30);
3717         register_expression_infix_parser(parse_call_expression,         '(',    30);
3718         register_expression_infix_parser(parse_select_expression,       '.',    30);
3719         register_expression_infix_parser(parse_select_expression,
3720                                                                 T_MINUSGREATER, 30);
3721         register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3722                                          T_PLUSPLUS, 30);
3723         register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3724                                          T_MINUSMINUS, 30);
3725
3726         register_expression_parser(parse_UNEXPR_NEGATE,           '-',          25);
3727         register_expression_parser(parse_UNEXPR_PLUS,             '+',          25);
3728         register_expression_parser(parse_UNEXPR_NOT,              '!',          25);
3729         register_expression_parser(parse_UNEXPR_BITWISE_NEGATE,   '~',          25);
3730         register_expression_parser(parse_UNEXPR_DEREFERENCE,      '*',          25);
3731         register_expression_parser(parse_UNEXPR_TAKE_ADDRESS,     '&',          25);
3732         register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS,   25);
3733         register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3734         register_expression_parser(parse_sizeof,                  T_sizeof,     25);
3735         register_expression_parser(parse_extension,            T___extension__, 25);
3736         register_expression_parser(parse_builtin_classify_type,
3737                                                      T___builtin_classify_type, 25);
3738 }
3739
3740
3741 static statement_t *parse_case_statement(void)
3742 {
3743         eat(T_case);
3744         case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3745         label->statement.type            = STATEMENT_CASE_LABEL;
3746         label->statement.source_position = token.source_position;
3747
3748         label->expression = parse_expression();
3749
3750         expect(':');
3751         label->statement.next = parse_statement();
3752
3753         return (statement_t*) label;
3754 }
3755
3756 static statement_t *parse_default_statement(void)
3757 {
3758         eat(T_default);
3759
3760         case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3761         label->statement.type            = STATEMENT_CASE_LABEL;
3762         label->statement.source_position = token.source_position;
3763
3764         expect(':');
3765         label->statement.next = parse_statement();
3766
3767         return (statement_t*) label;
3768 }
3769
3770 static declaration_t *get_label(symbol_t *symbol)
3771 {
3772         declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3773         assert(current_function != NULL);
3774         /* if we found a label in the same function, then we already created the
3775          * declaration */
3776         if(candidate != NULL
3777                         && candidate->parent_context == &current_function->context) {
3778                 return candidate;
3779         }
3780
3781         /* otherwise we need to create a new one */
3782         declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3783         declaration->namespc     = NAMESPACE_LABEL;
3784         declaration->symbol        = symbol;
3785
3786         label_push(declaration);
3787
3788         return declaration;
3789 }
3790
3791 static statement_t *parse_label_statement(void)
3792 {
3793         assert(token.type == T_IDENTIFIER);
3794         symbol_t *symbol = token.v.symbol;
3795         next_token();
3796
3797         declaration_t *label = get_label(symbol);
3798
3799         /* if source position is already set then the label is defined twice,
3800          * otherwise it was just mentioned in a goto so far */
3801         if(label->source_position.input_name != NULL) {
3802                 parser_print_error_prefix();
3803                 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3804                 parser_print_error_prefix_pos(label->source_position);
3805                 fprintf(stderr, "previous definition of '%s' was here\n",
3806                         symbol->string);
3807         } else {
3808                 label->source_position = token.source_position;
3809         }
3810
3811         label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
3812
3813         label_statement->statement.type            = STATEMENT_LABEL;
3814         label_statement->statement.source_position = token.source_position;
3815         label_statement->label                     = label;
3816
3817         expect(':');
3818
3819         if(token.type == '}') {
3820                 parse_error("label at end of compound statement");
3821                 return (statement_t*) label_statement;
3822         } else {
3823                 label_statement->label_statement = parse_statement();
3824         }
3825
3826         return (statement_t*) label_statement;
3827 }
3828
3829 static statement_t *parse_if(void)
3830 {
3831         eat(T_if);
3832
3833         if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3834         statement->statement.type            = STATEMENT_IF;
3835         statement->statement.source_position = token.source_position;
3836
3837         expect('(');
3838         statement->condition = parse_expression();
3839         expect(')');
3840
3841         statement->true_statement = parse_statement();
3842         if(token.type == T_else) {
3843                 next_token();
3844                 statement->false_statement = parse_statement();
3845         }
3846
3847         return (statement_t*) statement;
3848 }
3849
3850 static statement_t *parse_switch(void)
3851 {
3852         eat(T_switch);
3853
3854         switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3855         statement->statement.type            = STATEMENT_SWITCH;
3856         statement->statement.source_position = token.source_position;
3857
3858         expect('(');
3859         statement->expression = parse_expression();
3860         expect(')');
3861         statement->body = parse_statement();
3862
3863         return (statement_t*) statement;
3864 }
3865
3866 static statement_t *parse_while(void)
3867 {
3868         eat(T_while);
3869
3870         while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3871         statement->statement.type            = STATEMENT_WHILE;
3872         statement->statement.source_position = token.source_position;
3873
3874         expect('(');
3875         statement->condition = parse_expression();
3876         expect(')');
3877         statement->body = parse_statement();
3878
3879         return (statement_t*) statement;
3880 }
3881
3882 static statement_t *parse_do(void)
3883 {
3884         eat(T_do);
3885
3886         do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3887         statement->statement.type            = STATEMENT_DO_WHILE;
3888         statement->statement.source_position = token.source_position;
3889
3890         statement->body = parse_statement();
3891         expect(T_while);
3892         expect('(');
3893         statement->condition = parse_expression();
3894         expect(')');
3895         expect(';');
3896
3897         return (statement_t*) statement;
3898 }
3899
3900 static statement_t *parse_for(void)
3901 {
3902         eat(T_for);
3903
3904         for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3905         statement->statement.type            = STATEMENT_FOR;
3906         statement->statement.source_position = token.source_position;
3907
3908         expect('(');
3909
3910         int         top          = environment_top();
3911         context_t  *last_context = context;
3912         set_context(&statement->context);
3913
3914         if(token.type != ';') {
3915                 if(is_declaration_specifier(&token, false)) {
3916                         parse_declaration();
3917                 } else {
3918                         statement->initialisation = parse_expression();
3919                         expect(';');
3920                 }
3921         } else {
3922                 expect(';');
3923         }
3924
3925         if(token.type != ';') {
3926                 statement->condition = parse_expression();
3927         }
3928         expect(';');
3929         if(token.type != ')') {
3930                 statement->step = parse_expression();
3931         }
3932         expect(')');
3933         statement->body = parse_statement();
3934
3935         assert(context == &statement->context);
3936         set_context(last_context);
3937         environment_pop_to(top);
3938
3939         return (statement_t*) statement;
3940 }
3941
3942 static statement_t *parse_goto(void)
3943 {
3944         eat(T_goto);
3945
3946         if(token.type != T_IDENTIFIER) {
3947                 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
3948                 eat_statement();
3949                 return NULL;
3950         }
3951         symbol_t *symbol = token.v.symbol;
3952         next_token();
3953
3954         declaration_t *label = get_label(symbol);
3955
3956         goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3957
3958         statement->statement.type            = STATEMENT_GOTO;
3959         statement->statement.source_position = token.source_position;
3960
3961         statement->label = label;
3962
3963         expect(';');
3964
3965         return (statement_t*) statement;
3966 }
3967
3968 static statement_t *parse_continue(void)
3969 {
3970         eat(T_continue);
3971         expect(';');
3972
3973         statement_t *statement     = allocate_ast_zero(sizeof(statement[0]));
3974         statement->type            = STATEMENT_CONTINUE;
3975         statement->source_position = token.source_position;
3976
3977         return statement;
3978 }
3979
3980 static statement_t *parse_break(void)
3981 {
3982         eat(T_break);
3983         expect(';');
3984
3985         statement_t *statement     = allocate_ast_zero(sizeof(statement[0]));
3986         statement->type            = STATEMENT_BREAK;
3987         statement->source_position = token.source_position;
3988
3989         return statement;
3990 }
3991
3992 static statement_t *parse_return(void)
3993 {
3994         eat(T_return);
3995
3996         return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3997
3998         statement->statement.type            = STATEMENT_RETURN;
3999         statement->statement.source_position = token.source_position;
4000
4001         assert(current_function->type->type == TYPE_FUNCTION);
4002         function_type_t *function_type = (function_type_t*) current_function->type;
4003         type_t          *return_type   = function_type->result_type;
4004
4005         expression_t *return_value;
4006         if(token.type != ';') {
4007                 return_value = parse_expression();
4008
4009                 if(return_type == type_void && return_value->datatype != type_void) {
4010                         parse_warning("'return' with a value, in function returning void");
4011                         return_value = NULL;
4012                 } else {
4013                         if(return_type != NULL) {
4014                                 semantic_assign(return_type, &return_value, "'return'");
4015                         }
4016                 }
4017         } else {
4018                 return_value = NULL;
4019                 if(return_type != type_void) {
4020                         parse_warning("'return' without value, in function returning "
4021                                       "non-void");
4022                 }
4023         }
4024         statement->return_value = return_value;
4025
4026         expect(';');
4027
4028         return (statement_t*) statement;
4029 }
4030
4031 static statement_t *parse_declaration_statement(void)
4032 {
4033         declaration_t *before = last_declaration;
4034
4035         declaration_statement_t *statement
4036                 = allocate_ast_zero(sizeof(statement[0]));
4037         statement->statement.type            = STATEMENT_DECLARATION;
4038         statement->statement.source_position = token.source_position;
4039
4040         declaration_specifiers_t specifiers;
4041         memset(&specifiers, 0, sizeof(specifiers));
4042         parse_declaration_specifiers(&specifiers);
4043
4044         if(token.type == ';') {
4045                 eat(';');
4046         } else {
4047                 parse_init_declarators(&specifiers);
4048         }
4049
4050         if(before == NULL) {
4051                 statement->declarations_begin = context->declarations;
4052         } else {
4053                 statement->declarations_begin = before->next;
4054         }
4055         statement->declarations_end = last_declaration;
4056
4057         return (statement_t*) statement;
4058 }
4059
4060 static statement_t *parse_expression_statement(void)
4061 {
4062         expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4063         statement->statement.type            = STATEMENT_EXPRESSION;
4064         statement->statement.source_position = token.source_position;
4065
4066         statement->expression = parse_expression();
4067
4068         expect(';');
4069
4070         return (statement_t*) statement;
4071 }
4072
4073 static statement_t *parse_statement(void)
4074 {
4075         statement_t   *statement = NULL;
4076
4077         /* declaration or statement */
4078         switch(token.type) {
4079         case T_case:
4080                 statement = parse_case_statement();
4081                 break;
4082
4083         case T_default:
4084                 statement = parse_default_statement();
4085                 break;
4086
4087         case '{':
4088                 statement = parse_compound_statement();
4089                 break;
4090
4091         case T_if:
4092                 statement = parse_if();
4093                 break;
4094
4095         case T_switch:
4096                 statement = parse_switch();
4097                 break;
4098
4099         case T_while:
4100                 statement = parse_while();
4101                 break;
4102
4103         case T_do:
4104                 statement = parse_do();
4105                 break;
4106
4107         case T_for:
4108                 statement = parse_for();
4109                 break;
4110
4111         case T_goto:
4112                 statement = parse_goto();
4113                 break;
4114
4115         case T_continue:
4116                 statement = parse_continue();
4117                 break;
4118
4119         case T_break:
4120                 statement = parse_break();
4121                 break;
4122
4123         case T_return:
4124                 statement = parse_return();
4125                 break;
4126
4127         case ';':
4128                 next_token();
4129                 statement = NULL;
4130                 break;
4131
4132         case T_IDENTIFIER:
4133                 if(look_ahead(1)->type == ':') {
4134                         statement = parse_label_statement();
4135                         break;
4136                 }
4137
4138                 if(is_typedef_symbol(token.v.symbol)) {
4139                         statement = parse_declaration_statement();
4140                         break;
4141                 }
4142
4143                 statement = parse_expression_statement();
4144                 break;
4145
4146         case T___extension__:
4147                 /* this can be a prefix to a declaration or an expression statement */
4148                 /* we simply eat it now and parse the rest with tail recursion */
4149                 do {
4150                         next_token();
4151                 } while(token.type == T___extension__);
4152                 statement = parse_statement();
4153                 break;
4154
4155         DECLARATION_START
4156                 statement = parse_declaration_statement();
4157                 break;
4158
4159         default:
4160                 statement = parse_expression_statement();
4161                 break;
4162         }
4163
4164         assert(statement == NULL || statement->source_position.input_name != NULL);
4165
4166         return statement;
4167 }
4168
4169 static statement_t *parse_compound_statement(void)
4170 {
4171         compound_statement_t *compound_statement
4172                 = allocate_ast_zero(sizeof(compound_statement[0]));
4173         compound_statement->statement.type            = STATEMENT_COMPOUND;
4174         compound_statement->statement.source_position = token.source_position;
4175
4176         eat('{');
4177
4178         int        top          = environment_top();
4179         context_t *last_context = context;
4180         set_context(&compound_statement->context);
4181
4182         statement_t *last_statement = NULL;
4183
4184         while(token.type != '}' && token.type != T_EOF) {
4185                 statement_t *statement = parse_statement();
4186                 if(statement == NULL)
4187                         continue;
4188
4189                 if(last_statement != NULL) {
4190                         last_statement->next = statement;
4191                 } else {
4192                         compound_statement->statements = statement;
4193                 }
4194
4195                 while(statement->next != NULL)
4196                         statement = statement->next;
4197
4198                 last_statement = statement;
4199         }
4200
4201         if(token.type != '}') {
4202                 parser_print_error_prefix_pos(
4203                                 compound_statement->statement.source_position);
4204                 fprintf(stderr, "end of file while looking for closing '}'\n");
4205         }
4206         next_token();
4207
4208         assert(context == &compound_statement->context);
4209         set_context(last_context);
4210         environment_pop_to(top);
4211
4212         return (statement_t*) compound_statement;
4213 }
4214
4215 static translation_unit_t *parse_translation_unit(void)
4216 {
4217         translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4218
4219         assert(global_context == NULL);
4220         global_context = &unit->context;
4221
4222         assert(context == NULL);
4223         set_context(&unit->context);
4224
4225         while(token.type != T_EOF) {
4226                 parse_declaration();
4227         }
4228
4229         assert(context == &unit->context);
4230         context          = NULL;
4231         last_declaration = NULL;
4232
4233         assert(global_context == &unit->context);
4234         global_context = NULL;
4235
4236         return unit;
4237 }
4238
4239 translation_unit_t *parse(void)
4240 {
4241         environment_stack = NEW_ARR_F(stack_entry_t, 0);
4242         label_stack       = NEW_ARR_F(stack_entry_t, 0);
4243         found_error       = false;
4244
4245         type_set_output(stderr);
4246         ast_set_output(stderr);
4247
4248         lookahead_bufpos = 0;
4249         for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4250                 next_token();
4251         }
4252         translation_unit_t *unit = parse_translation_unit();
4253
4254         DEL_ARR_F(environment_stack);
4255         DEL_ARR_F(label_stack);
4256
4257         if(found_error)
4258                 return NULL;
4259
4260         return unit;
4261 }
4262
4263 void init_parser(void)
4264 {
4265         init_expression_parsers();
4266         obstack_init(&temp_obst);
4267
4268         type_int         = make_atomic_type(ATOMIC_TYPE_INT, 0);
4269         type_uint        = make_atomic_type(ATOMIC_TYPE_UINT, 0);
4270         type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
4271         type_double      = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
4272         type_float       = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
4273         type_size_t      = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
4274         type_ptrdiff_t   = make_atomic_type(ATOMIC_TYPE_LONG, 0);
4275         type_const_char  = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4276         type_void        = make_atomic_type(ATOMIC_TYPE_VOID, 0);
4277         type_string      = make_pointer_type(type_const_char, 0);
4278 }
4279
4280 void exit_parser(void)
4281 {
4282         obstack_free(&temp_obst, NULL);
4283 }