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