94c822b17463dae90fca6b48527ef059df0161f6
[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;
450                         }
451                         break;
452
453                         type_specifiers
454
455                 case T_struct:
456                         type = parse_struct_specifier();
457                         break;
458                 case T_union:
459                         type = parse_union_specifier();
460                         break;
461                 case T_enum:
462                         /* TODO */
463                         assert(0);
464                         break;
465
466                 case T_TYPENAME:
467                         if(type != NULL || type_specifiers != 0) {
468                                 goto finish_specifiers;
469                         }
470
471                         type = token.v.symbol->thing->declaration->type;
472                         assert(type != NULL);
473                         next_token();
474                         break;
475
476                 /* function specifier */
477                 default:
478                         goto finish_specifiers;
479                 }
480         }
481
482 finish_specifiers:
483
484         if(type == NULL) {
485                 atomic_type_type_t atomic_type;
486
487                 /* match valid basic types */
488                 switch(type_specifiers) {
489                 case SPECIFIER_VOID:
490                         atomic_type = ATOMIC_TYPE_VOID;
491                         break;
492                 case SPECIFIER_CHAR:
493                         atomic_type = ATOMIC_TYPE_CHAR;
494                         break;
495                 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
496                         atomic_type = ATOMIC_TYPE_SCHAR;
497                         break;
498                 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
499                         atomic_type = ATOMIC_TYPE_UCHAR;
500                         break;
501                 case SPECIFIER_SHORT:
502                 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
503                 case SPECIFIER_SHORT | SPECIFIER_INT:
504                 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
505                         atomic_type = ATOMIC_TYPE_SHORT;
506                         break;
507                 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
508                 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
509                         atomic_type = ATOMIC_TYPE_USHORT;
510                         break;
511                 case SPECIFIER_INT:
512                 case SPECIFIER_SIGNED:
513                 case SPECIFIER_SIGNED | SPECIFIER_INT:
514                         atomic_type = ATOMIC_TYPE_INT;
515                         break;
516                 case SPECIFIER_UNSIGNED:
517                 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
518                         atomic_type = ATOMIC_TYPE_UINT;
519                         break;
520                 case SPECIFIER_LONG:
521                 case SPECIFIER_SIGNED | SPECIFIER_LONG:
522                 case SPECIFIER_LONG | SPECIFIER_INT:
523                 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
524                         atomic_type = ATOMIC_TYPE_LONG;
525                         break;
526                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
527                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
528                         atomic_type = ATOMIC_TYPE_ULONG;
529                         break;
530                 case SPECIFIER_LONG_LONG:
531                 case SPECIFIER_SIGNED | SPECIFIER_LONG_LONG:
532                 case SPECIFIER_LONG_LONG | SPECIFIER_INT:
533                 case SPECIFIER_SIGNED | SPECIFIER_LONG_LONG | SPECIFIER_INT:
534                         atomic_type = ATOMIC_TYPE_LONGLONG;
535                         break;
536                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG_LONG:
537                 case SPECIFIER_UNSIGNED | SPECIFIER_LONG_LONG | SPECIFIER_INT:
538                         atomic_type = ATOMIC_TYPE_ULONGLONG;
539                         break;
540                 case SPECIFIER_FLOAT:
541                         atomic_type = ATOMIC_TYPE_FLOAT;
542                         break;
543                 case SPECIFIER_DOUBLE:
544                         atomic_type = ATOMIC_TYPE_DOUBLE;
545                         break;
546                 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
547                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
548                         break;
549                 case SPECIFIER_BOOL:
550                         atomic_type = ATOMIC_TYPE_BOOL;
551                         break;
552 #ifdef PROVIDE_COMPLEX
553                 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
554                         atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
555                         break;
556                 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
557                         atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
558                         break;
559                 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
560                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
561                         break;
562 #endif
563 #ifdef PROVIDE_IMAGINARY
564                 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
565                         atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
566                         break;
567                 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
568                         atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
569                         break;
570                 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
571                         atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
572                         break;
573 #endif
574                 default:
575                         /* invalid specifier combination, give an error message */
576                         if(type_specifiers == 0) {
577                                 parse_error("no type specifiers given in declaration");
578                         } else if((type_specifiers & SPECIFIER_SIGNED) &&
579                                   (type_specifiers & SPECIFIER_UNSIGNED)) {
580                                 parse_error("signed and unsigned specifiers gives");
581                         } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
582                                 parse_error("only integer types can be signed or unsigned");
583                         } else {
584                                 parse_error("multiple datatypes in declaration");
585                         }
586                         atomic_type = ATOMIC_TYPE_INVALID;
587                 }
588
589                 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
590                 atype->type.type     = TYPE_ATOMIC;
591                 atype->atype         = atomic_type;
592
593                 type = (type_t*) atype;
594         } else {
595                 if(type_specifiers != 0) {
596                         parse_error("multiple datatypes in declaration");
597                 }
598         }
599
600         type->qualifiers = type_qualifiers;
601
602         type_t *result = typehash_insert(type);
603         if(result != (type_t*) type) {
604                 obstack_free(type_obst, type);
605         }
606
607         specifiers->type = result;
608
609         fprintf(stderr, "Specifiers type: ");
610         print_type(stderr, result);
611         fprintf(stderr, "\n");
612 }
613
614 static
615 unsigned parse_type_qualifiers()
616 {
617         unsigned type_qualifiers = 0;
618
619         while(1) {
620                 switch(token.type) {
621                 /* type qualifiers */
622                 MATCH_TYPE_QUALIFIER(T_const,    TYPE_QUALIFIER_CONST);
623                 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
624                 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
625                 MATCH_TYPE_QUALIFIER(T_inline,   TYPE_QUALIFIER_INLINE);
626
627                 default:
628                         return type_qualifiers;
629                 }
630         }
631 }
632
633 static
634 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
635                       type_t *type)
636 {
637         while(token.type == '*') {
638                 /* pointer */
639                 next_token();
640
641                 pointer_type_t *pointer_type
642                         = allocate_type_zero(sizeof(pointer_type[0]));
643                 pointer_type->type.type = TYPE_POINTER;
644                 pointer_type->points_to = type;
645
646                 pointer_type->type.qualifiers = parse_type_qualifiers();
647
648                 type_t *result = typehash_insert((type_t*) pointer_type);
649                 if(result != (type_t*) pointer_type) {
650                         obstack_free(type_obst, pointer_type);
651                 }
652
653                 type = result;
654         }
655         declaration->storage_class = storage_class;
656         declaration->type          = type;
657
658         switch(token.type) {
659         case T_TYPENAME:
660         case T_IDENTIFIER:
661                 declaration->symbol = token.v.symbol;
662                 next_token();
663                 break;
664         case '(':
665                 next_token();
666                 parse_declarator(declaration, storage_class, type);
667                 expect_void(')');
668                 break;
669         default:
670                 parse_error("problem while parsing declarator");
671         }
672
673         if(token.type == '(') {
674                 next_token();
675
676                 /* parse parameter-type-list or identifier-list */
677
678                 expect_void(')');
679         } else if(token.type == '[') {
680                 next_token();
681
682                 /* multiple type qualifiers, and static */
683
684                 /* assignment_expression or '*' or nothing */
685
686                 expect_void(']');
687         }
688
689         fprintf(stderr, "Declarator type: ");
690         print_type(stderr, type);
691         fprintf(stderr, "\n");
692
693         symbol_t *symbol = declaration->symbol;
694
695         environment_entry_t *entry = environment_push(symbol);
696         entry->declaration         = declaration;
697         entry->old_symbol_ID       = symbol->ID;
698
699         if(storage_class == STORAGE_CLASS_TYPEDEF) {
700                 symbol->ID       = T_TYPENAME;
701         } else {
702                 symbol->ID       = T_IDENTIFIER;
703         }
704 }
705
706 static
707 void parse_init_declarators(const declaration_specifiers_t *specifiers)
708 {
709         while(1) {
710                 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
711
712                 parse_declarator(declaration, specifiers->storage_class,
713                                  specifiers->type);
714                 if(token.type == '=') {
715                         next_token();
716                         // parse_initializer TODO
717                 } else if(token.type == '{') {
718                         parse_compound_statement();
719                         return;
720                 }
721
722                 if(token.type != ',')
723                         break;
724                 next_token();
725         }
726         expect_void(';');
727 }
728
729 static
730 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
731 {
732         while(1) {
733                 declaration_t declaration;
734                 compound_entry_t *entry = allocate_ast_zero(sizeof(entry[0]));
735
736                 if(token.type == ':') {
737                         next_token();
738                         parse_constant_expression();
739                         /* TODO */
740                 } else {
741                         parse_declarator(&declaration, specifiers->storage_class,
742                                          specifiers->type);
743
744                         if(token.type == ':') {
745                                 next_token();
746                                 parse_constant_expression();
747                                 /* TODO */
748                         }
749                 }
750
751                 if(token.type != ',')
752                         break;
753                 next_token();
754         }
755         expect_void(';');
756 }
757
758 static compound_entry_t *parse_compound_type_entries(void)
759 {
760         eat('{');
761
762         compound_entry_t *entries = NULL;
763
764         while(token.type != '}' && token.type != T_EOF) {
765                 declaration_specifiers_t specifiers;
766                 memset(&specifiers, 0, sizeof(specifiers));
767                 /* TODO not correct as this allows storage class stuff... but only
768                  * specifiers and qualifiers sould be allowed here */
769                 parse_declaration_specifiers(&specifiers);
770
771                 parse_struct_declarators(&specifiers);
772         }
773         next_token();
774
775         return entries;
776 }
777
778 void parse_declaration(void)
779 {
780         declaration_specifiers_t specifiers;
781         memset(&specifiers, 0, sizeof(specifiers));
782         parse_declaration_specifiers(&specifiers);
783
784         if(token.type == ';') {
785                 next_token();
786                 return;
787         }
788         parse_init_declarators(&specifiers);
789 }
790
791
792
793 typedef expression_t* (*parse_expression_function) (unsigned precedence);
794 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
795                                                           expression_t *left);
796
797 typedef struct expression_parser_function_t expression_parser_function_t;
798 struct expression_parser_function_t {
799         unsigned                         precedence;
800         parse_expression_function        parser;
801         unsigned                         infix_precedence;
802         parse_expression_infix_function  infix_parser;
803 };
804
805 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
806
807 static
808 expression_t *expected_expression_error(void)
809 {
810         parser_print_error_prefix();
811         fprintf(stderr, "expected expression, got token ");
812         print_token(stderr, & token);
813         fprintf(stderr, "\n");
814
815         expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
816         expression->type = EXPR_INVALID;
817         next_token();
818
819         return expression;
820 }
821
822 static
823 expression_t *parse_string_const(void)
824 {
825         string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
826
827         cnst->expression.type = EXPR_STRING_LITERAL;
828         cnst->value           = token.v.string;
829
830         next_token();
831
832         return (expression_t*) cnst;
833 }
834
835 static
836 expression_t *parse_int_const(void)
837 {
838         const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
839
840         cnst->expression.type = EXPR_CONST;
841         cnst->value           = token.v.intvalue;
842
843         next_token();
844
845         return (expression_t*) cnst;
846 }
847
848 static
849 expression_t *parse_reference(void)
850 {
851         reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
852
853         ref->expression.type            = EXPR_REFERENCE;
854         ref->symbol                     = token.v.symbol;
855
856         next_token();
857
858         return (expression_t*) ref;
859 }
860
861 static
862 expression_t *parse_brace_expression(void)
863 {
864         eat('(');
865
866         expression_t *result = parse_expression();
867         expect(')');
868
869         return result;
870 }
871
872 static
873 expression_t *parse_primary_expression(void)
874 {
875         switch(token.type) {
876         case T_INTEGER:
877                 return parse_int_const();
878         case T_STRING_LITERAL:
879                 return parse_string_const();
880         case T_IDENTIFIER:
881                 return parse_reference();
882         case '(':
883                 return parse_brace_expression();
884         }
885
886         /* TODO: error message */
887         return NULL;
888 }
889
890 static
891 expression_t *parse_array_expression(unsigned precedence,
892                                      expression_t *array_ref)
893 {
894         (void) precedence;
895
896         eat('[');
897
898         array_access_expression_t *array_access
899                 = allocate_ast_zero(sizeof(array_access[0]));
900
901         array_access->expression.type = EXPR_ARRAY_ACCESS;
902         array_access->array_ref       = array_ref;
903         array_access->index           = parse_expression();
904
905         if(token.type != ']') {
906                 parse_error_expected("Problem while parsing array access", ']', 0);
907                 return NULL;
908         }
909         next_token();
910
911         return (expression_t*) array_access;
912 }
913
914 static
915 expression_t *parse_sizeof(unsigned precedence)
916 {
917         (void) precedence;
918         eat(T_sizeof);
919         /* TODO... */
920
921         return NULL;
922 }
923
924 static
925 expression_t *parse_select_expression(unsigned precedence,
926                                       expression_t *compound)
927 {
928         (void) precedence;
929
930         assert(token.type == '.' || token.type == T_SELECT);
931         next_token();
932
933         select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
934
935         select->expression.type = EXPR_SELECT;
936         select->compound        = compound;
937
938         if(token.type != T_IDENTIFIER) {
939                 parse_error_expected("Problem while parsing compound select",
940                                      T_IDENTIFIER, 0);
941                 return NULL;
942         }
943         select->symbol = token.v.symbol;
944         next_token();
945
946         return (expression_t*) select;
947 }
948
949 static
950 expression_t *parse_call_expression(unsigned precedence,
951                                     expression_t *expression)
952 {
953         (void) precedence;
954         call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
955
956         call->expression.type            = EXPR_CALL;
957         call->method                     = expression;
958
959         /* parse arguments */
960         eat('(');
961
962         if(token.type != ')') {
963                 call_argument_t *last_argument = NULL;
964
965                 while(1) {
966                         call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
967
968                         argument->expression = parse_expression();
969                         if(last_argument == NULL) {
970                                 call->arguments = argument;
971                         } else {
972                                 last_argument->next = argument;
973                         }
974                         last_argument = argument;
975
976                         if(token.type != ',')
977                                 break;
978                         next_token();
979                 }
980         }
981         expect(')');
982
983         return (expression_t*) call;
984 }
985
986 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type)     \
987 static                                                                    \
988 expression_t *parse_##unexpression_type(unsigned precedence)              \
989 {                                                                         \
990         eat(token_type);                                                      \
991                                                                           \
992         unary_expression_t *unary_expression                                  \
993                 = allocate_ast_zero(sizeof(unary_expression[0]));                 \
994         unary_expression->expression.type = EXPR_UNARY;                       \
995         unary_expression->type            = unexpression_type;                \
996         unary_expression->value           = parse_sub_expression(precedence); \
997                                                                           \
998         return (expression_t*) unary_expression;                              \
999 }
1000
1001 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1002 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1003 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1004 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1005 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1006 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1007 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_PREFIX_INCREMENT)
1008 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1009
1010 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1011 static                                                                        \
1012 expression_t *parse_##unexpression_type(unsigned precedence,                  \
1013                                         expression_t *left)                   \
1014 {                                                                             \
1015         (void) precedence;                                                        \
1016         eat(token_type);                                                          \
1017                                                                               \
1018         unary_expression_t *unary_expression                                      \
1019                 = allocate_ast_zero(sizeof(unary_expression[0]));                     \
1020         unary_expression->expression.type = EXPR_UNARY;                           \
1021         unary_expression->type            = unexpression_type;                    \
1022         unary_expression->value           = left;                                 \
1023                                                                               \
1024         return (expression_t*) unary_expression;                                  \
1025 }
1026
1027 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_POSTFIX_INCREMENT)
1028 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1029
1030 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type)    \
1031 static                                                           \
1032 expression_t *parse_##binexpression_type(unsigned precedence,    \
1033                                          expression_t *left)     \
1034 {                                                                \
1035         eat(token_type);                                             \
1036                                                                  \
1037         expression_t *right = parse_sub_expression(precedence);      \
1038                                                                  \
1039         binary_expression_t *binexpr                                 \
1040                 = allocate_ast_zero(sizeof(binexpr[0]));                 \
1041         binexpr->expression.type            = EXPR_BINARY;           \
1042         binexpr->type                       = binexpression_type;    \
1043         binexpr->left                       = left;                  \
1044         binexpr->right                      = right;                 \
1045                                                                  \
1046         return (expression_t*) binexpr;                              \
1047 }
1048
1049 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1050 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1051 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1052 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1053 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1054 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1055 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1056 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1057 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_NOTEQUAL)
1058 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1059 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1060 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1061 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1062 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1063 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1064 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1065
1066 static
1067 expression_t *parse_sub_expression(unsigned precedence)
1068 {
1069         if(token.type < 0) {
1070                 return expected_expression_error();
1071         }
1072
1073         expression_parser_function_t *parser
1074                 = &expression_parsers[token.type];
1075         source_position_t             source_position = source_position;
1076         expression_t                 *left;
1077
1078         if(parser->parser != NULL) {
1079                 left = parser->parser(parser->precedence);
1080         } else {
1081                 left = parse_primary_expression();
1082         }
1083         if(left != NULL)
1084                 left->source_position = source_position;
1085
1086         while(1) {
1087                 if(token.type < 0) {
1088                         return expected_expression_error();
1089                 }
1090
1091                 parser = &expression_parsers[token.type];
1092                 if(parser->infix_parser == NULL)
1093                         break;
1094                 if(parser->infix_precedence < precedence)
1095                         break;
1096
1097                 left = parser->infix_parser(parser->infix_precedence, left);
1098                 if(left != NULL)
1099                         left->source_position = source_position;
1100         }
1101
1102         return left;
1103 }
1104
1105 static
1106 expression_t *parse_expression(void)
1107 {
1108         return parse_sub_expression(1);
1109 }
1110
1111
1112
1113 void register_expression_parser(parse_expression_function parser,
1114                                 int token_type, unsigned precedence)
1115 {
1116         expression_parser_function_t *entry = &expression_parsers[token_type];
1117
1118         if(entry->parser != NULL) {
1119                 fprintf(stderr, "for token ");
1120                 print_token_type(stderr, token_type);
1121                 fprintf(stderr, "\n");
1122                 panic("trying to register multiple expression parsers for a token");
1123         }
1124         entry->parser     = parser;
1125         entry->precedence = precedence;
1126 }
1127
1128 void register_expression_infix_parser(parse_expression_infix_function parser,
1129                                       int token_type, unsigned precedence)
1130 {
1131         expression_parser_function_t *entry = &expression_parsers[token_type];
1132
1133         if(entry->infix_parser != NULL) {
1134                 fprintf(stderr, "for token ");
1135                 print_token_type(stderr, token_type);
1136                 fprintf(stderr, "\n");
1137                 panic("trying to register multiple infix expression parsers for a "
1138                       "token");
1139         }
1140         entry->infix_parser     = parser;
1141         entry->infix_precedence = precedence;
1142 }
1143
1144 static
1145 void init_expression_parsers(void)
1146 {
1147         memset(&expression_parsers, 0, sizeof(expression_parsers));
1148
1149         register_expression_infix_parser(parse_BINEXPR_MUL,       '*', 16);
1150         register_expression_infix_parser(parse_BINEXPR_DIV,       '/', 16);
1151         register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1152                                    T_LESSLESS, 16);
1153         register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1154                                    T_GREATERGREATER, 16);
1155         register_expression_infix_parser(parse_BINEXPR_ADD,       '+', 15);
1156         register_expression_infix_parser(parse_BINEXPR_SUB,       '-', 15);
1157         register_expression_infix_parser(parse_BINEXPR_LESS,      '<', 14);
1158         register_expression_infix_parser(parse_BINEXPR_GREATER,   '>', 14);
1159         register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1160         register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1161                                    T_GREATEREQUAL, 14);
1162         register_expression_infix_parser(parse_BINEXPR_EQUAL,     T_EQUALEQUAL, 13);
1163         register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1164                                          T_EXCLAMATIONMARKEQUAL, 13);
1165         register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&',        12);
1166         register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^',        11);
1167         register_expression_infix_parser(parse_BINEXPR_BITWISE_OR,  '|',        10);
1168         register_expression_infix_parser(parse_BINEXPR_ASSIGN,      T_EQUAL,     2);
1169
1170         register_expression_infix_parser(parse_array_expression,        '[',    30);
1171         register_expression_infix_parser(parse_call_expression,         '(',    30);
1172         register_expression_infix_parser(parse_select_expression,       '.',    30);
1173         register_expression_infix_parser(parse_select_expression,  T_SELECT,    30);
1174         register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
1175                                          T_PLUSPLUS, 30);
1176         register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
1177                                          T_MINUSMINUS, 30);
1178
1179         register_expression_parser(parse_UNEXPR_NEGATE,           '-',          25);
1180         register_expression_parser(parse_UNEXPR_PLUS,             '+',          25);
1181         register_expression_parser(parse_UNEXPR_NOT,              '!',          25);
1182         register_expression_parser(parse_UNEXPR_BITWISE_NEGATE,   '~',          25);
1183         register_expression_parser(parse_UNEXPR_DEREFERENCE,      '*',          25);
1184         register_expression_parser(parse_UNEXPR_TAKE_ADDRESS,     '&',          25);
1185         register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS,   25);
1186         register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
1187         register_expression_parser(parse_sizeof,                  T_sizeof,     25);
1188 }
1189
1190
1191 static
1192 statement_t *parse_case_statement(void)
1193 {
1194         eat(T_case);
1195         parse_expression();
1196         expect(':');
1197         parse_statement();
1198
1199         return NULL;
1200 }
1201
1202 static
1203 statement_t *parse_default_statement(void)
1204 {
1205         eat(T_default);
1206         expect(':');
1207         parse_statement();
1208
1209         return NULL;
1210 }
1211
1212 static
1213 statement_t *parse_label_statement(void)
1214 {
1215         eat(T_IDENTIFIER);
1216         expect(';');
1217         parse_statement();
1218
1219         return NULL;
1220 }
1221
1222 static
1223 statement_t *parse_if(void)
1224 {
1225         eat(T_if);
1226         expect('(');
1227         parse_expression();
1228         expect(')');
1229
1230         parse_statement();
1231         if(token.type == T_else) {
1232                 next_token();
1233                 parse_statement();
1234         }
1235
1236         return NULL;
1237 }
1238
1239 static
1240 statement_t *parse_switch(void)
1241 {
1242         eat(T_switch);
1243         expect('(');
1244         parse_expression();
1245         expect(')');
1246         parse_statement();
1247
1248         return NULL;
1249 }
1250
1251 static
1252 statement_t *parse_while(void)
1253 {
1254         eat(T_while);
1255         expect('(');
1256         parse_expression();
1257         expect(')');
1258         parse_statement();
1259
1260         return NULL;
1261 }
1262
1263 static
1264 statement_t *parse_do(void)
1265 {
1266         eat(T_do);
1267         parse_statement();
1268         expect(T_while);
1269         expect('(');
1270         parse_expression();
1271         expect(')');
1272
1273         return NULL;
1274 }
1275
1276 static
1277 statement_t *parse_for(void)
1278 {
1279         eat(T_for);
1280         expect('(');
1281         if(token.type != ';') {
1282                 /* TODO not correct... this could also be a declaration */
1283                 parse_expression();
1284         }
1285         expect(';');
1286         if(token.type != ';') {
1287                 parse_expression();
1288         }
1289         expect(';');
1290         if(token.type != ')') {
1291                 parse_expression();
1292         }
1293         expect(')');
1294         parse_statement();
1295
1296         return NULL;
1297 }
1298
1299 static
1300 statement_t *parse_goto(void)
1301 {
1302         eat(T_goto);
1303         expect(T_IDENTIFIER);
1304         expect(';');
1305
1306         return NULL;
1307 }
1308
1309 static
1310 statement_t *parse_continue(void)
1311 {
1312         eat(T_continue);
1313         expect(';');
1314
1315         return NULL;
1316 }
1317
1318 static
1319 statement_t *parse_break(void)
1320 {
1321         eat(T_break);
1322         expect(';');
1323
1324         return NULL;
1325 }
1326
1327 static
1328 statement_t *parse_return(void)
1329 {
1330         eat(T_return);
1331         parse_expression();
1332         expect(';');
1333
1334         return NULL;
1335 }
1336
1337 static
1338 statement_t *parse_declaration_statement(void)
1339 {
1340         parse_declaration();
1341         return NULL;
1342 }
1343
1344 static
1345 statement_t *parse_statement(void)
1346 {
1347         statement_t *statement = NULL;
1348
1349         /* declaration or statement */
1350         switch(token.type) {
1351         case T_case:
1352                 statement = parse_case_statement();
1353                 break;
1354
1355         case T_default:
1356                 statement = parse_default_statement();
1357                 break;
1358
1359         case T_IDENTIFIER:
1360                 statement = parse_label_statement();
1361                 break;
1362
1363         case '{':
1364                 statement = parse_compound_statement();
1365                 break;
1366
1367         case T_if:
1368                 statement = parse_if();
1369                 break;
1370
1371         case T_switch:
1372                 statement = parse_switch();
1373                 break;
1374
1375         case T_while:
1376                 statement = parse_while();
1377                 break;
1378
1379         case T_do:
1380                 statement = parse_do();
1381                 break;
1382
1383         case T_for:
1384                 statement = parse_for();
1385                 break;
1386
1387         case T_goto:
1388                 statement = parse_goto();
1389                 break;
1390
1391         case T_continue:
1392                 statement = parse_continue();
1393                 break;
1394
1395         case T_break:
1396                 statement = parse_break();
1397                 break;
1398
1399         case T_return:
1400                 statement = parse_return();
1401                 break;
1402
1403         case ';':
1404                 statement = NULL;
1405                 break;
1406
1407         STORAGE_CLASSES
1408         TYPE_QUALIFIERS
1409         TYPE_SPECIFIERS
1410                 statement = parse_declaration_statement();
1411                 break;
1412         }
1413
1414         return statement;
1415 }
1416
1417 static
1418 statement_t *parse_compound_statement(void)
1419 {
1420         eat('{');
1421
1422         int top = environment_top();
1423
1424         while(token.type != '}') {
1425                 parse_statement();
1426         }
1427
1428         environment_pop_to(top);
1429
1430         next_token();
1431
1432         return NULL;
1433 }
1434
1435 static
1436 translation_unit_t *parse_translation_unit(void)
1437 {
1438         translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
1439
1440         assert(translation_unit == NULL);
1441         assert(context == NULL);
1442         translation_unit = unit;
1443
1444         while(token.type != T_EOF) {
1445                 parse_declaration();
1446         }
1447
1448         translation_unit = NULL;
1449         return unit;
1450 }
1451
1452 translation_unit_t *parse(void)
1453 {
1454         obstack_init(&environment_obstack);
1455         environment_stack = NEW_ARR_F(environment_entry_t*, 0);
1456
1457         next_token();
1458         translation_unit_t *unit = parse_translation_unit();
1459
1460         DEL_ARR_F(environment_stack);
1461         obstack_free(&environment_obstack, NULL);
1462
1463         return unit;
1464 }
1465
1466 void init_parser(void)
1467 {
1468         init_expression_parsers();
1469 }
1470
1471 void exit_parser(void)
1472 {
1473 }