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 block_statement_t *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 symbol_t *symbol = declaration->symbol;
194 assert(declaration != symbol->declaration);
196 if(symbol->context == context) {
197 if(context != NULL) {
198 assert(symbol->declaration != NULL);
199 parser_print_error_prefix_pos(declaration->source_position);
200 fprintf(stderr, "multiple definitions for symbol '%s'.\n",
202 parser_print_error_prefix_pos(symbol->declaration->source_position);
203 fprintf(stderr, "this is the location of the previous declaration.\n");
207 entry->old_declaration = symbol->declaration;
208 entry->old_context = symbol->context;
209 entry->symbol = symbol;
210 symbol->declaration = declaration;
211 symbol->context = context;
215 * pops symbols from the environment stack until @p new_top is the top element
218 void environment_pop_to(size_t new_top)
220 environment_entry_t *entry = NULL;
221 size_t top = ARR_LEN(environment_stack);
227 assert(new_top < top);
230 entry = environment_stack[i - 1];
232 symbol_t *symbol = entry->symbol;
234 symbol->declaration = entry->old_declaration;
235 symbol->context = entry->old_context;
238 } while(i != new_top);
239 obstack_free(&environment_obstack, entry);
241 ARR_SHRINKLEN(environment_stack, (int) new_top);
246 static expression_t *parse_constant_expression(void)
248 /* TODO: not correct yet */
249 return parse_expression();
252 static expression_t *parse_assignment_expression(void)
254 /* TODO: not correct yet */
255 return parse_expression();
258 static compound_entry_t *parse_compound_type_entries(void);
259 static void parse_declarator(declaration_t *declaration,
260 storage_class_t storage_class, type_t *type);
262 typedef struct declaration_specifiers_t declaration_specifiers_t;
263 struct declaration_specifiers_t {
264 storage_class_t storage_class;
268 static type_t *parse_struct_specifier(void)
272 compound_type_t *struct_type = allocate_type_zero(sizeof(struct_type[0]));
273 struct_type->type.type = TYPE_COMPOUND_STRUCT;
274 struct_type->source_position = token.source_position;
276 if(token.type == T_IDENTIFIER) {
279 if(token.type == '{') {
280 parse_compound_type_entries();
282 } else if(token.type == '{') {
283 parse_compound_type_entries();
285 parse_error_expected("problem while parsing struct type specifiers",
286 T_IDENTIFIER, '{', 0);
290 return (type_t*) struct_type;
293 static type_t *parse_union_specifier(void)
297 compound_type_t *union_type = allocate_type_zero(sizeof(union_type[0]));
298 union_type->type.type = TYPE_COMPOUND_UNION;
299 union_type->source_position = token.source_position;
301 if(token.type == T_IDENTIFIER) {
302 union_type->symbol = token.v.symbol;
304 if(token.type == '{') {
305 parse_compound_type_entries();
307 } else if(token.type == '{') {
308 parse_compound_type_entries();
310 parse_error_expected("problem while parsing union type specifiers",
314 return (type_t*) union_type;
317 static void parse_enum_type_entries()
321 if(token.type == '}') {
323 parse_error("empty enum not allowed");
328 if(token.type != T_IDENTIFIER) {
329 parse_error_expected("problem while parsing enum entry",
336 if(token.type == '=') {
337 parse_constant_expression();
340 if(token.type != ',')
343 } while(token.type != '}');
348 static type_t *parse_enum_specifier(void)
352 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
353 enum_type->type.type = TYPE_ENUM;
354 enum_type->source_position = token.source_position;
356 if(token.type == T_IDENTIFIER) {
357 enum_type->symbol = token.v.symbol;
359 if(token.type == '{') {
360 parse_enum_type_entries();
362 } else if(token.type == '{') {
363 parse_enum_type_entries();
365 parse_error_expected("problem while parsing enum type specifiers",
369 return (type_t*) enum_type;
373 SPECIFIER_SIGNED = 1 << 0,
374 SPECIFIER_UNSIGNED = 1 << 1,
375 SPECIFIER_LONG = 1 << 2,
376 SPECIFIER_INT = 1 << 3,
377 SPECIFIER_DOUBLE = 1 << 4,
378 SPECIFIER_CHAR = 1 << 5,
379 SPECIFIER_SHORT = 1 << 6,
380 SPECIFIER_LONG_LONG = 1 << 7,
381 SPECIFIER_FLOAT = 1 << 8,
382 SPECIFIER_BOOL = 1 << 9,
383 SPECIFIER_VOID = 1 << 10,
384 #ifdef PROVIDE_COMPLEX
385 SPECIFIER_COMPLEX = 1 << 11,
387 #ifdef PROVIDE_IMAGINARY
388 SPECIFIER_IMAGINARY = 1 << 12,
392 #define STORAGE_CLASSES \
399 #define TYPE_QUALIFIERS \
404 case T___extension__:
406 #ifdef PROVIDE_COMPLEX
407 #define COMPLEX_SPECIFIERS \
410 #define COMPLEX_SPECIFIERS
413 #ifdef PROVIDE_IMAGINARY
414 #define IMAGINARY_SPECIFIERS \
417 #define IMAGINARY_SPECIFIERS
420 #define TYPE_SPECIFIERS \
436 /* TODO: T_IDENTIFIER && typename */
438 #define DECLARATION_START \
444 type_t *create_builtin_type(symbol_t *symbol)
446 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
447 type->type.type = TYPE_BUILTIN;
448 type->symbol = symbol;
450 type_t *result = typehash_insert((type_t*) type);
451 if(result != (type_t*) type) {
452 obstack_free(type_obst, type);
459 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
461 declaration_t *declaration;
463 unsigned type_qualifiers = 0;
464 unsigned type_specifiers = 0;
470 #define MATCH_STORAGE_CLASS(token, class) \
472 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
473 parse_error("multiple storage classes in declaration " \
476 specifiers->storage_class = class; \
480 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
481 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
482 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
483 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
484 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
486 /* type qualifiers */
487 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
489 type_qualifiers |= qualifier; \
493 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
494 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
495 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
496 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
498 case T___extension__:
503 /* type specifiers */
504 #define MATCH_SPECIFIER(token, specifier, name) \
507 if(type_specifiers & specifier) { \
508 parse_error("multiple " name " type specifiers given"); \
510 type_specifiers |= specifier; \
514 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
515 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
516 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
517 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
518 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
519 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
520 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
521 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
522 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
523 #ifdef PROVIDE_COMPLEX
524 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
526 #ifdef PROVIDE_IMAGINARY
527 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
531 if(type_specifiers & SPECIFIER_LONG_LONG) {
532 parse_error("multiple type specifiers given");
533 } else if(type_specifiers & SPECIFIER_LONG) {
534 type_specifiers |= SPECIFIER_LONG_LONG;
536 type_specifiers |= SPECIFIER_LONG;
540 /* TODO: if type != NULL for the following rules issue an error */
542 type = parse_struct_specifier();
545 type = parse_union_specifier();
548 type = parse_enum_specifier();
550 case T___builtin_va_list:
551 type = create_builtin_type(token.v.symbol);
556 declaration = token.v.symbol->declaration;
557 if(declaration == NULL ||
558 declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
559 goto finish_specifiers;
562 type = declaration->type;
563 assert(type != NULL);
567 /* function specifier */
569 goto finish_specifiers;
576 atomic_type_type_t atomic_type;
578 /* match valid basic types */
579 switch(type_specifiers) {
581 atomic_type = ATOMIC_TYPE_VOID;
584 atomic_type = ATOMIC_TYPE_CHAR;
586 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
587 atomic_type = ATOMIC_TYPE_SCHAR;
589 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
590 atomic_type = ATOMIC_TYPE_UCHAR;
592 case SPECIFIER_SHORT:
593 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
594 case SPECIFIER_SHORT | SPECIFIER_INT:
595 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
596 atomic_type = ATOMIC_TYPE_SHORT;
598 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
599 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
600 atomic_type = ATOMIC_TYPE_USHORT;
603 case SPECIFIER_SIGNED:
604 case SPECIFIER_SIGNED | SPECIFIER_INT:
605 atomic_type = ATOMIC_TYPE_INT;
607 case SPECIFIER_UNSIGNED:
608 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
609 atomic_type = ATOMIC_TYPE_UINT;
612 case SPECIFIER_SIGNED | SPECIFIER_LONG:
613 case SPECIFIER_LONG | SPECIFIER_INT:
614 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
615 atomic_type = ATOMIC_TYPE_LONG;
617 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
618 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
619 atomic_type = ATOMIC_TYPE_ULONG;
621 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
622 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
623 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
624 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
626 atomic_type = ATOMIC_TYPE_LONGLONG;
628 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
629 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
631 atomic_type = ATOMIC_TYPE_ULONGLONG;
633 case SPECIFIER_FLOAT:
634 atomic_type = ATOMIC_TYPE_FLOAT;
636 case SPECIFIER_DOUBLE:
637 atomic_type = ATOMIC_TYPE_DOUBLE;
639 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
640 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
643 atomic_type = ATOMIC_TYPE_BOOL;
645 #ifdef PROVIDE_COMPLEX
646 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
647 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
649 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
650 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
652 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
653 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
656 #ifdef PROVIDE_IMAGINARY
657 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
658 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
660 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
661 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
663 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
664 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
668 /* invalid specifier combination, give an error message */
669 if(type_specifiers == 0) {
670 parse_error("no type specifiers given in declaration");
671 } else if((type_specifiers & SPECIFIER_SIGNED) &&
672 (type_specifiers & SPECIFIER_UNSIGNED)) {
673 parse_error("signed and unsigned specifiers gives");
674 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
675 parse_error("only integer types can be signed or unsigned");
677 parse_error("multiple datatypes in declaration");
679 atomic_type = ATOMIC_TYPE_INVALID;
682 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
683 atype->type.type = TYPE_ATOMIC;
684 atype->atype = atomic_type;
686 type = (type_t*) atype;
688 if(type_specifiers != 0) {
689 parse_error("multiple datatypes in declaration");
693 type->qualifiers = type_qualifiers;
695 type_t *result = typehash_insert(type);
696 if(result != (type_t*) type) {
697 obstack_free(type_obst, type);
700 specifiers->type = result;
702 fprintf(stderr, "Specifiers type: ");
703 print_type(stderr, result);
704 fprintf(stderr, "\n");
708 unsigned parse_type_qualifiers()
710 unsigned type_qualifiers = 0;
714 /* type qualifiers */
715 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
716 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
717 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
718 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
721 return type_qualifiers;
727 type_t *parse_pointer(type_t *type)
729 while(token.type == '*') {
733 pointer_type_t *pointer_type
734 = allocate_type_zero(sizeof(pointer_type[0]));
735 pointer_type->type.type = TYPE_POINTER;
736 pointer_type->points_to = type;
738 pointer_type->type.qualifiers = parse_type_qualifiers();
740 type_t *result = typehash_insert((type_t*) pointer_type);
741 if(result != (type_t*) pointer_type) {
742 obstack_free(type_obst, pointer_type);
752 void parse_identifier_list()
755 if(token.type != T_IDENTIFIER) {
756 parse_error_expected("problem while parsing parameter identifier "
757 "list", T_IDENTIFIER, 0);
761 if(token.type != ',')
768 void parse_parameter()
770 if(token.type == T_DOTDOTDOT) {
775 declaration_specifiers_t specifiers;
776 memset(&specifiers, 0, sizeof(specifiers));
778 parse_declaration_specifiers(&specifiers);
779 specifiers.type = parse_pointer(specifiers.type);
781 if(token.type == '(' || token.type == T_IDENTIFIER) {
782 declaration_t declaration;
783 memset(&declaration, 0, sizeof(declaration));
784 parse_declarator(&declaration, specifiers.storage_class,
790 void parse_parameters()
792 if(token.type == T_IDENTIFIER) {
793 parse_identifier_list();
806 if(token.type != ',')
813 void parse_attributes(void)
815 while(token.type == T___attribute__) {
817 fprintf(stderr, "TODO: __attribute__ not handled yet\n");
824 parse_error("EOF while parsing attribute");
842 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
845 type = parse_pointer(type);
846 declaration->storage_class = storage_class;
847 declaration->type = type;
851 declaration->symbol = token.v.symbol;
856 parse_declarator(declaration, storage_class, type);
860 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
876 if(token.type == T_static) {
880 unsigned type_qualifiers = parse_type_qualifiers();
881 if(type_qualifiers != 0) {
882 if(token.type == T_static) {
887 if(token.type == '*' /* TODO: && lookahead == ']' */) {
889 } else if(token.type != ']') {
890 parse_assignment_expression();
896 goto declarator_finished;
903 fprintf(stderr, "Declarator type: ");
904 print_type(stderr, type);
905 fprintf(stderr, "\n");
907 symbol_t *symbol = declaration->symbol;
910 environment_push(declaration, context);
915 void parse_init_declarators(const declaration_specifiers_t *specifiers)
918 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
920 parse_declarator(declaration, specifiers->storage_class,
922 if(token.type == '=') {
924 if(token.type == '{') {
928 parse_assignment_expression();
930 } else if(token.type == '{') {
931 parse_compound_statement();
935 if(token.type != ',')
943 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
946 declaration_t declaration;
947 compound_entry_t *entry = allocate_ast_zero(sizeof(entry[0]));
949 if(token.type == ':') {
951 parse_constant_expression();
954 parse_declarator(&declaration, specifiers->storage_class,
957 if(token.type == ':') {
959 parse_constant_expression();
964 if(token.type != ',')
971 static compound_entry_t *parse_compound_type_entries(void)
975 compound_entry_t *entries = NULL;
977 while(token.type != '}' && token.type != T_EOF) {
978 declaration_specifiers_t specifiers;
979 memset(&specifiers, 0, sizeof(specifiers));
980 /* TODO not correct as this allows storage class stuff... but only
981 * specifiers and qualifiers sould be allowed here */
982 parse_declaration_specifiers(&specifiers);
984 parse_struct_declarators(&specifiers);
991 void parse_declaration(void)
993 declaration_specifiers_t specifiers;
994 memset(&specifiers, 0, sizeof(specifiers));
995 parse_declaration_specifiers(&specifiers);
997 if(token.type == ';') {
1001 parse_init_declarators(&specifiers);
1004 type_t *parse_typename(void)
1006 declaration_specifiers_t specifiers;
1007 memset(&specifiers, 0, sizeof(specifiers));
1008 /* TODO not correct storage class elements are not allowed here */
1009 parse_declaration_specifiers(&specifiers);
1011 specifiers.type = parse_pointer(specifiers.type);
1013 return specifiers.type;
1019 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1020 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1021 expression_t *left);
1023 typedef struct expression_parser_function_t expression_parser_function_t;
1024 struct expression_parser_function_t {
1025 unsigned precedence;
1026 parse_expression_function parser;
1027 unsigned infix_precedence;
1028 parse_expression_infix_function infix_parser;
1031 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1034 expression_t *expected_expression_error(void)
1036 parser_print_error_prefix();
1037 fprintf(stderr, "expected expression, got token ");
1038 print_token(stderr, & token);
1039 fprintf(stderr, "\n");
1041 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1042 expression->type = EXPR_INVALID;
1049 expression_t *parse_string_const(void)
1051 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1053 cnst->expression.type = EXPR_STRING_LITERAL;
1054 cnst->value = token.v.string;
1058 return (expression_t*) cnst;
1062 expression_t *parse_int_const(void)
1064 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1066 cnst->expression.type = EXPR_CONST;
1067 cnst->value = token.v.intvalue;
1071 return (expression_t*) cnst;
1075 expression_t *parse_reference(void)
1077 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1079 ref->expression.type = EXPR_REFERENCE;
1080 ref->symbol = token.v.symbol;
1084 return (expression_t*) ref;
1088 expression_t *parse_brace_expression(void)
1092 expression_t *result = parse_expression();
1099 expression_t *parse_primary_expression(void)
1101 switch(token.type) {
1103 return parse_int_const();
1104 case T_STRING_LITERAL:
1105 return parse_string_const();
1107 return parse_reference();
1109 return parse_brace_expression();
1112 /* TODO: error message */
1117 expression_t *parse_array_expression(unsigned precedence,
1118 expression_t *array_ref)
1124 array_access_expression_t *array_access
1125 = allocate_ast_zero(sizeof(array_access[0]));
1127 array_access->expression.type = EXPR_ARRAY_ACCESS;
1128 array_access->array_ref = array_ref;
1129 array_access->index = parse_expression();
1131 if(token.type != ']') {
1132 parse_error_expected("Problem while parsing array access", ']', 0);
1137 return (expression_t*) array_access;
1141 type_t *get_expression_type(const expression_t *expression)
1149 expression_t *parse_sizeof(unsigned precedence)
1153 sizeof_expression_t *sizeof_expression
1154 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1155 sizeof_expression->expression.type = EXPR_SIZEOF;
1157 if(token.type == '(' /* && LA1 is type_specifier */) {
1159 sizeof_expression->type = parse_typename();
1162 expression_t *expression = parse_sub_expression(precedence);
1163 sizeof_expression->type = get_expression_type(expression);
1166 return (expression_t*) sizeof_expression;
1170 expression_t *parse_select_expression(unsigned precedence,
1171 expression_t *compound)
1175 assert(token.type == '.' || token.type == T_SELECT);
1178 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
1180 select->expression.type = EXPR_SELECT;
1181 select->compound = compound;
1183 if(token.type != T_IDENTIFIER) {
1184 parse_error_expected("Problem while parsing compound select",
1188 select->symbol = token.v.symbol;
1191 return (expression_t*) select;
1195 expression_t *parse_call_expression(unsigned precedence,
1196 expression_t *expression)
1199 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
1201 call->expression.type = EXPR_CALL;
1202 call->method = expression;
1204 /* parse arguments */
1207 if(token.type != ')') {
1208 call_argument_t *last_argument = NULL;
1211 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
1213 argument->expression = parse_expression();
1214 if(last_argument == NULL) {
1215 call->arguments = argument;
1217 last_argument->next = argument;
1219 last_argument = argument;
1221 if(token.type != ',')
1228 return (expression_t*) call;
1231 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
1233 expression_t *parse_##unexpression_type(unsigned precedence) \
1237 unary_expression_t *unary_expression \
1238 = allocate_ast_zero(sizeof(unary_expression[0])); \
1239 unary_expression->expression.type = EXPR_UNARY; \
1240 unary_expression->type = unexpression_type; \
1241 unary_expression->value = parse_sub_expression(precedence); \
1243 return (expression_t*) unary_expression; \
1246 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1247 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1248 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1249 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1250 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1251 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1252 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
1253 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1255 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1257 expression_t *parse_##unexpression_type(unsigned precedence, \
1258 expression_t *left) \
1260 (void) precedence; \
1263 unary_expression_t *unary_expression \
1264 = allocate_ast_zero(sizeof(unary_expression[0])); \
1265 unary_expression->expression.type = EXPR_UNARY; \
1266 unary_expression->type = unexpression_type; \
1267 unary_expression->value = left; \
1269 return (expression_t*) unary_expression; \
1272 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
1273 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1275 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
1277 expression_t *parse_##binexpression_type(unsigned precedence, \
1278 expression_t *left) \
1282 expression_t *right = parse_sub_expression(precedence); \
1284 binary_expression_t *binexpr \
1285 = allocate_ast_zero(sizeof(binexpr[0])); \
1286 binexpr->expression.type = EXPR_BINARY; \
1287 binexpr->type = binexpression_type; \
1288 binexpr->left = left; \
1289 binexpr->right = right; \
1291 return (expression_t*) binexpr; \
1294 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1295 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1296 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1297 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1298 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1299 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1300 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1301 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1302 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_NOTEQUAL)
1303 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1304 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1305 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1306 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1307 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1308 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1309 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1312 expression_t *parse_sub_expression(unsigned precedence)
1314 if(token.type < 0) {
1315 return expected_expression_error();
1318 expression_parser_function_t *parser
1319 = &expression_parsers[token.type];
1320 source_position_t source_position = token.source_position;
1323 if(parser->parser != NULL) {
1324 left = parser->parser(parser->precedence);
1326 left = parse_primary_expression();
1329 left->source_position = source_position;
1332 if(token.type < 0) {
1333 return expected_expression_error();
1336 parser = &expression_parsers[token.type];
1337 if(parser->infix_parser == NULL)
1339 if(parser->infix_precedence < precedence)
1342 left = parser->infix_parser(parser->infix_precedence, left);
1344 left->source_position = source_position;
1351 expression_t *parse_expression(void)
1353 return parse_sub_expression(1);
1358 void register_expression_parser(parse_expression_function parser,
1359 int token_type, unsigned precedence)
1361 expression_parser_function_t *entry = &expression_parsers[token_type];
1363 if(entry->parser != NULL) {
1364 fprintf(stderr, "for token ");
1365 print_token_type(stderr, token_type);
1366 fprintf(stderr, "\n");
1367 panic("trying to register multiple expression parsers for a token");
1369 entry->parser = parser;
1370 entry->precedence = precedence;
1373 void register_expression_infix_parser(parse_expression_infix_function parser,
1374 int token_type, unsigned precedence)
1376 expression_parser_function_t *entry = &expression_parsers[token_type];
1378 if(entry->infix_parser != NULL) {
1379 fprintf(stderr, "for token ");
1380 print_token_type(stderr, token_type);
1381 fprintf(stderr, "\n");
1382 panic("trying to register multiple infix expression parsers for a "
1385 entry->infix_parser = parser;
1386 entry->infix_precedence = precedence;
1390 void init_expression_parsers(void)
1392 memset(&expression_parsers, 0, sizeof(expression_parsers));
1394 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
1395 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
1396 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1398 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1399 T_GREATERGREATER, 16);
1400 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
1401 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
1402 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
1403 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
1404 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1405 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1406 T_GREATEREQUAL, 14);
1407 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
1408 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1409 T_EXCLAMATIONMARKEQUAL, 13);
1410 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
1411 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
1412 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
1413 register_expression_infix_parser(parse_BINEXPR_ASSIGN, T_EQUAL, 2);
1415 register_expression_infix_parser(parse_array_expression, '[', 30);
1416 register_expression_infix_parser(parse_call_expression, '(', 30);
1417 register_expression_infix_parser(parse_select_expression, '.', 30);
1418 register_expression_infix_parser(parse_select_expression, T_SELECT, 30);
1419 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
1421 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
1424 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
1425 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
1426 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
1427 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
1428 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
1429 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
1430 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
1431 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
1432 register_expression_parser(parse_sizeof, T_sizeof, 25);
1437 statement_t *parse_case_statement(void)
1448 statement_t *parse_default_statement(void)
1458 statement_t *parse_label_statement(void)
1468 statement_t *parse_if(void)
1476 if(token.type == T_else) {
1485 statement_t *parse_switch(void)
1497 statement_t *parse_while(void)
1509 statement_t *parse_do(void)
1522 statement_t *parse_for(void)
1526 if(token.type != ';') {
1527 /* TODO not correct... this could also be a declaration */
1531 if(token.type != ';') {
1535 if(token.type != ')') {
1545 statement_t *parse_goto(void)
1548 expect(T_IDENTIFIER);
1555 statement_t *parse_continue(void)
1564 statement_t *parse_break(void)
1573 statement_t *parse_return(void)
1583 statement_t *parse_declaration_statement(void)
1585 parse_declaration();
1590 statement_t *parse_expression_statement(void)
1597 statement_t *parse_statement(void)
1599 declaration_t *declaration;
1600 statement_t *statement = NULL;
1602 /* declaration or statement */
1603 switch(token.type) {
1605 statement = parse_case_statement();
1609 statement = parse_default_statement();
1613 statement = parse_compound_statement();
1617 statement = parse_if();
1621 statement = parse_switch();
1625 statement = parse_while();
1629 statement = parse_do();
1633 statement = parse_for();
1637 statement = parse_goto();
1641 statement = parse_continue();
1645 statement = parse_break();
1649 statement = parse_return();
1657 if(la(1)->type == ':') {
1658 statement = parse_label_statement();
1662 declaration = token.v.symbol->declaration;
1663 if(declaration != NULL &&
1664 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1665 statement = parse_declaration_statement();
1669 statement = parse_expression_statement();
1673 statement = parse_declaration_statement();
1681 statement_t *parse_compound_statement(void)
1685 int top = environment_top();
1687 while(token.type != '}') {
1691 environment_pop_to(top);
1699 translation_unit_t *parse_translation_unit(void)
1701 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
1703 assert(translation_unit == NULL);
1704 assert(context == NULL);
1705 translation_unit = unit;
1707 while(token.type != T_EOF) {
1708 parse_declaration();
1711 translation_unit = NULL;
1715 translation_unit_t *parse(void)
1717 obstack_init(&environment_obstack);
1718 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
1720 lookahead_bufpos = 0;
1721 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
1724 translation_unit_t *unit = parse_translation_unit();
1726 DEL_ARR_F(environment_stack);
1727 obstack_free(&environment_obstack, NULL);
1732 void init_parser(void)
1734 init_expression_parsers();
1737 void exit_parser(void)