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