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