454f058e509dba38064fe261f5fd0eca09f57714
[cparser] / parser.c
1 #include <config.h>
2
3 #include <assert.h>
4 #include <stdarg.h>
5
6 #include "parser.h"
7 #include "lexer_t.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
18 struct environment_entry_t {
19         symbol_t            *symbol;
20         environment_entry_t *old_entry;
21         declaration_t       *declaration;
22         unsigned short       old_symbol_ID;
23 };
24
25 static token_t               token;
26 static struct obstack        environment_obstack;
27 static environment_entry_t **environment_stack = NULL;
28 static translation_unit_t   *translation_unit  = NULL;
29 static block_statement_t    *context           = NULL;
30
31 static
32 statement_t *parse_compound_statement(void);
33 static
34 statement_t *parse_statement(void);
35
36 static
37 expression_t *parse_sub_expression(unsigned precedence);
38 static
39 expression_t *parse_expression(void);
40
41 static inline
42 void *allocate_ast_zero(size_t size)
43 {
44         void *res = allocate_ast(size);
45         memset(res, 0, size);
46         return res;
47 }
48
49 static inline
50 void *allocate_type_zero(size_t size)
51 {
52         void *res = obstack_alloc(type_obst, size);
53         memset(res, 0, size);
54         return res;
55 }
56
57 /**
58  * pushs an environment_entry on the environment stack and links the
59  * corresponding symbol to the new entry
60  */
61 static inline
62 environment_entry_t *environment_push(symbol_t *symbol)
63 {
64         environment_entry_t *entry
65                 = obstack_alloc(&environment_obstack, sizeof(entry[0]));
66         memset(entry, 0, sizeof(entry[0]));
67
68         int top = ARR_LEN(environment_stack);
69         ARR_RESIZE(environment_stack, top + 1);
70         environment_stack[top] = entry;
71
72         entry->old_entry = symbol->thing;
73         entry->symbol    = symbol;
74         symbol->thing    = entry;
75
76         return entry;
77 }
78
79 /**
80  * pops symbols from the environment stack until @p new_top is the top element
81  */
82 static inline
83 void environment_pop_to(size_t new_top)
84 {
85         environment_entry_t *entry = NULL;
86         size_t top = ARR_LEN(environment_stack);
87         size_t i;
88
89         if(new_top == top)
90                 return;
91
92         assert(new_top < top);
93         i = top;
94         do {
95                 entry = environment_stack[i - 1];
96
97                 symbol_t *symbol = entry->symbol;
98
99 #if 0
100                 if(entry->type == ENTRY_LOCAL_VARIABLE
101                                 && entry->e.variable->refs == 0) {
102                         variable_declaration_statement_t *variable = entry->e.variable;
103                         print_warning_prefix(env, variable->statement.source_position);
104                         fprintf(stderr, "variable '%s' was declared but never read\n",
105                                 symbol->string);
106                 }
107 #endif
108
109                 if(entry->declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
110                         fprintf(stderr, "pop typename '%s'\n", entry->symbol->string);
111                         symbol->ID = entry->old_symbol_ID;
112                 }
113
114                 assert(symbol->thing == entry);
115                 symbol->thing = entry->old_entry;
116
117                 --i;
118         } while(i != new_top);
119         obstack_free(&environment_obstack, entry);
120
121         ARR_SHRINKLEN(environment_stack, (int) new_top);
122 }
123
124 /**
125  * returns the top element of the environment stack
126  */
127 static inline
128 size_t environment_top()
129 {
130         return ARR_LEN(environment_stack);
131 }
132
133
134
135 static inline
136 void next_token(void)
137 {
138         lexer_next_token(&token);
139
140 #ifdef PRINT_TOKENS
141         print_token(stderr, &token);
142         fprintf(stderr, "\n");
143 #endif
144 }
145
146 static inline
147 void eat(token_type_t type)
148 {
149         assert(token.type == type);
150         next_token();
151 }
152
153 void parser_print_error_prefix(void)
154 {
155     fputs(source_position.input_name, stderr);
156     fputc(':', stderr);
157     fprintf(stderr, "%d", source_position.linenr);
158     fputs(": error: ", stderr);
159 }
160
161 static
162 void parse_error(const char *message)
163 {
164         parser_print_error_prefix();
165         fprintf(stderr, "parse error: %s\n", message);
166 }
167
168 static
169 void parse_error_expected(const char *message, ...)
170 {
171         va_list args;
172         int first = 1;
173
174         if(message != NULL) {
175                 parser_print_error_prefix();
176                 fprintf(stderr, "%s\n", message);
177         }
178         parser_print_error_prefix();
179         fputs("Parse error: got ", stderr);
180         print_token(stderr, &token);
181         fputs(", expected ", stderr);
182
183         va_start(args, message);
184         token_type_t token_type = va_arg(args, token_type_t);
185         while(token_type != 0) {
186                 if(first == 1) {
187                         first = 0;
188                 } else {
189                         fprintf(stderr, ", ");
190                 }
191                 print_token_type(stderr, token_type);
192                 token_type = va_arg(args, token_type_t);
193         }
194         va_end(args);
195         fprintf(stderr, "\n");
196 }
197
198 static
199 void eat_until_semi(void)
200 {
201         while(token.type != ';') {
202                 next_token();
203                 if(token.type == T_EOF)
204                         return;
205         }
206         next_token();
207 }
208
209 #define expect(expected)                           \
210     if(UNLIKELY(token.type != (expected))) {       \
211         parse_error_expected(NULL, (expected), 0); \
212         eat_until_semi();                          \
213         return NULL;                               \
214     }                                              \
215     next_token();
216
217 #define expect_void(expected)                      \
218     if(UNLIKELY(token.type != (expected))) {       \
219         parse_error_expected(NULL, (expected), 0); \
220         eat_until_semi();                          \
221         return;                                    \
222     }                                              \
223     next_token();
224
225 static expression_t *parse_constant_expression(void)
226 {
227         /* TODO: not correct yet */
228         return parse_expression();
229 }
230
231 static compound_entry_t *parse_compound_type_entries(void);
232
233 typedef struct declaration_specifiers_t  declaration_specifiers_t;
234 struct declaration_specifiers_t {
235         storage_class_t  storage_class;
236         type_t          *type;
237 };
238
239 static type_t *parse_struct_specifier(void)
240 {
241         eat(T_struct);
242
243         compound_type_t *struct_type = allocate_type_zero(sizeof(struct_type[0]));
244         struct_type->type.type       = TYPE_COMPOUND_STRUCT;
245         struct_type->source_position = source_position;
246
247         if(token.type == T_IDENTIFIER) {
248                 /* TODO */
249                 next_token();
250                 if(token.type == '{') {
251                         parse_compound_type_entries();
252                 }
253         } else if(token.type == '{') {
254                 parse_compound_type_entries();
255         } else {
256                 parse_error_expected("problem while parsing struct type specifiers",
257                                      T_IDENTIFIER, '{');
258                 return NULL;
259         }
260
261         return (type_t*) struct_type;
262 }
263
264 static type_t *parse_union_specifier(void)
265 {
266         eat(T_union);
267
268         compound_type_t *union_type = allocate_type_zero(sizeof(union_type[0]));
269         union_type->type.type       = TYPE_COMPOUND_UNION;
270         union_type->source_position = source_position;
271
272         if(token.type == T_IDENTIFIER) {
273                 /* TODO */
274                 next_token();
275                 if(token.type == '{') {
276                         parse_compound_type_entries();
277                 }
278         } else if(token.type == '{') {
279                 parse_compound_type_entries();
280         } else {
281                 parse_error_expected("problem while parsing union type specifiers",
282                                      T_IDENTIFIER, '{');
283         }
284
285         return (type_t*) union_type;
286 }
287
288 typedef enum {
289         SPECIFIER_SIGNED    = 1 << 0,
290         SPECIFIER_UNSIGNED  = 1 << 1,
291         SPECIFIER_LONG      = 1 << 2,
292         SPECIFIER_INT       = 1 << 3,
293         SPECIFIER_DOUBLE    = 1 << 4,
294         SPECIFIER_CHAR      = 1 << 5,
295         SPECIFIER_SHORT     = 1 << 6,
296         SPECIFIER_LONG_LONG = 1 << 7,
297         SPECIFIER_FLOAT     = 1 << 8,
298         SPECIFIER_BOOL      = 1 << 9,
299         SPECIFIER_VOID      = 1 << 10,
300 #ifdef PROVIDE_COMPLEX
301         SPECIFIER_COMPLEX   = 1 << 11,
302 #endif
303 #ifdef PROVIDE_IMAGINARY
304         SPECIFIER_IMAGINARY = 1 << 12,
305 #endif
306 } specifiers_t;
307
308 #define STORAGE_CLASSES     \
309         case T_typedef:         \
310         case T_extern:          \
311         case T_static:          \
312         case T_auto:            \
313         case T_register:
314
315 #define TYPE_QUALIFIERS     \
316         case T_const:           \
317         case T_restrict:        \
318         case T_volatile:        \
319         case T_inline:          \
320         case T___extension__:   \
321         case T___attribute__:
322
323 #ifdef PROVIDE_COMPLEX
324 #define COMPLEX_SPECIFIERS  \
325         case T__Complex:
326 #else
327 #define COMPLEX_SPECIFIERS
328 #endif
329
330 #ifdef PROVIDE_IMAGINARY
331 #define IMAGINARY_SPECIFIERS \
332         case T__Imaginary:
333 #else
334 #define IMAGINARY_SPECIFIERS
335 #endif
336
337 #define TYPE_SPECIFIERS     \
338         case T_TYPENAME:        \
339         case T_void:            \
340         case T_char:            \
341         case T_short:           \
342         case T_int:             \
343         case T_long:            \
344         case T_float:           \
345         case T_double:          \
346         case T_signed:          \
347         case T_unsigned:        \
348         case T__Bool:           \
349         case T_struct:          \
350         case T_union:           \
351         case T_enum:            \
352         case T___quad_t:        \
353         case T___u_quad_t:      \
354         COMPLEX_SPECIFIERS      \
355         IMAGINARY_SPECIFIERS
356
357 static
358 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
359 {
360         type_t             *type            = NULL;
361         unsigned            type_qualifiers = 0;
362         unsigned            type_specifiers = 0;
363
364         while(1) {
365                 switch(token.type) {
366
367                 /* storage class */
368 #define MATCH_STORAGE_CLASS(token, class)                                \
369                 case token:                                                      \
370                         if(specifiers->storage_class != STORAGE_CLASS_NONE) {        \
371                                 parse_error("multiple storage classes in declaration "   \
372                                             "specifiers");                               \
373                         }                                                            \
374                         specifiers->storage_class = class;                           \
375                         next_token();                                                \
376                         break;
377
378                 MATCH_STORAGE_CLASS(T_typedef,  STORAGE_CLASS_TYPEDEF)
379                 MATCH_STORAGE_CLASS(T_extern,   STORAGE_CLASS_EXTERN)
380                 MATCH_STORAGE_CLASS(T_static,   STORAGE_CLASS_STATIC)
381                 MATCH_STORAGE_CLASS(T_auto,     STORAGE_CLASS_AUTO)
382                 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
383
384                 /* type qualifiers */
385 #define MATCH_TYPE_QUALIFIER(token, qualifier)                          \
386                 case token:                                                     \
387                         type_qualifiers |= qualifier;                               \
388                         next_token();                                               \
389                         break;
390
391                 MATCH_TYPE_QUALIFIER(T_const,    TYPE_QUALIFIER_CONST);
392                 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
393                 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
394                 MATCH_TYPE_QUALIFIER(T_inline,   TYPE_QUALIFIER_INLINE);
395
396                 case T___extension__:
397                         /* TODO */
398                         next_token();
399                         break;
400
401                 case T___attribute__:
402                         fprintf(stderr, "TODO: __attribute__ not handled yet\n");
403                         next_token();
404                         break;
405
406                 /* type specifiers */
407 #define MATCH_SPECIFIER(token, specifier, name)                         \
408                 case token:                                                     \
409                         next_token();                                               \
410                         if(type_specifiers & specifier) {                           \
411                                 parse_error("multiple " name " type specifiers given"); \
412                         } else {                                                    \
413                                 type_specifiers |= specifier;                           \
414                         }                                                           \
415                         break;
416
417                 MATCH_SPECIFIER(T_void,       SPECIFIER_VOID,      "void")
418                 MATCH_SPECIFIER(T_char,       SPECIFIER_CHAR,      "char")
419                 MATCH_SPECIFIER(T_short,      SPECIFIER_SHORT,     "short")
420                 MATCH_SPECIFIER(T_int,        SPECIFIER_INT,       "int")
421                 MATCH_SPECIFIER(T_float,      SPECIFIER_FLOAT,     "float")
422                 MATCH_SPECIFIER(T_double,     SPECIFIER_DOUBLE,    "double")
423                 MATCH_SPECIFIER(T_signed,     SPECIFIER_SIGNED,    "signed")
424                 MATCH_SPECIFIER(T_unsigned,   SPECIFIER_UNSIGNED,  "unsigned")
425                 MATCH_SPECIFIER(T__Bool,      SPECIFIER_BOOL,      "_Bool")
426 #ifdef PROVIDE_COMPLEX
427                 MATCH_SPECIFIER(T__Complex,   SPECIFIER_COMPLEX,   "_Complex")
428 #endif
429 #ifdef PROVIDE_IMAGINARY
430                 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
431 #endif
432                 case T_long:
433                         next_token();
434                         if(type_specifiers & SPECIFIER_LONG_LONG) {
435                                 parse_error("multiple type specifiers given");
436                         } else if(type_specifiers & SPECIFIER_LONG) {
437                                 type_specifiers |= SPECIFIER_LONG_LONG;
438                         } else {
439                                 type_specifiers |= SPECIFIER_LONG;
440                         }
441                         break;
442
443                 case T___quad_t:
444                         next_token();
445                         if(type_specifiers & SPECIFIER_LONG_LONG ||
446                                         type_specifiers & SPECIFIER_LONG) {
447                                 parse_error("multiple type specifiers given");
448                         } else {
449                                 type_specifiers |= SPECIFIER_LONG_LONG;
450                         }
451                         break;
452
453                 case T___u_quad_t:
454                         next_token();
455                         if(type_specifiers & SPECIFIER_LONG_LONG ||
456                                         type_specifiers & SPECIFIER_LONG ||
457                                         type_specifiers & SPECIFIER_UNSIGNED) {
458                                 parse_error("multiple type specifiers given");
459                         } else {
460                                 type_specifiers |= SPECIFIER_LONG_LONG | SPECIFIER_UNSIGNED;
461                         }
462                         break;
463
464                 case T_struct:
465                         type = parse_struct_specifier();
466                         break;
467                 case T_union:
468                         type = parse_union_specifier();
469                         break;
470                 case T_enum:
471                         /* TODO */
472                         assert(0);
473                         break;
474
475                 case T_TYPENAME:
476                         if(type != NULL || type_specifiers != 0) {
477                                 goto finish_specifiers;
478                         }
479
480                         type = token.v.symbol->thing->declaration->type;
481                         assert(type != NULL);
482                         next_token();
483                         break;
484
485                 /* function specifier */
486                 default:
487                         goto finish_specifiers;
488                 }
489         }
490
491 finish_specifiers:
492
493         if(type == NULL) {
494                 atomic_type_type_t atomic_type;
495
496                 /* match valid basic types */
497                 switch(type_specifiers) {
498                 case SPECIFIER_VOID:
499                         atomic_type = ATOMIC_TYPE_VOID;
500                         break;
501                 case SPECIFIER_CHAR:
502                         atomic_type = ATOMIC_TYPE_CHAR;
503                         break;
504                 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
505                         atomic_type = ATOMIC_TYPE_SCHAR;
506                         break;
507                 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
508                         atomic_type = ATOMIC_TYPE_UCHAR;
509                         break;
510                 case SPECIFIER_SHORT:
511                 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
512                 case SPECIFIER_SHORT | SPECIFIER_INT:
513                 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
514                         atomic_type = ATOMIC_TYPE_SHORT;
515                         break;
516                 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
517                 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
518                         atomic_type = ATOMIC_TYPE_USHORT;
519                         break;
520                 case SPECIFIER_INT:
521                 case SPECIFIER_SIGNED:
522                 case SPECIFIER_SIGNED | SPECIFIER_INT:
523                         atomic_type = ATOMIC_TYPE_INT;
524                         break;
525                 case SPECIFIER_UNSIGNED:
526                 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
527                         atomic_type = ATOMIC_TYPE_UINT;
528                         break;
529                 case SPECIFIER_LONG:
530                 case SPECIFIER_SIGNED | SPECIFIER_LONG:
531                 case SPECIFIER_LONG | SPECIFIER_INT:
532                 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
533                         atomic_type = ATOMIC_TYPE_LONG;
534                         break;
535                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
536                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
537                         atomic_type = ATOMIC_TYPE_ULONG;
538                         break;
539                 case SPECIFIER_LONG_LONG:
540                 case SPECIFIER_SIGNED | SPECIFIER_LONG_LONG:
541                 case SPECIFIER_LONG_LONG | SPECIFIER_INT:
542                 case SPECIFIER_SIGNED | SPECIFIER_LONG_LONG | SPECIFIER_INT:
543                         atomic_type = ATOMIC_TYPE_LONGLONG;
544                         break;
545                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG_LONG:
546                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG_LONG | SPECIFIER_INT:
547                         atomic_type = ATOMIC_TYPE_ULONGLONG;
548                         break;
549                 case SPECIFIER_FLOAT:
550                         atomic_type = ATOMIC_TYPE_FLOAT;
551                         break;
552                 case SPECIFIER_DOUBLE:
553                         atomic_type = ATOMIC_TYPE_DOUBLE;
554                         break;
555                 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
556                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
557                         break;
558                 case SPECIFIER_BOOL:
559                         atomic_type = ATOMIC_TYPE_BOOL;
560                         break;
561 #ifdef PROVIDE_COMPLEX
562                 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
563                         atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
564                         break;
565                 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
566                         atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
567                         break;
568                 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
569                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
570                         break;
571 #endif
572 #ifdef PROVIDE_IMAGINARY
573                 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
574                         atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
575                         break;
576                 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
577                         atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
578                         break;
579                 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
580                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
581                         break;
582 #endif
583                 default:
584                         /* invalid specifier combination, give an error message */
585                         if(type_specifiers == 0) {
586                                 parse_error("no type specifiers given in declaration");
587                         } else if((type_specifiers & SPECIFIER_SIGNED) &&
588                                   (type_specifiers & SPECIFIER_UNSIGNED)) {
589                                 parse_error("signed and unsigned specifiers gives");
590                         } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
591                                 parse_error("only integer types can be signed or unsigned");
592                         } else {
593                                 parse_error("multiple datatypes in declaration");
594                         }
595                         atomic_type = ATOMIC_TYPE_INVALID;
596                 }
597
598                 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
599                 atype->type.type     = TYPE_ATOMIC;
600                 atype->atype         = atomic_type;
601
602                 type = (type_t*) atype;
603         } else {
604                 if(type_specifiers != 0) {
605                         parse_error("multiple datatypes in declaration");
606                 }
607         }
608
609         type->qualifiers = type_qualifiers;
610
611         type_t *result = typehash_insert(type);
612         if(result != (type_t*) type) {
613                 obstack_free(type_obst, type);
614         }
615
616         specifiers->type = result;
617
618         fprintf(stderr, "Specifiers type: ");
619         print_type(stderr, result);
620         fprintf(stderr, "\n");
621 }
622
623 static
624 unsigned parse_type_qualifiers()
625 {
626         unsigned type_qualifiers = 0;
627
628         while(1) {
629                 switch(token.type) {
630                 /* type qualifiers */
631                 MATCH_TYPE_QUALIFIER(T_const,    TYPE_QUALIFIER_CONST);
632                 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
633                 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
634                 MATCH_TYPE_QUALIFIER(T_inline,   TYPE_QUALIFIER_INLINE);
635
636                 default:
637                         return type_qualifiers;
638                 }
639         }
640 }
641
642 static
643 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
644                       type_t *type)
645 {
646         while(token.type == '*') {
647                 /* pointer */
648                 next_token();
649
650                 pointer_type_t *pointer_type
651                         = allocate_type_zero(sizeof(pointer_type[0]));
652                 pointer_type->type.type = TYPE_POINTER;
653                 pointer_type->points_to = type;
654
655                 pointer_type->type.qualifiers = parse_type_qualifiers();
656
657                 type_t *result = typehash_insert((type_t*) pointer_type);
658                 if(result != (type_t*) pointer_type) {
659                         obstack_free(type_obst, pointer_type);
660                 }
661
662                 type = result;
663         }
664         declaration->storage_class = storage_class;
665         declaration->type          = type;
666
667         switch(token.type) {
668         case T_TYPENAME:
669         case T_IDENTIFIER:
670                 declaration->symbol = token.v.symbol;
671                 next_token();
672                 break;
673         case '(':
674                 next_token();
675                 parse_declarator(declaration, storage_class, type);
676                 expect_void(')');
677                 break;
678         default:
679                 parse_error("problem while parsing declarator");
680         }
681
682         if(token.type == '(') {
683                 next_token();
684
685                 /* parse parameter-type-list or identifier-list */
686
687                 expect_void(')');
688         } else if(token.type == '[') {
689                 next_token();
690
691                 /* multiple type qualifiers, and static */
692
693                 /* assignment_expression or '*' or nothing */
694
695                 expect_void(']');
696         }
697
698         fprintf(stderr, "Declarator type: ");
699         print_type(stderr, type);
700         fprintf(stderr, "\n");
701
702         symbol_t *symbol = declaration->symbol;
703
704         environment_entry_t *entry = environment_push(symbol);
705         entry->declaration         = declaration;
706         entry->old_symbol_ID       = symbol->ID;
707
708         if(storage_class == STORAGE_CLASS_TYPEDEF) {
709                 symbol->ID       = T_TYPENAME;
710                 fprintf(stderr, "typedef '%s'\n", symbol->string);
711         } else {
712                 symbol->ID       = T_IDENTIFIER;
713         }
714 }
715
716 static
717 void parse_init_declarators(const declaration_specifiers_t *specifiers)
718 {
719         while(1) {
720                 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
721
722                 parse_declarator(declaration, specifiers->storage_class,
723                                  specifiers->type);
724                 if(token.type == '=') {
725                         next_token();
726                         // parse_initializer TODO
727                 } else if(token.type == '{') {
728                         parse_compound_statement();
729                         return;
730                 }
731
732                 if(token.type != ',')
733                         break;
734                 next_token();
735         }
736         expect_void(';');
737 }
738
739 static
740 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
741 {
742         while(1) {
743                 declaration_t declaration;
744                 compound_entry_t *entry = allocate_ast_zero(sizeof(entry[0]));
745
746                 if(token.type == ':') {
747                         next_token();
748                         parse_constant_expression();
749                         /* TODO */
750                 } else {
751                         parse_declarator(&declaration, specifiers->storage_class,
752                                          specifiers->type);
753
754                         if(token.type == ':') {
755                                 next_token();
756                                 parse_constant_expression();
757                                 /* TODO */
758                         }
759                 }
760
761                 if(token.type != ',')
762                         break;
763                 next_token();
764         }
765         expect_void(';');
766 }
767
768 static compound_entry_t *parse_compound_type_entries(void)
769 {
770         eat('{');
771
772         compound_entry_t *entries = NULL;
773
774         while(token.type != '}' && token.type != T_EOF) {
775                 declaration_specifiers_t specifiers;
776                 memset(&specifiers, 0, sizeof(specifiers));
777                 /* TODO not correct as this allows storage class stuff... but only
778                  * specifiers and qualifiers sould be allowed here */
779                 parse_declaration_specifiers(&specifiers);
780
781                 parse_struct_declarators(&specifiers);
782         }
783         next_token();
784
785         return entries;
786 }
787
788 void parse_declaration(void)
789 {
790         declaration_specifiers_t specifiers;
791         memset(&specifiers, 0, sizeof(specifiers));
792         parse_declaration_specifiers(&specifiers);
793
794         if(token.type == ';') {
795                 next_token();
796                 return;
797         }
798         parse_init_declarators(&specifiers);
799 }
800
801
802
803 typedef expression_t* (*parse_expression_function) (unsigned precedence);
804 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
805                                                           expression_t *left);
806
807 typedef struct expression_parser_function_t expression_parser_function_t;
808 struct expression_parser_function_t {
809         unsigned                         precedence;
810         parse_expression_function        parser;
811         unsigned                         infix_precedence;
812         parse_expression_infix_function  infix_parser;
813 };
814
815 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
816
817 static
818 expression_t *expected_expression_error(void)
819 {
820         parser_print_error_prefix();
821         fprintf(stderr, "expected expression, got token ");
822         print_token(stderr, & token);
823         fprintf(stderr, "\n");
824
825         expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
826         expression->type = EXPR_INVALID;
827         next_token();
828
829         return expression;
830 }
831
832 static
833 expression_t *parse_string_const(void)
834 {
835         string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
836
837         cnst->expression.type = EXPR_STRING_LITERAL;
838         cnst->value           = token.v.string;
839
840         next_token();
841
842         return (expression_t*) cnst;
843 }
844
845 static
846 expression_t *parse_int_const(void)
847 {
848         const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
849
850         cnst->expression.type = EXPR_CONST;
851         cnst->value           = token.v.intvalue;
852
853         next_token();
854
855         return (expression_t*) cnst;
856 }
857
858 static
859 expression_t *parse_reference(void)
860 {
861         reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
862
863         ref->expression.type            = EXPR_REFERENCE;
864         ref->symbol                     = token.v.symbol;
865
866         next_token();
867
868         return (expression_t*) ref;
869 }
870
871 static
872 expression_t *parse_brace_expression(void)
873 {
874         eat('(');
875
876         expression_t *result = parse_expression();
877         expect(')');
878
879         return result;
880 }
881
882 static
883 expression_t *parse_primary_expression(void)
884 {
885         switch(token.type) {
886         case T_INTEGER:
887                 return parse_int_const();
888         case T_STRING_LITERAL:
889                 return parse_string_const();
890         case T_IDENTIFIER:
891                 return parse_reference();
892         case '(':
893                 return parse_brace_expression();
894         }
895
896         /* TODO: error message */
897         return NULL;
898 }
899
900 static
901 expression_t *parse_array_expression(unsigned precedence,
902                                      expression_t *array_ref)
903 {
904         (void) precedence;
905
906         eat('[');
907
908         array_access_expression_t *array_access
909                 = allocate_ast_zero(sizeof(array_access[0]));
910
911         array_access->expression.type = EXPR_ARRAY_ACCESS;
912         array_access->array_ref       = array_ref;
913         array_access->index           = parse_expression();
914
915         if(token.type != ']') {
916                 parse_error_expected("Problem while parsing array access", ']', 0);
917                 return NULL;
918         }
919         next_token();
920
921         return (expression_t*) array_access;
922 }
923
924 static
925 expression_t *parse_sizeof(unsigned precedence)
926 {
927         (void) precedence;
928         eat(T_sizeof);
929         /* TODO... */
930
931         return NULL;
932 }
933
934 static
935 expression_t *parse_select_expression(unsigned precedence,
936                                       expression_t *compound)
937 {
938         (void) precedence;
939
940         assert(token.type == '.' || token.type == T_SELECT);
941         next_token();
942
943         select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
944
945         select->expression.type = EXPR_SELECT;
946         select->compound        = compound;
947
948         if(token.type != T_IDENTIFIER) {
949                 parse_error_expected("Problem while parsing compound select",
950                                      T_IDENTIFIER, 0);
951                 return NULL;
952         }
953         select->symbol = token.v.symbol;
954         next_token();
955
956         return (expression_t*) select;
957 }
958
959 static
960 expression_t *parse_call_expression(unsigned precedence,
961                                     expression_t *expression)
962 {
963         (void) precedence;
964         call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
965
966         call->expression.type            = EXPR_CALL;
967         call->method                     = expression;
968
969         /* parse arguments */
970         eat('(');
971
972         if(token.type != ')') {
973                 call_argument_t *last_argument = NULL;
974
975                 while(1) {
976                         call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
977
978                         argument->expression = parse_expression();
979                         if(last_argument == NULL) {
980                                 call->arguments = argument;
981                         } else {
982                                 last_argument->next = argument;
983                         }
984                         last_argument = argument;
985
986                         if(token.type != ',')
987                                 break;
988                         next_token();
989                 }
990         }
991         expect(')');
992
993         return (expression_t*) call;
994 }
995
996 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type)     \
997 static                                                                    \
998 expression_t *parse_##unexpression_type(unsigned precedence)              \
999 {                                                                         \
1000         eat(token_type);                                                      \
1001                                                                           \
1002         unary_expression_t *unary_expression                                  \
1003                 = allocate_ast_zero(sizeof(unary_expression[0]));                 \
1004         unary_expression->expression.type = EXPR_UNARY;                       \
1005         unary_expression->type            = unexpression_type;                \
1006         unary_expression->value           = parse_sub_expression(precedence); \
1007                                                                           \
1008         return (expression_t*) unary_expression;                              \
1009 }
1010
1011 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1012 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1013 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1014 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1015 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1016 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1017 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_PREFIX_INCREMENT)
1018 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1019
1020 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1021 static                                                                        \
1022 expression_t *parse_##unexpression_type(unsigned precedence,                  \
1023                                         expression_t *left)                   \
1024 {                                                                             \
1025         (void) precedence;                                                        \
1026         eat(token_type);                                                          \
1027                                                                               \
1028         unary_expression_t *unary_expression                                      \
1029                 = allocate_ast_zero(sizeof(unary_expression[0]));                     \
1030         unary_expression->expression.type = EXPR_UNARY;                           \
1031         unary_expression->type            = unexpression_type;                    \
1032         unary_expression->value           = left;                                 \
1033                                                                               \
1034         return (expression_t*) unary_expression;                                  \
1035 }
1036
1037 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_POSTFIX_INCREMENT)
1038 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1039
1040 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type)    \
1041 static                                                           \
1042 expression_t *parse_##binexpression_type(unsigned precedence,    \
1043                                          expression_t *left)     \
1044 {                                                                \
1045         eat(token_type);                                             \
1046                                                                  \
1047         expression_t *right = parse_sub_expression(precedence);      \
1048                                                                  \
1049         binary_expression_t *binexpr                                 \
1050                 = allocate_ast_zero(sizeof(binexpr[0]));                 \
1051         binexpr->expression.type            = EXPR_BINARY;           \
1052         binexpr->type                       = binexpression_type;    \
1053         binexpr->left                       = left;                  \
1054         binexpr->right                      = right;                 \
1055                                                                  \
1056         return (expression_t*) binexpr;                              \
1057 }
1058
1059 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1060 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1061 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1062 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1063 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1064 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1065 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1066 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1067 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_NOTEQUAL)
1068 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1069 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1070 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1071 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1072 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1073 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1074 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1075
1076 static
1077 expression_t *parse_sub_expression(unsigned precedence)
1078 {
1079         if(token.type < 0) {
1080                 return expected_expression_error();
1081         }
1082
1083         expression_parser_function_t *parser
1084                 = &expression_parsers[token.type];
1085         source_position_t             source_position = source_position;
1086         expression_t                 *left;
1087
1088         if(parser->parser != NULL) {
1089                 left = parser->parser(parser->precedence);
1090         } else {
1091                 left = parse_primary_expression();
1092         }
1093         if(left != NULL)
1094                 left->source_position = source_position;
1095
1096         while(1) {
1097                 if(token.type < 0) {
1098                         return expected_expression_error();
1099                 }
1100
1101                 parser = &expression_parsers[token.type];
1102                 if(parser->infix_parser == NULL)
1103                         break;
1104                 if(parser->infix_precedence < precedence)
1105                         break;
1106
1107                 left = parser->infix_parser(parser->infix_precedence, left);
1108                 if(left != NULL)
1109                         left->source_position = source_position;
1110         }
1111
1112         return left;
1113 }
1114
1115 static
1116 expression_t *parse_expression(void)
1117 {
1118         return parse_sub_expression(1);
1119 }
1120
1121
1122
1123 void register_expression_parser(parse_expression_function parser,
1124                                 int token_type, unsigned precedence)
1125 {
1126         expression_parser_function_t *entry = &expression_parsers[token_type];
1127
1128         if(entry->parser != NULL) {
1129                 fprintf(stderr, "for token ");
1130                 print_token_type(stderr, token_type);
1131                 fprintf(stderr, "\n");
1132                 panic("trying to register multiple expression parsers for a token");
1133         }
1134         entry->parser     = parser;
1135         entry->precedence = precedence;
1136 }
1137
1138 void register_expression_infix_parser(parse_expression_infix_function parser,
1139                                       int token_type, unsigned precedence)
1140 {
1141         expression_parser_function_t *entry = &expression_parsers[token_type];
1142
1143         if(entry->infix_parser != NULL) {
1144                 fprintf(stderr, "for token ");
1145                 print_token_type(stderr, token_type);
1146                 fprintf(stderr, "\n");
1147                 panic("trying to register multiple infix expression parsers for a "
1148                       "token");
1149         }
1150         entry->infix_parser     = parser;
1151         entry->infix_precedence = precedence;
1152 }
1153
1154 static
1155 void init_expression_parsers(void)
1156 {
1157         memset(&expression_parsers, 0, sizeof(expression_parsers));
1158
1159         register_expression_infix_parser(parse_BINEXPR_MUL,       '*', 16);
1160         register_expression_infix_parser(parse_BINEXPR_DIV,       '/', 16);
1161         register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1162                                    T_LESSLESS, 16);
1163         register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1164                                    T_GREATERGREATER, 16);
1165         register_expression_infix_parser(parse_BINEXPR_ADD,       '+', 15);
1166         register_expression_infix_parser(parse_BINEXPR_SUB,       '-', 15);
1167         register_expression_infix_parser(parse_BINEXPR_LESS,      '<', 14);
1168         register_expression_infix_parser(parse_BINEXPR_GREATER,   '>', 14);
1169         register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1170         register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1171                                    T_GREATEREQUAL, 14);
1172         register_expression_infix_parser(parse_BINEXPR_EQUAL,     T_EQUALEQUAL, 13);
1173         register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1174                                          T_EXCLAMATIONMARKEQUAL, 13);
1175         register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&',        12);
1176         register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^',        11);
1177         register_expression_infix_parser(parse_BINEXPR_BITWISE_OR,  '|',        10);
1178         register_expression_infix_parser(parse_BINEXPR_ASSIGN,      T_EQUAL,     2);
1179
1180         register_expression_infix_parser(parse_array_expression,        '[',    30);
1181         register_expression_infix_parser(parse_call_expression,         '(',    30);
1182         register_expression_infix_parser(parse_select_expression,       '.',    30);
1183         register_expression_infix_parser(parse_select_expression,  T_SELECT,    30);
1184         register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
1185                                          T_PLUSPLUS, 30);
1186         register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
1187                                          T_MINUSMINUS, 30);
1188
1189         register_expression_parser(parse_UNEXPR_NEGATE,           '-',          25);
1190         register_expression_parser(parse_UNEXPR_PLUS,             '+',          25);
1191         register_expression_parser(parse_UNEXPR_NOT,              '!',          25);
1192         register_expression_parser(parse_UNEXPR_BITWISE_NEGATE,   '~',          25);
1193         register_expression_parser(parse_UNEXPR_DEREFERENCE,      '*',          25);
1194         register_expression_parser(parse_UNEXPR_TAKE_ADDRESS,     '&',          25);
1195         register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS,   25);
1196         register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
1197         register_expression_parser(parse_sizeof,                  T_sizeof,     25);
1198 }
1199
1200
1201 static
1202 statement_t *parse_case_statement(void)
1203 {
1204         eat(T_case);
1205         parse_expression();
1206         expect(':');
1207         parse_statement();
1208
1209         return NULL;
1210 }
1211
1212 static
1213 statement_t *parse_default_statement(void)
1214 {
1215         eat(T_default);
1216         expect(':');
1217         parse_statement();
1218
1219         return NULL;
1220 }
1221
1222 static
1223 statement_t *parse_label_statement(void)
1224 {
1225         eat(T_IDENTIFIER);
1226         expect(';');
1227         parse_statement();
1228
1229         return NULL;
1230 }
1231
1232 static
1233 statement_t *parse_if(void)
1234 {
1235         eat(T_if);
1236         expect('(');
1237         parse_expression();
1238         expect(')');
1239
1240         parse_statement();
1241         if(token.type == T_else) {
1242                 next_token();
1243                 parse_statement();
1244         }
1245
1246         return NULL;
1247 }
1248
1249 static
1250 statement_t *parse_switch(void)
1251 {
1252         eat(T_switch);
1253         expect('(');
1254         parse_expression();
1255         expect(')');
1256         parse_statement();
1257
1258         return NULL;
1259 }
1260
1261 static
1262 statement_t *parse_while(void)
1263 {
1264         eat(T_while);
1265         expect('(');
1266         parse_expression();
1267         expect(')');
1268         parse_statement();
1269
1270         return NULL;
1271 }
1272
1273 static
1274 statement_t *parse_do(void)
1275 {
1276         eat(T_do);
1277         parse_statement();
1278         expect(T_while);
1279         expect('(');
1280         parse_expression();
1281         expect(')');
1282
1283         return NULL;
1284 }
1285
1286 static
1287 statement_t *parse_for(void)
1288 {
1289         eat(T_for);
1290         expect('(');
1291         if(token.type != ';') {
1292                 /* TODO not correct... this could also be a declaration */
1293                 parse_expression();
1294         }
1295         expect(';');
1296         if(token.type != ';') {
1297                 parse_expression();
1298         }
1299         expect(';');
1300         if(token.type != ')') {
1301                 parse_expression();
1302         }
1303         expect(')');
1304         parse_statement();
1305
1306         return NULL;
1307 }
1308
1309 static
1310 statement_t *parse_goto(void)
1311 {
1312         eat(T_goto);
1313         expect(T_IDENTIFIER);
1314         expect(';');
1315
1316         return NULL;
1317 }
1318
1319 static
1320 statement_t *parse_continue(void)
1321 {
1322         eat(T_continue);
1323         expect(';');
1324
1325         return NULL;
1326 }
1327
1328 static
1329 statement_t *parse_break(void)
1330 {
1331         eat(T_break);
1332         expect(';');
1333
1334         return NULL;
1335 }
1336
1337 static
1338 statement_t *parse_return(void)
1339 {
1340         eat(T_return);
1341         parse_expression();
1342         expect(';');
1343
1344         return NULL;
1345 }
1346
1347 static
1348 statement_t *parse_declaration_statement(void)
1349 {
1350         parse_declaration();
1351         return NULL;
1352 }
1353
1354 static
1355 statement_t *parse_statement(void)
1356 {
1357         statement_t *statement = NULL;
1358
1359         /* declaration or statement */
1360         switch(token.type) {
1361         case T_case:
1362                 statement = parse_case_statement();
1363                 break;
1364
1365         case T_default:
1366                 statement = parse_default_statement();
1367                 break;
1368
1369         case T_IDENTIFIER:
1370                 statement = parse_label_statement();
1371                 break;
1372
1373         case '{':
1374                 statement = parse_compound_statement();
1375                 break;
1376
1377         case T_if:
1378                 statement = parse_if();
1379                 break;
1380
1381         case T_switch:
1382                 statement = parse_switch();
1383                 break;
1384
1385         case T_while:
1386                 statement = parse_while();
1387                 break;
1388
1389         case T_do:
1390                 statement = parse_do();
1391                 break;
1392
1393         case T_for:
1394                 statement = parse_for();
1395                 break;
1396
1397         case T_goto:
1398                 statement = parse_goto();
1399                 break;
1400
1401         case T_continue:
1402                 statement = parse_continue();
1403                 break;
1404
1405         case T_break:
1406                 statement = parse_break();
1407                 break;
1408
1409         case T_return:
1410                 statement = parse_return();
1411                 break;
1412
1413         case ';':
1414                 statement = NULL;
1415                 break;
1416
1417         STORAGE_CLASSES
1418         TYPE_QUALIFIERS
1419         TYPE_SPECIFIERS
1420                 statement = parse_declaration_statement();
1421                 break;
1422         }
1423
1424         return statement;
1425 }
1426
1427 static
1428 statement_t *parse_compound_statement(void)
1429 {
1430         eat('{');
1431
1432         int top = environment_top();
1433
1434         while(token.type != '}') {
1435                 parse_statement();
1436         }
1437
1438         environment_pop_to(top);
1439
1440         next_token();
1441
1442         return NULL;
1443 }
1444
1445 static
1446 translation_unit_t *parse_translation_unit(void)
1447 {
1448         translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
1449
1450         assert(translation_unit == NULL);
1451         assert(context == NULL);
1452         translation_unit = unit;
1453
1454         while(token.type != T_EOF) {
1455                 parse_declaration();
1456         }
1457
1458         translation_unit = NULL;
1459         return unit;
1460 }
1461
1462 translation_unit_t *parse(void)
1463 {
1464         obstack_init(&environment_obstack);
1465         environment_stack = NEW_ARR_F(environment_entry_t*, 0);
1466
1467         next_token();
1468         translation_unit_t *unit = parse_translation_unit();
1469
1470         DEL_ARR_F(environment_stack);
1471         obstack_free(&environment_obstack, NULL);
1472
1473         return unit;
1474 }
1475
1476 void init_parser(void)
1477 {
1478         init_expression_parsers();
1479 }
1480
1481 void exit_parser(void)
1482 {
1483 }