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