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