11 #include "type_hash.h"
13 #include "adt/bitfiddle.h"
14 #include "adt/error.h"
15 #include "adt/array.h"
17 //#define PRINT_TOKENS
18 //#define ABORT_ON_ERROR
19 #define MAX_LOOKAHEAD 2
23 declaration_t *old_declaration;
25 unsigned short namespc;
29 static token_t lookahead_buffer[MAX_LOOKAHEAD];
30 static int lookahead_bufpos;
31 static stack_entry_t *environment_stack = NULL;
32 static stack_entry_t *label_stack = NULL;
33 static context_t *global_context = NULL;
34 static context_t *context = NULL;
35 static declaration_t *last_declaration = NULL;
36 static declaration_t *current_function = NULL;
37 static struct obstack temp_obst;
38 static bool found_error;
40 static type_t *type_int = NULL;
41 static type_t *type_uint = NULL;
42 static type_t *type_long_double = NULL;
43 static type_t *type_double = NULL;
44 static type_t *type_float = NULL;
45 static type_t *type_const_char = NULL;
46 static type_t *type_string = NULL;
47 static type_t *type_void = NULL;
48 static type_t *type_size_t = NULL;
49 static type_t *type_ptrdiff_t = NULL;
51 static statement_t *parse_compound_statement(void);
52 static statement_t *parse_statement(void);
54 static expression_t *parse_sub_expression(unsigned precedence);
55 static expression_t *parse_expression(void);
56 static type_t *parse_typename(void);
58 #define STORAGE_CLASSES \
65 #define TYPE_QUALIFIERS \
71 #ifdef PROVIDE_COMPLEX
72 #define COMPLEX_SPECIFIERS \
75 #define COMPLEX_SPECIFIERS
78 #ifdef PROVIDE_IMAGINARY
79 #define IMAGINARY_SPECIFIERS \
82 #define IMAGINARY_SPECIFIERS
85 #define TYPE_SPECIFIERS \
103 #define DECLARATION_START \
108 #define TYPENAME_START \
112 static inline void *allocate_ast_zero(size_t size)
114 void *res = allocate_ast(size);
115 memset(res, 0, size);
119 static inline void *allocate_type_zero(size_t size)
121 void *res = obstack_alloc(type_obst, size);
122 memset(res, 0, size);
126 static inline void free_type(void *type)
128 obstack_free(type_obst, type);
132 * returns the top element of the environment stack
134 static inline size_t environment_top(void)
136 return ARR_LEN(environment_stack);
139 static inline size_t label_top(void)
141 return ARR_LEN(label_stack);
146 static inline void next_token(void)
148 token = lookahead_buffer[lookahead_bufpos];
149 lookahead_buffer[lookahead_bufpos] = lexer_token;
152 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
155 print_token(stderr, &token);
156 fprintf(stderr, "\n");
160 static inline const token_t *look_ahead(int num)
162 assert(num > 0 && num <= MAX_LOOKAHEAD);
163 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
164 return & lookahead_buffer[pos];
167 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
169 static void error(void)
172 #ifdef ABORT_ON_ERROR
177 static void parser_print_prefix_pos(const source_position_t source_position)
179 fputs(source_position.input_name, stderr);
181 fprintf(stderr, "%d", source_position.linenr);
185 static void parser_print_error_prefix_pos(
186 const source_position_t source_position)
188 parser_print_prefix_pos(source_position);
189 fputs("error: ", stderr);
193 static void parser_print_error_prefix(void)
195 parser_print_error_prefix_pos(token.source_position);
198 static void parse_error(const char *message)
200 parser_print_error_prefix();
201 fprintf(stderr, "parse error: %s\n", message);
204 static void parser_print_warning_prefix_pos(
205 const source_position_t source_position)
207 parser_print_prefix_pos(source_position);
208 fputs("warning: ", stderr);
211 static void parse_warning_pos(const source_position_t source_position,
212 const char *const message)
214 parser_print_prefix_pos(source_position);
215 fprintf(stderr, "warning: %s\n", message);
218 static void parse_warning(const char *message)
220 parse_warning_pos(token.source_position, message);
223 static void parse_error_expected(const char *message, ...)
228 if(message != NULL) {
229 parser_print_error_prefix();
230 fprintf(stderr, "%s\n", message);
232 parser_print_error_prefix();
233 fputs("Parse error: got ", stderr);
234 print_token(stderr, &token);
235 fputs(", expected ", stderr);
237 va_start(args, message);
238 token_type_t token_type = va_arg(args, token_type_t);
239 while(token_type != 0) {
243 fprintf(stderr, ", ");
245 print_token_type(stderr, token_type);
246 token_type = va_arg(args, token_type_t);
249 fprintf(stderr, "\n");
252 static void print_type_quoted(type_t *type)
259 static void type_error(const char *msg, const source_position_t source_position,
262 parser_print_error_prefix_pos(source_position);
263 fprintf(stderr, "%s, but found type ", msg);
264 print_type_quoted(type);
268 static void type_error_incompatible(const char *msg,
269 const source_position_t source_position, type_t *type1, type_t *type2)
271 parser_print_error_prefix_pos(source_position);
272 fprintf(stderr, "%s, incompatible types: ", msg);
273 print_type_quoted(type1);
274 fprintf(stderr, " - ");
275 print_type_quoted(type2);
276 fprintf(stderr, ")\n");
279 static void eat_block(void)
281 if(token.type == '{')
284 while(token.type != '}') {
285 if(token.type == T_EOF)
287 if(token.type == '{') {
296 static void eat_statement(void)
298 while(token.type != ';') {
299 if(token.type == T_EOF)
301 if(token.type == '}')
303 if(token.type == '{') {
312 static void eat_brace(void)
314 if(token.type == '(')
317 while(token.type != ')') {
318 if(token.type == T_EOF)
320 if(token.type == ')' || token.type == ';' || token.type == '}') {
323 if(token.type == '(') {
327 if(token.type == '{') {
336 #define expect(expected) \
337 if(UNLIKELY(token.type != (expected))) { \
338 parse_error_expected(NULL, (expected), 0); \
344 #define expect_block(expected) \
345 if(UNLIKELY(token.type != (expected))) { \
346 parse_error_expected(NULL, (expected), 0); \
352 #define expect_void(expected) \
353 if(UNLIKELY(token.type != (expected))) { \
354 parse_error_expected(NULL, (expected), 0); \
360 static void set_context(context_t *new_context)
362 context = new_context;
364 last_declaration = new_context->declarations;
365 if(last_declaration != NULL) {
366 while(last_declaration->next != NULL) {
367 last_declaration = last_declaration->next;
373 * called when we find a 2nd declarator for an identifier we already have a
376 static bool is_compatible_declaration (declaration_t *declaration,
377 declaration_t *previous)
379 /* TODO: not correct yet */
380 return declaration->type == previous->type;
383 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
385 declaration_t *declaration = symbol->declaration;
386 for( ; declaration != NULL; declaration = declaration->symbol_next) {
387 if(declaration->namespc == namespc)
394 static const char *get_namespace_prefix(namespace_t namespc)
397 case NAMESPACE_NORMAL:
399 case NAMESPACE_UNION:
401 case NAMESPACE_STRUCT:
405 case NAMESPACE_LABEL:
408 panic("invalid namespace found");
412 * pushs an environment_entry on the environment stack and links the
413 * corresponding symbol to the new entry
415 static declaration_t *stack_push(stack_entry_t **stack_ptr,
416 declaration_t *declaration,
417 context_t *parent_context)
419 symbol_t *symbol = declaration->symbol;
420 namespace_t namespc = (namespace_t)declaration->namespc;
422 /* a declaration should be only pushed once */
423 assert(declaration->parent_context == NULL);
424 declaration->parent_context = parent_context;
426 declaration_t *previous_declaration = get_declaration(symbol, namespc);
427 assert(declaration != previous_declaration);
428 if(previous_declaration != NULL
429 && previous_declaration->parent_context == context) {
430 if(!is_compatible_declaration(declaration, previous_declaration)) {
431 parser_print_error_prefix_pos(declaration->source_position);
432 fprintf(stderr, "definition of symbol %s%s with type ",
433 get_namespace_prefix(namespc), symbol->string);
434 print_type_quoted(declaration->type);
436 parser_print_error_prefix_pos(
437 previous_declaration->source_position);
438 fprintf(stderr, "is incompatible with previous declaration "
440 print_type_quoted(previous_declaration->type);
443 const storage_class_t old_storage = previous_declaration->storage_class;
444 const storage_class_t new_storage = declaration->storage_class;
445 if (current_function == NULL) {
446 if (old_storage != STORAGE_CLASS_STATIC &&
447 new_storage == STORAGE_CLASS_STATIC) {
448 parser_print_error_prefix_pos(declaration->source_position);
450 "static declaration of '%s' follows non-static declaration\n",
452 parser_print_error_prefix_pos(previous_declaration->source_position);
453 fprintf(stderr, "previous declaration of '%s' was here\n",
456 if (old_storage == STORAGE_CLASS_EXTERN) {
457 if (new_storage == STORAGE_CLASS_NONE) {
458 previous_declaration->storage_class = STORAGE_CLASS_NONE;
461 parser_print_warning_prefix_pos(declaration->source_position);
462 fprintf(stderr, "redundant declaration for '%s'\n",
464 parser_print_warning_prefix_pos(previous_declaration->source_position);
465 fprintf(stderr, "previous declaration of '%s' was here\n",
470 if (old_storage == STORAGE_CLASS_EXTERN &&
471 new_storage == STORAGE_CLASS_EXTERN) {
472 parser_print_warning_prefix_pos(declaration->source_position);
473 fprintf(stderr, "redundant extern declaration for '%s'\n",
475 parser_print_warning_prefix_pos(previous_declaration->source_position);
476 fprintf(stderr, "previous declaration of '%s' was here\n",
479 parser_print_error_prefix_pos(declaration->source_position);
480 if (old_storage == new_storage) {
481 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
483 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
485 parser_print_error_prefix_pos(previous_declaration->source_position);
486 fprintf(stderr, "previous declaration of '%s' was here\n",
491 return previous_declaration;
494 /* remember old declaration */
496 entry.symbol = symbol;
497 entry.old_declaration = symbol->declaration;
498 entry.namespc = namespc;
499 ARR_APP1(stack_entry_t, *stack_ptr, entry);
501 /* replace/add declaration into declaration list of the symbol */
502 if(symbol->declaration == NULL) {
503 symbol->declaration = declaration;
505 declaration_t *iter_last = NULL;
506 declaration_t *iter = symbol->declaration;
507 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
508 /* replace an entry? */
509 if(iter->namespc == namespc) {
510 if(iter_last == NULL) {
511 symbol->declaration = declaration;
513 iter_last->symbol_next = declaration;
515 declaration->symbol_next = iter->symbol_next;
520 assert(iter_last->symbol_next == NULL);
521 iter_last->symbol_next = declaration;
528 static declaration_t *environment_push(declaration_t *declaration)
530 assert(declaration->source_position.input_name != NULL);
531 return stack_push(&environment_stack, declaration, context);
534 static declaration_t *label_push(declaration_t *declaration)
536 return stack_push(&label_stack, declaration, ¤t_function->context);
540 * pops symbols from the environment stack until @p new_top is the top element
542 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
544 stack_entry_t *stack = *stack_ptr;
545 size_t top = ARR_LEN(stack);
548 assert(new_top <= top);
552 for(i = top; i > new_top; --i) {
553 stack_entry_t *entry = & stack[i - 1];
555 declaration_t *old_declaration = entry->old_declaration;
556 symbol_t *symbol = entry->symbol;
557 namespace_t namespc = (namespace_t)entry->namespc;
559 /* replace/remove declaration */
560 declaration_t *declaration = symbol->declaration;
561 assert(declaration != NULL);
562 if(declaration->namespc == namespc) {
563 if(old_declaration == NULL) {
564 symbol->declaration = declaration->symbol_next;
566 symbol->declaration = old_declaration;
569 declaration_t *iter_last = declaration;
570 declaration_t *iter = declaration->symbol_next;
571 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
572 /* replace an entry? */
573 if(iter->namespc == namespc) {
574 assert(iter_last != NULL);
575 iter_last->symbol_next = old_declaration;
576 old_declaration->symbol_next = iter->symbol_next;
580 assert(iter != NULL);
584 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
587 static void environment_pop_to(size_t new_top)
589 stack_pop_to(&environment_stack, new_top);
592 static void label_pop_to(size_t new_top)
594 stack_pop_to(&label_stack, new_top);
598 static int get_rank(const type_t *type)
600 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
601 * and esp. footnote 108). However we can't fold constants (yet), so we
602 * can't decide wether unsigned int is possible, while int always works.
603 * (unsigned int would be preferable when possible... for stuff like
604 * struct { enum { ... } bla : 4; } ) */
605 if(type->type == TYPE_ENUM)
606 return ATOMIC_TYPE_INT;
608 assert(type->type == TYPE_ATOMIC);
609 atomic_type_t *atomic_type = (atomic_type_t*) type;
610 atomic_type_type_t atype = atomic_type->atype;
614 static type_t *promote_integer(type_t *type)
616 if(get_rank(type) < ATOMIC_TYPE_INT)
622 static expression_t *create_cast_expression(expression_t *expression,
625 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
627 cast->expression.type = EXPR_UNARY;
628 cast->type = UNEXPR_CAST;
629 cast->value = expression;
630 cast->expression.datatype = dest_type;
632 return (expression_t*) cast;
635 static bool is_null_expression(const expression_t *const expr)
637 if (expr->type != EXPR_CONST) return false;
639 type_t *const type = skip_typeref(expr->datatype);
640 if (!is_type_integer(type)) return false;
642 const const_t *const const_expr = (const const_t*)expr;
643 return const_expr->v.int_value == 0;
646 static expression_t *create_implicit_cast(expression_t *expression,
649 type_t *source_type = expression->datatype;
651 if(source_type == NULL)
654 source_type = skip_typeref(source_type);
655 dest_type = skip_typeref(dest_type);
657 if(source_type == dest_type)
660 if(dest_type->type == TYPE_ATOMIC) {
661 if(source_type->type != TYPE_ATOMIC)
662 panic("casting of non-atomic types not implemented yet");
664 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
665 type_error_incompatible("can't cast types",
666 expression->source_position,
667 source_type, dest_type);
671 return create_cast_expression(expression, dest_type);
673 if(dest_type->type == TYPE_POINTER) {
674 pointer_type_t *pointer_type
675 = (pointer_type_t*) dest_type;
676 switch (source_type->type) {
678 if (is_null_expression(expression)) {
679 return create_cast_expression(expression, dest_type);
684 if (pointers_compatible(source_type, dest_type)) {
685 return create_cast_expression(expression, dest_type);
690 array_type_t *const array_type = (array_type_t*) source_type;
691 if (types_compatible(array_type->element_type,
692 pointer_type->points_to)) {
693 return create_cast_expression(expression, dest_type);
699 panic("casting of non-atomic types not implemented yet");
702 type_error_incompatible("can't implicitly cast types",
703 expression->source_position,
704 source_type, dest_type);
708 panic("casting of non-atomic types not implemented yet");
711 static void semantic_assign(type_t *orig_type_left, expression_t **right,
714 type_t *orig_type_right = (*right)->datatype;
716 if(orig_type_right == NULL)
719 type_t *const type_left = skip_typeref(orig_type_left);
720 type_t *const type_right = skip_typeref(orig_type_right);
722 if (type_left == type_right) {
726 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
727 (type_left->type == TYPE_POINTER && is_null_expression(*right)) ||
728 (type_left->type == TYPE_POINTER && type_right->type == TYPE_POINTER)) {
729 *right = create_implicit_cast(*right, type_left);
733 if (type_left->type == TYPE_POINTER) {
734 switch (type_right->type) {
735 case TYPE_FUNCTION: {
736 pointer_type_t *const ptr_type = (pointer_type_t*)type_left;
737 if (ptr_type->points_to == type_right) {
744 pointer_type_t *const ptr_type = (pointer_type_t*)type_left;
745 array_type_t *const arr_type = (array_type_t*)type_right;
746 if (ptr_type->points_to == arr_type->element_type) {
756 /* TODO: improve error message */
757 parser_print_error_prefix();
758 fprintf(stderr, "incompatible types in %s\n", context);
759 parser_print_error_prefix();
760 print_type_quoted(type_left);
761 fputs(" <- ", stderr);
762 print_type_quoted(type_right);
766 static expression_t *parse_constant_expression(void)
768 /* start parsing at precedence 7 (conditional expression) */
769 return parse_sub_expression(7);
772 static expression_t *parse_assignment_expression(void)
774 /* start parsing at precedence 2 (assignment expression) */
775 return parse_sub_expression(2);
778 typedef struct declaration_specifiers_t declaration_specifiers_t;
779 struct declaration_specifiers_t {
780 storage_class_t storage_class;
785 static void parse_compound_type_entries(void);
786 static declaration_t *parse_declarator(
787 const declaration_specifiers_t *specifiers, type_t *type,
788 bool may_be_abstract);
789 static declaration_t *record_declaration(declaration_t *declaration);
791 static const char *parse_string_literals(void)
793 assert(token.type == T_STRING_LITERAL);
794 const char *result = token.v.string;
798 while(token.type == T_STRING_LITERAL) {
799 result = concat_strings(result, token.v.string);
806 static void parse_attributes(void)
810 case T___attribute__:
818 parse_error("EOF while parsing attribute");
836 if(token.type != T_STRING_LITERAL) {
837 parse_error_expected("while parsing assembler attribute",
842 parse_string_literals();
847 goto attributes_finished;
856 static designator_t *parse_designation(void)
858 if(token.type != '[' && token.type != '.')
861 designator_t *result = NULL;
862 designator_t *last = NULL;
865 designator_t *designator;
868 designator = allocate_ast_zero(sizeof(designator[0]));
870 designator->array_access = parse_constant_expression();
874 designator = allocate_ast_zero(sizeof(designator[0]));
876 if(token.type != T_IDENTIFIER) {
877 parse_error_expected("while parsing designator",
881 designator->symbol = token.v.symbol;
889 assert(designator != NULL);
891 last->next = designator;
900 static initializer_t *initializer_from_expression(type_t *type,
901 expression_t *expression)
903 initializer_value_t *result = allocate_ast_zero(sizeof(result[0]));
905 /* TODO check that expression is a constant expression */
907 /* § 6.7.8.14/15 char array may be initialized by string literals */
908 if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
909 array_type_t *array_type = (array_type_t*) type;
910 type_t *element_type = array_type->element_type;
912 if(element_type->type == TYPE_ATOMIC) {
913 atomic_type_t *atomic_type = (atomic_type_t*) element_type;
914 atomic_type_type_t atype = atomic_type->atype;
916 /* TODO handle wide strings */
917 if(atype == ATOMIC_TYPE_CHAR
918 || atype == ATOMIC_TYPE_SCHAR
919 || atype == ATOMIC_TYPE_UCHAR) {
920 /* it's fine TODO: check for length of string array... */
921 goto initializer_from_expression_finished;
926 semantic_assign(type, &expression, "initializer");
928 initializer_from_expression_finished:
929 result->initializer.type = INITIALIZER_VALUE;
930 result->value = expression;
932 return (initializer_t*) result;
935 static initializer_t *parse_sub_initializer(type_t *type,
936 expression_t *expression,
937 type_t *expression_type);
939 static initializer_t *parse_sub_initializer_elem(type_t *type)
941 if(token.type == '{') {
942 return parse_sub_initializer(type, NULL, NULL);
945 expression_t *expression = parse_assignment_expression();
946 type_t *expression_type = skip_typeref(expression->datatype);
948 return parse_sub_initializer(type, expression, expression_type);
951 static bool had_initializer_brace_warning;
953 static initializer_t *parse_sub_initializer(type_t *type,
954 expression_t *expression,
955 type_t *expression_type)
957 if(is_type_scalar(type)) {
958 /* there might be extra {} hierarchies */
959 if(token.type == '{') {
961 if(!had_initializer_brace_warning) {
962 parse_warning("braces around scalar initializer");
963 had_initializer_brace_warning = true;
965 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
966 if(token.type == ',') {
968 /* TODO: warn about excessive elements */
974 if(expression == NULL) {
975 expression = parse_assignment_expression();
977 return initializer_from_expression(type, expression);
980 /* TODO: ignore qualifiers, comparing pointers is probably
982 if(expression != NULL && expression_type == type) {
983 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
984 result->type = INITIALIZER_VALUE;
987 semantic_assign(type, &expression, "initializer");
989 //result->v.value = expression;
994 bool read_paren = false;
995 if(token.type == '{') {
1000 /* descend into subtype */
1001 initializer_t *result = NULL;
1002 if(type->type == TYPE_ARRAY) {
1003 array_type_t *array_type = (array_type_t*) type;
1004 type_t *element_type = array_type->element_type;
1005 element_type = skip_typeref(element_type);
1008 = parse_sub_initializer(element_type, expression, expression_type);
1010 assert(type->type == TYPE_COMPOUND_STRUCT
1011 || type->type == TYPE_COMPOUND_UNION);
1012 compound_type_t *compound_type = (compound_type_t*) type;
1013 context_t *context = & compound_type->declaration->context;
1015 declaration_t *first = context->declarations;
1018 type_t *first_type = first->type;
1019 first_type = skip_typeref(first_type);
1022 had_initializer_brace_warning = false;
1023 if(expression == NULL) {
1024 sub = parse_sub_initializer_elem(first_type);
1026 sub = parse_sub_initializer(first_type, expression,expression_type);
1029 /* didn't match the subtypes -> try our parent type */
1031 assert(!read_paren);
1035 initializer_t **elems = NEW_ARR_F(initializer_t*, 0);
1036 ARR_APP1(initializer_t*, elems, sub);
1038 declaration_t *iter = first->next;
1039 for( ; iter != NULL; iter = iter->next) {
1040 if(iter->symbol == NULL)
1042 if(iter->namespc != NAMESPACE_NORMAL)
1045 if(token.type == '}')
1049 type_t *iter_type = iter->type;
1050 iter_type = skip_typeref(iter_type);
1052 /* read next token */
1053 initializer_t *sub = parse_sub_initializer(iter_type, NULL, NULL);
1055 /* TODO error, do nicer cleanup*/
1056 parse_error("member initializer didn't match");
1060 ARR_APP1(initializer_t*, elems, sub);
1063 int len = ARR_LEN(elems);
1064 size_t elems_size = sizeof(initializer_t*) * len;
1066 initializer_list_t *init
1067 = allocate_ast_zero(sizeof(init[0]) + elems_size);
1069 init->initializer.type = INITIALIZER_LIST;
1071 memcpy(init->initializers, elems, elems_size);
1074 result = (initializer_t*) init;
1078 if(token.type == ',')
1085 static initializer_t *parse_initializer(type_t *type)
1087 initializer_t *result;
1089 type = skip_typeref(type);
1091 if(token.type != '{') {
1092 expression_t *expression = parse_assignment_expression();
1093 return initializer_from_expression(type, expression);
1096 if(is_type_scalar(type)) {
1100 expression_t *expression = parse_assignment_expression();
1101 result = initializer_from_expression(type, expression);
1103 if(token.type == ',')
1109 result = parse_sub_initializer(type, NULL, NULL);
1117 static declaration_t *parse_compound_type_specifier(bool is_struct)
1125 symbol_t *symbol = NULL;
1126 declaration_t *declaration = NULL;
1128 if (token.type == T___attribute__) {
1133 if(token.type == T_IDENTIFIER) {
1134 symbol = token.v.symbol;
1138 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1140 declaration = get_declaration(symbol, NAMESPACE_UNION);
1142 } else if(token.type != '{') {
1144 parse_error_expected("while parsing struct type specifier",
1145 T_IDENTIFIER, '{', 0);
1147 parse_error_expected("while parsing union type specifier",
1148 T_IDENTIFIER, '{', 0);
1154 if(declaration == NULL) {
1155 declaration = allocate_type_zero(sizeof(declaration[0]));
1158 declaration->namespc = NAMESPACE_STRUCT;
1160 declaration->namespc = NAMESPACE_UNION;
1162 declaration->source_position = token.source_position;
1163 declaration->symbol = symbol;
1164 record_declaration(declaration);
1167 if(token.type == '{') {
1168 if(declaration->init.is_defined) {
1169 assert(symbol != NULL);
1170 parser_print_error_prefix();
1171 fprintf(stderr, "multiple definition of %s %s\n",
1172 is_struct ? "struct" : "union", symbol->string);
1173 declaration->context.declarations = NULL;
1175 declaration->init.is_defined = true;
1177 int top = environment_top();
1178 context_t *last_context = context;
1179 set_context(& declaration->context);
1181 parse_compound_type_entries();
1184 assert(context == & declaration->context);
1185 set_context(last_context);
1186 environment_pop_to(top);
1192 static void parse_enum_entries(void)
1196 if(token.type == '}') {
1198 parse_error("empty enum not allowed");
1203 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1205 if(token.type != T_IDENTIFIER) {
1206 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1210 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1211 entry->symbol = token.v.symbol;
1212 entry->source_position = token.source_position;
1215 if(token.type == '=') {
1217 entry->init.initializer = parse_initializer(type_int);
1220 record_declaration(entry);
1222 if(token.type != ',')
1225 } while(token.type != '}');
1230 static declaration_t *parse_enum_specifier(void)
1234 declaration_t *declaration;
1237 if(token.type == T_IDENTIFIER) {
1238 symbol = token.v.symbol;
1241 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1242 } else if(token.type != '{') {
1243 parse_error_expected("while parsing enum type specifier",
1244 T_IDENTIFIER, '{', 0);
1251 if(declaration == NULL) {
1252 declaration = allocate_type_zero(sizeof(declaration[0]));
1254 declaration->namespc = NAMESPACE_ENUM;
1255 declaration->source_position = token.source_position;
1256 declaration->symbol = symbol;
1259 if(token.type == '{') {
1260 if(declaration->init.is_defined) {
1261 parser_print_error_prefix();
1262 fprintf(stderr, "multiple definitions of enum %s\n",
1265 record_declaration(declaration);
1266 declaration->init.is_defined = 1;
1268 parse_enum_entries();
1276 * if a symbol is a typedef to another type, return true
1278 static bool is_typedef_symbol(symbol_t *symbol)
1280 const declaration_t *const declaration =
1281 get_declaration(symbol, NAMESPACE_NORMAL);
1283 declaration != NULL &&
1284 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1287 static type_t *parse_typeof(void)
1295 expression_t *expression = NULL;
1298 switch(token.type) {
1299 case T___extension__:
1300 /* this can be a prefix to a typename or an expression */
1301 /* we simply eat it now. */
1304 } while(token.type == T___extension__);
1308 if(is_typedef_symbol(token.v.symbol)) {
1309 type = parse_typename();
1311 expression = parse_expression();
1312 type = expression->datatype;
1317 type = parse_typename();
1321 expression = parse_expression();
1322 type = expression->datatype;
1328 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1329 typeof->type.type = TYPE_TYPEOF;
1330 typeof->expression = expression;
1331 typeof->typeof_type = type;
1333 return (type_t*) typeof;
1337 SPECIFIER_SIGNED = 1 << 0,
1338 SPECIFIER_UNSIGNED = 1 << 1,
1339 SPECIFIER_LONG = 1 << 2,
1340 SPECIFIER_INT = 1 << 3,
1341 SPECIFIER_DOUBLE = 1 << 4,
1342 SPECIFIER_CHAR = 1 << 5,
1343 SPECIFIER_SHORT = 1 << 6,
1344 SPECIFIER_LONG_LONG = 1 << 7,
1345 SPECIFIER_FLOAT = 1 << 8,
1346 SPECIFIER_BOOL = 1 << 9,
1347 SPECIFIER_VOID = 1 << 10,
1348 #ifdef PROVIDE_COMPLEX
1349 SPECIFIER_COMPLEX = 1 << 11,
1351 #ifdef PROVIDE_IMAGINARY
1352 SPECIFIER_IMAGINARY = 1 << 12,
1356 static type_t *create_builtin_type(symbol_t *symbol)
1358 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1359 type->type.type = TYPE_BUILTIN;
1360 type->symbol = symbol;
1362 type->real_type = type_int;
1364 return (type_t*) type;
1367 static type_t *get_typedef_type(symbol_t *symbol)
1369 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1370 if(declaration == NULL
1371 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1374 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1375 typedef_type->type.type = TYPE_TYPEDEF;
1376 typedef_type->declaration = declaration;
1378 return (type_t*) typedef_type;
1381 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1383 type_t *type = NULL;
1384 unsigned type_qualifiers = 0;
1385 unsigned type_specifiers = 0;
1389 switch(token.type) {
1392 #define MATCH_STORAGE_CLASS(token, class) \
1394 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1395 parse_error("multiple storage classes in declaration " \
1398 specifiers->storage_class = class; \
1402 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1403 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1404 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1405 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1406 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1408 /* type qualifiers */
1409 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1411 type_qualifiers |= qualifier; \
1415 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1416 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1417 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1419 case T___extension__:
1424 /* type specifiers */
1425 #define MATCH_SPECIFIER(token, specifier, name) \
1428 if(type_specifiers & specifier) { \
1429 parse_error("multiple " name " type specifiers given"); \
1431 type_specifiers |= specifier; \
1435 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1436 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1437 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1438 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1439 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1440 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1441 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1442 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1443 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1444 #ifdef PROVIDE_COMPLEX
1445 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1447 #ifdef PROVIDE_IMAGINARY
1448 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1452 specifiers->is_inline = true;
1457 if(type_specifiers & SPECIFIER_LONG_LONG) {
1458 parse_error("multiple type specifiers given");
1459 } else if(type_specifiers & SPECIFIER_LONG) {
1460 type_specifiers |= SPECIFIER_LONG_LONG;
1462 type_specifiers |= SPECIFIER_LONG;
1466 /* TODO: if type != NULL for the following rules should issue
1469 compound_type_t *compound_type
1470 = allocate_type_zero(sizeof(compound_type[0]));
1471 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1472 compound_type->declaration = parse_compound_type_specifier(true);
1474 type = (type_t*) compound_type;
1478 compound_type_t *compound_type
1479 = allocate_type_zero(sizeof(compound_type[0]));
1480 compound_type->type.type = TYPE_COMPOUND_UNION;
1481 compound_type->declaration = parse_compound_type_specifier(false);
1483 type = (type_t*) compound_type;
1487 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1488 enum_type->type.type = TYPE_ENUM;
1489 enum_type->declaration = parse_enum_specifier();
1491 type = (type_t*) enum_type;
1495 type = parse_typeof();
1497 case T___builtin_va_list:
1498 type = create_builtin_type(token.v.symbol);
1502 case T___attribute__:
1507 case T_IDENTIFIER: {
1508 type_t *typedef_type = get_typedef_type(token.v.symbol);
1510 if(typedef_type == NULL)
1511 goto finish_specifiers;
1514 type = typedef_type;
1518 /* function specifier */
1520 goto finish_specifiers;
1527 atomic_type_type_t atomic_type;
1529 /* match valid basic types */
1530 switch(type_specifiers) {
1531 case SPECIFIER_VOID:
1532 atomic_type = ATOMIC_TYPE_VOID;
1534 case SPECIFIER_CHAR:
1535 atomic_type = ATOMIC_TYPE_CHAR;
1537 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1538 atomic_type = ATOMIC_TYPE_SCHAR;
1540 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1541 atomic_type = ATOMIC_TYPE_UCHAR;
1543 case SPECIFIER_SHORT:
1544 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1545 case SPECIFIER_SHORT | SPECIFIER_INT:
1546 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1547 atomic_type = ATOMIC_TYPE_SHORT;
1549 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1550 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1551 atomic_type = ATOMIC_TYPE_USHORT;
1554 case SPECIFIER_SIGNED:
1555 case SPECIFIER_SIGNED | SPECIFIER_INT:
1556 atomic_type = ATOMIC_TYPE_INT;
1558 case SPECIFIER_UNSIGNED:
1559 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1560 atomic_type = ATOMIC_TYPE_UINT;
1562 case SPECIFIER_LONG:
1563 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1564 case SPECIFIER_LONG | SPECIFIER_INT:
1565 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1566 atomic_type = ATOMIC_TYPE_LONG;
1568 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1569 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1570 atomic_type = ATOMIC_TYPE_ULONG;
1572 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1573 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1574 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1575 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1577 atomic_type = ATOMIC_TYPE_LONGLONG;
1579 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1580 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1582 atomic_type = ATOMIC_TYPE_ULONGLONG;
1584 case SPECIFIER_FLOAT:
1585 atomic_type = ATOMIC_TYPE_FLOAT;
1587 case SPECIFIER_DOUBLE:
1588 atomic_type = ATOMIC_TYPE_DOUBLE;
1590 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1591 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1593 case SPECIFIER_BOOL:
1594 atomic_type = ATOMIC_TYPE_BOOL;
1596 #ifdef PROVIDE_COMPLEX
1597 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1598 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1600 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1601 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1603 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1604 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1607 #ifdef PROVIDE_IMAGINARY
1608 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1609 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1611 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1612 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1614 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1615 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1619 /* invalid specifier combination, give an error message */
1620 if(type_specifiers == 0) {
1622 parse_warning("no type specifiers in declaration (using int)");
1623 atomic_type = ATOMIC_TYPE_INT;
1626 parse_error("no type specifiers given in declaration");
1628 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1629 (type_specifiers & SPECIFIER_UNSIGNED)) {
1630 parse_error("signed and unsigned specifiers gives");
1631 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1632 parse_error("only integer types can be signed or unsigned");
1634 parse_error("multiple datatypes in declaration");
1636 atomic_type = ATOMIC_TYPE_INVALID;
1639 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1640 atype->type.type = TYPE_ATOMIC;
1641 atype->atype = atomic_type;
1644 type = (type_t*) atype;
1646 if(type_specifiers != 0) {
1647 parse_error("multiple datatypes in declaration");
1651 type->qualifiers = (type_qualifier_t)type_qualifiers;
1653 type_t *result = typehash_insert(type);
1654 if(newtype && result != (type_t*) type) {
1658 specifiers->type = result;
1661 static unsigned parse_type_qualifiers(void)
1663 unsigned type_qualifiers = TYPE_QUALIFIER_NONE;
1666 switch(token.type) {
1667 /* type qualifiers */
1668 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1669 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1670 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1673 return type_qualifiers;
1678 static void parse_identifier_list(void)
1681 if(token.type != T_IDENTIFIER) {
1682 parse_error_expected("while parsing parameter identifier list",
1687 if(token.type != ',')
1693 static declaration_t *parse_parameter(void)
1695 declaration_specifiers_t specifiers;
1696 memset(&specifiers, 0, sizeof(specifiers));
1698 parse_declaration_specifiers(&specifiers);
1700 declaration_t *declaration
1701 = parse_declarator(&specifiers, specifiers.type, true);
1703 /* TODO check declaration constraints for parameters */
1704 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1705 parse_error("typedef not allowed in parameter list");
1708 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1710 if (declaration->type->type == TYPE_ARRAY) {
1711 const array_type_t *const arr_type =
1712 (const array_type_t*)declaration->type;
1714 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
1720 static declaration_t *parse_parameters(function_type_t *type)
1722 if(token.type == T_IDENTIFIER) {
1723 symbol_t *symbol = token.v.symbol;
1724 if(!is_typedef_symbol(symbol)) {
1726 parse_identifier_list();
1731 if(token.type == ')') {
1732 type->unspecified_parameters = 1;
1735 if(token.type == T_void && look_ahead(1)->type == ')') {
1740 declaration_t *declarations = NULL;
1741 declaration_t *declaration;
1742 declaration_t *last_declaration = NULL;
1743 function_parameter_t *parameter;
1744 function_parameter_t *last_parameter = NULL;
1747 switch(token.type) {
1751 return declarations;
1754 case T___extension__:
1756 declaration = parse_parameter();
1758 parameter = allocate_type_zero(sizeof(parameter[0]));
1759 parameter->type = declaration->type;
1761 if(last_parameter != NULL) {
1762 last_declaration->next = declaration;
1763 last_parameter->next = parameter;
1765 type->parameters = parameter;
1766 declarations = declaration;
1768 last_parameter = parameter;
1769 last_declaration = declaration;
1773 return declarations;
1775 if(token.type != ',')
1776 return declarations;
1786 } construct_type_type_t;
1788 typedef struct construct_type_t construct_type_t;
1789 struct construct_type_t {
1790 construct_type_type_t type;
1791 construct_type_t *next;
1794 typedef struct parsed_pointer_t parsed_pointer_t;
1795 struct parsed_pointer_t {
1796 construct_type_t construct_type;
1797 type_qualifier_t type_qualifiers;
1800 typedef struct construct_function_type_t construct_function_type_t;
1801 struct construct_function_type_t {
1802 construct_type_t construct_type;
1803 function_type_t *function_type;
1806 typedef struct parsed_array_t parsed_array_t;
1807 struct parsed_array_t {
1808 construct_type_t construct_type;
1809 type_qualifier_t type_qualifiers;
1815 typedef struct construct_base_type_t construct_base_type_t;
1816 struct construct_base_type_t {
1817 construct_type_t construct_type;
1821 static construct_type_t *parse_pointer_declarator(void)
1825 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1826 memset(pointer, 0, sizeof(pointer[0]));
1827 pointer->construct_type.type = CONSTRUCT_POINTER;
1828 pointer->type_qualifiers = parse_type_qualifiers();
1830 return (construct_type_t*) pointer;
1833 static construct_type_t *parse_array_declarator(void)
1837 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1838 memset(array, 0, sizeof(array[0]));
1839 array->construct_type.type = CONSTRUCT_ARRAY;
1841 if(token.type == T_static) {
1842 array->is_static = true;
1846 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1847 if(type_qualifiers != 0) {
1848 if(token.type == T_static) {
1849 array->is_static = true;
1853 array->type_qualifiers = type_qualifiers;
1855 if(token.type == '*' && look_ahead(1)->type == ']') {
1856 array->is_variable = true;
1858 } else if(token.type != ']') {
1859 array->size = parse_assignment_expression();
1864 return (construct_type_t*) array;
1867 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1871 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1872 type->type.type = TYPE_FUNCTION;
1874 declaration_t *parameters = parse_parameters(type);
1875 if(declaration != NULL) {
1876 declaration->context.declarations = parameters;
1879 construct_function_type_t *construct_function_type =
1880 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1881 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1882 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1883 construct_function_type->function_type = type;
1887 return (construct_type_t*) construct_function_type;
1890 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1891 int may_be_abstract)
1893 construct_type_t *result = NULL;
1894 construct_type_t *last = NULL;
1896 while(token.type == '*') {
1897 construct_type_t *type = parse_pointer_declarator();
1906 /* TODO: find out if this is correct */
1909 construct_type_t *inner_types = NULL;
1911 switch(token.type) {
1913 if(declaration == NULL) {
1914 parse_error("no identifier expected in typename");
1916 declaration->symbol = token.v.symbol;
1917 declaration->source_position = token.source_position;
1923 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1929 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
1930 /* avoid a loop in the outermost scope, because eat_statement doesn't
1932 if(token.type == '}' && current_function == NULL) {
1941 construct_type_t *type;
1942 switch(token.type) {
1944 type = parse_function_declarator(declaration);
1947 type = parse_array_declarator();
1950 goto declarator_finished;
1961 declarator_finished:
1964 if(inner_types != NULL) {
1966 last->next = inner_types;
1968 result = inner_types;
1976 static type_t *construct_declarator_type(construct_type_t *construct_list,
1979 construct_type_t *iter = construct_list;
1980 for( ; iter != NULL; iter = iter->next) {
1981 parsed_pointer_t *parsed_pointer;
1982 parsed_array_t *parsed_array;
1983 construct_function_type_t *construct_function_type;
1984 function_type_t *function_type;
1985 pointer_type_t *pointer_type;
1986 array_type_t *array_type;
1988 switch(iter->type) {
1989 case CONSTRUCT_INVALID:
1990 panic("invalid type construction found");
1991 case CONSTRUCT_FUNCTION:
1992 construct_function_type = (construct_function_type_t*) iter;
1993 function_type = construct_function_type->function_type;
1995 function_type->result_type = type;
1996 type = (type_t*) function_type;
1999 case CONSTRUCT_POINTER:
2000 parsed_pointer = (parsed_pointer_t*) iter;
2001 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
2003 pointer_type->type.type = TYPE_POINTER;
2004 pointer_type->points_to = type;
2005 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
2006 type = (type_t*) pointer_type;
2009 case CONSTRUCT_ARRAY:
2010 parsed_array = (parsed_array_t*) iter;
2011 array_type = allocate_type_zero(sizeof(array_type[0]));
2013 array_type->type.type = TYPE_ARRAY;
2014 array_type->element_type = type;
2015 array_type->type.qualifiers = parsed_array->type_qualifiers;
2016 array_type->is_static = parsed_array->is_static;
2017 array_type->is_variable = parsed_array->is_variable;
2018 array_type->size = parsed_array->size;
2019 type = (type_t*) array_type;
2023 type_t *hashed_type = typehash_insert((type_t*) type);
2024 if(hashed_type != type) {
2033 static declaration_t *parse_declarator(
2034 const declaration_specifiers_t *specifiers,
2035 type_t *type, bool may_be_abstract)
2037 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2038 declaration->storage_class = specifiers->storage_class;
2039 declaration->is_inline = specifiers->is_inline;
2041 construct_type_t *construct_type
2042 = parse_inner_declarator(declaration, may_be_abstract);
2043 declaration->type = construct_declarator_type(construct_type, type);
2045 if(construct_type != NULL) {
2046 obstack_free(&temp_obst, construct_type);
2052 static type_t *parse_abstract_declarator(type_t *base_type)
2054 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2056 type_t *result = construct_declarator_type(construct_type, base_type);
2057 if(construct_type != NULL) {
2058 obstack_free(&temp_obst, construct_type);
2064 static declaration_t *record_declaration(declaration_t *declaration)
2066 assert(context != NULL);
2068 symbol_t *symbol = declaration->symbol;
2069 if(symbol != NULL) {
2070 declaration_t *alias = environment_push(declaration);
2071 if(alias != declaration)
2074 declaration->parent_context = context;
2077 if(last_declaration != NULL) {
2078 last_declaration->next = declaration;
2080 context->declarations = declaration;
2082 last_declaration = declaration;
2087 static void parser_error_multiple_definition(declaration_t *previous,
2088 declaration_t *declaration)
2090 parser_print_error_prefix_pos(declaration->source_position);
2091 fprintf(stderr, "multiple definition of symbol '%s'\n",
2092 declaration->symbol->string);
2093 parser_print_error_prefix_pos(previous->source_position);
2094 fprintf(stderr, "this is the location of the previous definition.\n");
2097 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
2100 declaration_t *ndeclaration
2101 = parse_declarator(specifiers, specifiers->type, false);
2103 declaration_t *declaration = record_declaration(ndeclaration);
2105 type_t *type = declaration->type;
2106 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2107 parser_print_warning_prefix_pos(declaration->source_position);
2108 fprintf(stderr, "variable '%s' declared 'inline'\n",
2109 declaration->symbol->string);
2112 if(token.type == '=') {
2115 /* TODO: check that this is an allowed type (no function type) */
2117 if(declaration->init.initializer != NULL) {
2118 parser_error_multiple_definition(declaration, ndeclaration);
2121 ndeclaration->init.initializer = parse_initializer(declaration->type);
2122 } else if(token.type == '{') {
2123 if(declaration->type->type != TYPE_FUNCTION) {
2124 parser_print_error_prefix();
2125 fprintf(stderr, "Declarator ");
2126 print_type_ext(declaration->type, declaration->symbol, NULL);
2127 fprintf(stderr, " has a body but is not a function type.\n");
2132 if(declaration->init.statement != NULL) {
2133 parser_error_multiple_definition(declaration, ndeclaration);
2135 if(ndeclaration != declaration) {
2136 memcpy(&declaration->context, &ndeclaration->context,
2137 sizeof(declaration->context));
2140 int top = environment_top();
2141 context_t *last_context = context;
2142 set_context(&declaration->context);
2144 /* push function parameters */
2145 declaration_t *parameter = declaration->context.declarations;
2146 for( ; parameter != NULL; parameter = parameter->next) {
2147 environment_push(parameter);
2150 int label_stack_top = label_top();
2151 declaration_t *old_current_function = current_function;
2152 current_function = declaration;
2154 statement_t *statement = parse_compound_statement();
2156 assert(current_function == declaration);
2157 current_function = old_current_function;
2158 label_pop_to(label_stack_top);
2160 assert(context == &declaration->context);
2161 set_context(last_context);
2162 environment_pop_to(top);
2164 declaration->init.statement = statement;
2168 if(token.type != ',')
2175 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2178 if(token.type == ':') {
2180 parse_constant_expression();
2181 /* TODO (bitfields) */
2183 declaration_t *declaration
2184 = parse_declarator(specifiers, specifiers->type, true);
2186 /* TODO: check constraints for struct declarations */
2187 /* TODO: check for doubled fields */
2188 record_declaration(declaration);
2190 if(token.type == ':') {
2192 parse_constant_expression();
2193 /* TODO (bitfields) */
2197 if(token.type != ',')
2204 static void parse_compound_type_entries(void)
2208 while(token.type != '}' && token.type != T_EOF) {
2209 declaration_specifiers_t specifiers;
2210 memset(&specifiers, 0, sizeof(specifiers));
2211 parse_declaration_specifiers(&specifiers);
2213 parse_struct_declarators(&specifiers);
2215 if(token.type == T_EOF) {
2216 parse_error("unexpected error while parsing struct");
2221 static void parse_declaration(void)
2223 source_position_t source_position = token.source_position;
2225 declaration_specifiers_t specifiers;
2226 memset(&specifiers, 0, sizeof(specifiers));
2227 parse_declaration_specifiers(&specifiers);
2229 if(token.type == ';') {
2230 if (specifiers.storage_class != STORAGE_CLASS_NONE) {
2231 parse_warning_pos(source_position,
2232 "useless keyword in empty declaration");
2234 switch (specifiers.type->type) {
2235 case TYPE_COMPOUND_STRUCT:
2236 case TYPE_COMPOUND_UNION: {
2237 const compound_type_t *const comp_type =
2238 (const compound_type_t*)specifiers.type;
2239 if (comp_type->declaration->symbol == NULL) {
2240 parse_warning_pos(source_position,
2241 "unnamed struct/union that defines no instances");
2246 case TYPE_ENUM: break;
2249 parse_warning_pos(source_position, "empty declaration");
2255 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2257 declaration->type = specifiers.type;
2258 declaration->storage_class = specifiers.storage_class;
2259 declaration->source_position = source_position;
2260 record_declaration(declaration);
2263 parse_init_declarators(&specifiers);
2266 static type_t *parse_typename(void)
2268 declaration_specifiers_t specifiers;
2269 memset(&specifiers, 0, sizeof(specifiers));
2270 parse_declaration_specifiers(&specifiers);
2271 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2272 /* TODO: improve error message, user does probably not know what a
2273 * storage class is...
2275 parse_error("typename may not have a storage class");
2278 type_t *result = parse_abstract_declarator(specifiers.type);
2286 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2287 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2288 expression_t *left);
2290 typedef struct expression_parser_function_t expression_parser_function_t;
2291 struct expression_parser_function_t {
2292 unsigned precedence;
2293 parse_expression_function parser;
2294 unsigned infix_precedence;
2295 parse_expression_infix_function infix_parser;
2298 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2300 static expression_t *make_invalid_expression(void)
2302 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2303 expression->type = EXPR_INVALID;
2304 expression->source_position = token.source_position;
2308 static expression_t *expected_expression_error(void)
2310 parser_print_error_prefix();
2311 fprintf(stderr, "expected expression, got token ");
2312 print_token(stderr, & token);
2313 fprintf(stderr, "\n");
2317 return make_invalid_expression();
2320 static expression_t *parse_string_const(void)
2322 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2324 cnst->expression.type = EXPR_STRING_LITERAL;
2325 cnst->expression.datatype = type_string;
2326 cnst->value = parse_string_literals();
2328 return (expression_t*) cnst;
2331 static expression_t *parse_int_const(void)
2333 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2335 cnst->expression.type = EXPR_CONST;
2336 cnst->expression.datatype = type_int;
2337 cnst->v.int_value = token.v.intvalue;
2341 return (expression_t*) cnst;
2344 static expression_t *parse_float_const(void)
2346 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2348 cnst->expression.type = EXPR_CONST;
2349 cnst->expression.datatype = type_double;
2350 cnst->v.float_value = token.v.floatvalue;
2354 return (expression_t*) cnst;
2357 static declaration_t *create_implicit_function(symbol_t *symbol,
2358 const source_position_t source_position)
2360 function_type_t *function_type
2361 = allocate_type_zero(sizeof(function_type[0]));
2363 function_type->type.type = TYPE_FUNCTION;
2364 function_type->result_type = type_int;
2365 function_type->unspecified_parameters = true;
2367 type_t *type = typehash_insert((type_t*) function_type);
2368 if(type != (type_t*) function_type) {
2369 free_type(function_type);
2372 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2374 declaration->storage_class = STORAGE_CLASS_EXTERN;
2375 declaration->type = type;
2376 declaration->symbol = symbol;
2377 declaration->source_position = source_position;
2379 /* prepend the implicit definition to the global context
2380 * this is safe since the symbol wasn't declared as anything else yet
2382 assert(symbol->declaration == NULL);
2384 context_t *last_context = context;
2385 context = global_context;
2387 environment_push(declaration);
2388 declaration->next = context->declarations;
2389 context->declarations = declaration;
2391 context = last_context;
2396 static expression_t *parse_reference(void)
2398 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2400 ref->expression.type = EXPR_REFERENCE;
2401 ref->symbol = token.v.symbol;
2403 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2405 source_position_t source_position = token.source_position;
2408 if(declaration == NULL) {
2410 /* an implicitly defined function */
2411 if(token.type == '(') {
2412 parser_print_prefix_pos(token.source_position);
2413 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2414 ref->symbol->string);
2416 declaration = create_implicit_function(ref->symbol,
2421 parser_print_error_prefix();
2422 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2423 return (expression_t*) ref;
2427 ref->declaration = declaration;
2428 ref->expression.datatype = declaration->type;
2430 return (expression_t*) ref;
2433 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2437 /* TODO check if explicit cast is allowed and issue warnings/errors */
2440 static expression_t *parse_cast(void)
2442 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2444 cast->expression.type = EXPR_UNARY;
2445 cast->type = UNEXPR_CAST;
2446 cast->expression.source_position = token.source_position;
2448 type_t *type = parse_typename();
2451 expression_t *value = parse_sub_expression(20);
2453 check_cast_allowed(value, type);
2455 cast->expression.datatype = type;
2456 cast->value = value;
2458 return (expression_t*) cast;
2461 static expression_t *parse_statement_expression(void)
2463 statement_expression_t *expression
2464 = allocate_ast_zero(sizeof(expression[0]));
2465 expression->expression.type = EXPR_STATEMENT;
2467 statement_t *statement = parse_compound_statement();
2468 expression->statement = statement;
2469 if(statement == NULL) {
2474 assert(statement->type == STATEMENT_COMPOUND);
2475 compound_statement_t *compound_statement
2476 = (compound_statement_t*) statement;
2478 /* find last statement and use it's type */
2479 const statement_t *last_statement = NULL;
2480 const statement_t *iter = compound_statement->statements;
2481 for( ; iter != NULL; iter = iter->next) {
2482 last_statement = iter;
2485 if(last_statement->type == STATEMENT_EXPRESSION) {
2486 const expression_statement_t *expression_statement =
2487 (const expression_statement_t*) last_statement;
2488 expression->expression.datatype
2489 = expression_statement->expression->datatype;
2491 expression->expression.datatype = type_void;
2496 return (expression_t*) expression;
2499 static expression_t *parse_brace_expression(void)
2503 switch(token.type) {
2505 /* gcc extension: a stement expression */
2506 return parse_statement_expression();
2510 return parse_cast();
2512 if(is_typedef_symbol(token.v.symbol)) {
2513 return parse_cast();
2517 expression_t *result = parse_expression();
2523 static expression_t *parse_function_keyword(void)
2528 if (current_function == NULL) {
2529 parse_error("'__func__' used outside of a function");
2532 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2533 expression->expression.type = EXPR_FUNCTION;
2534 expression->expression.datatype = type_string;
2535 expression->value = "TODO: FUNCTION";
2537 return (expression_t*) expression;
2540 static expression_t *parse_pretty_function_keyword(void)
2542 eat(T___PRETTY_FUNCTION__);
2545 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2546 expression->expression.type = EXPR_PRETTY_FUNCTION;
2547 expression->expression.datatype = type_string;
2548 expression->value = "TODO: PRETTY FUNCTION";
2550 return (expression_t*) expression;
2553 static designator_t *parse_designator(void)
2555 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2557 if(token.type != T_IDENTIFIER) {
2558 parse_error_expected("while parsing member designator",
2563 result->symbol = token.v.symbol;
2566 designator_t *last_designator = result;
2568 if(token.type == '.') {
2570 if(token.type != T_IDENTIFIER) {
2571 parse_error_expected("while parsing member designator",
2576 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2577 designator->symbol = token.v.symbol;
2580 last_designator->next = designator;
2581 last_designator = designator;
2584 if(token.type == '[') {
2586 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2587 designator->array_access = parse_expression();
2588 if(designator->array_access == NULL) {
2594 last_designator->next = designator;
2595 last_designator = designator;
2604 static expression_t *parse_offsetof(void)
2606 eat(T___builtin_offsetof);
2608 offsetof_expression_t *expression
2609 = allocate_ast_zero(sizeof(expression[0]));
2610 expression->expression.type = EXPR_OFFSETOF;
2611 expression->expression.datatype = type_size_t;
2614 expression->type = parse_typename();
2616 expression->designator = parse_designator();
2619 return (expression_t*) expression;
2622 static expression_t *parse_va_arg(void)
2624 eat(T___builtin_va_arg);
2626 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2627 expression->expression.type = EXPR_VA_ARG;
2630 expression->arg = parse_assignment_expression();
2632 expression->expression.datatype = parse_typename();
2635 return (expression_t*) expression;
2638 static expression_t *parse_builtin_symbol(void)
2640 builtin_symbol_expression_t *expression
2641 = allocate_ast_zero(sizeof(expression[0]));
2642 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2644 /* TODO: set datatype */
2646 expression->symbol = token.v.symbol;
2650 return (expression_t*) expression;
2653 static expression_t *parse_primary_expression(void)
2655 switch(token.type) {
2657 return parse_int_const();
2658 case T_FLOATINGPOINT:
2659 return parse_float_const();
2660 case T_STRING_LITERAL:
2661 return parse_string_const();
2663 return parse_reference();
2664 case T___FUNCTION__:
2666 return parse_function_keyword();
2667 case T___PRETTY_FUNCTION__:
2668 return parse_pretty_function_keyword();
2669 case T___builtin_offsetof:
2670 return parse_offsetof();
2671 case T___builtin_va_arg:
2672 return parse_va_arg();
2673 case T___builtin_expect:
2674 case T___builtin_va_start:
2675 case T___builtin_va_end:
2676 return parse_builtin_symbol();
2679 return parse_brace_expression();
2682 parser_print_error_prefix();
2683 fprintf(stderr, "unexpected token ");
2684 print_token(stderr, &token);
2685 fprintf(stderr, "\n");
2688 return make_invalid_expression();
2691 static expression_t *parse_array_expression(unsigned precedence,
2692 expression_t *array_ref)
2698 array_access_expression_t *array_access
2699 = allocate_ast_zero(sizeof(array_access[0]));
2701 array_access->expression.type = EXPR_ARRAY_ACCESS;
2702 array_access->array_ref = array_ref;
2703 array_access->index = parse_expression();
2705 type_t *type = array_ref->datatype;
2707 if(type->type == TYPE_POINTER) {
2708 pointer_type_t *pointer = (pointer_type_t*) type;
2709 array_access->expression.datatype = pointer->points_to;
2710 } else if(type->type == TYPE_ARRAY) {
2711 array_type_t *array_type = (array_type_t*) type;
2712 array_access->expression.datatype = array_type->element_type;
2714 parser_print_error_prefix();
2715 fprintf(stderr, "array access on object with non-pointer type ");
2716 print_type_quoted(type);
2717 fprintf(stderr, "\n");
2721 if(token.type != ']') {
2722 parse_error_expected("Problem while parsing array access", ']', 0);
2723 return (expression_t*) array_access;
2727 return (expression_t*) array_access;
2730 static bool is_declaration_specifier(const token_t *token,
2731 bool only_type_specifiers)
2733 switch(token->type) {
2737 return is_typedef_symbol(token->v.symbol);
2740 if(only_type_specifiers)
2749 static expression_t *parse_sizeof(unsigned precedence)
2753 sizeof_expression_t *sizeof_expression
2754 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2755 sizeof_expression->expression.type = EXPR_SIZEOF;
2756 sizeof_expression->expression.datatype = type_size_t;
2758 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2760 sizeof_expression->type = parse_typename();
2763 expression_t *expression = parse_sub_expression(precedence);
2764 sizeof_expression->type = expression->datatype;
2765 sizeof_expression->size_expression = expression;
2768 return (expression_t*) sizeof_expression;
2771 static expression_t *parse_select_expression(unsigned precedence,
2772 expression_t *compound)
2775 assert(token.type == '.' || token.type == T_MINUSGREATER);
2777 bool is_pointer = (token.type == T_MINUSGREATER);
2780 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2782 select->expression.type = EXPR_SELECT;
2783 select->compound = compound;
2785 if(token.type != T_IDENTIFIER) {
2786 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
2787 return (expression_t*) select;
2789 symbol_t *symbol = token.v.symbol;
2790 select->symbol = symbol;
2793 type_t *type = compound->datatype;
2795 return make_invalid_expression();
2797 type_t *type_left = type;
2799 if(type->type != TYPE_POINTER) {
2800 parser_print_error_prefix();
2801 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
2802 print_type_quoted(type);
2803 fputc('\n', stderr);
2804 return make_invalid_expression();
2806 pointer_type_t *pointer_type = (pointer_type_t*) type;
2807 type_left = pointer_type->points_to;
2809 type_left = skip_typeref(type_left);
2811 if(type_left->type != TYPE_COMPOUND_STRUCT
2812 && type_left->type != TYPE_COMPOUND_UNION) {
2813 parser_print_error_prefix();
2814 fprintf(stderr, "request for member '%s' in something not a struct or "
2815 "union, but ", symbol->string);
2816 print_type_quoted(type_left);
2817 fputc('\n', stderr);
2818 return make_invalid_expression();
2821 compound_type_t *compound_type = (compound_type_t*) type_left;
2822 declaration_t *declaration = compound_type->declaration;
2824 if(!declaration->init.is_defined) {
2825 parser_print_error_prefix();
2826 fprintf(stderr, "request for member '%s' of incomplete type ",
2828 print_type_quoted(type_left);
2829 fputc('\n', stderr);
2830 return make_invalid_expression();
2833 declaration_t *iter = declaration->context.declarations;
2834 for( ; iter != NULL; iter = iter->next) {
2835 if(iter->symbol == symbol) {
2840 parser_print_error_prefix();
2841 print_type_quoted(type_left);
2842 fprintf(stderr, " has no member named '%s'\n", symbol->string);
2843 return make_invalid_expression();
2846 select->compound_entry = iter;
2847 select->expression.datatype = iter->type;
2848 return (expression_t*) select;
2851 static expression_t *parse_call_expression(unsigned precedence,
2852 expression_t *expression)
2855 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2856 call->expression.type = EXPR_CALL;
2857 call->function = expression;
2859 function_type_t *function_type;
2860 type_t *type = expression->datatype;
2861 if (type->type == TYPE_FUNCTION) {
2862 function_type = (function_type_t*) type;
2863 call->expression.datatype = function_type->result_type;
2864 } else if (type->type == TYPE_POINTER &&
2865 ((pointer_type_t*)type)->points_to->type == TYPE_FUNCTION) {
2866 pointer_type_t *const ptr_type = (pointer_type_t*)type;
2867 function_type = (function_type_t*)ptr_type->points_to;
2868 call->expression.datatype = function_type->result_type;
2870 parser_print_error_prefix();
2871 fputs("called object '", stderr);
2872 print_expression(expression);
2873 fputs("' (type ", stderr);
2874 print_type_quoted(type);
2875 fputs(") is not a function\n", stderr);
2877 function_type = NULL;
2878 call->expression.datatype = NULL;
2881 /* parse arguments */
2884 if(token.type != ')') {
2885 call_argument_t *last_argument = NULL;
2888 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2890 argument->expression = parse_assignment_expression();
2891 if(last_argument == NULL) {
2892 call->arguments = argument;
2894 last_argument->next = argument;
2896 last_argument = argument;
2898 if(token.type != ',')
2905 if(function_type != NULL) {
2906 function_parameter_t *parameter = function_type->parameters;
2907 call_argument_t *argument = call->arguments;
2908 for( ; parameter != NULL && argument != NULL;
2909 parameter = parameter->next, argument = argument->next) {
2910 type_t *expected_type = parameter->type;
2911 /* TODO report context in error messages */
2912 argument->expression = create_implicit_cast(argument->expression,
2915 /* too few parameters */
2916 if(parameter != NULL) {
2917 parser_print_error_prefix();
2918 fprintf(stderr, "too few arguments to function '");
2919 print_expression(expression);
2920 fprintf(stderr, "'\n");
2921 } else if(argument != NULL) {
2922 /* too many parameters */
2923 if(!function_type->variadic
2924 && !function_type->unspecified_parameters) {
2925 parser_print_error_prefix();
2926 fprintf(stderr, "too many arguments to function '");
2927 print_expression(expression);
2928 fprintf(stderr, "'\n");
2930 /* do default promotion */
2931 for( ; argument != NULL; argument = argument->next) {
2932 type_t *type = argument->expression->datatype;
2937 if(is_type_integer(type)) {
2938 type = promote_integer(type);
2939 } else if(type == type_float) {
2942 argument->expression
2943 = create_implicit_cast(argument->expression, type);
2949 return (expression_t*) call;
2952 static type_t *get_type_after_conversion(const type_t *type1,
2953 const type_t *type2)
2957 return (type_t*) type1;
2960 static expression_t *parse_conditional_expression(unsigned precedence,
2961 expression_t *expression)
2965 conditional_expression_t *conditional
2966 = allocate_ast_zero(sizeof(conditional[0]));
2967 conditional->expression.type = EXPR_CONDITIONAL;
2968 conditional->condition = expression;
2971 type_t *condition_type_orig = conditional->condition->datatype;
2972 type_t *condition_type = skip_typeref(condition_type_orig);
2973 if(condition_type != NULL && !is_type_scalar(condition_type)) {
2974 type_error("expected a scalar type", expression->source_position,
2975 condition_type_orig);
2978 conditional->true_expression = parse_expression();
2980 conditional->false_expression = parse_sub_expression(precedence);
2982 type_t *true_type = conditional->true_expression->datatype;
2983 if(true_type == NULL)
2984 return (expression_t*) conditional;
2985 type_t *false_type = conditional->false_expression->datatype;
2986 if(false_type == NULL)
2987 return (expression_t*) conditional;
2989 type_t *const skipped_true_type = skip_typeref(true_type);
2990 type_t *const skipped_false_type = skip_typeref(false_type);
2993 if (skipped_true_type == skipped_false_type) {
2994 conditional->expression.datatype = skipped_true_type;
2995 } else if (is_type_arithmetic(skipped_true_type) &&
2996 is_type_arithmetic(skipped_false_type)) {
2997 type_t *const result = get_type_after_conversion(skipped_true_type,
2998 skipped_false_type);
2999 /* TODO: create implicit convs if necessary */
3000 conditional->expression.datatype = result;
3001 } else if (skipped_true_type->type == TYPE_POINTER &&
3002 skipped_false_type->type == TYPE_POINTER &&
3003 true /* TODO compatible points_to types */) {
3005 } else if(/* (is_null_ptr_const(skipped_true_type) &&
3006 skipped_false_type->type == TYPE_POINTER)
3007 || (is_null_ptr_const(skipped_false_type) &&
3008 skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3010 } else if(/* 1 is pointer to object type, other is void* */ false) {
3013 type_error_incompatible("while parsing conditional",
3014 expression->source_position, true_type,
3015 skipped_false_type);
3018 return (expression_t*) conditional;
3021 static expression_t *parse_extension(unsigned precedence)
3023 eat(T___extension__);
3025 /* TODO enable extensions */
3027 return parse_sub_expression(precedence);
3030 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3032 eat(T___builtin_classify_type);
3034 classify_type_expression_t *const classify_type_expr =
3035 allocate_ast_zero(sizeof(classify_type_expr[0]));
3036 classify_type_expr->expression.type = EXPR_CLASSIFY_TYPE;
3037 classify_type_expr->expression.datatype = type_int;
3040 expression_t *const expression = parse_sub_expression(precedence);
3042 classify_type_expr->type_expression = expression;
3044 return (expression_t*)classify_type_expr;
3047 static void semantic_incdec(unary_expression_t *expression)
3049 type_t *orig_type = expression->value->datatype;
3050 if(orig_type == NULL)
3053 type_t *type = skip_typeref(orig_type);
3054 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3055 /* TODO: improve error message */
3056 parser_print_error_prefix();
3057 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3061 expression->expression.datatype = orig_type;
3064 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3066 type_t *orig_type = expression->value->datatype;
3067 if(orig_type == NULL)
3070 type_t *type = skip_typeref(orig_type);
3071 if(!is_type_arithmetic(type)) {
3072 /* TODO: improve error message */
3073 parser_print_error_prefix();
3074 fprintf(stderr, "operation needs an arithmetic type\n");
3078 expression->expression.datatype = orig_type;
3081 static void semantic_unexpr_scalar(unary_expression_t *expression)
3083 type_t *orig_type = expression->value->datatype;
3084 if(orig_type == NULL)
3087 type_t *type = skip_typeref(orig_type);
3088 if (!is_type_scalar(type)) {
3089 parse_error("operand of ! must be of scalar type\n");
3093 expression->expression.datatype = orig_type;
3096 static void semantic_unexpr_integer(unary_expression_t *expression)
3098 type_t *orig_type = expression->value->datatype;
3099 if(orig_type == NULL)
3102 type_t *type = skip_typeref(orig_type);
3103 if (!is_type_integer(type)) {
3104 parse_error("operand of ~ must be of integer type\n");
3108 expression->expression.datatype = orig_type;
3111 static void semantic_dereference(unary_expression_t *expression)
3113 type_t *orig_type = expression->value->datatype;
3114 if(orig_type == NULL)
3117 type_t *type = skip_typeref(orig_type);
3118 switch (type->type) {
3120 array_type_t *const array_type = (array_type_t*)type;
3121 expression->expression.datatype = array_type->element_type;
3125 case TYPE_POINTER: {
3126 pointer_type_t *pointer_type = (pointer_type_t*)type;
3127 expression->expression.datatype = pointer_type->points_to;
3132 parser_print_error_prefix();
3133 fputs("'Unary *' needs pointer or arrray type, but type ", stderr);
3134 print_type_quoted(orig_type);
3135 fputs(" given.\n", stderr);
3140 static void semantic_take_addr(unary_expression_t *expression)
3142 type_t *orig_type = expression->value->datatype;
3143 if(orig_type == NULL)
3146 expression_t *value = expression->value;
3147 if(value->type == EXPR_REFERENCE) {
3148 reference_expression_t *reference = (reference_expression_t*) value;
3149 declaration_t *declaration = reference->declaration;
3150 if(declaration != NULL) {
3151 declaration->address_taken = 1;
3155 expression->expression.datatype = make_pointer_type(orig_type, 0);
3158 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3159 static expression_t *parse_##unexpression_type(unsigned precedence) \
3163 unary_expression_t *unary_expression \
3164 = allocate_ast_zero(sizeof(unary_expression[0])); \
3165 unary_expression->expression.type = EXPR_UNARY; \
3166 unary_expression->type = unexpression_type; \
3167 unary_expression->value = parse_sub_expression(precedence); \
3169 sfunc(unary_expression); \
3171 return (expression_t*) unary_expression; \
3174 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3175 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3176 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3177 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3178 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3179 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3180 semantic_unexpr_integer)
3181 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3183 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3186 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3188 static expression_t *parse_##unexpression_type(unsigned precedence, \
3189 expression_t *left) \
3191 (void) precedence; \
3194 unary_expression_t *unary_expression \
3195 = allocate_ast_zero(sizeof(unary_expression[0])); \
3196 unary_expression->expression.type = EXPR_UNARY; \
3197 unary_expression->type = unexpression_type; \
3198 unary_expression->value = left; \
3200 sfunc(unary_expression); \
3202 return (expression_t*) unary_expression; \
3205 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3207 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3210 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3212 /* TODO: handle complex + imaginary types */
3214 /* § 6.3.1.8 Usual arithmetic conversions */
3215 if(type_left == type_long_double || type_right == type_long_double) {
3216 return type_long_double;
3217 } else if(type_left == type_double || type_right == type_double) {
3219 } else if(type_left == type_float || type_right == type_float) {
3223 type_right = promote_integer(type_right);
3224 type_left = promote_integer(type_left);
3226 if(type_left == type_right)
3229 bool signed_left = is_type_signed(type_left);
3230 bool signed_right = is_type_signed(type_right);
3231 if(get_rank(type_left) < get_rank(type_right)) {
3232 if(signed_left == signed_right || !signed_right) {
3238 if(signed_left == signed_right || !signed_left) {
3246 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3248 expression_t *left = expression->left;
3249 expression_t *right = expression->right;
3250 type_t *orig_type_left = left->datatype;
3251 type_t *orig_type_right = right->datatype;
3253 if(orig_type_left == NULL || orig_type_right == NULL)
3256 type_t *type_left = skip_typeref(orig_type_left);
3257 type_t *type_right = skip_typeref(orig_type_right);
3259 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3260 /* TODO: improve error message */
3261 parser_print_error_prefix();
3262 fprintf(stderr, "operation needs arithmetic types\n");
3266 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3267 expression->left = create_implicit_cast(left, arithmetic_type);
3268 expression->right = create_implicit_cast(right, arithmetic_type);
3269 expression->expression.datatype = arithmetic_type;
3272 static void semantic_shift_op(binary_expression_t *expression)
3274 expression_t *left = expression->left;
3275 expression_t *right = expression->right;
3276 type_t *orig_type_left = left->datatype;
3277 type_t *orig_type_right = right->datatype;
3279 if(orig_type_left == NULL || orig_type_right == NULL)
3282 type_t *type_left = skip_typeref(orig_type_left);
3283 type_t *type_right = skip_typeref(orig_type_right);
3285 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3286 /* TODO: improve error message */
3287 parser_print_error_prefix();
3288 fprintf(stderr, "operation needs integer types\n");
3292 type_left = promote_integer(type_left);
3293 type_right = promote_integer(type_right);
3295 expression->left = create_implicit_cast(left, type_left);
3296 expression->right = create_implicit_cast(right, type_right);
3297 expression->expression.datatype = type_left;
3300 static void semantic_add(binary_expression_t *expression)
3302 expression_t *left = expression->left;
3303 expression_t *right = expression->right;
3304 type_t *orig_type_left = left->datatype;
3305 type_t *orig_type_right = right->datatype;
3307 if(orig_type_left == NULL || orig_type_right == NULL)
3310 type_t *type_left = skip_typeref(orig_type_left);
3311 type_t *type_right = skip_typeref(orig_type_right);
3314 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3315 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3316 expression->left = create_implicit_cast(left, arithmetic_type);
3317 expression->right = create_implicit_cast(right, arithmetic_type);
3318 expression->expression.datatype = arithmetic_type;
3320 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3321 expression->expression.datatype = type_left;
3322 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
3323 expression->expression.datatype = type_right;
3324 } else if (type_left->type == TYPE_ARRAY && is_type_integer(type_right)) {
3325 const array_type_t *const arr_type = (const array_type_t*)type_left;
3326 expression->expression.datatype =
3327 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3328 } else if (type_right->type == TYPE_ARRAY && is_type_integer(type_left)) {
3329 const array_type_t *const arr_type = (const array_type_t*)type_right;
3330 expression->expression.datatype =
3331 make_pointer_type(arr_type->element_type, TYPE_QUALIFIER_NONE);
3333 parser_print_error_prefix();
3334 fprintf(stderr, "invalid operands to binary + (");
3335 print_type_quoted(orig_type_left);
3336 fprintf(stderr, ", ");
3337 print_type_quoted(orig_type_right);
3338 fprintf(stderr, ")\n");
3342 static void semantic_sub(binary_expression_t *expression)
3344 expression_t *left = expression->left;
3345 expression_t *right = expression->right;
3346 type_t *orig_type_left = left->datatype;
3347 type_t *orig_type_right = right->datatype;
3349 if(orig_type_left == NULL || orig_type_right == NULL)
3352 type_t *type_left = skip_typeref(orig_type_left);
3353 type_t *type_right = skip_typeref(orig_type_right);
3356 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3357 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3358 expression->left = create_implicit_cast(left, arithmetic_type);
3359 expression->right = create_implicit_cast(right, arithmetic_type);
3360 expression->expression.datatype = arithmetic_type;
3362 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3363 expression->expression.datatype = type_left;
3364 } else if(type_left->type == TYPE_POINTER &&
3365 type_right->type == TYPE_POINTER) {
3366 if(!pointers_compatible(type_left, type_right)) {
3367 parser_print_error_prefix();
3368 fprintf(stderr, "pointers to incompatible objects to binary - (");
3369 print_type_quoted(orig_type_left);
3370 fprintf(stderr, ", ");
3371 print_type_quoted(orig_type_right);
3372 fprintf(stderr, ")\n");
3374 expression->expression.datatype = type_ptrdiff_t;
3377 parser_print_error_prefix();
3378 fprintf(stderr, "invalid operands to binary - (");
3379 print_type_quoted(orig_type_left);
3380 fprintf(stderr, ", ");
3381 print_type_quoted(orig_type_right);
3382 fprintf(stderr, ")\n");
3386 static void semantic_comparison(binary_expression_t *expression)
3388 expression_t *left = expression->left;
3389 expression_t *right = expression->right;
3390 type_t *orig_type_left = left->datatype;
3391 type_t *orig_type_right = right->datatype;
3393 if(orig_type_left == NULL || orig_type_right == NULL)
3396 type_t *type_left = skip_typeref(orig_type_left);
3397 type_t *type_right = skip_typeref(orig_type_right);
3399 /* TODO non-arithmetic types */
3400 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3401 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3402 expression->left = create_implicit_cast(left, arithmetic_type);
3403 expression->right = create_implicit_cast(right, arithmetic_type);
3404 expression->expression.datatype = arithmetic_type;
3405 } else if (type_left->type == TYPE_POINTER &&
3406 type_right->type == TYPE_POINTER) {
3407 /* TODO check compatibility */
3408 } else if (type_left->type == TYPE_POINTER) {
3409 expression->right = create_implicit_cast(right, type_left);
3410 } else if (type_right->type == TYPE_POINTER) {
3411 expression->left = create_implicit_cast(left, type_right);
3413 type_error_incompatible("invalid operands in comparison",
3414 expression->expression.source_position,
3415 type_left, type_right);
3417 expression->expression.datatype = type_int;
3420 static void semantic_arithmetic_assign(binary_expression_t *expression)
3422 expression_t *left = expression->left;
3423 expression_t *right = expression->right;
3424 type_t *orig_type_left = left->datatype;
3425 type_t *orig_type_right = right->datatype;
3427 if(orig_type_left == NULL || orig_type_right == NULL)
3430 type_t *type_left = skip_typeref(orig_type_left);
3431 type_t *type_right = skip_typeref(orig_type_right);
3433 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3434 /* TODO: improve error message */
3435 parser_print_error_prefix();
3436 fprintf(stderr, "operation needs arithmetic types\n");
3440 /* combined instructions are tricky. We can't create an implicit cast on
3441 * the left side, because we need the uncasted form for the store.
3442 * The ast2firm pass has to know that left_type must be right_type
3443 * for the arithmeitc operation and create a cast by itself */
3444 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3445 expression->right = create_implicit_cast(right, arithmetic_type);
3446 expression->expression.datatype = type_left;
3449 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3451 expression_t *left = expression->left;
3452 expression_t *right = expression->right;
3453 type_t *orig_type_left = left->datatype;
3454 type_t *orig_type_right = right->datatype;
3456 if(orig_type_left == NULL || orig_type_right == NULL)
3459 type_t *type_left = skip_typeref(orig_type_left);
3460 type_t *type_right = skip_typeref(orig_type_right);
3462 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3463 /* combined instructions are tricky. We can't create an implicit cast on
3464 * the left side, because we need the uncasted form for the store.
3465 * The ast2firm pass has to know that left_type must be right_type
3466 * for the arithmeitc operation and create a cast by itself */
3467 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3468 expression->right = create_implicit_cast(right, arithmetic_type);
3469 expression->expression.datatype = type_left;
3470 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3471 expression->expression.datatype = type_left;
3473 parser_print_error_prefix();
3474 fputs("Incompatible types ", stderr);
3475 print_type_quoted(orig_type_left);
3476 fputs(" and ", stderr);
3477 print_type_quoted(orig_type_right);
3478 fputs(" in assignment\n", stderr);
3483 static void semantic_logical_op(binary_expression_t *expression)
3485 expression_t *left = expression->left;
3486 expression_t *right = expression->right;
3487 type_t *orig_type_left = left->datatype;
3488 type_t *orig_type_right = right->datatype;
3490 if(orig_type_left == NULL || orig_type_right == NULL)
3493 type_t *type_left = skip_typeref(orig_type_left);
3494 type_t *type_right = skip_typeref(orig_type_right);
3496 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3497 /* TODO: improve error message */
3498 parser_print_error_prefix();
3499 fprintf(stderr, "operation needs scalar types\n");
3503 expression->expression.datatype = type_int;
3506 static void semantic_binexpr_assign(binary_expression_t *expression)
3508 expression_t *left = expression->left;
3509 type_t *type_left = left->datatype;
3511 if (type_left->type == TYPE_ARRAY) {
3512 parse_error("Cannot assign to arrays.");
3513 } else if (type_left != NULL) {
3514 semantic_assign(type_left, &expression->right, "assignment");
3517 expression->expression.datatype = type_left;
3520 static void semantic_comma(binary_expression_t *expression)
3522 expression->expression.datatype = expression->right->datatype;
3525 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3526 static expression_t *parse_##binexpression_type(unsigned precedence, \
3527 expression_t *left) \
3531 expression_t *right = parse_sub_expression(precedence + lr); \
3533 binary_expression_t *binexpr \
3534 = allocate_ast_zero(sizeof(binexpr[0])); \
3535 binexpr->expression.type = EXPR_BINARY; \
3536 binexpr->type = binexpression_type; \
3537 binexpr->left = left; \
3538 binexpr->right = right; \
3541 return (expression_t*) binexpr; \
3544 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3545 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3546 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3547 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3548 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3549 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3550 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3551 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3552 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3553 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3554 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3555 semantic_comparison, 1)
3556 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3557 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3558 semantic_comparison, 1)
3559 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3560 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3561 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3562 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3563 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3564 /* TODO shift has a bit special semantic */
3565 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3566 semantic_shift_op, 1)
3567 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3568 semantic_shift_op, 1)
3569 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3570 semantic_arithmetic_addsubb_assign, 0)
3571 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3572 semantic_arithmetic_addsubb_assign, 0)
3573 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3574 semantic_arithmetic_assign, 0)
3575 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3576 semantic_arithmetic_assign, 0)
3577 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3578 semantic_arithmetic_assign, 0)
3579 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3580 semantic_arithmetic_assign, 0)
3581 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3582 semantic_arithmetic_assign, 0)
3583 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3584 semantic_arithmetic_assign, 0)
3585 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3586 semantic_arithmetic_assign, 0)
3587 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3588 semantic_arithmetic_assign, 0)
3590 static expression_t *parse_sub_expression(unsigned precedence)
3592 if(token.type < 0) {
3593 return expected_expression_error();
3596 expression_parser_function_t *parser
3597 = &expression_parsers[token.type];
3598 source_position_t source_position = token.source_position;
3601 if(parser->parser != NULL) {
3602 left = parser->parser(parser->precedence);
3604 left = parse_primary_expression();
3606 assert(left != NULL);
3607 left->source_position = source_position;
3610 if(token.type < 0) {
3611 return expected_expression_error();
3614 parser = &expression_parsers[token.type];
3615 if(parser->infix_parser == NULL)
3617 if(parser->infix_precedence < precedence)
3620 left = parser->infix_parser(parser->infix_precedence, left);
3622 assert(left != NULL);
3623 assert(left->type != EXPR_UNKNOWN);
3624 left->source_position = source_position;
3630 static expression_t *parse_expression(void)
3632 return parse_sub_expression(1);
3637 static void register_expression_parser(parse_expression_function parser,
3638 int token_type, unsigned precedence)
3640 expression_parser_function_t *entry = &expression_parsers[token_type];
3642 if(entry->parser != NULL) {
3643 fprintf(stderr, "for token ");
3644 print_token_type(stderr, token_type);
3645 fprintf(stderr, "\n");
3646 panic("trying to register multiple expression parsers for a token");
3648 entry->parser = parser;
3649 entry->precedence = precedence;
3652 static void register_expression_infix_parser(
3653 parse_expression_infix_function parser, int token_type,
3654 unsigned precedence)
3656 expression_parser_function_t *entry = &expression_parsers[token_type];
3658 if(entry->infix_parser != NULL) {
3659 fprintf(stderr, "for token ");
3660 print_token_type(stderr, token_type);
3661 fprintf(stderr, "\n");
3662 panic("trying to register multiple infix expression parsers for a "
3665 entry->infix_parser = parser;
3666 entry->infix_precedence = precedence;
3669 static void init_expression_parsers(void)
3671 memset(&expression_parsers, 0, sizeof(expression_parsers));
3673 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3674 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3675 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3676 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3677 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3678 T_GREATERGREATER, 16);
3679 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3680 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3681 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3682 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3683 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3684 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3685 T_GREATEREQUAL, 14);
3686 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3687 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3688 T_EXCLAMATIONMARKEQUAL, 13);
3689 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3690 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3691 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3692 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3693 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3694 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3695 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3696 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3697 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3698 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3699 T_ASTERISKEQUAL, 2);
3700 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3701 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3703 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3704 T_LESSLESSEQUAL, 2);
3705 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3706 T_GREATERGREATEREQUAL, 2);
3707 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3709 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3711 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3714 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3716 register_expression_infix_parser(parse_array_expression, '[', 30);
3717 register_expression_infix_parser(parse_call_expression, '(', 30);
3718 register_expression_infix_parser(parse_select_expression, '.', 30);
3719 register_expression_infix_parser(parse_select_expression,
3720 T_MINUSGREATER, 30);
3721 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3723 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3726 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3727 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3728 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3729 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3730 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3731 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3732 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3733 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3734 register_expression_parser(parse_sizeof, T_sizeof, 25);
3735 register_expression_parser(parse_extension, T___extension__, 25);
3736 register_expression_parser(parse_builtin_classify_type,
3737 T___builtin_classify_type, 25);
3741 static statement_t *parse_case_statement(void)
3744 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3745 label->statement.type = STATEMENT_CASE_LABEL;
3746 label->statement.source_position = token.source_position;
3748 label->expression = parse_expression();
3751 label->statement.next = parse_statement();
3753 return (statement_t*) label;
3756 static statement_t *parse_default_statement(void)
3760 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3761 label->statement.type = STATEMENT_CASE_LABEL;
3762 label->statement.source_position = token.source_position;
3765 label->statement.next = parse_statement();
3767 return (statement_t*) label;
3770 static declaration_t *get_label(symbol_t *symbol)
3772 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3773 assert(current_function != NULL);
3774 /* if we found a label in the same function, then we already created the
3776 if(candidate != NULL
3777 && candidate->parent_context == ¤t_function->context) {
3781 /* otherwise we need to create a new one */
3782 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3783 declaration->namespc = NAMESPACE_LABEL;
3784 declaration->symbol = symbol;
3786 label_push(declaration);
3791 static statement_t *parse_label_statement(void)
3793 assert(token.type == T_IDENTIFIER);
3794 symbol_t *symbol = token.v.symbol;
3797 declaration_t *label = get_label(symbol);
3799 /* if source position is already set then the label is defined twice,
3800 * otherwise it was just mentioned in a goto so far */
3801 if(label->source_position.input_name != NULL) {
3802 parser_print_error_prefix();
3803 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3804 parser_print_error_prefix_pos(label->source_position);
3805 fprintf(stderr, "previous definition of '%s' was here\n",
3808 label->source_position = token.source_position;
3811 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
3813 label_statement->statement.type = STATEMENT_LABEL;
3814 label_statement->statement.source_position = token.source_position;
3815 label_statement->label = label;
3819 if(token.type == '}') {
3820 parse_error("label at end of compound statement");
3821 return (statement_t*) label_statement;
3823 label_statement->label_statement = parse_statement();
3826 return (statement_t*) label_statement;
3829 static statement_t *parse_if(void)
3833 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3834 statement->statement.type = STATEMENT_IF;
3835 statement->statement.source_position = token.source_position;
3838 statement->condition = parse_expression();
3841 statement->true_statement = parse_statement();
3842 if(token.type == T_else) {
3844 statement->false_statement = parse_statement();
3847 return (statement_t*) statement;
3850 static statement_t *parse_switch(void)
3854 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3855 statement->statement.type = STATEMENT_SWITCH;
3856 statement->statement.source_position = token.source_position;
3859 statement->expression = parse_expression();
3861 statement->body = parse_statement();
3863 return (statement_t*) statement;
3866 static statement_t *parse_while(void)
3870 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3871 statement->statement.type = STATEMENT_WHILE;
3872 statement->statement.source_position = token.source_position;
3875 statement->condition = parse_expression();
3877 statement->body = parse_statement();
3879 return (statement_t*) statement;
3882 static statement_t *parse_do(void)
3886 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3887 statement->statement.type = STATEMENT_DO_WHILE;
3888 statement->statement.source_position = token.source_position;
3890 statement->body = parse_statement();
3893 statement->condition = parse_expression();
3897 return (statement_t*) statement;
3900 static statement_t *parse_for(void)
3904 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3905 statement->statement.type = STATEMENT_FOR;
3906 statement->statement.source_position = token.source_position;
3910 int top = environment_top();
3911 context_t *last_context = context;
3912 set_context(&statement->context);
3914 if(token.type != ';') {
3915 if(is_declaration_specifier(&token, false)) {
3916 parse_declaration();
3918 statement->initialisation = parse_expression();
3925 if(token.type != ';') {
3926 statement->condition = parse_expression();
3929 if(token.type != ')') {
3930 statement->step = parse_expression();
3933 statement->body = parse_statement();
3935 assert(context == &statement->context);
3936 set_context(last_context);
3937 environment_pop_to(top);
3939 return (statement_t*) statement;
3942 static statement_t *parse_goto(void)
3946 if(token.type != T_IDENTIFIER) {
3947 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
3951 symbol_t *symbol = token.v.symbol;
3954 declaration_t *label = get_label(symbol);
3956 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3958 statement->statement.type = STATEMENT_GOTO;
3959 statement->statement.source_position = token.source_position;
3961 statement->label = label;
3965 return (statement_t*) statement;
3968 static statement_t *parse_continue(void)
3973 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3974 statement->type = STATEMENT_CONTINUE;
3975 statement->source_position = token.source_position;
3980 static statement_t *parse_break(void)
3985 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3986 statement->type = STATEMENT_BREAK;
3987 statement->source_position = token.source_position;
3992 static statement_t *parse_return(void)
3996 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3998 statement->statement.type = STATEMENT_RETURN;
3999 statement->statement.source_position = token.source_position;
4001 assert(current_function->type->type == TYPE_FUNCTION);
4002 function_type_t *function_type = (function_type_t*) current_function->type;
4003 type_t *return_type = function_type->result_type;
4005 expression_t *return_value;
4006 if(token.type != ';') {
4007 return_value = parse_expression();
4009 if(return_type == type_void && return_value->datatype != type_void) {
4010 parse_warning("'return' with a value, in function returning void");
4011 return_value = NULL;
4013 if(return_type != NULL) {
4014 semantic_assign(return_type, &return_value, "'return'");
4018 return_value = NULL;
4019 if(return_type != type_void) {
4020 parse_warning("'return' without value, in function returning "
4024 statement->return_value = return_value;
4028 return (statement_t*) statement;
4031 static statement_t *parse_declaration_statement(void)
4033 declaration_t *before = last_declaration;
4035 declaration_statement_t *statement
4036 = allocate_ast_zero(sizeof(statement[0]));
4037 statement->statement.type = STATEMENT_DECLARATION;
4038 statement->statement.source_position = token.source_position;
4040 declaration_specifiers_t specifiers;
4041 memset(&specifiers, 0, sizeof(specifiers));
4042 parse_declaration_specifiers(&specifiers);
4044 if(token.type == ';') {
4047 parse_init_declarators(&specifiers);
4050 if(before == NULL) {
4051 statement->declarations_begin = context->declarations;
4053 statement->declarations_begin = before->next;
4055 statement->declarations_end = last_declaration;
4057 return (statement_t*) statement;
4060 static statement_t *parse_expression_statement(void)
4062 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4063 statement->statement.type = STATEMENT_EXPRESSION;
4064 statement->statement.source_position = token.source_position;
4066 statement->expression = parse_expression();
4070 return (statement_t*) statement;
4073 static statement_t *parse_statement(void)
4075 statement_t *statement = NULL;
4077 /* declaration or statement */
4078 switch(token.type) {
4080 statement = parse_case_statement();
4084 statement = parse_default_statement();
4088 statement = parse_compound_statement();
4092 statement = parse_if();
4096 statement = parse_switch();
4100 statement = parse_while();
4104 statement = parse_do();
4108 statement = parse_for();
4112 statement = parse_goto();
4116 statement = parse_continue();
4120 statement = parse_break();
4124 statement = parse_return();
4133 if(look_ahead(1)->type == ':') {
4134 statement = parse_label_statement();
4138 if(is_typedef_symbol(token.v.symbol)) {
4139 statement = parse_declaration_statement();
4143 statement = parse_expression_statement();
4146 case T___extension__:
4147 /* this can be a prefix to a declaration or an expression statement */
4148 /* we simply eat it now and parse the rest with tail recursion */
4151 } while(token.type == T___extension__);
4152 statement = parse_statement();
4156 statement = parse_declaration_statement();
4160 statement = parse_expression_statement();
4164 assert(statement == NULL || statement->source_position.input_name != NULL);
4169 static statement_t *parse_compound_statement(void)
4171 compound_statement_t *compound_statement
4172 = allocate_ast_zero(sizeof(compound_statement[0]));
4173 compound_statement->statement.type = STATEMENT_COMPOUND;
4174 compound_statement->statement.source_position = token.source_position;
4178 int top = environment_top();
4179 context_t *last_context = context;
4180 set_context(&compound_statement->context);
4182 statement_t *last_statement = NULL;
4184 while(token.type != '}' && token.type != T_EOF) {
4185 statement_t *statement = parse_statement();
4186 if(statement == NULL)
4189 if(last_statement != NULL) {
4190 last_statement->next = statement;
4192 compound_statement->statements = statement;
4195 while(statement->next != NULL)
4196 statement = statement->next;
4198 last_statement = statement;
4201 if(token.type != '}') {
4202 parser_print_error_prefix_pos(
4203 compound_statement->statement.source_position);
4204 fprintf(stderr, "end of file while looking for closing '}'\n");
4208 assert(context == &compound_statement->context);
4209 set_context(last_context);
4210 environment_pop_to(top);
4212 return (statement_t*) compound_statement;
4215 static translation_unit_t *parse_translation_unit(void)
4217 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4219 assert(global_context == NULL);
4220 global_context = &unit->context;
4222 assert(context == NULL);
4223 set_context(&unit->context);
4225 while(token.type != T_EOF) {
4226 parse_declaration();
4229 assert(context == &unit->context);
4231 last_declaration = NULL;
4233 assert(global_context == &unit->context);
4234 global_context = NULL;
4239 translation_unit_t *parse(void)
4241 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4242 label_stack = NEW_ARR_F(stack_entry_t, 0);
4243 found_error = false;
4245 type_set_output(stderr);
4246 ast_set_output(stderr);
4248 lookahead_bufpos = 0;
4249 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4252 translation_unit_t *unit = parse_translation_unit();
4254 DEL_ARR_F(environment_stack);
4255 DEL_ARR_F(label_stack);
4263 void init_parser(void)
4265 init_expression_parsers();
4266 obstack_init(&temp_obst);
4268 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
4269 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
4270 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
4271 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
4272 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
4273 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
4274 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
4275 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4276 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
4277 type_string = make_pointer_type(type_const_char, 0);
4280 void exit_parser(void)
4282 obstack_free(&temp_obst, NULL);