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;
28 typedef struct declaration_specifiers_t declaration_specifiers_t;
29 struct declaration_specifiers_t {
30 source_position_t source_position;
31 unsigned char storage_class;
33 decl_modifiers_t decl_modifiers;
37 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
40 static token_t lookahead_buffer[MAX_LOOKAHEAD];
41 static int lookahead_bufpos;
42 static stack_entry_t *environment_stack = NULL;
43 static stack_entry_t *label_stack = NULL;
44 static context_t *global_context = NULL;
45 static context_t *context = NULL;
46 static declaration_t *last_declaration = NULL;
47 static declaration_t *current_function = NULL;
48 static struct obstack temp_obst;
49 static bool found_error;
51 static type_t *type_int = NULL;
52 static type_t *type_long_double = NULL;
53 static type_t *type_double = NULL;
54 static type_t *type_float = NULL;
55 static type_t *type_char = NULL;
56 static type_t *type_string = NULL;
57 static type_t *type_void = NULL;
58 static type_t *type_void_ptr = NULL;
59 static type_t *type_valist = NULL;
61 type_t *type_size_t = NULL;
62 type_t *type_ptrdiff_t = NULL;
63 type_t *type_wchar_t = NULL;
64 type_t *type_wchar_t_ptr = NULL;
66 static statement_t *parse_compound_statement(void);
67 static statement_t *parse_statement(void);
69 static expression_t *parse_sub_expression(unsigned precedence);
70 static expression_t *parse_expression(void);
71 static type_t *parse_typename(void);
73 static void parse_compound_type_entries(void);
74 static declaration_t *parse_declarator(
75 const declaration_specifiers_t *specifiers, bool may_be_abstract);
76 static declaration_t *record_declaration(declaration_t *declaration);
78 static void semantic_comparison(binary_expression_t *expression);
80 #define STORAGE_CLASSES \
87 #define TYPE_QUALIFIERS \
94 #ifdef PROVIDE_COMPLEX
95 #define COMPLEX_SPECIFIERS \
97 #define IMAGINARY_SPECIFIERS \
100 #define COMPLEX_SPECIFIERS
101 #define IMAGINARY_SPECIFIERS
104 #define TYPE_SPECIFIERS \
119 case T___builtin_va_list: \
123 #define DECLARATION_START \
128 #define TYPENAME_START \
132 static void *allocate_ast_zero(size_t size)
134 void *res = allocate_ast(size);
135 memset(res, 0, size);
139 static size_t get_statement_struct_size(statement_type_t type)
141 static const size_t sizes[] = {
142 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
143 [STATEMENT_RETURN] = sizeof(return_statement_t),
144 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
145 [STATEMENT_IF] = sizeof(if_statement_t),
146 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
147 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
148 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
149 [STATEMENT_BREAK] = sizeof(statement_base_t),
150 [STATEMENT_GOTO] = sizeof(goto_statement_t),
151 [STATEMENT_LABEL] = sizeof(label_statement_t),
152 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
153 [STATEMENT_WHILE] = sizeof(while_statement_t),
154 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
155 [STATEMENT_FOR] = sizeof(for_statement_t),
156 [STATEMENT_ASM] = sizeof(asm_statement_t)
158 assert(type <= sizeof(sizes) / sizeof(sizes[0]));
159 assert(sizes[type] != 0);
163 static statement_t *allocate_statement_zero(statement_type_t type)
165 size_t size = get_statement_struct_size(type);
166 statement_t *res = allocate_ast_zero(size);
168 res->base.type = type;
173 static size_t get_expression_struct_size(expression_type_t type)
175 static const size_t sizes[] = {
176 [EXPR_INVALID] = sizeof(expression_base_t),
177 [EXPR_REFERENCE] = sizeof(reference_expression_t),
178 [EXPR_CONST] = sizeof(const_expression_t),
179 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
180 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
181 [EXPR_CALL] = sizeof(call_expression_t),
182 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
183 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
184 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
185 [EXPR_SELECT] = sizeof(select_expression_t),
186 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
187 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
188 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
189 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
190 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
191 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
192 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
193 [EXPR_VA_START] = sizeof(va_start_expression_t),
194 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
195 [EXPR_STATEMENT] = sizeof(statement_expression_t),
197 if(type >= EXPR_UNARY_FIRST && type <= EXPR_UNARY_LAST) {
198 return sizes[EXPR_UNARY_FIRST];
200 if(type >= EXPR_BINARY_FIRST && type <= EXPR_BINARY_LAST) {
201 return sizes[EXPR_BINARY_FIRST];
203 assert(type <= sizeof(sizes) / sizeof(sizes[0]));
204 assert(sizes[type] != 0);
208 static expression_t *allocate_expression_zero(expression_type_t type)
210 size_t size = get_expression_struct_size(type);
211 expression_t *res = allocate_ast_zero(size);
213 res->base.type = type;
217 static size_t get_type_struct_size(type_type_t type)
219 static const size_t sizes[] = {
220 [TYPE_ATOMIC] = sizeof(atomic_type_t),
221 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
222 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
223 [TYPE_ENUM] = sizeof(enum_type_t),
224 [TYPE_FUNCTION] = sizeof(function_type_t),
225 [TYPE_POINTER] = sizeof(pointer_type_t),
226 [TYPE_ARRAY] = sizeof(array_type_t),
227 [TYPE_BUILTIN] = sizeof(builtin_type_t),
228 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
229 [TYPE_TYPEOF] = sizeof(typeof_type_t),
231 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
232 assert(type <= TYPE_TYPEOF);
233 assert(sizes[type] != 0);
237 static type_t *allocate_type_zero(type_type_t type)
239 size_t size = get_type_struct_size(type);
240 type_t *res = obstack_alloc(type_obst, size);
241 memset(res, 0, size);
243 res->base.type = type;
247 static size_t get_initializer_size(initializer_type_t type)
249 static const size_t sizes[] = {
250 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
251 [INITIALIZER_STRING] = sizeof(initializer_string_t),
252 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
253 [INITIALIZER_LIST] = sizeof(initializer_list_t)
255 assert(type < sizeof(sizes) / sizeof(*sizes));
256 assert(sizes[type] != 0);
260 static initializer_t *allocate_initializer(initializer_type_t type)
262 initializer_t *result = allocate_ast_zero(get_initializer_size(type));
268 static void free_type(void *type)
270 obstack_free(type_obst, type);
274 * returns the top element of the environment stack
276 static size_t environment_top(void)
278 return ARR_LEN(environment_stack);
281 static size_t label_top(void)
283 return ARR_LEN(label_stack);
288 static inline void next_token(void)
290 token = lookahead_buffer[lookahead_bufpos];
291 lookahead_buffer[lookahead_bufpos] = lexer_token;
294 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
297 print_token(stderr, &token);
298 fprintf(stderr, "\n");
302 static inline const token_t *look_ahead(int num)
304 assert(num > 0 && num <= MAX_LOOKAHEAD);
305 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
306 return &lookahead_buffer[pos];
309 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
311 static void error(void)
314 #ifdef ABORT_ON_ERROR
319 static void parser_print_prefix_pos(const source_position_t source_position)
321 fputs(source_position.input_name, stderr);
323 fprintf(stderr, "%u", source_position.linenr);
327 static void parser_print_error_prefix_pos(
328 const source_position_t source_position)
330 parser_print_prefix_pos(source_position);
331 fputs("error: ", stderr);
335 static void parser_print_error_prefix(void)
337 parser_print_error_prefix_pos(token.source_position);
340 static void parse_error(const char *message)
342 parser_print_error_prefix();
343 fprintf(stderr, "parse error: %s\n", message);
346 static void parser_print_warning_prefix_pos(
347 const source_position_t source_position)
349 parser_print_prefix_pos(source_position);
350 fputs("warning: ", stderr);
353 static void parser_print_warning_prefix(void)
355 parser_print_warning_prefix_pos(token.source_position);
358 static void parse_warning_pos(const source_position_t source_position,
359 const char *const message)
361 parser_print_prefix_pos(source_position);
362 fprintf(stderr, "warning: %s\n", message);
365 static void parse_warning(const char *message)
367 parse_warning_pos(token.source_position, message);
370 static void parse_error_expected(const char *message, ...)
375 if(message != NULL) {
376 parser_print_error_prefix();
377 fprintf(stderr, "%s\n", message);
379 parser_print_error_prefix();
380 fputs("Parse error: got ", stderr);
381 print_token(stderr, &token);
382 fputs(", expected ", stderr);
384 va_start(args, message);
385 token_type_t token_type = va_arg(args, token_type_t);
386 while(token_type != 0) {
390 fprintf(stderr, ", ");
392 print_token_type(stderr, token_type);
393 token_type = va_arg(args, token_type_t);
396 fprintf(stderr, "\n");
399 static void print_type_quoted(type_t *type)
406 static void type_error(const char *msg, const source_position_t source_position,
409 parser_print_error_prefix_pos(source_position);
410 fprintf(stderr, "%s, but found type ", msg);
411 print_type_quoted(type);
415 static void type_error_incompatible(const char *msg,
416 const source_position_t source_position, type_t *type1, type_t *type2)
418 parser_print_error_prefix_pos(source_position);
419 fprintf(stderr, "%s, incompatible types: ", msg);
420 print_type_quoted(type1);
421 fprintf(stderr, " - ");
422 print_type_quoted(type2);
423 fprintf(stderr, ")\n");
426 static void eat_block(void)
428 if(token.type == '{')
431 while(token.type != '}') {
432 if(token.type == T_EOF)
434 if(token.type == '{') {
443 static void eat_statement(void)
445 while(token.type != ';') {
446 if(token.type == T_EOF)
448 if(token.type == '}')
450 if(token.type == '{') {
459 static void eat_paren(void)
461 if(token.type == '(')
464 while(token.type != ')') {
465 if(token.type == T_EOF)
467 if(token.type == ')' || token.type == ';' || token.type == '}') {
470 if(token.type == '(') {
474 if(token.type == '{') {
483 #define expect(expected) \
484 if(UNLIKELY(token.type != (expected))) { \
485 parse_error_expected(NULL, (expected), 0); \
491 #define expect_block(expected) \
492 if(UNLIKELY(token.type != (expected))) { \
493 parse_error_expected(NULL, (expected), 0); \
499 #define expect_void(expected) \
500 if(UNLIKELY(token.type != (expected))) { \
501 parse_error_expected(NULL, (expected), 0); \
507 static void set_context(context_t *new_context)
509 context = new_context;
511 last_declaration = new_context->declarations;
512 if(last_declaration != NULL) {
513 while(last_declaration->next != NULL) {
514 last_declaration = last_declaration->next;
520 * called when we find a 2nd declarator for an identifier we already have a
523 static bool is_compatible_declaration(declaration_t *declaration,
524 declaration_t *previous)
526 /* happens for K&R style function parameters */
527 if(previous->type == NULL) {
528 previous->type = declaration->type;
532 type_t *type1 = skip_typeref(declaration->type);
533 type_t *type2 = skip_typeref(previous->type);
535 return types_compatible(type1, type2);
538 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
540 declaration_t *declaration = symbol->declaration;
541 for( ; declaration != NULL; declaration = declaration->symbol_next) {
542 if(declaration->namespc == namespc)
549 static const char *get_namespace_prefix(namespace_t namespc)
552 case NAMESPACE_NORMAL:
554 case NAMESPACE_UNION:
556 case NAMESPACE_STRUCT:
560 case NAMESPACE_LABEL:
563 panic("invalid namespace found");
567 * pushs an environment_entry on the environment stack and links the
568 * corresponding symbol to the new entry
570 static declaration_t *stack_push(stack_entry_t **stack_ptr,
571 declaration_t *declaration,
572 context_t *parent_context)
574 symbol_t *symbol = declaration->symbol;
575 namespace_t namespc = (namespace_t)declaration->namespc;
577 /* a declaration should be only pushed once */
578 declaration->parent_context = parent_context;
580 declaration_t *previous_declaration = get_declaration(symbol, namespc);
581 assert(declaration != previous_declaration);
582 if(previous_declaration != NULL
583 && previous_declaration->parent_context == context) {
584 if(!is_compatible_declaration(declaration, previous_declaration)) {
585 parser_print_error_prefix_pos(declaration->source_position);
586 fprintf(stderr, "definition of symbol '%s%s' with type ",
587 get_namespace_prefix(namespc), symbol->string);
588 print_type_quoted(declaration->type);
590 parser_print_error_prefix_pos(
591 previous_declaration->source_position);
592 fprintf(stderr, "is incompatible with previous declaration "
594 print_type_quoted(previous_declaration->type);
597 unsigned old_storage_class = previous_declaration->storage_class;
598 unsigned new_storage_class = declaration->storage_class;
599 type_t *type = previous_declaration->type;
600 type = skip_typeref(type);
602 if (current_function == NULL) {
603 if (old_storage_class != STORAGE_CLASS_STATIC &&
604 new_storage_class == STORAGE_CLASS_STATIC) {
605 parser_print_error_prefix_pos(declaration->source_position);
607 "static declaration of '%s' follows non-static declaration\n",
609 parser_print_error_prefix_pos(previous_declaration->source_position);
610 fprintf(stderr, "previous declaration of '%s' was here\n",
613 if (old_storage_class == STORAGE_CLASS_EXTERN) {
614 if (new_storage_class == STORAGE_CLASS_NONE) {
615 previous_declaration->storage_class = STORAGE_CLASS_NONE;
617 } else if(!is_type_function(type)) {
618 parser_print_warning_prefix_pos(declaration->source_position);
619 fprintf(stderr, "redundant declaration for '%s'\n",
621 parser_print_warning_prefix_pos(previous_declaration->source_position);
622 fprintf(stderr, "previous declaration of '%s' was here\n",
627 if (old_storage_class == STORAGE_CLASS_EXTERN &&
628 new_storage_class == STORAGE_CLASS_EXTERN) {
629 parser_print_warning_prefix_pos(declaration->source_position);
630 fprintf(stderr, "redundant extern declaration for '%s'\n",
632 parser_print_warning_prefix_pos(previous_declaration->source_position);
633 fprintf(stderr, "previous declaration of '%s' was here\n",
636 parser_print_error_prefix_pos(declaration->source_position);
637 if (old_storage_class == new_storage_class) {
638 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
640 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
642 parser_print_error_prefix_pos(previous_declaration->source_position);
643 fprintf(stderr, "previous declaration of '%s' was here\n",
648 return previous_declaration;
651 /* remember old declaration */
653 entry.symbol = symbol;
654 entry.old_declaration = symbol->declaration;
655 entry.namespc = (unsigned short) namespc;
656 ARR_APP1(stack_entry_t, *stack_ptr, entry);
658 /* replace/add declaration into declaration list of the symbol */
659 if(symbol->declaration == NULL) {
660 symbol->declaration = declaration;
662 declaration_t *iter_last = NULL;
663 declaration_t *iter = symbol->declaration;
664 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
665 /* replace an entry? */
666 if(iter->namespc == namespc) {
667 if(iter_last == NULL) {
668 symbol->declaration = declaration;
670 iter_last->symbol_next = declaration;
672 declaration->symbol_next = iter->symbol_next;
677 assert(iter_last->symbol_next == NULL);
678 iter_last->symbol_next = declaration;
685 static declaration_t *environment_push(declaration_t *declaration)
687 assert(declaration->source_position.input_name != NULL);
688 return stack_push(&environment_stack, declaration, context);
691 static declaration_t *label_push(declaration_t *declaration)
693 return stack_push(&label_stack, declaration, ¤t_function->context);
697 * pops symbols from the environment stack until @p new_top is the top element
699 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
701 stack_entry_t *stack = *stack_ptr;
702 size_t top = ARR_LEN(stack);
705 assert(new_top <= top);
709 for(i = top; i > new_top; --i) {
710 stack_entry_t *entry = &stack[i - 1];
712 declaration_t *old_declaration = entry->old_declaration;
713 symbol_t *symbol = entry->symbol;
714 namespace_t namespc = (namespace_t)entry->namespc;
716 /* replace/remove declaration */
717 declaration_t *declaration = symbol->declaration;
718 assert(declaration != NULL);
719 if(declaration->namespc == namespc) {
720 if(old_declaration == NULL) {
721 symbol->declaration = declaration->symbol_next;
723 symbol->declaration = old_declaration;
726 declaration_t *iter_last = declaration;
727 declaration_t *iter = declaration->symbol_next;
728 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
729 /* replace an entry? */
730 if(iter->namespc == namespc) {
731 assert(iter_last != NULL);
732 iter_last->symbol_next = old_declaration;
733 old_declaration->symbol_next = iter->symbol_next;
737 assert(iter != NULL);
741 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
744 static void environment_pop_to(size_t new_top)
746 stack_pop_to(&environment_stack, new_top);
749 static void label_pop_to(size_t new_top)
751 stack_pop_to(&label_stack, new_top);
755 static int get_rank(const type_t *type)
757 assert(!is_typeref(type));
758 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
759 * and esp. footnote 108). However we can't fold constants (yet), so we
760 * can't decide wether unsigned int is possible, while int always works.
761 * (unsigned int would be preferable when possible... for stuff like
762 * struct { enum { ... } bla : 4; } ) */
763 if(type->type == TYPE_ENUM)
764 return ATOMIC_TYPE_INT;
766 assert(type->type == TYPE_ATOMIC);
767 const atomic_type_t *atomic_type = &type->atomic;
768 atomic_type_type_t atype = atomic_type->atype;
772 static type_t *promote_integer(type_t *type)
774 if(get_rank(type) < ATOMIC_TYPE_INT)
780 static expression_t *create_cast_expression(expression_t *expression,
783 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
785 cast->unary.value = expression;
786 cast->base.datatype = dest_type;
791 static bool is_null_pointer_constant(const expression_t *expression)
793 /* skip void* cast */
794 if(expression->type == EXPR_UNARY_CAST
795 || expression->type == EXPR_UNARY_CAST_IMPLICIT) {
796 expression = expression->unary.value;
799 /* TODO: not correct yet, should be any constant integer expression
800 * which evaluates to 0 */
801 if (expression->type != EXPR_CONST)
804 type_t *const type = skip_typeref(expression->base.datatype);
805 if (!is_type_integer(type))
808 return expression->conste.v.int_value == 0;
811 static expression_t *create_implicit_cast(expression_t *expression,
814 type_t *source_type = expression->base.datatype;
816 if(source_type == NULL)
819 source_type = skip_typeref(source_type);
820 dest_type = skip_typeref(dest_type);
822 if(source_type == dest_type)
825 switch (dest_type->type) {
827 /* TODO warning for implicitly converting to enum */
829 if (source_type->type != TYPE_ATOMIC &&
830 source_type->type != TYPE_ENUM) {
831 panic("casting of non-atomic types not implemented yet");
834 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
835 type_error_incompatible("can't cast types",
836 expression->base.source_position, source_type,
841 return create_cast_expression(expression, dest_type);
844 switch (source_type->type) {
846 if (is_null_pointer_constant(expression)) {
847 return create_cast_expression(expression, dest_type);
852 if (pointers_compatible(source_type, dest_type)) {
853 return create_cast_expression(expression, dest_type);
858 array_type_t *array_type = &source_type->array;
859 pointer_type_t *pointer_type = &dest_type->pointer;
860 if (types_compatible(array_type->element_type,
861 pointer_type->points_to)) {
862 return create_cast_expression(expression, dest_type);
868 panic("casting of non-atomic types not implemented yet");
871 type_error_incompatible("can't implicitly cast types",
872 expression->base.source_position, source_type, dest_type);
876 panic("casting of non-atomic types not implemented yet");
880 /** Implements the rules from § 6.5.16.1 */
881 static void semantic_assign(type_t *orig_type_left, expression_t **right,
884 type_t *orig_type_right = (*right)->base.datatype;
886 if(orig_type_right == NULL)
889 type_t *const type_left = skip_typeref(orig_type_left);
890 type_t *const type_right = skip_typeref(orig_type_right);
892 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
893 (is_type_pointer(type_left) && is_null_pointer_constant(*right)) ||
894 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
895 && is_type_pointer(type_right))) {
896 *right = create_implicit_cast(*right, type_left);
900 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
901 pointer_type_t *pointer_type_left = &type_left->pointer;
902 pointer_type_t *pointer_type_right = &type_right->pointer;
903 type_t *points_to_left = pointer_type_left->points_to;
904 type_t *points_to_right = pointer_type_right->points_to;
906 points_to_left = skip_typeref(points_to_left);
907 points_to_right = skip_typeref(points_to_right);
909 /* the left type has all qualifiers from the right type */
910 unsigned missing_qualifiers
911 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
912 if(missing_qualifiers != 0) {
913 parser_print_error_prefix();
914 fprintf(stderr, "destination type ");
915 print_type_quoted(type_left);
916 fprintf(stderr, " in %s from type ", context);
917 print_type_quoted(type_right);
918 fprintf(stderr, " lacks qualifiers '");
919 print_type_qualifiers(missing_qualifiers);
920 fprintf(stderr, "' in pointed-to type\n");
924 points_to_left = get_unqualified_type(points_to_left);
925 points_to_right = get_unqualified_type(points_to_right);
927 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
928 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
929 && !types_compatible(points_to_left, points_to_right)) {
930 goto incompatible_assign_types;
933 *right = create_implicit_cast(*right, type_left);
937 if (is_type_compound(type_left)
938 && types_compatible(type_left, type_right)) {
939 *right = create_implicit_cast(*right, type_left);
943 incompatible_assign_types:
944 /* TODO: improve error message */
945 parser_print_error_prefix();
946 fprintf(stderr, "incompatible types in %s\n", context);
947 parser_print_error_prefix();
948 print_type_quoted(orig_type_left);
949 fputs(" <- ", stderr);
950 print_type_quoted(orig_type_right);
954 static expression_t *parse_constant_expression(void)
956 /* start parsing at precedence 7 (conditional expression) */
957 return parse_sub_expression(7);
960 static expression_t *parse_assignment_expression(void)
962 /* start parsing at precedence 2 (assignment expression) */
963 return parse_sub_expression(2);
966 static type_t *make_global_typedef(const char *name, type_t *type)
968 symbol_t *symbol = symbol_table_insert(name);
970 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
971 declaration->namespc = NAMESPACE_NORMAL;
972 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
973 declaration->type = type;
974 declaration->symbol = symbol;
975 declaration->source_position = builtin_source_position;
977 record_declaration(declaration);
979 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
980 typedef_type->typedeft.declaration = declaration;
985 static const char *parse_string_literals(void)
987 assert(token.type == T_STRING_LITERAL);
988 const char *result = token.v.string;
992 while(token.type == T_STRING_LITERAL) {
993 result = concat_strings(result, token.v.string);
1000 static void parse_attributes(void)
1003 switch(token.type) {
1004 case T___attribute__: {
1010 switch(token.type) {
1012 parse_error("EOF while parsing attribute");
1031 if(token.type != T_STRING_LITERAL) {
1032 parse_error_expected("while parsing assembler attribute",
1037 parse_string_literals();
1042 goto attributes_finished;
1046 attributes_finished:
1051 static designator_t *parse_designation(void)
1053 if(token.type != '[' && token.type != '.')
1056 designator_t *result = NULL;
1057 designator_t *last = NULL;
1060 designator_t *designator;
1061 switch(token.type) {
1063 designator = allocate_ast_zero(sizeof(designator[0]));
1065 designator->array_access = parse_constant_expression();
1069 designator = allocate_ast_zero(sizeof(designator[0]));
1071 if(token.type != T_IDENTIFIER) {
1072 parse_error_expected("while parsing designator",
1076 designator->symbol = token.v.symbol;
1084 assert(designator != NULL);
1086 last->next = designator;
1088 result = designator;
1095 static initializer_t *initializer_from_string(array_type_t *type,
1098 /* TODO: check len vs. size of array type */
1101 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
1102 initializer->string.string = string;
1107 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1108 wide_string_t *const string)
1110 /* TODO: check len vs. size of array type */
1113 initializer_t *const initializer =
1114 allocate_initializer(INITIALIZER_WIDE_STRING);
1115 initializer->wide_string.string = *string;
1120 static initializer_t *initializer_from_expression(type_t *type,
1121 expression_t *expression)
1123 /* TODO check that expression is a constant expression */
1125 /* § 6.7.8.14/15 char array may be initialized by string literals */
1126 type_t *const expr_type = expression->base.datatype;
1127 if (is_type_array(type) && expr_type->type == TYPE_POINTER) {
1128 array_type_t *const array_type = &type->array;
1129 type_t *const element_type = skip_typeref(array_type->element_type);
1131 if (element_type->type == TYPE_ATOMIC) {
1132 switch (expression->type) {
1133 case EXPR_STRING_LITERAL:
1134 if (element_type->atomic.atype == ATOMIC_TYPE_CHAR) {
1135 return initializer_from_string(array_type,
1136 expression->string.value);
1139 case EXPR_WIDE_STRING_LITERAL: {
1140 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1141 if (get_unqualified_type(element_type) == bare_wchar_type) {
1142 return initializer_from_wide_string(array_type,
1143 &expression->wide_string.value);
1152 type_t *expression_type = skip_typeref(expression->base.datatype);
1153 if(is_type_scalar(type) || types_compatible(type, expression_type)) {
1154 semantic_assign(type, &expression, "initializer");
1156 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1157 result->value.value = expression;
1165 static initializer_t *parse_sub_initializer(type_t *type,
1166 expression_t *expression,
1167 type_t *expression_type);
1169 static initializer_t *parse_sub_initializer_elem(type_t *type)
1171 if(token.type == '{') {
1172 return parse_sub_initializer(type, NULL, NULL);
1175 expression_t *expression = parse_assignment_expression();
1176 type_t *expression_type = skip_typeref(expression->base.datatype);
1178 return parse_sub_initializer(type, expression, expression_type);
1181 static bool had_initializer_brace_warning;
1183 static initializer_t *parse_sub_initializer(type_t *type,
1184 expression_t *expression,
1185 type_t *expression_type)
1187 if(is_type_scalar(type)) {
1188 /* there might be extra {} hierarchies */
1189 if(token.type == '{') {
1191 if(!had_initializer_brace_warning) {
1192 parse_warning("braces around scalar initializer");
1193 had_initializer_brace_warning = true;
1195 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1196 if(token.type == ',') {
1198 /* TODO: warn about excessive elements */
1204 if(expression == NULL) {
1205 expression = parse_assignment_expression();
1207 return initializer_from_expression(type, expression);
1210 /* does the expression match the currently looked at object to initalize */
1211 if(expression != NULL) {
1212 initializer_t *result = initializer_from_expression(type, expression);
1217 bool read_paren = false;
1218 if(token.type == '{') {
1223 /* descend into subtype */
1224 initializer_t *result = NULL;
1225 initializer_t **elems;
1226 if(is_type_array(type)) {
1227 array_type_t *array_type = &type->array;
1228 type_t *element_type = array_type->element_type;
1229 element_type = skip_typeref(element_type);
1232 had_initializer_brace_warning = false;
1233 if(expression == NULL) {
1234 sub = parse_sub_initializer_elem(element_type);
1236 sub = parse_sub_initializer(element_type, expression,
1240 /* didn't match the subtypes -> try the parent type */
1242 assert(!read_paren);
1246 elems = NEW_ARR_F(initializer_t*, 0);
1247 ARR_APP1(initializer_t*, elems, sub);
1250 if(token.type == '}')
1253 if(token.type == '}')
1256 sub = parse_sub_initializer_elem(element_type);
1258 /* TODO error, do nicer cleanup */
1259 parse_error("member initializer didn't match");
1263 ARR_APP1(initializer_t*, elems, sub);
1266 assert(is_type_compound(type));
1267 compound_type_t *compound_type = &type->compound;
1268 context_t *context = &compound_type->declaration->context;
1270 declaration_t *first = context->declarations;
1273 type_t *first_type = first->type;
1274 first_type = skip_typeref(first_type);
1277 had_initializer_brace_warning = false;
1278 if(expression == NULL) {
1279 sub = parse_sub_initializer_elem(first_type);
1281 sub = parse_sub_initializer(first_type, expression,expression_type);
1284 /* didn't match the subtypes -> try our parent type */
1286 assert(!read_paren);
1290 elems = NEW_ARR_F(initializer_t*, 0);
1291 ARR_APP1(initializer_t*, elems, sub);
1293 declaration_t *iter = first->next;
1294 for( ; iter != NULL; iter = iter->next) {
1295 if(iter->symbol == NULL)
1297 if(iter->namespc != NAMESPACE_NORMAL)
1300 if(token.type == '}')
1303 if(token.type == '}')
1306 type_t *iter_type = iter->type;
1307 iter_type = skip_typeref(iter_type);
1309 sub = parse_sub_initializer_elem(iter_type);
1311 /* TODO error, do nicer cleanup*/
1312 parse_error("member initializer didn't match");
1316 ARR_APP1(initializer_t*, elems, sub);
1320 int len = ARR_LEN(elems);
1321 size_t elems_size = sizeof(initializer_t*) * len;
1323 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1325 init->initializer.type = INITIALIZER_LIST;
1327 memcpy(init->initializers, elems, elems_size);
1330 result = (initializer_t*) init;
1333 if(token.type == ',')
1340 static initializer_t *parse_initializer(type_t *type)
1342 initializer_t *result;
1344 type = skip_typeref(type);
1346 if(token.type != '{') {
1347 expression_t *expression = parse_assignment_expression();
1348 initializer_t *initializer = initializer_from_expression(type, expression);
1349 if(initializer == NULL) {
1350 parser_print_error_prefix();
1351 fprintf(stderr, "initializer expression '");
1352 print_expression(expression);
1353 fprintf(stderr, "', type ");
1354 print_type_quoted(expression->base.datatype);
1355 fprintf(stderr, " is incompatible with type ");
1356 print_type_quoted(type);
1357 fprintf(stderr, "\n");
1362 if(is_type_scalar(type)) {
1366 expression_t *expression = parse_assignment_expression();
1367 result = initializer_from_expression(type, expression);
1369 if(token.type == ',')
1375 result = parse_sub_initializer(type, NULL, NULL);
1383 static declaration_t *parse_compound_type_specifier(bool is_struct)
1391 symbol_t *symbol = NULL;
1392 declaration_t *declaration = NULL;
1394 if (token.type == T___attribute__) {
1399 if(token.type == T_IDENTIFIER) {
1400 symbol = token.v.symbol;
1404 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1406 declaration = get_declaration(symbol, NAMESPACE_UNION);
1408 } else if(token.type != '{') {
1410 parse_error_expected("while parsing struct type specifier",
1411 T_IDENTIFIER, '{', 0);
1413 parse_error_expected("while parsing union type specifier",
1414 T_IDENTIFIER, '{', 0);
1420 if(declaration == NULL) {
1421 declaration = allocate_ast_zero(sizeof(declaration[0]));
1424 declaration->namespc = NAMESPACE_STRUCT;
1426 declaration->namespc = NAMESPACE_UNION;
1428 declaration->source_position = token.source_position;
1429 declaration->symbol = symbol;
1430 record_declaration(declaration);
1433 if(token.type == '{') {
1434 if(declaration->init.is_defined) {
1435 assert(symbol != NULL);
1436 parser_print_error_prefix();
1437 fprintf(stderr, "multiple definition of %s %s\n",
1438 is_struct ? "struct" : "union", symbol->string);
1439 declaration->context.declarations = NULL;
1441 declaration->init.is_defined = true;
1443 int top = environment_top();
1444 context_t *last_context = context;
1445 set_context(&declaration->context);
1447 parse_compound_type_entries();
1450 assert(context == &declaration->context);
1451 set_context(last_context);
1452 environment_pop_to(top);
1458 static void parse_enum_entries(enum_type_t *const enum_type)
1462 if(token.type == '}') {
1464 parse_error("empty enum not allowed");
1469 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1471 if(token.type != T_IDENTIFIER) {
1472 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1476 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1477 entry->type = (type_t*) enum_type;
1478 entry->symbol = token.v.symbol;
1479 entry->source_position = token.source_position;
1482 if(token.type == '=') {
1484 entry->init.enum_value = parse_constant_expression();
1489 record_declaration(entry);
1491 if(token.type != ',')
1494 } while(token.type != '}');
1499 static type_t *parse_enum_specifier(void)
1503 declaration_t *declaration;
1506 if(token.type == T_IDENTIFIER) {
1507 symbol = token.v.symbol;
1510 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1511 } else if(token.type != '{') {
1512 parse_error_expected("while parsing enum type specifier",
1513 T_IDENTIFIER, '{', 0);
1520 if(declaration == NULL) {
1521 declaration = allocate_ast_zero(sizeof(declaration[0]));
1523 declaration->namespc = NAMESPACE_ENUM;
1524 declaration->source_position = token.source_position;
1525 declaration->symbol = symbol;
1528 type_t *const type = allocate_type_zero(TYPE_ENUM);
1529 type->enumt.declaration = declaration;
1531 if(token.type == '{') {
1532 if(declaration->init.is_defined) {
1533 parser_print_error_prefix();
1534 fprintf(stderr, "multiple definitions of enum %s\n",
1537 record_declaration(declaration);
1538 declaration->init.is_defined = 1;
1540 parse_enum_entries(&type->enumt);
1548 * if a symbol is a typedef to another type, return true
1550 static bool is_typedef_symbol(symbol_t *symbol)
1552 const declaration_t *const declaration =
1553 get_declaration(symbol, NAMESPACE_NORMAL);
1555 declaration != NULL &&
1556 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1559 static type_t *parse_typeof(void)
1567 expression_t *expression = NULL;
1570 switch(token.type) {
1571 case T___extension__:
1572 /* this can be a prefix to a typename or an expression */
1573 /* we simply eat it now. */
1576 } while(token.type == T___extension__);
1580 if(is_typedef_symbol(token.v.symbol)) {
1581 type = parse_typename();
1583 expression = parse_expression();
1584 type = expression->base.datatype;
1589 type = parse_typename();
1593 expression = parse_expression();
1594 type = expression->base.datatype;
1600 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1601 typeof_type->typeoft.expression = expression;
1602 typeof_type->typeoft.typeof_type = type;
1608 SPECIFIER_SIGNED = 1 << 0,
1609 SPECIFIER_UNSIGNED = 1 << 1,
1610 SPECIFIER_LONG = 1 << 2,
1611 SPECIFIER_INT = 1 << 3,
1612 SPECIFIER_DOUBLE = 1 << 4,
1613 SPECIFIER_CHAR = 1 << 5,
1614 SPECIFIER_SHORT = 1 << 6,
1615 SPECIFIER_LONG_LONG = 1 << 7,
1616 SPECIFIER_FLOAT = 1 << 8,
1617 SPECIFIER_BOOL = 1 << 9,
1618 SPECIFIER_VOID = 1 << 10,
1619 #ifdef PROVIDE_COMPLEX
1620 SPECIFIER_COMPLEX = 1 << 11,
1621 SPECIFIER_IMAGINARY = 1 << 12,
1625 static type_t *create_builtin_type(symbol_t *const symbol,
1626 type_t *const real_type)
1628 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1629 type->builtin.symbol = symbol;
1630 type->builtin.real_type = real_type;
1632 type_t *result = typehash_insert(type);
1633 if (type != result) {
1640 static type_t *get_typedef_type(symbol_t *symbol)
1642 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1643 if(declaration == NULL
1644 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1647 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1648 type->typedeft.declaration = declaration;
1653 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1655 type_t *type = NULL;
1656 unsigned type_qualifiers = 0;
1657 unsigned type_specifiers = 0;
1660 specifiers->source_position = token.source_position;
1663 switch(token.type) {
1666 #define MATCH_STORAGE_CLASS(token, class) \
1668 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1669 parse_error("multiple storage classes in declaration " \
1672 specifiers->storage_class = class; \
1676 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1677 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1678 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1679 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1680 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1683 switch (specifiers->storage_class) {
1684 case STORAGE_CLASS_NONE:
1685 specifiers->storage_class = STORAGE_CLASS_THREAD;
1688 case STORAGE_CLASS_EXTERN:
1689 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1692 case STORAGE_CLASS_STATIC:
1693 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1697 parse_error("multiple storage classes in declaration specifiers");
1703 /* type qualifiers */
1704 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1706 type_qualifiers |= qualifier; \
1710 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1711 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1712 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1714 case T___extension__:
1719 /* type specifiers */
1720 #define MATCH_SPECIFIER(token, specifier, name) \
1723 if(type_specifiers & specifier) { \
1724 parse_error("multiple " name " type specifiers given"); \
1726 type_specifiers |= specifier; \
1730 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1731 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1732 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1733 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1734 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1735 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1736 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1737 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1738 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1739 #ifdef PROVIDE_COMPLEX
1740 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1741 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1744 /* only in microsoft mode */
1745 specifiers->decl_modifiers |= DM_FORCEINLINE;
1749 specifiers->is_inline = true;
1754 if(type_specifiers & SPECIFIER_LONG_LONG) {
1755 parse_error("multiple type specifiers given");
1756 } else if(type_specifiers & SPECIFIER_LONG) {
1757 type_specifiers |= SPECIFIER_LONG_LONG;
1759 type_specifiers |= SPECIFIER_LONG;
1763 /* TODO: if type != NULL for the following rules should issue
1766 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1768 type->compound.declaration = parse_compound_type_specifier(true);
1772 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1774 type->compound.declaration = parse_compound_type_specifier(false);
1778 type = parse_enum_specifier();
1781 type = parse_typeof();
1783 case T___builtin_va_list:
1784 type = duplicate_type(type_valist);
1788 case T___attribute__:
1793 case T_IDENTIFIER: {
1794 type_t *typedef_type = get_typedef_type(token.v.symbol);
1796 if(typedef_type == NULL)
1797 goto finish_specifiers;
1800 type = typedef_type;
1804 /* function specifier */
1806 goto finish_specifiers;
1813 atomic_type_type_t atomic_type;
1815 /* match valid basic types */
1816 switch(type_specifiers) {
1817 case SPECIFIER_VOID:
1818 atomic_type = ATOMIC_TYPE_VOID;
1820 case SPECIFIER_CHAR:
1821 atomic_type = ATOMIC_TYPE_CHAR;
1823 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1824 atomic_type = ATOMIC_TYPE_SCHAR;
1826 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1827 atomic_type = ATOMIC_TYPE_UCHAR;
1829 case SPECIFIER_SHORT:
1830 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1831 case SPECIFIER_SHORT | SPECIFIER_INT:
1832 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1833 atomic_type = ATOMIC_TYPE_SHORT;
1835 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1836 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1837 atomic_type = ATOMIC_TYPE_USHORT;
1840 case SPECIFIER_SIGNED:
1841 case SPECIFIER_SIGNED | SPECIFIER_INT:
1842 atomic_type = ATOMIC_TYPE_INT;
1844 case SPECIFIER_UNSIGNED:
1845 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1846 atomic_type = ATOMIC_TYPE_UINT;
1848 case SPECIFIER_LONG:
1849 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1850 case SPECIFIER_LONG | SPECIFIER_INT:
1851 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1852 atomic_type = ATOMIC_TYPE_LONG;
1854 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1855 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1856 atomic_type = ATOMIC_TYPE_ULONG;
1858 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1859 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1860 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1861 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1863 atomic_type = ATOMIC_TYPE_LONGLONG;
1865 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1866 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1868 atomic_type = ATOMIC_TYPE_ULONGLONG;
1870 case SPECIFIER_FLOAT:
1871 atomic_type = ATOMIC_TYPE_FLOAT;
1873 case SPECIFIER_DOUBLE:
1874 atomic_type = ATOMIC_TYPE_DOUBLE;
1876 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1877 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1879 case SPECIFIER_BOOL:
1880 atomic_type = ATOMIC_TYPE_BOOL;
1882 #ifdef PROVIDE_COMPLEX
1883 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1884 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1886 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1887 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1889 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1890 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1892 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1893 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1895 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1896 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1898 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1899 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1903 /* invalid specifier combination, give an error message */
1904 if(type_specifiers == 0) {
1906 parse_warning("no type specifiers in declaration, using int");
1907 atomic_type = ATOMIC_TYPE_INT;
1910 parse_error("no type specifiers given in declaration");
1912 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1913 (type_specifiers & SPECIFIER_UNSIGNED)) {
1914 parse_error("signed and unsigned specifiers gives");
1915 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1916 parse_error("only integer types can be signed or unsigned");
1918 parse_error("multiple datatypes in declaration");
1920 atomic_type = ATOMIC_TYPE_INVALID;
1923 type = allocate_type_zero(TYPE_ATOMIC);
1924 type->atomic.atype = atomic_type;
1927 if(type_specifiers != 0) {
1928 parse_error("multiple datatypes in declaration");
1932 type->base.qualifiers = type_qualifiers;
1934 type_t *result = typehash_insert(type);
1935 if(newtype && result != type) {
1939 specifiers->type = result;
1942 static type_qualifiers_t parse_type_qualifiers(void)
1944 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1947 switch(token.type) {
1948 /* type qualifiers */
1949 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1950 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1951 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1954 return type_qualifiers;
1959 static declaration_t *parse_identifier_list(void)
1961 declaration_t *declarations = NULL;
1962 declaration_t *last_declaration = NULL;
1964 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1966 declaration->source_position = token.source_position;
1967 declaration->symbol = token.v.symbol;
1970 if(last_declaration != NULL) {
1971 last_declaration->next = declaration;
1973 declarations = declaration;
1975 last_declaration = declaration;
1977 if(token.type != ',')
1980 } while(token.type == T_IDENTIFIER);
1982 return declarations;
1985 static void semantic_parameter(declaration_t *declaration)
1987 /* TODO: improve error messages */
1989 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1990 parse_error("typedef not allowed in parameter list");
1991 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1992 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1993 parse_error("parameter may only have none or register storage class");
1996 type_t *orig_type = declaration->type;
1997 if(orig_type == NULL)
1999 type_t *type = skip_typeref(orig_type);
2001 /* Array as last part of a paramter type is just syntactic sugar. Turn it
2002 * into a pointer. § 6.7.5.3 (7) */
2003 if (is_type_array(type)) {
2004 const array_type_t *arr_type = &type->array;
2005 type_t *element_type = arr_type->element_type;
2007 type = make_pointer_type(element_type, type->base.qualifiers);
2009 declaration->type = type;
2012 if(is_type_incomplete(type)) {
2013 parser_print_error_prefix();
2014 fprintf(stderr, "incomplete type (");
2015 print_type_quoted(orig_type);
2016 fprintf(stderr, ") not allowed for parameter '%s'\n",
2017 declaration->symbol->string);
2021 static declaration_t *parse_parameter(void)
2023 declaration_specifiers_t specifiers;
2024 memset(&specifiers, 0, sizeof(specifiers));
2026 parse_declaration_specifiers(&specifiers);
2028 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
2030 semantic_parameter(declaration);
2035 static declaration_t *parse_parameters(function_type_t *type)
2037 if(token.type == T_IDENTIFIER) {
2038 symbol_t *symbol = token.v.symbol;
2039 if(!is_typedef_symbol(symbol)) {
2040 type->kr_style_parameters = true;
2041 return parse_identifier_list();
2045 if(token.type == ')') {
2046 type->unspecified_parameters = 1;
2049 if(token.type == T_void && look_ahead(1)->type == ')') {
2054 declaration_t *declarations = NULL;
2055 declaration_t *declaration;
2056 declaration_t *last_declaration = NULL;
2057 function_parameter_t *parameter;
2058 function_parameter_t *last_parameter = NULL;
2061 switch(token.type) {
2065 return declarations;
2068 case T___extension__:
2070 declaration = parse_parameter();
2072 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2073 memset(parameter, 0, sizeof(parameter[0]));
2074 parameter->type = declaration->type;
2076 if(last_parameter != NULL) {
2077 last_declaration->next = declaration;
2078 last_parameter->next = parameter;
2080 type->parameters = parameter;
2081 declarations = declaration;
2083 last_parameter = parameter;
2084 last_declaration = declaration;
2088 return declarations;
2090 if(token.type != ',')
2091 return declarations;
2101 } construct_type_type_t;
2103 typedef struct construct_type_t construct_type_t;
2104 struct construct_type_t {
2105 construct_type_type_t type;
2106 construct_type_t *next;
2109 typedef struct parsed_pointer_t parsed_pointer_t;
2110 struct parsed_pointer_t {
2111 construct_type_t construct_type;
2112 type_qualifiers_t type_qualifiers;
2115 typedef struct construct_function_type_t construct_function_type_t;
2116 struct construct_function_type_t {
2117 construct_type_t construct_type;
2118 type_t *function_type;
2121 typedef struct parsed_array_t parsed_array_t;
2122 struct parsed_array_t {
2123 construct_type_t construct_type;
2124 type_qualifiers_t type_qualifiers;
2130 typedef struct construct_base_type_t construct_base_type_t;
2131 struct construct_base_type_t {
2132 construct_type_t construct_type;
2136 static construct_type_t *parse_pointer_declarator(void)
2140 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2141 memset(pointer, 0, sizeof(pointer[0]));
2142 pointer->construct_type.type = CONSTRUCT_POINTER;
2143 pointer->type_qualifiers = parse_type_qualifiers();
2145 return (construct_type_t*) pointer;
2148 static construct_type_t *parse_array_declarator(void)
2152 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2153 memset(array, 0, sizeof(array[0]));
2154 array->construct_type.type = CONSTRUCT_ARRAY;
2156 if(token.type == T_static) {
2157 array->is_static = true;
2161 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2162 if(type_qualifiers != 0) {
2163 if(token.type == T_static) {
2164 array->is_static = true;
2168 array->type_qualifiers = type_qualifiers;
2170 if(token.type == '*' && look_ahead(1)->type == ']') {
2171 array->is_variable = true;
2173 } else if(token.type != ']') {
2174 array->size = parse_assignment_expression();
2179 return (construct_type_t*) array;
2182 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2186 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2188 declaration_t *parameters = parse_parameters(&type->function);
2189 if(declaration != NULL) {
2190 declaration->context.declarations = parameters;
2193 construct_function_type_t *construct_function_type =
2194 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2195 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2196 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2197 construct_function_type->function_type = type;
2201 return (construct_type_t*) construct_function_type;
2204 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2205 bool may_be_abstract)
2207 /* construct a single linked list of construct_type_t's which describe
2208 * how to construct the final declarator type */
2209 construct_type_t *first = NULL;
2210 construct_type_t *last = NULL;
2213 while(token.type == '*') {
2214 construct_type_t *type = parse_pointer_declarator();
2225 /* TODO: find out if this is correct */
2228 construct_type_t *inner_types = NULL;
2230 switch(token.type) {
2232 if(declaration == NULL) {
2233 parse_error("no identifier expected in typename");
2235 declaration->symbol = token.v.symbol;
2236 declaration->source_position = token.source_position;
2242 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2248 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2249 /* avoid a loop in the outermost scope, because eat_statement doesn't
2251 if(token.type == '}' && current_function == NULL) {
2259 construct_type_t *p = last;
2262 construct_type_t *type;
2263 switch(token.type) {
2265 type = parse_function_declarator(declaration);
2268 type = parse_array_declarator();
2271 goto declarator_finished;
2274 /* insert in the middle of the list (behind p) */
2276 type->next = p->next;
2287 declarator_finished:
2290 /* append inner_types at the end of the list, we don't to set last anymore
2291 * as it's not needed anymore */
2293 assert(first == NULL);
2294 first = inner_types;
2296 last->next = inner_types;
2302 static type_t *construct_declarator_type(construct_type_t *construct_list,
2305 construct_type_t *iter = construct_list;
2306 for( ; iter != NULL; iter = iter->next) {
2307 switch(iter->type) {
2308 case CONSTRUCT_INVALID:
2309 panic("invalid type construction found");
2310 case CONSTRUCT_FUNCTION: {
2311 construct_function_type_t *construct_function_type
2312 = (construct_function_type_t*) iter;
2314 type_t *function_type = construct_function_type->function_type;
2316 function_type->function.return_type = type;
2318 type = function_type;
2322 case CONSTRUCT_POINTER: {
2323 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2324 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2325 pointer_type->pointer.points_to = type;
2326 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2328 type = pointer_type;
2332 case CONSTRUCT_ARRAY: {
2333 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2334 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2336 array_type->base.qualifiers = parsed_array->type_qualifiers;
2337 array_type->array.element_type = type;
2338 array_type->array.is_static = parsed_array->is_static;
2339 array_type->array.is_variable = parsed_array->is_variable;
2340 array_type->array.size = parsed_array->size;
2347 type_t *hashed_type = typehash_insert(type);
2348 if(hashed_type != type) {
2349 /* the function type was constructed earlier freeing it here will
2350 * destroy other types... */
2351 if(iter->type != CONSTRUCT_FUNCTION) {
2361 static declaration_t *parse_declarator(
2362 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2364 type_t *type = specifiers->type;
2365 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2366 declaration->storage_class = specifiers->storage_class;
2367 declaration->decl_modifiers = specifiers->decl_modifiers;
2368 declaration->is_inline = specifiers->is_inline;
2370 construct_type_t *construct_type
2371 = parse_inner_declarator(declaration, may_be_abstract);
2372 declaration->type = construct_declarator_type(construct_type, type);
2374 if(construct_type != NULL) {
2375 obstack_free(&temp_obst, construct_type);
2381 static type_t *parse_abstract_declarator(type_t *base_type)
2383 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2385 type_t *result = construct_declarator_type(construct_type, base_type);
2386 if(construct_type != NULL) {
2387 obstack_free(&temp_obst, construct_type);
2393 static declaration_t *record_declaration(declaration_t *declaration)
2395 assert(declaration->parent_context == NULL);
2396 assert(context != NULL);
2398 symbol_t *symbol = declaration->symbol;
2399 if(symbol != NULL) {
2400 declaration_t *alias = environment_push(declaration);
2401 if(alias != declaration)
2404 declaration->parent_context = context;
2407 if(last_declaration != NULL) {
2408 last_declaration->next = declaration;
2410 context->declarations = declaration;
2412 last_declaration = declaration;
2417 static void parser_error_multiple_definition(declaration_t *declaration,
2418 const source_position_t source_position)
2420 parser_print_error_prefix_pos(source_position);
2421 fprintf(stderr, "multiple definition of symbol '%s'\n",
2422 declaration->symbol->string);
2423 parser_print_error_prefix_pos(declaration->source_position);
2424 fprintf(stderr, "this is the location of the previous definition.\n");
2427 static bool is_declaration_specifier(const token_t *token,
2428 bool only_type_specifiers)
2430 switch(token->type) {
2434 return is_typedef_symbol(token->v.symbol);
2436 case T___extension__:
2439 return !only_type_specifiers;
2446 static void parse_init_declarator_rest(declaration_t *declaration)
2450 type_t *orig_type = declaration->type;
2451 type_t *type = NULL;
2452 if(orig_type != NULL)
2453 type = skip_typeref(orig_type);
2455 if(declaration->init.initializer != NULL) {
2456 parser_error_multiple_definition(declaration, token.source_position);
2459 initializer_t *initializer = parse_initializer(type);
2461 /* § 6.7.5 (22) array intializers for arrays with unknown size determine
2462 * the array type size */
2463 if(type != NULL && is_type_array(type) && initializer != NULL) {
2464 array_type_t *array_type = &type->array;
2466 if(array_type->size == NULL) {
2467 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2469 cnst->base.datatype = type_size_t;
2471 switch (initializer->type) {
2472 case INITIALIZER_LIST: {
2473 initializer_list_t *const list = &initializer->list;
2474 cnst->conste.v.int_value = list->len;
2478 case INITIALIZER_STRING: {
2479 initializer_string_t *const string = &initializer->string;
2480 cnst->conste.v.int_value = strlen(string->string) + 1;
2484 case INITIALIZER_WIDE_STRING: {
2485 initializer_wide_string_t *const string = &initializer->wide_string;
2486 cnst->conste.v.int_value = string->string.size;
2491 panic("invalid initializer type");
2494 array_type->size = cnst;
2498 if(type != NULL && is_type_function(type)) {
2499 parser_print_error_prefix_pos(declaration->source_position);
2500 fprintf(stderr, "initializers not allowed for function types at "
2501 "declator '%s' (type ", declaration->symbol->string);
2502 print_type_quoted(orig_type);
2503 fprintf(stderr, ")\n");
2505 declaration->init.initializer = initializer;
2509 /* parse rest of a declaration without any declarator */
2510 static void parse_anonymous_declaration_rest(
2511 const declaration_specifiers_t *specifiers,
2512 parsed_declaration_func finished_declaration)
2516 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2518 declaration->type = specifiers->type;
2519 declaration->storage_class = specifiers->storage_class;
2520 declaration->source_position = specifiers->source_position;
2522 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2523 parse_warning_pos(declaration->source_position,
2524 "useless storage class in empty declaration");
2527 type_t *type = declaration->type;
2528 switch (type->type) {
2529 case TYPE_COMPOUND_STRUCT:
2530 case TYPE_COMPOUND_UNION: {
2531 const compound_type_t *compound_type = &type->compound;
2532 if (compound_type->declaration->symbol == NULL) {
2533 parse_warning_pos(declaration->source_position,
2534 "unnamed struct/union that defines no instances");
2543 parse_warning_pos(declaration->source_position,
2544 "empty declaration");
2548 finished_declaration(declaration);
2551 static void parse_declaration_rest(declaration_t *ndeclaration,
2552 const declaration_specifiers_t *specifiers,
2553 parsed_declaration_func finished_declaration)
2556 declaration_t *declaration = finished_declaration(ndeclaration);
2558 type_t *orig_type = declaration->type;
2559 type_t *type = skip_typeref(orig_type);
2561 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2562 parser_print_warning_prefix_pos(declaration->source_position);
2563 fprintf(stderr, "variable '%s' declared 'inline'\n",
2564 declaration->symbol->string);
2567 if(token.type == '=') {
2568 parse_init_declarator_rest(declaration);
2571 if(token.type != ',')
2575 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2580 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2582 /* TODO: check that it was actually a parameter that gets a type */
2584 /* we should have a declaration for the parameter in the current
2586 return record_declaration(declaration);
2589 static void parse_declaration(parsed_declaration_func finished_declaration)
2591 declaration_specifiers_t specifiers;
2592 memset(&specifiers, 0, sizeof(specifiers));
2593 parse_declaration_specifiers(&specifiers);
2595 if(token.type == ';') {
2596 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2598 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2599 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2603 static void parse_kr_declaration_list(declaration_t *declaration)
2605 type_t *type = skip_typeref(declaration->type);
2606 if(!is_type_function(type))
2609 if(!type->function.kr_style_parameters)
2612 /* push function parameters */
2613 int top = environment_top();
2614 context_t *last_context = context;
2615 set_context(&declaration->context);
2617 declaration_t *parameter = declaration->context.declarations;
2618 for( ; parameter != NULL; parameter = parameter->next) {
2619 environment_push(parameter);
2622 /* parse declaration list */
2623 while(is_declaration_specifier(&token, false)) {
2624 parse_declaration(finished_kr_declaration);
2627 /* pop function parameters */
2628 assert(context == &declaration->context);
2629 set_context(last_context);
2630 environment_pop_to(top);
2632 /* update function type */
2633 type_t *new_type = duplicate_type(type);
2634 new_type->function.kr_style_parameters = false;
2636 function_parameter_t *parameters = NULL;
2637 function_parameter_t *last_parameter = NULL;
2639 declaration_t *parameter_declaration = declaration->context.declarations;
2640 for( ; parameter_declaration != NULL;
2641 parameter_declaration = parameter_declaration->next) {
2642 type_t *parameter_type = parameter_declaration->type;
2643 if(parameter_type == NULL) {
2645 parser_print_error_prefix();
2646 fprintf(stderr, "no type specified for function parameter '%s'\n",
2647 parameter_declaration->symbol->string);
2649 parser_print_warning_prefix();
2650 fprintf(stderr, "no type specified for function parameter '%s', "
2651 "using int\n", parameter_declaration->symbol->string);
2652 parameter_type = type_int;
2653 parameter_declaration->type = parameter_type;
2657 semantic_parameter(parameter_declaration);
2658 parameter_type = parameter_declaration->type;
2660 function_parameter_t *function_parameter
2661 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2662 memset(function_parameter, 0, sizeof(function_parameter[0]));
2664 function_parameter->type = parameter_type;
2665 if(last_parameter != NULL) {
2666 last_parameter->next = function_parameter;
2668 parameters = function_parameter;
2670 last_parameter = function_parameter;
2672 new_type->function.parameters = parameters;
2674 type = typehash_insert(new_type);
2675 if(type != new_type) {
2676 obstack_free(type_obst, new_type);
2679 declaration->type = type;
2682 static void parse_external_declaration(void)
2684 /* function-definitions and declarations both start with declaration
2686 declaration_specifiers_t specifiers;
2687 memset(&specifiers, 0, sizeof(specifiers));
2688 parse_declaration_specifiers(&specifiers);
2690 /* must be a declaration */
2691 if(token.type == ';') {
2692 parse_anonymous_declaration_rest(&specifiers, record_declaration);
2696 /* declarator is common to both function-definitions and declarations */
2697 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2699 /* must be a declaration */
2700 if(token.type == ',' || token.type == '=' || token.type == ';') {
2701 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2705 /* must be a function definition */
2706 parse_kr_declaration_list(ndeclaration);
2708 if(token.type != '{') {
2709 parse_error_expected("while parsing function definition", '{', 0);
2714 type_t *type = ndeclaration->type;
2720 /* note that we don't skip typerefs: the standard doesn't allow them here
2721 * (so we can't use is_type_function here) */
2722 if(type->type != TYPE_FUNCTION) {
2723 parser_print_error_prefix();
2724 fprintf(stderr, "declarator '");
2725 print_type_ext(type, ndeclaration->symbol, NULL);
2726 fprintf(stderr, "' has a body but is not a function type.\n");
2731 /* § 6.7.5.3 (14) a function definition with () means no
2732 * parameters (and not unspecified parameters) */
2733 if(type->function.unspecified_parameters) {
2734 type_t *duplicate = duplicate_type(type);
2735 duplicate->function.unspecified_parameters = false;
2737 type = typehash_insert(duplicate);
2738 if(type != duplicate) {
2739 obstack_free(type_obst, duplicate);
2741 ndeclaration->type = type;
2744 declaration_t *declaration = record_declaration(ndeclaration);
2745 if(ndeclaration != declaration) {
2746 memcpy(&declaration->context, &ndeclaration->context,
2747 sizeof(declaration->context));
2749 type = skip_typeref(declaration->type);
2751 /* push function parameters and switch context */
2752 int top = environment_top();
2753 context_t *last_context = context;
2754 set_context(&declaration->context);
2756 declaration_t *parameter = declaration->context.declarations;
2757 for( ; parameter != NULL; parameter = parameter->next) {
2758 environment_push(parameter);
2761 if(declaration->init.statement != NULL) {
2762 parser_error_multiple_definition(declaration, token.source_position);
2764 goto end_of_parse_external_declaration;
2766 /* parse function body */
2767 int label_stack_top = label_top();
2768 declaration_t *old_current_function = current_function;
2769 current_function = declaration;
2771 declaration->init.statement = parse_compound_statement();
2773 assert(current_function == declaration);
2774 current_function = old_current_function;
2775 label_pop_to(label_stack_top);
2778 end_of_parse_external_declaration:
2779 assert(context == &declaration->context);
2780 set_context(last_context);
2781 environment_pop_to(top);
2784 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2787 if(token.type == ':') {
2789 parse_constant_expression();
2790 /* TODO (bitfields) */
2792 declaration_t *declaration = parse_declarator(specifiers, /*may_be_abstract=*/true);
2794 /* TODO: check constraints for struct declarations */
2795 /* TODO: check for doubled fields */
2796 record_declaration(declaration);
2798 if(token.type == ':') {
2800 parse_constant_expression();
2801 /* TODO (bitfields) */
2805 if(token.type != ',')
2812 static void parse_compound_type_entries(void)
2816 while(token.type != '}' && token.type != T_EOF) {
2817 declaration_specifiers_t specifiers;
2818 memset(&specifiers, 0, sizeof(specifiers));
2819 parse_declaration_specifiers(&specifiers);
2821 parse_struct_declarators(&specifiers);
2823 if(token.type == T_EOF) {
2824 parse_error("EOF while parsing struct");
2829 static type_t *parse_typename(void)
2831 declaration_specifiers_t specifiers;
2832 memset(&specifiers, 0, sizeof(specifiers));
2833 parse_declaration_specifiers(&specifiers);
2834 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2835 /* TODO: improve error message, user does probably not know what a
2836 * storage class is...
2838 parse_error("typename may not have a storage class");
2841 type_t *result = parse_abstract_declarator(specifiers.type);
2849 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2850 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2851 expression_t *left);
2853 typedef struct expression_parser_function_t expression_parser_function_t;
2854 struct expression_parser_function_t {
2855 unsigned precedence;
2856 parse_expression_function parser;
2857 unsigned infix_precedence;
2858 parse_expression_infix_function infix_parser;
2861 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2863 static expression_t *create_invalid_expression(void)
2865 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2866 expression->base.source_position = token.source_position;
2870 static expression_t *expected_expression_error(void)
2872 parser_print_error_prefix();
2873 fprintf(stderr, "expected expression, got token ");
2874 print_token(stderr, &token);
2875 fprintf(stderr, "\n");
2879 return create_invalid_expression();
2882 static expression_t *parse_string_const(void)
2884 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2885 cnst->base.datatype = type_string;
2886 cnst->string.value = parse_string_literals();
2891 static expression_t *parse_wide_string_const(void)
2893 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2894 cnst->base.datatype = type_wchar_t_ptr;
2895 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2900 static expression_t *parse_int_const(void)
2902 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2903 cnst->base.datatype = token.datatype;
2904 cnst->conste.v.int_value = token.v.intvalue;
2911 static expression_t *parse_float_const(void)
2913 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2914 cnst->base.datatype = token.datatype;
2915 cnst->conste.v.float_value = token.v.floatvalue;
2922 static declaration_t *create_implicit_function(symbol_t *symbol,
2923 const source_position_t source_position)
2925 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2926 ntype->function.return_type = type_int;
2927 ntype->function.unspecified_parameters = true;
2929 type_t *type = typehash_insert(ntype);
2934 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2936 declaration->storage_class = STORAGE_CLASS_EXTERN;
2937 declaration->type = type;
2938 declaration->symbol = symbol;
2939 declaration->source_position = source_position;
2941 /* prepend the implicit definition to the global context
2942 * this is safe since the symbol wasn't declared as anything else yet
2944 assert(symbol->declaration == NULL);
2946 context_t *last_context = context;
2947 context = global_context;
2949 environment_push(declaration);
2950 declaration->next = context->declarations;
2951 context->declarations = declaration;
2953 context = last_context;
2958 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
2960 function_parameter_t *parameter
2961 = obstack_alloc(type_obst, sizeof(parameter[0]));
2962 memset(parameter, 0, sizeof(parameter[0]));
2963 parameter->type = argument_type;
2965 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2966 type->function.return_type = return_type;
2967 type->function.parameters = parameter;
2969 type_t *result = typehash_insert(type);
2970 if(result != type) {
2977 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2979 switch(symbol->ID) {
2980 case T___builtin_alloca:
2981 return make_function_1_type(type_void_ptr, type_size_t);
2982 case T___builtin_nan:
2983 return make_function_1_type(type_double, type_string);
2984 case T___builtin_nanf:
2985 return make_function_1_type(type_float, type_string);
2986 case T___builtin_nand:
2987 return make_function_1_type(type_long_double, type_string);
2988 case T___builtin_va_end:
2989 return make_function_1_type(type_void, type_valist);
2991 panic("not implemented builtin symbol found");
2996 * performs automatic type cast as described in § 6.3.2.1
2998 static type_t *automatic_type_conversion(type_t *orig_type)
3000 if(orig_type == NULL)
3003 type_t *type = skip_typeref(orig_type);
3004 if(is_type_array(type)) {
3005 array_type_t *array_type = &type->array;
3006 type_t *element_type = array_type->element_type;
3007 unsigned qualifiers = array_type->type.qualifiers;
3009 return make_pointer_type(element_type, qualifiers);
3012 if(is_type_function(type)) {
3013 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3020 * reverts the automatic casts of array to pointer types and function
3021 * to function-pointer types as defined § 6.3.2.1
3023 type_t *revert_automatic_type_conversion(const expression_t *expression)
3025 if(expression->base.datatype == NULL)
3028 switch(expression->type) {
3029 case EXPR_REFERENCE: {
3030 const reference_expression_t *ref = &expression->reference;
3031 return ref->declaration->type;
3034 const select_expression_t *select = &expression->select;
3035 return select->compound_entry->type;
3037 case EXPR_UNARY_DEREFERENCE: {
3038 expression_t *value = expression->unary.value;
3039 type_t *type = skip_typeref(value->base.datatype);
3040 pointer_type_t *pointer_type = &type->pointer;
3042 return pointer_type->points_to;
3044 case EXPR_BUILTIN_SYMBOL: {
3045 const builtin_symbol_expression_t *builtin
3046 = &expression->builtin_symbol;
3047 return get_builtin_symbol_type(builtin->symbol);
3049 case EXPR_ARRAY_ACCESS: {
3050 const array_access_expression_t *array_access
3051 = &expression->array_access;
3052 const expression_t *array_ref = array_access->array_ref;
3053 type_t *type_left = skip_typeref(array_ref->base.datatype);
3054 assert(is_type_pointer(type_left));
3055 pointer_type_t *pointer_type = &type_left->pointer;
3056 return pointer_type->points_to;
3063 return expression->base.datatype;
3066 static expression_t *parse_reference(void)
3068 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3070 reference_expression_t *ref = &expression->reference;
3071 ref->symbol = token.v.symbol;
3073 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3075 source_position_t source_position = token.source_position;
3078 if(declaration == NULL) {
3080 /* an implicitly defined function */
3081 if(token.type == '(') {
3082 parser_print_prefix_pos(token.source_position);
3083 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
3084 ref->symbol->string);
3086 declaration = create_implicit_function(ref->symbol,
3091 parser_print_error_prefix();
3092 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
3097 type_t *type = declaration->type;
3098 /* we always do the auto-type conversions; the & and sizeof parser contains
3099 * code to revert this! */
3100 type = automatic_type_conversion(type);
3102 ref->declaration = declaration;
3103 ref->expression.datatype = type;
3108 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3112 /* TODO check if explicit cast is allowed and issue warnings/errors */
3115 static expression_t *parse_cast(void)
3117 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3119 cast->base.source_position = token.source_position;
3121 type_t *type = parse_typename();
3124 expression_t *value = parse_sub_expression(20);
3126 check_cast_allowed(value, type);
3128 cast->base.datatype = type;
3129 cast->unary.value = value;
3134 static expression_t *parse_statement_expression(void)
3136 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3138 statement_t *statement = parse_compound_statement();
3139 expression->statement.statement = statement;
3140 if(statement == NULL) {
3145 assert(statement->type == STATEMENT_COMPOUND);
3146 compound_statement_t *compound_statement = &statement->compound;
3148 /* find last statement and use it's type */
3149 const statement_t *last_statement = NULL;
3150 const statement_t *iter = compound_statement->statements;
3151 for( ; iter != NULL; iter = iter->base.next) {
3152 last_statement = iter;
3155 if(last_statement->type == STATEMENT_EXPRESSION) {
3156 const expression_statement_t *expression_statement
3157 = &last_statement->expression;
3158 expression->base.datatype
3159 = expression_statement->expression->base.datatype;
3161 expression->base.datatype = type_void;
3169 static expression_t *parse_brace_expression(void)
3173 switch(token.type) {
3175 /* gcc extension: a stement expression */
3176 return parse_statement_expression();
3180 return parse_cast();
3182 if(is_typedef_symbol(token.v.symbol)) {
3183 return parse_cast();
3187 expression_t *result = parse_expression();
3193 static expression_t *parse_function_keyword(void)
3198 if (current_function == NULL) {
3199 parse_error("'__func__' used outside of a function");
3202 string_literal_expression_t *expression
3203 = allocate_ast_zero(sizeof(expression[0]));
3205 expression->expression.type = EXPR_FUNCTION;
3206 expression->expression.datatype = type_string;
3207 expression->value = current_function->symbol->string;
3209 return (expression_t*) expression;
3212 static expression_t *parse_pretty_function_keyword(void)
3214 eat(T___PRETTY_FUNCTION__);
3217 if (current_function == NULL) {
3218 parse_error("'__PRETTY_FUNCTION__' used outside of a function");
3221 string_literal_expression_t *expression
3222 = allocate_ast_zero(sizeof(expression[0]));
3224 expression->expression.type = EXPR_PRETTY_FUNCTION;
3225 expression->expression.datatype = type_string;
3226 expression->value = current_function->symbol->string;
3228 return (expression_t*) expression;
3231 static designator_t *parse_designator(void)
3233 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3235 if(token.type != T_IDENTIFIER) {
3236 parse_error_expected("while parsing member designator",
3241 result->symbol = token.v.symbol;
3244 designator_t *last_designator = result;
3246 if(token.type == '.') {
3248 if(token.type != T_IDENTIFIER) {
3249 parse_error_expected("while parsing member designator",
3254 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3255 designator->symbol = token.v.symbol;
3258 last_designator->next = designator;
3259 last_designator = designator;
3262 if(token.type == '[') {
3264 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3265 designator->array_access = parse_expression();
3266 if(designator->array_access == NULL) {
3272 last_designator->next = designator;
3273 last_designator = designator;
3282 static expression_t *parse_offsetof(void)
3284 eat(T___builtin_offsetof);
3286 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3287 expression->base.datatype = type_size_t;
3290 expression->offsetofe.type = parse_typename();
3292 expression->offsetofe.designator = parse_designator();
3298 static expression_t *parse_va_start(void)
3300 eat(T___builtin_va_start);
3302 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3305 expression->va_starte.ap = parse_assignment_expression();
3307 expression_t *const expr = parse_assignment_expression();
3308 if (expr->type == EXPR_REFERENCE) {
3309 declaration_t *const decl = expr->reference.declaration;
3310 if (decl->parent_context == ¤t_function->context &&
3311 decl->next == NULL) {
3312 expression->va_starte.parameter = decl;
3317 parser_print_error_prefix_pos(expr->base.source_position);
3318 fprintf(stderr, "second argument of 'va_start' must be last parameter "
3319 "of the current function\n");
3321 return create_invalid_expression();
3324 static expression_t *parse_va_arg(void)
3326 eat(T___builtin_va_arg);
3328 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3331 expression->va_arge.ap = parse_assignment_expression();
3333 expression->base.datatype = parse_typename();
3339 static expression_t *parse_builtin_symbol(void)
3341 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3343 symbol_t *symbol = token.v.symbol;
3345 expression->builtin_symbol.symbol = symbol;
3348 type_t *type = get_builtin_symbol_type(symbol);
3349 type = automatic_type_conversion(type);
3351 expression->base.datatype = type;
3355 static expression_t *parse_compare_builtin(void)
3357 expression_t *expression;
3359 switch(token.type) {
3360 case T___builtin_isgreater:
3361 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3363 case T___builtin_isgreaterequal:
3364 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3366 case T___builtin_isless:
3367 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3369 case T___builtin_islessequal:
3370 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3372 case T___builtin_islessgreater:
3373 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3375 case T___builtin_isunordered:
3376 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3379 panic("invalid compare builtin found");
3385 expression->binary.left = parse_assignment_expression();
3387 expression->binary.right = parse_assignment_expression();
3390 type_t *orig_type_left = expression->binary.left->base.datatype;
3391 type_t *orig_type_right = expression->binary.right->base.datatype;
3392 if(orig_type_left == NULL || orig_type_right == NULL)
3395 type_t *type_left = skip_typeref(orig_type_left);
3396 type_t *type_right = skip_typeref(orig_type_right);
3397 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3398 type_error_incompatible("invalid operands in comparison",
3399 token.source_position, type_left, type_right);
3401 semantic_comparison(&expression->binary);
3407 static expression_t *parse_primary_expression(void)
3409 switch(token.type) {
3411 return parse_int_const();
3412 case T_FLOATINGPOINT:
3413 return parse_float_const();
3414 case T_STRING_LITERAL: /* TODO merge */
3415 return parse_string_const();
3416 case T_WIDE_STRING_LITERAL:
3417 return parse_wide_string_const();
3419 return parse_reference();
3420 case T___FUNCTION__:
3422 return parse_function_keyword();
3423 case T___PRETTY_FUNCTION__:
3424 return parse_pretty_function_keyword();
3425 case T___builtin_offsetof:
3426 return parse_offsetof();
3427 case T___builtin_va_start:
3428 return parse_va_start();
3429 case T___builtin_va_arg:
3430 return parse_va_arg();
3431 case T___builtin_nanf:
3432 case T___builtin_alloca:
3433 case T___builtin_expect:
3434 case T___builtin_va_end:
3435 return parse_builtin_symbol();
3436 case T___builtin_isgreater:
3437 case T___builtin_isgreaterequal:
3438 case T___builtin_isless:
3439 case T___builtin_islessequal:
3440 case T___builtin_islessgreater:
3441 case T___builtin_isunordered:
3442 return parse_compare_builtin();
3445 return parse_brace_expression();
3448 parser_print_error_prefix();
3449 fprintf(stderr, "unexpected token ");
3450 print_token(stderr, &token);
3451 fprintf(stderr, "\n");
3454 return create_invalid_expression();
3457 static expression_t *parse_array_expression(unsigned precedence,
3464 expression_t *inside = parse_expression();
3466 array_access_expression_t *array_access
3467 = allocate_ast_zero(sizeof(array_access[0]));
3469 array_access->expression.type = EXPR_ARRAY_ACCESS;
3471 type_t *type_left = left->base.datatype;
3472 type_t *type_inside = inside->base.datatype;
3473 type_t *return_type = NULL;
3475 if(type_left != NULL && type_inside != NULL) {
3476 type_left = skip_typeref(type_left);
3477 type_inside = skip_typeref(type_inside);
3479 if(is_type_pointer(type_left)) {
3480 pointer_type_t *pointer = &type_left->pointer;
3481 return_type = pointer->points_to;
3482 array_access->array_ref = left;
3483 array_access->index = inside;
3484 } else if(is_type_pointer(type_inside)) {
3485 pointer_type_t *pointer = &type_inside->pointer;
3486 return_type = pointer->points_to;
3487 array_access->array_ref = inside;
3488 array_access->index = left;
3489 array_access->flipped = true;
3491 parser_print_error_prefix();
3492 fprintf(stderr, "array access on object with non-pointer types ");
3493 print_type_quoted(type_left);
3494 fprintf(stderr, ", ");
3495 print_type_quoted(type_inside);
3496 fprintf(stderr, "\n");
3499 array_access->array_ref = left;
3500 array_access->index = inside;
3503 if(token.type != ']') {
3504 parse_error_expected("Problem while parsing array access", ']', 0);
3505 return (expression_t*) array_access;
3509 return_type = automatic_type_conversion(return_type);
3510 array_access->expression.datatype = return_type;
3512 return (expression_t*) array_access;
3515 static expression_t *parse_sizeof(unsigned precedence)
3519 sizeof_expression_t *sizeof_expression
3520 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3521 sizeof_expression->expression.type = EXPR_SIZEOF;
3522 sizeof_expression->expression.datatype = type_size_t;
3524 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3526 sizeof_expression->type = parse_typename();
3529 expression_t *expression = parse_sub_expression(precedence);
3530 expression->base.datatype = revert_automatic_type_conversion(expression);
3532 sizeof_expression->type = expression->base.datatype;
3533 sizeof_expression->size_expression = expression;
3536 return (expression_t*) sizeof_expression;
3539 static expression_t *parse_select_expression(unsigned precedence,
3540 expression_t *compound)
3543 assert(token.type == '.' || token.type == T_MINUSGREATER);
3545 bool is_pointer = (token.type == T_MINUSGREATER);
3548 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3549 select->select.compound = compound;
3551 if(token.type != T_IDENTIFIER) {
3552 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3555 symbol_t *symbol = token.v.symbol;
3556 select->select.symbol = symbol;
3559 type_t *orig_type = compound->base.datatype;
3560 if(orig_type == NULL)
3561 return create_invalid_expression();
3563 type_t *type = skip_typeref(orig_type);
3565 type_t *type_left = type;
3567 if(type->type != TYPE_POINTER) {
3568 parser_print_error_prefix();
3569 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
3570 print_type_quoted(orig_type);
3571 fputc('\n', stderr);
3572 return create_invalid_expression();
3574 pointer_type_t *pointer_type = &type->pointer;
3575 type_left = pointer_type->points_to;
3577 type_left = skip_typeref(type_left);
3579 if(type_left->type != TYPE_COMPOUND_STRUCT
3580 && type_left->type != TYPE_COMPOUND_UNION) {
3581 parser_print_error_prefix();
3582 fprintf(stderr, "request for member '%s' in something not a struct or "
3583 "union, but ", symbol->string);
3584 print_type_quoted(type_left);
3585 fputc('\n', stderr);
3586 return create_invalid_expression();
3589 compound_type_t *compound_type = &type_left->compound;
3590 declaration_t *declaration = compound_type->declaration;
3592 if(!declaration->init.is_defined) {
3593 parser_print_error_prefix();
3594 fprintf(stderr, "request for member '%s' of incomplete type ",
3596 print_type_quoted(type_left);
3597 fputc('\n', stderr);
3598 return create_invalid_expression();
3601 declaration_t *iter = declaration->context.declarations;
3602 for( ; iter != NULL; iter = iter->next) {
3603 if(iter->symbol == symbol) {
3608 parser_print_error_prefix();
3609 print_type_quoted(type_left);
3610 fprintf(stderr, " has no member named '%s'\n", symbol->string);
3611 return create_invalid_expression();
3614 /* we always do the auto-type conversions; the & and sizeof parser contains
3615 * code to revert this! */
3616 type_t *expression_type = automatic_type_conversion(iter->type);
3618 select->select.compound_entry = iter;
3619 select->base.datatype = expression_type;
3623 static expression_t *parse_call_expression(unsigned precedence,
3624 expression_t *expression)
3627 expression_t *result = allocate_expression_zero(EXPR_CALL);
3629 call_expression_t *call = &result->call;
3630 call->function = expression;
3632 function_type_t *function_type = NULL;
3633 type_t *orig_type = expression->base.datatype;
3634 if(orig_type != NULL) {
3635 type_t *type = skip_typeref(orig_type);
3637 if(is_type_pointer(type)) {
3638 pointer_type_t *pointer_type = &type->pointer;
3640 type = skip_typeref(pointer_type->points_to);
3642 if (is_type_function(type)) {
3643 function_type = &type->function;
3644 call->expression.datatype = function_type->return_type;
3647 if(function_type == NULL) {
3648 parser_print_error_prefix();
3649 fputs("called object '", stderr);
3650 print_expression(expression);
3651 fputs("' (type ", stderr);
3652 print_type_quoted(orig_type);
3653 fputs(") is not a pointer to a function\n", stderr);
3655 function_type = NULL;
3656 call->expression.datatype = NULL;
3660 /* parse arguments */
3663 if(token.type != ')') {
3664 call_argument_t *last_argument = NULL;
3667 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3669 argument->expression = parse_assignment_expression();
3670 if(last_argument == NULL) {
3671 call->arguments = argument;
3673 last_argument->next = argument;
3675 last_argument = argument;
3677 if(token.type != ',')
3684 if(function_type != NULL) {
3685 function_parameter_t *parameter = function_type->parameters;
3686 call_argument_t *argument = call->arguments;
3687 for( ; parameter != NULL && argument != NULL;
3688 parameter = parameter->next, argument = argument->next) {
3689 type_t *expected_type = parameter->type;
3690 /* TODO report context in error messages */
3691 argument->expression = create_implicit_cast(argument->expression,
3694 /* too few parameters */
3695 if(parameter != NULL) {
3696 parser_print_error_prefix();
3697 fprintf(stderr, "too few arguments to function '");
3698 print_expression(expression);
3699 fprintf(stderr, "'\n");
3700 } else if(argument != NULL) {
3701 /* too many parameters */
3702 if(!function_type->variadic
3703 && !function_type->unspecified_parameters) {
3704 parser_print_error_prefix();
3705 fprintf(stderr, "too many arguments to function '");
3706 print_expression(expression);
3707 fprintf(stderr, "'\n");
3709 /* do default promotion */
3710 for( ; argument != NULL; argument = argument->next) {
3711 type_t *type = argument->expression->base.datatype;
3716 type = skip_typeref(type);
3717 if(is_type_integer(type)) {
3718 type = promote_integer(type);
3719 } else if(type == type_float) {
3723 argument->expression
3724 = create_implicit_cast(argument->expression, type);
3733 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3735 static bool same_compound_type(const type_t *type1, const type_t *type2)
3737 if(!is_type_compound(type1))
3739 if(type1->type != type2->type)
3742 const compound_type_t *compound1 = &type1->compound;
3743 const compound_type_t *compound2 = &type2->compound;
3745 return compound1->declaration == compound2->declaration;
3748 static expression_t *parse_conditional_expression(unsigned precedence,
3749 expression_t *expression)
3753 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3755 conditional_expression_t *conditional = &result->conditional;
3756 conditional->condition = expression;
3759 type_t *condition_type_orig = expression->base.datatype;
3760 if(condition_type_orig != NULL) {
3761 type_t *condition_type = skip_typeref(condition_type_orig);
3762 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3763 type_error("expected a scalar type in conditional condition",
3764 expression->base.source_position, condition_type_orig);
3768 expression_t *true_expression = parse_expression();
3770 expression_t *false_expression = parse_sub_expression(precedence);
3772 conditional->true_expression = true_expression;
3773 conditional->false_expression = false_expression;
3775 type_t *orig_true_type = true_expression->base.datatype;
3776 type_t *orig_false_type = false_expression->base.datatype;
3777 if(orig_true_type == NULL || orig_false_type == NULL)
3780 type_t *true_type = skip_typeref(orig_true_type);
3781 type_t *false_type = skip_typeref(orig_false_type);
3784 type_t *result_type = NULL;
3785 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3786 result_type = semantic_arithmetic(true_type, false_type);
3788 true_expression = create_implicit_cast(true_expression, result_type);
3789 false_expression = create_implicit_cast(false_expression, result_type);
3791 conditional->true_expression = true_expression;
3792 conditional->false_expression = false_expression;
3793 conditional->expression.datatype = result_type;
3794 } else if (same_compound_type(true_type, false_type)
3795 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3796 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
3797 /* just take 1 of the 2 types */
3798 result_type = true_type;
3799 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3800 && pointers_compatible(true_type, false_type)) {
3802 result_type = true_type;
3805 type_error_incompatible("while parsing conditional",
3806 expression->base.source_position, true_type,
3810 conditional->expression.datatype = result_type;
3814 static expression_t *parse_extension(unsigned precedence)
3816 eat(T___extension__);
3818 /* TODO enable extensions */
3820 return parse_sub_expression(precedence);
3823 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3825 eat(T___builtin_classify_type);
3827 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3828 result->base.datatype = type_int;
3831 expression_t *expression = parse_sub_expression(precedence);
3833 result->classify_type.type_expression = expression;
3838 static void semantic_incdec(unary_expression_t *expression)
3840 type_t *orig_type = expression->value->base.datatype;
3841 if(orig_type == NULL)
3844 type_t *type = skip_typeref(orig_type);
3845 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3846 /* TODO: improve error message */
3847 parser_print_error_prefix();
3848 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3852 expression->expression.datatype = orig_type;
3855 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3857 type_t *orig_type = expression->value->base.datatype;
3858 if(orig_type == NULL)
3861 type_t *type = skip_typeref(orig_type);
3862 if(!is_type_arithmetic(type)) {
3863 /* TODO: improve error message */
3864 parser_print_error_prefix();
3865 fprintf(stderr, "operation needs an arithmetic type\n");
3869 expression->expression.datatype = orig_type;
3872 static void semantic_unexpr_scalar(unary_expression_t *expression)
3874 type_t *orig_type = expression->value->base.datatype;
3875 if(orig_type == NULL)
3878 type_t *type = skip_typeref(orig_type);
3879 if (!is_type_scalar(type)) {
3880 parse_error("operand of ! must be of scalar type\n");
3884 expression->expression.datatype = orig_type;
3887 static void semantic_unexpr_integer(unary_expression_t *expression)
3889 type_t *orig_type = expression->value->base.datatype;
3890 if(orig_type == NULL)
3893 type_t *type = skip_typeref(orig_type);
3894 if (!is_type_integer(type)) {
3895 parse_error("operand of ~ must be of integer type\n");
3899 expression->expression.datatype = orig_type;
3902 static void semantic_dereference(unary_expression_t *expression)
3904 type_t *orig_type = expression->value->base.datatype;
3905 if(orig_type == NULL)
3908 type_t *type = skip_typeref(orig_type);
3909 if(!is_type_pointer(type)) {
3910 parser_print_error_prefix();
3911 fputs("Unary '*' needs pointer or arrray type, but type ", stderr);
3912 print_type_quoted(orig_type);
3913 fputs(" given.\n", stderr);
3917 pointer_type_t *pointer_type = &type->pointer;
3918 type_t *result_type = pointer_type->points_to;
3920 result_type = automatic_type_conversion(result_type);
3921 expression->expression.datatype = result_type;
3924 static void semantic_take_addr(unary_expression_t *expression)
3926 expression_t *value = expression->value;
3927 value->base.datatype = revert_automatic_type_conversion(value);
3929 type_t *orig_type = value->base.datatype;
3930 if(orig_type == NULL)
3933 if(value->type == EXPR_REFERENCE) {
3934 reference_expression_t *reference = (reference_expression_t*) value;
3935 declaration_t *declaration = reference->declaration;
3936 if(declaration != NULL) {
3937 declaration->address_taken = 1;
3941 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3944 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3945 static expression_t *parse_##unexpression_type(unsigned precedence) \
3949 expression_t *unary_expression \
3950 = allocate_expression_zero(unexpression_type); \
3951 unary_expression->unary.value = parse_sub_expression(precedence); \
3953 sfunc(&unary_expression->unary); \
3955 return unary_expression; \
3958 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
3959 semantic_unexpr_arithmetic)
3960 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
3961 semantic_unexpr_arithmetic)
3962 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
3963 semantic_unexpr_scalar)
3964 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
3965 semantic_dereference)
3966 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
3968 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
3969 semantic_unexpr_integer)
3970 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
3972 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
3975 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3977 static expression_t *parse_##unexpression_type(unsigned precedence, \
3978 expression_t *left) \
3980 (void) precedence; \
3983 expression_t *unary_expression \
3984 = allocate_expression_zero(unexpression_type); \
3985 unary_expression->unary.value = left; \
3987 sfunc(&unary_expression->unary); \
3989 return unary_expression; \
3992 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
3993 EXPR_UNARY_POSTFIX_INCREMENT,
3995 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
3996 EXPR_UNARY_POSTFIX_DECREMENT,
3999 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4001 /* TODO: handle complex + imaginary types */
4003 /* § 6.3.1.8 Usual arithmetic conversions */
4004 if(type_left == type_long_double || type_right == type_long_double) {
4005 return type_long_double;
4006 } else if(type_left == type_double || type_right == type_double) {
4008 } else if(type_left == type_float || type_right == type_float) {
4012 type_right = promote_integer(type_right);
4013 type_left = promote_integer(type_left);
4015 if(type_left == type_right)
4018 bool signed_left = is_type_signed(type_left);
4019 bool signed_right = is_type_signed(type_right);
4020 int rank_left = get_rank(type_left);
4021 int rank_right = get_rank(type_right);
4022 if(rank_left < rank_right) {
4023 if(signed_left == signed_right || !signed_right) {
4029 if(signed_left == signed_right || !signed_left) {
4037 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4039 expression_t *left = expression->left;
4040 expression_t *right = expression->right;
4041 type_t *orig_type_left = left->base.datatype;
4042 type_t *orig_type_right = right->base.datatype;
4044 if(orig_type_left == NULL || orig_type_right == NULL)
4047 type_t *type_left = skip_typeref(orig_type_left);
4048 type_t *type_right = skip_typeref(orig_type_right);
4050 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4051 /* TODO: improve error message */
4052 parser_print_error_prefix();
4053 fprintf(stderr, "operation needs arithmetic types\n");
4057 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4058 expression->left = create_implicit_cast(left, arithmetic_type);
4059 expression->right = create_implicit_cast(right, arithmetic_type);
4060 expression->expression.datatype = arithmetic_type;
4063 static void semantic_shift_op(binary_expression_t *expression)
4065 expression_t *left = expression->left;
4066 expression_t *right = expression->right;
4067 type_t *orig_type_left = left->base.datatype;
4068 type_t *orig_type_right = right->base.datatype;
4070 if(orig_type_left == NULL || orig_type_right == NULL)
4073 type_t *type_left = skip_typeref(orig_type_left);
4074 type_t *type_right = skip_typeref(orig_type_right);
4076 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4077 /* TODO: improve error message */
4078 parser_print_error_prefix();
4079 fprintf(stderr, "operation needs integer types\n");
4083 type_left = promote_integer(type_left);
4084 type_right = promote_integer(type_right);
4086 expression->left = create_implicit_cast(left, type_left);
4087 expression->right = create_implicit_cast(right, type_right);
4088 expression->expression.datatype = type_left;
4091 static void semantic_add(binary_expression_t *expression)
4093 expression_t *left = expression->left;
4094 expression_t *right = expression->right;
4095 type_t *orig_type_left = left->base.datatype;
4096 type_t *orig_type_right = right->base.datatype;
4098 if(orig_type_left == NULL || orig_type_right == NULL)
4101 type_t *type_left = skip_typeref(orig_type_left);
4102 type_t *type_right = skip_typeref(orig_type_right);
4105 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4106 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4107 expression->left = create_implicit_cast(left, arithmetic_type);
4108 expression->right = create_implicit_cast(right, arithmetic_type);
4109 expression->expression.datatype = arithmetic_type;
4111 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4112 expression->expression.datatype = type_left;
4113 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4114 expression->expression.datatype = type_right;
4116 parser_print_error_prefix();
4117 fprintf(stderr, "invalid operands to binary + (");
4118 print_type_quoted(orig_type_left);
4119 fprintf(stderr, ", ");
4120 print_type_quoted(orig_type_right);
4121 fprintf(stderr, ")\n");
4125 static void semantic_sub(binary_expression_t *expression)
4127 expression_t *left = expression->left;
4128 expression_t *right = expression->right;
4129 type_t *orig_type_left = left->base.datatype;
4130 type_t *orig_type_right = right->base.datatype;
4132 if(orig_type_left == NULL || orig_type_right == NULL)
4135 type_t *type_left = skip_typeref(orig_type_left);
4136 type_t *type_right = skip_typeref(orig_type_right);
4139 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4140 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4141 expression->left = create_implicit_cast(left, arithmetic_type);
4142 expression->right = create_implicit_cast(right, arithmetic_type);
4143 expression->expression.datatype = arithmetic_type;
4145 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4146 expression->expression.datatype = type_left;
4147 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4148 if(!pointers_compatible(type_left, type_right)) {
4149 parser_print_error_prefix();
4150 fprintf(stderr, "pointers to incompatible objects to binary - (");
4151 print_type_quoted(orig_type_left);
4152 fprintf(stderr, ", ");
4153 print_type_quoted(orig_type_right);
4154 fprintf(stderr, ")\n");
4156 expression->expression.datatype = type_ptrdiff_t;
4159 parser_print_error_prefix();
4160 fprintf(stderr, "invalid operands to binary - (");
4161 print_type_quoted(orig_type_left);
4162 fprintf(stderr, ", ");
4163 print_type_quoted(orig_type_right);
4164 fprintf(stderr, ")\n");
4168 static void semantic_comparison(binary_expression_t *expression)
4170 expression_t *left = expression->left;
4171 expression_t *right = expression->right;
4172 type_t *orig_type_left = left->base.datatype;
4173 type_t *orig_type_right = right->base.datatype;
4175 if(orig_type_left == NULL || orig_type_right == NULL)
4178 type_t *type_left = skip_typeref(orig_type_left);
4179 type_t *type_right = skip_typeref(orig_type_right);
4181 /* TODO non-arithmetic types */
4182 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4183 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4184 expression->left = create_implicit_cast(left, arithmetic_type);
4185 expression->right = create_implicit_cast(right, arithmetic_type);
4186 expression->expression.datatype = arithmetic_type;
4187 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4188 /* TODO check compatibility */
4189 } else if (is_type_pointer(type_left)) {
4190 expression->right = create_implicit_cast(right, type_left);
4191 } else if (is_type_pointer(type_right)) {
4192 expression->left = create_implicit_cast(left, type_right);
4194 type_error_incompatible("invalid operands in comparison",
4195 token.source_position, type_left, type_right);
4197 expression->expression.datatype = type_int;
4200 static void semantic_arithmetic_assign(binary_expression_t *expression)
4202 expression_t *left = expression->left;
4203 expression_t *right = expression->right;
4204 type_t *orig_type_left = left->base.datatype;
4205 type_t *orig_type_right = right->base.datatype;
4207 if(orig_type_left == NULL || orig_type_right == NULL)
4210 type_t *type_left = skip_typeref(orig_type_left);
4211 type_t *type_right = skip_typeref(orig_type_right);
4213 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4214 /* TODO: improve error message */
4215 parser_print_error_prefix();
4216 fprintf(stderr, "operation needs arithmetic types\n");
4220 /* combined instructions are tricky. We can't create an implicit cast on
4221 * the left side, because we need the uncasted form for the store.
4222 * The ast2firm pass has to know that left_type must be right_type
4223 * for the arithmeitc operation and create a cast by itself */
4224 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4225 expression->right = create_implicit_cast(right, arithmetic_type);
4226 expression->expression.datatype = type_left;
4229 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4231 expression_t *left = expression->left;
4232 expression_t *right = expression->right;
4233 type_t *orig_type_left = left->base.datatype;
4234 type_t *orig_type_right = right->base.datatype;
4236 if(orig_type_left == NULL || orig_type_right == NULL)
4239 type_t *type_left = skip_typeref(orig_type_left);
4240 type_t *type_right = skip_typeref(orig_type_right);
4242 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4243 /* combined instructions are tricky. We can't create an implicit cast on
4244 * the left side, because we need the uncasted form for the store.
4245 * The ast2firm pass has to know that left_type must be right_type
4246 * for the arithmeitc operation and create a cast by itself */
4247 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4248 expression->right = create_implicit_cast(right, arithmetic_type);
4249 expression->expression.datatype = type_left;
4250 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4251 expression->expression.datatype = type_left;
4253 parser_print_error_prefix();
4254 fputs("Incompatible types ", stderr);
4255 print_type_quoted(orig_type_left);
4256 fputs(" and ", stderr);
4257 print_type_quoted(orig_type_right);
4258 fputs(" in assignment\n", stderr);
4263 static void semantic_logical_op(binary_expression_t *expression)
4265 expression_t *left = expression->left;
4266 expression_t *right = expression->right;
4267 type_t *orig_type_left = left->base.datatype;
4268 type_t *orig_type_right = right->base.datatype;
4270 if(orig_type_left == NULL || orig_type_right == NULL)
4273 type_t *type_left = skip_typeref(orig_type_left);
4274 type_t *type_right = skip_typeref(orig_type_right);
4276 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4277 /* TODO: improve error message */
4278 parser_print_error_prefix();
4279 fprintf(stderr, "operation needs scalar types\n");
4283 expression->expression.datatype = type_int;
4286 static bool has_const_fields(type_t *type)
4293 static void semantic_binexpr_assign(binary_expression_t *expression)
4295 expression_t *left = expression->left;
4296 type_t *orig_type_left = left->base.datatype;
4298 if(orig_type_left == NULL)
4301 type_t *type_left = revert_automatic_type_conversion(left);
4302 type_left = skip_typeref(orig_type_left);
4304 /* must be a modifiable lvalue */
4305 if (is_type_array(type_left)) {
4306 parser_print_error_prefix();
4307 fprintf(stderr, "Cannot assign to arrays ('");
4308 print_expression(left);
4309 fprintf(stderr, "')\n");
4312 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4313 parser_print_error_prefix();
4314 fprintf(stderr, "assignment to readonly location '");
4315 print_expression(left);
4316 fprintf(stderr, "' (type ");
4317 print_type_quoted(orig_type_left);
4318 fprintf(stderr, ")\n");
4321 if(is_type_incomplete(type_left)) {
4322 parser_print_error_prefix();
4323 fprintf(stderr, "left-hand side of assignment '");
4324 print_expression(left);
4325 fprintf(stderr, "' has incomplete type ");
4326 print_type_quoted(orig_type_left);
4327 fprintf(stderr, "\n");
4330 if(is_type_compound(type_left) && has_const_fields(type_left)) {
4331 parser_print_error_prefix();
4332 fprintf(stderr, "can't assign to '");
4333 print_expression(left);
4334 fprintf(stderr, "' because compound type ");
4335 print_type_quoted(orig_type_left);
4336 fprintf(stderr, " has readonly fields\n");
4340 semantic_assign(orig_type_left, &expression->right, "assignment");
4342 expression->expression.datatype = orig_type_left;
4345 static void semantic_comma(binary_expression_t *expression)
4347 expression->expression.datatype = expression->right->base.datatype;
4350 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4351 static expression_t *parse_##binexpression_type(unsigned precedence, \
4352 expression_t *left) \
4356 expression_t *right = parse_sub_expression(precedence + lr); \
4358 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4359 binexpr->binary.left = left; \
4360 binexpr->binary.right = right; \
4361 sfunc(&binexpr->binary); \
4366 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4367 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4368 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4369 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4370 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4371 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4372 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4373 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4374 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4376 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4377 semantic_comparison, 1)
4378 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4379 semantic_comparison, 1)
4380 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4381 semantic_comparison, 1)
4382 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4383 semantic_comparison, 1)
4385 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4386 semantic_binexpr_arithmetic, 1)
4387 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4388 semantic_binexpr_arithmetic, 1)
4389 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4390 semantic_binexpr_arithmetic, 1)
4391 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4392 semantic_logical_op, 1)
4393 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4394 semantic_logical_op, 1)
4395 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4396 semantic_shift_op, 1)
4397 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4398 semantic_shift_op, 1)
4399 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4400 semantic_arithmetic_addsubb_assign, 0)
4401 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4402 semantic_arithmetic_addsubb_assign, 0)
4403 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4404 semantic_arithmetic_assign, 0)
4405 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4406 semantic_arithmetic_assign, 0)
4407 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4408 semantic_arithmetic_assign, 0)
4409 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4410 semantic_arithmetic_assign, 0)
4411 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4412 semantic_arithmetic_assign, 0)
4413 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4414 semantic_arithmetic_assign, 0)
4415 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4416 semantic_arithmetic_assign, 0)
4417 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4418 semantic_arithmetic_assign, 0)
4420 static expression_t *parse_sub_expression(unsigned precedence)
4422 if(token.type < 0) {
4423 return expected_expression_error();
4426 expression_parser_function_t *parser
4427 = &expression_parsers[token.type];
4428 source_position_t source_position = token.source_position;
4431 if(parser->parser != NULL) {
4432 left = parser->parser(parser->precedence);
4434 left = parse_primary_expression();
4436 assert(left != NULL);
4437 left->base.source_position = source_position;
4440 if(token.type < 0) {
4441 return expected_expression_error();
4444 parser = &expression_parsers[token.type];
4445 if(parser->infix_parser == NULL)
4447 if(parser->infix_precedence < precedence)
4450 left = parser->infix_parser(parser->infix_precedence, left);
4452 assert(left != NULL);
4453 assert(left->type != EXPR_UNKNOWN);
4454 left->base.source_position = source_position;
4460 static expression_t *parse_expression(void)
4462 return parse_sub_expression(1);
4467 static void register_expression_parser(parse_expression_function parser,
4468 int token_type, unsigned precedence)
4470 expression_parser_function_t *entry = &expression_parsers[token_type];
4472 if(entry->parser != NULL) {
4473 fprintf(stderr, "for token ");
4474 print_token_type(stderr, (token_type_t) token_type);
4475 fprintf(stderr, "\n");
4476 panic("trying to register multiple expression parsers for a token");
4478 entry->parser = parser;
4479 entry->precedence = precedence;
4482 static void register_infix_parser(parse_expression_infix_function parser,
4483 int token_type, unsigned precedence)
4485 expression_parser_function_t *entry = &expression_parsers[token_type];
4487 if(entry->infix_parser != NULL) {
4488 fprintf(stderr, "for token ");
4489 print_token_type(stderr, (token_type_t) token_type);
4490 fprintf(stderr, "\n");
4491 panic("trying to register multiple infix expression parsers for a "
4494 entry->infix_parser = parser;
4495 entry->infix_precedence = precedence;
4498 static void init_expression_parsers(void)
4500 memset(&expression_parsers, 0, sizeof(expression_parsers));
4502 register_infix_parser(parse_array_expression, '[', 30);
4503 register_infix_parser(parse_call_expression, '(', 30);
4504 register_infix_parser(parse_select_expression, '.', 30);
4505 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4506 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4508 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4511 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4512 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4513 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4514 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4515 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4516 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4517 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4518 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4519 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4520 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4521 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4522 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4523 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4524 T_EXCLAMATIONMARKEQUAL, 13);
4525 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4526 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4527 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4528 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4529 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4530 register_infix_parser(parse_conditional_expression, '?', 7);
4531 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4532 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4533 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4534 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4535 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4536 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4537 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4538 T_LESSLESSEQUAL, 2);
4539 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4540 T_GREATERGREATEREQUAL, 2);
4541 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4543 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4545 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4548 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4550 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4551 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4552 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4553 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4554 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4555 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4556 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4558 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4560 register_expression_parser(parse_sizeof, T_sizeof, 25);
4561 register_expression_parser(parse_extension, T___extension__, 25);
4562 register_expression_parser(parse_builtin_classify_type,
4563 T___builtin_classify_type, 25);
4566 static asm_constraint_t *parse_asm_constraints(void)
4568 asm_constraint_t *result = NULL;
4569 asm_constraint_t *last = NULL;
4571 while(token.type == T_STRING_LITERAL || token.type == '[') {
4572 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4573 memset(constraint, 0, sizeof(constraint[0]));
4575 if(token.type == '[') {
4577 if(token.type != T_IDENTIFIER) {
4578 parse_error_expected("while parsing asm constraint",
4582 constraint->symbol = token.v.symbol;
4587 constraint->constraints = parse_string_literals();
4589 constraint->expression = parse_expression();
4593 last->next = constraint;
4595 result = constraint;
4599 if(token.type != ',')
4607 static asm_clobber_t *parse_asm_clobbers(void)
4609 asm_clobber_t *result = NULL;
4610 asm_clobber_t *last = NULL;
4612 while(token.type == T_STRING_LITERAL) {
4613 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4614 clobber->clobber = parse_string_literals();
4617 last->next = clobber;
4623 if(token.type != ',')
4631 static statement_t *parse_asm_statement(void)
4635 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4636 statement->base.source_position = token.source_position;
4638 asm_statement_t *asm_statement = &statement->asms;
4640 if(token.type == T_volatile) {
4642 asm_statement->is_volatile = true;
4646 asm_statement->asm_text = parse_string_literals();
4648 if(token.type != ':')
4652 asm_statement->inputs = parse_asm_constraints();
4653 if(token.type != ':')
4657 asm_statement->outputs = parse_asm_constraints();
4658 if(token.type != ':')
4662 asm_statement->clobbers = parse_asm_clobbers();
4670 static statement_t *parse_case_statement(void)
4674 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4676 statement->base.source_position = token.source_position;
4677 statement->case_label.expression = parse_expression();
4680 statement->case_label.label_statement = parse_statement();
4685 static statement_t *parse_default_statement(void)
4689 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4691 statement->base.source_position = token.source_position;
4694 statement->label.label_statement = parse_statement();
4699 static declaration_t *get_label(symbol_t *symbol)
4701 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4702 assert(current_function != NULL);
4703 /* if we found a label in the same function, then we already created the
4705 if(candidate != NULL
4706 && candidate->parent_context == ¤t_function->context) {
4710 /* otherwise we need to create a new one */
4711 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4712 declaration->namespc = NAMESPACE_LABEL;
4713 declaration->symbol = symbol;
4715 label_push(declaration);
4720 static statement_t *parse_label_statement(void)
4722 assert(token.type == T_IDENTIFIER);
4723 symbol_t *symbol = token.v.symbol;
4726 declaration_t *label = get_label(symbol);
4728 /* if source position is already set then the label is defined twice,
4729 * otherwise it was just mentioned in a goto so far */
4730 if(label->source_position.input_name != NULL) {
4731 parser_print_error_prefix();
4732 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
4733 parser_print_error_prefix_pos(label->source_position);
4734 fprintf(stderr, "previous definition of '%s' was here\n",
4737 label->source_position = token.source_position;
4740 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4742 label_statement->statement.type = STATEMENT_LABEL;
4743 label_statement->statement.source_position = token.source_position;
4744 label_statement->label = label;
4748 if(token.type == '}') {
4749 parse_error("label at end of compound statement");
4750 return (statement_t*) label_statement;
4752 label_statement->label_statement = parse_statement();
4755 return (statement_t*) label_statement;
4758 static statement_t *parse_if(void)
4762 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4763 statement->statement.type = STATEMENT_IF;
4764 statement->statement.source_position = token.source_position;
4767 statement->condition = parse_expression();
4770 statement->true_statement = parse_statement();
4771 if(token.type == T_else) {
4773 statement->false_statement = parse_statement();
4776 return (statement_t*) statement;
4779 static statement_t *parse_switch(void)
4783 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4784 statement->statement.type = STATEMENT_SWITCH;
4785 statement->statement.source_position = token.source_position;
4788 statement->expression = parse_expression();
4790 statement->body = parse_statement();
4792 return (statement_t*) statement;
4795 static statement_t *parse_while(void)
4799 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4800 statement->statement.type = STATEMENT_WHILE;
4801 statement->statement.source_position = token.source_position;
4804 statement->condition = parse_expression();
4806 statement->body = parse_statement();
4808 return (statement_t*) statement;
4811 static statement_t *parse_do(void)
4815 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4816 statement->statement.type = STATEMENT_DO_WHILE;
4817 statement->statement.source_position = token.source_position;
4819 statement->body = parse_statement();
4822 statement->condition = parse_expression();
4826 return (statement_t*) statement;
4829 static statement_t *parse_for(void)
4833 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4834 statement->statement.type = STATEMENT_FOR;
4835 statement->statement.source_position = token.source_position;
4839 int top = environment_top();
4840 context_t *last_context = context;
4841 set_context(&statement->context);
4843 if(token.type != ';') {
4844 if(is_declaration_specifier(&token, false)) {
4845 parse_declaration(record_declaration);
4847 statement->initialisation = parse_expression();
4854 if(token.type != ';') {
4855 statement->condition = parse_expression();
4858 if(token.type != ')') {
4859 statement->step = parse_expression();
4862 statement->body = parse_statement();
4864 assert(context == &statement->context);
4865 set_context(last_context);
4866 environment_pop_to(top);
4868 return (statement_t*) statement;
4871 static statement_t *parse_goto(void)
4875 if(token.type != T_IDENTIFIER) {
4876 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4880 symbol_t *symbol = token.v.symbol;
4883 declaration_t *label = get_label(symbol);
4885 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4887 statement->statement.type = STATEMENT_GOTO;
4888 statement->statement.source_position = token.source_position;
4890 statement->label = label;
4894 return (statement_t*) statement;
4897 static statement_t *parse_continue(void)
4902 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4903 statement->type = STATEMENT_CONTINUE;
4904 statement->base.source_position = token.source_position;
4909 static statement_t *parse_break(void)
4914 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4915 statement->type = STATEMENT_BREAK;
4916 statement->base.source_position = token.source_position;
4921 static statement_t *parse_return(void)
4925 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4927 statement->statement.type = STATEMENT_RETURN;
4928 statement->statement.source_position = token.source_position;
4930 assert(is_type_function(current_function->type));
4931 function_type_t *function_type = ¤t_function->type->function;
4932 type_t *return_type = function_type->return_type;
4934 expression_t *return_value = NULL;
4935 if(token.type != ';') {
4936 return_value = parse_expression();
4940 if(return_type == NULL)
4941 return (statement_t*) statement;
4942 if(return_value != NULL && return_value->base.datatype == NULL)
4943 return (statement_t*) statement;
4945 return_type = skip_typeref(return_type);
4947 if(return_value != NULL) {
4948 type_t *return_value_type = skip_typeref(return_value->base.datatype);
4950 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
4951 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
4952 parse_warning("'return' with a value, in function returning void");
4953 return_value = NULL;
4955 if(return_type != NULL) {
4956 semantic_assign(return_type, &return_value, "'return'");
4960 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4961 parse_warning("'return' without value, in function returning "
4965 statement->return_value = return_value;
4967 return (statement_t*) statement;
4970 static statement_t *parse_declaration_statement(void)
4972 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
4974 statement->base.source_position = token.source_position;
4976 declaration_t *before = last_declaration;
4977 parse_declaration(record_declaration);
4979 if(before == NULL) {
4980 statement->declaration.declarations_begin = context->declarations;
4982 statement->declaration.declarations_begin = before->next;
4984 statement->declaration.declarations_end = last_declaration;
4989 static statement_t *parse_expression_statement(void)
4991 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
4993 statement->base.source_position = token.source_position;
4994 statement->expression.expression = parse_expression();
5001 static statement_t *parse_statement(void)
5003 statement_t *statement = NULL;
5005 /* declaration or statement */
5006 switch(token.type) {
5008 statement = parse_asm_statement();
5012 statement = parse_case_statement();
5016 statement = parse_default_statement();
5020 statement = parse_compound_statement();
5024 statement = parse_if();
5028 statement = parse_switch();
5032 statement = parse_while();
5036 statement = parse_do();
5040 statement = parse_for();
5044 statement = parse_goto();
5048 statement = parse_continue();
5052 statement = parse_break();
5056 statement = parse_return();
5065 if(look_ahead(1)->type == ':') {
5066 statement = parse_label_statement();
5070 if(is_typedef_symbol(token.v.symbol)) {
5071 statement = parse_declaration_statement();
5075 statement = parse_expression_statement();
5078 case T___extension__:
5079 /* this can be a prefix to a declaration or an expression statement */
5080 /* we simply eat it now and parse the rest with tail recursion */
5083 } while(token.type == T___extension__);
5084 statement = parse_statement();
5088 statement = parse_declaration_statement();
5092 statement = parse_expression_statement();
5096 assert(statement == NULL
5097 || statement->base.source_position.input_name != NULL);
5102 static statement_t *parse_compound_statement(void)
5104 compound_statement_t *compound_statement
5105 = allocate_ast_zero(sizeof(compound_statement[0]));
5106 compound_statement->statement.type = STATEMENT_COMPOUND;
5107 compound_statement->statement.source_position = token.source_position;
5111 int top = environment_top();
5112 context_t *last_context = context;
5113 set_context(&compound_statement->context);
5115 statement_t *last_statement = NULL;
5117 while(token.type != '}' && token.type != T_EOF) {
5118 statement_t *statement = parse_statement();
5119 if(statement == NULL)
5122 if(last_statement != NULL) {
5123 last_statement->base.next = statement;
5125 compound_statement->statements = statement;
5128 while(statement->base.next != NULL)
5129 statement = statement->base.next;
5131 last_statement = statement;
5134 if(token.type != '}') {
5135 parser_print_error_prefix_pos(
5136 compound_statement->statement.source_position);
5137 fprintf(stderr, "end of file while looking for closing '}'\n");
5141 assert(context == &compound_statement->context);
5142 set_context(last_context);
5143 environment_pop_to(top);
5145 return (statement_t*) compound_statement;
5148 static void initialize_builtins(void)
5150 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5151 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5152 type_size_t = make_global_typedef("__SIZE_TYPE__",
5153 make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_NONE));
5154 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__",
5155 make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_NONE));
5158 static translation_unit_t *parse_translation_unit(void)
5160 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5162 assert(global_context == NULL);
5163 global_context = &unit->context;
5165 assert(context == NULL);
5166 set_context(&unit->context);
5168 initialize_builtins();
5170 while(token.type != T_EOF) {
5171 parse_external_declaration();
5174 assert(context == &unit->context);
5176 last_declaration = NULL;
5178 assert(global_context == &unit->context);
5179 global_context = NULL;
5184 translation_unit_t *parse(void)
5186 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5187 label_stack = NEW_ARR_F(stack_entry_t, 0);
5188 found_error = false;
5190 type_set_output(stderr);
5191 ast_set_output(stderr);
5193 lookahead_bufpos = 0;
5194 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5197 translation_unit_t *unit = parse_translation_unit();
5199 DEL_ARR_F(environment_stack);
5200 DEL_ARR_F(label_stack);
5208 void init_parser(void)
5210 init_expression_parsers();
5211 obstack_init(&temp_obst);
5213 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
5214 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE,
5215 TYPE_QUALIFIER_NONE);
5216 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE,
5217 TYPE_QUALIFIER_NONE);
5218 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_NONE);
5219 type_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_NONE);
5220 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
5221 type_void_ptr = make_pointer_type(type_void, TYPE_QUALIFIER_NONE);
5222 type_string = make_pointer_type(type_char, TYPE_QUALIFIER_NONE);
5224 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5225 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5228 void exit_parser(void)
5230 obstack_free(&temp_obst, NULL);