Improve error messages slightly.
[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: '%T' <- '%T'",
826                context, 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,
1265                                 "initializer expression '%E' of type '%T' is incompatible with type '%T'",
1266                                 expression, expression->base.datatype, type);
1267                 }
1268                 return initializer;
1269         }
1270
1271         if(is_type_scalar(type)) {
1272                 /* Â§ 6.7.8.11 */
1273                 eat('{');
1274
1275                 expression_t *expression = parse_assignment_expression();
1276                 result = initializer_from_expression(type, expression);
1277
1278                 if(token.type == ',')
1279                         next_token();
1280
1281                 expect('}');
1282                 return result;
1283         } else {
1284                 result = parse_sub_initializer(type, NULL, NULL);
1285         }
1286
1287         return result;
1288 }
1289
1290 static declaration_t *append_declaration(declaration_t *declaration);
1291
1292 static declaration_t *parse_compound_type_specifier(bool is_struct)
1293 {
1294         if(is_struct) {
1295                 eat(T_struct);
1296         } else {
1297                 eat(T_union);
1298         }
1299
1300         symbol_t      *symbol      = NULL;
1301         declaration_t *declaration = NULL;
1302
1303         if (token.type == T___attribute__) {
1304                 /* TODO */
1305                 parse_attributes();
1306         }
1307
1308         if(token.type == T_IDENTIFIER) {
1309                 symbol = token.v.symbol;
1310                 next_token();
1311
1312                 if(is_struct) {
1313                         declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1314                 } else {
1315                         declaration = get_declaration(symbol, NAMESPACE_UNION);
1316                 }
1317         } else if(token.type != '{') {
1318                 if(is_struct) {
1319                         parse_error_expected("while parsing struct type specifier",
1320                                              T_IDENTIFIER, '{', 0);
1321                 } else {
1322                         parse_error_expected("while parsing union type specifier",
1323                                              T_IDENTIFIER, '{', 0);
1324                 }
1325
1326                 return NULL;
1327         }
1328
1329         if(declaration == NULL) {
1330                 declaration = allocate_ast_zero(sizeof(declaration[0]));
1331
1332                 if(is_struct) {
1333                         declaration->namespc = NAMESPACE_STRUCT;
1334                 } else {
1335                         declaration->namespc = NAMESPACE_UNION;
1336                 }
1337                 declaration->source_position = token.source_position;
1338                 declaration->symbol          = symbol;
1339                 declaration->parent_context  = context;
1340                 if (symbol != NULL) {
1341                         environment_push(declaration);
1342                 }
1343                 append_declaration(declaration);
1344         }
1345
1346         if(token.type == '{') {
1347                 if(declaration->init.is_defined) {
1348                         assert(symbol != NULL);
1349                         errorf(HERE, "multiple definition of '%s %Y'",
1350                                is_struct ? "struct" : "union", symbol);
1351                         declaration->context.declarations = NULL;
1352                 }
1353                 declaration->init.is_defined = true;
1354
1355                 int         top          = environment_top();
1356                 context_t  *last_context = context;
1357                 set_context(&declaration->context);
1358
1359                 parse_compound_type_entries();
1360                 parse_attributes();
1361
1362                 assert(context == &declaration->context);
1363                 set_context(last_context);
1364                 environment_pop_to(top);
1365         }
1366
1367         return declaration;
1368 }
1369
1370 static void parse_enum_entries(enum_type_t *const enum_type)
1371 {
1372         eat('{');
1373
1374         if(token.type == '}') {
1375                 next_token();
1376                 errorf(HERE, "empty enum not allowed");
1377                 return;
1378         }
1379
1380         do {
1381                 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1382
1383                 if(token.type != T_IDENTIFIER) {
1384                         parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1385                         eat_block();
1386                         return;
1387                 }
1388                 entry->storage_class   = STORAGE_CLASS_ENUM_ENTRY;
1389                 entry->type            = (type_t*) enum_type;
1390                 entry->symbol          = token.v.symbol;
1391                 entry->source_position = token.source_position;
1392                 next_token();
1393
1394                 if(token.type == '=') {
1395                         next_token();
1396                         entry->init.enum_value = parse_constant_expression();
1397
1398                         /* TODO semantic */
1399                 }
1400
1401                 record_declaration(entry);
1402
1403                 if(token.type != ',')
1404                         break;
1405                 next_token();
1406         } while(token.type != '}');
1407
1408         expect_void('}');
1409 }
1410
1411 static type_t *parse_enum_specifier(void)
1412 {
1413         eat(T_enum);
1414
1415         declaration_t *declaration;
1416         symbol_t      *symbol;
1417
1418         if(token.type == T_IDENTIFIER) {
1419                 symbol = token.v.symbol;
1420                 next_token();
1421
1422                 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1423         } else if(token.type != '{') {
1424                 parse_error_expected("while parsing enum type specifier",
1425                                      T_IDENTIFIER, '{', 0);
1426                 return NULL;
1427         } else {
1428                 declaration = NULL;
1429                 symbol      = NULL;
1430         }
1431
1432         if(declaration == NULL) {
1433                 declaration = allocate_ast_zero(sizeof(declaration[0]));
1434
1435                 declaration->namespc       = NAMESPACE_ENUM;
1436                 declaration->source_position = token.source_position;
1437                 declaration->symbol          = symbol;
1438                 declaration->parent_context  = context;
1439         }
1440
1441         type_t *const type      = allocate_type_zero(TYPE_ENUM);
1442         type->enumt.declaration = declaration;
1443
1444         if(token.type == '{') {
1445                 if(declaration->init.is_defined) {
1446                         errorf(HERE, "multiple definitions of enum %Y", symbol);
1447                 }
1448                 if (symbol != NULL) {
1449                         environment_push(declaration);
1450                 }
1451                 append_declaration(declaration);
1452                 declaration->init.is_defined = 1;
1453
1454                 parse_enum_entries(&type->enumt);
1455                 parse_attributes();
1456         }
1457
1458         return type;
1459 }
1460
1461 /**
1462  * if a symbol is a typedef to another type, return true
1463  */
1464 static bool is_typedef_symbol(symbol_t *symbol)
1465 {
1466         const declaration_t *const declaration =
1467                 get_declaration(symbol, NAMESPACE_NORMAL);
1468         return
1469                 declaration != NULL &&
1470                 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1471 }
1472
1473 static type_t *parse_typeof(void)
1474 {
1475         eat(T___typeof__);
1476
1477         type_t *type;
1478
1479         expect('(');
1480
1481         expression_t *expression  = NULL;
1482
1483 restart:
1484         switch(token.type) {
1485         case T___extension__:
1486                 /* this can be a prefix to a typename or an expression */
1487                 /* we simply eat it now. */
1488                 do {
1489                         next_token();
1490                 } while(token.type == T___extension__);
1491                 goto restart;
1492
1493         case T_IDENTIFIER:
1494                 if(is_typedef_symbol(token.v.symbol)) {
1495                         type = parse_typename();
1496                 } else {
1497                         expression = parse_expression();
1498                         type       = expression->base.datatype;
1499                 }
1500                 break;
1501
1502         TYPENAME_START
1503                 type = parse_typename();
1504                 break;
1505
1506         default:
1507                 expression = parse_expression();
1508                 type       = expression->base.datatype;
1509                 break;
1510         }
1511
1512         expect(')');
1513
1514         type_t *typeof_type              = allocate_type_zero(TYPE_TYPEOF);
1515         typeof_type->typeoft.expression  = expression;
1516         typeof_type->typeoft.typeof_type = type;
1517
1518         return typeof_type;
1519 }
1520
1521 typedef enum {
1522         SPECIFIER_SIGNED    = 1 << 0,
1523         SPECIFIER_UNSIGNED  = 1 << 1,
1524         SPECIFIER_LONG      = 1 << 2,
1525         SPECIFIER_INT       = 1 << 3,
1526         SPECIFIER_DOUBLE    = 1 << 4,
1527         SPECIFIER_CHAR      = 1 << 5,
1528         SPECIFIER_SHORT     = 1 << 6,
1529         SPECIFIER_LONG_LONG = 1 << 7,
1530         SPECIFIER_FLOAT     = 1 << 8,
1531         SPECIFIER_BOOL      = 1 << 9,
1532         SPECIFIER_VOID      = 1 << 10,
1533 #ifdef PROVIDE_COMPLEX
1534         SPECIFIER_COMPLEX   = 1 << 11,
1535         SPECIFIER_IMAGINARY = 1 << 12,
1536 #endif
1537 } specifiers_t;
1538
1539 static type_t *create_builtin_type(symbol_t *const symbol,
1540                                    type_t *const real_type)
1541 {
1542         type_t *type            = allocate_type_zero(TYPE_BUILTIN);
1543         type->builtin.symbol    = symbol;
1544         type->builtin.real_type = real_type;
1545
1546         type_t *result = typehash_insert(type);
1547         if (type != result) {
1548                 free_type(type);
1549         }
1550
1551         return result;
1552 }
1553
1554 static type_t *get_typedef_type(symbol_t *symbol)
1555 {
1556         declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1557         if(declaration == NULL
1558                         || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1559                 return NULL;
1560
1561         type_t *type               = allocate_type_zero(TYPE_TYPEDEF);
1562         type->typedeft.declaration = declaration;
1563
1564         return type;
1565 }
1566
1567 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1568 {
1569         type_t   *type            = NULL;
1570         unsigned  type_qualifiers = 0;
1571         unsigned  type_specifiers = 0;
1572         int       newtype         = 0;
1573
1574         specifiers->source_position = token.source_position;
1575
1576         while(true) {
1577                 switch(token.type) {
1578
1579                 /* storage class */
1580 #define MATCH_STORAGE_CLASS(token, class)                                \
1581                 case token:                                                      \
1582                         if(specifiers->storage_class != STORAGE_CLASS_NONE) {        \
1583                                 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1584                         }                                                            \
1585                         specifiers->storage_class = class;                           \
1586                         next_token();                                                \
1587                         break;
1588
1589                 MATCH_STORAGE_CLASS(T_typedef,  STORAGE_CLASS_TYPEDEF)
1590                 MATCH_STORAGE_CLASS(T_extern,   STORAGE_CLASS_EXTERN)
1591                 MATCH_STORAGE_CLASS(T_static,   STORAGE_CLASS_STATIC)
1592                 MATCH_STORAGE_CLASS(T_auto,     STORAGE_CLASS_AUTO)
1593                 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1594
1595                 case T___thread:
1596                         switch (specifiers->storage_class) {
1597                                 case STORAGE_CLASS_NONE:
1598                                         specifiers->storage_class = STORAGE_CLASS_THREAD;
1599                                         break;
1600
1601                                 case STORAGE_CLASS_EXTERN:
1602                                         specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1603                                         break;
1604
1605                                 case STORAGE_CLASS_STATIC:
1606                                         specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1607                                         break;
1608
1609                                 default:
1610                                         errorf(HERE, "multiple storage classes in declaration specifiers");
1611                                         break;
1612                         }
1613                         next_token();
1614                         break;
1615
1616                 /* type qualifiers */
1617 #define MATCH_TYPE_QUALIFIER(token, qualifier)                          \
1618                 case token:                                                     \
1619                         type_qualifiers |= qualifier;                               \
1620                         next_token();                                               \
1621                         break;
1622
1623                 MATCH_TYPE_QUALIFIER(T_const,    TYPE_QUALIFIER_CONST);
1624                 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1625                 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1626
1627                 case T___extension__:
1628                         /* TODO */
1629                         next_token();
1630                         break;
1631
1632                 /* type specifiers */
1633 #define MATCH_SPECIFIER(token, specifier, name)                         \
1634                 case token:                                                     \
1635                         next_token();                                               \
1636                         if(type_specifiers & specifier) {                           \
1637                                 errorf(HERE, "multiple " name " type specifiers given"); \
1638                         } else {                                                    \
1639                                 type_specifiers |= specifier;                           \
1640                         }                                                           \
1641                         break;
1642
1643                 MATCH_SPECIFIER(T_void,       SPECIFIER_VOID,      "void")
1644                 MATCH_SPECIFIER(T_char,       SPECIFIER_CHAR,      "char")
1645                 MATCH_SPECIFIER(T_short,      SPECIFIER_SHORT,     "short")
1646                 MATCH_SPECIFIER(T_int,        SPECIFIER_INT,       "int")
1647                 MATCH_SPECIFIER(T_float,      SPECIFIER_FLOAT,     "float")
1648                 MATCH_SPECIFIER(T_double,     SPECIFIER_DOUBLE,    "double")
1649                 MATCH_SPECIFIER(T_signed,     SPECIFIER_SIGNED,    "signed")
1650                 MATCH_SPECIFIER(T_unsigned,   SPECIFIER_UNSIGNED,  "unsigned")
1651                 MATCH_SPECIFIER(T__Bool,      SPECIFIER_BOOL,      "_Bool")
1652 #ifdef PROVIDE_COMPLEX
1653                 MATCH_SPECIFIER(T__Complex,   SPECIFIER_COMPLEX,   "_Complex")
1654                 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1655 #endif
1656                 case T_forceinline:
1657                         /* only in microsoft mode */
1658                         specifiers->decl_modifiers |= DM_FORCEINLINE;
1659
1660                 case T_inline:
1661                         next_token();
1662                         specifiers->is_inline = true;
1663                         break;
1664
1665                 case T_long:
1666                         next_token();
1667                         if(type_specifiers & SPECIFIER_LONG_LONG) {
1668                                 errorf(HERE, "multiple type specifiers given");
1669                         } else if(type_specifiers & SPECIFIER_LONG) {
1670                                 type_specifiers |= SPECIFIER_LONG_LONG;
1671                         } else {
1672                                 type_specifiers |= SPECIFIER_LONG;
1673                         }
1674                         break;
1675
1676                 /* TODO: if type != NULL for the following rules should issue
1677                  * an error */
1678                 case T_struct: {
1679                         type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1680
1681                         type->compound.declaration = parse_compound_type_specifier(true);
1682                         break;
1683                 }
1684                 case T_union: {
1685                         type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1686
1687                         type->compound.declaration = parse_compound_type_specifier(false);
1688                         break;
1689                 }
1690                 case T_enum:
1691                         type = parse_enum_specifier();
1692                         break;
1693                 case T___typeof__:
1694                         type = parse_typeof();
1695                         break;
1696                 case T___builtin_va_list:
1697                         type = duplicate_type(type_valist);
1698                         next_token();
1699                         break;
1700
1701                 case T___attribute__:
1702                         /* TODO */
1703                         parse_attributes();
1704                         break;
1705
1706                 case T_IDENTIFIER: {
1707                         type_t *typedef_type = get_typedef_type(token.v.symbol);
1708
1709                         if(typedef_type == NULL)
1710                                 goto finish_specifiers;
1711
1712                         next_token();
1713                         type = typedef_type;
1714                         break;
1715                 }
1716
1717                 /* function specifier */
1718                 default:
1719                         goto finish_specifiers;
1720                 }
1721         }
1722
1723 finish_specifiers:
1724
1725         if(type == NULL) {
1726                 atomic_type_kind_t atomic_type;
1727
1728                 /* match valid basic types */
1729                 switch(type_specifiers) {
1730                 case SPECIFIER_VOID:
1731                         atomic_type = ATOMIC_TYPE_VOID;
1732                         break;
1733                 case SPECIFIER_CHAR:
1734                         atomic_type = ATOMIC_TYPE_CHAR;
1735                         break;
1736                 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1737                         atomic_type = ATOMIC_TYPE_SCHAR;
1738                         break;
1739                 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1740                         atomic_type = ATOMIC_TYPE_UCHAR;
1741                         break;
1742                 case SPECIFIER_SHORT:
1743                 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1744                 case SPECIFIER_SHORT | SPECIFIER_INT:
1745                 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1746                         atomic_type = ATOMIC_TYPE_SHORT;
1747                         break;
1748                 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1749                 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1750                         atomic_type = ATOMIC_TYPE_USHORT;
1751                         break;
1752                 case SPECIFIER_INT:
1753                 case SPECIFIER_SIGNED:
1754                 case SPECIFIER_SIGNED | SPECIFIER_INT:
1755                         atomic_type = ATOMIC_TYPE_INT;
1756                         break;
1757                 case SPECIFIER_UNSIGNED:
1758                 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1759                         atomic_type = ATOMIC_TYPE_UINT;
1760                         break;
1761                 case SPECIFIER_LONG:
1762                 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1763                 case SPECIFIER_LONG | SPECIFIER_INT:
1764                 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1765                         atomic_type = ATOMIC_TYPE_LONG;
1766                         break;
1767                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1768                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1769                         atomic_type = ATOMIC_TYPE_ULONG;
1770                         break;
1771                 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1772                 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1773                 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1774                 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1775                         | SPECIFIER_INT:
1776                         atomic_type = ATOMIC_TYPE_LONGLONG;
1777                         break;
1778                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1779                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1780                         | SPECIFIER_INT:
1781                         atomic_type = ATOMIC_TYPE_ULONGLONG;
1782                         break;
1783                 case SPECIFIER_FLOAT:
1784                         atomic_type = ATOMIC_TYPE_FLOAT;
1785                         break;
1786                 case SPECIFIER_DOUBLE:
1787                         atomic_type = ATOMIC_TYPE_DOUBLE;
1788                         break;
1789                 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1790                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1791                         break;
1792                 case SPECIFIER_BOOL:
1793                         atomic_type = ATOMIC_TYPE_BOOL;
1794                         break;
1795 #ifdef PROVIDE_COMPLEX
1796                 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1797                         atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1798                         break;
1799                 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1800                         atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1801                         break;
1802                 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1803                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1804                         break;
1805                 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1806                         atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1807                         break;
1808                 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1809                         atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1810                         break;
1811                 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1812                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1813                         break;
1814 #endif
1815                 default:
1816                         /* invalid specifier combination, give an error message */
1817                         if(type_specifiers == 0) {
1818                                 if (! strict_mode) {
1819                                         warningf(HERE, "no type specifiers in declaration, using int");
1820                                         atomic_type = ATOMIC_TYPE_INT;
1821                                         break;
1822                                 } else {
1823                                         errorf(HERE, "no type specifiers given in declaration");
1824                                 }
1825                         } else if((type_specifiers & SPECIFIER_SIGNED) &&
1826                                   (type_specifiers & SPECIFIER_UNSIGNED)) {
1827                                 errorf(HERE, "signed and unsigned specifiers gives");
1828                         } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1829                                 errorf(HERE, "only integer types can be signed or unsigned");
1830                         } else {
1831                                 errorf(HERE, "multiple datatypes in declaration");
1832                         }
1833                         atomic_type = ATOMIC_TYPE_INVALID;
1834                 }
1835
1836                 type               = allocate_type_zero(TYPE_ATOMIC);
1837                 type->atomic.akind = atomic_type;
1838                 newtype            = 1;
1839         } else {
1840                 if(type_specifiers != 0) {
1841                         errorf(HERE, "multiple datatypes in declaration");
1842                 }
1843         }
1844
1845         type->base.qualifiers = type_qualifiers;
1846
1847         type_t *result = typehash_insert(type);
1848         if(newtype && result != type) {
1849                 free_type(type);
1850         }
1851
1852         specifiers->type = result;
1853 }
1854
1855 static type_qualifiers_t parse_type_qualifiers(void)
1856 {
1857         type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1858
1859         while(true) {
1860                 switch(token.type) {
1861                 /* type qualifiers */
1862                 MATCH_TYPE_QUALIFIER(T_const,    TYPE_QUALIFIER_CONST);
1863                 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1864                 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1865
1866                 default:
1867                         return type_qualifiers;
1868                 }
1869         }
1870 }
1871
1872 static declaration_t *parse_identifier_list(void)
1873 {
1874         declaration_t *declarations     = NULL;
1875         declaration_t *last_declaration = NULL;
1876         do {
1877                 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1878
1879                 declaration->source_position = token.source_position;
1880                 declaration->symbol          = token.v.symbol;
1881                 next_token();
1882
1883                 if(last_declaration != NULL) {
1884                         last_declaration->next = declaration;
1885                 } else {
1886                         declarations = declaration;
1887                 }
1888                 last_declaration = declaration;
1889
1890                 if(token.type != ',')
1891                         break;
1892                 next_token();
1893         } while(token.type == T_IDENTIFIER);
1894
1895         return declarations;
1896 }
1897
1898 static void semantic_parameter(declaration_t *declaration)
1899 {
1900         /* TODO: improve error messages */
1901
1902         if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1903                 errorf(HERE, "typedef not allowed in parameter list");
1904         } else if(declaration->storage_class != STORAGE_CLASS_NONE
1905                         && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1906                 errorf(HERE, "parameter may only have none or register storage class");
1907         }
1908
1909         type_t *orig_type = declaration->type;
1910         if(orig_type == NULL)
1911                 return;
1912         type_t *type = skip_typeref(orig_type);
1913
1914         /* Array as last part of a parameter type is just syntactic sugar.  Turn it
1915          * into a pointer. Â§ 6.7.5.3 (7) */
1916         if (is_type_array(type)) {
1917                 const array_type_t *arr_type     = &type->array;
1918                 type_t             *element_type = arr_type->element_type;
1919
1920                 type = make_pointer_type(element_type, type->base.qualifiers);
1921
1922                 declaration->type = type;
1923         }
1924
1925         if(is_type_incomplete(type)) {
1926                 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%Y'",
1927                        orig_type, declaration->symbol);
1928         }
1929 }
1930
1931 static declaration_t *parse_parameter(void)
1932 {
1933         declaration_specifiers_t specifiers;
1934         memset(&specifiers, 0, sizeof(specifiers));
1935
1936         parse_declaration_specifiers(&specifiers);
1937
1938         declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1939
1940         semantic_parameter(declaration);
1941
1942         return declaration;
1943 }
1944
1945 static declaration_t *parse_parameters(function_type_t *type)
1946 {
1947         if(token.type == T_IDENTIFIER) {
1948                 symbol_t *symbol = token.v.symbol;
1949                 if(!is_typedef_symbol(symbol)) {
1950                         type->kr_style_parameters = true;
1951                         return parse_identifier_list();
1952                 }
1953         }
1954
1955         if(token.type == ')') {
1956                 type->unspecified_parameters = 1;
1957                 return NULL;
1958         }
1959         if(token.type == T_void && look_ahead(1)->type == ')') {
1960                 next_token();
1961                 return NULL;
1962         }
1963
1964         declaration_t        *declarations = NULL;
1965         declaration_t        *declaration;
1966         declaration_t        *last_declaration = NULL;
1967         function_parameter_t *parameter;
1968         function_parameter_t *last_parameter = NULL;
1969
1970         while(true) {
1971                 switch(token.type) {
1972                 case T_DOTDOTDOT:
1973                         next_token();
1974                         type->variadic = 1;
1975                         return declarations;
1976
1977                 case T_IDENTIFIER:
1978                 case T___extension__:
1979                 DECLARATION_START
1980                         declaration = parse_parameter();
1981
1982                         parameter       = obstack_alloc(type_obst, sizeof(parameter[0]));
1983                         memset(parameter, 0, sizeof(parameter[0]));
1984                         parameter->type = declaration->type;
1985
1986                         if(last_parameter != NULL) {
1987                                 last_declaration->next = declaration;
1988                                 last_parameter->next   = parameter;
1989                         } else {
1990                                 type->parameters = parameter;
1991                                 declarations     = declaration;
1992                         }
1993                         last_parameter   = parameter;
1994                         last_declaration = declaration;
1995                         break;
1996
1997                 default:
1998                         return declarations;
1999                 }
2000                 if(token.type != ',')
2001                         return declarations;
2002                 next_token();
2003         }
2004 }
2005
2006 typedef enum {
2007         CONSTRUCT_INVALID,
2008         CONSTRUCT_POINTER,
2009         CONSTRUCT_FUNCTION,
2010         CONSTRUCT_ARRAY
2011 } construct_type_type_t;
2012
2013 typedef struct construct_type_t construct_type_t;
2014 struct construct_type_t {
2015         construct_type_type_t  type;
2016         construct_type_t      *next;
2017 };
2018
2019 typedef struct parsed_pointer_t parsed_pointer_t;
2020 struct parsed_pointer_t {
2021         construct_type_t  construct_type;
2022         type_qualifiers_t type_qualifiers;
2023 };
2024
2025 typedef struct construct_function_type_t construct_function_type_t;
2026 struct construct_function_type_t {
2027         construct_type_t  construct_type;
2028         type_t           *function_type;
2029 };
2030
2031 typedef struct parsed_array_t parsed_array_t;
2032 struct parsed_array_t {
2033         construct_type_t  construct_type;
2034         type_qualifiers_t type_qualifiers;
2035         bool              is_static;
2036         bool              is_variable;
2037         expression_t     *size;
2038 };
2039
2040 typedef struct construct_base_type_t construct_base_type_t;
2041 struct construct_base_type_t {
2042         construct_type_t  construct_type;
2043         type_t           *type;
2044 };
2045
2046 static construct_type_t *parse_pointer_declarator(void)
2047 {
2048         eat('*');
2049
2050         parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2051         memset(pointer, 0, sizeof(pointer[0]));
2052         pointer->construct_type.type = CONSTRUCT_POINTER;
2053         pointer->type_qualifiers     = parse_type_qualifiers();
2054
2055         return (construct_type_t*) pointer;
2056 }
2057
2058 static construct_type_t *parse_array_declarator(void)
2059 {
2060         eat('[');
2061
2062         parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2063         memset(array, 0, sizeof(array[0]));
2064         array->construct_type.type = CONSTRUCT_ARRAY;
2065
2066         if(token.type == T_static) {
2067                 array->is_static = true;
2068                 next_token();
2069         }
2070
2071         type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2072         if(type_qualifiers != 0) {
2073                 if(token.type == T_static) {
2074                         array->is_static = true;
2075                         next_token();
2076                 }
2077         }
2078         array->type_qualifiers = type_qualifiers;
2079
2080         if(token.type == '*' && look_ahead(1)->type == ']') {
2081                 array->is_variable = true;
2082                 next_token();
2083         } else if(token.type != ']') {
2084                 array->size = parse_assignment_expression();
2085         }
2086
2087         expect(']');
2088
2089         return (construct_type_t*) array;
2090 }
2091
2092 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2093 {
2094         eat('(');
2095
2096         type_t *type = allocate_type_zero(TYPE_FUNCTION);
2097
2098         declaration_t *parameters = parse_parameters(&type->function);
2099         if(declaration != NULL) {
2100                 declaration->context.declarations = parameters;
2101         }
2102
2103         construct_function_type_t *construct_function_type =
2104                 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2105         memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2106         construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2107         construct_function_type->function_type       = type;
2108
2109         expect(')');
2110
2111         return (construct_type_t*) construct_function_type;
2112 }
2113
2114 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2115                 bool may_be_abstract)
2116 {
2117         /* construct a single linked list of construct_type_t's which describe
2118          * how to construct the final declarator type */
2119         construct_type_t *first = NULL;
2120         construct_type_t *last  = NULL;
2121
2122         /* pointers */
2123         while(token.type == '*') {
2124                 construct_type_t *type = parse_pointer_declarator();
2125
2126                 if(last == NULL) {
2127                         first = type;
2128                         last  = type;
2129                 } else {
2130                         last->next = type;
2131                         last       = type;
2132                 }
2133         }
2134
2135         /* TODO: find out if this is correct */
2136         parse_attributes();
2137
2138         construct_type_t *inner_types = NULL;
2139
2140         switch(token.type) {
2141         case T_IDENTIFIER:
2142                 if(declaration == NULL) {
2143                         errorf(HERE, "no identifier expected in typename");
2144                 } else {
2145                         declaration->symbol          = token.v.symbol;
2146                         declaration->source_position = token.source_position;
2147                 }
2148                 next_token();
2149                 break;
2150         case '(':
2151                 next_token();
2152                 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2153                 expect(')');
2154                 break;
2155         default:
2156                 if(may_be_abstract)
2157                         break;
2158                 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2159                 /* avoid a loop in the outermost scope, because eat_statement doesn't
2160                  * eat '}' */
2161                 if(token.type == '}' && current_function == NULL) {
2162                         next_token();
2163                 } else {
2164                         eat_statement();
2165                 }
2166                 return NULL;
2167         }
2168
2169         construct_type_t *p = last;
2170
2171         while(true) {
2172                 construct_type_t *type;
2173                 switch(token.type) {
2174                 case '(':
2175                         type = parse_function_declarator(declaration);
2176                         break;
2177                 case '[':
2178                         type = parse_array_declarator();
2179                         break;
2180                 default:
2181                         goto declarator_finished;
2182                 }
2183
2184                 /* insert in the middle of the list (behind p) */
2185                 if(p != NULL) {
2186                         type->next = p->next;
2187                         p->next    = type;
2188                 } else {
2189                         type->next = first;
2190                         first      = type;
2191                 }
2192                 if(last == p) {
2193                         last = type;
2194                 }
2195         }
2196
2197 declarator_finished:
2198         parse_attributes();
2199
2200         /* append inner_types at the end of the list, we don't to set last anymore
2201          * as it's not needed anymore */
2202         if(last == NULL) {
2203                 assert(first == NULL);
2204                 first = inner_types;
2205         } else {
2206                 last->next = inner_types;
2207         }
2208
2209         return first;
2210 }
2211
2212 static type_t *construct_declarator_type(construct_type_t *construct_list,
2213                                          type_t *type)
2214 {
2215         construct_type_t *iter = construct_list;
2216         for( ; iter != NULL; iter = iter->next) {
2217                 switch(iter->type) {
2218                 case CONSTRUCT_INVALID:
2219                         panic("invalid type construction found");
2220                 case CONSTRUCT_FUNCTION: {
2221                         construct_function_type_t *construct_function_type
2222                                 = (construct_function_type_t*) iter;
2223
2224                         type_t *function_type = construct_function_type->function_type;
2225
2226                         function_type->function.return_type = type;
2227
2228                         type = function_type;
2229                         break;
2230                 }
2231
2232                 case CONSTRUCT_POINTER: {
2233                         parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2234                         type_t           *pointer_type   = allocate_type_zero(TYPE_POINTER);
2235                         pointer_type->pointer.points_to  = type;
2236                         pointer_type->base.qualifiers    = parsed_pointer->type_qualifiers;
2237
2238                         type = pointer_type;
2239                         break;
2240                 }
2241
2242                 case CONSTRUCT_ARRAY: {
2243                         parsed_array_t *parsed_array  = (parsed_array_t*) iter;
2244                         type_t         *array_type    = allocate_type_zero(TYPE_ARRAY);
2245
2246                         array_type->base.qualifiers    = parsed_array->type_qualifiers;
2247                         array_type->array.element_type = type;
2248                         array_type->array.is_static    = parsed_array->is_static;
2249                         array_type->array.is_variable  = parsed_array->is_variable;
2250                         array_type->array.size         = parsed_array->size;
2251
2252                         type = array_type;
2253                         break;
2254                 }
2255                 }
2256
2257                 type_t *hashed_type = typehash_insert(type);
2258                 if(hashed_type != type) {
2259                         /* the function type was constructed earlier freeing it here will
2260                          * destroy other types... */
2261                         if(iter->type != CONSTRUCT_FUNCTION) {
2262                                 free_type(type);
2263                         }
2264                         type = hashed_type;
2265                 }
2266         }
2267
2268         return type;
2269 }
2270
2271 static declaration_t *parse_declarator(
2272                 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2273 {
2274         type_t        *type         = specifiers->type;
2275         declaration_t *declaration  = allocate_ast_zero(sizeof(declaration[0]));
2276         declaration->storage_class  = specifiers->storage_class;
2277         declaration->modifiers      = specifiers->decl_modifiers;
2278         declaration->is_inline      = specifiers->is_inline;
2279
2280         construct_type_t *construct_type
2281                 = parse_inner_declarator(declaration, may_be_abstract);
2282         declaration->type = construct_declarator_type(construct_type, type);
2283
2284         if(construct_type != NULL) {
2285                 obstack_free(&temp_obst, construct_type);
2286         }
2287
2288         return declaration;
2289 }
2290
2291 static type_t *parse_abstract_declarator(type_t *base_type)
2292 {
2293         construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2294
2295         type_t *result = construct_declarator_type(construct_type, base_type);
2296         if(construct_type != NULL) {
2297                 obstack_free(&temp_obst, construct_type);
2298         }
2299
2300         return result;
2301 }
2302
2303 static declaration_t *append_declaration(declaration_t* const declaration)
2304 {
2305         if (last_declaration != NULL) {
2306                 last_declaration->next = declaration;
2307         } else {
2308                 context->declarations = declaration;
2309         }
2310         last_declaration = declaration;
2311         return declaration;
2312 }
2313
2314 static declaration_t *internal_record_declaration(
2315         declaration_t *const declaration,
2316         const bool is_function_definition)
2317 {
2318         const symbol_t *const symbol  = declaration->symbol;
2319         const namespace_t     namespc = (namespace_t)declaration->namespc;
2320
2321         const type_t *const type = skip_typeref(declaration->type);
2322         if (is_type_function(type) && type->function.unspecified_parameters) {
2323                 warningf(declaration->source_position,
2324                          "function declaration '%#T' is not a prototype",
2325                          type, declaration->symbol);
2326         }
2327
2328         declaration_t *const previous_declaration = get_declaration(symbol, namespc);
2329         assert(declaration != previous_declaration);
2330         if (previous_declaration != NULL
2331                         && previous_declaration->parent_context == context) {
2332                 /* can happen for K&R style declarations */
2333                 if(previous_declaration->type == NULL) {
2334                         previous_declaration->type = declaration->type;
2335                 }
2336
2337                 const type_t *const prev_type = skip_typeref(previous_declaration->type);
2338                 if (!types_compatible(type, prev_type)) {
2339                         errorf(declaration->source_position,
2340                                 "declaration '%#T' is incompatible with previous declaration '%#T'",
2341                                 type, symbol, previous_declaration->type, symbol);
2342                         errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2343                 } else {
2344                         unsigned old_storage_class = previous_declaration->storage_class;
2345                         unsigned new_storage_class = declaration->storage_class;
2346
2347                         /* pretend no storage class means extern for function declarations
2348                          * (except if the previous declaration is neither none nor extern) */
2349                         if (is_type_function(type)) {
2350                                 switch (old_storage_class) {
2351                                         case STORAGE_CLASS_NONE:
2352                                                 old_storage_class = STORAGE_CLASS_EXTERN;
2353
2354                                         case STORAGE_CLASS_EXTERN:
2355                                                 if (new_storage_class == STORAGE_CLASS_NONE && !is_function_definition) {
2356                                                         new_storage_class = STORAGE_CLASS_EXTERN;
2357                                                 }
2358                                                 break;
2359
2360                                         default: break;
2361                                 }
2362                         }
2363
2364                         if (old_storage_class == STORAGE_CLASS_EXTERN &&
2365                             new_storage_class == STORAGE_CLASS_EXTERN) {
2366 warn_redundant_declaration:
2367                                         warningf(declaration->source_position, "redundant declaration for '%Y'", symbol);
2368                                         warningf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2369                         } else if (current_function == NULL) {
2370                                 if (old_storage_class != STORAGE_CLASS_STATIC &&
2371                                     new_storage_class == STORAGE_CLASS_STATIC) {
2372                                         errorf(declaration->source_position, "static declaration of '%Y' follows non-static declaration", symbol);
2373                                         errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2374                                 } else {
2375                                         if (old_storage_class != STORAGE_CLASS_EXTERN) {
2376                                                 goto warn_redundant_declaration;
2377                                         }
2378                                         if (new_storage_class == STORAGE_CLASS_NONE) {
2379                                                 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2380                                         }
2381                                 }
2382                         } else {
2383                                 if (old_storage_class == new_storage_class) {
2384                                         errorf(declaration->source_position, "redeclaration of '%Y'", symbol);
2385                                 } else {
2386                                         errorf(declaration->source_position, "redeclaration of '%Y' with different linkage", symbol);
2387                                 }
2388                                 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2389                         }
2390                 }
2391                 return previous_declaration;
2392         }
2393
2394         assert(declaration->parent_context == NULL);
2395         assert(declaration->symbol != NULL);
2396         assert(context != NULL);
2397
2398         declaration->parent_context = context;
2399
2400         environment_push(declaration);
2401         return append_declaration(declaration);
2402 }
2403
2404 static declaration_t *record_declaration(declaration_t *declaration)
2405 {
2406         return internal_record_declaration(declaration, false);
2407 }
2408
2409 static declaration_t *record_function_definition(declaration_t *const declaration)
2410 {
2411         return internal_record_declaration(declaration, true);
2412 }
2413
2414 static void parser_error_multiple_definition(declaration_t *declaration,
2415                 const source_position_t source_position)
2416 {
2417         errorf(source_position, "multiple definition of symbol '%Y'",
2418                declaration->symbol);
2419         errorf(declaration->source_position,
2420                "this is the location of the previous definition.");
2421 }
2422
2423 static bool is_declaration_specifier(const token_t *token,
2424                                      bool only_type_specifiers)
2425 {
2426         switch(token->type) {
2427                 TYPE_SPECIFIERS
2428                         return true;
2429                 case T_IDENTIFIER:
2430                         return is_typedef_symbol(token->v.symbol);
2431
2432                 case T___extension__:
2433                 STORAGE_CLASSES
2434                 TYPE_QUALIFIERS
2435                         return !only_type_specifiers;
2436
2437                 default:
2438                         return false;
2439         }
2440 }
2441
2442 static void parse_init_declarator_rest(declaration_t *declaration)
2443 {
2444         eat('=');
2445
2446         type_t *orig_type = declaration->type;
2447         type_t *type      = NULL;
2448         if(orig_type != NULL)
2449                 type = skip_typeref(orig_type);
2450
2451         if(declaration->init.initializer != NULL) {
2452                 parser_error_multiple_definition(declaration, token.source_position);
2453         }
2454
2455         initializer_t *initializer = parse_initializer(type);
2456
2457         /* Â§ 6.7.5 (22)  array initializers for arrays with unknown size determine
2458          * the array type size */
2459         if(type != NULL && is_type_array(type) && initializer != NULL) {
2460                 array_type_t *array_type = &type->array;
2461
2462                 if(array_type->size == NULL) {
2463                         expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2464
2465                         cnst->base.datatype = type_size_t;
2466
2467                         switch (initializer->kind) {
2468                                 case INITIALIZER_LIST: {
2469                                         initializer_list_t *const list = &initializer->list;
2470                                         cnst->conste.v.int_value = list->len;
2471                                         break;
2472                                 }
2473
2474                                 case INITIALIZER_STRING: {
2475                                         initializer_string_t *const string = &initializer->string;
2476                                         cnst->conste.v.int_value = strlen(string->string) + 1;
2477                                         break;
2478                                 }
2479
2480                                 case INITIALIZER_WIDE_STRING: {
2481                                         initializer_wide_string_t *const string = &initializer->wide_string;
2482                                         cnst->conste.v.int_value = string->string.size;
2483                                         break;
2484                                 }
2485
2486                                 default:
2487                                         panic("invalid initializer type");
2488                         }
2489
2490                         array_type->size = cnst;
2491                 }
2492         }
2493
2494         if(type != NULL && is_type_function(type)) {
2495                 errorf(declaration->source_position,
2496                        "initializers not allowed for function types at declator '%Y' (type '%T')",
2497                        declaration->symbol, orig_type);
2498         } else {
2499                 declaration->init.initializer = initializer;
2500         }
2501 }
2502
2503 /* parse rest of a declaration without any declarator */
2504 static void parse_anonymous_declaration_rest(
2505                 const declaration_specifiers_t *specifiers,
2506                 parsed_declaration_func finished_declaration)
2507 {
2508         eat(';');
2509
2510         declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2511
2512         declaration->type            = specifiers->type;
2513         declaration->storage_class   = specifiers->storage_class;
2514         declaration->source_position = specifiers->source_position;
2515
2516         if (declaration->storage_class != STORAGE_CLASS_NONE) {
2517                 warningf(declaration->source_position, "useless storage class in empty declaration");
2518         }
2519
2520         type_t *type = declaration->type;
2521         switch (type->kind) {
2522                 case TYPE_COMPOUND_STRUCT:
2523                 case TYPE_COMPOUND_UNION: {
2524                         const compound_type_t *compound_type = &type->compound;
2525                         if (compound_type->declaration->symbol == NULL) {
2526                                 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2527                         }
2528                         break;
2529                 }
2530
2531                 case TYPE_ENUM:
2532                         break;
2533
2534                 default:
2535                         warningf(declaration->source_position, "empty declaration");
2536                         break;
2537         }
2538
2539         finished_declaration(declaration);
2540 }
2541
2542 static void parse_declaration_rest(declaration_t *ndeclaration,
2543                 const declaration_specifiers_t *specifiers,
2544                 parsed_declaration_func finished_declaration)
2545 {
2546         while(true) {
2547                 declaration_t *declaration = finished_declaration(ndeclaration);
2548
2549                 type_t *orig_type = declaration->type;
2550                 type_t *type      = skip_typeref(orig_type);
2551
2552                 if(type->kind != TYPE_FUNCTION && declaration->is_inline) {
2553                         warningf(declaration->source_position,
2554                                  "variable '%Y' declared 'inline'\n", declaration->symbol);
2555                 }
2556
2557                 if(token.type == '=') {
2558                         parse_init_declarator_rest(declaration);
2559                 }
2560
2561                 if(token.type != ',')
2562                         break;
2563                 eat(',');
2564
2565                 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2566         }
2567         expect_void(';');
2568 }
2569
2570 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2571 {
2572         symbol_t *symbol  = declaration->symbol;
2573         if(symbol == NULL) {
2574                 errorf(HERE, "anonymous declaration not valid as function parameter");
2575                 return declaration;
2576         }
2577         namespace_t namespc = (namespace_t) declaration->namespc;
2578         if(namespc != NAMESPACE_NORMAL) {
2579                 return record_declaration(declaration);
2580         }
2581
2582         declaration_t *previous_declaration = get_declaration(symbol, namespc);
2583         if(previous_declaration == NULL ||
2584                         previous_declaration->parent_context != context) {
2585                 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2586                        symbol);
2587                 return declaration;
2588         }
2589
2590         if(previous_declaration->type == NULL) {
2591                 previous_declaration->type           = declaration->type;
2592                 previous_declaration->storage_class  = declaration->storage_class;
2593                 previous_declaration->parent_context = context;
2594                 return previous_declaration;
2595         } else {
2596                 return record_declaration(declaration);
2597         }
2598 }
2599
2600 static void parse_declaration(parsed_declaration_func finished_declaration)
2601 {
2602         declaration_specifiers_t specifiers;
2603         memset(&specifiers, 0, sizeof(specifiers));
2604         parse_declaration_specifiers(&specifiers);
2605
2606         if(token.type == ';') {
2607                 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2608         } else {
2609                 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2610                 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2611         }
2612 }
2613
2614 static void parse_kr_declaration_list(declaration_t *declaration)
2615 {
2616         type_t *type = skip_typeref(declaration->type);
2617         if(!is_type_function(type))
2618                 return;
2619
2620         if(!type->function.kr_style_parameters)
2621                 return;
2622
2623         /* push function parameters */
2624         int         top          = environment_top();
2625         context_t  *last_context = context;
2626         set_context(&declaration->context);
2627
2628         declaration_t *parameter = declaration->context.declarations;
2629         for( ; parameter != NULL; parameter = parameter->next) {
2630                 assert(parameter->parent_context == NULL);
2631                 parameter->parent_context = context;
2632                 environment_push(parameter);
2633         }
2634
2635         /* parse declaration list */
2636         while(is_declaration_specifier(&token, false)) {
2637                 parse_declaration(finished_kr_declaration);
2638         }
2639
2640         /* pop function parameters */
2641         assert(context == &declaration->context);
2642         set_context(last_context);
2643         environment_pop_to(top);
2644
2645         /* update function type */
2646         type_t *new_type = duplicate_type(type);
2647         new_type->function.kr_style_parameters = false;
2648
2649         function_parameter_t *parameters     = NULL;
2650         function_parameter_t *last_parameter = NULL;
2651
2652         declaration_t *parameter_declaration = declaration->context.declarations;
2653         for( ; parameter_declaration != NULL;
2654                         parameter_declaration = parameter_declaration->next) {
2655                 type_t *parameter_type = parameter_declaration->type;
2656                 if(parameter_type == NULL) {
2657                         if (strict_mode) {
2658                                 errorf(HERE, "no type specified for function parameter '%Y'",
2659                                        parameter_declaration->symbol);
2660                         } else {
2661                                 warningf(HERE, "no type specified for function parameter '%Y', using int",
2662                                          parameter_declaration->symbol);
2663                                 parameter_type              = type_int;
2664                                 parameter_declaration->type = parameter_type;
2665                         }
2666                 }
2667
2668                 semantic_parameter(parameter_declaration);
2669                 parameter_type = parameter_declaration->type;
2670
2671                 function_parameter_t *function_parameter
2672                         = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2673                 memset(function_parameter, 0, sizeof(function_parameter[0]));
2674
2675                 function_parameter->type = parameter_type;
2676                 if(last_parameter != NULL) {
2677                         last_parameter->next = function_parameter;
2678                 } else {
2679                         parameters = function_parameter;
2680                 }
2681                 last_parameter = function_parameter;
2682         }
2683         new_type->function.parameters = parameters;
2684
2685         type = typehash_insert(new_type);
2686         if(type != new_type) {
2687                 obstack_free(type_obst, new_type);
2688         }
2689
2690         declaration->type = type;
2691 }
2692
2693 /**
2694  * Check if all labels are defined in the current function.
2695  */
2696 static void check_for_missing_labels(void)
2697 {
2698         bool first_err = true;
2699         for (const goto_statement_t *goto_statement = goto_first;
2700              goto_statement != NULL;
2701              goto_statement = goto_statement->next) {
2702                  const declaration_t *label = goto_statement->label;
2703
2704                  if (label->source_position.input_name == NULL) {
2705                          if (first_err) {
2706                                  first_err = false;
2707                                  diagnosticf("%s: In function '%Y':\n",
2708                                          current_function->source_position.input_name,
2709                                          current_function->symbol);
2710                          }
2711                          errorf(goto_statement->statement.source_position,
2712                                  "label '%Y' used but not defined", label->symbol);
2713                  }
2714         }
2715         goto_first = goto_last = NULL;
2716 }
2717
2718 static void parse_external_declaration(void)
2719 {
2720         /* function-definitions and declarations both start with declaration
2721          * specifiers */
2722         declaration_specifiers_t specifiers;
2723         memset(&specifiers, 0, sizeof(specifiers));
2724         parse_declaration_specifiers(&specifiers);
2725
2726         /* must be a declaration */
2727         if(token.type == ';') {
2728                 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2729                 return;
2730         }
2731
2732         /* declarator is common to both function-definitions and declarations */
2733         declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2734
2735         /* must be a declaration */
2736         if(token.type == ',' || token.type == '=' || token.type == ';') {
2737                 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2738                 return;
2739         }
2740
2741         /* must be a function definition */
2742         parse_kr_declaration_list(ndeclaration);
2743
2744         if(token.type != '{') {
2745                 parse_error_expected("while parsing function definition", '{', 0);
2746                 eat_statement();
2747                 return;
2748         }
2749
2750         type_t *type = ndeclaration->type;
2751         if(type == NULL) {
2752                 eat_block();
2753                 return;
2754         }
2755
2756         /* note that we don't skip typerefs: the standard doesn't allow them here
2757          * (so we can't use is_type_function here) */
2758         if(type->kind != TYPE_FUNCTION) {
2759                 errorf(HERE, "declarator '%#T' has a body but is not a function type",
2760                        type, ndeclaration->symbol);
2761                 eat_block();
2762                 return;
2763         }
2764
2765         /* Â§ 6.7.5.3 (14) a function definition with () means no
2766          * parameters (and not unspecified parameters) */
2767         if(type->function.unspecified_parameters) {
2768                 type_t *duplicate = duplicate_type(type);
2769                 duplicate->function.unspecified_parameters = false;
2770
2771                 type = typehash_insert(duplicate);
2772                 if(type != duplicate) {
2773                         obstack_free(type_obst, duplicate);
2774                 }
2775                 ndeclaration->type = type;
2776         }
2777
2778         declaration_t *const declaration = record_function_definition(ndeclaration);
2779         if(ndeclaration != declaration) {
2780                 declaration->context = ndeclaration->context;
2781         }
2782         type = skip_typeref(declaration->type);
2783
2784         /* push function parameters and switch context */
2785         int         top          = environment_top();
2786         context_t  *last_context = context;
2787         set_context(&declaration->context);
2788
2789         declaration_t *parameter = declaration->context.declarations;
2790         for( ; parameter != NULL; parameter = parameter->next) {
2791                 if(parameter->parent_context == &ndeclaration->context) {
2792                         parameter->parent_context = context;
2793                 }
2794                 assert(parameter->parent_context == NULL
2795                                 || parameter->parent_context == context);
2796                 parameter->parent_context = context;
2797                 environment_push(parameter);
2798         }
2799
2800         if(declaration->init.statement != NULL) {
2801                 parser_error_multiple_definition(declaration, token.source_position);
2802                 eat_block();
2803                 goto end_of_parse_external_declaration;
2804         } else {
2805                 /* parse function body */
2806                 int            label_stack_top      = label_top();
2807                 declaration_t *old_current_function = current_function;
2808                 current_function                    = declaration;
2809
2810                 declaration->init.statement = parse_compound_statement();
2811                 check_for_missing_labels();
2812
2813                 assert(current_function == declaration);
2814                 current_function = old_current_function;
2815                 label_pop_to(label_stack_top);
2816         }
2817
2818 end_of_parse_external_declaration:
2819         assert(context == &declaration->context);
2820         set_context(last_context);
2821         environment_pop_to(top);
2822 }
2823
2824 static type_t *make_bitfield_type(type_t *base, expression_t *size)
2825 {
2826         type_t *type        = allocate_type_zero(TYPE_BITFIELD);
2827         type->bitfield.base = base;
2828         type->bitfield.size = size;
2829
2830         return type;
2831 }
2832
2833 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2834 {
2835         /* TODO: check constraints for struct declarations (in specifiers) */
2836         while(1) {
2837                 declaration_t *declaration;
2838
2839                 if(token.type == ':') {
2840                         next_token();
2841
2842                         type_t *base_type = specifiers->type;
2843                         expression_t *size = parse_constant_expression();
2844
2845                         type_t *type = make_bitfield_type(base_type, size);
2846
2847                         declaration = allocate_ast_zero(sizeof(declaration[0]));
2848
2849                         declaration->namespc         = NAMESPACE_NORMAL;
2850                         declaration->storage_class   = STORAGE_CLASS_NONE;
2851                         declaration->source_position = token.source_position;
2852                         declaration->modifiers       = specifiers->decl_modifiers;
2853                         declaration->type            = type;
2854
2855                         record_declaration(declaration);
2856                 } else {
2857                         declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2858
2859                         if(token.type == ':') {
2860                                 next_token();
2861                                 expression_t *size = parse_constant_expression();
2862
2863                                 type_t *type = make_bitfield_type(declaration->type, size);
2864                                 declaration->type = type;
2865                         }
2866                 }
2867                 record_declaration(declaration);
2868
2869                 if(token.type != ',')
2870                         break;
2871                 next_token();
2872         }
2873         expect_void(';');
2874 }
2875
2876 static void parse_compound_type_entries(void)
2877 {
2878         eat('{');
2879
2880         while(token.type != '}' && token.type != T_EOF) {
2881                 declaration_specifiers_t specifiers;
2882                 memset(&specifiers, 0, sizeof(specifiers));
2883                 parse_declaration_specifiers(&specifiers);
2884
2885                 parse_struct_declarators(&specifiers);
2886         }
2887         if(token.type == T_EOF) {
2888                 errorf(HERE, "EOF while parsing struct");
2889         }
2890         next_token();
2891 }
2892
2893 static type_t *parse_typename(void)
2894 {
2895         declaration_specifiers_t specifiers;
2896         memset(&specifiers, 0, sizeof(specifiers));
2897         parse_declaration_specifiers(&specifiers);
2898         if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2899                 /* TODO: improve error message, user does probably not know what a
2900                  * storage class is...
2901                  */
2902                 errorf(HERE, "typename may not have a storage class");
2903         }
2904
2905         type_t *result = parse_abstract_declarator(specifiers.type);
2906
2907         return result;
2908 }
2909
2910
2911
2912
2913 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2914 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2915                                                           expression_t *left);
2916
2917 typedef struct expression_parser_function_t expression_parser_function_t;
2918 struct expression_parser_function_t {
2919         unsigned                         precedence;
2920         parse_expression_function        parser;
2921         unsigned                         infix_precedence;
2922         parse_expression_infix_function  infix_parser;
2923 };
2924
2925 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2926
2927 /**
2928  * Creates a new invalid expression.
2929  */
2930 static expression_t *create_invalid_expression(void)
2931 {
2932         expression_t *expression         = allocate_expression_zero(EXPR_INVALID);
2933         expression->base.source_position = token.source_position;
2934         return expression;
2935 }
2936
2937 static expression_t *expected_expression_error(void)
2938 {
2939         errorf(HERE, "expected expression, got token '%K'", &token);
2940
2941         next_token();
2942
2943         return create_invalid_expression();
2944 }
2945
2946 /**
2947  * Parse a string constant.
2948  */
2949 static expression_t *parse_string_const(void)
2950 {
2951         expression_t *cnst  = allocate_expression_zero(EXPR_STRING_LITERAL);
2952         cnst->base.datatype = type_string;
2953         cnst->string.value  = parse_string_literals();
2954
2955         return cnst;
2956 }
2957
2958 /**
2959  * Parse a wide string constant.
2960  */
2961 static expression_t *parse_wide_string_const(void)
2962 {
2963         expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2964         cnst->base.datatype      = type_wchar_t_ptr;
2965         cnst->wide_string.value  = token.v.wide_string; /* TODO concatenate */
2966         next_token();
2967         return cnst;
2968 }
2969
2970 /**
2971  * Parse an integer constant.
2972  */
2973 static expression_t *parse_int_const(void)
2974 {
2975         expression_t *cnst       = allocate_expression_zero(EXPR_CONST);
2976         cnst->base.datatype      = token.datatype;
2977         cnst->conste.v.int_value = token.v.intvalue;
2978
2979         next_token();
2980
2981         return cnst;
2982 }
2983
2984 /**
2985  * Parse a float constant.
2986  */
2987 static expression_t *parse_float_const(void)
2988 {
2989         expression_t *cnst         = allocate_expression_zero(EXPR_CONST);
2990         cnst->base.datatype        = token.datatype;
2991         cnst->conste.v.float_value = token.v.floatvalue;
2992
2993         next_token();
2994
2995         return cnst;
2996 }
2997
2998 static declaration_t *create_implicit_function(symbol_t *symbol,
2999                 const source_position_t source_position)
3000 {
3001         type_t *ntype                          = allocate_type_zero(TYPE_FUNCTION);
3002         ntype->function.return_type            = type_int;
3003         ntype->function.unspecified_parameters = true;
3004
3005         type_t *type = typehash_insert(ntype);
3006         if(type != ntype) {
3007                 free_type(ntype);
3008         }
3009
3010         declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3011
3012         declaration->storage_class   = STORAGE_CLASS_EXTERN;
3013         declaration->type            = type;
3014         declaration->symbol          = symbol;
3015         declaration->source_position = source_position;
3016         declaration->parent_context  = global_context;
3017
3018         context_t *old_context = context;
3019         set_context(global_context);
3020
3021         environment_push(declaration);
3022         /* prepend the declaration to the global declarations list */
3023         declaration->next     = context->declarations;
3024         context->declarations = declaration;
3025
3026         assert(context == global_context);
3027         set_context(old_context);
3028
3029         return declaration;
3030 }
3031
3032 /**
3033  * Creates a return_type (func)(argument_type) function type if not
3034  * already exists.
3035  *
3036  * @param return_type    the return type
3037  * @param argument_type  the argument type
3038  */
3039 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3040 {
3041         function_parameter_t *parameter
3042                 = obstack_alloc(type_obst, sizeof(parameter[0]));
3043         memset(parameter, 0, sizeof(parameter[0]));
3044         parameter->type = argument_type;
3045
3046         type_t *type               = allocate_type_zero(TYPE_FUNCTION);
3047         type->function.return_type = return_type;
3048         type->function.parameters  = parameter;
3049
3050         type_t *result = typehash_insert(type);
3051         if(result != type) {
3052                 free_type(type);
3053         }
3054
3055         return result;
3056 }
3057
3058 /**
3059  * Creates a function type for some function like builtins.
3060  *
3061  * @param symbol   the symbol describing the builtin
3062  */
3063 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3064 {
3065         switch(symbol->ID) {
3066         case T___builtin_alloca:
3067                 return make_function_1_type(type_void_ptr, type_size_t);
3068         case T___builtin_nan:
3069                 return make_function_1_type(type_double, type_string);
3070         case T___builtin_nanf:
3071                 return make_function_1_type(type_float, type_string);
3072         case T___builtin_nand:
3073                 return make_function_1_type(type_long_double, type_string);
3074         case T___builtin_va_end:
3075                 return make_function_1_type(type_void, type_valist);
3076         default:
3077                 panic("not implemented builtin symbol found");
3078         }
3079 }
3080
3081 /**
3082  * Performs automatic type cast as described in Â§ 6.3.2.1.
3083  *
3084  * @param orig_type  the original type
3085  */
3086 static type_t *automatic_type_conversion(type_t *orig_type)
3087 {
3088         if(orig_type == NULL)
3089                 return NULL;
3090
3091         type_t *type = skip_typeref(orig_type);
3092         if(is_type_array(type)) {
3093                 array_type_t *array_type   = &type->array;
3094                 type_t       *element_type = array_type->element_type;
3095                 unsigned      qualifiers   = array_type->type.qualifiers;
3096
3097                 return make_pointer_type(element_type, qualifiers);
3098         }
3099
3100         if(is_type_function(type)) {
3101                 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3102         }
3103
3104         return orig_type;
3105 }
3106
3107 /**
3108  * reverts the automatic casts of array to pointer types and function
3109  * to function-pointer types as defined Â§ 6.3.2.1
3110  */
3111 type_t *revert_automatic_type_conversion(const expression_t *expression)
3112 {
3113         if(expression->base.datatype == NULL)
3114                 return NULL;
3115
3116         switch(expression->kind) {
3117         case EXPR_REFERENCE: {
3118                 const reference_expression_t *ref = &expression->reference;
3119                 return ref->declaration->type;
3120         }
3121         case EXPR_SELECT: {
3122                 const select_expression_t *select = &expression->select;
3123                 return select->compound_entry->type;
3124         }
3125         case EXPR_UNARY_DEREFERENCE: {
3126                 expression_t   *value        = expression->unary.value;
3127                 type_t         *type         = skip_typeref(value->base.datatype);
3128                 pointer_type_t *pointer_type = &type->pointer;
3129
3130                 return pointer_type->points_to;
3131         }
3132         case EXPR_BUILTIN_SYMBOL: {
3133                 const builtin_symbol_expression_t *builtin
3134                         = &expression->builtin_symbol;
3135                 return get_builtin_symbol_type(builtin->symbol);
3136         }
3137         case EXPR_ARRAY_ACCESS: {
3138                 const array_access_expression_t *array_access
3139                         = &expression->array_access;
3140                 const expression_t *array_ref = array_access->array_ref;
3141                 type_t *type_left  = skip_typeref(array_ref->base.datatype);
3142                 assert(is_type_pointer(type_left));
3143                 pointer_type_t *pointer_type = &type_left->pointer;
3144                 return pointer_type->points_to;
3145         }
3146
3147         default:
3148                 break;
3149         }
3150
3151         return expression->base.datatype;
3152 }
3153
3154 static expression_t *parse_reference(void)
3155 {
3156         expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3157
3158         reference_expression_t *ref = &expression->reference;
3159         ref->symbol = token.v.symbol;
3160
3161         declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3162
3163         source_position_t source_position = token.source_position;
3164         next_token();
3165
3166         if(declaration == NULL) {
3167                 if (! strict_mode && token.type == '(') {
3168                         /* an implicitly defined function */
3169                         warningf(HERE, "implicit declaration of function '%Y'",
3170                                  ref->symbol);
3171
3172                         declaration = create_implicit_function(ref->symbol,
3173                                                                source_position);
3174                 } else {
3175                         errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3176                         return expression;
3177                 }
3178         }
3179
3180         type_t *type         = declaration->type;
3181
3182         /* we always do the auto-type conversions; the & and sizeof parser contains
3183          * code to revert this! */
3184         type = automatic_type_conversion(type);
3185
3186         ref->declaration         = declaration;
3187         ref->expression.datatype = type;
3188
3189         return expression;
3190 }
3191
3192 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3193 {
3194         (void) expression;
3195         (void) dest_type;
3196         /* TODO check if explicit cast is allowed and issue warnings/errors */
3197 }
3198
3199 static expression_t *parse_cast(void)
3200 {
3201         expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3202
3203         cast->base.source_position = token.source_position;
3204
3205         type_t *type  = parse_typename();
3206
3207         expect(')');
3208         expression_t *value = parse_sub_expression(20);
3209
3210         check_cast_allowed(value, type);
3211
3212         cast->base.datatype = type;
3213         cast->unary.value   = value;
3214
3215         return cast;
3216 }
3217
3218 static expression_t *parse_statement_expression(void)
3219 {
3220         expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3221
3222         statement_t *statement          = parse_compound_statement();
3223         expression->statement.statement = statement;
3224         if(statement == NULL) {
3225                 expect(')');
3226                 return NULL;
3227         }
3228
3229         assert(statement->kind == STATEMENT_COMPOUND);
3230         compound_statement_t *compound_statement = &statement->compound;
3231
3232         /* find last statement and use it's type */
3233         const statement_t *last_statement = NULL;
3234         const statement_t *iter           = compound_statement->statements;
3235         for( ; iter != NULL; iter = iter->base.next) {
3236                 last_statement = iter;
3237         }
3238
3239         if(last_statement->kind == STATEMENT_EXPRESSION) {
3240                 const expression_statement_t *expression_statement
3241                         = &last_statement->expression;
3242                 expression->base.datatype
3243                         = expression_statement->expression->base.datatype;
3244         } else {
3245                 expression->base.datatype = type_void;
3246         }
3247
3248         expect(')');
3249
3250         return expression;
3251 }
3252
3253 static expression_t *parse_brace_expression(void)
3254 {
3255         eat('(');
3256
3257         switch(token.type) {
3258         case '{':
3259                 /* gcc extension: a statement expression */
3260                 return parse_statement_expression();
3261
3262         TYPE_QUALIFIERS
3263         TYPE_SPECIFIERS
3264                 return parse_cast();
3265         case T_IDENTIFIER:
3266                 if(is_typedef_symbol(token.v.symbol)) {
3267                         return parse_cast();
3268                 }
3269         }
3270
3271         expression_t *result = parse_expression();
3272         expect(')');
3273
3274         return result;
3275 }
3276
3277 static expression_t *parse_function_keyword(void)
3278 {
3279         next_token();
3280         /* TODO */
3281
3282         if (current_function == NULL) {
3283                 errorf(HERE, "'__func__' used outside of a function");
3284         }
3285
3286         string_literal_expression_t *expression
3287                 = allocate_ast_zero(sizeof(expression[0]));
3288
3289         expression->expression.kind     = EXPR_FUNCTION;
3290         expression->expression.datatype = type_string;
3291         expression->value               = current_function->symbol->string;
3292
3293         return (expression_t*) expression;
3294 }
3295
3296 static expression_t *parse_pretty_function_keyword(void)
3297 {
3298         eat(T___PRETTY_FUNCTION__);
3299         /* TODO */
3300
3301         if (current_function == NULL) {
3302                 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3303         }
3304
3305         string_literal_expression_t *expression
3306                 = allocate_ast_zero(sizeof(expression[0]));
3307
3308         expression->expression.kind     = EXPR_PRETTY_FUNCTION;
3309         expression->expression.datatype = type_string;
3310         expression->value               = current_function->symbol->string;
3311
3312         return (expression_t*) expression;
3313 }
3314
3315 static designator_t *parse_designator(void)
3316 {
3317         designator_t *result = allocate_ast_zero(sizeof(result[0]));
3318
3319         if(token.type != T_IDENTIFIER) {
3320                 parse_error_expected("while parsing member designator",
3321                                      T_IDENTIFIER, 0);
3322                 eat_paren();
3323                 return NULL;
3324         }
3325         result->symbol = token.v.symbol;
3326         next_token();
3327
3328         designator_t *last_designator = result;
3329         while(true) {
3330                 if(token.type == '.') {
3331                         next_token();
3332                         if(token.type != T_IDENTIFIER) {
3333                                 parse_error_expected("while parsing member designator",
3334                                                      T_IDENTIFIER, 0);
3335                                 eat_paren();
3336                                 return NULL;
3337                         }
3338                         designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3339                         designator->symbol       = token.v.symbol;
3340                         next_token();
3341
3342                         last_designator->next = designator;
3343                         last_designator       = designator;
3344                         continue;
3345                 }
3346                 if(token.type == '[') {
3347                         next_token();
3348                         designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3349                         designator->array_access = parse_expression();
3350                         if(designator->array_access == NULL) {
3351                                 eat_paren();
3352                                 return NULL;
3353                         }
3354                         expect(']');
3355
3356                         last_designator->next = designator;
3357                         last_designator       = designator;
3358                         continue;
3359                 }
3360                 break;
3361         }
3362
3363         return result;
3364 }
3365
3366 static expression_t *parse_offsetof(void)
3367 {
3368         eat(T___builtin_offsetof);
3369
3370         expression_t *expression  = allocate_expression_zero(EXPR_OFFSETOF);
3371         expression->base.datatype = type_size_t;
3372
3373         expect('(');
3374         expression->offsetofe.type = parse_typename();
3375         expect(',');
3376         expression->offsetofe.designator = parse_designator();
3377         expect(')');
3378
3379         return expression;
3380 }
3381
3382 static expression_t *parse_va_start(void)
3383 {
3384         eat(T___builtin_va_start);
3385
3386         expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3387
3388         expect('(');
3389         expression->va_starte.ap = parse_assignment_expression();
3390         expect(',');
3391         expression_t *const expr = parse_assignment_expression();
3392         if (expr->kind == EXPR_REFERENCE) {
3393                 declaration_t *const decl = expr->reference.declaration;
3394                 if (decl->parent_context == &current_function->context &&
3395                     decl->next == NULL) {
3396                         expression->va_starte.parameter = decl;
3397                         expect(')');
3398                         return expression;
3399                 }
3400         }
3401         errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3402
3403         return create_invalid_expression();
3404 }
3405
3406 static expression_t *parse_va_arg(void)
3407 {
3408         eat(T___builtin_va_arg);
3409
3410         expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3411
3412         expect('(');
3413         expression->va_arge.ap = parse_assignment_expression();
3414         expect(',');
3415         expression->base.datatype = parse_typename();
3416         expect(')');
3417
3418         return expression;
3419 }
3420
3421 static expression_t *parse_builtin_symbol(void)
3422 {
3423         expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3424
3425         symbol_t *symbol = token.v.symbol;
3426
3427         expression->builtin_symbol.symbol = symbol;
3428         next_token();
3429
3430         type_t *type = get_builtin_symbol_type(symbol);
3431         type = automatic_type_conversion(type);
3432
3433         expression->base.datatype = type;
3434         return expression;
3435 }
3436
3437 static expression_t *parse_builtin_constant(void)
3438 {
3439         eat(T___builtin_constant_p);
3440
3441         expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3442
3443         expect('(');
3444         expression->builtin_constant.value = parse_assignment_expression();
3445         expect(')');
3446         expression->base.datatype = type_int;
3447
3448         return expression;
3449 }
3450
3451 static expression_t *parse_builtin_prefetch(void)
3452 {
3453         eat(T___builtin_prefetch);
3454
3455         expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3456
3457         expect('(');
3458         expression->builtin_prefetch.adr = parse_assignment_expression();
3459         if (token.type == ',') {
3460                 next_token();
3461                 expression->builtin_prefetch.rw = parse_assignment_expression();
3462         }
3463         if (token.type == ',') {
3464                 next_token();
3465                 expression->builtin_prefetch.locality = parse_assignment_expression();
3466         }
3467         expect(')');
3468         expression->base.datatype = type_void;
3469
3470         return expression;
3471 }
3472
3473 static expression_t *parse_compare_builtin(void)
3474 {
3475         expression_t *expression;
3476
3477         switch(token.type) {
3478         case T___builtin_isgreater:
3479                 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3480                 break;
3481         case T___builtin_isgreaterequal:
3482                 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3483                 break;
3484         case T___builtin_isless:
3485                 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3486                 break;
3487         case T___builtin_islessequal:
3488                 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3489                 break;
3490         case T___builtin_islessgreater:
3491                 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3492                 break;
3493         case T___builtin_isunordered:
3494                 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3495                 break;
3496         default:
3497                 panic("invalid compare builtin found");
3498                 break;
3499         }
3500         next_token();
3501
3502         expect('(');
3503         expression->binary.left = parse_assignment_expression();
3504         expect(',');
3505         expression->binary.right = parse_assignment_expression();
3506         expect(')');
3507
3508         type_t *orig_type_left  = expression->binary.left->base.datatype;
3509         type_t *orig_type_right = expression->binary.right->base.datatype;
3510         if(orig_type_left == NULL || orig_type_right == NULL)
3511                 return expression;
3512
3513         type_t *type_left  = skip_typeref(orig_type_left);
3514         type_t *type_right = skip_typeref(orig_type_right);
3515         if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3516                 type_error_incompatible("invalid operands in comparison",
3517                                         token.source_position, type_left, type_right);
3518         } else {
3519                 semantic_comparison(&expression->binary);
3520         }
3521
3522         return expression;
3523 }
3524
3525 static expression_t *parse_builtin_expect(void)
3526 {
3527         eat(T___builtin_expect);
3528
3529         expression_t *expression
3530                 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3531
3532         expect('(');
3533         expression->binary.left = parse_assignment_expression();
3534         expect(',');
3535         expression->binary.right = parse_constant_expression();
3536         expect(')');
3537
3538         expression->base.datatype = expression->binary.left->base.datatype;
3539
3540         return expression;
3541 }
3542
3543 static expression_t *parse_assume(void) {
3544         eat(T_assume);
3545
3546         expression_t *expression
3547                 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3548
3549         expect('(');
3550         expression->unary.value = parse_assignment_expression();
3551         expect(')');
3552
3553         expression->base.datatype = type_void;
3554         return expression;
3555 }
3556
3557 static expression_t *parse_alignof(void) {
3558         eat(T___alignof__);
3559
3560         expression_t *expression
3561                 = allocate_expression_zero(EXPR_ALIGNOF);
3562
3563         expect('(');
3564         expression->alignofe.type = parse_typename();
3565         expect(')');
3566
3567         expression->base.datatype = type_size_t;
3568         return expression;
3569 }
3570
3571 static expression_t *parse_primary_expression(void)
3572 {
3573         switch(token.type) {
3574         case T_INTEGER:
3575                 return parse_int_const();
3576         case T_FLOATINGPOINT:
3577                 return parse_float_const();
3578         case T_STRING_LITERAL:
3579                 return parse_string_const();
3580         case T_WIDE_STRING_LITERAL:
3581                 return parse_wide_string_const();
3582         case T_IDENTIFIER:
3583                 return parse_reference();
3584         case T___FUNCTION__:
3585         case T___func__:
3586                 return parse_function_keyword();
3587         case T___PRETTY_FUNCTION__:
3588                 return parse_pretty_function_keyword();
3589         case T___builtin_offsetof:
3590                 return parse_offsetof();
3591         case T___builtin_va_start:
3592                 return parse_va_start();
3593         case T___builtin_va_arg:
3594                 return parse_va_arg();
3595         case T___builtin_expect:
3596                 return parse_builtin_expect();
3597         case T___builtin_nanf:
3598         case T___builtin_alloca:
3599         case T___builtin_va_end:
3600                 return parse_builtin_symbol();
3601         case T___builtin_isgreater:
3602         case T___builtin_isgreaterequal:
3603         case T___builtin_isless:
3604         case T___builtin_islessequal:
3605         case T___builtin_islessgreater:
3606         case T___builtin_isunordered:
3607                 return parse_compare_builtin();
3608         case T___builtin_constant_p:
3609                 return parse_builtin_constant();
3610         case T___builtin_prefetch:
3611                 return parse_builtin_prefetch();
3612         case T___alignof__:
3613                 return parse_alignof();
3614         case T_assume:
3615                 return parse_assume();
3616
3617         case '(':
3618                 return parse_brace_expression();
3619         }
3620
3621         errorf(HERE, "unexpected token '%K'", &token);
3622         eat_statement();
3623
3624         return create_invalid_expression();
3625 }
3626
3627 /**
3628  * Check if the expression has the character type and issue a warning then.
3629  */
3630 static void check_for_char_index_type(const expression_t *expression) {
3631         type_t *type      = expression->base.datatype;
3632         type_t *base_type = skip_typeref(type);
3633
3634         if (base_type->base.kind == TYPE_ATOMIC) {
3635                 if (base_type->atomic.akind == ATOMIC_TYPE_CHAR) {
3636                         warningf(expression->base.source_position,
3637                                 "array subscript has type '%T'", type);
3638                 }
3639         }
3640 }
3641
3642 static expression_t *parse_array_expression(unsigned precedence,
3643                                             expression_t *left)
3644 {
3645         (void) precedence;
3646
3647         eat('[');
3648
3649         expression_t *inside = parse_expression();
3650
3651         array_access_expression_t *array_access
3652                 = allocate_ast_zero(sizeof(array_access[0]));
3653
3654         array_access->expression.kind = EXPR_ARRAY_ACCESS;
3655
3656         type_t *type_left   = left->base.datatype;
3657         type_t *type_inside = inside->base.datatype;
3658         type_t *return_type = NULL;
3659
3660         if(type_left != NULL && type_inside != NULL) {
3661                 type_left   = skip_typeref(type_left);
3662                 type_inside = skip_typeref(type_inside);
3663
3664                 if(is_type_pointer(type_left)) {
3665                         pointer_type_t *pointer = &type_left->pointer;
3666                         return_type             = pointer->points_to;
3667                         array_access->array_ref = left;
3668                         array_access->index     = inside;
3669                         check_for_char_index_type(inside);
3670                 } else if(is_type_pointer(type_inside)) {
3671                         pointer_type_t *pointer = &type_inside->pointer;
3672                         return_type             = pointer->points_to;
3673                         array_access->array_ref = inside;
3674                         array_access->index     = left;
3675                         array_access->flipped   = true;
3676                         check_for_char_index_type(left);
3677                 } else {
3678                         errorf(HERE, "array access on object with non-pointer types '%T', '%T'", type_left, type_inside);
3679                 }
3680         } else {
3681                 array_access->array_ref = left;
3682                 array_access->index     = inside;
3683         }
3684
3685         if(token.type != ']') {
3686                 parse_error_expected("Problem while parsing array access", ']', 0);
3687                 return (expression_t*) array_access;
3688         }
3689         next_token();
3690
3691         return_type = automatic_type_conversion(return_type);
3692         array_access->expression.datatype = return_type;
3693
3694         return (expression_t*) array_access;
3695 }
3696
3697 static expression_t *parse_sizeof(unsigned precedence)
3698 {
3699         eat(T_sizeof);
3700
3701         sizeof_expression_t *sizeof_expression
3702                 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3703         sizeof_expression->expression.kind     = EXPR_SIZEOF;
3704         sizeof_expression->expression.datatype = type_size_t;
3705
3706         if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3707                 next_token();
3708                 sizeof_expression->type = parse_typename();
3709                 expect(')');
3710         } else {
3711                 expression_t *expression  = parse_sub_expression(precedence);
3712                 expression->base.datatype = revert_automatic_type_conversion(expression);
3713
3714                 sizeof_expression->type            = expression->base.datatype;
3715                 sizeof_expression->size_expression = expression;
3716         }
3717
3718         return (expression_t*) sizeof_expression;
3719 }
3720
3721 static expression_t *parse_select_expression(unsigned precedence,
3722                                              expression_t *compound)
3723 {
3724         (void) precedence;
3725         assert(token.type == '.' || token.type == T_MINUSGREATER);
3726
3727         bool is_pointer = (token.type == T_MINUSGREATER);
3728         next_token();
3729
3730         expression_t *select    = allocate_expression_zero(EXPR_SELECT);
3731         select->select.compound = compound;
3732
3733         if(token.type != T_IDENTIFIER) {
3734                 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3735                 return select;
3736         }
3737         symbol_t *symbol      = token.v.symbol;
3738         select->select.symbol = symbol;
3739         next_token();
3740
3741         type_t *orig_type = compound->base.datatype;
3742         if(orig_type == NULL)
3743                 return create_invalid_expression();
3744
3745         type_t *type = skip_typeref(orig_type);
3746
3747         type_t *type_left = type;
3748         if(is_pointer) {
3749                 if(type->kind != TYPE_POINTER) {
3750                         errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3751                         return create_invalid_expression();
3752                 }
3753                 pointer_type_t *pointer_type = &type->pointer;
3754                 type_left                    = pointer_type->points_to;
3755         }
3756         type_left = skip_typeref(type_left);
3757
3758         if(type_left->kind != TYPE_COMPOUND_STRUCT
3759                         && type_left->kind != TYPE_COMPOUND_UNION) {
3760                 errorf(HERE, "request for member '%Y' in something not a struct or "
3761                        "union, but '%T'", symbol, type_left);
3762                 return create_invalid_expression();
3763         }
3764
3765         compound_type_t *compound_type = &type_left->compound;
3766         declaration_t   *declaration   = compound_type->declaration;
3767
3768         if(!declaration->init.is_defined) {
3769                 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3770                        symbol, type_left);
3771                 return create_invalid_expression();
3772         }
3773
3774         declaration_t *iter = declaration->context.declarations;
3775         for( ; iter != NULL; iter = iter->next) {
3776                 if(iter->symbol == symbol) {
3777                         break;
3778                 }
3779         }
3780         if(iter == NULL) {
3781                 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3782                 return create_invalid_expression();
3783         }
3784
3785         /* we always do the auto-type conversions; the & and sizeof parser contains
3786          * code to revert this! */
3787         type_t *expression_type = automatic_type_conversion(iter->type);
3788
3789         select->select.compound_entry = iter;
3790         select->base.datatype         = expression_type;
3791
3792         if(expression_type->kind == TYPE_BITFIELD) {
3793                 expression_t *extract
3794                         = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3795                 extract->unary.value   = select;
3796                 extract->base.datatype = expression_type->bitfield.base;
3797
3798                 return extract;
3799         }
3800
3801         return select;
3802 }
3803
3804 /**
3805  * Parse a call expression, ie. expression '( ... )'.
3806  *
3807  * @param expression  the function address
3808  */
3809 static expression_t *parse_call_expression(unsigned precedence,
3810                                            expression_t *expression)
3811 {
3812         (void) precedence;
3813         expression_t *result = allocate_expression_zero(EXPR_CALL);
3814
3815         call_expression_t *call = &result->call;
3816         call->function          = expression;
3817
3818         function_type_t *function_type = NULL;
3819         type_t          *orig_type     = expression->base.datatype;
3820         if(orig_type != NULL) {
3821                 type_t *type  = skip_typeref(orig_type);
3822
3823                 if(is_type_pointer(type)) {
3824                         pointer_type_t *pointer_type = &type->pointer;
3825
3826                         type = skip_typeref(pointer_type->points_to);
3827
3828                         if (is_type_function(type)) {
3829                                 function_type             = &type->function;
3830                                 call->expression.datatype = function_type->return_type;
3831                         }
3832                 }
3833                 if(function_type == NULL) {
3834                         errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3835
3836                         function_type             = NULL;
3837                         call->expression.datatype = NULL;
3838                 }
3839         }
3840
3841         /* parse arguments */
3842         eat('(');
3843
3844         if(token.type != ')') {
3845                 call_argument_t *last_argument = NULL;
3846
3847                 while(true) {
3848                         call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3849
3850                         argument->expression = parse_assignment_expression();
3851                         if(last_argument == NULL) {
3852                                 call->arguments = argument;
3853                         } else {
3854                                 last_argument->next = argument;
3855                         }
3856                         last_argument = argument;
3857
3858                         if(token.type != ',')
3859                                 break;
3860                         next_token();
3861                 }
3862         }
3863         expect(')');
3864
3865         if(function_type != NULL) {
3866                 function_parameter_t *parameter = function_type->parameters;
3867                 call_argument_t      *argument  = call->arguments;
3868                 for( ; parameter != NULL && argument != NULL;
3869                                 parameter = parameter->next, argument = argument->next) {
3870                         type_t *expected_type = parameter->type;
3871                         /* TODO report context in error messages */
3872                         argument->expression = create_implicit_cast(argument->expression,
3873                                                                     expected_type);
3874                 }
3875                 /* too few parameters */
3876                 if(parameter != NULL) {
3877                         errorf(HERE, "too few arguments to function '%E'", expression);
3878                 } else if(argument != NULL) {
3879                         /* too many parameters */
3880                         if(!function_type->variadic
3881                                         && !function_type->unspecified_parameters) {
3882                                 errorf(HERE, "too many arguments to function '%E'", expression);
3883                         } else {
3884                                 /* do default promotion */
3885                                 for( ; argument != NULL; argument = argument->next) {
3886                                         type_t *type = argument->expression->base.datatype;
3887
3888                                         if(type == NULL)
3889                                                 continue;
3890
3891                                         type = skip_typeref(type);
3892                                         if(is_type_integer(type)) {
3893                                                 type = promote_integer(type);
3894                                         } else if(type == type_float) {
3895                                                 type = type_double;
3896                                         }
3897
3898                                         argument->expression
3899                                                 = create_implicit_cast(argument->expression, type);
3900                                 }
3901
3902                                 check_format(&result->call);
3903                         }
3904                 } else {
3905                         check_format(&result->call);
3906                 }
3907         }
3908
3909         return result;
3910 }
3911
3912 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3913
3914 static bool same_compound_type(const type_t *type1, const type_t *type2)
3915 {
3916         if(!is_type_compound(type1))
3917                 return false;
3918         if(type1->kind != type2->kind)
3919                 return false;
3920
3921         const compound_type_t *compound1 = &type1->compound;
3922         const compound_type_t *compound2 = &type2->compound;
3923
3924         return compound1->declaration == compound2->declaration;
3925 }
3926
3927 /**
3928  * Parse a conditional expression, ie. 'expression ? ... : ...'.
3929  *
3930  * @param expression  the conditional expression
3931  */
3932 static expression_t *parse_conditional_expression(unsigned precedence,
3933                                                   expression_t *expression)
3934 {
3935         eat('?');
3936
3937         expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3938
3939         conditional_expression_t *conditional = &result->conditional;
3940         conditional->condition = expression;
3941
3942         /* 6.5.15.2 */
3943         type_t *condition_type_orig = expression->base.datatype;
3944         if(condition_type_orig != NULL) {
3945                 type_t *condition_type = skip_typeref(condition_type_orig);
3946                 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3947                         type_error("expected a scalar type in conditional condition",
3948                                    expression->base.source_position, condition_type_orig);
3949                 }
3950         }
3951
3952         expression_t *true_expression = parse_expression();
3953         expect(':');
3954         expression_t *false_expression = parse_sub_expression(precedence);
3955
3956         conditional->true_expression  = true_expression;
3957         conditional->false_expression = false_expression;
3958
3959         type_t *orig_true_type  = true_expression->base.datatype;
3960         type_t *orig_false_type = false_expression->base.datatype;
3961         if(orig_true_type == NULL || orig_false_type == NULL)
3962                 return result;
3963
3964         type_t *true_type  = skip_typeref(orig_true_type);
3965         type_t *false_type = skip_typeref(orig_false_type);
3966
3967         /* 6.5.15.3 */
3968         type_t *result_type = NULL;
3969         if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3970                 result_type = semantic_arithmetic(true_type, false_type);
3971
3972                 true_expression  = create_implicit_cast(true_expression, result_type);
3973                 false_expression = create_implicit_cast(false_expression, result_type);
3974
3975                 conditional->true_expression     = true_expression;
3976                 conditional->false_expression    = false_expression;
3977                 conditional->expression.datatype = result_type;
3978         } else if (same_compound_type(true_type, false_type)
3979                         || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3980                                 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
3981                 /* just take 1 of the 2 types */
3982                 result_type = true_type;
3983         } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3984                         && pointers_compatible(true_type, false_type)) {
3985                 /* ok */
3986                 result_type = true_type;
3987         } else {
3988                 /* TODO */
3989                 type_error_incompatible("while parsing conditional",
3990                                         expression->base.source_position, true_type,
3991                                         false_type);
3992         }
3993
3994         conditional->expression.datatype = result_type;
3995         return result;
3996 }
3997
3998 /**
3999  * Parse an extension expression.
4000  */
4001 static expression_t *parse_extension(unsigned precedence)
4002 {
4003         eat(T___extension__);
4004
4005         /* TODO enable extensions */
4006         expression_t *expression = parse_sub_expression(precedence);
4007         /* TODO disable extensions */
4008         return expression;
4009 }
4010
4011 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4012 {
4013         eat(T___builtin_classify_type);
4014
4015         expression_t *result  = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4016         result->base.datatype = type_int;
4017
4018         expect('(');
4019         expression_t *expression = parse_sub_expression(precedence);
4020         expect(')');
4021         result->classify_type.type_expression = expression;
4022
4023         return result;
4024 }
4025
4026 static void semantic_incdec(unary_expression_t *expression)
4027 {
4028         type_t *orig_type = expression->value->base.datatype;
4029         if(orig_type == NULL)
4030                 return;
4031
4032         type_t *type = skip_typeref(orig_type);
4033         if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4034                 /* TODO: improve error message */
4035                 errorf(HERE, "operation needs an arithmetic or pointer type");
4036                 return;
4037         }
4038
4039         expression->expression.datatype = orig_type;
4040 }
4041
4042 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4043 {
4044         type_t *orig_type = expression->value->base.datatype;
4045         if(orig_type == NULL)
4046                 return;
4047
4048         type_t *type = skip_typeref(orig_type);
4049         if(!is_type_arithmetic(type)) {
4050                 /* TODO: improve error message */
4051                 errorf(HERE, "operation needs an arithmetic type");
4052                 return;
4053         }
4054
4055         expression->expression.datatype = orig_type;
4056 }
4057
4058 static void semantic_unexpr_scalar(unary_expression_t *expression)
4059 {
4060         type_t *orig_type = expression->value->base.datatype;
4061         if(orig_type == NULL)
4062                 return;
4063
4064         type_t *type = skip_typeref(orig_type);
4065         if (!is_type_scalar(type)) {
4066                 errorf(HERE, "operand of ! must be of scalar type");
4067                 return;
4068         }
4069
4070         expression->expression.datatype = orig_type;
4071 }
4072
4073 static void semantic_unexpr_integer(unary_expression_t *expression)
4074 {
4075         type_t *orig_type = expression->value->base.datatype;
4076         if(orig_type == NULL)
4077                 return;
4078
4079         type_t *type = skip_typeref(orig_type);
4080         if (!is_type_integer(type)) {
4081                 errorf(HERE, "operand of ~ must be of integer type");
4082                 return;
4083         }
4084
4085         expression->expression.datatype = orig_type;
4086 }
4087
4088 static void semantic_dereference(unary_expression_t *expression)
4089 {
4090         type_t *orig_type = expression->value->base.datatype;
4091         if(orig_type == NULL)
4092                 return;
4093
4094         type_t *type = skip_typeref(orig_type);
4095         if(!is_type_pointer(type)) {
4096                 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4097                 return;
4098         }
4099
4100         pointer_type_t *pointer_type = &type->pointer;
4101         type_t         *result_type  = pointer_type->points_to;
4102
4103         result_type = automatic_type_conversion(result_type);
4104         expression->expression.datatype = result_type;
4105 }
4106
4107 /**
4108  * Check the semantic of the address taken expression.
4109  */
4110 static void semantic_take_addr(unary_expression_t *expression)
4111 {
4112         expression_t *value  = expression->value;
4113         value->base.datatype = revert_automatic_type_conversion(value);
4114
4115         type_t *orig_type = value->base.datatype;
4116         if(orig_type == NULL)
4117                 return;
4118
4119         if(value->kind == EXPR_REFERENCE) {
4120                 reference_expression_t *reference   = (reference_expression_t*) value;
4121                 declaration_t          *declaration = reference->declaration;
4122                 if(declaration != NULL) {
4123                         if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4124                                 errorf(expression->expression.source_position,
4125                                         "address of register variable '%Y' requested",
4126                                         declaration->symbol);
4127                         }
4128                         declaration->address_taken = 1;
4129                 }
4130         }
4131
4132         expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4133 }
4134
4135 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc)   \
4136 static expression_t *parse_##unexpression_type(unsigned precedence)            \
4137 {                                                                              \
4138         eat(token_type);                                                           \
4139                                                                                    \
4140         expression_t *unary_expression                                             \
4141                 = allocate_expression_zero(unexpression_type);                         \
4142         unary_expression->base.source_position = HERE;                             \
4143         unary_expression->unary.value = parse_sub_expression(precedence);          \
4144                                                                                    \
4145         sfunc(&unary_expression->unary);                                           \
4146                                                                                    \
4147         return unary_expression;                                                   \
4148 }
4149
4150 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4151                                semantic_unexpr_arithmetic)
4152 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4153                                semantic_unexpr_arithmetic)
4154 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4155                                semantic_unexpr_scalar)
4156 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4157                                semantic_dereference)
4158 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4159                                semantic_take_addr)
4160 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4161                                semantic_unexpr_integer)
4162 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS,   EXPR_UNARY_PREFIX_INCREMENT,
4163                                semantic_incdec)
4164 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4165                                semantic_incdec)
4166
4167 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4168                                                sfunc)                         \
4169 static expression_t *parse_##unexpression_type(unsigned precedence,           \
4170                                                expression_t *left)            \
4171 {                                                                             \
4172         (void) precedence;                                                        \
4173         eat(token_type);                                                          \
4174                                                                               \
4175         expression_t *unary_expression                                            \
4176                 = allocate_expression_zero(unexpression_type);                        \
4177         unary_expression->unary.value = left;                                     \
4178                                                                                   \
4179         sfunc(&unary_expression->unary);                                          \
4180                                                                               \
4181         return unary_expression;                                                  \
4182 }
4183
4184 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4185                                        EXPR_UNARY_POSTFIX_INCREMENT,
4186                                        semantic_incdec)
4187 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4188                                        EXPR_UNARY_POSTFIX_DECREMENT,
4189                                        semantic_incdec)
4190
4191 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4192 {
4193         /* TODO: handle complex + imaginary types */
4194
4195         /* Â§ 6.3.1.8 Usual arithmetic conversions */
4196         if(type_left == type_long_double || type_right == type_long_double) {
4197                 return type_long_double;
4198         } else if(type_left == type_double || type_right == type_double) {
4199                 return type_double;
4200         } else if(type_left == type_float || type_right == type_float) {
4201                 return type_float;
4202         }
4203
4204         type_right = promote_integer(type_right);
4205         type_left  = promote_integer(type_left);
4206
4207         if(type_left == type_right)
4208                 return type_left;
4209
4210         bool signed_left  = is_type_signed(type_left);
4211         bool signed_right = is_type_signed(type_right);
4212         int  rank_left    = get_rank(type_left);
4213         int  rank_right   = get_rank(type_right);
4214         if(rank_left < rank_right) {
4215                 if(signed_left == signed_right || !signed_right) {
4216                         return type_right;
4217                 } else {
4218                         return type_left;
4219                 }
4220         } else {
4221                 if(signed_left == signed_right || !signed_left) {
4222                         return type_left;
4223                 } else {
4224                         return type_right;
4225                 }
4226         }
4227 }
4228
4229 /**
4230  * Check the semantic restrictions for a binary expression.
4231  */
4232 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4233 {
4234         expression_t *left       = expression->left;
4235         expression_t *right      = expression->right;
4236         type_t       *orig_type_left  = left->base.datatype;
4237         type_t       *orig_type_right = right->base.datatype;
4238
4239         if(orig_type_left == NULL || orig_type_right == NULL)
4240                 return;
4241
4242         type_t *type_left  = skip_typeref(orig_type_left);
4243         type_t *type_right = skip_typeref(orig_type_right);
4244
4245         if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4246                 /* TODO: improve error message */
4247                 errorf(HERE, "operation needs arithmetic types");
4248                 return;
4249         }
4250
4251         type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4252         expression->left  = create_implicit_cast(left, arithmetic_type);
4253         expression->right = create_implicit_cast(right, arithmetic_type);
4254         expression->expression.datatype = arithmetic_type;
4255 }
4256
4257 static void semantic_shift_op(binary_expression_t *expression)
4258 {
4259         expression_t *left       = expression->left;
4260         expression_t *right      = expression->right;
4261         type_t       *orig_type_left  = left->base.datatype;
4262         type_t       *orig_type_right = right->base.datatype;
4263
4264         if(orig_type_left == NULL || orig_type_right == NULL)
4265                 return;
4266
4267         type_t *type_left  = skip_typeref(orig_type_left);
4268         type_t *type_right = skip_typeref(orig_type_right);
4269
4270         if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4271                 /* TODO: improve error message */
4272                 errorf(HERE, "operation needs integer types");
4273                 return;
4274         }
4275
4276         type_left  = promote_integer(type_left);
4277         type_right = promote_integer(type_right);
4278
4279         expression->left  = create_implicit_cast(left, type_left);
4280         expression->right = create_implicit_cast(right, type_right);
4281         expression->expression.datatype = type_left;
4282 }
4283
4284 static void semantic_add(binary_expression_t *expression)
4285 {
4286         expression_t *left            = expression->left;
4287         expression_t *right           = expression->right;
4288         type_t       *orig_type_left  = left->base.datatype;
4289         type_t       *orig_type_right = right->base.datatype;
4290
4291         if(orig_type_left == NULL || orig_type_right == NULL)
4292                 return;
4293
4294         type_t *type_left  = skip_typeref(orig_type_left);
4295         type_t *type_right = skip_typeref(orig_type_right);
4296
4297         /* Â§ 5.6.5 */
4298         if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4299                 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4300                 expression->left  = create_implicit_cast(left, arithmetic_type);
4301                 expression->right = create_implicit_cast(right, arithmetic_type);
4302                 expression->expression.datatype = arithmetic_type;
4303                 return;
4304         } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4305                 expression->expression.datatype = type_left;
4306         } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4307                 expression->expression.datatype = type_right;
4308         } else {
4309                 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4310         }
4311 }
4312
4313 static void semantic_sub(binary_expression_t *expression)
4314 {
4315         expression_t *left            = expression->left;
4316         expression_t *right           = expression->right;
4317         type_t       *orig_type_left  = left->base.datatype;
4318         type_t       *orig_type_right = right->base.datatype;
4319
4320         if(orig_type_left == NULL || orig_type_right == NULL)
4321                 return;
4322
4323         type_t       *type_left       = skip_typeref(orig_type_left);
4324         type_t       *type_right      = skip_typeref(orig_type_right);
4325
4326         /* Â§ 5.6.5 */
4327         if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4328                 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4329                 expression->left  = create_implicit_cast(left, arithmetic_type);
4330                 expression->right = create_implicit_cast(right, arithmetic_type);
4331                 expression->expression.datatype = arithmetic_type;
4332                 return;
4333         } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4334                 expression->expression.datatype = type_left;
4335         } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4336                 if(!pointers_compatible(type_left, type_right)) {
4337                         errorf(HERE, "pointers to incompatible objects to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4338                 } else {
4339                         expression->expression.datatype = type_ptrdiff_t;
4340                 }
4341         } else {
4342                 errorf(HERE, "invalid operands to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4343         }
4344 }
4345
4346 static void semantic_comparison(binary_expression_t *expression)
4347 {
4348         expression_t *left            = expression->left;
4349         expression_t *right           = expression->right;
4350         type_t       *orig_type_left  = left->base.datatype;
4351         type_t       *orig_type_right = right->base.datatype;
4352
4353         if(orig_type_left == NULL || orig_type_right == NULL)
4354                 return;
4355
4356         type_t *type_left  = skip_typeref(orig_type_left);
4357         type_t *type_right = skip_typeref(orig_type_right);
4358
4359         /* TODO non-arithmetic types */
4360         if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4361                 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4362                 expression->left  = create_implicit_cast(left, arithmetic_type);
4363                 expression->right = create_implicit_cast(right, arithmetic_type);
4364                 expression->expression.datatype = arithmetic_type;
4365         } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4366                 /* TODO check compatibility */
4367         } else if (is_type_pointer(type_left)) {
4368                 expression->right = create_implicit_cast(right, type_left);
4369         } else if (is_type_pointer(type_right)) {
4370                 expression->left = create_implicit_cast(left, type_right);
4371         } else {
4372                 type_error_incompatible("invalid operands in comparison",
4373                                         token.source_position, type_left, type_right);
4374         }
4375         expression->expression.datatype = type_int;
4376 }
4377
4378 static void semantic_arithmetic_assign(binary_expression_t *expression)
4379 {
4380         expression_t *left            = expression->left;
4381         expression_t *right           = expression->right;
4382         type_t       *orig_type_left  = left->base.datatype;
4383         type_t       *orig_type_right = right->base.datatype;
4384
4385         if(orig_type_left == NULL || orig_type_right == NULL)
4386                 return;
4387
4388         type_t *type_left  = skip_typeref(orig_type_left);
4389         type_t *type_right = skip_typeref(orig_type_right);
4390
4391         if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4392                 /* TODO: improve error message */
4393                 errorf(HERE, "operation needs arithmetic types");
4394                 return;
4395         }
4396
4397         /* combined instructions are tricky. We can't create an implicit cast on
4398          * the left side, because we need the uncasted form for the store.
4399          * The ast2firm pass has to know that left_type must be right_type
4400          * for the arithmetic operation and create a cast by itself */
4401         type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4402         expression->right       = create_implicit_cast(right, arithmetic_type);
4403         expression->expression.datatype = type_left;
4404 }
4405
4406 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4407 {
4408         expression_t *left            = expression->left;
4409         expression_t *right           = expression->right;
4410         type_t       *orig_type_left  = left->base.datatype;
4411         type_t       *orig_type_right = right->base.datatype;
4412
4413         if(orig_type_left == NULL || orig_type_right == NULL)
4414                 return;
4415
4416         type_t *type_left  = skip_typeref(orig_type_left);
4417         type_t *type_right = skip_typeref(orig_type_right);
4418
4419         if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4420                 /* combined instructions are tricky. We can't create an implicit cast on
4421                  * the left side, because we need the uncasted form for the store.
4422                  * The ast2firm pass has to know that left_type must be right_type
4423                  * for the arithmetic operation and create a cast by itself */
4424                 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4425                 expression->right = create_implicit_cast(right, arithmetic_type);
4426                 expression->expression.datatype = type_left;
4427         } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4428                 expression->expression.datatype = type_left;
4429         } else {
4430                 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4431                 return;
4432         }
4433 }
4434
4435 /**
4436  * Check the semantic restrictions of a logical expression.
4437  */
4438 static void semantic_logical_op(binary_expression_t *expression)
4439 {
4440         expression_t *left            = expression->left;
4441         expression_t *right           = expression->right;
4442         type_t       *orig_type_left  = left->base.datatype;
4443         type_t       *orig_type_right = right->base.datatype;
4444
4445         if(orig_type_left == NULL || orig_type_right == NULL)
4446                 return;
4447
4448         type_t *type_left  = skip_typeref(orig_type_left);
4449         type_t *type_right = skip_typeref(orig_type_right);
4450
4451         if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4452                 /* TODO: improve error message */
4453                 errorf(HERE, "operation needs scalar types");
4454                 return;
4455         }
4456
4457         expression->expression.datatype = type_int;
4458 }
4459
4460 /**
4461  * Checks if a compound type has constant fields.
4462  */
4463 static bool has_const_fields(const compound_type_t *type)
4464 {
4465         const context_t     *context = &type->declaration->context;
4466         const declaration_t *declaration = context->declarations;
4467
4468         for (; declaration != NULL; declaration = declaration->next) {
4469                 if (declaration->namespc != NAMESPACE_NORMAL)
4470                         continue;
4471
4472                 const type_t *decl_type = skip_typeref(declaration->type);
4473                 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4474                         return true;
4475         }
4476         /* TODO */
4477         return false;
4478 }
4479
4480 /**
4481  * Check the semantic restrictions of a binary assign expression.
4482  */
4483 static void semantic_binexpr_assign(binary_expression_t *expression)
4484 {
4485         expression_t *left           = expression->left;
4486         type_t       *orig_type_left = left->base.datatype;
4487
4488         if(orig_type_left == NULL)
4489                 return;
4490
4491         type_t *type_left = revert_automatic_type_conversion(left);
4492         type_left         = skip_typeref(orig_type_left);
4493
4494         /* must be a modifiable lvalue */
4495         if (is_type_array(type_left)) {
4496                 errorf(HERE, "cannot assign to arrays ('%E')", left);
4497                 return;
4498         }
4499         if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4500                 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4501                        orig_type_left);
4502                 return;
4503         }
4504         if(is_type_incomplete(type_left)) {
4505                 errorf(HERE,
4506                        "left-hand side of assignment '%E' has incomplete type '%T'",
4507                        left, orig_type_left);
4508                 return;
4509         }
4510         if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4511                 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4512                        left, orig_type_left);
4513                 return;
4514         }
4515
4516         semantic_assign(orig_type_left, &expression->right, "assignment");
4517
4518         expression->expression.datatype = orig_type_left;
4519 }
4520
4521 static void semantic_comma(binary_expression_t *expression)
4522 {
4523         expression->expression.datatype = expression->right->base.datatype;
4524 }
4525
4526 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr)  \
4527 static expression_t *parse_##binexpression_type(unsigned precedence,      \
4528                                                 expression_t *left)       \
4529 {                                                                         \
4530         eat(token_type);                                                      \
4531                                                                           \
4532         expression_t *right = parse_sub_expression(precedence + lr);          \
4533                                                                           \
4534         expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4535         binexpr->binary.left  = left;                                         \
4536         binexpr->binary.right = right;                                        \
4537         sfunc(&binexpr->binary);                                              \
4538                                                                           \
4539         return binexpr;                                                       \
4540 }
4541
4542 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA,    semantic_comma, 1)
4543 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL,      semantic_binexpr_arithmetic, 1)
4544 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV,      semantic_binexpr_arithmetic, 1)
4545 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD,      semantic_binexpr_arithmetic, 1)
4546 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD,      semantic_add, 1)
4547 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB,      semantic_sub, 1)
4548 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS,     semantic_comparison, 1)
4549 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER,  semantic_comparison, 1)
4550 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN,   semantic_binexpr_assign, 0)
4551
4552 CREATE_BINEXPR_PARSER(T_EQUALEQUAL,           EXPR_BINARY_EQUAL,
4553                       semantic_comparison, 1)
4554 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4555                       semantic_comparison, 1)
4556 CREATE_BINEXPR_PARSER(T_LESSEQUAL,            EXPR_BINARY_LESSEQUAL,
4557                       semantic_comparison, 1)
4558 CREATE_BINEXPR_PARSER(T_GREATEREQUAL,         EXPR_BINARY_GREATEREQUAL,
4559                       semantic_comparison, 1)
4560
4561 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4562                       semantic_binexpr_arithmetic, 1)
4563 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4564                       semantic_binexpr_arithmetic, 1)
4565 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4566                       semantic_binexpr_arithmetic, 1)
4567 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4568                       semantic_logical_op, 1)
4569 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4570                       semantic_logical_op, 1)
4571 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4572                       semantic_shift_op, 1)
4573 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4574                       semantic_shift_op, 1)
4575 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4576                       semantic_arithmetic_addsubb_assign, 0)
4577 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4578                       semantic_arithmetic_addsubb_assign, 0)
4579 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4580                       semantic_arithmetic_assign, 0)
4581 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4582                       semantic_arithmetic_assign, 0)
4583 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4584                       semantic_arithmetic_assign, 0)
4585 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4586                       semantic_arithmetic_assign, 0)
4587 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4588                       semantic_arithmetic_assign, 0)
4589 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4590                       semantic_arithmetic_assign, 0)
4591 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4592                       semantic_arithmetic_assign, 0)
4593 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4594                       semantic_arithmetic_assign, 0)
4595
4596 static expression_t *parse_sub_expression(unsigned precedence)
4597 {
4598         if(token.type < 0) {
4599                 return expected_expression_error();
4600         }
4601
4602         expression_parser_function_t *parser
4603                 = &expression_parsers[token.type];
4604         source_position_t             source_position = token.source_position;
4605         expression_t                 *left;
4606
4607         if(parser->parser != NULL) {
4608                 left = parser->parser(parser->precedence);
4609         } else {
4610                 left = parse_primary_expression();
4611         }
4612         assert(left != NULL);
4613         left->base.source_position = source_position;
4614
4615         while(true) {
4616                 if(token.type < 0) {
4617                         return expected_expression_error();
4618                 }
4619
4620                 parser = &expression_parsers[token.type];
4621                 if(parser->infix_parser == NULL)
4622                         break;
4623                 if(parser->infix_precedence < precedence)
4624                         break;
4625
4626                 left = parser->infix_parser(parser->infix_precedence, left);
4627
4628                 assert(left != NULL);
4629                 assert(left->kind != EXPR_UNKNOWN);
4630                 left->base.source_position = source_position;
4631         }
4632
4633         return left;
4634 }
4635
4636 /**
4637  * Parse an expression.
4638  */
4639 static expression_t *parse_expression(void)
4640 {
4641         return parse_sub_expression(1);
4642 }
4643
4644 /**
4645  * Register a parser for a prefix-like operator with given precedence.
4646  *
4647  * @param parser      the parser function
4648  * @param token_type  the token type of the prefix token
4649  * @param precedence  the precedence of the operator
4650  */
4651 static void register_expression_parser(parse_expression_function parser,
4652                                        int token_type, unsigned precedence)
4653 {
4654         expression_parser_function_t *entry = &expression_parsers[token_type];
4655
4656         if(entry->parser != NULL) {
4657                 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4658                 panic("trying to register multiple expression parsers for a token");
4659         }
4660         entry->parser     = parser;
4661         entry->precedence = precedence;
4662 }
4663
4664 /**
4665  * Register a parser for an infix operator with given precedence.
4666  *
4667  * @param parser      the parser function
4668  * @param token_type  the token type of the infix operator
4669  * @param precedence  the precedence of the operator
4670  */
4671 static void register_infix_parser(parse_expression_infix_function parser,
4672                 int token_type, unsigned precedence)
4673 {
4674         expression_parser_function_t *entry = &expression_parsers[token_type];
4675
4676         if(entry->infix_parser != NULL) {
4677                 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4678                 panic("trying to register multiple infix expression parsers for a "
4679                       "token");
4680         }
4681         entry->infix_parser     = parser;
4682         entry->infix_precedence = precedence;
4683 }
4684
4685 /**
4686  * Initialize the expression parsers.
4687  */
4688 static void init_expression_parsers(void)
4689 {
4690         memset(&expression_parsers, 0, sizeof(expression_parsers));
4691
4692         register_infix_parser(parse_array_expression,         '[',              30);
4693         register_infix_parser(parse_call_expression,          '(',              30);
4694         register_infix_parser(parse_select_expression,        '.',              30);
4695         register_infix_parser(parse_select_expression,        T_MINUSGREATER,   30);
4696         register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4697                                                               T_PLUSPLUS,       30);
4698         register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4699                                                               T_MINUSMINUS,     30);
4700
4701         register_infix_parser(parse_EXPR_BINARY_MUL,          '*',              16);
4702         register_infix_parser(parse_EXPR_BINARY_DIV,          '/',              16);
4703         register_infix_parser(parse_EXPR_BINARY_MOD,          '%',              16);
4704         register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT,    T_LESSLESS,       16);
4705         register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT,   T_GREATERGREATER, 16);
4706         register_infix_parser(parse_EXPR_BINARY_ADD,          '+',              15);
4707         register_infix_parser(parse_EXPR_BINARY_SUB,          '-',              15);
4708         register_infix_parser(parse_EXPR_BINARY_LESS,         '<',              14);
4709         register_infix_parser(parse_EXPR_BINARY_GREATER,      '>',              14);
4710         register_infix_parser(parse_EXPR_BINARY_LESSEQUAL,    T_LESSEQUAL,      14);
4711         register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL,   14);
4712         register_infix_parser(parse_EXPR_BINARY_EQUAL,        T_EQUALEQUAL,     13);
4713         register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4714                                                     T_EXCLAMATIONMARKEQUAL, 13);
4715         register_infix_parser(parse_EXPR_BINARY_BITWISE_AND,  '&',              12);
4716         register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR,  '^',              11);
4717         register_infix_parser(parse_EXPR_BINARY_BITWISE_OR,   '|',              10);
4718         register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND,  T_ANDAND,          9);
4719         register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR,   T_PIPEPIPE,        8);
4720         register_infix_parser(parse_conditional_expression,   '?',               7);
4721         register_infix_parser(parse_EXPR_BINARY_ASSIGN,       '=',               2);
4722         register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN,   T_PLUSEQUAL,       2);
4723         register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN,   T_MINUSEQUAL,      2);
4724         register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN,   T_ASTERISKEQUAL,   2);
4725         register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN,   T_SLASHEQUAL,      2);
4726         register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN,   T_PERCENTEQUAL,    2);
4727         register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4728                                                                 T_LESSLESSEQUAL, 2);
4729         register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4730                                                           T_GREATERGREATEREQUAL, 2);
4731         register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4732                                                                      T_ANDEQUAL, 2);
4733         register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4734                                                                     T_PIPEEQUAL, 2);
4735         register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4736                                                                    T_CARETEQUAL, 2);
4737
4738         register_infix_parser(parse_EXPR_BINARY_COMMA,        ',',               1);
4739
4740         register_expression_parser(parse_EXPR_UNARY_NEGATE,           '-',      25);
4741         register_expression_parser(parse_EXPR_UNARY_PLUS,             '+',      25);
4742         register_expression_parser(parse_EXPR_UNARY_NOT,              '!',      25);
4743         register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE,   '~',      25);
4744         register_expression_parser(parse_EXPR_UNARY_DEREFERENCE,      '*',      25);
4745         register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS,     '&',      25);
4746         register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4747                                                                   T_PLUSPLUS,   25);
4748         register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4749                                                                   T_MINUSMINUS, 25);
4750         register_expression_parser(parse_sizeof,                  T_sizeof,     25);
4751         register_expression_parser(parse_extension,            T___extension__, 25);
4752         register_expression_parser(parse_builtin_classify_type,
4753                                                      T___builtin_classify_type, 25);
4754 }
4755
4756 /**
4757  * Parse a asm statement constraints specification.
4758  */
4759 static asm_constraint_t *parse_asm_constraints(void)
4760 {
4761         asm_constraint_t *result = NULL;
4762         asm_constraint_t *last   = NULL;
4763
4764         while(token.type == T_STRING_LITERAL || token.type == '[') {
4765                 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4766                 memset(constraint, 0, sizeof(constraint[0]));
4767
4768                 if(token.type == '[') {
4769                         eat('[');
4770                         if(token.type != T_IDENTIFIER) {
4771                                 parse_error_expected("while parsing asm constraint",
4772                                                      T_IDENTIFIER, 0);
4773                                 return NULL;
4774                         }
4775                         constraint->symbol = token.v.symbol;
4776
4777                         expect(']');
4778                 }
4779
4780                 constraint->constraints = parse_string_literals();
4781                 expect('(');
4782                 constraint->expression = parse_expression();
4783                 expect(')');
4784
4785                 if(last != NULL) {
4786                         last->next = constraint;
4787                 } else {
4788                         result = constraint;
4789                 }
4790                 last = constraint;
4791
4792                 if(token.type != ',')
4793                         break;
4794                 eat(',');
4795         }
4796
4797         return result;
4798 }
4799
4800 /**
4801  * Parse a asm statement clobber specification.
4802  */
4803 static asm_clobber_t *parse_asm_clobbers(void)
4804 {
4805         asm_clobber_t *result = NULL;
4806         asm_clobber_t *last   = NULL;
4807
4808         while(token.type == T_STRING_LITERAL) {
4809                 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4810                 clobber->clobber       = parse_string_literals();
4811
4812                 if(last != NULL) {
4813                         last->next = clobber;
4814                 } else {
4815                         result = clobber;
4816                 }
4817                 last = clobber;
4818
4819                 if(token.type != ',')
4820                         break;
4821                 eat(',');
4822         }
4823
4824         return result;
4825 }
4826
4827 /**
4828  * Parse an asm statement.
4829  */
4830 static statement_t *parse_asm_statement(void)
4831 {
4832         eat(T_asm);
4833
4834         statement_t *statement          = allocate_statement_zero(STATEMENT_ASM);
4835         statement->base.source_position = token.source_position;
4836
4837         asm_statement_t *asm_statement = &statement->asms;
4838
4839         if(token.type == T_volatile) {
4840                 next_token();
4841                 asm_statement->is_volatile = true;
4842         }
4843
4844         expect('(');
4845         asm_statement->asm_text = parse_string_literals();
4846
4847         if(token.type != ':')
4848                 goto end_of_asm;
4849         eat(':');
4850
4851         asm_statement->inputs = parse_asm_constraints();
4852         if(token.type != ':')
4853                 goto end_of_asm;
4854         eat(':');
4855
4856         asm_statement->outputs = parse_asm_constraints();
4857         if(token.type != ':')
4858                 goto end_of_asm;
4859         eat(':');
4860
4861         asm_statement->clobbers = parse_asm_clobbers();
4862
4863 end_of_asm:
4864         expect(')');
4865         expect(';');
4866         return statement;
4867 }
4868
4869 /**
4870  * Parse a case statement.
4871  */
4872 static statement_t *parse_case_statement(void)
4873 {
4874         eat(T_case);
4875
4876         statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4877
4878         statement->base.source_position  = token.source_position;
4879         statement->case_label.expression = parse_expression();
4880
4881         expect(':');
4882
4883         if (! is_constant_expression(statement->case_label.expression)) {
4884                 errorf(statement->base.source_position,
4885                         "case label does not reduce to an integer constant");
4886         } else {
4887                 /* TODO: check if the case label is already known */
4888                 if (current_switch != NULL) {
4889                         /* link all cases into the switch statement */
4890                         if (current_switch->last_case == NULL) {
4891                                 current_switch->first_case =
4892                                 current_switch->last_case  = &statement->case_label;
4893                         } else {
4894                                 current_switch->last_case->next = &statement->case_label;
4895                         }
4896                 } else {
4897                         errorf(statement->base.source_position,
4898                                 "case label not within a switch statement");
4899                 }
4900         }
4901         statement->case_label.label_statement = parse_statement();
4902
4903         return statement;
4904 }
4905
4906 /**
4907  * Finds an existing default label of a switch statement.
4908  */
4909 static case_label_statement_t *
4910 find_default_label(const switch_statement_t *statement)
4911 {
4912         for (case_label_statement_t *label = statement->first_case;
4913              label != NULL;
4914                  label = label->next) {
4915                 if (label->expression == NULL)
4916                         return label;
4917         }
4918         return NULL;
4919 }
4920
4921 /**
4922  * Parse a default statement.
4923  */
4924 static statement_t *parse_default_statement(void)
4925 {
4926         eat(T_default);
4927
4928         statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4929
4930         statement->base.source_position = token.source_position;
4931
4932         expect(':');
4933         if (current_switch != NULL) {
4934                 const case_label_statement_t *def_label = find_default_label(current_switch);
4935                 if (def_label != NULL) {
4936                         errorf(HERE, "multiple default labels in one switch");
4937                         errorf(def_label->statement.source_position,
4938                                 "this is the first default label");
4939                 } else {
4940                         /* link all cases into the switch statement */
4941                         if (current_switch->last_case == NULL) {
4942                                 current_switch->first_case =
4943                                         current_switch->last_case  = &statement->case_label;
4944                         } else {
4945                                 current_switch->last_case->next = &statement->case_label;
4946                         }
4947                 }
4948         } else {
4949                 errorf(statement->base.source_position,
4950                         "'default' label not within a switch statement");
4951         }
4952         statement->label.label_statement = parse_statement();
4953
4954         return statement;
4955 }
4956
4957 /**
4958  * Return the declaration for a given label symbol or create a new one.
4959  */
4960 static declaration_t *get_label(symbol_t *symbol)
4961 {
4962         declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4963         assert(current_function != NULL);
4964         /* if we found a label in the same function, then we already created the
4965          * declaration */
4966         if(candidate != NULL
4967                         && candidate->parent_context == &current_function->context) {
4968                 return candidate;
4969         }
4970
4971         /* otherwise we need to create a new one */
4972         declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4973         declaration->namespc       = NAMESPACE_LABEL;
4974         declaration->symbol        = symbol;
4975
4976         label_push(declaration);
4977
4978         return declaration;
4979 }
4980
4981 /**
4982  * Parse a label statement.
4983  */
4984 static statement_t *parse_label_statement(void)
4985 {
4986         assert(token.type == T_IDENTIFIER);
4987         symbol_t *symbol = token.v.symbol;
4988         next_token();
4989
4990         declaration_t *label = get_label(symbol);
4991
4992         /* if source position is already set then the label is defined twice,
4993          * otherwise it was just mentioned in a goto so far */
4994         if(label->source_position.input_name != NULL) {
4995                 errorf(HERE, "duplicate label '%Y'", symbol);
4996                 errorf(label->source_position, "previous definition of '%Y' was here",
4997                        symbol);
4998         } else {
4999                 label->source_position = token.source_position;
5000         }
5001
5002         label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
5003
5004         label_statement->statement.kind            = STATEMENT_LABEL;
5005         label_statement->statement.source_position = token.source_position;
5006         label_statement->label                     = label;
5007
5008         eat(':');
5009
5010         if(token.type == '}') {
5011                 /* TODO only warn? */
5012                 errorf(HERE, "label at end of compound statement");
5013                 return (statement_t*) label_statement;
5014         } else {
5015                 label_statement->label_statement = parse_statement();
5016         }
5017
5018         return (statement_t*) label_statement;
5019 }
5020
5021 /**
5022  * Parse an if statement.
5023  */
5024 static statement_t *parse_if(void)
5025 {
5026         eat(T_if);
5027
5028         if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5029         statement->statement.kind            = STATEMENT_IF;
5030         statement->statement.source_position = token.source_position;
5031
5032         expect('(');
5033         statement->condition = parse_expression();
5034         expect(')');
5035
5036         statement->true_statement = parse_statement();
5037         if(token.type == T_else) {
5038                 next_token();
5039                 statement->false_statement = parse_statement();
5040         }
5041
5042         return (statement_t*) statement;
5043 }
5044
5045 /**
5046  * Parse a switch statement.
5047  */
5048 static statement_t *parse_switch(void)
5049 {
5050         eat(T_switch);
5051
5052         switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5053         statement->statement.kind            = STATEMENT_SWITCH;
5054         statement->statement.source_position = token.source_position;
5055
5056         expect('(');
5057         expression_t *const expr = parse_expression();
5058         type_t       *const type = promote_integer(skip_typeref(expr->base.datatype));
5059         statement->expression = create_implicit_cast(expr, type);
5060         expect(')');
5061
5062         switch_statement_t *rem = current_switch;
5063         current_switch  = statement;
5064         statement->body = parse_statement();
5065         current_switch  = rem;
5066
5067         return (statement_t*) statement;
5068 }
5069
5070 static statement_t *parse_loop_body(statement_t *const loop)
5071 {
5072         statement_t *const rem = current_loop;
5073         current_loop = loop;
5074         statement_t *const body = parse_statement();
5075         current_loop = rem;
5076         return body;
5077 }
5078
5079 /**
5080  * Parse a while statement.
5081  */
5082 static statement_t *parse_while(void)
5083 {
5084         eat(T_while);
5085
5086         while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5087         statement->statement.kind            = STATEMENT_WHILE;
5088         statement->statement.source_position = token.source_position;
5089
5090         expect('(');
5091         statement->condition = parse_expression();
5092         expect(')');
5093
5094         statement->body = parse_loop_body((statement_t*)statement);
5095
5096         return (statement_t*) statement;
5097 }
5098
5099 /**
5100  * Parse a do statement.
5101  */
5102 static statement_t *parse_do(void)
5103 {
5104         eat(T_do);
5105
5106         do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5107         statement->statement.kind            = STATEMENT_DO_WHILE;
5108         statement->statement.source_position = token.source_position;
5109
5110         statement->body = parse_loop_body((statement_t*)statement);
5111         expect(T_while);
5112         expect('(');
5113         statement->condition = parse_expression();
5114         expect(')');
5115         expect(';');
5116
5117         return (statement_t*) statement;
5118 }
5119
5120 /**
5121  * Parse a for statement.
5122  */
5123 static statement_t *parse_for(void)
5124 {
5125         eat(T_for);
5126
5127         for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5128         statement->statement.kind            = STATEMENT_FOR;
5129         statement->statement.source_position = token.source_position;
5130
5131         expect('(');
5132
5133         int         top          = environment_top();
5134         context_t  *last_context = context;
5135         set_context(&statement->context);
5136
5137         if(token.type != ';') {
5138                 if(is_declaration_specifier(&token, false)) {
5139                         parse_declaration(record_declaration);
5140                 } else {
5141                         statement->initialisation = parse_expression();
5142                         expect(';');
5143                 }
5144         } else {
5145                 expect(';');
5146         }
5147
5148         if(token.type != ';') {
5149                 statement->condition = parse_expression();
5150         }
5151         expect(';');
5152         if(token.type != ')') {
5153                 statement->step = parse_expression();
5154         }
5155         expect(')');
5156         statement->body = parse_loop_body((statement_t*)statement);
5157
5158         assert(context == &statement->context);
5159         set_context(last_context);
5160         environment_pop_to(top);
5161
5162         return (statement_t*) statement;
5163 }
5164
5165 /**
5166  * Parse a goto statement.
5167  */
5168 static statement_t *parse_goto(void)
5169 {
5170         eat(T_goto);
5171
5172         if(token.type != T_IDENTIFIER) {
5173                 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5174                 eat_statement();
5175                 return NULL;
5176         }
5177         symbol_t *symbol = token.v.symbol;
5178         next_token();
5179
5180         declaration_t *label = get_label(symbol);
5181
5182         goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5183
5184         statement->statement.kind            = STATEMENT_GOTO;
5185         statement->statement.source_position = token.source_position;
5186
5187         statement->label = label;
5188
5189         /* remember the goto's in a list for later checking */
5190         if (goto_last == NULL) {
5191                 goto_first = goto_last = statement;
5192         } else {
5193                 goto_last->next = statement;
5194         }
5195
5196         expect(';');
5197
5198         return (statement_t*) statement;
5199 }
5200
5201 /**
5202  * Parse a continue statement.
5203  */
5204 static statement_t *parse_continue(void)
5205 {
5206         statement_base_t *statement;
5207         if (current_loop == NULL) {
5208                 errorf(HERE, "continue statement not within loop");
5209                 statement = NULL;
5210         } else {
5211                 statement                  = allocate_ast_zero(sizeof(statement[0]));
5212                 statement->kind            = STATEMENT_CONTINUE;
5213                 statement->source_position = token.source_position;
5214         }
5215
5216         eat(T_continue);
5217         expect(';');
5218
5219         return (statement_t*)statement;
5220 }
5221
5222 /**
5223  * Parse a break statement.
5224  */
5225 static statement_t *parse_break(void)
5226 {
5227         statement_base_t *statement;
5228         if (current_switch == NULL && current_loop == NULL) {
5229                 errorf(HERE, "break statement not within loop or switch");
5230                 statement = NULL;
5231         } else {
5232                 statement                  = allocate_ast_zero(sizeof(statement[0]));
5233                 statement->kind            = STATEMENT_BREAK;
5234                 statement->source_position = token.source_position;
5235         }
5236
5237         eat(T_break);
5238         expect(';');
5239
5240         return (statement_t*)statement;
5241 }
5242
5243 /**
5244  * Check if a given declaration represents a local variable.
5245  */
5246 static bool is_local_var_declaration(const declaration_t *declaration) {
5247         switch ((storage_class_tag_t) declaration->storage_class) {
5248         case STORAGE_CLASS_NONE:
5249         case STORAGE_CLASS_AUTO:
5250         case STORAGE_CLASS_REGISTER: {
5251                 const type_t *type = skip_typeref(declaration->type);
5252                 if(is_type_function(type)) {
5253                         return false;
5254                 } else {
5255                         return true;
5256                 }
5257         }
5258         default:
5259                 return false;
5260         }
5261 }
5262
5263 /**
5264  * Check if a given expression represents a local variable.
5265  */
5266 static bool is_local_variable(const expression_t *expression)
5267 {
5268         if (expression->base.kind != EXPR_REFERENCE) {
5269                 return false;
5270         }
5271         const declaration_t *declaration = expression->reference.declaration;
5272         return is_local_var_declaration(declaration);
5273 }
5274
5275 /**
5276  * Parse a return statement.
5277  */
5278 static statement_t *parse_return(void)
5279 {
5280         eat(T_return);
5281
5282         return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5283
5284         statement->statement.kind            = STATEMENT_RETURN;
5285         statement->statement.source_position = token.source_position;
5286
5287         assert(is_type_function(current_function->type));
5288         function_type_t *function_type = &current_function->type->function;
5289         type_t          *return_type   = function_type->return_type;
5290
5291         expression_t *return_value = NULL;
5292         if(token.type != ';') {
5293                 return_value = parse_expression();
5294         }
5295         expect(';');
5296
5297         if(return_type == NULL)
5298                 return (statement_t*) statement;
5299         if(return_value != NULL && return_value->base.datatype == NULL)
5300                 return (statement_t*) statement;
5301
5302         return_type = skip_typeref(return_type);
5303
5304         if(return_value != NULL) {
5305                 type_t *return_value_type = skip_typeref(return_value->base.datatype);
5306
5307                 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5308                                 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5309                         warningf(statement->statement.source_position,
5310                                 "'return' with a value, in function returning void");
5311                         return_value = NULL;
5312                 } else {
5313                         if(return_type != NULL) {
5314                                 semantic_assign(return_type, &return_value, "'return'");
5315                         }
5316                 }
5317                 /* check for returning address of a local var */
5318                 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5319                         const expression_t *expression = return_value->unary.value;
5320                         if (is_local_variable(expression)) {
5321                                 warningf(statement->statement.source_position,
5322                                         "function returns address of local variable");
5323                         }
5324                 }
5325         } else {
5326                 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5327                         warningf(statement->statement.source_position,
5328                                 "'return' without value, in function returning non-void");
5329                 }
5330         }
5331         statement->return_value = return_value;
5332
5333         return (statement_t*) statement;
5334 }
5335
5336 /**
5337  * Parse a declaration statement.
5338  */
5339 static statement_t *parse_declaration_statement(void)
5340 {
5341         statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5342
5343         statement->base.source_position = token.source_position;
5344
5345         declaration_t *before = last_declaration;
5346         parse_declaration(record_declaration);
5347
5348         if(before == NULL) {
5349                 statement->declaration.declarations_begin = context->declarations;
5350         } else {
5351                 statement->declaration.declarations_begin = before->next;
5352         }
5353         statement->declaration.declarations_end = last_declaration;
5354
5355         return statement;
5356 }
5357
5358 /**
5359  * Parse an expression statement, ie. expr ';'.
5360  */
5361 static statement_t *parse_expression_statement(void)
5362 {
5363         statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5364
5365         statement->base.source_position  = token.source_position;
5366         statement->expression.expression = parse_expression();
5367
5368         expect(';');
5369
5370         return statement;
5371 }
5372
5373 /**
5374  * Parse a statement.
5375  */
5376 static statement_t *parse_statement(void)
5377 {
5378         statement_t   *statement = NULL;
5379
5380         /* declaration or statement */
5381         switch(token.type) {
5382         case T_asm:
5383                 statement = parse_asm_statement();
5384                 break;
5385
5386         case T_case:
5387                 statement = parse_case_statement();
5388                 break;
5389
5390         case T_default:
5391                 statement = parse_default_statement();
5392                 break;
5393
5394         case '{':
5395                 statement = parse_compound_statement();
5396                 break;
5397
5398         case T_if:
5399                 statement = parse_if();
5400                 break;
5401
5402         case T_switch:
5403                 statement = parse_switch();
5404                 break;
5405
5406         case T_while:
5407                 statement = parse_while();
5408                 break;
5409
5410         case T_do:
5411                 statement = parse_do();
5412                 break;
5413
5414         case T_for:
5415                 statement = parse_for();
5416                 break;
5417
5418         case T_goto:
5419                 statement = parse_goto();
5420                 break;
5421
5422         case T_continue:
5423                 statement = parse_continue();
5424                 break;
5425
5426         case T_break:
5427                 statement = parse_break();
5428                 break;
5429
5430         case T_return:
5431                 statement = parse_return();
5432                 break;
5433
5434         case ';':
5435                 next_token();
5436                 statement = NULL;
5437                 break;
5438
5439         case T_IDENTIFIER:
5440                 if(look_ahead(1)->type == ':') {
5441                         statement = parse_label_statement();
5442                         break;
5443                 }
5444
5445                 if(is_typedef_symbol(token.v.symbol)) {
5446                         statement = parse_declaration_statement();
5447                         break;
5448                 }
5449
5450                 statement = parse_expression_statement();
5451                 break;
5452
5453         case T___extension__:
5454                 /* this can be a prefix to a declaration or an expression statement */
5455                 /* we simply eat it now and parse the rest with tail recursion */
5456                 do {
5457                         next_token();
5458                 } while(token.type == T___extension__);
5459                 statement = parse_statement();
5460                 break;
5461
5462         DECLARATION_START
5463                 statement = parse_declaration_statement();
5464                 break;
5465
5466         default:
5467                 statement = parse_expression_statement();
5468                 break;
5469         }
5470
5471         assert(statement == NULL
5472                         || statement->base.source_position.input_name != NULL);
5473
5474         return statement;
5475 }
5476
5477 /**
5478  * Parse a compound statement.
5479  */
5480 static statement_t *parse_compound_statement(void)
5481 {
5482         compound_statement_t *compound_statement
5483                 = allocate_ast_zero(sizeof(compound_statement[0]));
5484         compound_statement->statement.kind            = STATEMENT_COMPOUND;
5485         compound_statement->statement.source_position = token.source_position;
5486
5487         eat('{');
5488
5489         int        top          = environment_top();
5490         context_t *last_context = context;
5491         set_context(&compound_statement->context);
5492
5493         statement_t *last_statement = NULL;
5494
5495         while(token.type != '}' && token.type != T_EOF) {
5496                 statement_t *statement = parse_statement();
5497                 if(statement == NULL)
5498                         continue;
5499
5500                 if(last_statement != NULL) {
5501                         last_statement->base.next = statement;
5502                 } else {
5503                         compound_statement->statements = statement;
5504                 }
5505
5506                 while(statement->base.next != NULL)
5507                         statement = statement->base.next;
5508
5509                 last_statement = statement;
5510         }
5511
5512         if(token.type == '}') {
5513                 next_token();
5514         } else {
5515                 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
5516         }
5517
5518         assert(context == &compound_statement->context);
5519         set_context(last_context);
5520         environment_pop_to(top);
5521
5522         return (statement_t*) compound_statement;
5523 }
5524
5525 /**
5526  * Initialize builtin types.
5527  */
5528 static void initialize_builtin_types(void)
5529 {
5530         type_intmax_t    = make_global_typedef("__intmax_t__",      type_long_long);
5531         type_size_t      = make_global_typedef("__SIZE_TYPE__",     type_unsigned_long);
5532         type_ssize_t     = make_global_typedef("__SSIZE_TYPE__",    type_long);
5533         type_ptrdiff_t   = make_global_typedef("__PTRDIFF_TYPE__",  type_long);
5534         type_uintmax_t   = make_global_typedef("__uintmax_t__",     type_unsigned_long_long);
5535         type_uptrdiff_t  = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5536         type_wchar_t     = make_global_typedef("__WCHAR_TYPE__",    type_int);
5537         type_wint_t      = make_global_typedef("__WINT_TYPE__",     type_int);
5538
5539         type_intmax_t_ptr  = make_pointer_type(type_intmax_t,  TYPE_QUALIFIER_NONE);
5540         type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5541         type_ssize_t_ptr   = make_pointer_type(type_ssize_t,   TYPE_QUALIFIER_NONE);
5542         type_wchar_t_ptr   = make_pointer_type(type_wchar_t,   TYPE_QUALIFIER_NONE);
5543 }
5544
5545 /**
5546  * Parse a translation unit.
5547  */
5548 static translation_unit_t *parse_translation_unit(void)
5549 {
5550         translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5551
5552         assert(global_context == NULL);
5553         global_context = &unit->context;
5554
5555         assert(context == NULL);
5556         set_context(&unit->context);
5557
5558         initialize_builtin_types();
5559
5560         while(token.type != T_EOF) {
5561                 if (token.type == ';') {
5562                         /* TODO error in strict mode */
5563                         warningf(HERE, "stray ';' outside of function");
5564                         next_token();
5565                 } else {
5566                         parse_external_declaration();
5567                 }
5568         }
5569
5570         assert(context == &unit->context);
5571         context          = NULL;
5572         last_declaration = NULL;
5573
5574         assert(global_context == &unit->context);
5575         global_context = NULL;
5576
5577         return unit;
5578 }
5579
5580 /**
5581  * Parse the input.
5582  *
5583  * @return  the translation unit or NULL if errors occurred.
5584  */
5585 translation_unit_t *parse(void)
5586 {
5587         environment_stack = NEW_ARR_F(stack_entry_t, 0);
5588         label_stack       = NEW_ARR_F(stack_entry_t, 0);
5589         diagnostic_count  = 0;
5590         error_count       = 0;
5591         warning_count     = 0;
5592
5593         type_set_output(stderr);
5594         ast_set_output(stderr);
5595
5596         lookahead_bufpos = 0;
5597         for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5598                 next_token();
5599         }
5600         translation_unit_t *unit = parse_translation_unit();
5601
5602         DEL_ARR_F(environment_stack);
5603         DEL_ARR_F(label_stack);
5604
5605         if(error_count > 0)
5606                 return NULL;
5607
5608         return unit;
5609 }
5610
5611 /**
5612  * Initialize the parser.
5613  */
5614 void init_parser(void)
5615 {
5616         init_expression_parsers();
5617         obstack_init(&temp_obst);
5618
5619         symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5620         type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5621 }
5622
5623 /**
5624  * Terminate the parser.
5625  */
5626 void exit_parser(void)
5627 {
5628         obstack_free(&temp_obst, NULL);
5629 }