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