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