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(int token_type)
201 while(token.type != token_type) {
202 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 expression_t *parse_assignment_expression(void)
233 /* TODO: not correct yet */
234 return parse_expression();
237 static compound_entry_t *parse_compound_type_entries(void);
238 static void parse_declarator(declaration_t *declaration,
239 storage_class_t storage_class, type_t *type);
241 typedef struct declaration_specifiers_t declaration_specifiers_t;
242 struct declaration_specifiers_t {
243 storage_class_t storage_class;
247 static type_t *parse_struct_specifier(void)
251 compound_type_t *struct_type = allocate_type_zero(sizeof(struct_type[0]));
252 struct_type->type.type = TYPE_COMPOUND_STRUCT;
253 struct_type->source_position = source_position;
255 if(token.type == T_IDENTIFIER || token.type == T_TYPENAME) {
258 if(token.type == '{') {
259 parse_compound_type_entries();
261 } else if(token.type == '{') {
262 parse_compound_type_entries();
264 parse_error_expected("problem while parsing struct type specifiers",
265 T_IDENTIFIER, '{', 0);
269 return (type_t*) struct_type;
272 static type_t *parse_union_specifier(void)
276 compound_type_t *union_type = allocate_type_zero(sizeof(union_type[0]));
277 union_type->type.type = TYPE_COMPOUND_UNION;
278 union_type->source_position = source_position;
280 if(token.type == T_IDENTIFIER) {
281 union_type->symbol = token.v.symbol;
283 if(token.type == '{') {
284 parse_compound_type_entries();
286 } else if(token.type == '{') {
287 parse_compound_type_entries();
289 parse_error_expected("problem while parsing union type specifiers",
293 return (type_t*) union_type;
296 static void parse_enum_type_entries()
300 if(token.type == '}') {
302 parse_error("empty enum not allowed");
307 if(token.type != T_IDENTIFIER) {
308 parse_error_expected("problem while parsing enum entry",
315 if(token.type == '=') {
316 parse_constant_expression();
319 if(token.type != ',')
322 } while(token.type != '}');
327 static type_t *parse_enum_specifier(void)
331 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
332 enum_type->type.type = TYPE_ENUM;
333 enum_type->source_position = source_position;
335 if(token.type == T_IDENTIFIER) {
336 enum_type->symbol = token.v.symbol;
338 if(token.type == '{') {
339 parse_enum_type_entries();
341 } else if(token.type == '{') {
342 parse_enum_type_entries();
344 parse_error_expected("problem while parsing enum type specifiers",
348 return (type_t*) enum_type;
352 SPECIFIER_SIGNED = 1 << 0,
353 SPECIFIER_UNSIGNED = 1 << 1,
354 SPECIFIER_LONG = 1 << 2,
355 SPECIFIER_INT = 1 << 3,
356 SPECIFIER_DOUBLE = 1 << 4,
357 SPECIFIER_CHAR = 1 << 5,
358 SPECIFIER_SHORT = 1 << 6,
359 SPECIFIER_LONG_LONG = 1 << 7,
360 SPECIFIER_FLOAT = 1 << 8,
361 SPECIFIER_BOOL = 1 << 9,
362 SPECIFIER_VOID = 1 << 10,
363 #ifdef PROVIDE_COMPLEX
364 SPECIFIER_COMPLEX = 1 << 11,
366 #ifdef PROVIDE_IMAGINARY
367 SPECIFIER_IMAGINARY = 1 << 12,
371 #define STORAGE_CLASSES \
378 #define TYPE_QUALIFIERS \
383 case T___extension__:
385 #ifdef PROVIDE_COMPLEX
386 #define COMPLEX_SPECIFIERS \
389 #define COMPLEX_SPECIFIERS
392 #ifdef PROVIDE_IMAGINARY
393 #define IMAGINARY_SPECIFIERS \
396 #define IMAGINARY_SPECIFIERS
399 #define TYPE_SPECIFIERS \
417 #define DECLARATION_START \
423 type_t *create_builtin_type(symbol_t *symbol)
425 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
426 type->type.type = TYPE_BUILTIN;
427 type->symbol = symbol;
429 type_t *result = typehash_insert((type_t*) type);
430 if(result != (type_t*) type) {
431 obstack_free(type_obst, type);
438 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
441 unsigned type_qualifiers = 0;
442 unsigned type_specifiers = 0;
448 #define MATCH_STORAGE_CLASS(token, class) \
450 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
451 parse_error("multiple storage classes in declaration " \
454 specifiers->storage_class = class; \
458 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
459 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
460 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
461 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
462 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
464 /* type qualifiers */
465 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
467 type_qualifiers |= qualifier; \
471 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
472 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
473 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
474 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
476 case T___extension__:
481 /* type specifiers */
482 #define MATCH_SPECIFIER(token, specifier, name) \
485 if(type_specifiers & specifier) { \
486 parse_error("multiple " name " type specifiers given"); \
488 type_specifiers |= specifier; \
492 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
493 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
494 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
495 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
496 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
497 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
498 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
499 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
500 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
501 #ifdef PROVIDE_COMPLEX
502 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
504 #ifdef PROVIDE_IMAGINARY
505 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
509 if(type_specifiers & SPECIFIER_LONG_LONG) {
510 parse_error("multiple type specifiers given");
511 } else if(type_specifiers & SPECIFIER_LONG) {
512 type_specifiers |= SPECIFIER_LONG_LONG;
514 type_specifiers |= SPECIFIER_LONG;
519 type = parse_struct_specifier();
522 type = parse_union_specifier();
525 type = parse_enum_specifier();
527 case T___builtin_va_list:
528 type = create_builtin_type(token.v.symbol);
533 if(type != NULL || type_specifiers != 0) {
534 goto finish_specifiers;
537 type = token.v.symbol->thing->declaration->type;
538 assert(type != NULL);
542 /* function specifier */
544 goto finish_specifiers;
551 atomic_type_type_t atomic_type;
553 /* match valid basic types */
554 switch(type_specifiers) {
556 atomic_type = ATOMIC_TYPE_VOID;
559 atomic_type = ATOMIC_TYPE_CHAR;
561 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
562 atomic_type = ATOMIC_TYPE_SCHAR;
564 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
565 atomic_type = ATOMIC_TYPE_UCHAR;
567 case SPECIFIER_SHORT:
568 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
569 case SPECIFIER_SHORT | SPECIFIER_INT:
570 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
571 atomic_type = ATOMIC_TYPE_SHORT;
573 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
574 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
575 atomic_type = ATOMIC_TYPE_USHORT;
578 case SPECIFIER_SIGNED:
579 case SPECIFIER_SIGNED | SPECIFIER_INT:
580 atomic_type = ATOMIC_TYPE_INT;
582 case SPECIFIER_UNSIGNED:
583 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
584 atomic_type = ATOMIC_TYPE_UINT;
587 case SPECIFIER_SIGNED | SPECIFIER_LONG:
588 case SPECIFIER_LONG | SPECIFIER_INT:
589 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
590 atomic_type = ATOMIC_TYPE_LONG;
592 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
593 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
594 atomic_type = ATOMIC_TYPE_ULONG;
596 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
597 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
598 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
599 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
601 atomic_type = ATOMIC_TYPE_LONGLONG;
603 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
604 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
606 atomic_type = ATOMIC_TYPE_ULONGLONG;
608 case SPECIFIER_FLOAT:
609 atomic_type = ATOMIC_TYPE_FLOAT;
611 case SPECIFIER_DOUBLE:
612 atomic_type = ATOMIC_TYPE_DOUBLE;
614 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
615 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
618 atomic_type = ATOMIC_TYPE_BOOL;
620 #ifdef PROVIDE_COMPLEX
621 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
622 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
624 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
625 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
627 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
628 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
631 #ifdef PROVIDE_IMAGINARY
632 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
633 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
635 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
636 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
638 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
639 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
643 /* invalid specifier combination, give an error message */
644 if(type_specifiers == 0) {
645 parse_error("no type specifiers given in declaration");
646 } else if((type_specifiers & SPECIFIER_SIGNED) &&
647 (type_specifiers & SPECIFIER_UNSIGNED)) {
648 parse_error("signed and unsigned specifiers gives");
649 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
650 parse_error("only integer types can be signed or unsigned");
652 parse_error("multiple datatypes in declaration");
654 atomic_type = ATOMIC_TYPE_INVALID;
657 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
658 atype->type.type = TYPE_ATOMIC;
659 atype->atype = atomic_type;
661 type = (type_t*) atype;
663 if(type_specifiers != 0) {
664 parse_error("multiple datatypes in declaration");
668 type->qualifiers = type_qualifiers;
670 type_t *result = typehash_insert(type);
671 if(result != (type_t*) type) {
672 obstack_free(type_obst, type);
675 specifiers->type = result;
677 fprintf(stderr, "Specifiers type: ");
678 print_type(stderr, result);
679 fprintf(stderr, "\n");
683 unsigned parse_type_qualifiers()
685 unsigned type_qualifiers = 0;
689 /* type qualifiers */
690 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
691 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
692 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
693 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
696 return type_qualifiers;
702 type_t *parse_pointer(type_t *type)
704 while(token.type == '*') {
708 pointer_type_t *pointer_type
709 = allocate_type_zero(sizeof(pointer_type[0]));
710 pointer_type->type.type = TYPE_POINTER;
711 pointer_type->points_to = type;
713 pointer_type->type.qualifiers = parse_type_qualifiers();
715 type_t *result = typehash_insert((type_t*) pointer_type);
716 if(result != (type_t*) pointer_type) {
717 obstack_free(type_obst, pointer_type);
727 void parse_identifier_list()
730 if(token.type != T_IDENTIFIER) {
731 parse_error_expected("problem while parsing parameter identifier "
732 "list", T_IDENTIFIER, 0);
736 if(token.type != ',')
743 void parse_parameter()
745 if(token.type == T_DOTDOTDOT) {
750 declaration_specifiers_t specifiers;
751 memset(&specifiers, 0, sizeof(specifiers));
753 parse_declaration_specifiers(&specifiers);
754 specifiers.type = parse_pointer(specifiers.type);
756 if(token.type == '(' || token.type == T_IDENTIFIER
757 || token.type == T_TYPENAME) {
758 declaration_t declaration;
759 memset(&declaration, 0, sizeof(declaration));
760 parse_declarator(&declaration, specifiers.storage_class,
766 void parse_parameters()
768 if(token.type == T_IDENTIFIER) {
769 parse_identifier_list();
782 if(token.type != ',')
789 void parse_attributes(void)
791 while(token.type == T___attribute__) {
793 fprintf(stderr, "TODO: __attribute__ not handled yet\n");
800 parse_error("EOF while parsing attribute");
818 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
821 type = parse_pointer(type);
822 declaration->storage_class = storage_class;
823 declaration->type = type;
828 declaration->symbol = token.v.symbol;
833 parse_declarator(declaration, storage_class, type);
837 parse_error_expected("problem while parsing declarator", T_TYPENAME,
838 T_IDENTIFIER, '(', 0);
853 if(token.type == T_static) {
857 unsigned type_qualifiers = parse_type_qualifiers();
858 if(type_qualifiers != 0) {
859 if(token.type == T_static) {
864 if(token.type == '*' /* TODO: && lookahead == ']' */) {
866 } else if(token.type != ']') {
867 parse_assignment_expression();
873 goto declarator_finished;
880 fprintf(stderr, "Declarator type: ");
881 print_type(stderr, type);
882 fprintf(stderr, "\n");
884 symbol_t *symbol = declaration->symbol;
887 environment_entry_t *entry = environment_push(symbol);
888 entry->declaration = declaration;
889 entry->old_symbol_ID = symbol->ID;
891 if(storage_class == STORAGE_CLASS_TYPEDEF) {
892 symbol->ID = T_TYPENAME;
893 fprintf(stderr, "typedef '%s'\n", symbol->string);
895 symbol->ID = T_IDENTIFIER;
901 void parse_init_declarators(const declaration_specifiers_t *specifiers)
904 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
906 parse_declarator(declaration, specifiers->storage_class,
908 if(token.type == '=') {
910 if(token.type == '{') {
914 parse_assignment_expression();
916 } else if(token.type == '{') {
917 parse_compound_statement();
921 if(token.type != ',')
929 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
932 declaration_t declaration;
933 compound_entry_t *entry = allocate_ast_zero(sizeof(entry[0]));
935 if(token.type == ':') {
937 parse_constant_expression();
940 parse_declarator(&declaration, specifiers->storage_class,
943 if(token.type == ':') {
945 parse_constant_expression();
950 if(token.type != ',')
957 static compound_entry_t *parse_compound_type_entries(void)
961 compound_entry_t *entries = NULL;
963 while(token.type != '}' && token.type != T_EOF) {
964 declaration_specifiers_t specifiers;
965 memset(&specifiers, 0, sizeof(specifiers));
966 /* TODO not correct as this allows storage class stuff... but only
967 * specifiers and qualifiers sould be allowed here */
968 parse_declaration_specifiers(&specifiers);
970 parse_struct_declarators(&specifiers);
977 void parse_declaration(void)
979 declaration_specifiers_t specifiers;
980 memset(&specifiers, 0, sizeof(specifiers));
981 parse_declaration_specifiers(&specifiers);
983 if(token.type == ';') {
987 parse_init_declarators(&specifiers);
990 type_t *parse_typename(void)
992 declaration_specifiers_t specifiers;
993 memset(&specifiers, 0, sizeof(specifiers));
994 /* TODO not correct storage class elements are not allowed here */
995 parse_declaration_specifiers(&specifiers);
997 specifiers.type = parse_pointer(specifiers.type);
999 return specifiers.type;
1005 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1006 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1007 expression_t *left);
1009 typedef struct expression_parser_function_t expression_parser_function_t;
1010 struct expression_parser_function_t {
1011 unsigned precedence;
1012 parse_expression_function parser;
1013 unsigned infix_precedence;
1014 parse_expression_infix_function infix_parser;
1017 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1020 expression_t *expected_expression_error(void)
1022 parser_print_error_prefix();
1023 fprintf(stderr, "expected expression, got token ");
1024 print_token(stderr, & token);
1025 fprintf(stderr, "\n");
1027 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1028 expression->type = EXPR_INVALID;
1035 expression_t *parse_string_const(void)
1037 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1039 cnst->expression.type = EXPR_STRING_LITERAL;
1040 cnst->value = token.v.string;
1044 return (expression_t*) cnst;
1048 expression_t *parse_int_const(void)
1050 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1052 cnst->expression.type = EXPR_CONST;
1053 cnst->value = token.v.intvalue;
1057 return (expression_t*) cnst;
1061 expression_t *parse_reference(void)
1063 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1065 ref->expression.type = EXPR_REFERENCE;
1066 ref->symbol = token.v.symbol;
1070 return (expression_t*) ref;
1074 expression_t *parse_brace_expression(void)
1078 expression_t *result = parse_expression();
1085 expression_t *parse_primary_expression(void)
1087 switch(token.type) {
1089 return parse_int_const();
1090 case T_STRING_LITERAL:
1091 return parse_string_const();
1093 return parse_reference();
1095 return parse_brace_expression();
1098 /* TODO: error message */
1103 expression_t *parse_array_expression(unsigned precedence,
1104 expression_t *array_ref)
1110 array_access_expression_t *array_access
1111 = allocate_ast_zero(sizeof(array_access[0]));
1113 array_access->expression.type = EXPR_ARRAY_ACCESS;
1114 array_access->array_ref = array_ref;
1115 array_access->index = parse_expression();
1117 if(token.type != ']') {
1118 parse_error_expected("Problem while parsing array access", ']', 0);
1123 return (expression_t*) array_access;
1127 type_t *get_expression_type(const expression_t *expression)
1135 expression_t *parse_sizeof(unsigned precedence)
1139 sizeof_expression_t *sizeof_expression
1140 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1141 sizeof_expression->expression.type = EXPR_SIZEOF;
1143 if(token.type == '(' /* && LA1 is type_specifier */) {
1145 sizeof_expression->type = parse_typename();
1148 expression_t *expression = parse_sub_expression(precedence);
1149 sizeof_expression->type = get_expression_type(expression);
1152 return (expression_t*) sizeof_expression;
1156 expression_t *parse_select_expression(unsigned precedence,
1157 expression_t *compound)
1161 assert(token.type == '.' || token.type == T_SELECT);
1164 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
1166 select->expression.type = EXPR_SELECT;
1167 select->compound = compound;
1169 if(token.type != T_IDENTIFIER) {
1170 parse_error_expected("Problem while parsing compound select",
1174 select->symbol = token.v.symbol;
1177 return (expression_t*) select;
1181 expression_t *parse_call_expression(unsigned precedence,
1182 expression_t *expression)
1185 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
1187 call->expression.type = EXPR_CALL;
1188 call->method = expression;
1190 /* parse arguments */
1193 if(token.type != ')') {
1194 call_argument_t *last_argument = NULL;
1197 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
1199 argument->expression = parse_expression();
1200 if(last_argument == NULL) {
1201 call->arguments = argument;
1203 last_argument->next = argument;
1205 last_argument = argument;
1207 if(token.type != ',')
1214 return (expression_t*) call;
1217 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
1219 expression_t *parse_##unexpression_type(unsigned precedence) \
1223 unary_expression_t *unary_expression \
1224 = allocate_ast_zero(sizeof(unary_expression[0])); \
1225 unary_expression->expression.type = EXPR_UNARY; \
1226 unary_expression->type = unexpression_type; \
1227 unary_expression->value = parse_sub_expression(precedence); \
1229 return (expression_t*) unary_expression; \
1232 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1233 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1234 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1235 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1236 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1237 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1238 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
1239 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1241 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1243 expression_t *parse_##unexpression_type(unsigned precedence, \
1244 expression_t *left) \
1246 (void) precedence; \
1249 unary_expression_t *unary_expression \
1250 = allocate_ast_zero(sizeof(unary_expression[0])); \
1251 unary_expression->expression.type = EXPR_UNARY; \
1252 unary_expression->type = unexpression_type; \
1253 unary_expression->value = left; \
1255 return (expression_t*) unary_expression; \
1258 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
1259 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1261 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
1263 expression_t *parse_##binexpression_type(unsigned precedence, \
1264 expression_t *left) \
1268 expression_t *right = parse_sub_expression(precedence); \
1270 binary_expression_t *binexpr \
1271 = allocate_ast_zero(sizeof(binexpr[0])); \
1272 binexpr->expression.type = EXPR_BINARY; \
1273 binexpr->type = binexpression_type; \
1274 binexpr->left = left; \
1275 binexpr->right = right; \
1277 return (expression_t*) binexpr; \
1280 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1281 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1282 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1283 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1284 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1285 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1286 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1287 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1288 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_NOTEQUAL)
1289 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1290 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1291 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1292 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1293 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1294 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1295 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1298 expression_t *parse_sub_expression(unsigned precedence)
1300 if(token.type < 0) {
1301 return expected_expression_error();
1304 expression_parser_function_t *parser
1305 = &expression_parsers[token.type];
1306 source_position_t source_position = source_position;
1309 if(parser->parser != NULL) {
1310 left = parser->parser(parser->precedence);
1312 left = parse_primary_expression();
1315 left->source_position = source_position;
1318 if(token.type < 0) {
1319 return expected_expression_error();
1322 parser = &expression_parsers[token.type];
1323 if(parser->infix_parser == NULL)
1325 if(parser->infix_precedence < precedence)
1328 left = parser->infix_parser(parser->infix_precedence, left);
1330 left->source_position = source_position;
1337 expression_t *parse_expression(void)
1339 return parse_sub_expression(1);
1344 void register_expression_parser(parse_expression_function parser,
1345 int token_type, unsigned precedence)
1347 expression_parser_function_t *entry = &expression_parsers[token_type];
1349 if(entry->parser != NULL) {
1350 fprintf(stderr, "for token ");
1351 print_token_type(stderr, token_type);
1352 fprintf(stderr, "\n");
1353 panic("trying to register multiple expression parsers for a token");
1355 entry->parser = parser;
1356 entry->precedence = precedence;
1359 void register_expression_infix_parser(parse_expression_infix_function parser,
1360 int token_type, unsigned precedence)
1362 expression_parser_function_t *entry = &expression_parsers[token_type];
1364 if(entry->infix_parser != NULL) {
1365 fprintf(stderr, "for token ");
1366 print_token_type(stderr, token_type);
1367 fprintf(stderr, "\n");
1368 panic("trying to register multiple infix expression parsers for a "
1371 entry->infix_parser = parser;
1372 entry->infix_precedence = precedence;
1376 void init_expression_parsers(void)
1378 memset(&expression_parsers, 0, sizeof(expression_parsers));
1380 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
1381 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
1382 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1384 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1385 T_GREATERGREATER, 16);
1386 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
1387 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
1388 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
1389 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
1390 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1391 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1392 T_GREATEREQUAL, 14);
1393 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
1394 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1395 T_EXCLAMATIONMARKEQUAL, 13);
1396 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
1397 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
1398 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
1399 register_expression_infix_parser(parse_BINEXPR_ASSIGN, T_EQUAL, 2);
1401 register_expression_infix_parser(parse_array_expression, '[', 30);
1402 register_expression_infix_parser(parse_call_expression, '(', 30);
1403 register_expression_infix_parser(parse_select_expression, '.', 30);
1404 register_expression_infix_parser(parse_select_expression, T_SELECT, 30);
1405 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
1407 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
1410 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
1411 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
1412 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
1413 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
1414 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
1415 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
1416 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
1417 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
1418 register_expression_parser(parse_sizeof, T_sizeof, 25);
1423 statement_t *parse_case_statement(void)
1434 statement_t *parse_default_statement(void)
1444 statement_t *parse_label_statement(void)
1454 statement_t *parse_if(void)
1462 if(token.type == T_else) {
1471 statement_t *parse_switch(void)
1483 statement_t *parse_while(void)
1495 statement_t *parse_do(void)
1508 statement_t *parse_for(void)
1512 if(token.type != ';') {
1513 /* TODO not correct... this could also be a declaration */
1517 if(token.type != ';') {
1521 if(token.type != ')') {
1531 statement_t *parse_goto(void)
1534 expect(T_IDENTIFIER);
1541 statement_t *parse_continue(void)
1550 statement_t *parse_break(void)
1559 statement_t *parse_return(void)
1569 statement_t *parse_declaration_statement(void)
1571 parse_declaration();
1576 statement_t *parse_statement(void)
1578 statement_t *statement = NULL;
1580 /* declaration or statement */
1581 switch(token.type) {
1583 statement = parse_case_statement();
1587 statement = parse_default_statement();
1591 statement = parse_label_statement();
1595 statement = parse_compound_statement();
1599 statement = parse_if();
1603 statement = parse_switch();
1607 statement = parse_while();
1611 statement = parse_do();
1615 statement = parse_for();
1619 statement = parse_goto();
1623 statement = parse_continue();
1627 statement = parse_break();
1631 statement = parse_return();
1639 statement = parse_declaration_statement();
1647 statement_t *parse_compound_statement(void)
1651 int top = environment_top();
1653 while(token.type != '}') {
1657 environment_pop_to(top);
1665 translation_unit_t *parse_translation_unit(void)
1667 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
1669 assert(translation_unit == NULL);
1670 assert(context == NULL);
1671 translation_unit = unit;
1673 while(token.type != T_EOF) {
1674 parse_declaration();
1677 translation_unit = NULL;
1681 translation_unit_t *parse(void)
1683 obstack_init(&environment_obstack);
1684 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
1687 translation_unit_t *unit = parse_translation_unit();
1689 DEL_ARR_F(environment_stack);
1690 obstack_free(&environment_obstack, NULL);
1695 void init_parser(void)
1697 init_expression_parsers();
1700 void exit_parser(void)