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;
59 type_t *type_size_t = NULL;
60 type_t *type_ptrdiff_t = NULL;
61 type_t *type_wchar_t = NULL;
62 type_t *type_wchar_t_ptr = NULL;
64 static statement_t *parse_compound_statement(void);
65 static statement_t *parse_statement(void);
67 static expression_t *parse_sub_expression(unsigned precedence);
68 static expression_t *parse_expression(void);
69 static type_t *parse_typename(void);
71 static void parse_compound_type_entries(void);
72 static declaration_t *parse_declarator(
73 const declaration_specifiers_t *specifiers, bool may_be_abstract);
74 static declaration_t *record_declaration(declaration_t *declaration);
76 #define STORAGE_CLASSES \
83 #define TYPE_QUALIFIERS \
89 #ifdef PROVIDE_COMPLEX
90 #define COMPLEX_SPECIFIERS \
92 #define IMAGINARY_SPECIFIERS \
95 #define COMPLEX_SPECIFIERS
96 #define IMAGINARY_SPECIFIERS
99 #define TYPE_SPECIFIERS \
117 #define DECLARATION_START \
122 #define TYPENAME_START \
126 static void *allocate_ast_zero(size_t size)
128 void *res = allocate_ast(size);
129 memset(res, 0, size);
133 static size_t get_statement_struct_size(statement_type_t type)
135 static const size_t sizes[] = {
136 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
137 [STATEMENT_RETURN] = sizeof(return_statement_t),
138 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
139 [STATEMENT_IF] = sizeof(if_statement_t),
140 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
141 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
142 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
143 [STATEMENT_BREAK] = sizeof(statement_base_t),
144 [STATEMENT_GOTO] = sizeof(goto_statement_t),
145 [STATEMENT_LABEL] = sizeof(label_statement_t),
146 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
147 [STATEMENT_WHILE] = sizeof(while_statement_t),
148 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
149 [STATEMENT_FOR] = sizeof(for_statement_t),
150 [STATEMENT_ASM] = sizeof(asm_statement_t)
152 assert(sizeof(sizes) / sizeof(sizes[0]) == STATEMENT_ASM + 1);
153 assert(type <= STATEMENT_ASM);
154 assert(sizes[type] != 0);
158 static statement_t *allocate_statement_zero(statement_type_t type)
160 size_t size = get_statement_struct_size(type);
161 statement_t *res = allocate_ast_zero(size);
163 res->base.type = type;
168 static size_t get_expression_struct_size(expression_type_t type)
170 static const size_t sizes[] = {
171 [EXPR_INVALID] = sizeof(expression_base_t),
172 [EXPR_REFERENCE] = sizeof(reference_expression_t),
173 [EXPR_CONST] = sizeof(const_expression_t),
174 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
175 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
176 [EXPR_CALL] = sizeof(call_expression_t),
177 [EXPR_UNARY] = sizeof(unary_expression_t),
178 [EXPR_BINARY] = sizeof(binary_expression_t),
179 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
180 [EXPR_SELECT] = sizeof(select_expression_t),
181 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
182 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
183 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
184 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
185 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
186 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
187 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
188 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
189 [EXPR_STATEMENT] = sizeof(statement_expression_t)
191 assert(sizeof(sizes) / sizeof(sizes[0]) == EXPR_STATEMENT + 1);
192 assert(type <= EXPR_STATEMENT);
193 assert(sizes[type] != 0);
197 static expression_t *allocate_expression_zero(expression_type_t type)
199 size_t size = get_expression_struct_size(type);
200 expression_t *res = allocate_ast_zero(size);
202 res->base.type = type;
206 static size_t get_type_struct_size(type_type_t type)
208 static const size_t sizes[] = {
209 [TYPE_ATOMIC] = sizeof(atomic_type_t),
210 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
211 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
212 [TYPE_ENUM] = sizeof(enum_type_t),
213 [TYPE_FUNCTION] = sizeof(function_type_t),
214 [TYPE_POINTER] = sizeof(pointer_type_t),
215 [TYPE_ARRAY] = sizeof(array_type_t),
216 [TYPE_BUILTIN] = sizeof(builtin_type_t),
217 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
218 [TYPE_TYPEOF] = sizeof(typeof_type_t),
220 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
221 assert(type <= TYPE_TYPEOF);
222 assert(sizes[type] != 0);
226 static type_t *allocate_type_zero(type_type_t type)
228 size_t size = get_type_struct_size(type);
229 type_t *res = obstack_alloc(type_obst, size);
230 memset(res, 0, size);
232 res->base.type = type;
236 static size_t get_initializer_size(initializer_type_t type)
238 static const size_t sizes[] = {
239 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
240 [INITIALIZER_STRING] = sizeof(initializer_string_t),
241 [INITIALIZER_LIST] = sizeof(initializer_list_t)
243 assert(type < INITIALIZER_COUNT);
244 assert(sizes[type] != 0);
248 static initializer_t *allocate_initializer(initializer_type_t type)
250 initializer_t *result = allocate_ast_zero(get_initializer_size(type));
256 static void free_type(void *type)
258 obstack_free(type_obst, type);
262 * returns the top element of the environment stack
264 static size_t environment_top(void)
266 return ARR_LEN(environment_stack);
269 static size_t label_top(void)
271 return ARR_LEN(label_stack);
276 static inline void next_token(void)
278 token = lookahead_buffer[lookahead_bufpos];
279 lookahead_buffer[lookahead_bufpos] = lexer_token;
282 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
285 print_token(stderr, &token);
286 fprintf(stderr, "\n");
290 static inline const token_t *look_ahead(int num)
292 assert(num > 0 && num <= MAX_LOOKAHEAD);
293 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
294 return &lookahead_buffer[pos];
297 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
299 static void error(void)
302 #ifdef ABORT_ON_ERROR
307 static void parser_print_prefix_pos(const source_position_t source_position)
309 fputs(source_position.input_name, stderr);
311 fprintf(stderr, "%u", source_position.linenr);
315 static void parser_print_error_prefix_pos(
316 const source_position_t source_position)
318 parser_print_prefix_pos(source_position);
319 fputs("error: ", stderr);
323 static void parser_print_error_prefix(void)
325 parser_print_error_prefix_pos(token.source_position);
328 static void parse_error(const char *message)
330 parser_print_error_prefix();
331 fprintf(stderr, "parse error: %s\n", message);
334 static void parser_print_warning_prefix_pos(
335 const source_position_t source_position)
337 parser_print_prefix_pos(source_position);
338 fputs("warning: ", stderr);
341 static void parser_print_warning_prefix(void)
343 parser_print_warning_prefix_pos(token.source_position);
346 static void parse_warning_pos(const source_position_t source_position,
347 const char *const message)
349 parser_print_prefix_pos(source_position);
350 fprintf(stderr, "warning: %s\n", message);
353 static void parse_warning(const char *message)
355 parse_warning_pos(token.source_position, message);
358 static void parse_error_expected(const char *message, ...)
363 if(message != NULL) {
364 parser_print_error_prefix();
365 fprintf(stderr, "%s\n", message);
367 parser_print_error_prefix();
368 fputs("Parse error: got ", stderr);
369 print_token(stderr, &token);
370 fputs(", expected ", stderr);
372 va_start(args, message);
373 token_type_t token_type = va_arg(args, token_type_t);
374 while(token_type != 0) {
378 fprintf(stderr, ", ");
380 print_token_type(stderr, token_type);
381 token_type = va_arg(args, token_type_t);
384 fprintf(stderr, "\n");
387 static void print_type_quoted(type_t *type)
394 static void type_error(const char *msg, const source_position_t source_position,
397 parser_print_error_prefix_pos(source_position);
398 fprintf(stderr, "%s, but found type ", msg);
399 print_type_quoted(type);
403 static void type_error_incompatible(const char *msg,
404 const source_position_t source_position, type_t *type1, type_t *type2)
406 parser_print_error_prefix_pos(source_position);
407 fprintf(stderr, "%s, incompatible types: ", msg);
408 print_type_quoted(type1);
409 fprintf(stderr, " - ");
410 print_type_quoted(type2);
411 fprintf(stderr, ")\n");
414 static void eat_block(void)
416 if(token.type == '{')
419 while(token.type != '}') {
420 if(token.type == T_EOF)
422 if(token.type == '{') {
431 static void eat_statement(void)
433 while(token.type != ';') {
434 if(token.type == T_EOF)
436 if(token.type == '}')
438 if(token.type == '{') {
447 static void eat_brace(void)
449 if(token.type == '(')
452 while(token.type != ')') {
453 if(token.type == T_EOF)
455 if(token.type == ')' || token.type == ';' || token.type == '}') {
458 if(token.type == '(') {
462 if(token.type == '{') {
471 #define expect(expected) \
472 if(UNLIKELY(token.type != (expected))) { \
473 parse_error_expected(NULL, (expected), 0); \
479 #define expect_block(expected) \
480 if(UNLIKELY(token.type != (expected))) { \
481 parse_error_expected(NULL, (expected), 0); \
487 #define expect_void(expected) \
488 if(UNLIKELY(token.type != (expected))) { \
489 parse_error_expected(NULL, (expected), 0); \
495 static void set_context(context_t *new_context)
497 context = new_context;
499 last_declaration = new_context->declarations;
500 if(last_declaration != NULL) {
501 while(last_declaration->next != NULL) {
502 last_declaration = last_declaration->next;
508 * called when we find a 2nd declarator for an identifier we already have a
511 static bool is_compatible_declaration(declaration_t *declaration,
512 declaration_t *previous)
514 /* happens for K&R style function parameters */
515 if(previous->type == NULL) {
516 previous->type = declaration->type;
520 /* shortcur, same types are always compatible */
521 if(declaration->type == previous->type)
524 if (declaration->type->type == TYPE_FUNCTION &&
525 previous->type->type == TYPE_FUNCTION) {
526 function_type_t* const prev_func = &previous->type->function;
527 function_type_t* const decl_func = &declaration->type->function;
529 /* 1 of the 2 declarations might have unspecified parameters */
530 if(decl_func->unspecified_parameters) {
532 } else if(prev_func->unspecified_parameters) {
533 declaration->type = previous->type;
538 /* TODO: not correct/complete yet */
542 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
544 declaration_t *declaration = symbol->declaration;
545 for( ; declaration != NULL; declaration = declaration->symbol_next) {
546 if(declaration->namespc == namespc)
553 static const char *get_namespace_prefix(namespace_t namespc)
556 case NAMESPACE_NORMAL:
558 case NAMESPACE_UNION:
560 case NAMESPACE_STRUCT:
564 case NAMESPACE_LABEL:
567 panic("invalid namespace found");
571 * pushs an environment_entry on the environment stack and links the
572 * corresponding symbol to the new entry
574 static declaration_t *stack_push(stack_entry_t **stack_ptr,
575 declaration_t *declaration,
576 context_t *parent_context)
578 symbol_t *symbol = declaration->symbol;
579 namespace_t namespc = (namespace_t)declaration->namespc;
581 /* a declaration should be only pushed once */
582 declaration->parent_context = parent_context;
584 declaration_t *previous_declaration = get_declaration(symbol, namespc);
585 assert(declaration != previous_declaration);
586 if(previous_declaration != NULL
587 && previous_declaration->parent_context == context) {
588 if(!is_compatible_declaration(declaration, previous_declaration)) {
589 parser_print_error_prefix_pos(declaration->source_position);
590 fprintf(stderr, "definition of symbol %s%s with type ",
591 get_namespace_prefix(namespc), symbol->string);
592 print_type_quoted(declaration->type);
594 parser_print_error_prefix_pos(
595 previous_declaration->source_position);
596 fprintf(stderr, "is incompatible with previous declaration "
598 print_type_quoted(previous_declaration->type);
601 unsigned old_storage_class = previous_declaration->storage_class;
602 unsigned new_storage_class = declaration->storage_class;
603 if (current_function == NULL) {
604 if (old_storage_class != STORAGE_CLASS_STATIC &&
605 new_storage_class == STORAGE_CLASS_STATIC) {
606 parser_print_error_prefix_pos(declaration->source_position);
608 "static declaration of '%s' follows non-static declaration\n",
610 parser_print_error_prefix_pos(previous_declaration->source_position);
611 fprintf(stderr, "previous declaration of '%s' was here\n",
614 if (old_storage_class == STORAGE_CLASS_EXTERN) {
615 if (new_storage_class == STORAGE_CLASS_NONE) {
616 previous_declaration->storage_class = STORAGE_CLASS_NONE;
619 parser_print_warning_prefix_pos(declaration->source_position);
620 fprintf(stderr, "redundant declaration for '%s'\n",
622 parser_print_warning_prefix_pos(previous_declaration->source_position);
623 fprintf(stderr, "previous declaration of '%s' was here\n",
628 if (old_storage_class == STORAGE_CLASS_EXTERN &&
629 new_storage_class == STORAGE_CLASS_EXTERN) {
630 parser_print_warning_prefix_pos(declaration->source_position);
631 fprintf(stderr, "redundant extern declaration for '%s'\n",
633 parser_print_warning_prefix_pos(previous_declaration->source_position);
634 fprintf(stderr, "previous declaration of '%s' was here\n",
637 parser_print_error_prefix_pos(declaration->source_position);
638 if (old_storage_class == new_storage_class) {
639 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
641 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
643 parser_print_error_prefix_pos(previous_declaration->source_position);
644 fprintf(stderr, "previous declaration of '%s' was here\n",
649 return previous_declaration;
652 /* remember old declaration */
654 entry.symbol = symbol;
655 entry.old_declaration = symbol->declaration;
656 entry.namespc = (unsigned short) namespc;
657 ARR_APP1(stack_entry_t, *stack_ptr, entry);
659 /* replace/add declaration into declaration list of the symbol */
660 if(symbol->declaration == NULL) {
661 symbol->declaration = declaration;
663 declaration_t *iter_last = NULL;
664 declaration_t *iter = symbol->declaration;
665 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
666 /* replace an entry? */
667 if(iter->namespc == namespc) {
668 if(iter_last == NULL) {
669 symbol->declaration = declaration;
671 iter_last->symbol_next = declaration;
673 declaration->symbol_next = iter->symbol_next;
678 assert(iter_last->symbol_next == NULL);
679 iter_last->symbol_next = declaration;
686 static declaration_t *environment_push(declaration_t *declaration)
688 assert(declaration->source_position.input_name != NULL);
689 return stack_push(&environment_stack, declaration, context);
692 static declaration_t *label_push(declaration_t *declaration)
694 return stack_push(&label_stack, declaration, ¤t_function->context);
698 * pops symbols from the environment stack until @p new_top is the top element
700 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
702 stack_entry_t *stack = *stack_ptr;
703 size_t top = ARR_LEN(stack);
706 assert(new_top <= top);
710 for(i = top; i > new_top; --i) {
711 stack_entry_t *entry = &stack[i - 1];
713 declaration_t *old_declaration = entry->old_declaration;
714 symbol_t *symbol = entry->symbol;
715 namespace_t namespc = (namespace_t)entry->namespc;
717 /* replace/remove declaration */
718 declaration_t *declaration = symbol->declaration;
719 assert(declaration != NULL);
720 if(declaration->namespc == namespc) {
721 if(old_declaration == NULL) {
722 symbol->declaration = declaration->symbol_next;
724 symbol->declaration = old_declaration;
727 declaration_t *iter_last = declaration;
728 declaration_t *iter = declaration->symbol_next;
729 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
730 /* replace an entry? */
731 if(iter->namespc == namespc) {
732 assert(iter_last != NULL);
733 iter_last->symbol_next = old_declaration;
734 old_declaration->symbol_next = iter->symbol_next;
738 assert(iter != NULL);
742 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
745 static void environment_pop_to(size_t new_top)
747 stack_pop_to(&environment_stack, new_top);
750 static void label_pop_to(size_t new_top)
752 stack_pop_to(&label_stack, new_top);
756 static int get_rank(const type_t *type)
758 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
759 * and esp. footnote 108). However we can't fold constants (yet), so we
760 * can't decide wether unsigned int is possible, while int always works.
761 * (unsigned int would be preferable when possible... for stuff like
762 * struct { enum { ... } bla : 4; } ) */
763 if(type->type == TYPE_ENUM)
764 return ATOMIC_TYPE_INT;
766 assert(type->type == TYPE_ATOMIC);
767 const atomic_type_t *atomic_type = &type->atomic;
768 atomic_type_type_t atype = atomic_type->atype;
772 static type_t *promote_integer(type_t *type)
774 if(get_rank(type) < ATOMIC_TYPE_INT)
780 static expression_t *create_cast_expression(expression_t *expression,
783 expression_t *cast = allocate_expression_zero(EXPR_UNARY);
785 cast->unary.type = UNEXPR_CAST_IMPLICIT;
786 cast->unary.value = expression;
787 cast->base.datatype = dest_type;
792 static bool is_null_pointer_constant(const expression_t *expression)
794 /* skip void* cast */
795 if(expression->type == EXPR_UNARY) {
796 const unary_expression_t *unary = &expression->unary;
797 if(unary->type == UNEXPR_CAST
798 && expression->base.datatype == type_void_ptr) {
799 expression = unary->value;
803 /* TODO: not correct yet, should be any constant integer expression
804 * which evaluates to 0 */
805 if (expression->type != EXPR_CONST)
808 type_t *const type = skip_typeref(expression->base.datatype);
809 if (!is_type_integer(type))
812 return expression->conste.v.int_value == 0;
815 static expression_t *create_implicit_cast(expression_t *expression,
818 type_t *source_type = expression->base.datatype;
820 if(source_type == NULL)
823 source_type = skip_typeref(source_type);
824 dest_type = skip_typeref(dest_type);
826 if(source_type == dest_type)
829 switch (dest_type->type) {
831 /* TODO warning for implicitly converting to enum */
833 if (source_type->type != TYPE_ATOMIC &&
834 source_type->type != TYPE_ENUM) {
835 panic("casting of non-atomic types not implemented yet");
838 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
839 type_error_incompatible("can't cast types",
840 expression->base.source_position, source_type,
845 return create_cast_expression(expression, dest_type);
848 switch (source_type->type) {
850 if (is_null_pointer_constant(expression)) {
851 return create_cast_expression(expression, dest_type);
856 if (pointers_compatible(source_type, dest_type)) {
857 return create_cast_expression(expression, dest_type);
862 array_type_t *array_type = &source_type->array;
863 pointer_type_t *pointer_type = &dest_type->pointer;
864 if (types_compatible(array_type->element_type,
865 pointer_type->points_to)) {
866 return create_cast_expression(expression, dest_type);
872 panic("casting of non-atomic types not implemented yet");
875 type_error_incompatible("can't implicitly cast types",
876 expression->base.source_position, source_type, dest_type);
880 panic("casting of non-atomic types not implemented yet");
884 /** Implements the rules from § 6.5.16.1 */
885 static void semantic_assign(type_t *orig_type_left, expression_t **right,
888 type_t *orig_type_right = (*right)->base.datatype;
890 if(orig_type_right == NULL)
893 type_t *const type_left = skip_typeref(orig_type_left);
894 type_t *const type_right = skip_typeref(orig_type_right);
896 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
897 (is_type_pointer(type_left) && is_null_pointer_constant(*right)) ||
898 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
899 && is_type_pointer(type_right))) {
900 *right = create_implicit_cast(*right, type_left);
904 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
905 pointer_type_t *pointer_type_left = &type_left->pointer;
906 pointer_type_t *pointer_type_right = &type_right->pointer;
907 type_t *points_to_left = pointer_type_left->points_to;
908 type_t *points_to_right = pointer_type_right->points_to;
910 points_to_left = skip_typeref(points_to_left);
911 points_to_right = skip_typeref(points_to_right);
913 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
914 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
915 && !types_compatible(points_to_left, points_to_right)) {
916 goto incompatible_assign_types;
919 /* the left type has all qualifiers from the right type */
920 unsigned missing_qualifiers
921 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
922 if(missing_qualifiers != 0) {
923 parser_print_error_prefix();
924 fprintf(stderr, "destination type ");
925 print_type_quoted(type_left);
926 fprintf(stderr, " in %s from type ", context);
927 print_type_quoted(type_right);
928 fprintf(stderr, " lacks qualifiers '");
929 print_type_qualifiers(missing_qualifiers);
930 fprintf(stderr, "' in pointed-to type\n");
934 *right = create_implicit_cast(*right, type_left);
938 if (is_type_compound(type_left)
939 && types_compatible(type_left, type_right)) {
940 *right = create_implicit_cast(*right, type_left);
944 incompatible_assign_types:
945 /* TODO: improve error message */
946 parser_print_error_prefix();
947 fprintf(stderr, "incompatible types in %s\n", context);
948 parser_print_error_prefix();
949 print_type_quoted(orig_type_left);
950 fputs(" <- ", stderr);
951 print_type_quoted(orig_type_right);
955 static expression_t *parse_constant_expression(void)
957 /* start parsing at precedence 7 (conditional expression) */
958 return parse_sub_expression(7);
961 static expression_t *parse_assignment_expression(void)
963 /* start parsing at precedence 2 (assignment expression) */
964 return parse_sub_expression(2);
967 static type_t *make_global_typedef(const char *name, type_t *type)
969 symbol_t *symbol = symbol_table_insert(name);
971 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
972 declaration->namespc = NAMESPACE_NORMAL;
973 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
974 declaration->type = type;
975 declaration->symbol = symbol;
976 declaration->source_position = builtin_source_position;
978 record_declaration(declaration);
980 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
981 typedef_type->typedeft.declaration = declaration;
986 static const char *parse_string_literals(void)
988 assert(token.type == T_STRING_LITERAL);
989 const char *result = token.v.string;
993 while(token.type == T_STRING_LITERAL) {
994 result = concat_strings(result, token.v.string);
1001 static void parse_attributes(void)
1004 switch(token.type) {
1005 case T___attribute__: {
1011 switch(token.type) {
1013 parse_error("EOF while parsing attribute");
1032 if(token.type != T_STRING_LITERAL) {
1033 parse_error_expected("while parsing assembler attribute",
1038 parse_string_literals();
1043 goto attributes_finished;
1047 attributes_finished:
1052 static designator_t *parse_designation(void)
1054 if(token.type != '[' && token.type != '.')
1057 designator_t *result = NULL;
1058 designator_t *last = NULL;
1061 designator_t *designator;
1062 switch(token.type) {
1064 designator = allocate_ast_zero(sizeof(designator[0]));
1066 designator->array_access = parse_constant_expression();
1070 designator = allocate_ast_zero(sizeof(designator[0]));
1072 if(token.type != T_IDENTIFIER) {
1073 parse_error_expected("while parsing designator",
1077 designator->symbol = token.v.symbol;
1085 assert(designator != NULL);
1087 last->next = designator;
1089 result = designator;
1096 static initializer_t *initializer_from_string(array_type_t *type,
1099 /* TODO: check len vs. size of array type */
1102 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
1103 initializer->string.string = string;
1108 static initializer_t *initializer_from_expression(type_t *type,
1109 expression_t *expression)
1111 /* TODO check that expression is a constant expression */
1113 /* § 6.7.8.14/15 char array may be initialized by string literals */
1114 if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
1115 array_type_t *array_type = &type->array;
1116 type_t *element_type = array_type->element_type;
1118 if(element_type->type == TYPE_ATOMIC) {
1119 atomic_type_t *atomic_type = &element_type->atomic;
1120 atomic_type_type_t atype = atomic_type->atype;
1122 /* TODO handle wide strings */
1123 if(atype == ATOMIC_TYPE_CHAR
1124 || atype == ATOMIC_TYPE_SCHAR
1125 || atype == ATOMIC_TYPE_UCHAR) {
1127 string_literal_expression_t *literal = &expression->string;
1128 return initializer_from_string(array_type, literal->value);
1133 if(is_type_scalar(type)) {
1134 semantic_assign(type, &expression, "initializer");
1136 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1137 result->value.value = expression;
1145 static initializer_t *parse_sub_initializer(type_t *type,
1146 expression_t *expression,
1147 type_t *expression_type);
1149 static initializer_t *parse_sub_initializer_elem(type_t *type)
1151 if(token.type == '{') {
1152 return parse_sub_initializer(type, NULL, NULL);
1155 expression_t *expression = parse_assignment_expression();
1156 type_t *expression_type = skip_typeref(expression->base.datatype);
1158 return parse_sub_initializer(type, expression, expression_type);
1161 static bool had_initializer_brace_warning;
1163 static initializer_t *parse_sub_initializer(type_t *type,
1164 expression_t *expression,
1165 type_t *expression_type)
1167 if(is_type_scalar(type)) {
1168 /* there might be extra {} hierarchies */
1169 if(token.type == '{') {
1171 if(!had_initializer_brace_warning) {
1172 parse_warning("braces around scalar initializer");
1173 had_initializer_brace_warning = true;
1175 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1176 if(token.type == ',') {
1178 /* TODO: warn about excessive elements */
1184 if(expression == NULL) {
1185 expression = parse_assignment_expression();
1187 return initializer_from_expression(type, expression);
1190 /* does the expression match the currently looked at object to initalize */
1191 if(expression != NULL) {
1192 initializer_t *result = initializer_from_expression(type, expression);
1197 bool read_paren = false;
1198 if(token.type == '{') {
1203 /* descend into subtype */
1204 initializer_t *result = NULL;
1205 initializer_t **elems;
1206 if(type->type == TYPE_ARRAY) {
1207 array_type_t *array_type = &type->array;
1208 type_t *element_type = array_type->element_type;
1209 element_type = skip_typeref(element_type);
1212 had_initializer_brace_warning = false;
1213 if(expression == NULL) {
1214 sub = parse_sub_initializer_elem(element_type);
1216 sub = parse_sub_initializer(element_type, expression,
1220 /* didn't match the subtypes -> try the parent type */
1222 assert(!read_paren);
1226 elems = NEW_ARR_F(initializer_t*, 0);
1227 ARR_APP1(initializer_t*, elems, sub);
1230 if(token.type == '}')
1233 if(token.type == '}')
1236 sub = parse_sub_initializer_elem(element_type);
1238 /* TODO error, do nicer cleanup */
1239 parse_error("member initializer didn't match");
1243 ARR_APP1(initializer_t*, elems, sub);
1246 assert(type->type == TYPE_COMPOUND_STRUCT
1247 || type->type == TYPE_COMPOUND_UNION);
1248 compound_type_t *compound_type = &type->compound;
1249 context_t *context = &compound_type->declaration->context;
1251 declaration_t *first = context->declarations;
1254 type_t *first_type = first->type;
1255 first_type = skip_typeref(first_type);
1258 had_initializer_brace_warning = false;
1259 if(expression == NULL) {
1260 sub = parse_sub_initializer_elem(first_type);
1262 sub = parse_sub_initializer(first_type, expression,expression_type);
1265 /* didn't match the subtypes -> try our parent type */
1267 assert(!read_paren);
1271 elems = NEW_ARR_F(initializer_t*, 0);
1272 ARR_APP1(initializer_t*, elems, sub);
1274 declaration_t *iter = first->next;
1275 for( ; iter != NULL; iter = iter->next) {
1276 if(iter->symbol == NULL)
1278 if(iter->namespc != NAMESPACE_NORMAL)
1281 if(token.type == '}')
1284 if(token.type == '}')
1287 type_t *iter_type = iter->type;
1288 iter_type = skip_typeref(iter_type);
1290 sub = parse_sub_initializer_elem(iter_type);
1292 /* TODO error, do nicer cleanup*/
1293 parse_error("member initializer didn't match");
1297 ARR_APP1(initializer_t*, elems, sub);
1301 int len = ARR_LEN(elems);
1302 size_t elems_size = sizeof(initializer_t*) * len;
1304 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1306 init->initializer.type = INITIALIZER_LIST;
1308 memcpy(init->initializers, elems, elems_size);
1311 result = (initializer_t*) init;
1314 if(token.type == ',')
1321 static initializer_t *parse_initializer(type_t *type)
1323 initializer_t *result;
1325 type = skip_typeref(type);
1327 if(token.type != '{') {
1328 expression_t *expression = parse_assignment_expression();
1329 return initializer_from_expression(type, expression);
1332 if(is_type_scalar(type)) {
1336 expression_t *expression = parse_assignment_expression();
1337 result = initializer_from_expression(type, expression);
1339 if(token.type == ',')
1345 result = parse_sub_initializer(type, NULL, NULL);
1353 static declaration_t *parse_compound_type_specifier(bool is_struct)
1361 symbol_t *symbol = NULL;
1362 declaration_t *declaration = NULL;
1364 if (token.type == T___attribute__) {
1369 if(token.type == T_IDENTIFIER) {
1370 symbol = token.v.symbol;
1374 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1376 declaration = get_declaration(symbol, NAMESPACE_UNION);
1378 } else if(token.type != '{') {
1380 parse_error_expected("while parsing struct type specifier",
1381 T_IDENTIFIER, '{', 0);
1383 parse_error_expected("while parsing union type specifier",
1384 T_IDENTIFIER, '{', 0);
1390 if(declaration == NULL) {
1391 declaration = allocate_ast_zero(sizeof(declaration[0]));
1394 declaration->namespc = NAMESPACE_STRUCT;
1396 declaration->namespc = NAMESPACE_UNION;
1398 declaration->source_position = token.source_position;
1399 declaration->symbol = symbol;
1400 record_declaration(declaration);
1403 if(token.type == '{') {
1404 if(declaration->init.is_defined) {
1405 assert(symbol != NULL);
1406 parser_print_error_prefix();
1407 fprintf(stderr, "multiple definition of %s %s\n",
1408 is_struct ? "struct" : "union", symbol->string);
1409 declaration->context.declarations = NULL;
1411 declaration->init.is_defined = true;
1413 int top = environment_top();
1414 context_t *last_context = context;
1415 set_context(&declaration->context);
1417 parse_compound_type_entries();
1420 assert(context == &declaration->context);
1421 set_context(last_context);
1422 environment_pop_to(top);
1428 static void parse_enum_entries(enum_type_t *const enum_type)
1432 if(token.type == '}') {
1434 parse_error("empty enum not allowed");
1439 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1441 if(token.type != T_IDENTIFIER) {
1442 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1446 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1447 entry->type = (type_t*) enum_type;
1448 entry->symbol = token.v.symbol;
1449 entry->source_position = token.source_position;
1452 if(token.type == '=') {
1454 entry->init.enum_value = parse_constant_expression();
1459 record_declaration(entry);
1461 if(token.type != ',')
1464 } while(token.type != '}');
1469 static type_t *parse_enum_specifier(void)
1473 declaration_t *declaration;
1476 if(token.type == T_IDENTIFIER) {
1477 symbol = token.v.symbol;
1480 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1481 } else if(token.type != '{') {
1482 parse_error_expected("while parsing enum type specifier",
1483 T_IDENTIFIER, '{', 0);
1490 if(declaration == NULL) {
1491 declaration = allocate_ast_zero(sizeof(declaration[0]));
1493 declaration->namespc = NAMESPACE_ENUM;
1494 declaration->source_position = token.source_position;
1495 declaration->symbol = symbol;
1498 type_t *const type = allocate_type_zero(TYPE_ENUM);
1499 type->enumt.declaration = declaration;
1501 if(token.type == '{') {
1502 if(declaration->init.is_defined) {
1503 parser_print_error_prefix();
1504 fprintf(stderr, "multiple definitions of enum %s\n",
1507 record_declaration(declaration);
1508 declaration->init.is_defined = 1;
1510 parse_enum_entries(&type->enumt);
1518 * if a symbol is a typedef to another type, return true
1520 static bool is_typedef_symbol(symbol_t *symbol)
1522 const declaration_t *const declaration =
1523 get_declaration(symbol, NAMESPACE_NORMAL);
1525 declaration != NULL &&
1526 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1529 static type_t *parse_typeof(void)
1537 expression_t *expression = NULL;
1540 switch(token.type) {
1541 case T___extension__:
1542 /* this can be a prefix to a typename or an expression */
1543 /* we simply eat it now. */
1546 } while(token.type == T___extension__);
1550 if(is_typedef_symbol(token.v.symbol)) {
1551 type = parse_typename();
1553 expression = parse_expression();
1554 type = expression->base.datatype;
1559 type = parse_typename();
1563 expression = parse_expression();
1564 type = expression->base.datatype;
1570 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1571 typeof_type->typeoft.expression = expression;
1572 typeof_type->typeoft.typeof_type = type;
1578 SPECIFIER_SIGNED = 1 << 0,
1579 SPECIFIER_UNSIGNED = 1 << 1,
1580 SPECIFIER_LONG = 1 << 2,
1581 SPECIFIER_INT = 1 << 3,
1582 SPECIFIER_DOUBLE = 1 << 4,
1583 SPECIFIER_CHAR = 1 << 5,
1584 SPECIFIER_SHORT = 1 << 6,
1585 SPECIFIER_LONG_LONG = 1 << 7,
1586 SPECIFIER_FLOAT = 1 << 8,
1587 SPECIFIER_BOOL = 1 << 9,
1588 SPECIFIER_VOID = 1 << 10,
1589 #ifdef PROVIDE_COMPLEX
1590 SPECIFIER_COMPLEX = 1 << 11,
1591 SPECIFIER_IMAGINARY = 1 << 12,
1595 static type_t *create_builtin_type(symbol_t *symbol)
1597 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1598 type->builtin.symbol = symbol;
1600 type->builtin.real_type = type_int;
1605 static type_t *get_typedef_type(symbol_t *symbol)
1607 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1608 if(declaration == NULL
1609 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1612 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1613 type->typedeft.declaration = declaration;
1618 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1620 type_t *type = NULL;
1621 unsigned type_qualifiers = 0;
1622 unsigned type_specifiers = 0;
1625 specifiers->source_position = token.source_position;
1628 switch(token.type) {
1631 #define MATCH_STORAGE_CLASS(token, class) \
1633 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1634 parse_error("multiple storage classes in declaration " \
1637 specifiers->storage_class = class; \
1641 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1642 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1643 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1644 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1645 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1648 switch (specifiers->storage_class) {
1649 case STORAGE_CLASS_NONE:
1650 specifiers->storage_class = STORAGE_CLASS_THREAD;
1653 case STORAGE_CLASS_EXTERN:
1654 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1657 case STORAGE_CLASS_STATIC:
1658 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1662 parse_error("multiple storage classes in declaration specifiers");
1668 /* type qualifiers */
1669 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1671 type_qualifiers |= qualifier; \
1675 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1676 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1677 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1679 case T___extension__:
1684 /* type specifiers */
1685 #define MATCH_SPECIFIER(token, specifier, name) \
1688 if(type_specifiers & specifier) { \
1689 parse_error("multiple " name " type specifiers given"); \
1691 type_specifiers |= specifier; \
1695 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1696 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1697 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1698 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1699 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1700 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1701 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1702 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1703 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1704 #ifdef PROVIDE_COMPLEX
1705 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1706 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1710 specifiers->is_inline = true;
1715 if(type_specifiers & SPECIFIER_LONG_LONG) {
1716 parse_error("multiple type specifiers given");
1717 } else if(type_specifiers & SPECIFIER_LONG) {
1718 type_specifiers |= SPECIFIER_LONG_LONG;
1720 type_specifiers |= SPECIFIER_LONG;
1724 /* TODO: if type != NULL for the following rules should issue
1727 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1729 type->compound.declaration = parse_compound_type_specifier(true);
1733 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1735 type->compound.declaration = parse_compound_type_specifier(false);
1739 type = parse_enum_specifier();
1742 type = parse_typeof();
1744 case T___builtin_va_list:
1745 type = create_builtin_type(token.v.symbol);
1749 case T___attribute__:
1754 case T_IDENTIFIER: {
1755 type_t *typedef_type = get_typedef_type(token.v.symbol);
1757 if(typedef_type == NULL)
1758 goto finish_specifiers;
1761 type = typedef_type;
1765 /* function specifier */
1767 goto finish_specifiers;
1774 atomic_type_type_t atomic_type;
1776 /* match valid basic types */
1777 switch(type_specifiers) {
1778 case SPECIFIER_VOID:
1779 atomic_type = ATOMIC_TYPE_VOID;
1781 case SPECIFIER_CHAR:
1782 atomic_type = ATOMIC_TYPE_CHAR;
1784 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1785 atomic_type = ATOMIC_TYPE_SCHAR;
1787 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1788 atomic_type = ATOMIC_TYPE_UCHAR;
1790 case SPECIFIER_SHORT:
1791 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1792 case SPECIFIER_SHORT | SPECIFIER_INT:
1793 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1794 atomic_type = ATOMIC_TYPE_SHORT;
1796 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1797 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1798 atomic_type = ATOMIC_TYPE_USHORT;
1801 case SPECIFIER_SIGNED:
1802 case SPECIFIER_SIGNED | SPECIFIER_INT:
1803 atomic_type = ATOMIC_TYPE_INT;
1805 case SPECIFIER_UNSIGNED:
1806 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1807 atomic_type = ATOMIC_TYPE_UINT;
1809 case SPECIFIER_LONG:
1810 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1811 case SPECIFIER_LONG | SPECIFIER_INT:
1812 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1813 atomic_type = ATOMIC_TYPE_LONG;
1815 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1816 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1817 atomic_type = ATOMIC_TYPE_ULONG;
1819 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1820 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1821 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1822 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1824 atomic_type = ATOMIC_TYPE_LONGLONG;
1826 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1827 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1829 atomic_type = ATOMIC_TYPE_ULONGLONG;
1831 case SPECIFIER_FLOAT:
1832 atomic_type = ATOMIC_TYPE_FLOAT;
1834 case SPECIFIER_DOUBLE:
1835 atomic_type = ATOMIC_TYPE_DOUBLE;
1837 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1838 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1840 case SPECIFIER_BOOL:
1841 atomic_type = ATOMIC_TYPE_BOOL;
1843 #ifdef PROVIDE_COMPLEX
1844 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1845 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1847 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1848 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1850 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1851 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1853 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1854 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1856 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1857 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1859 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1860 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1864 /* invalid specifier combination, give an error message */
1865 if(type_specifiers == 0) {
1867 parse_warning("no type specifiers in declaration, using int");
1868 atomic_type = ATOMIC_TYPE_INT;
1871 parse_error("no type specifiers given in declaration");
1873 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1874 (type_specifiers & SPECIFIER_UNSIGNED)) {
1875 parse_error("signed and unsigned specifiers gives");
1876 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1877 parse_error("only integer types can be signed or unsigned");
1879 parse_error("multiple datatypes in declaration");
1881 atomic_type = ATOMIC_TYPE_INVALID;
1884 type = allocate_type_zero(TYPE_ATOMIC);
1885 type->atomic.atype = atomic_type;
1888 if(type_specifiers != 0) {
1889 parse_error("multiple datatypes in declaration");
1893 type->base.qualifiers = type_qualifiers;
1895 type_t *result = typehash_insert(type);
1896 if(newtype && result != type) {
1900 specifiers->type = result;
1903 static type_qualifiers_t parse_type_qualifiers(void)
1905 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1908 switch(token.type) {
1909 /* type qualifiers */
1910 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1911 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1912 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1915 return type_qualifiers;
1920 static declaration_t *parse_identifier_list(void)
1922 declaration_t *declarations = NULL;
1923 declaration_t *last_declaration = NULL;
1925 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1927 declaration->source_position = token.source_position;
1928 declaration->symbol = token.v.symbol;
1931 if(last_declaration != NULL) {
1932 last_declaration->next = declaration;
1934 declarations = declaration;
1936 last_declaration = declaration;
1938 if(token.type != ',')
1941 } while(token.type == T_IDENTIFIER);
1943 return declarations;
1946 static declaration_t *parse_parameter(void)
1948 declaration_specifiers_t specifiers;
1949 memset(&specifiers, 0, sizeof(specifiers));
1951 parse_declaration_specifiers(&specifiers);
1953 declaration_t *declaration = parse_declarator(&specifiers, true);
1955 /* TODO check declaration constraints for parameters */
1956 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1957 parse_error("typedef not allowed in parameter list");
1960 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1962 if (declaration->type->type == TYPE_ARRAY) {
1963 const array_type_t *const arr_type = &declaration->type->array;
1964 type_t *element_type = arr_type->element_type;
1965 declaration->type = make_pointer_type(element_type, TYPE_QUALIFIER_NONE);
1971 static declaration_t *parse_parameters(function_type_t *type)
1973 if(token.type == T_IDENTIFIER) {
1974 symbol_t *symbol = token.v.symbol;
1975 if(!is_typedef_symbol(symbol)) {
1976 type->kr_style_parameters = true;
1977 return parse_identifier_list();
1981 if(token.type == ')') {
1982 type->unspecified_parameters = 1;
1985 if(token.type == T_void && look_ahead(1)->type == ')') {
1990 declaration_t *declarations = NULL;
1991 declaration_t *declaration;
1992 declaration_t *last_declaration = NULL;
1993 function_parameter_t *parameter;
1994 function_parameter_t *last_parameter = NULL;
1997 switch(token.type) {
2001 return declarations;
2004 case T___extension__:
2006 declaration = parse_parameter();
2008 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2009 memset(parameter, 0, sizeof(parameter[0]));
2010 parameter->type = declaration->type;
2012 if(last_parameter != NULL) {
2013 last_declaration->next = declaration;
2014 last_parameter->next = parameter;
2016 type->parameters = parameter;
2017 declarations = declaration;
2019 last_parameter = parameter;
2020 last_declaration = declaration;
2024 return declarations;
2026 if(token.type != ',')
2027 return declarations;
2037 } construct_type_type_t;
2039 typedef struct construct_type_t construct_type_t;
2040 struct construct_type_t {
2041 construct_type_type_t type;
2042 construct_type_t *next;
2045 typedef struct parsed_pointer_t parsed_pointer_t;
2046 struct parsed_pointer_t {
2047 construct_type_t construct_type;
2048 type_qualifiers_t type_qualifiers;
2051 typedef struct construct_function_type_t construct_function_type_t;
2052 struct construct_function_type_t {
2053 construct_type_t construct_type;
2054 type_t *function_type;
2057 typedef struct parsed_array_t parsed_array_t;
2058 struct parsed_array_t {
2059 construct_type_t construct_type;
2060 type_qualifiers_t type_qualifiers;
2066 typedef struct construct_base_type_t construct_base_type_t;
2067 struct construct_base_type_t {
2068 construct_type_t construct_type;
2072 static construct_type_t *parse_pointer_declarator(void)
2076 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2077 memset(pointer, 0, sizeof(pointer[0]));
2078 pointer->construct_type.type = CONSTRUCT_POINTER;
2079 pointer->type_qualifiers = parse_type_qualifiers();
2081 return (construct_type_t*) pointer;
2084 static construct_type_t *parse_array_declarator(void)
2088 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2089 memset(array, 0, sizeof(array[0]));
2090 array->construct_type.type = CONSTRUCT_ARRAY;
2092 if(token.type == T_static) {
2093 array->is_static = true;
2097 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2098 if(type_qualifiers != 0) {
2099 if(token.type == T_static) {
2100 array->is_static = true;
2104 array->type_qualifiers = type_qualifiers;
2106 if(token.type == '*' && look_ahead(1)->type == ']') {
2107 array->is_variable = true;
2109 } else if(token.type != ']') {
2110 array->size = parse_assignment_expression();
2115 return (construct_type_t*) array;
2118 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2122 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2124 declaration_t *parameters = parse_parameters(&type->function);
2125 if(declaration != NULL) {
2126 declaration->context.declarations = parameters;
2129 construct_function_type_t *construct_function_type =
2130 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2131 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2132 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2133 construct_function_type->function_type = type;
2137 return (construct_type_t*) construct_function_type;
2140 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2141 bool may_be_abstract)
2143 /* construct a single linked list of construct_type_t's which describe
2144 * how to construct the final declarator type */
2145 construct_type_t *first = NULL;
2146 construct_type_t *last = NULL;
2149 while(token.type == '*') {
2150 construct_type_t *type = parse_pointer_declarator();
2161 /* TODO: find out if this is correct */
2164 construct_type_t *inner_types = NULL;
2166 switch(token.type) {
2168 if(declaration == NULL) {
2169 parse_error("no identifier expected in typename");
2171 declaration->symbol = token.v.symbol;
2172 declaration->source_position = token.source_position;
2178 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2184 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2185 /* avoid a loop in the outermost scope, because eat_statement doesn't
2187 if(token.type == '}' && current_function == NULL) {
2195 construct_type_t *p = last;
2198 construct_type_t *type;
2199 switch(token.type) {
2201 type = parse_function_declarator(declaration);
2204 type = parse_array_declarator();
2207 goto declarator_finished;
2210 /* insert in the middle of the list (behind p) */
2212 type->next = p->next;
2223 declarator_finished:
2226 /* append inner_types at the end of the list, we don't to set last anymore
2227 * as it's not needed anymore */
2229 assert(first == NULL);
2230 first = inner_types;
2232 last->next = inner_types;
2238 static type_t *construct_declarator_type(construct_type_t *construct_list,
2241 construct_type_t *iter = construct_list;
2242 for( ; iter != NULL; iter = iter->next) {
2243 switch(iter->type) {
2244 case CONSTRUCT_INVALID:
2245 panic("invalid type construction found");
2246 case CONSTRUCT_FUNCTION: {
2247 construct_function_type_t *construct_function_type
2248 = (construct_function_type_t*) iter;
2250 type_t *function_type = construct_function_type->function_type;
2252 function_type->function.result_type = type;
2254 type = function_type;
2258 case CONSTRUCT_POINTER: {
2259 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2260 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2261 pointer_type->pointer.points_to = type;
2262 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2264 type = pointer_type;
2268 case CONSTRUCT_ARRAY: {
2269 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2270 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2272 array_type->base.qualifiers = parsed_array->type_qualifiers;
2273 array_type->array.element_type = type;
2274 array_type->array.is_static = parsed_array->is_static;
2275 array_type->array.is_variable = parsed_array->is_variable;
2276 array_type->array.size = parsed_array->size;
2283 type_t *hashed_type = typehash_insert(type);
2284 if(hashed_type != type) {
2285 /* the function type was constructed earlier freeing it here will
2286 * destroy other types... */
2287 if(iter->type != CONSTRUCT_FUNCTION) {
2297 static declaration_t *parse_declarator(
2298 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2300 type_t *type = specifiers->type;
2301 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2302 declaration->storage_class = specifiers->storage_class;
2303 declaration->is_inline = specifiers->is_inline;
2305 construct_type_t *construct_type
2306 = parse_inner_declarator(declaration, may_be_abstract);
2307 declaration->type = construct_declarator_type(construct_type, type);
2309 if(construct_type != NULL) {
2310 obstack_free(&temp_obst, construct_type);
2316 static type_t *parse_abstract_declarator(type_t *base_type)
2318 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2320 type_t *result = construct_declarator_type(construct_type, base_type);
2321 if(construct_type != NULL) {
2322 obstack_free(&temp_obst, construct_type);
2328 static declaration_t *record_declaration(declaration_t *declaration)
2330 assert(declaration->parent_context == NULL);
2331 assert(context != NULL);
2333 symbol_t *symbol = declaration->symbol;
2334 if(symbol != NULL) {
2335 declaration_t *alias = environment_push(declaration);
2336 if(alias != declaration)
2339 declaration->parent_context = context;
2342 if(last_declaration != NULL) {
2343 last_declaration->next = declaration;
2345 context->declarations = declaration;
2347 last_declaration = declaration;
2352 static void parser_error_multiple_definition(declaration_t *declaration,
2353 const source_position_t source_position)
2355 parser_print_error_prefix_pos(source_position);
2356 fprintf(stderr, "multiple definition of symbol '%s'\n",
2357 declaration->symbol->string);
2358 parser_print_error_prefix_pos(declaration->source_position);
2359 fprintf(stderr, "this is the location of the previous definition.\n");
2362 static bool is_declaration_specifier(const token_t *token,
2363 bool only_type_specifiers)
2365 switch(token->type) {
2369 return is_typedef_symbol(token->v.symbol);
2371 case T___extension__:
2374 return !only_type_specifiers;
2381 static void parse_init_declarator_rest(declaration_t *declaration)
2385 type_t *orig_type = declaration->type;
2386 type_t *type = NULL;
2387 if(orig_type != NULL)
2388 type = skip_typeref(orig_type);
2390 if(declaration->init.initializer != NULL) {
2391 parser_error_multiple_definition(declaration, token.source_position);
2394 initializer_t *initializer = parse_initializer(type);
2396 /* § 6.7.5 (22) array intializers for arrays with unknown size determine
2397 * the array type size */
2398 if(type != NULL && type->type == TYPE_ARRAY && initializer != NULL) {
2399 array_type_t *array_type = &type->array;
2401 if(array_type->size == NULL) {
2402 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2404 cnst->base.datatype = type_size_t;
2406 if(initializer->type == INITIALIZER_LIST) {
2407 initializer_list_t *list = &initializer->list;
2408 cnst->conste.v.int_value = list->len;
2410 assert(initializer->type == INITIALIZER_STRING);
2411 initializer_string_t *string = &initializer->string;
2412 cnst->conste.v.int_value = strlen(string->string) + 1;
2415 array_type->size = cnst;
2419 if(type != NULL && type->type == TYPE_FUNCTION) {
2420 parser_print_error_prefix_pos(declaration->source_position);
2421 fprintf(stderr, "initializers not allowed for function types at "
2422 "declator '%s' (type ", declaration->symbol->string);
2423 print_type_quoted(orig_type);
2424 fprintf(stderr, ")\n");
2426 declaration->init.initializer = initializer;
2430 /* parse rest of a declaration without any declarator */
2431 static void parse_anonymous_declaration_rest(
2432 const declaration_specifiers_t *specifiers,
2433 parsed_declaration_func finished_declaration)
2437 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2439 declaration->type = specifiers->type;
2440 declaration->storage_class = specifiers->storage_class;
2441 declaration->source_position = specifiers->source_position;
2443 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2444 parse_warning_pos(declaration->source_position,
2445 "useless storage class in empty declaration");
2448 type_t *type = declaration->type;
2449 switch (type->type) {
2450 case TYPE_COMPOUND_STRUCT:
2451 case TYPE_COMPOUND_UNION: {
2452 const compound_type_t *compound_type = &type->compound;
2453 if (compound_type->declaration->symbol == NULL) {
2454 parse_warning_pos(declaration->source_position,
2455 "unnamed struct/union that defines no instances");
2464 parse_warning_pos(declaration->source_position,
2465 "empty declaration");
2469 finished_declaration(declaration);
2472 static void parse_declaration_rest(declaration_t *ndeclaration,
2473 const declaration_specifiers_t *specifiers,
2474 parsed_declaration_func finished_declaration)
2477 declaration_t *declaration = finished_declaration(ndeclaration);
2479 type_t *orig_type = declaration->type;
2480 type_t *type = skip_typeref(orig_type);
2482 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2483 parser_print_warning_prefix_pos(declaration->source_position);
2484 fprintf(stderr, "variable '%s' declared 'inline'\n",
2485 declaration->symbol->string);
2488 if(token.type == '=') {
2489 parse_init_declarator_rest(declaration);
2492 if(token.type != ',')
2496 ndeclaration = parse_declarator(specifiers, false);
2501 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2503 /* TODO: check that it was actually a parameter that gets a type */
2505 /* we should have a declaration for the parameter in the current
2507 return record_declaration(declaration);
2510 static void parse_declaration(parsed_declaration_func finished_declaration)
2512 declaration_specifiers_t specifiers;
2513 memset(&specifiers, 0, sizeof(specifiers));
2514 parse_declaration_specifiers(&specifiers);
2516 if(token.type == ';') {
2517 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2519 declaration_t *declaration = parse_declarator(&specifiers, false);
2520 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2524 static void parse_kr_declaration_list(declaration_t *declaration)
2526 type_t *type = skip_typeref(declaration->type);
2527 assert(type->type == TYPE_FUNCTION);
2529 if(!type->function.kr_style_parameters)
2532 /* push function parameters */
2533 int top = environment_top();
2534 context_t *last_context = context;
2535 set_context(&declaration->context);
2537 declaration_t *parameter = declaration->context.declarations;
2538 for( ; parameter != NULL; parameter = parameter->next) {
2539 environment_push(parameter);
2542 /* parse declaration list */
2543 while(is_declaration_specifier(&token, false)) {
2544 parse_declaration(finished_kr_declaration);
2547 /* pop function parameters */
2548 assert(context == &declaration->context);
2549 set_context(last_context);
2550 environment_pop_to(top);
2552 /* update function type */
2553 type_t *new_type = duplicate_type(type);
2554 new_type->function.kr_style_parameters = false;
2556 function_parameter_t *parameters = NULL;
2557 function_parameter_t *last_parameter = NULL;
2559 declaration_t *parameter_declaration = declaration->context.declarations;
2560 for( ; parameter_declaration != NULL;
2561 parameter_declaration = parameter_declaration->next) {
2562 type_t *parameter_type = parameter_declaration->type;
2563 if(parameter_type == NULL) {
2565 parser_print_error_prefix();
2566 fprintf(stderr, "no type specified for function parameter '%s'\n",
2567 parameter_declaration->symbol->string);
2569 parser_print_warning_prefix();
2570 fprintf(stderr, "no type specified for function parameter '%s', "
2571 "using int\n", parameter_declaration->symbol->string);
2572 parameter_type = type_int;
2573 parameter_declaration->type = parameter_type;
2577 function_parameter_t *function_parameter
2578 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2579 memset(function_parameter, 0, sizeof(function_parameter[0]));
2581 function_parameter->type = parameter_type;
2582 if(last_parameter != NULL) {
2583 last_parameter->next = function_parameter;
2585 parameters = function_parameter;
2587 last_parameter = function_parameter;
2589 new_type->function.parameters = parameters;
2591 type = typehash_insert(new_type);
2592 if(type != new_type) {
2593 obstack_free(type_obst, new_type);
2596 declaration->type = type;
2599 static void parse_external_declaration(void)
2601 /* function-definitions and declarations both start with declaration
2603 declaration_specifiers_t specifiers;
2604 memset(&specifiers, 0, sizeof(specifiers));
2605 parse_declaration_specifiers(&specifiers);
2607 /* must be a declaration */
2608 if(token.type == ';') {
2609 parse_anonymous_declaration_rest(&specifiers, record_declaration);
2613 /* declarator is common to both function-definitions and declarations */
2614 declaration_t *ndeclaration = parse_declarator(&specifiers, false);
2616 /* must be a declaration */
2617 if(token.type == ',' || token.type == '=' || token.type == ';') {
2618 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2622 /* must be a function definition */
2623 parse_kr_declaration_list(ndeclaration);
2625 if(token.type != '{') {
2626 parse_error_expected("while parsing function definition", '{', 0);
2631 type_t *orig_type = ndeclaration->type;
2632 if(orig_type == NULL) {
2637 type_t *type = skip_typeref(orig_type);
2638 if(type->type != TYPE_FUNCTION) {
2639 parser_print_error_prefix();
2640 fprintf(stderr, "declarator '");
2641 print_type_ext(orig_type, ndeclaration->symbol, NULL);
2642 fprintf(stderr, "' has a body but is not a function type.\n");
2647 /* § 6.7.5.3 (14) a function definition with () means no
2648 * parameters (and not unspecified parameters) */
2649 if(type->function.unspecified_parameters) {
2650 type_t *duplicate = duplicate_type(type);
2651 duplicate->function.unspecified_parameters = false;
2653 type = typehash_insert(duplicate);
2654 if(type != duplicate) {
2655 obstack_free(type_obst, duplicate);
2657 ndeclaration->type = type;
2660 declaration_t *declaration = record_declaration(ndeclaration);
2661 if(ndeclaration != declaration) {
2662 memcpy(&declaration->context, &ndeclaration->context,
2663 sizeof(declaration->context));
2665 type = skip_typeref(declaration->type);
2667 /* push function parameters and switch context */
2668 int top = environment_top();
2669 context_t *last_context = context;
2670 set_context(&declaration->context);
2672 declaration_t *parameter = declaration->context.declarations;
2673 for( ; parameter != NULL; parameter = parameter->next) {
2674 environment_push(parameter);
2677 if(declaration->init.statement != NULL) {
2678 parser_error_multiple_definition(declaration, token.source_position);
2680 goto end_of_parse_external_declaration;
2682 /* parse function body */
2683 int label_stack_top = label_top();
2684 declaration_t *old_current_function = current_function;
2685 current_function = declaration;
2687 declaration->init.statement = parse_compound_statement();
2689 assert(current_function == declaration);
2690 current_function = old_current_function;
2691 label_pop_to(label_stack_top);
2694 end_of_parse_external_declaration:
2695 assert(context == &declaration->context);
2696 set_context(last_context);
2697 environment_pop_to(top);
2700 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2703 if(token.type == ':') {
2705 parse_constant_expression();
2706 /* TODO (bitfields) */
2708 declaration_t *declaration = parse_declarator(specifiers, true);
2710 /* TODO: check constraints for struct declarations */
2711 /* TODO: check for doubled fields */
2712 record_declaration(declaration);
2714 if(token.type == ':') {
2716 parse_constant_expression();
2717 /* TODO (bitfields) */
2721 if(token.type != ',')
2728 static void parse_compound_type_entries(void)
2732 while(token.type != '}' && token.type != T_EOF) {
2733 declaration_specifiers_t specifiers;
2734 memset(&specifiers, 0, sizeof(specifiers));
2735 parse_declaration_specifiers(&specifiers);
2737 parse_struct_declarators(&specifiers);
2739 if(token.type == T_EOF) {
2740 parse_error("EOF while parsing struct");
2745 static type_t *parse_typename(void)
2747 declaration_specifiers_t specifiers;
2748 memset(&specifiers, 0, sizeof(specifiers));
2749 parse_declaration_specifiers(&specifiers);
2750 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2751 /* TODO: improve error message, user does probably not know what a
2752 * storage class is...
2754 parse_error("typename may not have a storage class");
2757 type_t *result = parse_abstract_declarator(specifiers.type);
2765 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2766 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2767 expression_t *left);
2769 typedef struct expression_parser_function_t expression_parser_function_t;
2770 struct expression_parser_function_t {
2771 unsigned precedence;
2772 parse_expression_function parser;
2773 unsigned infix_precedence;
2774 parse_expression_infix_function infix_parser;
2777 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2779 static expression_t *make_invalid_expression(void)
2781 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2782 expression->base.source_position = token.source_position;
2786 static expression_t *expected_expression_error(void)
2788 parser_print_error_prefix();
2789 fprintf(stderr, "expected expression, got token ");
2790 print_token(stderr, &token);
2791 fprintf(stderr, "\n");
2795 return make_invalid_expression();
2798 static expression_t *parse_string_const(void)
2800 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2801 cnst->base.datatype = type_string;
2802 cnst->string.value = parse_string_literals();
2807 static expression_t *parse_wide_string_const(void)
2809 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2810 cnst->base.datatype = type_wchar_t_ptr;
2811 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2816 static expression_t *parse_int_const(void)
2818 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2819 cnst->base.datatype = token.datatype;
2820 cnst->conste.v.int_value = token.v.intvalue;
2827 static expression_t *parse_float_const(void)
2829 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2830 cnst->base.datatype = token.datatype;
2831 cnst->conste.v.float_value = token.v.floatvalue;
2838 static declaration_t *create_implicit_function(symbol_t *symbol,
2839 const source_position_t source_position)
2841 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2842 ntype->function.result_type = type_int;
2843 ntype->function.unspecified_parameters = true;
2845 type_t *type = typehash_insert(ntype);
2850 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2852 declaration->storage_class = STORAGE_CLASS_EXTERN;
2853 declaration->type = type;
2854 declaration->symbol = symbol;
2855 declaration->source_position = source_position;
2857 /* prepend the implicit definition to the global context
2858 * this is safe since the symbol wasn't declared as anything else yet
2860 assert(symbol->declaration == NULL);
2862 context_t *last_context = context;
2863 context = global_context;
2865 environment_push(declaration);
2866 declaration->next = context->declarations;
2867 context->declarations = declaration;
2869 context = last_context;
2874 static type_t *make_function_1_type(type_t *result_type, type_t *argument_type)
2876 function_parameter_t *parameter
2877 = obstack_alloc(type_obst, sizeof(parameter[0]));
2878 memset(parameter, 0, sizeof(parameter[0]));
2879 parameter->type = argument_type;
2881 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2882 type->function.result_type = result_type;
2883 type->function.parameters = parameter;
2885 type_t *result = typehash_insert(type);
2886 if(result != type) {
2893 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2895 switch(symbol->ID) {
2896 case T___builtin_alloca:
2897 return make_function_1_type(type_void_ptr, type_size_t);
2898 case T___builtin_nan:
2899 return make_function_1_type(type_double, type_string);
2900 case T___builtin_nanf:
2901 return make_function_1_type(type_float, type_string);
2902 case T___builtin_nand:
2903 return make_function_1_type(type_long_double, type_string);
2905 panic("not implemented builtin symbol found");
2910 * performs automatic type cast as described in § 6.3.2.1
2912 static type_t *automatic_type_conversion(type_t *type)
2917 if(type->type == TYPE_ARRAY) {
2918 array_type_t *array_type = &type->array;
2919 type_t *element_type = array_type->element_type;
2920 unsigned qualifiers = array_type->type.qualifiers;
2922 return make_pointer_type(element_type, qualifiers);
2925 if(type->type == TYPE_FUNCTION) {
2926 return make_pointer_type(type, TYPE_QUALIFIER_NONE);
2933 * reverts the automatic casts of array to pointer types and function
2934 * to function-pointer types as defined § 6.3.2.1
2936 type_t *revert_automatic_type_conversion(const expression_t *expression)
2938 if(expression->base.datatype == NULL)
2941 switch(expression->type) {
2942 case EXPR_REFERENCE: {
2943 const reference_expression_t *ref = &expression->reference;
2944 return ref->declaration->type;
2947 const select_expression_t *select = &expression->select;
2948 return select->compound_entry->type;
2951 const unary_expression_t *unary = &expression->unary;
2952 if(unary->type == UNEXPR_DEREFERENCE) {
2953 expression_t *value = unary->value;
2954 type_t *type = skip_typeref(value->base.datatype);
2955 pointer_type_t *pointer_type = &type->pointer;
2957 return pointer_type->points_to;
2961 case EXPR_BUILTIN_SYMBOL: {
2962 const builtin_symbol_expression_t *builtin
2963 = &expression->builtin_symbol;
2964 return get_builtin_symbol_type(builtin->symbol);
2966 case EXPR_ARRAY_ACCESS: {
2967 const array_access_expression_t *array_access
2968 = &expression->array_access;
2969 const expression_t *array_ref = array_access->array_ref;
2970 type_t *type_left = skip_typeref(array_ref->base.datatype);
2971 assert(is_type_pointer(type_left));
2972 pointer_type_t *pointer_type = &type_left->pointer;
2973 return pointer_type->points_to;
2980 return expression->base.datatype;
2983 static expression_t *parse_reference(void)
2985 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
2987 reference_expression_t *ref = &expression->reference;
2988 ref->symbol = token.v.symbol;
2990 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2992 source_position_t source_position = token.source_position;
2995 if(declaration == NULL) {
2997 /* an implicitly defined function */
2998 if(token.type == '(') {
2999 parser_print_prefix_pos(token.source_position);
3000 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
3001 ref->symbol->string);
3003 declaration = create_implicit_function(ref->symbol,
3008 parser_print_error_prefix();
3009 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
3014 type_t *type = declaration->type;
3015 /* we always do the auto-type conversions; the & and sizeof parser contains
3016 * code to revert this! */
3017 type = automatic_type_conversion(type);
3019 ref->declaration = declaration;
3020 ref->expression.datatype = type;
3025 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3029 /* TODO check if explicit cast is allowed and issue warnings/errors */
3032 static expression_t *parse_cast(void)
3034 expression_t *cast = allocate_expression_zero(EXPR_UNARY);
3036 cast->unary.type = UNEXPR_CAST;
3037 cast->base.source_position = token.source_position;
3039 type_t *type = parse_typename();
3042 expression_t *value = parse_sub_expression(20);
3044 check_cast_allowed(value, type);
3046 cast->base.datatype = type;
3047 cast->unary.value = value;
3052 static expression_t *parse_statement_expression(void)
3054 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3056 statement_t *statement = parse_compound_statement();
3057 expression->statement.statement = statement;
3058 if(statement == NULL) {
3063 assert(statement->type == STATEMENT_COMPOUND);
3064 compound_statement_t *compound_statement = &statement->compound;
3066 /* find last statement and use it's type */
3067 const statement_t *last_statement = NULL;
3068 const statement_t *iter = compound_statement->statements;
3069 for( ; iter != NULL; iter = iter->base.next) {
3070 last_statement = iter;
3073 if(last_statement->type == STATEMENT_EXPRESSION) {
3074 const expression_statement_t *expression_statement
3075 = &last_statement->expression;
3076 expression->base.datatype
3077 = expression_statement->expression->base.datatype;
3079 expression->base.datatype = type_void;
3087 static expression_t *parse_brace_expression(void)
3091 switch(token.type) {
3093 /* gcc extension: a stement expression */
3094 return parse_statement_expression();
3098 return parse_cast();
3100 if(is_typedef_symbol(token.v.symbol)) {
3101 return parse_cast();
3105 expression_t *result = parse_expression();
3111 static expression_t *parse_function_keyword(void)
3116 if (current_function == NULL) {
3117 parse_error("'__func__' used outside of a function");
3120 string_literal_expression_t *expression
3121 = allocate_ast_zero(sizeof(expression[0]));
3123 expression->expression.type = EXPR_FUNCTION;
3124 expression->expression.datatype = type_string;
3125 expression->value = "TODO: FUNCTION";
3127 return (expression_t*) expression;
3130 static expression_t *parse_pretty_function_keyword(void)
3132 eat(T___PRETTY_FUNCTION__);
3135 string_literal_expression_t *expression
3136 = allocate_ast_zero(sizeof(expression[0]));
3138 expression->expression.type = EXPR_PRETTY_FUNCTION;
3139 expression->expression.datatype = type_string;
3140 expression->value = "TODO: PRETTY FUNCTION";
3142 return (expression_t*) expression;
3145 static designator_t *parse_designator(void)
3147 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3149 if(token.type != T_IDENTIFIER) {
3150 parse_error_expected("while parsing member designator",
3155 result->symbol = token.v.symbol;
3158 designator_t *last_designator = result;
3160 if(token.type == '.') {
3162 if(token.type != T_IDENTIFIER) {
3163 parse_error_expected("while parsing member designator",
3168 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3169 designator->symbol = token.v.symbol;
3172 last_designator->next = designator;
3173 last_designator = designator;
3176 if(token.type == '[') {
3178 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3179 designator->array_access = parse_expression();
3180 if(designator->array_access == NULL) {
3186 last_designator->next = designator;
3187 last_designator = designator;
3196 static expression_t *parse_offsetof(void)
3198 eat(T___builtin_offsetof);
3200 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3201 expression->base.datatype = type_size_t;
3204 expression->offsetofe.type = parse_typename();
3206 expression->offsetofe.designator = parse_designator();
3212 static expression_t *parse_va_arg(void)
3214 eat(T___builtin_va_arg);
3216 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3219 expression->va_arge.arg = parse_assignment_expression();
3221 expression->base.datatype = parse_typename();
3227 static expression_t *parse_builtin_symbol(void)
3229 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3231 symbol_t *symbol = token.v.symbol;
3233 expression->builtin_symbol.symbol = symbol;
3236 type_t *type = get_builtin_symbol_type(symbol);
3237 type = automatic_type_conversion(type);
3239 expression->base.datatype = type;
3243 static expression_t *parse_primary_expression(void)
3245 switch(token.type) {
3247 return parse_int_const();
3248 case T_FLOATINGPOINT:
3249 return parse_float_const();
3250 case T_STRING_LITERAL: /* TODO merge */
3251 return parse_string_const();
3252 case T_WIDE_STRING_LITERAL:
3253 return parse_wide_string_const();
3255 return parse_reference();
3256 case T___FUNCTION__:
3258 return parse_function_keyword();
3259 case T___PRETTY_FUNCTION__:
3260 return parse_pretty_function_keyword();
3261 case T___builtin_offsetof:
3262 return parse_offsetof();
3263 case T___builtin_va_arg:
3264 return parse_va_arg();
3265 case T___builtin_nanf:
3266 case T___builtin_alloca:
3267 case T___builtin_expect:
3268 case T___builtin_va_start:
3269 case T___builtin_va_end:
3270 return parse_builtin_symbol();
3273 return parse_brace_expression();
3276 parser_print_error_prefix();
3277 fprintf(stderr, "unexpected token ");
3278 print_token(stderr, &token);
3279 fprintf(stderr, "\n");
3282 return make_invalid_expression();
3285 static expression_t *parse_array_expression(unsigned precedence,
3292 expression_t *inside = parse_expression();
3294 array_access_expression_t *array_access
3295 = allocate_ast_zero(sizeof(array_access[0]));
3297 array_access->expression.type = EXPR_ARRAY_ACCESS;
3299 type_t *type_left = left->base.datatype;
3300 type_t *type_inside = inside->base.datatype;
3301 type_t *result_type = NULL;
3303 if(type_left != NULL && type_inside != NULL) {
3304 type_left = skip_typeref(type_left);
3305 type_inside = skip_typeref(type_inside);
3307 if(is_type_pointer(type_left)) {
3308 pointer_type_t *pointer = &type_left->pointer;
3309 result_type = pointer->points_to;
3310 array_access->array_ref = left;
3311 array_access->index = inside;
3312 } else if(is_type_pointer(type_inside)) {
3313 pointer_type_t *pointer = &type_inside->pointer;
3314 result_type = pointer->points_to;
3315 array_access->array_ref = inside;
3316 array_access->index = left;
3317 array_access->flipped = true;
3319 parser_print_error_prefix();
3320 fprintf(stderr, "array access on object with non-pointer types ");
3321 print_type_quoted(type_left);
3322 fprintf(stderr, ", ");
3323 print_type_quoted(type_inside);
3324 fprintf(stderr, "\n");
3327 array_access->array_ref = left;
3328 array_access->index = inside;
3331 if(token.type != ']') {
3332 parse_error_expected("Problem while parsing array access", ']', 0);
3333 return (expression_t*) array_access;
3337 result_type = automatic_type_conversion(result_type);
3338 array_access->expression.datatype = result_type;
3340 return (expression_t*) array_access;
3343 static expression_t *parse_sizeof(unsigned precedence)
3347 sizeof_expression_t *sizeof_expression
3348 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3349 sizeof_expression->expression.type = EXPR_SIZEOF;
3350 sizeof_expression->expression.datatype = type_size_t;
3352 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3354 sizeof_expression->type = parse_typename();
3357 expression_t *expression = parse_sub_expression(precedence);
3358 expression->base.datatype = revert_automatic_type_conversion(expression);
3360 sizeof_expression->type = expression->base.datatype;
3361 sizeof_expression->size_expression = expression;
3364 return (expression_t*) sizeof_expression;
3367 static expression_t *parse_select_expression(unsigned precedence,
3368 expression_t *compound)
3371 assert(token.type == '.' || token.type == T_MINUSGREATER);
3373 bool is_pointer = (token.type == T_MINUSGREATER);
3376 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3377 select->select.compound = compound;
3379 if(token.type != T_IDENTIFIER) {
3380 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3383 symbol_t *symbol = token.v.symbol;
3384 select->select.symbol = symbol;
3387 type_t *orig_type = compound->base.datatype;
3388 if(orig_type == NULL)
3389 return make_invalid_expression();
3391 type_t *type = skip_typeref(orig_type);
3393 type_t *type_left = type;
3395 if(type->type != TYPE_POINTER) {
3396 parser_print_error_prefix();
3397 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
3398 print_type_quoted(orig_type);
3399 fputc('\n', stderr);
3400 return make_invalid_expression();
3402 pointer_type_t *pointer_type = &type->pointer;
3403 type_left = pointer_type->points_to;
3405 type_left = skip_typeref(type_left);
3407 if(type_left->type != TYPE_COMPOUND_STRUCT
3408 && type_left->type != TYPE_COMPOUND_UNION) {
3409 parser_print_error_prefix();
3410 fprintf(stderr, "request for member '%s' in something not a struct or "
3411 "union, but ", symbol->string);
3412 print_type_quoted(type_left);
3413 fputc('\n', stderr);
3414 return make_invalid_expression();
3417 compound_type_t *compound_type = &type_left->compound;
3418 declaration_t *declaration = compound_type->declaration;
3420 if(!declaration->init.is_defined) {
3421 parser_print_error_prefix();
3422 fprintf(stderr, "request for member '%s' of incomplete type ",
3424 print_type_quoted(type_left);
3425 fputc('\n', stderr);
3426 return make_invalid_expression();
3429 declaration_t *iter = declaration->context.declarations;
3430 for( ; iter != NULL; iter = iter->next) {
3431 if(iter->symbol == symbol) {
3436 parser_print_error_prefix();
3437 print_type_quoted(type_left);
3438 fprintf(stderr, " has no member named '%s'\n", symbol->string);
3439 return make_invalid_expression();
3442 /* we always do the auto-type conversions; the & and sizeof parser contains
3443 * code to revert this! */
3444 type_t *expression_type = automatic_type_conversion(iter->type);
3446 select->select.compound_entry = iter;
3447 select->base.datatype = expression_type;
3451 static expression_t *parse_call_expression(unsigned precedence,
3452 expression_t *expression)
3455 expression_t *result = allocate_expression_zero(EXPR_CALL);
3457 call_expression_t *call = &result->call;
3458 call->function = expression;
3460 function_type_t *function_type = NULL;
3461 type_t *orig_type = expression->base.datatype;
3462 if(orig_type != NULL) {
3463 type_t *type = skip_typeref(orig_type);
3465 if(is_type_pointer(type)) {
3466 pointer_type_t *pointer_type = &type->pointer;
3468 type = skip_typeref(pointer_type->points_to);
3470 if (type->type == TYPE_FUNCTION) {
3471 function_type = &type->function;
3472 call->expression.datatype = function_type->result_type;
3475 if(function_type == NULL) {
3476 parser_print_error_prefix();
3477 fputs("called object '", stderr);
3478 print_expression(expression);
3479 fputs("' (type ", stderr);
3480 print_type_quoted(orig_type);
3481 fputs(") is not a pointer to a function\n", stderr);
3483 function_type = NULL;
3484 call->expression.datatype = NULL;
3488 /* parse arguments */
3491 if(token.type != ')') {
3492 call_argument_t *last_argument = NULL;
3495 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3497 argument->expression = parse_assignment_expression();
3498 if(last_argument == NULL) {
3499 call->arguments = argument;
3501 last_argument->next = argument;
3503 last_argument = argument;
3505 if(token.type != ',')
3512 if(function_type != NULL) {
3513 function_parameter_t *parameter = function_type->parameters;
3514 call_argument_t *argument = call->arguments;
3515 for( ; parameter != NULL && argument != NULL;
3516 parameter = parameter->next, argument = argument->next) {
3517 type_t *expected_type = parameter->type;
3518 /* TODO report context in error messages */
3519 argument->expression = create_implicit_cast(argument->expression,
3522 /* too few parameters */
3523 if(parameter != NULL) {
3524 parser_print_error_prefix();
3525 fprintf(stderr, "too few arguments to function '");
3526 print_expression(expression);
3527 fprintf(stderr, "'\n");
3528 } else if(argument != NULL) {
3529 /* too many parameters */
3530 if(!function_type->variadic
3531 && !function_type->unspecified_parameters) {
3532 parser_print_error_prefix();
3533 fprintf(stderr, "too many arguments to function '");
3534 print_expression(expression);
3535 fprintf(stderr, "'\n");
3537 /* do default promotion */
3538 for( ; argument != NULL; argument = argument->next) {
3539 type_t *type = argument->expression->base.datatype;
3544 type = skip_typeref(type);
3545 if(is_type_integer(type)) {
3546 type = promote_integer(type);
3547 } else if(type == type_float) {
3551 argument->expression
3552 = create_implicit_cast(argument->expression, type);
3561 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3563 static bool same_compound_type(const type_t *type1, const type_t *type2)
3565 if(!is_type_compound(type1))
3567 if(type1->type != type2->type)
3570 const compound_type_t *compound1 = &type1->compound;
3571 const compound_type_t *compound2 = &type2->compound;
3573 return compound1->declaration == compound2->declaration;
3576 static expression_t *parse_conditional_expression(unsigned precedence,
3577 expression_t *expression)
3581 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3583 conditional_expression_t *conditional = &result->conditional;
3584 conditional->condition = expression;
3587 type_t *condition_type_orig = expression->base.datatype;
3588 if(condition_type_orig != NULL) {
3589 type_t *condition_type = skip_typeref(condition_type_orig);
3590 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3591 type_error("expected a scalar type in conditional condition",
3592 expression->base.source_position, condition_type_orig);
3596 expression_t *true_expression = parse_expression();
3598 expression_t *false_expression = parse_sub_expression(precedence);
3600 conditional->true_expression = true_expression;
3601 conditional->false_expression = false_expression;
3603 type_t *orig_true_type = true_expression->base.datatype;
3604 type_t *orig_false_type = false_expression->base.datatype;
3605 if(orig_true_type == NULL || orig_false_type == NULL)
3608 type_t *true_type = skip_typeref(orig_true_type);
3609 type_t *false_type = skip_typeref(orig_false_type);
3612 type_t *result_type = NULL;
3613 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3614 result_type = semantic_arithmetic(true_type, false_type);
3616 true_expression = create_implicit_cast(true_expression, result_type);
3617 false_expression = create_implicit_cast(false_expression, result_type);
3619 conditional->true_expression = true_expression;
3620 conditional->false_expression = false_expression;
3621 conditional->expression.datatype = result_type;
3622 } else if (same_compound_type(true_type, false_type)
3623 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3624 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
3625 /* just take 1 of the 2 types */
3626 result_type = true_type;
3627 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3628 && pointers_compatible(true_type, false_type)) {
3630 result_type = true_type;
3633 type_error_incompatible("while parsing conditional",
3634 expression->base.source_position, true_type,
3638 conditional->expression.datatype = result_type;
3642 static expression_t *parse_extension(unsigned precedence)
3644 eat(T___extension__);
3646 /* TODO enable extensions */
3648 return parse_sub_expression(precedence);
3651 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3653 eat(T___builtin_classify_type);
3655 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3656 result->base.datatype = type_int;
3659 expression_t *expression = parse_sub_expression(precedence);
3661 result->classify_type.type_expression = expression;
3666 static void semantic_incdec(unary_expression_t *expression)
3668 type_t *orig_type = expression->value->base.datatype;
3669 if(orig_type == NULL)
3672 type_t *type = skip_typeref(orig_type);
3673 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3674 /* TODO: improve error message */
3675 parser_print_error_prefix();
3676 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3680 expression->expression.datatype = orig_type;
3683 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3685 type_t *orig_type = expression->value->base.datatype;
3686 if(orig_type == NULL)
3689 type_t *type = skip_typeref(orig_type);
3690 if(!is_type_arithmetic(type)) {
3691 /* TODO: improve error message */
3692 parser_print_error_prefix();
3693 fprintf(stderr, "operation needs an arithmetic type\n");
3697 expression->expression.datatype = orig_type;
3700 static void semantic_unexpr_scalar(unary_expression_t *expression)
3702 type_t *orig_type = expression->value->base.datatype;
3703 if(orig_type == NULL)
3706 type_t *type = skip_typeref(orig_type);
3707 if (!is_type_scalar(type)) {
3708 parse_error("operand of ! must be of scalar type\n");
3712 expression->expression.datatype = orig_type;
3715 static void semantic_unexpr_integer(unary_expression_t *expression)
3717 type_t *orig_type = expression->value->base.datatype;
3718 if(orig_type == NULL)
3721 type_t *type = skip_typeref(orig_type);
3722 if (!is_type_integer(type)) {
3723 parse_error("operand of ~ must be of integer type\n");
3727 expression->expression.datatype = orig_type;
3730 static void semantic_dereference(unary_expression_t *expression)
3732 type_t *orig_type = expression->value->base.datatype;
3733 if(orig_type == NULL)
3736 type_t *type = skip_typeref(orig_type);
3737 if(!is_type_pointer(type)) {
3738 parser_print_error_prefix();
3739 fputs("Unary '*' needs pointer or arrray type, but type ", stderr);
3740 print_type_quoted(orig_type);
3741 fputs(" given.\n", stderr);
3745 pointer_type_t *pointer_type = &type->pointer;
3746 type_t *result_type = pointer_type->points_to;
3748 result_type = automatic_type_conversion(result_type);
3749 expression->expression.datatype = result_type;
3752 static void semantic_take_addr(unary_expression_t *expression)
3754 expression_t *value = expression->value;
3755 value->base.datatype = revert_automatic_type_conversion(value);
3757 type_t *orig_type = value->base.datatype;
3758 if(orig_type == NULL)
3761 if(value->type == EXPR_REFERENCE) {
3762 reference_expression_t *reference = (reference_expression_t*) value;
3763 declaration_t *declaration = reference->declaration;
3764 if(declaration != NULL) {
3765 declaration->address_taken = 1;
3769 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3772 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3773 static expression_t *parse_##unexpression_type(unsigned precedence) \
3777 unary_expression_t *unary_expression \
3778 = allocate_ast_zero(sizeof(unary_expression[0])); \
3779 unary_expression->expression.type = EXPR_UNARY; \
3780 unary_expression->type = unexpression_type; \
3781 unary_expression->value = parse_sub_expression(precedence); \
3783 sfunc(unary_expression); \
3785 return (expression_t*) unary_expression; \
3788 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3789 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3790 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3791 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3792 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3793 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3794 semantic_unexpr_integer)
3795 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3797 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3800 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3802 static expression_t *parse_##unexpression_type(unsigned precedence, \
3803 expression_t *left) \
3805 (void) precedence; \
3808 unary_expression_t *unary_expression \
3809 = allocate_ast_zero(sizeof(unary_expression[0])); \
3810 unary_expression->expression.type = EXPR_UNARY; \
3811 unary_expression->type = unexpression_type; \
3812 unary_expression->value = left; \
3814 sfunc(unary_expression); \
3816 return (expression_t*) unary_expression; \
3819 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3821 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3824 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3826 /* TODO: handle complex + imaginary types */
3828 /* § 6.3.1.8 Usual arithmetic conversions */
3829 if(type_left == type_long_double || type_right == type_long_double) {
3830 return type_long_double;
3831 } else if(type_left == type_double || type_right == type_double) {
3833 } else if(type_left == type_float || type_right == type_float) {
3837 type_right = promote_integer(type_right);
3838 type_left = promote_integer(type_left);
3840 if(type_left == type_right)
3843 bool signed_left = is_type_signed(type_left);
3844 bool signed_right = is_type_signed(type_right);
3845 int rank_left = get_rank(type_left);
3846 int rank_right = get_rank(type_right);
3847 if(rank_left < rank_right) {
3848 if(signed_left == signed_right || !signed_right) {
3854 if(signed_left == signed_right || !signed_left) {
3862 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3864 expression_t *left = expression->left;
3865 expression_t *right = expression->right;
3866 type_t *orig_type_left = left->base.datatype;
3867 type_t *orig_type_right = right->base.datatype;
3869 if(orig_type_left == NULL || orig_type_right == NULL)
3872 type_t *type_left = skip_typeref(orig_type_left);
3873 type_t *type_right = skip_typeref(orig_type_right);
3875 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3876 /* TODO: improve error message */
3877 parser_print_error_prefix();
3878 fprintf(stderr, "operation needs arithmetic types\n");
3882 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3883 expression->left = create_implicit_cast(left, arithmetic_type);
3884 expression->right = create_implicit_cast(right, arithmetic_type);
3885 expression->expression.datatype = arithmetic_type;
3888 static void semantic_shift_op(binary_expression_t *expression)
3890 expression_t *left = expression->left;
3891 expression_t *right = expression->right;
3892 type_t *orig_type_left = left->base.datatype;
3893 type_t *orig_type_right = right->base.datatype;
3895 if(orig_type_left == NULL || orig_type_right == NULL)
3898 type_t *type_left = skip_typeref(orig_type_left);
3899 type_t *type_right = skip_typeref(orig_type_right);
3901 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3902 /* TODO: improve error message */
3903 parser_print_error_prefix();
3904 fprintf(stderr, "operation needs integer types\n");
3908 type_left = promote_integer(type_left);
3909 type_right = promote_integer(type_right);
3911 expression->left = create_implicit_cast(left, type_left);
3912 expression->right = create_implicit_cast(right, type_right);
3913 expression->expression.datatype = type_left;
3916 static void semantic_add(binary_expression_t *expression)
3918 expression_t *left = expression->left;
3919 expression_t *right = expression->right;
3920 type_t *orig_type_left = left->base.datatype;
3921 type_t *orig_type_right = right->base.datatype;
3923 if(orig_type_left == NULL || orig_type_right == NULL)
3926 type_t *type_left = skip_typeref(orig_type_left);
3927 type_t *type_right = skip_typeref(orig_type_right);
3930 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3931 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3932 expression->left = create_implicit_cast(left, arithmetic_type);
3933 expression->right = create_implicit_cast(right, arithmetic_type);
3934 expression->expression.datatype = arithmetic_type;
3936 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
3937 expression->expression.datatype = type_left;
3938 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
3939 expression->expression.datatype = type_right;
3941 parser_print_error_prefix();
3942 fprintf(stderr, "invalid operands to binary + (");
3943 print_type_quoted(orig_type_left);
3944 fprintf(stderr, ", ");
3945 print_type_quoted(orig_type_right);
3946 fprintf(stderr, ")\n");
3950 static void semantic_sub(binary_expression_t *expression)
3952 expression_t *left = expression->left;
3953 expression_t *right = expression->right;
3954 type_t *orig_type_left = left->base.datatype;
3955 type_t *orig_type_right = right->base.datatype;
3957 if(orig_type_left == NULL || orig_type_right == NULL)
3960 type_t *type_left = skip_typeref(orig_type_left);
3961 type_t *type_right = skip_typeref(orig_type_right);
3964 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3965 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3966 expression->left = create_implicit_cast(left, arithmetic_type);
3967 expression->right = create_implicit_cast(right, arithmetic_type);
3968 expression->expression.datatype = arithmetic_type;
3970 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3971 expression->expression.datatype = type_left;
3972 } else if(type_left->type == TYPE_POINTER &&
3973 type_right->type == TYPE_POINTER) {
3974 if(!pointers_compatible(type_left, type_right)) {
3975 parser_print_error_prefix();
3976 fprintf(stderr, "pointers to incompatible objects to binary - (");
3977 print_type_quoted(orig_type_left);
3978 fprintf(stderr, ", ");
3979 print_type_quoted(orig_type_right);
3980 fprintf(stderr, ")\n");
3982 expression->expression.datatype = type_ptrdiff_t;
3985 parser_print_error_prefix();
3986 fprintf(stderr, "invalid operands to binary - (");
3987 print_type_quoted(orig_type_left);
3988 fprintf(stderr, ", ");
3989 print_type_quoted(orig_type_right);
3990 fprintf(stderr, ")\n");
3994 static void semantic_comparison(binary_expression_t *expression)
3996 expression_t *left = expression->left;
3997 expression_t *right = expression->right;
3998 type_t *orig_type_left = left->base.datatype;
3999 type_t *orig_type_right = right->base.datatype;
4001 if(orig_type_left == NULL || orig_type_right == NULL)
4004 type_t *type_left = skip_typeref(orig_type_left);
4005 type_t *type_right = skip_typeref(orig_type_right);
4007 /* TODO non-arithmetic types */
4008 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4009 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4010 expression->left = create_implicit_cast(left, arithmetic_type);
4011 expression->right = create_implicit_cast(right, arithmetic_type);
4012 expression->expression.datatype = arithmetic_type;
4013 } else if (type_left->type == TYPE_POINTER &&
4014 type_right->type == TYPE_POINTER) {
4015 /* TODO check compatibility */
4016 } else if (type_left->type == TYPE_POINTER) {
4017 expression->right = create_implicit_cast(right, type_left);
4018 } else if (type_right->type == TYPE_POINTER) {
4019 expression->left = create_implicit_cast(left, type_right);
4021 type_error_incompatible("invalid operands in comparison",
4022 token.source_position, type_left, type_right);
4024 expression->expression.datatype = type_int;
4027 static void semantic_arithmetic_assign(binary_expression_t *expression)
4029 expression_t *left = expression->left;
4030 expression_t *right = expression->right;
4031 type_t *orig_type_left = left->base.datatype;
4032 type_t *orig_type_right = right->base.datatype;
4034 if(orig_type_left == NULL || orig_type_right == NULL)
4037 type_t *type_left = skip_typeref(orig_type_left);
4038 type_t *type_right = skip_typeref(orig_type_right);
4040 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4041 /* TODO: improve error message */
4042 parser_print_error_prefix();
4043 fprintf(stderr, "operation needs arithmetic types\n");
4047 /* combined instructions are tricky. We can't create an implicit cast on
4048 * the left side, because we need the uncasted form for the store.
4049 * The ast2firm pass has to know that left_type must be right_type
4050 * for the arithmeitc operation and create a cast by itself */
4051 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4052 expression->right = create_implicit_cast(right, arithmetic_type);
4053 expression->expression.datatype = type_left;
4056 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4058 expression_t *left = expression->left;
4059 expression_t *right = expression->right;
4060 type_t *orig_type_left = left->base.datatype;
4061 type_t *orig_type_right = right->base.datatype;
4063 if(orig_type_left == NULL || orig_type_right == NULL)
4066 type_t *type_left = skip_typeref(orig_type_left);
4067 type_t *type_right = skip_typeref(orig_type_right);
4069 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4070 /* combined instructions are tricky. We can't create an implicit cast on
4071 * the left side, because we need the uncasted form for the store.
4072 * The ast2firm pass has to know that left_type must be right_type
4073 * for the arithmeitc operation and create a cast by itself */
4074 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4075 expression->right = create_implicit_cast(right, arithmetic_type);
4076 expression->expression.datatype = type_left;
4077 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
4078 expression->expression.datatype = type_left;
4080 parser_print_error_prefix();
4081 fputs("Incompatible types ", stderr);
4082 print_type_quoted(orig_type_left);
4083 fputs(" and ", stderr);
4084 print_type_quoted(orig_type_right);
4085 fputs(" in assignment\n", stderr);
4090 static void semantic_logical_op(binary_expression_t *expression)
4092 expression_t *left = expression->left;
4093 expression_t *right = expression->right;
4094 type_t *orig_type_left = left->base.datatype;
4095 type_t *orig_type_right = right->base.datatype;
4097 if(orig_type_left == NULL || orig_type_right == NULL)
4100 type_t *type_left = skip_typeref(orig_type_left);
4101 type_t *type_right = skip_typeref(orig_type_right);
4103 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4104 /* TODO: improve error message */
4105 parser_print_error_prefix();
4106 fprintf(stderr, "operation needs scalar types\n");
4110 expression->expression.datatype = type_int;
4113 static bool has_const_fields(type_t *type)
4120 static void semantic_binexpr_assign(binary_expression_t *expression)
4122 expression_t *left = expression->left;
4123 type_t *orig_type_left = left->base.datatype;
4125 if(orig_type_left == NULL)
4128 type_t *type_left = revert_automatic_type_conversion(left);
4129 type_left = skip_typeref(orig_type_left);
4131 /* must be a modifiable lvalue */
4132 if (type_left->type == TYPE_ARRAY) {
4133 parser_print_error_prefix();
4134 fprintf(stderr, "Cannot assign to arrays ('");
4135 print_expression(left);
4136 fprintf(stderr, "')\n");
4139 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4140 parser_print_error_prefix();
4141 fprintf(stderr, "assignment to readonly location '");
4142 print_expression(left);
4143 fprintf(stderr, "' (type ");
4144 print_type_quoted(orig_type_left);
4145 fprintf(stderr, ")\n");
4148 if(is_type_incomplete(type_left)) {
4149 parser_print_error_prefix();
4150 fprintf(stderr, "left-hand side of assignment '");
4151 print_expression(left);
4152 fprintf(stderr, "' has incomplete type ");
4153 print_type_quoted(orig_type_left);
4154 fprintf(stderr, "\n");
4157 if(is_type_compound(type_left) && has_const_fields(type_left)) {
4158 parser_print_error_prefix();
4159 fprintf(stderr, "can't assign to '");
4160 print_expression(left);
4161 fprintf(stderr, "' because compound type ");
4162 print_type_quoted(orig_type_left);
4163 fprintf(stderr, " has readonly fields\n");
4167 semantic_assign(orig_type_left, &expression->right, "assignment");
4169 expression->expression.datatype = orig_type_left;
4172 static void semantic_comma(binary_expression_t *expression)
4174 expression->expression.datatype = expression->right->base.datatype;
4177 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4178 static expression_t *parse_##binexpression_type(unsigned precedence, \
4179 expression_t *left) \
4183 expression_t *right = parse_sub_expression(precedence + lr); \
4185 binary_expression_t *binexpr \
4186 = allocate_ast_zero(sizeof(binexpr[0])); \
4187 binexpr->expression.type = EXPR_BINARY; \
4188 binexpr->type = binexpression_type; \
4189 binexpr->left = left; \
4190 binexpr->right = right; \
4193 return (expression_t*) binexpr; \
4196 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
4197 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
4198 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
4199 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
4200 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
4201 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
4202 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
4203 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
4204 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
4205 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
4206 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
4207 semantic_comparison, 1)
4208 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
4209 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
4210 semantic_comparison, 1)
4211 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
4212 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
4213 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
4214 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
4215 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
4216 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
4217 semantic_shift_op, 1)
4218 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
4219 semantic_shift_op, 1)
4220 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
4221 semantic_arithmetic_addsubb_assign, 0)
4222 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
4223 semantic_arithmetic_addsubb_assign, 0)
4224 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
4225 semantic_arithmetic_assign, 0)
4226 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
4227 semantic_arithmetic_assign, 0)
4228 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
4229 semantic_arithmetic_assign, 0)
4230 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
4231 semantic_arithmetic_assign, 0)
4232 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
4233 semantic_arithmetic_assign, 0)
4234 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
4235 semantic_arithmetic_assign, 0)
4236 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
4237 semantic_arithmetic_assign, 0)
4238 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
4239 semantic_arithmetic_assign, 0)
4241 static expression_t *parse_sub_expression(unsigned precedence)
4243 if(token.type < 0) {
4244 return expected_expression_error();
4247 expression_parser_function_t *parser
4248 = &expression_parsers[token.type];
4249 source_position_t source_position = token.source_position;
4252 if(parser->parser != NULL) {
4253 left = parser->parser(parser->precedence);
4255 left = parse_primary_expression();
4257 assert(left != NULL);
4258 left->base.source_position = source_position;
4261 if(token.type < 0) {
4262 return expected_expression_error();
4265 parser = &expression_parsers[token.type];
4266 if(parser->infix_parser == NULL)
4268 if(parser->infix_precedence < precedence)
4271 left = parser->infix_parser(parser->infix_precedence, left);
4273 assert(left != NULL);
4274 assert(left->type != EXPR_UNKNOWN);
4275 left->base.source_position = source_position;
4281 static expression_t *parse_expression(void)
4283 return parse_sub_expression(1);
4288 static void register_expression_parser(parse_expression_function parser,
4289 int token_type, unsigned precedence)
4291 expression_parser_function_t *entry = &expression_parsers[token_type];
4293 if(entry->parser != NULL) {
4294 fprintf(stderr, "for token ");
4295 print_token_type(stderr, (token_type_t) token_type);
4296 fprintf(stderr, "\n");
4297 panic("trying to register multiple expression parsers for a token");
4299 entry->parser = parser;
4300 entry->precedence = precedence;
4303 static void register_expression_infix_parser(
4304 parse_expression_infix_function parser, int token_type,
4305 unsigned precedence)
4307 expression_parser_function_t *entry = &expression_parsers[token_type];
4309 if(entry->infix_parser != NULL) {
4310 fprintf(stderr, "for token ");
4311 print_token_type(stderr, (token_type_t) token_type);
4312 fprintf(stderr, "\n");
4313 panic("trying to register multiple infix expression parsers for a "
4316 entry->infix_parser = parser;
4317 entry->infix_precedence = precedence;
4320 static void init_expression_parsers(void)
4322 memset(&expression_parsers, 0, sizeof(expression_parsers));
4324 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
4325 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
4326 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
4327 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
4328 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
4329 T_GREATERGREATER, 16);
4330 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
4331 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
4332 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
4333 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
4334 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
4335 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
4336 T_GREATEREQUAL, 14);
4337 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
4338 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
4339 T_EXCLAMATIONMARKEQUAL, 13);
4340 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
4341 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
4342 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
4343 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
4344 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
4345 register_expression_infix_parser(parse_conditional_expression, '?', 7);
4346 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
4347 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
4348 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
4349 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
4350 T_ASTERISKEQUAL, 2);
4351 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
4352 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
4354 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
4355 T_LESSLESSEQUAL, 2);
4356 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
4357 T_GREATERGREATEREQUAL, 2);
4358 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
4360 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
4362 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
4365 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
4367 register_expression_infix_parser(parse_array_expression, '[', 30);
4368 register_expression_infix_parser(parse_call_expression, '(', 30);
4369 register_expression_infix_parser(parse_select_expression, '.', 30);
4370 register_expression_infix_parser(parse_select_expression,
4371 T_MINUSGREATER, 30);
4372 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
4374 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
4377 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
4378 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
4379 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
4380 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
4381 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
4382 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
4383 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
4384 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
4385 register_expression_parser(parse_sizeof, T_sizeof, 25);
4386 register_expression_parser(parse_extension, T___extension__, 25);
4387 register_expression_parser(parse_builtin_classify_type,
4388 T___builtin_classify_type, 25);
4391 static asm_constraint_t *parse_asm_constraints(void)
4393 asm_constraint_t *result = NULL;
4394 asm_constraint_t *last = NULL;
4396 while(token.type == T_STRING_LITERAL || token.type == '[') {
4397 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4398 memset(constraint, 0, sizeof(constraint[0]));
4400 if(token.type == '[') {
4402 if(token.type != T_IDENTIFIER) {
4403 parse_error_expected("while parsing asm constraint",
4407 constraint->symbol = token.v.symbol;
4412 constraint->constraints = parse_string_literals();
4414 constraint->expression = parse_expression();
4418 last->next = constraint;
4420 result = constraint;
4424 if(token.type != ',')
4432 static asm_clobber_t *parse_asm_clobbers(void)
4434 asm_clobber_t *result = NULL;
4435 asm_clobber_t *last = NULL;
4437 while(token.type == T_STRING_LITERAL) {
4438 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4439 clobber->clobber = parse_string_literals();
4442 last->next = clobber;
4448 if(token.type != ',')
4456 static statement_t *parse_asm_statement(void)
4460 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4461 statement->base.source_position = token.source_position;
4463 asm_statement_t *asm_statement = &statement->asms;
4465 if(token.type == T_volatile) {
4467 asm_statement->is_volatile = true;
4471 asm_statement->asm_text = parse_string_literals();
4473 if(token.type != ':')
4477 asm_statement->inputs = parse_asm_constraints();
4478 if(token.type != ':')
4482 asm_statement->outputs = parse_asm_constraints();
4483 if(token.type != ':')
4487 asm_statement->clobbers = parse_asm_clobbers();
4495 static statement_t *parse_case_statement(void)
4499 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4501 statement->base.source_position = token.source_position;
4502 statement->case_label.expression = parse_expression();
4505 statement->case_label.label_statement = parse_statement();
4510 static statement_t *parse_default_statement(void)
4514 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4516 statement->base.source_position = token.source_position;
4519 statement->label.label_statement = parse_statement();
4524 static declaration_t *get_label(symbol_t *symbol)
4526 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4527 assert(current_function != NULL);
4528 /* if we found a label in the same function, then we already created the
4530 if(candidate != NULL
4531 && candidate->parent_context == ¤t_function->context) {
4535 /* otherwise we need to create a new one */
4536 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4537 declaration->namespc = NAMESPACE_LABEL;
4538 declaration->symbol = symbol;
4540 label_push(declaration);
4545 static statement_t *parse_label_statement(void)
4547 assert(token.type == T_IDENTIFIER);
4548 symbol_t *symbol = token.v.symbol;
4551 declaration_t *label = get_label(symbol);
4553 /* if source position is already set then the label is defined twice,
4554 * otherwise it was just mentioned in a goto so far */
4555 if(label->source_position.input_name != NULL) {
4556 parser_print_error_prefix();
4557 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
4558 parser_print_error_prefix_pos(label->source_position);
4559 fprintf(stderr, "previous definition of '%s' was here\n",
4562 label->source_position = token.source_position;
4565 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4567 label_statement->statement.type = STATEMENT_LABEL;
4568 label_statement->statement.source_position = token.source_position;
4569 label_statement->label = label;
4573 if(token.type == '}') {
4574 parse_error("label at end of compound statement");
4575 return (statement_t*) label_statement;
4577 label_statement->label_statement = parse_statement();
4580 return (statement_t*) label_statement;
4583 static statement_t *parse_if(void)
4587 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4588 statement->statement.type = STATEMENT_IF;
4589 statement->statement.source_position = token.source_position;
4592 statement->condition = parse_expression();
4595 statement->true_statement = parse_statement();
4596 if(token.type == T_else) {
4598 statement->false_statement = parse_statement();
4601 return (statement_t*) statement;
4604 static statement_t *parse_switch(void)
4608 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4609 statement->statement.type = STATEMENT_SWITCH;
4610 statement->statement.source_position = token.source_position;
4613 statement->expression = parse_expression();
4615 statement->body = parse_statement();
4617 return (statement_t*) statement;
4620 static statement_t *parse_while(void)
4624 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4625 statement->statement.type = STATEMENT_WHILE;
4626 statement->statement.source_position = token.source_position;
4629 statement->condition = parse_expression();
4631 statement->body = parse_statement();
4633 return (statement_t*) statement;
4636 static statement_t *parse_do(void)
4640 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4641 statement->statement.type = STATEMENT_DO_WHILE;
4642 statement->statement.source_position = token.source_position;
4644 statement->body = parse_statement();
4647 statement->condition = parse_expression();
4651 return (statement_t*) statement;
4654 static statement_t *parse_for(void)
4658 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4659 statement->statement.type = STATEMENT_FOR;
4660 statement->statement.source_position = token.source_position;
4664 int top = environment_top();
4665 context_t *last_context = context;
4666 set_context(&statement->context);
4668 if(token.type != ';') {
4669 if(is_declaration_specifier(&token, false)) {
4670 parse_declaration(record_declaration);
4672 statement->initialisation = parse_expression();
4679 if(token.type != ';') {
4680 statement->condition = parse_expression();
4683 if(token.type != ')') {
4684 statement->step = parse_expression();
4687 statement->body = parse_statement();
4689 assert(context == &statement->context);
4690 set_context(last_context);
4691 environment_pop_to(top);
4693 return (statement_t*) statement;
4696 static statement_t *parse_goto(void)
4700 if(token.type != T_IDENTIFIER) {
4701 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4705 symbol_t *symbol = token.v.symbol;
4708 declaration_t *label = get_label(symbol);
4710 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4712 statement->statement.type = STATEMENT_GOTO;
4713 statement->statement.source_position = token.source_position;
4715 statement->label = label;
4719 return (statement_t*) statement;
4722 static statement_t *parse_continue(void)
4727 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4728 statement->type = STATEMENT_CONTINUE;
4729 statement->base.source_position = token.source_position;
4734 static statement_t *parse_break(void)
4739 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4740 statement->type = STATEMENT_BREAK;
4741 statement->base.source_position = token.source_position;
4746 static statement_t *parse_return(void)
4750 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4752 statement->statement.type = STATEMENT_RETURN;
4753 statement->statement.source_position = token.source_position;
4755 assert(current_function->type->type == TYPE_FUNCTION);
4756 function_type_t *function_type = ¤t_function->type->function;
4757 type_t *return_type = function_type->result_type;
4759 expression_t *return_value = NULL;
4760 if(token.type != ';') {
4761 return_value = parse_expression();
4765 if(return_type == NULL)
4766 return (statement_t*) statement;
4768 return_type = skip_typeref(return_type);
4770 if(return_value != NULL) {
4771 type_t *return_value_type = skip_typeref(return_value->base.datatype);
4773 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
4774 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
4775 parse_warning("'return' with a value, in function returning void");
4776 return_value = NULL;
4778 if(return_type != NULL) {
4779 semantic_assign(return_type, &return_value, "'return'");
4783 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4784 parse_warning("'return' without value, in function returning "
4788 statement->return_value = return_value;
4790 return (statement_t*) statement;
4793 static statement_t *parse_declaration_statement(void)
4795 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
4797 statement->base.source_position = token.source_position;
4799 declaration_t *before = last_declaration;
4800 parse_declaration(record_declaration);
4802 if(before == NULL) {
4803 statement->declaration.declarations_begin = context->declarations;
4805 statement->declaration.declarations_begin = before->next;
4807 statement->declaration.declarations_end = last_declaration;
4812 static statement_t *parse_expression_statement(void)
4814 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
4816 statement->base.source_position = token.source_position;
4817 statement->expression.expression = parse_expression();
4824 static statement_t *parse_statement(void)
4826 statement_t *statement = NULL;
4828 /* declaration or statement */
4829 switch(token.type) {
4831 statement = parse_asm_statement();
4835 statement = parse_case_statement();
4839 statement = parse_default_statement();
4843 statement = parse_compound_statement();
4847 statement = parse_if();
4851 statement = parse_switch();
4855 statement = parse_while();
4859 statement = parse_do();
4863 statement = parse_for();
4867 statement = parse_goto();
4871 statement = parse_continue();
4875 statement = parse_break();
4879 statement = parse_return();
4888 if(look_ahead(1)->type == ':') {
4889 statement = parse_label_statement();
4893 if(is_typedef_symbol(token.v.symbol)) {
4894 statement = parse_declaration_statement();
4898 statement = parse_expression_statement();
4901 case T___extension__:
4902 /* this can be a prefix to a declaration or an expression statement */
4903 /* we simply eat it now and parse the rest with tail recursion */
4906 } while(token.type == T___extension__);
4907 statement = parse_statement();
4911 statement = parse_declaration_statement();
4915 statement = parse_expression_statement();
4919 assert(statement == NULL
4920 || statement->base.source_position.input_name != NULL);
4925 static statement_t *parse_compound_statement(void)
4927 compound_statement_t *compound_statement
4928 = allocate_ast_zero(sizeof(compound_statement[0]));
4929 compound_statement->statement.type = STATEMENT_COMPOUND;
4930 compound_statement->statement.source_position = token.source_position;
4934 int top = environment_top();
4935 context_t *last_context = context;
4936 set_context(&compound_statement->context);
4938 statement_t *last_statement = NULL;
4940 while(token.type != '}' && token.type != T_EOF) {
4941 statement_t *statement = parse_statement();
4942 if(statement == NULL)
4945 if(last_statement != NULL) {
4946 last_statement->base.next = statement;
4948 compound_statement->statements = statement;
4951 while(statement->base.next != NULL)
4952 statement = statement->base.next;
4954 last_statement = statement;
4957 if(token.type != '}') {
4958 parser_print_error_prefix_pos(
4959 compound_statement->statement.source_position);
4960 fprintf(stderr, "end of file while looking for closing '}'\n");
4964 assert(context == &compound_statement->context);
4965 set_context(last_context);
4966 environment_pop_to(top);
4968 return (statement_t*) compound_statement;
4971 static void initialize_builtins(void)
4973 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
4974 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
4975 type_size_t = make_global_typedef("__SIZE_TYPE__",
4976 make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_NONE));
4977 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__",
4978 make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_NONE));
4981 static translation_unit_t *parse_translation_unit(void)
4983 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4985 assert(global_context == NULL);
4986 global_context = &unit->context;
4988 assert(context == NULL);
4989 set_context(&unit->context);
4991 initialize_builtins();
4993 while(token.type != T_EOF) {
4994 parse_external_declaration();
4997 assert(context == &unit->context);
4999 last_declaration = NULL;
5001 assert(global_context == &unit->context);
5002 global_context = NULL;
5007 translation_unit_t *parse(void)
5009 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5010 label_stack = NEW_ARR_F(stack_entry_t, 0);
5011 found_error = false;
5013 type_set_output(stderr);
5014 ast_set_output(stderr);
5016 lookahead_bufpos = 0;
5017 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5020 translation_unit_t *unit = parse_translation_unit();
5022 DEL_ARR_F(environment_stack);
5023 DEL_ARR_F(label_stack);
5031 void init_parser(void)
5033 init_expression_parsers();
5034 obstack_init(&temp_obst);
5036 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
5037 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE,
5038 TYPE_QUALIFIER_NONE);
5039 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE,
5040 TYPE_QUALIFIER_NONE);
5041 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_NONE);
5042 type_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_NONE);
5043 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
5044 type_void_ptr = make_pointer_type(type_void, TYPE_QUALIFIER_NONE);
5045 type_string = make_pointer_type(type_char, TYPE_QUALIFIER_NONE);
5048 void exit_parser(void)
5050 obstack_free(&temp_obst, NULL);