improvements in statement parsing, improvements in ast printing
[cparser] / parser.c
1 #include <config.h>
2
3 #include <assert.h>
4 #include <stdarg.h>
5
6 #include "parser.h"
7 #include "lexer.h"
8 #include "token_t.h"
9 #include "type_t.h"
10 #include "type_hash.h"
11 #include "ast_t.h"
12 #include "adt/bitfiddle.h"
13 #include "adt/error.h"
14 #include "adt/array.h"
15
16 //#define PRINT_TOKENS
17 //#define ABORT_ON_ERROR
18 #define MAX_LOOKAHEAD 2
19
20 struct environment_entry_t {
21         symbol_t      *symbol;
22         declaration_t *old_declaration;
23         const void    *old_context;
24 };
25
26 static token_t               token;
27 static token_t               lookahead_buffer[MAX_LOOKAHEAD];
28 static int                   lookahead_bufpos;
29 static struct obstack        environment_obstack;
30 static environment_entry_t **environment_stack = NULL;
31 static context_t            *context           = NULL;
32 static declaration_t        *last_declaration  = NULL;
33 static struct obstack        temp_obst;
34
35 static
36 statement_t *parse_compound_statement(void);
37 static
38 statement_t *parse_statement(void);
39
40 static
41 expression_t *parse_sub_expression(unsigned precedence);
42 static
43 expression_t *parse_expression(void);
44
45 static inline
46 void *allocate_ast_zero(size_t size)
47 {
48         void *res = allocate_ast(size);
49         memset(res, 0, size);
50         return res;
51 }
52
53 static inline
54 void *allocate_type_zero(size_t size)
55 {
56         void *res = obstack_alloc(type_obst, size);
57         memset(res, 0, size);
58         return res;
59 }
60
61 /**
62  * returns the top element of the environment stack
63  */
64 static inline
65 size_t environment_top(void)
66 {
67         return ARR_LEN(environment_stack);
68 }
69
70
71
72 static inline
73 void next_token(void)
74 {
75         token                              = lookahead_buffer[lookahead_bufpos];
76         lookahead_buffer[lookahead_bufpos] = lexer_token;
77         lexer_next_token();
78
79         lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
80
81 #ifdef PRINT_TOKENS
82         print_token(stderr, &token);
83         fprintf(stderr, "\n");
84 #endif
85 }
86
87 static inline
88 const token_t *look_ahead(int num)
89 {
90         assert(num > 0 && num <= MAX_LOOKAHEAD);
91         int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
92         return & lookahead_buffer[pos];
93 }
94
95 static inline
96 void eat(token_type_t type)
97 {
98         assert(token.type == type);
99         next_token();
100 }
101
102 void parser_print_error_prefix_pos(const source_position_t source_position)
103 {
104     fputs(source_position.input_name, stderr);
105     fputc(':', stderr);
106     fprintf(stderr, "%d", source_position.linenr);
107     fputs(": error: ", stderr);
108 #ifdef ABORT_ON_ERROR
109         abort();
110 #endif
111 }
112
113 void parser_print_error_prefix(void)
114 {
115         parser_print_error_prefix_pos(token.source_position);
116 }
117
118 static
119 void parse_error(const char *message)
120 {
121         parser_print_error_prefix();
122         fprintf(stderr, "parse error: %s\n", message);
123 }
124
125 static
126 void parse_error_expected(const char *message, ...)
127 {
128         va_list args;
129         int first = 1;
130
131         if(message != NULL) {
132                 parser_print_error_prefix();
133                 fprintf(stderr, "%s\n", message);
134         }
135         parser_print_error_prefix();
136         fputs("Parse error: got ", stderr);
137         print_token(stderr, &token);
138         fputs(", expected ", stderr);
139
140         va_start(args, message);
141         token_type_t token_type = va_arg(args, token_type_t);
142         while(token_type != 0) {
143                 if(first == 1) {
144                         first = 0;
145                 } else {
146                         fprintf(stderr, ", ");
147                 }
148                 print_token_type(stderr, token_type);
149                 token_type = va_arg(args, token_type_t);
150         }
151         va_end(args);
152         fprintf(stderr, "\n");
153 }
154
155 static void eat_block(void)
156 {
157         if(token.type == '{')
158                 next_token();
159
160         while(token.type != '}') {
161                 if(token.type == T_EOF)
162                         return;
163                 if(token.type == '{') {
164                         eat_block();
165                         continue;
166                 }
167                 next_token();
168         }
169         eat('}');
170 }
171
172 static void eat_statement(void)
173 {
174         while(token.type != ';') {
175                 if(token.type == T_EOF)
176                         return;
177                 if(token.type == '}')
178                         return;
179                 if(token.type == '{') {
180                         eat_block();
181                         continue;
182                 }
183                 next_token();
184         }
185         eat(';');
186 }
187
188 static void eat_brace(void)
189 {
190         if(token.type == '(')
191                 next_token();
192
193         while(token.type != ')') {
194                 if(token.type == T_EOF)
195                         return;
196                 if(token.type == '{') {
197                         eat_block();
198                         continue;
199                 }
200                 next_token();
201         }
202         eat(')');
203 }
204
205 #define expect(expected)                           \
206     if(UNLIKELY(token.type != (expected))) {       \
207         parse_error_expected(NULL, (expected), 0); \
208         eat_statement();                           \
209         return NULL;                               \
210     }                                              \
211     next_token();
212
213 #define expect_void(expected)                      \
214     if(UNLIKELY(token.type != (expected))) {       \
215         parse_error_expected(NULL, (expected), 0); \
216         eat_statement();                           \
217         return;                                    \
218     }                                              \
219     next_token();
220
221 static void set_context(context_t *new_context)
222 {
223         context = new_context;
224
225         declaration_t *declaration = new_context->declarations;
226         if(declaration != NULL) {
227                 while(1) {
228                         if(declaration->next == NULL)
229                                 break;
230                         declaration = declaration->next;
231                 }
232         }
233
234         last_declaration = declaration;
235 }
236
237 /**
238  * called when we find a 2nd declarator for an identifier we already have a
239  * declarator for
240  */
241 static int is_compatible_declaration (declaration_t *declaration,
242                                       declaration_t *previous)
243 {
244         /* TODO: not correct yet */
245         return declaration->type == previous->type;
246 }
247
248 /**
249  * pushs an environment_entry on the environment stack and links the
250  * corresponding symbol to the new entry
251  */
252 static inline
253 declaration_t *environment_push(declaration_t *declaration, const void *context)
254 {
255         environment_entry_t *entry
256                 = obstack_alloc(&environment_obstack, sizeof(entry[0]));
257         memset(entry, 0, sizeof(entry[0]));
258
259         int top = ARR_LEN(environment_stack);
260         ARR_RESIZE(environment_stack, top + 1);
261         environment_stack[top] = entry;
262
263         assert(declaration->source_position.input_name != NULL);
264
265         symbol_t *symbol = declaration->symbol;
266         assert(declaration != symbol->declaration);
267
268         if(symbol->context == context) {
269                 declaration_t *previous_declaration = symbol->declaration;
270                 if(symbol->declaration != NULL) {
271                         if(!is_compatible_declaration(declaration, previous_declaration)) {
272                                 parser_print_error_prefix_pos(declaration->source_position);
273                                 fprintf(stderr, "definition of symbol '%s' with type ",
274                                         declaration->symbol->string);
275                                 print_type(declaration->type, NULL);
276                                 fputc('\n', stderr);
277                                 parser_print_error_prefix_pos(
278                                                 previous_declaration->source_position);
279                                 fprintf(stderr, "is incompatible with previous declaration "
280                                         "of type ");
281                                 print_type(previous_declaration->type, NULL);
282                                 fputc('\n', stderr);
283                         }
284                         return previous_declaration;
285                 }
286         }
287
288         entry->old_declaration = symbol->declaration;
289         entry->old_context     = symbol->context;
290         entry->symbol          = symbol;
291         symbol->declaration    = declaration;
292         symbol->context        = context;
293
294         return declaration;
295 }
296
297 /**
298  * pops symbols from the environment stack until @p new_top is the top element
299  */
300 static inline
301 void environment_pop_to(size_t new_top)
302 {
303         environment_entry_t *entry = NULL;
304         size_t top = ARR_LEN(environment_stack);
305         size_t i;
306
307         if(new_top == top)
308                 return;
309
310         assert(new_top < top);
311         i = top;
312         do {
313                 entry = environment_stack[i - 1];
314
315                 symbol_t *symbol = entry->symbol;
316
317                 symbol->declaration = entry->old_declaration;
318                 symbol->context     = entry->old_context;
319
320                 --i;
321         } while(i != new_top);
322         obstack_free(&environment_obstack, entry);
323
324         ARR_SHRINKLEN(environment_stack, (int) new_top);
325 }
326
327
328
329 static expression_t *parse_constant_expression(void)
330 {
331         /* TODO: not correct yet */
332         return parse_expression();
333 }
334
335 static expression_t *parse_assignment_expression(void)
336 {
337         /* TODO: not correct yet */
338         return parse_expression();
339 }
340
341 static void parse_compound_type_entries(void);
342 static void parse_declarator(declaration_t *declaration,
343                              storage_class_t storage_class, type_t *type,
344                              int may_be_abstract);
345 static declaration_t *record_declaration(declaration_t *declaration);
346
347 typedef struct declaration_specifiers_t  declaration_specifiers_t;
348 struct declaration_specifiers_t {
349         storage_class_t  storage_class;
350         type_t          *type;
351 };
352
353 static compound_type_t *find_compound_type(compound_type_t *types,
354                                            const symbol_t *symbol)
355 {
356         compound_type_t *type = types;
357         for( ; type != NULL; type = type->next) {
358                 if(type->symbol == symbol)
359                         return type;
360         }
361
362         return NULL;
363 }
364
365 static type_t *parse_compound_type_specifier(int is_struct)
366 {
367         if(is_struct) {
368                 eat(T_struct);
369         } else {
370                 eat(T_union);
371         }
372
373         symbol_t        *symbol        = NULL;
374         compound_type_t *compound_type = NULL;
375
376         if(token.type == T_IDENTIFIER) {
377                 symbol = token.v.symbol;
378                 next_token();
379
380                 if(context != NULL) {
381                         if(is_struct) {
382                                 compound_type = find_compound_type(context->structs, symbol);
383                         } else {
384                                 compound_type = find_compound_type(context->unions, symbol);
385                         }
386                 }
387         } else if(token.type != '{') {
388                 if(is_struct) {
389                         parse_error_expected("problem while parsing struct type specifiers",
390                                              T_IDENTIFIER, '{', 0);
391                 } else {
392                         parse_error_expected("problem while parsing union type specifiers",
393                                              T_IDENTIFIER, '{', 0);
394                 }
395
396                 return NULL;
397         }
398
399         if(compound_type == NULL) {
400                 compound_type = allocate_type_zero(sizeof(compound_type[0]));
401
402                 if(is_struct) {
403                         compound_type->type.type = TYPE_COMPOUND_STRUCT;
404                 } else {
405                         compound_type->type.type = TYPE_COMPOUND_UNION;
406                 }
407                 compound_type->source_position = token.source_position;
408                 compound_type->symbol          = symbol;
409         }
410
411         if(token.type == '{') {
412                 if(compound_type->defined) {
413                         parser_print_error_prefix();
414                         fprintf(stderr, "multiple definition of %s %s\n",
415                                         is_struct ? "struct" : "union", symbol->string);
416                         compound_type->context.declarations = NULL;
417                 }
418                 compound_type->defined = 1;
419
420                 int         top          = environment_top();
421                 context_t  *last_context = context;
422                 set_context(&compound_type->context);
423
424                 parse_compound_type_entries();
425
426                 assert(context == &compound_type->context);
427                 set_context(last_context);
428                 environment_pop_to(top);
429         }
430
431         return (type_t*) compound_type;
432 }
433
434 static enum_entry_t *parse_enum_type_entries(void)
435 {
436         eat('{');
437
438         if(token.type == '}') {
439                 next_token();
440                 parse_error("empty enum not allowed");
441                 return NULL;
442         }
443
444         enum_entry_t *result     = NULL;
445         enum_entry_t *last_entry = NULL;
446         do {
447                 enum_entry_t *entry = allocate_ast_zero(sizeof(entry[0]));
448                 if(token.type != T_IDENTIFIER) {
449                         parse_error_expected("problem while parsing enum entry",
450                                              T_IDENTIFIER, 0);
451                         eat_block();
452                         return result;
453                 }
454                 entry->symbol = token.v.symbol;
455                 next_token();
456
457                 if(token.type == '=') {
458                         next_token();
459                         entry->value = parse_constant_expression();
460                 }
461
462                 if(last_entry != NULL) {
463                         last_entry->next = entry;
464                 } else {
465                         result = entry;
466                 }
467                 last_entry = entry;
468
469                 if(token.type != ',')
470                         break;
471                 next_token();
472         } while(token.type != '}');
473
474         expect('}');
475         return result;
476 }
477
478 static type_t *parse_enum_specifier(void)
479 {
480         eat(T_enum);
481
482         enum_type_t *enum_type     = allocate_type_zero(sizeof(enum_type[0]));
483         enum_type->type.type       = TYPE_ENUM;
484         enum_type->source_position = token.source_position;
485
486         /* TODO: rewrite to the same style as struct/union above to handle
487          * type identities correctly
488          */
489
490         if(token.type == T_IDENTIFIER) {
491                 enum_type->symbol = token.v.symbol;
492                 next_token();
493                 if(token.type == '{') {
494                         enum_type->entries = parse_enum_type_entries();
495                 }
496         } else if(token.type == '{') {
497                 enum_type->entries = parse_enum_type_entries();
498         } else {
499                 parse_error_expected("problem while parsing enum type specifiers",
500                                      T_IDENTIFIER, '{');
501         }
502
503         return (type_t*) enum_type;
504 }
505
506 static
507 const char *parse_string_literals(void)
508 {
509         assert(token.type == T_STRING_LITERAL);
510         const char *result = token.v.string;
511
512         next_token();
513
514         while(token.type == T_STRING_LITERAL) {
515                 result = concat_strings(result, token.v.string);
516                 next_token();
517         }
518
519         return result;
520 }
521
522 static
523 void parse_attributes(void)
524 {
525         while(1) {
526                 switch(token.type) {
527                 case T___attribute__:
528                         next_token();
529
530                         expect_void('(');
531                         int depth = 1;
532                         while(depth > 0) {
533                                 switch(token.type) {
534                                 case T_EOF:
535                                         parse_error("EOF while parsing attribute");
536                                         break;
537                                 case '(':
538                                         next_token();
539                                         depth++;
540                                         break;
541                                 case ')':
542                                         next_token();
543                                         depth--;
544                                         break;
545                                 default:
546                                         next_token();
547                                 }
548                         }
549                         break;
550                 case T_asm:
551                         next_token();
552                         expect_void('(');
553                         if(token.type != T_STRING_LITERAL) {
554                                 parse_error_expected("while parsing assembler attribute",
555                                                      T_STRING_LITERAL);
556                                 eat_brace();
557                                 break;
558                         } else {
559                                 parse_string_literals();
560                         }
561                         expect_void(')');
562                         break;
563                 default:
564                         goto attributes_finished;
565                 }
566         }
567
568 attributes_finished:
569         ;
570 }
571
572 typedef enum {
573         SPECIFIER_SIGNED    = 1 << 0,
574         SPECIFIER_UNSIGNED  = 1 << 1,
575         SPECIFIER_LONG      = 1 << 2,
576         SPECIFIER_INT       = 1 << 3,
577         SPECIFIER_DOUBLE    = 1 << 4,
578         SPECIFIER_CHAR      = 1 << 5,
579         SPECIFIER_SHORT     = 1 << 6,
580         SPECIFIER_LONG_LONG = 1 << 7,
581         SPECIFIER_FLOAT     = 1 << 8,
582         SPECIFIER_BOOL      = 1 << 9,
583         SPECIFIER_VOID      = 1 << 10,
584 #ifdef PROVIDE_COMPLEX
585         SPECIFIER_COMPLEX   = 1 << 11,
586 #endif
587 #ifdef PROVIDE_IMAGINARY
588         SPECIFIER_IMAGINARY = 1 << 12,
589 #endif
590 } specifiers_t;
591
592 #define STORAGE_CLASSES     \
593         case T_typedef:         \
594         case T_extern:          \
595         case T_static:          \
596         case T_auto:            \
597         case T_register:
598
599 #define TYPE_QUALIFIERS     \
600         case T_const:           \
601         case T_restrict:        \
602         case T_volatile:        \
603         case T_inline:          \
604         case T___extension__:
605
606 #ifdef PROVIDE_COMPLEX
607 #define COMPLEX_SPECIFIERS  \
608         case T__Complex:
609 #else
610 #define COMPLEX_SPECIFIERS
611 #endif
612
613 #ifdef PROVIDE_IMAGINARY
614 #define IMAGINARY_SPECIFIERS \
615         case T__Imaginary:
616 #else
617 #define IMAGINARY_SPECIFIERS
618 #endif
619
620 #define TYPE_SPECIFIERS     \
621         case T_void:            \
622         case T_char:            \
623         case T_short:           \
624         case T_int:             \
625         case T_long:            \
626         case T_float:           \
627         case T_double:          \
628         case T_signed:          \
629         case T_unsigned:        \
630         case T__Bool:           \
631         case T_struct:          \
632         case T_union:           \
633         case T_enum:            \
634         COMPLEX_SPECIFIERS      \
635         IMAGINARY_SPECIFIERS
636
637 #define DECLARATION_START   \
638         STORAGE_CLASSES         \
639         TYPE_QUALIFIERS         \
640         TYPE_SPECIFIERS
641
642 static
643 type_t *create_builtin_type(symbol_t *symbol)
644 {
645         builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
646         type->type.type      = TYPE_BUILTIN;
647         type->symbol         = symbol;
648
649         type_t *result = typehash_insert((type_t*) type);
650         if(result != (type_t*) type) {
651                 obstack_free(type_obst, type);
652         }
653
654         return result;
655 }
656
657 static
658 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
659 {
660         declaration_t *declaration;
661         type_t        *type            = NULL;
662         unsigned       type_qualifiers = 0;
663         unsigned       type_specifiers = 0;
664         int            newtype         = 0;
665
666         while(1) {
667                 switch(token.type) {
668
669                 /* storage class */
670 #define MATCH_STORAGE_CLASS(token, class)                                \
671                 case token:                                                      \
672                         if(specifiers->storage_class != STORAGE_CLASS_NONE) {        \
673                                 parse_error("multiple storage classes in declaration "   \
674                                             "specifiers");                               \
675                         }                                                            \
676                         specifiers->storage_class = class;                           \
677                         next_token();                                                \
678                         break;
679
680                 MATCH_STORAGE_CLASS(T_typedef,  STORAGE_CLASS_TYPEDEF)
681                 MATCH_STORAGE_CLASS(T_extern,   STORAGE_CLASS_EXTERN)
682                 MATCH_STORAGE_CLASS(T_static,   STORAGE_CLASS_STATIC)
683                 MATCH_STORAGE_CLASS(T_auto,     STORAGE_CLASS_AUTO)
684                 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
685
686                 /* type qualifiers */
687 #define MATCH_TYPE_QUALIFIER(token, qualifier)                          \
688                 case token:                                                     \
689                         type_qualifiers |= qualifier;                               \
690                         next_token();                                               \
691                         break;
692
693                 MATCH_TYPE_QUALIFIER(T_const,    TYPE_QUALIFIER_CONST);
694                 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
695                 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
696                 MATCH_TYPE_QUALIFIER(T_inline,   TYPE_QUALIFIER_INLINE);
697
698                 case T___extension__:
699                         /* TODO */
700                         next_token();
701                         break;
702
703                 /* type specifiers */
704 #define MATCH_SPECIFIER(token, specifier, name)                         \
705                 case token:                                                     \
706                         next_token();                                               \
707                         if(type_specifiers & specifier) {                           \
708                                 parse_error("multiple " name " type specifiers given"); \
709                         } else {                                                    \
710                                 type_specifiers |= specifier;                           \
711                         }                                                           \
712                         break;
713
714                 MATCH_SPECIFIER(T_void,       SPECIFIER_VOID,      "void")
715                 MATCH_SPECIFIER(T_char,       SPECIFIER_CHAR,      "char")
716                 MATCH_SPECIFIER(T_short,      SPECIFIER_SHORT,     "short")
717                 MATCH_SPECIFIER(T_int,        SPECIFIER_INT,       "int")
718                 MATCH_SPECIFIER(T_float,      SPECIFIER_FLOAT,     "float")
719                 MATCH_SPECIFIER(T_double,     SPECIFIER_DOUBLE,    "double")
720                 MATCH_SPECIFIER(T_signed,     SPECIFIER_SIGNED,    "signed")
721                 MATCH_SPECIFIER(T_unsigned,   SPECIFIER_UNSIGNED,  "unsigned")
722                 MATCH_SPECIFIER(T__Bool,      SPECIFIER_BOOL,      "_Bool")
723 #ifdef PROVIDE_COMPLEX
724                 MATCH_SPECIFIER(T__Complex,   SPECIFIER_COMPLEX,   "_Complex")
725 #endif
726 #ifdef PROVIDE_IMAGINARY
727                 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
728 #endif
729                 case T_long:
730                         next_token();
731                         if(type_specifiers & SPECIFIER_LONG_LONG) {
732                                 parse_error("multiple type specifiers given");
733                         } else if(type_specifiers & SPECIFIER_LONG) {
734                                 type_specifiers |= SPECIFIER_LONG_LONG;
735                         } else {
736                                 type_specifiers |= SPECIFIER_LONG;
737                         }
738                         break;
739
740                 /* TODO: if type != NULL for the following rules issue an error */
741                 case T_struct:
742                         type = parse_compound_type_specifier(1);
743                         break;
744                 case T_union:
745                         type = parse_compound_type_specifier(0);
746                         break;
747                 case T_enum:
748                         type = parse_enum_specifier();
749                         break;
750                 case T___builtin_va_list:
751                         type = create_builtin_type(token.v.symbol);
752                         next_token();
753                         break;
754
755                 case T___attribute__:
756                         /* TODO */
757                         parse_attributes();
758                         break;
759
760                 case T_IDENTIFIER:
761                         declaration = token.v.symbol->declaration;
762                         if(declaration == NULL ||
763                                         declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
764                                 goto finish_specifiers;
765                         }
766
767                         type = declaration->type;
768                         assert(type != NULL);
769                         next_token();
770                         break;
771
772                 /* function specifier */
773                 default:
774                         goto finish_specifiers;
775                 }
776         }
777
778 finish_specifiers:
779
780         if(type == NULL) {
781                 atomic_type_type_t atomic_type;
782
783                 /* match valid basic types */
784                 switch(type_specifiers) {
785                 case SPECIFIER_VOID:
786                         atomic_type = ATOMIC_TYPE_VOID;
787                         break;
788                 case SPECIFIER_CHAR:
789                         atomic_type = ATOMIC_TYPE_CHAR;
790                         break;
791                 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
792                         atomic_type = ATOMIC_TYPE_SCHAR;
793                         break;
794                 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
795                         atomic_type = ATOMIC_TYPE_UCHAR;
796                         break;
797                 case SPECIFIER_SHORT:
798                 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
799                 case SPECIFIER_SHORT | SPECIFIER_INT:
800                 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
801                         atomic_type = ATOMIC_TYPE_SHORT;
802                         break;
803                 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
804                 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
805                         atomic_type = ATOMIC_TYPE_USHORT;
806                         break;
807                 case SPECIFIER_INT:
808                 case SPECIFIER_SIGNED:
809                 case SPECIFIER_SIGNED | SPECIFIER_INT:
810                         atomic_type = ATOMIC_TYPE_INT;
811                         break;
812                 case SPECIFIER_UNSIGNED:
813                 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
814                         atomic_type = ATOMIC_TYPE_UINT;
815                         break;
816                 case SPECIFIER_LONG:
817                 case SPECIFIER_SIGNED | SPECIFIER_LONG:
818                 case SPECIFIER_LONG | SPECIFIER_INT:
819                 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
820                         atomic_type = ATOMIC_TYPE_LONG;
821                         break;
822                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
823                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
824                         atomic_type = ATOMIC_TYPE_ULONG;
825                         break;
826                 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
827                 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
828                 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
829                 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
830                         | SPECIFIER_INT:
831                         atomic_type = ATOMIC_TYPE_LONGLONG;
832                         break;
833                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
834                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
835                         | SPECIFIER_INT:
836                         atomic_type = ATOMIC_TYPE_ULONGLONG;
837                         break;
838                 case SPECIFIER_FLOAT:
839                         atomic_type = ATOMIC_TYPE_FLOAT;
840                         break;
841                 case SPECIFIER_DOUBLE:
842                         atomic_type = ATOMIC_TYPE_DOUBLE;
843                         break;
844                 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
845                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
846                         break;
847                 case SPECIFIER_BOOL:
848                         atomic_type = ATOMIC_TYPE_BOOL;
849                         break;
850 #ifdef PROVIDE_COMPLEX
851                 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
852                         atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
853                         break;
854                 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
855                         atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
856                         break;
857                 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
858                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
859                         break;
860 #endif
861 #ifdef PROVIDE_IMAGINARY
862                 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
863                         atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
864                         break;
865                 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
866                         atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
867                         break;
868                 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
869                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
870                         break;
871 #endif
872                 default:
873                         /* invalid specifier combination, give an error message */
874                         if(type_specifiers == 0) {
875                                 parse_error("no type specifiers given in declaration");
876                         } else if((type_specifiers & SPECIFIER_SIGNED) &&
877                                   (type_specifiers & SPECIFIER_UNSIGNED)) {
878                                 parse_error("signed and unsigned specifiers gives");
879                         } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
880                                 parse_error("only integer types can be signed or unsigned");
881                         } else {
882                                 parse_error("multiple datatypes in declaration");
883                         }
884                         atomic_type = ATOMIC_TYPE_INVALID;
885                 }
886
887                 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
888                 atype->type.type     = TYPE_ATOMIC;
889                 atype->atype         = atomic_type;
890                 newtype              = 1;
891
892                 type = (type_t*) atype;
893         } else {
894                 if(type_specifiers != 0) {
895                         parse_error("multiple datatypes in declaration");
896                 }
897         }
898
899         type->qualifiers = type_qualifiers;
900
901         type_t *result = typehash_insert(type);
902         if(newtype && result != (type_t*) type) {
903                 obstack_free(type_obst, type);
904         }
905
906         specifiers->type = result;
907 }
908
909 static
910 unsigned parse_type_qualifiers(void)
911 {
912         unsigned type_qualifiers = 0;
913
914         while(1) {
915                 switch(token.type) {
916                 /* type qualifiers */
917                 MATCH_TYPE_QUALIFIER(T_const,    TYPE_QUALIFIER_CONST);
918                 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
919                 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
920                 MATCH_TYPE_QUALIFIER(T_inline,   TYPE_QUALIFIER_INLINE);
921
922                 default:
923                         return type_qualifiers;
924                 }
925         }
926 }
927
928 typedef struct parsed_pointer_t parsed_pointer_t;
929 struct parsed_pointer_t {
930         unsigned          type_qualifiers;
931         parsed_pointer_t *next;
932 };
933
934 static
935 parsed_pointer_t *parse_pointers(void)
936 {
937         parsed_pointer_t *result       = NULL;
938         parsed_pointer_t *last_pointer = NULL;
939
940         while(token.type == '*') {
941                 next_token();
942                 parsed_pointer_t *pointer
943                         = obstack_alloc(&temp_obst, sizeof(pointer[0]));
944                 pointer->type_qualifiers = parse_type_qualifiers();
945
946                 if(last_pointer != NULL) {
947                         last_pointer->next = pointer;
948                 } else {
949                         result = pointer;
950                 }
951                 last_pointer = pointer;
952         }
953
954         return result;
955 }
956
957 static
958 type_t *make_pointers(type_t *type, parsed_pointer_t *pointer)
959 {
960         for( ; pointer != NULL; pointer = pointer->next) {
961                 pointer_type_t *pointer_type
962                         = allocate_type_zero(sizeof(pointer_type[0]));
963                 pointer_type->type.type       = TYPE_POINTER;
964                 pointer_type->points_to       = type;
965                 pointer_type->type.qualifiers = pointer->type_qualifiers;
966
967                 type_t *result = typehash_insert((type_t*) pointer_type);
968                 if(result != (type_t*) pointer_type) {
969                         obstack_free(type_obst, pointer_type);
970                 }
971
972                 type = result;
973         }
974
975         return type;
976 }
977
978 static
979 void parse_identifier_list(void)
980 {
981         while(1) {
982                 if(token.type != T_IDENTIFIER) {
983                         parse_error_expected("problem while parsing parameter identifier "
984                                              "list", T_IDENTIFIER, 0);
985                         return;
986                 }
987                 next_token();
988                 if(token.type != ',')
989                         break;
990                 next_token();
991         }
992 }
993
994 static
995 declaration_t *parse_parameter(void)
996 {
997         declaration_specifiers_t specifiers;
998         memset(&specifiers, 0, sizeof(specifiers));
999
1000         parse_declaration_specifiers(&specifiers);
1001
1002         declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1003         parse_declarator(declaration, specifiers.storage_class,
1004                          specifiers.type, 1);
1005
1006         return declaration;
1007 }
1008
1009 static
1010 declaration_t *parse_parameters(method_type_t *type)
1011 {
1012         if(token.type == T_IDENTIFIER) {
1013                 symbol_t      *symbol      = token.v.symbol;
1014                 declaration_t *declaration = symbol->declaration;
1015                 if(declaration == NULL
1016                                 || declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
1017                         /* TODO */
1018                         parse_identifier_list();
1019                         return NULL;
1020                 }
1021         }
1022
1023         if(token.type == ')') {
1024                 type->unspecified_parameters = 1;
1025                 return NULL;
1026         }
1027         if(token.type == T_void && look_ahead(1)->type == ')') {
1028                 next_token();
1029                 return NULL;
1030         }
1031
1032         declaration_t      *declarations = NULL;
1033         declaration_t      *declaration;
1034         declaration_t      *last_declaration = NULL;
1035         method_parameter_t *parameter;
1036         method_parameter_t *last_parameter = NULL;
1037
1038         while(1) {
1039                 switch(token.type) {
1040                 case T_DOTDOTDOT:
1041                         next_token();
1042                         type->variadic = 1;
1043                         return declarations;
1044
1045                 case T_IDENTIFIER:
1046                 DECLARATION_START
1047                         declaration = parse_parameter();
1048
1049                         parameter       = allocate_type_zero(sizeof(parameter[0]));
1050                         parameter->type = declaration->type;
1051
1052                         if(last_parameter != NULL) {
1053                                 last_declaration->next = declaration;
1054                                 last_parameter->next   = parameter;
1055                         } else {
1056                                 type->parameters = parameter;
1057                                 declarations     = declaration;
1058                         }
1059                         last_parameter   = parameter;
1060                         last_declaration = declaration;
1061                         break;
1062
1063                 default:
1064                         return declarations;
1065                 }
1066                 if(token.type != ',')
1067                         return declarations;
1068                 next_token();
1069         }
1070 }
1071
1072 typedef struct declarator_part declarator_part;
1073 struct declarator_part {
1074         parsed_pointer_t *pointers;
1075         method_type_t    *method_type;
1076         declarator_part  *inner;
1077 };
1078
1079
1080 static
1081 declarator_part *parse_inner_declarator(declaration_t *declaration,
1082                                         int may_be_abstract)
1083 {
1084         declarator_part *part = obstack_alloc(&temp_obst, sizeof(part[0]));
1085         memset(part, 0, sizeof(part[0]));
1086
1087         part->pointers = parse_pointers();
1088
1089         /* TODO: find out if this is correct */
1090         parse_attributes();
1091
1092         switch(token.type) {
1093         case T_IDENTIFIER:
1094                 if(declaration == NULL) {
1095                         parse_error("no identifier expected in typename");
1096                 } else {
1097                         declaration->symbol          = token.v.symbol;
1098                         declaration->source_position = token.source_position;
1099                 }
1100                 next_token();
1101                 break;
1102         case '(':
1103                 next_token();
1104                 part->inner = parse_inner_declarator(declaration, may_be_abstract);
1105                 expect(')');
1106                 break;
1107         default:
1108                 if(may_be_abstract)
1109                         break;
1110                 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1111                                      '(', 0);
1112         }
1113
1114         while(1) {
1115                 switch(token.type) {
1116                 case '(':
1117                         next_token();
1118
1119                         method_type_t *method_type
1120                                 = allocate_type_zero(sizeof(method_type[0]));
1121                         method_type->type.type   = TYPE_METHOD;
1122
1123                         declaration_t *parameters = parse_parameters(method_type);
1124                         if(declaration != NULL) {
1125                                 declaration->context.declarations = parameters;
1126                         }
1127
1128                         part->method_type = method_type;
1129
1130                         expect(')');
1131                         break;
1132                 case '[':
1133                         next_token();
1134
1135                         if(token.type == T_static) {
1136                                 next_token();
1137                         }
1138
1139                         unsigned type_qualifiers = parse_type_qualifiers();
1140                         if(type_qualifiers != 0) {
1141                                 if(token.type == T_static) {
1142                                         next_token();
1143                                 }
1144                         }
1145
1146                         /* TODO */
1147
1148                         if(token.type == '*' && look_ahead(1)->type == ']') {
1149                                 next_token();
1150                         } else if(token.type != ']') {
1151                                 parse_assignment_expression();
1152                         }
1153
1154                         expect(']');
1155                         break;
1156                 default:
1157                         goto declarator_finished;
1158                 }
1159         }
1160
1161 declarator_finished:
1162         parse_attributes();
1163
1164         return part;
1165 }
1166
1167 static
1168 type_t *construct_declarator_type(declarator_part *part, type_t *type)
1169 {
1170         do {
1171                 type = make_pointers(type, part->pointers);
1172
1173                 method_type_t *method_type = part->method_type;
1174                 if(method_type != NULL) {
1175                         method_type->result_type = type;
1176
1177                         type_t *result = typehash_insert((type_t*) method_type);
1178                         if(result != (type_t*) method_type) {
1179                                 obstack_free(type_obst, method_type);
1180                         }
1181                         type = result;
1182                 }
1183
1184                 part = part->inner;
1185         } while(part != NULL);
1186
1187         return type;
1188 }
1189
1190 static
1191 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
1192                       type_t *type, int may_be_abstract)
1193 {
1194         declarator_part *part
1195                 = parse_inner_declarator(declaration, may_be_abstract);
1196
1197         if(part != NULL) {
1198                 declaration->type          = construct_declarator_type(part, type);
1199                 declaration->storage_class = storage_class;
1200                 obstack_free(&temp_obst, part);
1201         }
1202 }
1203
1204 static
1205 type_t *parse_abstract_declarator(type_t *base_type)
1206 {
1207         declarator_part *part = parse_inner_declarator(NULL, 1);
1208
1209         if(part == NULL)
1210                 return NULL;
1211
1212         type_t *result = construct_declarator_type(part, base_type);
1213         obstack_free(&temp_obst, part);
1214
1215         return result;
1216 }
1217
1218 static declaration_t *record_declaration(declaration_t *declaration)
1219 {
1220         if(context == NULL)
1221                 return declaration;
1222
1223         symbol_t *symbol = declaration->symbol;
1224         if(symbol != NULL) {
1225                 declaration_t *alias = environment_push(declaration, context);
1226                 if(alias != declaration)
1227                         return alias;
1228         }
1229
1230         if(last_declaration != NULL) {
1231                 last_declaration->next = declaration;
1232         } else {
1233                 context->declarations  = declaration;
1234         }
1235         last_declaration = declaration;
1236
1237         return declaration;
1238 }
1239
1240 static
1241 void parser_error_multiple_definition(declaration_t *previous,
1242                                       declaration_t *declaration)
1243 {
1244         parser_print_error_prefix_pos(declaration->source_position);
1245         fprintf(stderr, "multiple definition of symbol '%s'\n",
1246                 declaration->symbol->string);
1247         parser_print_error_prefix_pos(previous->source_position);
1248         fprintf(stderr, "this is the location of the previous "
1249                 "definition.\n");
1250 }
1251
1252 static
1253 void parse_init_declarators(const declaration_specifiers_t *specifiers)
1254 {
1255         while(1) {
1256                 declaration_t *ndeclaration
1257                         = allocate_ast_zero(sizeof(ndeclaration[0]));
1258
1259                 parse_declarator(ndeclaration, specifiers->storage_class,
1260                                  specifiers->type, 0);
1261                 declaration_t *declaration = record_declaration(ndeclaration);
1262                 if(token.type == '=') {
1263                         next_token();
1264
1265                         /* TODO: check that this is an allowed type (esp. no method type) */
1266
1267                         if(declaration->initializer != NULL) {
1268                                 parser_error_multiple_definition(declaration, ndeclaration);
1269                         }
1270
1271                         if(token.type == '{') {
1272                                 // TODO
1273                                 expect_void('}');
1274                         } else {
1275                                 declaration->initializer = parse_assignment_expression();
1276                         }
1277                 } else if(token.type == '{') {
1278                         if(declaration->type->type != TYPE_METHOD) {
1279                                 parser_print_error_prefix();
1280                                 fprintf(stderr, "Declarator ");
1281                                 print_type(declaration->type, declaration->symbol);
1282                                 fprintf(stderr, " is not a method type.\n");
1283                         }
1284
1285                         if(declaration->initializer != NULL) {
1286                                 parser_error_multiple_definition(declaration, ndeclaration);
1287                         }
1288
1289                         int         top          = environment_top();
1290                         context_t  *last_context = context;
1291                         set_context(&declaration->context);
1292
1293                         /* push function parameters */
1294                         declaration_t *parameter = declaration->context.declarations;
1295                         for( ; parameter != NULL; parameter = parameter->next) {
1296                                 environment_push(parameter, context);
1297                         }
1298
1299                         statement_t *statement = parse_compound_statement();
1300
1301                         assert(context == &declaration->context);
1302                         set_context(last_context);
1303                         environment_pop_to(top);
1304
1305                         declaration->statement = statement;
1306                         return;
1307                 }
1308
1309                 if(token.type != ',')
1310                         break;
1311                 next_token();
1312         }
1313         expect_void(';');
1314 }
1315
1316 static
1317 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1318 {
1319         while(1) {
1320                 if(token.type == ':') {
1321                         next_token();
1322                         parse_constant_expression();
1323                         /* TODO (bitfields) */
1324                 } else {
1325                         declaration_t *declaration
1326                                 = allocate_ast_zero(sizeof(declaration[0]));
1327                         parse_declarator(declaration, specifiers->storage_class,
1328                                          specifiers->type, 1);
1329
1330                         /* TODO: check for doubled fields */
1331                         record_declaration(declaration);
1332
1333                         if(token.type == ':') {
1334                                 next_token();
1335                                 parse_constant_expression();
1336                                 /* TODO (bitfields) */
1337                         }
1338                 }
1339
1340                 if(token.type != ',')
1341                         break;
1342                 next_token();
1343         }
1344         expect_void(';');
1345 }
1346
1347 static void parse_compound_type_entries(void)
1348 {
1349         eat('{');
1350
1351         while(token.type != '}' && token.type != T_EOF) {
1352                 declaration_specifiers_t specifiers;
1353                 memset(&specifiers, 0, sizeof(specifiers));
1354                 /* TODO not correct as this allows storage class stuff... but only
1355                  * specifiers and qualifiers sould be allowed here */
1356                 parse_declaration_specifiers(&specifiers);
1357
1358                 parse_struct_declarators(&specifiers);
1359         }
1360         if(token.type == T_EOF) {
1361                 parse_error("unexpected error while parsing struct");
1362         }
1363         next_token();
1364 }
1365
1366 void parse_declaration(void)
1367 {
1368         declaration_specifiers_t specifiers;
1369         memset(&specifiers, 0, sizeof(specifiers));
1370         parse_declaration_specifiers(&specifiers);
1371
1372         if(token.type == ';') {
1373                 next_token();
1374                 return;
1375         }
1376         parse_init_declarators(&specifiers);
1377 }
1378
1379 type_t *parse_typename(void)
1380 {
1381         declaration_specifiers_t specifiers;
1382         memset(&specifiers, 0, sizeof(specifiers));
1383         parse_declaration_specifiers(&specifiers);
1384         if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1385                 /* TODO: improve error message, user does probably not know what a
1386                  * storage class is...
1387                  */
1388                 parse_error("typename may not have a storage class");
1389         }
1390
1391         type_t *result = parse_abstract_declarator(specifiers.type);
1392
1393         return result;
1394 }
1395
1396
1397
1398
1399 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1400 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1401                                                           expression_t *left);
1402
1403 typedef struct expression_parser_function_t expression_parser_function_t;
1404 struct expression_parser_function_t {
1405         unsigned                         precedence;
1406         parse_expression_function        parser;
1407         unsigned                         infix_precedence;
1408         parse_expression_infix_function  infix_parser;
1409 };
1410
1411 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1412
1413 static
1414 expression_t *expected_expression_error(void)
1415 {
1416         parser_print_error_prefix();
1417         fprintf(stderr, "expected expression, got token ");
1418         print_token(stderr, & token);
1419         fprintf(stderr, "\n");
1420
1421         expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1422         expression->type = EXPR_INVALID;
1423         next_token();
1424
1425         return expression;
1426 }
1427
1428 static
1429 expression_t *parse_string_const(void)
1430 {
1431         string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1432
1433         cnst->expression.type = EXPR_STRING_LITERAL;
1434         cnst->value           = parse_string_literals();
1435
1436         return (expression_t*) cnst;
1437 }
1438
1439 static
1440 expression_t *parse_int_const(void)
1441 {
1442         const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1443
1444         cnst->expression.type = EXPR_CONST;
1445         cnst->value           = token.v.intvalue;
1446
1447         next_token();
1448
1449         return (expression_t*) cnst;
1450 }
1451
1452 static
1453 expression_t *parse_reference(void)
1454 {
1455         reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1456
1457         ref->expression.type = EXPR_REFERENCE;
1458         ref->symbol          = token.v.symbol;
1459
1460         if(ref->symbol->declaration == NULL) {
1461                 parser_print_error_prefix();
1462                 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
1463         }
1464         ref->declaration     = ref->symbol->declaration;
1465
1466         next_token();
1467
1468         return (expression_t*) ref;
1469 }
1470
1471 static
1472 expression_t *parse_cast(void)
1473 {
1474         unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
1475
1476         cast->expression.type            = EXPR_UNARY;
1477         cast->type                       = UNEXPR_CAST;
1478         cast->expression.source_position = token.source_position;
1479
1480         type_t *type  = parse_typename();
1481
1482         expect(')');
1483         expression_t *value = parse_sub_expression(20);
1484
1485         cast->expression.datatype = type;
1486         cast->value               = value;
1487
1488         return (expression_t*) cast;
1489 }
1490
1491 static
1492 expression_t *parse_brace_expression(void)
1493 {
1494         eat('(');
1495
1496         declaration_t *declaration;
1497         switch(token.type) {
1498         TYPE_QUALIFIERS
1499         TYPE_SPECIFIERS
1500                 return parse_cast();
1501         case T_IDENTIFIER:
1502                 declaration = token.v.symbol->declaration;
1503                 if(declaration != NULL &&
1504                                 (declaration->storage_class & STORAGE_CLASS_TYPEDEF)) {
1505                         return parse_cast();
1506                 }
1507         }
1508
1509         expression_t *result = parse_expression();
1510         expect(')');
1511
1512         return result;
1513 }
1514
1515 static
1516 expression_t *parse_primary_expression(void)
1517 {
1518         switch(token.type) {
1519         case T_INTEGER:
1520                 return parse_int_const();
1521         case T_STRING_LITERAL:
1522                 return parse_string_const();
1523         case T_IDENTIFIER:
1524                 return parse_reference();
1525         case '(':
1526                 return parse_brace_expression();
1527         }
1528
1529         parser_print_error_prefix();
1530         fprintf(stderr, "unexpected token ");
1531         print_token(stderr, &token);
1532         fprintf(stderr, "\n");
1533         eat_statement();
1534         return NULL;
1535 }
1536
1537 static
1538 expression_t *parse_array_expression(unsigned precedence,
1539                                      expression_t *array_ref)
1540 {
1541         (void) precedence;
1542
1543         eat('[');
1544
1545         array_access_expression_t *array_access
1546                 = allocate_ast_zero(sizeof(array_access[0]));
1547
1548         array_access->expression.type = EXPR_ARRAY_ACCESS;
1549         array_access->array_ref       = array_ref;
1550         array_access->index           = parse_expression();
1551
1552         if(token.type != ']') {
1553                 parse_error_expected("Problem while parsing array access", ']', 0);
1554                 return NULL;
1555         }
1556         next_token();
1557
1558         return (expression_t*) array_access;
1559 }
1560
1561 static
1562 type_t *get_expression_type(const expression_t *expression)
1563 {
1564         (void) expression;
1565         /* TODO */
1566         return NULL;
1567 }
1568
1569 static
1570 int is_type_specifier(const token_t *token)
1571 {
1572         declaration_t *declaration;
1573
1574         switch(token->type) {
1575                 TYPE_SPECIFIERS
1576                         return 1;
1577                 case T_IDENTIFIER:
1578                         declaration = token->v.symbol->declaration;
1579                         if(declaration == NULL)
1580                                 return 0;
1581                         if(declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1582                                 return 0;
1583                         return 1;
1584                 default:
1585                         return 0;
1586         }
1587 }
1588
1589 static
1590 expression_t *parse_sizeof(unsigned precedence)
1591 {
1592         eat(T_sizeof);
1593
1594         sizeof_expression_t *sizeof_expression
1595                 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1596         sizeof_expression->expression.type = EXPR_SIZEOF;
1597
1598         if(token.type == '(' && is_type_specifier(look_ahead(1))) {
1599                 next_token();
1600                 sizeof_expression->type = parse_typename();
1601                 expect(')');
1602         } else {
1603                 expression_t *expression = parse_sub_expression(precedence);
1604                 sizeof_expression->type  = get_expression_type(expression);
1605         }
1606
1607         return (expression_t*) sizeof_expression;
1608 }
1609
1610 static
1611 expression_t *parse_select_expression(unsigned precedence,
1612                                       expression_t *compound)
1613 {
1614         (void) precedence;
1615
1616         assert(token.type == '.' || token.type == T_SELECT);
1617         next_token();
1618
1619         select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
1620
1621         select->expression.type = EXPR_SELECT;
1622         select->compound        = compound;
1623
1624         if(token.type != T_IDENTIFIER) {
1625                 parse_error_expected("Problem while parsing compound select",
1626                                      T_IDENTIFIER, 0);
1627                 return NULL;
1628         }
1629         select->symbol = token.v.symbol;
1630         next_token();
1631
1632         return (expression_t*) select;
1633 }
1634
1635 static
1636 expression_t *parse_call_expression(unsigned precedence,
1637                                     expression_t *expression)
1638 {
1639         (void) precedence;
1640         call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
1641
1642         call->expression.type            = EXPR_CALL;
1643         call->method                     = expression;
1644
1645         /* parse arguments */
1646         eat('(');
1647
1648         if(token.type != ')') {
1649                 call_argument_t *last_argument = NULL;
1650
1651                 while(1) {
1652                         call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
1653
1654                         argument->expression = parse_expression();
1655                         if(last_argument == NULL) {
1656                                 call->arguments = argument;
1657                         } else {
1658                                 last_argument->next = argument;
1659                         }
1660                         last_argument = argument;
1661
1662                         if(token.type != ',')
1663                                 break;
1664                         next_token();
1665                 }
1666         }
1667         expect(')');
1668
1669         return (expression_t*) call;
1670 }
1671
1672 static
1673 expression_t *parse_conditional_expression(unsigned precedence,
1674                                            expression_t *expression)
1675 {
1676         eat('?');
1677
1678         conditional_expression_t *conditional
1679                 = allocate_ast_zero(sizeof(conditional[0]));
1680         conditional->condition = expression;
1681
1682         conditional->true_expression = parse_expression();
1683         expect(':');
1684         conditional->false_expression = parse_sub_expression(precedence);
1685
1686         return (expression_t*) conditional;
1687 }
1688
1689 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type)     \
1690 static                                                                    \
1691 expression_t *parse_##unexpression_type(unsigned precedence)              \
1692 {                                                                         \
1693         eat(token_type);                                                      \
1694                                                                           \
1695         unary_expression_t *unary_expression                                  \
1696                 = allocate_ast_zero(sizeof(unary_expression[0]));                 \
1697         unary_expression->expression.type = EXPR_UNARY;                       \
1698         unary_expression->type            = unexpression_type;                \
1699         unary_expression->value           = parse_sub_expression(precedence); \
1700                                                                           \
1701         return (expression_t*) unary_expression;                              \
1702 }
1703
1704 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1705 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1706 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1707 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1708 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1709 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1710 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_PREFIX_INCREMENT)
1711 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1712
1713 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1714 static                                                                        \
1715 expression_t *parse_##unexpression_type(unsigned precedence,                  \
1716                                         expression_t *left)                   \
1717 {                                                                             \
1718         (void) precedence;                                                        \
1719         eat(token_type);                                                          \
1720                                                                               \
1721         unary_expression_t *unary_expression                                      \
1722                 = allocate_ast_zero(sizeof(unary_expression[0]));                     \
1723         unary_expression->expression.type = EXPR_UNARY;                           \
1724         unary_expression->type            = unexpression_type;                    \
1725         unary_expression->value           = left;                                 \
1726                                                                               \
1727         return (expression_t*) unary_expression;                                  \
1728 }
1729
1730 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_POSTFIX_INCREMENT)
1731 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1732
1733 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type)    \
1734 static                                                           \
1735 expression_t *parse_##binexpression_type(unsigned precedence,    \
1736                                          expression_t *left)     \
1737 {                                                                \
1738         eat(token_type);                                             \
1739                                                                  \
1740         expression_t *right = parse_sub_expression(precedence);      \
1741                                                                  \
1742         binary_expression_t *binexpr                                 \
1743                 = allocate_ast_zero(sizeof(binexpr[0]));                 \
1744         binexpr->expression.type            = EXPR_BINARY;           \
1745         binexpr->type                       = binexpression_type;    \
1746         binexpr->left                       = left;                  \
1747         binexpr->right                      = right;                 \
1748                                                                  \
1749         return (expression_t*) binexpr;                              \
1750 }
1751
1752 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1753 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1754 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1755 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1756 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1757 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1758 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1759 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1760 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL)
1761 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1762 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1763 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1764 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1765 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1766 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
1767 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
1768 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1769 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1770
1771 static
1772 expression_t *parse_sub_expression(unsigned precedence)
1773 {
1774         if(token.type < 0) {
1775                 return expected_expression_error();
1776         }
1777
1778         expression_parser_function_t *parser
1779                 = &expression_parsers[token.type];
1780         source_position_t             source_position = token.source_position;
1781         expression_t                 *left;
1782
1783         if(parser->parser != NULL) {
1784                 left = parser->parser(parser->precedence);
1785         } else {
1786                 left = parse_primary_expression();
1787         }
1788         if(left != NULL)
1789                 left->source_position = source_position;
1790
1791         while(1) {
1792                 if(token.type < 0) {
1793                         return expected_expression_error();
1794                 }
1795
1796                 parser = &expression_parsers[token.type];
1797                 if(parser->infix_parser == NULL)
1798                         break;
1799                 if(parser->infix_precedence < precedence)
1800                         break;
1801
1802                 left = parser->infix_parser(parser->infix_precedence, left);
1803                 if(left != NULL)
1804                         left->source_position = source_position;
1805         }
1806
1807         return left;
1808 }
1809
1810 static
1811 expression_t *parse_expression(void)
1812 {
1813         return parse_sub_expression(1);
1814 }
1815
1816
1817
1818 void register_expression_parser(parse_expression_function parser,
1819                                 int token_type, unsigned precedence)
1820 {
1821         expression_parser_function_t *entry = &expression_parsers[token_type];
1822
1823         if(entry->parser != NULL) {
1824                 fprintf(stderr, "for token ");
1825                 print_token_type(stderr, token_type);
1826                 fprintf(stderr, "\n");
1827                 panic("trying to register multiple expression parsers for a token");
1828         }
1829         entry->parser     = parser;
1830         entry->precedence = precedence;
1831 }
1832
1833 void register_expression_infix_parser(parse_expression_infix_function parser,
1834                                       int token_type, unsigned precedence)
1835 {
1836         expression_parser_function_t *entry = &expression_parsers[token_type];
1837
1838         if(entry->infix_parser != NULL) {
1839                 fprintf(stderr, "for token ");
1840                 print_token_type(stderr, token_type);
1841                 fprintf(stderr, "\n");
1842                 panic("trying to register multiple infix expression parsers for a "
1843                       "token");
1844         }
1845         entry->infix_parser     = parser;
1846         entry->infix_precedence = precedence;
1847 }
1848
1849 static
1850 void init_expression_parsers(void)
1851 {
1852         memset(&expression_parsers, 0, sizeof(expression_parsers));
1853
1854         register_expression_infix_parser(parse_BINEXPR_MUL,       '*', 16);
1855         register_expression_infix_parser(parse_BINEXPR_DIV,       '/', 16);
1856         register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1857                                    T_LESSLESS, 16);
1858         register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1859                                    T_GREATERGREATER, 16);
1860         register_expression_infix_parser(parse_BINEXPR_ADD,       '+', 15);
1861         register_expression_infix_parser(parse_BINEXPR_SUB,       '-', 15);
1862         register_expression_infix_parser(parse_BINEXPR_LESS,      '<', 14);
1863         register_expression_infix_parser(parse_BINEXPR_GREATER,   '>', 14);
1864         register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1865         register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1866                                    T_GREATEREQUAL, 14);
1867         register_expression_infix_parser(parse_BINEXPR_EQUAL,     T_EQUALEQUAL, 13);
1868         register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1869                                          T_EXCLAMATIONMARKEQUAL, 13);
1870         register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&',        12);
1871         register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^',        11);
1872         register_expression_infix_parser(parse_BINEXPR_BITWISE_OR,  '|',        10);
1873         register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND,    9);
1874         register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR,  T_PIPEPIPE,  8);
1875         register_expression_infix_parser(parse_conditional_expression, '?',      7);
1876         register_expression_infix_parser(parse_BINEXPR_ASSIGN,      T_EQUAL,     2);
1877
1878         register_expression_infix_parser(parse_array_expression,        '[',    30);
1879         register_expression_infix_parser(parse_call_expression,         '(',    30);
1880         register_expression_infix_parser(parse_select_expression,       '.',    30);
1881         register_expression_infix_parser(parse_select_expression,  T_SELECT,    30);
1882         register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
1883                                          T_PLUSPLUS, 30);
1884         register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
1885                                          T_MINUSMINUS, 30);
1886
1887         register_expression_parser(parse_UNEXPR_NEGATE,           '-',          25);
1888         register_expression_parser(parse_UNEXPR_PLUS,             '+',          25);
1889         register_expression_parser(parse_UNEXPR_NOT,              '!',          25);
1890         register_expression_parser(parse_UNEXPR_BITWISE_NEGATE,   '~',          25);
1891         register_expression_parser(parse_UNEXPR_DEREFERENCE,      '*',          25);
1892         register_expression_parser(parse_UNEXPR_TAKE_ADDRESS,     '&',          25);
1893         register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS,   25);
1894         register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
1895         register_expression_parser(parse_sizeof,                  T_sizeof,     25);
1896 }
1897
1898
1899 static
1900 statement_t *parse_case_statement(void)
1901 {
1902         eat(T_case);
1903         parse_expression();
1904         expect(':');
1905         parse_statement();
1906
1907         return NULL;
1908 }
1909
1910 static
1911 statement_t *parse_default_statement(void)
1912 {
1913         eat(T_default);
1914         expect(':');
1915         parse_statement();
1916
1917         return NULL;
1918 }
1919
1920 static
1921 statement_t *parse_label_statement(void)
1922 {
1923         eat(T_IDENTIFIER);
1924         expect(':');
1925         parse_statement();
1926
1927         return NULL;
1928 }
1929
1930 static
1931 statement_t *parse_if(void)
1932 {
1933         eat(T_if);
1934
1935         if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
1936         statement->statement.type = STATEMENT_IF;
1937
1938         expect('(');
1939         statement->condition = parse_expression();
1940         expect(')');
1941
1942         statement->true_statement = parse_statement();
1943         if(token.type == T_else) {
1944                 next_token();
1945                 statement->false_statement = parse_statement();
1946         }
1947
1948         return (statement_t*) statement;
1949 }
1950
1951 static
1952 statement_t *parse_switch(void)
1953 {
1954         eat(T_switch);
1955         expect('(');
1956         parse_expression();
1957         expect(')');
1958         parse_statement();
1959
1960         return NULL;
1961 }
1962
1963 static
1964 statement_t *parse_while(void)
1965 {
1966         eat(T_while);
1967         expect('(');
1968         parse_expression();
1969         expect(')');
1970         parse_statement();
1971
1972         return NULL;
1973 }
1974
1975 static
1976 statement_t *parse_do(void)
1977 {
1978         eat(T_do);
1979         parse_statement();
1980         expect(T_while);
1981         expect('(');
1982         parse_expression();
1983         expect(')');
1984
1985         return NULL;
1986 }
1987
1988 static
1989 statement_t *parse_for(void)
1990 {
1991         eat(T_for);
1992         expect('(');
1993         if(token.type != ';') {
1994                 /* TODO not correct... this could also be a declaration */
1995                 parse_expression();
1996         }
1997         expect(';');
1998         if(token.type != ';') {
1999                 parse_expression();
2000         }
2001         expect(';');
2002         if(token.type != ')') {
2003                 parse_expression();
2004         }
2005         expect(')');
2006         parse_statement();
2007
2008         return NULL;
2009 }
2010
2011 static
2012 statement_t *parse_goto(void)
2013 {
2014         eat(T_goto);
2015         expect(T_IDENTIFIER);
2016         expect(';');
2017
2018         return NULL;
2019 }
2020
2021 static
2022 statement_t *parse_continue(void)
2023 {
2024         eat(T_continue);
2025         expect(';');
2026
2027         statement_t *statement     = allocate_ast_zero(sizeof(statement[0]));
2028         statement->source_position = token.source_position;
2029         statement->type            = STATEMENT_CONTINUE;
2030
2031         return statement;
2032 }
2033
2034 static
2035 statement_t *parse_break(void)
2036 {
2037         eat(T_break);
2038         expect(';');
2039
2040         return NULL;
2041 }
2042
2043 static
2044 statement_t *parse_return(void)
2045 {
2046         eat(T_return);
2047
2048         return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2049         statement->statement.type = STATEMENT_RETURN;
2050         if(token.type != ';') {
2051                 statement->return_value = parse_expression();
2052         }
2053         expect(';');
2054
2055         return (statement_t*) statement;
2056 }
2057
2058 static
2059 statement_t *parse_declaration_statement(void)
2060 {
2061         parse_declaration();
2062         return NULL;
2063 }
2064
2065 static
2066 statement_t *parse_expression_statement(void)
2067 {
2068         parse_expression();
2069         return NULL;
2070 }
2071
2072 static
2073 statement_t *parse_statement(void)
2074 {
2075         declaration_t *declaration;
2076         statement_t   *statement = NULL;
2077
2078         /* declaration or statement */
2079         switch(token.type) {
2080         case T_case:
2081                 statement = parse_case_statement();
2082                 break;
2083
2084         case T_default:
2085                 statement = parse_default_statement();
2086                 break;
2087
2088         case '{':
2089                 statement = parse_compound_statement();
2090                 break;
2091
2092         case T_if:
2093                 statement = parse_if();
2094                 break;
2095
2096         case T_switch:
2097                 statement = parse_switch();
2098                 break;
2099
2100         case T_while:
2101                 statement = parse_while();
2102                 break;
2103
2104         case T_do:
2105                 statement = parse_do();
2106                 break;
2107
2108         case T_for:
2109                 statement = parse_for();
2110                 break;
2111
2112         case T_goto:
2113                 statement = parse_goto();
2114                 break;
2115
2116         case T_continue:
2117                 statement = parse_continue();
2118                 break;
2119
2120         case T_break:
2121                 statement = parse_break();
2122                 break;
2123
2124         case T_return:
2125                 statement = parse_return();
2126                 break;
2127
2128         case ';':
2129                 next_token();
2130                 statement = NULL;
2131                 break;
2132
2133         case T_IDENTIFIER:
2134                 if(look_ahead(1)->type == ':') {
2135                         statement = parse_label_statement();
2136                         break;
2137                 }
2138
2139                 declaration = token.v.symbol->declaration;
2140                 if(declaration != NULL &&
2141                                 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
2142                         statement = parse_declaration_statement();
2143                         break;
2144                 }
2145
2146                 statement = parse_expression_statement();
2147                 break;
2148
2149         DECLARATION_START
2150                 statement = parse_declaration_statement();
2151                 break;
2152
2153         default:
2154                 statement = parse_expression_statement();
2155                 break;
2156         }
2157
2158         return statement;
2159 }
2160
2161 static
2162 statement_t *parse_compound_statement(void)
2163 {
2164         eat('{');
2165
2166         compound_statement_t *compound_statement
2167                 = allocate_ast_zero(sizeof(compound_statement[0]));
2168         compound_statement->statement.type = STATEMENT_COMPOUND;
2169
2170         int        top          = environment_top();
2171         context_t *last_context = context;
2172         set_context(&compound_statement->context);
2173
2174         statement_t *last_statement = NULL;
2175
2176         while(token.type != '}' && token.type != T_EOF) {
2177                 statement_t *statement = parse_statement();
2178
2179                 if(last_statement != NULL) {
2180                         last_statement->next = statement;
2181                 } else {
2182                         compound_statement->statements = statement;
2183                 }
2184                 last_statement = statement;
2185         }
2186
2187         assert(context == &compound_statement->context);
2188         set_context(last_context);
2189         environment_pop_to(top);
2190
2191         next_token();
2192
2193         return (statement_t*) compound_statement;
2194 }
2195
2196 static
2197 translation_unit_t *parse_translation_unit(void)
2198 {
2199         translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
2200
2201         assert(context == NULL);
2202         set_context(&unit->context);
2203
2204         while(token.type != T_EOF) {
2205                 parse_declaration();
2206         }
2207
2208         assert(context == &unit->context);
2209         context          = NULL;
2210         last_declaration = NULL;
2211
2212         return unit;
2213 }
2214
2215 translation_unit_t *parse(void)
2216 {
2217         obstack_init(&environment_obstack);
2218         environment_stack = NEW_ARR_F(environment_entry_t*, 0);
2219
2220         type_set_output(stderr);
2221
2222         lookahead_bufpos = 0;
2223         for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
2224                 next_token();
2225         }
2226         translation_unit_t *unit = parse_translation_unit();
2227
2228         DEL_ARR_F(environment_stack);
2229         obstack_free(&environment_obstack, NULL);
2230
2231         return unit;
2232 }
2233
2234 void init_parser(void)
2235 {
2236         init_expression_parsers();
2237         obstack_init(&temp_obst);
2238 }
2239
2240 void exit_parser(void)
2241 {
2242         obstack_free(&temp_obst, NULL);
2243 }