10 #include "type_hash.h"
12 #include "adt/bitfiddle.h"
13 #include "adt/error.h"
14 #include "adt/array.h"
17 #define MAX_LOOKAHEAD 2
19 struct environment_entry_t {
21 declaration_t *old_declaration;
22 const void *old_context;
26 static token_t lookahead_buffer[MAX_LOOKAHEAD];
27 static int lookahead_bufpos;
28 static struct obstack environment_obstack;
29 static environment_entry_t **environment_stack = NULL;
30 static translation_unit_t *translation_unit = NULL;
31 static const void *context = NULL;
34 statement_t *parse_compound_statement(void);
36 statement_t *parse_statement(void);
39 expression_t *parse_sub_expression(unsigned precedence);
41 expression_t *parse_expression(void);
44 void *allocate_ast_zero(size_t size)
46 void *res = allocate_ast(size);
52 void *allocate_type_zero(size_t size)
54 void *res = obstack_alloc(type_obst, size);
60 * returns the top element of the environment stack
63 size_t environment_top()
65 return ARR_LEN(environment_stack);
73 token = lookahead_buffer[lookahead_bufpos];
74 lookahead_buffer[lookahead_bufpos] = lexer_token;
77 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
80 print_token(stderr, &token);
81 fprintf(stderr, "\n");
86 const token_t *la(int num)
88 assert(num > 0 && num <= MAX_LOOKAHEAD);
89 int pos = (num-1) % MAX_LOOKAHEAD;
90 return & lookahead_buffer[pos];
94 void eat(token_type_t type)
96 assert(token.type == type);
100 void parser_print_error_prefix_pos(const source_position_t source_position)
102 fputs(source_position.input_name, stderr);
104 fprintf(stderr, "%d", source_position.linenr);
105 fputs(": error: ", stderr);
108 void parser_print_error_prefix(void)
110 parser_print_error_prefix_pos(token.source_position);
114 void parse_error(const char *message)
116 parser_print_error_prefix();
117 fprintf(stderr, "parse error: %s\n", message);
121 void parse_error_expected(const char *message, ...)
126 if(message != NULL) {
127 parser_print_error_prefix();
128 fprintf(stderr, "%s\n", message);
130 parser_print_error_prefix();
131 fputs("Parse error: got ", stderr);
132 print_token(stderr, &token);
133 fputs(", expected ", stderr);
135 va_start(args, message);
136 token_type_t token_type = va_arg(args, token_type_t);
137 while(token_type != 0) {
141 fprintf(stderr, ", ");
143 print_token_type(stderr, token_type);
144 token_type = va_arg(args, token_type_t);
147 fprintf(stderr, "\n");
151 void eat_until(int token_type)
153 while(token.type != token_type) {
154 if(token.type == T_EOF)
161 #define expect(expected) \
162 if(UNLIKELY(token.type != (expected))) { \
163 parse_error_expected(NULL, (expected), 0); \
169 #define expect_void(expected) \
170 if(UNLIKELY(token.type != (expected))) { \
171 parse_error_expected(NULL, (expected), 0); \
179 * pushs an environment_entry on the environment stack and links the
180 * corresponding symbol to the new entry
183 void environment_push(declaration_t *declaration, const void *context)
185 environment_entry_t *entry
186 = obstack_alloc(&environment_obstack, sizeof(entry[0]));
187 memset(entry, 0, sizeof(entry[0]));
189 int top = ARR_LEN(environment_stack);
190 ARR_RESIZE(environment_stack, top + 1);
191 environment_stack[top] = entry;
193 assert(declaration->source_position.input_name != NULL);
195 symbol_t *symbol = declaration->symbol;
196 assert(declaration != symbol->declaration);
198 if(symbol->context == context) {
199 if(symbol->declaration != NULL) {
200 assert(symbol->declaration != NULL);
201 parser_print_error_prefix_pos(declaration->source_position);
202 fprintf(stderr, "multiple definitions for symbol '%s'.\n",
204 parser_print_error_prefix_pos(symbol->declaration->source_position);
205 fprintf(stderr, "this is the location of the previous declaration.\n");
209 fprintf(stderr, "Set '%s' to %p\n", symbol->string, (void*) declaration);
211 entry->old_declaration = symbol->declaration;
212 entry->old_context = symbol->context;
213 entry->symbol = symbol;
214 symbol->declaration = declaration;
215 symbol->context = context;
219 * pops symbols from the environment stack until @p new_top is the top element
222 void environment_pop_to(size_t new_top)
224 environment_entry_t *entry = NULL;
225 size_t top = ARR_LEN(environment_stack);
231 assert(new_top < top);
234 entry = environment_stack[i - 1];
236 symbol_t *symbol = entry->symbol;
238 symbol->declaration = entry->old_declaration;
239 symbol->context = entry->old_context;
242 } while(i != new_top);
243 obstack_free(&environment_obstack, entry);
245 ARR_SHRINKLEN(environment_stack, (int) new_top);
250 static expression_t *parse_constant_expression(void)
252 /* TODO: not correct yet */
253 return parse_expression();
256 static expression_t *parse_assignment_expression(void)
258 /* TODO: not correct yet */
259 return parse_expression();
262 static compound_entry_t *parse_compound_type_entries(void);
263 static void parse_declarator(declaration_t *declaration,
264 storage_class_t storage_class, type_t *type);
265 static void maybe_push_declaration(declaration_t *declaration);
267 typedef struct declaration_specifiers_t declaration_specifiers_t;
268 struct declaration_specifiers_t {
269 storage_class_t storage_class;
273 static type_t *parse_struct_specifier(void)
277 compound_type_t *struct_type = allocate_type_zero(sizeof(struct_type[0]));
278 struct_type->type.type = TYPE_COMPOUND_STRUCT;
279 struct_type->source_position = token.source_position;
281 fprintf(stderr, "New struct %p\n", (void*) struct_type);
283 int top = environment_top();
284 const void *last_context = context;
285 context = struct_type;
287 if(token.type == T_IDENTIFIER) {
289 if(token.type == '{') {
290 parse_compound_type_entries();
292 fprintf(stderr, "Finished struct %p\n",(void*) struct_type);
293 } else if(token.type == '{') {
294 parse_compound_type_entries();
295 fprintf(stderr, "Finished struct %p\n", (void*) struct_type);
297 parse_error_expected("problem while parsing struct type specifiers",
298 T_IDENTIFIER, '{', 0);
302 assert(context == struct_type);
303 context = last_context;
304 environment_pop_to(top);
306 return (type_t*) struct_type;
309 static type_t *parse_union_specifier(void)
313 compound_type_t *union_type = allocate_type_zero(sizeof(union_type[0]));
314 union_type->type.type = TYPE_COMPOUND_UNION;
315 union_type->source_position = token.source_position;
317 int top = environment_top();
318 const void *last_context = context;
319 context = union_type;
321 if(token.type == T_IDENTIFIER) {
322 union_type->symbol = token.v.symbol;
324 if(token.type == '{') {
325 parse_compound_type_entries();
327 } else if(token.type == '{') {
328 parse_compound_type_entries();
330 parse_error_expected("problem while parsing union type specifiers",
335 assert(context == union_type);
336 context = last_context;
337 environment_pop_to(top);
339 return (type_t*) union_type;
342 static void parse_enum_type_entries()
346 if(token.type == '}') {
348 parse_error("empty enum not allowed");
353 if(token.type != T_IDENTIFIER) {
354 parse_error_expected("problem while parsing enum entry",
361 if(token.type == '=') {
362 parse_constant_expression();
365 if(token.type != ',')
368 } while(token.type != '}');
373 static type_t *parse_enum_specifier(void)
377 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
378 enum_type->type.type = TYPE_ENUM;
379 enum_type->source_position = token.source_position;
381 if(token.type == T_IDENTIFIER) {
382 enum_type->symbol = token.v.symbol;
384 if(token.type == '{') {
385 parse_enum_type_entries();
387 } else if(token.type == '{') {
388 parse_enum_type_entries();
390 parse_error_expected("problem while parsing enum type specifiers",
394 return (type_t*) enum_type;
398 SPECIFIER_SIGNED = 1 << 0,
399 SPECIFIER_UNSIGNED = 1 << 1,
400 SPECIFIER_LONG = 1 << 2,
401 SPECIFIER_INT = 1 << 3,
402 SPECIFIER_DOUBLE = 1 << 4,
403 SPECIFIER_CHAR = 1 << 5,
404 SPECIFIER_SHORT = 1 << 6,
405 SPECIFIER_LONG_LONG = 1 << 7,
406 SPECIFIER_FLOAT = 1 << 8,
407 SPECIFIER_BOOL = 1 << 9,
408 SPECIFIER_VOID = 1 << 10,
409 #ifdef PROVIDE_COMPLEX
410 SPECIFIER_COMPLEX = 1 << 11,
412 #ifdef PROVIDE_IMAGINARY
413 SPECIFIER_IMAGINARY = 1 << 12,
417 #define STORAGE_CLASSES \
424 #define TYPE_QUALIFIERS \
429 case T___extension__:
431 #ifdef PROVIDE_COMPLEX
432 #define COMPLEX_SPECIFIERS \
435 #define COMPLEX_SPECIFIERS
438 #ifdef PROVIDE_IMAGINARY
439 #define IMAGINARY_SPECIFIERS \
442 #define IMAGINARY_SPECIFIERS
445 #define TYPE_SPECIFIERS \
462 #define DECLARATION_START \
468 type_t *create_builtin_type(symbol_t *symbol)
470 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
471 type->type.type = TYPE_BUILTIN;
472 type->symbol = symbol;
474 type_t *result = typehash_insert((type_t*) type);
475 if(result != (type_t*) type) {
476 obstack_free(type_obst, type);
483 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
485 declaration_t *declaration;
487 unsigned type_qualifiers = 0;
488 unsigned type_specifiers = 0;
495 #define MATCH_STORAGE_CLASS(token, class) \
497 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
498 parse_error("multiple storage classes in declaration " \
501 specifiers->storage_class = class; \
505 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
506 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
507 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
508 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
509 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
511 /* type qualifiers */
512 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
514 type_qualifiers |= qualifier; \
518 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
519 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
520 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
521 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
523 case T___extension__:
528 /* type specifiers */
529 #define MATCH_SPECIFIER(token, specifier, name) \
532 if(type_specifiers & specifier) { \
533 parse_error("multiple " name " type specifiers given"); \
535 type_specifiers |= specifier; \
539 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
540 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
541 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
542 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
543 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
544 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
545 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
546 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
547 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
548 #ifdef PROVIDE_COMPLEX
549 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
551 #ifdef PROVIDE_IMAGINARY
552 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
556 if(type_specifiers & SPECIFIER_LONG_LONG) {
557 parse_error("multiple type specifiers given");
558 } else if(type_specifiers & SPECIFIER_LONG) {
559 type_specifiers |= SPECIFIER_LONG_LONG;
561 type_specifiers |= SPECIFIER_LONG;
565 /* TODO: if type != NULL for the following rules issue an error */
567 type = parse_struct_specifier();
570 type = parse_union_specifier();
573 type = parse_enum_specifier();
575 case T___builtin_va_list:
576 type = create_builtin_type(token.v.symbol);
581 declaration = token.v.symbol->declaration;
582 if(declaration == NULL ||
583 declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
584 goto finish_specifiers;
587 type = declaration->type;
588 assert(type != NULL);
592 /* function specifier */
594 goto finish_specifiers;
601 atomic_type_type_t atomic_type;
603 /* match valid basic types */
604 switch(type_specifiers) {
606 atomic_type = ATOMIC_TYPE_VOID;
609 atomic_type = ATOMIC_TYPE_CHAR;
611 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
612 atomic_type = ATOMIC_TYPE_SCHAR;
614 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
615 atomic_type = ATOMIC_TYPE_UCHAR;
617 case SPECIFIER_SHORT:
618 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
619 case SPECIFIER_SHORT | SPECIFIER_INT:
620 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
621 atomic_type = ATOMIC_TYPE_SHORT;
623 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
624 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
625 atomic_type = ATOMIC_TYPE_USHORT;
628 case SPECIFIER_SIGNED:
629 case SPECIFIER_SIGNED | SPECIFIER_INT:
630 atomic_type = ATOMIC_TYPE_INT;
632 case SPECIFIER_UNSIGNED:
633 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
634 atomic_type = ATOMIC_TYPE_UINT;
637 case SPECIFIER_SIGNED | SPECIFIER_LONG:
638 case SPECIFIER_LONG | SPECIFIER_INT:
639 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
640 atomic_type = ATOMIC_TYPE_LONG;
642 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
643 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
644 atomic_type = ATOMIC_TYPE_ULONG;
646 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
647 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
648 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
649 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
651 atomic_type = ATOMIC_TYPE_LONGLONG;
653 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
654 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
656 atomic_type = ATOMIC_TYPE_ULONGLONG;
658 case SPECIFIER_FLOAT:
659 atomic_type = ATOMIC_TYPE_FLOAT;
661 case SPECIFIER_DOUBLE:
662 atomic_type = ATOMIC_TYPE_DOUBLE;
664 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
665 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
668 atomic_type = ATOMIC_TYPE_BOOL;
670 #ifdef PROVIDE_COMPLEX
671 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
672 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
674 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
675 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
677 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
678 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
681 #ifdef PROVIDE_IMAGINARY
682 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
683 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
685 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
686 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
688 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
689 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
693 /* invalid specifier combination, give an error message */
694 if(type_specifiers == 0) {
695 parse_error("no type specifiers given in declaration");
696 } else if((type_specifiers & SPECIFIER_SIGNED) &&
697 (type_specifiers & SPECIFIER_UNSIGNED)) {
698 parse_error("signed and unsigned specifiers gives");
699 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
700 parse_error("only integer types can be signed or unsigned");
702 parse_error("multiple datatypes in declaration");
704 atomic_type = ATOMIC_TYPE_INVALID;
707 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
708 atype->type.type = TYPE_ATOMIC;
709 atype->atype = atomic_type;
712 type = (type_t*) atype;
714 if(type_specifiers != 0) {
715 parse_error("multiple datatypes in declaration");
719 type->qualifiers = type_qualifiers;
721 type_t *result = typehash_insert(type);
722 if(newtype && result != (type_t*) type) {
723 obstack_free(type_obst, type);
726 specifiers->type = result;
728 fprintf(stderr, "Specifiers type: ");
729 print_type(stderr, result);
730 fprintf(stderr, "\n");
734 unsigned parse_type_qualifiers()
736 unsigned type_qualifiers = 0;
740 /* type qualifiers */
741 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
742 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
743 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
744 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
747 return type_qualifiers;
753 type_t *parse_pointer(type_t *type)
755 while(token.type == '*') {
759 pointer_type_t *pointer_type
760 = allocate_type_zero(sizeof(pointer_type[0]));
761 pointer_type->type.type = TYPE_POINTER;
762 pointer_type->points_to = type;
764 pointer_type->type.qualifiers = parse_type_qualifiers();
766 type_t *result = typehash_insert((type_t*) pointer_type);
767 if(result != (type_t*) pointer_type) {
768 obstack_free(type_obst, pointer_type);
778 void parse_identifier_list()
781 if(token.type != T_IDENTIFIER) {
782 parse_error_expected("problem while parsing parameter identifier "
783 "list", T_IDENTIFIER, 0);
787 if(token.type != ',')
794 void parse_parameter()
796 if(token.type == T_DOTDOTDOT) {
801 declaration_specifiers_t specifiers;
802 memset(&specifiers, 0, sizeof(specifiers));
804 parse_declaration_specifiers(&specifiers);
805 specifiers.type = parse_pointer(specifiers.type);
807 if(token.type == '(' || token.type == T_IDENTIFIER) {
808 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
809 parse_declarator(declaration, specifiers.storage_class,
811 maybe_push_declaration(declaration);
816 void parse_parameters()
818 if(token.type == T_IDENTIFIER) {
819 symbol_t *symbol = token.v.symbol;
820 declaration_t *declaration = symbol->declaration;
821 if(declaration == NULL
822 || declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
823 parse_identifier_list();
838 if(token.type != ',')
845 void parse_attributes(void)
847 while(token.type == T___attribute__) {
849 fprintf(stderr, "TODO: __attribute__ not handled yet\n");
856 parse_error("EOF while parsing attribute");
874 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
877 type = parse_pointer(type);
878 declaration->storage_class = storage_class;
879 declaration->type = type;
883 declaration->symbol = token.v.symbol;
884 declaration->source_position = token.source_position;
889 parse_declarator(declaration, storage_class, type);
893 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
902 int top = environment_top();
903 const void *last_context = context;
908 assert(context == NULL);
909 context = last_context;
910 environment_pop_to(top);
917 if(token.type == T_static) {
921 unsigned type_qualifiers = parse_type_qualifiers();
922 if(type_qualifiers != 0) {
923 if(token.type == T_static) {
928 if(token.type == '*' && la(1)->type == ']') {
930 } else if(token.type != ']') {
931 parse_assignment_expression();
937 goto declarator_finished;
946 void maybe_push_declaration(declaration_t *declaration)
948 fprintf(stderr, "Declarator '%s' type: ",
949 declaration->symbol ? declaration->symbol->string : "");
950 print_type(stderr, declaration->type);
951 fprintf(stderr, "\n");
953 symbol_t *symbol = declaration->symbol;
956 environment_push(declaration, context);
961 void parse_init_declarators(const declaration_specifiers_t *specifiers)
964 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
966 parse_declarator(declaration, specifiers->storage_class,
968 maybe_push_declaration(declaration);
969 if(token.type == '=') {
971 if(token.type == '{') {
975 parse_assignment_expression();
977 } else if(token.type == '{') {
978 parse_compound_statement();
982 if(token.type != ',')
990 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
993 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
995 compound_entry_t *entry = allocate_ast_zero(sizeof(entry[0]));
996 entry->declaration = declaration;
998 if(token.type == ':') {
1000 parse_constant_expression();
1003 parse_declarator(declaration, specifiers->storage_class,
1005 maybe_push_declaration(declaration);
1007 if(token.type == ':') {
1009 parse_constant_expression();
1014 if(token.type != ',')
1021 static compound_entry_t *parse_compound_type_entries(void)
1025 compound_entry_t *entries = NULL;
1027 while(token.type != '}' && token.type != T_EOF) {
1028 declaration_specifiers_t specifiers;
1029 memset(&specifiers, 0, sizeof(specifiers));
1030 /* TODO not correct as this allows storage class stuff... but only
1031 * specifiers and qualifiers sould be allowed here */
1032 parse_declaration_specifiers(&specifiers);
1034 parse_struct_declarators(&specifiers);
1041 void parse_declaration(void)
1043 declaration_specifiers_t specifiers;
1044 memset(&specifiers, 0, sizeof(specifiers));
1045 parse_declaration_specifiers(&specifiers);
1047 if(token.type == ';') {
1051 parse_init_declarators(&specifiers);
1054 type_t *parse_typename(void)
1056 declaration_specifiers_t specifiers;
1057 memset(&specifiers, 0, sizeof(specifiers));
1058 /* TODO not correct storage class elements are not allowed here */
1059 parse_declaration_specifiers(&specifiers);
1061 specifiers.type = parse_pointer(specifiers.type);
1063 return specifiers.type;
1069 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1070 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1071 expression_t *left);
1073 typedef struct expression_parser_function_t expression_parser_function_t;
1074 struct expression_parser_function_t {
1075 unsigned precedence;
1076 parse_expression_function parser;
1077 unsigned infix_precedence;
1078 parse_expression_infix_function infix_parser;
1081 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1084 expression_t *expected_expression_error(void)
1086 parser_print_error_prefix();
1087 fprintf(stderr, "expected expression, got token ");
1088 print_token(stderr, & token);
1089 fprintf(stderr, "\n");
1091 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1092 expression->type = EXPR_INVALID;
1099 expression_t *parse_string_const(void)
1101 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1103 cnst->expression.type = EXPR_STRING_LITERAL;
1104 cnst->value = token.v.string;
1108 return (expression_t*) cnst;
1112 expression_t *parse_int_const(void)
1114 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1116 cnst->expression.type = EXPR_CONST;
1117 cnst->value = token.v.intvalue;
1121 return (expression_t*) cnst;
1125 expression_t *parse_reference(void)
1127 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1129 ref->expression.type = EXPR_REFERENCE;
1130 ref->symbol = token.v.symbol;
1134 return (expression_t*) ref;
1138 expression_t *parse_brace_expression(void)
1142 expression_t *result = parse_expression();
1149 expression_t *parse_primary_expression(void)
1151 switch(token.type) {
1153 return parse_int_const();
1154 case T_STRING_LITERAL:
1155 return parse_string_const();
1157 return parse_reference();
1159 return parse_brace_expression();
1162 /* TODO: error message */
1167 expression_t *parse_array_expression(unsigned precedence,
1168 expression_t *array_ref)
1174 array_access_expression_t *array_access
1175 = allocate_ast_zero(sizeof(array_access[0]));
1177 array_access->expression.type = EXPR_ARRAY_ACCESS;
1178 array_access->array_ref = array_ref;
1179 array_access->index = parse_expression();
1181 if(token.type != ']') {
1182 parse_error_expected("Problem while parsing array access", ']', 0);
1187 return (expression_t*) array_access;
1191 type_t *get_expression_type(const expression_t *expression)
1199 expression_t *parse_sizeof(unsigned precedence)
1203 sizeof_expression_t *sizeof_expression
1204 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1205 sizeof_expression->expression.type = EXPR_SIZEOF;
1207 if(token.type == '(' /* && LA1 is type_specifier */) {
1209 sizeof_expression->type = parse_typename();
1212 expression_t *expression = parse_sub_expression(precedence);
1213 sizeof_expression->type = get_expression_type(expression);
1216 return (expression_t*) sizeof_expression;
1220 expression_t *parse_select_expression(unsigned precedence,
1221 expression_t *compound)
1225 assert(token.type == '.' || token.type == T_SELECT);
1228 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
1230 select->expression.type = EXPR_SELECT;
1231 select->compound = compound;
1233 if(token.type != T_IDENTIFIER) {
1234 parse_error_expected("Problem while parsing compound select",
1238 select->symbol = token.v.symbol;
1241 return (expression_t*) select;
1245 expression_t *parse_call_expression(unsigned precedence,
1246 expression_t *expression)
1249 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
1251 call->expression.type = EXPR_CALL;
1252 call->method = expression;
1254 /* parse arguments */
1257 if(token.type != ')') {
1258 call_argument_t *last_argument = NULL;
1261 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
1263 argument->expression = parse_expression();
1264 if(last_argument == NULL) {
1265 call->arguments = argument;
1267 last_argument->next = argument;
1269 last_argument = argument;
1271 if(token.type != ',')
1278 return (expression_t*) call;
1281 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
1283 expression_t *parse_##unexpression_type(unsigned precedence) \
1287 unary_expression_t *unary_expression \
1288 = allocate_ast_zero(sizeof(unary_expression[0])); \
1289 unary_expression->expression.type = EXPR_UNARY; \
1290 unary_expression->type = unexpression_type; \
1291 unary_expression->value = parse_sub_expression(precedence); \
1293 return (expression_t*) unary_expression; \
1296 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1297 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1298 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1299 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1300 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1301 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1302 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
1303 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1305 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1307 expression_t *parse_##unexpression_type(unsigned precedence, \
1308 expression_t *left) \
1310 (void) precedence; \
1313 unary_expression_t *unary_expression \
1314 = allocate_ast_zero(sizeof(unary_expression[0])); \
1315 unary_expression->expression.type = EXPR_UNARY; \
1316 unary_expression->type = unexpression_type; \
1317 unary_expression->value = left; \
1319 return (expression_t*) unary_expression; \
1322 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
1323 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1325 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
1327 expression_t *parse_##binexpression_type(unsigned precedence, \
1328 expression_t *left) \
1332 expression_t *right = parse_sub_expression(precedence); \
1334 binary_expression_t *binexpr \
1335 = allocate_ast_zero(sizeof(binexpr[0])); \
1336 binexpr->expression.type = EXPR_BINARY; \
1337 binexpr->type = binexpression_type; \
1338 binexpr->left = left; \
1339 binexpr->right = right; \
1341 return (expression_t*) binexpr; \
1344 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1345 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1346 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1347 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1348 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1349 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1350 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1351 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1352 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_NOTEQUAL)
1353 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1354 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1355 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1356 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1357 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1358 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1359 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1362 expression_t *parse_sub_expression(unsigned precedence)
1364 if(token.type < 0) {
1365 return expected_expression_error();
1368 expression_parser_function_t *parser
1369 = &expression_parsers[token.type];
1370 source_position_t source_position = token.source_position;
1373 if(parser->parser != NULL) {
1374 left = parser->parser(parser->precedence);
1376 left = parse_primary_expression();
1379 left->source_position = source_position;
1382 if(token.type < 0) {
1383 return expected_expression_error();
1386 parser = &expression_parsers[token.type];
1387 if(parser->infix_parser == NULL)
1389 if(parser->infix_precedence < precedence)
1392 left = parser->infix_parser(parser->infix_precedence, left);
1394 left->source_position = source_position;
1401 expression_t *parse_expression(void)
1403 return parse_sub_expression(1);
1408 void register_expression_parser(parse_expression_function parser,
1409 int token_type, unsigned precedence)
1411 expression_parser_function_t *entry = &expression_parsers[token_type];
1413 if(entry->parser != NULL) {
1414 fprintf(stderr, "for token ");
1415 print_token_type(stderr, token_type);
1416 fprintf(stderr, "\n");
1417 panic("trying to register multiple expression parsers for a token");
1419 entry->parser = parser;
1420 entry->precedence = precedence;
1423 void register_expression_infix_parser(parse_expression_infix_function parser,
1424 int token_type, unsigned precedence)
1426 expression_parser_function_t *entry = &expression_parsers[token_type];
1428 if(entry->infix_parser != NULL) {
1429 fprintf(stderr, "for token ");
1430 print_token_type(stderr, token_type);
1431 fprintf(stderr, "\n");
1432 panic("trying to register multiple infix expression parsers for a "
1435 entry->infix_parser = parser;
1436 entry->infix_precedence = precedence;
1440 void init_expression_parsers(void)
1442 memset(&expression_parsers, 0, sizeof(expression_parsers));
1444 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
1445 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
1446 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1448 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1449 T_GREATERGREATER, 16);
1450 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
1451 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
1452 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
1453 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
1454 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1455 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1456 T_GREATEREQUAL, 14);
1457 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
1458 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1459 T_EXCLAMATIONMARKEQUAL, 13);
1460 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
1461 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
1462 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
1463 register_expression_infix_parser(parse_BINEXPR_ASSIGN, T_EQUAL, 2);
1465 register_expression_infix_parser(parse_array_expression, '[', 30);
1466 register_expression_infix_parser(parse_call_expression, '(', 30);
1467 register_expression_infix_parser(parse_select_expression, '.', 30);
1468 register_expression_infix_parser(parse_select_expression, T_SELECT, 30);
1469 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
1471 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
1474 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
1475 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
1476 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
1477 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
1478 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
1479 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
1480 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
1481 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
1482 register_expression_parser(parse_sizeof, T_sizeof, 25);
1487 statement_t *parse_case_statement(void)
1498 statement_t *parse_default_statement(void)
1508 statement_t *parse_label_statement(void)
1518 statement_t *parse_if(void)
1526 if(token.type == T_else) {
1535 statement_t *parse_switch(void)
1547 statement_t *parse_while(void)
1559 statement_t *parse_do(void)
1572 statement_t *parse_for(void)
1576 if(token.type != ';') {
1577 /* TODO not correct... this could also be a declaration */
1581 if(token.type != ';') {
1585 if(token.type != ')') {
1595 statement_t *parse_goto(void)
1598 expect(T_IDENTIFIER);
1605 statement_t *parse_continue(void)
1614 statement_t *parse_break(void)
1623 statement_t *parse_return(void)
1633 statement_t *parse_declaration_statement(void)
1635 parse_declaration();
1640 statement_t *parse_expression_statement(void)
1647 statement_t *parse_statement(void)
1649 declaration_t *declaration;
1650 statement_t *statement = NULL;
1652 /* declaration or statement */
1653 switch(token.type) {
1655 statement = parse_case_statement();
1659 statement = parse_default_statement();
1663 statement = parse_compound_statement();
1667 statement = parse_if();
1671 statement = parse_switch();
1675 statement = parse_while();
1679 statement = parse_do();
1683 statement = parse_for();
1687 statement = parse_goto();
1691 statement = parse_continue();
1695 statement = parse_break();
1699 statement = parse_return();
1707 if(la(1)->type == ':') {
1708 statement = parse_label_statement();
1712 declaration = token.v.symbol->declaration;
1713 if(declaration != NULL &&
1714 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1715 statement = parse_declaration_statement();
1719 statement = parse_expression_statement();
1723 statement = parse_declaration_statement();
1731 statement_t *parse_compound_statement(void)
1735 compound_statement_t *compound_statement
1736 = allocate_ast_zero(sizeof(compound_statement[0]));
1737 compound_statement->statement.type = STATEMENT_COMPOUND;
1739 int top = environment_top();
1740 const void *last_context = context;
1741 context = compound_statement;
1743 while(token.type != '}') {
1747 assert(context == compound_statement);
1748 context = last_context;
1749 environment_pop_to(top);
1757 translation_unit_t *parse_translation_unit(void)
1759 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
1761 assert(translation_unit == NULL);
1762 assert(context == NULL);
1763 translation_unit = unit;
1765 while(token.type != T_EOF) {
1766 parse_declaration();
1769 translation_unit = NULL;
1773 translation_unit_t *parse(void)
1775 obstack_init(&environment_obstack);
1776 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
1778 lookahead_bufpos = 0;
1779 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
1782 translation_unit_t *unit = parse_translation_unit();
1784 DEL_ARR_F(environment_stack);
1785 obstack_free(&environment_obstack, NULL);
1790 void init_parser(void)
1792 init_expression_parsers();
1795 void exit_parser(void)