11 #include "type_hash.h"
13 #include "adt/bitfiddle.h"
14 #include "adt/error.h"
15 #include "adt/array.h"
17 //#define PRINT_TOKENS
18 //#define ABORT_ON_ERROR
19 #define MAX_LOOKAHEAD 2
23 declaration_t *old_declaration;
25 unsigned short namespc;
28 typedef struct declaration_specifiers_t declaration_specifiers_t;
29 struct declaration_specifiers_t {
30 source_position_t source_position;
31 unsigned char storage_class;
36 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
39 static token_t lookahead_buffer[MAX_LOOKAHEAD];
40 static int lookahead_bufpos;
41 static stack_entry_t *environment_stack = NULL;
42 static stack_entry_t *label_stack = NULL;
43 static context_t *global_context = NULL;
44 static context_t *context = NULL;
45 static declaration_t *last_declaration = NULL;
46 static declaration_t *current_function = NULL;
47 static struct obstack temp_obst;
48 static bool found_error;
50 static type_t *type_int = NULL;
51 static type_t *type_long_double = NULL;
52 static type_t *type_double = NULL;
53 static type_t *type_float = NULL;
54 static type_t *type_char = NULL;
55 static type_t *type_string = NULL;
56 static type_t *type_void = NULL;
57 static type_t *type_void_ptr = NULL;
58 static type_t *type_valist = NULL;
60 type_t *type_size_t = NULL;
61 type_t *type_ptrdiff_t = NULL;
62 type_t *type_wchar_t = NULL;
63 type_t *type_wchar_t_ptr = NULL;
65 static statement_t *parse_compound_statement(void);
66 static statement_t *parse_statement(void);
68 static expression_t *parse_sub_expression(unsigned precedence);
69 static expression_t *parse_expression(void);
70 static type_t *parse_typename(void);
72 static void parse_compound_type_entries(void);
73 static declaration_t *parse_declarator(
74 const declaration_specifiers_t *specifiers, bool may_be_abstract);
75 static declaration_t *record_declaration(declaration_t *declaration);
77 #define STORAGE_CLASSES \
84 #define TYPE_QUALIFIERS \
90 #ifdef PROVIDE_COMPLEX
91 #define COMPLEX_SPECIFIERS \
93 #define IMAGINARY_SPECIFIERS \
96 #define COMPLEX_SPECIFIERS
97 #define IMAGINARY_SPECIFIERS
100 #define TYPE_SPECIFIERS \
115 case T___builtin_va_list: \
119 #define DECLARATION_START \
124 #define TYPENAME_START \
128 static void *allocate_ast_zero(size_t size)
130 void *res = allocate_ast(size);
131 memset(res, 0, size);
135 static size_t get_statement_struct_size(statement_type_t type)
137 static const size_t sizes[] = {
138 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
139 [STATEMENT_RETURN] = sizeof(return_statement_t),
140 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
141 [STATEMENT_IF] = sizeof(if_statement_t),
142 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
143 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
144 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
145 [STATEMENT_BREAK] = sizeof(statement_base_t),
146 [STATEMENT_GOTO] = sizeof(goto_statement_t),
147 [STATEMENT_LABEL] = sizeof(label_statement_t),
148 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
149 [STATEMENT_WHILE] = sizeof(while_statement_t),
150 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
151 [STATEMENT_FOR] = sizeof(for_statement_t),
152 [STATEMENT_ASM] = sizeof(asm_statement_t)
154 assert(sizeof(sizes) / sizeof(sizes[0]) == STATEMENT_ASM + 1);
155 assert(type <= STATEMENT_ASM);
156 assert(sizes[type] != 0);
160 static statement_t *allocate_statement_zero(statement_type_t type)
162 size_t size = get_statement_struct_size(type);
163 statement_t *res = allocate_ast_zero(size);
165 res->base.type = type;
170 static size_t get_expression_struct_size(expression_type_t type)
172 static const size_t sizes[] = {
173 [EXPR_INVALID] = sizeof(expression_base_t),
174 [EXPR_REFERENCE] = sizeof(reference_expression_t),
175 [EXPR_CONST] = sizeof(const_expression_t),
176 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
177 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
178 [EXPR_CALL] = sizeof(call_expression_t),
179 [EXPR_UNARY] = sizeof(unary_expression_t),
180 [EXPR_BINARY] = sizeof(binary_expression_t),
181 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
182 [EXPR_SELECT] = sizeof(select_expression_t),
183 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
184 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
185 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
186 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
187 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
188 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
189 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
190 [EXPR_VA_START] = sizeof(va_start_expression_t),
191 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
192 [EXPR_STATEMENT] = sizeof(statement_expression_t)
194 assert(sizeof(sizes) / sizeof(sizes[0]) == EXPR_STATEMENT + 1);
195 assert(type <= EXPR_STATEMENT);
196 assert(sizes[type] != 0);
200 static expression_t *allocate_expression_zero(expression_type_t type)
202 size_t size = get_expression_struct_size(type);
203 expression_t *res = allocate_ast_zero(size);
205 res->base.type = type;
209 static size_t get_type_struct_size(type_type_t type)
211 static const size_t sizes[] = {
212 [TYPE_ATOMIC] = sizeof(atomic_type_t),
213 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
214 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
215 [TYPE_ENUM] = sizeof(enum_type_t),
216 [TYPE_FUNCTION] = sizeof(function_type_t),
217 [TYPE_POINTER] = sizeof(pointer_type_t),
218 [TYPE_ARRAY] = sizeof(array_type_t),
219 [TYPE_BUILTIN] = sizeof(builtin_type_t),
220 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
221 [TYPE_TYPEOF] = sizeof(typeof_type_t),
223 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
224 assert(type <= TYPE_TYPEOF);
225 assert(sizes[type] != 0);
229 static type_t *allocate_type_zero(type_type_t type)
231 size_t size = get_type_struct_size(type);
232 type_t *res = obstack_alloc(type_obst, size);
233 memset(res, 0, size);
235 res->base.type = type;
239 static size_t get_initializer_size(initializer_type_t type)
241 static const size_t sizes[] = {
242 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
243 [INITIALIZER_STRING] = sizeof(initializer_string_t),
244 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
245 [INITIALIZER_LIST] = sizeof(initializer_list_t)
247 assert(type < sizeof(sizes) / sizeof(*sizes));
248 assert(sizes[type] != 0);
252 static initializer_t *allocate_initializer(initializer_type_t type)
254 initializer_t *result = allocate_ast_zero(get_initializer_size(type));
260 static void free_type(void *type)
262 obstack_free(type_obst, type);
266 * returns the top element of the environment stack
268 static size_t environment_top(void)
270 return ARR_LEN(environment_stack);
273 static size_t label_top(void)
275 return ARR_LEN(label_stack);
280 static inline void next_token(void)
282 token = lookahead_buffer[lookahead_bufpos];
283 lookahead_buffer[lookahead_bufpos] = lexer_token;
286 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
289 print_token(stderr, &token);
290 fprintf(stderr, "\n");
294 static inline const token_t *look_ahead(int num)
296 assert(num > 0 && num <= MAX_LOOKAHEAD);
297 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
298 return &lookahead_buffer[pos];
301 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
303 static void error(void)
306 #ifdef ABORT_ON_ERROR
311 static void parser_print_prefix_pos(const source_position_t source_position)
313 fputs(source_position.input_name, stderr);
315 fprintf(stderr, "%u", source_position.linenr);
319 static void parser_print_error_prefix_pos(
320 const source_position_t source_position)
322 parser_print_prefix_pos(source_position);
323 fputs("error: ", stderr);
327 static void parser_print_error_prefix(void)
329 parser_print_error_prefix_pos(token.source_position);
332 static void parse_error(const char *message)
334 parser_print_error_prefix();
335 fprintf(stderr, "parse error: %s\n", message);
338 static void parser_print_warning_prefix_pos(
339 const source_position_t source_position)
341 parser_print_prefix_pos(source_position);
342 fputs("warning: ", stderr);
345 static void parser_print_warning_prefix(void)
347 parser_print_warning_prefix_pos(token.source_position);
350 static void parse_warning_pos(const source_position_t source_position,
351 const char *const message)
353 parser_print_prefix_pos(source_position);
354 fprintf(stderr, "warning: %s\n", message);
357 static void parse_warning(const char *message)
359 parse_warning_pos(token.source_position, message);
362 static void parse_error_expected(const char *message, ...)
367 if(message != NULL) {
368 parser_print_error_prefix();
369 fprintf(stderr, "%s\n", message);
371 parser_print_error_prefix();
372 fputs("Parse error: got ", stderr);
373 print_token(stderr, &token);
374 fputs(", expected ", stderr);
376 va_start(args, message);
377 token_type_t token_type = va_arg(args, token_type_t);
378 while(token_type != 0) {
382 fprintf(stderr, ", ");
384 print_token_type(stderr, token_type);
385 token_type = va_arg(args, token_type_t);
388 fprintf(stderr, "\n");
391 static void print_type_quoted(type_t *type)
398 static void type_error(const char *msg, const source_position_t source_position,
401 parser_print_error_prefix_pos(source_position);
402 fprintf(stderr, "%s, but found type ", msg);
403 print_type_quoted(type);
407 static void type_error_incompatible(const char *msg,
408 const source_position_t source_position, type_t *type1, type_t *type2)
410 parser_print_error_prefix_pos(source_position);
411 fprintf(stderr, "%s, incompatible types: ", msg);
412 print_type_quoted(type1);
413 fprintf(stderr, " - ");
414 print_type_quoted(type2);
415 fprintf(stderr, ")\n");
418 static void eat_block(void)
420 if(token.type == '{')
423 while(token.type != '}') {
424 if(token.type == T_EOF)
426 if(token.type == '{') {
435 static void eat_statement(void)
437 while(token.type != ';') {
438 if(token.type == T_EOF)
440 if(token.type == '}')
442 if(token.type == '{') {
451 static void eat_paren(void)
453 if(token.type == '(')
456 while(token.type != ')') {
457 if(token.type == T_EOF)
459 if(token.type == ')' || token.type == ';' || token.type == '}') {
462 if(token.type == '(') {
466 if(token.type == '{') {
475 #define expect(expected) \
476 if(UNLIKELY(token.type != (expected))) { \
477 parse_error_expected(NULL, (expected), 0); \
483 #define expect_block(expected) \
484 if(UNLIKELY(token.type != (expected))) { \
485 parse_error_expected(NULL, (expected), 0); \
491 #define expect_void(expected) \
492 if(UNLIKELY(token.type != (expected))) { \
493 parse_error_expected(NULL, (expected), 0); \
499 static void set_context(context_t *new_context)
501 context = new_context;
503 last_declaration = new_context->declarations;
504 if(last_declaration != NULL) {
505 while(last_declaration->next != NULL) {
506 last_declaration = last_declaration->next;
512 * called when we find a 2nd declarator for an identifier we already have a
515 static bool is_compatible_declaration(declaration_t *declaration,
516 declaration_t *previous)
518 /* happens for K&R style function parameters */
519 if(previous->type == NULL) {
520 previous->type = declaration->type;
524 type_t *type1 = skip_typeref(declaration->type);
525 type_t *type2 = skip_typeref(previous->type);
527 return types_compatible(type1, type2);
530 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
532 declaration_t *declaration = symbol->declaration;
533 for( ; declaration != NULL; declaration = declaration->symbol_next) {
534 if(declaration->namespc == namespc)
541 static const char *get_namespace_prefix(namespace_t namespc)
544 case NAMESPACE_NORMAL:
546 case NAMESPACE_UNION:
548 case NAMESPACE_STRUCT:
552 case NAMESPACE_LABEL:
555 panic("invalid namespace found");
559 * pushs an environment_entry on the environment stack and links the
560 * corresponding symbol to the new entry
562 static declaration_t *stack_push(stack_entry_t **stack_ptr,
563 declaration_t *declaration,
564 context_t *parent_context)
566 symbol_t *symbol = declaration->symbol;
567 namespace_t namespc = (namespace_t)declaration->namespc;
569 /* a declaration should be only pushed once */
570 declaration->parent_context = parent_context;
572 declaration_t *previous_declaration = get_declaration(symbol, namespc);
573 assert(declaration != previous_declaration);
574 if(previous_declaration != NULL
575 && previous_declaration->parent_context == context) {
576 if(!is_compatible_declaration(declaration, previous_declaration)) {
577 parser_print_error_prefix_pos(declaration->source_position);
578 fprintf(stderr, "definition of symbol '%s%s' with type ",
579 get_namespace_prefix(namespc), symbol->string);
580 print_type_quoted(declaration->type);
582 parser_print_error_prefix_pos(
583 previous_declaration->source_position);
584 fprintf(stderr, "is incompatible with previous declaration "
586 print_type_quoted(previous_declaration->type);
589 unsigned old_storage_class = previous_declaration->storage_class;
590 unsigned new_storage_class = declaration->storage_class;
591 if (current_function == NULL) {
592 if (old_storage_class != STORAGE_CLASS_STATIC &&
593 new_storage_class == STORAGE_CLASS_STATIC) {
594 parser_print_error_prefix_pos(declaration->source_position);
596 "static declaration of '%s' follows non-static declaration\n",
598 parser_print_error_prefix_pos(previous_declaration->source_position);
599 fprintf(stderr, "previous declaration of '%s' was here\n",
602 if (old_storage_class == STORAGE_CLASS_EXTERN) {
603 if (new_storage_class == STORAGE_CLASS_NONE) {
604 previous_declaration->storage_class = STORAGE_CLASS_NONE;
607 parser_print_warning_prefix_pos(declaration->source_position);
608 fprintf(stderr, "redundant declaration for '%s'\n",
610 parser_print_warning_prefix_pos(previous_declaration->source_position);
611 fprintf(stderr, "previous declaration of '%s' was here\n",
616 if (old_storage_class == STORAGE_CLASS_EXTERN &&
617 new_storage_class == STORAGE_CLASS_EXTERN) {
618 parser_print_warning_prefix_pos(declaration->source_position);
619 fprintf(stderr, "redundant extern declaration for '%s'\n",
621 parser_print_warning_prefix_pos(previous_declaration->source_position);
622 fprintf(stderr, "previous declaration of '%s' was here\n",
625 parser_print_error_prefix_pos(declaration->source_position);
626 if (old_storage_class == new_storage_class) {
627 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
629 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
631 parser_print_error_prefix_pos(previous_declaration->source_position);
632 fprintf(stderr, "previous declaration of '%s' was here\n",
637 return previous_declaration;
640 /* remember old declaration */
642 entry.symbol = symbol;
643 entry.old_declaration = symbol->declaration;
644 entry.namespc = (unsigned short) namespc;
645 ARR_APP1(stack_entry_t, *stack_ptr, entry);
647 /* replace/add declaration into declaration list of the symbol */
648 if(symbol->declaration == NULL) {
649 symbol->declaration = declaration;
651 declaration_t *iter_last = NULL;
652 declaration_t *iter = symbol->declaration;
653 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
654 /* replace an entry? */
655 if(iter->namespc == namespc) {
656 if(iter_last == NULL) {
657 symbol->declaration = declaration;
659 iter_last->symbol_next = declaration;
661 declaration->symbol_next = iter->symbol_next;
666 assert(iter_last->symbol_next == NULL);
667 iter_last->symbol_next = declaration;
674 static declaration_t *environment_push(declaration_t *declaration)
676 assert(declaration->source_position.input_name != NULL);
677 return stack_push(&environment_stack, declaration, context);
680 static declaration_t *label_push(declaration_t *declaration)
682 return stack_push(&label_stack, declaration, ¤t_function->context);
686 * pops symbols from the environment stack until @p new_top is the top element
688 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
690 stack_entry_t *stack = *stack_ptr;
691 size_t top = ARR_LEN(stack);
694 assert(new_top <= top);
698 for(i = top; i > new_top; --i) {
699 stack_entry_t *entry = &stack[i - 1];
701 declaration_t *old_declaration = entry->old_declaration;
702 symbol_t *symbol = entry->symbol;
703 namespace_t namespc = (namespace_t)entry->namespc;
705 /* replace/remove declaration */
706 declaration_t *declaration = symbol->declaration;
707 assert(declaration != NULL);
708 if(declaration->namespc == namespc) {
709 if(old_declaration == NULL) {
710 symbol->declaration = declaration->symbol_next;
712 symbol->declaration = old_declaration;
715 declaration_t *iter_last = declaration;
716 declaration_t *iter = declaration->symbol_next;
717 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
718 /* replace an entry? */
719 if(iter->namespc == namespc) {
720 assert(iter_last != NULL);
721 iter_last->symbol_next = old_declaration;
722 old_declaration->symbol_next = iter->symbol_next;
726 assert(iter != NULL);
730 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
733 static void environment_pop_to(size_t new_top)
735 stack_pop_to(&environment_stack, new_top);
738 static void label_pop_to(size_t new_top)
740 stack_pop_to(&label_stack, new_top);
744 static int get_rank(const type_t *type)
746 assert(!is_typeref(type));
747 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
748 * and esp. footnote 108). However we can't fold constants (yet), so we
749 * can't decide wether unsigned int is possible, while int always works.
750 * (unsigned int would be preferable when possible... for stuff like
751 * struct { enum { ... } bla : 4; } ) */
752 if(type->type == TYPE_ENUM)
753 return ATOMIC_TYPE_INT;
755 assert(type->type == TYPE_ATOMIC);
756 const atomic_type_t *atomic_type = &type->atomic;
757 atomic_type_type_t atype = atomic_type->atype;
761 static type_t *promote_integer(type_t *type)
763 if(get_rank(type) < ATOMIC_TYPE_INT)
769 static expression_t *create_cast_expression(expression_t *expression,
772 expression_t *cast = allocate_expression_zero(EXPR_UNARY);
774 cast->unary.type = UNEXPR_CAST_IMPLICIT;
775 cast->unary.value = expression;
776 cast->base.datatype = dest_type;
781 static bool is_null_pointer_constant(const expression_t *expression)
783 /* skip void* cast */
784 if(expression->type == EXPR_UNARY) {
785 const unary_expression_t *unary = &expression->unary;
786 if(unary->type == UNEXPR_CAST
787 && expression->base.datatype == type_void_ptr) {
788 expression = unary->value;
792 /* TODO: not correct yet, should be any constant integer expression
793 * which evaluates to 0 */
794 if (expression->type != EXPR_CONST)
797 type_t *const type = skip_typeref(expression->base.datatype);
798 if (!is_type_integer(type))
801 return expression->conste.v.int_value == 0;
804 static expression_t *create_implicit_cast(expression_t *expression,
807 type_t *source_type = expression->base.datatype;
809 if(source_type == NULL)
812 source_type = skip_typeref(source_type);
813 dest_type = skip_typeref(dest_type);
815 if(source_type == dest_type)
818 switch (dest_type->type) {
820 /* TODO warning for implicitly converting to enum */
822 if (source_type->type != TYPE_ATOMIC &&
823 source_type->type != TYPE_ENUM) {
824 panic("casting of non-atomic types not implemented yet");
827 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
828 type_error_incompatible("can't cast types",
829 expression->base.source_position, source_type,
834 return create_cast_expression(expression, dest_type);
837 switch (source_type->type) {
839 if (is_null_pointer_constant(expression)) {
840 return create_cast_expression(expression, dest_type);
845 if (pointers_compatible(source_type, dest_type)) {
846 return create_cast_expression(expression, dest_type);
851 array_type_t *array_type = &source_type->array;
852 pointer_type_t *pointer_type = &dest_type->pointer;
853 if (types_compatible(array_type->element_type,
854 pointer_type->points_to)) {
855 return create_cast_expression(expression, dest_type);
861 panic("casting of non-atomic types not implemented yet");
864 type_error_incompatible("can't implicitly cast types",
865 expression->base.source_position, source_type, dest_type);
869 panic("casting of non-atomic types not implemented yet");
873 /** Implements the rules from § 6.5.16.1 */
874 static void semantic_assign(type_t *orig_type_left, expression_t **right,
877 type_t *orig_type_right = (*right)->base.datatype;
879 if(orig_type_right == NULL)
882 type_t *const type_left = skip_typeref(orig_type_left);
883 type_t *const type_right = skip_typeref(orig_type_right);
885 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
886 (is_type_pointer(type_left) && is_null_pointer_constant(*right)) ||
887 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
888 && is_type_pointer(type_right))) {
889 *right = create_implicit_cast(*right, type_left);
893 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
894 pointer_type_t *pointer_type_left = &type_left->pointer;
895 pointer_type_t *pointer_type_right = &type_right->pointer;
896 type_t *points_to_left = pointer_type_left->points_to;
897 type_t *points_to_right = pointer_type_right->points_to;
899 points_to_left = skip_typeref(points_to_left);
900 points_to_right = skip_typeref(points_to_right);
902 /* the left type has all qualifiers from the right type */
903 unsigned missing_qualifiers
904 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
905 if(missing_qualifiers != 0) {
906 parser_print_error_prefix();
907 fprintf(stderr, "destination type ");
908 print_type_quoted(type_left);
909 fprintf(stderr, " in %s from type ", context);
910 print_type_quoted(type_right);
911 fprintf(stderr, " lacks qualifiers '");
912 print_type_qualifiers(missing_qualifiers);
913 fprintf(stderr, "' in pointed-to type\n");
917 points_to_left = get_unqualified_type(points_to_left);
918 points_to_right = get_unqualified_type(points_to_right);
920 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
921 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
922 && !types_compatible(points_to_left, points_to_right)) {
923 goto incompatible_assign_types;
926 *right = create_implicit_cast(*right, type_left);
930 if (is_type_compound(type_left)
931 && types_compatible(type_left, type_right)) {
932 *right = create_implicit_cast(*right, type_left);
936 incompatible_assign_types:
937 /* TODO: improve error message */
938 parser_print_error_prefix();
939 fprintf(stderr, "incompatible types in %s\n", context);
940 parser_print_error_prefix();
941 print_type_quoted(orig_type_left);
942 fputs(" <- ", stderr);
943 print_type_quoted(orig_type_right);
947 static expression_t *parse_constant_expression(void)
949 /* start parsing at precedence 7 (conditional expression) */
950 return parse_sub_expression(7);
953 static expression_t *parse_assignment_expression(void)
955 /* start parsing at precedence 2 (assignment expression) */
956 return parse_sub_expression(2);
959 static type_t *make_global_typedef(const char *name, type_t *type)
961 symbol_t *symbol = symbol_table_insert(name);
963 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
964 declaration->namespc = NAMESPACE_NORMAL;
965 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
966 declaration->type = type;
967 declaration->symbol = symbol;
968 declaration->source_position = builtin_source_position;
970 record_declaration(declaration);
972 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
973 typedef_type->typedeft.declaration = declaration;
978 static const char *parse_string_literals(void)
980 assert(token.type == T_STRING_LITERAL);
981 const char *result = token.v.string;
985 while(token.type == T_STRING_LITERAL) {
986 result = concat_strings(result, token.v.string);
993 static void parse_attributes(void)
997 case T___attribute__: {
1003 switch(token.type) {
1005 parse_error("EOF while parsing attribute");
1024 if(token.type != T_STRING_LITERAL) {
1025 parse_error_expected("while parsing assembler attribute",
1030 parse_string_literals();
1035 goto attributes_finished;
1039 attributes_finished:
1044 static designator_t *parse_designation(void)
1046 if(token.type != '[' && token.type != '.')
1049 designator_t *result = NULL;
1050 designator_t *last = NULL;
1053 designator_t *designator;
1054 switch(token.type) {
1056 designator = allocate_ast_zero(sizeof(designator[0]));
1058 designator->array_access = parse_constant_expression();
1062 designator = allocate_ast_zero(sizeof(designator[0]));
1064 if(token.type != T_IDENTIFIER) {
1065 parse_error_expected("while parsing designator",
1069 designator->symbol = token.v.symbol;
1077 assert(designator != NULL);
1079 last->next = designator;
1081 result = designator;
1088 static initializer_t *initializer_from_string(array_type_t *type,
1091 /* TODO: check len vs. size of array type */
1094 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
1095 initializer->string.string = string;
1100 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1101 wide_string_t *const string)
1103 /* TODO: check len vs. size of array type */
1106 initializer_t *const initializer =
1107 allocate_initializer(INITIALIZER_WIDE_STRING);
1108 initializer->wide_string.string = *string;
1113 static initializer_t *initializer_from_expression(type_t *type,
1114 expression_t *expression)
1116 /* TODO check that expression is a constant expression */
1118 /* § 6.7.8.14/15 char array may be initialized by string literals */
1119 type_t *const expr_type = expression->base.datatype;
1120 if (is_type_array(type) && expr_type->type == TYPE_POINTER) {
1121 array_type_t *const array_type = &type->array;
1122 type_t *const element_type = skip_typeref(array_type->element_type);
1124 if (element_type->type == TYPE_ATOMIC) {
1125 switch (expression->type) {
1126 case EXPR_STRING_LITERAL:
1127 if (element_type->atomic.atype == ATOMIC_TYPE_CHAR) {
1128 return initializer_from_string(array_type,
1129 expression->string.value);
1132 case EXPR_WIDE_STRING_LITERAL:
1133 if (get_unqualified_type(element_type) == skip_typeref(type_wchar_t)) {
1134 return initializer_from_wide_string(array_type,
1135 &expression->wide_string.value);
1143 type_t *expression_type = skip_typeref(expression->base.datatype);
1144 if(is_type_scalar(type) || types_compatible(type, expression_type)) {
1145 semantic_assign(type, &expression, "initializer");
1147 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1148 result->value.value = expression;
1156 static initializer_t *parse_sub_initializer(type_t *type,
1157 expression_t *expression,
1158 type_t *expression_type);
1160 static initializer_t *parse_sub_initializer_elem(type_t *type)
1162 if(token.type == '{') {
1163 return parse_sub_initializer(type, NULL, NULL);
1166 expression_t *expression = parse_assignment_expression();
1167 type_t *expression_type = skip_typeref(expression->base.datatype);
1169 return parse_sub_initializer(type, expression, expression_type);
1172 static bool had_initializer_brace_warning;
1174 static initializer_t *parse_sub_initializer(type_t *type,
1175 expression_t *expression,
1176 type_t *expression_type)
1178 if(is_type_scalar(type)) {
1179 /* there might be extra {} hierarchies */
1180 if(token.type == '{') {
1182 if(!had_initializer_brace_warning) {
1183 parse_warning("braces around scalar initializer");
1184 had_initializer_brace_warning = true;
1186 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1187 if(token.type == ',') {
1189 /* TODO: warn about excessive elements */
1195 if(expression == NULL) {
1196 expression = parse_assignment_expression();
1198 return initializer_from_expression(type, expression);
1201 /* does the expression match the currently looked at object to initalize */
1202 if(expression != NULL) {
1203 initializer_t *result = initializer_from_expression(type, expression);
1208 bool read_paren = false;
1209 if(token.type == '{') {
1214 /* descend into subtype */
1215 initializer_t *result = NULL;
1216 initializer_t **elems;
1217 if(is_type_array(type)) {
1218 array_type_t *array_type = &type->array;
1219 type_t *element_type = array_type->element_type;
1220 element_type = skip_typeref(element_type);
1223 had_initializer_brace_warning = false;
1224 if(expression == NULL) {
1225 sub = parse_sub_initializer_elem(element_type);
1227 sub = parse_sub_initializer(element_type, expression,
1231 /* didn't match the subtypes -> try the parent type */
1233 assert(!read_paren);
1237 elems = NEW_ARR_F(initializer_t*, 0);
1238 ARR_APP1(initializer_t*, elems, sub);
1241 if(token.type == '}')
1244 if(token.type == '}')
1247 sub = parse_sub_initializer_elem(element_type);
1249 /* TODO error, do nicer cleanup */
1250 parse_error("member initializer didn't match");
1254 ARR_APP1(initializer_t*, elems, sub);
1257 assert(is_type_compound(type));
1258 compound_type_t *compound_type = &type->compound;
1259 context_t *context = &compound_type->declaration->context;
1261 declaration_t *first = context->declarations;
1264 type_t *first_type = first->type;
1265 first_type = skip_typeref(first_type);
1268 had_initializer_brace_warning = false;
1269 if(expression == NULL) {
1270 sub = parse_sub_initializer_elem(first_type);
1272 sub = parse_sub_initializer(first_type, expression,expression_type);
1275 /* didn't match the subtypes -> try our parent type */
1277 assert(!read_paren);
1281 elems = NEW_ARR_F(initializer_t*, 0);
1282 ARR_APP1(initializer_t*, elems, sub);
1284 declaration_t *iter = first->next;
1285 for( ; iter != NULL; iter = iter->next) {
1286 if(iter->symbol == NULL)
1288 if(iter->namespc != NAMESPACE_NORMAL)
1291 if(token.type == '}')
1294 if(token.type == '}')
1297 type_t *iter_type = iter->type;
1298 iter_type = skip_typeref(iter_type);
1300 sub = parse_sub_initializer_elem(iter_type);
1302 /* TODO error, do nicer cleanup*/
1303 parse_error("member initializer didn't match");
1307 ARR_APP1(initializer_t*, elems, sub);
1311 int len = ARR_LEN(elems);
1312 size_t elems_size = sizeof(initializer_t*) * len;
1314 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1316 init->initializer.type = INITIALIZER_LIST;
1318 memcpy(init->initializers, elems, elems_size);
1321 result = (initializer_t*) init;
1324 if(token.type == ',')
1331 static initializer_t *parse_initializer(type_t *type)
1333 initializer_t *result;
1335 type = skip_typeref(type);
1337 if(token.type != '{') {
1338 expression_t *expression = parse_assignment_expression();
1339 initializer_t *initializer = initializer_from_expression(type, expression);
1340 if(initializer == NULL) {
1341 parser_print_error_prefix();
1342 fprintf(stderr, "initializer expression '");
1343 print_expression(expression);
1344 fprintf(stderr, "', type ");
1345 print_type_quoted(expression->base.datatype);
1346 fprintf(stderr, " is incompatible with type ");
1347 print_type_quoted(type);
1348 fprintf(stderr, "\n");
1353 if(is_type_scalar(type)) {
1357 expression_t *expression = parse_assignment_expression();
1358 result = initializer_from_expression(type, expression);
1360 if(token.type == ',')
1366 result = parse_sub_initializer(type, NULL, NULL);
1374 static declaration_t *parse_compound_type_specifier(bool is_struct)
1382 symbol_t *symbol = NULL;
1383 declaration_t *declaration = NULL;
1385 if (token.type == T___attribute__) {
1390 if(token.type == T_IDENTIFIER) {
1391 symbol = token.v.symbol;
1395 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1397 declaration = get_declaration(symbol, NAMESPACE_UNION);
1399 } else if(token.type != '{') {
1401 parse_error_expected("while parsing struct type specifier",
1402 T_IDENTIFIER, '{', 0);
1404 parse_error_expected("while parsing union type specifier",
1405 T_IDENTIFIER, '{', 0);
1411 if(declaration == NULL) {
1412 declaration = allocate_ast_zero(sizeof(declaration[0]));
1415 declaration->namespc = NAMESPACE_STRUCT;
1417 declaration->namespc = NAMESPACE_UNION;
1419 declaration->source_position = token.source_position;
1420 declaration->symbol = symbol;
1421 record_declaration(declaration);
1424 if(token.type == '{') {
1425 if(declaration->init.is_defined) {
1426 assert(symbol != NULL);
1427 parser_print_error_prefix();
1428 fprintf(stderr, "multiple definition of %s %s\n",
1429 is_struct ? "struct" : "union", symbol->string);
1430 declaration->context.declarations = NULL;
1432 declaration->init.is_defined = true;
1434 int top = environment_top();
1435 context_t *last_context = context;
1436 set_context(&declaration->context);
1438 parse_compound_type_entries();
1441 assert(context == &declaration->context);
1442 set_context(last_context);
1443 environment_pop_to(top);
1449 static void parse_enum_entries(enum_type_t *const enum_type)
1453 if(token.type == '}') {
1455 parse_error("empty enum not allowed");
1460 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1462 if(token.type != T_IDENTIFIER) {
1463 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1467 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1468 entry->type = (type_t*) enum_type;
1469 entry->symbol = token.v.symbol;
1470 entry->source_position = token.source_position;
1473 if(token.type == '=') {
1475 entry->init.enum_value = parse_constant_expression();
1480 record_declaration(entry);
1482 if(token.type != ',')
1485 } while(token.type != '}');
1490 static type_t *parse_enum_specifier(void)
1494 declaration_t *declaration;
1497 if(token.type == T_IDENTIFIER) {
1498 symbol = token.v.symbol;
1501 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1502 } else if(token.type != '{') {
1503 parse_error_expected("while parsing enum type specifier",
1504 T_IDENTIFIER, '{', 0);
1511 if(declaration == NULL) {
1512 declaration = allocate_ast_zero(sizeof(declaration[0]));
1514 declaration->namespc = NAMESPACE_ENUM;
1515 declaration->source_position = token.source_position;
1516 declaration->symbol = symbol;
1519 type_t *const type = allocate_type_zero(TYPE_ENUM);
1520 type->enumt.declaration = declaration;
1522 if(token.type == '{') {
1523 if(declaration->init.is_defined) {
1524 parser_print_error_prefix();
1525 fprintf(stderr, "multiple definitions of enum %s\n",
1528 record_declaration(declaration);
1529 declaration->init.is_defined = 1;
1531 parse_enum_entries(&type->enumt);
1539 * if a symbol is a typedef to another type, return true
1541 static bool is_typedef_symbol(symbol_t *symbol)
1543 const declaration_t *const declaration =
1544 get_declaration(symbol, NAMESPACE_NORMAL);
1546 declaration != NULL &&
1547 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1550 static type_t *parse_typeof(void)
1558 expression_t *expression = NULL;
1561 switch(token.type) {
1562 case T___extension__:
1563 /* this can be a prefix to a typename or an expression */
1564 /* we simply eat it now. */
1567 } while(token.type == T___extension__);
1571 if(is_typedef_symbol(token.v.symbol)) {
1572 type = parse_typename();
1574 expression = parse_expression();
1575 type = expression->base.datatype;
1580 type = parse_typename();
1584 expression = parse_expression();
1585 type = expression->base.datatype;
1591 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1592 typeof_type->typeoft.expression = expression;
1593 typeof_type->typeoft.typeof_type = type;
1599 SPECIFIER_SIGNED = 1 << 0,
1600 SPECIFIER_UNSIGNED = 1 << 1,
1601 SPECIFIER_LONG = 1 << 2,
1602 SPECIFIER_INT = 1 << 3,
1603 SPECIFIER_DOUBLE = 1 << 4,
1604 SPECIFIER_CHAR = 1 << 5,
1605 SPECIFIER_SHORT = 1 << 6,
1606 SPECIFIER_LONG_LONG = 1 << 7,
1607 SPECIFIER_FLOAT = 1 << 8,
1608 SPECIFIER_BOOL = 1 << 9,
1609 SPECIFIER_VOID = 1 << 10,
1610 #ifdef PROVIDE_COMPLEX
1611 SPECIFIER_COMPLEX = 1 << 11,
1612 SPECIFIER_IMAGINARY = 1 << 12,
1616 static type_t *create_builtin_type(symbol_t *const symbol,
1617 type_t *const real_type)
1619 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1620 type->builtin.symbol = symbol;
1621 type->builtin.real_type = real_type;
1623 type_t *result = typehash_insert(type);
1624 if (type != result) {
1631 static type_t *get_typedef_type(symbol_t *symbol)
1633 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1634 if(declaration == NULL
1635 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1638 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1639 type->typedeft.declaration = declaration;
1644 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1646 type_t *type = NULL;
1647 unsigned type_qualifiers = 0;
1648 unsigned type_specifiers = 0;
1651 specifiers->source_position = token.source_position;
1654 switch(token.type) {
1657 #define MATCH_STORAGE_CLASS(token, class) \
1659 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1660 parse_error("multiple storage classes in declaration " \
1663 specifiers->storage_class = class; \
1667 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1668 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1669 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1670 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1671 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1674 switch (specifiers->storage_class) {
1675 case STORAGE_CLASS_NONE:
1676 specifiers->storage_class = STORAGE_CLASS_THREAD;
1679 case STORAGE_CLASS_EXTERN:
1680 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1683 case STORAGE_CLASS_STATIC:
1684 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1688 parse_error("multiple storage classes in declaration specifiers");
1694 /* type qualifiers */
1695 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1697 type_qualifiers |= qualifier; \
1701 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1702 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1703 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1705 case T___extension__:
1710 /* type specifiers */
1711 #define MATCH_SPECIFIER(token, specifier, name) \
1714 if(type_specifiers & specifier) { \
1715 parse_error("multiple " name " type specifiers given"); \
1717 type_specifiers |= specifier; \
1721 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1722 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1723 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1724 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1725 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1726 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1727 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1728 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1729 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1730 #ifdef PROVIDE_COMPLEX
1731 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1732 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1736 specifiers->is_inline = true;
1741 if(type_specifiers & SPECIFIER_LONG_LONG) {
1742 parse_error("multiple type specifiers given");
1743 } else if(type_specifiers & SPECIFIER_LONG) {
1744 type_specifiers |= SPECIFIER_LONG_LONG;
1746 type_specifiers |= SPECIFIER_LONG;
1750 /* TODO: if type != NULL for the following rules should issue
1753 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1755 type->compound.declaration = parse_compound_type_specifier(true);
1759 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1761 type->compound.declaration = parse_compound_type_specifier(false);
1765 type = parse_enum_specifier();
1768 type = parse_typeof();
1770 case T___builtin_va_list:
1771 type = duplicate_type(type_valist);
1775 case T___attribute__:
1780 case T_IDENTIFIER: {
1781 type_t *typedef_type = get_typedef_type(token.v.symbol);
1783 if(typedef_type == NULL)
1784 goto finish_specifiers;
1787 type = typedef_type;
1791 /* function specifier */
1793 goto finish_specifiers;
1800 atomic_type_type_t atomic_type;
1802 /* match valid basic types */
1803 switch(type_specifiers) {
1804 case SPECIFIER_VOID:
1805 atomic_type = ATOMIC_TYPE_VOID;
1807 case SPECIFIER_CHAR:
1808 atomic_type = ATOMIC_TYPE_CHAR;
1810 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1811 atomic_type = ATOMIC_TYPE_SCHAR;
1813 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1814 atomic_type = ATOMIC_TYPE_UCHAR;
1816 case SPECIFIER_SHORT:
1817 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1818 case SPECIFIER_SHORT | SPECIFIER_INT:
1819 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1820 atomic_type = ATOMIC_TYPE_SHORT;
1822 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1823 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1824 atomic_type = ATOMIC_TYPE_USHORT;
1827 case SPECIFIER_SIGNED:
1828 case SPECIFIER_SIGNED | SPECIFIER_INT:
1829 atomic_type = ATOMIC_TYPE_INT;
1831 case SPECIFIER_UNSIGNED:
1832 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1833 atomic_type = ATOMIC_TYPE_UINT;
1835 case SPECIFIER_LONG:
1836 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1837 case SPECIFIER_LONG | SPECIFIER_INT:
1838 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1839 atomic_type = ATOMIC_TYPE_LONG;
1841 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1842 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1843 atomic_type = ATOMIC_TYPE_ULONG;
1845 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1846 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1847 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1848 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1850 atomic_type = ATOMIC_TYPE_LONGLONG;
1852 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1853 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1855 atomic_type = ATOMIC_TYPE_ULONGLONG;
1857 case SPECIFIER_FLOAT:
1858 atomic_type = ATOMIC_TYPE_FLOAT;
1860 case SPECIFIER_DOUBLE:
1861 atomic_type = ATOMIC_TYPE_DOUBLE;
1863 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1864 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1866 case SPECIFIER_BOOL:
1867 atomic_type = ATOMIC_TYPE_BOOL;
1869 #ifdef PROVIDE_COMPLEX
1870 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1871 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1873 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1874 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1876 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1877 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1879 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1880 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1882 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1883 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1885 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1886 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1890 /* invalid specifier combination, give an error message */
1891 if(type_specifiers == 0) {
1893 parse_warning("no type specifiers in declaration, using int");
1894 atomic_type = ATOMIC_TYPE_INT;
1897 parse_error("no type specifiers given in declaration");
1899 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1900 (type_specifiers & SPECIFIER_UNSIGNED)) {
1901 parse_error("signed and unsigned specifiers gives");
1902 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1903 parse_error("only integer types can be signed or unsigned");
1905 parse_error("multiple datatypes in declaration");
1907 atomic_type = ATOMIC_TYPE_INVALID;
1910 type = allocate_type_zero(TYPE_ATOMIC);
1911 type->atomic.atype = atomic_type;
1914 if(type_specifiers != 0) {
1915 parse_error("multiple datatypes in declaration");
1919 type->base.qualifiers = type_qualifiers;
1921 type_t *result = typehash_insert(type);
1922 if(newtype && result != type) {
1926 specifiers->type = result;
1929 static type_qualifiers_t parse_type_qualifiers(void)
1931 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1934 switch(token.type) {
1935 /* type qualifiers */
1936 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1937 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1938 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1941 return type_qualifiers;
1946 static declaration_t *parse_identifier_list(void)
1948 declaration_t *declarations = NULL;
1949 declaration_t *last_declaration = NULL;
1951 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1953 declaration->source_position = token.source_position;
1954 declaration->symbol = token.v.symbol;
1957 if(last_declaration != NULL) {
1958 last_declaration->next = declaration;
1960 declarations = declaration;
1962 last_declaration = declaration;
1964 if(token.type != ',')
1967 } while(token.type == T_IDENTIFIER);
1969 return declarations;
1972 static void semantic_parameter(declaration_t *declaration)
1974 /* TODO: improve error messages */
1976 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1977 parse_error("typedef not allowed in parameter list");
1978 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1979 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1980 parse_error("parameter may only have none or register storage class");
1983 type_t *orig_type = declaration->type;
1984 if(orig_type == NULL)
1986 type_t *type = skip_typeref(orig_type);
1988 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1989 * into a pointer. § 6.7.5.3 (7) */
1990 if (is_type_array(type)) {
1991 const array_type_t *arr_type = &type->array;
1992 type_t *element_type = arr_type->element_type;
1994 type = make_pointer_type(element_type, type->base.qualifiers);
1996 declaration->type = type;
1999 if(is_type_incomplete(type)) {
2000 parser_print_error_prefix();
2001 fprintf(stderr, "incomplete type (");
2002 print_type_quoted(orig_type);
2003 fprintf(stderr, ") not allowed for parameter '%s'\n",
2004 declaration->symbol->string);
2008 static declaration_t *parse_parameter(void)
2010 declaration_specifiers_t specifiers;
2011 memset(&specifiers, 0, sizeof(specifiers));
2013 parse_declaration_specifiers(&specifiers);
2015 declaration_t *declaration = parse_declarator(&specifiers, true);
2017 semantic_parameter(declaration);
2022 static declaration_t *parse_parameters(function_type_t *type)
2024 if(token.type == T_IDENTIFIER) {
2025 symbol_t *symbol = token.v.symbol;
2026 if(!is_typedef_symbol(symbol)) {
2027 type->kr_style_parameters = true;
2028 return parse_identifier_list();
2032 if(token.type == ')') {
2033 type->unspecified_parameters = 1;
2036 if(token.type == T_void && look_ahead(1)->type == ')') {
2041 declaration_t *declarations = NULL;
2042 declaration_t *declaration;
2043 declaration_t *last_declaration = NULL;
2044 function_parameter_t *parameter;
2045 function_parameter_t *last_parameter = NULL;
2048 switch(token.type) {
2052 return declarations;
2055 case T___extension__:
2057 declaration = parse_parameter();
2059 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2060 memset(parameter, 0, sizeof(parameter[0]));
2061 parameter->type = declaration->type;
2063 if(last_parameter != NULL) {
2064 last_declaration->next = declaration;
2065 last_parameter->next = parameter;
2067 type->parameters = parameter;
2068 declarations = declaration;
2070 last_parameter = parameter;
2071 last_declaration = declaration;
2075 return declarations;
2077 if(token.type != ',')
2078 return declarations;
2088 } construct_type_type_t;
2090 typedef struct construct_type_t construct_type_t;
2091 struct construct_type_t {
2092 construct_type_type_t type;
2093 construct_type_t *next;
2096 typedef struct parsed_pointer_t parsed_pointer_t;
2097 struct parsed_pointer_t {
2098 construct_type_t construct_type;
2099 type_qualifiers_t type_qualifiers;
2102 typedef struct construct_function_type_t construct_function_type_t;
2103 struct construct_function_type_t {
2104 construct_type_t construct_type;
2105 type_t *function_type;
2108 typedef struct parsed_array_t parsed_array_t;
2109 struct parsed_array_t {
2110 construct_type_t construct_type;
2111 type_qualifiers_t type_qualifiers;
2117 typedef struct construct_base_type_t construct_base_type_t;
2118 struct construct_base_type_t {
2119 construct_type_t construct_type;
2123 static construct_type_t *parse_pointer_declarator(void)
2127 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2128 memset(pointer, 0, sizeof(pointer[0]));
2129 pointer->construct_type.type = CONSTRUCT_POINTER;
2130 pointer->type_qualifiers = parse_type_qualifiers();
2132 return (construct_type_t*) pointer;
2135 static construct_type_t *parse_array_declarator(void)
2139 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2140 memset(array, 0, sizeof(array[0]));
2141 array->construct_type.type = CONSTRUCT_ARRAY;
2143 if(token.type == T_static) {
2144 array->is_static = true;
2148 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2149 if(type_qualifiers != 0) {
2150 if(token.type == T_static) {
2151 array->is_static = true;
2155 array->type_qualifiers = type_qualifiers;
2157 if(token.type == '*' && look_ahead(1)->type == ']') {
2158 array->is_variable = true;
2160 } else if(token.type != ']') {
2161 array->size = parse_assignment_expression();
2166 return (construct_type_t*) array;
2169 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2173 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2175 declaration_t *parameters = parse_parameters(&type->function);
2176 if(declaration != NULL) {
2177 declaration->context.declarations = parameters;
2180 construct_function_type_t *construct_function_type =
2181 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2182 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2183 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2184 construct_function_type->function_type = type;
2188 return (construct_type_t*) construct_function_type;
2191 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2192 bool may_be_abstract)
2194 /* construct a single linked list of construct_type_t's which describe
2195 * how to construct the final declarator type */
2196 construct_type_t *first = NULL;
2197 construct_type_t *last = NULL;
2200 while(token.type == '*') {
2201 construct_type_t *type = parse_pointer_declarator();
2212 /* TODO: find out if this is correct */
2215 construct_type_t *inner_types = NULL;
2217 switch(token.type) {
2219 if(declaration == NULL) {
2220 parse_error("no identifier expected in typename");
2222 declaration->symbol = token.v.symbol;
2223 declaration->source_position = token.source_position;
2229 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2235 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2236 /* avoid a loop in the outermost scope, because eat_statement doesn't
2238 if(token.type == '}' && current_function == NULL) {
2246 construct_type_t *p = last;
2249 construct_type_t *type;
2250 switch(token.type) {
2252 type = parse_function_declarator(declaration);
2255 type = parse_array_declarator();
2258 goto declarator_finished;
2261 /* insert in the middle of the list (behind p) */
2263 type->next = p->next;
2274 declarator_finished:
2277 /* append inner_types at the end of the list, we don't to set last anymore
2278 * as it's not needed anymore */
2280 assert(first == NULL);
2281 first = inner_types;
2283 last->next = inner_types;
2289 static type_t *construct_declarator_type(construct_type_t *construct_list,
2292 construct_type_t *iter = construct_list;
2293 for( ; iter != NULL; iter = iter->next) {
2294 switch(iter->type) {
2295 case CONSTRUCT_INVALID:
2296 panic("invalid type construction found");
2297 case CONSTRUCT_FUNCTION: {
2298 construct_function_type_t *construct_function_type
2299 = (construct_function_type_t*) iter;
2301 type_t *function_type = construct_function_type->function_type;
2303 function_type->function.return_type = type;
2305 type = function_type;
2309 case CONSTRUCT_POINTER: {
2310 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2311 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2312 pointer_type->pointer.points_to = type;
2313 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2315 type = pointer_type;
2319 case CONSTRUCT_ARRAY: {
2320 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2321 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2323 array_type->base.qualifiers = parsed_array->type_qualifiers;
2324 array_type->array.element_type = type;
2325 array_type->array.is_static = parsed_array->is_static;
2326 array_type->array.is_variable = parsed_array->is_variable;
2327 array_type->array.size = parsed_array->size;
2334 type_t *hashed_type = typehash_insert(type);
2335 if(hashed_type != type) {
2336 /* the function type was constructed earlier freeing it here will
2337 * destroy other types... */
2338 if(iter->type != CONSTRUCT_FUNCTION) {
2348 static declaration_t *parse_declarator(
2349 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2351 type_t *type = specifiers->type;
2352 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2353 declaration->storage_class = specifiers->storage_class;
2354 declaration->is_inline = specifiers->is_inline;
2356 construct_type_t *construct_type
2357 = parse_inner_declarator(declaration, may_be_abstract);
2358 declaration->type = construct_declarator_type(construct_type, type);
2360 if(construct_type != NULL) {
2361 obstack_free(&temp_obst, construct_type);
2367 static type_t *parse_abstract_declarator(type_t *base_type)
2369 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2371 type_t *result = construct_declarator_type(construct_type, base_type);
2372 if(construct_type != NULL) {
2373 obstack_free(&temp_obst, construct_type);
2379 static declaration_t *record_declaration(declaration_t *declaration)
2381 assert(declaration->parent_context == NULL);
2382 assert(context != NULL);
2384 symbol_t *symbol = declaration->symbol;
2385 if(symbol != NULL) {
2386 declaration_t *alias = environment_push(declaration);
2387 if(alias != declaration)
2390 declaration->parent_context = context;
2393 if(last_declaration != NULL) {
2394 last_declaration->next = declaration;
2396 context->declarations = declaration;
2398 last_declaration = declaration;
2403 static void parser_error_multiple_definition(declaration_t *declaration,
2404 const source_position_t source_position)
2406 parser_print_error_prefix_pos(source_position);
2407 fprintf(stderr, "multiple definition of symbol '%s'\n",
2408 declaration->symbol->string);
2409 parser_print_error_prefix_pos(declaration->source_position);
2410 fprintf(stderr, "this is the location of the previous definition.\n");
2413 static bool is_declaration_specifier(const token_t *token,
2414 bool only_type_specifiers)
2416 switch(token->type) {
2420 return is_typedef_symbol(token->v.symbol);
2422 case T___extension__:
2425 return !only_type_specifiers;
2432 static void parse_init_declarator_rest(declaration_t *declaration)
2436 type_t *orig_type = declaration->type;
2437 type_t *type = NULL;
2438 if(orig_type != NULL)
2439 type = skip_typeref(orig_type);
2441 if(declaration->init.initializer != NULL) {
2442 parser_error_multiple_definition(declaration, token.source_position);
2445 initializer_t *initializer = parse_initializer(type);
2447 /* § 6.7.5 (22) array intializers for arrays with unknown size determine
2448 * the array type size */
2449 if(type != NULL && is_type_array(type) && initializer != NULL) {
2450 array_type_t *array_type = &type->array;
2452 if(array_type->size == NULL) {
2453 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2455 cnst->base.datatype = type_size_t;
2457 switch (initializer->type) {
2458 case INITIALIZER_LIST: {
2459 initializer_list_t *const list = &initializer->list;
2460 cnst->conste.v.int_value = list->len;
2464 case INITIALIZER_STRING: {
2465 initializer_string_t *const string = &initializer->string;
2466 cnst->conste.v.int_value = strlen(string->string) + 1;
2470 case INITIALIZER_WIDE_STRING: {
2471 initializer_wide_string_t *const string = &initializer->wide_string;
2472 cnst->conste.v.int_value = string->string.size;
2477 panic("invalid initializer type");
2480 array_type->size = cnst;
2484 if(type != NULL && is_type_function(type)) {
2485 parser_print_error_prefix_pos(declaration->source_position);
2486 fprintf(stderr, "initializers not allowed for function types at "
2487 "declator '%s' (type ", declaration->symbol->string);
2488 print_type_quoted(orig_type);
2489 fprintf(stderr, ")\n");
2491 declaration->init.initializer = initializer;
2495 /* parse rest of a declaration without any declarator */
2496 static void parse_anonymous_declaration_rest(
2497 const declaration_specifiers_t *specifiers,
2498 parsed_declaration_func finished_declaration)
2502 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2504 declaration->type = specifiers->type;
2505 declaration->storage_class = specifiers->storage_class;
2506 declaration->source_position = specifiers->source_position;
2508 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2509 parse_warning_pos(declaration->source_position,
2510 "useless storage class in empty declaration");
2513 type_t *type = declaration->type;
2514 switch (type->type) {
2515 case TYPE_COMPOUND_STRUCT:
2516 case TYPE_COMPOUND_UNION: {
2517 const compound_type_t *compound_type = &type->compound;
2518 if (compound_type->declaration->symbol == NULL) {
2519 parse_warning_pos(declaration->source_position,
2520 "unnamed struct/union that defines no instances");
2529 parse_warning_pos(declaration->source_position,
2530 "empty declaration");
2534 finished_declaration(declaration);
2537 static void parse_declaration_rest(declaration_t *ndeclaration,
2538 const declaration_specifiers_t *specifiers,
2539 parsed_declaration_func finished_declaration)
2542 declaration_t *declaration = finished_declaration(ndeclaration);
2544 type_t *orig_type = declaration->type;
2545 type_t *type = skip_typeref(orig_type);
2547 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2548 parser_print_warning_prefix_pos(declaration->source_position);
2549 fprintf(stderr, "variable '%s' declared 'inline'\n",
2550 declaration->symbol->string);
2553 if(token.type == '=') {
2554 parse_init_declarator_rest(declaration);
2557 if(token.type != ',')
2561 ndeclaration = parse_declarator(specifiers, false);
2566 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2568 /* TODO: check that it was actually a parameter that gets a type */
2570 /* we should have a declaration for the parameter in the current
2572 return record_declaration(declaration);
2575 static void parse_declaration(parsed_declaration_func finished_declaration)
2577 declaration_specifiers_t specifiers;
2578 memset(&specifiers, 0, sizeof(specifiers));
2579 parse_declaration_specifiers(&specifiers);
2581 if(token.type == ';') {
2582 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2584 declaration_t *declaration = parse_declarator(&specifiers, false);
2585 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2589 static void parse_kr_declaration_list(declaration_t *declaration)
2591 type_t *type = skip_typeref(declaration->type);
2592 assert(is_type_function(type));
2594 if(!type->function.kr_style_parameters)
2597 /* push function parameters */
2598 int top = environment_top();
2599 context_t *last_context = context;
2600 set_context(&declaration->context);
2602 declaration_t *parameter = declaration->context.declarations;
2603 for( ; parameter != NULL; parameter = parameter->next) {
2604 environment_push(parameter);
2607 /* parse declaration list */
2608 while(is_declaration_specifier(&token, false)) {
2609 parse_declaration(finished_kr_declaration);
2612 /* pop function parameters */
2613 assert(context == &declaration->context);
2614 set_context(last_context);
2615 environment_pop_to(top);
2617 /* update function type */
2618 type_t *new_type = duplicate_type(type);
2619 new_type->function.kr_style_parameters = false;
2621 function_parameter_t *parameters = NULL;
2622 function_parameter_t *last_parameter = NULL;
2624 declaration_t *parameter_declaration = declaration->context.declarations;
2625 for( ; parameter_declaration != NULL;
2626 parameter_declaration = parameter_declaration->next) {
2627 type_t *parameter_type = parameter_declaration->type;
2628 if(parameter_type == NULL) {
2630 parser_print_error_prefix();
2631 fprintf(stderr, "no type specified for function parameter '%s'\n",
2632 parameter_declaration->symbol->string);
2634 parser_print_warning_prefix();
2635 fprintf(stderr, "no type specified for function parameter '%s', "
2636 "using int\n", parameter_declaration->symbol->string);
2637 parameter_type = type_int;
2638 parameter_declaration->type = parameter_type;
2642 semantic_parameter(parameter_declaration);
2643 parameter_type = parameter_declaration->type;
2645 function_parameter_t *function_parameter
2646 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2647 memset(function_parameter, 0, sizeof(function_parameter[0]));
2649 function_parameter->type = parameter_type;
2650 if(last_parameter != NULL) {
2651 last_parameter->next = function_parameter;
2653 parameters = function_parameter;
2655 last_parameter = function_parameter;
2657 new_type->function.parameters = parameters;
2659 type = typehash_insert(new_type);
2660 if(type != new_type) {
2661 obstack_free(type_obst, new_type);
2664 declaration->type = type;
2667 static void parse_external_declaration(void)
2669 /* function-definitions and declarations both start with declaration
2671 declaration_specifiers_t specifiers;
2672 memset(&specifiers, 0, sizeof(specifiers));
2673 parse_declaration_specifiers(&specifiers);
2675 /* must be a declaration */
2676 if(token.type == ';') {
2677 parse_anonymous_declaration_rest(&specifiers, record_declaration);
2681 /* declarator is common to both function-definitions and declarations */
2682 declaration_t *ndeclaration = parse_declarator(&specifiers, false);
2684 /* must be a declaration */
2685 if(token.type == ',' || token.type == '=' || token.type == ';') {
2686 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2690 /* must be a function definition */
2691 parse_kr_declaration_list(ndeclaration);
2693 if(token.type != '{') {
2694 parse_error_expected("while parsing function definition", '{', 0);
2699 type_t *type = ndeclaration->type;
2705 /* note that we don't skip typerefs: the standard doesn't allow them here
2706 * (so we can't use is_type_function here) */
2707 if(type->type != TYPE_FUNCTION) {
2708 parser_print_error_prefix();
2709 fprintf(stderr, "declarator '");
2710 print_type_ext(type, ndeclaration->symbol, NULL);
2711 fprintf(stderr, "' has a body but is not a function type.\n");
2716 /* § 6.7.5.3 (14) a function definition with () means no
2717 * parameters (and not unspecified parameters) */
2718 if(type->function.unspecified_parameters) {
2719 type_t *duplicate = duplicate_type(type);
2720 duplicate->function.unspecified_parameters = false;
2722 type = typehash_insert(duplicate);
2723 if(type != duplicate) {
2724 obstack_free(type_obst, duplicate);
2726 ndeclaration->type = type;
2729 declaration_t *declaration = record_declaration(ndeclaration);
2730 if(ndeclaration != declaration) {
2731 memcpy(&declaration->context, &ndeclaration->context,
2732 sizeof(declaration->context));
2734 type = skip_typeref(declaration->type);
2736 /* push function parameters and switch context */
2737 int top = environment_top();
2738 context_t *last_context = context;
2739 set_context(&declaration->context);
2741 declaration_t *parameter = declaration->context.declarations;
2742 for( ; parameter != NULL; parameter = parameter->next) {
2743 environment_push(parameter);
2746 if(declaration->init.statement != NULL) {
2747 parser_error_multiple_definition(declaration, token.source_position);
2749 goto end_of_parse_external_declaration;
2751 /* parse function body */
2752 int label_stack_top = label_top();
2753 declaration_t *old_current_function = current_function;
2754 current_function = declaration;
2756 declaration->init.statement = parse_compound_statement();
2758 assert(current_function == declaration);
2759 current_function = old_current_function;
2760 label_pop_to(label_stack_top);
2763 end_of_parse_external_declaration:
2764 assert(context == &declaration->context);
2765 set_context(last_context);
2766 environment_pop_to(top);
2769 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2772 if(token.type == ':') {
2774 parse_constant_expression();
2775 /* TODO (bitfields) */
2777 declaration_t *declaration = parse_declarator(specifiers, true);
2779 /* TODO: check constraints for struct declarations */
2780 /* TODO: check for doubled fields */
2781 record_declaration(declaration);
2783 if(token.type == ':') {
2785 parse_constant_expression();
2786 /* TODO (bitfields) */
2790 if(token.type != ',')
2797 static void parse_compound_type_entries(void)
2801 while(token.type != '}' && token.type != T_EOF) {
2802 declaration_specifiers_t specifiers;
2803 memset(&specifiers, 0, sizeof(specifiers));
2804 parse_declaration_specifiers(&specifiers);
2806 parse_struct_declarators(&specifiers);
2808 if(token.type == T_EOF) {
2809 parse_error("EOF while parsing struct");
2814 static type_t *parse_typename(void)
2816 declaration_specifiers_t specifiers;
2817 memset(&specifiers, 0, sizeof(specifiers));
2818 parse_declaration_specifiers(&specifiers);
2819 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2820 /* TODO: improve error message, user does probably not know what a
2821 * storage class is...
2823 parse_error("typename may not have a storage class");
2826 type_t *result = parse_abstract_declarator(specifiers.type);
2834 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2835 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2836 expression_t *left);
2838 typedef struct expression_parser_function_t expression_parser_function_t;
2839 struct expression_parser_function_t {
2840 unsigned precedence;
2841 parse_expression_function parser;
2842 unsigned infix_precedence;
2843 parse_expression_infix_function infix_parser;
2846 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2848 static expression_t *create_invalid_expression(void)
2850 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2851 expression->base.source_position = token.source_position;
2855 static expression_t *expected_expression_error(void)
2857 parser_print_error_prefix();
2858 fprintf(stderr, "expected expression, got token ");
2859 print_token(stderr, &token);
2860 fprintf(stderr, "\n");
2864 return create_invalid_expression();
2867 static expression_t *parse_string_const(void)
2869 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2870 cnst->base.datatype = type_string;
2871 cnst->string.value = parse_string_literals();
2876 static expression_t *parse_wide_string_const(void)
2878 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2879 cnst->base.datatype = type_wchar_t_ptr;
2880 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2885 static expression_t *parse_int_const(void)
2887 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2888 cnst->base.datatype = token.datatype;
2889 cnst->conste.v.int_value = token.v.intvalue;
2896 static expression_t *parse_float_const(void)
2898 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2899 cnst->base.datatype = token.datatype;
2900 cnst->conste.v.float_value = token.v.floatvalue;
2907 static declaration_t *create_implicit_function(symbol_t *symbol,
2908 const source_position_t source_position)
2910 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2911 ntype->function.return_type = type_int;
2912 ntype->function.unspecified_parameters = true;
2914 type_t *type = typehash_insert(ntype);
2919 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2921 declaration->storage_class = STORAGE_CLASS_EXTERN;
2922 declaration->type = type;
2923 declaration->symbol = symbol;
2924 declaration->source_position = source_position;
2926 /* prepend the implicit definition to the global context
2927 * this is safe since the symbol wasn't declared as anything else yet
2929 assert(symbol->declaration == NULL);
2931 context_t *last_context = context;
2932 context = global_context;
2934 environment_push(declaration);
2935 declaration->next = context->declarations;
2936 context->declarations = declaration;
2938 context = last_context;
2943 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
2945 function_parameter_t *parameter
2946 = obstack_alloc(type_obst, sizeof(parameter[0]));
2947 memset(parameter, 0, sizeof(parameter[0]));
2948 parameter->type = argument_type;
2950 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2951 type->function.return_type = return_type;
2952 type->function.parameters = parameter;
2954 type_t *result = typehash_insert(type);
2955 if(result != type) {
2962 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2964 switch(symbol->ID) {
2965 case T___builtin_alloca:
2966 return make_function_1_type(type_void_ptr, type_size_t);
2967 case T___builtin_nan:
2968 return make_function_1_type(type_double, type_string);
2969 case T___builtin_nanf:
2970 return make_function_1_type(type_float, type_string);
2971 case T___builtin_nand:
2972 return make_function_1_type(type_long_double, type_string);
2973 case T___builtin_va_end:
2974 return make_function_1_type(type_void, type_valist);
2976 panic("not implemented builtin symbol found");
2981 * performs automatic type cast as described in § 6.3.2.1
2983 static type_t *automatic_type_conversion(type_t *orig_type)
2985 if(orig_type == NULL)
2988 type_t *type = skip_typeref(orig_type);
2989 if(is_type_array(type)) {
2990 array_type_t *array_type = &type->array;
2991 type_t *element_type = array_type->element_type;
2992 unsigned qualifiers = array_type->type.qualifiers;
2994 return make_pointer_type(element_type, qualifiers);
2997 if(is_type_function(type)) {
2998 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3005 * reverts the automatic casts of array to pointer types and function
3006 * to function-pointer types as defined § 6.3.2.1
3008 type_t *revert_automatic_type_conversion(const expression_t *expression)
3010 if(expression->base.datatype == NULL)
3013 switch(expression->type) {
3014 case EXPR_REFERENCE: {
3015 const reference_expression_t *ref = &expression->reference;
3016 return ref->declaration->type;
3019 const select_expression_t *select = &expression->select;
3020 return select->compound_entry->type;
3023 const unary_expression_t *unary = &expression->unary;
3024 if(unary->type == UNEXPR_DEREFERENCE) {
3025 expression_t *value = unary->value;
3026 type_t *type = skip_typeref(value->base.datatype);
3027 pointer_type_t *pointer_type = &type->pointer;
3029 return pointer_type->points_to;
3033 case EXPR_BUILTIN_SYMBOL: {
3034 const builtin_symbol_expression_t *builtin
3035 = &expression->builtin_symbol;
3036 return get_builtin_symbol_type(builtin->symbol);
3038 case EXPR_ARRAY_ACCESS: {
3039 const array_access_expression_t *array_access
3040 = &expression->array_access;
3041 const expression_t *array_ref = array_access->array_ref;
3042 type_t *type_left = skip_typeref(array_ref->base.datatype);
3043 assert(is_type_pointer(type_left));
3044 pointer_type_t *pointer_type = &type_left->pointer;
3045 return pointer_type->points_to;
3052 return expression->base.datatype;
3055 static expression_t *parse_reference(void)
3057 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3059 reference_expression_t *ref = &expression->reference;
3060 ref->symbol = token.v.symbol;
3062 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3064 source_position_t source_position = token.source_position;
3067 if(declaration == NULL) {
3069 /* an implicitly defined function */
3070 if(token.type == '(') {
3071 parser_print_prefix_pos(token.source_position);
3072 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
3073 ref->symbol->string);
3075 declaration = create_implicit_function(ref->symbol,
3080 parser_print_error_prefix();
3081 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
3086 type_t *type = declaration->type;
3087 /* we always do the auto-type conversions; the & and sizeof parser contains
3088 * code to revert this! */
3089 type = automatic_type_conversion(type);
3091 ref->declaration = declaration;
3092 ref->expression.datatype = type;
3097 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3101 /* TODO check if explicit cast is allowed and issue warnings/errors */
3104 static expression_t *parse_cast(void)
3106 expression_t *cast = allocate_expression_zero(EXPR_UNARY);
3108 cast->unary.type = UNEXPR_CAST;
3109 cast->base.source_position = token.source_position;
3111 type_t *type = parse_typename();
3114 expression_t *value = parse_sub_expression(20);
3116 check_cast_allowed(value, type);
3118 cast->base.datatype = type;
3119 cast->unary.value = value;
3124 static expression_t *parse_statement_expression(void)
3126 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3128 statement_t *statement = parse_compound_statement();
3129 expression->statement.statement = statement;
3130 if(statement == NULL) {
3135 assert(statement->type == STATEMENT_COMPOUND);
3136 compound_statement_t *compound_statement = &statement->compound;
3138 /* find last statement and use it's type */
3139 const statement_t *last_statement = NULL;
3140 const statement_t *iter = compound_statement->statements;
3141 for( ; iter != NULL; iter = iter->base.next) {
3142 last_statement = iter;
3145 if(last_statement->type == STATEMENT_EXPRESSION) {
3146 const expression_statement_t *expression_statement
3147 = &last_statement->expression;
3148 expression->base.datatype
3149 = expression_statement->expression->base.datatype;
3151 expression->base.datatype = type_void;
3159 static expression_t *parse_brace_expression(void)
3163 switch(token.type) {
3165 /* gcc extension: a stement expression */
3166 return parse_statement_expression();
3170 return parse_cast();
3172 if(is_typedef_symbol(token.v.symbol)) {
3173 return parse_cast();
3177 expression_t *result = parse_expression();
3183 static expression_t *parse_function_keyword(void)
3188 if (current_function == NULL) {
3189 parse_error("'__func__' used outside of a function");
3192 string_literal_expression_t *expression
3193 = allocate_ast_zero(sizeof(expression[0]));
3195 expression->expression.type = EXPR_FUNCTION;
3196 expression->expression.datatype = type_string;
3197 expression->value = "TODO: FUNCTION";
3199 return (expression_t*) expression;
3202 static expression_t *parse_pretty_function_keyword(void)
3204 eat(T___PRETTY_FUNCTION__);
3207 string_literal_expression_t *expression
3208 = allocate_ast_zero(sizeof(expression[0]));
3210 expression->expression.type = EXPR_PRETTY_FUNCTION;
3211 expression->expression.datatype = type_string;
3212 expression->value = "TODO: PRETTY FUNCTION";
3214 return (expression_t*) expression;
3217 static designator_t *parse_designator(void)
3219 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3221 if(token.type != T_IDENTIFIER) {
3222 parse_error_expected("while parsing member designator",
3227 result->symbol = token.v.symbol;
3230 designator_t *last_designator = result;
3232 if(token.type == '.') {
3234 if(token.type != T_IDENTIFIER) {
3235 parse_error_expected("while parsing member designator",
3240 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3241 designator->symbol = token.v.symbol;
3244 last_designator->next = designator;
3245 last_designator = designator;
3248 if(token.type == '[') {
3250 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3251 designator->array_access = parse_expression();
3252 if(designator->array_access == NULL) {
3258 last_designator->next = designator;
3259 last_designator = designator;
3268 static expression_t *parse_offsetof(void)
3270 eat(T___builtin_offsetof);
3272 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3273 expression->base.datatype = type_size_t;
3276 expression->offsetofe.type = parse_typename();
3278 expression->offsetofe.designator = parse_designator();
3284 static expression_t *parse_va_start(void)
3286 eat(T___builtin_va_start);
3288 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3291 expression->va_starte.ap = parse_assignment_expression();
3293 if (token.type != T_IDENTIFIER) {
3294 parse_error_expected("while parsing va_start", T_IDENTIFIER, 0);
3296 return create_invalid_expression();
3298 expression_t *const expr = parse_reference();
3299 if (expr->type == EXPR_INVALID) {
3300 return create_invalid_expression();
3302 assert(expr->type == EXPR_REFERENCE);
3303 declaration_t *const decl = expr->reference.declaration;
3304 if (decl->parent_context != ¤t_function->context ||
3305 decl->next != NULL) {
3306 parser_print_error_prefix_pos(decl->source_position);
3307 fprintf(stderr, "second argument of 'va_start' must be last parameter "
3308 "of the current function\n");
3310 expression->va_starte.parameter = decl;
3316 static expression_t *parse_va_arg(void)
3318 eat(T___builtin_va_arg);
3320 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3323 expression->va_arge.ap = parse_assignment_expression();
3325 expression->base.datatype = parse_typename();
3331 static expression_t *parse_builtin_symbol(void)
3333 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3335 symbol_t *symbol = token.v.symbol;
3337 expression->builtin_symbol.symbol = symbol;
3340 type_t *type = get_builtin_symbol_type(symbol);
3341 type = automatic_type_conversion(type);
3343 expression->base.datatype = type;
3347 static expression_t *parse_primary_expression(void)
3349 switch(token.type) {
3351 return parse_int_const();
3352 case T_FLOATINGPOINT:
3353 return parse_float_const();
3354 case T_STRING_LITERAL: /* TODO merge */
3355 return parse_string_const();
3356 case T_WIDE_STRING_LITERAL:
3357 return parse_wide_string_const();
3359 return parse_reference();
3360 case T___FUNCTION__:
3362 return parse_function_keyword();
3363 case T___PRETTY_FUNCTION__:
3364 return parse_pretty_function_keyword();
3365 case T___builtin_offsetof:
3366 return parse_offsetof();
3367 case T___builtin_va_start:
3368 return parse_va_start();
3369 case T___builtin_va_arg:
3370 return parse_va_arg();
3371 case T___builtin_nanf:
3372 case T___builtin_alloca:
3373 case T___builtin_expect:
3374 case T___builtin_va_end:
3375 return parse_builtin_symbol();
3378 return parse_brace_expression();
3381 parser_print_error_prefix();
3382 fprintf(stderr, "unexpected token ");
3383 print_token(stderr, &token);
3384 fprintf(stderr, "\n");
3387 return create_invalid_expression();
3390 static expression_t *parse_array_expression(unsigned precedence,
3397 expression_t *inside = parse_expression();
3399 array_access_expression_t *array_access
3400 = allocate_ast_zero(sizeof(array_access[0]));
3402 array_access->expression.type = EXPR_ARRAY_ACCESS;
3404 type_t *type_left = left->base.datatype;
3405 type_t *type_inside = inside->base.datatype;
3406 type_t *return_type = NULL;
3408 if(type_left != NULL && type_inside != NULL) {
3409 type_left = skip_typeref(type_left);
3410 type_inside = skip_typeref(type_inside);
3412 if(is_type_pointer(type_left)) {
3413 pointer_type_t *pointer = &type_left->pointer;
3414 return_type = pointer->points_to;
3415 array_access->array_ref = left;
3416 array_access->index = inside;
3417 } else if(is_type_pointer(type_inside)) {
3418 pointer_type_t *pointer = &type_inside->pointer;
3419 return_type = pointer->points_to;
3420 array_access->array_ref = inside;
3421 array_access->index = left;
3422 array_access->flipped = true;
3424 parser_print_error_prefix();
3425 fprintf(stderr, "array access on object with non-pointer types ");
3426 print_type_quoted(type_left);
3427 fprintf(stderr, ", ");
3428 print_type_quoted(type_inside);
3429 fprintf(stderr, "\n");
3432 array_access->array_ref = left;
3433 array_access->index = inside;
3436 if(token.type != ']') {
3437 parse_error_expected("Problem while parsing array access", ']', 0);
3438 return (expression_t*) array_access;
3442 return_type = automatic_type_conversion(return_type);
3443 array_access->expression.datatype = return_type;
3445 return (expression_t*) array_access;
3448 static expression_t *parse_sizeof(unsigned precedence)
3452 sizeof_expression_t *sizeof_expression
3453 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3454 sizeof_expression->expression.type = EXPR_SIZEOF;
3455 sizeof_expression->expression.datatype = type_size_t;
3457 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3459 sizeof_expression->type = parse_typename();
3462 expression_t *expression = parse_sub_expression(precedence);
3463 expression->base.datatype = revert_automatic_type_conversion(expression);
3465 sizeof_expression->type = expression->base.datatype;
3466 sizeof_expression->size_expression = expression;
3469 return (expression_t*) sizeof_expression;
3472 static expression_t *parse_select_expression(unsigned precedence,
3473 expression_t *compound)
3476 assert(token.type == '.' || token.type == T_MINUSGREATER);
3478 bool is_pointer = (token.type == T_MINUSGREATER);
3481 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3482 select->select.compound = compound;
3484 if(token.type != T_IDENTIFIER) {
3485 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3488 symbol_t *symbol = token.v.symbol;
3489 select->select.symbol = symbol;
3492 type_t *orig_type = compound->base.datatype;
3493 if(orig_type == NULL)
3494 return create_invalid_expression();
3496 type_t *type = skip_typeref(orig_type);
3498 type_t *type_left = type;
3500 if(type->type != TYPE_POINTER) {
3501 parser_print_error_prefix();
3502 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
3503 print_type_quoted(orig_type);
3504 fputc('\n', stderr);
3505 return create_invalid_expression();
3507 pointer_type_t *pointer_type = &type->pointer;
3508 type_left = pointer_type->points_to;
3510 type_left = skip_typeref(type_left);
3512 if(type_left->type != TYPE_COMPOUND_STRUCT
3513 && type_left->type != TYPE_COMPOUND_UNION) {
3514 parser_print_error_prefix();
3515 fprintf(stderr, "request for member '%s' in something not a struct or "
3516 "union, but ", symbol->string);
3517 print_type_quoted(type_left);
3518 fputc('\n', stderr);
3519 return create_invalid_expression();
3522 compound_type_t *compound_type = &type_left->compound;
3523 declaration_t *declaration = compound_type->declaration;
3525 if(!declaration->init.is_defined) {
3526 parser_print_error_prefix();
3527 fprintf(stderr, "request for member '%s' of incomplete type ",
3529 print_type_quoted(type_left);
3530 fputc('\n', stderr);
3531 return create_invalid_expression();
3534 declaration_t *iter = declaration->context.declarations;
3535 for( ; iter != NULL; iter = iter->next) {
3536 if(iter->symbol == symbol) {
3541 parser_print_error_prefix();
3542 print_type_quoted(type_left);
3543 fprintf(stderr, " has no member named '%s'\n", symbol->string);
3544 return create_invalid_expression();
3547 /* we always do the auto-type conversions; the & and sizeof parser contains
3548 * code to revert this! */
3549 type_t *expression_type = automatic_type_conversion(iter->type);
3551 select->select.compound_entry = iter;
3552 select->base.datatype = expression_type;
3556 static expression_t *parse_call_expression(unsigned precedence,
3557 expression_t *expression)
3560 expression_t *result = allocate_expression_zero(EXPR_CALL);
3562 call_expression_t *call = &result->call;
3563 call->function = expression;
3565 function_type_t *function_type = NULL;
3566 type_t *orig_type = expression->base.datatype;
3567 if(orig_type != NULL) {
3568 type_t *type = skip_typeref(orig_type);
3570 if(is_type_pointer(type)) {
3571 pointer_type_t *pointer_type = &type->pointer;
3573 type = skip_typeref(pointer_type->points_to);
3575 if (is_type_function(type)) {
3576 function_type = &type->function;
3577 call->expression.datatype = function_type->return_type;
3580 if(function_type == NULL) {
3581 parser_print_error_prefix();
3582 fputs("called object '", stderr);
3583 print_expression(expression);
3584 fputs("' (type ", stderr);
3585 print_type_quoted(orig_type);
3586 fputs(") is not a pointer to a function\n", stderr);
3588 function_type = NULL;
3589 call->expression.datatype = NULL;
3593 /* parse arguments */
3596 if(token.type != ')') {
3597 call_argument_t *last_argument = NULL;
3600 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3602 argument->expression = parse_assignment_expression();
3603 if(last_argument == NULL) {
3604 call->arguments = argument;
3606 last_argument->next = argument;
3608 last_argument = argument;
3610 if(token.type != ',')
3617 if(function_type != NULL) {
3618 function_parameter_t *parameter = function_type->parameters;
3619 call_argument_t *argument = call->arguments;
3620 for( ; parameter != NULL && argument != NULL;
3621 parameter = parameter->next, argument = argument->next) {
3622 type_t *expected_type = parameter->type;
3623 /* TODO report context in error messages */
3624 argument->expression = create_implicit_cast(argument->expression,
3627 /* too few parameters */
3628 if(parameter != NULL) {
3629 parser_print_error_prefix();
3630 fprintf(stderr, "too few arguments to function '");
3631 print_expression(expression);
3632 fprintf(stderr, "'\n");
3633 } else if(argument != NULL) {
3634 /* too many parameters */
3635 if(!function_type->variadic
3636 && !function_type->unspecified_parameters) {
3637 parser_print_error_prefix();
3638 fprintf(stderr, "too many arguments to function '");
3639 print_expression(expression);
3640 fprintf(stderr, "'\n");
3642 /* do default promotion */
3643 for( ; argument != NULL; argument = argument->next) {
3644 type_t *type = argument->expression->base.datatype;
3649 type = skip_typeref(type);
3650 if(is_type_integer(type)) {
3651 type = promote_integer(type);
3652 } else if(type == type_float) {
3656 argument->expression
3657 = create_implicit_cast(argument->expression, type);
3666 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3668 static bool same_compound_type(const type_t *type1, const type_t *type2)
3670 if(!is_type_compound(type1))
3672 if(type1->type != type2->type)
3675 const compound_type_t *compound1 = &type1->compound;
3676 const compound_type_t *compound2 = &type2->compound;
3678 return compound1->declaration == compound2->declaration;
3681 static expression_t *parse_conditional_expression(unsigned precedence,
3682 expression_t *expression)
3686 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3688 conditional_expression_t *conditional = &result->conditional;
3689 conditional->condition = expression;
3692 type_t *condition_type_orig = expression->base.datatype;
3693 if(condition_type_orig != NULL) {
3694 type_t *condition_type = skip_typeref(condition_type_orig);
3695 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3696 type_error("expected a scalar type in conditional condition",
3697 expression->base.source_position, condition_type_orig);
3701 expression_t *true_expression = parse_expression();
3703 expression_t *false_expression = parse_sub_expression(precedence);
3705 conditional->true_expression = true_expression;
3706 conditional->false_expression = false_expression;
3708 type_t *orig_true_type = true_expression->base.datatype;
3709 type_t *orig_false_type = false_expression->base.datatype;
3710 if(orig_true_type == NULL || orig_false_type == NULL)
3713 type_t *true_type = skip_typeref(orig_true_type);
3714 type_t *false_type = skip_typeref(orig_false_type);
3717 type_t *result_type = NULL;
3718 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3719 result_type = semantic_arithmetic(true_type, false_type);
3721 true_expression = create_implicit_cast(true_expression, result_type);
3722 false_expression = create_implicit_cast(false_expression, result_type);
3724 conditional->true_expression = true_expression;
3725 conditional->false_expression = false_expression;
3726 conditional->expression.datatype = result_type;
3727 } else if (same_compound_type(true_type, false_type)
3728 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3729 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
3730 /* just take 1 of the 2 types */
3731 result_type = true_type;
3732 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3733 && pointers_compatible(true_type, false_type)) {
3735 result_type = true_type;
3738 type_error_incompatible("while parsing conditional",
3739 expression->base.source_position, true_type,
3743 conditional->expression.datatype = result_type;
3747 static expression_t *parse_extension(unsigned precedence)
3749 eat(T___extension__);
3751 /* TODO enable extensions */
3753 return parse_sub_expression(precedence);
3756 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3758 eat(T___builtin_classify_type);
3760 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3761 result->base.datatype = type_int;
3764 expression_t *expression = parse_sub_expression(precedence);
3766 result->classify_type.type_expression = expression;
3771 static void semantic_incdec(unary_expression_t *expression)
3773 type_t *orig_type = expression->value->base.datatype;
3774 if(orig_type == NULL)
3777 type_t *type = skip_typeref(orig_type);
3778 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3779 /* TODO: improve error message */
3780 parser_print_error_prefix();
3781 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3785 expression->expression.datatype = orig_type;
3788 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3790 type_t *orig_type = expression->value->base.datatype;
3791 if(orig_type == NULL)
3794 type_t *type = skip_typeref(orig_type);
3795 if(!is_type_arithmetic(type)) {
3796 /* TODO: improve error message */
3797 parser_print_error_prefix();
3798 fprintf(stderr, "operation needs an arithmetic type\n");
3802 expression->expression.datatype = orig_type;
3805 static void semantic_unexpr_scalar(unary_expression_t *expression)
3807 type_t *orig_type = expression->value->base.datatype;
3808 if(orig_type == NULL)
3811 type_t *type = skip_typeref(orig_type);
3812 if (!is_type_scalar(type)) {
3813 parse_error("operand of ! must be of scalar type\n");
3817 expression->expression.datatype = orig_type;
3820 static void semantic_unexpr_integer(unary_expression_t *expression)
3822 type_t *orig_type = expression->value->base.datatype;
3823 if(orig_type == NULL)
3826 type_t *type = skip_typeref(orig_type);
3827 if (!is_type_integer(type)) {
3828 parse_error("operand of ~ must be of integer type\n");
3832 expression->expression.datatype = orig_type;
3835 static void semantic_dereference(unary_expression_t *expression)
3837 type_t *orig_type = expression->value->base.datatype;
3838 if(orig_type == NULL)
3841 type_t *type = skip_typeref(orig_type);
3842 if(!is_type_pointer(type)) {
3843 parser_print_error_prefix();
3844 fputs("Unary '*' needs pointer or arrray type, but type ", stderr);
3845 print_type_quoted(orig_type);
3846 fputs(" given.\n", stderr);
3850 pointer_type_t *pointer_type = &type->pointer;
3851 type_t *result_type = pointer_type->points_to;
3853 result_type = automatic_type_conversion(result_type);
3854 expression->expression.datatype = result_type;
3857 static void semantic_take_addr(unary_expression_t *expression)
3859 expression_t *value = expression->value;
3860 value->base.datatype = revert_automatic_type_conversion(value);
3862 type_t *orig_type = value->base.datatype;
3863 if(orig_type == NULL)
3866 if(value->type == EXPR_REFERENCE) {
3867 reference_expression_t *reference = (reference_expression_t*) value;
3868 declaration_t *declaration = reference->declaration;
3869 if(declaration != NULL) {
3870 declaration->address_taken = 1;
3874 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3877 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3878 static expression_t *parse_##unexpression_type(unsigned precedence) \
3882 unary_expression_t *unary_expression \
3883 = allocate_ast_zero(sizeof(unary_expression[0])); \
3884 unary_expression->expression.type = EXPR_UNARY; \
3885 unary_expression->type = unexpression_type; \
3886 unary_expression->value = parse_sub_expression(precedence); \
3888 sfunc(unary_expression); \
3890 return (expression_t*) unary_expression; \
3893 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3894 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3895 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3896 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3897 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3898 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3899 semantic_unexpr_integer)
3900 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3902 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3905 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3907 static expression_t *parse_##unexpression_type(unsigned precedence, \
3908 expression_t *left) \
3910 (void) precedence; \
3913 unary_expression_t *unary_expression \
3914 = allocate_ast_zero(sizeof(unary_expression[0])); \
3915 unary_expression->expression.type = EXPR_UNARY; \
3916 unary_expression->type = unexpression_type; \
3917 unary_expression->value = left; \
3919 sfunc(unary_expression); \
3921 return (expression_t*) unary_expression; \
3924 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3926 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3929 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3931 /* TODO: handle complex + imaginary types */
3933 /* § 6.3.1.8 Usual arithmetic conversions */
3934 if(type_left == type_long_double || type_right == type_long_double) {
3935 return type_long_double;
3936 } else if(type_left == type_double || type_right == type_double) {
3938 } else if(type_left == type_float || type_right == type_float) {
3942 type_right = promote_integer(type_right);
3943 type_left = promote_integer(type_left);
3945 if(type_left == type_right)
3948 bool signed_left = is_type_signed(type_left);
3949 bool signed_right = is_type_signed(type_right);
3950 int rank_left = get_rank(type_left);
3951 int rank_right = get_rank(type_right);
3952 if(rank_left < rank_right) {
3953 if(signed_left == signed_right || !signed_right) {
3959 if(signed_left == signed_right || !signed_left) {
3967 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3969 expression_t *left = expression->left;
3970 expression_t *right = expression->right;
3971 type_t *orig_type_left = left->base.datatype;
3972 type_t *orig_type_right = right->base.datatype;
3974 if(orig_type_left == NULL || orig_type_right == NULL)
3977 type_t *type_left = skip_typeref(orig_type_left);
3978 type_t *type_right = skip_typeref(orig_type_right);
3980 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3981 /* TODO: improve error message */
3982 parser_print_error_prefix();
3983 fprintf(stderr, "operation needs arithmetic types\n");
3987 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3988 expression->left = create_implicit_cast(left, arithmetic_type);
3989 expression->right = create_implicit_cast(right, arithmetic_type);
3990 expression->expression.datatype = arithmetic_type;
3993 static void semantic_shift_op(binary_expression_t *expression)
3995 expression_t *left = expression->left;
3996 expression_t *right = expression->right;
3997 type_t *orig_type_left = left->base.datatype;
3998 type_t *orig_type_right = right->base.datatype;
4000 if(orig_type_left == NULL || orig_type_right == NULL)
4003 type_t *type_left = skip_typeref(orig_type_left);
4004 type_t *type_right = skip_typeref(orig_type_right);
4006 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4007 /* TODO: improve error message */
4008 parser_print_error_prefix();
4009 fprintf(stderr, "operation needs integer types\n");
4013 type_left = promote_integer(type_left);
4014 type_right = promote_integer(type_right);
4016 expression->left = create_implicit_cast(left, type_left);
4017 expression->right = create_implicit_cast(right, type_right);
4018 expression->expression.datatype = type_left;
4021 static void semantic_add(binary_expression_t *expression)
4023 expression_t *left = expression->left;
4024 expression_t *right = expression->right;
4025 type_t *orig_type_left = left->base.datatype;
4026 type_t *orig_type_right = right->base.datatype;
4028 if(orig_type_left == NULL || orig_type_right == NULL)
4031 type_t *type_left = skip_typeref(orig_type_left);
4032 type_t *type_right = skip_typeref(orig_type_right);
4035 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4036 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4037 expression->left = create_implicit_cast(left, arithmetic_type);
4038 expression->right = create_implicit_cast(right, arithmetic_type);
4039 expression->expression.datatype = arithmetic_type;
4041 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4042 expression->expression.datatype = type_left;
4043 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4044 expression->expression.datatype = type_right;
4046 parser_print_error_prefix();
4047 fprintf(stderr, "invalid operands to binary + (");
4048 print_type_quoted(orig_type_left);
4049 fprintf(stderr, ", ");
4050 print_type_quoted(orig_type_right);
4051 fprintf(stderr, ")\n");
4055 static void semantic_sub(binary_expression_t *expression)
4057 expression_t *left = expression->left;
4058 expression_t *right = expression->right;
4059 type_t *orig_type_left = left->base.datatype;
4060 type_t *orig_type_right = right->base.datatype;
4062 if(orig_type_left == NULL || orig_type_right == NULL)
4065 type_t *type_left = skip_typeref(orig_type_left);
4066 type_t *type_right = skip_typeref(orig_type_right);
4069 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4070 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4071 expression->left = create_implicit_cast(left, arithmetic_type);
4072 expression->right = create_implicit_cast(right, arithmetic_type);
4073 expression->expression.datatype = arithmetic_type;
4075 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4076 expression->expression.datatype = type_left;
4077 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4078 if(!pointers_compatible(type_left, type_right)) {
4079 parser_print_error_prefix();
4080 fprintf(stderr, "pointers to incompatible objects to binary - (");
4081 print_type_quoted(orig_type_left);
4082 fprintf(stderr, ", ");
4083 print_type_quoted(orig_type_right);
4084 fprintf(stderr, ")\n");
4086 expression->expression.datatype = type_ptrdiff_t;
4089 parser_print_error_prefix();
4090 fprintf(stderr, "invalid operands to binary - (");
4091 print_type_quoted(orig_type_left);
4092 fprintf(stderr, ", ");
4093 print_type_quoted(orig_type_right);
4094 fprintf(stderr, ")\n");
4098 static void semantic_comparison(binary_expression_t *expression)
4100 expression_t *left = expression->left;
4101 expression_t *right = expression->right;
4102 type_t *orig_type_left = left->base.datatype;
4103 type_t *orig_type_right = right->base.datatype;
4105 if(orig_type_left == NULL || orig_type_right == NULL)
4108 type_t *type_left = skip_typeref(orig_type_left);
4109 type_t *type_right = skip_typeref(orig_type_right);
4111 /* TODO non-arithmetic types */
4112 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4113 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4114 expression->left = create_implicit_cast(left, arithmetic_type);
4115 expression->right = create_implicit_cast(right, arithmetic_type);
4116 expression->expression.datatype = arithmetic_type;
4117 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4118 /* TODO check compatibility */
4119 } else if (is_type_pointer(type_left)) {
4120 expression->right = create_implicit_cast(right, type_left);
4121 } else if (is_type_pointer(type_right)) {
4122 expression->left = create_implicit_cast(left, type_right);
4124 type_error_incompatible("invalid operands in comparison",
4125 token.source_position, type_left, type_right);
4127 expression->expression.datatype = type_int;
4130 static void semantic_arithmetic_assign(binary_expression_t *expression)
4132 expression_t *left = expression->left;
4133 expression_t *right = expression->right;
4134 type_t *orig_type_left = left->base.datatype;
4135 type_t *orig_type_right = right->base.datatype;
4137 if(orig_type_left == NULL || orig_type_right == NULL)
4140 type_t *type_left = skip_typeref(orig_type_left);
4141 type_t *type_right = skip_typeref(orig_type_right);
4143 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4144 /* TODO: improve error message */
4145 parser_print_error_prefix();
4146 fprintf(stderr, "operation needs arithmetic types\n");
4150 /* combined instructions are tricky. We can't create an implicit cast on
4151 * the left side, because we need the uncasted form for the store.
4152 * The ast2firm pass has to know that left_type must be right_type
4153 * for the arithmeitc operation and create a cast by itself */
4154 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4155 expression->right = create_implicit_cast(right, arithmetic_type);
4156 expression->expression.datatype = type_left;
4159 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4161 expression_t *left = expression->left;
4162 expression_t *right = expression->right;
4163 type_t *orig_type_left = left->base.datatype;
4164 type_t *orig_type_right = right->base.datatype;
4166 if(orig_type_left == NULL || orig_type_right == NULL)
4169 type_t *type_left = skip_typeref(orig_type_left);
4170 type_t *type_right = skip_typeref(orig_type_right);
4172 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4173 /* combined instructions are tricky. We can't create an implicit cast on
4174 * the left side, because we need the uncasted form for the store.
4175 * The ast2firm pass has to know that left_type must be right_type
4176 * for the arithmeitc operation and create a cast by itself */
4177 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4178 expression->right = create_implicit_cast(right, arithmetic_type);
4179 expression->expression.datatype = type_left;
4180 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4181 expression->expression.datatype = type_left;
4183 parser_print_error_prefix();
4184 fputs("Incompatible types ", stderr);
4185 print_type_quoted(orig_type_left);
4186 fputs(" and ", stderr);
4187 print_type_quoted(orig_type_right);
4188 fputs(" in assignment\n", stderr);
4193 static void semantic_logical_op(binary_expression_t *expression)
4195 expression_t *left = expression->left;
4196 expression_t *right = expression->right;
4197 type_t *orig_type_left = left->base.datatype;
4198 type_t *orig_type_right = right->base.datatype;
4200 if(orig_type_left == NULL || orig_type_right == NULL)
4203 type_t *type_left = skip_typeref(orig_type_left);
4204 type_t *type_right = skip_typeref(orig_type_right);
4206 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4207 /* TODO: improve error message */
4208 parser_print_error_prefix();
4209 fprintf(stderr, "operation needs scalar types\n");
4213 expression->expression.datatype = type_int;
4216 static bool has_const_fields(type_t *type)
4223 static void semantic_binexpr_assign(binary_expression_t *expression)
4225 expression_t *left = expression->left;
4226 type_t *orig_type_left = left->base.datatype;
4228 if(orig_type_left == NULL)
4231 type_t *type_left = revert_automatic_type_conversion(left);
4232 type_left = skip_typeref(orig_type_left);
4234 /* must be a modifiable lvalue */
4235 if (is_type_array(type_left)) {
4236 parser_print_error_prefix();
4237 fprintf(stderr, "Cannot assign to arrays ('");
4238 print_expression(left);
4239 fprintf(stderr, "')\n");
4242 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4243 parser_print_error_prefix();
4244 fprintf(stderr, "assignment to readonly location '");
4245 print_expression(left);
4246 fprintf(stderr, "' (type ");
4247 print_type_quoted(orig_type_left);
4248 fprintf(stderr, ")\n");
4251 if(is_type_incomplete(type_left)) {
4252 parser_print_error_prefix();
4253 fprintf(stderr, "left-hand side of assignment '");
4254 print_expression(left);
4255 fprintf(stderr, "' has incomplete type ");
4256 print_type_quoted(orig_type_left);
4257 fprintf(stderr, "\n");
4260 if(is_type_compound(type_left) && has_const_fields(type_left)) {
4261 parser_print_error_prefix();
4262 fprintf(stderr, "can't assign to '");
4263 print_expression(left);
4264 fprintf(stderr, "' because compound type ");
4265 print_type_quoted(orig_type_left);
4266 fprintf(stderr, " has readonly fields\n");
4270 semantic_assign(orig_type_left, &expression->right, "assignment");
4272 expression->expression.datatype = orig_type_left;
4275 static void semantic_comma(binary_expression_t *expression)
4277 expression->expression.datatype = expression->right->base.datatype;
4280 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4281 static expression_t *parse_##binexpression_type(unsigned precedence, \
4282 expression_t *left) \
4286 expression_t *right = parse_sub_expression(precedence + lr); \
4288 binary_expression_t *binexpr \
4289 = allocate_ast_zero(sizeof(binexpr[0])); \
4290 binexpr->expression.type = EXPR_BINARY; \
4291 binexpr->type = binexpression_type; \
4292 binexpr->left = left; \
4293 binexpr->right = right; \
4296 return (expression_t*) binexpr; \
4299 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
4300 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
4301 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
4302 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
4303 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
4304 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
4305 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
4306 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
4307 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
4308 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
4309 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
4310 semantic_comparison, 1)
4311 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
4312 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
4313 semantic_comparison, 1)
4314 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
4315 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
4316 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
4317 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
4318 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
4319 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
4320 semantic_shift_op, 1)
4321 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
4322 semantic_shift_op, 1)
4323 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
4324 semantic_arithmetic_addsubb_assign, 0)
4325 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
4326 semantic_arithmetic_addsubb_assign, 0)
4327 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
4328 semantic_arithmetic_assign, 0)
4329 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
4330 semantic_arithmetic_assign, 0)
4331 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
4332 semantic_arithmetic_assign, 0)
4333 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
4334 semantic_arithmetic_assign, 0)
4335 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
4336 semantic_arithmetic_assign, 0)
4337 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
4338 semantic_arithmetic_assign, 0)
4339 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
4340 semantic_arithmetic_assign, 0)
4341 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
4342 semantic_arithmetic_assign, 0)
4344 static expression_t *parse_sub_expression(unsigned precedence)
4346 if(token.type < 0) {
4347 return expected_expression_error();
4350 expression_parser_function_t *parser
4351 = &expression_parsers[token.type];
4352 source_position_t source_position = token.source_position;
4355 if(parser->parser != NULL) {
4356 left = parser->parser(parser->precedence);
4358 left = parse_primary_expression();
4360 assert(left != NULL);
4361 left->base.source_position = source_position;
4364 if(token.type < 0) {
4365 return expected_expression_error();
4368 parser = &expression_parsers[token.type];
4369 if(parser->infix_parser == NULL)
4371 if(parser->infix_precedence < precedence)
4374 left = parser->infix_parser(parser->infix_precedence, left);
4376 assert(left != NULL);
4377 assert(left->type != EXPR_UNKNOWN);
4378 left->base.source_position = source_position;
4384 static expression_t *parse_expression(void)
4386 return parse_sub_expression(1);
4391 static void register_expression_parser(parse_expression_function parser,
4392 int token_type, unsigned precedence)
4394 expression_parser_function_t *entry = &expression_parsers[token_type];
4396 if(entry->parser != NULL) {
4397 fprintf(stderr, "for token ");
4398 print_token_type(stderr, (token_type_t) token_type);
4399 fprintf(stderr, "\n");
4400 panic("trying to register multiple expression parsers for a token");
4402 entry->parser = parser;
4403 entry->precedence = precedence;
4406 static void register_expression_infix_parser(
4407 parse_expression_infix_function parser, int token_type,
4408 unsigned precedence)
4410 expression_parser_function_t *entry = &expression_parsers[token_type];
4412 if(entry->infix_parser != NULL) {
4413 fprintf(stderr, "for token ");
4414 print_token_type(stderr, (token_type_t) token_type);
4415 fprintf(stderr, "\n");
4416 panic("trying to register multiple infix expression parsers for a "
4419 entry->infix_parser = parser;
4420 entry->infix_precedence = precedence;
4423 static void init_expression_parsers(void)
4425 memset(&expression_parsers, 0, sizeof(expression_parsers));
4427 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
4428 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
4429 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
4430 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
4431 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
4432 T_GREATERGREATER, 16);
4433 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
4434 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
4435 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
4436 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
4437 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
4438 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
4439 T_GREATEREQUAL, 14);
4440 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
4441 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
4442 T_EXCLAMATIONMARKEQUAL, 13);
4443 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
4444 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
4445 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
4446 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
4447 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
4448 register_expression_infix_parser(parse_conditional_expression, '?', 7);
4449 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
4450 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
4451 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
4452 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
4453 T_ASTERISKEQUAL, 2);
4454 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
4455 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
4457 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
4458 T_LESSLESSEQUAL, 2);
4459 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
4460 T_GREATERGREATEREQUAL, 2);
4461 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
4463 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
4465 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
4468 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
4470 register_expression_infix_parser(parse_array_expression, '[', 30);
4471 register_expression_infix_parser(parse_call_expression, '(', 30);
4472 register_expression_infix_parser(parse_select_expression, '.', 30);
4473 register_expression_infix_parser(parse_select_expression,
4474 T_MINUSGREATER, 30);
4475 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
4477 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
4480 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
4481 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
4482 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
4483 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
4484 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
4485 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
4486 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
4487 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
4488 register_expression_parser(parse_sizeof, T_sizeof, 25);
4489 register_expression_parser(parse_extension, T___extension__, 25);
4490 register_expression_parser(parse_builtin_classify_type,
4491 T___builtin_classify_type, 25);
4494 static asm_constraint_t *parse_asm_constraints(void)
4496 asm_constraint_t *result = NULL;
4497 asm_constraint_t *last = NULL;
4499 while(token.type == T_STRING_LITERAL || token.type == '[') {
4500 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4501 memset(constraint, 0, sizeof(constraint[0]));
4503 if(token.type == '[') {
4505 if(token.type != T_IDENTIFIER) {
4506 parse_error_expected("while parsing asm constraint",
4510 constraint->symbol = token.v.symbol;
4515 constraint->constraints = parse_string_literals();
4517 constraint->expression = parse_expression();
4521 last->next = constraint;
4523 result = constraint;
4527 if(token.type != ',')
4535 static asm_clobber_t *parse_asm_clobbers(void)
4537 asm_clobber_t *result = NULL;
4538 asm_clobber_t *last = NULL;
4540 while(token.type == T_STRING_LITERAL) {
4541 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4542 clobber->clobber = parse_string_literals();
4545 last->next = clobber;
4551 if(token.type != ',')
4559 static statement_t *parse_asm_statement(void)
4563 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4564 statement->base.source_position = token.source_position;
4566 asm_statement_t *asm_statement = &statement->asms;
4568 if(token.type == T_volatile) {
4570 asm_statement->is_volatile = true;
4574 asm_statement->asm_text = parse_string_literals();
4576 if(token.type != ':')
4580 asm_statement->inputs = parse_asm_constraints();
4581 if(token.type != ':')
4585 asm_statement->outputs = parse_asm_constraints();
4586 if(token.type != ':')
4590 asm_statement->clobbers = parse_asm_clobbers();
4598 static statement_t *parse_case_statement(void)
4602 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4604 statement->base.source_position = token.source_position;
4605 statement->case_label.expression = parse_expression();
4608 statement->case_label.label_statement = parse_statement();
4613 static statement_t *parse_default_statement(void)
4617 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4619 statement->base.source_position = token.source_position;
4622 statement->label.label_statement = parse_statement();
4627 static declaration_t *get_label(symbol_t *symbol)
4629 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4630 assert(current_function != NULL);
4631 /* if we found a label in the same function, then we already created the
4633 if(candidate != NULL
4634 && candidate->parent_context == ¤t_function->context) {
4638 /* otherwise we need to create a new one */
4639 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4640 declaration->namespc = NAMESPACE_LABEL;
4641 declaration->symbol = symbol;
4643 label_push(declaration);
4648 static statement_t *parse_label_statement(void)
4650 assert(token.type == T_IDENTIFIER);
4651 symbol_t *symbol = token.v.symbol;
4654 declaration_t *label = get_label(symbol);
4656 /* if source position is already set then the label is defined twice,
4657 * otherwise it was just mentioned in a goto so far */
4658 if(label->source_position.input_name != NULL) {
4659 parser_print_error_prefix();
4660 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
4661 parser_print_error_prefix_pos(label->source_position);
4662 fprintf(stderr, "previous definition of '%s' was here\n",
4665 label->source_position = token.source_position;
4668 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4670 label_statement->statement.type = STATEMENT_LABEL;
4671 label_statement->statement.source_position = token.source_position;
4672 label_statement->label = label;
4676 if(token.type == '}') {
4677 parse_error("label at end of compound statement");
4678 return (statement_t*) label_statement;
4680 label_statement->label_statement = parse_statement();
4683 return (statement_t*) label_statement;
4686 static statement_t *parse_if(void)
4690 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4691 statement->statement.type = STATEMENT_IF;
4692 statement->statement.source_position = token.source_position;
4695 statement->condition = parse_expression();
4698 statement->true_statement = parse_statement();
4699 if(token.type == T_else) {
4701 statement->false_statement = parse_statement();
4704 return (statement_t*) statement;
4707 static statement_t *parse_switch(void)
4711 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4712 statement->statement.type = STATEMENT_SWITCH;
4713 statement->statement.source_position = token.source_position;
4716 statement->expression = parse_expression();
4718 statement->body = parse_statement();
4720 return (statement_t*) statement;
4723 static statement_t *parse_while(void)
4727 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4728 statement->statement.type = STATEMENT_WHILE;
4729 statement->statement.source_position = token.source_position;
4732 statement->condition = parse_expression();
4734 statement->body = parse_statement();
4736 return (statement_t*) statement;
4739 static statement_t *parse_do(void)
4743 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4744 statement->statement.type = STATEMENT_DO_WHILE;
4745 statement->statement.source_position = token.source_position;
4747 statement->body = parse_statement();
4750 statement->condition = parse_expression();
4754 return (statement_t*) statement;
4757 static statement_t *parse_for(void)
4761 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4762 statement->statement.type = STATEMENT_FOR;
4763 statement->statement.source_position = token.source_position;
4767 int top = environment_top();
4768 context_t *last_context = context;
4769 set_context(&statement->context);
4771 if(token.type != ';') {
4772 if(is_declaration_specifier(&token, false)) {
4773 parse_declaration(record_declaration);
4775 statement->initialisation = parse_expression();
4782 if(token.type != ';') {
4783 statement->condition = parse_expression();
4786 if(token.type != ')') {
4787 statement->step = parse_expression();
4790 statement->body = parse_statement();
4792 assert(context == &statement->context);
4793 set_context(last_context);
4794 environment_pop_to(top);
4796 return (statement_t*) statement;
4799 static statement_t *parse_goto(void)
4803 if(token.type != T_IDENTIFIER) {
4804 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4808 symbol_t *symbol = token.v.symbol;
4811 declaration_t *label = get_label(symbol);
4813 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4815 statement->statement.type = STATEMENT_GOTO;
4816 statement->statement.source_position = token.source_position;
4818 statement->label = label;
4822 return (statement_t*) statement;
4825 static statement_t *parse_continue(void)
4830 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4831 statement->type = STATEMENT_CONTINUE;
4832 statement->base.source_position = token.source_position;
4837 static statement_t *parse_break(void)
4842 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4843 statement->type = STATEMENT_BREAK;
4844 statement->base.source_position = token.source_position;
4849 static statement_t *parse_return(void)
4853 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4855 statement->statement.type = STATEMENT_RETURN;
4856 statement->statement.source_position = token.source_position;
4858 assert(is_type_function(current_function->type));
4859 function_type_t *function_type = ¤t_function->type->function;
4860 type_t *return_type = function_type->return_type;
4862 expression_t *return_value = NULL;
4863 if(token.type != ';') {
4864 return_value = parse_expression();
4868 if(return_type == NULL)
4869 return (statement_t*) statement;
4871 return_type = skip_typeref(return_type);
4873 if(return_value != NULL) {
4874 type_t *return_value_type = skip_typeref(return_value->base.datatype);
4876 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
4877 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
4878 parse_warning("'return' with a value, in function returning void");
4879 return_value = NULL;
4881 if(return_type != NULL) {
4882 semantic_assign(return_type, &return_value, "'return'");
4886 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4887 parse_warning("'return' without value, in function returning "
4891 statement->return_value = return_value;
4893 return (statement_t*) statement;
4896 static statement_t *parse_declaration_statement(void)
4898 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
4900 statement->base.source_position = token.source_position;
4902 declaration_t *before = last_declaration;
4903 parse_declaration(record_declaration);
4905 if(before == NULL) {
4906 statement->declaration.declarations_begin = context->declarations;
4908 statement->declaration.declarations_begin = before->next;
4910 statement->declaration.declarations_end = last_declaration;
4915 static statement_t *parse_expression_statement(void)
4917 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
4919 statement->base.source_position = token.source_position;
4920 statement->expression.expression = parse_expression();
4927 static statement_t *parse_statement(void)
4929 statement_t *statement = NULL;
4931 /* declaration or statement */
4932 switch(token.type) {
4934 statement = parse_asm_statement();
4938 statement = parse_case_statement();
4942 statement = parse_default_statement();
4946 statement = parse_compound_statement();
4950 statement = parse_if();
4954 statement = parse_switch();
4958 statement = parse_while();
4962 statement = parse_do();
4966 statement = parse_for();
4970 statement = parse_goto();
4974 statement = parse_continue();
4978 statement = parse_break();
4982 statement = parse_return();
4991 if(look_ahead(1)->type == ':') {
4992 statement = parse_label_statement();
4996 if(is_typedef_symbol(token.v.symbol)) {
4997 statement = parse_declaration_statement();
5001 statement = parse_expression_statement();
5004 case T___extension__:
5005 /* this can be a prefix to a declaration or an expression statement */
5006 /* we simply eat it now and parse the rest with tail recursion */
5009 } while(token.type == T___extension__);
5010 statement = parse_statement();
5014 statement = parse_declaration_statement();
5018 statement = parse_expression_statement();
5022 assert(statement == NULL
5023 || statement->base.source_position.input_name != NULL);
5028 static statement_t *parse_compound_statement(void)
5030 compound_statement_t *compound_statement
5031 = allocate_ast_zero(sizeof(compound_statement[0]));
5032 compound_statement->statement.type = STATEMENT_COMPOUND;
5033 compound_statement->statement.source_position = token.source_position;
5037 int top = environment_top();
5038 context_t *last_context = context;
5039 set_context(&compound_statement->context);
5041 statement_t *last_statement = NULL;
5043 while(token.type != '}' && token.type != T_EOF) {
5044 statement_t *statement = parse_statement();
5045 if(statement == NULL)
5048 if(last_statement != NULL) {
5049 last_statement->base.next = statement;
5051 compound_statement->statements = statement;
5054 while(statement->base.next != NULL)
5055 statement = statement->base.next;
5057 last_statement = statement;
5060 if(token.type != '}') {
5061 parser_print_error_prefix_pos(
5062 compound_statement->statement.source_position);
5063 fprintf(stderr, "end of file while looking for closing '}'\n");
5067 assert(context == &compound_statement->context);
5068 set_context(last_context);
5069 environment_pop_to(top);
5071 return (statement_t*) compound_statement;
5074 static void initialize_builtins(void)
5076 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5077 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5078 type_size_t = make_global_typedef("__SIZE_TYPE__",
5079 make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_NONE));
5080 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__",
5081 make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_NONE));
5084 static translation_unit_t *parse_translation_unit(void)
5086 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5088 assert(global_context == NULL);
5089 global_context = &unit->context;
5091 assert(context == NULL);
5092 set_context(&unit->context);
5094 initialize_builtins();
5096 while(token.type != T_EOF) {
5097 parse_external_declaration();
5100 assert(context == &unit->context);
5102 last_declaration = NULL;
5104 assert(global_context == &unit->context);
5105 global_context = NULL;
5110 translation_unit_t *parse(void)
5112 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5113 label_stack = NEW_ARR_F(stack_entry_t, 0);
5114 found_error = false;
5116 type_set_output(stderr);
5117 ast_set_output(stderr);
5119 lookahead_bufpos = 0;
5120 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5123 translation_unit_t *unit = parse_translation_unit();
5125 DEL_ARR_F(environment_stack);
5126 DEL_ARR_F(label_stack);
5134 void init_parser(void)
5136 init_expression_parsers();
5137 obstack_init(&temp_obst);
5139 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
5140 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE,
5141 TYPE_QUALIFIER_NONE);
5142 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE,
5143 TYPE_QUALIFIER_NONE);
5144 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_NONE);
5145 type_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_NONE);
5146 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
5147 type_void_ptr = make_pointer_type(type_void, TYPE_QUALIFIER_NONE);
5148 type_string = make_pointer_type(type_char, TYPE_QUALIFIER_NONE);
5150 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5151 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5154 void exit_parser(void)
5156 obstack_free(&temp_obst, NULL);