More work on cparser:
[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 struct environment_entry_t {
23         symbol_t      *symbol;
24         declaration_t *old_declaration;
25         const void    *old_context;
26 };
27
28 static token_t               token;
29 static token_t               lookahead_buffer[MAX_LOOKAHEAD];
30 static int                   lookahead_bufpos;
31 static struct obstack        environment_obstack;
32 static environment_entry_t **environment_stack = NULL;
33 static context_t            *context           = NULL;
34 static declaration_t        *last_declaration  = NULL;
35 static struct obstack        temp_obst;
36
37 static type_t               *type_int        = NULL;
38 static type_t               *type_const_char = NULL;
39 static type_t               *type_string     = NULL;
40 static type_t               *type_void       = NULL;
41 static type_t               *type_size_t     = NULL;
42
43 static statement_t *parse_compound_statement(void);
44 static statement_t *parse_statement(void);
45
46 static expression_t *parse_sub_expression(unsigned precedence);
47 static expression_t *parse_expression(void);
48 static type_t       *parse_typename(void);
49
50 #define STORAGE_CLASSES     \
51         case T_typedef:         \
52         case T_extern:          \
53         case T_static:          \
54         case T_auto:            \
55         case T_register:
56
57 #define TYPE_QUALIFIERS     \
58         case T_const:           \
59         case T_restrict:        \
60         case T_volatile:        \
61         case T_inline:
62
63 #ifdef PROVIDE_COMPLEX
64 #define COMPLEX_SPECIFIERS  \
65         case T__Complex:
66 #else
67 #define COMPLEX_SPECIFIERS
68 #endif
69
70 #ifdef PROVIDE_IMAGINARY
71 #define IMAGINARY_SPECIFIERS \
72         case T__Imaginary:
73 #else
74 #define IMAGINARY_SPECIFIERS
75 #endif
76
77 #define TYPE_SPECIFIERS     \
78         case T_void:            \
79         case T_char:            \
80         case T_short:           \
81         case T_int:             \
82         case T_long:            \
83         case T_float:           \
84         case T_double:          \
85         case T_signed:          \
86         case T_unsigned:        \
87         case T__Bool:           \
88         case T_struct:          \
89         case T_union:           \
90         case T_enum:            \
91         case T___typeof__:      \
92         COMPLEX_SPECIFIERS      \
93         IMAGINARY_SPECIFIERS
94
95 #define DECLARATION_START   \
96         STORAGE_CLASSES         \
97         TYPE_QUALIFIERS         \
98         TYPE_SPECIFIERS
99
100 #define TYPENAME_START      \
101         TYPE_QUALIFIERS         \
102         TYPE_SPECIFIERS
103
104 static inline void *allocate_ast_zero(size_t size)
105 {
106         void *res = allocate_ast(size);
107         memset(res, 0, size);
108         return res;
109 }
110
111 static inline void *allocate_type_zero(size_t size)
112 {
113         void *res = obstack_alloc(type_obst, size);
114         memset(res, 0, size);
115         return res;
116 }
117
118 /**
119  * returns the top element of the environment stack
120  */
121 static inline size_t environment_top(void)
122 {
123         return ARR_LEN(environment_stack);
124 }
125
126
127
128 static inline void next_token(void)
129 {
130         token                              = lookahead_buffer[lookahead_bufpos];
131         lookahead_buffer[lookahead_bufpos] = lexer_token;
132         lexer_next_token();
133
134         lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
135
136 #ifdef PRINT_TOKENS
137         print_token(stderr, &token);
138         fprintf(stderr, "\n");
139 #endif
140 }
141
142 static inline const token_t *look_ahead(int num)
143 {
144         assert(num > 0 && num <= MAX_LOOKAHEAD);
145         int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
146         return & lookahead_buffer[pos];
147 }
148
149 static inline void eat(token_type_t type)
150 {
151         assert(token.type == type);
152         next_token();
153 }
154
155 void error(void)
156 {
157 #ifdef ABORT_ON_ERROR
158         abort();
159 #endif
160 }
161
162 void parser_print_prefix_pos(const source_position_t source_position)
163 {
164     fputs(source_position.input_name, stderr);
165     fputc(':', stderr);
166     fprintf(stderr, "%d", source_position.linenr);
167     fputs(": ", stderr);
168 }
169
170 void parser_print_error_prefix_pos(const source_position_t source_position)
171 {
172         parser_print_prefix_pos(source_position);
173         fputs("error: ", stderr);
174         error();
175 }
176
177 void parser_print_error_prefix(void)
178 {
179         parser_print_prefix_pos(token.source_position);
180         error();
181 }
182
183 static void parse_error(const char *message)
184 {
185         parser_print_error_prefix();
186         fprintf(stderr, "parse error: %s\n", message);
187 }
188
189 __attribute__((unused))
190 static void parse_warning(const char *message)
191 {
192         parser_print_prefix_pos(token.source_position);
193         fprintf(stderr, "warning: %s\n", message);
194 }
195
196 static void parse_error_expected(const char *message, ...)
197 {
198         va_list args;
199         int first = 1;
200
201         if(message != NULL) {
202                 parser_print_error_prefix();
203                 fprintf(stderr, "%s\n", message);
204         }
205         parser_print_error_prefix();
206         fputs("Parse error: got ", stderr);
207         print_token(stderr, &token);
208         fputs(", expected ", stderr);
209
210         va_start(args, message);
211         token_type_t token_type = va_arg(args, token_type_t);
212         while(token_type != 0) {
213                 if(first == 1) {
214                         first = 0;
215                 } else {
216                         fprintf(stderr, ", ");
217                 }
218                 print_token_type(stderr, token_type);
219                 token_type = va_arg(args, token_type_t);
220         }
221         va_end(args);
222         fprintf(stderr, "\n");
223 }
224
225 static void eat_block(void)
226 {
227         if(token.type == '{')
228                 next_token();
229
230         while(token.type != '}') {
231                 if(token.type == T_EOF)
232                         return;
233                 if(token.type == '{') {
234                         eat_block();
235                         continue;
236                 }
237                 next_token();
238         }
239         eat('}');
240 }
241
242 static void eat_statement(void)
243 {
244         while(token.type != ';') {
245                 if(token.type == T_EOF)
246                         return;
247                 if(token.type == '}')
248                         return;
249                 if(token.type == '{') {
250                         eat_block();
251                         continue;
252                 }
253                 next_token();
254         }
255         eat(';');
256 }
257
258 static void eat_brace(void)
259 {
260         if(token.type == '(')
261                 next_token();
262
263         while(token.type != ')') {
264                 if(token.type == T_EOF)
265                         return;
266                 if(token.type == ')' || token.type == ';' || token.type == '}') {
267                         return;
268                 }
269                 if(token.type == '(') {
270                         eat_brace();
271                         continue;
272                 }
273                 if(token.type == '{') {
274                         eat_block();
275                         continue;
276                 }
277                 next_token();
278         }
279         eat(')');
280 }
281
282 #define expect(expected)                           \
283     if(UNLIKELY(token.type != (expected))) {       \
284         parse_error_expected(NULL, (expected), 0); \
285         eat_statement();                           \
286         return NULL;                               \
287     }                                              \
288     next_token();
289
290 #define expect_void(expected)                      \
291     if(UNLIKELY(token.type != (expected))) {       \
292         parse_error_expected(NULL, (expected), 0); \
293         eat_statement();                           \
294         return;                                    \
295     }                                              \
296     next_token();
297
298 static void set_context(context_t *new_context)
299 {
300         context = new_context;
301
302         declaration_t *declaration = new_context->declarations;
303         if(declaration != NULL) {
304                 while(true) {
305                         if(declaration->next == NULL)
306                                 break;
307                         declaration = declaration->next;
308                 }
309         }
310
311         last_declaration = declaration;
312 }
313
314 /**
315  * called when we find a 2nd declarator for an identifier we already have a
316  * declarator for
317  */
318 static bool is_compatible_declaration (declaration_t *declaration,
319                                       declaration_t *previous)
320 {
321         /* TODO: not correct yet */
322         return declaration->type == previous->type;
323 }
324
325 /**
326  * pushs an environment_entry on the environment stack and links the
327  * corresponding symbol to the new entry
328  */
329 static inline declaration_t *environment_push(declaration_t *declaration,
330                                               const void *context)
331 {
332         symbol_t *symbol = declaration->symbol;
333         assert(declaration != symbol->declaration);
334         assert(declaration->source_position.input_name != NULL);
335
336         if(symbol->context == context) {
337                 declaration_t *previous_declaration = symbol->declaration;
338                 if(symbol->declaration != NULL) {
339                         if(!is_compatible_declaration(declaration, previous_declaration)) {
340                                 parser_print_error_prefix_pos(declaration->source_position);
341                                 fprintf(stderr, "definition of symbol '%s' with type ",
342                                         declaration->symbol->string);
343                                 error();
344                                 print_type(declaration->type);
345                                 fputc('\n', stderr);
346                                 parser_print_error_prefix_pos(
347                                                 previous_declaration->source_position);
348                                 fprintf(stderr, "is incompatible with previous declaration "
349                                         "of type ");
350                                 print_type(previous_declaration->type);
351                                 fputc('\n', stderr);
352                         }
353                         return previous_declaration;
354                 }
355         }
356
357         environment_entry_t *entry
358                 = obstack_alloc(&environment_obstack, sizeof(entry[0]));
359         memset(entry, 0, sizeof(entry[0]));
360
361         int top = ARR_LEN(environment_stack);
362         ARR_RESIZE(environment_stack, top + 1);
363         environment_stack[top] = entry;
364
365         entry->old_declaration = symbol->declaration;
366         entry->old_context     = symbol->context;
367         entry->symbol          = symbol;
368         symbol->declaration    = declaration;
369         symbol->context        = context;
370
371         return declaration;
372 }
373
374 /**
375  * pops symbols from the environment stack until @p new_top is the top element
376  */
377 static inline void environment_pop_to(size_t new_top)
378 {
379         environment_entry_t *entry = NULL;
380         size_t top = ARR_LEN(environment_stack);
381         size_t i;
382
383         if(new_top == top)
384                 return;
385
386         assert(new_top < top);
387         i = top;
388         do {
389                 entry = environment_stack[i - 1];
390
391                 symbol_t *symbol = entry->symbol;
392
393                 symbol->declaration = entry->old_declaration;
394                 symbol->context     = entry->old_context;
395
396                 --i;
397         } while(i != new_top);
398         obstack_free(&environment_obstack, entry);
399
400         ARR_SHRINKLEN(environment_stack, (int) new_top);
401 }
402
403
404
405 static expression_t *parse_constant_expression(void)
406 {
407         /* start parsing at precedence 7 (conditional expression) */
408         return parse_sub_expression(7);
409 }
410
411 static expression_t *parse_assignment_expression(void)
412 {
413         /* start parsing at precedence 2 (assignment expression) */
414         return parse_sub_expression(2);
415 }
416
417 static void parse_compound_type_entries(void);
418 static void parse_declarator(declaration_t *declaration,
419                              storage_class_t storage_class, type_t *type,
420                              int may_be_abstract);
421 static declaration_t *record_declaration(declaration_t *declaration);
422
423 typedef struct declaration_specifiers_t  declaration_specifiers_t;
424 struct declaration_specifiers_t {
425         storage_class_t  storage_class;
426         type_t          *type;
427 };
428
429 static compound_type_t *find_compound_type(compound_type_t *types,
430                                            const symbol_t *symbol)
431 {
432         compound_type_t *type = types;
433         for( ; type != NULL; type = type->next) {
434                 if(type->symbol == symbol)
435                         return type;
436         }
437
438         return NULL;
439 }
440
441 static type_t *parse_compound_type_specifier(bool is_struct)
442 {
443         if(is_struct) {
444                 eat(T_struct);
445         } else {
446                 eat(T_union);
447         }
448
449         symbol_t        *symbol        = NULL;
450         compound_type_t *compound_type = NULL;
451
452         if(token.type == T_IDENTIFIER) {
453                 symbol = token.v.symbol;
454                 next_token();
455
456                 if(context != NULL) {
457                         if(is_struct) {
458                                 compound_type = find_compound_type(context->structs, symbol);
459                         } else {
460                                 compound_type = find_compound_type(context->unions, symbol);
461                         }
462                 }
463         } else if(token.type != '{') {
464                 if(is_struct) {
465                         parse_error_expected("problem while parsing struct type specifier",
466                                              T_IDENTIFIER, '{', 0);
467                 } else {
468                         parse_error_expected("problem while parsing union type specifier",
469                                              T_IDENTIFIER, '{', 0);
470                 }
471
472                 return NULL;
473         }
474
475         if(compound_type == NULL) {
476                 compound_type = allocate_type_zero(sizeof(compound_type[0]));
477
478                 if(is_struct) {
479                         compound_type->type.type = TYPE_COMPOUND_STRUCT;
480                 } else {
481                         compound_type->type.type = TYPE_COMPOUND_UNION;
482                 }
483                 compound_type->source_position = token.source_position;
484                 compound_type->symbol          = symbol;
485         }
486
487         if(token.type == '{') {
488                 if(compound_type->defined) {
489                         parser_print_error_prefix();
490                         fprintf(stderr, "multiple definition of %s %s\n",
491                                         is_struct ? "struct" : "union", symbol->string);
492                         compound_type->context.declarations = NULL;
493                 }
494                 compound_type->defined = 1;
495
496                 int         top          = environment_top();
497                 context_t  *last_context = context;
498                 set_context(&compound_type->context);
499
500                 parse_compound_type_entries();
501
502                 assert(context == &compound_type->context);
503                 set_context(last_context);
504                 environment_pop_to(top);
505         }
506
507         return (type_t*) compound_type;
508 }
509
510 static void parse_enum_entries(void)
511 {
512         eat('{');
513
514         if(token.type == '}') {
515                 next_token();
516                 parse_error("empty enum not allowed");
517                 return;
518         }
519
520         do {
521                 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
522
523                 if(token.type != T_IDENTIFIER) {
524                         parse_error_expected("problem while parsing enum entry",
525                                              T_IDENTIFIER, 0);
526                         eat_block();
527                         return;
528                 }
529                 entry->storage_class   = STORAGE_CLASS_ENUM_ENTRY;
530                 entry->symbol          = token.v.symbol;
531                 entry->source_position = token.source_position;
532                 next_token();
533
534                 if(token.type == '=') {
535                         next_token();
536                         entry->initializer = parse_constant_expression();
537                 }
538
539                 record_declaration(entry);
540
541                 if(token.type != ',')
542                         break;
543                 next_token();
544         } while(token.type != '}');
545
546         expect_void('}');
547 }
548
549 static enum_type_t *find_enum_type(enum_type_t *types, const symbol_t *symbol)
550 {
551         enum_type_t *type = types;
552         for( ; type != NULL; type = type->next) {
553                 if(type->symbol == symbol)
554                         return type;
555         }
556
557         return NULL;
558 }
559
560 static type_t *parse_enum_specifier(void)
561 {
562         eat(T_enum);
563
564         symbol_t    *symbol    = NULL;
565         enum_type_t *enum_type = NULL;
566
567         if(token.type == T_IDENTIFIER) {
568                 symbol = token.v.symbol;
569                 next_token();
570
571                 if(context != NULL) {
572                         enum_type = find_enum_type(context->enums, symbol);
573                 }
574         } else if(token.type != '{') {
575                 parse_error_expected("problem while parsing enum type specifier",
576                                      T_IDENTIFIER, '{', 0);
577                 return NULL;
578         }
579
580         if(enum_type == NULL) {
581                 enum_type                  = allocate_type_zero(sizeof(enum_type[0]));
582                 enum_type->type.type       = TYPE_ENUM;
583                 enum_type->source_position = token.source_position;
584                 enum_type->symbol          = symbol;
585         }
586
587         if(token.type == '{') {
588                 if(enum_type->defined) {
589                         parser_print_error_prefix();
590                         fprintf(stderr, "multiple definitions of enum %s\n",
591                                 symbol->string);
592                         enum_type->entries_begin = NULL;
593                         enum_type->entries_end   = NULL;
594                 }
595                 enum_type->defined = 1;
596
597                 declaration_t *before = last_declaration;
598
599                 parse_enum_entries();
600
601                 if(before == NULL) {
602                         enum_type->entries_begin = context->declarations;
603                 } else {
604                         enum_type->entries_begin = before->next;
605                 }
606                 enum_type->entries_end = last_declaration;
607         }
608
609         return (type_t*) enum_type;
610 }
611
612 static type_t *parse_typeof(void)
613 {
614         eat(T___typeof__);
615
616         type_t *result;
617
618         expect('(');
619
620         declaration_t *declaration;
621         expression_t  *expression;
622
623 restart:
624         switch(token.type) {
625         case T___extension__:
626                 /* this can be a prefix to a typename or an expression */
627                 /* we simply eat it now. */
628                 do {
629                         next_token();
630                 } while(token.type == T___extension__);
631                 goto restart;
632
633         case T_IDENTIFIER:
634                 declaration = token.v.symbol->declaration;
635                 if(declaration != NULL
636                                 && declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
637                         result = parse_typename();
638                         break;
639                 }
640                 expression = parse_expression();
641                 result     = expression->datatype;
642                 break;
643
644         TYPENAME_START
645                 result = parse_typename();
646                 break;
647
648         default:
649                 expression = parse_expression();
650                 result     = expression->datatype;
651                 break;
652         }
653
654         expect(')');
655
656         return result;
657 }
658
659 static const char *parse_string_literals(void)
660 {
661         assert(token.type == T_STRING_LITERAL);
662         const char *result = token.v.string;
663
664         next_token();
665
666         while(token.type == T_STRING_LITERAL) {
667                 result = concat_strings(result, token.v.string);
668                 next_token();
669         }
670
671         return result;
672 }
673
674 static void parse_attributes(void)
675 {
676         while(true) {
677                 switch(token.type) {
678                 case T___attribute__:
679                         next_token();
680
681                         expect_void('(');
682                         int depth = 1;
683                         while(depth > 0) {
684                                 switch(token.type) {
685                                 case T_EOF:
686                                         parse_error("EOF while parsing attribute");
687                                         break;
688                                 case '(':
689                                         next_token();
690                                         depth++;
691                                         break;
692                                 case ')':
693                                         next_token();
694                                         depth--;
695                                         break;
696                                 default:
697                                         next_token();
698                                 }
699                         }
700                         break;
701                 case T_asm:
702                         next_token();
703                         expect_void('(');
704                         if(token.type != T_STRING_LITERAL) {
705                                 parse_error_expected("while parsing assembler attribute",
706                                                      T_STRING_LITERAL);
707                                 eat_brace();
708                                 break;
709                         } else {
710                                 parse_string_literals();
711                         }
712                         expect_void(')');
713                         break;
714                 default:
715                         goto attributes_finished;
716                 }
717         }
718
719 attributes_finished:
720         ;
721 }
722
723 typedef enum {
724         SPECIFIER_SIGNED    = 1 << 0,
725         SPECIFIER_UNSIGNED  = 1 << 1,
726         SPECIFIER_LONG      = 1 << 2,
727         SPECIFIER_INT       = 1 << 3,
728         SPECIFIER_DOUBLE    = 1 << 4,
729         SPECIFIER_CHAR      = 1 << 5,
730         SPECIFIER_SHORT     = 1 << 6,
731         SPECIFIER_LONG_LONG = 1 << 7,
732         SPECIFIER_FLOAT     = 1 << 8,
733         SPECIFIER_BOOL      = 1 << 9,
734         SPECIFIER_VOID      = 1 << 10,
735 #ifdef PROVIDE_COMPLEX
736         SPECIFIER_COMPLEX   = 1 << 11,
737 #endif
738 #ifdef PROVIDE_IMAGINARY
739         SPECIFIER_IMAGINARY = 1 << 12,
740 #endif
741 } specifiers_t;
742
743 static type_t *create_builtin_type(symbol_t *symbol)
744 {
745         builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
746         type->type.type      = TYPE_BUILTIN;
747         type->symbol         = symbol;
748
749         type_t *result = typehash_insert((type_t*) type);
750         if(result != (type_t*) type) {
751                 obstack_free(type_obst, type);
752         }
753
754         return result;
755 }
756
757 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
758 {
759         declaration_t *declaration;
760         type_t        *type            = NULL;
761         unsigned       type_qualifiers = 0;
762         unsigned       type_specifiers = 0;
763         int            newtype         = 0;
764
765         while(true) {
766                 switch(token.type) {
767
768                 /* storage class */
769 #define MATCH_STORAGE_CLASS(token, class)                                \
770                 case token:                                                      \
771                         if(specifiers->storage_class != STORAGE_CLASS_NONE) {        \
772                                 parse_error("multiple storage classes in declaration "   \
773                                             "specifiers");                               \
774                         }                                                            \
775                         specifiers->storage_class = class;                           \
776                         next_token();                                                \
777                         break;
778
779                 MATCH_STORAGE_CLASS(T_typedef,  STORAGE_CLASS_TYPEDEF)
780                 MATCH_STORAGE_CLASS(T_extern,   STORAGE_CLASS_EXTERN)
781                 MATCH_STORAGE_CLASS(T_static,   STORAGE_CLASS_STATIC)
782                 MATCH_STORAGE_CLASS(T_auto,     STORAGE_CLASS_AUTO)
783                 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
784
785                 /* type qualifiers */
786 #define MATCH_TYPE_QUALIFIER(token, qualifier)                          \
787                 case token:                                                     \
788                         type_qualifiers |= qualifier;                               \
789                         next_token();                                               \
790                         break;
791
792                 MATCH_TYPE_QUALIFIER(T_const,    TYPE_QUALIFIER_CONST);
793                 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
794                 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
795                 MATCH_TYPE_QUALIFIER(T_inline,   TYPE_QUALIFIER_INLINE);
796
797                 case T___extension__:
798                         /* TODO */
799                         next_token();
800                         break;
801
802                 /* type specifiers */
803 #define MATCH_SPECIFIER(token, specifier, name)                         \
804                 case token:                                                     \
805                         next_token();                                               \
806                         if(type_specifiers & specifier) {                           \
807                                 parse_error("multiple " name " type specifiers given"); \
808                         } else {                                                    \
809                                 type_specifiers |= specifier;                           \
810                         }                                                           \
811                         break;
812
813                 MATCH_SPECIFIER(T_void,       SPECIFIER_VOID,      "void")
814                 MATCH_SPECIFIER(T_char,       SPECIFIER_CHAR,      "char")
815                 MATCH_SPECIFIER(T_short,      SPECIFIER_SHORT,     "short")
816                 MATCH_SPECIFIER(T_int,        SPECIFIER_INT,       "int")
817                 MATCH_SPECIFIER(T_float,      SPECIFIER_FLOAT,     "float")
818                 MATCH_SPECIFIER(T_double,     SPECIFIER_DOUBLE,    "double")
819                 MATCH_SPECIFIER(T_signed,     SPECIFIER_SIGNED,    "signed")
820                 MATCH_SPECIFIER(T_unsigned,   SPECIFIER_UNSIGNED,  "unsigned")
821                 MATCH_SPECIFIER(T__Bool,      SPECIFIER_BOOL,      "_Bool")
822 #ifdef PROVIDE_COMPLEX
823                 MATCH_SPECIFIER(T__Complex,   SPECIFIER_COMPLEX,   "_Complex")
824 #endif
825 #ifdef PROVIDE_IMAGINARY
826                 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
827 #endif
828                 case T_long:
829                         next_token();
830                         if(type_specifiers & SPECIFIER_LONG_LONG) {
831                                 parse_error("multiple type specifiers given");
832                         } else if(type_specifiers & SPECIFIER_LONG) {
833                                 type_specifiers |= SPECIFIER_LONG_LONG;
834                         } else {
835                                 type_specifiers |= SPECIFIER_LONG;
836                         }
837                         break;
838
839                 /* TODO: if type != NULL for the following rules issue an error */
840                 case T_struct:
841                         type = parse_compound_type_specifier(true);
842                         break;
843                 case T_union:
844                         type = parse_compound_type_specifier(false);
845                         break;
846                 case T_enum:
847                         type = parse_enum_specifier();
848                         break;
849                 case T___typeof__:
850                         type = parse_typeof();
851                         break;
852                 case T___builtin_va_list:
853                         type = create_builtin_type(token.v.symbol);
854                         next_token();
855                         break;
856
857                 case T___attribute__:
858                         /* TODO */
859                         parse_attributes();
860                         break;
861
862                 case T_IDENTIFIER:
863                         declaration = token.v.symbol->declaration;
864                         if(declaration == NULL ||
865                                         declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
866                                 goto finish_specifiers;
867                         }
868
869                         type = declaration->type;
870                         assert(type != NULL);
871                         next_token();
872                         break;
873
874                 /* function specifier */
875                 default:
876                         goto finish_specifiers;
877                 }
878         }
879
880 finish_specifiers:
881
882         if(type == NULL) {
883                 atomic_type_type_t atomic_type;
884
885                 /* match valid basic types */
886                 switch(type_specifiers) {
887                 case SPECIFIER_VOID:
888                         atomic_type = ATOMIC_TYPE_VOID;
889                         break;
890                 case SPECIFIER_CHAR:
891                         atomic_type = ATOMIC_TYPE_CHAR;
892                         break;
893                 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
894                         atomic_type = ATOMIC_TYPE_SCHAR;
895                         break;
896                 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
897                         atomic_type = ATOMIC_TYPE_UCHAR;
898                         break;
899                 case SPECIFIER_SHORT:
900                 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
901                 case SPECIFIER_SHORT | SPECIFIER_INT:
902                 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
903                         atomic_type = ATOMIC_TYPE_SHORT;
904                         break;
905                 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
906                 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
907                         atomic_type = ATOMIC_TYPE_USHORT;
908                         break;
909                 case SPECIFIER_INT:
910                 case SPECIFIER_SIGNED:
911                 case SPECIFIER_SIGNED | SPECIFIER_INT:
912                         atomic_type = ATOMIC_TYPE_INT;
913                         break;
914                 case SPECIFIER_UNSIGNED:
915                 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
916                         atomic_type = ATOMIC_TYPE_UINT;
917                         break;
918                 case SPECIFIER_LONG:
919                 case SPECIFIER_SIGNED | SPECIFIER_LONG:
920                 case SPECIFIER_LONG | SPECIFIER_INT:
921                 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
922                         atomic_type = ATOMIC_TYPE_LONG;
923                         break;
924                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
925                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
926                         atomic_type = ATOMIC_TYPE_ULONG;
927                         break;
928                 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
929                 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
930                 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
931                 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
932                         | SPECIFIER_INT:
933                         atomic_type = ATOMIC_TYPE_LONGLONG;
934                         break;
935                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
936                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
937                         | SPECIFIER_INT:
938                         atomic_type = ATOMIC_TYPE_ULONGLONG;
939                         break;
940                 case SPECIFIER_FLOAT:
941                         atomic_type = ATOMIC_TYPE_FLOAT;
942                         break;
943                 case SPECIFIER_DOUBLE:
944                         atomic_type = ATOMIC_TYPE_DOUBLE;
945                         break;
946                 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
947                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
948                         break;
949                 case SPECIFIER_BOOL:
950                         atomic_type = ATOMIC_TYPE_BOOL;
951                         break;
952 #ifdef PROVIDE_COMPLEX
953                 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
954                         atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
955                         break;
956                 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
957                         atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
958                         break;
959                 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
960                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
961                         break;
962 #endif
963 #ifdef PROVIDE_IMAGINARY
964                 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
965                         atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
966                         break;
967                 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
968                         atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
969                         break;
970                 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
971                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
972                         break;
973 #endif
974                 default:
975                         /* invalid specifier combination, give an error message */
976                         if(type_specifiers == 0) {
977 #ifndef STRICT_C99
978                                 parse_warning("no type specifiers in declaration (using int)");
979                                 atomic_type = ATOMIC_TYPE_INT;
980                                 break;
981 #else
982                                 parse_error("no type specifiers given in declaration");
983 #endif
984                         } else if((type_specifiers & SPECIFIER_SIGNED) &&
985                                   (type_specifiers & SPECIFIER_UNSIGNED)) {
986                                 parse_error("signed and unsigned specifiers gives");
987                         } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
988                                 parse_error("only integer types can be signed or unsigned");
989                         } else {
990                                 parse_error("multiple datatypes in declaration");
991                         }
992                         atomic_type = ATOMIC_TYPE_INVALID;
993                 }
994
995                 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
996                 atype->type.type     = TYPE_ATOMIC;
997                 atype->atype         = atomic_type;
998                 newtype              = 1;
999
1000                 type = (type_t*) atype;
1001         } else {
1002                 if(type_specifiers != 0) {
1003                         parse_error("multiple datatypes in declaration");
1004                 }
1005         }
1006
1007         type->qualifiers = type_qualifiers;
1008
1009         type_t *result = typehash_insert(type);
1010         if(newtype && result != (type_t*) type) {
1011                 obstack_free(type_obst, type);
1012         }
1013
1014         specifiers->type = result;
1015 }
1016
1017 static type_qualifier_t parse_type_qualifiers(void)
1018 {
1019         type_qualifier_t type_qualifiers = 0;
1020
1021         while(true) {
1022                 switch(token.type) {
1023                 /* type qualifiers */
1024                 MATCH_TYPE_QUALIFIER(T_const,    TYPE_QUALIFIER_CONST);
1025                 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1026                 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1027                 MATCH_TYPE_QUALIFIER(T_inline,   TYPE_QUALIFIER_INLINE);
1028
1029                 default:
1030                         return type_qualifiers;
1031                 }
1032         }
1033 }
1034
1035 static void parse_identifier_list(void)
1036 {
1037         while(true) {
1038                 if(token.type != T_IDENTIFIER) {
1039                         parse_error_expected("problem while parsing parameter identifier "
1040                                              "list", T_IDENTIFIER, 0);
1041                         return;
1042                 }
1043                 next_token();
1044                 if(token.type != ',')
1045                         break;
1046                 next_token();
1047         }
1048 }
1049
1050 static declaration_t *parse_parameter(void)
1051 {
1052         declaration_specifiers_t specifiers;
1053         memset(&specifiers, 0, sizeof(specifiers));
1054
1055         parse_declaration_specifiers(&specifiers);
1056
1057         declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1058         parse_declarator(declaration, specifiers.storage_class,
1059                          specifiers.type, 1);
1060
1061         return declaration;
1062 }
1063
1064 static declaration_t *parse_parameters(method_type_t *type)
1065 {
1066         if(token.type == T_IDENTIFIER) {
1067                 symbol_t      *symbol      = token.v.symbol;
1068                 declaration_t *declaration = symbol->declaration;
1069                 if(declaration == NULL
1070                                 || declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
1071                         /* TODO */
1072                         parse_identifier_list();
1073                         return NULL;
1074                 }
1075         }
1076
1077         if(token.type == ')') {
1078                 type->unspecified_parameters = 1;
1079                 return NULL;
1080         }
1081         if(token.type == T_void && look_ahead(1)->type == ')') {
1082                 next_token();
1083                 return NULL;
1084         }
1085
1086         declaration_t      *declarations = NULL;
1087         declaration_t      *declaration;
1088         declaration_t      *last_declaration = NULL;
1089         method_parameter_t *parameter;
1090         method_parameter_t *last_parameter = NULL;
1091
1092         while(true) {
1093                 switch(token.type) {
1094                 case T_DOTDOTDOT:
1095                         next_token();
1096                         type->variadic = 1;
1097                         return declarations;
1098
1099                 case T_IDENTIFIER:
1100                 case T___extension__:
1101                 DECLARATION_START
1102                         declaration = parse_parameter();
1103
1104                         parameter       = allocate_type_zero(sizeof(parameter[0]));
1105                         parameter->type = declaration->type;
1106
1107                         if(last_parameter != NULL) {
1108                                 last_declaration->next = declaration;
1109                                 last_parameter->next   = parameter;
1110                         } else {
1111                                 type->parameters = parameter;
1112                                 declarations     = declaration;
1113                         }
1114                         last_parameter   = parameter;
1115                         last_declaration = declaration;
1116                         break;
1117
1118                 default:
1119                         return declarations;
1120                 }
1121                 if(token.type != ',')
1122                         return declarations;
1123                 next_token();
1124         }
1125 }
1126
1127 typedef enum {
1128         CONSTRUCT_POINTER,
1129         CONSTRUCT_METHOD,
1130         CONSTRUCT_ARRAY
1131 } construct_type_type_t;
1132
1133 typedef struct construct_type_t construct_type_t;
1134 struct construct_type_t {
1135         construct_type_type_t  type;
1136         construct_type_t      *next;
1137 };
1138
1139 typedef struct parsed_pointer_t parsed_pointer_t;
1140 struct parsed_pointer_t {
1141         construct_type_t  construct_type;
1142         type_qualifier_t  type_qualifiers;
1143 };
1144
1145 typedef struct construct_method_type_t construct_method_type_t;
1146 struct construct_method_type_t {
1147         construct_type_t  construct_type;
1148         method_type_t    *method_type;
1149 };
1150
1151 typedef struct parsed_array_t parsed_array_t;
1152 struct parsed_array_t {
1153         construct_type_t  construct_type;
1154         type_qualifier_t  type_qualifiers;
1155         bool              is_static;
1156         bool              is_variable;
1157         expression_t     *size;
1158 };
1159
1160 typedef struct construct_base_type_t construct_base_type_t;
1161 struct construct_base_type_t {
1162         construct_type_t  construct_type;
1163         type_t           *type;
1164 };
1165
1166 static construct_type_t *parse_pointer_declarator(void)
1167 {
1168         eat('*');
1169
1170         parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1171         memset(pointer, 0, sizeof(pointer[0]));
1172         pointer->type_qualifiers = parse_type_qualifiers();
1173
1174         return (construct_type_t*) pointer;
1175 }
1176
1177 static construct_type_t *parse_array_declarator(void)
1178 {
1179         eat('[');
1180
1181         parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1182         memset(array, 0, sizeof(array[0]));
1183
1184         if(token.type == T_static) {
1185                 array->is_static = true;
1186                 next_token();
1187         }
1188
1189         type_qualifier_t type_qualifiers = parse_type_qualifiers();
1190         if(type_qualifiers != 0) {
1191                 if(token.type == T_static) {
1192                         array->is_static = true;
1193                         next_token();
1194                 }
1195         }
1196         array->type_qualifiers = type_qualifiers;
1197
1198         if(token.type == '*' && look_ahead(1)->type == ']') {
1199                 array->is_variable = true;
1200                 next_token();
1201         } else if(token.type != ']') {
1202                 array->size = parse_assignment_expression();
1203         }
1204
1205         expect(']');
1206
1207         return (construct_type_t*) array;
1208 }
1209
1210 static construct_type_t *parse_method_declarator(declaration_t *declaration)
1211 {
1212         eat('(');
1213
1214         method_type_t *method_type
1215                 = allocate_type_zero(sizeof(method_type[0]));
1216         method_type->type.type   = TYPE_METHOD;
1217
1218         declaration_t *parameters = parse_parameters(method_type);
1219         if(declaration != NULL) {
1220                 declaration->context.declarations = parameters;
1221         }
1222
1223         construct_method_type_t *construct_method_type =
1224                 obstack_alloc(&temp_obst, sizeof(construct_method_type[0]));
1225         memset(construct_method_type, 0, sizeof(construct_method_type[0]));
1226         construct_method_type->construct_type.type = CONSTRUCT_METHOD;
1227         construct_method_type->method_type         = method_type;
1228
1229         expect(')');
1230
1231         return (construct_type_t*) construct_method_type;
1232 }
1233
1234 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1235                 int may_be_abstract)
1236 {
1237         construct_type_t *result = NULL;
1238         construct_type_t *last   = NULL;
1239
1240         while(token.type == '*') {
1241                 construct_type_t *type = parse_pointer_declarator();
1242                 if(last != NULL) {
1243                         last->next = type;
1244                 } else {
1245                         result = type;
1246                 }
1247                 last = type;
1248         }
1249
1250         /* TODO: find out if this is correct */
1251         parse_attributes();
1252
1253         construct_type_t *inner_types = NULL;
1254
1255         switch(token.type) {
1256         case T_IDENTIFIER:
1257                 if(declaration == NULL) {
1258                         parse_error("no identifier expected in typename");
1259                 } else {
1260                         declaration->symbol          = token.v.symbol;
1261                         declaration->source_position = token.source_position;
1262                 }
1263                 next_token();
1264                 break;
1265         case '(':
1266                 next_token();
1267                 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1268                 expect(')');
1269                 break;
1270         default:
1271                 if(may_be_abstract)
1272                         break;
1273                 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1274                                      '(', 0);
1275         }
1276
1277         while(true) {
1278                 construct_type_t *type;
1279                 switch(token.type) {
1280                 case '(':
1281                         type = parse_method_declarator(declaration);
1282                         break;
1283                 case '[':
1284                         type = parse_array_declarator();
1285                         break;
1286                 default:
1287                         goto declarator_finished;
1288                 }
1289
1290                 if(last != NULL) {
1291                         last->next = type;
1292                 } else {
1293                         result = type;
1294                 }
1295                 last = type;
1296         }
1297
1298 declarator_finished:
1299         parse_attributes();
1300
1301         if(inner_types != NULL) {
1302                 if(last != NULL) {
1303                         last->next = inner_types;
1304                 } else {
1305                         result = inner_types;
1306                 }
1307                 last = inner_types;
1308         }
1309
1310         return result;
1311 }
1312
1313 static type_t *construct_declarator_type(construct_type_t *construct_list,
1314                                          type_t *type)
1315 {
1316         construct_type_t *iter = construct_list;
1317         for( ; iter != NULL; iter = iter->next) {
1318                 parsed_pointer_t        *parsed_pointer;
1319                 parsed_array_t          *parsed_array;
1320                 construct_method_type_t *construct_method_type;
1321                 method_type_t           *method_type;
1322                 pointer_type_t          *pointer_type;
1323                 array_type_t            *array_type;
1324
1325                 switch(iter->type) {
1326                 case CONSTRUCT_METHOD:
1327                         construct_method_type = (construct_method_type_t*) iter;
1328                         method_type           = construct_method_type->method_type;
1329
1330                         method_type->result_type = type;
1331                         type                     = (type_t*) method_type;
1332                         break;
1333
1334                 case CONSTRUCT_POINTER:
1335                         parsed_pointer = (parsed_pointer_t*) iter;
1336                         pointer_type   = allocate_type_zero(sizeof(pointer_type[0]));
1337
1338                         pointer_type->type.type       = TYPE_POINTER;
1339                         pointer_type->points_to       = type;
1340                         pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
1341                         type                          = (type_t*) pointer_type;
1342                         break;
1343
1344                 case CONSTRUCT_ARRAY:
1345                         parsed_array  = (parsed_array_t*) iter;
1346                         array_type    = allocate_type_zero(sizeof(array_type[0]));
1347
1348                         array_type->type.type       = TYPE_ARRAY;
1349                         array_type->element_type    = type;
1350                         array_type->type.qualifiers = parsed_array->type_qualifiers;
1351                         array_type->is_static       = parsed_array->is_static;
1352                         array_type->is_variable     = parsed_array->is_variable;
1353                         array_type->size            = parsed_array->size;
1354                         type                        = (type_t*) array_type;
1355                         break;
1356                 }
1357
1358                 type_t *hashed_type = typehash_insert((type_t*) type);
1359                 if(hashed_type != type) {
1360                         obstack_free(type_obst, type);
1361                         type = hashed_type;
1362                 }
1363         }
1364
1365         return type;
1366 }
1367
1368 static void parse_declarator(declaration_t *declaration,
1369                              storage_class_t storage_class, type_t *type,
1370                              int may_be_abstract)
1371 {
1372         construct_type_t *construct_type
1373                 = parse_inner_declarator(declaration, may_be_abstract);
1374
1375         declaration->type         = construct_declarator_type(construct_type, type);
1376         declaration->storage_class = storage_class;
1377         if(construct_type != NULL) {
1378                 obstack_free(&temp_obst, construct_type);
1379         }
1380 }
1381
1382 static type_t *parse_abstract_declarator(type_t *base_type)
1383 {
1384         construct_type_t *construct_type
1385                 = parse_inner_declarator(NULL, 1);
1386
1387         if(construct_type == NULL)
1388                 return NULL;
1389
1390         type_t *result = construct_declarator_type(construct_type, base_type);
1391         obstack_free(&temp_obst, construct_type);
1392
1393         return result;
1394 }
1395
1396 static declaration_t *record_declaration(declaration_t *declaration)
1397 {
1398         if(context == NULL)
1399                 return declaration;
1400
1401         symbol_t *symbol = declaration->symbol;
1402         if(symbol != NULL) {
1403                 declaration_t *alias = environment_push(declaration, context);
1404                 if(alias != declaration)
1405                         return alias;
1406         }
1407
1408         if(last_declaration != NULL) {
1409                 last_declaration->next = declaration;
1410         } else {
1411                 context->declarations  = declaration;
1412         }
1413         last_declaration = declaration;
1414
1415         return declaration;
1416 }
1417
1418 static void parser_error_multiple_definition(declaration_t *previous,
1419                                              declaration_t *declaration)
1420 {
1421         parser_print_error_prefix_pos(declaration->source_position);
1422         fprintf(stderr, "multiple definition of symbol '%s'\n",
1423                 declaration->symbol->string);
1424         parser_print_error_prefix_pos(previous->source_position);
1425         fprintf(stderr, "this is the location of the previous "
1426                 "definition.\n");
1427         error();
1428 }
1429
1430 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1431 {
1432         while(true) {
1433                 declaration_t *ndeclaration
1434                         = allocate_ast_zero(sizeof(ndeclaration[0]));
1435
1436                 parse_declarator(ndeclaration, specifiers->storage_class,
1437                                  specifiers->type, 0);
1438                 declaration_t *declaration = record_declaration(ndeclaration);
1439                 if(token.type == '=') {
1440                         next_token();
1441
1442                         /* TODO: check that this is an allowed type (esp. no method type) */
1443
1444                         if(declaration->initializer != NULL) {
1445                                 parser_error_multiple_definition(declaration, ndeclaration);
1446                         }
1447
1448                         if(token.type == '{') {
1449                                 // TODO
1450                                 expect_void('}');
1451                         } else {
1452                                 declaration->initializer = parse_assignment_expression();
1453                         }
1454                 } else if(token.type == '{') {
1455                         if(declaration->type->type != TYPE_METHOD) {
1456                                 parser_print_error_prefix();
1457                                 fprintf(stderr, "Declarator ");
1458                                 print_type_ext(declaration->type, declaration->symbol, NULL);
1459                                 fprintf(stderr, " is not a method type.\n");
1460                         }
1461
1462                         if(declaration->initializer != NULL) {
1463                                 parser_error_multiple_definition(declaration, ndeclaration);
1464                         }
1465                         if(ndeclaration != declaration) {
1466                                 memcpy(&declaration->context, &ndeclaration->context,
1467                                        sizeof(declaration->context));
1468                         }
1469
1470                         int         top          = environment_top();
1471                         context_t  *last_context = context;
1472                         set_context(&declaration->context);
1473
1474                         /* push function parameters */
1475                         declaration_t *parameter = declaration->context.declarations;
1476                         for( ; parameter != NULL; parameter = parameter->next) {
1477                                 environment_push(parameter, context);
1478                         }
1479
1480                         statement_t *statement = parse_compound_statement();
1481
1482                         assert(context == &declaration->context);
1483                         set_context(last_context);
1484                         environment_pop_to(top);
1485
1486                         declaration->statement = statement;
1487                         return;
1488                 }
1489
1490                 if(token.type != ',')
1491                         break;
1492                 next_token();
1493         }
1494         expect_void(';');
1495 }
1496
1497 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1498 {
1499         while(1) {
1500                 if(token.type == ':') {
1501                         next_token();
1502                         parse_constant_expression();
1503                         /* TODO (bitfields) */
1504                 } else {
1505                         declaration_t *declaration
1506                                 = allocate_ast_zero(sizeof(declaration[0]));
1507                         parse_declarator(declaration, specifiers->storage_class,
1508                                          specifiers->type, 1);
1509
1510                         /* TODO: check for doubled fields */
1511                         record_declaration(declaration);
1512
1513                         if(token.type == ':') {
1514                                 next_token();
1515                                 parse_constant_expression();
1516                                 /* TODO (bitfields) */
1517                         }
1518                 }
1519
1520                 if(token.type != ',')
1521                         break;
1522                 next_token();
1523         }
1524         expect_void(';');
1525 }
1526
1527 static void parse_compound_type_entries(void)
1528 {
1529         eat('{');
1530
1531         while(token.type != '}' && token.type != T_EOF) {
1532                 declaration_specifiers_t specifiers;
1533                 memset(&specifiers, 0, sizeof(specifiers));
1534                 /* TODO not correct as this allows storage class stuff... but only
1535                  * specifiers and qualifiers sould be allowed here */
1536                 parse_declaration_specifiers(&specifiers);
1537
1538                 parse_struct_declarators(&specifiers);
1539         }
1540         if(token.type == T_EOF) {
1541                 parse_error("unexpected error while parsing struct");
1542         }
1543         next_token();
1544 }
1545
1546 static void parse_declaration(void)
1547 {
1548         declaration_specifiers_t specifiers;
1549         memset(&specifiers, 0, sizeof(specifiers));
1550         parse_declaration_specifiers(&specifiers);
1551
1552         if(token.type == ';') {
1553                 next_token();
1554                 return;
1555         }
1556         parse_init_declarators(&specifiers);
1557 }
1558
1559 static type_t *parse_typename(void)
1560 {
1561         declaration_specifiers_t specifiers;
1562         memset(&specifiers, 0, sizeof(specifiers));
1563         parse_declaration_specifiers(&specifiers);
1564         if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1565                 /* TODO: improve error message, user does probably not know what a
1566                  * storage class is...
1567                  */
1568                 parse_error("typename may not have a storage class");
1569         }
1570
1571         type_t *result = parse_abstract_declarator(specifiers.type);
1572
1573         return result;
1574 }
1575
1576
1577
1578
1579 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1580 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1581                                                           expression_t *left);
1582
1583 typedef struct expression_parser_function_t expression_parser_function_t;
1584 struct expression_parser_function_t {
1585         unsigned                         precedence;
1586         parse_expression_function        parser;
1587         unsigned                         infix_precedence;
1588         parse_expression_infix_function  infix_parser;
1589 };
1590
1591 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1592
1593 static expression_t *expected_expression_error(void)
1594 {
1595         parser_print_error_prefix();
1596         fprintf(stderr, "expected expression, got token ");
1597         print_token(stderr, & token);
1598         fprintf(stderr, "\n");
1599
1600         expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1601         expression->type = EXPR_INVALID;
1602         next_token();
1603
1604         return expression;
1605 }
1606
1607 static expression_t *parse_string_const(void)
1608 {
1609         string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1610
1611         cnst->expression.type     = EXPR_STRING_LITERAL;
1612         cnst->expression.datatype = type_string;
1613         cnst->value               = parse_string_literals();
1614
1615         return (expression_t*) cnst;
1616 }
1617
1618 static expression_t *parse_int_const(void)
1619 {
1620         const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1621
1622         cnst->expression.type     = EXPR_CONST;
1623         cnst->expression.datatype = type_int;
1624         cnst->v.int_value         = token.v.intvalue;
1625
1626         next_token();
1627
1628         return (expression_t*) cnst;
1629 }
1630
1631 static expression_t *parse_float_const(void)
1632 {
1633         const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1634
1635         cnst->expression.type     = EXPR_CONST;
1636         cnst->expression.datatype = type_int;
1637         cnst->v.float_value       = token.v.floatvalue;
1638
1639         next_token();
1640
1641         return (expression_t*) cnst;
1642 }
1643
1644 static expression_t *parse_reference(void)
1645 {
1646         reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1647
1648         ref->expression.type = EXPR_REFERENCE;
1649         ref->symbol          = token.v.symbol;
1650
1651         declaration_t *declaration = ref->symbol->declaration;
1652         next_token();
1653
1654         if(declaration == NULL) {
1655 #ifndef STRICT_C99
1656                 /* is it an implicitely defined function */
1657                 if(token.type == '(') {
1658                         parser_print_prefix_pos(token.source_position);
1659                         fprintf(stderr, "warning: implicit declaration of function '%s'\n",
1660                                 ref->symbol->string);
1661                         /* TODO: do this correctly */
1662                         return (expression_t*) ref;
1663                 }
1664 #endif
1665
1666                 parser_print_error_prefix();
1667                 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
1668         } else {
1669                 ref->declaration         = declaration;
1670                 ref->expression.datatype = declaration->type;
1671         }
1672
1673
1674         return (expression_t*) ref;
1675 }
1676
1677 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
1678 {
1679         (void) expression;
1680         (void) dest_type;
1681         /* TODO check if cast is allowed and issue warnings/errors */
1682 }
1683
1684 static expression_t *parse_cast(void)
1685 {
1686         unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
1687
1688         cast->expression.type            = EXPR_UNARY;
1689         cast->type                       = UNEXPR_CAST;
1690         cast->expression.source_position = token.source_position;
1691
1692         type_t *type  = parse_typename();
1693
1694         expect(')');
1695         expression_t *value = parse_sub_expression(20);
1696
1697         check_cast_allowed(value, type);
1698
1699         cast->expression.datatype = type;
1700         cast->value               = value;
1701
1702         return (expression_t*) cast;
1703 }
1704
1705 static expression_t *parse_statement_expression(void)
1706 {
1707         statement_expression_t *expression
1708                 = allocate_ast_zero(sizeof(expression[0]));
1709         expression->expression.type = EXPR_STATEMENT;
1710         expression->statement       = parse_compound_statement();
1711
1712         /* find last statement and use it's type */
1713         const statement_t *last_statement = NULL;
1714         const statement_t *statement      = expression->statement;
1715         for( ; statement != NULL; statement = statement->next) {
1716                 last_statement = statement;
1717         }
1718
1719         if(last_statement->type == STATEMENT_EXPRESSION) {
1720                 const expression_statement_t *expression_statement =
1721                         (const expression_statement_t*) last_statement;
1722                 expression->expression.datatype
1723                         = expression_statement->expression->datatype;
1724         } else {
1725                 expression->expression.datatype = type_void;
1726         }
1727
1728         expect(')');
1729
1730         return (expression_t*) expression;
1731 }
1732
1733 static expression_t *parse_brace_expression(void)
1734 {
1735         eat('(');
1736
1737         declaration_t *declaration;
1738         switch(token.type) {
1739         case '{':
1740                 /* gcc extension: a stement expression */
1741                 return parse_statement_expression();
1742
1743         TYPE_QUALIFIERS
1744         TYPE_SPECIFIERS
1745                 return parse_cast();
1746         case T_IDENTIFIER:
1747                 declaration = token.v.symbol->declaration;
1748                 if(declaration != NULL &&
1749                                 (declaration->storage_class == STORAGE_CLASS_TYPEDEF)) {
1750                         return parse_cast();
1751                 }
1752         }
1753
1754         expression_t *result = parse_expression();
1755         expect(')');
1756
1757         return result;
1758 }
1759
1760 static expression_t *parse_function_keyword(void)
1761 {
1762         eat(T___FUNCTION__);
1763         /* TODO */
1764
1765         string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1766         expression->expression.type     = EXPR_FUNCTION;
1767         expression->expression.datatype = type_string;
1768         expression->value               = "TODO: FUNCTION";
1769
1770         return (expression_t*) expression;
1771 }
1772
1773 static expression_t *parse_pretty_function_keyword(void)
1774 {
1775         eat(T___PRETTY_FUNCTION__);
1776         /* TODO */
1777
1778         string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1779         expression->expression.type     = EXPR_PRETTY_FUNCTION;
1780         expression->expression.datatype = type_string;
1781         expression->value               = "TODO: PRETTY FUNCTION";
1782
1783         return (expression_t*) expression;
1784 }
1785
1786 static member_designator_t *parse_member_designators(void)
1787 {
1788         member_designator_t *result = allocate_ast_zero(sizeof(result[0]));
1789
1790         if(token.type != T_IDENTIFIER) {
1791                 parse_error_expected("problem while parsing member designator",
1792                                      T_IDENTIFIER, 0);
1793                 eat_brace();
1794                 return NULL;
1795         }
1796         result->symbol = token.v.symbol;
1797         next_token();
1798
1799         member_designator_t *last_designator = result;
1800         while(true) {
1801                 if(token.type == '.') {
1802                         next_token();
1803                         if(token.type != T_IDENTIFIER) {
1804                                 parse_error_expected("problem while parsing member designator",
1805                                         T_IDENTIFIER, 0);
1806                                 eat_brace();
1807                                 return NULL;
1808                         }
1809                         member_designator_t *designator
1810                                 = allocate_ast_zero(sizeof(result[0]));
1811                         designator->symbol = token.v.symbol;
1812                         next_token();
1813
1814                         last_designator->next = designator;
1815                         last_designator       = designator;
1816                         continue;
1817                 }
1818                 if(token.type == '[') {
1819                         next_token();
1820                         member_designator_t *designator
1821                                 = allocate_ast_zero(sizeof(result[0]));
1822                         designator->array_access = parse_expression();
1823                         if(designator->array_access == NULL) {
1824                                 eat_brace();
1825                                 return NULL;
1826                         }
1827                         expect(']');
1828
1829                         last_designator->next = designator;
1830                         last_designator       = designator;
1831                         continue;
1832                 }
1833                 break;
1834         }
1835
1836         return result;
1837 }
1838
1839 static expression_t *parse_offsetof(void)
1840 {
1841         eat(T___builtin_offsetof);
1842
1843         offsetof_expression_t *expression
1844                 = allocate_ast_zero(sizeof(expression[0]));
1845         expression->expression.type     = EXPR_OFFSETOF;
1846         expression->expression.datatype = type_size_t;
1847
1848         expect('(');
1849         expression->type = parse_typename();
1850         expect(',');
1851         expression->member_designators = parse_member_designators();
1852         expect(')');
1853
1854         return (expression_t*) expression;
1855 }
1856
1857 static expression_t *parse_builtin_symbol(void)
1858 {
1859         builtin_symbol_expression_t *expression
1860                 = allocate_ast_zero(sizeof(expression[0]));
1861         expression->expression.type = EXPR_BUILTIN_SYMBOL;
1862
1863         /* TODO: set datatype */
1864
1865         expression->symbol = token.v.symbol;
1866
1867         next_token();
1868
1869         return (expression_t*) expression;
1870 }
1871
1872 static expression_t *parse_primary_expression(void)
1873 {
1874         switch(token.type) {
1875         case T_INTEGER:
1876                 return parse_int_const();
1877         case T_FLOATINGPOINT:
1878                 return parse_float_const();
1879         case T_STRING_LITERAL:
1880                 return parse_string_const();
1881         case T_IDENTIFIER:
1882                 return parse_reference();
1883         case T___FUNCTION__:
1884                 return parse_function_keyword();
1885         case T___PRETTY_FUNCTION__:
1886                 return parse_pretty_function_keyword();
1887         case T___builtin_offsetof:
1888                 return parse_offsetof();
1889         case T___builtin_expect:
1890         case T___builtin_va_start:
1891         case T___builtin_va_arg:
1892         case T___builtin_va_end:
1893                 return parse_builtin_symbol();
1894
1895         case '(':
1896                 return parse_brace_expression();
1897         }
1898
1899         parser_print_error_prefix();
1900         fprintf(stderr, "unexpected token ");
1901         print_token(stderr, &token);
1902         fprintf(stderr, "\n");
1903         eat_statement();
1904
1905         expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1906         expression->type     = EXPR_INVALID;
1907         expression->datatype = type_void;
1908
1909         return expression;
1910 }
1911
1912 static expression_t *parse_array_expression(unsigned precedence,
1913                                             expression_t *array_ref)
1914 {
1915         (void) precedence;
1916
1917         eat('[');
1918
1919         array_access_expression_t *array_access
1920                 = allocate_ast_zero(sizeof(array_access[0]));
1921
1922         array_access->expression.type     = EXPR_ARRAY_ACCESS;
1923         array_access->array_ref           = array_ref;
1924         array_access->index               = parse_expression();
1925
1926         type_t *array_type = array_ref->datatype;
1927         if(array_type != NULL) {
1928                 if(array_type->type == TYPE_POINTER) {
1929                         pointer_type_t *pointer           = (pointer_type_t*) array_type;
1930                         array_access->expression.datatype = pointer->points_to;
1931                 } else {
1932                         parser_print_error_prefix();
1933                         fprintf(stderr, "array access on object with non-pointer type ");
1934                         print_type(array_type);
1935                         fprintf(stderr, "\n");
1936                 }
1937         }
1938
1939         if(token.type != ']') {
1940                 parse_error_expected("Problem while parsing array access", ']', 0);
1941                 return (expression_t*) array_access;
1942         }
1943         next_token();
1944
1945         return (expression_t*) array_access;
1946 }
1947
1948 static bool is_declaration_specifier(const token_t *token,
1949                                      bool only_type_specifiers)
1950 {
1951         declaration_t *declaration;
1952
1953         switch(token->type) {
1954                 TYPE_SPECIFIERS
1955                         return 1;
1956                 case T_IDENTIFIER:
1957                         declaration = token->v.symbol->declaration;
1958                         if(declaration == NULL)
1959                                 return 0;
1960                         if(declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1961                                 return 0;
1962                         return 1;
1963                 STORAGE_CLASSES
1964                 TYPE_QUALIFIERS
1965                         if(only_type_specifiers)
1966                                 return 0;
1967                         return 1;
1968
1969                 default:
1970                         return 0;
1971         }
1972 }
1973
1974 static expression_t *parse_sizeof(unsigned precedence)
1975 {
1976         eat(T_sizeof);
1977
1978         sizeof_expression_t *sizeof_expression
1979                 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1980         sizeof_expression->expression.type     = EXPR_SIZEOF;
1981         sizeof_expression->expression.datatype = type_size_t;
1982
1983         if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
1984                 next_token();
1985                 sizeof_expression->type = parse_typename();
1986                 expect(')');
1987         } else {
1988                 expression_t *expression           = parse_sub_expression(precedence);
1989                 sizeof_expression->type            = expression->datatype;
1990                 sizeof_expression->size_expression = expression;
1991         }
1992
1993         return (expression_t*) sizeof_expression;
1994 }
1995
1996 static expression_t *parse_select_expression(unsigned precedence,
1997                                              expression_t *compound)
1998 {
1999         (void) precedence;
2000
2001         assert(token.type == '.' || token.type == T_MINUSGREATER);
2002         next_token();
2003
2004         select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2005
2006         select->expression.type = EXPR_SELECT;
2007         select->compound        = compound;
2008
2009         /* TODO: datatype */
2010
2011         if(token.type != T_IDENTIFIER) {
2012                 parse_error_expected("Problem while parsing select", T_IDENTIFIER, 0);
2013                 return (expression_t*) select;
2014         }
2015         select->symbol = token.v.symbol;
2016         next_token();
2017
2018         return (expression_t*) select;
2019 }
2020
2021 static expression_t *parse_call_expression(unsigned precedence,
2022                                            expression_t *expression)
2023 {
2024         (void) precedence;
2025         call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2026
2027         call->expression.type     = EXPR_CALL;
2028         call->method              = expression;
2029
2030         /* parse arguments */
2031         eat('(');
2032
2033         if(token.type != ')') {
2034                 call_argument_t *last_argument = NULL;
2035
2036                 while(true) {
2037                         call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2038
2039                         argument->expression = parse_assignment_expression();
2040                         if(last_argument == NULL) {
2041                                 call->arguments = argument;
2042                         } else {
2043                                 last_argument->next = argument;
2044                         }
2045                         last_argument = argument;
2046
2047                         if(token.type != ',')
2048                                 break;
2049                         next_token();
2050                 }
2051         }
2052         expect(')');
2053
2054         type_t *type = expression->datatype;
2055         if(type != NULL) {
2056                 /* we can call pointer to function */
2057                 if(type->type == TYPE_POINTER) {
2058                         pointer_type_t *pointer = (pointer_type_t*) type;
2059                         type = pointer->points_to;
2060                 }
2061
2062                 if(type == NULL || type->type != TYPE_METHOD) {
2063                         parser_print_error_prefix();
2064                         fprintf(stderr, "expected a method type for call but found type ");
2065                         print_type(expression->datatype);
2066                         fprintf(stderr, "\n");
2067                 } else {
2068                         method_type_t *method_type = (method_type_t*) type;
2069                         call->expression.datatype  = method_type->result_type;
2070                 }
2071         }
2072
2073         return (expression_t*) call;
2074 }
2075
2076 static void type_error(const char *msg, const source_position_t source_position,
2077                        const type_t *type)
2078 {
2079         parser_print_error_prefix_pos(source_position);
2080         fprintf(stderr, "%s, but found type ", msg);
2081         print_type(type);
2082         fputc('\n', stderr);
2083         error();
2084 }
2085
2086 static void type_error_incompatible(const char *msg,
2087                 const source_position_t source_position, const type_t *type1,
2088                 const type_t *type2)
2089 {
2090         parser_print_error_prefix_pos(source_position);
2091         fprintf(stderr, "%s, incompatible types: ", msg);
2092         print_type(type1);
2093         fprintf(stderr, " - ");
2094         print_type(type2);
2095         fprintf(stderr, ")\n");
2096         error();
2097 }
2098
2099 static type_t *get_type_after_conversion(const type_t *type1,
2100                                          const type_t *type2)
2101 {
2102         /* TODO... */
2103         (void) type2;
2104         return (type_t*) type1;
2105 }
2106
2107 static expression_t *parse_conditional_expression(unsigned precedence,
2108                                                   expression_t *expression)
2109 {
2110         eat('?');
2111
2112         conditional_expression_t *conditional
2113                 = allocate_ast_zero(sizeof(conditional[0]));
2114         conditional->expression.type = EXPR_CONDITIONAL;
2115         conditional->condition = expression;
2116
2117         /* 6.5.15.2 */
2118         type_t *condition_type = conditional->condition->datatype;
2119         if(condition_type != NULL) {
2120                 if(!is_type_scalar(condition_type)) {
2121                         type_error("expected a scalar type", expression->source_position,
2122                                    condition_type);
2123                 }
2124         }
2125
2126         conditional->true_expression = parse_expression();
2127         expect(':');
2128         conditional->false_expression = parse_sub_expression(precedence);
2129
2130         type_t *true_type  = conditional->true_expression->datatype;
2131         if(true_type == NULL)
2132                 return (expression_t*) conditional;
2133         type_t *false_type = conditional->false_expression->datatype;
2134         if(false_type == NULL)
2135                 return (expression_t*) conditional;
2136
2137         /* 6.4.15.3 */
2138         if(true_type == false_type) {
2139                 conditional->expression.datatype = true_type;
2140         } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2141                 type_t *result = get_type_after_conversion(true_type, false_type);
2142                 /* TODO: create implicit convs if necessary */
2143                 conditional->expression.datatype = result;
2144         } else if(true_type->type == TYPE_POINTER &&
2145                   false_type->type == TYPE_POINTER &&
2146                           true /* TODO compatible points_to types */) {
2147                 /* TODO */
2148         } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2149                || (is_null_ptr_const(false_type) &&
2150                    true_type->type == TYPE_POINTER) TODO*/ false) {
2151                 /* TODO */
2152         } else if(/* 1 is pointer to object type, other is void* */ false) {
2153                 /* TODO */
2154         } else {
2155                 type_error_incompatible("problem while parsing conditional",
2156                                         expression->source_position, true_type,
2157                                         false_type);
2158         }
2159
2160         return (expression_t*) conditional;
2161 }
2162
2163 static expression_t *parse_extension(unsigned precedence)
2164 {
2165         eat(T___extension__);
2166
2167         /* TODO enable extensions */
2168
2169         return parse_sub_expression(precedence);
2170 }
2171
2172 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type)     \
2173 static                                                                    \
2174 expression_t *parse_##unexpression_type(unsigned precedence)              \
2175 {                                                                         \
2176         eat(token_type);                                                      \
2177                                                                           \
2178         unary_expression_t *unary_expression                                  \
2179                 = allocate_ast_zero(sizeof(unary_expression[0]));                 \
2180         unary_expression->expression.type = EXPR_UNARY;                       \
2181         unary_expression->type            = unexpression_type;                \
2182         unary_expression->value           = parse_sub_expression(precedence); \
2183                                                                           \
2184         return (expression_t*) unary_expression;                              \
2185 }
2186
2187 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
2188 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
2189 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
2190 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
2191 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
2192 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
2193 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_PREFIX_INCREMENT)
2194 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
2195
2196 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
2197 static                                                                        \
2198 expression_t *parse_##unexpression_type(unsigned precedence,                  \
2199                                         expression_t *left)                   \
2200 {                                                                             \
2201         (void) precedence;                                                        \
2202         eat(token_type);                                                          \
2203                                                                               \
2204         unary_expression_t *unary_expression                                      \
2205                 = allocate_ast_zero(sizeof(unary_expression[0]));                     \
2206         unary_expression->expression.type = EXPR_UNARY;                           \
2207         unary_expression->type            = unexpression_type;                    \
2208         unary_expression->value           = left;                                 \
2209                                                                               \
2210         return (expression_t*) unary_expression;                                  \
2211 }
2212
2213 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_POSTFIX_INCREMENT)
2214 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
2215
2216 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type)    \
2217 static                                                           \
2218 expression_t *parse_##binexpression_type(unsigned precedence,    \
2219                                          expression_t *left)     \
2220 {                                                                \
2221         eat(token_type);                                             \
2222                                                                  \
2223         expression_t *right = parse_sub_expression(precedence);      \
2224                                                                  \
2225         binary_expression_t *binexpr                                 \
2226                 = allocate_ast_zero(sizeof(binexpr[0]));                 \
2227         binexpr->expression.type = EXPR_BINARY;                      \
2228         binexpr->type            = binexpression_type;               \
2229         binexpr->left            = left;                             \
2230         binexpr->right           = right;                            \
2231                                                                  \
2232         return (expression_t*) binexpr;                              \
2233 }
2234
2235 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA)
2236 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
2237 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
2238 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD)
2239 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
2240 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
2241 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
2242 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
2243 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
2244 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
2245 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL)
2246 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
2247 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
2248 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
2249 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
2250 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
2251 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
2252 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
2253 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
2254 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
2255 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN)
2256 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN)
2257 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN)
2258 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN)
2259 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN)
2260 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN)
2261 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN)
2262 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN)
2263 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN)
2264 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN)
2265
2266 static expression_t *parse_sub_expression(unsigned precedence)
2267 {
2268         if(token.type < 0) {
2269                 return expected_expression_error();
2270         }
2271
2272         expression_parser_function_t *parser
2273                 = &expression_parsers[token.type];
2274         source_position_t             source_position = token.source_position;
2275         expression_t                 *left;
2276
2277         if(parser->parser != NULL) {
2278                 left = parser->parser(parser->precedence);
2279         } else {
2280                 left = parse_primary_expression();
2281         }
2282         assert(left != NULL);
2283         left->source_position = source_position;
2284
2285         while(true) {
2286                 if(token.type < 0) {
2287                         return expected_expression_error();
2288                 }
2289
2290                 parser = &expression_parsers[token.type];
2291                 if(parser->infix_parser == NULL)
2292                         break;
2293                 if(parser->infix_precedence < precedence)
2294                         break;
2295
2296                 left = parser->infix_parser(parser->infix_precedence, left);
2297
2298                 assert(left != NULL);
2299                 assert(left->type != EXPR_INVALID);
2300                 left->source_position = source_position;
2301         }
2302
2303         return left;
2304 }
2305
2306 static expression_t *parse_expression(void)
2307 {
2308         return parse_sub_expression(1);
2309 }
2310
2311
2312
2313 void register_expression_parser(parse_expression_function parser,
2314                                 int token_type, unsigned precedence)
2315 {
2316         expression_parser_function_t *entry = &expression_parsers[token_type];
2317
2318         if(entry->parser != NULL) {
2319                 fprintf(stderr, "for token ");
2320                 print_token_type(stderr, token_type);
2321                 fprintf(stderr, "\n");
2322                 panic("trying to register multiple expression parsers for a token");
2323         }
2324         entry->parser     = parser;
2325         entry->precedence = precedence;
2326 }
2327
2328 void register_expression_infix_parser(parse_expression_infix_function parser,
2329                                       int token_type, unsigned precedence)
2330 {
2331         expression_parser_function_t *entry = &expression_parsers[token_type];
2332
2333         if(entry->infix_parser != NULL) {
2334                 fprintf(stderr, "for token ");
2335                 print_token_type(stderr, token_type);
2336                 fprintf(stderr, "\n");
2337                 panic("trying to register multiple infix expression parsers for a "
2338                       "token");
2339         }
2340         entry->infix_parser     = parser;
2341         entry->infix_precedence = precedence;
2342 }
2343
2344 static void init_expression_parsers(void)
2345 {
2346         memset(&expression_parsers, 0, sizeof(expression_parsers));
2347
2348         register_expression_infix_parser(parse_BINEXPR_MUL,         '*',        16);
2349         register_expression_infix_parser(parse_BINEXPR_DIV,         '/',        16);
2350         register_expression_infix_parser(parse_BINEXPR_MOD,         '%',        16);
2351         register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,   T_LESSLESS, 16);
2352         register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
2353                                                               T_GREATERGREATER, 16);
2354         register_expression_infix_parser(parse_BINEXPR_ADD,         '+',        15);
2355         register_expression_infix_parser(parse_BINEXPR_SUB,         '-',        15);
2356         register_expression_infix_parser(parse_BINEXPR_LESS,        '<',        14);
2357         register_expression_infix_parser(parse_BINEXPR_GREATER,     '>',        14);
2358         register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL,  14);
2359         register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
2360                                                                 T_GREATEREQUAL, 14);
2361         register_expression_infix_parser(parse_BINEXPR_EQUAL,     T_EQUALEQUAL, 13);
2362         register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
2363                                                         T_EXCLAMATIONMARKEQUAL, 13);
2364         register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&',        12);
2365         register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^',        11);
2366         register_expression_infix_parser(parse_BINEXPR_BITWISE_OR,  '|',        10);
2367         register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND,    9);
2368         register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR,  T_PIPEPIPE,  8);
2369         register_expression_infix_parser(parse_conditional_expression, '?',      7);
2370         register_expression_infix_parser(parse_BINEXPR_ASSIGN,      '=',         2);
2371         register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL,  2);
2372         register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
2373         register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
2374                                                                 T_ASTERISKEQUAL, 2);
2375         register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
2376         register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
2377                                                                  T_PERCENTEQUAL, 2);
2378         register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
2379                                                                 T_LESSLESSEQUAL, 2);
2380         register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
2381                                                           T_GREATERGREATEREQUAL, 2);
2382         register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
2383                                                                      T_ANDEQUAL, 2);
2384         register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
2385                                                                     T_PIPEEQUAL, 2);
2386         register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
2387                                                                    T_CARETEQUAL, 2);
2388
2389         register_expression_infix_parser(parse_BINEXPR_COMMA,       ',',         1);
2390
2391         register_expression_infix_parser(parse_array_expression,        '[',    30);
2392         register_expression_infix_parser(parse_call_expression,         '(',    30);
2393         register_expression_infix_parser(parse_select_expression,       '.',    30);
2394         register_expression_infix_parser(parse_select_expression,
2395                                                                 T_MINUSGREATER, 30);
2396         register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
2397                                          T_PLUSPLUS, 30);
2398         register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
2399                                          T_MINUSMINUS, 30);
2400
2401         register_expression_parser(parse_UNEXPR_NEGATE,           '-',          25);
2402         register_expression_parser(parse_UNEXPR_PLUS,             '+',          25);
2403         register_expression_parser(parse_UNEXPR_NOT,              '!',          25);
2404         register_expression_parser(parse_UNEXPR_BITWISE_NEGATE,   '~',          25);
2405         register_expression_parser(parse_UNEXPR_DEREFERENCE,      '*',          25);
2406         register_expression_parser(parse_UNEXPR_TAKE_ADDRESS,     '&',          25);
2407         register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS,   25);
2408         register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
2409         register_expression_parser(parse_sizeof,                  T_sizeof,     25);
2410         register_expression_parser(parse_extension,            T___extension__, 25);
2411 }
2412
2413
2414 static statement_t *parse_case_statement(void)
2415 {
2416         eat(T_case);
2417         case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2418         label->statement.type            = STATEMENT_CASE_LABEL;
2419         label->statement.source_position = token.source_position;
2420
2421         label->expression = parse_expression();
2422
2423         expect(':');
2424         label->statement.next = parse_statement();
2425
2426         return (statement_t*) label;
2427 }
2428
2429 static statement_t *parse_default_statement(void)
2430 {
2431         eat(T_default);
2432
2433         case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2434         label->statement.type            = STATEMENT_CASE_LABEL;
2435         label->statement.source_position = token.source_position;
2436
2437         expect(':');
2438         label->statement.next = parse_statement();
2439
2440         return (statement_t*) label;
2441 }
2442
2443 static statement_t *parse_label_statement(void)
2444 {
2445         eat(T_IDENTIFIER);
2446         expect(':');
2447         parse_statement();
2448
2449         return NULL;
2450 }
2451
2452 static statement_t *parse_if(void)
2453 {
2454         eat(T_if);
2455
2456         if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2457         statement->statement.type            = STATEMENT_IF;
2458         statement->statement.source_position = token.source_position;
2459
2460         expect('(');
2461         statement->condition = parse_expression();
2462         expect(')');
2463
2464         statement->true_statement = parse_statement();
2465         if(token.type == T_else) {
2466                 next_token();
2467                 statement->false_statement = parse_statement();
2468         }
2469
2470         return (statement_t*) statement;
2471 }
2472
2473 static statement_t *parse_switch(void)
2474 {
2475         eat(T_switch);
2476
2477         switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2478         statement->statement.type            = STATEMENT_SWITCH;
2479         statement->statement.source_position = token.source_position;
2480
2481         expect('(');
2482         statement->expression = parse_expression();
2483         expect(')');
2484         statement->body = parse_statement();
2485
2486         return (statement_t*) statement;
2487 }
2488
2489 static statement_t *parse_while(void)
2490 {
2491         eat(T_while);
2492
2493         while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2494         statement->statement.type            = STATEMENT_WHILE;
2495         statement->statement.source_position = token.source_position;
2496
2497         expect('(');
2498         statement->condition = parse_expression();
2499         expect(')');
2500         statement->body = parse_statement();
2501
2502         return (statement_t*) statement;
2503 }
2504
2505 static statement_t *parse_do(void)
2506 {
2507         eat(T_do);
2508
2509         do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2510         statement->statement.type            = STATEMENT_DO_WHILE;
2511         statement->statement.source_position = token.source_position;
2512
2513         statement->body = parse_statement();
2514         expect(T_while);
2515         expect('(');
2516         statement->condition = parse_expression();
2517         expect(')');
2518         expect(';');
2519
2520         return (statement_t*) statement;
2521 }
2522
2523 static statement_t *parse_for(void)
2524 {
2525         eat(T_for);
2526
2527         for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2528         statement->statement.type            = STATEMENT_FOR;
2529         statement->statement.source_position = token.source_position;
2530
2531         expect('(');
2532
2533         int         top          = environment_top();
2534         context_t  *last_context = context;
2535         set_context(&statement->context);
2536
2537         if(token.type != ';') {
2538                 if(is_declaration_specifier(&token, false)) {
2539                         parse_declaration();
2540                 } else {
2541                         statement->initialisation = parse_expression();
2542                         expect(';');
2543                 }
2544         } else {
2545                 expect(';');
2546         }
2547
2548         if(token.type != ';') {
2549                 statement->condition = parse_expression();
2550         }
2551         expect(';');
2552         if(token.type != ')') {
2553                 statement->step = parse_expression();
2554         }
2555         expect(')');
2556         statement->body = parse_statement();
2557
2558         assert(context == &statement->context);
2559         set_context(last_context);
2560         environment_pop_to(top);
2561
2562         return (statement_t*) statement;
2563 }
2564
2565 static statement_t *parse_goto(void)
2566 {
2567         eat(T_goto);
2568         expect(T_IDENTIFIER);
2569         expect(';');
2570
2571         return NULL;
2572 }
2573
2574 static statement_t *parse_continue(void)
2575 {
2576         eat(T_continue);
2577         expect(';');
2578
2579         statement_t *statement     = allocate_ast_zero(sizeof(statement[0]));
2580         statement->source_position = token.source_position;
2581         statement->type            = STATEMENT_CONTINUE;
2582
2583         return statement;
2584 }
2585
2586 static statement_t *parse_break(void)
2587 {
2588         eat(T_break);
2589         expect(';');
2590
2591         statement_t *statement     = allocate_ast_zero(sizeof(statement[0]));
2592         statement->source_position = token.source_position;
2593         statement->type            = STATEMENT_BREAK;
2594
2595         return statement;
2596 }
2597
2598 static statement_t *parse_return(void)
2599 {
2600         eat(T_return);
2601
2602         return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2603
2604         statement->statement.type            = STATEMENT_RETURN;
2605         statement->statement.source_position = token.source_position;
2606         if(token.type != ';') {
2607                 statement->return_value = parse_expression();
2608         }
2609         expect(';');
2610
2611         return (statement_t*) statement;
2612 }
2613
2614 static statement_t *parse_declaration_statement(void)
2615 {
2616         declaration_t *before = last_declaration;
2617
2618         declaration_statement_t *statement
2619                 = allocate_ast_zero(sizeof(statement[0]));
2620         statement->statement.type            = STATEMENT_DECLARATION;
2621         statement->statement.source_position = token.source_position;
2622
2623         declaration_specifiers_t specifiers;
2624         memset(&specifiers, 0, sizeof(specifiers));
2625         parse_declaration_specifiers(&specifiers);
2626
2627         if(token.type == ';') {
2628                 eat(';');
2629         } else {
2630                 parse_init_declarators(&specifiers);
2631         }
2632
2633         if(before == NULL) {
2634                 statement->declarations_begin = context->declarations;
2635         } else {
2636                 statement->declarations_begin = before->next;
2637         }
2638         statement->declarations_end = last_declaration;
2639
2640         return (statement_t*) statement;
2641 }
2642
2643 static statement_t *parse_expression_statement(void)
2644 {
2645         expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2646         statement->statement.type            = STATEMENT_EXPRESSION;
2647         statement->statement.source_position = token.source_position;
2648
2649         statement->expression = parse_expression();
2650
2651         expect(';');
2652
2653         return (statement_t*) statement;
2654 }
2655
2656 static statement_t *parse_statement(void)
2657 {
2658         declaration_t *declaration;
2659         statement_t   *statement = NULL;
2660
2661         /* declaration or statement */
2662         switch(token.type) {
2663         case T_case:
2664                 statement = parse_case_statement();
2665                 break;
2666
2667         case T_default:
2668                 statement = parse_default_statement();
2669                 break;
2670
2671         case '{':
2672                 statement = parse_compound_statement();
2673                 break;
2674
2675         case T_if:
2676                 statement = parse_if();
2677                 break;
2678
2679         case T_switch:
2680                 statement = parse_switch();
2681                 break;
2682
2683         case T_while:
2684                 statement = parse_while();
2685                 break;
2686
2687         case T_do:
2688                 statement = parse_do();
2689                 break;
2690
2691         case T_for:
2692                 statement = parse_for();
2693                 break;
2694
2695         case T_goto:
2696                 statement = parse_goto();
2697                 break;
2698
2699         case T_continue:
2700                 statement = parse_continue();
2701                 break;
2702
2703         case T_break:
2704                 statement = parse_break();
2705                 break;
2706
2707         case T_return:
2708                 statement = parse_return();
2709                 break;
2710
2711         case ';':
2712                 next_token();
2713                 statement = NULL;
2714                 break;
2715
2716         case T_IDENTIFIER:
2717                 if(look_ahead(1)->type == ':') {
2718                         statement = parse_label_statement();
2719                         break;
2720                 }
2721
2722                 declaration = token.v.symbol->declaration;
2723                 if(declaration != NULL &&
2724                                 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
2725                         statement = parse_declaration_statement();
2726                         break;
2727                 }
2728
2729                 statement = parse_expression_statement();
2730                 break;
2731
2732         case T___extension__:
2733                 /* this can be a prefix to a declaration or an expression statement */
2734                 /* we simply eat it now and parse the rest with tail recursion */
2735                 do {
2736                         next_token();
2737                 } while(token.type == T___extension__);
2738                 statement = parse_statement();
2739                 break;
2740
2741         DECLARATION_START
2742                 statement = parse_declaration_statement();
2743                 break;
2744
2745         default:
2746                 statement = parse_expression_statement();
2747                 break;
2748         }
2749
2750         assert(statement == NULL || statement->source_position.input_name != NULL);
2751
2752         return statement;
2753 }
2754
2755 static statement_t *parse_compound_statement(void)
2756 {
2757         eat('{');
2758
2759         compound_statement_t *compound_statement
2760                 = allocate_ast_zero(sizeof(compound_statement[0]));
2761         compound_statement->statement.type            = STATEMENT_COMPOUND;
2762         compound_statement->statement.source_position = token.source_position;
2763
2764         int        top          = environment_top();
2765         context_t *last_context = context;
2766         set_context(&compound_statement->context);
2767
2768         statement_t *last_statement = NULL;
2769
2770         while(token.type != '}' && token.type != T_EOF) {
2771                 statement_t *statement = parse_statement();
2772                 if(statement == NULL)
2773                         continue;
2774
2775                 if(last_statement != NULL) {
2776                         last_statement->next = statement;
2777                 } else {
2778                         compound_statement->statements = statement;
2779                 }
2780
2781                 while(statement->next != NULL)
2782                         statement = statement->next;
2783
2784                 last_statement = statement;
2785         }
2786
2787         assert(context == &compound_statement->context);
2788         set_context(last_context);
2789         environment_pop_to(top);
2790
2791         next_token();
2792
2793         return (statement_t*) compound_statement;
2794 }
2795
2796 static translation_unit_t *parse_translation_unit(void)
2797 {
2798         translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
2799
2800         assert(context == NULL);
2801         set_context(&unit->context);
2802
2803         while(token.type != T_EOF) {
2804                 parse_declaration();
2805         }
2806
2807         assert(context == &unit->context);
2808         context          = NULL;
2809         last_declaration = NULL;
2810
2811         return unit;
2812 }
2813
2814 translation_unit_t *parse(void)
2815 {
2816         obstack_init(&environment_obstack);
2817         environment_stack = NEW_ARR_F(environment_entry_t*, 0);
2818
2819         type_set_output(stderr);
2820
2821         lookahead_bufpos = 0;
2822         for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
2823                 next_token();
2824         }
2825         translation_unit_t *unit = parse_translation_unit();
2826
2827         DEL_ARR_F(environment_stack);
2828         obstack_free(&environment_obstack, NULL);
2829
2830         return unit;
2831 }
2832
2833 void init_parser(void)
2834 {
2835         init_expression_parsers();
2836         obstack_init(&temp_obst);
2837
2838         type_int        = make_atomic_type(ATOMIC_TYPE_INT, 0);
2839         type_size_t     = make_atomic_type(ATOMIC_TYPE_UINT, 0);
2840         type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
2841         type_void       = make_atomic_type(ATOMIC_TYPE_VOID, 0);
2842         type_string     = make_pointer_type(type_const_char, 0);
2843 }
2844
2845 void exit_parser(void)
2846 {
2847         obstack_free(&temp_obst, NULL);
2848 }