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