10 #include "type_hash.h"
12 #include "adt/bitfiddle.h"
13 #include "adt/error.h"
14 #include "adt/array.h"
18 struct environment_entry_t {
20 environment_entry_t *old_entry;
21 declaration_t *declaration;
22 unsigned short old_symbol_ID;
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;
32 statement_t *parse_compound_statement(void);
34 statement_t *parse_statement(void);
37 expression_t *parse_sub_expression(unsigned precedence);
39 expression_t *parse_expression(void);
42 void *allocate_ast_zero(size_t size)
44 void *res = allocate_ast(size);
50 void *allocate_type_zero(size_t size)
52 void *res = obstack_alloc(type_obst, size);
58 * pushs an environment_entry on the environment stack and links the
59 * corresponding symbol to the new entry
62 environment_entry_t *environment_push(symbol_t *symbol)
64 environment_entry_t *entry
65 = obstack_alloc(&environment_obstack, sizeof(entry[0]));
66 memset(entry, 0, sizeof(entry[0]));
68 int top = ARR_LEN(environment_stack);
69 ARR_RESIZE(environment_stack, top + 1);
70 environment_stack[top] = entry;
72 entry->old_entry = symbol->thing;
73 entry->symbol = symbol;
74 symbol->thing = entry;
80 * pops symbols from the environment stack until @p new_top is the top element
83 void environment_pop_to(size_t new_top)
85 environment_entry_t *entry = NULL;
86 size_t top = ARR_LEN(environment_stack);
92 assert(new_top < top);
95 entry = environment_stack[i - 1];
97 symbol_t *symbol = entry->symbol;
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",
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;
114 assert(symbol->thing == entry);
115 symbol->thing = entry->old_entry;
118 } while(i != new_top);
119 obstack_free(&environment_obstack, entry);
121 ARR_SHRINKLEN(environment_stack, (int) new_top);
125 * returns the top element of the environment stack
128 size_t environment_top()
130 return ARR_LEN(environment_stack);
136 void next_token(void)
138 lexer_next_token(&token);
141 print_token(stderr, &token);
142 fprintf(stderr, "\n");
147 void eat(token_type_t type)
149 assert(token.type == type);
153 void parser_print_error_prefix(void)
155 fputs(source_position.input_name, stderr);
157 fprintf(stderr, "%d", source_position.linenr);
158 fputs(": error: ", stderr);
162 void parse_error(const char *message)
164 parser_print_error_prefix();
165 fprintf(stderr, "parse error: %s\n", message);
169 void parse_error_expected(const char *message, ...)
174 if(message != NULL) {
175 parser_print_error_prefix();
176 fprintf(stderr, "%s\n", message);
178 parser_print_error_prefix();
179 fputs("Parse error: got ", stderr);
180 print_token(stderr, &token);
181 fputs(", expected ", stderr);
183 va_start(args, message);
184 token_type_t token_type = va_arg(args, token_type_t);
185 while(token_type != 0) {
189 fprintf(stderr, ", ");
191 print_token_type(stderr, token_type);
192 token_type = va_arg(args, token_type_t);
195 fprintf(stderr, "\n");
199 void eat_until_semi(void)
201 while(token.type != ';') {
203 if(token.type == T_EOF)
209 #define expect(expected) \
210 if(UNLIKELY(token.type != (expected))) { \
211 parse_error_expected(NULL, (expected), 0); \
217 #define expect_void(expected) \
218 if(UNLIKELY(token.type != (expected))) { \
219 parse_error_expected(NULL, (expected), 0); \
225 static expression_t *parse_constant_expression(void)
227 /* TODO: not correct yet */
228 return parse_expression();
231 static compound_entry_t *parse_compound_type_entries(void);
233 typedef struct declaration_specifiers_t declaration_specifiers_t;
234 struct declaration_specifiers_t {
235 storage_class_t storage_class;
239 static type_t *parse_struct_specifier(void)
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;
247 if(token.type == T_IDENTIFIER) {
250 if(token.type == '{') {
251 parse_compound_type_entries();
253 } else if(token.type == '{') {
254 parse_compound_type_entries();
256 parse_error_expected("problem while parsing struct type specifiers",
261 return (type_t*) struct_type;
264 static type_t *parse_union_specifier(void)
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;
272 if(token.type == T_IDENTIFIER) {
275 if(token.type == '{') {
276 parse_compound_type_entries();
278 } else if(token.type == '{') {
279 parse_compound_type_entries();
281 parse_error_expected("problem while parsing union type specifiers",
285 return (type_t*) union_type;
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,
303 #ifdef PROVIDE_IMAGINARY
304 SPECIFIER_IMAGINARY = 1 << 12,
308 #define STORAGE_CLASSES \
315 #define TYPE_QUALIFIERS \
320 case T___extension__: \
321 case T___attribute__:
323 #ifdef PROVIDE_COMPLEX
324 #define COMPLEX_SPECIFIERS \
327 #define COMPLEX_SPECIFIERS
330 #ifdef PROVIDE_IMAGINARY
331 #define IMAGINARY_SPECIFIERS \
334 #define IMAGINARY_SPECIFIERS
337 #define TYPE_SPECIFIERS \
358 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
361 unsigned type_qualifiers = 0;
362 unsigned type_specifiers = 0;
368 #define MATCH_STORAGE_CLASS(token, class) \
370 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
371 parse_error("multiple storage classes in declaration " \
374 specifiers->storage_class = class; \
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)
384 /* type qualifiers */
385 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
387 type_qualifiers |= qualifier; \
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);
396 case T___extension__:
401 case T___attribute__:
402 fprintf(stderr, "TODO: __attribute__ not handled yet\n");
406 /* type specifiers */
407 #define MATCH_SPECIFIER(token, specifier, name) \
410 if(type_specifiers & specifier) { \
411 parse_error("multiple " name " type specifiers given"); \
413 type_specifiers |= specifier; \
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")
429 #ifdef PROVIDE_IMAGINARY
430 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
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;
439 type_specifiers |= SPECIFIER_LONG;
445 if(type_specifiers & SPECIFIER_LONG_LONG ||
446 type_specifiers & SPECIFIER_LONG) {
447 parse_error("multiple type specifiers given");
449 type_specifiers |= SPECIFIER_LONG_LONG;
455 if(type_specifiers & SPECIFIER_LONG_LONG ||
456 type_specifiers & SPECIFIER_LONG ||
457 type_specifiers & SPECIFIER_UNSIGNED) {
458 parse_error("multiple type specifiers given");
460 type_specifiers |= SPECIFIER_LONG_LONG | SPECIFIER_UNSIGNED;
465 type = parse_struct_specifier();
468 type = parse_union_specifier();
476 if(type != NULL || type_specifiers != 0) {
477 goto finish_specifiers;
480 type = token.v.symbol->thing->declaration->type;
481 assert(type != NULL);
485 /* function specifier */
487 goto finish_specifiers;
494 atomic_type_type_t atomic_type;
496 /* match valid basic types */
497 switch(type_specifiers) {
499 atomic_type = ATOMIC_TYPE_VOID;
502 atomic_type = ATOMIC_TYPE_CHAR;
504 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
505 atomic_type = ATOMIC_TYPE_SCHAR;
507 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
508 atomic_type = ATOMIC_TYPE_UCHAR;
510 case SPECIFIER_SHORT:
511 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
512 case SPECIFIER_SHORT | SPECIFIER_INT:
513 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
514 atomic_type = ATOMIC_TYPE_SHORT;
516 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
517 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
518 atomic_type = ATOMIC_TYPE_USHORT;
521 case SPECIFIER_SIGNED:
522 case SPECIFIER_SIGNED | SPECIFIER_INT:
523 atomic_type = ATOMIC_TYPE_INT;
525 case SPECIFIER_UNSIGNED:
526 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
527 atomic_type = ATOMIC_TYPE_UINT;
530 case SPECIFIER_SIGNED | SPECIFIER_LONG:
531 case SPECIFIER_LONG | SPECIFIER_INT:
532 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
533 atomic_type = ATOMIC_TYPE_LONG;
535 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
536 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
537 atomic_type = ATOMIC_TYPE_ULONG;
539 case SPECIFIER_LONG_LONG:
540 case SPECIFIER_SIGNED | SPECIFIER_LONG_LONG:
541 case SPECIFIER_LONG_LONG | SPECIFIER_INT:
542 case SPECIFIER_SIGNED | SPECIFIER_LONG_LONG | SPECIFIER_INT:
543 atomic_type = ATOMIC_TYPE_LONGLONG;
545 case SPECIFIER_UNSIGNED | SPECIFIER_LONG_LONG:
546 case SPECIFIER_UNSIGNED | SPECIFIER_LONG_LONG | SPECIFIER_INT:
547 atomic_type = ATOMIC_TYPE_ULONGLONG;
549 case SPECIFIER_FLOAT:
550 atomic_type = ATOMIC_TYPE_FLOAT;
552 case SPECIFIER_DOUBLE:
553 atomic_type = ATOMIC_TYPE_DOUBLE;
555 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
556 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
559 atomic_type = ATOMIC_TYPE_BOOL;
561 #ifdef PROVIDE_COMPLEX
562 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
563 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
565 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
566 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
568 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
569 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
572 #ifdef PROVIDE_IMAGINARY
573 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
574 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
576 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
577 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
579 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
580 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
584 /* invalid specifier combination, give an error message */
585 if(type_specifiers == 0) {
586 parse_error("no type specifiers given in declaration");
587 } else if((type_specifiers & SPECIFIER_SIGNED) &&
588 (type_specifiers & SPECIFIER_UNSIGNED)) {
589 parse_error("signed and unsigned specifiers gives");
590 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
591 parse_error("only integer types can be signed or unsigned");
593 parse_error("multiple datatypes in declaration");
595 atomic_type = ATOMIC_TYPE_INVALID;
598 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
599 atype->type.type = TYPE_ATOMIC;
600 atype->atype = atomic_type;
602 type = (type_t*) atype;
604 if(type_specifiers != 0) {
605 parse_error("multiple datatypes in declaration");
609 type->qualifiers = type_qualifiers;
611 type_t *result = typehash_insert(type);
612 if(result != (type_t*) type) {
613 obstack_free(type_obst, type);
616 specifiers->type = result;
618 fprintf(stderr, "Specifiers type: ");
619 print_type(stderr, result);
620 fprintf(stderr, "\n");
624 unsigned parse_type_qualifiers()
626 unsigned type_qualifiers = 0;
630 /* type qualifiers */
631 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
632 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
633 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
634 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
637 return type_qualifiers;
643 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
646 while(token.type == '*') {
650 pointer_type_t *pointer_type
651 = allocate_type_zero(sizeof(pointer_type[0]));
652 pointer_type->type.type = TYPE_POINTER;
653 pointer_type->points_to = type;
655 pointer_type->type.qualifiers = parse_type_qualifiers();
657 type_t *result = typehash_insert((type_t*) pointer_type);
658 if(result != (type_t*) pointer_type) {
659 obstack_free(type_obst, pointer_type);
664 declaration->storage_class = storage_class;
665 declaration->type = type;
670 declaration->symbol = token.v.symbol;
675 parse_declarator(declaration, storage_class, type);
679 parse_error("problem while parsing declarator");
682 if(token.type == '(') {
685 /* parse parameter-type-list or identifier-list */
688 } else if(token.type == '[') {
691 /* multiple type qualifiers, and static */
693 /* assignment_expression or '*' or nothing */
698 fprintf(stderr, "Declarator type: ");
699 print_type(stderr, type);
700 fprintf(stderr, "\n");
702 symbol_t *symbol = declaration->symbol;
704 environment_entry_t *entry = environment_push(symbol);
705 entry->declaration = declaration;
706 entry->old_symbol_ID = symbol->ID;
708 if(storage_class == STORAGE_CLASS_TYPEDEF) {
709 symbol->ID = T_TYPENAME;
710 fprintf(stderr, "typedef '%s'\n", symbol->string);
712 symbol->ID = T_IDENTIFIER;
717 void parse_init_declarators(const declaration_specifiers_t *specifiers)
720 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
722 parse_declarator(declaration, specifiers->storage_class,
724 if(token.type == '=') {
726 // parse_initializer TODO
727 } else if(token.type == '{') {
728 parse_compound_statement();
732 if(token.type != ',')
740 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
743 declaration_t declaration;
744 compound_entry_t *entry = allocate_ast_zero(sizeof(entry[0]));
746 if(token.type == ':') {
748 parse_constant_expression();
751 parse_declarator(&declaration, specifiers->storage_class,
754 if(token.type == ':') {
756 parse_constant_expression();
761 if(token.type != ',')
768 static compound_entry_t *parse_compound_type_entries(void)
772 compound_entry_t *entries = NULL;
774 while(token.type != '}' && token.type != T_EOF) {
775 declaration_specifiers_t specifiers;
776 memset(&specifiers, 0, sizeof(specifiers));
777 /* TODO not correct as this allows storage class stuff... but only
778 * specifiers and qualifiers sould be allowed here */
779 parse_declaration_specifiers(&specifiers);
781 parse_struct_declarators(&specifiers);
788 void parse_declaration(void)
790 declaration_specifiers_t specifiers;
791 memset(&specifiers, 0, sizeof(specifiers));
792 parse_declaration_specifiers(&specifiers);
794 if(token.type == ';') {
798 parse_init_declarators(&specifiers);
803 typedef expression_t* (*parse_expression_function) (unsigned precedence);
804 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
807 typedef struct expression_parser_function_t expression_parser_function_t;
808 struct expression_parser_function_t {
810 parse_expression_function parser;
811 unsigned infix_precedence;
812 parse_expression_infix_function infix_parser;
815 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
818 expression_t *expected_expression_error(void)
820 parser_print_error_prefix();
821 fprintf(stderr, "expected expression, got token ");
822 print_token(stderr, & token);
823 fprintf(stderr, "\n");
825 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
826 expression->type = EXPR_INVALID;
833 expression_t *parse_string_const(void)
835 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
837 cnst->expression.type = EXPR_STRING_LITERAL;
838 cnst->value = token.v.string;
842 return (expression_t*) cnst;
846 expression_t *parse_int_const(void)
848 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
850 cnst->expression.type = EXPR_CONST;
851 cnst->value = token.v.intvalue;
855 return (expression_t*) cnst;
859 expression_t *parse_reference(void)
861 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
863 ref->expression.type = EXPR_REFERENCE;
864 ref->symbol = token.v.symbol;
868 return (expression_t*) ref;
872 expression_t *parse_brace_expression(void)
876 expression_t *result = parse_expression();
883 expression_t *parse_primary_expression(void)
887 return parse_int_const();
888 case T_STRING_LITERAL:
889 return parse_string_const();
891 return parse_reference();
893 return parse_brace_expression();
896 /* TODO: error message */
901 expression_t *parse_array_expression(unsigned precedence,
902 expression_t *array_ref)
908 array_access_expression_t *array_access
909 = allocate_ast_zero(sizeof(array_access[0]));
911 array_access->expression.type = EXPR_ARRAY_ACCESS;
912 array_access->array_ref = array_ref;
913 array_access->index = parse_expression();
915 if(token.type != ']') {
916 parse_error_expected("Problem while parsing array access", ']', 0);
921 return (expression_t*) array_access;
925 expression_t *parse_sizeof(unsigned precedence)
935 expression_t *parse_select_expression(unsigned precedence,
936 expression_t *compound)
940 assert(token.type == '.' || token.type == T_SELECT);
943 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
945 select->expression.type = EXPR_SELECT;
946 select->compound = compound;
948 if(token.type != T_IDENTIFIER) {
949 parse_error_expected("Problem while parsing compound select",
953 select->symbol = token.v.symbol;
956 return (expression_t*) select;
960 expression_t *parse_call_expression(unsigned precedence,
961 expression_t *expression)
964 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
966 call->expression.type = EXPR_CALL;
967 call->method = expression;
969 /* parse arguments */
972 if(token.type != ')') {
973 call_argument_t *last_argument = NULL;
976 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
978 argument->expression = parse_expression();
979 if(last_argument == NULL) {
980 call->arguments = argument;
982 last_argument->next = argument;
984 last_argument = argument;
986 if(token.type != ',')
993 return (expression_t*) call;
996 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
998 expression_t *parse_##unexpression_type(unsigned precedence) \
1002 unary_expression_t *unary_expression \
1003 = allocate_ast_zero(sizeof(unary_expression[0])); \
1004 unary_expression->expression.type = EXPR_UNARY; \
1005 unary_expression->type = unexpression_type; \
1006 unary_expression->value = parse_sub_expression(precedence); \
1008 return (expression_t*) unary_expression; \
1011 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1012 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1013 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1014 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1015 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1016 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1017 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
1018 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1020 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1022 expression_t *parse_##unexpression_type(unsigned precedence, \
1023 expression_t *left) \
1025 (void) precedence; \
1028 unary_expression_t *unary_expression \
1029 = allocate_ast_zero(sizeof(unary_expression[0])); \
1030 unary_expression->expression.type = EXPR_UNARY; \
1031 unary_expression->type = unexpression_type; \
1032 unary_expression->value = left; \
1034 return (expression_t*) unary_expression; \
1037 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
1038 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1040 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
1042 expression_t *parse_##binexpression_type(unsigned precedence, \
1043 expression_t *left) \
1047 expression_t *right = parse_sub_expression(precedence); \
1049 binary_expression_t *binexpr \
1050 = allocate_ast_zero(sizeof(binexpr[0])); \
1051 binexpr->expression.type = EXPR_BINARY; \
1052 binexpr->type = binexpression_type; \
1053 binexpr->left = left; \
1054 binexpr->right = right; \
1056 return (expression_t*) binexpr; \
1059 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1060 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1061 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1062 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1063 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1064 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1065 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1066 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1067 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_NOTEQUAL)
1068 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1069 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1070 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1071 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1072 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1073 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1074 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1077 expression_t *parse_sub_expression(unsigned precedence)
1079 if(token.type < 0) {
1080 return expected_expression_error();
1083 expression_parser_function_t *parser
1084 = &expression_parsers[token.type];
1085 source_position_t source_position = source_position;
1088 if(parser->parser != NULL) {
1089 left = parser->parser(parser->precedence);
1091 left = parse_primary_expression();
1094 left->source_position = source_position;
1097 if(token.type < 0) {
1098 return expected_expression_error();
1101 parser = &expression_parsers[token.type];
1102 if(parser->infix_parser == NULL)
1104 if(parser->infix_precedence < precedence)
1107 left = parser->infix_parser(parser->infix_precedence, left);
1109 left->source_position = source_position;
1116 expression_t *parse_expression(void)
1118 return parse_sub_expression(1);
1123 void register_expression_parser(parse_expression_function parser,
1124 int token_type, unsigned precedence)
1126 expression_parser_function_t *entry = &expression_parsers[token_type];
1128 if(entry->parser != NULL) {
1129 fprintf(stderr, "for token ");
1130 print_token_type(stderr, token_type);
1131 fprintf(stderr, "\n");
1132 panic("trying to register multiple expression parsers for a token");
1134 entry->parser = parser;
1135 entry->precedence = precedence;
1138 void register_expression_infix_parser(parse_expression_infix_function parser,
1139 int token_type, unsigned precedence)
1141 expression_parser_function_t *entry = &expression_parsers[token_type];
1143 if(entry->infix_parser != NULL) {
1144 fprintf(stderr, "for token ");
1145 print_token_type(stderr, token_type);
1146 fprintf(stderr, "\n");
1147 panic("trying to register multiple infix expression parsers for a "
1150 entry->infix_parser = parser;
1151 entry->infix_precedence = precedence;
1155 void init_expression_parsers(void)
1157 memset(&expression_parsers, 0, sizeof(expression_parsers));
1159 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
1160 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
1161 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1163 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1164 T_GREATERGREATER, 16);
1165 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
1166 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
1167 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
1168 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
1169 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1170 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1171 T_GREATEREQUAL, 14);
1172 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
1173 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1174 T_EXCLAMATIONMARKEQUAL, 13);
1175 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
1176 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
1177 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
1178 register_expression_infix_parser(parse_BINEXPR_ASSIGN, T_EQUAL, 2);
1180 register_expression_infix_parser(parse_array_expression, '[', 30);
1181 register_expression_infix_parser(parse_call_expression, '(', 30);
1182 register_expression_infix_parser(parse_select_expression, '.', 30);
1183 register_expression_infix_parser(parse_select_expression, T_SELECT, 30);
1184 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
1186 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
1189 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
1190 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
1191 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
1192 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
1193 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
1194 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
1195 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
1196 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
1197 register_expression_parser(parse_sizeof, T_sizeof, 25);
1202 statement_t *parse_case_statement(void)
1213 statement_t *parse_default_statement(void)
1223 statement_t *parse_label_statement(void)
1233 statement_t *parse_if(void)
1241 if(token.type == T_else) {
1250 statement_t *parse_switch(void)
1262 statement_t *parse_while(void)
1274 statement_t *parse_do(void)
1287 statement_t *parse_for(void)
1291 if(token.type != ';') {
1292 /* TODO not correct... this could also be a declaration */
1296 if(token.type != ';') {
1300 if(token.type != ')') {
1310 statement_t *parse_goto(void)
1313 expect(T_IDENTIFIER);
1320 statement_t *parse_continue(void)
1329 statement_t *parse_break(void)
1338 statement_t *parse_return(void)
1348 statement_t *parse_declaration_statement(void)
1350 parse_declaration();
1355 statement_t *parse_statement(void)
1357 statement_t *statement = NULL;
1359 /* declaration or statement */
1360 switch(token.type) {
1362 statement = parse_case_statement();
1366 statement = parse_default_statement();
1370 statement = parse_label_statement();
1374 statement = parse_compound_statement();
1378 statement = parse_if();
1382 statement = parse_switch();
1386 statement = parse_while();
1390 statement = parse_do();
1394 statement = parse_for();
1398 statement = parse_goto();
1402 statement = parse_continue();
1406 statement = parse_break();
1410 statement = parse_return();
1420 statement = parse_declaration_statement();
1428 statement_t *parse_compound_statement(void)
1432 int top = environment_top();
1434 while(token.type != '}') {
1438 environment_pop_to(top);
1446 translation_unit_t *parse_translation_unit(void)
1448 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
1450 assert(translation_unit == NULL);
1451 assert(context == NULL);
1452 translation_unit = unit;
1454 while(token.type != T_EOF) {
1455 parse_declaration();
1458 translation_unit = NULL;
1462 translation_unit_t *parse(void)
1464 obstack_init(&environment_obstack);
1465 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
1468 translation_unit_t *unit = parse_translation_unit();
1470 DEL_ARR_F(environment_stack);
1471 obstack_free(&environment_obstack, NULL);
1476 void init_parser(void)
1478 init_expression_parsers();
1481 void exit_parser(void)