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