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;
36 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
39 static token_t lookahead_buffer[MAX_LOOKAHEAD];
40 static int lookahead_bufpos;
41 static stack_entry_t *environment_stack = NULL;
42 static stack_entry_t *label_stack = NULL;
43 static context_t *global_context = NULL;
44 static context_t *context = NULL;
45 static declaration_t *last_declaration = NULL;
46 static declaration_t *current_function = NULL;
47 static struct obstack temp_obst;
48 static bool found_error;
50 static type_t *type_int = NULL;
51 static type_t *type_long_double = NULL;
52 static type_t *type_double = NULL;
53 static type_t *type_float = NULL;
54 static type_t *type_char = NULL;
55 static type_t *type_string = NULL;
56 static type_t *type_void = NULL;
57 static type_t *type_void_ptr = NULL;
58 static type_t *type_valist = NULL;
60 type_t *type_size_t = NULL;
61 type_t *type_ptrdiff_t = NULL;
62 type_t *type_wchar_t = NULL;
63 type_t *type_wchar_t_ptr = NULL;
65 static statement_t *parse_compound_statement(void);
66 static statement_t *parse_statement(void);
68 static expression_t *parse_sub_expression(unsigned precedence);
69 static expression_t *parse_expression(void);
70 static type_t *parse_typename(void);
72 static void parse_compound_type_entries(void);
73 static declaration_t *parse_declarator(
74 const declaration_specifiers_t *specifiers, bool may_be_abstract);
75 static declaration_t *record_declaration(declaration_t *declaration);
77 static void semantic_comparison(binary_expression_t *expression);
79 #define STORAGE_CLASSES \
86 #define TYPE_QUALIFIERS \
92 #ifdef PROVIDE_COMPLEX
93 #define COMPLEX_SPECIFIERS \
95 #define IMAGINARY_SPECIFIERS \
98 #define COMPLEX_SPECIFIERS
99 #define IMAGINARY_SPECIFIERS
102 #define TYPE_SPECIFIERS \
117 case T___builtin_va_list: \
121 #define DECLARATION_START \
126 #define TYPENAME_START \
130 static void *allocate_ast_zero(size_t size)
132 void *res = allocate_ast(size);
133 memset(res, 0, size);
137 static size_t get_statement_struct_size(statement_type_t type)
139 static const size_t sizes[] = {
140 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
141 [STATEMENT_RETURN] = sizeof(return_statement_t),
142 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
143 [STATEMENT_IF] = sizeof(if_statement_t),
144 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
145 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
146 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
147 [STATEMENT_BREAK] = sizeof(statement_base_t),
148 [STATEMENT_GOTO] = sizeof(goto_statement_t),
149 [STATEMENT_LABEL] = sizeof(label_statement_t),
150 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
151 [STATEMENT_WHILE] = sizeof(while_statement_t),
152 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
153 [STATEMENT_FOR] = sizeof(for_statement_t),
154 [STATEMENT_ASM] = sizeof(asm_statement_t)
156 assert(type <= sizeof(sizes) / sizeof(sizes[0]));
157 assert(sizes[type] != 0);
161 static statement_t *allocate_statement_zero(statement_type_t type)
163 size_t size = get_statement_struct_size(type);
164 statement_t *res = allocate_ast_zero(size);
166 res->base.type = type;
171 static size_t get_expression_struct_size(expression_type_t type)
173 static const size_t sizes[] = {
174 [EXPR_INVALID] = sizeof(expression_base_t),
175 [EXPR_REFERENCE] = sizeof(reference_expression_t),
176 [EXPR_CONST] = sizeof(const_expression_t),
177 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
178 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
179 [EXPR_CALL] = sizeof(call_expression_t),
180 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
181 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
182 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
183 [EXPR_SELECT] = sizeof(select_expression_t),
184 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
185 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
186 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
187 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
188 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
189 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
190 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
191 [EXPR_VA_START] = sizeof(va_start_expression_t),
192 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
193 [EXPR_STATEMENT] = sizeof(statement_expression_t),
195 if(type >= EXPR_UNARY_FIRST && type <= EXPR_UNARY_LAST) {
196 return sizes[EXPR_UNARY_FIRST];
198 if(type >= EXPR_BINARY_FIRST && type <= EXPR_BINARY_LAST) {
199 return sizes[EXPR_BINARY_FIRST];
201 assert(type <= sizeof(sizes) / sizeof(sizes[0]));
202 assert(sizes[type] != 0);
206 static expression_t *allocate_expression_zero(expression_type_t type)
208 size_t size = get_expression_struct_size(type);
209 expression_t *res = allocate_ast_zero(size);
211 res->base.type = type;
215 static size_t get_type_struct_size(type_type_t type)
217 static const size_t sizes[] = {
218 [TYPE_ATOMIC] = sizeof(atomic_type_t),
219 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
220 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
221 [TYPE_ENUM] = sizeof(enum_type_t),
222 [TYPE_FUNCTION] = sizeof(function_type_t),
223 [TYPE_POINTER] = sizeof(pointer_type_t),
224 [TYPE_ARRAY] = sizeof(array_type_t),
225 [TYPE_BUILTIN] = sizeof(builtin_type_t),
226 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
227 [TYPE_TYPEOF] = sizeof(typeof_type_t),
229 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
230 assert(type <= TYPE_TYPEOF);
231 assert(sizes[type] != 0);
235 static type_t *allocate_type_zero(type_type_t type)
237 size_t size = get_type_struct_size(type);
238 type_t *res = obstack_alloc(type_obst, size);
239 memset(res, 0, size);
241 res->base.type = type;
245 static size_t get_initializer_size(initializer_type_t type)
247 static const size_t sizes[] = {
248 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
249 [INITIALIZER_STRING] = sizeof(initializer_string_t),
250 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
251 [INITIALIZER_LIST] = sizeof(initializer_list_t)
253 assert(type < sizeof(sizes) / sizeof(*sizes));
254 assert(sizes[type] != 0);
258 static initializer_t *allocate_initializer(initializer_type_t type)
260 initializer_t *result = allocate_ast_zero(get_initializer_size(type));
266 static void free_type(void *type)
268 obstack_free(type_obst, type);
272 * returns the top element of the environment stack
274 static size_t environment_top(void)
276 return ARR_LEN(environment_stack);
279 static size_t label_top(void)
281 return ARR_LEN(label_stack);
286 static inline void next_token(void)
288 token = lookahead_buffer[lookahead_bufpos];
289 lookahead_buffer[lookahead_bufpos] = lexer_token;
292 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
295 print_token(stderr, &token);
296 fprintf(stderr, "\n");
300 static inline const token_t *look_ahead(int num)
302 assert(num > 0 && num <= MAX_LOOKAHEAD);
303 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
304 return &lookahead_buffer[pos];
307 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
309 static void error(void)
312 #ifdef ABORT_ON_ERROR
317 static void parser_print_prefix_pos(const source_position_t source_position)
319 fputs(source_position.input_name, stderr);
321 fprintf(stderr, "%u", source_position.linenr);
325 static void parser_print_error_prefix_pos(
326 const source_position_t source_position)
328 parser_print_prefix_pos(source_position);
329 fputs("error: ", stderr);
333 static void parser_print_error_prefix(void)
335 parser_print_error_prefix_pos(token.source_position);
338 static void parse_error(const char *message)
340 parser_print_error_prefix();
341 fprintf(stderr, "parse error: %s\n", message);
344 static void parser_print_warning_prefix_pos(
345 const source_position_t source_position)
347 parser_print_prefix_pos(source_position);
348 fputs("warning: ", stderr);
351 static void parser_print_warning_prefix(void)
353 parser_print_warning_prefix_pos(token.source_position);
356 static void parse_warning_pos(const source_position_t source_position,
357 const char *const message)
359 parser_print_prefix_pos(source_position);
360 fprintf(stderr, "warning: %s\n", message);
363 static void parse_warning(const char *message)
365 parse_warning_pos(token.source_position, message);
368 static void parse_error_expected(const char *message, ...)
373 if(message != NULL) {
374 parser_print_error_prefix();
375 fprintf(stderr, "%s\n", message);
377 parser_print_error_prefix();
378 fputs("Parse error: got ", stderr);
379 print_token(stderr, &token);
380 fputs(", expected ", stderr);
382 va_start(args, message);
383 token_type_t token_type = va_arg(args, token_type_t);
384 while(token_type != 0) {
388 fprintf(stderr, ", ");
390 print_token_type(stderr, token_type);
391 token_type = va_arg(args, token_type_t);
394 fprintf(stderr, "\n");
397 static void print_type_quoted(type_t *type)
404 static void type_error(const char *msg, const source_position_t source_position,
407 parser_print_error_prefix_pos(source_position);
408 fprintf(stderr, "%s, but found type ", msg);
409 print_type_quoted(type);
413 static void type_error_incompatible(const char *msg,
414 const source_position_t source_position, type_t *type1, type_t *type2)
416 parser_print_error_prefix_pos(source_position);
417 fprintf(stderr, "%s, incompatible types: ", msg);
418 print_type_quoted(type1);
419 fprintf(stderr, " - ");
420 print_type_quoted(type2);
421 fprintf(stderr, ")\n");
424 static void eat_block(void)
426 if(token.type == '{')
429 while(token.type != '}') {
430 if(token.type == T_EOF)
432 if(token.type == '{') {
441 static void eat_statement(void)
443 while(token.type != ';') {
444 if(token.type == T_EOF)
446 if(token.type == '}')
448 if(token.type == '{') {
457 static void eat_paren(void)
459 if(token.type == '(')
462 while(token.type != ')') {
463 if(token.type == T_EOF)
465 if(token.type == ')' || token.type == ';' || token.type == '}') {
468 if(token.type == '(') {
472 if(token.type == '{') {
481 #define expect(expected) \
482 if(UNLIKELY(token.type != (expected))) { \
483 parse_error_expected(NULL, (expected), 0); \
489 #define expect_block(expected) \
490 if(UNLIKELY(token.type != (expected))) { \
491 parse_error_expected(NULL, (expected), 0); \
497 #define expect_void(expected) \
498 if(UNLIKELY(token.type != (expected))) { \
499 parse_error_expected(NULL, (expected), 0); \
505 static void set_context(context_t *new_context)
507 context = new_context;
509 last_declaration = new_context->declarations;
510 if(last_declaration != NULL) {
511 while(last_declaration->next != NULL) {
512 last_declaration = last_declaration->next;
518 * called when we find a 2nd declarator for an identifier we already have a
521 static bool is_compatible_declaration(declaration_t *declaration,
522 declaration_t *previous)
524 /* happens for K&R style function parameters */
525 if(previous->type == NULL) {
526 previous->type = declaration->type;
530 type_t *type1 = skip_typeref(declaration->type);
531 type_t *type2 = skip_typeref(previous->type);
533 return types_compatible(type1, type2);
536 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
538 declaration_t *declaration = symbol->declaration;
539 for( ; declaration != NULL; declaration = declaration->symbol_next) {
540 if(declaration->namespc == namespc)
547 static const char *get_namespace_prefix(namespace_t namespc)
550 case NAMESPACE_NORMAL:
552 case NAMESPACE_UNION:
554 case NAMESPACE_STRUCT:
558 case NAMESPACE_LABEL:
561 panic("invalid namespace found");
565 * pushs an environment_entry on the environment stack and links the
566 * corresponding symbol to the new entry
568 static declaration_t *stack_push(stack_entry_t **stack_ptr,
569 declaration_t *declaration,
570 context_t *parent_context)
572 symbol_t *symbol = declaration->symbol;
573 namespace_t namespc = (namespace_t)declaration->namespc;
575 /* a declaration should be only pushed once */
576 declaration->parent_context = parent_context;
578 declaration_t *previous_declaration = get_declaration(symbol, namespc);
579 assert(declaration != previous_declaration);
580 if(previous_declaration != NULL
581 && previous_declaration->parent_context == context) {
582 if(!is_compatible_declaration(declaration, previous_declaration)) {
583 parser_print_error_prefix_pos(declaration->source_position);
584 fprintf(stderr, "definition of symbol '%s%s' with type ",
585 get_namespace_prefix(namespc), symbol->string);
586 print_type_quoted(declaration->type);
588 parser_print_error_prefix_pos(
589 previous_declaration->source_position);
590 fprintf(stderr, "is incompatible with previous declaration "
592 print_type_quoted(previous_declaration->type);
595 unsigned old_storage_class = previous_declaration->storage_class;
596 unsigned new_storage_class = declaration->storage_class;
597 if (current_function == NULL) {
598 if (old_storage_class != STORAGE_CLASS_STATIC &&
599 new_storage_class == STORAGE_CLASS_STATIC) {
600 parser_print_error_prefix_pos(declaration->source_position);
602 "static declaration of '%s' follows non-static declaration\n",
604 parser_print_error_prefix_pos(previous_declaration->source_position);
605 fprintf(stderr, "previous declaration of '%s' was here\n",
608 if (old_storage_class == STORAGE_CLASS_EXTERN) {
609 if (new_storage_class == STORAGE_CLASS_NONE) {
610 previous_declaration->storage_class = STORAGE_CLASS_NONE;
613 parser_print_warning_prefix_pos(declaration->source_position);
614 fprintf(stderr, "redundant declaration for '%s'\n",
616 parser_print_warning_prefix_pos(previous_declaration->source_position);
617 fprintf(stderr, "previous declaration of '%s' was here\n",
622 if (old_storage_class == STORAGE_CLASS_EXTERN &&
623 new_storage_class == STORAGE_CLASS_EXTERN) {
624 parser_print_warning_prefix_pos(declaration->source_position);
625 fprintf(stderr, "redundant extern declaration for '%s'\n",
627 parser_print_warning_prefix_pos(previous_declaration->source_position);
628 fprintf(stderr, "previous declaration of '%s' was here\n",
631 parser_print_error_prefix_pos(declaration->source_position);
632 if (old_storage_class == new_storage_class) {
633 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
635 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
637 parser_print_error_prefix_pos(previous_declaration->source_position);
638 fprintf(stderr, "previous declaration of '%s' was here\n",
643 return previous_declaration;
646 /* remember old declaration */
648 entry.symbol = symbol;
649 entry.old_declaration = symbol->declaration;
650 entry.namespc = (unsigned short) namespc;
651 ARR_APP1(stack_entry_t, *stack_ptr, entry);
653 /* replace/add declaration into declaration list of the symbol */
654 if(symbol->declaration == NULL) {
655 symbol->declaration = declaration;
657 declaration_t *iter_last = NULL;
658 declaration_t *iter = symbol->declaration;
659 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
660 /* replace an entry? */
661 if(iter->namespc == namespc) {
662 if(iter_last == NULL) {
663 symbol->declaration = declaration;
665 iter_last->symbol_next = declaration;
667 declaration->symbol_next = iter->symbol_next;
672 assert(iter_last->symbol_next == NULL);
673 iter_last->symbol_next = declaration;
680 static declaration_t *environment_push(declaration_t *declaration)
682 assert(declaration->source_position.input_name != NULL);
683 return stack_push(&environment_stack, declaration, context);
686 static declaration_t *label_push(declaration_t *declaration)
688 return stack_push(&label_stack, declaration, ¤t_function->context);
692 * pops symbols from the environment stack until @p new_top is the top element
694 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
696 stack_entry_t *stack = *stack_ptr;
697 size_t top = ARR_LEN(stack);
700 assert(new_top <= top);
704 for(i = top; i > new_top; --i) {
705 stack_entry_t *entry = &stack[i - 1];
707 declaration_t *old_declaration = entry->old_declaration;
708 symbol_t *symbol = entry->symbol;
709 namespace_t namespc = (namespace_t)entry->namespc;
711 /* replace/remove declaration */
712 declaration_t *declaration = symbol->declaration;
713 assert(declaration != NULL);
714 if(declaration->namespc == namespc) {
715 if(old_declaration == NULL) {
716 symbol->declaration = declaration->symbol_next;
718 symbol->declaration = old_declaration;
721 declaration_t *iter_last = declaration;
722 declaration_t *iter = declaration->symbol_next;
723 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
724 /* replace an entry? */
725 if(iter->namespc == namespc) {
726 assert(iter_last != NULL);
727 iter_last->symbol_next = old_declaration;
728 old_declaration->symbol_next = iter->symbol_next;
732 assert(iter != NULL);
736 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
739 static void environment_pop_to(size_t new_top)
741 stack_pop_to(&environment_stack, new_top);
744 static void label_pop_to(size_t new_top)
746 stack_pop_to(&label_stack, new_top);
750 static int get_rank(const type_t *type)
752 assert(!is_typeref(type));
753 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
754 * and esp. footnote 108). However we can't fold constants (yet), so we
755 * can't decide wether unsigned int is possible, while int always works.
756 * (unsigned int would be preferable when possible... for stuff like
757 * struct { enum { ... } bla : 4; } ) */
758 if(type->type == TYPE_ENUM)
759 return ATOMIC_TYPE_INT;
761 assert(type->type == TYPE_ATOMIC);
762 const atomic_type_t *atomic_type = &type->atomic;
763 atomic_type_type_t atype = atomic_type->atype;
767 static type_t *promote_integer(type_t *type)
769 if(get_rank(type) < ATOMIC_TYPE_INT)
775 static expression_t *create_cast_expression(expression_t *expression,
778 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
780 cast->unary.value = expression;
781 cast->base.datatype = dest_type;
786 static bool is_null_pointer_constant(const expression_t *expression)
788 /* skip void* cast */
789 if(expression->type == EXPR_UNARY_CAST
790 || expression->type == EXPR_UNARY_CAST_IMPLICIT) {
791 expression = expression->unary.value;
794 /* TODO: not correct yet, should be any constant integer expression
795 * which evaluates to 0 */
796 if (expression->type != EXPR_CONST)
799 type_t *const type = skip_typeref(expression->base.datatype);
800 if (!is_type_integer(type))
803 return expression->conste.v.int_value == 0;
806 static expression_t *create_implicit_cast(expression_t *expression,
809 type_t *source_type = expression->base.datatype;
811 if(source_type == NULL)
814 source_type = skip_typeref(source_type);
815 dest_type = skip_typeref(dest_type);
817 if(source_type == dest_type)
820 switch (dest_type->type) {
822 /* TODO warning for implicitly converting to enum */
824 if (source_type->type != TYPE_ATOMIC &&
825 source_type->type != TYPE_ENUM) {
826 panic("casting of non-atomic types not implemented yet");
829 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
830 type_error_incompatible("can't cast types",
831 expression->base.source_position, source_type,
836 return create_cast_expression(expression, dest_type);
839 switch (source_type->type) {
841 if (is_null_pointer_constant(expression)) {
842 return create_cast_expression(expression, dest_type);
847 if (pointers_compatible(source_type, dest_type)) {
848 return create_cast_expression(expression, dest_type);
853 array_type_t *array_type = &source_type->array;
854 pointer_type_t *pointer_type = &dest_type->pointer;
855 if (types_compatible(array_type->element_type,
856 pointer_type->points_to)) {
857 return create_cast_expression(expression, dest_type);
863 panic("casting of non-atomic types not implemented yet");
866 type_error_incompatible("can't implicitly cast types",
867 expression->base.source_position, source_type, dest_type);
871 panic("casting of non-atomic types not implemented yet");
875 /** Implements the rules from § 6.5.16.1 */
876 static void semantic_assign(type_t *orig_type_left, expression_t **right,
879 type_t *orig_type_right = (*right)->base.datatype;
881 if(orig_type_right == NULL)
884 type_t *const type_left = skip_typeref(orig_type_left);
885 type_t *const type_right = skip_typeref(orig_type_right);
887 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
888 (is_type_pointer(type_left) && is_null_pointer_constant(*right)) ||
889 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
890 && is_type_pointer(type_right))) {
891 *right = create_implicit_cast(*right, type_left);
895 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
896 pointer_type_t *pointer_type_left = &type_left->pointer;
897 pointer_type_t *pointer_type_right = &type_right->pointer;
898 type_t *points_to_left = pointer_type_left->points_to;
899 type_t *points_to_right = pointer_type_right->points_to;
901 points_to_left = skip_typeref(points_to_left);
902 points_to_right = skip_typeref(points_to_right);
904 /* the left type has all qualifiers from the right type */
905 unsigned missing_qualifiers
906 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
907 if(missing_qualifiers != 0) {
908 parser_print_error_prefix();
909 fprintf(stderr, "destination type ");
910 print_type_quoted(type_left);
911 fprintf(stderr, " in %s from type ", context);
912 print_type_quoted(type_right);
913 fprintf(stderr, " lacks qualifiers '");
914 print_type_qualifiers(missing_qualifiers);
915 fprintf(stderr, "' in pointed-to type\n");
919 points_to_left = get_unqualified_type(points_to_left);
920 points_to_right = get_unqualified_type(points_to_right);
922 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
923 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
924 && !types_compatible(points_to_left, points_to_right)) {
925 goto incompatible_assign_types;
928 *right = create_implicit_cast(*right, type_left);
932 if (is_type_compound(type_left)
933 && types_compatible(type_left, type_right)) {
934 *right = create_implicit_cast(*right, type_left);
938 incompatible_assign_types:
939 /* TODO: improve error message */
940 parser_print_error_prefix();
941 fprintf(stderr, "incompatible types in %s\n", context);
942 parser_print_error_prefix();
943 print_type_quoted(orig_type_left);
944 fputs(" <- ", stderr);
945 print_type_quoted(orig_type_right);
949 static expression_t *parse_constant_expression(void)
951 /* start parsing at precedence 7 (conditional expression) */
952 return parse_sub_expression(7);
955 static expression_t *parse_assignment_expression(void)
957 /* start parsing at precedence 2 (assignment expression) */
958 return parse_sub_expression(2);
961 static type_t *make_global_typedef(const char *name, type_t *type)
963 symbol_t *symbol = symbol_table_insert(name);
965 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
966 declaration->namespc = NAMESPACE_NORMAL;
967 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
968 declaration->type = type;
969 declaration->symbol = symbol;
970 declaration->source_position = builtin_source_position;
972 record_declaration(declaration);
974 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
975 typedef_type->typedeft.declaration = declaration;
980 static const char *parse_string_literals(void)
982 assert(token.type == T_STRING_LITERAL);
983 const char *result = token.v.string;
987 while(token.type == T_STRING_LITERAL) {
988 result = concat_strings(result, token.v.string);
995 static void parse_attributes(void)
999 case T___attribute__: {
1005 switch(token.type) {
1007 parse_error("EOF while parsing attribute");
1026 if(token.type != T_STRING_LITERAL) {
1027 parse_error_expected("while parsing assembler attribute",
1032 parse_string_literals();
1037 goto attributes_finished;
1041 attributes_finished:
1046 static designator_t *parse_designation(void)
1048 if(token.type != '[' && token.type != '.')
1051 designator_t *result = NULL;
1052 designator_t *last = NULL;
1055 designator_t *designator;
1056 switch(token.type) {
1058 designator = allocate_ast_zero(sizeof(designator[0]));
1060 designator->array_access = parse_constant_expression();
1064 designator = allocate_ast_zero(sizeof(designator[0]));
1066 if(token.type != T_IDENTIFIER) {
1067 parse_error_expected("while parsing designator",
1071 designator->symbol = token.v.symbol;
1079 assert(designator != NULL);
1081 last->next = designator;
1083 result = designator;
1090 static initializer_t *initializer_from_string(array_type_t *type,
1093 /* TODO: check len vs. size of array type */
1096 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
1097 initializer->string.string = string;
1102 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1103 wide_string_t *const string)
1105 /* TODO: check len vs. size of array type */
1108 initializer_t *const initializer =
1109 allocate_initializer(INITIALIZER_WIDE_STRING);
1110 initializer->wide_string.string = *string;
1115 static initializer_t *initializer_from_expression(type_t *type,
1116 expression_t *expression)
1118 /* TODO check that expression is a constant expression */
1120 /* § 6.7.8.14/15 char array may be initialized by string literals */
1121 type_t *const expr_type = expression->base.datatype;
1122 if (is_type_array(type) && expr_type->type == TYPE_POINTER) {
1123 array_type_t *const array_type = &type->array;
1124 type_t *const element_type = skip_typeref(array_type->element_type);
1126 if (element_type->type == TYPE_ATOMIC) {
1127 switch (expression->type) {
1128 case EXPR_STRING_LITERAL:
1129 if (element_type->atomic.atype == ATOMIC_TYPE_CHAR) {
1130 return initializer_from_string(array_type,
1131 expression->string.value);
1134 case EXPR_WIDE_STRING_LITERAL: {
1135 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1136 if (get_unqualified_type(element_type) == bare_wchar_type) {
1137 return initializer_from_wide_string(array_type,
1138 &expression->wide_string.value);
1147 type_t *expression_type = skip_typeref(expression->base.datatype);
1148 if(is_type_scalar(type) || types_compatible(type, expression_type)) {
1149 semantic_assign(type, &expression, "initializer");
1151 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1152 result->value.value = expression;
1160 static initializer_t *parse_sub_initializer(type_t *type,
1161 expression_t *expression,
1162 type_t *expression_type);
1164 static initializer_t *parse_sub_initializer_elem(type_t *type)
1166 if(token.type == '{') {
1167 return parse_sub_initializer(type, NULL, NULL);
1170 expression_t *expression = parse_assignment_expression();
1171 type_t *expression_type = skip_typeref(expression->base.datatype);
1173 return parse_sub_initializer(type, expression, expression_type);
1176 static bool had_initializer_brace_warning;
1178 static initializer_t *parse_sub_initializer(type_t *type,
1179 expression_t *expression,
1180 type_t *expression_type)
1182 if(is_type_scalar(type)) {
1183 /* there might be extra {} hierarchies */
1184 if(token.type == '{') {
1186 if(!had_initializer_brace_warning) {
1187 parse_warning("braces around scalar initializer");
1188 had_initializer_brace_warning = true;
1190 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1191 if(token.type == ',') {
1193 /* TODO: warn about excessive elements */
1199 if(expression == NULL) {
1200 expression = parse_assignment_expression();
1202 return initializer_from_expression(type, expression);
1205 /* does the expression match the currently looked at object to initalize */
1206 if(expression != NULL) {
1207 initializer_t *result = initializer_from_expression(type, expression);
1212 bool read_paren = false;
1213 if(token.type == '{') {
1218 /* descend into subtype */
1219 initializer_t *result = NULL;
1220 initializer_t **elems;
1221 if(is_type_array(type)) {
1222 array_type_t *array_type = &type->array;
1223 type_t *element_type = array_type->element_type;
1224 element_type = skip_typeref(element_type);
1227 had_initializer_brace_warning = false;
1228 if(expression == NULL) {
1229 sub = parse_sub_initializer_elem(element_type);
1231 sub = parse_sub_initializer(element_type, expression,
1235 /* didn't match the subtypes -> try the parent type */
1237 assert(!read_paren);
1241 elems = NEW_ARR_F(initializer_t*, 0);
1242 ARR_APP1(initializer_t*, elems, sub);
1245 if(token.type == '}')
1248 if(token.type == '}')
1251 sub = parse_sub_initializer_elem(element_type);
1253 /* TODO error, do nicer cleanup */
1254 parse_error("member initializer didn't match");
1258 ARR_APP1(initializer_t*, elems, sub);
1261 assert(is_type_compound(type));
1262 compound_type_t *compound_type = &type->compound;
1263 context_t *context = &compound_type->declaration->context;
1265 declaration_t *first = context->declarations;
1268 type_t *first_type = first->type;
1269 first_type = skip_typeref(first_type);
1272 had_initializer_brace_warning = false;
1273 if(expression == NULL) {
1274 sub = parse_sub_initializer_elem(first_type);
1276 sub = parse_sub_initializer(first_type, expression,expression_type);
1279 /* didn't match the subtypes -> try our parent type */
1281 assert(!read_paren);
1285 elems = NEW_ARR_F(initializer_t*, 0);
1286 ARR_APP1(initializer_t*, elems, sub);
1288 declaration_t *iter = first->next;
1289 for( ; iter != NULL; iter = iter->next) {
1290 if(iter->symbol == NULL)
1292 if(iter->namespc != NAMESPACE_NORMAL)
1295 if(token.type == '}')
1298 if(token.type == '}')
1301 type_t *iter_type = iter->type;
1302 iter_type = skip_typeref(iter_type);
1304 sub = parse_sub_initializer_elem(iter_type);
1306 /* TODO error, do nicer cleanup*/
1307 parse_error("member initializer didn't match");
1311 ARR_APP1(initializer_t*, elems, sub);
1315 int len = ARR_LEN(elems);
1316 size_t elems_size = sizeof(initializer_t*) * len;
1318 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1320 init->initializer.type = INITIALIZER_LIST;
1322 memcpy(init->initializers, elems, elems_size);
1325 result = (initializer_t*) init;
1328 if(token.type == ',')
1335 static initializer_t *parse_initializer(type_t *type)
1337 initializer_t *result;
1339 type = skip_typeref(type);
1341 if(token.type != '{') {
1342 expression_t *expression = parse_assignment_expression();
1343 initializer_t *initializer = initializer_from_expression(type, expression);
1344 if(initializer == NULL) {
1345 parser_print_error_prefix();
1346 fprintf(stderr, "initializer expression '");
1347 print_expression(expression);
1348 fprintf(stderr, "', type ");
1349 print_type_quoted(expression->base.datatype);
1350 fprintf(stderr, " is incompatible with type ");
1351 print_type_quoted(type);
1352 fprintf(stderr, "\n");
1357 if(is_type_scalar(type)) {
1361 expression_t *expression = parse_assignment_expression();
1362 result = initializer_from_expression(type, expression);
1364 if(token.type == ',')
1370 result = parse_sub_initializer(type, NULL, NULL);
1378 static declaration_t *parse_compound_type_specifier(bool is_struct)
1386 symbol_t *symbol = NULL;
1387 declaration_t *declaration = NULL;
1389 if (token.type == T___attribute__) {
1394 if(token.type == T_IDENTIFIER) {
1395 symbol = token.v.symbol;
1399 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1401 declaration = get_declaration(symbol, NAMESPACE_UNION);
1403 } else if(token.type != '{') {
1405 parse_error_expected("while parsing struct type specifier",
1406 T_IDENTIFIER, '{', 0);
1408 parse_error_expected("while parsing union type specifier",
1409 T_IDENTIFIER, '{', 0);
1415 if(declaration == NULL) {
1416 declaration = allocate_ast_zero(sizeof(declaration[0]));
1419 declaration->namespc = NAMESPACE_STRUCT;
1421 declaration->namespc = NAMESPACE_UNION;
1423 declaration->source_position = token.source_position;
1424 declaration->symbol = symbol;
1425 record_declaration(declaration);
1428 if(token.type == '{') {
1429 if(declaration->init.is_defined) {
1430 assert(symbol != NULL);
1431 parser_print_error_prefix();
1432 fprintf(stderr, "multiple definition of %s %s\n",
1433 is_struct ? "struct" : "union", symbol->string);
1434 declaration->context.declarations = NULL;
1436 declaration->init.is_defined = true;
1438 int top = environment_top();
1439 context_t *last_context = context;
1440 set_context(&declaration->context);
1442 parse_compound_type_entries();
1445 assert(context == &declaration->context);
1446 set_context(last_context);
1447 environment_pop_to(top);
1453 static void parse_enum_entries(enum_type_t *const enum_type)
1457 if(token.type == '}') {
1459 parse_error("empty enum not allowed");
1464 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1466 if(token.type != T_IDENTIFIER) {
1467 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1471 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1472 entry->type = (type_t*) enum_type;
1473 entry->symbol = token.v.symbol;
1474 entry->source_position = token.source_position;
1477 if(token.type == '=') {
1479 entry->init.enum_value = parse_constant_expression();
1484 record_declaration(entry);
1486 if(token.type != ',')
1489 } while(token.type != '}');
1494 static type_t *parse_enum_specifier(void)
1498 declaration_t *declaration;
1501 if(token.type == T_IDENTIFIER) {
1502 symbol = token.v.symbol;
1505 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1506 } else if(token.type != '{') {
1507 parse_error_expected("while parsing enum type specifier",
1508 T_IDENTIFIER, '{', 0);
1515 if(declaration == NULL) {
1516 declaration = allocate_ast_zero(sizeof(declaration[0]));
1518 declaration->namespc = NAMESPACE_ENUM;
1519 declaration->source_position = token.source_position;
1520 declaration->symbol = symbol;
1523 type_t *const type = allocate_type_zero(TYPE_ENUM);
1524 type->enumt.declaration = declaration;
1526 if(token.type == '{') {
1527 if(declaration->init.is_defined) {
1528 parser_print_error_prefix();
1529 fprintf(stderr, "multiple definitions of enum %s\n",
1532 record_declaration(declaration);
1533 declaration->init.is_defined = 1;
1535 parse_enum_entries(&type->enumt);
1543 * if a symbol is a typedef to another type, return true
1545 static bool is_typedef_symbol(symbol_t *symbol)
1547 const declaration_t *const declaration =
1548 get_declaration(symbol, NAMESPACE_NORMAL);
1550 declaration != NULL &&
1551 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1554 static type_t *parse_typeof(void)
1562 expression_t *expression = NULL;
1565 switch(token.type) {
1566 case T___extension__:
1567 /* this can be a prefix to a typename or an expression */
1568 /* we simply eat it now. */
1571 } while(token.type == T___extension__);
1575 if(is_typedef_symbol(token.v.symbol)) {
1576 type = parse_typename();
1578 expression = parse_expression();
1579 type = expression->base.datatype;
1584 type = parse_typename();
1588 expression = parse_expression();
1589 type = expression->base.datatype;
1595 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1596 typeof_type->typeoft.expression = expression;
1597 typeof_type->typeoft.typeof_type = type;
1603 SPECIFIER_SIGNED = 1 << 0,
1604 SPECIFIER_UNSIGNED = 1 << 1,
1605 SPECIFIER_LONG = 1 << 2,
1606 SPECIFIER_INT = 1 << 3,
1607 SPECIFIER_DOUBLE = 1 << 4,
1608 SPECIFIER_CHAR = 1 << 5,
1609 SPECIFIER_SHORT = 1 << 6,
1610 SPECIFIER_LONG_LONG = 1 << 7,
1611 SPECIFIER_FLOAT = 1 << 8,
1612 SPECIFIER_BOOL = 1 << 9,
1613 SPECIFIER_VOID = 1 << 10,
1614 #ifdef PROVIDE_COMPLEX
1615 SPECIFIER_COMPLEX = 1 << 11,
1616 SPECIFIER_IMAGINARY = 1 << 12,
1620 static type_t *create_builtin_type(symbol_t *const symbol,
1621 type_t *const real_type)
1623 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1624 type->builtin.symbol = symbol;
1625 type->builtin.real_type = real_type;
1627 type_t *result = typehash_insert(type);
1628 if (type != result) {
1635 static type_t *get_typedef_type(symbol_t *symbol)
1637 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1638 if(declaration == NULL
1639 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1642 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1643 type->typedeft.declaration = declaration;
1648 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1650 type_t *type = NULL;
1651 unsigned type_qualifiers = 0;
1652 unsigned type_specifiers = 0;
1655 specifiers->source_position = token.source_position;
1658 switch(token.type) {
1661 #define MATCH_STORAGE_CLASS(token, class) \
1663 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1664 parse_error("multiple storage classes in declaration " \
1667 specifiers->storage_class = class; \
1671 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1672 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1673 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1674 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1675 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1678 switch (specifiers->storage_class) {
1679 case STORAGE_CLASS_NONE:
1680 specifiers->storage_class = STORAGE_CLASS_THREAD;
1683 case STORAGE_CLASS_EXTERN:
1684 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1687 case STORAGE_CLASS_STATIC:
1688 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1692 parse_error("multiple storage classes in declaration specifiers");
1698 /* type qualifiers */
1699 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1701 type_qualifiers |= qualifier; \
1705 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1706 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1707 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1709 case T___extension__:
1714 /* type specifiers */
1715 #define MATCH_SPECIFIER(token, specifier, name) \
1718 if(type_specifiers & specifier) { \
1719 parse_error("multiple " name " type specifiers given"); \
1721 type_specifiers |= specifier; \
1725 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1726 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1727 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1728 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1729 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1730 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1731 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1732 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1733 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1734 #ifdef PROVIDE_COMPLEX
1735 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1736 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1740 specifiers->is_inline = true;
1745 if(type_specifiers & SPECIFIER_LONG_LONG) {
1746 parse_error("multiple type specifiers given");
1747 } else if(type_specifiers & SPECIFIER_LONG) {
1748 type_specifiers |= SPECIFIER_LONG_LONG;
1750 type_specifiers |= SPECIFIER_LONG;
1754 /* TODO: if type != NULL for the following rules should issue
1757 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1759 type->compound.declaration = parse_compound_type_specifier(true);
1763 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1765 type->compound.declaration = parse_compound_type_specifier(false);
1769 type = parse_enum_specifier();
1772 type = parse_typeof();
1774 case T___builtin_va_list:
1775 type = duplicate_type(type_valist);
1779 case T___attribute__:
1784 case T_IDENTIFIER: {
1785 type_t *typedef_type = get_typedef_type(token.v.symbol);
1787 if(typedef_type == NULL)
1788 goto finish_specifiers;
1791 type = typedef_type;
1795 /* function specifier */
1797 goto finish_specifiers;
1804 atomic_type_type_t atomic_type;
1806 /* match valid basic types */
1807 switch(type_specifiers) {
1808 case SPECIFIER_VOID:
1809 atomic_type = ATOMIC_TYPE_VOID;
1811 case SPECIFIER_CHAR:
1812 atomic_type = ATOMIC_TYPE_CHAR;
1814 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1815 atomic_type = ATOMIC_TYPE_SCHAR;
1817 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1818 atomic_type = ATOMIC_TYPE_UCHAR;
1820 case SPECIFIER_SHORT:
1821 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1822 case SPECIFIER_SHORT | SPECIFIER_INT:
1823 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1824 atomic_type = ATOMIC_TYPE_SHORT;
1826 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1827 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1828 atomic_type = ATOMIC_TYPE_USHORT;
1831 case SPECIFIER_SIGNED:
1832 case SPECIFIER_SIGNED | SPECIFIER_INT:
1833 atomic_type = ATOMIC_TYPE_INT;
1835 case SPECIFIER_UNSIGNED:
1836 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1837 atomic_type = ATOMIC_TYPE_UINT;
1839 case SPECIFIER_LONG:
1840 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1841 case SPECIFIER_LONG | SPECIFIER_INT:
1842 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1843 atomic_type = ATOMIC_TYPE_LONG;
1845 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1846 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1847 atomic_type = ATOMIC_TYPE_ULONG;
1849 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1850 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1851 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1852 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1854 atomic_type = ATOMIC_TYPE_LONGLONG;
1856 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1857 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1859 atomic_type = ATOMIC_TYPE_ULONGLONG;
1861 case SPECIFIER_FLOAT:
1862 atomic_type = ATOMIC_TYPE_FLOAT;
1864 case SPECIFIER_DOUBLE:
1865 atomic_type = ATOMIC_TYPE_DOUBLE;
1867 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1868 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1870 case SPECIFIER_BOOL:
1871 atomic_type = ATOMIC_TYPE_BOOL;
1873 #ifdef PROVIDE_COMPLEX
1874 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1875 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1877 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1878 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1880 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1881 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1883 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1884 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1886 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1887 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1889 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1890 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1894 /* invalid specifier combination, give an error message */
1895 if(type_specifiers == 0) {
1897 parse_warning("no type specifiers in declaration, using int");
1898 atomic_type = ATOMIC_TYPE_INT;
1901 parse_error("no type specifiers given in declaration");
1903 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1904 (type_specifiers & SPECIFIER_UNSIGNED)) {
1905 parse_error("signed and unsigned specifiers gives");
1906 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1907 parse_error("only integer types can be signed or unsigned");
1909 parse_error("multiple datatypes in declaration");
1911 atomic_type = ATOMIC_TYPE_INVALID;
1914 type = allocate_type_zero(TYPE_ATOMIC);
1915 type->atomic.atype = atomic_type;
1918 if(type_specifiers != 0) {
1919 parse_error("multiple datatypes in declaration");
1923 type->base.qualifiers = type_qualifiers;
1925 type_t *result = typehash_insert(type);
1926 if(newtype && result != type) {
1930 specifiers->type = result;
1933 static type_qualifiers_t parse_type_qualifiers(void)
1935 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1938 switch(token.type) {
1939 /* type qualifiers */
1940 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1941 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1942 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1945 return type_qualifiers;
1950 static declaration_t *parse_identifier_list(void)
1952 declaration_t *declarations = NULL;
1953 declaration_t *last_declaration = NULL;
1955 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1957 declaration->source_position = token.source_position;
1958 declaration->symbol = token.v.symbol;
1961 if(last_declaration != NULL) {
1962 last_declaration->next = declaration;
1964 declarations = declaration;
1966 last_declaration = declaration;
1968 if(token.type != ',')
1971 } while(token.type == T_IDENTIFIER);
1973 return declarations;
1976 static void semantic_parameter(declaration_t *declaration)
1978 /* TODO: improve error messages */
1980 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1981 parse_error("typedef not allowed in parameter list");
1982 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1983 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1984 parse_error("parameter may only have none or register storage class");
1987 type_t *orig_type = declaration->type;
1988 if(orig_type == NULL)
1990 type_t *type = skip_typeref(orig_type);
1992 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1993 * into a pointer. § 6.7.5.3 (7) */
1994 if (is_type_array(type)) {
1995 const array_type_t *arr_type = &type->array;
1996 type_t *element_type = arr_type->element_type;
1998 type = make_pointer_type(element_type, type->base.qualifiers);
2000 declaration->type = type;
2003 if(is_type_incomplete(type)) {
2004 parser_print_error_prefix();
2005 fprintf(stderr, "incomplete type (");
2006 print_type_quoted(orig_type);
2007 fprintf(stderr, ") not allowed for parameter '%s'\n",
2008 declaration->symbol->string);
2012 static declaration_t *parse_parameter(void)
2014 declaration_specifiers_t specifiers;
2015 memset(&specifiers, 0, sizeof(specifiers));
2017 parse_declaration_specifiers(&specifiers);
2019 declaration_t *declaration = parse_declarator(&specifiers, true);
2021 semantic_parameter(declaration);
2026 static declaration_t *parse_parameters(function_type_t *type)
2028 if(token.type == T_IDENTIFIER) {
2029 symbol_t *symbol = token.v.symbol;
2030 if(!is_typedef_symbol(symbol)) {
2031 type->kr_style_parameters = true;
2032 return parse_identifier_list();
2036 if(token.type == ')') {
2037 type->unspecified_parameters = 1;
2040 if(token.type == T_void && look_ahead(1)->type == ')') {
2045 declaration_t *declarations = NULL;
2046 declaration_t *declaration;
2047 declaration_t *last_declaration = NULL;
2048 function_parameter_t *parameter;
2049 function_parameter_t *last_parameter = NULL;
2052 switch(token.type) {
2056 return declarations;
2059 case T___extension__:
2061 declaration = parse_parameter();
2063 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2064 memset(parameter, 0, sizeof(parameter[0]));
2065 parameter->type = declaration->type;
2067 if(last_parameter != NULL) {
2068 last_declaration->next = declaration;
2069 last_parameter->next = parameter;
2071 type->parameters = parameter;
2072 declarations = declaration;
2074 last_parameter = parameter;
2075 last_declaration = declaration;
2079 return declarations;
2081 if(token.type != ',')
2082 return declarations;
2092 } construct_type_type_t;
2094 typedef struct construct_type_t construct_type_t;
2095 struct construct_type_t {
2096 construct_type_type_t type;
2097 construct_type_t *next;
2100 typedef struct parsed_pointer_t parsed_pointer_t;
2101 struct parsed_pointer_t {
2102 construct_type_t construct_type;
2103 type_qualifiers_t type_qualifiers;
2106 typedef struct construct_function_type_t construct_function_type_t;
2107 struct construct_function_type_t {
2108 construct_type_t construct_type;
2109 type_t *function_type;
2112 typedef struct parsed_array_t parsed_array_t;
2113 struct parsed_array_t {
2114 construct_type_t construct_type;
2115 type_qualifiers_t type_qualifiers;
2121 typedef struct construct_base_type_t construct_base_type_t;
2122 struct construct_base_type_t {
2123 construct_type_t construct_type;
2127 static construct_type_t *parse_pointer_declarator(void)
2131 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2132 memset(pointer, 0, sizeof(pointer[0]));
2133 pointer->construct_type.type = CONSTRUCT_POINTER;
2134 pointer->type_qualifiers = parse_type_qualifiers();
2136 return (construct_type_t*) pointer;
2139 static construct_type_t *parse_array_declarator(void)
2143 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2144 memset(array, 0, sizeof(array[0]));
2145 array->construct_type.type = CONSTRUCT_ARRAY;
2147 if(token.type == T_static) {
2148 array->is_static = true;
2152 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2153 if(type_qualifiers != 0) {
2154 if(token.type == T_static) {
2155 array->is_static = true;
2159 array->type_qualifiers = type_qualifiers;
2161 if(token.type == '*' && look_ahead(1)->type == ']') {
2162 array->is_variable = true;
2164 } else if(token.type != ']') {
2165 array->size = parse_assignment_expression();
2170 return (construct_type_t*) array;
2173 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2177 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2179 declaration_t *parameters = parse_parameters(&type->function);
2180 if(declaration != NULL) {
2181 declaration->context.declarations = parameters;
2184 construct_function_type_t *construct_function_type =
2185 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2186 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2187 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2188 construct_function_type->function_type = type;
2192 return (construct_type_t*) construct_function_type;
2195 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2196 bool may_be_abstract)
2198 /* construct a single linked list of construct_type_t's which describe
2199 * how to construct the final declarator type */
2200 construct_type_t *first = NULL;
2201 construct_type_t *last = NULL;
2204 while(token.type == '*') {
2205 construct_type_t *type = parse_pointer_declarator();
2216 /* TODO: find out if this is correct */
2219 construct_type_t *inner_types = NULL;
2221 switch(token.type) {
2223 if(declaration == NULL) {
2224 parse_error("no identifier expected in typename");
2226 declaration->symbol = token.v.symbol;
2227 declaration->source_position = token.source_position;
2233 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2239 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2240 /* avoid a loop in the outermost scope, because eat_statement doesn't
2242 if(token.type == '}' && current_function == NULL) {
2250 construct_type_t *p = last;
2253 construct_type_t *type;
2254 switch(token.type) {
2256 type = parse_function_declarator(declaration);
2259 type = parse_array_declarator();
2262 goto declarator_finished;
2265 /* insert in the middle of the list (behind p) */
2267 type->next = p->next;
2278 declarator_finished:
2281 /* append inner_types at the end of the list, we don't to set last anymore
2282 * as it's not needed anymore */
2284 assert(first == NULL);
2285 first = inner_types;
2287 last->next = inner_types;
2293 static type_t *construct_declarator_type(construct_type_t *construct_list,
2296 construct_type_t *iter = construct_list;
2297 for( ; iter != NULL; iter = iter->next) {
2298 switch(iter->type) {
2299 case CONSTRUCT_INVALID:
2300 panic("invalid type construction found");
2301 case CONSTRUCT_FUNCTION: {
2302 construct_function_type_t *construct_function_type
2303 = (construct_function_type_t*) iter;
2305 type_t *function_type = construct_function_type->function_type;
2307 function_type->function.return_type = type;
2309 type = function_type;
2313 case CONSTRUCT_POINTER: {
2314 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2315 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2316 pointer_type->pointer.points_to = type;
2317 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2319 type = pointer_type;
2323 case CONSTRUCT_ARRAY: {
2324 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2325 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2327 array_type->base.qualifiers = parsed_array->type_qualifiers;
2328 array_type->array.element_type = type;
2329 array_type->array.is_static = parsed_array->is_static;
2330 array_type->array.is_variable = parsed_array->is_variable;
2331 array_type->array.size = parsed_array->size;
2338 type_t *hashed_type = typehash_insert(type);
2339 if(hashed_type != type) {
2340 /* the function type was constructed earlier freeing it here will
2341 * destroy other types... */
2342 if(iter->type != CONSTRUCT_FUNCTION) {
2352 static declaration_t *parse_declarator(
2353 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2355 type_t *type = specifiers->type;
2356 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2357 declaration->storage_class = specifiers->storage_class;
2358 declaration->is_inline = specifiers->is_inline;
2360 construct_type_t *construct_type
2361 = parse_inner_declarator(declaration, may_be_abstract);
2362 declaration->type = construct_declarator_type(construct_type, type);
2364 if(construct_type != NULL) {
2365 obstack_free(&temp_obst, construct_type);
2371 static type_t *parse_abstract_declarator(type_t *base_type)
2373 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2375 type_t *result = construct_declarator_type(construct_type, base_type);
2376 if(construct_type != NULL) {
2377 obstack_free(&temp_obst, construct_type);
2383 static declaration_t *record_declaration(declaration_t *declaration)
2385 assert(declaration->parent_context == NULL);
2386 assert(context != NULL);
2388 symbol_t *symbol = declaration->symbol;
2389 if(symbol != NULL) {
2390 declaration_t *alias = environment_push(declaration);
2391 if(alias != declaration)
2394 declaration->parent_context = context;
2397 if(last_declaration != NULL) {
2398 last_declaration->next = declaration;
2400 context->declarations = declaration;
2402 last_declaration = declaration;
2407 static void parser_error_multiple_definition(declaration_t *declaration,
2408 const source_position_t source_position)
2410 parser_print_error_prefix_pos(source_position);
2411 fprintf(stderr, "multiple definition of symbol '%s'\n",
2412 declaration->symbol->string);
2413 parser_print_error_prefix_pos(declaration->source_position);
2414 fprintf(stderr, "this is the location of the previous definition.\n");
2417 static bool is_declaration_specifier(const token_t *token,
2418 bool only_type_specifiers)
2420 switch(token->type) {
2424 return is_typedef_symbol(token->v.symbol);
2426 case T___extension__:
2429 return !only_type_specifiers;
2436 static void parse_init_declarator_rest(declaration_t *declaration)
2440 type_t *orig_type = declaration->type;
2441 type_t *type = NULL;
2442 if(orig_type != NULL)
2443 type = skip_typeref(orig_type);
2445 if(declaration->init.initializer != NULL) {
2446 parser_error_multiple_definition(declaration, token.source_position);
2449 initializer_t *initializer = parse_initializer(type);
2451 /* § 6.7.5 (22) array intializers for arrays with unknown size determine
2452 * the array type size */
2453 if(type != NULL && is_type_array(type) && initializer != NULL) {
2454 array_type_t *array_type = &type->array;
2456 if(array_type->size == NULL) {
2457 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2459 cnst->base.datatype = type_size_t;
2461 switch (initializer->type) {
2462 case INITIALIZER_LIST: {
2463 initializer_list_t *const list = &initializer->list;
2464 cnst->conste.v.int_value = list->len;
2468 case INITIALIZER_STRING: {
2469 initializer_string_t *const string = &initializer->string;
2470 cnst->conste.v.int_value = strlen(string->string) + 1;
2474 case INITIALIZER_WIDE_STRING: {
2475 initializer_wide_string_t *const string = &initializer->wide_string;
2476 cnst->conste.v.int_value = string->string.size;
2481 panic("invalid initializer type");
2484 array_type->size = cnst;
2488 if(type != NULL && is_type_function(type)) {
2489 parser_print_error_prefix_pos(declaration->source_position);
2490 fprintf(stderr, "initializers not allowed for function types at "
2491 "declator '%s' (type ", declaration->symbol->string);
2492 print_type_quoted(orig_type);
2493 fprintf(stderr, ")\n");
2495 declaration->init.initializer = initializer;
2499 /* parse rest of a declaration without any declarator */
2500 static void parse_anonymous_declaration_rest(
2501 const declaration_specifiers_t *specifiers,
2502 parsed_declaration_func finished_declaration)
2506 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2508 declaration->type = specifiers->type;
2509 declaration->storage_class = specifiers->storage_class;
2510 declaration->source_position = specifiers->source_position;
2512 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2513 parse_warning_pos(declaration->source_position,
2514 "useless storage class in empty declaration");
2517 type_t *type = declaration->type;
2518 switch (type->type) {
2519 case TYPE_COMPOUND_STRUCT:
2520 case TYPE_COMPOUND_UNION: {
2521 const compound_type_t *compound_type = &type->compound;
2522 if (compound_type->declaration->symbol == NULL) {
2523 parse_warning_pos(declaration->source_position,
2524 "unnamed struct/union that defines no instances");
2533 parse_warning_pos(declaration->source_position,
2534 "empty declaration");
2538 finished_declaration(declaration);
2541 static void parse_declaration_rest(declaration_t *ndeclaration,
2542 const declaration_specifiers_t *specifiers,
2543 parsed_declaration_func finished_declaration)
2546 declaration_t *declaration = finished_declaration(ndeclaration);
2548 type_t *orig_type = declaration->type;
2549 type_t *type = skip_typeref(orig_type);
2551 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2552 parser_print_warning_prefix_pos(declaration->source_position);
2553 fprintf(stderr, "variable '%s' declared 'inline'\n",
2554 declaration->symbol->string);
2557 if(token.type == '=') {
2558 parse_init_declarator_rest(declaration);
2561 if(token.type != ',')
2565 ndeclaration = parse_declarator(specifiers, false);
2570 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2572 /* TODO: check that it was actually a parameter that gets a type */
2574 /* we should have a declaration for the parameter in the current
2576 return record_declaration(declaration);
2579 static void parse_declaration(parsed_declaration_func finished_declaration)
2581 declaration_specifiers_t specifiers;
2582 memset(&specifiers, 0, sizeof(specifiers));
2583 parse_declaration_specifiers(&specifiers);
2585 if(token.type == ';') {
2586 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2588 declaration_t *declaration = parse_declarator(&specifiers, false);
2589 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2593 static void parse_kr_declaration_list(declaration_t *declaration)
2595 type_t *type = skip_typeref(declaration->type);
2596 assert(is_type_function(type));
2598 if(!type->function.kr_style_parameters)
2601 /* push function parameters */
2602 int top = environment_top();
2603 context_t *last_context = context;
2604 set_context(&declaration->context);
2606 declaration_t *parameter = declaration->context.declarations;
2607 for( ; parameter != NULL; parameter = parameter->next) {
2608 environment_push(parameter);
2611 /* parse declaration list */
2612 while(is_declaration_specifier(&token, false)) {
2613 parse_declaration(finished_kr_declaration);
2616 /* pop function parameters */
2617 assert(context == &declaration->context);
2618 set_context(last_context);
2619 environment_pop_to(top);
2621 /* update function type */
2622 type_t *new_type = duplicate_type(type);
2623 new_type->function.kr_style_parameters = false;
2625 function_parameter_t *parameters = NULL;
2626 function_parameter_t *last_parameter = NULL;
2628 declaration_t *parameter_declaration = declaration->context.declarations;
2629 for( ; parameter_declaration != NULL;
2630 parameter_declaration = parameter_declaration->next) {
2631 type_t *parameter_type = parameter_declaration->type;
2632 if(parameter_type == NULL) {
2634 parser_print_error_prefix();
2635 fprintf(stderr, "no type specified for function parameter '%s'\n",
2636 parameter_declaration->symbol->string);
2638 parser_print_warning_prefix();
2639 fprintf(stderr, "no type specified for function parameter '%s', "
2640 "using int\n", parameter_declaration->symbol->string);
2641 parameter_type = type_int;
2642 parameter_declaration->type = parameter_type;
2646 semantic_parameter(parameter_declaration);
2647 parameter_type = parameter_declaration->type;
2649 function_parameter_t *function_parameter
2650 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2651 memset(function_parameter, 0, sizeof(function_parameter[0]));
2653 function_parameter->type = parameter_type;
2654 if(last_parameter != NULL) {
2655 last_parameter->next = function_parameter;
2657 parameters = function_parameter;
2659 last_parameter = function_parameter;
2661 new_type->function.parameters = parameters;
2663 type = typehash_insert(new_type);
2664 if(type != new_type) {
2665 obstack_free(type_obst, new_type);
2668 declaration->type = type;
2671 static void parse_external_declaration(void)
2673 /* function-definitions and declarations both start with declaration
2675 declaration_specifiers_t specifiers;
2676 memset(&specifiers, 0, sizeof(specifiers));
2677 parse_declaration_specifiers(&specifiers);
2679 /* must be a declaration */
2680 if(token.type == ';') {
2681 parse_anonymous_declaration_rest(&specifiers, record_declaration);
2685 /* declarator is common to both function-definitions and declarations */
2686 declaration_t *ndeclaration = parse_declarator(&specifiers, false);
2688 /* must be a declaration */
2689 if(token.type == ',' || token.type == '=' || token.type == ';') {
2690 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2694 /* must be a function definition */
2695 parse_kr_declaration_list(ndeclaration);
2697 if(token.type != '{') {
2698 parse_error_expected("while parsing function definition", '{', 0);
2703 type_t *type = ndeclaration->type;
2709 /* note that we don't skip typerefs: the standard doesn't allow them here
2710 * (so we can't use is_type_function here) */
2711 if(type->type != TYPE_FUNCTION) {
2712 parser_print_error_prefix();
2713 fprintf(stderr, "declarator '");
2714 print_type_ext(type, ndeclaration->symbol, NULL);
2715 fprintf(stderr, "' has a body but is not a function type.\n");
2720 /* § 6.7.5.3 (14) a function definition with () means no
2721 * parameters (and not unspecified parameters) */
2722 if(type->function.unspecified_parameters) {
2723 type_t *duplicate = duplicate_type(type);
2724 duplicate->function.unspecified_parameters = false;
2726 type = typehash_insert(duplicate);
2727 if(type != duplicate) {
2728 obstack_free(type_obst, duplicate);
2730 ndeclaration->type = type;
2733 declaration_t *declaration = record_declaration(ndeclaration);
2734 if(ndeclaration != declaration) {
2735 memcpy(&declaration->context, &ndeclaration->context,
2736 sizeof(declaration->context));
2738 type = skip_typeref(declaration->type);
2740 /* push function parameters and switch context */
2741 int top = environment_top();
2742 context_t *last_context = context;
2743 set_context(&declaration->context);
2745 declaration_t *parameter = declaration->context.declarations;
2746 for( ; parameter != NULL; parameter = parameter->next) {
2747 environment_push(parameter);
2750 if(declaration->init.statement != NULL) {
2751 parser_error_multiple_definition(declaration, token.source_position);
2753 goto end_of_parse_external_declaration;
2755 /* parse function body */
2756 int label_stack_top = label_top();
2757 declaration_t *old_current_function = current_function;
2758 current_function = declaration;
2760 declaration->init.statement = parse_compound_statement();
2762 assert(current_function == declaration);
2763 current_function = old_current_function;
2764 label_pop_to(label_stack_top);
2767 end_of_parse_external_declaration:
2768 assert(context == &declaration->context);
2769 set_context(last_context);
2770 environment_pop_to(top);
2773 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2776 if(token.type == ':') {
2778 parse_constant_expression();
2779 /* TODO (bitfields) */
2781 declaration_t *declaration = parse_declarator(specifiers, true);
2783 /* TODO: check constraints for struct declarations */
2784 /* TODO: check for doubled fields */
2785 record_declaration(declaration);
2787 if(token.type == ':') {
2789 parse_constant_expression();
2790 /* TODO (bitfields) */
2794 if(token.type != ',')
2801 static void parse_compound_type_entries(void)
2805 while(token.type != '}' && token.type != T_EOF) {
2806 declaration_specifiers_t specifiers;
2807 memset(&specifiers, 0, sizeof(specifiers));
2808 parse_declaration_specifiers(&specifiers);
2810 parse_struct_declarators(&specifiers);
2812 if(token.type == T_EOF) {
2813 parse_error("EOF while parsing struct");
2818 static type_t *parse_typename(void)
2820 declaration_specifiers_t specifiers;
2821 memset(&specifiers, 0, sizeof(specifiers));
2822 parse_declaration_specifiers(&specifiers);
2823 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2824 /* TODO: improve error message, user does probably not know what a
2825 * storage class is...
2827 parse_error("typename may not have a storage class");
2830 type_t *result = parse_abstract_declarator(specifiers.type);
2838 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2839 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2840 expression_t *left);
2842 typedef struct expression_parser_function_t expression_parser_function_t;
2843 struct expression_parser_function_t {
2844 unsigned precedence;
2845 parse_expression_function parser;
2846 unsigned infix_precedence;
2847 parse_expression_infix_function infix_parser;
2850 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2852 static expression_t *create_invalid_expression(void)
2854 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2855 expression->base.source_position = token.source_position;
2859 static expression_t *expected_expression_error(void)
2861 parser_print_error_prefix();
2862 fprintf(stderr, "expected expression, got token ");
2863 print_token(stderr, &token);
2864 fprintf(stderr, "\n");
2868 return create_invalid_expression();
2871 static expression_t *parse_string_const(void)
2873 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2874 cnst->base.datatype = type_string;
2875 cnst->string.value = parse_string_literals();
2880 static expression_t *parse_wide_string_const(void)
2882 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2883 cnst->base.datatype = type_wchar_t_ptr;
2884 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2889 static expression_t *parse_int_const(void)
2891 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2892 cnst->base.datatype = token.datatype;
2893 cnst->conste.v.int_value = token.v.intvalue;
2900 static expression_t *parse_float_const(void)
2902 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2903 cnst->base.datatype = token.datatype;
2904 cnst->conste.v.float_value = token.v.floatvalue;
2911 static declaration_t *create_implicit_function(symbol_t *symbol,
2912 const source_position_t source_position)
2914 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2915 ntype->function.return_type = type_int;
2916 ntype->function.unspecified_parameters = true;
2918 type_t *type = typehash_insert(ntype);
2923 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2925 declaration->storage_class = STORAGE_CLASS_EXTERN;
2926 declaration->type = type;
2927 declaration->symbol = symbol;
2928 declaration->source_position = source_position;
2930 /* prepend the implicit definition to the global context
2931 * this is safe since the symbol wasn't declared as anything else yet
2933 assert(symbol->declaration == NULL);
2935 context_t *last_context = context;
2936 context = global_context;
2938 environment_push(declaration);
2939 declaration->next = context->declarations;
2940 context->declarations = declaration;
2942 context = last_context;
2947 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
2949 function_parameter_t *parameter
2950 = obstack_alloc(type_obst, sizeof(parameter[0]));
2951 memset(parameter, 0, sizeof(parameter[0]));
2952 parameter->type = argument_type;
2954 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2955 type->function.return_type = return_type;
2956 type->function.parameters = parameter;
2958 type_t *result = typehash_insert(type);
2959 if(result != type) {
2966 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2968 switch(symbol->ID) {
2969 case T___builtin_alloca:
2970 return make_function_1_type(type_void_ptr, type_size_t);
2971 case T___builtin_nan:
2972 return make_function_1_type(type_double, type_string);
2973 case T___builtin_nanf:
2974 return make_function_1_type(type_float, type_string);
2975 case T___builtin_nand:
2976 return make_function_1_type(type_long_double, type_string);
2977 case T___builtin_va_end:
2978 return make_function_1_type(type_void, type_valist);
2980 panic("not implemented builtin symbol found");
2985 * performs automatic type cast as described in § 6.3.2.1
2987 static type_t *automatic_type_conversion(type_t *orig_type)
2989 if(orig_type == NULL)
2992 type_t *type = skip_typeref(orig_type);
2993 if(is_type_array(type)) {
2994 array_type_t *array_type = &type->array;
2995 type_t *element_type = array_type->element_type;
2996 unsigned qualifiers = array_type->type.qualifiers;
2998 return make_pointer_type(element_type, qualifiers);
3001 if(is_type_function(type)) {
3002 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3009 * reverts the automatic casts of array to pointer types and function
3010 * to function-pointer types as defined § 6.3.2.1
3012 type_t *revert_automatic_type_conversion(const expression_t *expression)
3014 if(expression->base.datatype == NULL)
3017 switch(expression->type) {
3018 case EXPR_REFERENCE: {
3019 const reference_expression_t *ref = &expression->reference;
3020 return ref->declaration->type;
3023 const select_expression_t *select = &expression->select;
3024 return select->compound_entry->type;
3026 case EXPR_UNARY_DEREFERENCE: {
3027 expression_t *value = expression->unary.value;
3028 type_t *type = skip_typeref(value->base.datatype);
3029 pointer_type_t *pointer_type = &type->pointer;
3031 return pointer_type->points_to;
3033 case EXPR_BUILTIN_SYMBOL: {
3034 const builtin_symbol_expression_t *builtin
3035 = &expression->builtin_symbol;
3036 return get_builtin_symbol_type(builtin->symbol);
3038 case EXPR_ARRAY_ACCESS: {
3039 const array_access_expression_t *array_access
3040 = &expression->array_access;
3041 const expression_t *array_ref = array_access->array_ref;
3042 type_t *type_left = skip_typeref(array_ref->base.datatype);
3043 assert(is_type_pointer(type_left));
3044 pointer_type_t *pointer_type = &type_left->pointer;
3045 return pointer_type->points_to;
3052 return expression->base.datatype;
3055 static expression_t *parse_reference(void)
3057 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3059 reference_expression_t *ref = &expression->reference;
3060 ref->symbol = token.v.symbol;
3062 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3064 source_position_t source_position = token.source_position;
3067 if(declaration == NULL) {
3069 /* an implicitly defined function */
3070 if(token.type == '(') {
3071 parser_print_prefix_pos(token.source_position);
3072 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
3073 ref->symbol->string);
3075 declaration = create_implicit_function(ref->symbol,
3080 parser_print_error_prefix();
3081 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
3086 type_t *type = declaration->type;
3087 /* we always do the auto-type conversions; the & and sizeof parser contains
3088 * code to revert this! */
3089 type = automatic_type_conversion(type);
3091 ref->declaration = declaration;
3092 ref->expression.datatype = type;
3097 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3101 /* TODO check if explicit cast is allowed and issue warnings/errors */
3104 static expression_t *parse_cast(void)
3106 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3108 cast->base.source_position = token.source_position;
3110 type_t *type = parse_typename();
3113 expression_t *value = parse_sub_expression(20);
3115 check_cast_allowed(value, type);
3117 cast->base.datatype = type;
3118 cast->unary.value = value;
3123 static expression_t *parse_statement_expression(void)
3125 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3127 statement_t *statement = parse_compound_statement();
3128 expression->statement.statement = statement;
3129 if(statement == NULL) {
3134 assert(statement->type == STATEMENT_COMPOUND);
3135 compound_statement_t *compound_statement = &statement->compound;
3137 /* find last statement and use it's type */
3138 const statement_t *last_statement = NULL;
3139 const statement_t *iter = compound_statement->statements;
3140 for( ; iter != NULL; iter = iter->base.next) {
3141 last_statement = iter;
3144 if(last_statement->type == STATEMENT_EXPRESSION) {
3145 const expression_statement_t *expression_statement
3146 = &last_statement->expression;
3147 expression->base.datatype
3148 = expression_statement->expression->base.datatype;
3150 expression->base.datatype = type_void;
3158 static expression_t *parse_brace_expression(void)
3162 switch(token.type) {
3164 /* gcc extension: a stement expression */
3165 return parse_statement_expression();
3169 return parse_cast();
3171 if(is_typedef_symbol(token.v.symbol)) {
3172 return parse_cast();
3176 expression_t *result = parse_expression();
3182 static expression_t *parse_function_keyword(void)
3187 if (current_function == NULL) {
3188 parse_error("'__func__' used outside of a function");
3191 string_literal_expression_t *expression
3192 = allocate_ast_zero(sizeof(expression[0]));
3194 expression->expression.type = EXPR_FUNCTION;
3195 expression->expression.datatype = type_string;
3196 expression->value = "TODO: FUNCTION";
3198 return (expression_t*) expression;
3201 static expression_t *parse_pretty_function_keyword(void)
3203 eat(T___PRETTY_FUNCTION__);
3206 string_literal_expression_t *expression
3207 = allocate_ast_zero(sizeof(expression[0]));
3209 expression->expression.type = EXPR_PRETTY_FUNCTION;
3210 expression->expression.datatype = type_string;
3211 expression->value = "TODO: PRETTY FUNCTION";
3213 return (expression_t*) expression;
3216 static designator_t *parse_designator(void)
3218 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3220 if(token.type != T_IDENTIFIER) {
3221 parse_error_expected("while parsing member designator",
3226 result->symbol = token.v.symbol;
3229 designator_t *last_designator = result;
3231 if(token.type == '.') {
3233 if(token.type != T_IDENTIFIER) {
3234 parse_error_expected("while parsing member designator",
3239 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3240 designator->symbol = token.v.symbol;
3243 last_designator->next = designator;
3244 last_designator = designator;
3247 if(token.type == '[') {
3249 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3250 designator->array_access = parse_expression();
3251 if(designator->array_access == NULL) {
3257 last_designator->next = designator;
3258 last_designator = designator;
3267 static expression_t *parse_offsetof(void)
3269 eat(T___builtin_offsetof);
3271 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3272 expression->base.datatype = type_size_t;
3275 expression->offsetofe.type = parse_typename();
3277 expression->offsetofe.designator = parse_designator();
3283 static expression_t *parse_va_start(void)
3285 eat(T___builtin_va_start);
3287 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3290 expression->va_starte.ap = parse_assignment_expression();
3292 expression_t *const expr = parse_assignment_expression();
3293 if (expr->type == EXPR_REFERENCE) {
3294 declaration_t *const decl = expr->reference.declaration;
3295 if (decl->parent_context == ¤t_function->context &&
3296 decl->next == NULL) {
3297 expression->va_starte.parameter = decl;
3302 parser_print_error_prefix_pos(expr->base.source_position);
3303 fprintf(stderr, "second argument of 'va_start' must be last parameter "
3304 "of the current function\n");
3306 return create_invalid_expression();
3309 static expression_t *parse_va_arg(void)
3311 eat(T___builtin_va_arg);
3313 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3316 expression->va_arge.ap = parse_assignment_expression();
3318 expression->base.datatype = parse_typename();
3324 static expression_t *parse_builtin_symbol(void)
3326 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3328 symbol_t *symbol = token.v.symbol;
3330 expression->builtin_symbol.symbol = symbol;
3333 type_t *type = get_builtin_symbol_type(symbol);
3334 type = automatic_type_conversion(type);
3336 expression->base.datatype = type;
3340 static expression_t *parse_compare_builtin(void)
3342 expression_t *expression;
3344 switch(token.type) {
3345 case T___builtin_isgreater:
3346 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3348 case T___builtin_isgreaterequal:
3349 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3351 case T___builtin_isless:
3352 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3354 case T___builtin_islessequal:
3355 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3357 case T___builtin_islessgreater:
3358 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3360 case T___builtin_isunordered:
3361 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3364 panic("invalid compare builtin found");
3370 expression->binary.left = parse_assignment_expression();
3372 expression->binary.right = parse_assignment_expression();
3375 type_t *orig_type_left = expression->binary.left->base.datatype;
3376 type_t *orig_type_right = expression->binary.right->base.datatype;
3377 if(orig_type_left == NULL || orig_type_right == NULL)
3380 type_t *type_left = skip_typeref(orig_type_left);
3381 type_t *type_right = skip_typeref(orig_type_right);
3382 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3383 type_error_incompatible("invalid operands in comparison",
3384 token.source_position, type_left, type_right);
3386 semantic_comparison(&expression->binary);
3392 static expression_t *parse_primary_expression(void)
3394 switch(token.type) {
3396 return parse_int_const();
3397 case T_FLOATINGPOINT:
3398 return parse_float_const();
3399 case T_STRING_LITERAL: /* TODO merge */
3400 return parse_string_const();
3401 case T_WIDE_STRING_LITERAL:
3402 return parse_wide_string_const();
3404 return parse_reference();
3405 case T___FUNCTION__:
3407 return parse_function_keyword();
3408 case T___PRETTY_FUNCTION__:
3409 return parse_pretty_function_keyword();
3410 case T___builtin_offsetof:
3411 return parse_offsetof();
3412 case T___builtin_va_start:
3413 return parse_va_start();
3414 case T___builtin_va_arg:
3415 return parse_va_arg();
3416 case T___builtin_nanf:
3417 case T___builtin_alloca:
3418 case T___builtin_expect:
3419 case T___builtin_va_end:
3420 return parse_builtin_symbol();
3421 case T___builtin_isgreater:
3422 case T___builtin_isgreaterequal:
3423 case T___builtin_isless:
3424 case T___builtin_islessequal:
3425 case T___builtin_islessgreater:
3426 case T___builtin_isunordered:
3427 return parse_compare_builtin();
3430 return parse_brace_expression();
3433 parser_print_error_prefix();
3434 fprintf(stderr, "unexpected token ");
3435 print_token(stderr, &token);
3436 fprintf(stderr, "\n");
3439 return create_invalid_expression();
3442 static expression_t *parse_array_expression(unsigned precedence,
3449 expression_t *inside = parse_expression();
3451 array_access_expression_t *array_access
3452 = allocate_ast_zero(sizeof(array_access[0]));
3454 array_access->expression.type = EXPR_ARRAY_ACCESS;
3456 type_t *type_left = left->base.datatype;
3457 type_t *type_inside = inside->base.datatype;
3458 type_t *return_type = NULL;
3460 if(type_left != NULL && type_inside != NULL) {
3461 type_left = skip_typeref(type_left);
3462 type_inside = skip_typeref(type_inside);
3464 if(is_type_pointer(type_left)) {
3465 pointer_type_t *pointer = &type_left->pointer;
3466 return_type = pointer->points_to;
3467 array_access->array_ref = left;
3468 array_access->index = inside;
3469 } else if(is_type_pointer(type_inside)) {
3470 pointer_type_t *pointer = &type_inside->pointer;
3471 return_type = pointer->points_to;
3472 array_access->array_ref = inside;
3473 array_access->index = left;
3474 array_access->flipped = true;
3476 parser_print_error_prefix();
3477 fprintf(stderr, "array access on object with non-pointer types ");
3478 print_type_quoted(type_left);
3479 fprintf(stderr, ", ");
3480 print_type_quoted(type_inside);
3481 fprintf(stderr, "\n");
3484 array_access->array_ref = left;
3485 array_access->index = inside;
3488 if(token.type != ']') {
3489 parse_error_expected("Problem while parsing array access", ']', 0);
3490 return (expression_t*) array_access;
3494 return_type = automatic_type_conversion(return_type);
3495 array_access->expression.datatype = return_type;
3497 return (expression_t*) array_access;
3500 static expression_t *parse_sizeof(unsigned precedence)
3504 sizeof_expression_t *sizeof_expression
3505 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3506 sizeof_expression->expression.type = EXPR_SIZEOF;
3507 sizeof_expression->expression.datatype = type_size_t;
3509 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3511 sizeof_expression->type = parse_typename();
3514 expression_t *expression = parse_sub_expression(precedence);
3515 expression->base.datatype = revert_automatic_type_conversion(expression);
3517 sizeof_expression->type = expression->base.datatype;
3518 sizeof_expression->size_expression = expression;
3521 return (expression_t*) sizeof_expression;
3524 static expression_t *parse_select_expression(unsigned precedence,
3525 expression_t *compound)
3528 assert(token.type == '.' || token.type == T_MINUSGREATER);
3530 bool is_pointer = (token.type == T_MINUSGREATER);
3533 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3534 select->select.compound = compound;
3536 if(token.type != T_IDENTIFIER) {
3537 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3540 symbol_t *symbol = token.v.symbol;
3541 select->select.symbol = symbol;
3544 type_t *orig_type = compound->base.datatype;
3545 if(orig_type == NULL)
3546 return create_invalid_expression();
3548 type_t *type = skip_typeref(orig_type);
3550 type_t *type_left = type;
3552 if(type->type != TYPE_POINTER) {
3553 parser_print_error_prefix();
3554 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
3555 print_type_quoted(orig_type);
3556 fputc('\n', stderr);
3557 return create_invalid_expression();
3559 pointer_type_t *pointer_type = &type->pointer;
3560 type_left = pointer_type->points_to;
3562 type_left = skip_typeref(type_left);
3564 if(type_left->type != TYPE_COMPOUND_STRUCT
3565 && type_left->type != TYPE_COMPOUND_UNION) {
3566 parser_print_error_prefix();
3567 fprintf(stderr, "request for member '%s' in something not a struct or "
3568 "union, but ", symbol->string);
3569 print_type_quoted(type_left);
3570 fputc('\n', stderr);
3571 return create_invalid_expression();
3574 compound_type_t *compound_type = &type_left->compound;
3575 declaration_t *declaration = compound_type->declaration;
3577 if(!declaration->init.is_defined) {
3578 parser_print_error_prefix();
3579 fprintf(stderr, "request for member '%s' of incomplete type ",
3581 print_type_quoted(type_left);
3582 fputc('\n', stderr);
3583 return create_invalid_expression();
3586 declaration_t *iter = declaration->context.declarations;
3587 for( ; iter != NULL; iter = iter->next) {
3588 if(iter->symbol == symbol) {
3593 parser_print_error_prefix();
3594 print_type_quoted(type_left);
3595 fprintf(stderr, " has no member named '%s'\n", symbol->string);
3596 return create_invalid_expression();
3599 /* we always do the auto-type conversions; the & and sizeof parser contains
3600 * code to revert this! */
3601 type_t *expression_type = automatic_type_conversion(iter->type);
3603 select->select.compound_entry = iter;
3604 select->base.datatype = expression_type;
3608 static expression_t *parse_call_expression(unsigned precedence,
3609 expression_t *expression)
3612 expression_t *result = allocate_expression_zero(EXPR_CALL);
3614 call_expression_t *call = &result->call;
3615 call->function = expression;
3617 function_type_t *function_type = NULL;
3618 type_t *orig_type = expression->base.datatype;
3619 if(orig_type != NULL) {
3620 type_t *type = skip_typeref(orig_type);
3622 if(is_type_pointer(type)) {
3623 pointer_type_t *pointer_type = &type->pointer;
3625 type = skip_typeref(pointer_type->points_to);
3627 if (is_type_function(type)) {
3628 function_type = &type->function;
3629 call->expression.datatype = function_type->return_type;
3632 if(function_type == NULL) {
3633 parser_print_error_prefix();
3634 fputs("called object '", stderr);
3635 print_expression(expression);
3636 fputs("' (type ", stderr);
3637 print_type_quoted(orig_type);
3638 fputs(") is not a pointer to a function\n", stderr);
3640 function_type = NULL;
3641 call->expression.datatype = NULL;
3645 /* parse arguments */
3648 if(token.type != ')') {
3649 call_argument_t *last_argument = NULL;
3652 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3654 argument->expression = parse_assignment_expression();
3655 if(last_argument == NULL) {
3656 call->arguments = argument;
3658 last_argument->next = argument;
3660 last_argument = argument;
3662 if(token.type != ',')
3669 if(function_type != NULL) {
3670 function_parameter_t *parameter = function_type->parameters;
3671 call_argument_t *argument = call->arguments;
3672 for( ; parameter != NULL && argument != NULL;
3673 parameter = parameter->next, argument = argument->next) {
3674 type_t *expected_type = parameter->type;
3675 /* TODO report context in error messages */
3676 argument->expression = create_implicit_cast(argument->expression,
3679 /* too few parameters */
3680 if(parameter != NULL) {
3681 parser_print_error_prefix();
3682 fprintf(stderr, "too few arguments to function '");
3683 print_expression(expression);
3684 fprintf(stderr, "'\n");
3685 } else if(argument != NULL) {
3686 /* too many parameters */
3687 if(!function_type->variadic
3688 && !function_type->unspecified_parameters) {
3689 parser_print_error_prefix();
3690 fprintf(stderr, "too many arguments to function '");
3691 print_expression(expression);
3692 fprintf(stderr, "'\n");
3694 /* do default promotion */
3695 for( ; argument != NULL; argument = argument->next) {
3696 type_t *type = argument->expression->base.datatype;
3701 type = skip_typeref(type);
3702 if(is_type_integer(type)) {
3703 type = promote_integer(type);
3704 } else if(type == type_float) {
3708 argument->expression
3709 = create_implicit_cast(argument->expression, type);
3718 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3720 static bool same_compound_type(const type_t *type1, const type_t *type2)
3722 if(!is_type_compound(type1))
3724 if(type1->type != type2->type)
3727 const compound_type_t *compound1 = &type1->compound;
3728 const compound_type_t *compound2 = &type2->compound;
3730 return compound1->declaration == compound2->declaration;
3733 static expression_t *parse_conditional_expression(unsigned precedence,
3734 expression_t *expression)
3738 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3740 conditional_expression_t *conditional = &result->conditional;
3741 conditional->condition = expression;
3744 type_t *condition_type_orig = expression->base.datatype;
3745 if(condition_type_orig != NULL) {
3746 type_t *condition_type = skip_typeref(condition_type_orig);
3747 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3748 type_error("expected a scalar type in conditional condition",
3749 expression->base.source_position, condition_type_orig);
3753 expression_t *true_expression = parse_expression();
3755 expression_t *false_expression = parse_sub_expression(precedence);
3757 conditional->true_expression = true_expression;
3758 conditional->false_expression = false_expression;
3760 type_t *orig_true_type = true_expression->base.datatype;
3761 type_t *orig_false_type = false_expression->base.datatype;
3762 if(orig_true_type == NULL || orig_false_type == NULL)
3765 type_t *true_type = skip_typeref(orig_true_type);
3766 type_t *false_type = skip_typeref(orig_false_type);
3769 type_t *result_type = NULL;
3770 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3771 result_type = semantic_arithmetic(true_type, false_type);
3773 true_expression = create_implicit_cast(true_expression, result_type);
3774 false_expression = create_implicit_cast(false_expression, result_type);
3776 conditional->true_expression = true_expression;
3777 conditional->false_expression = false_expression;
3778 conditional->expression.datatype = result_type;
3779 } else if (same_compound_type(true_type, false_type)
3780 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3781 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
3782 /* just take 1 of the 2 types */
3783 result_type = true_type;
3784 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3785 && pointers_compatible(true_type, false_type)) {
3787 result_type = true_type;
3790 type_error_incompatible("while parsing conditional",
3791 expression->base.source_position, true_type,
3795 conditional->expression.datatype = result_type;
3799 static expression_t *parse_extension(unsigned precedence)
3801 eat(T___extension__);
3803 /* TODO enable extensions */
3805 return parse_sub_expression(precedence);
3808 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3810 eat(T___builtin_classify_type);
3812 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3813 result->base.datatype = type_int;
3816 expression_t *expression = parse_sub_expression(precedence);
3818 result->classify_type.type_expression = expression;
3823 static void semantic_incdec(unary_expression_t *expression)
3825 type_t *orig_type = expression->value->base.datatype;
3826 if(orig_type == NULL)
3829 type_t *type = skip_typeref(orig_type);
3830 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3831 /* TODO: improve error message */
3832 parser_print_error_prefix();
3833 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3837 expression->expression.datatype = orig_type;
3840 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3842 type_t *orig_type = expression->value->base.datatype;
3843 if(orig_type == NULL)
3846 type_t *type = skip_typeref(orig_type);
3847 if(!is_type_arithmetic(type)) {
3848 /* TODO: improve error message */
3849 parser_print_error_prefix();
3850 fprintf(stderr, "operation needs an arithmetic type\n");
3854 expression->expression.datatype = orig_type;
3857 static void semantic_unexpr_scalar(unary_expression_t *expression)
3859 type_t *orig_type = expression->value->base.datatype;
3860 if(orig_type == NULL)
3863 type_t *type = skip_typeref(orig_type);
3864 if (!is_type_scalar(type)) {
3865 parse_error("operand of ! must be of scalar type\n");
3869 expression->expression.datatype = orig_type;
3872 static void semantic_unexpr_integer(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_integer(type)) {
3880 parse_error("operand of ~ must be of integer type\n");
3884 expression->expression.datatype = orig_type;
3887 static void semantic_dereference(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_pointer(type)) {
3895 parser_print_error_prefix();
3896 fputs("Unary '*' needs pointer or arrray type, but type ", stderr);
3897 print_type_quoted(orig_type);
3898 fputs(" given.\n", stderr);
3902 pointer_type_t *pointer_type = &type->pointer;
3903 type_t *result_type = pointer_type->points_to;
3905 result_type = automatic_type_conversion(result_type);
3906 expression->expression.datatype = result_type;
3909 static void semantic_take_addr(unary_expression_t *expression)
3911 expression_t *value = expression->value;
3912 value->base.datatype = revert_automatic_type_conversion(value);
3914 type_t *orig_type = value->base.datatype;
3915 if(orig_type == NULL)
3918 if(value->type == EXPR_REFERENCE) {
3919 reference_expression_t *reference = (reference_expression_t*) value;
3920 declaration_t *declaration = reference->declaration;
3921 if(declaration != NULL) {
3922 declaration->address_taken = 1;
3926 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3929 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3930 static expression_t *parse_##unexpression_type(unsigned precedence) \
3934 expression_t *unary_expression \
3935 = allocate_expression_zero(unexpression_type); \
3936 unary_expression->unary.value = parse_sub_expression(precedence); \
3938 sfunc(&unary_expression->unary); \
3940 return unary_expression; \
3943 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
3944 semantic_unexpr_arithmetic)
3945 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
3946 semantic_unexpr_arithmetic)
3947 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
3948 semantic_unexpr_scalar)
3949 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
3950 semantic_dereference)
3951 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
3953 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
3954 semantic_unexpr_integer)
3955 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
3957 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
3960 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3962 static expression_t *parse_##unexpression_type(unsigned precedence, \
3963 expression_t *left) \
3965 (void) precedence; \
3968 expression_t *unary_expression \
3969 = allocate_expression_zero(unexpression_type); \
3970 unary_expression->unary.value = left; \
3972 sfunc(&unary_expression->unary); \
3974 return unary_expression; \
3977 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
3978 EXPR_UNARY_POSTFIX_INCREMENT,
3980 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
3981 EXPR_UNARY_POSTFIX_DECREMENT,
3984 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3986 /* TODO: handle complex + imaginary types */
3988 /* § 6.3.1.8 Usual arithmetic conversions */
3989 if(type_left == type_long_double || type_right == type_long_double) {
3990 return type_long_double;
3991 } else if(type_left == type_double || type_right == type_double) {
3993 } else if(type_left == type_float || type_right == type_float) {
3997 type_right = promote_integer(type_right);
3998 type_left = promote_integer(type_left);
4000 if(type_left == type_right)
4003 bool signed_left = is_type_signed(type_left);
4004 bool signed_right = is_type_signed(type_right);
4005 int rank_left = get_rank(type_left);
4006 int rank_right = get_rank(type_right);
4007 if(rank_left < rank_right) {
4008 if(signed_left == signed_right || !signed_right) {
4014 if(signed_left == signed_right || !signed_left) {
4022 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4024 expression_t *left = expression->left;
4025 expression_t *right = expression->right;
4026 type_t *orig_type_left = left->base.datatype;
4027 type_t *orig_type_right = right->base.datatype;
4029 if(orig_type_left == NULL || orig_type_right == NULL)
4032 type_t *type_left = skip_typeref(orig_type_left);
4033 type_t *type_right = skip_typeref(orig_type_right);
4035 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4036 /* TODO: improve error message */
4037 parser_print_error_prefix();
4038 fprintf(stderr, "operation needs arithmetic types\n");
4042 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4043 expression->left = create_implicit_cast(left, arithmetic_type);
4044 expression->right = create_implicit_cast(right, arithmetic_type);
4045 expression->expression.datatype = arithmetic_type;
4048 static void semantic_shift_op(binary_expression_t *expression)
4050 expression_t *left = expression->left;
4051 expression_t *right = expression->right;
4052 type_t *orig_type_left = left->base.datatype;
4053 type_t *orig_type_right = right->base.datatype;
4055 if(orig_type_left == NULL || orig_type_right == NULL)
4058 type_t *type_left = skip_typeref(orig_type_left);
4059 type_t *type_right = skip_typeref(orig_type_right);
4061 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4062 /* TODO: improve error message */
4063 parser_print_error_prefix();
4064 fprintf(stderr, "operation needs integer types\n");
4068 type_left = promote_integer(type_left);
4069 type_right = promote_integer(type_right);
4071 expression->left = create_implicit_cast(left, type_left);
4072 expression->right = create_implicit_cast(right, type_right);
4073 expression->expression.datatype = type_left;
4076 static void semantic_add(binary_expression_t *expression)
4078 expression_t *left = expression->left;
4079 expression_t *right = expression->right;
4080 type_t *orig_type_left = left->base.datatype;
4081 type_t *orig_type_right = right->base.datatype;
4083 if(orig_type_left == NULL || orig_type_right == NULL)
4086 type_t *type_left = skip_typeref(orig_type_left);
4087 type_t *type_right = skip_typeref(orig_type_right);
4090 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4091 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4092 expression->left = create_implicit_cast(left, arithmetic_type);
4093 expression->right = create_implicit_cast(right, arithmetic_type);
4094 expression->expression.datatype = arithmetic_type;
4096 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4097 expression->expression.datatype = type_left;
4098 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4099 expression->expression.datatype = type_right;
4101 parser_print_error_prefix();
4102 fprintf(stderr, "invalid operands to binary + (");
4103 print_type_quoted(orig_type_left);
4104 fprintf(stderr, ", ");
4105 print_type_quoted(orig_type_right);
4106 fprintf(stderr, ")\n");
4110 static void semantic_sub(binary_expression_t *expression)
4112 expression_t *left = expression->left;
4113 expression_t *right = expression->right;
4114 type_t *orig_type_left = left->base.datatype;
4115 type_t *orig_type_right = right->base.datatype;
4117 if(orig_type_left == NULL || orig_type_right == NULL)
4120 type_t *type_left = skip_typeref(orig_type_left);
4121 type_t *type_right = skip_typeref(orig_type_right);
4124 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4125 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4126 expression->left = create_implicit_cast(left, arithmetic_type);
4127 expression->right = create_implicit_cast(right, arithmetic_type);
4128 expression->expression.datatype = arithmetic_type;
4130 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4131 expression->expression.datatype = type_left;
4132 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4133 if(!pointers_compatible(type_left, type_right)) {
4134 parser_print_error_prefix();
4135 fprintf(stderr, "pointers to incompatible objects to binary - (");
4136 print_type_quoted(orig_type_left);
4137 fprintf(stderr, ", ");
4138 print_type_quoted(orig_type_right);
4139 fprintf(stderr, ")\n");
4141 expression->expression.datatype = type_ptrdiff_t;
4144 parser_print_error_prefix();
4145 fprintf(stderr, "invalid operands to binary - (");
4146 print_type_quoted(orig_type_left);
4147 fprintf(stderr, ", ");
4148 print_type_quoted(orig_type_right);
4149 fprintf(stderr, ")\n");
4153 static void semantic_comparison(binary_expression_t *expression)
4155 expression_t *left = expression->left;
4156 expression_t *right = expression->right;
4157 type_t *orig_type_left = left->base.datatype;
4158 type_t *orig_type_right = right->base.datatype;
4160 if(orig_type_left == NULL || orig_type_right == NULL)
4163 type_t *type_left = skip_typeref(orig_type_left);
4164 type_t *type_right = skip_typeref(orig_type_right);
4166 /* TODO non-arithmetic types */
4167 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4168 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4169 expression->left = create_implicit_cast(left, arithmetic_type);
4170 expression->right = create_implicit_cast(right, arithmetic_type);
4171 expression->expression.datatype = arithmetic_type;
4172 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4173 /* TODO check compatibility */
4174 } else if (is_type_pointer(type_left)) {
4175 expression->right = create_implicit_cast(right, type_left);
4176 } else if (is_type_pointer(type_right)) {
4177 expression->left = create_implicit_cast(left, type_right);
4179 type_error_incompatible("invalid operands in comparison",
4180 token.source_position, type_left, type_right);
4182 expression->expression.datatype = type_int;
4185 static void semantic_arithmetic_assign(binary_expression_t *expression)
4187 expression_t *left = expression->left;
4188 expression_t *right = expression->right;
4189 type_t *orig_type_left = left->base.datatype;
4190 type_t *orig_type_right = right->base.datatype;
4192 if(orig_type_left == NULL || orig_type_right == NULL)
4195 type_t *type_left = skip_typeref(orig_type_left);
4196 type_t *type_right = skip_typeref(orig_type_right);
4198 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4199 /* TODO: improve error message */
4200 parser_print_error_prefix();
4201 fprintf(stderr, "operation needs arithmetic types\n");
4205 /* combined instructions are tricky. We can't create an implicit cast on
4206 * the left side, because we need the uncasted form for the store.
4207 * The ast2firm pass has to know that left_type must be right_type
4208 * for the arithmeitc operation and create a cast by itself */
4209 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4210 expression->right = create_implicit_cast(right, arithmetic_type);
4211 expression->expression.datatype = type_left;
4214 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4216 expression_t *left = expression->left;
4217 expression_t *right = expression->right;
4218 type_t *orig_type_left = left->base.datatype;
4219 type_t *orig_type_right = right->base.datatype;
4221 if(orig_type_left == NULL || orig_type_right == NULL)
4224 type_t *type_left = skip_typeref(orig_type_left);
4225 type_t *type_right = skip_typeref(orig_type_right);
4227 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4228 /* combined instructions are tricky. We can't create an implicit cast on
4229 * the left side, because we need the uncasted form for the store.
4230 * The ast2firm pass has to know that left_type must be right_type
4231 * for the arithmeitc operation and create a cast by itself */
4232 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4233 expression->right = create_implicit_cast(right, arithmetic_type);
4234 expression->expression.datatype = type_left;
4235 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4236 expression->expression.datatype = type_left;
4238 parser_print_error_prefix();
4239 fputs("Incompatible types ", stderr);
4240 print_type_quoted(orig_type_left);
4241 fputs(" and ", stderr);
4242 print_type_quoted(orig_type_right);
4243 fputs(" in assignment\n", stderr);
4248 static void semantic_logical_op(binary_expression_t *expression)
4250 expression_t *left = expression->left;
4251 expression_t *right = expression->right;
4252 type_t *orig_type_left = left->base.datatype;
4253 type_t *orig_type_right = right->base.datatype;
4255 if(orig_type_left == NULL || orig_type_right == NULL)
4258 type_t *type_left = skip_typeref(orig_type_left);
4259 type_t *type_right = skip_typeref(orig_type_right);
4261 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4262 /* TODO: improve error message */
4263 parser_print_error_prefix();
4264 fprintf(stderr, "operation needs scalar types\n");
4268 expression->expression.datatype = type_int;
4271 static bool has_const_fields(type_t *type)
4278 static void semantic_binexpr_assign(binary_expression_t *expression)
4280 expression_t *left = expression->left;
4281 type_t *orig_type_left = left->base.datatype;
4283 if(orig_type_left == NULL)
4286 type_t *type_left = revert_automatic_type_conversion(left);
4287 type_left = skip_typeref(orig_type_left);
4289 /* must be a modifiable lvalue */
4290 if (is_type_array(type_left)) {
4291 parser_print_error_prefix();
4292 fprintf(stderr, "Cannot assign to arrays ('");
4293 print_expression(left);
4294 fprintf(stderr, "')\n");
4297 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4298 parser_print_error_prefix();
4299 fprintf(stderr, "assignment to readonly location '");
4300 print_expression(left);
4301 fprintf(stderr, "' (type ");
4302 print_type_quoted(orig_type_left);
4303 fprintf(stderr, ")\n");
4306 if(is_type_incomplete(type_left)) {
4307 parser_print_error_prefix();
4308 fprintf(stderr, "left-hand side of assignment '");
4309 print_expression(left);
4310 fprintf(stderr, "' has incomplete type ");
4311 print_type_quoted(orig_type_left);
4312 fprintf(stderr, "\n");
4315 if(is_type_compound(type_left) && has_const_fields(type_left)) {
4316 parser_print_error_prefix();
4317 fprintf(stderr, "can't assign to '");
4318 print_expression(left);
4319 fprintf(stderr, "' because compound type ");
4320 print_type_quoted(orig_type_left);
4321 fprintf(stderr, " has readonly fields\n");
4325 semantic_assign(orig_type_left, &expression->right, "assignment");
4327 expression->expression.datatype = orig_type_left;
4330 static void semantic_comma(binary_expression_t *expression)
4332 expression->expression.datatype = expression->right->base.datatype;
4335 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4336 static expression_t *parse_##binexpression_type(unsigned precedence, \
4337 expression_t *left) \
4341 expression_t *right = parse_sub_expression(precedence + lr); \
4343 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4344 binexpr->binary.left = left; \
4345 binexpr->binary.right = right; \
4346 sfunc(&binexpr->binary); \
4351 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4352 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4353 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4354 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4355 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4356 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4357 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4358 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4359 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4361 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4362 semantic_comparison, 1)
4363 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4364 semantic_comparison, 1)
4365 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4366 semantic_comparison, 1)
4367 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4368 semantic_comparison, 1)
4370 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4371 semantic_binexpr_arithmetic, 1)
4372 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4373 semantic_binexpr_arithmetic, 1)
4374 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4375 semantic_binexpr_arithmetic, 1)
4376 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4377 semantic_logical_op, 1)
4378 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4379 semantic_logical_op, 1)
4380 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4381 semantic_shift_op, 1)
4382 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4383 semantic_shift_op, 1)
4384 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4385 semantic_arithmetic_addsubb_assign, 0)
4386 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4387 semantic_arithmetic_addsubb_assign, 0)
4388 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4389 semantic_arithmetic_assign, 0)
4390 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4391 semantic_arithmetic_assign, 0)
4392 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4393 semantic_arithmetic_assign, 0)
4394 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4395 semantic_arithmetic_assign, 0)
4396 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4397 semantic_arithmetic_assign, 0)
4398 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4399 semantic_arithmetic_assign, 0)
4400 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4401 semantic_arithmetic_assign, 0)
4402 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4403 semantic_arithmetic_assign, 0)
4405 static expression_t *parse_sub_expression(unsigned precedence)
4407 if(token.type < 0) {
4408 return expected_expression_error();
4411 expression_parser_function_t *parser
4412 = &expression_parsers[token.type];
4413 source_position_t source_position = token.source_position;
4416 if(parser->parser != NULL) {
4417 left = parser->parser(parser->precedence);
4419 left = parse_primary_expression();
4421 assert(left != NULL);
4422 left->base.source_position = source_position;
4425 if(token.type < 0) {
4426 return expected_expression_error();
4429 parser = &expression_parsers[token.type];
4430 if(parser->infix_parser == NULL)
4432 if(parser->infix_precedence < precedence)
4435 left = parser->infix_parser(parser->infix_precedence, left);
4437 assert(left != NULL);
4438 assert(left->type != EXPR_UNKNOWN);
4439 left->base.source_position = source_position;
4445 static expression_t *parse_expression(void)
4447 return parse_sub_expression(1);
4452 static void register_expression_parser(parse_expression_function parser,
4453 int token_type, unsigned precedence)
4455 expression_parser_function_t *entry = &expression_parsers[token_type];
4457 if(entry->parser != NULL) {
4458 fprintf(stderr, "for token ");
4459 print_token_type(stderr, (token_type_t) token_type);
4460 fprintf(stderr, "\n");
4461 panic("trying to register multiple expression parsers for a token");
4463 entry->parser = parser;
4464 entry->precedence = precedence;
4467 static void register_infix_parser(parse_expression_infix_function parser,
4468 int token_type, unsigned precedence)
4470 expression_parser_function_t *entry = &expression_parsers[token_type];
4472 if(entry->infix_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 infix expression parsers for a "
4479 entry->infix_parser = parser;
4480 entry->infix_precedence = precedence;
4483 static void init_expression_parsers(void)
4485 memset(&expression_parsers, 0, sizeof(expression_parsers));
4487 register_infix_parser(parse_array_expression, '[', 30);
4488 register_infix_parser(parse_call_expression, '(', 30);
4489 register_infix_parser(parse_select_expression, '.', 30);
4490 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4491 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4493 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4496 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4497 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4498 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4499 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4500 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4501 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4502 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4503 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4504 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4505 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4506 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4507 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4508 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4509 T_EXCLAMATIONMARKEQUAL, 13);
4510 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4511 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4512 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4513 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4514 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4515 register_infix_parser(parse_conditional_expression, '?', 7);
4516 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4517 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4518 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4519 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4520 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4521 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4522 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4523 T_LESSLESSEQUAL, 2);
4524 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4525 T_GREATERGREATEREQUAL, 2);
4526 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4528 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4530 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4533 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4535 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4536 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4537 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4538 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4539 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4540 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4541 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4543 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4545 register_expression_parser(parse_sizeof, T_sizeof, 25);
4546 register_expression_parser(parse_extension, T___extension__, 25);
4547 register_expression_parser(parse_builtin_classify_type,
4548 T___builtin_classify_type, 25);
4551 static asm_constraint_t *parse_asm_constraints(void)
4553 asm_constraint_t *result = NULL;
4554 asm_constraint_t *last = NULL;
4556 while(token.type == T_STRING_LITERAL || token.type == '[') {
4557 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4558 memset(constraint, 0, sizeof(constraint[0]));
4560 if(token.type == '[') {
4562 if(token.type != T_IDENTIFIER) {
4563 parse_error_expected("while parsing asm constraint",
4567 constraint->symbol = token.v.symbol;
4572 constraint->constraints = parse_string_literals();
4574 constraint->expression = parse_expression();
4578 last->next = constraint;
4580 result = constraint;
4584 if(token.type != ',')
4592 static asm_clobber_t *parse_asm_clobbers(void)
4594 asm_clobber_t *result = NULL;
4595 asm_clobber_t *last = NULL;
4597 while(token.type == T_STRING_LITERAL) {
4598 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4599 clobber->clobber = parse_string_literals();
4602 last->next = clobber;
4608 if(token.type != ',')
4616 static statement_t *parse_asm_statement(void)
4620 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4621 statement->base.source_position = token.source_position;
4623 asm_statement_t *asm_statement = &statement->asms;
4625 if(token.type == T_volatile) {
4627 asm_statement->is_volatile = true;
4631 asm_statement->asm_text = parse_string_literals();
4633 if(token.type != ':')
4637 asm_statement->inputs = parse_asm_constraints();
4638 if(token.type != ':')
4642 asm_statement->outputs = parse_asm_constraints();
4643 if(token.type != ':')
4647 asm_statement->clobbers = parse_asm_clobbers();
4655 static statement_t *parse_case_statement(void)
4659 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4661 statement->base.source_position = token.source_position;
4662 statement->case_label.expression = parse_expression();
4665 statement->case_label.label_statement = parse_statement();
4670 static statement_t *parse_default_statement(void)
4674 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4676 statement->base.source_position = token.source_position;
4679 statement->label.label_statement = parse_statement();
4684 static declaration_t *get_label(symbol_t *symbol)
4686 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4687 assert(current_function != NULL);
4688 /* if we found a label in the same function, then we already created the
4690 if(candidate != NULL
4691 && candidate->parent_context == ¤t_function->context) {
4695 /* otherwise we need to create a new one */
4696 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4697 declaration->namespc = NAMESPACE_LABEL;
4698 declaration->symbol = symbol;
4700 label_push(declaration);
4705 static statement_t *parse_label_statement(void)
4707 assert(token.type == T_IDENTIFIER);
4708 symbol_t *symbol = token.v.symbol;
4711 declaration_t *label = get_label(symbol);
4713 /* if source position is already set then the label is defined twice,
4714 * otherwise it was just mentioned in a goto so far */
4715 if(label->source_position.input_name != NULL) {
4716 parser_print_error_prefix();
4717 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
4718 parser_print_error_prefix_pos(label->source_position);
4719 fprintf(stderr, "previous definition of '%s' was here\n",
4722 label->source_position = token.source_position;
4725 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4727 label_statement->statement.type = STATEMENT_LABEL;
4728 label_statement->statement.source_position = token.source_position;
4729 label_statement->label = label;
4733 if(token.type == '}') {
4734 parse_error("label at end of compound statement");
4735 return (statement_t*) label_statement;
4737 label_statement->label_statement = parse_statement();
4740 return (statement_t*) label_statement;
4743 static statement_t *parse_if(void)
4747 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4748 statement->statement.type = STATEMENT_IF;
4749 statement->statement.source_position = token.source_position;
4752 statement->condition = parse_expression();
4755 statement->true_statement = parse_statement();
4756 if(token.type == T_else) {
4758 statement->false_statement = parse_statement();
4761 return (statement_t*) statement;
4764 static statement_t *parse_switch(void)
4768 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4769 statement->statement.type = STATEMENT_SWITCH;
4770 statement->statement.source_position = token.source_position;
4773 statement->expression = parse_expression();
4775 statement->body = parse_statement();
4777 return (statement_t*) statement;
4780 static statement_t *parse_while(void)
4784 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4785 statement->statement.type = STATEMENT_WHILE;
4786 statement->statement.source_position = token.source_position;
4789 statement->condition = parse_expression();
4791 statement->body = parse_statement();
4793 return (statement_t*) statement;
4796 static statement_t *parse_do(void)
4800 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4801 statement->statement.type = STATEMENT_DO_WHILE;
4802 statement->statement.source_position = token.source_position;
4804 statement->body = parse_statement();
4807 statement->condition = parse_expression();
4811 return (statement_t*) statement;
4814 static statement_t *parse_for(void)
4818 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4819 statement->statement.type = STATEMENT_FOR;
4820 statement->statement.source_position = token.source_position;
4824 int top = environment_top();
4825 context_t *last_context = context;
4826 set_context(&statement->context);
4828 if(token.type != ';') {
4829 if(is_declaration_specifier(&token, false)) {
4830 parse_declaration(record_declaration);
4832 statement->initialisation = parse_expression();
4839 if(token.type != ';') {
4840 statement->condition = parse_expression();
4843 if(token.type != ')') {
4844 statement->step = parse_expression();
4847 statement->body = parse_statement();
4849 assert(context == &statement->context);
4850 set_context(last_context);
4851 environment_pop_to(top);
4853 return (statement_t*) statement;
4856 static statement_t *parse_goto(void)
4860 if(token.type != T_IDENTIFIER) {
4861 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4865 symbol_t *symbol = token.v.symbol;
4868 declaration_t *label = get_label(symbol);
4870 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4872 statement->statement.type = STATEMENT_GOTO;
4873 statement->statement.source_position = token.source_position;
4875 statement->label = label;
4879 return (statement_t*) statement;
4882 static statement_t *parse_continue(void)
4887 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4888 statement->type = STATEMENT_CONTINUE;
4889 statement->base.source_position = token.source_position;
4894 static statement_t *parse_break(void)
4899 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4900 statement->type = STATEMENT_BREAK;
4901 statement->base.source_position = token.source_position;
4906 static statement_t *parse_return(void)
4910 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4912 statement->statement.type = STATEMENT_RETURN;
4913 statement->statement.source_position = token.source_position;
4915 assert(is_type_function(current_function->type));
4916 function_type_t *function_type = ¤t_function->type->function;
4917 type_t *return_type = function_type->return_type;
4919 expression_t *return_value = NULL;
4920 if(token.type != ';') {
4921 return_value = parse_expression();
4925 if(return_type == NULL)
4926 return (statement_t*) statement;
4928 return_type = skip_typeref(return_type);
4930 if(return_value != NULL) {
4931 type_t *return_value_type = skip_typeref(return_value->base.datatype);
4933 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
4934 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
4935 parse_warning("'return' with a value, in function returning void");
4936 return_value = NULL;
4938 if(return_type != NULL) {
4939 semantic_assign(return_type, &return_value, "'return'");
4943 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4944 parse_warning("'return' without value, in function returning "
4948 statement->return_value = return_value;
4950 return (statement_t*) statement;
4953 static statement_t *parse_declaration_statement(void)
4955 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
4957 statement->base.source_position = token.source_position;
4959 declaration_t *before = last_declaration;
4960 parse_declaration(record_declaration);
4962 if(before == NULL) {
4963 statement->declaration.declarations_begin = context->declarations;
4965 statement->declaration.declarations_begin = before->next;
4967 statement->declaration.declarations_end = last_declaration;
4972 static statement_t *parse_expression_statement(void)
4974 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
4976 statement->base.source_position = token.source_position;
4977 statement->expression.expression = parse_expression();
4984 static statement_t *parse_statement(void)
4986 statement_t *statement = NULL;
4988 /* declaration or statement */
4989 switch(token.type) {
4991 statement = parse_asm_statement();
4995 statement = parse_case_statement();
4999 statement = parse_default_statement();
5003 statement = parse_compound_statement();
5007 statement = parse_if();
5011 statement = parse_switch();
5015 statement = parse_while();
5019 statement = parse_do();
5023 statement = parse_for();
5027 statement = parse_goto();
5031 statement = parse_continue();
5035 statement = parse_break();
5039 statement = parse_return();
5048 if(look_ahead(1)->type == ':') {
5049 statement = parse_label_statement();
5053 if(is_typedef_symbol(token.v.symbol)) {
5054 statement = parse_declaration_statement();
5058 statement = parse_expression_statement();
5061 case T___extension__:
5062 /* this can be a prefix to a declaration or an expression statement */
5063 /* we simply eat it now and parse the rest with tail recursion */
5066 } while(token.type == T___extension__);
5067 statement = parse_statement();
5071 statement = parse_declaration_statement();
5075 statement = parse_expression_statement();
5079 assert(statement == NULL
5080 || statement->base.source_position.input_name != NULL);
5085 static statement_t *parse_compound_statement(void)
5087 compound_statement_t *compound_statement
5088 = allocate_ast_zero(sizeof(compound_statement[0]));
5089 compound_statement->statement.type = STATEMENT_COMPOUND;
5090 compound_statement->statement.source_position = token.source_position;
5094 int top = environment_top();
5095 context_t *last_context = context;
5096 set_context(&compound_statement->context);
5098 statement_t *last_statement = NULL;
5100 while(token.type != '}' && token.type != T_EOF) {
5101 statement_t *statement = parse_statement();
5102 if(statement == NULL)
5105 if(last_statement != NULL) {
5106 last_statement->base.next = statement;
5108 compound_statement->statements = statement;
5111 while(statement->base.next != NULL)
5112 statement = statement->base.next;
5114 last_statement = statement;
5117 if(token.type != '}') {
5118 parser_print_error_prefix_pos(
5119 compound_statement->statement.source_position);
5120 fprintf(stderr, "end of file while looking for closing '}'\n");
5124 assert(context == &compound_statement->context);
5125 set_context(last_context);
5126 environment_pop_to(top);
5128 return (statement_t*) compound_statement;
5131 static void initialize_builtins(void)
5133 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5134 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5135 type_size_t = make_global_typedef("__SIZE_TYPE__",
5136 make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_NONE));
5137 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__",
5138 make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_NONE));
5141 static translation_unit_t *parse_translation_unit(void)
5143 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5145 assert(global_context == NULL);
5146 global_context = &unit->context;
5148 assert(context == NULL);
5149 set_context(&unit->context);
5151 initialize_builtins();
5153 while(token.type != T_EOF) {
5154 parse_external_declaration();
5157 assert(context == &unit->context);
5159 last_declaration = NULL;
5161 assert(global_context == &unit->context);
5162 global_context = NULL;
5167 translation_unit_t *parse(void)
5169 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5170 label_stack = NEW_ARR_F(stack_entry_t, 0);
5171 found_error = false;
5173 type_set_output(stderr);
5174 ast_set_output(stderr);
5176 lookahead_bufpos = 0;
5177 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5180 translation_unit_t *unit = parse_translation_unit();
5182 DEL_ARR_F(environment_stack);
5183 DEL_ARR_F(label_stack);
5191 void init_parser(void)
5193 init_expression_parsers();
5194 obstack_init(&temp_obst);
5196 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
5197 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE,
5198 TYPE_QUALIFIER_NONE);
5199 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE,
5200 TYPE_QUALIFIER_NONE);
5201 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_NONE);
5202 type_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_NONE);
5203 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
5204 type_void_ptr = make_pointer_type(type_void, TYPE_QUALIFIER_NONE);
5205 type_string = make_pointer_type(type_char, TYPE_QUALIFIER_NONE);
5207 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5208 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5211 void exit_parser(void)
5213 obstack_free(&temp_obst, NULL);